From 1a72c8a45cd70d111464ba5a2cb0c53f6b2b5614 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 10 Oct 2023 06:31:48 +0700 Subject: [PATCH] Improved initialization of rehlds api Prefer Sys_GetModuleHandle over Sys_LoadModule to avoid unnecessary reference count increase --- reapi/include/cssdk/public/interface.cpp | 8 ++++++++ reapi/include/cssdk/public/interface.h | 2 ++ reapi/src/mods/mod_regamedll_api.cpp | 2 +- reapi/src/mods/mod_rehlds_api.cpp | 19 ++++++++++++++----- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/reapi/include/cssdk/public/interface.cpp b/reapi/include/cssdk/public/interface.cpp index e6de1be..de0a184 100644 --- a/reapi/include/cssdk/public/interface.cpp +++ b/reapi/include/cssdk/public/interface.cpp @@ -121,6 +121,14 @@ void *Sys_GetProcAddress(void *pModuleHandle, const char *pName) return GetProcAddress((HMODULE)pModuleHandle, pName); } +// Purpose: Returns a module handle by its name. +// Input : pModuleName - module name +// Output : the module handle or NULL in case of an error +CSysModule *Sys_GetModuleHandle(const char *pModuleName) +{ + return reinterpret_cast(GetModuleHandle(pModuleName)); +} + // Purpose: Loads a DLL/component from disk and returns a handle to it // Input : *pModuleName - filename of the component // Output : opaque handle to the module (hides system dependency) diff --git a/reapi/include/cssdk/public/interface.h b/reapi/include/cssdk/public/interface.h index 0aeaca4..abe45b5 100644 --- a/reapi/include/cssdk/public/interface.h +++ b/reapi/include/cssdk/public/interface.h @@ -114,6 +114,8 @@ extern CreateInterfaceFn Sys_GetFactory(const char *pModuleName); // load/unload components class CSysModule; +extern CSysModule *Sys_GetModuleHandle(const char *pModuleName); + // Load & Unload should be called in exactly one place for each module // The factory for that module should be passed on to dependent components for // proper versioning. diff --git a/reapi/src/mods/mod_regamedll_api.cpp b/reapi/src/mods/mod_regamedll_api.cpp index bd7051e..22dfcbd 100644 --- a/reapi/src/mods/mod_regamedll_api.cpp +++ b/reapi/src/mods/mod_regamedll_api.cpp @@ -11,7 +11,7 @@ bool RegamedllApi_Init() if (!szGameDLLModule) return false; - CSysModule *gameModule = Sys_LoadModule(szGameDLLModule); + CSysModule *gameModule = Sys_GetModuleHandle(szGameDLLModule); if (!gameModule) return false; diff --git a/reapi/src/mods/mod_rehlds_api.cpp b/reapi/src/mods/mod_rehlds_api.cpp index 7dedb30..109c582 100644 --- a/reapi/src/mods/mod_rehlds_api.cpp +++ b/reapi/src/mods/mod_rehlds_api.cpp @@ -8,15 +8,24 @@ IRehldsServerStatic* g_RehldsSvs; bool RehldsApi_Init() { - if (!IS_DEDICATED_SERVER()) +#ifdef WIN32 + // Find the most appropriate module handle from a list of DLL candidates + // Notes: + // - "swds.dll" is the library Dedicated Server + // + // Let's also attempt to locate the ReHLDS API in the client's library + // - "sw.dll" is the client library for Software render, with a built-in listenserver + // - "hw.dll" is the client library for Hardware render, with a built-in listenserver + const char *dllNames[] = { "swds.dll", "sw.dll", "hw.dll" }; // List of DLL candidates to lookup for the ReHLDS API + CSysModule *engineModule = NULL; // The module handle of the selected DLL + for (const char *dllName : dllNames) { - return false; + if (engineModule = Sys_GetModuleHandle(dllName)) + break; // gotcha } -#ifdef WIN32 - CSysModule* engineModule = Sys_LoadModule("swds.dll"); #else - CSysModule* engineModule = Sys_LoadModule("engine_i486.so"); + CSysModule *engineModule = Sys_GetModuleHandle("engine_i486.so"); #endif if (!engineModule)