Improved initialization of rehlds api

Prefer Sys_GetModuleHandle over Sys_LoadModule to avoid unnecessary reference count increase
This commit is contained in:
s1lentq 2023-10-10 13:38:41 +07:00
parent 33c4062e31
commit 8cae39a981
4 changed files with 68 additions and 64 deletions

View File

@ -128,6 +128,14 @@ void *Sys_GetProcAddress( void *pModuleHandle, const char *pName )
#endif
}
// 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<CSysModule *>(GetModuleHandle(pModuleName));
}
//-----------------------------------------------------------------------------
// Purpose: Loads a DLL/component from disk and returns a handle to it
// Input : *pModuleName - filename of the component

View File

@ -133,6 +133,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

View File

@ -3,8 +3,11 @@
#pragma once
extern IRehldsApi* g_RehldsApi;
extern IRehldsHookchains* g_RehldsHookchains;
extern IRehldsApi *g_RehldsApi;
extern const RehldsFuncs_t *g_RehldsFuncs;
extern IRehldsServerData *g_RehldsData;
extern IRehldsHookchains *g_RehldsHookchains;
extern IRehldsServerStatic *g_RehldsSvs;
extern bool RehldsApi_Init();
typedef struct packet_entities_s {

View File

@ -1,48 +1,63 @@
#include "precompiled.h"
IRehldsApi* g_RehldsApi;
IRehldsHookchains* g_RehldsHookchains;
IRehldsApi *g_RehldsApi;
const RehldsFuncs_t *g_RehldsFuncs;
IRehldsServerData *g_RehldsData;
IRehldsHookchains *g_RehldsHookchains;
IRehldsServerStatic *g_RehldsSvs;
bool RehldsApi_TryInit(CSysModule* engineModule, char* failureReason)
bool RehldsApi_Init()
{
if (!engineModule) {
SEM_PRINT("[%s]: ReHLDS Failed to locate engine module.", Plugin_info.logtag);
return false;
#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)
{
if (engineModule = Sys_GetModuleHandle(dllName))
break; // gotcha
}
#else
CSysModule *engineModule = Sys_GetModuleHandle("engine_i486.so");
#endif
if (!engineModule)
return false;
CreateInterfaceFn ifaceFactory = Sys_GetFactory(engineModule);
if (!ifaceFactory) {
SEM_PRINT("[%s]: ReHLDS Failed to locate interface factory in engine module.", Plugin_info.logtag);
if (!ifaceFactory)
return false;
}
int retCode = 0;
g_RehldsApi = (IRehldsApi*)ifaceFactory(VREHLDS_HLDS_API_VERSION, &retCode);
if (!g_RehldsApi) {
SEM_PRINT("[%s]: ReHLDS Failed to locate retrieve rehlds api interface from engine module, return code is %d.", Plugin_info.logtag, retCode);
if (!g_RehldsApi)
return false;
}
int majorVersion = g_RehldsApi->GetMajorVersion();
int minorVersion = g_RehldsApi->GetMinorVersion();
if (majorVersion != REHLDS_API_VERSION_MAJOR)
{
SEM_PRINT("[%s]: ReHLDS API major version mismatch; expected %d, real %d.", Plugin_info.logtag, REHLDS_API_VERSION_MAJOR, majorVersion);
SEM_PRINT("[%s]: ReHLDS API major version mismatch; expected %d, real %d\n", Plugin_info.logtag, REHLDS_API_VERSION_MAJOR, majorVersion);
// need to notify that it is necessary to update the ReHLDS.
if (majorVersion < REHLDS_API_VERSION_MAJOR)
{
SEM_PRINT("[%s]: Please update the ReHLDS up to a major version API >= %d.", Plugin_info.logtag, REHLDS_API_VERSION_MAJOR);
SEM_PRINT("[%s]: Please update the ReHLDS up to a major version API >= %d\n", Plugin_info.logtag, REHLDS_API_VERSION_MAJOR);
}
// need to notify that it is necessary to update the module.
else if (majorVersion > REHLDS_API_VERSION_MAJOR)
{
SEM_PRINT("[%s]: Please update the %s up to a major version API >= %d.", Plugin_info.logtag, Plugin_info.name, majorVersion);
SEM_PRINT("[%s]: Please update the %s up to a major version API >= %d\n", Plugin_info.logtag, Plugin_info.logtag, majorVersion);
}
return false;
@ -50,39 +65,15 @@ bool RehldsApi_TryInit(CSysModule* engineModule, char* failureReason)
if (minorVersion < REHLDS_API_VERSION_MINOR)
{
SEM_PRINT("[%s]: ReHLDS API minor version mismatch; expected at least %d, real %d.", Plugin_info.logtag, REHLDS_API_VERSION_MINOR, minorVersion);
SEM_PRINT("[%s]: Please update the ReHLDS up to a minor version API >= %d.", Plugin_info.logtag, REHLDS_API_VERSION_MINOR);
SEM_PRINT("[%s]: ReHLDS API minor version mismatch; expected at least %d, real %d\n", Plugin_info.logtag, REHLDS_API_VERSION_MINOR, minorVersion);
SEM_PRINT("[%s]: Please update the ReHLDS up to a minor version API >= %d\n", Plugin_info.logtag, REHLDS_API_VERSION_MINOR);
return false;
}
g_RehldsFuncs = g_RehldsApi->GetFuncs();
g_RehldsData = g_RehldsApi->GetServerData();
g_RehldsHookchains = g_RehldsApi->GetHookchains();
return true;
}
bool RehldsApi_Init()
{
char failReason[2048];
#ifdef WIN32
CSysModule* engineModule = Sys_LoadModule("swds.dll");
if (!RehldsApi_TryInit(engineModule, failReason))
{
engineModule = Sys_LoadModule("filesystem_stdio.dll");
if (!RehldsApi_TryInit(engineModule, failReason))
{
UTIL_LogPrintf("%s", failReason);
return false;
}
}
#else
CSysModule* engineModule = Sys_LoadModule("engine_i486.so");
if (!RehldsApi_TryInit(engineModule, failReason))
{
UTIL_LogPrintf("%s", failReason);
return false;
}
#endif
g_RehldsSvs = g_RehldsApi->GetServerStatic();
return true;
}