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;
47 using namespace SourceHook::Impl;
50 * @brief Implementation of main SourceMM GameDLL functionality
54 SH_DECL_MANUALHOOK4(SGD_DLLInit, 0, 0, 0, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
55 SH_DECL_MANUALHOOK0(SGD_GameInit, 0, 0, 0, bool);
56 SH_DECL_MANUALHOOK6(SGD_LevelInit, 0, 0, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
57 SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
58 SH_DECL_MANUALHOOK0_void(SGD_DLLShutdown, 0, 0, 0);
60 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
61 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
62 void Handler_DLLShutdown();
63 void Handler_LevelShutdown();
64 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
65 bool Handler_GameInit();
71 CreateInterfaceFn factory;
77 bool parsed_game_info = false;
79 int gamedll_version = 0;
80 int engine_build = SOURCE_ENGINE_UNKNOWN;
81 List<game_dll_t *> gamedll_list;
82 bool is_gamedll_loaded = false;
83 bool in_first_level = true;
84 bool is_game_init = false;
85 bool vsp_load_attempted = false;
86 bool vsp_load_requested = false;
87 bool vsp_loaded = false;
88 game_dll_t gamedll_info;
89 ConVar *metamod_version = NULL;
90 ConVar *mm_pluginsfile = NULL;
91 IServerGameDLL *server = NULL;
92 CreateInterfaceFn engine_factory = NULL;
93 CreateInterfaceFn physics_factory = NULL;
94 CreateInterfaceFn filesystem_factory = NULL;
95 CGlobalVars *gpGlobals = NULL;
96 CSourceHookImpl g_SourceHook;
97 ISourceHook *g_SHPtr = &g_SourceHook;
98 PluginId g_PLID = Pl_Console;
99 META_RES last_meta_res;
100 IServerPluginCallbacks *vsp_callbacks = NULL;
102 MetamodSource g_Metamod;
104 void ClearGamedllList();
107 #define IFACE_MACRO(orig,nam) \
108 CPluginManager::CPlugin *pl; \
109 SourceHook::List<IMetamodListener *>::iterator event; \
110 IMetamodListener *api; \
113 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
115 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
117 mret = IFACE_FAILED; \
118 if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
119 if (ret) *ret = mret; \
124 return (orig)(iface, ret);
126 #define ITER_EVENT(evn, args) \
127 CPluginManager::CPlugin *pl; \
128 SourceHook::List<IMetamodListener *>::iterator event; \
129 IMetamodListener *api; \
130 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
132 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
138 /* Initialize everything here */
139 void InitMainStates()
141 char full_path[PATH_SIZE] = {0};
142 GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
143 full_bin_path.assign(full_path);
145 /* Like Metamod, reload plugins at the end of the map.
146 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
147 * if the server is shut down (silly, but rare case).
149 in_first_level = true;
151 SourceHook::MemFuncInfo info;
153 if (!provider->GetHookInfo(ProvidedHook_DLLInit, &info))
155 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLInit");
157 SH_MANUALHOOK_RECONFIGURE(SGD_DLLInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
158 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit, false);
159 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit_Post, true);
161 if (!provider->GetHookInfo(ProvidedHook_GameInit, &info))
163 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::GameInit");
165 SH_MANUALHOOK_RECONFIGURE(SGD_GameInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
166 SH_ADD_MANUALHOOK_STATICFUNC(SGD_GameInit, server, Handler_GameInit, false);
168 if (!provider->GetHookInfo(ProvidedHook_LevelInit, &info))
170 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelInit");
172 SH_MANUALHOOK_RECONFIGURE(SGD_LevelInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
173 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelInit, server, Handler_LevelInit, true);
175 if (!provider->GetHookInfo(ProvidedHook_LevelShutdown, &info))
177 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelShutdown");
179 SH_MANUALHOOK_RECONFIGURE(SGD_LevelShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
180 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelShutdown, server, Handler_LevelShutdown, true);
182 if (!provider->GetHookInfo(ProvidedHook_DLLShutdown, &info))
184 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLShutdown");
186 SH_MANUALHOOK_RECONFIGURE(SGD_DLLShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
187 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
190 /* This is where the magic happens */
191 SMM_API void *CreateInterface(const char *iface, int *ret)
193 /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
194 if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
196 provider->DisplayWarning("Do not try loading Metamod:Source as a plugin.\n");
206 if (strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
208 vsp_callbacks = provider->GetVSPCallbacks(iface);
210 if (vsp_callbacks != NULL && vsp_version == 0)
212 vsp_version = atoi(&iface[22]);
217 *ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
220 return vsp_callbacks;
223 if (!parsed_game_info)
225 parsed_game_info = true;
226 const char *game_dir = NULL;
227 char game_path[PATH_SIZE];
228 char mm_path[PATH_SIZE];
230 /* Get path to SourceMM DLL */
231 if (!GetFileOfAddress((void *)CreateInterface, mm_path, sizeof(mm_path)))
233 provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
237 metamod_path.assign(mm_path);
239 /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
240 game_dir = provider->GetCommandLineValue("-game", "hl2");
242 engine_build = provider->DetermineSourceEngine(game_dir);;
244 /* Get absolute path */
245 abspath(game_path, game_dir);
246 mod_path.assign(game_path);
248 char temp_path[PATH_SIZE];
250 /* Path to gameinfo.txt */
251 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
253 FILE *fp = fopen(temp_path, "rt");
257 provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
262 char key[128], val[128];
264 bool gamebin = false;
267 char cur_path[PATH_SIZE];
269 getcwd(cur_path, PATH_SIZE);
271 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
273 UTIL_TrimComments(buffer);
274 UTIL_TrimLeft(buffer);
275 UTIL_TrimRight(buffer);
277 if (stricmp(buffer, "SearchPaths") == 0)
287 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
288 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
290 if (stricmp(key, "Game") == 0)
299 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
301 ptr = &(val[sizeof("|gameinfo_path|") - 1]);
306 lptr = mod_path.c_str();
312 size_t ptr_len = strlen(ptr);
313 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
315 ptr[--ptr_len] = '\0';
318 /* No need to append "bin" if key is GameBin */
321 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
325 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
329 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
332 /* If not path to SourceMM... */
333 if (!UTIL_PathCmp(mm_path, temp_path))
335 FILE *temp_fp = fopen(temp_path, "rb");
342 /* Search for a matching game dll */
343 List<game_dll_t *>::iterator iter;
346 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
349 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
351 if (UTIL_PathCmp(temp_path, buffer))
364 HINSTANCE gamedll = dlmount(temp_path);
370 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
377 game_dll_t *pInfo = new game_dll_t;
379 pInfo->lib = gamedll;
380 gamedll_list.push_back(pInfo);
388 if (!is_gamedll_loaded)
390 if (strncmp(iface, "ServerGameDLL", 13) == 0)
392 List<game_dll_t *>::iterator iter;
394 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
397 if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
399 if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
401 /* We have a match. Erase us from the list and save our info. */
402 gamedll_list.erase(iter);
403 gamedll_info = *pInfo;
405 is_gamedll_loaded = true;
408 /* :TODO: error otherwise? */
412 if (is_gamedll_loaded)
428 /* wtf do we do... */
429 /* :TODO: .. something a bit more intelligent? */
430 provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
435 /* If we got here, there's definitely a GameDLL */
436 IFACE_MACRO(gamedll_info.factory, GameDLL);
439 void ClearGamedllList()
441 List<game_dll_t *>::iterator iter;
444 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
451 gamedll_list.clear();
454 int LoadPluginsFromFile(const char *_file)
457 int total = 0, skipped=0;
461 fp = fopen(_file, "rt");
464 LogMessage("[META] Could not open plugins file %s\n", _file);
468 char buffer[255], error[255], full_path[255];
469 const char *ptr, *ext, *file;
471 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
473 UTIL_TrimLeft(buffer);
474 UTIL_TrimRight(buffer);
476 length = strlen(buffer);
482 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
488 if (buffer[0] == '"')
511 while (*cptr && isspace(*cptr))
516 UTIL_TrimRight(cptr);
517 if (*cptr && isalpha(*cptr))
519 g_PluginMngr.SetAlias(buffer, cptr);
531 /* First find if it's an absolute path or not... */
532 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
534 /* If we're in an absolute path, ignore our normal heuristics */
535 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
536 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
538 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
554 /* Attempt to find a file extension */
555 ptr = UTIL_GetExtension(file);
556 /* Add an extension if there's none there */
559 #if defined WIN32 || defined _WIN32
569 /* Format the new path */
570 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
571 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
572 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
574 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
593 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
597 LogMessage("[META] Loaded %d plugins from file.", total);
606 char engine_file[PATH_SIZE];
607 char engine_path[PATH_SIZE];
608 char rel_path[PATH_SIZE * 2];
610 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
612 /* Chop off the "engine" file part */
613 len = strlen(engine_file);
614 for (size_t i = len - 1; i >= 0 && i < len; i--)
616 if (engine_file[i] == '/' || engine_file[i] == '\\')
618 engine_file[i] = '\0';
622 abspath(engine_path, engine_file);
624 const char *usepath = metamod_path.c_str();
625 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
630 char command[PATH_SIZE * 2];
631 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
632 provider->ServerCommand(command);
635 /* Wrapper function. This is called when the GameDLL thinks it's using
636 * the engine's real engineFactory.
638 void *EngineFactory(const char *iface, int *ret)
640 IFACE_MACRO(engine_factory, Engine);
643 /* Wrapper function. This is called when the GameDLL thinks it's using
644 * the engine's real physicsFactory.
646 void *PhysicsFactory(const char *iface, int *ret)
648 IFACE_MACRO(physics_factory, Physics);
651 /* Wrapper function. This is called when the GameDLL thinks it's using
652 * the engine's real fileSystemFactory.
654 void *FileSystemFactory(const char *iface, int *ret)
656 IFACE_MACRO(filesystem_factory, FileSystem);
659 void LogMessage(const char *msg, ...)
662 static char buffer[2048];
665 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
668 buffer[len++] = '\n';
671 if (!provider->LogMessage(buffer))
673 fprintf(stdout, "%s", buffer);
677 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
679 engine_factory = engineFactory;
680 filesystem_factory = filesystemFactory;
681 physics_factory = physicsFactory;
682 gpGlobals = pGlobals;
684 provider->Notify_DLLInit_Pre(server, engineFactory, gamedll_info.factory);
686 metamod_version = provider->CreateConVar("metamod_version",
688 "Metamod:Source Version",
689 ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
691 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
692 #if defined WIN32 || defined _WIN32
693 "addons\\metamod\\metaplugins.ini",
695 "addons/metamod/metaplugins.ini",
697 "Metamod:Source Plugins File",
700 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
703 pluginFile = provider->GetConVarString(mm_pluginsfile);
704 if (pluginFile == NULL)
706 pluginFile = "addons/metamod/metaplugins.ini";
711 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
713 LoadPluginsFromFile(full_path);
715 in_first_level = true;
717 RETURN_META_VALUE(MRES_IGNORED, true);
720 bool Handler_GameInit()
727 if (vsp_load_requested)
734 RETURN_META_VALUE(MRES_IGNORED, true);
737 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
739 g_PluginMngr.SetAllLoaded();
740 RETURN_META_VALUE(MRES_IGNORED, true);
743 void Handler_DLLShutdown()
746 g_PluginMngr.UnloadAll();
748 provider->Notify_DLLShutdown_Pre();
750 SH_CALL(server, &IServerGameDLL::DLLShutdown)();
752 g_SourceHook.CompleteShutdown();
754 if (is_gamedll_loaded && gamedll_info.lib)
756 dlclose(gamedll_info.lib);
757 is_gamedll_loaded = false;
760 RETURN_META(MRES_SUPERCEDE);
763 void Handler_LevelShutdown(void)
768 g_Metamod.PathFormat(full_path,
772 provider->GetConVarString(mm_pluginsfile));
774 LoadPluginsFromFile(full_path);
778 in_first_level = false;
781 ITER_EVENT(OnLevelShutdown, ());
783 RETURN_META(MRES_IGNORED);
786 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
788 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
790 RETURN_META_VALUE(MRES_IGNORED, false);
793 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
799 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
802 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
805 CreateInterfaceFn MetamodSource::GetEngineFactory(bool syn/* =true */)
809 return EngineFactory;
812 return engine_factory;
815 CreateInterfaceFn MetamodSource::GetPhysicsFactory(bool syn/* =true */)
819 return PhysicsFactory;
822 return physics_factory;
825 CreateInterfaceFn MetamodSource::GetFileSystemFactory(bool syn/* =true */)
829 return FileSystemFactory;
832 return filesystem_factory;
835 CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
839 return CreateInterface;
842 return gamedll_info.factory;
845 CGlobalVars *MetamodSource::GetCGlobals()
850 void MetamodSource::SetLastMetaReturn(META_RES res)
855 META_RES MetamodSource::GetLastMetaReturn()
857 return last_meta_res;
860 void MetamodSource::ConPrint(const char *str)
862 provider->ConsolePrint(str);
865 void MetamodSource::ConPrintf(const char *fmt, ...)
871 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
874 provider->ConsolePrint(buffer);
877 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
879 major = METAMOD_API_MAJOR;
880 minor = METAMOD_API_MINOR;
881 plvers = METAMOD_PLAPI_VERSION;
882 plmin = PLAPI_MIN_VERSION;
885 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
887 shvers = SH_IFACE_VERSION;
888 shimpl = SH_IMPL_VERSION;
891 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
893 int length = (int)strlen(iface);
897 for (i = length - 1; i >= 0; i--)
899 if (!isdigit(iface[i]))
909 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
916 num = atoi(&(iface[++i]));
921 snprintf(&(iface[i]), 4, "%03d", num);
926 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
928 char _if[256]; /* assume no interface goes beyond this */
929 size_t len = strlen(iface);
938 if (len + 4 > sizeof(_if))
951 if ((pf = (fn)(_if, ret)) != NULL)
959 } while ((num = FormatIface(_if, len+1)));
964 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
966 char buffer[256]; /* assume no interface will go beyond this */
967 size_t len = strlen(iface);
968 int ret; /* just in case something doesn't handle NULL properly */
970 if (len > sizeof(buffer) - 4)
975 strcpy(buffer, iface);
979 char *ptr = &buffer[len - 1];
981 while (isdigit(*ptr) && digits <=3)
989 /* for now, assume this is an error */
990 strcpy(buffer, iface);
995 min = (min == 0) ? 1 : min;
996 snprintf(num, sizeof(num), "%03d", min);
1001 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1004 const char *MetamodSource::GetBaseDir()
1006 return mod_path.c_str();
1009 size_t MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1013 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1016 for (size_t i = 0; i < mylen; i++)
1018 if (buffer[i] == ALT_SEP_CHAR)
1020 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::RegisterConCommandBase(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::UnregisterConCommandBase(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;
1248 size_t MetamodSource::Format(char *buffer, size_t maxlength, const char *format, ...)
1253 va_start(ap, format);
1254 result = FormatArgs(buffer, maxlength, format, ap);
1260 size_t MetamodSource::FormatArgs(char *buffer, size_t maxlength, const char *format, va_list ap)
1262 return UTIL_FormatArgs(buffer, maxlength, format, ap);