diff options
author | Filip Gawin <filip.gawin@zoho.com> | 2020-03-27 21:50:52 +0100 |
---|---|---|
committer | Filip Gawin <filip.gawin@zoho.com> | 2020-03-28 19:46:09 +0100 |
commit | b235c358341b288f34cee5936a376f71d0cfbf9a (patch) | |
tree | 8e4e605bcdecafe171defbc2099b8ce06c519686 /src/core/patcher.cpp | |
parent | Remove assembly from patcher.h (diff) | |
download | re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar.gz re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar.bz2 re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar.lz re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar.xz re3-b235c358341b288f34cee5936a376f71d0cfbf9a.tar.zst re3-b235c358341b288f34cee5936a376f71d0cfbf9a.zip |
Diffstat (limited to 'src/core/patcher.cpp')
-rw-r--r-- | src/core/patcher.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/core/patcher.cpp b/src/core/patcher.cpp index 5fdbdf8b..19ca5f07 100644 --- a/src/core/patcher.cpp +++ b/src/core/patcher.cpp @@ -1,6 +1,11 @@ #include "common.h" #include "patcher.h" +#include <algorithm> +#include <vector> + +#include <Windows.h> + StaticPatcher *StaticPatcher::ms_head; StaticPatcher::StaticPatcher(Patcher func) @@ -20,3 +25,55 @@ StaticPatcher::Apply() } ms_head = nil; } + +std::vector<uint32> usedAddresses; + +static DWORD protect[2]; +static uint32 protect_address; +static uint32 protect_size; + +void +Protect_internal(uint32 address, uint32 size) +{ + protect_address = address; + protect_size = size; + VirtualProtect((void*)address, size, PAGE_EXECUTE_READWRITE, &protect[0]); +} + +void +Unprotect_internal(void) +{ + VirtualProtect((void*)protect_address, protect_size, protect[0], &protect[1]); +} + +void +InjectHook_internal(uint32 address, uint32 hook, int type) +{ + if(std::any_of(usedAddresses.begin(), usedAddresses.end(), + [address](uint32 value) { return value == address; })) { + debug("Used address %#06x twice when injecting hook\n", address); + } + + usedAddresses.push_back(address); + + + switch(type){ + case PATCH_JUMP: + VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); + *(uint8*)address = 0xE9; + break; + case PATCH_CALL: + VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); + *(uint8*)address = 0xE8; + break; + default: + VirtualProtect((void*)(address + 1), 4, PAGE_EXECUTE_READWRITE, &protect[0]); + break; + } + + *(ptrdiff_t*)(address + 1) = hook - address - 5; + if(type == PATCH_NOTHING) + VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); + else + VirtualProtect((void*)address, 5, protect[0], &protect[1]); +}
\ No newline at end of file |