loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Mar 19 11:18:44 2013 -0400 (2013-03-19)
changeset 3853 3c8f57923db5
parent 3613 b0740093756d
child 3961 0dc477c29792
permissions -rw-r--r--
Added basic support for Dota 2 (bug 5656, r=asherkin).
faluco@1557
     1
/**
ds@2860
     2
 * vim: set ts=4 sw=4 tw=99 noet :
faluco@1557
     3
 * =============================================================================
faluco@1557
     4
 * SourceMod
ds@3099
     5
 * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
faluco@1557
     6
 * =============================================================================
faluco@1557
     7
 *
faluco@1557
     8
 * This program is free software; you can redistribute it and/or modify it under
faluco@1557
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
faluco@1557
    10
 * Free Software Foundation.
faluco@1557
    11
 * 
faluco@1557
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
faluco@1557
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
faluco@1557
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
faluco@1557
    15
 * details.
faluco@1557
    16
 *
faluco@1557
    17
 * You should have received a copy of the GNU General Public License along with
faluco@1557
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
faluco@1557
    19
 *
faluco@1557
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
faluco@1557
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
faluco@1557
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
faluco@1557
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
faluco@1557
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
faluco@1557
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
faluco@1557
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
faluco@1557
    27
 * or <http://www.sourcemod.net/license.php>.
faluco@1557
    28
 */
faluco@1557
    29
faluco@1557
    30
#include <ISmmPluginExt.h>
faluco@1557
    31
#include <stdarg.h>
faluco@1557
    32
#include <string.h>
faluco@1557
    33
#include <stdio.h>
dvander@2471
    34
#include <stdlib.h>
faluco@1557
    35
faluco@1557
    36
#if defined _MSC_VER
faluco@1557
    37
	#define DLL_EXPORT				extern "C" __declspec(dllexport)
faluco@1557
    38
	#define openlib(lib)			LoadLibrary(lib)
faluco@1557
    39
	#define closelib(lib)			FreeLibrary(lib)
faluco@1557
    40
	#define findsym(lib, sym)		GetProcAddress(lib, sym)
faluco@1557
    41
	#define PLATFORM_EXT			".dll"
faluco@1557
    42
	#define vsnprintf				_vsnprintf
faluco@1557
    43
	#define PATH_SEP_CHAR			"\\"
faluco@1557
    44
	inline bool IsPathSepChar(char c) 
faluco@1557
    45
	{
faluco@1557
    46
		return (c == '/' || c == '\\');
faluco@1557
    47
	}
faluco@1557
    48
	#include <Windows.h>
faluco@1557
    49
#else
faluco@1557
    50
	#define DLL_EXPORT				extern "C" __attribute__((visibility("default")))
faluco@1557
    51
	#define openlib(lib)			dlopen(lib, RTLD_NOW)
faluco@1557
    52
	#define closelib(lib)			dlclose(lib)
faluco@1557
    53
	#define findsym(lib, sym)		dlsym(lib, sym)
ds@2997
    54
	#if defined __linux__
faluco@1557
    55
	#define PLATFORM_EXT			".so"
ds@2997
    56
	#elif defined __APPLE__
ds@2997
    57
	#define PLATFORM_EXT			".dylib"
ds@2997
    58
	#endif
faluco@1557
    59
	typedef void *					HINSTANCE;
faluco@1557
    60
	#define PATH_SEP_CHAR			"/"
faluco@1557
    61
	inline bool IsPathSepChar(char c) 
faluco@1557
    62
	{
faluco@1557
    63
		return (c == '/');
faluco@1557
    64
	}
faluco@1557
    65
	#include <dlfcn.h>
faluco@1557
    66
#endif
faluco@1557
    67
faluco@1557
    68
#define METAMOD_API_MAJOR			2
faluco@1557
    69
#define FILENAME_1_4_EP1			"sourcemod.1.ep1" PLATFORM_EXT
faluco@1557
    70
#define FILENAME_1_6_EP2			"sourcemod.2.ep2" PLATFORM_EXT
dvander@2782
    71
#define FILENAME_1_6_EP2VALVE		"sourcemod.2.ep2v" PLATFORM_EXT
faluco@1557
    72
#define FILENAME_1_6_EP1			"sourcemod.2.ep1" PLATFORM_EXT
ds@2583
    73
#define FILENAME_1_6_L4D			"sourcemod.2.l4d" PLATFORM_EXT
ds@2583
    74
#define FILENAME_1_6_DARKM			"sourcemod.2.darkm" PLATFORM_EXT
fyrenmoo@2847
    75
#define FILENAME_1_6_L4D2			"sourcemod.2.l4d2" PLATFORM_EXT
ds@3099
    76
#define FILENAME_1_6_SWARM			"sourcemod.2.swarm" PLATFORM_EXT
skamonkey@3180
    77
#define FILENAME_1_6_BGT			"sourcemod.2.bgt" PLATFORM_EXT
skamonkey@3373
    78
#define FILENAME_1_6_EYE			"sourcemod.2.eye" PLATFORM_EXT
skamonkey@3515
    79
#define FILENAME_1_6_PORTAL2		"sourcemod.2.portal2" PLATFORM_EXT
skamonkey@3515
    80
#define FILENAME_1_6_CSGO			"sourcemod.2.csgo" PLATFORM_EXT
skamonkey@3613
    81
#define FILENAME_1_6_CSS			"sourcemod.2.css" PLATFORM_EXT
skamonkey@3853
    82
#define FILENAME_1_6_DOTA			"sourcemod.2.dota" PLATFORM_EXT
faluco@1557
    83
faluco@1557
    84
HINSTANCE g_hCore = NULL;
dvander@1561
    85
bool load_attempted = false;
faluco@1557
    86
dvander@1840
    87
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
dvander@1840
    88
dvander@1840
    89
class FailPlugin : public SourceMM::ISmmFailPlugin
dvander@1840
    90
{
dvander@1840
    91
public:
dvander@1840
    92
	int GetApiVersion()
dvander@1840
    93
	{
dvander@1840
    94
		return fail_version;
dvander@1840
    95
	}
dvander@1840
    96
dvander@1840
    97
	bool Load(SourceMM::PluginId id, SourceMM::ISmmAPI *ismm, char *error, size_t maxlength, bool late)
dvander@1840
    98
	{
dvander@1840
    99
		if (error != NULL && maxlength != 0)
dvander@1840
   100
		{
dvander@1840
   101
			UTIL_Format(error, maxlength, "%s", error_buffer);
dvander@1840
   102
		}
dvander@1840
   103
		return false;
dvander@1840
   104
	}
dvander@1840
   105
dvander@1840
   106
	int fail_version;
dvander@1840
   107
	char error_buffer[512];
dvander@1840
   108
} s_FailPlugin;
dvander@1840
   109
faluco@1557
   110
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
faluco@1557
   111
{
faluco@1557
   112
	va_list ap;
faluco@1557
   113
faluco@1557
   114
	va_start(ap, fmt);
faluco@1557
   115
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
faluco@1557
   116
	va_end(ap);
faluco@1557
   117
faluco@1557
   118
	if (len >= maxlength)
faluco@1557
   119
	{
faluco@1557
   120
		len = maxlength - 1;
faluco@1557
   121
		buffer[len] = '\0';
faluco@1557
   122
	}
faluco@1557
   123
faluco@1557
   124
	return len;
faluco@1557
   125
}
faluco@1557
   126
dvander@1840
   127
METAMOD_PLUGIN *_GetPluginPtr(const char *path, int fail_api)
faluco@1557
   128
{
faluco@1557
   129
	METAMOD_FN_ORIG_LOAD fn;
faluco@1557
   130
	METAMOD_PLUGIN *pl;
faluco@1557
   131
	int ret;
faluco@1557
   132
faluco@1557
   133
	if (!(g_hCore=openlib(path)))
faluco@1557
   134
	{
dvander@2471
   135
#if defined __linux__ || defined __APPLE__
dvander@1840
   136
		UTIL_Format(s_FailPlugin.error_buffer, 
dvander@1840
   137
			sizeof(s_FailPlugin.error_buffer),
dvander@1840
   138
			"%s",
dvander@1840
   139
			dlerror());
dvander@1840
   140
#else
dvander@1840
   141
		DWORD err = GetLastError();
dvander@1840
   142
dvander@1840
   143
		if (FormatMessageA(
dvander@1840
   144
				FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
dvander@1840
   145
				NULL,
dvander@1840
   146
				err,
dvander@1840
   147
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
dvander@1840
   148
				s_FailPlugin.error_buffer,
dvander@1840
   149
				sizeof(s_FailPlugin.error_buffer),
dvander@1840
   150
				NULL)
dvander@1840
   151
			== 0)
dvander@1840
   152
		{
dvander@1840
   153
			UTIL_Format(s_FailPlugin.error_buffer, 
dvander@1840
   154
				sizeof(s_FailPlugin.error_buffer),
dvander@1840
   155
				"unknown error %x",
dvander@1840
   156
				err);
dvander@1840
   157
		}
dvander@1840
   158
#endif
dvander@1840
   159
dvander@1840
   160
		s_FailPlugin.fail_version = fail_api;
dvander@1840
   161
dvander@1840
   162
		return (METAMOD_PLUGIN *)&s_FailPlugin;
faluco@1557
   163
	}
faluco@1557
   164
faluco@1557
   165
	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
faluco@1557
   166
	{
faluco@1557
   167
		goto error;
faluco@1557
   168
	}
faluco@1557
   169
faluco@1557
   170
	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
faluco@1557
   171
	if (!pl)
faluco@1557
   172
	{
faluco@1557
   173
		goto error;
faluco@1557
   174
	}
faluco@1557
   175
faluco@1557
   176
	return pl;
faluco@1557
   177
error:
faluco@1557
   178
	closelib(g_hCore);
faluco@1557
   179
	g_hCore = NULL;
faluco@1557
   180
	return NULL;
faluco@1557
   181
}
faluco@1557
   182
faluco@1557
   183
bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
faluco@1557
   184
{
faluco@1557
   185
#if defined _MSC_VER
faluco@1557
   186
	MEMORY_BASIC_INFORMATION mem;
faluco@1557
   187
	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
faluco@1557
   188
		return false;
faluco@1557
   189
	if (mem.AllocationBase == NULL)
faluco@1557
   190
		return false;
faluco@1557
   191
	HMODULE dll = (HMODULE)mem.AllocationBase;
faluco@1557
   192
	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
faluco@1557
   193
#else
faluco@1557
   194
	Dl_info info;
faluco@1557
   195
	if (!dladdr(pAddr, &info))
faluco@1557
   196
		return false;
faluco@1557
   197
	if (!info.dli_fbase || !info.dli_fname)
faluco@1557
   198
		return false;
faluco@1557
   199
	const char *dllpath = info.dli_fname;
faluco@1557
   200
	snprintf(buffer, maxlength, "%s", dllpath);
faluco@1557
   201
#endif
faluco@1557
   202
	return true;
faluco@1557
   203
}
faluco@1557
   204
faluco@1557
   205
DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
faluco@1557
   206
{
fyrenmoo@2982
   207
	const char *filename;
dvander@1561
   208
dvander@1561
   209
	load_attempted = true;
faluco@1557
   210
	
faluco@1557
   211
	if (mvi->api_major > METAMOD_API_MAJOR)
faluco@1557
   212
	{
faluco@1557
   213
		return NULL;
faluco@1557
   214
	}
faluco@1557
   215
faluco@1557
   216
	switch (mvi->source_engine)
faluco@1557
   217
	{
ds@2583
   218
	case SOURCE_ENGINE_ORIGINAL:
ds@2583
   219
	case SOURCE_ENGINE_EPISODEONE:
ds@2583
   220
		{
ds@2583
   221
			filename = FILENAME_1_6_EP1;
ds@2583
   222
			break;
ds@2583
   223
		}
faluco@1557
   224
	case SOURCE_ENGINE_ORANGEBOX:
faluco@1557
   225
		{
faluco@1557
   226
			filename = FILENAME_1_6_EP2;
faluco@1557
   227
			break;
faluco@1557
   228
		}
ds@2583
   229
	case SOURCE_ENGINE_LEFT4DEAD:
faluco@1557
   230
		{
ds@2583
   231
			filename = FILENAME_1_6_L4D;
faluco@1557
   232
			break;
faluco@1557
   233
		}
ds@2583
   234
	case SOURCE_ENGINE_DARKMESSIAH:
ds@2400
   235
		{
ds@2583
   236
			filename = FILENAME_1_6_DARKM;
ds@2400
   237
			break;
ds@2400
   238
		}
ds@2860
   239
	case SOURCE_ENGINE_ORANGEBOXVALVE:
ds@2860
   240
		{
ds@2860
   241
			filename = FILENAME_1_6_EP2VALVE;
ds@2860
   242
			break;
ds@2860
   243
		}
fyrenmoo@2847
   244
	case SOURCE_ENGINE_LEFT4DEAD2:
fyrenmoo@2847
   245
		{
fyrenmoo@2847
   246
			filename = FILENAME_1_6_L4D2;
fyrenmoo@2847
   247
			break;
fyrenmoo@2847
   248
		}
ds@3099
   249
	case SOURCE_ENGINE_ALIENSWARM:
ds@3099
   250
		{
ds@3099
   251
			filename = FILENAME_1_6_SWARM;
ds@3099
   252
			break;
ds@3099
   253
		}
skamonkey@3180
   254
	case SOURCE_ENGINE_BLOODYGOODTIME:
skamonkey@3180
   255
		{
skamonkey@3180
   256
			filename = FILENAME_1_6_BGT;
skamonkey@3180
   257
			break;
skamonkey@3180
   258
		}
skamonkey@3373
   259
	case SOURCE_ENGINE_EYE:
skamonkey@3373
   260
		{
skamonkey@3373
   261
			filename = FILENAME_1_6_EYE;
skamonkey@3373
   262
			break;
skamonkey@3373
   263
		}
skamonkey@3515
   264
	case SOURCE_ENGINE_PORTAL2:
skamonkey@3515
   265
		{
skamonkey@3515
   266
			filename = FILENAME_1_6_PORTAL2;
skamonkey@3515
   267
			break;
skamonkey@3515
   268
		}
skamonkey@3515
   269
	case SOURCE_ENGINE_CSGO:
skamonkey@3515
   270
		{
skamonkey@3515
   271
			filename = FILENAME_1_6_CSGO;
skamonkey@3515
   272
			break;
skamonkey@3515
   273
		}
skamonkey@3613
   274
	case SOURCE_ENGINE_CSS:
skamonkey@3613
   275
		{
skamonkey@3613
   276
			filename = FILENAME_1_6_CSS;
skamonkey@3613
   277
			break;
skamonkey@3613
   278
		}
skamonkey@3853
   279
	case SOURCE_ENGINE_DOTA:
skamonkey@3853
   280
		{
skamonkey@3853
   281
			filename = FILENAME_1_6_DOTA;
skamonkey@3853
   282
			break;
skamonkey@3853
   283
		}
faluco@1557
   284
	default:
faluco@1557
   285
		{
faluco@1557
   286
			return NULL;
faluco@1557
   287
		}
faluco@1557
   288
	}
faluco@1557
   289
faluco@1557
   290
	char abspath[256];
faluco@1557
   291
	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
faluco@1557
   292
dvander@1840
   293
	return _GetPluginPtr(abspath, METAMOD_FAIL_API_V2);
faluco@1557
   294
}
faluco@1557
   295
faluco@1557
   296
DLL_EXPORT void UnloadInterface_MMS()
faluco@1557
   297
{
faluco@1557
   298
	if (g_hCore)
faluco@1557
   299
	{
faluco@1557
   300
		closelib(g_hCore);
faluco@1557
   301
		g_hCore = NULL;
faluco@1557
   302
	}
faluco@1557
   303
}
faluco@1557
   304
faluco@1557
   305
DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
faluco@1557
   306
{
dvander@1561
   307
	/**
dvander@1561
   308
	 * If a load has already been attempted, bail out immediately.
dvander@1561
   309
	 */
dvander@1561
   310
	if (load_attempted)
dvander@1561
   311
	{
dvander@1561
   312
		return NULL;
dvander@1561
   313
	}
dvander@1561
   314
dvander@1561
   315
	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
faluco@1557
   316
	{
faluco@1557
   317
		char thisfile[256];
faluco@1557
   318
		char targetfile[256];
faluco@1557
   319
faluco@1557
   320
		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
faluco@1557
   321
		{
faluco@1557
   322
			return NULL;
faluco@1557
   323
		}
faluco@1557
   324
faluco@1557
   325
		size_t len = strlen(thisfile);
ds@3261
   326
		for (size_t iter=len-1; iter<len; iter--)
faluco@1557
   327
		{
faluco@1557
   328
			if (IsPathSepChar(thisfile[iter]))
faluco@1557
   329
			{
faluco@1557
   330
				thisfile[iter] = '\0';
faluco@1557
   331
				break;
faluco@1557
   332
			}
faluco@1557
   333
		}
faluco@1557
   334
faluco@1557
   335
		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
faluco@1557
   336
dvander@1840
   337
		return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
faluco@1557
   338
	}
faluco@1557
   339
faluco@1557
   340
	return NULL;
faluco@1557
   341
}
faluco@1557
   342
faluco@1557
   343
#if defined _MSC_VER
faluco@1557
   344
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
faluco@1557
   345
{
faluco@1557
   346
	if (fdwReason == DLL_PROCESS_DETACH)
faluco@1557
   347
	{
faluco@1557
   348
		UnloadInterface_MMS();
faluco@1557
   349
	}
faluco@1557
   350
	return TRUE;
faluco@1557
   351
}
faluco@1557
   352
#else
faluco@1557
   353
__attribute__((destructor)) static void gcc_fini()
faluco@1557
   354
{
faluco@1557
   355
	UnloadInterface_MMS();
faluco@1557
   356
}
faluco@1557
   357
#endif
dvander@1843
   358
dvander@1843
   359
/* Overload a few things to prevent libstdc++ linking */
dvander@2471
   360
#if defined __linux__ || defined __APPLE__
dvander@1843
   361
extern "C" void __cxa_pure_virtual(void)
dvander@1843
   362
{
dvander@1843
   363
}
dvander@1843
   364
dvander@1843
   365
void *operator new(size_t size)
dvander@1843
   366
{
dvander@1843
   367
	return malloc(size);
dvander@1843
   368
}
dvander@1843
   369
dvander@1843
   370
void *operator new[](size_t size)
dvander@1843
   371
{
dvander@1843
   372
	return malloc(size);
dvander@1843
   373
}
dvander@1843
   374
dvander@1843
   375
void operator delete(void *ptr)
dvander@1843
   376
{
dvander@1843
   377
	free(ptr);
dvander@1843
   378
}
dvander@1843
   379
dvander@1843
   380
void operator delete[](void * ptr)
dvander@1843
   381
{
dvander@1843
   382
	free(ptr);
dvander@1843
   383
}
dvander@1843
   384
#endif
dvander@1843
   385
dvander@1843
   386