1.1 --- a/loader/loader.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,238 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <ISmmPluginExt.h>
1.36 -#include <stdarg.h>
1.37 -#include <string.h>
1.38 -#include <stdio.h>
1.39 -
1.40 -#if defined _MSC_VER
1.41 - #define DLL_EXPORT extern "C" __declspec(dllexport)
1.42 - #define openlib(lib) LoadLibrary(lib)
1.43 - #define closelib(lib) FreeLibrary(lib)
1.44 - #define findsym(lib, sym) GetProcAddress(lib, sym)
1.45 - #define PLATFORM_EXT ".dll"
1.46 - #define vsnprintf _vsnprintf
1.47 - #define PATH_SEP_CHAR "\\"
1.48 - inline bool IsPathSepChar(char c)
1.49 - {
1.50 - return (c == '/' || c == '\\');
1.51 - }
1.52 - #include <Windows.h>
1.53 -#else
1.54 - #define DLL_EXPORT extern "C" __attribute__((visibility("default")))
1.55 - #define openlib(lib) dlopen(lib, RTLD_NOW)
1.56 - #define closelib(lib) dlclose(lib)
1.57 - #define findsym(lib, sym) dlsym(lib, sym)
1.58 - #define PLATFORM_EXT ".so"
1.59 - typedef void * HINSTANCE;
1.60 - #define PATH_SEP_CHAR "/"
1.61 - inline bool IsPathSepChar(char c)
1.62 - {
1.63 - return (c == '/');
1.64 - }
1.65 - #include <dlfcn.h>
1.66 -#endif
1.67 -
1.68 -#define METAMOD_API_MAJOR 2
1.69 -#define FILENAME_1_4_EP1 "sourcemod.1.ep1" PLATFORM_EXT
1.70 -#define FILENAME_1_6_EP2 "sourcemod.2.ep2" PLATFORM_EXT
1.71 -#define FILENAME_1_6_EP1 "sourcemod.2.ep1" PLATFORM_EXT
1.72 -
1.73 -HINSTANCE g_hCore = NULL;
1.74 -bool load_attempted = false;
1.75 -
1.76 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.77 -{
1.78 - va_list ap;
1.79 -
1.80 - va_start(ap, fmt);
1.81 - size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.82 - va_end(ap);
1.83 -
1.84 - if (len >= maxlength)
1.85 - {
1.86 - len = maxlength - 1;
1.87 - buffer[len] = '\0';
1.88 - }
1.89 -
1.90 - return len;
1.91 -}
1.92 -
1.93 -METAMOD_PLUGIN *_GetPluginPtr(const char *path)
1.94 -{
1.95 - METAMOD_FN_ORIG_LOAD fn;
1.96 - METAMOD_PLUGIN *pl;
1.97 - int ret;
1.98 -
1.99 - if (!(g_hCore=openlib(path)))
1.100 - {
1.101 - return NULL;
1.102 - }
1.103 -
1.104 - if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
1.105 - {
1.106 - goto error;
1.107 - }
1.108 -
1.109 - pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
1.110 - if (!pl)
1.111 - {
1.112 - goto error;
1.113 - }
1.114 -
1.115 - return pl;
1.116 -error:
1.117 - closelib(g_hCore);
1.118 - g_hCore = NULL;
1.119 - return NULL;
1.120 -}
1.121 -
1.122 -bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.123 -{
1.124 -#if defined _MSC_VER
1.125 - MEMORY_BASIC_INFORMATION mem;
1.126 - if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.127 - return false;
1.128 - if (mem.AllocationBase == NULL)
1.129 - return false;
1.130 - HMODULE dll = (HMODULE)mem.AllocationBase;
1.131 - GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.132 -#else
1.133 - Dl_info info;
1.134 - if (!dladdr(pAddr, &info))
1.135 - return false;
1.136 - if (!info.dli_fbase || !info.dli_fname)
1.137 - return false;
1.138 - const char *dllpath = info.dli_fname;
1.139 - snprintf(buffer, maxlength, "%s", dllpath);
1.140 -#endif
1.141 - return true;
1.142 -}
1.143 -
1.144 -DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
1.145 -{
1.146 - char *filename;
1.147 -
1.148 - load_attempted = true;
1.149 -
1.150 - if (mvi->api_major > METAMOD_API_MAJOR)
1.151 - {
1.152 - return NULL;
1.153 - }
1.154 -
1.155 - switch (mvi->source_engine)
1.156 - {
1.157 - case SOURCE_ENGINE_ORANGEBOX:
1.158 - {
1.159 - filename = FILENAME_1_6_EP2;
1.160 - break;
1.161 - }
1.162 - case SOURCE_ENGINE_EPISODEONE:
1.163 - case SOURCE_ENGINE_ORIGINAL:
1.164 - {
1.165 - filename = FILENAME_1_6_EP1;
1.166 - break;
1.167 - }
1.168 - default:
1.169 - {
1.170 - return NULL;
1.171 - }
1.172 - }
1.173 -
1.174 - char abspath[256];
1.175 - UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
1.176 -
1.177 - return _GetPluginPtr(abspath);
1.178 -}
1.179 -
1.180 -DLL_EXPORT void UnloadInterface_MMS()
1.181 -{
1.182 - if (g_hCore)
1.183 - {
1.184 - closelib(g_hCore);
1.185 - g_hCore = NULL;
1.186 - }
1.187 -}
1.188 -
1.189 -DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
1.190 -{
1.191 - /**
1.192 - * If a load has already been attempted, bail out immediately.
1.193 - */
1.194 - if (load_attempted)
1.195 - {
1.196 - return NULL;
1.197 - }
1.198 -
1.199 - if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
1.200 - {
1.201 - char thisfile[256];
1.202 - char targetfile[256];
1.203 -
1.204 - if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
1.205 - {
1.206 - return NULL;
1.207 - }
1.208 -
1.209 - size_t len = strlen(thisfile);
1.210 - for (size_t iter=len-1; iter>=0 && iter<len; iter--)
1.211 - {
1.212 - if (IsPathSepChar(thisfile[iter]))
1.213 - {
1.214 - thisfile[iter] = '\0';
1.215 - break;
1.216 - }
1.217 - }
1.218 -
1.219 - UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
1.220 -
1.221 - return _GetPluginPtr(targetfile);
1.222 - }
1.223 -
1.224 - return NULL;
1.225 -}
1.226 -
1.227 -#if defined _MSC_VER
1.228 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1.229 -{
1.230 - if (fdwReason == DLL_PROCESS_DETACH)
1.231 - {
1.232 - UnloadInterface_MMS();
1.233 - }
1.234 - return TRUE;
1.235 -}
1.236 -#else
1.237 -__attribute__((destructor)) static void gcc_fini()
1.238 -{
1.239 - UnloadInterface_MMS();
1.240 -}
1.241 -#endif