Use kernel32 export in ModuleList if possible

Make ModuleList a local variable
This commit is contained in:
Silent 2017-10-04 19:12:33 +02:00
parent e8b07f9f50
commit 4827c94a98
5 changed files with 56 additions and 40 deletions

View file

@ -4,45 +4,64 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#define PSAPI_VERSION 1
#include <Psapi.h>
#include <malloc.h>
#pragma comment(lib, "Psapi.lib")
// Stores a list of loaded modules with their names, WITHOUT extension // Stores a list of loaded modules with their names, WITHOUT extension
class ModuleList class ModuleList
{ {
public: public:
void Enumerate() void Enumerate()
{ {
const HANDLE currentProcess = GetCurrentProcess();
constexpr size_t INITIAL_SIZE = sizeof(HMODULE) * 256; constexpr size_t INITIAL_SIZE = sizeof(HMODULE) * 256;
HMODULE* modules = static_cast<HMODULE*>(malloc( INITIAL_SIZE )); HMODULE* modules = static_cast<HMODULE*>(malloc( INITIAL_SIZE ));
if ( modules != nullptr ) if ( modules != nullptr )
{ {
DWORD cbNeeded = 0; typedef BOOL (WINAPI * Func)(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded);
if ( EnumProcessModules( currentProcess, modules, INITIAL_SIZE, &cbNeeded ) != 0 )
{
if ( cbNeeded > INITIAL_SIZE )
{
HMODULE* newModules = static_cast<HMODULE*>(realloc( modules, cbNeeded ));
if ( newModules != nullptr )
{
modules = newModules;
if ( EnumProcessModules( currentProcess, modules, cbNeeded, &cbNeeded ) != 0 ) HMODULE hLib = LoadLibrary( TEXT("kernel32") );
{ assert( hLib != nullptr ); // If this fails then everything is probably broken anyway
EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) );
} Func pEnumProcessModules = reinterpret_cast<Func>(GetProcAddress( hLib, "K32EnumProcessModules" ));
} if ( pEnumProcessModules == nullptr )
} {
else // Try psapi
FreeLibrary( hLib );
hLib = LoadLibrary( TEXT("psapi") );
if ( hLib != nullptr )
{ {
EnumerateInternal( modules, cbNeeded / sizeof(HMODULE) ); pEnumProcessModules = reinterpret_cast<Func>(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<HMODULE*>(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 ); free( modules );
} }
} }

View file

@ -5,15 +5,13 @@
#include "ModuleList.hpp" #include "ModuleList.hpp"
extern ModuleList moduleList;
int32_t (*FLAUtils::GetExtendedID8Func)(const uint8_t* ptr) = FLAUtils::GetExtendedID8_Stock; int32_t (*FLAUtils::GetExtendedID8Func)(const uint8_t* ptr) = FLAUtils::GetExtendedID8_Stock;
int32_t (*FLAUtils::GetExtendedID16Func)(const uint16_t* ptr) = FLAUtils::GetExtendedID16_Stock; int32_t (*FLAUtils::GetExtendedID16Func)(const uint16_t* ptr) = FLAUtils::GetExtendedID16_Stock;
void (*FLAUtils::SetCdStreamWakeFunc)(CdStreamWakeFunc func) = nullptr; void (*FLAUtils::SetCdStreamWakeFunc)(CdStreamWakeFunc func) = nullptr;
static HMODULE flaModule = nullptr; static HMODULE flaModule = nullptr;
void FLAUtils::Init() void FLAUtils::Init( const ModuleList& moduleList )
{ {
flaModule = moduleList.Get( L"$fastman92limitAdjuster" ); flaModule = moduleList.Get( L"$fastman92limitAdjuster" );
if ( flaModule != nullptr ) if ( flaModule != nullptr )

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
class ModuleList;
class FLAUtils class FLAUtils
{ {
@ -37,7 +38,7 @@ public:
typedef void(*CdStreamWakeFunc)( struct CdStream* ); typedef void(*CdStreamWakeFunc)( struct CdStream* );
static void Init(); static void Init( const ModuleList& moduleList );
static bool UsesEnhancedIMGs(); static bool UsesEnhancedIMGs();
static void SetCdStreamWakeFunction( CdStreamWakeFunc func ) static void SetCdStreamWakeFunction( CdStreamWakeFunc func )

View file

@ -24,8 +24,6 @@
#include "debugmenu_public.h" #include "debugmenu_public.h"
ModuleList moduleList;
// ============= Mod compatibility stuff ============= // ============= Mod compatibility stuff =============
namespace ModCompat namespace ModCompat
@ -2186,6 +2184,7 @@ BOOL InjectDelayedPatches_10()
GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension
PathRenameExtensionW(wcModulePath, L".ini"); PathRenameExtensionW(wcModulePath, L".ini");
ModuleList moduleList;
moduleList.Enumerate(); moduleList.Enumerate();
const bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; const bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr;
@ -2417,8 +2416,7 @@ BOOL InjectDelayedPatches_10()
InjectHook(0x713ACB, HandleMoonStuffStub, PATCH_JUMP); InjectHook(0x713ACB, HandleMoonStuffStub, PATCH_JUMP);
} }
FLAUtils::Init(); FLAUtils::Init( moduleList );
moduleList.Clear();
// Race condition in CdStream fixed // Race condition in CdStream fixed
// Not taking effect with modloader // Not taking effect with modloader
@ -2523,6 +2521,7 @@ BOOL InjectDelayedPatches_11()
GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension
PathRenameExtensionW(wcModulePath, L".ini"); PathRenameExtensionW(wcModulePath, L".ini");
ModuleList moduleList;
moduleList.Enumerate(); moduleList.Enumerate();
bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr;
@ -2657,8 +2656,7 @@ BOOL InjectDelayedPatches_11()
// Albeit 1.01 obfuscates this function // Albeit 1.01 obfuscates this function
CCustomCarPlateMgr::GeneratePlateText = (decltype(CCustomCarPlateMgr::GeneratePlateText))0x6FDDE0; CCustomCarPlateMgr::GeneratePlateText = (decltype(CCustomCarPlateMgr::GeneratePlateText))0x6FDDE0;
FLAUtils::Init(); FLAUtils::Init( moduleList );
moduleList.Clear();
return FALSE; return FALSE;
} }
@ -2693,6 +2691,7 @@ BOOL InjectDelayedPatches_Steam()
GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension
PathRenameExtensionW(wcModulePath, L".ini"); PathRenameExtensionW(wcModulePath, L".ini");
ModuleList moduleList;
moduleList.Enumerate(); moduleList.Enumerate();
bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr;
@ -2828,8 +2827,7 @@ BOOL InjectDelayedPatches_Steam()
// to work fine with Deji's Custom Plate Format // to work fine with Deji's Custom Plate Format
ReadCall( 0x4D3DA4, CCustomCarPlateMgr::GeneratePlateText ); ReadCall( 0x4D3DA4, CCustomCarPlateMgr::GeneratePlateText );
FLAUtils::Init(); FLAUtils::Init( moduleList );
moduleList.Clear();
return FALSE; return FALSE;
} }

View file

@ -91,7 +91,7 @@
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<DelayLoadDLLs>shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>shell32.dll;shlwapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi" <Command>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
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<DelayLoadDLLs>shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>shell32.dll;shlwapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi" <Command>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
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<DelayLoadDLLs>shell32.dll;shlwapi.dll;psapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>shell32.dll;shlwapi.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi" <Command>copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\SilentPatchSA.asi"