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