Initial import of gameinfo loader.
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;
30 #define TIER0_NAME "bin\\tier0.dll"
31 #define VSTDLIB_NAME "bin\\vstdlib.dll"
32 #define SERVER_NAME "server.dll"
33 #elif defined __linux__
34 #define TIER0_NAME "bin/tier0_i486.so"
35 #define VSTDLIB_NAME "bin/vstdlib_i486.so"
36 #define SERVER_NAME "server_i486.so"
40 mm_DetectGameInformation()
44 GetCommandLine valve_cmdline;
45 char mm_path[PLATFORM_MAX_PATH];
46 char lib_path[PLATFORM_MAX_PATH];
47 char game_name[PLATFORM_MAX_PATH];
48 char game_path[PLATFORM_MAX_PATH];
50 if (game_info_detected)
51 return game_info_detected == 1 ? true : false;
53 game_info_detected = -1;
55 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
57 mm_LogFatal("Could not find path for: " TIER0_NAME);
61 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
63 mm_LogFatal("Could not load %s: %s", lib_path, error);
67 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
68 if (valve_cmdline == NULL)
70 /* We probably have a Ship engine. */
71 mm_UnloadLibrary(lib);
72 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
74 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
78 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
80 mm_LogFatal("Could not load %s: %s", lib_path, error);
84 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
87 mm_UnloadLibrary(lib);
89 if (valve_cmdline == NULL)
91 mm_LogFatal("Could not locate any command line functionality");
95 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
97 mm_LogFatal("Could not locate metamod loader library path");
104 valve_cmdline()->ParmValue("-game", "hl2"));
105 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
107 mm_LogFatal("Could not resolve path: %s", game_name);
112 char gameinfo_path[PLATFORM_MAX_PATH];
114 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
115 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
117 mm_LogFatal("Could not read file: %s", gameinfo_path);
121 char temp_path[PLATFORM_MAX_PATH];
122 char cur_path[PLATFORM_MAX_PATH];
123 getcwd(cur_path, sizeof(cur_path));
128 char buffer[255], key[128], val[128];
129 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
131 mm_TrimComments(buffer);
133 mm_TrimRight(buffer);
135 if (stricmp(buffer, "SearchPaths") == 0)
141 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
142 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
145 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
147 ptr = &val[sizeof("|gameinfo_path|") - 1];
158 if (stricmp(key, "GameBin") == 0)
159 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
161 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
163 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
165 if (mm_PathCmp(mm_path, temp_path))
168 FILE *exists = fopen(temp_path, "rb");
173 /* exists is still non-NULL... use this as a flag */
174 for (unsigned int i = 0; i < gamedll_path_count; i++)
176 if (mm_PathCmp(gamedll_paths[i], temp_path))
186 mm_Format(gamedll_paths[gamedll_path_count],
190 gamedll_path_count++;
192 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
197 game_info_detected = 1;
203 mm_FreeCachedLibraries()
205 for (unsigned int i = 0; i < gamedll_path_count; i++)
207 if (gamedll_libs[i] == NULL)
209 mm_UnloadLibrary(gamedll_libs[i]);
214 mm_PatchDllInit(bool patch);
216 static void *isgd_orig_call = NULL;
225 virtual bool DLLInit(QueryValveInterface engineFactory,
226 QueryValveInterface physicsFactory,
227 QueryValveInterface fileSystemFactory,
230 MetamodBackend backend = mm_DetermineBackend(engineFactory);
233 if (backend == MMBackend_UNKNOWN)
235 mm_LogFatal("Could not detect engine version");
239 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
241 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
245 typedef IGameDllBridge *(*GetGameDllBridge)();
246 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
247 if (get_bridge == NULL)
249 mm_UnloadMetamodLibrary();
250 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", backend);
252 gamedll_bridge = get_bridge();
258 gamedll_bridge_info info;
260 info.engineFactory = (QueryValveInterface)engineFactory;
261 info.physicsFactory = (QueryValveInterface)physicsFactory;
262 info.fsFactory = (QueryValveInterface)fileSystemFactory;
263 info.pGlobals = pGlobals;
264 info.dllVersion = gamedll_version;
265 info.isgd = gamedll_iface;
266 info.gsFactory = gamedll_qvi;
268 strcpy(error, "Unknown error");
269 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
271 gamedll_bridge = NULL;
272 mm_UnloadMetamodLibrary();
273 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
277 /* Call the original */
282 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
283 QueryValveInterface physicsFactory,
284 QueryValveInterface fileSystemFactory,
289 u.addr = isgd_orig_call;
297 u.s.addr = isgd_orig_call;
300 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
307 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
308 * I'm pretty sure we'll die horribly.
313 gamedll_bridge->Unload();
314 mm_UnloadMetamodLibrary();
315 gamedll_bridge = NULL;
317 else if (gamedll_bridge != NULL)
319 gamedll_bridge->DLLInit_Post();
322 mm_PatchDllInit(false);
328 static IServerGameDLL isgd_thunk;
331 mm_PatchDllInit(bool patch)
335 SourceHook::MemFuncInfo mfp;
337 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
339 assert(mfp.isVirtual);
340 assert(mfp.thisptroffs == 0);
341 assert(mfp.vtbloffs == 0);
343 vtable_src = (void **)*(void **)&isgd_thunk;
344 vtable_dest = (void **)*(void **)gamedll_iface;
346 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
348 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
352 assert(isgd_orig_call == NULL);
353 isgd_orig_call = vtable_dest[mfp.vtblindex];
354 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
358 assert(isgd_orig_call != NULL);
359 vtable_dest[mfp.vtblindex] = isgd_orig_call;
360 isgd_orig_call = NULL;
367 mm_PatchDllInit(true);
371 mm_GameDllRequest(const char *name, int *ret)
373 if (gamedll_lib != NULL && gamedll_bridge == NULL)
375 mm_LogFatal("Requested unknown interface before game load: %s", name);
376 return gamedll_qvi(name, ret);
379 if (strncmp(name, "ServerGameDLL", 13) == 0)
381 if (!mm_DetectGameInformation())
391 QueryValveInterface qvi;
392 for (unsigned int i = 0; i < gamedll_path_count; i++)
394 if (gamedll_libs[i] == NULL)
396 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
399 gamedll_libs[i] = lib;
401 lib = gamedll_libs[i];
402 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
405 ptr = qvi(name, ret);
408 gamedll_libs[i] = NULL;
415 mm_FreeCachedLibraries();
417 gamedll_iface = (IServerGameDLL *)ptr;
419 gamedll_version = atoi(&name[13]);
420 mm_PrepForGameLoad();
427 else if (game_info_detected == 0)
429 mm_LogFatal("Received interface request too early: %s", name);