2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2010 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 "serverplugin.h"
37 #include <crt_externs.h>
40 static HMODULE mm_library = NULL;
41 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
42 MetamodBackend mm_backend = MMBackend_UNKNOWN;
45 mm_LogFatal(const char *message, ...)
52 fp = fopen(mm_fatal_logfile, "at");
53 if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
57 strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
58 fprintf(fp, "L %s: ", header);
60 va_start(ap, message);
61 vfprintf(fp, message, ap);
69 static const char *backend_names[] =
94 #define LIBRARY_EXT ".dll"
95 #define LIBRARY_MINEXT ".dll"
96 #elif defined __APPLE__
97 #define LIBRARY_EXT ".dylib"
98 #define LIBRARY_MINEXT ".dylib"
99 #elif defined __linux__
100 #define LIBRARY_EXT LIB_SUFFIX
101 #define LIBRARY_MINEXT ".so"
105 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
107 size_t len, temp_len;
108 char mm_path[PLATFORM_MAX_PATH * 2];
111 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
114 len = strlen(mm_path);
115 temp_len = strlen("server" LIBRARY_EXT);
119 /* Build log file name */
120 mm_path[len - temp_len] = '\0';
121 mm_Format(mm_fatal_logfile,
122 sizeof(mm_fatal_logfile),
123 "%smetamod-fatal.log",
126 /* Replace server.dll with the new binary we want */
127 mm_Format(&mm_path[len - temp_len],
128 sizeof(mm_path) - (len - temp_len),
129 "metamod.%s" LIBRARY_MINEXT,
130 backend_names[backend]);
132 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
134 return (mm_library != NULL);
138 mm_UnloadMetamodLibrary()
140 mm_UnloadLibrary(mm_library);
145 #define EXPORT extern "C" __declspec(dllexport)
146 #elif defined __GNUC__
148 #define EXPORT extern "C" __attribute__ ((visibility("default")))
150 #define EXPORT extern "C"
155 CreateInterface(const char *name, int *ret)
157 /* If we've got a VSP bridge, do nothing. */
158 if (vsp_bridge != NULL)
166 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
168 /* Either load as VSP or start VSP listener */
169 ptr = mm_GetVspCallbacks(atoi(&name[22]));
171 else if (gamedll_bridge == NULL)
173 /* Load as gamedll */
174 ptr = mm_GameDllRequest(name, ret);
178 /* If we've got a gamedll bridge, forward the request. */
179 return gamedll_bridge->QueryInterface(name, ret);
183 *ret = (ptr != NULL) ? 0 : 1;
189 mm_GetProcAddress(const char *name)
191 return mm_GetLibAddress(mm_library, name);
195 mm_GetGameName(char *buffer, size_t size)
200 static char game[128];
202 LPWSTR pCmdLine = GetCommandLineW();
204 LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc);
205 for (int i = 0; i < argc; ++i)
207 if (wcscmp(wargv[i], L"-game") != 0)
213 wcstombs(buffer, wargv[i], size);
214 buffer[size-1] = '\0';
220 #elif defined __APPLE__
221 int argc = *_NSGetArgc();
222 char **argv = *_NSGetArgv();
223 for (int i = 0; i < argc; ++i)
225 if (strcmp(argv[i], "-game") != 0)
231 strncpy(buffer, argv[i], size);
232 buffer[size-1] = '\0';
236 #elif defined __linux__
237 FILE *pFile = fopen("/proc/self/cmdline", "rb");
243 bool bNextIsGame = false;
245 while (getdelim(&arg, &argsize, 0, pFile) != -1)
249 strncpy(buffer, arg, size);
250 buffer[size-1] = '\0';
254 if (strcmp(arg, "-game") == 0)
264 #error unsupported platform
269 strncpy(buffer, ".", size);
274 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
276 if (engineFactory("VEngineServer024", NULL) != NULL)
278 return MMBackend_DOTA;
280 else if (engineFactory("VEngineServer023", NULL) != NULL)
282 if (strcmp(game_name, "insurgency") == 0)
284 return MMBackend_Insurgency;
286 return MMBackend_CSGO;
288 else if (engineFactory("VEngineServer022", NULL) != NULL &&
289 engineFactory("VEngineCvar007", NULL) != NULL)
291 if (strcmp(game_name, "berimbau") == 0)
293 return MMBackend_Blade;
295 if (strcmp(game_name, "portal2") == 0)
297 return MMBackend_Portal2;
299 if (engineFactory("EngineTraceServer004", NULL) != NULL)
301 return MMBackend_AlienSwarm;
303 else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
305 if (strcmp(game_name, "nucleardawn") == 0)
307 return MMBackend_NuclearDawn;
311 return MMBackend_Left4Dead2;
314 return MMBackend_Left4Dead;
316 else if (engineFactory("VEngineServer021", NULL) != NULL)
319 if (engineFactory("VEngineCvar004", NULL) != NULL)
321 if (engineFactory("VModelInfoServer002", NULL) != NULL)
323 /* BGT has same iface version numbers and libs as ep2 */
324 if (strcmp(game_name, "pm") == 0)
326 return MMBackend_BloodyGoodTime;
330 return MMBackend_Episode2;
333 else if (engineFactory("VModelInfoServer003", NULL) != NULL)
335 if (engineFactory("VFileSystem017", NULL) != NULL)
337 return MMBackend_EYE;
339 else if (strcmp(game_name, "cstrike") == 0)
341 return MMBackend_CSS;
343 else if (strcmp(game_name, "tf") == 0)
345 return MMBackend_TF2;
347 else if (strcmp(game_name, "dod") == 0)
349 return MMBackend_DODS;
351 else if (strcmp(game_name, "hl2mp") == 0)
353 return MMBackend_HL2DM;
357 return MMBackend_SDK2013;
361 /* Check for Episode One/Old Engine */
362 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
363 (engineFactory("VEngineCvar003", NULL) != NULL ||
364 engineFactory("VEngineCvar002", NULL) != NULL))
366 /* Check for Dark Messiah which has a weird directory structure */
367 if (strcmp(game_name, ".") == 0)
369 return MMBackend_DarkMessiah;
371 return MMBackend_Episode1;
375 return MMBackend_UNKNOWN;