fix: Properly restore the byte patches when unloading (#486)

Co-authored-by: Yimura <24669514+Yimura@users.noreply.github.com>
This commit is contained in:
Quentin E. / iDeath 2022-10-21 13:25:37 +02:00 committed by GitHub
parent 91ae0512fa
commit 005ce81c4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 13 deletions

View File

@ -5,3 +5,4 @@
#include "pattern.hpp"
#include "batch.hpp"
#include "range.hpp"
#include "byte_patch.hpp"

View File

@ -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());
}
/// <summary>
/// To guarantee proper restoration of bytes all shared_ptr instances will be invalidated that point to this object.
/// </summary>
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 <typename TAddr>
static std::shared_ptr<byte_patch> make(TAddr address, std::remove_pointer_t<std::remove_reference_t<TAddr>> value)
{
auto patch = std::shared_ptr<byte_patch>(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 <typename TAddr>
byte_patch(TAddr address, std::remove_pointer_t<std::remove_reference_t<TAddr>> value)
: m_address(address)
{
constexpr auto size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);
m_original_bytes.resize(size);
memcpy(m_original_bytes.data(), m_address, size);
*address = value;
}
protected:
static inline std::vector<std::shared_ptr<byte_patch>> m_patches;
private:
void* m_address;
std::vector<uint8_t> m_original_bytes;
friend bool operator== (const std::shared_ptr<byte_patch> a, const byte_patch* b);
};
bool operator== (const std::shared_ptr<byte_patch> a, const byte_patch* b)
{
return a->m_address == b->m_address;
}
}

View File

@ -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<decltype(m_send_event_ack)>();
m_send_event_ack = ptr.sub(5).as<decltype(m_send_event_ack)>();
});
// 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<PUSHORT>();
*m_spectator_check = 0x9090;
memory::byte_patch::make(ptr.add(0x13).as<std::uint16_t*>(), 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<uint32_t*>() = 0xc9310272;
*pat1.add(6).as<uint16_t*>() = 0x9090;
memory::byte_patch::make(pat1.add(2).as<uint32_t*>(), 0xc9310272);
memory::byte_patch::make(pat1.add(6).as<uint16_t*>(), 0x9090);
}
if (auto pat2 = mem_region.bruteforce_scan("3b 0a 0f 83 ? ? ? ? 49 03 fa"))
{
*pat2.add(2).as<uint32_t*>() = 0xc9310272;
*pat2.add(6).as<uint16_t*>() = 0x9090;
memory::byte_patch::make(pat2.add(2).as<uint32_t*>(), 0xc9310272);
memory::byte_patch::make(pat2.add(6).as<uint16_t*>(), 0x9090);
}
auto pat3 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 48 ff c7");
for (auto& handle : pat3)
{
*handle.add(2).as<uint32_t*>() = 0xd2310272;
*handle.add(6).as<uint16_t*>() = 0x9090;
memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272);
memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 0x9090);
}
auto pat4 = mem_region.scan_all("3b 11 0f 83 ? ? ? ? 49 03 fa");
for (auto& handle : pat4)
{
*handle.add(2).as<uint32_t*>() = 0xd2310272;
*handle.add(6).as<uint16_t*>() = 0x9090;
memory::byte_patch::make(handle.add(2).as<uint32_t*>(), 0xd2310272);
memory::byte_patch::make(handle.add(6).as<uint16_t*>(), 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;
}

View File

@ -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{};