Updated changelog, added license headers.
3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
35 #include <sh_memfuncinfo.h>
36 #include <sh_memory.h>
39 #include "valve_commandline.h"
44 typedef ICommandLine *(*GetCommandLine)();
46 #define MAX_GAMEDLL_PATHS 10
48 IGameDllBridge* gamedll_bridge = NULL;
49 static int game_info_detected = 0;
50 static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
51 static void *gamedll_libs[MAX_GAMEDLL_PATHS];
52 static unsigned int gamedll_path_count = 0;
53 static void *gamedll_lib = NULL;
54 static IServerGameDLL *gamedll_iface = NULL;
55 static QueryValveInterface gamedll_qvi = NULL;
56 static int gamedll_version = 0;
57 static int isgd_shutdown_index = -1;
60 #define TIER0_NAME "bin\\tier0.dll"
61 #define VSTDLIB_NAME "bin\\vstdlib.dll"
62 #define SERVER_NAME "server.dll"
63 #elif defined __linux__
64 #define TIER0_NAME "bin/tier0_i486.so"
65 #define VSTDLIB_NAME "bin/vstdlib_i486.so"
66 #define SERVER_NAME "server_i486.so"
70 mm_DetectGameInformation()
74 GetCommandLine valve_cmdline;
75 char mm_path[PLATFORM_MAX_PATH];
76 char lib_path[PLATFORM_MAX_PATH];
77 char game_name[PLATFORM_MAX_PATH];
78 char game_path[PLATFORM_MAX_PATH];
80 if (game_info_detected)
81 return game_info_detected == 1 ? true : false;
83 game_info_detected = -1;
85 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
87 mm_LogFatal("Could not find path for: " TIER0_NAME);
91 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
93 mm_LogFatal("Could not load %s: %s", lib_path, error);
97 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
98 if (valve_cmdline == NULL)
100 /* We probably have a Ship engine. */
101 mm_UnloadLibrary(lib);
102 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
104 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
108 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
110 mm_LogFatal("Could not load %s: %s", lib_path, error);
114 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
117 mm_UnloadLibrary(lib);
119 if (valve_cmdline == NULL)
121 mm_LogFatal("Could not locate any command line functionality");
125 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
127 mm_LogFatal("Could not locate metamod loader library path");
134 valve_cmdline()->ParmValue("-game", "hl2"));
135 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
137 mm_LogFatal("Could not resolve path: %s", game_name);
142 char gameinfo_path[PLATFORM_MAX_PATH];
144 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
145 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
147 mm_LogFatal("Could not read file: %s", gameinfo_path);
151 char temp_path[PLATFORM_MAX_PATH];
152 char cur_path[PLATFORM_MAX_PATH];
153 getcwd(cur_path, sizeof(cur_path));
158 char buffer[255], key[128], val[128];
159 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
161 mm_TrimComments(buffer);
163 mm_TrimRight(buffer);
165 if (stricmp(buffer, "SearchPaths") == 0)
171 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
172 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
175 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
177 ptr = &val[sizeof("|gameinfo_path|") - 1];
188 if (stricmp(key, "GameBin") == 0)
189 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
191 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
193 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
195 if (mm_PathCmp(mm_path, temp_path))
198 FILE *exists = fopen(temp_path, "rb");
203 /* exists is still non-NULL... use this as a flag */
204 for (unsigned int i = 0; i < gamedll_path_count; i++)
206 if (mm_PathCmp(gamedll_paths[i], temp_path))
216 mm_Format(gamedll_paths[gamedll_path_count],
220 gamedll_path_count++;
222 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
227 game_info_detected = 1;
233 mm_FreeCachedLibraries()
235 for (unsigned int i = 0; i < gamedll_path_count; i++)
237 if (gamedll_libs[i] == NULL)
239 mm_UnloadLibrary(gamedll_libs[i]);
244 mm_PatchDllInit(bool patch);
247 mm_PatchDllShutdown();
249 static void *isgd_orig_init = NULL;
250 static void *isgd_orig_shutdown = NULL;
259 virtual bool DLLInit(QueryValveInterface engineFactory,
260 QueryValveInterface physicsFactory,
261 QueryValveInterface fileSystemFactory,
264 MetamodBackend backend = mm_DetermineBackend(engineFactory);
267 if (backend == MMBackend_UNKNOWN)
269 mm_LogFatal("Could not detect engine version");
273 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
275 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
279 typedef IGameDllBridge *(*GetGameDllBridge)();
280 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
281 if (get_bridge == NULL)
283 mm_UnloadMetamodLibrary();
284 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
288 gamedll_bridge = get_bridge();
295 gamedll_bridge_info info;
297 info.engineFactory = (QueryValveInterface)engineFactory;
298 info.physicsFactory = (QueryValveInterface)physicsFactory;
299 info.fsFactory = (QueryValveInterface)fileSystemFactory;
300 info.pGlobals = pGlobals;
301 info.dllVersion = gamedll_version;
302 info.isgd = gamedll_iface;
303 info.gsFactory = gamedll_qvi;
305 strcpy(error, "Unknown error");
306 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
308 gamedll_bridge = NULL;
309 mm_UnloadMetamodLibrary();
310 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
314 /* Call the original */
319 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
320 QueryValveInterface physicsFactory,
321 QueryValveInterface fileSystemFactory,
326 u.addr = isgd_orig_init;
334 u.s.addr = isgd_orig_init;
337 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
344 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
345 * I'm pretty sure we'll die horribly.
350 gamedll_bridge->Unload();
351 mm_UnloadMetamodLibrary();
352 gamedll_bridge = NULL;
354 else if (gamedll_bridge != NULL)
356 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
357 assert(isgd_shutdown_index != -1);
358 mm_PatchDllShutdown();
361 mm_PatchDllInit(false);
366 virtual void DLLShutdown()
368 gamedll_bridge->Unload();
369 gamedll_bridge = NULL;
370 mm_UnloadMetamodLibrary();
372 /* Call original function */
376 void (VEmptyClass::*mfpnew)();
380 u.addr = isgd_orig_shutdown;
388 u.s.addr = isgd_orig_shutdown;
391 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
394 mm_UnloadLibrary(gamedll_lib);
399 static IServerGameDLL isgd_thunk;
402 mm_PatchDllInit(bool patch)
406 SourceHook::MemFuncInfo mfp;
408 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
410 assert(mfp.isVirtual);
411 assert(mfp.thisptroffs == 0);
412 assert(mfp.vtbloffs == 0);
414 vtable_src = (void **)*(void **)&isgd_thunk;
415 vtable_dest = (void **)*(void **)gamedll_iface;
417 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
419 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
423 assert(isgd_orig_init == NULL);
424 isgd_orig_init = vtable_dest[mfp.vtblindex];
425 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
429 assert(isgd_orig_init != NULL);
430 vtable_dest[mfp.vtblindex] = isgd_orig_init;
431 isgd_orig_init = NULL;
436 mm_PatchDllShutdown()
440 SourceHook::MemFuncInfo mfp;
442 mfp.isVirtual = false;
443 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
444 assert(mfp.isVirtual);
445 assert(mfp.thisptroffs == 0);
446 assert(mfp.vtbloffs == 0);
448 vtable_src = (void **)*(void **)&isgd_thunk;
449 vtable_dest = (void **)*(void **)gamedll_iface;
451 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
452 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
458 mm_PatchDllInit(true);
462 mm_GameDllRequest(const char *name, int *ret)
464 if (gamedll_lib != NULL && gamedll_bridge == NULL)
466 return gamedll_qvi(name, ret);
469 if (strncmp(name, "ServerGameDLL", 13) == 0)
471 if (!mm_DetectGameInformation())
481 QueryValveInterface qvi;
482 for (unsigned int i = 0; i < gamedll_path_count; i++)
484 if (gamedll_libs[i] == NULL)
486 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
489 gamedll_libs[i] = lib;
491 lib = gamedll_libs[i];
492 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
495 ptr = qvi(name, ret);
498 gamedll_libs[i] = NULL;
505 mm_FreeCachedLibraries();
507 gamedll_iface = (IServerGameDLL *)ptr;
509 gamedll_version = atoi(&name[13]);
510 mm_PrepForGameLoad();
517 else if (game_info_detected == 0)
519 mm_LogFatal("Received interface request too early: %s", name);