1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/loader/stripper_loader.cpp Wed Oct 10 01:52:25 2007 +0000
1.3 @@ -0,0 +1,182 @@
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 +EXPORT METAMOD_PLUGIN *CreateInterface_MMS(const MetamodVersionInfo *mvi, const MetamodLoaderInfo *mli)
1.101 +{
1.102 + /* We can only handle API v2 right now. In case the plugin API gets bumped,
1.103 + * we ignore other cases.
1.104 + */
1.105 + if (mvi->api_major != 2)
1.106 + {
1.107 + return NULL;
1.108 + }
1.109 +
1.110 + const char *file = NULL;
1.111 +
1.112 + if (mvi->source_engine == SOURCE_ENGINE_ORANGEBOX)
1.113 + {
1.114 + file = MMS_1_6_EP2_FILE;
1.115 + }
1.116 + else if (mvi->source_engine <= SOURCE_ENGINE_EPISODEONE)
1.117 + {
1.118 + file = MMS_1_6_EP1_FILE;
1.119 + }
1.120 + else
1.121 + {
1.122 + return NULL;
1.123 + }
1.124 +
1.125 + char our_path[256];
1.126 + UTIL_Format(our_path, sizeof(our_path), "%s" PATH_SEP_CHAR MMS_1_4_EP1_FILE, mli->pl_path);
1.127 +
1.128 + return TryAndLoadLibrary(our_path);
1.129 +}
1.130 +
1.131 +EXPORT void UnloadInterface_MMS()
1.132 +{
1.133 + if (stripper_library != NULL)
1.134 + {
1.135 + FreeLibrary(stripper_library);
1.136 + stripper_library = NULL;
1.137 + }
1.138 +}
1.139 +
1.140 +EXPORT void *CreateInterface(const char *pName, int *pReturnCode)
1.141 +{
1.142 + if (strcmp(pName, METAMOD_PLAPI_NAME) == 0)
1.143 + {
1.144 + char our_file[256];
1.145 + if (!GetFileOfAddress(CreateInterface_MMS, our_file, sizeof(our_file)))
1.146 + {
1.147 + return NULL;
1.148 + }
1.149 +
1.150 + /* Go backwards and get the first token */
1.151 + size_t len = strlen(our_file);
1.152 + for (size_t i = len - 1; i >= 0 && i < len; i--)
1.153 + {
1.154 + if (IsPathSepChar(our_file[i]))
1.155 + {
1.156 + our_file[i] = '\0';
1.157 + break;
1.158 + }
1.159 + }
1.160 +
1.161 + char new_file[256];
1.162 + UTIL_Format(new_file, sizeof(new_file), "%s" PATH_SEP_CHAR MMS_1_4_EP1_FILE, our_file);
1.163 +
1.164 + return TryAndLoadLibrary(new_file);
1.165 + }
1.166 +
1.167 + return NULL;
1.168 +}
1.169 +
1.170 +#if defined _MSC_VER
1.171 +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1.172 +{
1.173 + if (fdwReason == DLL_PROCESS_DETACH)
1.174 + {
1.175 + UnloadInterface_MMS();
1.176 + }
1.177 + return TRUE;
1.178 +}
1.179 +#else
1.180 +__attribute__((destructor))
1.181 +static void gcc_fini()
1.182 +{
1.183 + UnloadInterface_MMS();
1.184 +}
1.185 +#endif