diff --git a/BigBaseV2/src/memory/all.hpp b/BigBaseV2/src/memory/all.hpp index 1b188278..53b4c685 100644 --- a/BigBaseV2/src/memory/all.hpp +++ b/BigBaseV2/src/memory/all.hpp @@ -5,3 +5,4 @@ #include "pattern.hpp" #include "batch.hpp" #include "range.hpp" +#include "byte_patch.hpp" diff --git a/BigBaseV2/src/memory/byte_patch.hpp b/BigBaseV2/src/memory/byte_patch.hpp new file mode 100644 index 00000000..717dfb6b --- /dev/null +++ b/BigBaseV2/src/memory/byte_patch.hpp @@ -0,0 +1,67 @@ +#pragma once + +namespace memory +{ + class byte_patch + { + public: + virtual ~byte_patch() + { + memcpy(m_address, m_original_bytes.data(), m_original_bytes.size()); + } + + /// + /// To guarantee proper restoration of bytes all shared_ptr instances will be invalidated that point to this object. + /// + void restore() const + { + if (const auto it = std::find(m_patches.begin(), m_patches.end(), this); it != m_patches.end()) + { + it->reset(); + m_patches.erase(it); + } + } + + template + static std::shared_ptr make(TAddr address, std::remove_pointer_t> value) + { + auto patch = std::shared_ptr(new byte_patch(address, value)); + m_patches.emplace_back(patch); + return patch; + } + + static void restore_all() + { + for (const auto& patch : m_patches) + { + patch->restore(); + } + } + + private: + template + 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); + + *address = value; + } + + protected: + static inline std::vector> m_patches; + + private: + void* m_address; + std::vector m_original_bytes; + + friend bool operator== (const std::shared_ptr a, const byte_patch* b); + }; + + bool operator== (const std::shared_ptr a, const byte_patch* b) + { + return a->m_address == b->m_address; + } +} diff --git a/BigBaseV2/src/pointers.cpp b/BigBaseV2/src/pointers.cpp index 6a4c9876..843664a1 100644 --- a/BigBaseV2/src/pointers.cpp +++ b/BigBaseV2/src/pointers.cpp @@ -131,7 +131,7 @@ namespace big // Send Event Acknowledge main_batch.add("SEA", "48 89 6C 24 ? 48 89 74 24 ? 57 48 83 EC 20 80 7A", [this](memory::handle ptr) { - m_send_event_ack = ptr.sub(5).as(); + m_send_event_ack = ptr.sub(5).as(); }); // Received Event Signatures END @@ -205,8 +205,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) { - m_spectator_check = ptr.add(0x13).as(); - *m_spectator_check = 0x9090; + memory::byte_patch::make(ptr.add(0x13).as(), 0x9090); }); // Replay Interface @@ -404,28 +403,28 @@ namespace big if (auto pat1 = mem_region.bruteforce_scan("3b 0a 0f 83 ? ? ? ? 48 ff c7")) { - *pat1.add(2).as() = 0xc9310272; - *pat1.add(6).as() = 0x9090; + memory::byte_patch::make(pat1.add(2).as(), 0xc9310272); + memory::byte_patch::make(pat1.add(6).as(), 0x9090); } if (auto pat2 = mem_region.bruteforce_scan("3b 0a 0f 83 ? ? ? ? 49 03 fa")) { - *pat2.add(2).as() = 0xc9310272; - *pat2.add(6).as() = 0x9090; + memory::byte_patch::make(pat2.add(2).as(), 0xc9310272); + memory::byte_patch::make(pat2.add(6).as(), 0x9090); } auto pat3 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 48 ff c7"); for (auto& handle : pat3) { - *handle.add(2).as() = 0xd2310272; - *handle.add(6).as() = 0x9090; + memory::byte_patch::make(handle.add(2).as(), 0xd2310272); + memory::byte_patch::make(handle.add(6).as(), 0x9090); } auto pat4 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 49 03 fa"); for (auto& handle : pat4) { - *handle.add(2).as() = 0xd2310272; - *handle.add(6).as() = 0x9090; + memory::byte_patch::make(handle.add(2).as(), 0xd2310272); + memory::byte_patch::make(handle.add(6).as(), 0x9090); } m_hwnd = FindWindowW(L"grcWindow", nullptr); @@ -438,7 +437,7 @@ namespace big pointers::~pointers() { - *m_spectator_check = 0x6A75; + memory::byte_patch::restore_all(); g_pointers = nullptr; } diff --git a/BigBaseV2/src/pointers.hpp b/BigBaseV2/src/pointers.hpp index 79ab99ae..855a79a7 100644 --- a/BigBaseV2/src/pointers.hpp +++ b/BigBaseV2/src/pointers.hpp @@ -49,7 +49,6 @@ namespace big PVOID m_world_model_spawn_bypass; PVOID m_native_return; PVOID m_network_group_override; - PUSHORT m_spectator_check; PVOID m_get_label_text; FriendRegistry* m_friend_registry{};