Merge.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
30 #include <ISmmPluginExt.h>
37 #define DLL_EXPORT extern "C" __declspec(dllexport)
38 #define openlib(lib) LoadLibrary(lib)
39 #define closelib(lib) FreeLibrary(lib)
40 #define findsym(lib, sym) GetProcAddress(lib, sym)
41 #define PLATFORM_EXT ".dll"
42 #define vsnprintf _vsnprintf
43 #define PATH_SEP_CHAR "\\"
44 inline bool IsPathSepChar(char c)
46 return (c == '/' || c == '\\');
50 #define DLL_EXPORT extern "C" __attribute__((visibility("default")))
51 #define openlib(lib) dlopen(lib, RTLD_NOW)
52 #define closelib(lib) dlclose(lib)
53 #define findsym(lib, sym) dlsym(lib, sym)
55 #define PLATFORM_EXT ".so"
56 #elif defined __APPLE__
57 #define PLATFORM_EXT ".dylib"
59 typedef void * HINSTANCE;
60 #define PATH_SEP_CHAR "/"
61 inline bool IsPathSepChar(char c)
68 #define METAMOD_API_MAJOR 2
69 #define FILENAME_1_4_EP1 "sourcemod.1.ep1" PLATFORM_EXT
70 #define FILENAME_1_6_EP2 "sourcemod.2.ep2" PLATFORM_EXT
71 #define FILENAME_1_6_EP2VALVE "sourcemod.2.ep2v" PLATFORM_EXT
72 #define FILENAME_1_6_EP1 "sourcemod.2.ep1" PLATFORM_EXT
73 #define FILENAME_1_6_L4D "sourcemod.2.l4d" PLATFORM_EXT
74 #define FILENAME_1_6_DARKM "sourcemod.2.darkm" PLATFORM_EXT
75 #define FILENAME_1_6_L4D2 "sourcemod.2.l4d2" PLATFORM_EXT
76 #define FILENAME_1_6_SWARM "sourcemod.2.swarm" PLATFORM_EXT
77 #define FILENAME_1_6_BGT "sourcemod.2.bgt" PLATFORM_EXT
78 #define FILENAME_1_6_EYE "sourcemod.2.eye" PLATFORM_EXT
79 #define FILENAME_1_6_PORTAL2 "sourcemod.2.portal2" PLATFORM_EXT
80 #define FILENAME_1_6_CSGO "sourcemod.2.csgo" PLATFORM_EXT
81 #define FILENAME_1_6_CSS "sourcemod.2.css" PLATFORM_EXT
83 HINSTANCE g_hCore = NULL;
84 bool load_attempted = false;
86 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
88 class FailPlugin : public SourceMM::ISmmFailPlugin
96 bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
98 if (error != NULL && maxlength != 0)
100 UTIL_Format(error, maxlength, "%s", error_buffer);
106 char error_buffer[512];
109 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
114 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
117 if (len >= maxlength)
126 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
128 METAMOD_FN_ORIG_LOAD fn;
132 if (!(g_hCore=openlib(path)))
134 #if defined __linux__ || defined __APPLE__
135 UTIL_Format(s_FailPlugin.error_buffer,
136 sizeof(s_FailPlugin.error_buffer),
140 DWORD err = GetLastError();
143 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
146 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
147 s_FailPlugin.error_buffer,
148 sizeof(s_FailPlugin.error_buffer),
152 UTIL_Format(s_FailPlugin.error_buffer,
153 sizeof(s_FailPlugin.error_buffer),
159 s_FailPlugin.fail_version = fail_api;
161 return (METAMOD_PLUGIN *)&s_FailPlugin;
164 if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
169 pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
182 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
185 MEMORY_BASIC_INFORMATION mem;
186 if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
188 if (mem.AllocationBase == NULL)
190 HMODULE dll = (HMODULE)mem.AllocationBase;
191 GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
194 if (!dladdr(pAddr, &info))
196 if (!info.dli_fbase || !info.dli_fname)
198 const char *dllpath = info.dli_fname;
199 snprintf(buffer, maxlength, "%s", dllpath);
204 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
206 const char *filename;
208 load_attempted = true;
210 if (mvi->api_major > METAMOD_API_MAJOR)
215 switch (mvi->source_engine)
217 case SOURCE_ENGINE_ORIGINAL:
218 case SOURCE_ENGINE_EPISODEONE:
220 filename = FILENAME_1_6_EP1;
223 case SOURCE_ENGINE_ORANGEBOX:
225 filename = FILENAME_1_6_EP2;
228 case SOURCE_ENGINE_LEFT4DEAD:
230 filename = FILENAME_1_6_L4D;
233 case SOURCE_ENGINE_DARKMESSIAH:
235 filename = FILENAME_1_6_DARKM;
238 case SOURCE_ENGINE_ORANGEBOXVALVE:
240 filename = FILENAME_1_6_EP2VALVE;
243 case SOURCE_ENGINE_LEFT4DEAD2:
245 filename = FILENAME_1_6_L4D2;
248 case SOURCE_ENGINE_ALIENSWARM:
250 filename = FILENAME_1_6_SWARM;
253 case SOURCE_ENGINE_BLOODYGOODTIME:
255 filename = FILENAME_1_6_BGT;
258 case SOURCE_ENGINE_EYE:
260 filename = FILENAME_1_6_EYE;
263 case SOURCE_ENGINE_PORTAL2:
265 filename = FILENAME_1_6_PORTAL2;
268 case SOURCE_ENGINE_CSGO:
270 filename = FILENAME_1_6_CSGO;
273 case SOURCE_ENGINE_CSS:
275 filename = FILENAME_1_6_CSS;
285 UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
287 return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
290 DLL_EXPORT void UnloadInterface_MMS()
299 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
302 * If a load has already been attempted, bail out immediately.
309 if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
312 char targetfile[256];
314 if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
319 size_t len = strlen(thisfile);
320 for (size_t iter=len-1; iter<len; iter--)
322 if (IsPathSepChar(thisfile[iter]))
324 thisfile[iter] = '\0';
329 UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
331 return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
338 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
340 if (fdwReason == DLL_PROCESS_DETACH)
342 UnloadInterface_MMS();
347 __attribute__((destructor)) static void gcc_fini()
349 UnloadInterface_MMS();
353 /* Overload a few things to prevent libstdc++ linking */
354 #if defined __linux__ || defined __APPLE__
355 extern "C" void __cxa_pure_virtual(void)
359 void *operator new(size_t size)
364 void *operator new[](size_t size)
369 void operator delete(void *ptr)
374 void operator delete[](void * ptr)