1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * Contributor(s): Scott "Damaged Soul" Ehlert
9 * : Pavol "PM OnoTo" Marko
10 * ============================
14 #include "CServerGameDLL.h"
15 #include "CServerGameEnts.h"
16 #include "CServerGameClients.h"
17 #include "CHLTVDirector.h"
18 #include "concommands.h"
23 * @brief Implementation of main SourceMM GameDLL functionality
27 CServerGameDLL *g_TempGameDLL = NULL;
28 CServerGameEnts *g_TempGameEnts = NULL;
29 CServerGameClients *g_TempGameClients = NULL;
30 CHLTVDirector *g_TempDirector = NULL;
31 GameDllInfo g_GameDll = {false, NULL, NULL};
32 EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
33 SourceHook::CSourceHookImpl g_SourceHook;
34 std::string g_ModPath;
35 std::string g_BinPath;
37 ///////////////////////////////////
38 // Main code for HL2 Interaction //
39 ///////////////////////////////////
41 //This is where the magic happens
42 SMM_API void *CreateInterface(const char *name, int *ret)
44 if (!g_GameDll.loaded)
47 // Currently, HL2 Engine has loaded Metamod:Source
48 // It is now asking it to get an interface. We don't have one,
49 // so we're gonna try to give it a fake one to get the information we need.
50 // Then we'll swap the vtables with the real one from the real gamedll.
52 if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
54 //We're in. Give the server our fake class as bait.
58 g_TempGameDLL = new CServerGameDLL;
60 return static_cast<void *>(g_TempGameDLL);
61 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
65 g_TempGameEnts = new CServerGameEnts;
67 return static_cast<void *>(g_TempGameEnts);
68 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
72 g_TempGameClients = new CServerGameClients;
74 return static_cast<void *>(g_TempGameClients);
75 } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
79 g_TempDirector = new CHLTVDirector;
81 return static_cast<void *>(g_TempDirector);
89 META_INTERFACE_MACRO(server, g_GameDll.factory);
93 bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
95 if (!g_GameDll.loaded)
97 //The gamedll isn't loaded yet. We need to find out where it's hiding.
98 IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
101 Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
105 //Guess the file name
106 //We'll have better heurestics[sp?] later on
107 char mod_path[128], full_path[255];
108 ive->GetGameDir(mod_path, sizeof(mod_path)-1);
109 g_ModPath.assign(mod_path);
110 #if defined WIN32 || defined _WIN32
111 snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
113 snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
116 g_BinPath.assign(full_path);
118 //See if the file even exists
119 FILE *fp = fopen(g_BinPath.c_str(), "r");
122 Error("Metamod:Source could not read %s", g_BinPath.c_str());
129 g_GameDll.lib = dlmount(g_BinPath.c_str());
132 Error("Metamod:Source could not load GameDLL: %s", dlerror());
136 g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
137 if (!g_GameDll.factory)
139 Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
140 dlclose(g_GameDll.lib);
144 //Find the new IServerGameDLL pointer
145 IServerGameDLL *serverDll;
146 serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
149 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
150 dlclose(g_GameDll.lib);
154 //Set this information early in case our wrappers are called somehow
155 g_Engine.engineFactory = engineFactory;
156 g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
159 Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
160 dlclose(g_GameDll.lib);
163 g_Engine.fileSystemFactory = fileSystemFactory;
164 g_Engine.pGlobals = pGlobals;
165 g_Engine.physicsFactory = physicsFactory;
166 g_Engine.engine = ive;
168 //Attempt to load the GameDLL
169 // Note that nothing will be intercepting yet.
170 // This is the one and only call that plugins have no chance of seeing.
171 // Likewise, you won't be able to trick the Server DLL into loading random things.
172 // Luckily, because of SourceHook, this really isn't a problem - you can change
173 // the virtual interfaces in anything it requests.
174 if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
176 //For some reason, the GameDLL failed to load.
177 Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
178 dlclose(g_GameDll.lib);
182 //Retrieve the pointers we'll need from the GameDLL
183 IServerGameEnts *serverEnts;
184 IServerGameClients *serverClients;
185 IHLTVDirector *serverHLTV;
187 serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
190 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
191 dlclose(g_GameDll.lib);
195 serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
198 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
199 dlclose(g_GameDll.lib);
203 serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
206 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
207 dlclose(g_GameDll.lib);
211 //The GameDLL has given the go - now we'll swap the vtables ( eww! )
212 //This ugly hack will patch the pointer we've given the engine
213 // so it will just be calling the real GameDLL instead.
214 // Unlike Metamod:HL1, this is the effect we want, rather than duplicating
215 // the entire class (because SourceHook doesn't do that).
216 void *vtableDst = ((void *)(g_TempGameDLL));
217 void *vtableSrc = ((void *)(serverDll));
219 SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameDLL), SH_MEM_READ|SH_MEM_WRITE);
220 memcpy(vtableDst, vtableSrc, sizeof(IServerGameDLL));
222 //Now patch IServerGameEnts
223 vtableDst = ((void *)(g_TempGameEnts));
224 vtableSrc = ((void *)(serverEnts));
225 SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameEnts), SH_MEM_READ|SH_MEM_WRITE);
226 memcpy(vtableDst, vtableSrc, sizeof(IServerGameEnts));
228 //Now patch IServerGameClients
229 vtableDst = ((void *)(g_TempGameClients));
230 vtableSrc = ((void *)(serverClients));
231 SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameClients), SH_MEM_READ|SH_MEM_WRITE);
232 memcpy(vtableDst, vtableSrc, sizeof(IServerGameClients));
234 //Now patch IHLTVDirector
235 vtableDst = ((void *)(g_TempDirector));
236 vtableSrc = ((void *)(serverHLTV));
237 SourceHook::SetMemAccess(vtableDst, sizeof(IHLTVDirector), SH_MEM_READ|SH_MEM_WRITE);
238 memcpy(vtableDst, vtableSrc, sizeof(IHLTVDirector));
241 g_GameDll.loaded = true;
243 //Initialize our console hooks
244 ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
246 //Now it's safe to load plugins.
247 #if defined WIN32 || defined _WIN32
248 snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
250 snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
253 LoadPluginsFromFile(full_path);
259 //Somehow, the function got here. This should be impossible, as not only is it
260 // only called once, but the vtables should be overridden.
261 Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
266 int LoadPluginsFromFile(const char *file)
269 int total = 0, skipped=0;
273 fp = fopen(file, "rt");
276 LogMessage("[META] Could not open plugins file %s\n", file);
280 char buffer[255], error[255], full_path[128];
281 const char *ptr, *ext;
285 fgets(buffer, sizeof(buffer)-1, fp);
286 if (buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
288 //First find if it's an absolute path or not...
289 if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
291 //If we're in an absolute path, ignore our normal heuristics
292 id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
293 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
295 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
303 //Attempt to find a file extension
304 ptr = UTIL_GetExtension(buffer);
305 //Add an extension if there's none there
308 #if defined WIN32 || defined _WIN32
316 //Format the new path
317 #if defined WIN32 || defined _WIN32
318 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
320 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
322 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
323 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
325 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
338 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
340 LogMessage("[META] Loaded %d plugins from file.", total);
346 //Wrapper function. This is called when the GameDLL thinks it's using
347 // the engine's real engineFactory.
348 void *EngineFactory(const char *name, int *ret)
350 META_INTERFACE_MACRO(engine, g_Engine.engineFactory);
353 //Wrapper function. This is called when the GameDLL thinks it's using
354 // the engine's real physicsFactory.
355 void *PhysicsFactory(const char *name, int *ret)
357 META_INTERFACE_MACRO(physics, g_Engine.physicsFactory);
360 //Wrapper function. This is called when the GameDLL thinks it's using
361 // the engine's real fileSystemFactory.
362 void *FileSystemFactory(const char *name, int *ret)
364 META_INTERFACE_MACRO(fileSystem, g_Engine.fileSystemFactory);
367 void LogMessage(const char *msg, ...)
370 static char buffer[2048];
375 vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
376 strcat(buffer, "\n");
379 g_Engine.engine->LogPrint(buffer);