1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/sourcemm.cpp Sat Apr 16 19:59:26 2005 +0000
1.3 @@ -0,0 +1,379 @@
1.4 +/* ======== SourceMM ========
1.5 +* Copyright (C) 2004-2005 SourceMM Development Team
1.6 +* No warranties of any kind
1.7 +*
1.8 +* License: zlib/libpng
1.9 +*
1.10 +* Author(s): David "BAILOPAN" Anderson
1.11 +* Contributor(s): Scott "Damaged Soul" Ehlert
1.12 +* : Pavol "PM OnoTo" Marko
1.13 +* ============================
1.14 +*/
1.15 +
1.16 +#include "sourcemm.h"
1.17 +#include "CServerGameDLL.h"
1.18 +#include "CServerGameEnts.h"
1.19 +#include "CServerGameClients.h"
1.20 +#include "CHLTVDirector.h"
1.21 +#include "concommands.h"
1.22 +
1.23 +/**
1.24 + * @brief Implementation of main SourceMM GameDLL functionality
1.25 + * @file sourcemm.cpp
1.26 + */
1.27 +
1.28 +CServerGameDLL *g_TempGameDLL = NULL;
1.29 +CServerGameEnts *g_TempGameEnts = NULL;
1.30 +CServerGameClients *g_TempGameClients = NULL;
1.31 +CHLTVDirector *g_TempDirector = NULL;
1.32 +GameDllInfo g_GameDll = {false, NULL, NULL};
1.33 +EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
1.34 +SourceHook::CSourceHookImpl g_SourceHook;
1.35 +std::string g_ModPath;
1.36 +std::string g_BinPath;
1.37 +
1.38 +///////////////////////////////////
1.39 +// Main code for HL2 Interaction //
1.40 +///////////////////////////////////
1.41 +
1.42 +//This is where the magic happens
1.43 +SMM_API void *CreateInterface(const char *name, int *ret)
1.44 +{
1.45 + if (!g_GameDll.loaded)
1.46 + {
1.47 + //State of the Mod:
1.48 + // Currently, HL2 Engine has loaded Metamod:Source
1.49 + // It is now asking it to get an interface. We don't have one,
1.50 + // so we're gonna try to give it a fake one to get the information we need.
1.51 + // Then we'll swap the vtables with the real one from the real gamedll.
1.52 +
1.53 + if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
1.54 + {
1.55 + //We're in. Give the server our fake class as bait.
1.56 + if (ret)
1.57 + *ret = IFACE_OK;
1.58 +
1.59 + g_TempGameDLL = new CServerGameDLL;
1.60 +
1.61 + return static_cast<void *>(g_TempGameDLL);
1.62 + } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
1.63 + if (ret)
1.64 + *ret = IFACE_OK;
1.65 +
1.66 + g_TempGameEnts = new CServerGameEnts;
1.67 +
1.68 + return static_cast<void *>(g_TempGameEnts);
1.69 + } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
1.70 + if (ret)
1.71 + *ret = IFACE_OK;
1.72 +
1.73 + g_TempGameClients = new CServerGameClients;
1.74 +
1.75 + return static_cast<void *>(g_TempGameClients);
1.76 + } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
1.77 + if (ret)
1.78 + *ret = IFACE_OK;
1.79 +
1.80 + g_TempDirector = new CHLTVDirector;
1.81 +
1.82 + return static_cast<void *>(g_TempDirector);
1.83 + } else {
1.84 + if (ret)
1.85 + *ret = IFACE_FAILED;
1.86 +
1.87 + return NULL;
1.88 + }
1.89 + } else {
1.90 + void *d = (g_GameDll.factory)(name, ret);
1.91 +
1.92 + return d;
1.93 + }
1.94 +}
1.95 +
1.96 +bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
1.97 +{
1.98 + if (!g_GameDll.loaded)
1.99 + {
1.100 + //The gamedll isn't loaded yet. We need to find out where it's hiding.
1.101 + IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
1.102 + if (!ive)
1.103 + {
1.104 + Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
1.105 + return false;
1.106 + }
1.107 +
1.108 + //Guess the file name
1.109 + //We'll have better heurestics[sp?] later on
1.110 + char mod_path[128], full_path[255];
1.111 + ive->GetGameDir(mod_path, sizeof(mod_path)-1);
1.112 + g_ModPath.assign(mod_path);
1.113 +#if defined WIN32 || defined _WIN32
1.114 + snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
1.115 +#else
1.116 + snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
1.117 +#endif
1.118 +
1.119 + g_BinPath.assign(full_path);
1.120 +
1.121 + //See if the file even exists
1.122 + FILE *fp = fopen(g_BinPath.c_str(), "r");
1.123 + if (!fp)
1.124 + {
1.125 + Error("Metamod:Source could not read %s", g_BinPath.c_str());
1.126 + return false;
1.127 + }
1.128 + fclose(fp);
1.129 + fp = NULL;
1.130 +
1.131 + //Load the DLL
1.132 + g_GameDll.lib = dlmount(g_BinPath.c_str());
1.133 + if (!g_GameDll.lib)
1.134 + {
1.135 + Error("Metamod:Source could not load GameDLL: %s", dlerror());
1.136 + return false;
1.137 + } else {
1.138 + //Find its factory
1.139 + g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
1.140 + if (!g_GameDll.factory)
1.141 + {
1.142 + Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
1.143 + dlclose(g_GameDll.lib);
1.144 + return false;
1.145 + }
1.146 +
1.147 + //Find the new IServerGameDLL pointer
1.148 + IServerGameDLL *serverDll;
1.149 + serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
1.150 + if (!serverDll)
1.151 + {
1.152 + Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
1.153 + dlclose(g_GameDll.lib);
1.154 + return false;
1.155 + }
1.156 +
1.157 + //Set this information early in case our wrappers are called somehow
1.158 + g_Engine.engineFactory = engineFactory;
1.159 + g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
1.160 + if (!g_Engine.icvar)
1.161 + {
1.162 + Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
1.163 + dlclose(g_GameDll.lib);
1.164 + return false;
1.165 + }
1.166 + g_Engine.fileSystemFactory = fileSystemFactory;
1.167 + g_Engine.pGlobals = pGlobals;
1.168 + g_Engine.physicsFactory = physicsFactory;
1.169 + g_Engine.engine = ive;
1.170 +
1.171 + //Attempt to load the GameDLL
1.172 + // Note that nothing will be intercepting yet.
1.173 + // This is the one and only call that plugins have no chance of seeing.
1.174 + // Likewise, you won't be able to trick the Server DLL into loading random things.
1.175 + // Luckily, because of SourceHook, this really isn't a problem - you can change
1.176 + // the virtual interfaces in anything it requests.
1.177 + if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
1.178 + {
1.179 + //For some reason, the GameDLL failed to load.
1.180 + Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
1.181 + dlclose(g_GameDll.lib);
1.182 + return false;
1.183 + }
1.184 +
1.185 + IServerGameEnts *serverEnts;
1.186 + IServerGameClients *serverClients;
1.187 + IHLTVDirector *serverHLTV;
1.188 +
1.189 + serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
1.190 + if (!serverEnts)
1.191 + {
1.192 + Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
1.193 + dlclose(g_GameDll.lib);
1.194 + return false;
1.195 + }
1.196 +
1.197 + serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
1.198 + if (!serverClients)
1.199 + {
1.200 + Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
1.201 + dlclose(g_GameDll.lib);
1.202 + return false;
1.203 + }
1.204 +
1.205 + serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
1.206 + if (!serverHLTV)
1.207 + {
1.208 + Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
1.209 + dlclose(g_GameDll.lib);
1.210 + return false;
1.211 + }
1.212 +
1.213 + //The GameDLL has given the go - now we'll swap the vtables ( eww! )
1.214 + //This ugly hack will patch the pointer we've given the engine
1.215 + // so it will just be calling the real GameDLL instead.
1.216 + // Unlike Metamod:HL1, this is the effect we want, rather than duplicating
1.217 + // the entire class (because SourceHook doesn't do that).
1.218 + void *vtableDst = ((void *)(g_TempGameDLL));
1.219 + void *vtableSrc = ((void *)(serverDll));
1.220 +
1.221 + SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameDLL), SH_MEM_READ|SH_MEM_WRITE);
1.222 + memcpy(vtableDst, vtableSrc, sizeof(IServerGameDLL));
1.223 +
1.224 + //Now patch IServerGameEnts
1.225 + vtableDst = ((void *)(g_TempGameEnts));
1.226 + vtableSrc = ((void *)(serverEnts));
1.227 + SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameEnts), SH_MEM_READ|SH_MEM_WRITE);
1.228 + memcpy(vtableDst, vtableSrc, sizeof(IServerGameEnts));
1.229 +
1.230 + //Now patch IServerGameClients
1.231 + vtableDst = ((void *)(g_TempGameClients));
1.232 + vtableSrc = ((void *)(serverClients));
1.233 + SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameClients), SH_MEM_READ|SH_MEM_WRITE);
1.234 + memcpy(vtableDst, vtableSrc, sizeof(IServerGameClients));
1.235 +
1.236 + //Now patch IHLTVDirector
1.237 + vtableDst = ((void *)(g_TempDirector));
1.238 + vtableSrc = ((void *)(serverHLTV));
1.239 + SourceHook::SetMemAccess(vtableDst, sizeof(IHLTVDirector), SH_MEM_READ|SH_MEM_WRITE);
1.240 + memcpy(vtableDst, vtableSrc, sizeof(IHLTVDirector));
1.241 +
1.242 + //Everything's done.
1.243 + g_GameDll.loaded = true;
1.244 +
1.245 + //Initialize our console hooks
1.246 + ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
1.247 +
1.248 + //Now it's safe to load plugins.
1.249 +#if defined WIN32 || defined _WIN32
1.250 + snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
1.251 +#else
1.252 + snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
1.253 +#endif
1.254 +
1.255 + LoadPluginsFromFile(full_path);
1.256 +
1.257 + return true;
1.258 + }
1.259 + }
1.260 +
1.261 + //Somehow, the function got here. This should be impossible, as not only is it
1.262 + // only called once, but the vtables should be overridden.
1.263 + Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
1.264 +
1.265 + return false;
1.266 +}
1.267 +
1.268 +int LoadPluginsFromFile(const char *file)
1.269 +{
1.270 + FILE *fp;
1.271 + int total = 0, skipped=0;
1.272 + PluginId id;
1.273 + bool already;
1.274 +
1.275 + fp = fopen(file, "rt");
1.276 + if (!fp)
1.277 + {
1.278 + LogMessage("[META] Could not open plugins file %s\n", file);
1.279 + return -1;
1.280 + }
1.281 +
1.282 + char buffer[255], error[255], full_path[128];
1.283 + const char *ptr, *ext;
1.284 + while (!feof(fp))
1.285 + {
1.286 + buffer[0] = '\0';
1.287 + fgets(buffer, sizeof(buffer)-1, fp);
1.288 + if (buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
1.289 + continue;
1.290 + //First find if it's an absolute path or not...
1.291 + if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
1.292 + {
1.293 + //If we're in an absolute path, ignore our normal heuristics
1.294 + id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
1.295 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.296 + {
1.297 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.298 + } else {
1.299 + if (already)
1.300 + skipped++;
1.301 + else
1.302 + total++;
1.303 + }
1.304 + } else {
1.305 + //Attempt to find a file extension
1.306 + ptr = UTIL_GetExtension(buffer);
1.307 + //Add an extension if there's none there
1.308 + if (!ptr)
1.309 + {
1.310 +#if defined WIN32 || defined _WIN32
1.311 + ext = ".dll";
1.312 +#else
1.313 + ext = "_i486.so";
1.314 +#endif
1.315 + } else {
1.316 + ext = "";
1.317 + }
1.318 + //Format the new path
1.319 +#if defined WIN32 || defined _WIN32
1.320 + snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
1.321 +#else
1.322 + snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
1.323 +#endif
1.324 + id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
1.325 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.326 + {
1.327 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.328 + } else {
1.329 + if (already)
1.330 + skipped++;
1.331 + else
1.332 + total++;
1.333 + }
1.334 + }
1.335 + }
1.336 + fclose(fp);
1.337 +
1.338 + if (skipped)
1.339 + {
1.340 + LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
1.341 + } else {
1.342 + LogMessage("[META] Loaded %d plugins from file.", total);
1.343 + }
1.344 +
1.345 + return total;
1.346 +}
1.347 +
1.348 +//Wrapper function. This is called when the GameDLL thinks it's using
1.349 +// the engine's real engineFactory.
1.350 +void *EngineFactory(const char *name, int *ret)
1.351 +{
1.352 + return (g_Engine.engineFactory)(name, ret);
1.353 +}
1.354 +
1.355 +//Wrapper function. This is called when the GameDLL thinks it's using
1.356 +// the engine's real physicsFactory.
1.357 +void *PhysicsFactory(const char *name, int *ret)
1.358 +{
1.359 + return (g_Engine.physicsFactory)(name, ret);
1.360 +}
1.361 +
1.362 +//Wrapper function. This is called when the GameDLL thinks it's using
1.363 +// the engine's real fileSystemFactory.
1.364 +void *FileSystemFactory(const char *name, int *ret)
1.365 +{
1.366 + return (g_Engine.fileSystemFactory)(name, ret);
1.367 +}
1.368 +
1.369 +void LogMessage(const char *msg, ...)
1.370 +{
1.371 + va_list ap;
1.372 + static char buffer[2048];
1.373 +
1.374 + buffer[0] = '\0';
1.375 +
1.376 + va_start(ap, msg);
1.377 + vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
1.378 + strcat(buffer, "\n");
1.379 + va_end(ap);
1.380 +
1.381 + g_Engine.engine->LogPrint(buffer);
1.382 +}