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 "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[] =
82 #define LIBRARY_EXT ".dll"
83 #define LIBRARY_MINEXT ".dll"
84 #elif defined __linux__
85 #define LIBRARY_EXT LIB_SUFFIX
86 #define LIBRARY_MINEXT ".so"
90 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
93 char mm_path[PLATFORM_MAX_PATH * 2];
96 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
99 len = strlen(mm_path);
100 temp_len = strlen("server" LIBRARY_EXT);
104 /* Build log file name */
105 mm_path[len - temp_len] = '\0';
106 mm_Format(mm_fatal_logfile,
107 sizeof(mm_fatal_logfile),
108 "%smetamod-fatal.log",
111 /* Replace server.dll with the new binary we want */
112 mm_Format(&mm_path[len - temp_len],
113 sizeof(mm_path) - (len - temp_len),
114 "metamod.%s" LIBRARY_MINEXT,
115 backend_names[backend]);
117 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
119 return (mm_library != NULL);
123 mm_UnloadMetamodLibrary()
125 mm_UnloadLibrary(mm_library);
130 #define EXPORT extern "C" __declspec(dllexport)
131 #elif defined __GNUC__
133 #define EXPORT extern "C" __attribute__ ((visibility("default")))
135 #define EXPORT extern "C"
140 CreateInterface(const char *name, int *ret)
142 /* If we've got a VSP bridge, do nothing. */
143 if (vsp_bridge != NULL)
151 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
153 /* Either load as VSP or start VSP listener */
154 ptr = mm_GetVspCallbacks(atoi(&name[22]));
156 else if (gamedll_bridge == NULL)
158 /* Load as gamedll */
159 ptr = mm_GameDllRequest(name, ret);
163 /* If we've got a gamedll bridge, forward the request. */
164 return gamedll_bridge->QueryInterface(name, ret);
168 *ret = (ptr != NULL) ? 0 : 1;
174 mm_GetProcAddress(const char *name)
176 return mm_GetLibAddress(mm_library, name);
180 #define TIER0_NAME "bin\\tier0.dll"
181 #define VSTDLIB_NAME "bin\\vstdlib.dll"
182 #elif defined __linux__
183 #define TIER0_NAME "bin/" LIB_PREFIX "tier0" LIB_SUFFIX
184 #define VSTDLIB_NAME "bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
192 GetCommandLine valve_cmdline;
193 char lib_path[PLATFORM_MAX_PATH];
194 const char *game_name;
196 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
198 mm_LogFatal("Could not find path for: " TIER0_NAME);
202 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
204 mm_LogFatal("Could not load %s: %s", lib_path, error);
208 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
209 if (valve_cmdline == NULL)
211 /* We probably have a Ship engine. */
212 mm_UnloadLibrary(lib);
213 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
215 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
219 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
221 mm_LogFatal("Could not load %s: %s", lib_path, error);
225 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
228 mm_UnloadLibrary(lib);
230 if (valve_cmdline == NULL)
232 mm_LogFatal("Could not locate any command line functionality");
236 game_name = valve_cmdline()->ParmValue("-game");
238 /* This probably means that the game directory is actually the current directory */
248 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
251 if (engineFactory("VEngineServer022", NULL) != NULL &&
252 engineFactory("VEngineCvar007", NULL) != NULL)
254 if (engineFactory("VPrecacheSystem001", NULL) != NULL)
256 return MMBackend_Left4Dead2;
258 return MMBackend_Left4Dead;
260 else if (engineFactory("VEngineServer021", NULL) != NULL)
263 if (engineFactory("VEngineCvar004", NULL) != NULL &&
264 engineFactory("VModelInfoServer002", NULL) != NULL)
266 if (strcmp(game_name, "tf") == 0 || strcmp(game_name, "dod") == 0)
268 return MMBackend_Episode2Valve;
270 return MMBackend_Episode2;
272 /* Check for Episode One/Old Engine */
273 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
274 (engineFactory("VEngineCvar003", NULL) != NULL ||
275 engineFactory("VEngineCvar002", NULL) != NULL))
277 /* Check for Dark Messiah which has a weird directory structure */
278 if (strcmp(game_name, ".") == 0)
280 return MMBackend_DarkMessiah;
282 return MMBackend_Episode1;
286 return MMBackend_UNKNOWN;