From 1549f157a65455c6161d9b3f2fc2266142b9aefc Mon Sep 17 00:00:00 2001 From: Yimura <24669514+Yimura@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:20:26 +0200 Subject: [PATCH] feat(BytePatch): add restore & apply methods (#524) --- BigBaseV2/src/memory/byte_patch.hpp | 25 +++++++++++++++++++------ BigBaseV2/src/pointers.cpp | 18 +++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/BigBaseV2/src/memory/byte_patch.hpp b/BigBaseV2/src/memory/byte_patch.hpp index f91ed9d2..6865cbd2 100644 --- a/BigBaseV2/src/memory/byte_patch.hpp +++ b/BigBaseV2/src/memory/byte_patch.hpp @@ -7,10 +7,20 @@ namespace memory public: virtual ~byte_patch() { - memcpy(m_address, m_original_bytes.data(), m_original_bytes.size()); + restore(); + } + + void apply() const + { + memcpy(m_address, m_value.get(), m_size); } void restore() const + { + memcpy(m_address, m_original_bytes.get(), m_size); + } + + void remove() const { if (const auto it = std::find(m_patches.begin(), m_patches.end(), this); it != m_patches.end()) { @@ -35,11 +45,12 @@ namespace memory byte_patch(TAddr address, std::remove_pointer_t> value) : m_address(address) { - constexpr auto size = sizeof(std::remove_pointer_t>); - m_original_bytes.resize(size); - memcpy(m_original_bytes.data(), m_address, size); + m_size = sizeof(std::remove_pointer_t>); + m_original_bytes = std::make_unique(m_size); + m_value = std::make_unique(m_size); - *address = value; + memcpy(m_original_bytes.get(), m_address, m_size); + memcpy(m_value.get(), &value, m_size); } protected: @@ -47,7 +58,9 @@ namespace memory private: void* m_address; - std::vector m_original_bytes; + std::unique_ptr m_value; + std::unique_ptr m_original_bytes; + std::size_t m_size; friend bool operator== (const std::unique_ptr& a, const byte_patch* b); }; diff --git a/BigBaseV2/src/pointers.cpp b/BigBaseV2/src/pointers.cpp index f6569c50..6c3301ef 100644 --- a/BigBaseV2/src/pointers.cpp +++ b/BigBaseV2/src/pointers.cpp @@ -203,7 +203,7 @@ namespace big // Request Control of Entity PATCH main_batch.add("RCOE-Patch", "48 89 5C 24 ? 57 48 83 EC 20 8B D9 E8 ? ? ? ? ? ? ? ? 8B CB", [this](memory::handle ptr) { - memory::byte_patch::make(ptr.add(0x13).as(), 0x9090); + memory::byte_patch::make(ptr.add(0x13).as(), 0x9090)->apply(); }); // Replay Interface @@ -418,28 +418,28 @@ namespace big */ if (auto pat1 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 48 ff c7")) { - memory::byte_patch::make(pat1.add(2).as(), 0xc9310272); - memory::byte_patch::make(pat1.add(6).as(), 0x9090); + memory::byte_patch::make(pat1.add(2).as(), 0xc9310272)->apply(); + memory::byte_patch::make(pat1.add(6).as(), 0x9090)->apply(); } if (auto pat2 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 49 03 fa")) { - memory::byte_patch::make(pat2.add(2).as(), 0xc9310272); - memory::byte_patch::make(pat2.add(6).as(), 0x9090); + memory::byte_patch::make(pat2.add(2).as(), 0xc9310272)->apply(); + memory::byte_patch::make(pat2.add(6).as(), 0x9090)->apply(); } auto pat3 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 48 ff c7"); for (auto& handle : pat3) { - memory::byte_patch::make(handle.add(2).as(), 0xd2310272); - memory::byte_patch::make(handle.add(6).as(), 0x9090); + memory::byte_patch::make(handle.add(2).as(), 0xd2310272)->apply(); + memory::byte_patch::make(handle.add(6).as(), 0x9090)->apply(); } auto pat4 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 49 03 fa"); for (auto& handle : pat4) { - memory::byte_patch::make(handle.add(2).as(), 0xd2310272); - memory::byte_patch::make(handle.add(6).as(), 0x9090); + memory::byte_patch::make(handle.add(2).as(), 0xd2310272)->apply(); + memory::byte_patch::make(handle.add(6).as(), 0x9090)->apply(); } m_hwnd = FindWindowW(L"grcWindow", nullptr);