loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sun Oct 13 14:15:29 2013 -0400 (2013-10-13)
changeset 4142 0a4da1ca54c9
parent 4141 8ff66ae5ffcc
child 4144 353e86f4c6ab
permissions -rw-r--r--
Added engine support for Blade Symphony (bug 5949, r=asherkin).
     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_EP1			"sourcemod.2.ep1" PLATFORM_EXT
    72 #define FILENAME_1_6_L4D			"sourcemod.2.l4d" PLATFORM_EXT
    73 #define FILENAME_1_6_DARKM			"sourcemod.2.darkm" PLATFORM_EXT
    74 #define FILENAME_1_6_L4D2			"sourcemod.2.l4d2" PLATFORM_EXT
    75 #define FILENAME_1_6_SWARM			"sourcemod.2.swarm" PLATFORM_EXT
    76 #define FILENAME_1_6_BGT			"sourcemod.2.bgt" PLATFORM_EXT
    77 #define FILENAME_1_6_EYE			"sourcemod.2.eye" PLATFORM_EXT
    78 #define FILENAME_1_6_PORTAL2		"sourcemod.2.portal2" PLATFORM_EXT
    79 #define FILENAME_1_6_CSGO			"sourcemod.2.csgo" PLATFORM_EXT
    80 #define FILENAME_1_6_CSS			"sourcemod.2.css" PLATFORM_EXT
    81 #define FILENAME_1_6_HL2DM			"sourcemod.2.hl2dm" PLATFORM_EXT
    82 #define FILENAME_1_6_DODS			"sourcemod.2.dods" PLATFORM_EXT
    83 #define FILENAME_1_6_SDK2013		"sourcemod.2.sdk2013" PLATFORM_EXT
    84 #define FILENAME_1_6_TF2			"sourcemod.2.tf2" PLATFORM_EXT
    85 #define FILENAME_1_6_ND				"sourcemod.2.nd" PLATFORM_EXT
    86 #define FILENAME_1_6_DOTA			"sourcemod.2.dota" PLATFORM_EXT
    87 #define FILENAME_1_6_BLADE			"sourcemod.2.blade" PLATFORM_EXT
    88 
    89 HINSTANCE g_hCore = NULL;
    90 bool load_attempted = false;
    91 
    92 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    93 
    94 class FailPlugin : public SourceMM::ISmmFailPlugin
    95 {
    96 public:
    97 	int GetApiVersion()
    98 	{
    99 		return fail_version;
   100 	}
   101 
   102 	bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
   103 	{
   104 		if (error != NULL && maxlength != 0)
   105 		{
   106 			UTIL_Format(error, maxlength, "%s", error_buffer);
   107 		}
   108 		return false;
   109 	}
   110 
   111 	int fail_version;
   112 	char error_buffer[512];
   113 } s_FailPlugin;
   114 
   115 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   116 {
   117 	va_list ap;
   118 
   119 	va_start(ap, fmt);
   120 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   121 	va_end(ap);
   122 
   123 	if (len >= maxlength)
   124 	{
   125 		len = maxlength - 1;
   126 		buffer[len] = '\0';
   127 	}
   128 
   129 	return len;
   130 }
   131 
   132 METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
   133 {
   134 	METAMOD_FN_ORIG_LOAD fn;
   135 	METAMOD_PLUGIN *pl;
   136 	int ret;
   137 
   138 	if (!(g_hCore=openlib(path)))
   139 	{
   140 #if defined __linux__ || defined __APPLE__
   141 		UTIL_Format(s_FailPlugin.error_buffer, 
   142 			sizeof(s_FailPlugin.error_buffer),
   143 			"%s",
   144 			dlerror());
   145 #else
   146 		DWORD err = GetLastError();
   147 
   148 		if (FormatMessageA(
   149 				FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   150 				NULL,
   151 				err,
   152 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   153 				s_FailPlugin.error_buffer,
   154 				sizeof(s_FailPlugin.error_buffer),
   155 				NULL)
   156 			== 0)
   157 		{
   158 			UTIL_Format(s_FailPlugin.error_buffer, 
   159 				sizeof(s_FailPlugin.error_buffer),
   160 				"unknown error %x",
   161 				err);
   162 		}
   163 #endif
   164 
   165 		s_FailPlugin.fail_version = fail_api;
   166 
   167 		return (METAMOD_PLUGIN *)&s_FailPlugin;
   168 	}
   169 
   170 	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
   171 	{
   172 		goto error;
   173 	}
   174 
   175 	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
   176 	if (!pl)
   177 	{
   178 		goto error;
   179 	}
   180 
   181 	return pl;
   182 error:
   183 	closelib(g_hCore);
   184 	g_hCore = NULL;
   185 	return NULL;
   186 }
   187 
   188 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   189 {
   190 #if defined _MSC_VER
   191 	MEMORY_BASIC_INFORMATION mem;
   192 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   193 		return false;
   194 	if (mem.AllocationBase == NULL)
   195 		return false;
   196 	HMODULE dll = (HMODULE)mem.AllocationBase;
   197 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   198 #else
   199 	Dl_info info;
   200 	if (!dladdr(pAddr, &info))
   201 		return false;
   202 	if (!info.dli_fbase || !info.dli_fname)
   203 		return false;
   204 	const char *dllpath = info.dli_fname;
   205 	snprintf(buffer, maxlength, "%s", dllpath);
   206 #endif
   207 	return true;
   208 }
   209 
   210 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
   211 {
   212 	const char *filename;
   213 
   214 	load_attempted = true;
   215 	
   216 	if (mvi->api_major > METAMOD_API_MAJOR)
   217 	{
   218 		return NULL;
   219 	}
   220 
   221 	switch (mvi->source_engine)
   222 	{
   223 	case SOURCE_ENGINE_ORIGINAL:
   224 	case SOURCE_ENGINE_EPISODEONE:
   225 		{
   226 			filename = FILENAME_1_6_EP1;
   227 			break;
   228 		}
   229 	case SOURCE_ENGINE_ORANGEBOX:
   230 		{
   231 			filename = FILENAME_1_6_EP2;
   232 			break;
   233 		}
   234 	case SOURCE_ENGINE_LEFT4DEAD:
   235 		{
   236 			filename = FILENAME_1_6_L4D;
   237 			break;
   238 		}
   239 	case SOURCE_ENGINE_DARKMESSIAH:
   240 		{
   241 			filename = FILENAME_1_6_DARKM;
   242 			break;
   243 		}
   244 	case SOURCE_ENGINE_LEFT4DEAD2:
   245 		{
   246 			const char *gamedir = mvi->GetGameDir();
   247 			if (strcmp(gamedir, "nucleardawn") == 0)
   248 			{
   249 				filename = FILENAME_1_6_ND;
   250 			}
   251 			else
   252 			{
   253 				filename = FILENAME_1_6_L4D2;
   254 			}
   255 			break;
   256 		}
   257 	case SOURCE_ENGINE_NUCLEARDAWN:
   258 		{
   259 			filename = FILENAME_1_6_ND;
   260 			break;
   261 		}
   262 	case SOURCE_ENGINE_ALIENSWARM:
   263 		{
   264 			filename = FILENAME_1_6_SWARM;
   265 			break;
   266 		}
   267 	case SOURCE_ENGINE_BLOODYGOODTIME:
   268 		{
   269 			filename = FILENAME_1_6_BGT;
   270 			break;
   271 		}
   272 	case SOURCE_ENGINE_EYE:
   273 		{
   274 			filename = FILENAME_1_6_EYE;
   275 			break;
   276 		}
   277 	case SOURCE_ENGINE_PORTAL2:
   278 		{
   279 			filename = FILENAME_1_6_PORTAL2;
   280 			break;
   281 		}
   282 	case SOURCE_ENGINE_CSGO:
   283 		{
   284 			filename = FILENAME_1_6_CSGO;
   285 			break;
   286 		}
   287 	case SOURCE_ENGINE_CSS:
   288 		{
   289 			filename = FILENAME_1_6_CSS;
   290 			break;
   291 		}
   292 	case SOURCE_ENGINE_HL2DM:
   293 		{
   294 			filename = FILENAME_1_6_HL2DM;
   295 			break;
   296 		}
   297 	case SOURCE_ENGINE_DODS:
   298 		{
   299 			filename = FILENAME_1_6_DODS;
   300 			break;
   301 		}
   302 	case SOURCE_ENGINE_SDK2013:
   303 		{
   304 			filename = FILENAME_1_6_SDK2013;
   305 			break;
   306 		}
   307 	case SOURCE_ENGINE_TF2:
   308 		{
   309 			filename = FILENAME_1_6_TF2;
   310 			break;
   311 		}
   312 	case SOURCE_ENGINE_ORANGEBOXVALVE_DEPRECATED:
   313 		{
   314 			const char *gamedir = mvi->GetGameDir();
   315 			if (strcmp(gamedir, "tf") == 0)
   316 			{
   317 				filename = FILENAME_1_6_TF2;
   318 			}
   319 			else if (strcmp(gamedir, "dod") == 0)
   320 			{
   321 				filename = FILENAME_1_6_DODS;
   322 			}
   323 			else if (strcmp(gamedir, "hl2mp") == 0)
   324 			{
   325 				filename = FILENAME_1_6_HL2DM;
   326 			}
   327 			else
   328 			{
   329 				return NULL;
   330 			}
   331 			break;
   332 		}
   333 	case SOURCE_ENGINE_DOTA:
   334 		{
   335 			filename = FILENAME_1_6_DOTA;
   336 			break;
   337 		}
   338 	case SOURCE_ENGINE_BLADE:
   339 		{
   340 			filename = FILENAME_1_6_BLADE;
   341 			break;
   342 		}
   343 	default:
   344 		{
   345 			return NULL;
   346 		}
   347 	}
   348 
   349 	char abspath[256];
   350 	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
   351 
   352 	return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
   353 }
   354 
   355 DLL_EXPORT void UnloadInterface_MMS()
   356 {
   357 	if (g_hCore)
   358 	{
   359 		closelib(g_hCore);
   360 		g_hCore = NULL;
   361 	}
   362 }
   363 
   364 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
   365 {
   366 	/**
   367 	 * If a load has already been attempted, bail out immediately.
   368 	 */
   369 	if (load_attempted)
   370 	{
   371 		return NULL;
   372 	}
   373 
   374 	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
   375 	{
   376 		char thisfile[256];
   377 		char targetfile[256];
   378 
   379 		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
   380 		{
   381 			return NULL;
   382 		}
   383 
   384 		size_t len = strlen(thisfile);
   385 		for (size_t iter=len-1; iter<len; iter--)
   386 		{
   387 			if (IsPathSepChar(thisfile[iter]))
   388 			{
   389 				thisfile[iter] = '\0';
   390 				break;
   391 			}
   392 		}
   393 
   394 		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
   395 
   396 		return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
   397 	}
   398 
   399 	return NULL;
   400 }
   401 
   402 #if defined _MSC_VER
   403 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
   404 {
   405 	if (fdwReason == DLL_PROCESS_DETACH)
   406 	{
   407 		UnloadInterface_MMS();
   408 	}
   409 	return TRUE;
   410 }
   411 #else
   412 __attribute__((destructor)) static void gcc_fini()
   413 {
   414 	UnloadInterface_MMS();
   415 }
   416 #endif
   417 
   418 /* Overload a few things to prevent libstdc++ linking */
   419 #if defined __linux__ || defined __APPLE__
   420 extern "C" void __cxa_pure_virtual(void)
   421 {
   422 }
   423 
   424 void *operator new(size_t size)
   425 {
   426 	return malloc(size);
   427 }
   428 
   429 void *operator new[](size_t size)
   430 {
   431 	return malloc(size);
   432 }
   433 
   434 void operator delete(void *ptr)
   435 {
   436 	free(ptr);
   437 }
   438 
   439 void operator delete[](void * ptr)
   440 {
   441 	free(ptr);
   442 }
   443 #endif
   444 
   445