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