Fixed NULL conversion warning in loader.
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[] =
87 #define LIBRARY_EXT ".dll"
88 #define LIBRARY_MINEXT ".dll"
89 #elif defined __APPLE__
90 #define LIBRARY_EXT ".dylib"
91 #define LIBRARY_MINEXT ".dylib"
92 #elif defined __linux__
93 #define LIBRARY_EXT LIB_SUFFIX
94 #define LIBRARY_MINEXT ".so"
98 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
100 size_t len, temp_len;
101 char mm_path[PLATFORM_MAX_PATH * 2];
104 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
107 len = strlen(mm_path);
108 temp_len = strlen("server" LIBRARY_EXT);
112 /* Build log file name */
113 mm_path[len - temp_len] = '\0';
114 mm_Format(mm_fatal_logfile,
115 sizeof(mm_fatal_logfile),
116 "%smetamod-fatal.log",
119 /* Replace server.dll with the new binary we want */
120 mm_Format(&mm_path[len - temp_len],
121 sizeof(mm_path) - (len - temp_len),
122 "metamod.%s" LIBRARY_MINEXT,
123 backend_names[backend]);
125 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
127 return (mm_library != NULL);
131 mm_UnloadMetamodLibrary()
133 mm_UnloadLibrary(mm_library);
138 #define EXPORT extern "C" __declspec(dllexport)
139 #elif defined __GNUC__
141 #define EXPORT extern "C" __attribute__ ((visibility("default")))
143 #define EXPORT extern "C"
148 CreateInterface(const char *name, int *ret)
150 /* If we've got a VSP bridge, do nothing. */
151 if (vsp_bridge != NULL)
159 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
161 /* Either load as VSP or start VSP listener */
162 ptr = mm_GetVspCallbacks(atoi(&name[22]));
164 else if (gamedll_bridge == NULL)
166 /* Load as gamedll */
167 ptr = mm_GameDllRequest(name, ret);
171 /* If we've got a gamedll bridge, forward the request. */
172 return gamedll_bridge->QueryInterface(name, ret);
176 *ret = (ptr != NULL) ? 0 : 1;
182 mm_GetProcAddress(const char *name)
184 return mm_GetLibAddress(mm_library, name);
188 mm_GetGameName(char *buffer, size_t size)
193 static char game[128];
195 LPWSTR pCmdLine = GetCommandLineW();
197 LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc);
198 for (int i = 0; i < argc; ++i)
200 if (wcscmp(wargv[i], L"-game") != 0)
206 wcstombs(buffer, wargv[i], size);
207 buffer[size-1] = '\0';
213 return buffer[0] != 0;
214 #elif defined __APPLE__
215 int argc = *_NSGetArgc();
216 char **argv = *_NSGetArgv();
217 for (int i = 0; i < argc; ++i)
219 if (strcmp(argv[i], "-game") != 0)
225 strncpy(buffer, argv[i], size);
226 buffer[size-1] = '\0';
230 return buffer[0] != 0;
231 #elif defined __linux__
232 FILE *pFile = fopen("/proc/self/cmdline", "rb");
238 bool bNextIsGame = false;
240 while (getdelim(&arg, &argsize, 0, pFile) != -1)
244 strncpy(buffer, arg, size);
245 buffer[size-1] = '\0';
249 if (strcmp(arg, "-game") == 0)
258 return buffer[0] != 0;
260 #error unsupported platform
265 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
267 if (engineFactory("VEngineServer024", NULL) != NULL)
269 return MMBackend_DOTA;
271 else if (engineFactory("VEngineServer023", NULL) != NULL)
273 return MMBackend_CSGO;
275 else if (engineFactory("VEngineServer022", NULL) != NULL &&
276 engineFactory("VEngineCvar007", NULL) != NULL)
278 if (strcmp(game_name, "portal2") == 0)
280 return MMBackend_Portal2;
282 if (engineFactory("EngineTraceServer004", NULL) != NULL)
284 return MMBackend_AlienSwarm;
286 else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
288 return MMBackend_Left4Dead2;
290 return MMBackend_Left4Dead;
292 else if (engineFactory("VEngineServer021", NULL) != NULL)
295 if (engineFactory("VEngineCvar004", NULL) != NULL)
297 if (engineFactory("VModelInfoServer002", NULL) != NULL)
299 /* BGT has same iface version numbers and libs as ep2 */
300 if (strcmp(game_name, "pm") == 0)
302 return MMBackend_BloodyGoodTime;
306 return MMBackend_Episode2;
309 else if (engineFactory("VModelInfoServer003", NULL) != NULL)
311 if (engineFactory("VFileSystem017", NULL) != NULL)
313 return MMBackend_EYE;
315 else if (strcmp(game_name, "cstrike") == 0)
317 return MMBackend_CSS;
321 return MMBackend_Episode2Valve;
325 /* Check for Episode One/Old Engine */
326 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
327 (engineFactory("VEngineCvar003", NULL) != NULL ||
328 engineFactory("VEngineCvar002", NULL) != NULL))
330 /* Check for Dark Messiah which has a weird directory structure */
331 if (strcmp(game_name, ".") == 0)
333 return MMBackend_DarkMessiah;
335 return MMBackend_Episode1;
339 return MMBackend_UNKNOWN;