Moved adding of FCVAR_GAMEDLL to Unload; added SourceHook iface/impl info to meta version
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 * ============================
13 #include <interface.h>
15 #include "CServerGameDLL.h"
16 #include "CServerGameEnts.h"
17 #include "CServerGameClients.h"
18 #include "CHLTVDirector.h"
20 #include "concommands.h"
25 * @brief Implementation of main SourceMM GameDLL functionality
29 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
31 CServerGameDLL g_TempGameDLL;
32 CServerGameEnts g_TempGameEnts;
33 CServerGameClients g_TempGameClients;
34 CHLTVDirector g_TempDirector;
35 GameDllInfo g_GameDll = {false, NULL, NULL};
36 EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
37 SourceHook::CSourceHookImpl g_SourceHook;
38 SourceHook::ISourceHook *g_SHPtr;
39 std::string g_ModPath;
40 std::string g_BinPath;
41 PluginId g_PLID = Pl_Console; //Technically, SourceMM is the "Console" plugin... :p
42 bool bInShutdown = false;
43 bool bInFirstLevel = true;
45 ///////////////////////////////////
46 // Main code for HL2 Interaction //
47 ///////////////////////////////////
49 //This is where the magic happens
50 SMM_API void *CreateInterface(const char *name, int *ret)
52 if (!g_GameDll.loaded)
55 // Currently, HL2 Engine has loaded Metamod:Source
56 // It is now asking it to get an interface. We don't have one,
57 // so we're gonna try to give it a fake one to get the information we need.
58 // Then, the the interface will forward the calls to the original interface
60 if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
62 //We're in. Give the server our fake class as bait.
65 return static_cast<void *>(&g_TempGameDLL);
66 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
70 return static_cast<void *>(&g_TempGameEnts);
71 } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
75 return static_cast<void *>(&g_TempGameClients);
76 } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
80 return static_cast<void *>(&g_TempDirector);
88 META_INTERFACE_MACRO(server, g_GameDll.factory);
92 bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
95 return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
97 if (!g_GameDll.loaded)
99 //Initialize SourceHook
100 g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
102 //The gamedll isn't loaded yet. We need to find out where it's hiding.
103 IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
106 Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
110 //Guess the file name
111 //We'll have better heurestics[sp?] later on
112 char mod_path[128], full_path[255];
113 ive->GetGameDir(mod_path, sizeof(mod_path)-1);
114 g_ModPath.assign(mod_path);
115 #if defined WIN32 || defined _WIN32
116 snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
118 snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
121 g_BinPath.assign(full_path);
123 //See if the file even exists
124 FILE *fp = fopen(g_BinPath.c_str(), "r");
127 Error("Metamod:Source could not read %s", g_BinPath.c_str());
134 g_GameDll.lib = dlmount(g_BinPath.c_str());
137 Error("Metamod:Source could not load GameDLL: %s", dlerror());
141 g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
142 if (!g_GameDll.factory)
144 Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
145 dlclose(g_GameDll.lib);
149 //Find the new IServerGameDLL pointer
150 IServerGameDLL *serverDll;
151 serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
154 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
155 dlclose(g_GameDll.lib);
159 //Set this information early in case our wrappers are called somehow
160 g_Engine.engineFactory = engineFactory;
161 g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
164 Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
165 dlclose(g_GameDll.lib);
168 g_Engine.fileSystemFactory = fileSystemFactory;
169 g_Engine.pGlobals = pGlobals;
170 g_Engine.physicsFactory = physicsFactory;
171 g_Engine.engine = ive;
173 //Attempt to load the GameDLL
174 // Note that nothing will be intercepting yet.
175 // This is the one and only call that plugins have no chance of seeing.
176 // Likewise, you won't be able to trick the Server DLL into loading random things.
177 // Luckily, because of SourceHook, this really isn't a problem - you can change
178 // the virtual interfaces in anything it requests.
179 if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
181 //For some reason, the GameDLL failed to load.
182 Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
183 dlclose(g_GameDll.lib);
187 //Retrieve the pointers we'll need from the GameDLL
188 IServerGameEnts *serverEnts;
189 IServerGameClients *serverClients;
190 IHLTVDirector *serverHLTV;
192 serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
195 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
196 dlclose(g_GameDll.lib);
200 serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
203 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
204 dlclose(g_GameDll.lib);
208 serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
211 Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
212 dlclose(g_GameDll.lib);
216 // Now tell the global temp classes that they can call the original functions
217 g_TempDirector.SetOrig(serverHLTV);
218 g_TempGameClients.SetOrig(serverClients);
219 g_TempGameEnts.SetOrig(serverEnts);
220 g_TempGameDLL.SetOrig(serverDll);
223 g_GameDll.loaded = true;
225 //Initialize our console hooks
226 ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
228 //Now it's safe to load plugins.
229 #if defined WIN32 || defined _WIN32
230 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
232 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
235 LoadPluginsFromFile(full_path);
237 //All plugins are now loaded.
238 g_PluginMngr.SetAllLoaded();
240 //Like metamod, reload plugins at the end of the map.
241 //This is so plugins can hook everything on load, BUT, new plugins will be reloaded
242 // if the server is shut down (silly, but rare case).
243 bInFirstLevel = true;
244 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, serverDll, LevelShutdown_handler, false);
250 //Somehow, the function got here. This should be impossible.
251 Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
256 // The engine uses the DLL even after it has call DLLShutdown, so we unload it
257 // when it unloads us
265 if (fdwReason == DLL_PROCESS_DETACH)
267 if (g_GameDll.lib && g_GameDll.loaded)
268 dlclose(g_GameDll.lib);
269 memset(&g_GameDll, 0, sizeof(GameDllInfo));
273 #elif defined __linux__
274 void __attribute__ ((destructor)) app_fini(void)
276 if (g_GameDll.lib && g_GameDll.loaded)
277 dlclose(g_GameDll.lib);
278 memset(&g_GameDll, 0, sizeof(GameDllInfo));
282 void CServerGameDLL::DLLShutdown()
284 //Call the original function
285 m_pOrig->DLLShutdown();
288 g_PluginMngr.UnloadAll();
290 // Shutdown sourcehook now
291 g_SourceHook.CompleteShutdown();
293 // Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly
294 g_SMConVarAccessor.MarkCommandsAsGameDLL();
297 int LoadPluginsFromFile(const char *file)
300 int total = 0, skipped=0;
304 fp = fopen(file, "rt");
307 LogMessage("[META] Could not open plugins file %s\n", file);
311 char buffer[255], error[255], full_path[128];
312 const char *ptr, *ext;
317 fgets(buffer, sizeof(buffer)-1, fp);
318 length = strlen(buffer);
321 if (buffer[length-1] == '\n')
322 buffer[length-1] = '\0';
323 if (buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
325 //First find if it's an absolute path or not...
326 if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
328 //If we're in an absolute path, ignore our normal heuristics
329 id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
330 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
332 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
340 //Attempt to find a file extension
341 ptr = UTIL_GetExtension(buffer);
342 //Add an extension if there's none there
345 #if defined WIN32 || defined _WIN32
353 //Format the new path
354 #if defined WIN32 || defined _WIN32
355 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
357 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
359 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
360 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
362 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
375 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
377 LogMessage("[META] Loaded %d plugins from file.", total);
383 //Wrapper function. This is called when the GameDLL thinks it's using
384 // the engine's real engineFactory.
385 void *EngineFactory(const char *name, int *ret)
387 META_INTERFACE_MACRO(engine, g_Engine.engineFactory);
390 //Wrapper function. This is called when the GameDLL thinks it's using
391 // the engine's real physicsFactory.
392 void *PhysicsFactory(const char *name, int *ret)
394 META_INTERFACE_MACRO(physics, g_Engine.physicsFactory);
397 //Wrapper function. This is called when the GameDLL thinks it's using
398 // the engine's real fileSystemFactory.
399 void *FileSystemFactory(const char *name, int *ret)
401 META_INTERFACE_MACRO(fileSystem, g_Engine.fileSystemFactory);
404 void LogMessage(const char *msg, ...)
407 static char buffer[2048];
412 vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
413 strcat(buffer, "\n");
416 g_Engine.engine->LogPrint(buffer);
419 void LevelShutdown_handler(void)
424 #if defined WIN32 || defined _WIN32
425 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
427 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
429 LoadPluginsFromFile(full_path);
431 bInFirstLevel = false;
434 RETURN_META(MRES_IGNORED);