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;
28 CServerGameEnts g_TempGameEnts;
29 CServerGameClients g_TempGameClients;
30 CHLTVDirector g_TempDirector;
31 GameDllInfo g_GameDll = {false, NULL, NULL};
32 EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
33 SourceHook::CSourceHookImpl g_SourceHook;
34 SourceHook::ISourceHook *g_SHPtr;
35 std::string g_ModPath;
36 std::string g_BinPath;
37 PluginId g_PLID = Pl_Console; //Technically, SourceMM is the "Console" plugin... :p
38 bool bInShutdown = false;
40 ///////////////////////////////////
41 // Main code for HL2 Interaction //
42 ///////////////////////////////////
44 //This is where the magic happens
45 SMM_API void *CreateInterface(const char *name, int *ret)
47 if (!g_GameDll.loaded)
50 // Currently, HL2 Engine has loaded Metamod:Source
51 // It is now asking it to get an interface. We don't have one,
52 // so we're gonna try to give it a fake one to get the information we need.
53 // Then, the the interface will forward the calls to the original interface
55 if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
57 //We're in. Give the server our fake class as bait.
60 return static_cast<void *>(&g_TempGameDLL);
61 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
65 return static_cast<void *>(&g_TempGameEnts);
66 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
70 return static_cast<void *>(&g_TempGameClients);
71 } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
75 return static_cast<void *>(&g_TempDirector);
83 META_INTERFACE_MACRO(server, g_GameDll.factory);
87 bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
90 return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
92 if (!g_GameDll.loaded)
94 //Initialize SourceHook
95 g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
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 // Now tell the global temp classes that they can call the original functions
212 g_TempDirector.SetOrig(serverHLTV);
213 g_TempGameClients.SetOrig(serverClients);
214 g_TempGameEnts.SetOrig(serverEnts);
215 g_TempGameDLL.SetOrig(serverDll);
218 g_GameDll.loaded = true;
220 //Initialize our console hooks
221 ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
223 //Now it's safe to load plugins.
224 #if defined WIN32 || defined _WIN32
225 snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
227 snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
230 LoadPluginsFromFile(full_path);
232 //All plugins are now loaded.
233 g_PluginMngr.SetAllLoaded();
239 //Somehow, the function got here. This should be impossible.
240 Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
245 // The engine uses the DLL even after it has call DLLShutdown, so we unload it
246 // when it unloads us
254 if (fdwReason == DLL_PROCESS_DETACH)
256 if (g_GameDll.lib && g_GameDll.loaded)
257 dlclose(g_GameDll.lib);
258 memset(&g_GameDll, 0, sizeof(GameDllInfo));
262 #elif defined __linux__
263 void __attribute__ ((destructor)) app_fini(void)
265 if (g_GameDll.lib && g_GameDll.loaded)
266 dlclose(g_GameDll.lib);
267 memset(&g_GameDll, 0, sizeof(GameDllInfo));
271 void CServerGameDLL::DLLShutdown()
273 //Call the original function
274 m_pOrig->DLLShutdown();
277 g_PluginMngr.UnloadAll();
279 // Shutdown sourcehook now
280 g_SourceHook.CompleteShutdown();
283 int LoadPluginsFromFile(const char *file)
286 int total = 0, skipped=0;
290 fp = fopen(file, "rt");
293 LogMessage("[META] Could not open plugins file %s\n", file);
297 char buffer[255], error[255], full_path[128];
298 const char *ptr, *ext;
303 fgets(buffer, sizeof(buffer)-1, fp);
304 length = strlen(buffer);
307 if (buffer[length-1] == '\n')
308 buffer[length-1] = '\0';
309 if (buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
311 //First find if it's an absolute path or not...
312 if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
314 //If we're in an absolute path, ignore our normal heuristics
315 id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
316 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
318 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
326 //Attempt to find a file extension
327 ptr = UTIL_GetExtension(buffer);
328 //Add an extension if there's none there
331 #if defined WIN32 || defined _WIN32
339 //Format the new path
340 #if defined WIN32 || defined _WIN32
341 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
343 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
345 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
346 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
348 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
361 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
363 LogMessage("[META] Loaded %d plugins from file.", total);
369 //Wrapper function. This is called when the GameDLL thinks it's using
370 // the engine's real engineFactory.
371 void *EngineFactory(const char *name, int *ret)
373 META_INTERFACE_MACRO(engine, g_Engine.engineFactory);
376 //Wrapper function. This is called when the GameDLL thinks it's using
377 // the engine's real physicsFactory.
378 void *PhysicsFactory(const char *name, int *ret)
380 META_INTERFACE_MACRO(physics, g_Engine.physicsFactory);
383 //Wrapper function. This is called when the GameDLL thinks it's using
384 // the engine's real fileSystemFactory.
385 void *FileSystemFactory(const char *name, int *ret)
387 META_INTERFACE_MACRO(fileSystem, g_Engine.fileSystemFactory);
390 void LogMessage(const char *msg, ...)
393 static char buffer[2048];
398 vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
399 strcat(buffer, "\n");
402 g_Engine.engine->LogPrint(buffer);