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