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