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