Triggering build to test buildbot.
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 if (!mm_GetGameName(game_name, sizeof(game_name)))
78 if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
80 mm_LogFatal("Could not locate Metamod loader library path");
84 if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
86 mm_LogFatal("Could not resolve path: %s", game_name);
91 char gameinfo_path[PLATFORM_MAX_PATH];
93 mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
94 if ((fp = fopen(gameinfo_path, "rt")) == NULL)
96 mm_LogFatal("Could not read file: %s", gameinfo_path);
100 char temp_path[PLATFORM_MAX_PATH];
101 char cur_path[PLATFORM_MAX_PATH];
102 getcwd(cur_path, sizeof(cur_path));
107 char buffer[255], key[128], val[128];
108 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
110 mm_TrimComments(buffer);
112 mm_TrimRight(buffer);
114 if (stricmp(buffer, "SearchPaths") == 0)
120 mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
121 if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
124 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
126 ptr = &val[sizeof("|gameinfo_path|") - 1];
137 if (stricmp(key, "GameBin") == 0)
138 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
140 mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
142 mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);
144 if (mm_PathCmp(mm_path, temp_path))
147 FILE *exists = fopen(temp_path, "rb");
152 /* exists is still non-NULL... use this as a flag */
153 for (unsigned int i = 0; i < gamedll_path_count; i++)
155 if (mm_PathCmp(gamedll_paths[i], temp_path))
165 mm_Format(gamedll_paths[gamedll_path_count],
169 gamedll_path_count++;
171 if (gamedll_path_count == MAX_GAMEDLL_PATHS)
176 game_info_detected = 1;
178 if (gamedll_path_count == 0)
180 mm_LogFatal("Could not detect any valid game paths in gameinfo.txt");
188 mm_FreeCachedLibraries()
190 for (unsigned int i = 0; i < gamedll_path_count; i++)
192 if (gamedll_libs[i] == NULL)
194 mm_UnloadLibrary(gamedll_libs[i]);
199 mm_PatchDllInit(bool patch);
202 mm_PatchDllShutdown();
204 static void *isgd_orig_init = NULL;
205 static void *isgd_orig_shutdown = NULL;
214 virtual bool DLLInit(QueryValveInterface engineFactory,
215 QueryValveInterface physicsFactory,
216 QueryValveInterface fileSystemFactory,
219 mm_backend = mm_DetermineBackend(engineFactory, game_name);
222 if (mm_backend == MMBackend_UNKNOWN)
224 mm_LogFatal("Could not detect engine version");
228 if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
230 mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
234 typedef IGameDllBridge *(*GetGameDllBridge)();
235 GetGameDllBridge get_bridge = (GetGameDllBridge)mm_GetProcAddress("GetGameDllBridge");
236 if (get_bridge == NULL)
238 mm_UnloadMetamodLibrary();
239 mm_LogFatal("Detected engine %d but could not find GetGameDllBridge callback", mm_backend);
243 gamedll_bridge = get_bridge();
250 gamedll_bridge_info info;
252 info.engineFactory = (QueryValveInterface)engineFactory;
253 info.physicsFactory = (QueryValveInterface)physicsFactory;
254 info.fsFactory = (QueryValveInterface)fileSystemFactory;
255 info.pGlobals = pGlobals;
256 info.dllVersion = gamedll_version;
257 info.isgd = gamedll_iface;
258 info.gsFactory = gamedll_qvi;
259 info.vsp_listener_path = mm_path;
261 strcpy(error, "Unknown error");
262 if (!gamedll_bridge->DLLInit_Pre(&info, error, sizeof(error)))
264 gamedll_bridge = NULL;
265 mm_UnloadMetamodLibrary();
266 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
270 /* Call the original */
275 bool (VEmptyClass::*mfpnew)(QueryValveInterface engineFactory,
276 QueryValveInterface physicsFactory,
277 QueryValveInterface fileSystemFactory,
282 u.addr = isgd_orig_init;
290 u.s.addr = isgd_orig_init;
293 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
300 * :TODO: possible logic hole here, what happens if the gamedll REALLY returns false?
301 * I'm pretty sure we'll die horribly.
306 gamedll_bridge->Unload();
307 mm_UnloadMetamodLibrary();
308 gamedll_bridge = NULL;
310 else if (gamedll_bridge != NULL)
312 gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
313 assert(isgd_shutdown_index != -1);
314 mm_PatchDllShutdown();
317 mm_PatchDllInit(false);
322 virtual void DLLShutdown()
324 gamedll_bridge->Unload();
325 gamedll_bridge = NULL;
326 mm_UnloadMetamodLibrary();
328 /* Call original function */
332 void (VEmptyClass::*mfpnew)();
336 u.addr = isgd_orig_shutdown;
344 u.s.addr = isgd_orig_shutdown;
347 (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
350 mm_UnloadLibrary(gamedll_lib);
355 static IServerGameDLL isgd_thunk;
358 mm_PatchDllInit(bool patch)
362 SourceHook::MemFuncInfo mfp;
364 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfp);
366 assert(mfp.isVirtual);
367 assert(mfp.thisptroffs == 0);
368 assert(mfp.vtbloffs == 0);
370 vtable_src = (void **)*(void **)&isgd_thunk;
371 vtable_dest = (void **)*(void **)gamedll_iface;
373 SourceHook::SetMemAccess(&vtable_dest[mfp.vtblindex],
375 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
379 assert(isgd_orig_init == NULL);
380 isgd_orig_init = vtable_dest[mfp.vtblindex];
381 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
385 assert(isgd_orig_init != NULL);
386 vtable_dest[mfp.vtblindex] = isgd_orig_init;
387 isgd_orig_init = NULL;
392 mm_PatchDllShutdown()
396 SourceHook::MemFuncInfo mfp;
398 mfp.isVirtual = false;
399 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
400 assert(mfp.isVirtual);
401 assert(mfp.thisptroffs == 0);
402 assert(mfp.vtbloffs == 0);
404 vtable_src = (void **)*(void **)&isgd_thunk;
405 vtable_dest = (void **)*(void **)gamedll_iface;
407 isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
408 vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
414 mm_PatchDllInit(true);
418 mm_GameDllRequest(const char *name, int *ret)
420 if (gamedll_lib != NULL && gamedll_bridge == NULL)
422 return gamedll_qvi(name, ret);
425 if (strncmp(name, "ServerGameDLL", 13) == 0)
427 if (!mm_DetectGameInformation())
437 QueryValveInterface qvi;
438 for (unsigned int i = 0; i < gamedll_path_count; i++)
440 if (gamedll_libs[i] == NULL)
442 lib = mm_LoadLibrary(gamedll_paths[i], error, sizeof(error));
445 gamedll_libs[i] = lib;
447 lib = gamedll_libs[i];
448 qvi = (QueryValveInterface)mm_GetLibAddress(lib, "CreateInterface");
451 ptr = qvi(name, ret);
454 gamedll_libs[i] = NULL;
461 mm_FreeCachedLibraries();
463 gamedll_iface = (IServerGameDLL *)ptr;
465 gamedll_version = atoi(&name[13]);
466 mm_PrepForGameLoad();
473 else if (game_info_detected == 0)
475 mm_LogFatal("Received interface request too early: %s", name);