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