Updated changelog, added license headers.
3 * ======================================================
5 * Copyright (C) 2004-2008 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.
35 #include "serverplugin.h"
39 static HMODULE mm_library = NULL;
40 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
43 mm_LogFatal(const char *message, ...)
50 fp = fopen(mm_fatal_logfile, "at");
51 if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
55 strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
56 fprintf(fp, "L %s: ", header);
58 va_start(ap, message);
59 vfprintf(fp, message, ap);
67 static const char *backend_names[3] =
75 #define LIBRARY_EXT ".dll"
76 #define LIBRARY_MINEXT ".dll"
77 #elif defined __linux__
78 #define LIBRARY_EXT "_i486.so"
79 #define LIBRARY_MINEXT ".so"
83 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
86 char mm_path[PLATFORM_MAX_PATH * 2];
89 if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
92 len = strlen(mm_path);
93 temp_len = strlen("server" LIBRARY_EXT);
97 /* Build log file name */
98 mm_path[len - temp_len] = '\0';
99 mm_Format(mm_fatal_logfile,
100 sizeof(mm_fatal_logfile),
101 "%smetamod-fatal.log",
104 /* Replace server.dll with the new binary we want */
105 mm_Format(&mm_path[len - temp_len],
106 sizeof(mm_path) - (len - temp_len),
107 "metamod.%s" LIBRARY_MINEXT,
108 backend_names[backend]);
110 mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
112 return (mm_library != NULL);
116 mm_UnloadMetamodLibrary()
118 mm_UnloadLibrary(mm_library);
123 #define EXPORT extern "C" __declspec(dllexport)
124 #elif defined __GNUC__
126 #define EXPORT extern "C" __attribute__ ((visibility("default")))
128 #define EXPORT extern "C"
133 CreateInterface(const char *name, int *ret)
135 /* If we've got a VSP bridge, do nothing. */
136 if (vsp_bridge != NULL)
143 /* If we've got a gamedll bridge, forward the request. */
144 if (gamedll_bridge != NULL)
145 return gamedll_bridge->QueryInterface(name, ret);
147 /* Otherwise, we're probably trying to load Metamod. */
149 if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
150 ptr = mm_GetVspCallbacks(atoi(&name[22]));
152 ptr = mm_GameDllRequest(name, ret);
155 *ret = (ptr != NULL) ? 0 : 1;
161 mm_GetProcAddress(const char *name)
163 return mm_GetLibAddress(mm_library, name);
167 mm_DetermineBackend(QueryValveInterface engineFactory)
170 if (engineFactory("VEngineServer022", NULL) != NULL &&
171 engineFactory("VEngineCvar007", NULL) != NULL)
173 return MMBackend_Left4Dead;
175 else if (engineFactory("VEngineServer021", NULL) != NULL)
178 if (engineFactory("VEngineCvar004", NULL) != NULL &&
179 engineFactory("VModelInfoServer002", NULL) != NULL)
181 return MMBackend_Episode2;
184 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
185 (engineFactory("VEngineCvar003", NULL) != NULL ||
186 engineFactory("VEngineCvar002", NULL) != NULL))
188 return MMBackend_Episode1;
192 return MMBackend_UNKNOWN;