Attempt at fixing windows build errors.
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);
259 gamedll_bridge = get_bridge();
265 gamedll_bridge_info info;
267 info.engineFactory = (QueryValveInterface)engineFactory;
268 info.physicsFactory = (QueryValveInterface)physicsFactory;
269 info.fsFactory = (QueryValveInterface)fileSystemFactory;
270 info.pGlobals = pGlobals;
271 info.dllVersion = gamedll_version;
272 info.isgd = gamedll_iface;
273 info.gsFactory = gamedll_qvi;
275 strcpy(error, "Unknown error");
276 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
278 gamedll_bridge = NULL;
279 mm_UnloadMetamodLibrary();
280 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
284 /* Call the original */
289 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
290 QueryValveInterface physicsFactory,
291 QueryValveInterface fileSystemFactory,
296 u.addr = isgd_orig_init;
304 u.s.addr = isgd_orig_init;
307 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
314 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
315 * I'm pretty sure we'll die horribly.
320 gamedll_bridge->Unload();
321 mm_UnloadMetamodLibrary();
322 gamedll_bridge = NULL;
324 else if (gamedll_bridge != NULL)
326 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
327 assert(isgd_shutdown_index != -1);
328 mm_PatchDllShutdown();
331 mm_PatchDllInit(false);
336 virtual void DLLShutdown()
338 gamedll_bridge->Unload();
339 gamedll_bridge = NULL;
340 mm_UnloadMetamodLibrary();
342 /* Call original function */
346 void (VEmptyClass::*mfpnew)();
350 u.addr = isgd_orig_shutdown;
358 u.s.addr = isgd_orig_shutdown;
361 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
364 mm_UnloadLibrary(gamedll_lib);
369 static IServerGameDLL isgd_thunk;
372 mm_PatchDllInit(bool patch)
376 SourceHook::MemFuncInfo mfp;
378 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
380 assert(mfp.isVirtual);
381 assert(mfp.thisptroffs == 0);
382 assert(mfp.vtbloffs == 0);
384 vtable_src = (void **)*(void **)&isgd_thunk;
385 vtable_dest = (void **)*(void **)gamedll_iface;
387 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
389 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
393 assert(isgd_orig_init == NULL);
394 isgd_orig_init = vtable_dest[mfp.vtblindex];
395 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
399 assert(isgd_orig_init != NULL);
400 vtable_dest[mfp.vtblindex] = isgd_orig_init;
401 isgd_orig_init = NULL;
406 mm_PatchDllShutdown()
410 SourceHook::MemFuncInfo mfp;
412 mfp.isVirtual = false;
413 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
414 assert(mfp.isVirtual);
415 assert(mfp.thisptroffs == 0);
416 assert(mfp.vtbloffs == 0);
418 vtable_src = (void **)*(void **)&isgd_thunk;
419 vtable_dest = (void **)*(void **)gamedll_iface;
421 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
422 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
428 mm_PatchDllInit(true);
432 mm_GameDllRequest(const char *name, int *ret)
434 if (gamedll_lib != NULL && gamedll_bridge == NULL)
436 mm_LogFatal("Requested unknown interface before game load: %s", name);
437 return gamedll_qvi(name, ret);
440 if (strncmp(name, "ServerGameDLL", 13) == 0)
442 if (!mm_DetectGameInformation())
452 QueryValveInterface qvi;
453 for (unsigned int i = 0; i < gamedll_path_count; i++)
455 if (gamedll_libs[i] == NULL)
457 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
460 gamedll_libs[i] = lib;
462 lib = gamedll_libs[i];
463 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
466 ptr = qvi(name, ret);
469 gamedll_libs[i] = NULL;
476 mm_FreeCachedLibraries();
478 gamedll_iface = (IServerGameDLL *)ptr;
480 gamedll_version = atoi(&name[13]);
481 mm_PrepForGameLoad();
488 else if (game_info_detected == 0)
490 mm_LogFatal("Received interface request too early: %s", name);