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>
42 #define MAX_GAMEDLL_PATHS 10
44 IGameDllBridge* gamedll_bridge = NULL;
45 static int game_info_detected = 0;
46 static const char *game_name = NULL;
47 static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
48 static void *gamedll_libs[MAX_GAMEDLL_PATHS];
49 static unsigned int gamedll_path_count = 0;
50 static void *gamedll_lib = NULL;
51 static IServerGameDLL *gamedll_iface = NULL;
52 static QueryValveInterface gamedll_qvi = NULL;
53 static int gamedll_version = 0;
54 static int isgd_shutdown_index = -1;
57 #define SERVER_NAME "server.dll"
58 #elif defined __linux__
59 #define SERVER_NAME "server" BINARY_SUFFIX
63 mm_DetectGameInformation()
65 char mm_path[PLATFORM_MAX_PATH];
66 char game_path[PLATFORM_MAX_PATH];
68 if (game_info_detected)
69 return game_info_detected == 1 ? true : false;
71 game_info_detected = -1;
73 if ((game_name = mm_GetGameName()) == NULL)
78 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
80 mm_LogFatal("Could not locate metamod loader library path");
84 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
86 mm_LogFatal("Could not resolve path: %s", game_name);
91 char gameinfo_path[PLATFORM_MAX_PATH];
93 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
94 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
96 mm_LogFatal("Could not read file: %s", gameinfo_path);
100 char temp_path[PLATFORM_MAX_PATH];
101 char cur_path[PLATFORM_MAX_PATH];
102 getcwd(cur_path, sizeof(cur_path));
107 char buffer[255], key[128], val[128];
108 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
110 mm_TrimComments(buffer);
112 mm_TrimRight(buffer);
114 if (stricmp(buffer, "SearchPaths") == 0)
120 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
121 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
124 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
126 ptr = &val[sizeof("|gameinfo_path|") - 1];
137 if (stricmp(key, "GameBin") == 0)
138 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
140 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
142 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
144 if (mm_PathCmp(mm_path, temp_path))
147 FILE *exists = fopen(temp_path, "rb");
152 /* exists is still non-NULL... use this as a flag */
153 for (unsigned int i = 0; i < gamedll_path_count; i++)
155 if (mm_PathCmp(gamedll_paths[i], temp_path))
165 mm_Format(gamedll_paths[gamedll_path_count],
169 gamedll_path_count++;
171 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
176 game_info_detected = 1;
178 if (gamedll_path_count == 0)
180 mm_LogFatal("Could not detect any valid game paths in gameinfo.txt");
188 mm_FreeCachedLibraries()
190 for (unsigned int i = 0; i < gamedll_path_count; i++)
192 if (gamedll_libs[i] == NULL)
194 mm_UnloadLibrary(gamedll_libs[i]);
199 mm_PatchDllInit(bool patch);
202 mm_PatchDllShutdown();
204 static void *isgd_orig_init = NULL;
205 static void *isgd_orig_shutdown = NULL;
214 virtual bool DLLInit(QueryValveInterface engineFactory,
215 QueryValveInterface physicsFactory,
216 QueryValveInterface fileSystemFactory,
219 MetamodBackend backend = mm_DetermineBackend(engineFactory, game_name);
222 if (backend == MMBackend_UNKNOWN)
224 mm_LogFatal("Could not detect engine version");
228 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
230 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
234 typedef IGameDllBridge *(*GetGameDllBridge)();
235 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
236 if (get_bridge == NULL)
238 mm_UnloadMetamodLibrary();
239 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
243 gamedll_bridge = get_bridge();
250 gamedll_bridge_info info;
252 info.engineFactory = (QueryValveInterface)engineFactory;
253 info.physicsFactory = (QueryValveInterface)physicsFactory;
254 info.fsFactory = (QueryValveInterface)fileSystemFactory;
255 info.pGlobals = pGlobals;
256 info.dllVersion = gamedll_version;
257 info.isgd = gamedll_iface;
258 info.gsFactory = gamedll_qvi;
260 strcpy(error, "Unknown error");
261 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
263 gamedll_bridge = NULL;
264 mm_UnloadMetamodLibrary();
265 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
269 /* Call the original */
274 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
275 QueryValveInterface physicsFactory,
276 QueryValveInterface fileSystemFactory,
281 u.addr = isgd_orig_init;
289 u.s.addr = isgd_orig_init;
292 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
299 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
300 * I'm pretty sure we'll die horribly.
305 gamedll_bridge->Unload();
306 mm_UnloadMetamodLibrary();
307 gamedll_bridge = NULL;
309 else if (gamedll_bridge != NULL)
311 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
312 assert(isgd_shutdown_index != -1);
313 mm_PatchDllShutdown();
316 mm_PatchDllInit(false);
321 virtual void DLLShutdown()
323 gamedll_bridge->Unload();
324 gamedll_bridge = NULL;
325 mm_UnloadMetamodLibrary();
327 /* Call original function */
331 void (VEmptyClass::*mfpnew)();
335 u.addr = isgd_orig_shutdown;
343 u.s.addr = isgd_orig_shutdown;
346 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
349 mm_UnloadLibrary(gamedll_lib);
354 static IServerGameDLL isgd_thunk;
357 mm_PatchDllInit(bool patch)
361 SourceHook::MemFuncInfo mfp;
363 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
365 assert(mfp.isVirtual);
366 assert(mfp.thisptroffs == 0);
367 assert(mfp.vtbloffs == 0);
369 vtable_src = (void **)*(void **)&isgd_thunk;
370 vtable_dest = (void **)*(void **)gamedll_iface;
372 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
374 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
378 assert(isgd_orig_init == NULL);
379 isgd_orig_init = vtable_dest[mfp.vtblindex];
380 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
384 assert(isgd_orig_init != NULL);
385 vtable_dest[mfp.vtblindex] = isgd_orig_init;
386 isgd_orig_init = NULL;
391 mm_PatchDllShutdown()
395 SourceHook::MemFuncInfo mfp;
397 mfp.isVirtual = false;
398 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
399 assert(mfp.isVirtual);
400 assert(mfp.thisptroffs == 0);
401 assert(mfp.vtbloffs == 0);
403 vtable_src = (void **)*(void **)&isgd_thunk;
404 vtable_dest = (void **)*(void **)gamedll_iface;
406 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
407 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
413 mm_PatchDllInit(true);
417 mm_GameDllRequest(const char *name, int *ret)
419 if (gamedll_lib != NULL && gamedll_bridge == NULL)
421 return gamedll_qvi(name, ret);
424 if (strncmp(name, "ServerGameDLL", 13) == 0)
426 if (!mm_DetectGameInformation())
436 QueryValveInterface qvi;
437 for (unsigned int i = 0; i < gamedll_path_count; i++)
439 if (gamedll_libs[i] == NULL)
441 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
444 gamedll_libs[i] = lib;
446 lib = gamedll_libs[i];
447 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
450 ptr = qvi(name, ret);
453 gamedll_libs[i] = NULL;
460 mm_FreeCachedLibraries();
462 gamedll_iface = (IServerGameDLL *)ptr;
464 gamedll_version = atoi(&name[13]);
465 mm_PrepForGameLoad();
472 else if (game_info_detected == 0)
474 mm_LogFatal("Received interface request too early: %s", name);