Update ModuleList.hpp

This commit is contained in:
Silent 2018-06-05 01:16:51 +02:00
parent 3895f1c738
commit 3ec5663861
No known key found for this signature in database
GPG key ID: AE53149BB0C45AF1
2 changed files with 49 additions and 6 deletions

View file

@ -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<HMODULE*>(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<HMODULE> 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<HMODULE> 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 )
{

View file

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