From 98aafc93a2344cefcb2783dded309fb85f49964a Mon Sep 17 00:00:00 2001 From: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:23:10 +0100 Subject: [PATCH] Prevent module callbacks being ran for duplicate modules --- primedev/windows/libsys.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/primedev/windows/libsys.cpp b/primedev/windows/libsys.cpp index 0a3195dd3b..0aff820b72 100644 --- a/primedev/windows/libsys.cpp +++ b/primedev/windows/libsys.cpp @@ -16,15 +16,23 @@ ILoadLibraryExW o_LoadLibraryExW = nullptr; //----------------------------------------------------------------------------- // Purpose: Run detour callbacks for given HMODULE //----------------------------------------------------------------------------- -void LibSys_RunModuleCallbacks(HMODULE hModule, unsigned int iRecurse = 0) +void LibSys_RunModuleCallbacks(HMODULE hModule) { - if (!hModule || iRecurse > 1) + // Modules that we have already ran callbacks for. + // Note: If we ever hook unloading modules, then this will need updating to handle removal etc. + static std::vector vCalledModules; + + if (!hModule) { return; } - // FIXME [Fifty]: Instead of only recursing once we should store the modules we ran callbacks for - iRecurse++; + // If we have already ran callbacks for this module, don't run them again. + if (std::find(vCalledModules.begin(), vCalledModules.end(), hModule) != vCalledModules.end()) + { + return; + } + vCalledModules.push_back(hModule); // Get module base name in ASCII as noone wants to deal with unicode CHAR szModuleName[MAX_PATH]; @@ -33,7 +41,7 @@ void LibSys_RunModuleCallbacks(HMODULE hModule, unsigned int iRecurse = 0) // Run calllbacks for all imported modules CModule cModule(hModule); for (const std::string& svImport : cModule.GetImportedModules()) - LibSys_RunModuleCallbacks(GetModuleHandleA(svImport.c_str()), iRecurse); + LibSys_RunModuleCallbacks(GetModuleHandleA(svImport.c_str())); // DevMsg(eLog::NONE, "%s\n", szModuleName);