loader/stripper_loader.cpp
author David Anderson <dvander@alliedmods.net>
Tue May 12 14:13:01 2009 +0000 (2009-05-12)
changeset 20 443c594aaef8
parent 19 6164205b828d
child 24 b4bd4a80ae07
permissions -rw-r--r--
Added Windows support to Stripper 1.2.
     1 #include <stdio.h>
     2 #include <stdarg.h>
     3 #include <string.h>
     4 #include <ISmmPluginExt.h>
     5 
     6 #if defined _MSC_VER
     7 #define EXPORT extern "C" __declspec(dllexport)
     8 #define PLATFORM_EXT	".dll"
     9 #define PATH_SEP_CHAR	"\\"
    10 #define vsnprintf		_vsnprintf
    11 #define WINDOWS_MEAN_AND_LEAN
    12 #include <windows.h>
    13 inline bool IsPathSepChar(char c) { return (c == '/' || c == '\\'); }
    14 #else
    15 #define EXPORT extern "C" __attribute__((visibility("default")))
    16 #define PLATFORM_EXT		".so"
    17 #define PATH_SEP_CHAR		"/"
    18 #define LoadLibrary(x)		dlopen(x, RTLD_NOW)
    19 #define GetProcAddress(x,s)	dlsym(x,s)
    20 #define FreeLibrary(x)		dlclose(x)
    21 typedef void *		HINSTANCE;
    22 #include <dlfcn.h>
    23 inline bool IsPathSepChar(char c) { return (c == '/'); }
    24 #endif
    25 
    26 #define MMS_1_4_EP1_FILE		"stripper.14.ep1" PLATFORM_EXT
    27 #define MMS_1_6_EP1_FILE		"stripper.16.ep1" PLATFORM_EXT
    28 #define MMS_1_6_EP2_FILE		"stripper.16.ep2" PLATFORM_EXT
    29 #define MMS_1_6_L4D_FILE		"stripper.16.l4d" PLATFORM_EXT
    30 #define MMS_1_6_DARKM_FILE		"stripper.16.darkm" PLATFORM_EXT
    31 
    32 HINSTANCE stripper_library = NULL;
    33 
    34 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    35 {
    36 #if defined _MSC_VER
    37 	MEMORY_BASIC_INFORMATION mem;
    38 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    39 		return false;
    40 	if (mem.AllocationBase == NULL)
    41 		return false;
    42 	HMODULE dll = (HMODULE)mem.AllocationBase;
    43 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    44 #else
    45 	Dl_info info;
    46 	if (!dladdr(pAddr, &info))
    47 		return false;
    48 	if (!info.dli_fbase || !info.dli_fname)
    49 		return false;
    50 	const char *dllpath = info.dli_fname;
    51 	snprintf(buffer, maxlength, "%s", dllpath);
    52 #endif
    53 	return true;
    54 }
    55 
    56 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    57 {
    58 	va_list ap;
    59 
    60 	va_start(ap, fmt);
    61 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
    62 	va_end(ap);
    63 
    64 	if (len >= maxlength)
    65 	{
    66 		len = maxlength - 1;
    67 		buffer[len] = '\0';
    68 	}
    69 
    70 	return len;
    71 }
    72 
    73 METAMOD_PLUGIN *TryAndLoadLibrary(const char *path)
    74 {
    75 	if ((stripper_library = LoadLibrary(path)) == NULL)
    76 	{
    77 		DWORD dw = GetLastError();
    78 		return NULL;
    79 	}
    80 
    81 	METAMOD_FN_ORIG_LOAD ld = (METAMOD_FN_ORIG_LOAD)GetProcAddress(stripper_library, "CreateInterface");
    82 	if (ld == NULL)
    83 	{
    84 		FreeLibrary(stripper_library);
    85 		stripper_library = NULL;
    86 		return NULL;
    87 	}
    88 
    89 	int ret;
    90 	METAMOD_PLUGIN *pl = (METAMOD_PLUGIN *)ld(METAMOD_PLAPI_NAME, &ret);
    91 	if (pl == NULL)
    92 	{
    93 		FreeLibrary(stripper_library);
    94 		stripper_library = NULL;
    95 		return NULL;
    96 	}
    97 
    98 	return pl;
    99 }
   100 
   101 EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
   102 {
   103 	/* We can only handle API v2 right now.  In case the plugin API gets bumped,  
   104 	 * we ignore other cases.
   105 	 */
   106 	if (mvi->api_major != 2)
   107 	{
   108 		return NULL;
   109 	}
   110 
   111 	const char *file = NULL;
   112 
   113 	switch (mvi->source_engine)
   114 	{
   115 		case SOURCE_ENGINE_ORANGEBOX:
   116 			file = MMS_1_6_EP2_FILE;
   117 			break;
   118 		case SOURCE_ENGINE_ORIGINAL:
   119 		case SOURCE_ENGINE_EPISODEONE:
   120 			file = MMS_1_6_EP1_FILE;
   121 			break;
   122 		case SOURCE_ENGINE_LEFT4DEAD:
   123 			file = MMS_1_6_L4D_FILE;
   124 			break;
   125 		case SOURCE_ENGINE_DARKMESSIAH:
   126 			file = MMS_1_6_DARKM_FILE;
   127 			break;
   128 	}
   129 
   130 	char our_path[256];
   131 	UTIL_Format(our_path, sizeof(our_path), "%s" PATH_SEP_CHAR "%s", mli->pl_path, file);
   132 
   133 	return TryAndLoadLibrary(our_path);
   134 }
   135 
   136 EXPORT void UnloadInterface_MMS()
   137 {
   138 	if (stripper_library != NULL)
   139 	{
   140 		FreeLibrary(stripper_library);
   141 		stripper_library = NULL;
   142 	}
   143 }
   144 
   145 EXPORT void *CreateInterface(const char *pName, int *pReturnCode)
   146 {
   147 	if (strcmp(pName, METAMOD_PLAPI_NAME) == 0)
   148 	{
   149 		char our_file[256];
   150 		if (!GetFileOfAddress((void *)CreateInterface_MMS, our_file, sizeof(our_file)))
   151 		{
   152 			return NULL;
   153 		}
   154 
   155 		/* Go backwards and get the first token */
   156 		size_t len = strlen(our_file);
   157 		for (size_t i = len - 1; i >= 0 && i < len; i--)
   158 		{
   159 			if (IsPathSepChar(our_file[i]))
   160 			{
   161 				our_file[i] = '\0';
   162 				break;
   163 			}
   164 		}
   165 
   166 		char new_file[256];
   167 		UTIL_Format(new_file, sizeof(new_file), "%s" PATH_SEP_CHAR MMS_1_4_EP1_FILE, our_file);
   168 
   169 		return TryAndLoadLibrary(new_file);
   170 	}
   171 
   172 	return NULL;
   173 }
   174 
   175 #if defined _MSC_VER
   176 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
   177 {
   178 	if (fdwReason == DLL_PROCESS_DETACH)
   179 	{
   180 		UnloadInterface_MMS();
   181 	}
   182 	return TRUE;
   183 }
   184 #else
   185 __attribute__((destructor))
   186 static void gcc_fini()
   187 {
   188 	UnloadInterface_MMS();
   189 }
   190 #endif
   191