2008-11-26 07:19:30 +01:00
|
|
|
/**
|
2009-11-23 06:54:55 +01:00
|
|
|
* vim: set ts=4 sw=4 tw=99 noet :
|
2008-11-26 07:19:30 +01:00
|
|
|
* ======================================================
|
|
|
|
* Metamod:Source
|
2015-07-09 20:09:56 +02:00
|
|
|
* Copyright (C) 2004-2015 AlliedModders LLC and authors.
|
2008-11-26 07:19:30 +01:00
|
|
|
* All rights reserved.
|
|
|
|
* ======================================================
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
* In no event will the authors be held liable for any damages arising from
|
|
|
|
* the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software in a
|
|
|
|
* product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
2008-11-24 04:21:49 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "loader.h"
|
|
|
|
#include <sh_memfuncinfo.h>
|
|
|
|
#include <sh_memory.h>
|
|
|
|
#include "utility.h"
|
|
|
|
#include "gamedll.h"
|
2008-11-24 05:10:16 +01:00
|
|
|
|
2008-11-24 04:21:49 +01:00
|
|
|
class IServerGameDLL;
|
2015-09-21 19:28:52 +02:00
|
|
|
class ISource2ServerConfig;
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
#define MAX_GAMEDLL_PATHS 10
|
|
|
|
|
|
|
|
IGameDllBridge* gamedll_bridge = NULL;
|
|
|
|
static int game_info_detected = 0;
|
2013-03-23 01:47:35 +01:00
|
|
|
static char game_name[128];
|
2008-11-24 04:21:49 +01:00
|
|
|
static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
|
|
|
|
static void *gamedll_libs[MAX_GAMEDLL_PATHS];
|
|
|
|
static unsigned int gamedll_path_count = 0;
|
|
|
|
static void *gamedll_lib = NULL;
|
|
|
|
static IServerGameDLL *gamedll_iface = NULL;
|
2015-09-21 19:28:52 +02:00
|
|
|
static ISource2ServerConfig *config_iface = NULL;
|
2008-11-24 04:21:49 +01:00
|
|
|
static QueryValveInterface gamedll_qvi = NULL;
|
2023-05-21 21:27:46 +02:00
|
|
|
static char gamedll_iface_name[128] = { 0 };
|
2008-11-24 04:21:49 +01:00
|
|
|
static int gamedll_version = 0;
|
2008-11-24 04:50:17 +01:00
|
|
|
static int isgd_shutdown_index = -1;
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-12-18 14:42:41 +01:00
|
|
|
static int is2sc_allowdedi_index = 21;
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2009-11-23 06:54:55 +01:00
|
|
|
static char mm_path[PLATFORM_MAX_PATH];
|
2015-07-09 20:09:56 +02:00
|
|
|
static bool g_is_source2 = false;
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
#if defined _WIN32
|
|
|
|
#define SERVER_NAME "server.dll"
|
2015-07-09 23:30:13 +02:00
|
|
|
#if defined _WIN64
|
|
|
|
#define PLATFORM_NAME "win64"
|
|
|
|
#else
|
|
|
|
#define PLATFORM_NAME "win32"
|
|
|
|
#endif
|
2010-05-14 07:34:56 +02:00
|
|
|
#elif defined __APPLE__
|
|
|
|
#define SERVER_NAME "server.dylib"
|
2015-07-09 23:30:13 +02:00
|
|
|
#if defined __amd64__
|
|
|
|
#define PLATFORM_NAME "osx64"
|
|
|
|
#else
|
|
|
|
#define PLATFORM_NAME "osx32"
|
|
|
|
#endif
|
2008-11-24 04:21:49 +01:00
|
|
|
#elif defined __linux__
|
2015-07-09 23:30:13 +02:00
|
|
|
#if defined __amd64__
|
2016-07-22 20:03:21 +02:00
|
|
|
// hackhack - source2 uses libserver as name on POSIX, but source1 x64 does not
|
|
|
|
// (but source1 x64 is also client-only right now so what-ev)
|
|
|
|
#define SERVER_NAME "libserver" LIB_SUFFIX
|
2015-07-09 23:30:13 +02:00
|
|
|
#define PLATFORM_NAME "linuxsteamrt64"
|
|
|
|
#else
|
2016-07-22 20:03:21 +02:00
|
|
|
#define SERVER_NAME "server" LIB_SUFFIX
|
2015-07-09 23:30:13 +02:00
|
|
|
#define PLATFORM_NAME "linuxsteamrt32"
|
|
|
|
#endif
|
2008-11-24 04:21:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool
|
|
|
|
mm_DetectGameInformation()
|
|
|
|
{
|
|
|
|
char game_path[PLATFORM_MAX_PATH];
|
|
|
|
|
|
|
|
if (game_info_detected)
|
|
|
|
return game_info_detected == 1 ? true : false;
|
|
|
|
|
|
|
|
game_info_detected = -1;
|
|
|
|
|
2013-10-24 19:24:22 +02:00
|
|
|
mm_GetGameName(game_name, sizeof(game_name));
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
|
|
|
|
{
|
2009-11-23 06:54:55 +01:00
|
|
|
mm_LogFatal("Could not locate Metamod loader library path");
|
2008-11-24 04:21:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (!mm_ResolvePath(game_name, game_path, sizeof(game_path), g_is_source2))
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
|
|
|
mm_LogFatal("Could not resolve path: %s", game_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
char gameinfo_path[PLATFORM_MAX_PATH];
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
bool is_source2 = false;
|
2008-11-24 04:21:49 +01:00
|
|
|
mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
|
|
|
|
if ((fp = fopen(gameinfo_path, "rt")) == NULL)
|
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
// Try Source2 gameinfo
|
|
|
|
mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.gi", game_path);
|
|
|
|
if ((fp = fopen(gameinfo_path, "rt")) == NULL)
|
|
|
|
{
|
|
|
|
mm_LogFatal("Could not read file: %s", gameinfo_path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_source2 = true;
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char temp_path[PLATFORM_MAX_PATH];
|
|
|
|
char cur_path[PLATFORM_MAX_PATH];
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
const char *lptr;
|
|
|
|
bool search = false;
|
|
|
|
char buffer[255], key[128], val[128];
|
|
|
|
while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
|
|
|
|
{
|
|
|
|
mm_TrimComments(buffer);
|
|
|
|
mm_TrimLeft(buffer);
|
|
|
|
mm_TrimRight(buffer);
|
|
|
|
|
|
|
|
if (stricmp(buffer, "SearchPaths") == 0)
|
|
|
|
search = true;
|
|
|
|
|
|
|
|
if (!search)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
|
|
|
|
if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
|
|
|
|
{
|
|
|
|
ptr = &val[sizeof("|gameinfo_path|") - 1];
|
|
|
|
if (ptr[0] == '.')
|
|
|
|
ptr++;
|
|
|
|
lptr = game_path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = val;
|
2018-07-28 07:03:33 +02:00
|
|
|
if (getcwd(cur_path, sizeof(cur_path)))
|
|
|
|
lptr = cur_path;
|
|
|
|
else
|
|
|
|
lptr = "";
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:04:02 +02:00
|
|
|
const char *pRelPath = is_source2 ? "../../" : "";
|
|
|
|
const char *pOSDir = is_source2 ? PLATFORM_NAME "/" : "";
|
2008-11-24 04:21:49 +01:00
|
|
|
if (stricmp(key, "GameBin") == 0)
|
2015-07-09 20:09:56 +02:00
|
|
|
mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s%s/%s" SERVER_NAME, lptr, pRelPath, ptr, pOSDir);
|
2008-11-24 04:21:49 +01:00
|
|
|
else if (!ptr[0])
|
2015-07-09 20:09:56 +02:00
|
|
|
mm_PathFormat(temp_path, sizeof(temp_path), "%s/%sbin/%s" SERVER_NAME, lptr, pRelPath, pOSDir);
|
2008-11-24 04:21:49 +01:00
|
|
|
else
|
2015-07-09 20:09:56 +02:00
|
|
|
mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s%s/bin/%s" SERVER_NAME, lptr, pRelPath, ptr, pOSDir);
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
if (mm_PathCmp(mm_path, temp_path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FILE *exists = fopen(temp_path, "rb");
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
fclose(exists);
|
|
|
|
|
|
|
|
/* exists is still non-NULL... use this as a flag */
|
|
|
|
for (unsigned int i = 0; i < gamedll_path_count; i++)
|
|
|
|
{
|
|
|
|
if (mm_PathCmp(gamedll_paths[i], temp_path))
|
|
|
|
{
|
|
|
|
exists = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mm_Format(gamedll_paths[gamedll_path_count],
|
|
|
|
PLATFORM_MAX_PATH,
|
|
|
|
"%s",
|
|
|
|
temp_path);
|
|
|
|
gamedll_path_count++;
|
|
|
|
|
|
|
|
if (gamedll_path_count == MAX_GAMEDLL_PATHS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
game_info_detected = 1;
|
|
|
|
|
2009-11-16 04:52:54 +01:00
|
|
|
if (gamedll_path_count == 0)
|
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
mm_LogFatal("Could not detect any valid game paths in gameinfo file");
|
2009-11-16 04:52:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-24 04:21:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mm_FreeCachedLibraries()
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < gamedll_path_count; i++)
|
|
|
|
{
|
|
|
|
if (gamedll_libs[i] == NULL)
|
|
|
|
continue;
|
|
|
|
mm_UnloadLibrary(gamedll_libs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mm_PatchDllInit(bool patch);
|
|
|
|
|
2008-11-24 04:50:17 +01:00
|
|
|
static void
|
|
|
|
mm_PatchDllShutdown();
|
|
|
|
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-09-21 19:29:01 +02:00
|
|
|
static void
|
|
|
|
mm_PatchAllowDedicated(bool patch);
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2015-09-21 19:29:01 +02:00
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
static void
|
|
|
|
mm_PatchConnect(bool patch);
|
|
|
|
|
2008-11-24 04:50:17 +01:00
|
|
|
static void *isgd_orig_init = NULL;
|
|
|
|
static void *isgd_orig_shutdown = NULL;
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-09-21 19:29:01 +02:00
|
|
|
static void *is2sc_orig_allowdedi = NULL;
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2015-09-21 19:28:52 +02:00
|
|
|
static void *is2sc_orig_connect = NULL;
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
class VEmptyClass
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
gamedll_bridge_info g_bridge_info;
|
|
|
|
|
|
|
|
// Source2 - Rough start order
|
2015-09-21 19:29:01 +02:00
|
|
|
// CreateInterfaceFn (IS2SC) - hook Connect and AllowDedicatedServer
|
2015-07-09 20:09:56 +02:00
|
|
|
// IS2SC::Connect - save factory pointer. return orig. remove hook.
|
2015-09-21 19:29:01 +02:00
|
|
|
// IS2SC::AllowDedicatedServer - return true. remove hook.
|
2015-07-09 20:09:56 +02:00
|
|
|
// CreateInterfaceFn (IS2S) - hook Init and Shutdown
|
|
|
|
// IS2S::Init - do same as old ISGD::DLLInit, including core load. return orig. remove hook.
|
|
|
|
// IS2S::Shutdown - <-- this
|
|
|
|
|
|
|
|
enum InitReturnVal_t
|
|
|
|
{
|
|
|
|
INIT_FAILED = 0,
|
|
|
|
INIT_OK,
|
|
|
|
|
|
|
|
INIT_LAST_VAL,
|
|
|
|
};
|
|
|
|
|
2015-09-21 19:28:52 +02:00
|
|
|
class ISource2ServerConfig
|
2015-07-09 20:09:56 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-09-21 19:28:52 +02:00
|
|
|
virtual bool Connect(QueryValveInterface factory)
|
2015-07-09 20:09:56 +02:00
|
|
|
{
|
|
|
|
g_bridge_info.engineFactory = factory;
|
|
|
|
g_bridge_info.fsFactory = factory;
|
|
|
|
g_bridge_info.physicsFactory = factory;
|
|
|
|
|
|
|
|
|
|
|
|
/* Call the original */
|
|
|
|
bool result;
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
bool(VEmptyClass::*mfpnew)(QueryValveInterface factory);
|
|
|
|
#if defined _WIN32
|
|
|
|
void *addr;
|
|
|
|
} u;
|
2015-09-21 19:28:52 +02:00
|
|
|
u.addr = is2sc_orig_connect;
|
2015-07-09 20:09:56 +02:00
|
|
|
#else
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
intptr_t adjustor;
|
|
|
|
} s;
|
|
|
|
} u;
|
2015-09-21 19:28:52 +02:00
|
|
|
u.s.addr = is2sc_orig_connect;
|
2015-07-09 20:09:56 +02:00
|
|
|
u.s.adjustor = 0;
|
|
|
|
#endif
|
2015-09-21 19:28:52 +02:00
|
|
|
result = (((VEmptyClass *) config_iface)->*u.mfpnew)(factory);
|
|
|
|
}
|
2015-07-09 20:09:56 +02:00
|
|
|
|
|
|
|
mm_PatchConnect(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-09-21 19:29:01 +02:00
|
|
|
virtual bool AllowDedicatedServers(int universe) const
|
|
|
|
{
|
|
|
|
mm_PatchAllowDedicated(false);
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2015-09-21 19:28:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ISource2Server
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool Connect(QueryValveInterface factory) { return true; }
|
2015-07-09 20:09:56 +02:00
|
|
|
virtual void Disconnect() {}
|
|
|
|
virtual void *QueryInterface(const char *pInterfaceName) { return nullptr; }
|
|
|
|
|
|
|
|
virtual InitReturnVal_t Init()
|
|
|
|
{
|
2023-05-21 18:07:05 +02:00
|
|
|
if (!stricmp("csgo", game_name))
|
|
|
|
{
|
|
|
|
mm_backend = MMBackend_CS2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mm_backend = MMBackend_DOTA;
|
|
|
|
}
|
2015-07-09 20:27:18 +02:00
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
char error[255];
|
2015-07-09 20:27:18 +02:00
|
|
|
if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
|
2015-07-09 20:09:56 +02:00
|
|
|
{
|
2015-07-09 20:27:18 +02:00
|
|
|
mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
|
2015-07-09 20:09:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-09 20:27:18 +02:00
|
|
|
typedef IGameDllBridge *(*GetGameDllBridge)();
|
|
|
|
GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
|
|
|
|
if (get_bridge == NULL)
|
2015-07-09 20:09:56 +02:00
|
|
|
{
|
2015-07-09 20:27:18 +02:00
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", mm_backend);
|
2015-07-09 20:09:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-09 20:27:18 +02:00
|
|
|
gamedll_bridge = get_bridge();
|
2015-07-09 20:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gamedll_bridge)
|
|
|
|
{
|
|
|
|
g_bridge_info.pGlobals = nullptr;// pGlobals;
|
|
|
|
g_bridge_info.dllVersion = gamedll_version;
|
2023-05-21 21:27:46 +02:00
|
|
|
g_bridge_info.dllInterfaceName = gamedll_iface_name;
|
2015-07-09 20:09:56 +02:00
|
|
|
g_bridge_info.isgd = gamedll_iface;
|
|
|
|
g_bridge_info.gsFactory = gamedll_qvi;
|
|
|
|
g_bridge_info.vsp_listener_path = mm_path;
|
|
|
|
|
|
|
|
strcpy(error, "Unknown error");
|
|
|
|
if (!gamedll_bridge->DLLInit_Pre(&g_bridge_info, error, sizeof(error)))
|
|
|
|
{
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the original */
|
|
|
|
InitReturnVal_t result;
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
InitReturnVal_t(VEmptyClass::*mfpnew)();
|
|
|
|
#if defined _WIN32
|
|
|
|
void *addr;
|
|
|
|
} u;
|
|
|
|
u.addr = isgd_orig_init;
|
|
|
|
#else
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
intptr_t adjustor;
|
|
|
|
} s;
|
|
|
|
} u;
|
|
|
|
u.s.addr = isgd_orig_init;
|
|
|
|
u.s.adjustor = 0;
|
|
|
|
#endif
|
|
|
|
result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
|
|
|
|
* I'm pretty sure we'll die horribly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
gamedll_bridge->Unload();
|
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
}
|
|
|
|
else if (gamedll_bridge != NULL)
|
|
|
|
{
|
|
|
|
gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
|
|
|
|
assert(isgd_shutdown_index != -1);
|
|
|
|
mm_PatchDllShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_PatchDllInit(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Shutdown()
|
|
|
|
{
|
|
|
|
gamedll_bridge->Unload();
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
|
|
|
|
/* Call original function */
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
void (VEmptyClass::*mfpnew)();
|
|
|
|
#if defined _WIN32
|
|
|
|
void *addr;
|
|
|
|
} u;
|
|
|
|
u.addr = isgd_orig_shutdown;
|
|
|
|
#else
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
intptr_t adjustor;
|
|
|
|
} s;
|
|
|
|
} u;
|
|
|
|
u.s.addr = isgd_orig_shutdown;
|
|
|
|
u.s.adjustor = 0;
|
|
|
|
#endif
|
|
|
|
(((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_UnloadLibrary(gamedll_lib);
|
|
|
|
gamedll_lib = NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-24 04:21:49 +01:00
|
|
|
class IServerGameDLL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool DLLInit(QueryValveInterface engineFactory,
|
|
|
|
QueryValveInterface physicsFactory,
|
|
|
|
QueryValveInterface fileSystemFactory,
|
|
|
|
void *pGlobals)
|
|
|
|
{
|
2023-05-21 18:07:05 +02:00
|
|
|
mm_backend = mm_DetermineBackendS1(engineFactory, gamedll_qvi, game_name);
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
char error[255];
|
2009-11-23 06:54:55 +01:00
|
|
|
if (mm_backend == MMBackend_UNKNOWN)
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
|
|
|
mm_LogFatal("Could not detect engine version");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-23 06:54:55 +01:00
|
|
|
if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
2009-11-23 06:54:55 +01:00
|
|
|
mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typedef IGameDllBridge *(*GetGameDllBridge)();
|
|
|
|
GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
|
|
|
|
if (get_bridge == NULL)
|
|
|
|
{
|
|
|
|
mm_UnloadMetamodLibrary();
|
2009-11-23 06:54:55 +01:00
|
|
|
mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", mm_backend);
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
2008-11-24 05:36:06 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gamedll_bridge = get_bridge();
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gamedll_bridge)
|
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
g_bridge_info.engineFactory = (QueryValveInterface)engineFactory;
|
|
|
|
g_bridge_info.physicsFactory = (QueryValveInterface)physicsFactory;
|
|
|
|
g_bridge_info.fsFactory = (QueryValveInterface)fileSystemFactory;
|
|
|
|
g_bridge_info.pGlobals = pGlobals;
|
|
|
|
g_bridge_info.dllVersion = gamedll_version;
|
2023-05-21 21:27:46 +02:00
|
|
|
g_bridge_info.dllInterfaceName = gamedll_iface_name;
|
2015-07-09 20:09:56 +02:00
|
|
|
g_bridge_info.isgd = gamedll_iface;
|
|
|
|
g_bridge_info.gsFactory = gamedll_qvi;
|
|
|
|
g_bridge_info.vsp_listener_path = mm_path;
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
strcpy(error, "Unknown error");
|
2015-07-09 20:09:56 +02:00
|
|
|
if (!gamedll_bridge->DLLInit_Pre(&g_bridge_info, error, sizeof(error)))
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
mm_UnloadMetamodLibrary();
|
2009-11-23 06:54:55 +01:00
|
|
|
mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the original */
|
|
|
|
bool result;
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
|
|
|
|
QueryValveInterface physicsFactory,
|
|
|
|
QueryValveInterface fileSystemFactory,
|
|
|
|
void *pGlobals);
|
|
|
|
#if defined _WIN32
|
|
|
|
void *addr;
|
|
|
|
} u;
|
2008-11-24 04:50:17 +01:00
|
|
|
u.addr = isgd_orig_init;
|
2008-11-24 04:21:49 +01:00
|
|
|
#else
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
intptr_t adjustor;
|
|
|
|
} s;
|
|
|
|
} u;
|
2008-11-24 04:50:17 +01:00
|
|
|
u.s.addr = isgd_orig_init;
|
2008-11-24 04:21:49 +01:00
|
|
|
u.s.adjustor = 0;
|
|
|
|
#endif
|
|
|
|
result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
|
|
|
|
physicsFactory,
|
|
|
|
fileSystemFactory,
|
|
|
|
pGlobals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
|
|
|
|
* I'm pretty sure we'll die horribly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
gamedll_bridge->Unload();
|
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
}
|
|
|
|
else if (gamedll_bridge != NULL)
|
|
|
|
{
|
2008-11-24 04:50:17 +01:00
|
|
|
gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
|
|
|
|
assert(isgd_shutdown_index != -1);
|
|
|
|
mm_PatchDllShutdown();
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mm_PatchDllInit(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2008-11-24 04:50:17 +01:00
|
|
|
|
|
|
|
virtual void DLLShutdown()
|
|
|
|
{
|
|
|
|
gamedll_bridge->Unload();
|
|
|
|
gamedll_bridge = NULL;
|
|
|
|
mm_UnloadMetamodLibrary();
|
|
|
|
|
|
|
|
/* Call original function */
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
void (VEmptyClass::*mfpnew)();
|
|
|
|
#if defined _WIN32
|
|
|
|
void *addr;
|
|
|
|
} u;
|
|
|
|
u.addr = isgd_orig_shutdown;
|
|
|
|
#else
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
intptr_t adjustor;
|
|
|
|
} s;
|
|
|
|
} u;
|
|
|
|
u.s.addr = isgd_orig_shutdown;
|
|
|
|
u.s.adjustor = 0;
|
|
|
|
#endif
|
|
|
|
(((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_UnloadLibrary(gamedll_lib);
|
|
|
|
gamedll_lib = NULL;
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static IServerGameDLL isgd_thunk;
|
2015-07-09 20:09:56 +02:00
|
|
|
static ISource2Server is2s_thunk;
|
2015-09-21 19:28:52 +02:00
|
|
|
static ISource2ServerConfig is2sc_thunk;
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
mm_PatchDllInit(bool patch)
|
|
|
|
{
|
|
|
|
void **vtable_src;
|
|
|
|
void **vtable_dest;
|
|
|
|
SourceHook::MemFuncInfo mfp;
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (g_is_source2)
|
|
|
|
{
|
|
|
|
SourceHook::GetFuncInfo(&ISource2Server::Init, mfp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
assert(mfp.isVirtual);
|
|
|
|
assert(mfp.thisptroffs == 0);
|
|
|
|
assert(mfp.vtbloffs == 0);
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (g_is_source2)
|
|
|
|
{
|
|
|
|
vtable_src = (void **)*(void **)&is2s_thunk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vtable_src = (void **)*(void **)&isgd_thunk;
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
vtable_dest = (void **)*(void **)gamedll_iface;
|
|
|
|
|
|
|
|
SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
|
|
|
|
sizeof(void*),
|
|
|
|
SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
|
|
|
|
|
|
|
|
if (patch)
|
|
|
|
{
|
2008-11-24 04:50:17 +01:00
|
|
|
assert(isgd_orig_init == NULL);
|
|
|
|
isgd_orig_init = vtable_dest[mfp.vtblindex];
|
2008-11-24 04:21:49 +01:00
|
|
|
vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-24 04:50:17 +01:00
|
|
|
assert(isgd_orig_init != NULL);
|
|
|
|
vtable_dest[mfp.vtblindex] = isgd_orig_init;
|
|
|
|
isgd_orig_init = NULL;
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 04:50:17 +01:00
|
|
|
static void
|
|
|
|
mm_PatchDllShutdown()
|
|
|
|
{
|
|
|
|
void **vtable_src;
|
|
|
|
void **vtable_dest;
|
|
|
|
SourceHook::MemFuncInfo mfp;
|
|
|
|
|
|
|
|
mfp.isVirtual = false;
|
2015-07-09 20:09:56 +02:00
|
|
|
if (g_is_source2)
|
|
|
|
{
|
|
|
|
SourceHook::GetFuncInfo(&ISource2Server::Shutdown, mfp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
|
|
|
|
}
|
2008-11-24 04:50:17 +01:00
|
|
|
assert(mfp.isVirtual);
|
|
|
|
assert(mfp.thisptroffs == 0);
|
|
|
|
assert(mfp.vtbloffs == 0);
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (g_is_source2)
|
|
|
|
{
|
|
|
|
vtable_src = (void **)*(void **)&is2s_thunk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vtable_src = (void **)*(void **)&isgd_thunk;
|
|
|
|
}
|
2008-11-24 04:50:17 +01:00
|
|
|
vtable_dest = (void **)*(void **)gamedll_iface;
|
|
|
|
|
|
|
|
isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
|
|
|
|
vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-09-21 19:29:01 +02:00
|
|
|
static void
|
|
|
|
mm_PatchAllowDedicated(bool patch)
|
|
|
|
{
|
|
|
|
void **vtable_src;
|
|
|
|
void **vtable_dest;
|
|
|
|
SourceHook::MemFuncInfo mfp;
|
|
|
|
|
|
|
|
SourceHook::GetFuncInfo(&ISource2ServerConfig::AllowDedicatedServers, mfp);
|
|
|
|
|
|
|
|
assert(mfp.isVirtual);
|
|
|
|
assert(mfp.thisptroffs == 0);
|
|
|
|
assert(mfp.vtbloffs == 0);
|
|
|
|
|
|
|
|
vtable_src = (void **) *(void **) &is2sc_thunk;
|
|
|
|
vtable_dest = (void **) *(void **) config_iface;
|
|
|
|
|
|
|
|
SourceHook::SetMemAccess(&vtable_dest[is2sc_allowdedi_index],
|
|
|
|
sizeof(void*),
|
|
|
|
SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC);
|
|
|
|
|
|
|
|
if (patch)
|
|
|
|
{
|
|
|
|
assert(is2sc_orig_allowdedi == NULL);
|
|
|
|
is2sc_orig_allowdedi = vtable_dest[is2sc_allowdedi_index];
|
|
|
|
vtable_dest[is2sc_allowdedi_index] = vtable_src[mfp.vtblindex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(is2sc_orig_allowdedi != NULL);
|
|
|
|
vtable_dest[is2sc_allowdedi_index] = is2sc_orig_allowdedi;
|
|
|
|
is2sc_orig_allowdedi = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2015-09-21 19:29:01 +02:00
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
static void
|
|
|
|
mm_PatchConnect(bool patch)
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
void **vtable_src;
|
|
|
|
void **vtable_dest;
|
|
|
|
SourceHook::MemFuncInfo mfp;
|
|
|
|
|
2015-09-21 19:28:52 +02:00
|
|
|
SourceHook::GetFuncInfo(&ISource2ServerConfig::Connect, mfp);
|
2015-07-09 20:09:56 +02:00
|
|
|
|
|
|
|
assert(mfp.isVirtual);
|
|
|
|
assert(mfp.thisptroffs == 0);
|
|
|
|
assert(mfp.vtbloffs == 0);
|
|
|
|
|
2015-09-21 19:28:52 +02:00
|
|
|
vtable_src = (void **) *(void **) &is2sc_thunk;
|
|
|
|
vtable_dest = (void **) *(void **) config_iface;
|
2015-07-09 20:09:56 +02:00
|
|
|
|
|
|
|
SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
|
|
|
|
sizeof(void*),
|
|
|
|
SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC);
|
|
|
|
|
|
|
|
if (patch)
|
|
|
|
{
|
2015-09-21 19:28:52 +02:00
|
|
|
assert(is2sc_orig_connect == NULL);
|
|
|
|
is2sc_orig_connect = vtable_dest[mfp.vtblindex];
|
2015-07-09 20:09:56 +02:00
|
|
|
vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-21 19:28:52 +02:00
|
|
|
assert(is2sc_orig_connect != NULL);
|
|
|
|
vtable_dest[mfp.vtblindex] = is2sc_orig_connect;
|
|
|
|
is2sc_orig_connect = NULL;
|
2015-07-09 20:09:56 +02:00
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
|
2008-11-24 04:21:49 +01:00
|
|
|
void *
|
|
|
|
mm_GameDllRequest(const char *name, int *ret)
|
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
if (strncmp(name, "Source2ServerConfig", 19) == 0)
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
2015-07-09 20:09:56 +02:00
|
|
|
g_is_source2 = true;
|
|
|
|
if (!mm_DetectGameInformation())
|
|
|
|
{
|
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *lib;
|
|
|
|
char error[255];
|
|
|
|
void *ptr = NULL;
|
|
|
|
QueryValveInterface qvi;
|
|
|
|
for (unsigned int i = 0; i < gamedll_path_count; i++)
|
|
|
|
{
|
|
|
|
if (gamedll_libs[i] == NULL)
|
|
|
|
{
|
|
|
|
lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
|
|
|
|
if (lib == NULL)
|
|
|
|
continue;
|
|
|
|
gamedll_libs[i] = lib;
|
|
|
|
}
|
|
|
|
lib = gamedll_libs[i];
|
|
|
|
qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
|
|
|
|
if (qvi == NULL)
|
|
|
|
continue;
|
|
|
|
ptr = qvi(name, ret);
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
gamedll_libs[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
mm_FreeCachedLibraries();
|
|
|
|
gamedll_lib = lib;
|
2015-09-21 19:28:52 +02:00
|
|
|
config_iface = (ISource2ServerConfig *) ptr;
|
2015-07-09 20:09:56 +02:00
|
|
|
gamedll_qvi = qvi;
|
|
|
|
|
2015-09-21 19:28:52 +02:00
|
|
|
mm_PatchConnect(true);
|
2016-07-22 20:02:37 +02:00
|
|
|
#if defined _WIN32
|
2015-09-21 19:29:01 +02:00
|
|
|
mm_PatchAllowDedicated(true);
|
2016-07-22 20:02:37 +02:00
|
|
|
#endif
|
2015-09-21 19:28:52 +02:00
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 0;
|
|
|
|
return ptr;
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
}
|
2023-05-21 21:27:46 +02:00
|
|
|
else if (strncmp(name, "Source2Server", 13) == 0 && atoi(&name[13]) != 0)
|
2015-07-09 20:09:56 +02:00
|
|
|
{
|
|
|
|
gamedll_iface = (IServerGameDLL *)gamedll_qvi(name, ret);
|
2023-05-21 21:27:46 +02:00
|
|
|
strncpy(gamedll_iface_name, name, sizeof(gamedll_iface_name));
|
2015-07-09 20:09:56 +02:00
|
|
|
gamedll_version = atoi(&name[13]);
|
|
|
|
mm_PatchDllInit(true);
|
2008-11-24 04:21:49 +01:00
|
|
|
|
2015-07-09 20:09:56 +02:00
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 0;
|
|
|
|
return gamedll_iface;
|
|
|
|
}
|
|
|
|
else if (strncmp(name, "ServerGameDLL", 13) == 0)
|
2008-11-24 04:21:49 +01:00
|
|
|
{
|
|
|
|
if (!mm_DetectGameInformation())
|
|
|
|
{
|
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *lib;
|
|
|
|
char error[255];
|
|
|
|
void *ptr = NULL;
|
|
|
|
QueryValveInterface qvi;
|
|
|
|
for (unsigned int i = 0; i < gamedll_path_count; i++)
|
|
|
|
{
|
|
|
|
if (gamedll_libs[i] == NULL)
|
|
|
|
{
|
|
|
|
lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
|
|
|
|
if (lib == NULL)
|
|
|
|
continue;
|
|
|
|
gamedll_libs[i] = lib;
|
|
|
|
}
|
|
|
|
lib = gamedll_libs[i];
|
|
|
|
qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
|
|
|
|
if (qvi == NULL)
|
|
|
|
continue;
|
|
|
|
ptr = qvi(name, ret);
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
gamedll_libs[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
mm_FreeCachedLibraries();
|
|
|
|
gamedll_lib = lib;
|
|
|
|
gamedll_iface = (IServerGameDLL *)ptr;
|
2023-05-21 21:27:46 +02:00
|
|
|
strncpy(gamedll_iface_name, name, sizeof(gamedll_iface_name));
|
2008-11-24 04:21:49 +01:00
|
|
|
gamedll_qvi = qvi;
|
|
|
|
gamedll_version = atoi(&name[13]);
|
2015-07-09 20:09:56 +02:00
|
|
|
mm_PatchDllInit(true);
|
2008-11-24 04:21:49 +01:00
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 0;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 20:09:56 +02:00
|
|
|
else if (gamedll_lib != NULL && gamedll_bridge == NULL)
|
|
|
|
{
|
|
|
|
return gamedll_qvi(name, ret);
|
|
|
|
}
|
2008-11-24 04:21:49 +01:00
|
|
|
else if (game_info_detected == 0)
|
|
|
|
{
|
|
|
|
mm_LogFatal("Received interface request too early: %s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|