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