CModule construction optimization

Moved construction logic to separate method, and call that from constructor instead. When willing to change the entire context on the same object, you can now just call 'InitFromXXXX()'. Previously, a whole new object would be generated and copied into ours, and then deleted again.
This commit is contained in:
Kawe Mazidjatari 2023-07-10 13:54:00 +02:00
parent 8b0fecf8bc
commit 6f441292d0
5 changed files with 48 additions and 32 deletions

View File

@ -58,9 +58,9 @@ void Show_Emblem()
void Tier0_Init()
{
#if !defined (DEDICATED)
g_RadVideoToolsDll = CModule("bink2w64.dll");
g_RadAudioDecoderDll = CModule("binkawin64.dll");
g_RadAudioSystemDll = CModule("mileswin64.dll");
g_RadVideoToolsDll.InitFromName("bink2w64.dll");
g_RadAudioDecoderDll.InitFromName("binkawin64.dll");
g_RadAudioSystemDll.InitFromName("mileswin64.dll");
#endif // !DEDICATED
g_pCmdLine = g_GameDll.GetExportedSymbol("g_pCmdLine").RCast<CCommandLine*>();
@ -158,8 +158,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
PEB64* pEnv = CModule::GetProcessEnvironmentBlock();
g_GameDll = CModule(pEnv->ImageBaseAddress);
g_SDKDll = CModule((QWORD)hModule);
g_GameDll.InitFromBase(pEnv->ImageBaseAddress);
g_SDKDll.InitFromBase((QWORD)hModule);
SDK_Init();
break;

View File

@ -19,8 +19,10 @@
//---------------------------------------------------------------------------------
CPluginSDK::CPluginSDK(const char* pszSelfModule) : m_FactoryInstance(nullptr), m_PluginSystem(nullptr)
{
m_SelfModule = CModule(pszSelfModule);
m_GameModule = CModule("r5apex.exe");
m_SelfModule.InitFromName(pszSelfModule);
// !TODO: Use PEB!
m_GameModule.InitFromName("r5apex.exe");
}
//---------------------------------------------------------------------------------

View File

@ -54,7 +54,7 @@ bool CPluginSystem::LoadPluginInstance(PluginInstance_t& pluginInst)
if (loadedPlugin == INVALID_HANDLE_VALUE || loadedPlugin == 0)
return false;
CModule pluginModule = CModule(pluginInst.m_svPluginName.c_str());
CModule pluginModule(pluginInst.m_svPluginName.c_str());
// Pass selfModule here on load function, we have to do
// this because local listen/dedi/client dll's are called

View File

@ -22,7 +22,9 @@ public:
CModule(const char* szModuleName);
CModule(const QWORD nModuleBase);
void Init();
void InitFromName(const char* szModuleName);
void InitFromBase(const QWORD nModuleBase);
void LoadSections();
CMemory FindPatternSIMD(const char* szPattern, const ModuleSections_t* moduleSection = nullptr) const;

View File

@ -12,16 +12,7 @@
//-----------------------------------------------------------------------------
CModule::CModule(const char* szModuleName)
{
m_pModuleBase = reinterpret_cast<QWORD>(GetModuleHandleA(szModuleName));
if (!m_pModuleBase)
{
Assert(0);
return;
}
m_ModuleName = szModuleName;
Init();
InitFromName(szModuleName);
}
//-----------------------------------------------------------------------------
@ -30,15 +21,45 @@ CModule::CModule(const char* szModuleName)
//-----------------------------------------------------------------------------
CModule::CModule(const QWORD nModuleBase)
{
m_pModuleBase = nModuleBase;
InitFromBase(nModuleBase);
}
if(!m_pModuleBase)
//-----------------------------------------------------------------------------
// Purpose: initializes class from module name
// Input : *szModuleName -
//-----------------------------------------------------------------------------
void CModule::InitFromName(const char* szModuleName)
{
m_pModuleBase = reinterpret_cast<QWORD>(GetModuleHandleA(szModuleName));
if (!m_pModuleBase)
{
Assert(0);
return;
}
Init();
m_nModuleSize = GetNTHeaders()->OptionalHeader.SizeOfImage;
m_ModuleName = szModuleName;
LoadSections();
}
//-----------------------------------------------------------------------------
// Purpose: initializes class from module base
// Input : *nModuleBase -
//-----------------------------------------------------------------------------
void CModule::InitFromBase(const QWORD nModuleBase)
{
m_pModuleBase = nModuleBase;
if (!m_pModuleBase)
{
Assert(0);
return;
}
m_nModuleSize = GetNTHeaders()->OptionalHeader.SizeOfImage;
LoadSections();
CHAR szModuleName[MAX_FILEPATH];
DWORD m = GetModuleFileNameA(reinterpret_cast<HMODULE>(nModuleBase),
@ -56,15 +77,6 @@ CModule::CModule(const QWORD nModuleBase)
}
}
//-----------------------------------------------------------------------------
// Purpose: initializes module descriptors
//-----------------------------------------------------------------------------
void CModule::Init()
{
m_nModuleSize = GetNTHeaders()->OptionalHeader.SizeOfImage;
LoadSections();
}
//-----------------------------------------------------------------------------
// Purpose: initializes the default executable segments
//-----------------------------------------------------------------------------
@ -353,7 +365,7 @@ CMemory CModule::FindFreeDataPage(const size_t nSize) const
// for the actual 'page' sizes. Also can be
// optimized to search per 'section'.
const QWORD endOfModule = m_pModuleBase
+ GetNTHeaders()->OptionalHeader.SizeOfImage - sizeof(QWORD);
+ GetModuleSize() - sizeof(QWORD);
for (QWORD currAddr = endOfModule;
m_pModuleBase < currAddr; currAddr -= sizeof(QWORD))