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"
36 #include "valve_commandline.h"
40 typedef ICommandLine *(*GetCommandLine)();
42 static HMODULE mm_library = NULL;
43 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
44 MetamodBackend mm_backend = MMBackend_UNKNOWN;
47 mm_LogFatal(const char *message, ...)
54 fp = fopen(mm_fatal_logfile, "at");
55 if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
59 strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
60 fprintf(fp, "L %s: ", header);
62 va_start(ap, message);
63 vfprintf(fp, message, ap);
71 static const char *backend_names[] =
88 #define LIBRARY_EXT ".dll"
89 #define LIBRARY_MINEXT ".dll"
90 #elif defined __APPLE__
91 #define LIBRARY_EXT ".dylib"
92 #define LIBRARY_MINEXT ".dylib"
93 #elif defined __linux__
94 #define LIBRARY_EXT LIB_SUFFIX
95 #define LIBRARY_MINEXT ".so"
99 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
101 size_t len, temp_len;
102 char mm_path[PLATFORM_MAX_PATH * 2];
105 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
108 len = strlen(mm_path);
109 temp_len = strlen("server" LIBRARY_EXT);
113 /* Build log file name */
114 mm_path[len - temp_len] = '\0';
115 mm_Format(mm_fatal_logfile,
116 sizeof(mm_fatal_logfile),
117 "%smetamod-fatal.log",
120 /* Replace server.dll with the new binary we want */
121 mm_Format(&mm_path[len - temp_len],
122 sizeof(mm_path) - (len - temp_len),
123 "metamod.%s" LIBRARY_MINEXT,
124 backend_names[backend]);
126 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
128 return (mm_library != NULL);
132 mm_UnloadMetamodLibrary()
134 mm_UnloadLibrary(mm_library);
139 #define EXPORT extern "C" __declspec(dllexport)
140 #elif defined __GNUC__
142 #define EXPORT extern "C" __attribute__ ((visibility("default")))
144 #define EXPORT extern "C"
149 CreateInterface(const char *name, int *ret)
151 /* If we've got a VSP bridge, do nothing. */
152 if (vsp_bridge != NULL)
160 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
162 /* Either load as VSP or start VSP listener */
163 ptr = mm_GetVspCallbacks(atoi(&name[22]));
165 else if (gamedll_bridge == NULL)
167 /* Load as gamedll */
168 ptr = mm_GameDllRequest(name, ret);
172 /* If we've got a gamedll bridge, forward the request. */
173 return gamedll_bridge->QueryInterface(name, ret);
177 *ret = (ptr != NULL) ? 0 : 1;
183 mm_GetProcAddress(const char *name)
185 return mm_GetLibAddress(mm_library, name);
189 #define TIER0_NAME "bin\\tier0.dll"
190 #define VSTDLIB_NAME "bin\\vstdlib.dll"
191 #elif defined __APPLE__
192 #define TIER0_NAME "bin/libtier0.dylib"
193 #define VSTDLIB_NAME "bin/libvstdlib.dylib"
194 #elif defined __linux__
195 #define TIER0_NAME "bin/" LIB_PREFIX "tier0" LIB_SUFFIX
196 #define VSTDLIB_NAME "bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
204 GetCommandLine valve_cmdline;
205 char lib_path[PLATFORM_MAX_PATH];
206 const char *game_name;
209 if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path))
210 && !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path))
211 && !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path)))
213 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
216 mm_LogFatal("Could not find path for tier0");
220 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
222 mm_LogFatal("Could not load %s: %s", lib_path, error);
226 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
228 /* '_Tier0' dropped on Alien Swarm version */
229 if (valve_cmdline == NULL)
231 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
234 if (valve_cmdline == NULL)
236 /* We probably have a Ship engine. */
237 mm_UnloadLibrary(lib);
239 if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path))
240 && !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path))
241 && !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path)))
243 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
246 mm_LogFatal("Could not find path for vstdlib");
250 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
252 mm_LogFatal("Could not load %s: %s", lib_path, error);
256 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
259 if (valve_cmdline == NULL)
261 mm_LogFatal("Could not locate any command line functionality");
265 game_name = valve_cmdline()->ParmValue("-game");
267 mm_UnloadLibrary(lib);
269 /* This probably means that the game directory is actually the current directory */
279 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
282 if (engineFactory("VEngineServer023", NULL) != NULL)
284 return MMBackend_CSGO;
286 else if (engineFactory("VEngineServer022", NULL) != NULL &&
287 engineFactory("VEngineCvar007", NULL) != NULL)
289 if (strcmp(game_name, "portal2") == 0)
291 return MMBackend_Portal2;
293 if (engineFactory("EngineTraceServer004", NULL) != NULL)
295 return MMBackend_AlienSwarm;
297 else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
299 return MMBackend_Left4Dead2;
301 return MMBackend_Left4Dead;
303 else if (engineFactory("VEngineServer021", NULL) != NULL)
306 if (engineFactory("VEngineCvar004", NULL) != NULL)
308 if (engineFactory("VModelInfoServer002", NULL) != NULL)
310 /* BGT has same iface version numbers and libs as ep2 */
311 if (strcmp(game_name, "pm") == 0)
313 return MMBackend_BloodyGoodTime;
317 return MMBackend_Episode2;
320 else if (engineFactory("VModelInfoServer003", NULL) != NULL)
322 if (engineFactory("VFileSystem017", NULL) != NULL)
324 return MMBackend_EYE;
326 else if (strcmp(game_name, "cstrike") == 0)
328 return MMBackend_CSS;
332 return MMBackend_Episode2Valve;
336 /* Check for Episode One/Old Engine */
337 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
338 (engineFactory("VEngineCvar003", NULL) != NULL ||
339 engineFactory("VEngineCvar002", NULL) != NULL))
341 /* Check for Dark Messiah which has a weird directory structure */
342 if (strcmp(game_name, ".") == 0)
344 return MMBackend_DarkMessiah;
346 return MMBackend_Episode1;
350 return MMBackend_UNKNOWN;