From 0118e05280c95c389724c3fd52f139f1708a9b81 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 28 Nov 2023 16:39:09 +0700 Subject: [PATCH] Improved initialization of rehlds api Prefer Sys_GetModuleHandle over Sys_LoadModule to avoid unnecessary reference count increase --- metamod/include/public/interface.cpp | 8 ++++++++ metamod/include/public/interface.h | 2 ++ metamod/src/meta_rehlds_api.cpp | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/metamod/include/public/interface.cpp b/metamod/include/public/interface.cpp index 1bd0c92..c496d05 100644 --- a/metamod/include/public/interface.cpp +++ b/metamod/include/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/metamod/include/public/interface.h b/metamod/include/public/interface.h index 9773a99..cd7c090 100644 --- a/metamod/include/public/interface.h +++ b/metamod/include/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/metamod/src/meta_rehlds_api.cpp b/metamod/src/meta_rehlds_api.cpp index cbdd903..e6c2849 100644 --- a/metamod/src/meta_rehlds_api.cpp +++ b/metamod/src/meta_rehlds_api.cpp @@ -46,7 +46,26 @@ bool rehlds_api_init(CSysModule* engineModule) bool meta_init_rehlds_api() { - CSysModule* engineModule = Sys_LoadModule(ENGINE_LIB); +#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", "hw.dll", "sw.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) + { + if (engineModule = Sys_GetModuleHandle(dllName)) + break; // gotcha + } + +#else + CSysModule *engineModule = Sys_GetModuleHandle("engine_i486.so"); +#endif + if (!rehlds_api_init(engineModule)) { return false; }