loader/loader.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 21:51:30 2007 +0000 (2007-10-14)
changeset 1559 bb3fd890699d
parent 1557 eb8d17f6b271
child 1561 b437438914c5
permissions -rw-r--r--
fixed bug in build tool
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;
faluco@1557
    71
faluco@1557
    72
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
faluco@1557
    73
{
faluco@1557
    74
	va_list ap;
faluco@1557
    75
faluco@1557
    76
	va_start(ap, fmt);
faluco@1557
    77
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
faluco@1557
    78
	va_end(ap);
faluco@1557
    79
faluco@1557
    80
	if (len >= maxlength)
faluco@1557
    81
	{
faluco@1557
    82
		len = maxlength - 1;
faluco@1557
    83
		buffer[len] = '\0';
faluco@1557
    84
	}
faluco@1557
    85
faluco@1557
    86
	return len;
faluco@1557
    87
}
faluco@1557
    88
faluco@1557
    89
METAMOD_PLUGIN *_GetPluginPtr(const char *path)
faluco@1557
    90
{
faluco@1557
    91
	METAMOD_FN_ORIG_LOAD fn;
faluco@1557
    92
	METAMOD_PLUGIN *pl;
faluco@1557
    93
	int ret;
faluco@1557
    94
faluco@1557
    95
	if (!(g_hCore=openlib(path)))
faluco@1557
    96
	{
faluco@1557
    97
		return NULL;
faluco@1557
    98
	}
faluco@1557
    99
faluco@1557
   100
	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
faluco@1557
   101
	{
faluco@1557
   102
		goto error;
faluco@1557
   103
	}
faluco@1557
   104
faluco@1557
   105
	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
faluco@1557
   106
	if (!pl)
faluco@1557
   107
	{
faluco@1557
   108
		goto error;
faluco@1557
   109
	}
faluco@1557
   110
faluco@1557
   111
	return pl;
faluco@1557
   112
error:
faluco@1557
   113
	closelib(g_hCore);
faluco@1557
   114
	g_hCore = NULL;
faluco@1557
   115
	return NULL;
faluco@1557
   116
}
faluco@1557
   117
faluco@1557
   118
bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
faluco@1557
   119
{
faluco@1557
   120
#if defined _MSC_VER
faluco@1557
   121
	MEMORY_BASIC_INFORMATION mem;
faluco@1557
   122
	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
faluco@1557
   123
		return false;
faluco@1557
   124
	if (mem.AllocationBase == NULL)
faluco@1557
   125
		return false;
faluco@1557
   126
	HMODULE dll = (HMODULE)mem.AllocationBase;
faluco@1557
   127
	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
faluco@1557
   128
#else
faluco@1557
   129
	Dl_info info;
faluco@1557
   130
	if (!dladdr(pAddr, &info))
faluco@1557
   131
		return false;
faluco@1557
   132
	if (!info.dli_fbase || !info.dli_fname)
faluco@1557
   133
		return false;
faluco@1557
   134
	const char *dllpath = info.dli_fname;
faluco@1557
   135
	snprintf(buffer, maxlength, "%s", dllpath);
faluco@1557
   136
#endif
faluco@1557
   137
	return true;
faluco@1557
   138
}
faluco@1557
   139
faluco@1557
   140
DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
faluco@1557
   141
{
faluco@1557
   142
	char *filename;
faluco@1557
   143
	
faluco@1557
   144
	if (mvi->api_major > METAMOD_API_MAJOR)
faluco@1557
   145
	{
faluco@1557
   146
		return NULL;
faluco@1557
   147
	}
faluco@1557
   148
faluco@1557
   149
	switch (mvi->source_engine)
faluco@1557
   150
	{
faluco@1557
   151
	case SOURCE_ENGINE_ORANGEBOX:
faluco@1557
   152
		{
faluco@1557
   153
			filename = FILENAME_1_6_EP2;
faluco@1557
   154
			break;
faluco@1557
   155
		}
faluco@1557
   156
	case SOURCE_ENGINE_EPISODEONE:
faluco@1557
   157
	case SOURCE_ENGINE_ORIGINAL:
faluco@1557
   158
		{
faluco@1557
   159
			filename = FILENAME_1_6_EP1;
faluco@1557
   160
			break;
faluco@1557
   161
		}
faluco@1557
   162
	default:
faluco@1557
   163
		{
faluco@1557
   164
			return NULL;
faluco@1557
   165
		}
faluco@1557
   166
	}
faluco@1557
   167
faluco@1557
   168
	char abspath[256];
faluco@1557
   169
	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
faluco@1557
   170
faluco@1557
   171
	return _GetPluginPtr(abspath);
faluco@1557
   172
}
faluco@1557
   173
faluco@1557
   174
DLL_EXPORT void UnloadInterface_MMS()
faluco@1557
   175
{
faluco@1557
   176
	if (g_hCore)
faluco@1557
   177
	{
faluco@1557
   178
		closelib(g_hCore);
faluco@1557
   179
		g_hCore = NULL;
faluco@1557
   180
	}
faluco@1557
   181
}
faluco@1557
   182
faluco@1557
   183
DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
faluco@1557
   184
{
faluco@1557
   185
	if (strcmp(iface, METAMOD_PLAPI_NAME))
faluco@1557
   186
	{
faluco@1557
   187
		char thisfile[256];
faluco@1557
   188
		char targetfile[256];
faluco@1557
   189
faluco@1557
   190
		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
faluco@1557
   191
		{
faluco@1557
   192
			return NULL;
faluco@1557
   193
		}
faluco@1557
   194
faluco@1557
   195
		size_t len = strlen(thisfile);
faluco@1557
   196
		for (size_t iter=len-1; iter>=0 && iter<len; iter--)
faluco@1557
   197
		{
faluco@1557
   198
			if (IsPathSepChar(thisfile[iter]))
faluco@1557
   199
			{
faluco@1557
   200
				thisfile[iter] = '\0';
faluco@1557
   201
				break;
faluco@1557
   202
			}
faluco@1557
   203
		}
faluco@1557
   204
faluco@1557
   205
		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
faluco@1557
   206
faluco@1557
   207
		return _GetPluginPtr(targetfile);
faluco@1557
   208
	}
faluco@1557
   209
faluco@1557
   210
	return NULL;
faluco@1557
   211
}
faluco@1557
   212
faluco@1557
   213
#if defined _MSC_VER
faluco@1557
   214
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
faluco@1557
   215
{
faluco@1557
   216
	if (fdwReason == DLL_PROCESS_DETACH)
faluco@1557
   217
	{
faluco@1557
   218
		UnloadInterface_MMS();
faluco@1557
   219
	}
faluco@1557
   220
	return TRUE;
faluco@1557
   221
}
faluco@1557
   222
#else
faluco@1557
   223
__attribute__((destructor)) static void gcc_fini()
faluco@1557
   224
{
faluco@1557
   225
	UnloadInterface_MMS();
faluco@1557
   226
}
faluco@1557
   227
#endif