From 706e6f7fc086962136ac657f1577439b117b8612 Mon Sep 17 00:00:00 2001 From: gir489 <100792176+gir489returns@users.noreply.github.com> Date: Mon, 2 Sep 2024 01:52:34 -0400 Subject: [PATCH] Fixed a race condition with scr_patch's destructor. (#3639) * Fixed a race condition caused by disabling and enabling a Lua script with scr_patch more than once. * Fixed patch not being enabled if data is already created --------- Co-authored-by: Arthur <121949966+ShinyWasabi@users.noreply.github.com> --- src/lua/bindings/scr_patch.cpp | 5 ++--- src/services/script_patcher/script_patch.cpp | 3 +++ src/services/script_patcher/script_patcher_service.cpp | 10 ++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lua/bindings/scr_patch.cpp b/src/lua/bindings/scr_patch.cpp index 1ffbeb77..d12e2a42 100644 --- a/src/lua/bindings/scr_patch.cpp +++ b/src/lua/bindings/scr_patch.cpp @@ -27,7 +27,6 @@ namespace lua::scr_patch scr_patch::~scr_patch() { - disable(); big::g_script_patcher_service->remove_patch(m_patch_name); } @@ -36,7 +35,7 @@ namespace lua::scr_patch if (!m_enable) { m_enable = true; - big::g_script_patcher_service->update_all_patches_for_script(m_script); + big::g_script_patcher_service->update(); } } @@ -45,7 +44,7 @@ namespace lua::scr_patch if (m_enable) { m_enable = false; - big::g_script_patcher_service->update_all_patches_for_script(m_script); + big::g_script_patcher_service->update(); } } diff --git a/src/services/script_patcher/script_patch.cpp b/src/services/script_patcher/script_patch.cpp index 6d494f22..2124bfcd 100644 --- a/src/services/script_patcher/script_patch.cpp +++ b/src/services/script_patcher/script_patch.cpp @@ -54,7 +54,10 @@ namespace big { auto result = get_code_location_by_pattern(data, m_pattern); if (!result.has_value()) + { LOG(FATAL) << "Failed to find pattern: " << m_name; + return; + } m_ip = result.value() + m_offset; diff --git a/src/services/script_patcher/script_patcher_service.cpp b/src/services/script_patcher/script_patcher_service.cpp index 8466a9c6..7627ec64 100644 --- a/src/services/script_patcher/script_patcher_service.cpp +++ b/src/services/script_patcher/script_patcher_service.cpp @@ -87,11 +87,13 @@ namespace big void script_patcher_service::on_script_load(rage::scrProgram* program) { - if (get_data_for_script(program->m_name_hash) == nullptr && does_script_have_patches(program->m_name_hash)) - { + if (!does_script_have_patches(program->m_name_hash)) + return; + + if (get_data_for_script(program->m_name_hash) == nullptr) create_data_for_script(program); - update_all_patches_for_script(program->m_name_hash); - } + + update_all_patches_for_script(program->m_name_hash); } uint8_t** script_patcher_service::get_script_bytecode(rage::joaat_t script)