2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2009 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.
33 #include <sh_memfuncinfo.h>
34 #include <sh_memory.h>
40 #define MAX_GAMEDLL_PATHS 10
42 IGameDllBridge* gamedll_bridge = NULL;
43 static int game_info_detected = 0;
44 static const char *game_name = NULL;
45 static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
46 static void *gamedll_libs[MAX_GAMEDLL_PATHS];
47 static unsigned int gamedll_path_count = 0;
48 static void *gamedll_lib = NULL;
49 static IServerGameDLL *gamedll_iface = NULL;
50 static QueryValveInterface gamedll_qvi = NULL;
51 static int gamedll_version = 0;
52 static int isgd_shutdown_index = -1;
53 static char mm_path[PLATFORM_MAX_PATH];
56 #define SERVER_NAME "server.dll"
57 #elif defined __linux__
58 #define SERVER_NAME "server" LIB_SUFFIX
62 mm_DetectGameInformation()
64 char game_path[PLATFORM_MAX_PATH];
66 if (game_info_detected)
67 return game_info_detected == 1 ? true : false;
69 game_info_detected = -1;
71 if ((game_name = mm_GetGameName()) == NULL)
76 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
78 mm_LogFatal("Could not locate Metamod loader library path");
82 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
84 mm_LogFatal("Could not resolve path: %s", game_name);
89 char gameinfo_path[PLATFORM_MAX_PATH];
91 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
92 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
94 mm_LogFatal("Could not read file: %s", gameinfo_path);
98 char temp_path[PLATFORM_MAX_PATH];
99 char cur_path[PLATFORM_MAX_PATH];
100 getcwd(cur_path, sizeof(cur_path));
105 char buffer[255], key[128], val[128];
106 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
108 mm_TrimComments(buffer);
110 mm_TrimRight(buffer);
112 if (stricmp(buffer, "SearchPaths") == 0)
118 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
119 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
122 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
124 ptr = &val[sizeof("|gameinfo_path|") - 1];
135 if (stricmp(key, "GameBin") == 0)
136 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
138 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
140 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
142 if (mm_PathCmp(mm_path, temp_path))
145 FILE *exists = fopen(temp_path, "rb");
150 /* exists is still non-NULL... use this as a flag */
151 for (unsigned int i = 0; i < gamedll_path_count; i++)
153 if (mm_PathCmp(gamedll_paths[i], temp_path))
163 mm_Format(gamedll_paths[gamedll_path_count],
167 gamedll_path_count++;
169 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
174 game_info_detected = 1;
176 if (gamedll_path_count == 0)
178 mm_LogFatal("Could not detect any valid game paths in gameinfo.txt");
186 mm_FreeCachedLibraries()
188 for (unsigned int i = 0; i < gamedll_path_count; i++)
190 if (gamedll_libs[i] == NULL)
192 mm_UnloadLibrary(gamedll_libs[i]);
197 mm_PatchDllInit(bool patch);
200 mm_PatchDllShutdown();
202 static void *isgd_orig_init = NULL;
203 static void *isgd_orig_shutdown = NULL;
212 virtual bool DLLInit(QueryValveInterface engineFactory,
213 QueryValveInterface physicsFactory,
214 QueryValveInterface fileSystemFactory,
217 mm_backend = mm_DetermineBackend(engineFactory, game_name);
220 if (mm_backend == MMBackend_UNKNOWN)
222 mm_LogFatal("Could not detect engine version");
226 if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
228 mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
232 typedef IGameDllBridge *(*GetGameDllBridge)();
233 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
234 if (get_bridge == NULL)
236 mm_UnloadMetamodLibrary();
237 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", mm_backend);
241 gamedll_bridge = get_bridge();
248 gamedll_bridge_info info;
250 info.engineFactory = (QueryValveInterface)engineFactory;
251 info.physicsFactory = (QueryValveInterface)physicsFactory;
252 info.fsFactory = (QueryValveInterface)fileSystemFactory;
253 info.pGlobals = pGlobals;
254 info.dllVersion = gamedll_version;
255 info.isgd = gamedll_iface;
256 info.gsFactory = gamedll_qvi;
257 info.vsp_listener_path = mm_path;
259 strcpy(error, "Unknown error");
260 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
262 gamedll_bridge = NULL;
263 mm_UnloadMetamodLibrary();
264 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
268 /* Call the original */
273 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
274 QueryValveInterface physicsFactory,
275 QueryValveInterface fileSystemFactory,
280 u.addr = isgd_orig_init;
288 u.s.addr = isgd_orig_init;
291 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
298 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
299 * I'm pretty sure we'll die horribly.
304 gamedll_bridge->Unload();
305 mm_UnloadMetamodLibrary();
306 gamedll_bridge = NULL;
308 else if (gamedll_bridge != NULL)
310 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
311 assert(isgd_shutdown_index != -1);
312 mm_PatchDllShutdown();
315 mm_PatchDllInit(false);
320 virtual void DLLShutdown()
322 gamedll_bridge->Unload();
323 gamedll_bridge = NULL;
324 mm_UnloadMetamodLibrary();
326 /* Call original function */
330 void (VEmptyClass::*mfpnew)();
334 u.addr = isgd_orig_shutdown;
342 u.s.addr = isgd_orig_shutdown;
345 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
348 mm_UnloadLibrary(gamedll_lib);
353 static IServerGameDLL isgd_thunk;
356 mm_PatchDllInit(bool patch)
360 SourceHook::MemFuncInfo mfp;
362 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
364 assert(mfp.isVirtual);
365 assert(mfp.thisptroffs == 0);
366 assert(mfp.vtbloffs == 0);
368 vtable_src = (void **)*(void **)&isgd_thunk;
369 vtable_dest = (void **)*(void **)gamedll_iface;
371 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
373 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
377 assert(isgd_orig_init == NULL);
378 isgd_orig_init = vtable_dest[mfp.vtblindex];
379 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
383 assert(isgd_orig_init != NULL);
384 vtable_dest[mfp.vtblindex] = isgd_orig_init;
385 isgd_orig_init = NULL;
390 mm_PatchDllShutdown()
394 SourceHook::MemFuncInfo mfp;
396 mfp.isVirtual = false;
397 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
398 assert(mfp.isVirtual);
399 assert(mfp.thisptroffs == 0);
400 assert(mfp.vtbloffs == 0);
402 vtable_src = (void **)*(void **)&isgd_thunk;
403 vtable_dest = (void **)*(void **)gamedll_iface;
405 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
406 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
412 mm_PatchDllInit(true);
416 mm_GameDllRequest(const char *name, int *ret)
418 if (gamedll_lib != NULL && gamedll_bridge == NULL)
420 return gamedll_qvi(name, ret);
423 if (strncmp(name, "ServerGameDLL", 13) == 0)
425 if (!mm_DetectGameInformation())
435 QueryValveInterface qvi;
436 for (unsigned int i = 0; i < gamedll_path_count; i++)
438 if (gamedll_libs[i] == NULL)
440 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
443 gamedll_libs[i] = lib;
445 lib = gamedll_libs[i];
446 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
449 ptr = qvi(name, ret);
452 gamedll_libs[i] = NULL;
459 mm_FreeCachedLibraries();
461 gamedll_iface = (IServerGameDLL *)ptr;
463 gamedll_version = atoi(&name[13]);
464 mm_PrepForGameLoad();
471 else if (game_info_detected == 0)
473 mm_LogFatal("Received interface request too early: %s", name);