1
0
Fork 0
mirror of https://git.rip/DMCA_FUCKER/re3.git synced 2024-12-23 17:40:00 +00:00

Merge pull request #360 from ShFil119/impr/assembly

Cleanup patching system
This commit is contained in:
aap 2020-03-28 20:01:18 +01:00 committed by GitHub
commit 8623bc9752
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 63 deletions

View file

@ -1,6 +1,11 @@
#include "common.h" #include "common.h"
#include "patcher.h" #include "patcher.h"
#include <algorithm>
#include <vector>
#include <Windows.h>
StaticPatcher *StaticPatcher::ms_head; StaticPatcher *StaticPatcher::ms_head;
StaticPatcher::StaticPatcher(Patcher func) StaticPatcher::StaticPatcher(Patcher func)
@ -20,3 +25,55 @@ StaticPatcher::Apply()
} }
ms_head = nil; 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]);
}

View file

@ -117,16 +117,10 @@ Nop(AT address, unsigned int nCount)
Unprotect_internal(); Unprotect_internal();
} }
template<typename AT, typename HT> inline void template <typename T> inline void
InjectHook(AT address, HT hook, unsigned int nType=PATCH_NOTHING) InjectHook(uintptr_t address, T hook, unsigned int nType = PATCH_NOTHING)
{ {
uint32 uiHook; InjectHook_internal(address, reinterpret_cast<uintptr_t>((void *&)hook), nType);
_asm
{
mov eax, hook
mov uiHook, eax
}
InjectHook_internal((uint32)address, uiHook, nType);
} }
inline void ExtractCall(void *dst, uint32_t a) inline void ExtractCall(void *dst, uint32_t a)

View file

@ -22,62 +22,8 @@
#include "Console.h" #include "Console.h"
#include "Debug.h" #include "Debug.h"
#include <algorithm>
#include <vector>
#include <list> #include <list>
std::vector<int32> 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) = 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]);
}
void **rwengine = *(void***)0x5A10E1; void **rwengine = *(void***)0x5A10E1;
DebugMenuAPI gDebugMenuAPI; DebugMenuAPI gDebugMenuAPI;