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