1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/loader/gamedll.cpp Sun Nov 23 21:21:49 2008 -0600
1.3 @@ -0,0 +1,436 @@
1.4 +#include <stdio.h>
1.5 +#include <ctype.h>
1.6 +#include <string.h>
1.7 +#include <assert.h>
1.8 +#include <stdlib.h>
1.9 +#include <stddef.h>
1.10 +#include "loader.h"
1.11 +#include <sh_memfuncinfo.h>
1.12 +#include <sh_memory.h>
1.13 +#include "utility.h"
1.14 +#include "gamedll.h"
1.15 +#include "valve_commandline.h"
1.16 +
1.17 +class IServerGameDLL;
1.18 +typedef ICommandLine *(*GetCommandLine)();
1.19 +
1.20 +#define MAX_GAMEDLL_PATHS 10
1.21 +
1.22 +IGameDllBridge* gamedll_bridge = NULL;
1.23 +static int game_info_detected = 0;
1.24 +static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
1.25 +static void *gamedll_libs[MAX_GAMEDLL_PATHS];
1.26 +static unsigned int gamedll_path_count = 0;
1.27 +static void *gamedll_lib = NULL;
1.28 +static IServerGameDLL *gamedll_iface = NULL;
1.29 +static QueryValveInterface gamedll_qvi = NULL;
1.30 +static int gamedll_version = 0;
1.31 +
1.32 +#if defined _WIN32
1.33 +#define TIER0_NAME "bin\\tier0.dll"
1.34 +#define VSTDLIB_NAME "bin\\vstdlib.dll"
1.35 +#define SERVER_NAME "server.dll"
1.36 +#elif defined __linux__
1.37 +#define TIER0_NAME "bin/tier0_i486.so"
1.38 +#define VSTDLIB_NAME "bin/vstdlib_i486.so"
1.39 +#define SERVER_NAME "server_i486.so"
1.40 +#endif
1.41 +
1.42 +static bool
1.43 +mm_DetectGameInformation()
1.44 +{
1.45 + void *lib;
1.46 + char error[255];
1.47 + GetCommandLine valve_cmdline;
1.48 + char mm_path[PLATFORM_MAX_PATH];
1.49 + char lib_path[PLATFORM_MAX_PATH];
1.50 + char game_name[PLATFORM_MAX_PATH];
1.51 + char game_path[PLATFORM_MAX_PATH];
1.52 +
1.53 + if (game_info_detected)
1.54 + return game_info_detected == 1 ? true : false;
1.55 +
1.56 + game_info_detected = -1;
1.57 +
1.58 + if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
1.59 + {
1.60 + mm_LogFatal("Could not find path for: " TIER0_NAME);
1.61 + return false;
1.62 + }
1.63 +
1.64 + if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
1.65 + {
1.66 + mm_LogFatal("Could not load %s: %s", lib_path, error);
1.67 + return false;
1.68 + }
1.69 +
1.70 + valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
1.71 + if (valve_cmdline == NULL)
1.72 + {
1.73 + /* We probably have a Ship engine. */
1.74 + mm_UnloadLibrary(lib);
1.75 + if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
1.76 + {
1.77 + mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
1.78 + return false;
1.79 + }
1.80 +
1.81 + if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
1.82 + {
1.83 + mm_LogFatal("Could not load %s: %s", lib_path, error);
1.84 + return false;
1.85 + }
1.86 +
1.87 + valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
1.88 + }
1.89 +
1.90 + mm_UnloadLibrary(lib);
1.91 +
1.92 + if (valve_cmdline == NULL)
1.93 + {
1.94 + mm_LogFatal("Could not locate any command line functionality");
1.95 + return false;
1.96 + }
1.97 +
1.98 + if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
1.99 + {
1.100 + mm_LogFatal("Could not locate metamod loader library path");
1.101 + return false;
1.102 + }
1.103 +
1.104 + mm_Format(game_name,
1.105 + sizeof(game_name),
1.106 + "%s",
1.107 + valve_cmdline()->ParmValue("-game", "hl2"));
1.108 + if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
1.109 + {
1.110 + mm_LogFatal("Could not resolve path: %s", game_name);
1.111 + return false;
1.112 + }
1.113 +
1.114 + FILE *fp;
1.115 + char gameinfo_path[PLATFORM_MAX_PATH];
1.116 +
1.117 + mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
1.118 + if ((fp = fopen(gameinfo_path, "rt")) == NULL)
1.119 + {
1.120 + mm_LogFatal("Could not read file: %s", gameinfo_path);
1.121 + return false;
1.122 + }
1.123 +
1.124 + char temp_path[PLATFORM_MAX_PATH];
1.125 + char cur_path[PLATFORM_MAX_PATH];
1.126 + getcwd(cur_path, sizeof(cur_path));
1.127 +
1.128 + char *ptr;
1.129 + const char *lptr;
1.130 + bool search = false;
1.131 + char buffer[255], key[128], val[128];
1.132 + while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
1.133 + {
1.134 + mm_TrimComments(buffer);
1.135 + mm_TrimLeft(buffer);
1.136 + mm_TrimRight(buffer);
1.137 +
1.138 + if (stricmp(buffer, "SearchPaths") == 0)
1.139 + search = true;
1.140 +
1.141 + if (!search)
1.142 + continue;
1.143 +
1.144 + mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
1.145 + if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
1.146 + continue;
1.147 +
1.148 + if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
1.149 + {
1.150 + ptr = &val[sizeof("|gameinfo_path|") - 1];
1.151 + if (ptr[0] == '.')
1.152 + ptr++;
1.153 + lptr = game_path;
1.154 + }
1.155 + else
1.156 + {
1.157 + ptr = val;
1.158 + lptr = cur_path;
1.159 + }
1.160 +
1.161 + if (stricmp(key, "GameBin") == 0)
1.162 + mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
1.163 + else if (!ptr[0])
1.164 + mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
1.165 + else
1.166 + mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
1.167 +
1.168 + if (mm_PathCmp(mm_path, temp_path))
1.169 + continue;
1.170 +
1.171 + FILE *exists = fopen(temp_path, "rb");
1.172 + if (!exists)
1.173 + continue;
1.174 + fclose(exists);
1.175 +
1.176 + /* exists is still non-NULL... use this as a flag */
1.177 + for (unsigned int i = 0; i < gamedll_path_count; i++)
1.178 + {
1.179 + if (mm_PathCmp(gamedll_paths[i], temp_path))
1.180 + {
1.181 + exists = NULL;
1.182 + break;
1.183 + }
1.184 + }
1.185 +
1.186 + if (!exists)
1.187 + continue;
1.188 +
1.189 + mm_Format(gamedll_paths[gamedll_path_count],
1.190 + PLATFORM_MAX_PATH,
1.191 + "%s",
1.192 + temp_path);
1.193 + gamedll_path_count++;
1.194 +
1.195 + if (gamedll_path_count == MAX_GAMEDLL_PATHS)
1.196 + break;
1.197 + }
1.198 + fclose(fp);
1.199 +
1.200 + game_info_detected = 1;
1.201 +
1.202 + return true;
1.203 +}
1.204 +
1.205 +static void
1.206 +mm_FreeCachedLibraries()
1.207 +{
1.208 + for (unsigned int i = 0; i < gamedll_path_count; i++)
1.209 + {
1.210 + if (gamedll_libs[i] == NULL)
1.211 + continue;
1.212 + mm_UnloadLibrary(gamedll_libs[i]);
1.213 + }
1.214 +}
1.215 +
1.216 +static void
1.217 +mm_PatchDllInit(bool patch);
1.218 +
1.219 +static void *isgd_orig_call = NULL;
1.220 +
1.221 +class VEmptyClass
1.222 +{
1.223 +};
1.224 +
1.225 +class IServerGameDLL
1.226 +{
1.227 +public:
1.228 + virtual bool DLLInit(QueryValveInterface engineFactory,
1.229 + QueryValveInterface physicsFactory,
1.230 + QueryValveInterface fileSystemFactory,
1.231 + void *pGlobals)
1.232 + {
1.233 + MetamodBackend backend = mm_DetermineBackend(engineFactory);
1.234 +
1.235 + char error[255];
1.236 + if (backend == MMBackend_UNKNOWN)
1.237 + {
1.238 + mm_LogFatal("Could not detect engine version");
1.239 + }
1.240 + else
1.241 + {
1.242 + if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
1.243 + {
1.244 + mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
1.245 + }
1.246 + else
1.247 + {
1.248 + typedef IGameDllBridge *(*GetGameDllBridge)();
1.249 + GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
1.250 + if (get_bridge == NULL)
1.251 + {
1.252 + mm_UnloadMetamodLibrary();
1.253 + mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
1.254 + }
1.255 + gamedll_bridge = get_bridge();
1.256 + }
1.257 + }
1.258 +
1.259 + if (gamedll_bridge)
1.260 + {
1.261 + gamedll_bridge_info info;
1.262 +
1.263 + info.engineFactory = (QueryValveInterface)engineFactory;
1.264 + info.physicsFactory = (QueryValveInterface)physicsFactory;
1.265 + info.fsFactory = (QueryValveInterface)fileSystemFactory;
1.266 + info.pGlobals = pGlobals;
1.267 + info.dllVersion = gamedll_version;
1.268 + info.isgd = gamedll_iface;
1.269 + info.gsFactory = gamedll_qvi;
1.270 +
1.271 + strcpy(error, "Unknown error");
1.272 + if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
1.273 + {
1.274 + gamedll_bridge = NULL;
1.275 + mm_UnloadMetamodLibrary();
1.276 + mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
1.277 + }
1.278 + }
1.279 +
1.280 + /* Call the original */
1.281 + bool result;
1.282 + {
1.283 + union
1.284 + {
1.285 + bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
1.286 + QueryValveInterface physicsFactory,
1.287 + QueryValveInterface fileSystemFactory,
1.288 + void *pGlobals);
1.289 +#if defined _WIN32
1.290 + void *addr;
1.291 + } u;
1.292 + u.addr = isgd_orig_call;
1.293 +#else
1.294 + struct
1.295 + {
1.296 + void *addr;
1.297 + intptr_t adjustor;
1.298 + } s;
1.299 + } u;
1.300 + u.s.addr = isgd_orig_call;
1.301 + u.s.adjustor = 0;
1.302 +#endif
1.303 + result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
1.304 + physicsFactory,
1.305 + fileSystemFactory,
1.306 + pGlobals);
1.307 + }
1.308 +
1.309 + /**
1.310 + * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
1.311 + * I'm pretty sure we'll die horribly.
1.312 + */
1.313 +
1.314 + if (!result)
1.315 + {
1.316 + gamedll_bridge->Unload();
1.317 + mm_UnloadMetamodLibrary();
1.318 + gamedll_bridge = NULL;
1.319 + }
1.320 + else if (gamedll_bridge != NULL)
1.321 + {
1.322 + gamedll_bridge->DLLInit_Post();
1.323 + }
1.324 +
1.325 + mm_PatchDllInit(false);
1.326 +
1.327 + return result;
1.328 + }
1.329 +};
1.330 +
1.331 +static IServerGameDLL isgd_thunk;
1.332 +
1.333 +static void
1.334 +mm_PatchDllInit(bool patch)
1.335 +{
1.336 + void **vtable_src;
1.337 + void **vtable_dest;
1.338 + SourceHook::MemFuncInfo mfp;
1.339 +
1.340 + SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
1.341 +
1.342 + assert(mfp.isVirtual);
1.343 + assert(mfp.thisptroffs == 0);
1.344 + assert(mfp.vtbloffs == 0);
1.345 +
1.346 + vtable_src = (void **)*(void **)&isgd_thunk;
1.347 + vtable_dest = (void **)*(void **)gamedll_iface;
1.348 +
1.349 + SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
1.350 + sizeof(void*),
1.351 + SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
1.352 +
1.353 + if (patch)
1.354 + {
1.355 + assert(isgd_orig_call == NULL);
1.356 + isgd_orig_call = vtable_dest[mfp.vtblindex];
1.357 + vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
1.358 + }
1.359 + else
1.360 + {
1.361 + assert(isgd_orig_call != NULL);
1.362 + vtable_dest[mfp.vtblindex] = isgd_orig_call;
1.363 + isgd_orig_call = NULL;
1.364 + }
1.365 +}
1.366 +
1.367 +static void
1.368 +mm_PrepForGameLoad()
1.369 +{
1.370 + mm_PatchDllInit(true);
1.371 +}
1.372 +
1.373 +void *
1.374 +mm_GameDllRequest(const char *name, int *ret)
1.375 +{
1.376 + if (gamedll_lib != NULL && gamedll_bridge == NULL)
1.377 + {
1.378 + mm_LogFatal("Requested unknown interface before game load: %s", name);
1.379 + return gamedll_qvi(name, ret);
1.380 + }
1.381 +
1.382 + if (strncmp(name, "ServerGameDLL", 13) == 0)
1.383 + {
1.384 + if (!mm_DetectGameInformation())
1.385 + {
1.386 + if (ret != NULL)
1.387 + *ret = 1;
1.388 + return NULL;
1.389 + }
1.390 +
1.391 + void *lib;
1.392 + char error[255];
1.393 + void *ptr = NULL;
1.394 + QueryValveInterface qvi;
1.395 + for (unsigned int i = 0; i < gamedll_path_count; i++)
1.396 + {
1.397 + if (gamedll_libs[i] == NULL)
1.398 + {
1.399 + lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
1.400 + if (lib == NULL)
1.401 + continue;
1.402 + gamedll_libs[i] = lib;
1.403 + }
1.404 + lib = gamedll_libs[i];
1.405 + qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
1.406 + if (qvi == NULL)
1.407 + continue;
1.408 + ptr = qvi(name, ret);
1.409 + if (ptr != NULL)
1.410 + {
1.411 + gamedll_libs[i] = NULL;
1.412 + break;
1.413 + }
1.414 + }
1.415 +
1.416 + if (ptr != NULL)
1.417 + {
1.418 + mm_FreeCachedLibraries();
1.419 + gamedll_lib = lib;
1.420 + gamedll_iface = (IServerGameDLL *)ptr;
1.421 + gamedll_qvi = qvi;
1.422 + gamedll_version = atoi(&name[13]);
1.423 + mm_PrepForGameLoad();
1.424 +
1.425 + if (ret != NULL)
1.426 + *ret = 0;
1.427 + return ptr;
1.428 + }
1.429 + }
1.430 + else if (game_info_detected == 0)
1.431 + {
1.432 + mm_LogFatal("Received interface request too early: %s", name);
1.433 + }
1.434 +
1.435 + if (ret != NULL)
1.436 + *ret = 1;
1.437 + return NULL;
1.438 +}
1.439 +