From da5b5b1e12ece511cde49b0c491bf685c40b724b Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 24 Aug 2019 14:31:46 +0200 Subject: [PATCH] Set up facilities for delayed patching in III/VC --- SilentPatch/Common.cpp | 54 +++++++++++++++++++++++++++++++ SilentPatch/Common.h | 1 + SilentPatchIII/SilentPatchIII.cpp | 3 -- SilentPatchVC/SilentPatchVC.cpp | 3 -- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/SilentPatch/Common.cpp b/SilentPatch/Common.cpp index 118af34..af76978 100644 --- a/SilentPatch/Common.cpp +++ b/SilentPatch/Common.cpp @@ -19,6 +19,42 @@ namespace HandlingNameLoadFix } }; +// ============= Delayed patches ============= +namespace DelayedPatches +{ + static bool delayedPatchesDone = false; + void (*Func)(); + + static BOOL (*RsEventHandler)(int, void*); + static void (WINAPI **OldSetPreference)(int a, int b); + void WINAPI Inject_MSS(int a, int b) + { + (*OldSetPreference)(a, b); + if ( !std::exchange(delayedPatchesDone, true) ) + { + if ( Func != nullptr ) Func(); + // So we don't have to revert patches + HMODULE hDummyHandle; + GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, (LPCTSTR)&Inject_MSS, &hDummyHandle); + } + } + const auto pInjectMSS = Inject_MSS; + + BOOL Inject_UAL(int a, void* b) + { + if ( RsEventHandler(a, b) ) + { + if ( !std::exchange(delayedPatchesDone, true) && Func != nullptr ) + { + Func(); + } + return TRUE; + } + return FALSE; + } + +} + namespace Common { namespace Patches { void III_VC_Common() @@ -26,6 +62,19 @@ namespace Common { using namespace Memory; using namespace hook; + // Delayed patching + { + using namespace DelayedPatches; + + auto addr_mssHook = get_pattern( "6A 00 6A 02 6A 10 68 00 7D 00 00", -6 + 2 ); + OldSetPreference = *static_cast(addr_mssHook); + Patch( addr_mssHook, &pInjectMSS ); + + auto addr_ualHook = get_pattern( "FF 15 ? ? ? ? 6A 00 6A 18", 0xA ); + ReadCall( addr_ualHook, RsEventHandler ); + InjectHook( addr_ualHook, Inject_UAL ); + } + // Fixed bomb ownership/bombs saving for bikes { auto addr = get_pattern( "83 3C 33 00 74 19 89 F9 E8", 8 ); @@ -44,5 +93,10 @@ namespace Common { InjectHook( findExactWord.get( 0xD ), strncmp_Fix ); } } + + void III_VC_SetDelayedPatchesFunc( void(*func)() ) + { + DelayedPatches::Func = std::move(func); + } } } \ No newline at end of file diff --git a/SilentPatch/Common.h b/SilentPatch/Common.h index a0ed8d6..5a953cf 100644 --- a/SilentPatch/Common.h +++ b/SilentPatch/Common.h @@ -5,5 +5,6 @@ namespace Common namespace Patches { void III_VC_Common(); + void III_VC_SetDelayedPatchesFunc( void(*func)() ); } }; \ No newline at end of file diff --git a/SilentPatchIII/SilentPatchIII.cpp b/SilentPatchIII/SilentPatchIII.cpp index 3c30ab9..3ef7f78 100644 --- a/SilentPatchIII/SilentPatchIII.cpp +++ b/SilentPatchIII/SilentPatchIII.cpp @@ -981,9 +981,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) } Common::Patches::FixRwcseg_Patterns(); - - HMODULE hDummyHandle; - GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)&DllMain, &hDummyHandle); } return TRUE; } diff --git a/SilentPatchVC/SilentPatchVC.cpp b/SilentPatchVC/SilentPatchVC.cpp index f57abf0..74aec71 100644 --- a/SilentPatchVC/SilentPatchVC.cpp +++ b/SilentPatchVC/SilentPatchVC.cpp @@ -810,9 +810,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) } Common::Patches::FixRwcseg_Patterns(); - - HMODULE hDummyHandle; - GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)&DllMain, &hDummyHandle); } return TRUE; }