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