Properly implementing GameDLL unloading.
8 #include <sh_memfuncinfo.h>
12 #include "valve_commandline.h"
15 typedef ICommandLine *(*GetCommandLine)();
17 #define MAX_GAMEDLL_PATHS 10
19 IGameDllBridge* gamedll_bridge = NULL;
20 static int game_info_detected = 0;
21 static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
22 static void *gamedll_libs[MAX_GAMEDLL_PATHS];
23 static unsigned int gamedll_path_count = 0;
24 static void *gamedll_lib = NULL;
25 static IServerGameDLL *gamedll_iface = NULL;
26 static QueryValveInterface gamedll_qvi = NULL;
27 static int gamedll_version = 0;
28 static int isgd_shutdown_index = -1;
31 #define TIER0_NAME "bin\\tier0.dll"
32 #define VSTDLIB_NAME "bin\\vstdlib.dll"
33 #define SERVER_NAME "server.dll"
34 #elif defined __linux__
35 #define TIER0_NAME "bin/tier0_i486.so"
36 #define VSTDLIB_NAME "bin/vstdlib_i486.so"
37 #define SERVER_NAME "server_i486.so"
41 mm_DetectGameInformation()
45 GetCommandLine valve_cmdline;
46 char mm_path[PLATFORM_MAX_PATH];
47 char lib_path[PLATFORM_MAX_PATH];
48 char game_name[PLATFORM_MAX_PATH];
49 char game_path[PLATFORM_MAX_PATH];
51 if (game_info_detected)
52 return game_info_detected == 1 ? true : false;
54 game_info_detected = -1;
56 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
58 mm_LogFatal("Could not find path for: " TIER0_NAME);
62 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
64 mm_LogFatal("Could not load %s: %s", lib_path, error);
68 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
69 if (valve_cmdline == NULL)
71 /* We probably have a Ship engine. */
72 mm_UnloadLibrary(lib);
73 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
75 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
79 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
81 mm_LogFatal("Could not load %s: %s", lib_path, error);
85 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
88 mm_UnloadLibrary(lib);
90 if (valve_cmdline == NULL)
92 mm_LogFatal("Could not locate any command line functionality");
96 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
98 mm_LogFatal("Could not locate metamod loader library path");
105 valve_cmdline()->ParmValue("-game", "hl2"));
106 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
108 mm_LogFatal("Could not resolve path: %s", game_name);
113 char gameinfo_path[PLATFORM_MAX_PATH];
115 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
116 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
118 mm_LogFatal("Could not read file: %s", gameinfo_path);
122 char temp_path[PLATFORM_MAX_PATH];
123 char cur_path[PLATFORM_MAX_PATH];
124 getcwd(cur_path, sizeof(cur_path));
129 char buffer[255], key[128], val[128];
130 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
132 mm_TrimComments(buffer);
134 mm_TrimRight(buffer);
136 if (stricmp(buffer, "SearchPaths") == 0)
142 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
143 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
146 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
148 ptr = &val[sizeof("|gameinfo_path|") - 1];
159 if (stricmp(key, "GameBin") == 0)
160 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
162 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
164 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
166 if (mm_PathCmp(mm_path, temp_path))
169 FILE *exists = fopen(temp_path, "rb");
174 /* exists is still non-NULL... use this as a flag */
175 for (unsigned int i = 0; i < gamedll_path_count; i++)
177 if (mm_PathCmp(gamedll_paths[i], temp_path))
187 mm_Format(gamedll_paths[gamedll_path_count],
191 gamedll_path_count++;
193 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
198 game_info_detected = 1;
204 mm_FreeCachedLibraries()
206 for (unsigned int i = 0; i < gamedll_path_count; i++)
208 if (gamedll_libs[i] == NULL)
210 mm_UnloadLibrary(gamedll_libs[i]);
215 mm_PatchDllInit(bool patch);
218 mm_PatchDllShutdown();
220 static void *isgd_orig_init = NULL;
221 static void *isgd_orig_shutdown = NULL;
230 virtual bool DLLInit(QueryValveInterface engineFactory,
231 QueryValveInterface physicsFactory,
232 QueryValveInterface fileSystemFactory,
235 MetamodBackend backend = mm_DetermineBackend(engineFactory);
238 if (backend == MMBackend_UNKNOWN)
240 mm_LogFatal("Could not detect engine version");
244 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
246 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
250 typedef IGameDllBridge *(*GetGameDllBridge)();
251 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
252 if (get_bridge == NULL)
254 mm_UnloadMetamodLibrary();
255 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
257 gamedll_bridge = get_bridge();
263 gamedll_bridge_info info;
265 info.engineFactory = (QueryValveInterface)engineFactory;
266 info.physicsFactory = (QueryValveInterface)physicsFactory;
267 info.fsFactory = (QueryValveInterface)fileSystemFactory;
268 info.pGlobals = pGlobals;
269 info.dllVersion = gamedll_version;
270 info.isgd = gamedll_iface;
271 info.gsFactory = gamedll_qvi;
273 strcpy(error, "Unknown error");
274 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
276 gamedll_bridge = NULL;
277 mm_UnloadMetamodLibrary();
278 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
282 /* Call the original */
287 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
288 QueryValveInterface physicsFactory,
289 QueryValveInterface fileSystemFactory,
294 u.addr = isgd_orig_init;
302 u.s.addr = isgd_orig_init;
305 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
312 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
313 * I'm pretty sure we'll die horribly.
318 gamedll_bridge->Unload();
319 mm_UnloadMetamodLibrary();
320 gamedll_bridge = NULL;
322 else if (gamedll_bridge != NULL)
324 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
325 assert(isgd_shutdown_index != -1);
326 mm_PatchDllShutdown();
329 mm_PatchDllInit(false);
334 virtual void DLLShutdown()
336 gamedll_bridge->Unload();
337 gamedll_bridge = NULL;
338 mm_UnloadMetamodLibrary();
340 /* Call original function */
344 void (VEmptyClass::*mfpnew)();
348 u.addr = isgd_orig_shutdown;
356 u.s.addr = isgd_orig_shutdown;
359 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
362 mm_UnloadLibrary(gamedll_lib);
367 static IServerGameDLL isgd_thunk;
370 mm_PatchDllInit(bool patch)
374 SourceHook::MemFuncInfo mfp;
376 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
378 assert(mfp.isVirtual);
379 assert(mfp.thisptroffs == 0);
380 assert(mfp.vtbloffs == 0);
382 vtable_src = (void **)*(void **)&isgd_thunk;
383 vtable_dest = (void **)*(void **)gamedll_iface;
385 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
387 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
391 assert(isgd_orig_init == NULL);
392 isgd_orig_init = vtable_dest[mfp.vtblindex];
393 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
397 assert(isgd_orig_init != NULL);
398 vtable_dest[mfp.vtblindex] = isgd_orig_init;
399 isgd_orig_init = NULL;
404 mm_PatchDllShutdown()
408 SourceHook::MemFuncInfo mfp;
410 mfp.isVirtual = false;
411 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
412 assert(mfp.isVirtual);
413 assert(mfp.thisptroffs == 0);
414 assert(mfp.vtbloffs == 0);
416 vtable_src = (void **)*(void **)&isgd_thunk;
417 vtable_dest = (void **)*(void **)gamedll_iface;
419 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
420 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
426 mm_PatchDllInit(true);
430 mm_GameDllRequest(const char *name, int *ret)
432 if (gamedll_lib != NULL && gamedll_bridge == NULL)
434 mm_LogFatal("Requested unknown interface before game load: %s", name);
435 return gamedll_qvi(name, ret);
438 if (strncmp(name, "ServerGameDLL", 13) == 0)
440 if (!mm_DetectGameInformation())
450 QueryValveInterface qvi;
451 for (unsigned int i = 0; i < gamedll_path_count; i++)
453 if (gamedll_libs[i] == NULL)
455 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
458 gamedll_libs[i] = lib;
460 lib = gamedll_libs[i];
461 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
464 ptr = qvi(name, ret);
467 gamedll_libs[i] = NULL;
474 mm_FreeCachedLibraries();
476 gamedll_iface = (IServerGameDLL *)ptr;
478 gamedll_version = atoi(&name[13]);
479 mm_PrepForGameLoad();
486 else if (game_info_detected == 0)
488 mm_LogFatal("Received interface request too early: %s", name);