File saved wrong? Fixed...
3 * =============================================================================
5 * Copyright (C) 2004-2007 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 == '\\');
52 #define DLL_EXPORT extern "C" __attribute__((visibility("default")))
53 #define openlib(lib) dlopen(lib, RTLD_NOW)
54 #define closelib(lib) dlclose(lib)
55 #define findsym(lib, sym) dlsym(lib, sym)
56 #define PLATFORM_EXT ".so"
57 typedef void * HINSTANCE;
58 #define PATH_SEP_CHAR "/"
59 inline bool IsPathSepChar(char c)
66 #define METAMOD_API_MAJOR 2
67 #define FILENAME_1_4_EP1 "sourcemod.1.ep1" PLATFORM_EXT
68 #define FILENAME_1_6_EP2 "sourcemod.2.ep2" PLATFORM_EXT
69 #define FILENAME_1_6_EP1 "sourcemod.2.ep1" PLATFORM_EXT
71 HINSTANCE g_hCore = NULL;
72 bool load_attempted = false;
74 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
76 class FailPlugin : public SourceMM::ISmmFailPlugin
84 bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
86 if (error != NULL && maxlength != 0)
88 UTIL_Format(error, maxlength, "%s", error_buffer);
94 char error_buffer[512];
97 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
102 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
105 if (len >= maxlength)
114 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
116 METAMOD_FN_ORIG_LOAD fn;
120 if (!(g_hCore=openlib(path)))
122 #if defined __linux__
123 UTIL_Format(s_FailPlugin.error_buffer,
124 sizeof(s_FailPlugin.error_buffer),
128 DWORD err = GetLastError();
131 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
134 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
135 s_FailPlugin.error_buffer,
136 sizeof(s_FailPlugin.error_buffer),
140 UTIL_Format(s_FailPlugin.error_buffer,
141 sizeof(s_FailPlugin.error_buffer),
147 s_FailPlugin.fail_version = fail_api;
149 return (METAMOD_PLUGIN *)&s_FailPlugin;
152 if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
157 pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
170 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
173 MEMORY_BASIC_INFORMATION mem;
174 if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
176 if (mem.AllocationBase == NULL)
178 HMODULE dll = (HMODULE)mem.AllocationBase;
179 GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
182 if (!dladdr(pAddr, &info))
184 if (!info.dli_fbase || !info.dli_fname)
186 const char *dllpath = info.dli_fname;
187 snprintf(buffer, maxlength, "%s", dllpath);
192 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
196 load_attempted = true;
198 if (mvi->api_major > METAMOD_API_MAJOR)
203 switch (mvi->source_engine)
205 case SOURCE_ENGINE_ORANGEBOX:
207 filename = FILENAME_1_6_EP2;
210 case SOURCE_ENGINE_EPISODEONE:
211 case SOURCE_ENGINE_ORIGINAL:
213 filename = FILENAME_1_6_EP1;
223 UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
225 return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
228 DLL_EXPORT void UnloadInterface_MMS()
237 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
240 * If a load has already been attempted, bail out immediately.
247 if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
250 char targetfile[256];
252 if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
257 size_t len = strlen(thisfile);
258 for (size_t iter=len-1; iter>=0 && iter<len; iter--)
260 if (IsPathSepChar(thisfile[iter]))
262 thisfile[iter] = '\0';
267 UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
269 return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
276 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
278 if (fdwReason == DLL_PROCESS_DETACH)
280 UnloadInterface_MMS();
285 __attribute__((destructor)) static void gcc_fini()
287 UnloadInterface_MMS();
291 /* Overload a few things to prevent libstdc++ linking */
292 #if defined __linux__
293 extern "C" void __cxa_pure_virtual(void)
297 void *operator new(size_t size)
302 void *operator new[](size_t size)
307 void operator delete(void *ptr)
312 void operator delete[](void * ptr)