3 * =============================================================================
5 * Copyright (C) 2004-2008 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>.
32 #include <ISmmPluginExt.h>
39 #define DLL_EXPORT extern "C" __declspec(dllexport)
40 #define openlib(lib) LoadLibrary(lib)
41 #define closelib(lib) FreeLibrary(lib)
42 #define findsym(lib, sym) GetProcAddress(lib, sym)
43 #define PLATFORM_EXT ".dll"
44 #define vsnprintf _vsnprintf
45 #define PATH_SEP_CHAR "\\"
46 inline bool IsPathSepChar(char c)
48 return (c == '/' || c == '\\');
51 #define TF_PATH "\\tf\\"
52 #define DOD_PATH "\\dod\\"
54 #define DLL_EXPORT extern "C" __attribute__((visibility("default")))
55 #define openlib(lib) dlopen(lib, RTLD_NOW)
56 #define closelib(lib) dlclose(lib)
57 #define findsym(lib, sym) dlsym(lib, sym)
58 #define PLATFORM_EXT ".so"
59 typedef void * HINSTANCE;
60 #define PATH_SEP_CHAR "/"
61 inline bool IsPathSepChar(char c)
66 #define TF_PATH "/tf/"
67 #define DOD_PATH "/dod/"
70 #define METAMOD_API_MAJOR 2
71 #define FILENAME_1_4_EP1 "sourcemod.1.ep1" PLATFORM_EXT
72 #define FILENAME_1_6_EP2 "sourcemod.2.ep2" PLATFORM_EXT
73 #define FILENAME_1_6_EP2VALVE "sourcemod.2.ep2v" PLATFORM_EXT
74 #define FILENAME_1_6_EP1 "sourcemod.2.ep1" PLATFORM_EXT
75 #define FILENAME_1_6_L4D "sourcemod.2.l4d" PLATFORM_EXT
76 #define FILENAME_1_6_DARKM "sourcemod.2.darkm" PLATFORM_EXT
78 HINSTANCE g_hCore = NULL;
79 bool load_attempted = false;
81 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
83 class FailPlugin : public SourceMM::ISmmFailPlugin
91 bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
93 if (error != NULL && maxlength != 0)
95 UTIL_Format(error, maxlength, "%s", error_buffer);
101 char error_buffer[512];
104 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
109 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
112 if (len >= maxlength)
121 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
123 METAMOD_FN_ORIG_LOAD fn;
127 if (!(g_hCore=openlib(path)))
129 #if defined __linux__ || defined __APPLE__
130 UTIL_Format(s_FailPlugin.error_buffer,
131 sizeof(s_FailPlugin.error_buffer),
135 DWORD err = GetLastError();
138 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
141 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
142 s_FailPlugin.error_buffer,
143 sizeof(s_FailPlugin.error_buffer),
147 UTIL_Format(s_FailPlugin.error_buffer,
148 sizeof(s_FailPlugin.error_buffer),
154 s_FailPlugin.fail_version = fail_api;
156 return (METAMOD_PLUGIN *)&s_FailPlugin;
159 if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
164 pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
177 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
180 MEMORY_BASIC_INFORMATION mem;
181 if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
183 if (mem.AllocationBase == NULL)
185 HMODULE dll = (HMODULE)mem.AllocationBase;
186 GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
189 if (!dladdr(pAddr, &info))
191 if (!info.dli_fbase || !info.dli_fname)
193 const char *dllpath = info.dli_fname;
194 snprintf(buffer, maxlength, "%s", dllpath);
199 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
203 load_attempted = true;
205 if (mvi->api_major > METAMOD_API_MAJOR)
210 switch (mvi->source_engine)
212 case SOURCE_ENGINE_ORIGINAL:
213 case SOURCE_ENGINE_EPISODEONE:
215 filename = FILENAME_1_6_EP1;
218 case SOURCE_ENGINE_ORANGEBOX:
220 if (strstr(mli->pl_path, TF_PATH) || strstr(mli->pl_path, DOD_PATH))
222 filename = FILENAME_1_6_EP2VALVE;
226 filename = FILENAME_1_6_EP2;
229 case SOURCE_ENGINE_LEFT4DEAD:
231 filename = FILENAME_1_6_L4D;
234 case SOURCE_ENGINE_DARKMESSIAH:
236 filename = FILENAME_1_6_DARKM;
246 UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
248 return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
251 DLL_EXPORT void UnloadInterface_MMS()
260 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
263 * If a load has already been attempted, bail out immediately.
270 if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
273 char targetfile[256];
275 if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
280 size_t len = strlen(thisfile);
281 for (size_t iter=len-1; iter>=0 && iter<len; iter--)
283 if (IsPathSepChar(thisfile[iter]))
285 thisfile[iter] = '\0';
290 UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
292 return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
299 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
301 if (fdwReason == DLL_PROCESS_DETACH)
303 UnloadInterface_MMS();
308 __attribute__((destructor)) static void gcc_fini()
310 UnloadInterface_MMS();
314 /* Overload a few things to prevent libstdc++ linking */
315 #if defined __linux__ || defined __APPLE__
316 extern "C" void __cxa_pure_virtual(void)
320 void *operator new(size_t size)
325 void *operator new[](size_t size)
330 void operator delete(void *ptr)
335 void operator delete[](void * ptr)