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