diff --git a/src/lua/bindings/scr_patch.cpp b/src/lua/bindings/scr_patch.cpp index 1e27649b..1ffbeb77 100644 --- a/src/lua/bindings/scr_patch.cpp +++ b/src/lua/bindings/scr_patch.cpp @@ -25,6 +25,12 @@ namespace lua::scr_patch module->m_registered_script_patches.push_back(std::make_unique(*this)); } + scr_patch::~scr_patch() + { + disable(); + big::g_script_patcher_service->remove_patch(m_patch_name); + } + void scr_patch::enable() { if (!m_enable) diff --git a/src/lua/bindings/scr_patch.hpp b/src/lua/bindings/scr_patch.hpp index 2e509366..8d771a6d 100644 --- a/src/lua/bindings/scr_patch.hpp +++ b/src/lua/bindings/scr_patch.hpp @@ -2,7 +2,7 @@ namespace lua::scr_patch { - struct scr_patch + class scr_patch { rage::joaat_t m_script; std::string m_patch_name; @@ -21,6 +21,7 @@ namespace lua::scr_patch // Param: patch_: table: The bytes to be written into the script's bytecode. // Adds a patch for the specified script. explicit scr_patch(const std::string& script_name, const std::string& patch_name, const std::string& pattern, const int offset, sol::table patch_, sol::this_state state); + ~scr_patch(); // Lua API: Function // Class: scr_patch diff --git a/src/lua/lua_module.cpp b/src/lua/lua_module.cpp index 2e0a7c43..562c7825 100644 --- a/src/lua/lua_module.cpp +++ b/src/lua/lua_module.cpp @@ -126,6 +126,7 @@ namespace big { std::lock_guard guard(m_registered_scripts_mutex); m_registered_scripts.clear(); + m_registered_script_patches.clear(); } for (const auto owned_tab : m_owned_tabs) diff --git a/src/services/script_patcher/script_patch.hpp b/src/services/script_patcher/script_patch.hpp index e577be74..8ea866c5 100644 --- a/src/services/script_patcher/script_patch.hpp +++ b/src/services/script_patcher/script_patch.hpp @@ -1,34 +1,39 @@ -#pragma once -#include "memory/pattern.hpp" - -namespace big -{ - struct script_data; - - class script_patch - { - rage::joaat_t m_script; - const memory::pattern m_pattern; - std::string m_name; - int32_t m_offset; - std::vector m_patch; - std::vector m_original; - bool* m_bool; - int32_t m_ip; - - static uint8_t* get_code_address(script_data* data, uint32_t index); - static const std::optional get_code_location_by_pattern(script_data* data, const memory::pattern& pattern); - - public: - void enable(script_data* data); - void disable(script_data* data); - - inline rage::joaat_t get_script() - { - return m_script; - } - - script_patch(rage::joaat_t script, std::string name, const memory::pattern pattern, int32_t offset, std::vector patch, bool* enable_bool); - void update(script_data* data); - }; +#pragma once +#include "memory/pattern.hpp" + +namespace big +{ + struct script_data; + + class script_patch + { + rage::joaat_t m_script; + const memory::pattern m_pattern; + std::string m_name; + int32_t m_offset; + std::vector m_patch; + std::vector m_original; + bool* m_bool; + int32_t m_ip; + + static uint8_t* get_code_address(script_data* data, uint32_t index); + static const std::optional get_code_location_by_pattern(script_data* data, const memory::pattern& pattern); + + public: + void enable(script_data* data); + void disable(script_data* data); + + inline rage::joaat_t get_script() + { + return m_script; + } + + inline std::string get_name() + { + return m_name; + } + + script_patch(rage::joaat_t script, std::string name, const memory::pattern pattern, int32_t offset, std::vector patch, bool* enable_bool); + void update(script_data* data); + }; } \ No newline at end of file diff --git a/src/services/script_patcher/script_patcher_service.cpp b/src/services/script_patcher/script_patcher_service.cpp index 37cddb1f..8466a9c6 100644 --- a/src/services/script_patcher/script_patcher_service.cpp +++ b/src/services/script_patcher/script_patcher_service.cpp @@ -1,101 +1,116 @@ -#include "script_patcher_service.hpp" - -#include "script_data.hpp" -#include "script_patch.hpp" - -#include