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