Removed unnecessary logging to metamod-fatal.log when using GameDLL loading.
8 #include <sh_memfuncinfo.h>
12 #include "valve_commandline.h"
17 typedef ICommandLine *(*GetCommandLine)();
19 #define MAX_GAMEDLL_PATHS 10
21 IGameDllBridge* gamedll_bridge = NULL;
22 static int game_info_detected = 0;
23 static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH];
24 static void *gamedll_libs[MAX_GAMEDLL_PATHS];
25 static unsigned int gamedll_path_count = 0;
26 static void *gamedll_lib = NULL;
27 static IServerGameDLL *gamedll_iface = NULL;
28 static QueryValveInterface gamedll_qvi = NULL;
29 static int gamedll_version = 0;
30 static int isgd_shutdown_index = -1;
33 #define TIER0_NAME "bin\\tier0.dll"
34 #define VSTDLIB_NAME "bin\\vstdlib.dll"
35 #define SERVER_NAME "server.dll"
36 #elif defined __linux__
37 #define TIER0_NAME "bin/tier0_i486.so"
38 #define VSTDLIB_NAME "bin/vstdlib_i486.so"
39 #define SERVER_NAME "server_i486.so"
43 mm_DetectGameInformation()
47 GetCommandLine valve_cmdline;
48 char mm_path[PLATFORM_MAX_PATH];
49 char lib_path[PLATFORM_MAX_PATH];
50 char game_name[PLATFORM_MAX_PATH];
51 char game_path[PLATFORM_MAX_PATH];
53 if (game_info_detected)
54 return game_info_detected == 1 ? true : false;
56 game_info_detected = -1;
58 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
60 mm_LogFatal("Could not find path for: " TIER0_NAME);
64 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
66 mm_LogFatal("Could not load %s: %s", lib_path, error);
70 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
71 if (valve_cmdline == NULL)
73 /* We probably have a Ship engine. */
74 mm_UnloadLibrary(lib);
75 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
77 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
81 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
83 mm_LogFatal("Could not load %s: %s", lib_path, error);
87 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
90 mm_UnloadLibrary(lib);
92 if (valve_cmdline == NULL)
94 mm_LogFatal("Could not locate any command line functionality");
98 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
100 mm_LogFatal("Could not locate metamod loader library path");
107 valve_cmdline()->ParmValue("-game", "hl2"));
108 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
110 mm_LogFatal("Could not resolve path: %s", game_name);
115 char gameinfo_path[PLATFORM_MAX_PATH];
117 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
118 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
120 mm_LogFatal("Could not read file: %s", gameinfo_path);
124 char temp_path[PLATFORM_MAX_PATH];
125 char cur_path[PLATFORM_MAX_PATH];
126 getcwd(cur_path, sizeof(cur_path));
131 char buffer[255], key[128], val[128];
132 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
134 mm_TrimComments(buffer);
136 mm_TrimRight(buffer);
138 if (stricmp(buffer, "SearchPaths") == 0)
144 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
145 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
148 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
150 ptr = &val[sizeof("|gameinfo_path|") - 1];
161 if (stricmp(key, "GameBin") == 0)
162 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
164 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
166 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
168 if (mm_PathCmp(mm_path, temp_path))
171 FILE *exists = fopen(temp_path, "rb");
176 /* exists is still non-NULL... use this as a flag */
177 for (unsigned int i = 0; i < gamedll_path_count; i++)
179 if (mm_PathCmp(gamedll_paths[i], temp_path))
189 mm_Format(gamedll_paths[gamedll_path_count],
193 gamedll_path_count++;
195 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
200 game_info_detected = 1;
206 mm_FreeCachedLibraries()
208 for (unsigned int i = 0; i < gamedll_path_count; i++)
210 if (gamedll_libs[i] == NULL)
212 mm_UnloadLibrary(gamedll_libs[i]);
217 mm_PatchDllInit(bool patch);
220 mm_PatchDllShutdown();
222 static void *isgd_orig_init = NULL;
223 static void *isgd_orig_shutdown = NULL;
232 virtual bool DLLInit(QueryValveInterface engineFactory,
233 QueryValveInterface physicsFactory,
234 QueryValveInterface fileSystemFactory,
237 MetamodBackend backend = mm_DetermineBackend(engineFactory);
240 if (backend == MMBackend_UNKNOWN)
242 mm_LogFatal("Could not detect engine version");
246 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
248 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
252 typedef IGameDllBridge *(*GetGameDllBridge)();
253 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
254 if (get_bridge == NULL)
256 mm_UnloadMetamodLibrary();
257 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
261 gamedll_bridge = get_bridge();
268 gamedll_bridge_info info;
270 info.engineFactory = (QueryValveInterface)engineFactory;
271 info.physicsFactory = (QueryValveInterface)physicsFactory;
272 info.fsFactory = (QueryValveInterface)fileSystemFactory;
273 info.pGlobals = pGlobals;
274 info.dllVersion = gamedll_version;
275 info.isgd = gamedll_iface;
276 info.gsFactory = gamedll_qvi;
278 strcpy(error, "Unknown error");
279 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
281 gamedll_bridge = NULL;
282 mm_UnloadMetamodLibrary();
283 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
287 /* Call the original */
292 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
293 QueryValveInterface physicsFactory,
294 QueryValveInterface fileSystemFactory,
299 u.addr = isgd_orig_init;
307 u.s.addr = isgd_orig_init;
310 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
317 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
318 * I'm pretty sure we'll die horribly.
323 gamedll_bridge->Unload();
324 mm_UnloadMetamodLibrary();
325 gamedll_bridge = NULL;
327 else if (gamedll_bridge != NULL)
329 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
330 assert(isgd_shutdown_index != -1);
331 mm_PatchDllShutdown();
334 mm_PatchDllInit(false);
339 virtual void DLLShutdown()
341 gamedll_bridge->Unload();
342 gamedll_bridge = NULL;
343 mm_UnloadMetamodLibrary();
345 /* Call original function */
349 void (VEmptyClass::*mfpnew)();
353 u.addr = isgd_orig_shutdown;
361 u.s.addr = isgd_orig_shutdown;
364 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
367 mm_UnloadLibrary(gamedll_lib);
372 static IServerGameDLL isgd_thunk;
375 mm_PatchDllInit(bool patch)
379 SourceHook::MemFuncInfo mfp;
381 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
383 assert(mfp.isVirtual);
384 assert(mfp.thisptroffs == 0);
385 assert(mfp.vtbloffs == 0);
387 vtable_src = (void **)*(void **)&isgd_thunk;
388 vtable_dest = (void **)*(void **)gamedll_iface;
390 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
392 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
396 assert(isgd_orig_init == NULL);
397 isgd_orig_init = vtable_dest[mfp.vtblindex];
398 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
402 assert(isgd_orig_init != NULL);
403 vtable_dest[mfp.vtblindex] = isgd_orig_init;
404 isgd_orig_init = NULL;
409 mm_PatchDllShutdown()
413 SourceHook::MemFuncInfo mfp;
415 mfp.isVirtual = false;
416 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
417 assert(mfp.isVirtual);
418 assert(mfp.thisptroffs == 0);
419 assert(mfp.vtbloffs == 0);
421 vtable_src = (void **)*(void **)&isgd_thunk;
422 vtable_dest = (void **)*(void **)gamedll_iface;
424 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
425 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
431 mm_PatchDllInit(true);
435 mm_GameDllRequest(const char *name, int *ret)
437 if (gamedll_lib != NULL)
439 return gamedll_qvi(name, ret);
442 if (strncmp(name, "ServerGameDLL", 13) == 0)
444 if (!mm_DetectGameInformation())
454 QueryValveInterface qvi;
455 for (unsigned int i = 0; i < gamedll_path_count; i++)
457 if (gamedll_libs[i] == NULL)
459 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
462 gamedll_libs[i] = lib;
464 lib = gamedll_libs[i];
465 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
468 ptr = qvi(name, ret);
471 gamedll_libs[i] = NULL;
478 mm_FreeCachedLibraries();
480 gamedll_iface = (IServerGameDLL *)ptr;
482 gamedll_version = atoi(&name[13]);
483 mm_PrepForGameLoad();
490 else if (game_info_detected == 0)
492 mm_LogFatal("Received interface request too early: %s", name);