From 3ec5663861c734f2b08e19e48dd76a22907bd9ed Mon Sep 17 00:00:00 2001 From: Silent Date: Tue, 5 Jun 2018 01:16:51 +0200 Subject: [PATCH] Update ModuleList.hpp --- SilentPatch/ModuleList.hpp | 46 +++++++++++++++++++++++++++++++++ SilentPatchSA/SilentPatchSA.cpp | 9 +++---- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/SilentPatch/ModuleList.hpp b/SilentPatch/ModuleList.hpp index e724f59..34773e8 100644 --- a/SilentPatch/ModuleList.hpp +++ b/SilentPatch/ModuleList.hpp @@ -8,10 +8,24 @@ class ModuleList { public: + struct LazyEnumerateTag {}; + + ModuleList() + { + Enumerate(); + } + + explicit ModuleList( LazyEnumerateTag ) + { + } + // Initializes module list // Needs to be called before any calls to Get or GetAll void Enumerate() { + // Cannot enumerate twice without cleaing + assert( m_moduleList.size() == 0 ); + constexpr size_t INITIAL_SIZE = sizeof(HMODULE) * 256; HMODULE* modules = static_cast(malloc( INITIAL_SIZE )); if ( modules != nullptr ) @@ -115,6 +129,38 @@ public: return results; } + // Gets handle of a loaded module with given prefix, NULL otherwise + HMODULE GetByPrefix( const wchar_t* modulePrefix ) const + { + // If vector is empty then we're trying to call it without calling Enumerate first + assert( m_moduleList.size() != 0 ); + + const size_t len = wcslen( modulePrefix ); + auto it = std::find_if( m_moduleList.begin(), m_moduleList.end(), [&]( const auto& e ) { + return _wcsnicmp( modulePrefix, e.second.c_str(), len ) == 0; + } ); + return it != m_moduleList.end() ? it->first : nullptr; + } + + // Gets handles to all loaded modules with given prefix + std::vector GetAllByPrefix( const wchar_t* modulePrefix ) const + { + // If vector is empty then we're trying to call it without calling Enumerate first + assert( m_moduleList.size() != 0 ); + + const size_t len = wcslen( modulePrefix ); + std::vector results; + for ( auto& e : m_moduleList ) + { + if ( _wcsnicmp( modulePrefix, e.second.c_str(), len ) == 0 ) + { + results.push_back( e.first ); + } + } + + return results; + } + private: void EnumerateInternal( HMODULE* modules, size_t numModules ) { diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index 7abf7e3..dc74cec 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -2345,8 +2345,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 ModuleList moduleList; const bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; const bool bSAMP = moduleList.Get(L"samp") != nullptr; @@ -2716,8 +2715,7 @@ BOOL InjectDelayedPatches_11() GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); - ModuleList moduleList; - moduleList.Enumerate(); + const ModuleList moduleList; bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; bool bSAMP = moduleList.Get(L"samp") != nullptr; @@ -2887,8 +2885,7 @@ BOOL InjectDelayedPatches_Steam() GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); - ModuleList moduleList; - moduleList.Enumerate(); + const ModuleList moduleList; bool bHasImVehFt = moduleList.Get(L"ImVehFt") != nullptr; bool bSAMP = moduleList.Get(L"samp") != nullptr;