loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Mar 09 18:41:25 2010 -0500 (2010-03-09)
changeset 2932 77cd75e433b4
parent 2860 d8f31a35b1cb
child 2982 70d280fed21e
permissions -rw-r--r--
Fixed inaccurate description for sm_unban (bug 4245, r=dvander)
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  */
    29 
    30 #include <ISmmPluginExt.h>
    31 #include <stdarg.h>
    32 #include <string.h>
    33 #include <stdio.h>
    34 #include <stdlib.h>
    35 
    36 #if defined _MSC_VER
    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) 
    45 	{
    46 		return (c == '/' || c == '\\');
    47 	}
    48 	#include <Windows.h>
    49 #else
    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)
    54 	#define PLATFORM_EXT			".so"
    55 	typedef void *					HINSTANCE;
    56 	#define PATH_SEP_CHAR			"/"
    57 	inline bool IsPathSepChar(char c) 
    58 	{
    59 		return (c == '/');
    60 	}
    61 	#include <dlfcn.h>
    62 #endif
    63 
    64 #define METAMOD_API_MAJOR			2
    65 #define FILENAME_1_4_EP1			"sourcemod.1.ep1" PLATFORM_EXT
    66 #define FILENAME_1_6_EP2			"sourcemod.2.ep2" PLATFORM_EXT
    67 #define FILENAME_1_6_EP2VALVE		"sourcemod.2.ep2v" PLATFORM_EXT
    68 #define FILENAME_1_6_EP1			"sourcemod.2.ep1" PLATFORM_EXT
    69 #define FILENAME_1_6_L4D			"sourcemod.2.l4d" PLATFORM_EXT
    70 #define FILENAME_1_6_DARKM			"sourcemod.2.darkm" PLATFORM_EXT
    71 #define FILENAME_1_6_L4D2			"sourcemod.2.l4d2" PLATFORM_EXT
    72 
    73 HINSTANCE g_hCore = NULL;
    74 bool load_attempted = false;
    75 
    76 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    77 
    78 class FailPlugin : public SourceMM::ISmmFailPlugin
    79 {
    80 public:
    81 	int GetApiVersion()
    82 	{
    83 		return fail_version;
    84 	}
    85 
    86 	bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
    87 	{
    88 		if (error != NULL && maxlength != 0)
    89 		{
    90 			UTIL_Format(error, maxlength, "%s", error_buffer);
    91 		}
    92 		return false;
    93 	}
    94 
    95 	int fail_version;
    96 	char error_buffer[512];
    97 } s_FailPlugin;
    98 
    99 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   100 {
   101 	va_list ap;
   102 
   103 	va_start(ap, fmt);
   104 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   105 	va_end(ap);
   106 
   107 	if (len >= maxlength)
   108 	{
   109 		len = maxlength - 1;
   110 		buffer[len] = '\0';
   111 	}
   112 
   113 	return len;
   114 }
   115 
   116 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
   117 {
   118 	METAMOD_FN_ORIG_LOAD fn;
   119 	METAMOD_PLUGIN *pl;
   120 	int ret;
   121 
   122 	if (!(g_hCore=openlib(path)))
   123 	{
   124 #if defined __linux__ || defined __APPLE__
   125 		UTIL_Format(s_FailPlugin.error_buffer, 
   126 			sizeof(s_FailPlugin.error_buffer),
   127 			"%s",
   128 			dlerror());
   129 #else
   130 		DWORD err = GetLastError();
   131 
   132 		if (FormatMessageA(
   133 				FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   134 				NULL,
   135 				err,
   136 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   137 				s_FailPlugin.error_buffer,
   138 				sizeof(s_FailPlugin.error_buffer),
   139 				NULL)
   140 			== 0)
   141 		{
   142 			UTIL_Format(s_FailPlugin.error_buffer, 
   143 				sizeof(s_FailPlugin.error_buffer),
   144 				"unknown error %x",
   145 				err);
   146 		}
   147 #endif
   148 
   149 		s_FailPlugin.fail_version = fail_api;
   150 
   151 		return (METAMOD_PLUGIN *)&s_FailPlugin;
   152 	}
   153 
   154 	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
   155 	{
   156 		goto error;
   157 	}
   158 
   159 	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
   160 	if (!pl)
   161 	{
   162 		goto error;
   163 	}
   164 
   165 	return pl;
   166 error:
   167 	closelib(g_hCore);
   168 	g_hCore = NULL;
   169 	return NULL;
   170 }
   171 
   172 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   173 {
   174 #if defined _MSC_VER
   175 	MEMORY_BASIC_INFORMATION mem;
   176 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   177 		return false;
   178 	if (mem.AllocationBase == NULL)
   179 		return false;
   180 	HMODULE dll = (HMODULE)mem.AllocationBase;
   181 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   182 #else
   183 	Dl_info info;
   184 	if (!dladdr(pAddr, &info))
   185 		return false;
   186 	if (!info.dli_fbase || !info.dli_fname)
   187 		return false;
   188 	const char *dllpath = info.dli_fname;
   189 	snprintf(buffer, maxlength, "%s", dllpath);
   190 #endif
   191 	return true;
   192 }
   193 
   194 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
   195 {
   196 	char *filename;
   197 
   198 	load_attempted = true;
   199 	
   200 	if (mvi->api_major > METAMOD_API_MAJOR)
   201 	{
   202 		return NULL;
   203 	}
   204 
   205 	switch (mvi->source_engine)
   206 	{
   207 	case SOURCE_ENGINE_ORIGINAL:
   208 	case SOURCE_ENGINE_EPISODEONE:
   209 		{
   210 			filename = FILENAME_1_6_EP1;
   211 			break;
   212 		}
   213 	case SOURCE_ENGINE_ORANGEBOX:
   214 		{
   215 			filename = FILENAME_1_6_EP2;
   216 			break;
   217 		}
   218 	case SOURCE_ENGINE_LEFT4DEAD:
   219 		{
   220 			filename = FILENAME_1_6_L4D;
   221 			break;
   222 		}
   223 	case SOURCE_ENGINE_DARKMESSIAH:
   224 		{
   225 			filename = FILENAME_1_6_DARKM;
   226 			break;
   227 		}
   228 	case SOURCE_ENGINE_ORANGEBOXVALVE:
   229 		{
   230 			filename = FILENAME_1_6_EP2VALVE;
   231 			break;
   232 		}
   233 	case SOURCE_ENGINE_LEFT4DEAD2:
   234 		{
   235 			filename = FILENAME_1_6_L4D2;
   236 			break;
   237 		}
   238 	default:
   239 		{
   240 			return NULL;
   241 		}
   242 	}
   243 
   244 	char abspath[256];
   245 	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
   246 
   247 	return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
   248 }
   249 
   250 DLL_EXPORT void UnloadInterface_MMS()
   251 {
   252 	if (g_hCore)
   253 	{
   254 		closelib(g_hCore);
   255 		g_hCore = NULL;
   256 	}
   257 }
   258 
   259 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
   260 {
   261 	/**
   262 	 * If a load has already been attempted, bail out immediately.
   263 	 */
   264 	if (load_attempted)
   265 	{
   266 		return NULL;
   267 	}
   268 
   269 	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
   270 	{
   271 		char thisfile[256];
   272 		char targetfile[256];
   273 
   274 		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
   275 		{
   276 			return NULL;
   277 		}
   278 
   279 		size_t len = strlen(thisfile);
   280 		for (size_t iter=len-1; iter>=0 && iter<len; iter--)
   281 		{
   282 			if (IsPathSepChar(thisfile[iter]))
   283 			{
   284 				thisfile[iter] = '\0';
   285 				break;
   286 			}
   287 		}
   288 
   289 		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
   290 
   291 		return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
   292 	}
   293 
   294 	return NULL;
   295 }
   296 
   297 #if defined _MSC_VER
   298 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
   299 {
   300 	if (fdwReason == DLL_PROCESS_DETACH)
   301 	{
   302 		UnloadInterface_MMS();
   303 	}
   304 	return TRUE;
   305 }
   306 #else
   307 __attribute__((destructor)) static void gcc_fini()
   308 {
   309 	UnloadInterface_MMS();
   310 }
   311 #endif
   312 
   313 /* Overload a few things to prevent libstdc++ linking */
   314 #if defined __linux__ || defined __APPLE__
   315 extern "C" void __cxa_pure_virtual(void)
   316 {
   317 }
   318 
   319 void *operator new(size_t size)
   320 {
   321 	return malloc(size);
   322 }
   323 
   324 void *operator new[](size_t size)
   325 {
   326 	return malloc(size);
   327 }
   328 
   329 void operator delete(void *ptr)
   330 {
   331 	free(ptr);
   332 }
   333 
   334 void operator delete[](void * ptr)
   335 {
   336 	free(ptr);
   337 }
   338 #endif
   339 
   340