From 97f56131ac96dcdb03c93e97521d98fdd3b1bae9 Mon Sep 17 00:00:00 2001 From: narzoul Date: Tue, 18 May 2021 13:55:49 +0200 Subject: [PATCH] Fixed hooking of delay-loaded DLL functions Fixes blank intro videos in Midtown Madness 1. See issue #99. --- DDrawCompat/Common/Hook.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/DDrawCompat/Common/Hook.cpp b/DDrawCompat/Common/Hook.cpp index 9659317..6bf8f5e 100644 --- a/DDrawCompat/Common/Hook.cpp +++ b/DDrawCompat/Common/Hook.cpp @@ -128,11 +128,12 @@ namespace void hookFunction(void*& origFuncPtr, void* newFuncPtr, const char* funcName) { - BYTE* targetFunc = reinterpret_cast(origFuncPtr); + BYTE* targetFunc = static_cast(origFuncPtr); std::ostringstream oss; #ifdef DEBUGLOGS oss << Compat::funcPtrToStr(targetFunc) << ' '; +#endif char origFuncPtrStr[20] = {}; if (!funcName) @@ -142,8 +143,6 @@ namespace } auto prevTargetFunc = targetFunc; -#endif - while (true) { unsigned instructionSize = 0; @@ -161,6 +160,11 @@ namespace { instructionSize = 6; targetFunc = **reinterpret_cast(targetFunc + 2); + if (Compat::getModuleHandleFromAddress(targetFunc) == Compat::getModuleHandleFromAddress(prevTargetFunc)) + { + targetFunc = prevTargetFunc; + break; + } } else { @@ -168,8 +172,8 @@ namespace } #ifdef DEBUGLOGS oss << Compat::hexDump(prevTargetFunc, instructionSize) << " -> " << Compat::funcPtrToStr(targetFunc) << ' '; - prevTargetFunc = targetFunc; #endif + prevTargetFunc = targetFunc; } if (Compat::getModuleHandleFromAddress(targetFunc) == Dll::g_currentModule)