feat(BytePatch): add restore & apply methods (#524)

This commit is contained in:
Yimura 2022-10-26 21:20:26 +02:00 committed by GitHub
parent 0188477573
commit 1549f157a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 15 deletions

View File

@ -7,10 +7,20 @@ namespace memory
public: public:
virtual ~byte_patch() 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 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()) 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<std::remove_reference_t<TAddr>> value) byte_patch(TAddr address, std::remove_pointer_t<std::remove_reference_t<TAddr>> value)
: m_address(address) : m_address(address)
{ {
constexpr auto size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>); m_size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);
m_original_bytes.resize(size); m_original_bytes = std::make_unique<uint8_t[]>(m_size);
memcpy(m_original_bytes.data(), m_address, size); m_value = std::make_unique<uint8_t[]>(m_size);
*address = value; memcpy(m_original_bytes.get(), m_address, m_size);
memcpy(m_value.get(), &value, m_size);
} }
protected: protected:
@ -47,7 +58,9 @@ namespace memory
private: private:
void* m_address; void* m_address;
std::vector<uint8_t> m_original_bytes; std::unique_ptr<uint8_t[]> m_value;
std::unique_ptr<uint8_t[]> m_original_bytes;
std::size_t m_size;
friend bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b); friend bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b);
}; };

View File

@ -203,7 +203,7 @@ namespace big
// Request Control of Entity PATCH // 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) 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<std::uint16_t*>(), 0x9090); memory::byte_patch::make(ptr.add(0x13).as<std::uint16_t*>(), 0x9090)->apply();
}); });
// Replay Interface // Replay Interface
@ -418,28 +418,28 @@ namespace big
*/ */
if (auto pat1 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 48 ff c7")) if (auto pat1 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 48 ff c7"))
{ {
memory::byte_patch::make(pat1.add(2).as<uint32_t*>(), 0xc9310272); memory::byte_patch::make(pat1.add(2).as<uint32_t*>(), 0xc9310272)->apply();
memory::byte_patch::make(pat1.add(6).as<uint16_t*>(), 0x9090); memory::byte_patch::make(pat1.add(6).as<uint16_t*>(), 0x9090)->apply();
} }
if (auto pat2 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 49 03 fa")) if (auto pat2 = mem_region.scan("3b 0a 0f 83 ? ? ? ? 49 03 fa"))
{ {
memory::byte_patch::make(pat2.add(2).as<uint32_t*>(), 0xc9310272); memory::byte_patch::make(pat2.add(2).as<uint32_t*>(), 0xc9310272)->apply();
memory::byte_patch::make(pat2.add(6).as<uint16_t*>(), 0x9090); memory::byte_patch::make(pat2.add(6).as<uint16_t*>(), 0x9090)->apply();
} }
auto pat3 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 48 ff c7"); auto pat3 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 48 ff c7");
for (auto& handle : pat3) for (auto& handle : pat3)
{ {
memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272); memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272)->apply();
memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 0x9090); memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 0x9090)->apply();
} }
auto pat4 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 49 03 fa"); auto pat4 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 49 03 fa");
for (auto& handle : pat4) for (auto& handle : pat4)
{ {
memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272); memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272)->apply();
memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 0x9090); memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 0x9090)->apply();
} }
m_hwnd = FindWindowW(L"grcWindow", nullptr); m_hwnd = FindWindowW(L"grcWindow", nullptr);