1.1 --- a/loader/stripper_loader.cpp Wed Oct 10 01:52:25 2007 +0000
1.2 +++ b/loader/stripper_loader.cpp Wed Oct 10 01:58:10 2007 +0000
1.3 @@ -1,99 +1,100 @@
1.4 -#include <stdio.h>
1.5 -#include <stdarg.h>
1.6 -#include <ISmmPluginExt.h>
1.7 -
1.8 -#if defined _MSC_VER
1.9 -#define EXPORT extern "C" __declspec(dllexport)
1.10 -#define PLATFORM_EXT ".dll"
1.11 -#define PATH_SEP_CHAR "\\"
1.12 -#define vsnprintf _vsnprintf
1.13 -#define WINDOWS_MEAN_AND_LEAN
1.14 -#include <windows.h>
1.15 -inline bool IsPathSepChar(char c) { return (c == '/' || c == '\\'); }
1.16 -#else
1.17 -#define EXPORT extern "C" __attribute__((visibility("default")))
1.18 -#define PLATFORM_EXT "_i486.so"
1.19 -#define PATH_SEP_CHAR "/"
1.20 -#define LoadLibrary(x) dlopen(x, RTLD_NOW)
1.21 -#define GetProcAddress(x,s) dlsym(x,s)
1.22 -#define FreeLibrary(x) dlclose(x)
1.23 -typedef void * HINSTANCE;
1.24 -#include <dlfcn.h>
1.25 -inline bool IsPathSepChar(char c) { return (c == '/'); }
1.26 -#endif
1.27 -
1.28 -#define MMS_1_4_EP1_FILE "stripper.14.ep1"
1.29 -#define MMS_1_6_EP1_FILE "stripper.16.ep1"
1.30 -#define MMS_1_6_EP2_FILE "stripper.16.ep2"
1.31 -
1.32 -HINSTANCE stripper_library = NULL;
1.33 -
1.34 -bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.35 -{
1.36 -#if defined _MSC_VER
1.37 - MEMORY_BASIC_INFORMATION mem;
1.38 - if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.39 - return false;
1.40 - if (mem.AllocationBase == NULL)
1.41 - return false;
1.42 - HMODULE dll = (HMODULE)mem.AllocationBase;
1.43 - GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.44 -#else
1.45 - Dl_info info;
1.46 - if (!dladdr(pAddr, &info))
1.47 - return false;
1.48 - if (!info.dli_fbase || !info.dli_fname)
1.49 - return false;
1.50 - const char *dllpath = info.dli_fname;
1.51 - snprintf(buffer, maxlength, "%s", dllpath);
1.52 -#endif
1.53 - return true;
1.54 -}
1.55 -
1.56 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.57 -{
1.58 - va_list ap;
1.59 -
1.60 - va_start(ap, fmt);
1.61 - size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.62 - va_end(ap);
1.63 -
1.64 - if (len >= maxlength)
1.65 - {
1.66 - len = maxlength - 1;
1.67 - buffer[len] = '\0';
1.68 - }
1.69 -
1.70 - return len;
1.71 -}
1.72 -
1.73 -METAMOD_PLUGIN *TryAndLoadLibrary(const char *path)
1.74 -{
1.75 - if ((stripper_library = LoadLibrary(path)) == NULL)
1.76 - {
1.77 - return NULL;
1.78 - }
1.79 -
1.80 - METAMOD_FN_ORIG_LOAD ld = (METAMOD_FN_ORIG_LOAD)GetProcAddress(stripper_library, "CreateInterface");
1.81 - if (ld == NULL)
1.82 - {
1.83 - FreeLibrary(stripper_library);
1.84 - stripper_library = NULL;
1.85 - return NULL;
1.86 - }
1.87 -
1.88 - int ret;
1.89 - METAMOD_PLUGIN *pl = (METAMOD_PLUGIN *)ld(METAMOD_PLAPI_NAME, &ret);
1.90 - if (pl == NULL)
1.91 - {
1.92 - FreeLibrary(stripper_library);
1.93 - stripper_library = NULL;
1.94 - return NULL;
1.95 - }
1.96 -
1.97 - return pl;
1.98 -}
1.99 -
1.100 +#include <stdio.h>
1.101 +#include <stdarg.h>
1.102 +#include <string.h>
1.103 +#include <ISmmPluginExt.h>
1.104 +
1.105 +#if defined _MSC_VER
1.106 +#define EXPORT extern "C" __declspec(dllexport)
1.107 +#define PLATFORM_EXT ".dll"
1.108 +#define PATH_SEP_CHAR "\\"
1.109 +#define vsnprintf _vsnprintf
1.110 +#define WINDOWS_MEAN_AND_LEAN
1.111 +#include <windows.h>
1.112 +inline bool IsPathSepChar(char c) { return (c == '/' || c == '\\'); }
1.113 +#else
1.114 +#define EXPORT extern "C" __attribute__((visibility("default")))
1.115 +#define PLATFORM_EXT "_i486.so"
1.116 +#define PATH_SEP_CHAR "/"
1.117 +#define LoadLibrary(x) dlopen(x, RTLD_NOW)
1.118 +#define GetProcAddress(x,s) dlsym(x,s)
1.119 +#define FreeLibrary(x) dlclose(x)
1.120 +typedef void * HINSTANCE;
1.121 +#include <dlfcn.h>
1.122 +inline bool IsPathSepChar(char c) { return (c == '/'); }
1.123 +#endif
1.124 +
1.125 +#define MMS_1_4_EP1_FILE "stripper.14.ep1"
1.126 +#define MMS_1_6_EP1_FILE "stripper.16.ep1"
1.127 +#define MMS_1_6_EP2_FILE "stripper.16.ep2"
1.128 +
1.129 +HINSTANCE stripper_library = NULL;
1.130 +
1.131 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.132 +{
1.133 +#if defined _MSC_VER
1.134 + MEMORY_BASIC_INFORMATION mem;
1.135 + if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.136 + return false;
1.137 + if (mem.AllocationBase == NULL)
1.138 + return false;
1.139 + HMODULE dll = (HMODULE)mem.AllocationBase;
1.140 + GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.141 +#else
1.142 + Dl_info info;
1.143 + if (!dladdr(pAddr, &info))
1.144 + return false;
1.145 + if (!info.dli_fbase || !info.dli_fname)
1.146 + return false;
1.147 + const char *dllpath = info.dli_fname;
1.148 + snprintf(buffer, maxlength, "%s", dllpath);
1.149 +#endif
1.150 + return true;
1.151 +}
1.152 +
1.153 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.154 +{
1.155 + va_list ap;
1.156 +
1.157 + va_start(ap, fmt);
1.158 + size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.159 + va_end(ap);
1.160 +
1.161 + if (len >= maxlength)
1.162 + {
1.163 + len = maxlength - 1;
1.164 + buffer[len] = '\0';
1.165 + }
1.166 +
1.167 + return len;
1.168 +}
1.169 +
1.170 +METAMOD_PLUGIN *TryAndLoadLibrary(const char *path)
1.171 +{
1.172 + if ((stripper_library = LoadLibrary(path)) == NULL)
1.173 + {
1.174 + return NULL;
1.175 + }
1.176 +
1.177 + METAMOD_FN_ORIG_LOAD ld = (METAMOD_FN_ORIG_LOAD)GetProcAddress(stripper_library, "CreateInterface");
1.178 + if (ld == NULL)
1.179 + {
1.180 + FreeLibrary(stripper_library);
1.181 + stripper_library = NULL;
1.182 + return NULL;
1.183 + }
1.184 +
1.185 + int ret;
1.186 + METAMOD_PLUGIN *pl = (METAMOD_PLUGIN *)ld(METAMOD_PLAPI_NAME, &ret);
1.187 + if (pl == NULL)
1.188 + {
1.189 + FreeLibrary(stripper_library);
1.190 + stripper_library = NULL;
1.191 + return NULL;
1.192 + }
1.193 +
1.194 + return pl;
1.195 +}
1.196 +
1.197 EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
1.198 {
1.199 /* We can only handle API v2 right now. In case the plugin API gets bumped,
1.200 @@ -139,7 +140,7 @@
1.201 if (strcmp(pName, METAMOD_PLAPI_NAME) == 0)
1.202 {
1.203 char our_file[256];
1.204 - if (!GetFileOfAddress(CreateInterface_MMS, our_file, sizeof(our_file)))
1.205 + if (!GetFileOfAddress((void *)CreateInterface_MMS, our_file, sizeof(our_file)))
1.206 {
1.207 return NULL;
1.208 }
1.209 @@ -164,19 +165,19 @@
1.210 return NULL;
1.211 }
1.212
1.213 -#if defined _MSC_VER
1.214 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1.215 -{
1.216 - if (fdwReason == DLL_PROCESS_DETACH)
1.217 - {
1.218 - UnloadInterface_MMS();
1.219 - }
1.220 - return TRUE;
1.221 -}
1.222 -#else
1.223 -__attribute__((destructor))
1.224 -static void gcc_fini()
1.225 -{
1.226 - UnloadInterface_MMS();
1.227 -}
1.228 +#if defined _MSC_VER
1.229 +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1.230 +{
1.231 + if (fdwReason == DLL_PROCESS_DETACH)
1.232 + {
1.233 + UnloadInterface_MMS();
1.234 + }
1.235 + return TRUE;
1.236 +}
1.237 +#else
1.238 +__attribute__((destructor))
1.239 +static void gcc_fini()
1.240 +{
1.241 + UnloadInterface_MMS();
1.242 +}
1.243 #endif