Skip to content

Commit

Permalink
Port fix from primedev
Browse files Browse the repository at this point in the history
  • Loading branch information
ASpoonPlaysGames committed Aug 26, 2024
1 parent 54aaad9 commit 734ac49
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
15 changes: 15 additions & 0 deletions primedev/thirdparty/silver-bun/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ void CModule::Init()
m_ModuleSections.push_back(ModuleSections_t(reinterpret_cast<const char*>(hCurrentSection.Name),
static_cast<uintptr_t>(m_pModuleBase + hCurrentSection.VirtualAddress), hCurrentSection.SizeOfRawData)); // Push back a struct with the section data.
}

// Get the location of IMAGE_IMPORT_DESCRIPTOR for this module by adding the IMAGE_DIRECTORY_ENTRY_IMPORT relative virtual address onto our
// module base address.
IMAGE_IMPORT_DESCRIPTOR* pImageImportDescriptors = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(
m_pModuleBase + m_pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
if (!pImageImportDescriptors)
return;

for (IMAGE_IMPORT_DESCRIPTOR* pIID = pImageImportDescriptors; pIID->Name != 0; pIID++)
{
// Get virtual relative Address of the imported module name. Then add module base Address to get the actual location.
const char* szImportedModuleName = reinterpret_cast<char*>(reinterpret_cast<DWORD*>(m_pModuleBase + pIID->Name));

m_vImportedModules.push_back(szImportedModuleName);
}
}

//-----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions primedev/thirdparty/silver-bun/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CModule
ModuleSections_t GetSectionByName(const char* szSectionName) const;

inline const std::vector<CModule::ModuleSections_t>& GetSections() const { return m_ModuleSections; }
inline const std::vector<std::string>& GetImportedModules() const { return m_vImportedModules; }
inline uintptr_t GetModuleBase(void) const { return m_pModuleBase; }
inline DWORD GetModuleSize(void) const { return m_nModuleSize; }
inline const std::string& GetModuleName(void) const { return m_ModuleName; }
Expand All @@ -73,4 +74,5 @@ class CModule
uintptr_t m_pModuleBase;
DWORD m_nModuleSize;
std::vector<ModuleSections_t> m_ModuleSections;
std::vector<std::string> m_vImportedModules;
};
12 changes: 10 additions & 2 deletions primedev/windows/libsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ ILoadLibraryExW o_LoadLibraryExW = nullptr;
//-----------------------------------------------------------------------------
// Purpose: Run detour callbacks for given HMODULE
//-----------------------------------------------------------------------------
void LibSys_RunModuleCallbacks(HMODULE hModule)
void LibSys_RunModuleCallbacks(HMODULE hModule, unsigned int iRecurse = 0)
{
if (!hModule)
if (!hModule || iRecurse > 1)
{
return;
}

// FIXME [Fifty]: Instead of only recursing once we should store the modules we ran callbacks for
iRecurse++;

// Get module base name in ASCII as noone wants to deal with unicode
CHAR szModuleName[MAX_PATH];
GetModuleBaseNameA(GetCurrentProcess(), hModule, szModuleName, MAX_PATH);

// Run calllbacks for all imported modules
CModule cModule(hModule);
for (const std::string& svImport : cModule.GetImportedModules())
LibSys_RunModuleCallbacks(GetModuleHandleA(svImport.c_str()), iRecurse);

// DevMsg(eLog::NONE, "%s\n", szModuleName);

// Call callbacks
Expand Down

0 comments on commit 734ac49

Please sign in to comment.