Trigger build for hl2sdk-l4d changes.
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 char game_name[128];
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 __APPLE__
58 #define SERVER_NAME "server.dylib"
59 #elif defined __linux__
60 #define SERVER_NAME "server" LIB_SUFFIX
64 mm_DetectGameInformation()
66 char game_path[PLATFORM_MAX_PATH];
68 if (game_info_detected)
69 return game_info_detected == 1 ? true : false;
71 game_info_detected = -1;
73 mm_GetGameName(game_name, sizeof(game_name));
75 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
77 mm_LogFatal("Could not locate Metamod loader library path");
81 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
83 mm_LogFatal("Could not resolve path: %s", game_name);
88 char gameinfo_path[PLATFORM_MAX_PATH];
90 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
91 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
93 mm_LogFatal("Could not read file: %s", gameinfo_path);
97 char temp_path[PLATFORM_MAX_PATH];
98 char cur_path[PLATFORM_MAX_PATH];
99 getcwd(cur_path, sizeof(cur_path));
104 char buffer[255], key[128], val[128];
105 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
107 mm_TrimComments(buffer);
109 mm_TrimRight(buffer);
111 if (stricmp(buffer, "SearchPaths") == 0)
117 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
118 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
121 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
123 ptr = &val[sizeof("|gameinfo_path|") - 1];
134 if (stricmp(key, "GameBin") == 0)
135 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
137 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
139 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
141 if (mm_PathCmp(mm_path, temp_path))
144 FILE *exists = fopen(temp_path, "rb");
149 /* exists is still non-NULL... use this as a flag */
150 for (unsigned int i = 0; i < gamedll_path_count; i++)
152 if (mm_PathCmp(gamedll_paths[i], temp_path))
162 mm_Format(gamedll_paths[gamedll_path_count],
166 gamedll_path_count++;
168 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
173 game_info_detected = 1;
175 if (gamedll_path_count == 0)
177 mm_LogFatal("Could not detect any valid game paths in gameinfo.txt");
185 mm_FreeCachedLibraries()
187 for (unsigned int i = 0; i < gamedll_path_count; i++)
189 if (gamedll_libs[i] == NULL)
191 mm_UnloadLibrary(gamedll_libs[i]);
196 mm_PatchDllInit(bool patch);
199 mm_PatchDllShutdown();
201 static void *isgd_orig_init = NULL;
202 static void *isgd_orig_shutdown = NULL;
211 virtual bool DLLInit(QueryValveInterface engineFactory,
212 QueryValveInterface physicsFactory,
213 QueryValveInterface fileSystemFactory,
216 mm_backend = mm_DetermineBackend(engineFactory, gamedll_qvi, game_name);
219 if (mm_backend == MMBackend_UNKNOWN)
221 mm_LogFatal("Could not detect engine version");
225 if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
227 mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
231 typedef IGameDllBridge *(*GetGameDllBridge)();
232 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
233 if (get_bridge == NULL)
235 mm_UnloadMetamodLibrary();
236 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", mm_backend);
240 gamedll_bridge = get_bridge();
247 gamedll_bridge_info info;
249 info.engineFactory = (QueryValveInterface)engineFactory;
250 info.physicsFactory = (QueryValveInterface)physicsFactory;
251 info.fsFactory = (QueryValveInterface)fileSystemFactory;
252 info.pGlobals = pGlobals;
253 info.dllVersion = gamedll_version;
254 info.isgd = gamedll_iface;
255 info.gsFactory = gamedll_qvi;
256 info.vsp_listener_path = mm_path;
258 strcpy(error, "Unknown error");
259 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
261 gamedll_bridge = NULL;
262 mm_UnloadMetamodLibrary();
263 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
267 /* Call the original */
272 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
273 QueryValveInterface physicsFactory,
274 QueryValveInterface fileSystemFactory,
279 u.addr = isgd_orig_init;
287 u.s.addr = isgd_orig_init;
290 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
297 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
298 * I'm pretty sure we'll die horribly.
303 gamedll_bridge->Unload();
304 mm_UnloadMetamodLibrary();
305 gamedll_bridge = NULL;
307 else if (gamedll_bridge != NULL)
309 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
310 assert(isgd_shutdown_index != -1);
311 mm_PatchDllShutdown();
314 mm_PatchDllInit(false);
319 virtual void DLLShutdown()
321 gamedll_bridge->Unload();
322 gamedll_bridge = NULL;
323 mm_UnloadMetamodLibrary();
325 /* Call original function */
329 void (VEmptyClass::*mfpnew)();
333 u.addr = isgd_orig_shutdown;
341 u.s.addr = isgd_orig_shutdown;
344 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
347 mm_UnloadLibrary(gamedll_lib);
352 static IServerGameDLL isgd_thunk;
355 mm_PatchDllInit(bool patch)
359 SourceHook::MemFuncInfo mfp;
361 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
363 assert(mfp.isVirtual);
364 assert(mfp.thisptroffs == 0);
365 assert(mfp.vtbloffs == 0);
367 vtable_src = (void **)*(void **)&isgd_thunk;
368 vtable_dest = (void **)*(void **)gamedll_iface;
370 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
372 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
376 assert(isgd_orig_init == NULL);
377 isgd_orig_init = vtable_dest[mfp.vtblindex];
378 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
382 assert(isgd_orig_init != NULL);
383 vtable_dest[mfp.vtblindex] = isgd_orig_init;
384 isgd_orig_init = NULL;
389 mm_PatchDllShutdown()
393 SourceHook::MemFuncInfo mfp;
395 mfp.isVirtual = false;
396 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
397 assert(mfp.isVirtual);
398 assert(mfp.thisptroffs == 0);
399 assert(mfp.vtbloffs == 0);
401 vtable_src = (void **)*(void **)&isgd_thunk;
402 vtable_dest = (void **)*(void **)gamedll_iface;
404 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
405 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
411 mm_PatchDllInit(true);
415 mm_GameDllRequest(const char *name, int *ret)
417 if (gamedll_lib != NULL && gamedll_bridge == NULL)
419 return gamedll_qvi(name, ret);
422 if (strncmp(name, "ServerGameDLL", 13) == 0)
424 if (!mm_DetectGameInformation())
434 QueryValveInterface qvi;
435 for (unsigned int i = 0; i < gamedll_path_count; i++)
437 if (gamedll_libs[i] == NULL)
439 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
442 gamedll_libs[i] = lib;
444 lib = gamedll_libs[i];
445 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
448 ptr = qvi(name, ret);
451 gamedll_libs[i] = NULL;
458 mm_FreeCachedLibraries();
460 gamedll_iface = (IServerGameDLL *)ptr;
462 gamedll_version = atoi(&name[13]);
463 mm_PrepForGameLoad();
470 else if (game_info_detected == 0)
472 mm_LogFatal("Received interface request too early: %s", name);