From a65171db21f369eb02e73313252c97ae151a11d5 Mon Sep 17 00:00:00 2001 From: Yimura Date: Fri, 21 Jan 2022 23:13:10 +0100 Subject: [PATCH] feat(Hooks): Split freemode script recovery into 2 different files --- BigBaseV2/src/core/globals.hpp | 2 + .../src/hooks/freemode_script_recovery.cpp | 53 ------------------- BigBaseV2/src/hooks/gta_thread_kill.cpp | 17 ++++++ BigBaseV2/src/hooks/gta_thread_tick.cpp | 40 ++++++++++++++ 4 files changed, 59 insertions(+), 53 deletions(-) delete mode 100644 BigBaseV2/src/hooks/freemode_script_recovery.cpp create mode 100644 BigBaseV2/src/hooks/gta_thread_kill.cpp create mode 100644 BigBaseV2/src/hooks/gta_thread_tick.cpp diff --git a/BigBaseV2/src/core/globals.hpp b/BigBaseV2/src/core/globals.hpp index 1a3d0ede..bff8050b 100644 --- a/BigBaseV2/src/core/globals.hpp +++ b/BigBaseV2/src/core/globals.hpp @@ -46,6 +46,8 @@ struct globals { bool vehicle_kick = true; }; + bool freemode_terminated = false; + script_events script_events{}; }; diff --git a/BigBaseV2/src/hooks/freemode_script_recovery.cpp b/BigBaseV2/src/hooks/freemode_script_recovery.cpp deleted file mode 100644 index 1664a4de..00000000 --- a/BigBaseV2/src/hooks/freemode_script_recovery.cpp +++ /dev/null @@ -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()(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()(thread); - - if (thread->m_script_hash == RAGE_JOAAT("freemode")) - freemode_terminated = !(result == eThreadState::running); - - return result; - } -} \ No newline at end of file diff --git a/BigBaseV2/src/hooks/gta_thread_kill.cpp b/BigBaseV2/src/hooks/gta_thread_kill.cpp new file mode 100644 index 00000000..fab4b708 --- /dev/null +++ b/BigBaseV2/src/hooks/gta_thread_kill.cpp @@ -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()(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; + } +} \ No newline at end of file diff --git a/BigBaseV2/src/hooks/gta_thread_tick.cpp b/BigBaseV2/src/hooks/gta_thread_tick.cpp new file mode 100644 index 00000000..c2ec9c64 --- /dev/null +++ b/BigBaseV2/src/hooks/gta_thread_tick.cpp @@ -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()(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; + } +} \ No newline at end of file