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 <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
class ModuleList
{
public:
void Enumerate()
{
const HANDLE currentProcess = GetCurrentProcess();
constexpr size_t INITIAL_SIZE = sizeof(HMODULE) * 256;
HMODULE* modules = static_cast<HMODULE*>(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<HMODULE*>(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<Func>(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<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 );
}
}

View File

@ -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 )

View File

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
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 )

View File

@ -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;
}

View File

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