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
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <ISmmPluginExt.h>
    33 #include <stdarg.h>
    34 #include <string.h>
    35 #include <stdio.h>
    36 
    37 #if defined _MSC_VER
    38 	#define DLL_EXPORT				extern "C" __declspec(dllexport)
    39 	#define openlib(lib)			LoadLibrary(lib)
    40 	#define closelib(lib)			FreeLibrary(lib)
    41 	#define findsym(lib, sym)		GetProcAddress(lib, sym)
    42 	#define PLATFORM_EXT			".dll"
    43 	#define vsnprintf				_vsnprintf
    44 	#define PATH_SEP_CHAR			"\\"
    45 	inline bool IsPathSepChar(char c) 
    46 	{
    47 		return (c == '/' || c == '\\');
    48 	}
    49 	#include <Windows.h>
    50 #else
    51 	#define DLL_EXPORT				extern "C" __attribute__((visibility("default")))
    52 	#define openlib(lib)			dlopen(lib, RTLD_NOW)
    53 	#define closelib(lib)			dlclose(lib)
    54 	#define findsym(lib, sym)		dlsym(lib, sym)
    55 	#define PLATFORM_EXT			".so"
    56 	typedef void *					HINSTANCE;
    57 	#define PATH_SEP_CHAR			"/"
    58 	inline bool IsPathSepChar(char c) 
    59 	{
    60 		return (c == '/');
    61 	}
    62 	#include <dlfcn.h>
    63 #endif
    64 
    65 #define METAMOD_API_MAJOR			2
    66 #define FILENAME_1_4_EP1			"sourcemod.1.ep1" PLATFORM_EXT
    67 #define FILENAME_1_6_EP2			"sourcemod.2.ep2" PLATFORM_EXT
    68 #define FILENAME_1_6_EP1			"sourcemod.2.ep1" PLATFORM_EXT
    69 
    70 HINSTANCE g_hCore = NULL;
    71 
    72 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    73 {
    74 	va_list ap;
    75 
    76 	va_start(ap, fmt);
    77 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
    78 	va_end(ap);
    79 
    80 	if (len >= maxlength)
    81 	{
    82 		len = maxlength - 1;
    83 		buffer[len] = '\0';
    84 	}
    85 
    86 	return len;
    87 }
    88 
    89 METAMOD_PLUGIN *_GetPluginPtr(const char *path)
    90 {
    91 	METAMOD_FN_ORIG_LOAD fn;
    92 	METAMOD_PLUGIN *pl;
    93 	int ret;
    94 
    95 	if (!(g_hCore=openlib(path)))
    96 	{
    97 		return NULL;
    98 	}
    99 
   100 	if (!(fn=(METAMOD_FN_ORIG_LOAD)findsym(g_hCore, "CreateInterface")))
   101 	{
   102 		goto error;
   103 	}
   104 
   105 	pl = (METAMOD_PLUGIN *)fn(METAMOD_PLAPI_NAME, &ret);
   106 	if (!pl)
   107 	{
   108 		goto error;
   109 	}
   110 
   111 	return pl;
   112 error:
   113 	closelib(g_hCore);
   114 	g_hCore = NULL;
   115 	return NULL;
   116 }
   117 
   118 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   119 {
   120 #if defined _MSC_VER
   121 	MEMORY_BASIC_INFORMATION mem;
   122 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   123 		return false;
   124 	if (mem.AllocationBase == NULL)
   125 		return false;
   126 	HMODULE dll = (HMODULE)mem.AllocationBase;
   127 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   128 #else
   129 	Dl_info info;
   130 	if (!dladdr(pAddr, &info))
   131 		return false;
   132 	if (!info.dli_fbase || !info.dli_fname)
   133 		return false;
   134 	const char *dllpath = info.dli_fname;
   135 	snprintf(buffer, maxlength, "%s", dllpath);
   136 #endif
   137 	return true;
   138 }
   139 
   140 DLL_EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
   141 {
   142 	char *filename;
   143 	
   144 	if (mvi->api_major > METAMOD_API_MAJOR)
   145 	{
   146 		return NULL;
   147 	}
   148 
   149 	switch (mvi->source_engine)
   150 	{
   151 	case SOURCE_ENGINE_ORANGEBOX:
   152 		{
   153 			filename = FILENAME_1_6_EP2;
   154 			break;
   155 		}
   156 	case SOURCE_ENGINE_EPISODEONE:
   157 	case SOURCE_ENGINE_ORIGINAL:
   158 		{
   159 			filename = FILENAME_1_6_EP1;
   160 			break;
   161 		}
   162 	default:
   163 		{
   164 			return NULL;
   165 		}
   166 	}
   167 
   168 	char abspath[256];
   169 	UTIL_Format(abspath, sizeof(abspath), "%s" PATH_SEP_CHAR "%s", mli->pl_path, filename);
   170 
   171 	return _GetPluginPtr(abspath);
   172 }
   173 
   174 DLL_EXPORT void UnloadInterface_MMS()
   175 {
   176 	if (g_hCore)
   177 	{
   178 		closelib(g_hCore);
   179 		g_hCore = NULL;
   180 	}
   181 }
   182 
   183 DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
   184 {
   185 	if (strcmp(iface, METAMOD_PLAPI_NAME))
   186 	{
   187 		char thisfile[256];
   188 		char targetfile[256];
   189 
   190 		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
   191 		{
   192 			return NULL;
   193 		}
   194 
   195 		size_t len = strlen(thisfile);
   196 		for (size_t iter=len-1; iter>=0 && iter<len; iter--)
   197 		{
   198 			if (IsPathSepChar(thisfile[iter]))
   199 			{
   200 				thisfile[iter] = '\0';
   201 				break;
   202 			}
   203 		}
   204 
   205 		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);
   206 
   207 		return _GetPluginPtr(targetfile);
   208 	}
   209 
   210 	return NULL;
   211 }
   212 
   213 #if defined _MSC_VER
   214 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
   215 {
   216 	if (fdwReason == DLL_PROCESS_DETACH)
   217 	{
   218 		UnloadInterface_MMS();
   219 	}
   220 	return TRUE;
   221 }
   222 #else
   223 __attribute__((destructor)) static void gcc_fini()
   224 {
   225 	UnloadInterface_MMS();
   226 }
   227 #endif