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";
46 mm_LogFatal(const char *message, ...)
53 fp = fopen(mm_fatal_logfile, "at");
54 if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
58 strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
59 fprintf(fp, "L %s: ", header);
61 va_start(ap, message);
62 vfprintf(fp, message, ap);
70 static const char *backend_names[] =
81 #define LIBRARY_EXT ".dll"
82 #define LIBRARY_MINEXT ".dll"
83 #elif defined __linux__
84 #define LIBRARY_EXT BINARY_SUFFIX
85 #define LIBRARY_MINEXT ".so"
89 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
92 char mm_path[PLATFORM_MAX_PATH * 2];
95 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
98 len = strlen(mm_path);
99 temp_len = strlen("server" LIBRARY_EXT);
103 /* Build log file name */
104 mm_path[len - temp_len] = '\0';
105 mm_Format(mm_fatal_logfile,
106 sizeof(mm_fatal_logfile),
107 "%smetamod-fatal.log",
110 /* Replace server.dll with the new binary we want */
111 mm_Format(&mm_path[len - temp_len],
112 sizeof(mm_path) - (len - temp_len),
113 "metamod.%s" LIBRARY_MINEXT,
114 backend_names[backend]);
116 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
118 return (mm_library != NULL);
122 mm_UnloadMetamodLibrary()
124 mm_UnloadLibrary(mm_library);
129 #define EXPORT extern "C" __declspec(dllexport)
130 #elif defined __GNUC__
132 #define EXPORT extern "C" __attribute__ ((visibility("default")))
134 #define EXPORT extern "C"
139 CreateInterface(const char *name, int *ret)
141 /* If we've got a VSP bridge, do nothing. */
142 if (vsp_bridge != NULL)
149 /* If we've got a gamedll bridge, forward the request. */
150 if (gamedll_bridge != NULL)
151 return gamedll_bridge->QueryInterface(name, ret);
153 /* Otherwise, we're probably trying to load Metamod. */
155 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
156 ptr = mm_GetVspCallbacks(atoi(&name[22]));
158 ptr = mm_GameDllRequest(name, ret);
161 *ret = (ptr != NULL) ? 0 : 1;
167 mm_GetProcAddress(const char *name)
169 return mm_GetLibAddress(mm_library, name);
173 #define TIER0_NAME "bin\\tier0.dll"
174 #define VSTDLIB_NAME "bin\\vstdlib.dll"
175 #elif defined __linux__
176 #define TIER0_NAME "bin/tier0" BINARY_SUFFIX
177 #define VSTDLIB_NAME "bin/vstdlib" BINARY_SUFFIX
185 GetCommandLine valve_cmdline;
186 char lib_path[PLATFORM_MAX_PATH];
187 const char *game_name;
189 if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
191 mm_LogFatal("Could not find path for: " TIER0_NAME);
195 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
197 mm_LogFatal("Could not load %s: %s", lib_path, error);
201 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
202 if (valve_cmdline == NULL)
204 /* We probably have a Ship engine. */
205 mm_UnloadLibrary(lib);
206 if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
208 mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
212 if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
214 mm_LogFatal("Could not load %s: %s", lib_path, error);
218 valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
221 mm_UnloadLibrary(lib);
223 if (valve_cmdline == NULL)
225 mm_LogFatal("Could not locate any command line functionality");
229 game_name = valve_cmdline()->ParmValue("-game");
231 /* This probably means that the game directory is actually the current directory */
241 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
244 if (engineFactory("VEngineServer022", NULL) != NULL &&
245 engineFactory("VEngineCvar007", NULL) != NULL)
247 if (engineFactory("VPrecacheSystem001", NULL) != NULL)
249 return MMBackend_Left4Dead2;
251 return MMBackend_Left4Dead;
253 else if (engineFactory("VEngineServer021", NULL) != NULL)
256 if (engineFactory("VEngineCvar004", NULL) != NULL &&
257 engineFactory("VModelInfoServer002", NULL) != NULL)
259 if (strcmp(game_name, "tf") == 0 || strcmp(game_name, "dod") == 0)
261 return MMBackend_Episode2Valve;
263 return MMBackend_Episode2;
265 /* Check for Episode One/Old Engine */
266 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
267 (engineFactory("VEngineCvar003", NULL) != NULL ||
268 engineFactory("VEngineCvar002", NULL) != NULL))
270 /* Check for Dark Messiah which has a weird directory structure */
271 if (strcmp(game_name, ".") == 0)
273 return MMBackend_DarkMessiah;
275 return MMBackend_Episode1;
279 return MMBackend_UNKNOWN;