loader/loader.cpp
author Don <datal30mb@users.sourceforge.net>
Fri Oct 14 11:28:53 2011 -0400 (2011-10-14)
changeset 3411 0a2e40a50757
parent 3373 c6463cdc2180
child 3515 c14c14e06cc1
permissions -rw-r--r--
Added native to clientprefs to alter data on a disconnected player (bug 3882, r=pred).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 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 	#if defined __linux__
    55 	#define PLATFORM_EXT			".so"
    56 	#elif defined __APPLE__
    57 	#define PLATFORM_EXT			".dylib"
    58 	#endif
    59 	typedef void *					HINSTANCE;
    60 	#define PATH_SEP_CHAR			"/"
    61 	inline bool IsPathSepChar(char c) 
    62 	{
    63 		return (c == '/');
    64 	}
    65 	#include <dlfcn.h>
    66 #endif
    67 
    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 
    80 HINSTANCE g_hCore = NULL;
    81 bool load_attempted = false;
    82 
    83 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    84 
    85 class FailPlugin : public SourceMM::ISmmFailPlugin
    86 {
    87 public:
    88 	int GetApiVersion()
    89 	{
    90 		return fail_version;
    91 	}
    92 
    93 	bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
    94 	{
    95 		if (error != NULL && maxlength != 0)
    96 		{
    97 			UTIL_Format(error, maxlength, "%s", error_buffer);
    98 		}
    99 		return false;
   100 	}
   101 
   102 	int fail_version;
   103 	char error_buffer[512];
   104 } s_FailPlugin;
   105 
   106 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   107 {
   108 	va_list ap;
   109 
   110 	va_start(ap, fmt);
   111 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   112 	va_end(ap);
   113 
   114 	if (len >= maxlength)
   115 	{
   116 		len = maxlength - 1;
   117 		buffer[len] = '\0';
   118 	}
   119 
   120 	return len;
   121 }
   122 
   123 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
   124 {
   125 	METAMOD_FN_ORIG_LOAD fn;
   126 	METAMOD_PLUGIN *pl;
   127 	int ret;
   128 
   129 	if (!(g_hCore=openlib(path)))
   130 	{
   131 #if defined __linux__ || defined __APPLE__
   132 		UTIL_Format(s_FailPlugin.error_buffer, 
   133 			sizeof(s_FailPlugin.error_buffer),
   134 			"%s",
   135 			dlerror());
   136 #else
   137 		DWORD err = GetLastError();
   138 
   139 		if (FormatMessageA(
   140 				FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   141 				NULL,
   142 				err,
   143 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   144 				s_FailPlugin.error_buffer,
   145 				sizeof(s_FailPlugin.error_buffer),
   146 				NULL)
   147 			== 0)
   148 		{
   149 			UTIL_Format(s_FailPlugin.error_buffer, 
   150 				sizeof(s_FailPlugin.error_buffer),
   151 				"unknown error %x",
   152 				err);
   153 		}
   154 #endif
   155 
   156 		s_FailPlugin.fail_version = fail_api;
   157 
   158 		return (METAMOD_PLUGIN *)&s_FailPlugin;
   159 	}
   160 
   161 	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
   162 	{
   163 		goto error;
   164 	}
   165 
   166 	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
   167 	if (!pl)
   168 	{
   169 		goto error;
   170 	}
   171 
   172 	return pl;
   173 error:
   174 	closelib(g_hCore);
   175 	g_hCore = NULL;
   176 	return NULL;
   177 }
   178 
   179 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   180 {
   181 #if defined _MSC_VER
   182 	MEMORY_BASIC_INFORMATION mem;
   183 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   184 		return false;
   185 	if (mem.AllocationBase == NULL)
   186 		return false;
   187 	HMODULE dll = (HMODULE)mem.AllocationBase;
   188 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   189 #else
   190 	Dl_info info;
   191 	if (!dladdr(pAddr, &info))
   192 		return false;
   193 	if (!info.dli_fbase || !info.dli_fname)
   194 		return false;
   195 	const char *dllpath = info.dli_fname;
   196 	snprintf(buffer, maxlength, "%s", dllpath);
   197 #endif
   198 	return true;
   199 }
   200 
   201 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
   202 {
   203 	const char *filename;
   204 
   205 	load_attempted = true;
   206 	
   207 	if (mvi->api_major > METAMOD_API_MAJOR)
   208 	{
   209 		return NULL;
   210 	}
   211 
   212 	switch (mvi->source_engine)
   213 	{
   214 	case SOURCE_ENGINE_ORIGINAL:
   215 	case SOURCE_ENGINE_EPISODEONE:
   216 		{
   217 			filename = FILENAME_1_6_EP1;
   218 			break;
   219 		}
   220 	case SOURCE_ENGINE_ORANGEBOX:
   221 		{
   222 			filename = FILENAME_1_6_EP2;
   223 			break;
   224 		}
   225 	case SOURCE_ENGINE_LEFT4DEAD:
   226 		{
   227 			filename = FILENAME_1_6_L4D;
   228 			break;
   229 		}
   230 	case SOURCE_ENGINE_DARKMESSIAH:
   231 		{
   232 			filename = FILENAME_1_6_DARKM;
   233 			break;
   234 		}
   235 	case SOURCE_ENGINE_ORANGEBOXVALVE:
   236 		{
   237 			filename = FILENAME_1_6_EP2VALVE;
   238 			break;
   239 		}
   240 	case SOURCE_ENGINE_LEFT4DEAD2:
   241 		{
   242 			filename = FILENAME_1_6_L4D2;
   243 			break;
   244 		}
   245 	case SOURCE_ENGINE_ALIENSWARM:
   246 		{
   247 			filename = FILENAME_1_6_SWARM;
   248 			break;
   249 		}
   250 	case SOURCE_ENGINE_BLOODYGOODTIME:
   251 		{
   252 			filename = FILENAME_1_6_BGT;
   253 			break;
   254 		}
   255 	case SOURCE_ENGINE_EYE:
   256 		{
   257 			filename = FILENAME_1_6_EYE;
   258 			break;
   259 		}
   260 	default:
   261 		{
   262 			return NULL;
   263 		}
   264 	}
   265 
   266 	char abspath[256];
   267 	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
   268 
   269 	return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
   270 }
   271 
   272 DLL_EXPORT void UnloadInterface_MMS()
   273 {
   274 	if (g_hCore)
   275 	{
   276 		closelib(g_hCore);
   277 		g_hCore = NULL;
   278 	}
   279 }
   280 
   281 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
   282 {
   283 	/**
   284 	 * If a load has already been attempted, bail out immediately.
   285 	 */
   286 	if (load_attempted)
   287 	{
   288 		return NULL;
   289 	}
   290 
   291 	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
   292 	{
   293 		char thisfile[256];
   294 		char targetfile[256];
   295 
   296 		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
   297 		{
   298 			return NULL;
   299 		}
   300 
   301 		size_t len = strlen(thisfile);
   302 		for (size_t iter=len-1; iter<len; iter--)
   303 		{
   304 			if (IsPathSepChar(thisfile[iter]))
   305 			{
   306 				thisfile[iter] = '\0';
   307 				break;
   308 			}
   309 		}
   310 
   311 		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
   312 
   313 		return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
   314 	}
   315 
   316 	return NULL;
   317 }
   318 
   319 #if defined _MSC_VER
   320 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
   321 {
   322 	if (fdwReason == DLL_PROCESS_DETACH)
   323 	{
   324 		UnloadInterface_MMS();
   325 	}
   326 	return TRUE;
   327 }
   328 #else
   329 __attribute__((destructor)) static void gcc_fini()
   330 {
   331 	UnloadInterface_MMS();
   332 }
   333 #endif
   334 
   335 /* Overload a few things to prevent libstdc++ linking */
   336 #if defined __linux__ || defined __APPLE__
   337 extern "C" void __cxa_pure_virtual(void)
   338 {
   339 }
   340 
   341 void *operator new(size_t size)
   342 {
   343 	return malloc(size);
   344 }
   345 
   346 void *operator new[](size_t size)
   347 {
   348 	return malloc(size);
   349 }
   350 
   351 void operator delete(void *ptr)
   352 {
   353 	free(ptr);
   354 }
   355 
   356 void operator delete[](void * ptr)
   357 {
   358 	free(ptr);
   359 }
   360 #endif
   361 
   362