diff --git a/obse/obse_common/SafeWrite.cpp b/obse/obse_common/SafeWrite.cpp index 63ad3cc..7ceec6b 100644 --- a/obse/obse_common/SafeWrite.cpp +++ b/obse/obse_common/SafeWrite.cpp @@ -75,4 +75,28 @@ void WriteNop(UInt32 nopAddr, UInt8 numOfByte) { for (UInt8 i = 0; i < numOfByte; i++) { SafeWrite8(nopAddr + i, 0x90); } +} + +void PatchCallsInRange(UInt32 start, UInt32 end, UInt32 CallToPatch, UInt32 HookCall) { + UInt32 oldProtect; + + VirtualProtect((void*)start , end - start, PAGE_EXECUTE_READWRITE, &oldProtect); + for (UInt32 current = start; current < end; current++) { + if (*(UInt8*)current == 0xE8) { + UInt32 callTarget = *(UInt32*)(current + 1) + current + 1 + 4; + // _MESSAGE("%08X", callTarget); + if (callTarget == CallToPatch) { + // _MESSAGE("Call found"); + SafeWrite32(current + 1, HookCall - current - 1 - 4); + } + } + } + VirtualProtect((void*)start, end - start, oldProtect, &oldProtect); + + /* + 006020FB 054 db 61h ; a +.text:006020FC 054 db 59h ; Y +.text:006020FD 054 db 0EAh ; ê +.text:006020FE 054 db 0FFh ; ÿ + */ } \ No newline at end of file diff --git a/obse/obse_common/SafeWrite.h b/obse/obse_common/SafeWrite.h index f007dcf..1c0b857 100644 --- a/obse/obse_common/SafeWrite.h +++ b/obse/obse_common/SafeWrite.h @@ -15,4 +15,6 @@ void WriteRelJnz(UInt32 jumpSrc, UInt32 jumpTgt); void WriteRelJle(UInt32 jumpSrc, UInt32 jumpTgt); //Write arbitrary nop -void WriteNop(UInt32 nopAddr, UInt8 numOfByte); \ No newline at end of file +void WriteNop(UInt32 nopAddr, UInt8 numOfByte); +//PAtch calls to address in a certain range +void PatchCallsInRange(UInt32 start, UInt32 end, UInt32 CallToPatch, UInt32 HookCall); \ No newline at end of file