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 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
29 CServerGameDLL g_TempGameDLL;
30 CServerGameEnts g_TempGameEnts;
31 CServerGameClients g_TempGameClients;
32 CHLTVDirector g_TempDirector;
33 GameDllInfo g_GameDll = {false, NULL, NULL};
34 EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
35 SourceHook::CSourceHookImpl g_SourceHook;
36 SourceHook::ISourceHook *g_SHPtr;
37 std::string g_ModPath;
38 std::string g_BinPath;
39 PluginId g_PLID = Pl_Console; //Technically, SourceMM is the "Console" plugin... :p
40 bool bInShutdown = false;
41 bool bInFirstLevel = true;
43 ///////////////////////////////////
44 // Main code for HL2 Interaction //
45 ///////////////////////////////////
47 //This is where the magic happens
48 SMM_API void *CreateInterface(const char *name, int *ret)
50 if (!g_GameDll.loaded)
53 // Currently, HL2 Engine has loaded Metamod:Source
54 // It is now asking it to get an interface. We don't have one,
55 // so we're gonna try to give it a fake one to get the information we need.
56 // Then, the the interface will forward the calls to the original interface
58 if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
60 //We're in. Give the server our fake class as bait.
63 return static_cast<void *>(&g_TempGameDLL);
64 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
68 return static_cast<void *>(&g_TempGameEnts);
69 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
73 return static_cast<void *>(&g_TempGameClients);
74 } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
78 return static_cast<void *>(&g_TempDirector);
86 META_INTERFACE_MACRO(server, g_GameDll.factory);
90 bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
93 return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
95 if (!g_GameDll.loaded)
97 //Initialize SourceHook
98 g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
100 //The gamedll isn't loaded yet. We need to find out where it's hiding.
101 IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
104 Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
108 //Guess the file name
109 //We'll have better heurestics[sp?] later on
110 char mod_path[128], full_path[255];
111 ive->GetGameDir(mod_path, sizeof(mod_path)-1);
112 g_ModPath.assign(mod_path);
113 #if defined WIN32 || defined _WIN32
114 snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
116 snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
119 g_BinPath.assign(full_path);
121 //See if the file even exists
122 FILE *fp = fopen(g_BinPath.c_str(), "r");
125 Error("Metamod:Source could not read %s", g_BinPath.c_str());
132 g_GameDll.lib = dlmount(g_BinPath.c_str());
135 Error("Metamod:Source could not load GameDLL: %s", dlerror());
139 g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
140 if (!g_GameDll.factory)
142 Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
143 dlclose(g_GameDll.lib);
147 //Find the new IServerGameDLL pointer
148 IServerGameDLL *serverDll;
149 serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
152 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
153 dlclose(g_GameDll.lib);
157 //Set this information early in case our wrappers are called somehow
158 g_Engine.engineFactory = engineFactory;
159 g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
162 Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
163 dlclose(g_GameDll.lib);
166 g_Engine.fileSystemFactory = fileSystemFactory;
167 g_Engine.pGlobals = pGlobals;
168 g_Engine.physicsFactory = physicsFactory;
169 g_Engine.engine = ive;
171 //Attempt to load the GameDLL
172 // Note that nothing will be intercepting yet.
173 // This is the one and only call that plugins have no chance of seeing.
174 // Likewise, you won't be able to trick the Server DLL into loading random things.
175 // Luckily, because of SourceHook, this really isn't a problem - you can change
176 // the virtual interfaces in anything it requests.
177 if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
179 //For some reason, the GameDLL failed to load.
180 Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
181 dlclose(g_GameDll.lib);
185 //Retrieve the pointers we'll need from the GameDLL
186 IServerGameEnts *serverEnts;
187 IServerGameClients *serverClients;
188 IHLTVDirector *serverHLTV;
190 serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
193 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
194 dlclose(g_GameDll.lib);
198 serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
201 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
202 dlclose(g_GameDll.lib);
206 serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
209 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
210 dlclose(g_GameDll.lib);
214 // Now tell the global temp classes that they can call the original functions
215 g_TempDirector.SetOrig(serverHLTV);
216 g_TempGameClients.SetOrig(serverClients);
217 g_TempGameEnts.SetOrig(serverEnts);
218 g_TempGameDLL.SetOrig(serverDll);
221 g_GameDll.loaded = true;
223 //Initialize our console hooks
224 ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
226 //Now it's safe to load plugins.
227 #if defined WIN32 || defined _WIN32
228 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
230 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
233 LoadPluginsFromFile(full_path);
235 //All plugins are now loaded.
236 g_PluginMngr.SetAllLoaded();
238 //Like metamod, reload plugins at the end of the map.
239 //This is so plugins can hook everything on load, BUT, new plugins will be reloaded
240 // if the server is shut down (silly, but rare case).
241 bInFirstLevel = true;
242 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, serverDll, LevelShutdown_handler, false);
248 //Somehow, the function got here. This should be impossible.
249 Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
254 // The engine uses the DLL even after it has call DLLShutdown, so we unload it
255 // when it unloads us
263 if (fdwReason == DLL_PROCESS_DETACH)
265 if (g_GameDll.lib && g_GameDll.loaded)
266 dlclose(g_GameDll.lib);
267 memset(&g_GameDll, 0, sizeof(GameDllInfo));
271 #elif defined __linux__
272 void __attribute__ ((destructor)) app_fini(void)
274 if (g_GameDll.lib && g_GameDll.loaded)
275 dlclose(g_GameDll.lib);
276 memset(&g_GameDll, 0, sizeof(GameDllInfo));
280 void CServerGameDLL::DLLShutdown()
282 //Call the original function
283 m_pOrig->DLLShutdown();
286 g_PluginMngr.UnloadAll();
288 // Shutdown sourcehook now
289 g_SourceHook.CompleteShutdown();
292 int LoadPluginsFromFile(const char *file)
295 int total = 0, skipped=0;
299 fp = fopen(file, "rt");
302 LogMessage("[META] Could not open plugins file %s\n", file);
306 char buffer[255], error[255], full_path[128];
307 const char *ptr, *ext;
312 fgets(buffer, sizeof(buffer)-1, fp);
313 length = strlen(buffer);
316 if (buffer[length-1] == '\n')
317 buffer[length-1] = '\0';
318 if (buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
320 //First find if it's an absolute path or not...
321 if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
323 //If we're in an absolute path, ignore our normal heuristics
324 id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
325 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
327 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
335 //Attempt to find a file extension
336 ptr = UTIL_GetExtension(buffer);
337 //Add an extension if there's none there
340 #if defined WIN32 || defined _WIN32
348 //Format the new path
349 #if defined WIN32 || defined _WIN32
350 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
352 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
354 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
355 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
357 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
370 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
372 LogMessage("[META] Loaded %d plugins from file.", total);
378 //Wrapper function. This is called when the GameDLL thinks it's using
379 // the engine's real engineFactory.
380 void *EngineFactory(const char *name, int *ret)
382 META_INTERFACE_MACRO(engine, g_Engine.engineFactory);
385 //Wrapper function. This is called when the GameDLL thinks it's using
386 // the engine's real physicsFactory.
387 void *PhysicsFactory(const char *name, int *ret)
389 META_INTERFACE_MACRO(physics, g_Engine.physicsFactory);
392 //Wrapper function. This is called when the GameDLL thinks it's using
393 // the engine's real fileSystemFactory.
394 void *FileSystemFactory(const char *name, int *ret)
396 META_INTERFACE_MACRO(fileSystem, g_Engine.fileSystemFactory);
399 void LogMessage(const char *msg, ...)
402 static char buffer[2048];
407 vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
408 strcat(buffer, "\n");
411 g_Engine.engine->LogPrint(buffer);
414 void LevelShutdown_handler(void)
419 #if defined WIN32 || defined _WIN32
420 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
422 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
424 LoadPluginsFromFile(full_path);
426 bInFirstLevel = false;
429 RETURN_META(MRES_IGNORED);