From 6f441292d0d4c030920f10322b1908df64a09817 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 10 Jul 2023 13:54:00 +0200 Subject: [PATCH] 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. --- r5dev/core/dllmain.cpp | 10 ++--- r5dev/pluginsdk/pluginsdk.cpp | 6 ++- r5dev/pluginsystem/pluginsystem.cpp | 2 +- r5dev/public/tier0/module.h | 4 +- r5dev/tier0/module.cpp | 58 +++++++++++++++++------------ 5 files changed, 48 insertions(+), 32 deletions(-) diff --git a/r5dev/core/dllmain.cpp b/r5dev/core/dllmain.cpp index 7466be78..a036d939 100644 --- a/r5dev/core/dllmain.cpp +++ b/r5dev/core/dllmain.cpp @@ -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(); @@ -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; diff --git a/r5dev/pluginsdk/pluginsdk.cpp b/r5dev/pluginsdk/pluginsdk.cpp index ac53ecd7..ab541d44 100644 --- a/r5dev/pluginsdk/pluginsdk.cpp +++ b/r5dev/pluginsdk/pluginsdk.cpp @@ -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"); } //--------------------------------------------------------------------------------- diff --git a/r5dev/pluginsystem/pluginsystem.cpp b/r5dev/pluginsystem/pluginsystem.cpp index fb32f674..477c5dd4 100644 --- a/r5dev/pluginsystem/pluginsystem.cpp +++ b/r5dev/pluginsystem/pluginsystem.cpp @@ -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 diff --git a/r5dev/public/tier0/module.h b/r5dev/public/tier0/module.h index fff6e80a..ac5e1d0d 100644 --- a/r5dev/public/tier0/module.h +++ b/r5dev/public/tier0/module.h @@ -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; diff --git a/r5dev/tier0/module.cpp b/r5dev/tier0/module.cpp index ac411b3d..bd25aa1e 100644 --- a/r5dev/tier0/module.cpp +++ b/r5dev/tier0/module.cpp @@ -12,16 +12,7 @@ //----------------------------------------------------------------------------- CModule::CModule(const char* szModuleName) { - m_pModuleBase = reinterpret_cast(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(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(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))