loader/loader.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 22:17:14 2007 +0000 (2007-10-14)
changeset 1561 b437438914c5
parent 1557 eb8d17f6b271
child 1840 dd80e60b571c
permissions -rw-r--r--
added versioning/resource file
prevented loading after new api is called
faluco@1557
     1
/**
faluco@1557
     2
 * vim: set ts=4 :
faluco@1557
     3
 * =============================================================================
faluco@1557
     4
 * SourceMod
faluco@1557
     5
 * Copyright (C) 2004-2007 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
 * Version: $Id$
faluco@1557
    30
 */
faluco@1557
    31
faluco@1557
    32
#include <ISmmPluginExt.h>
faluco@1557
    33
#include <stdarg.h>
faluco@1557
    34
#include <string.h>
faluco@1557
    35
#include <stdio.h>
faluco@1557
    36
faluco@1557
    37
#if defined _MSC_VER
faluco@1557
    38
	#define DLL_EXPORT				extern "C" __declspec(dllexport)
faluco@1557
    39
	#define openlib(lib)			LoadLibrary(lib)
faluco@1557
    40
	#define closelib(lib)			FreeLibrary(lib)
faluco@1557
    41
	#define findsym(lib, sym)		GetProcAddress(lib, sym)
faluco@1557
    42
	#define PLATFORM_EXT			".dll"
faluco@1557
    43
	#define vsnprintf				_vsnprintf
faluco@1557
    44
	#define PATH_SEP_CHAR			"\\"
faluco@1557
    45
	inline bool IsPathSepChar(char c) 
faluco@1557
    46
	{
faluco@1557
    47
		return (c == '/' || c == '\\');
faluco@1557
    48
	}
faluco@1557
    49
	#include <Windows.h>
faluco@1557
    50
#else
faluco@1557
    51
	#define DLL_EXPORT				extern "C" __attribute__((visibility("default")))
faluco@1557
    52
	#define openlib(lib)			dlopen(lib, RTLD_NOW)
faluco@1557
    53
	#define closelib(lib)			dlclose(lib)
faluco@1557
    54
	#define findsym(lib, sym)		dlsym(lib, sym)
faluco@1557
    55
	#define PLATFORM_EXT			".so"
faluco@1557
    56
	typedef void *					HINSTANCE;
faluco@1557
    57
	#define PATH_SEP_CHAR			"/"
faluco@1557
    58
	inline bool IsPathSepChar(char c) 
faluco@1557
    59
	{
faluco@1557
    60
		return (c == '/');
faluco@1557
    61
	}
faluco@1557
    62
	#include <dlfcn.h>
faluco@1557
    63
#endif
faluco@1557
    64
faluco@1557
    65
#define METAMOD_API_MAJOR			2
faluco@1557
    66
#define FILENAME_1_4_EP1			"sourcemod.1.ep1" PLATFORM_EXT
faluco@1557
    67
#define FILENAME_1_6_EP2			"sourcemod.2.ep2" PLATFORM_EXT
faluco@1557
    68
#define FILENAME_1_6_EP1			"sourcemod.2.ep1" PLATFORM_EXT
faluco@1557
    69
faluco@1557
    70
HINSTANCE g_hCore = NULL;
dvander@1561
    71
bool load_attempted = false;
faluco@1557
    72
faluco@1557
    73
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
faluco@1557
    74
{
faluco@1557
    75
	va_list ap;
faluco@1557
    76
faluco@1557
    77
	va_start(ap, fmt);
faluco@1557
    78
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
faluco@1557
    79
	va_end(ap);
faluco@1557
    80
faluco@1557
    81
	if (len >= maxlength)
faluco@1557
    82
	{
faluco@1557
    83
		len = maxlength - 1;
faluco@1557
    84
		buffer[len] = '\0';
faluco@1557
    85
	}
faluco@1557
    86
faluco@1557
    87
	return len;
faluco@1557
    88
}
faluco@1557
    89
faluco@1557
    90
METAMOD_PLUGIN *_GetPluginPtr(const char *path)
faluco@1557
    91
{
faluco@1557
    92
	METAMOD_FN_ORIG_LOAD fn;
faluco@1557
    93
	METAMOD_PLUGIN *pl;
faluco@1557
    94
	int ret;
faluco@1557
    95
faluco@1557
    96
	if (!(g_hCore=openlib(path)))
faluco@1557
    97
	{
faluco@1557
    98
		return NULL;
faluco@1557
    99
	}
faluco@1557
   100
faluco@1557
   101
	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
faluco@1557
   102
	{
faluco@1557
   103
		goto error;
faluco@1557
   104
	}
faluco@1557
   105
faluco@1557
   106
	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
faluco@1557
   107
	if (!pl)
faluco@1557
   108
	{
faluco@1557
   109
		goto error;
faluco@1557
   110
	}
faluco@1557
   111
faluco@1557
   112
	return pl;
faluco@1557
   113
error:
faluco@1557
   114
	closelib(g_hCore);
faluco@1557
   115
	g_hCore = NULL;
faluco@1557
   116
	return NULL;
faluco@1557
   117
}
faluco@1557
   118
faluco@1557
   119
bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
faluco@1557
   120
{
faluco@1557
   121
#if defined _MSC_VER
faluco@1557
   122
	MEMORY_BASIC_INFORMATION mem;
faluco@1557
   123
	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
faluco@1557
   124
		return false;
faluco@1557
   125
	if (mem.AllocationBase == NULL)
faluco@1557
   126
		return false;
faluco@1557
   127
	HMODULE dll = (HMODULE)mem.AllocationBase;
faluco@1557
   128
	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
faluco@1557
   129
#else
faluco@1557
   130
	Dl_info info;
faluco@1557
   131
	if (!dladdr(pAddr, &info))
faluco@1557
   132
		return false;
faluco@1557
   133
	if (!info.dli_fbase || !info.dli_fname)
faluco@1557
   134
		return false;
faluco@1557
   135
	const char *dllpath = info.dli_fname;
faluco@1557
   136
	snprintf(buffer, maxlength, "%s", dllpath);
faluco@1557
   137
#endif
faluco@1557
   138
	return true;
faluco@1557
   139
}
faluco@1557
   140
faluco@1557
   141
DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
faluco@1557
   142
{
faluco@1557
   143
	char *filename;
dvander@1561
   144
dvander@1561
   145
	load_attempted = true;
faluco@1557
   146
	
faluco@1557
   147
	if (mvi->api_major > METAMOD_API_MAJOR)
faluco@1557
   148
	{
faluco@1557
   149
		return NULL;
faluco@1557
   150
	}
faluco@1557
   151
faluco@1557
   152
	switch (mvi->source_engine)
faluco@1557
   153
	{
faluco@1557
   154
	case SOURCE_ENGINE_ORANGEBOX:
faluco@1557
   155
		{
faluco@1557
   156
			filename = FILENAME_1_6_EP2;
faluco@1557
   157
			break;
faluco@1557
   158
		}
faluco@1557
   159
	case SOURCE_ENGINE_EPISODEONE:
faluco@1557
   160
	case SOURCE_ENGINE_ORIGINAL:
faluco@1557
   161
		{
faluco@1557
   162
			filename = FILENAME_1_6_EP1;
faluco@1557
   163
			break;
faluco@1557
   164
		}
faluco@1557
   165
	default:
faluco@1557
   166
		{
faluco@1557
   167
			return NULL;
faluco@1557
   168
		}
faluco@1557
   169
	}
faluco@1557
   170
faluco@1557
   171
	char abspath[256];
faluco@1557
   172
	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
faluco@1557
   173
faluco@1557
   174
	return _GetPluginPtr(abspath);
faluco@1557
   175
}
faluco@1557
   176
faluco@1557
   177
DLL_EXPORT void UnloadInterface_MMS()
faluco@1557
   178
{
faluco@1557
   179
	if (g_hCore)
faluco@1557
   180
	{
faluco@1557
   181
		closelib(g_hCore);
faluco@1557
   182
		g_hCore = NULL;
faluco@1557
   183
	}
faluco@1557
   184
}
faluco@1557
   185
faluco@1557
   186
DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
faluco@1557
   187
{
dvander@1561
   188
	/**
dvander@1561
   189
	 * If a load has already been attempted, bail out immediately.
dvander@1561
   190
	 */
dvander@1561
   191
	if (load_attempted)
dvander@1561
   192
	{
dvander@1561
   193
		return NULL;
dvander@1561
   194
	}
dvander@1561
   195
dvander@1561
   196
	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
faluco@1557
   197
	{
faluco@1557
   198
		char thisfile[256];
faluco@1557
   199
		char targetfile[256];
faluco@1557
   200
faluco@1557
   201
		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
faluco@1557
   202
		{
faluco@1557
   203
			return NULL;
faluco@1557
   204
		}
faluco@1557
   205
faluco@1557
   206
		size_t len = strlen(thisfile);
faluco@1557
   207
		for (size_t iter=len-1; iter>=0 && iter<len; iter--)
faluco@1557
   208
		{
faluco@1557
   209
			if (IsPathSepChar(thisfile[iter]))
faluco@1557
   210
			{
faluco@1557
   211
				thisfile[iter] = '\0';
faluco@1557
   212
				break;
faluco@1557
   213
			}
faluco@1557
   214
		}
faluco@1557
   215
faluco@1557
   216
		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
faluco@1557
   217
faluco@1557
   218
		return _GetPluginPtr(targetfile);
faluco@1557
   219
	}
faluco@1557
   220
faluco@1557
   221
	return NULL;
faluco@1557
   222
}
faluco@1557
   223
faluco@1557
   224
#if defined _MSC_VER
faluco@1557
   225
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
faluco@1557
   226
{
faluco@1557
   227
	if (fdwReason == DLL_PROCESS_DETACH)
faluco@1557
   228
	{
faluco@1557
   229
		UnloadInterface_MMS();
faluco@1557
   230
	}
faluco@1557
   231
	return TRUE;
faluco@1557
   232
}
faluco@1557
   233
#else
faluco@1557
   234
__attribute__((destructor)) static void gcc_fini()
faluco@1557
   235
{
faluco@1557
   236
	UnloadInterface_MMS();
faluco@1557
   237
}
faluco@1557
   238
#endif