From 184a80cc3b7ecd054f09ec5519fded5fb4efa162 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 27 Mar 2020 21:20:28 +0100 Subject: Remove assembly from patcher.h --- src/core/patcher.h | 12 +++--------- src/core/re3.cpp | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/patcher.h b/src/core/patcher.h index 3dfbb05c..2722b6fd 100644 --- a/src/core/patcher.h +++ b/src/core/patcher.h @@ -117,16 +117,10 @@ Nop(AT address, unsigned int nCount) Unprotect_internal(); } -template inline void -InjectHook(AT address, HT hook, unsigned int nType=PATCH_NOTHING) +template inline void +InjectHook(uintptr_t address, T hook, unsigned int nType = PATCH_NOTHING) { - uint32 uiHook; - _asm - { - mov eax, hook - mov uiHook, eax - } - InjectHook_internal((uint32)address, uiHook, nType); + InjectHook_internal(address, reinterpret_cast((void *&)hook), nType); } inline void ExtractCall(void *dst, uint32_t a) diff --git a/src/core/re3.cpp b/src/core/re3.cpp index 137a890c..ffb2a7a2 100644 --- a/src/core/re3.cpp +++ b/src/core/re3.cpp @@ -71,7 +71,7 @@ InjectHook_internal(uint32 address, uint32 hook, int type) break; } - *(ptrdiff_t*)(address + 1) = hook - address - 5; + *(ptrdiff_t*)(address + 1) = (uintptr_t)hook - (uintptr_t)address - 5; if(type == PATCH_NOTHING) VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); else -- cgit v1.2.3 From b235c358341b288f34cee5936a376f71d0cfbf9a Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 27 Mar 2020 21:50:52 +0100 Subject: Cleanup patching system --- src/core/patcher.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/re3.cpp | 54 ------------------------------------------------- 2 files changed, 57 insertions(+), 54 deletions(-) (limited to 'src/core') 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 +#include + +#include + StaticPatcher *StaticPatcher::ms_head; StaticPatcher::StaticPatcher(Patcher func) @@ -20,3 +25,55 @@ StaticPatcher::Apply() } ms_head = nil; } + +std::vector 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 diff --git a/src/core/re3.cpp b/src/core/re3.cpp index ffb2a7a2..a65e6d76 100644 --- a/src/core/re3.cpp +++ b/src/core/re3.cpp @@ -22,62 +22,8 @@ #include "Console.h" #include "Debug.h" -#include -#include #include -std::vector 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 (int32)value == address; })) { - debug("Used address %#06x twice when injecting hook\n", address); - } - - usedAddresses.push_back((int32)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*)((uint32)address + 1), 4, PAGE_EXECUTE_READWRITE, &protect[0]); - break; - } - - *(ptrdiff_t*)(address + 1) = (uintptr_t)hook - (uintptr_t)address - 5; - if(type == PATCH_NOTHING) - VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); - else - VirtualProtect((void*)address, 5, protect[0], &protect[1]); -} - void **rwengine = *(void***)0x5A10E1; DebugMenuAPI gDebugMenuAPI; -- cgit v1.2.3