Add support for other engine binaries in game configs (#1414). (#1626)

* Add support for other engine binaries in game configs (#1414).

* Add engine bin path for CRC bin lookup and filter out addons from GAMEBIN.

* MAX_PATH -> PLATFORM_MAX_PATH.

* Fix library lookup on Linux.

Before this, there was a bad assumption that, like on Windows, POSIX module
handle pointers were within the module's address space (and thus usable
with dladdr). That's not true!

Instead, to get a usable address on all platforms, we'll do a lookup of the
CreateInterface function that exists in all modules. This also has the
(arguable) benefit of further locking this implementation to modules owned
by the game.

To get a valid address inside the module now on both p
This commit is contained in:
Nicholas Hastings 2022-12-28 17:58:30 -05:00 committed by GitHub
parent 7e94bfb307
commit ecb707e38d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 156 additions and 84 deletions

View File

@ -86,11 +86,6 @@ public:
const char *gamesuffix; const char *gamesuffix;
/* Data */ /* Data */
ServerGlobals *serverGlobals; ServerGlobals *serverGlobals;
void * serverFactory;
void * engineFactory;
void * matchmakingDSFactory;
void * soundemittersystemFactory;
void * vscriptFactory;
SMGlobalClass * listeners; SMGlobalClass * listeners;
// ConVar functions. // ConVar functions.

View File

@ -56,6 +56,7 @@ public:
virtual void RenameFile(char const *pOldPath, char const *pNewPath, const char *pathID = 0) = 0; virtual void RenameFile(char const *pOldPath, char const *pNewPath, const char *pathID = 0) = 0;
virtual bool IsDirectory(const char *pFileName, const char *pathID = 0) = 0; virtual bool IsDirectory(const char *pFileName, const char *pathID = 0) = 0;
virtual void CreateDirHierarchy(const char *path, const char *pathID = 0) = 0; virtual void CreateDirHierarchy(const char *path, const char *pathID = 0) = 0;
virtual int GetSearchPath(const char* pathID, bool bGetPackFiles, char* pPath, int nMaxLen) = 0;
}; };
} // namespace SourceMod } // namespace SourceMod

View File

@ -30,6 +30,7 @@
#include "common_logic.h" #include "common_logic.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <sstream>
#include <sh_list.h> #include <sh_list.h>
#include <sh_string.h> #include <sh_string.h>
#include "GameConfigs.h" #include "GameConfigs.h"
@ -46,6 +47,7 @@
#include <am-string.h> #include <am-string.h>
#include <bridge/include/ILogger.h> #include <bridge/include/ILogger.h>
#include <bridge/include/CoreProvider.h> #include <bridge/include/CoreProvider.h>
#include <bridge/include/IFileSystemBridge.h>
#if defined PLATFORM_POSIX #if defined PLATFORM_POSIX
#include <dlfcn.h> #include <dlfcn.h>
@ -103,8 +105,6 @@ struct TempSigInfo
char sig[1024]; char sig[1024];
char library[64]; char library[64];
} s_TempSig; } s_TempSig;
unsigned int s_ServerBinCRC;
bool s_ServerBinCRC_Ok = false;
static bool DoesGameMatch(const char *value) static bool DoesGameMatch(const char *value)
{ {
@ -303,38 +303,21 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
{ {
char error[255]; char error[255];
error[0] = '\0'; error[0] = '\0';
if (strcmp(name, "server") != 0) GameBinaryInfo binInfo;
if (!g_GameConfigs.TryGetGameBinaryInfo(name, &binInfo))
{ {
ke::SafeSprintf(error, sizeof(error), "Unrecognized library \"%s\"", name); ke::SafeSprintf(error, sizeof(error), "Unrecognized library \"%s\"", name);
} }
else if (!s_ServerBinCRC_Ok) else if (!binInfo.m_crcOK)
{ {
FILE *fp; ke::SafeSprintf(error, sizeof(error), "Could not get CRC for binary: %s", name);
char path[PLATFORM_MAX_PATH]; }
else
char binName[64];
bridge->FormatSourceBinaryName(name, binName, sizeof(binName));
g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/%s", binName);
if ((fp = fopen(path, "rb")) == NULL)
{ {
ke::SafeSprintf(error, sizeof(error), "Could not open binary: %s", path); bCurrentBinCRC_Ok = binInfo.m_crcOK;
} else { bCurrentBinCRC = binInfo.m_crc;
size_t size;
void *buffer;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer = malloc(size);
fread(buffer, size, 1, fp);
s_ServerBinCRC = UTIL_CRC32(buffer, size);
free(buffer);
s_ServerBinCRC_Ok = true;
fclose(fp);
}
} }
if (error[0] != '\0') if (error[0] != '\0')
{ {
m_IgnoreLevel = 1; m_IgnoreLevel = 1;
@ -461,12 +444,12 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
} }
} else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) { } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
if (DoesPlatformMatch(key) if (DoesPlatformMatch(key)
&& s_ServerBinCRC_Ok && bCurrentBinCRC_Ok
&& !bShouldBeReadingDefault) && !bShouldBeReadingDefault)
{ {
unsigned int crc = 0; unsigned int crc = 0;
sscanf(value, "%08X", &crc); sscanf(value, "%08X", &crc);
if (s_ServerBinCRC == crc) if (bCurrentBinCRC == crc)
{ {
bShouldBeReadingDefault = true; bShouldBeReadingDefault = true;
} }
@ -603,18 +586,12 @@ SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library)); strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
} }
void *addrInBase = NULL; void *addrInBase = NULL;
if (strcmp(s_TempSig.library, "server") == 0) GameBinaryInfo binInfo;
if (g_GameConfigs.TryGetGameBinaryInfo(s_TempSig.library, &binInfo))
{ {
addrInBase = bridge->serverFactory; addrInBase = binInfo.m_pAddr;
} else if (strcmp(s_TempSig.library, "engine") == 0) {
addrInBase = bridge->engineFactory;
} else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
addrInBase = bridge->matchmakingDSFactory;
} else if (strcmp(s_TempSig.library, "soundemittersystem") == 0) {
addrInBase = bridge->soundemittersystemFactory;
} else if (strcmp(s_TempSig.library, "vscript") == 0) {
addrInBase = bridge->vscriptFactory;
} }
void *final_addr = NULL; void *final_addr = NULL;
if (addrInBase == NULL) if (addrInBase == NULL)
{ {
@ -1116,6 +1093,30 @@ GameConfigManager::~GameConfigManager()
void GameConfigManager::OnSourceModStartup(bool late) void GameConfigManager::OnSourceModStartup(bool late)
{ {
char search_path[PLATFORM_MAX_PATH * 8];
bridge->filesystem->GetSearchPath("GAMEBIN", false, search_path, sizeof(search_path));
char addons_folder[12];
ke::SafeSprintf(addons_folder, sizeof(addons_folder), "%caddons%c", PLATFORM_SEP_CHAR, PLATFORM_SEP_CHAR);
std::istringstream iss(search_path);
for (std::string path; std::getline(iss, path, ';');)
{
if (path.length() > 0
&& path.find(addons_folder) == std::string::npos
&& m_gameBinDirectories.find(path.c_str()) == m_gameBinDirectories.cend()
)
m_gameBinDirectories.insert(path);
}
bridge->filesystem->GetSearchPath("EXECUTABLE_PATH", false, search_path, sizeof(search_path));
std::istringstream iss2(search_path);
for (std::string path; std::getline(iss2, path, ';');)
{
if (m_gameBinDirectories.find(path.c_str()) == m_gameBinDirectories.cend())
m_gameBinDirectories.insert(path);
}
LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0); LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game)); strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
@ -1238,3 +1239,76 @@ void GameConfigManager::RemoveCachedConfig(CGameConfig *config)
{ {
m_Lookup.remove(config->m_File); m_Lookup.remove(config->m_File);
} }
void GameConfigManager::CacheGameBinaryInfo(const char* pszName)
{
GameBinaryInfo info;
char name[64];
bridge->FormatSourceBinaryName(pszName, name, sizeof(name));
bool binary_found = false;
char binary_path[PLATFORM_MAX_PATH];
for (auto it = m_gameBinDirectories.begin(); it != m_gameBinDirectories.end(); ++it)
{
ke::SafeSprintf(binary_path, sizeof(binary_path), "%s%s%s", it->c_str(), it->back() == PLATFORM_SEP_CHAR ? "" : PLATFORM_SEP, name);
#if defined PLATFORM_WINDOWS
HMODULE hModule = LoadLibraryA(binary_path);
if (hModule)
{
info.m_pAddr = GetProcAddress(hModule, "CreateInterface");
FreeLibrary(hModule);
}
#else
void *pHandle = dlopen(binary_path, RTLD_NOW);
if (pHandle)
{
info.m_pAddr = dlsym(pHandle, "CreateInterface");
dlclose(pHandle);
}
#endif
if (info.m_pAddr)
break;
}
// Don't bother trying to get CRC if we couldn't find the bin loaded
if (info.m_pAddr)
{
FILE *fp;
if ((fp = fopen(binary_path, "rb")) == 0)
{
info.m_crc = 0;
}
else
{
size_t size;
void* buffer;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer = malloc(size);
fread(buffer, size, 1, fp);
info.m_crc = UTIL_CRC32(buffer, size);
free(buffer);
info.m_crcOK = true;
fclose(fp);
}
}
// But insert regardless, to cache the first lookup (even as failed)
m_gameBinInfos.insert(pszName, info);
}
bool GameConfigManager::TryGetGameBinaryInfo(const char* pszName, GameBinaryInfo* pDest)
{
if (m_gameBinInfos.retrieve(pszName, pDest))
return pDest->m_pAddr != nullptr;
CacheGameBinaryInfo(pszName);
return m_gameBinInfos.retrieve(pszName, pDest);
}

View File

@ -38,6 +38,7 @@
#include <am-refcounting.h> #include <am-refcounting.h>
#include <sm_stringhashmap.h> #include <sm_stringhashmap.h>
#include <sm_namehashset.h> #include <sm_namehashset.h>
#include <set>
using namespace SourceMod; using namespace SourceMod;
@ -91,6 +92,8 @@ private:
std::string m_offset; std::string m_offset;
std::string m_Game; std::string m_Game;
std::string m_Key; std::string m_Key;
unsigned int bCurrentBinCRC;
bool bCurrentBinCRC_Ok = false;
bool bShouldBeReadingDefault; bool bShouldBeReadingDefault;
bool had_game; bool had_game;
bool matched_game; bool matched_game;
@ -126,6 +129,13 @@ private:
time_t m_ModTime; time_t m_ModTime;
}; };
struct GameBinaryInfo
{
void *m_pAddr = nullptr;
uint32_t m_crc = 0;
bool m_crcOK = false;
};
class GameConfigManager : class GameConfigManager :
public IGameConfigManager, public IGameConfigManager,
public SMGlobalClass public SMGlobalClass
@ -148,9 +158,14 @@ public: //SMGlobalClass
void OnSourceModAllInitialized(); void OnSourceModAllInitialized();
void OnSourceModAllShutdown(); void OnSourceModAllShutdown();
public: public:
bool TryGetGameBinaryInfo(const char* pszName, GameBinaryInfo* pDest);
void RemoveCachedConfig(CGameConfig *config); void RemoveCachedConfig(CGameConfig *config);
private:
void CacheGameBinaryInfo(const char* pszName);
private: private:
NameHashSet<CGameConfig *> m_Lookup; NameHashSet<CGameConfig *> m_Lookup;
StringHashMap<GameBinaryInfo> m_gameBinInfos;
std::set<std::string> m_gameBinDirectories;
public: public:
StringHashMap<ITextListener_SMC *> m_customHandlers; StringHashMap<ITextListener_SMC *> m_customHandlers;
}; };

View File

@ -102,90 +102,94 @@ public:
class VFileSystem_Logic : public IFileSystemBridge class VFileSystem_Logic : public IFileSystemBridge
{ {
public: public:
const char *FindFirstEx(const char *pWildCard, const char *pPathID, FileFindHandle_t *pHandle) const char *FindFirstEx(const char *pWildCard, const char *pPathID, FileFindHandle_t *pHandle) override
{ {
return filesystem->FindFirstEx(pWildCard, pPathID, pHandle); return filesystem->FindFirstEx(pWildCard, pPathID, pHandle);
} }
const char *FindNext(FileFindHandle_t handle) const char *FindNext(FileFindHandle_t handle) override
{ {
return filesystem->FindNext(handle); return filesystem->FindNext(handle);
} }
bool FindIsDirectory(FileFindHandle_t handle) bool FindIsDirectory(FileFindHandle_t handle) override
{ {
return filesystem->FindIsDirectory(handle); return filesystem->FindIsDirectory(handle);
} }
void FindClose(FileFindHandle_t handle) void FindClose(FileFindHandle_t handle) override
{ {
filesystem->FindClose(handle); filesystem->FindClose(handle);
} }
FileHandle_t Open(const char *pFileName, const char *pOptions, const char *pathID = 0) FileHandle_t Open(const char *pFileName, const char *pOptions, const char *pathID = 0) override
{ {
return filesystem->Open(pFileName, pOptions, pathID); return filesystem->Open(pFileName, pOptions, pathID);
} }
void Close(FileHandle_t file) void Close(FileHandle_t file) override
{ {
filesystem->Close(file); filesystem->Close(file);
} }
char *ReadLine(char *pOutput, int maxChars, FileHandle_t file) char *ReadLine(char *pOutput, int maxChars, FileHandle_t file) override
{ {
return filesystem->ReadLine(pOutput, maxChars, file); return filesystem->ReadLine(pOutput, maxChars, file);
} }
bool EndOfFile(FileHandle_t file) bool EndOfFile(FileHandle_t file) override
{ {
return filesystem->EndOfFile(file); return filesystem->EndOfFile(file);
} }
bool FileExists(const char *pFileName, const char *pPathID = 0) bool FileExists(const char *pFileName, const char *pPathID = 0) override
{ {
return filesystem->FileExists(pFileName, pPathID); return filesystem->FileExists(pFileName, pPathID);
} }
unsigned int Size(const char *pFileName, const char *pPathID = 0) unsigned int Size(const char *pFileName, const char *pPathID = 0) override
{ {
return filesystem->Size(pFileName, pPathID); return filesystem->Size(pFileName, pPathID);
} }
int Read(void* pOutput, int size, FileHandle_t file) int Read(void* pOutput, int size, FileHandle_t file) override
{ {
return filesystem->Read(pOutput, size, file); return filesystem->Read(pOutput, size, file);
} }
int Write(void const* pInput, int size, FileHandle_t file) int Write(void const* pInput, int size, FileHandle_t file) override
{ {
return filesystem->Write(pInput, size, file); return filesystem->Write(pInput, size, file);
} }
void Seek(FileHandle_t file, int pos, int seekType) void Seek(FileHandle_t file, int pos, int seekType) override
{ {
filesystem->Seek(file, pos, (FileSystemSeek_t) seekType); filesystem->Seek(file, pos, (FileSystemSeek_t) seekType);
} }
unsigned int Tell(FileHandle_t file) unsigned int Tell(FileHandle_t file) override
{ {
return filesystem->Tell(file); return filesystem->Tell(file);
} }
int FPrint(FileHandle_t file, const char *pData) int FPrint(FileHandle_t file, const char *pData) override
{ {
return filesystem->FPrintf(file, "%s", pData); return filesystem->FPrintf(file, "%s", pData);
} }
void Flush(FileHandle_t file) void Flush(FileHandle_t file) override
{ {
filesystem->Flush(file); filesystem->Flush(file);
} }
bool IsOk(FileHandle_t file) bool IsOk(FileHandle_t file) override
{ {
return filesystem->IsOk(file); return filesystem->IsOk(file);
} }
void RemoveFile(const char *pRelativePath, const char *pathID) void RemoveFile(const char *pRelativePath, const char *pathID) override
{ {
filesystem->RemoveFile(pRelativePath, pathID); filesystem->RemoveFile(pRelativePath, pathID);
} }
void RenameFile(char const *pOldPath, char const *pNewPath, const char *pathID) void RenameFile(char const *pOldPath, char const *pNewPath, const char *pathID) override
{ {
filesystem->RenameFile(pOldPath, pNewPath, pathID); filesystem->RenameFile(pOldPath, pNewPath, pathID);
} }
bool IsDirectory(const char *pFileName, const char *pathID) bool IsDirectory(const char *pFileName, const char *pathID) override
{ {
return filesystem->IsDirectory(pFileName, pathID); return filesystem->IsDirectory(pFileName, pathID);
} }
void CreateDirHierarchy(const char *path, const char *pathID) void CreateDirHierarchy(const char *path, const char *pathID) override
{ {
filesystem->CreateDirHierarchy(path, pathID); filesystem->CreateDirHierarchy(path, pathID);
} }
int GetSearchPath(const char* pathID, bool bGetPackFiles, char* pPath, int nMaxLen) override
{
return filesystem->GetSearchPath(pathID, bGetPackFiles, pPath, nMaxLen);
}
} fs_wrapper; } fs_wrapper;
class VPlayerInfo_Logic : public IPlayerInfoBridge class VPlayerInfo_Logic : public IPlayerInfoBridge
@ -409,9 +413,6 @@ CoreProviderImpl::CoreProviderImpl()
this->GetGlobalTarget = get_global_target; this->GetGlobalTarget = get_global_target;
this->gamesuffix = GAMEFIX; this->gamesuffix = GAMEFIX;
this->serverGlobals = &::serverGlobals; this->serverGlobals = &::serverGlobals;
this->serverFactory = nullptr;
this->engineFactory = nullptr;
this->matchmakingDSFactory = nullptr;
this->listeners = nullptr; this->listeners = nullptr;
} }
@ -637,22 +638,8 @@ void CoreProviderImpl::InitializeBridge()
::serverGlobals.frametime = &gpGlobals->frametime; ::serverGlobals.frametime = &gpGlobals->frametime;
::serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick; ::serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
this->engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
this->serverFactory = (void *)g_SMAPI->GetServerFactory(false);
this->listeners = SMGlobalClass::head; this->listeners = SMGlobalClass::head;
if (auto mmlib = ::filesystem->LoadModule("matchmaking_ds" SOURCE_BIN_SUFFIX, "GAMEBIN")) {
this->matchmakingDSFactory = (void*)Sys_GetFactory(mmlib);
}
if (auto mmlib = ::filesystem->LoadModule("soundemittersystem" SOURCE_BIN_SUFFIX)) {
this->soundemittersystemFactory = (void*)Sys_GetFactory(mmlib);
}
if (auto mmlib = ::filesystem->LoadModule("vscript" SOURCE_BIN_SUFFIX)) {
this->vscriptFactory = (void*)Sys_GetFactory(mmlib);
}
logic_init_(this, &logicore); logic_init_(this, &logicore);
// Join logic's SMGlobalClass instances. // Join logic's SMGlobalClass instances.