feat(Hooks): Split freemode script recovery into 2 different files

This commit is contained in:
Yimura 2022-01-21 23:13:10 +01:00
parent eba692ff37
commit ee38e194b7
No known key found for this signature in database
GPG Key ID: 3D8FF4397E768682
4 changed files with 59 additions and 53 deletions

View File

@ -46,6 +46,8 @@ struct globals {
bool vehicle_kick = true;
};
bool freemode_terminated = false;
script_events script_events{};
};

View File

@ -1,53 +0,0 @@
#include "hooking.hpp"
#include "gta/script_thread.hpp"
namespace big
{
using namespace rage;
static char struct_backup[sizeof(GtaThread)];
static char stack_buffer[0xFFFF];
static bool freemode_terminated = false;
eThreadState hooks::gta_thread_tick(GtaThread* thread, unsigned int a2)
{
eThreadState state = thread->m_context.m_state;
if (thread->m_script_hash == RAGE_JOAAT("freemode") && state == eThreadState::running && !freemode_terminated)
{
memcpy(struct_backup, (void*)thread, sizeof(GtaThread));
if (thread->m_stack)
memcpy(stack_buffer, thread->m_stack, thread->m_context.m_stack_size);
}
eThreadState result = g_hooking->m_gta_thread_tick_hook.get_original<decltype(&gta_thread_tick)>()(thread, a2);
if (thread->m_script_hash == RAGE_JOAAT("freemode"))
{
if (result == eThreadState::killed && state == eThreadState::running && !freemode_terminated)
{
LOG(INFO) << "Freemode script crashed, attempting recovery...";
result = eThreadState::running;
memcpy(thread, struct_backup, sizeof(GtaThread));
if (thread->m_stack)
memcpy(thread->m_stack, stack_buffer, thread->m_context.m_stack_size);
}
if (freemode_terminated) freemode_terminated = !(result == eThreadState::running);
}
return result;
}
eThreadState hooks::gta_thread_kill(GtaThread* thread)
{
eThreadState result = g_hooking->m_gta_thread_kill_hook.get_original<decltype(&gta_thread_kill)>()(thread);
if (thread->m_script_hash == RAGE_JOAAT("freemode"))
freemode_terminated = !(result == eThreadState::running);
return result;
}
}

View File

@ -0,0 +1,17 @@
#include "hooking.hpp"
#include "native_hooks/native_hooks.hpp"
namespace big
{
rage::eThreadState hooks::gta_thread_kill(GtaThread* thread)
{
rage::eThreadState result = g_hooking->m_gta_thread_kill_hook.get_original<decltype(&gta_thread_kill)>()(thread);
g_native_hooks->do_cleanup_for_thread(thread);
if (thread->m_script_hash == RAGE_JOAAT("freemode"))
g.protections.freemode_terminated = !(result == rage::eThreadState::running);
return result;
}
}

View File

@ -0,0 +1,40 @@
#include "hooking.hpp"
#include "gta/script_thread.hpp"
namespace big
{
static char struct_backup[sizeof(GtaThread)];
static char stack_buffer[0xFFFF];
rage::eThreadState hooks::gta_thread_tick(GtaThread* thread, unsigned int a2)
{
rage::eThreadState state = thread->m_context.m_state;
if (thread->m_script_hash == RAGE_JOAAT("freemode") && state == rage::eThreadState::running && !g.protections.freemode_terminated)
{
memcpy(struct_backup, (void*)thread, sizeof(GtaThread));
if (thread->m_stack)
memcpy(stack_buffer, thread->m_stack, thread->m_context.m_stack_size);
}
rage::eThreadState result = g_hooking->m_gta_thread_tick_hook.get_original<decltype(&gta_thread_tick)>()(thread, a2);
if (thread->m_script_hash == RAGE_JOAAT("freemode"))
{
if (result == rage::eThreadState::killed && state == rage::eThreadState::running && !g.protections.freemode_terminated)
{
LOG(INFO) << "Freemode script crashed, attempting recovery...";
result = rage::eThreadState::running;
memcpy(thread, struct_backup, sizeof(GtaThread));
if (thread->m_stack)
memcpy(thread->m_stack, stack_buffer, thread->m_context.m_stack_size);
}
if (g.protections.freemode_terminated) g.protections.freemode_terminated = !(result == rage::eThreadState::running);
}
return result;
}
}