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 engine_build = SOURCE_ENGINE_ORANGEBOX;
243 /* Get absolute path */
244 abspath(game_path, game_dir);
245 mod_path.assign(game_path);
247 char temp_path[PATH_SIZE];
249 /* Path to gameinfo.txt */
250 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
252 FILE *fp = fopen(temp_path, "rt");
256 provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
261 char key[128], val[128];
263 bool gamebin = false;
266 char cur_path[PATH_SIZE];
268 getcwd(cur_path, PATH_SIZE);
270 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
272 UTIL_TrimComments(buffer);
273 UTIL_TrimLeft(buffer);
274 UTIL_TrimRight(buffer);
276 if (stricmp(buffer, "SearchPaths") == 0)
286 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
287 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
289 if (stricmp(key, "Game") == 0)
298 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
300 ptr = &(val[sizeof("|gameinfo_path|") - 1]);
305 lptr = mod_path.c_str();
311 size_t ptr_len = strlen(ptr);
312 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
314 ptr[--ptr_len] = '\0';
317 /* No need to append "bin" if key is GameBin */
320 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
324 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
328 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
331 /* If not path to SourceMM... */
332 if (!UTIL_PathCmp(mm_path, temp_path))
334 FILE *temp_fp = fopen(temp_path, "rb");
341 /* Search for a matching game dll */
342 List<game_dll_t *>::iterator iter;
345 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
348 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
350 if (UTIL_PathCmp(temp_path, buffer))
363 HINSTANCE gamedll = dlmount(temp_path);
369 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
376 game_dll_t *pInfo = new game_dll_t;
378 pInfo->lib = gamedll;
379 gamedll_list.push_back(pInfo);
387 if (!is_gamedll_loaded)
389 if (strncmp(iface, "ServerGameDLL", 13) == 0)
391 List<game_dll_t *>::iterator iter;
393 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
396 if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
398 if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
400 /* We have a match. Erase us from the list and save our info. */
401 gamedll_list.erase(iter);
402 gamedll_info = *pInfo;
404 is_gamedll_loaded = true;
407 /* :TODO: error otherwise? */
411 if (is_gamedll_loaded)
427 /* wtf do we do... */
428 /* :TODO: .. something a bit more intelligent? */
429 provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
434 /* If we got here, there's definitely a GameDLL */
435 IFACE_MACRO(gamedll_info.factory, GameDLL);
438 void ClearGamedllList()
440 List<game_dll_t *>::iterator iter;
443 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
450 gamedll_list.clear();
453 int LoadPluginsFromFile(const char *_file)
456 int total = 0, skipped=0;
460 fp = fopen(_file, "rt");
463 LogMessage("[META] Could not open plugins file %s\n", _file);
467 char buffer[255], error[255], full_path[255];
468 const char *ptr, *ext, *file;
470 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
472 UTIL_TrimLeft(buffer);
473 UTIL_TrimRight(buffer);
475 length = strlen(buffer);
481 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
487 if (buffer[0] == '"')
510 while (*cptr && isspace(*cptr))
515 UTIL_TrimRight(cptr);
516 if (*cptr && isalpha(*cptr))
518 g_PluginMngr.SetAlias(buffer, cptr);
530 /* First find if it's an absolute path or not... */
531 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
533 /* If we're in an absolute path, ignore our normal heuristics */
534 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
535 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
537 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
553 /* Attempt to find a file extension */
554 ptr = UTIL_GetExtension(file);
555 /* Add an extension if there's none there */
558 #if defined WIN32 || defined _WIN32
568 /* Format the new path */
569 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
570 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
571 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
573 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
592 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
596 LogMessage("[META] Loaded %d plugins from file.", total);
605 char engine_file[PATH_SIZE];
606 char engine_path[PATH_SIZE];
607 char rel_path[PATH_SIZE * 2];
609 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
611 /* Chop off the "engine" file part */
612 len = strlen(engine_file);
613 for (size_t i = len - 1; i >= 0 && i < len; i--)
615 if (engine_file[i] == '/' || engine_file[i] == '\\')
617 engine_file[i] = '\0';
621 abspath(engine_path, engine_file);
623 const char *usepath = metamod_path.c_str();
624 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
629 char command[PATH_SIZE * 2];
630 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
631 provider->ServerCommand(command);
634 /* Wrapper function. This is called when the GameDLL thinks it's using
635 * the engine's real engineFactory.
637 void *EngineFactory(const char *iface, int *ret)
639 IFACE_MACRO(engine_factory, Engine);
642 /* Wrapper function. This is called when the GameDLL thinks it's using
643 * the engine's real physicsFactory.
645 void *PhysicsFactory(const char *iface, int *ret)
647 IFACE_MACRO(physics_factory, Physics);
650 /* Wrapper function. This is called when the GameDLL thinks it's using
651 * the engine's real fileSystemFactory.
653 void *FileSystemFactory(const char *iface, int *ret)
655 IFACE_MACRO(filesystem_factory, FileSystem);
658 void LogMessage(const char *msg, ...)
661 static char buffer[2048];
664 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
667 buffer[len++] = '\n';
670 if (!provider->LogMessage(buffer))
672 fprintf(stdout, "%s", buffer);
676 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
678 engine_factory = engineFactory;
679 filesystem_factory = filesystemFactory;
680 physics_factory = physicsFactory;
681 gpGlobals = pGlobals;
683 provider->Notify_DLLInit_Pre(server, engineFactory, gamedll_info.factory);
685 metamod_version = provider->CreateConVar("metamod_version",
687 "Metamod:Source Version",
688 ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
690 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
691 #if defined WIN32 || defined _WIN32
692 "addons\\metamod\\metaplugins.ini",
694 "addons/metamod/metaplugins.ini",
696 "Metamod:Source Plugins File",
699 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
702 pluginFile = provider->GetConVarString(mm_pluginsfile);
703 if (pluginFile == NULL)
705 pluginFile = "addons/metamod/metaplugins.ini";
710 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
712 LoadPluginsFromFile(full_path);
714 in_first_level = true;
716 RETURN_META_VALUE(MRES_IGNORED, true);
719 bool Handler_GameInit()
726 if (vsp_load_requested)
733 RETURN_META_VALUE(MRES_IGNORED, true);
736 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
738 g_PluginMngr.SetAllLoaded();
739 RETURN_META_VALUE(MRES_IGNORED, true);
742 void Handler_DLLShutdown()
745 g_PluginMngr.UnloadAll();
747 provider->Notify_DLLShutdown_Pre();
749 SH_CALL(server, &IServerGameDLL::DLLShutdown)();
751 g_SourceHook.CompleteShutdown();
753 if (is_gamedll_loaded && gamedll_info.lib)
755 dlclose(gamedll_info.lib);
756 is_gamedll_loaded = false;
759 RETURN_META(MRES_SUPERCEDE);
762 void Handler_LevelShutdown(void)
767 g_Metamod.PathFormat(full_path,
771 provider->GetConVarString(mm_pluginsfile));
773 LoadPluginsFromFile(full_path);
777 in_first_level = false;
780 ITER_EVENT(OnLevelShutdown, ());
782 RETURN_META(MRES_IGNORED);
785 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
787 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
789 RETURN_META_VALUE(MRES_IGNORED, false);
792 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
798 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
801 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
804 CreateInterfaceFn MetamodSource::engineFactory(bool syn/* =true */)
808 return EngineFactory;
811 return engine_factory;
814 CreateInterfaceFn MetamodSource::physicsFactory(bool syn/* =true */)
818 return PhysicsFactory;
821 return physics_factory;
824 CreateInterfaceFn MetamodSource::fileSystemFactory(bool syn/* =true */)
828 return FileSystemFactory;
831 return filesystem_factory;
834 CreateInterfaceFn MetamodSource::serverFactory(bool syn/* =true */)
838 return CreateInterface;
841 return gamedll_info.factory;
844 CGlobalVars *MetamodSource::pGlobals()
849 void MetamodSource::SetLastMetaReturn(META_RES res)
854 META_RES MetamodSource::GetLastMetaReturn()
856 return last_meta_res;
859 void MetamodSource::ConPrint(const char *str)
861 provider->ConsolePrint(str);
864 void MetamodSource::ConPrintf(const char *fmt, ...)
870 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
873 provider->ConsolePrint(buffer);
876 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
878 major = SM_VERS_API_MAJOR;
879 minor = SM_VERS_API_MINOR;
880 plvers = PLAPI_VERSION;
881 plmin = PLAPI_MIN_VERSION;
884 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
886 shvers = SH_IFACE_VERSION;
887 shimpl = SH_IMPL_VERSION;
890 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
892 int length = (int)strlen(iface);
896 for (i = length - 1; i >= 0; i--)
898 if (!isdigit(iface[i]))
908 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
915 num = atoi(&(iface[++i]));
920 snprintf(&(iface[i]), 4, "%03d", num);
925 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
927 char _if[256]; /* assume no interface goes beyond this */
928 size_t len = strlen(iface);
937 if (len + 4 > sizeof(_if))
950 if ((pf = (fn)(_if, ret)) != NULL)
958 } while ((num = FormatIface(_if, len+1)));
963 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
965 char buffer[256]; /* assume no interface will go beyond this */
966 size_t len = strlen(iface);
967 int ret; /* just in case something doesn't handle NULL properly */
969 if (len > sizeof(buffer) - 4)
974 strcpy(buffer, iface);
978 char *ptr = &buffer[len - 1];
980 while (isdigit(*ptr) && digits <=3)
988 /* for now, assume this is an error */
989 strcpy(buffer, iface);
994 min = (min == 0) ? 1 : min;
995 snprintf(num, sizeof(num), "%03d", min);
1000 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1003 const char *MetamodSource::GetBaseDir()
1005 return mod_path.c_str();
1008 void MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1012 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1015 for (size_t i = 0; i < mylen; i++)
1017 if (buffer[i] == ALT_SEP_CHAR)
1019 buffer[i] = PATH_SEP_CHAR;
1024 void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
1030 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1033 provider->ClientConsolePrint(client, buffer);
1036 void MetamodSource::EnableVSPListener()
1038 if (is_game_init && !vsp_load_requested && !vsp_loaded)
1043 vsp_load_requested = true;
1046 int MetamodSource::GetVSPVersion()
1051 int MetamodSource::GetGameDLLVersion()
1053 return gamedll_version;
1056 bool MetamodSource::RemotePrintingAvailable()
1058 return provider->IsRemotePrintingAvailable();
1061 void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
1073 /* First check ours... we get first chance! */
1074 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
1080 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
1082 else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
1088 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
1091 CPluginManager::CPlugin *pl;
1092 List<IMetamodListener *>::iterator event;
1093 IMetamodListener *api;
1097 for (PluginIter iter = g_PluginMngr._begin();
1098 iter != g_PluginMngr._end();
1102 for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
1105 subret = IFACE_FAILED;
1106 if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
1123 *ret = IFACE_FAILED;
1129 void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
1131 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
1133 pl->m_Events.push_back(pListener);
1136 const char *MetamodSource::GetGameBinaryPath()
1138 return full_bin_path.c_str();
1141 const char *MetamodSource::GetPluginsFile()
1143 return provider->GetConVarString(mm_pluginsfile);
1146 IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
1148 return provider->GetConCommandBaseAccessor();
1151 bool MetamodSource::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1153 if (provider->IsConCommandBaseACommand(pCommand))
1155 g_PluginMngr.AddPluginCmd(plugin, pCommand);
1159 g_PluginMngr.AddPluginCvar(plugin, pCommand);
1162 return provider->RegisterConCommandBase(pCommand);
1165 void MetamodSource::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1167 if (provider->IsConCommandBaseACommand(pCommand))
1169 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1173 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1176 CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1177 UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1180 void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1183 CPluginManager::CPlugin *pPlugin;
1184 List<IMetamodListener *>::iterator event;
1185 IMetamodListener *pML;
1186 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1189 if (pPlugin->m_Status < Pl_Paused)
1193 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1194 if (pPlugin->m_API->GetApiVersion() < 12)
1198 for (event=pPlugin->m_Events.begin();
1199 event!=pPlugin->m_Events.end();
1203 pML->OnUnlinkConCommandBase(id, pCommand);
1207 return provider->UnregisterConCommandBase(pCommand);
1210 int MetamodSource::GetUserMessageCount()
1212 return provider->GetUserMessageCount();
1215 int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1217 return provider->FindUserMessage(name, size);
1220 const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1222 return provider->GetUserMessage(index, size);
1225 int MetamodSource::GetSourceEngineBuild()
1227 return engine_build;
1230 void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks)
1232 ITER_EVENT(OnVSPListening, (callbacks));
1235 IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1239 *pVersion = vsp_version;
1242 return vsp_callbacks;