3 * ======================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
32 #include <interface.h>
35 #include "metamod_provider.h"
36 #include "metamod_plugins.h"
37 #include "metamod_util.h"
38 #include "metamod_console.h"
39 #include "metamod_oslink.h"
45 using namespace SourceMM;
46 using namespace SourceHook;
49 * @brief Implementation of main SourceMM GameDLL functionality
53 SH_DECL_MANUALHOOK4(SGD_DLLInit, 0, 0, 0, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
54 SH_DECL_MANUALHOOK0(SGD_GameInit, 0, 0, 0, bool);
55 SH_DECL_MANUALHOOK6(SGD_LevelInit, 0, 0, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
56 SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
57 SH_DECL_MANUALHOOK0_void(SGD_DLLShutdown, 0, 0, 0);
59 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
60 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
61 void Handler_DLLShutdown();
62 void Handler_LevelShutdown();
63 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
64 bool Handler_GameInit();
70 CreateInterfaceFn factory;
76 bool parsed_game_info = false;
78 int gamedll_version = 0;
79 int engine_build = SOURCE_ENGINE_UNKNOWN;
80 List<game_dll_t *> gamedll_list;
81 bool is_gamedll_loaded = false;
82 bool in_first_level = true;
83 bool is_game_init = false;
84 bool vsp_load_attempted = false;
85 bool vsp_load_requested = false;
86 bool vsp_loaded = false;
87 game_dll_t gamedll_info;
88 ConVar *metamod_version = NULL;
89 ConVar *mm_pluginsfile = NULL;
90 IServerGameDLL *server = NULL;
91 CreateInterfaceFn engine_factory = NULL;
92 CreateInterfaceFn physics_factory = NULL;
93 CreateInterfaceFn filesystem_factory = NULL;
94 CGlobalVars *gpGlobals = NULL;
95 SourceHook::CSourceHookImpl g_SourceHook;
96 SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
97 PluginId g_PLID = Pl_Console;
98 META_RES last_meta_res;
99 IServerPluginCallbacks *vsp_callbacks = NULL;
101 MetamodSource g_Metamod;
103 void ClearGamedllList();
106 #define IFACE_MACRO(orig,nam) \
107 CPluginManager::CPlugin *pl; \
108 SourceHook::List<IMetamodListener *>::iterator event; \
109 IMetamodListener *api; \
112 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
114 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
116 mret = IFACE_FAILED; \
117 if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
118 if (ret) *ret = mret; \
123 return (orig)(iface, ret);
125 #define ITER_EVENT(evn, args) \
126 CPluginManager::CPlugin *pl; \
127 SourceHook::List<IMetamodListener *>::iterator event; \
128 IMetamodListener *api; \
129 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
131 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
137 /* Initialize everything here */
138 void InitMainStates()
140 char full_path[PATH_SIZE] = {0};
141 GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
142 full_bin_path.assign(full_path);
144 /* Like Metamod, reload plugins at the end of the map.
145 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
146 * if the server is shut down (silly, but rare case).
148 in_first_level = true;
150 SourceHook::MemFuncInfo info;
152 if (!provider->GetHookInfo(ProvidedHook_DLLInit, &info))
154 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLInit");
156 SH_MANUALHOOK_RECONFIGURE(SGD_DLLInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
157 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit, false);
158 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit_Post, true);
160 if (!provider->GetHookInfo(ProvidedHook_GameInit, &info))
162 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::GameInit");
164 SH_MANUALHOOK_RECONFIGURE(SGD_GameInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
165 SH_ADD_MANUALHOOK_STATICFUNC(SGD_GameInit, server, Handler_GameInit, false);
167 if (!provider->GetHookInfo(ProvidedHook_LevelInit, &info))
169 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelInit");
171 SH_MANUALHOOK_RECONFIGURE(SGD_LevelInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
172 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelInit, server, Handler_LevelInit, true);
174 if (!provider->GetHookInfo(ProvidedHook_LevelShutdown, &info))
176 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelShutdown");
178 SH_MANUALHOOK_RECONFIGURE(SGD_LevelShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
179 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelShutdown, server, Handler_LevelShutdown, true);
181 if (!provider->GetHookInfo(ProvidedHook_DLLShutdown, &info))
183 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLShutdown");
185 SH_MANUALHOOK_RECONFIGURE(SGD_DLLShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
186 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
189 /* This is where the magic happens */
190 SMM_API void *CreateInterface(const char *iface, int *ret)
192 /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
193 if (strcmp(iface, PLAPI_NAME) == 0)
195 provider->DisplayWarning("Do not try loading Metamod:Source as a plugin.\n");
205 if (strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
207 vsp_callbacks = provider->GetVSPCallbacks(iface);
209 if (vsp_callbacks != NULL && vsp_version == 0)
211 vsp_version = atoi(&iface[22]);
216 *ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
219 return vsp_callbacks;
222 if (!parsed_game_info)
224 parsed_game_info = true;
225 const char *game_dir = NULL;
226 char game_path[PATH_SIZE];
227 char mm_path[PATH_SIZE];
229 /* Get path to SourceMM DLL */
230 if (!GetFileOfAddress((void *)CreateInterface, mm_path, sizeof(mm_path)))
232 provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
236 metamod_path.assign(mm_path);
238 /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
239 game_dir = provider->GetCommandLineValue("-game", "hl2");
241 if (strcasecmp(game_dir, "ship") == 0)
243 engine_build = SOURCE_ENGINE_ORIGINAL;
247 engine_build = SOURCE_ENGINE_EPISODEONE;
250 /* Get absolute path */
251 abspath(game_path, game_dir);
252 mod_path.assign(game_path);
254 char temp_path[PATH_SIZE];
256 /* Path to gameinfo.txt */
257 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
259 FILE *fp = fopen(temp_path, "rt");
263 provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
268 char key[128], val[128];
270 bool gamebin = false;
273 char cur_path[PATH_SIZE];
275 getcwd(cur_path, PATH_SIZE);
277 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
279 UTIL_TrimComments(buffer);
280 UTIL_TrimLeft(buffer);
281 UTIL_TrimRight(buffer);
283 if (stricmp(buffer, "SearchPaths") == 0)
293 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
294 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
296 if (stricmp(key, "Game") == 0)
305 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
307 ptr = &(val[sizeof("|gameinfo_path|") - 1]);
312 lptr = mod_path.c_str();
318 size_t ptr_len = strlen(ptr);
319 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
321 ptr[--ptr_len] = '\0';
324 /* No need to append "bin" if key is GameBin */
327 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
331 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
335 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
338 /* If not path to SourceMM... */
339 if (!UTIL_PathCmp(mm_path, temp_path))
341 FILE *temp_fp = fopen(temp_path, "rb");
348 /* Search for a matching game dll */
349 List<game_dll_t *>::iterator iter;
352 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
355 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
357 if (UTIL_PathCmp(temp_path, buffer))
370 HINSTANCE gamedll = dlmount(temp_path);
376 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
383 game_dll_t *pInfo = new game_dll_t;
385 pInfo->lib = gamedll;
386 gamedll_list.push_back(pInfo);
394 if (!is_gamedll_loaded)
396 if (strncmp(iface, "ServerGameDLL", 13) == 0)
398 List<game_dll_t *>::iterator iter;
400 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
403 if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
405 if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
407 /* We have a match. Erase us from the list and save our info. */
408 gamedll_list.erase(iter);
409 gamedll_info = *pInfo;
411 is_gamedll_loaded = true;
414 /* :TODO: error otherwise? */
418 if (is_gamedll_loaded)
434 /* wtf do we do... */
435 /* :TODO: .. something a bit more intelligent? */
436 provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
441 /* If we got here, there's definitely a GameDLL */
442 IFACE_MACRO(gamedll_info.factory, GameDLL);
445 void ClearGamedllList()
447 List<game_dll_t *>::iterator iter;
450 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
457 gamedll_list.clear();
460 int LoadPluginsFromFile(const char *_file)
463 int total = 0, skipped=0;
467 fp = fopen(_file, "rt");
470 LogMessage("[META] Could not open plugins file %s\n", _file);
474 char buffer[255], error[255], full_path[255];
475 const char *ptr, *ext, *file;
477 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
479 UTIL_TrimLeft(buffer);
480 UTIL_TrimRight(buffer);
482 length = strlen(buffer);
488 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
494 if (buffer[0] == '"')
517 while (*cptr && isspace(*cptr))
522 UTIL_TrimRight(cptr);
523 if (*cptr && isalpha(*cptr))
525 g_PluginMngr.SetAlias(buffer, cptr);
537 /* First find if it's an absolute path or not... */
538 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
540 /* If we're in an absolute path, ignore our normal heuristics */
541 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
542 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
544 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
560 /* Attempt to find a file extension */
561 ptr = UTIL_GetExtension(file);
562 /* Add an extension if there's none there */
565 #if defined WIN32 || defined _WIN32
575 /* Format the new path */
576 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
577 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
578 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
580 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
599 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
603 LogMessage("[META] Loaded %d plugins from file.", total);
612 char engine_file[PATH_SIZE];
613 char engine_path[PATH_SIZE];
614 char rel_path[PATH_SIZE * 2];
616 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
618 /* Chop off the "engine" file part */
619 len = strlen(engine_file);
620 for (size_t i = len - 1; i >= 0 && i < len; i--)
622 if (engine_file[i] == '/' || engine_file[i] == '\\')
624 engine_file[i] = '\0';
628 abspath(engine_path, engine_file);
630 const char *usepath = metamod_path.c_str();
631 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
636 char command[PATH_SIZE * 2];
637 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
638 provider->ServerCommand(command);
641 /* Wrapper function. This is called when the GameDLL thinks it's using
642 * the engine's real engineFactory.
644 void *EngineFactory(const char *iface, int *ret)
646 IFACE_MACRO(engine_factory, Engine);
649 /* Wrapper function. This is called when the GameDLL thinks it's using
650 * the engine's real physicsFactory.
652 void *PhysicsFactory(const char *iface, int *ret)
654 IFACE_MACRO(physics_factory, Physics);
657 /* Wrapper function. This is called when the GameDLL thinks it's using
658 * the engine's real fileSystemFactory.
660 void *FileSystemFactory(const char *iface, int *ret)
662 IFACE_MACRO(filesystem_factory, FileSystem);
665 void LogMessage(const char *msg, ...)
668 static char buffer[2048];
671 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
674 buffer[len++] = '\n';
677 if (!provider->LogMessage(buffer))
679 fprintf(stdout, "%s", buffer);
683 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
685 engine_factory = engineFactory;
686 filesystem_factory = filesystemFactory;
687 physics_factory = physicsFactory;
688 gpGlobals = pGlobals;
690 provider->Notify_DLLInit_Pre(server, engineFactory, gamedll_info.factory);
692 metamod_version = provider->CreateConVar("metamod_version",
694 "Metamod:Source Version",
695 ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
697 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
698 #if defined WIN32 || defined _WIN32
699 "addons\\metamod\\metaplugins.ini",
701 "addons/metamod/metaplugins.ini",
703 "Metamod:Source Plugins File",
706 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
709 pluginFile = provider->GetConVarString(mm_pluginsfile);
713 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
715 LoadPluginsFromFile(full_path);
717 in_first_level = true;
719 RETURN_META_VALUE(MRES_IGNORED, true);
722 bool Handler_GameInit()
729 if (vsp_load_requested)
736 RETURN_META_VALUE(MRES_IGNORED, true);
739 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
741 g_PluginMngr.SetAllLoaded();
742 RETURN_META_VALUE(MRES_IGNORED, true);
745 void Handler_DLLShutdown()
748 g_PluginMngr.UnloadAll();
750 provider->Notify_DLLShutdown_Pre();
752 SH_CALL(server, &IServerGameDLL::DLLShutdown)();
754 g_SourceHook.CompleteShutdown();
756 if (is_gamedll_loaded && gamedll_info.lib)
758 dlclose(gamedll_info.lib);
759 is_gamedll_loaded = false;
762 RETURN_META(MRES_SUPERCEDE);
765 void Handler_LevelShutdown(void)
770 g_Metamod.PathFormat(full_path,
774 provider->GetConVarString(mm_pluginsfile));
776 LoadPluginsFromFile(full_path);
780 in_first_level = false;
783 ITER_EVENT(OnLevelShutdown, ());
785 RETURN_META(MRES_IGNORED);
788 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
790 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
792 RETURN_META_VALUE(MRES_IGNORED, false);
795 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
801 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
804 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
807 CreateInterfaceFn MetamodSource::engineFactory(bool syn/* =true */)
811 return EngineFactory;
814 return engine_factory;
817 CreateInterfaceFn MetamodSource::physicsFactory(bool syn/* =true */)
821 return PhysicsFactory;
824 return physics_factory;
827 CreateInterfaceFn MetamodSource::fileSystemFactory(bool syn/* =true */)
831 return FileSystemFactory;
834 return filesystem_factory;
837 CreateInterfaceFn MetamodSource::serverFactory(bool syn/* =true */)
841 return CreateInterface;
844 return gamedll_info.factory;
847 CGlobalVars *MetamodSource::pGlobals()
852 void MetamodSource::SetLastMetaReturn(META_RES res)
857 META_RES MetamodSource::GetLastMetaReturn()
859 return last_meta_res;
862 void MetamodSource::ConPrint(const char *str)
864 provider->ConsolePrint(str);
867 void MetamodSource::ConPrintf(const char *fmt, ...)
873 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
876 provider->ConsolePrint(buffer);
879 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
881 major = SM_VERS_API_MAJOR;
882 minor = SM_VERS_API_MINOR;
883 plvers = PLAPI_VERSION;
884 plmin = PLAPI_MIN_VERSION;
887 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
889 shvers = SH_IFACE_VERSION;
890 shimpl = SH_IMPL_VERSION;
893 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
895 int length = (int)strlen(iface);
899 for (i = length - 1; i >= 0; i--)
901 if (!isdigit(iface[i]))
911 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
918 num = atoi(&(iface[++i]));
923 snprintf(&(iface[i]), 4, "%03d", num);
928 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
930 char _if[256]; /* assume no interface goes beyond this */
931 size_t len = strlen(iface);
940 if (len + 4 > sizeof(_if))
953 if ((pf = (fn)(_if, ret)) != NULL)
961 } while ((num = FormatIface(_if, len+1)));
966 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
968 char buffer[256]; /* assume no interface will go beyond this */
969 size_t len = strlen(iface);
970 int ret; /* just in case something doesn't handle NULL properly */
972 if (len > sizeof(buffer) - 4)
977 strcpy(buffer, iface);
981 char *ptr = &buffer[len - 1];
983 while (isdigit(*ptr) && digits <=3)
991 /* for now, assume this is an error */
992 strcpy(buffer, iface);
997 min = (min == 0) ? 1 : min;
998 snprintf(num, sizeof(num), "%03d", min);
1003 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1006 const char *MetamodSource::GetBaseDir()
1008 return mod_path.c_str();
1011 void MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1015 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1018 for (size_t i = 0; i < mylen; i++)
1020 if (buffer[i] == ALT_SEP_CHAR)
1022 buffer[i] = PATH_SEP_CHAR;
1027 void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
1033 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1036 provider->ClientConsolePrint(client, buffer);
1039 void MetamodSource::EnableVSPListener()
1041 if (is_game_init && !vsp_load_requested && !vsp_loaded)
1046 vsp_load_requested = true;
1049 int MetamodSource::GetVSPVersion()
1054 int MetamodSource::GetGameDLLVersion()
1056 return gamedll_version;
1059 bool MetamodSource::RemotePrintingAvailable()
1061 return provider->IsRemotePrintingAvailable();
1064 void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
1076 /* First check ours... we get first chance! */
1077 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
1083 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
1085 else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
1091 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
1094 CPluginManager::CPlugin *pl;
1095 List<IMetamodListener *>::iterator event;
1096 IMetamodListener *api;
1100 for (PluginIter iter = g_PluginMngr._begin();
1101 iter != g_PluginMngr._end();
1105 for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
1108 subret = IFACE_FAILED;
1109 if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
1126 *ret = IFACE_FAILED;
1132 void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
1134 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
1136 pl->m_Events.push_back(pListener);
1139 const char *MetamodSource::GetGameBinaryPath()
1141 return full_bin_path.c_str();
1144 const char *MetamodSource::GetPluginsFile()
1146 return provider->GetConVarString(mm_pluginsfile);
1149 IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
1151 return provider->GetConCommandBaseAccessor();
1154 bool MetamodSource::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1156 if (provider->IsConCommandBaseACommand(pCommand))
1158 g_PluginMngr.AddPluginCmd(plugin, pCommand);
1162 g_PluginMngr.AddPluginCvar(plugin, pCommand);
1165 return provider->RegisterConCommandBase(pCommand);
1168 void MetamodSource::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1170 if (provider->IsConCommandBaseACommand(pCommand))
1172 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1176 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1179 CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1180 UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1183 void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1186 CPluginManager::CPlugin *pPlugin;
1187 List<IMetamodListener *>::iterator event;
1188 IMetamodListener *pML;
1189 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1192 if (pPlugin->m_Status < Pl_Paused)
1196 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1197 if (pPlugin->m_API->GetApiVersion() < 12)
1201 for (event=pPlugin->m_Events.begin();
1202 event!=pPlugin->m_Events.end();
1206 pML->OnUnlinkConCommandBase(id, pCommand);
1210 return provider->UnregisterConCommandBase(pCommand);
1213 int MetamodSource::GetUserMessageCount()
1215 return provider->GetUserMessageCount();
1218 int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1220 return provider->FindUserMessage(name, size);
1223 const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1225 return provider->GetUserMessage(index, size);
1228 int MetamodSource::GetSourceEngineBuild()
1230 return engine_build;
1233 void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks)
1235 ITER_EVENT(OnVSPListening, (callbacks));
1238 IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1242 *pVersion = vsp_version;
1245 return vsp_callbacks;