diff --git a/SilentPatch/ModuleList.hpp b/SilentPatch/ModuleList.hpp index 03f0c3d..acb3595 100644 --- a/SilentPatch/ModuleList.hpp +++ b/SilentPatch/ModuleList.hpp @@ -4,45 +4,64 @@ #include #include -#define PSAPI_VERSION 1 -#include -#include - -#pragma comment(lib, "Psapi.lib") - // Stores a list of loaded modules with their names, WITHOUT extension class ModuleList { public: void Enumerate() { - const HANDLE currentProcess = GetCurrentProcess(); - constexpr size_t INITIAL_SIZE = sizeof(HMODULE) * 256; HMODULE* modules = static_cast(malloc( INITIAL_SIZE )); if ( modules != nullptr ) { - DWORD cbNeeded = 0; - if ( EnumProcessModules( currentProcess, modules, INITIAL_SIZE, &cbNeeded ) != 0 ) - { - if ( cbNeeded > INITIAL_SIZE ) - { - HMODULE* newModules = static_cast(realloc( modules, cbNeeded )); - if ( newModules != nullptr ) - { - modules = newModules; + typedef BOOL (WINAPI * Func)(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded); - if ( EnumProcessModules( currentProcess, modules, cbNeeded, &cbNeeded ) != 0 ) - { - EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) ); - } - } - } - else + HMODULE hLib = LoadLibrary( TEXT("kernel32") ); + assert( hLib != nullptr ); // If this fails then everything is probably broken anyway + + Func pEnumProcessModules = reinterpret_cast(GetProcAddress( hLib, "K32EnumProcessModules" )); + if ( pEnumProcessModules == nullptr ) + { + // Try psapi + FreeLibrary( hLib ); + hLib = LoadLibrary( TEXT("psapi") ); + if ( hLib != nullptr ) { - EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) ); + pEnumProcessModules = reinterpret_cast(GetProcAddress( hLib, "EnumProcessModules" )); } } + + if ( pEnumProcessModules != nullptr ) + { + const HANDLE currentProcess = GetCurrentProcess(); + DWORD cbNeeded = 0; + if ( pEnumProcessModules( currentProcess, modules, INITIAL_SIZE, &cbNeeded ) != 0 ) + { + if ( cbNeeded > INITIAL_SIZE ) + { + HMODULE* newModules = static_cast(realloc( modules, cbNeeded )); + if ( newModules != nullptr ) + { + modules = newModules; + + if ( pEnumProcessModules( currentProcess, modules, cbNeeded, &cbNeeded ) != 0 ) + { + EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) ); + } + } + } + else + { + EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) ); + } + } + } + + if ( hLib != nullptr ) + { + FreeLibrary( hLib ); + } + free( modules ); } } diff --git a/SilentPatch/TheFLAUtils.cpp b/SilentPatch/TheFLAUtils.cpp index e698c28..61b5038 100644 --- a/SilentPatch/TheFLAUtils.cpp +++ b/SilentPatch/TheFLAUtils.cpp @@ -5,15 +5,13 @@ #include "ModuleList.hpp" -extern ModuleList moduleList; - int32_t (*FLAUtils::GetExtendedID8Func)(const uint8_t* ptr) = FLAUtils::GetExtendedID8_Stock; int32_t (*FLAUtils::GetExtendedID16Func)(const uint16_t* ptr) = FLAUtils::GetExtendedID16_Stock; void (*FLAUtils::SetCdStreamWakeFunc)(CdStreamWakeFunc func) = nullptr; static HMODULE flaModule = nullptr; -void FLAUtils::Init() +void FLAUtils::Init( const ModuleList& moduleList ) { flaModule = moduleList.Get( L"$fastman92limitAdjuster" ); if ( flaModule != nullptr ) diff --git a/SilentPatch/TheFLAUtils.h b/SilentPatch/TheFLAUtils.h index 7962c9a..b60bfed 100644 --- a/SilentPatch/TheFLAUtils.h +++ b/SilentPatch/TheFLAUtils.h @@ -1,6 +1,7 @@ #pragma once #include +class ModuleList; class FLAUtils { @@ -37,7 +38,7 @@ public: typedef void(*CdStreamWakeFunc)( struct CdStream* ); - static void Init(); + static void Init( const ModuleList& moduleList ); static bool UsesEnhancedIMGs(); static void SetCdStreamWakeFunction( CdStreamWakeFunc func ) diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index 6afc7d4..486027d 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -24,8 +24,6 @@ #include "debugmenu_public.h" -ModuleList moduleList; - // ============= Mod compatibility stuff ============= namespace ModCompat @@ -2186,6 +2184,7 @@ BOOL InjectDelayedPatches_10() GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); + ModuleList moduleList; moduleList.Enumerate(); const bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; @@ -2417,8 +2416,7 @@ BOOL InjectDelayedPatches_10() InjectHook(0x713ACB, HandleMoonStuffStub, PATCH_JUMP); } - FLAUtils::Init(); - moduleList.Clear(); + FLAUtils::Init( moduleList ); // Race condition in CdStream fixed // Not taking effect with modloader @@ -2523,6 +2521,7 @@ BOOL InjectDelayedPatches_11() GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); + ModuleList moduleList; moduleList.Enumerate(); bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; @@ -2657,8 +2656,7 @@ BOOL InjectDelayedPatches_11() // Albeit 1.01 obfuscates this function CCustomCarPlateMgr::GeneratePlateText = (decltype(CCustomCarPlateMgr::GeneratePlateText))0x6FDDE0; - FLAUtils::Init(); - moduleList.Clear(); + FLAUtils::Init( moduleList ); return FALSE; } @@ -2693,6 +2691,7 @@ BOOL InjectDelayedPatches_Steam() GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); + ModuleList moduleList; moduleList.Enumerate(); bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; @@ -2828,8 +2827,7 @@ BOOL InjectDelayedPatches_Steam() // to work fine with Deji's Custom Plate Format ReadCall( 0x4D3DA4, CCustomCarPlateMgr::GeneratePlateText ); - FLAUtils::Init(); - moduleList.Clear(); + FLAUtils::Init( moduleList ); return FALSE; } diff --git a/SilentPatchSA/SilentPatchSA.vcxproj b/SilentPatchSA/SilentPatchSA.vcxproj index 70588f0..6a26dd1 100644 --- a/SilentPatchSA/SilentPatchSA.vcxproj +++ b/SilentPatchSA/SilentPatchSA.vcxproj @@ -91,7 +91,7 @@ true Windows - shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs) + shell32.dll;shlwapi.dll;%(DelayLoadDLLs) copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi" @@ -131,7 +131,7 @@ copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\scripts\newsteam_r2_lowvio true true Windows - shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs) + shell32.dll;shlwapi.dll;%(DelayLoadDLLs) copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi" @@ -171,7 +171,7 @@ copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\scripts\newsteam_r2_lowvio true true Windows - shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs) + shell32.dll;shlwapi.dll;%(DelayLoadDLLs) copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi"