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.
28 #include "metamod_oslink.h"
33 #include <interface.h>
36 #include "metamod_provider.h"
37 #include "metamod_plugins.h"
38 #include "metamod_util.h"
39 #include "metamod_console.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 CHookManagerAutoGen g_SH_HookManagerAutoGen(&g_SourceHook);
98 ISourceHook *g_SHPtr = &g_SourceHook;
99 PluginId g_PLID = Pl_Console;
100 META_RES last_meta_res;
101 IServerPluginCallbacks *vsp_callbacks = NULL;
102 bool were_plugins_loaded = false;
104 MetamodSource g_Metamod;
106 void ClearGamedllList();
109 #define IFACE_MACRO(orig,nam) \
110 CPluginManager::CPlugin *pl; \
111 SourceHook::List<IMetamodListener *>::iterator event; \
112 IMetamodListener *api; \
115 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
117 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
119 mret = IFACE_FAILED; \
120 if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
121 if (ret) *ret = mret; \
126 return (orig)(iface, ret);
128 #define ITER_EVENT(evn, args) \
129 CPluginManager::CPlugin *pl; \
130 SourceHook::List<IMetamodListener *>::iterator event; \
131 IMetamodListener *api; \
132 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
134 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
140 /* Initialize everything here */
141 void InitializeForLoad()
143 char full_path[PATH_SIZE] = {0};
144 GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
145 full_bin_path.assign(full_path);
147 /* Like Metamod, reload plugins at the end of the map.
148 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
149 * if the server is shut down (silly, but rare case).
151 in_first_level = true;
153 SourceHook::MemFuncInfo info;
155 if (!provider->GetHookInfo(ProvidedHook_DLLInit, &info))
157 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLInit");
159 SH_MANUALHOOK_RECONFIGURE(SGD_DLLInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
160 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit, false);
161 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit_Post, true);
163 if (!provider->GetHookInfo(ProvidedHook_GameInit, &info))
165 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::GameInit");
167 SH_MANUALHOOK_RECONFIGURE(SGD_GameInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
168 SH_ADD_MANUALHOOK_STATICFUNC(SGD_GameInit, server, Handler_GameInit, false);
170 if (!provider->GetHookInfo(ProvidedHook_LevelInit, &info))
172 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelInit");
174 SH_MANUALHOOK_RECONFIGURE(SGD_LevelInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
175 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelInit, server, Handler_LevelInit, true);
177 if (!provider->GetHookInfo(ProvidedHook_LevelShutdown, &info))
179 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelShutdown");
181 SH_MANUALHOOK_RECONFIGURE(SGD_LevelShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
182 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelShutdown, server, Handler_LevelShutdown, true);
184 if (!provider->GetHookInfo(ProvidedHook_DLLShutdown, &info))
186 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLShutdown");
188 SH_MANUALHOOK_RECONFIGURE(SGD_DLLShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
189 SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
192 bool DetectGameInformation()
194 char mm_path[PATH_SIZE];
195 char game_path[PATH_SIZE];
197 /* Get path to SourceMM DLL */
198 if (!GetFileOfAddress((void *)InitializeForLoad, mm_path, sizeof(mm_path)))
203 metamod_path.assign(mm_path);
205 /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
206 const char *game_dir = provider->GetCommandLineValue("-game", "hl2");
208 /* Get absolute path */
209 abspath(game_path, game_dir);
210 mod_path.assign(game_path);
212 engine_build = provider->DetermineSourceEngine(game_dir);;
217 /* This is where the magic happens */
218 SMM_API void *CreateInterface(const char *iface, int *ret)
220 /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
221 if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
223 provider->DisplayWarning("Do not try loading Metamod:Source as a plugin.\n");
233 if (strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
235 vsp_callbacks = provider->GetVSPCallbacks(iface);
237 if (vsp_callbacks != NULL && vsp_version == 0)
239 vsp_version = atoi(&iface[22]);
244 *ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
247 return vsp_callbacks;
250 if (provider->IsAlternatelyLoaded())
252 IFACE_MACRO(gamedll_info.factory, GameDLL);
255 if (!parsed_game_info)
257 parsed_game_info = true;
259 if (!DetectGameInformation())
261 provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
265 char temp_path[PATH_SIZE];
267 /* Path to gameinfo.txt */
268 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
270 FILE *fp = fopen(temp_path, "rt");
274 provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
279 char key[128], val[128];
281 bool gamebin = false;
284 char cur_path[PATH_SIZE];
286 getcwd(cur_path, PATH_SIZE);
288 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
290 UTIL_TrimComments(buffer);
291 UTIL_TrimLeft(buffer);
292 UTIL_TrimRight(buffer);
294 if (stricmp(buffer, "SearchPaths") == 0)
304 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
305 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
307 if (stricmp(key, "Game") == 0)
316 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
318 ptr = &(val[sizeof("|gameinfo_path|") - 1]);
323 lptr = mod_path.c_str();
329 size_t ptr_len = strlen(ptr);
330 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
332 ptr[--ptr_len] = '\0';
335 /* No need to append "bin" if key is GameBin */
338 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
342 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
346 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
349 /* If not path to SourceMM... */
350 if (!UTIL_PathCmp(metamod_path.c_str(), temp_path))
352 FILE *temp_fp = fopen(temp_path, "rb");
359 /* Search for a matching game dll */
360 List<game_dll_t *>::iterator iter;
363 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
366 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
368 if (UTIL_PathCmp(temp_path, buffer))
381 HINSTANCE gamedll = dlmount(temp_path);
387 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
394 game_dll_t *pInfo = new game_dll_t;
396 pInfo->lib = gamedll;
397 gamedll_list.push_back(pInfo);
405 if (!is_gamedll_loaded)
407 if (strncmp(iface, "ServerGameDLL", 13) == 0)
409 List<game_dll_t *>::iterator iter;
411 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
414 if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
416 if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
418 /* We have a match. Erase us from the list and save our info. */
419 gamedll_list.erase(iter);
420 gamedll_info = *pInfo;
422 is_gamedll_loaded = true;
425 /* :TODO: error otherwise? */
429 if (is_gamedll_loaded)
445 /* wtf do we do... */
446 /* :TODO: .. something a bit more intelligent? */
447 provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
452 /* If we got here, there's definitely a GameDLL */
453 IFACE_MACRO(gamedll_info.factory, GameDLL);
456 void ClearGamedllList()
458 List<game_dll_t *>::iterator iter;
461 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
468 gamedll_list.clear();
471 int LoadPluginsFromFile(const char *_file)
474 int total = 0, skipped=0;
478 fp = fopen(_file, "rt");
481 LogMessage("[META] Could not open plugins file %s\n", _file);
485 char buffer[255], error[255], full_path[255];
486 const char *ptr, *ext, *file;
488 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
490 UTIL_TrimLeft(buffer);
491 UTIL_TrimRight(buffer);
493 length = strlen(buffer);
499 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
505 if (buffer[0] == '"')
528 while (*cptr && isspace(*cptr))
533 UTIL_TrimRight(cptr);
534 if (*cptr && isalpha(*cptr))
536 g_PluginMngr.SetAlias(buffer, cptr);
548 /* First find if it's an absolute path or not... */
549 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
551 /* If we're in an absolute path, ignore our normal heuristics */
552 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
553 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
555 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
571 /* Attempt to find a file extension */
572 ptr = UTIL_GetExtension(file);
573 /* Add an extension if there's none there */
576 #if defined WIN32 || defined _WIN32
586 /* Format the new path */
587 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
588 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
589 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
591 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
610 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
614 LogMessage("[META] Loaded %d plugins from file.", total);
622 if (provider->IsAlternatelyLoaded())
628 char engine_file[PATH_SIZE];
629 char engine_path[PATH_SIZE];
630 char rel_path[PATH_SIZE * 2];
632 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
634 /* Chop off the "engine" file part */
635 len = strlen(engine_file);
636 for (size_t i = len - 1; i >= 0 && i < len; i--)
638 if (engine_file[i] == '/' || engine_file[i] == '\\')
640 engine_file[i] = '\0';
644 abspath(engine_path, engine_file);
646 const char *usepath = metamod_path.c_str();
647 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
652 char command[PATH_SIZE * 2];
653 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
654 provider->ServerCommand(command);
657 /* Wrapper function. This is called when the GameDLL thinks it's using
658 * the engine's real engineFactory.
660 void *EngineFactory(const char *iface, int *ret)
662 IFACE_MACRO(engine_factory, Engine);
665 /* Wrapper function. This is called when the GameDLL thinks it's using
666 * the engine's real physicsFactory.
668 void *PhysicsFactory(const char *iface, int *ret)
670 IFACE_MACRO(physics_factory, Physics);
673 /* Wrapper function. This is called when the GameDLL thinks it's using
674 * the engine's real fileSystemFactory.
676 void *FileSystemFactory(const char *iface, int *ret)
678 IFACE_MACRO(filesystem_factory, FileSystem);
681 void LogMessage(const char *msg, ...)
684 static char buffer[2048];
687 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
690 buffer[len++] = '\n';
693 if (!provider->LogMessage(buffer))
695 fprintf(stdout, "%s", buffer);
699 void DoInitialPluginLoads()
701 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
704 pluginFile = provider->GetConVarString(mm_pluginsfile);
705 if (pluginFile == NULL)
707 pluginFile = "addons/metamod/metaplugins.ini";
712 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
714 LoadPluginsFromFile(full_path);
717 void StartupMetamod(bool bWaitForGameInit)
719 metamod_version = provider->CreateConVar("metamod_version",
721 "Metamod:Source Version",
722 ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
724 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
725 #if defined WIN32 || defined _WIN32
726 "addons\\metamod\\metaplugins.ini",
728 "addons/metamod/metaplugins.ini",
730 "Metamod:Source Plugins File",
733 if (!bWaitForGameInit)
735 DoInitialPluginLoads();
736 in_first_level = true;
740 void InitializeGlobals(CreateInterfaceFn engineFactory,
741 CreateInterfaceFn physicsFactory,
742 CreateInterfaceFn filesystemFactory,
743 CGlobalVars *pGlobals)
745 engine_factory = engineFactory;
746 physics_factory = physicsFactory;
747 filesystem_factory = filesystemFactory;
748 gpGlobals = pGlobals;
749 provider->Notify_DLLInit_Pre(engineFactory, gamedll_info.factory);
752 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
754 InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
755 StartupMetamod(false);
757 RETURN_META_VALUE(MRES_IGNORED, true);
760 bool Handler_GameInit()
767 if (vsp_load_requested)
772 if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
774 DoInitialPluginLoads();
775 g_PluginMngr.SetAllLoaded();
776 were_plugins_loaded = true;
781 RETURN_META_VALUE(MRES_IGNORED, true);
784 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
786 g_PluginMngr.SetAllLoaded();
787 RETURN_META_VALUE(MRES_IGNORED, true);
793 g_PluginMngr.UnloadAll();
795 provider->Notify_DLLShutdown_Pre();
797 if (is_gamedll_loaded)
799 SH_CALL(server, &IServerGameDLL::DLLShutdown)();
802 g_SourceHook.CompleteShutdown();
804 if (is_gamedll_loaded && gamedll_info.lib)
806 dlclose(gamedll_info.lib);
807 is_gamedll_loaded = false;
811 void Handler_DLLShutdown()
814 RETURN_META(MRES_SUPERCEDE);
817 void Handler_LevelShutdown(void)
819 if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
821 g_PluginMngr.SetAllLoaded();
822 DoInitialPluginLoads();
823 were_plugins_loaded = true;
824 in_first_level = true;
830 g_Metamod.PathFormat(full_path,
834 provider->GetConVarString(mm_pluginsfile));
836 LoadPluginsFromFile(full_path);
840 in_first_level = false;
843 ITER_EVENT(OnLevelShutdown, ());
845 RETURN_META(MRES_IGNORED);
848 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
850 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
852 RETURN_META_VALUE(MRES_IGNORED, false);
855 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
861 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
864 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
867 CreateInterfaceFn MetamodSource::GetEngineFactory(bool syn/* =true */)
871 return EngineFactory;
874 return engine_factory;
877 CreateInterfaceFn MetamodSource::GetPhysicsFactory(bool syn/* =true */)
881 return PhysicsFactory;
884 return physics_factory;
887 CreateInterfaceFn MetamodSource::GetFileSystemFactory(bool syn/* =true */)
891 return FileSystemFactory;
894 return filesystem_factory;
897 CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
901 return CreateInterface;
904 return gamedll_info.factory;
907 CGlobalVars *MetamodSource::GetCGlobals()
912 void MetamodSource::SetLastMetaReturn(META_RES res)
917 META_RES MetamodSource::GetLastMetaReturn()
919 return last_meta_res;
922 void MetamodSource::ConPrint(const char *str)
924 provider->ConsolePrint(str);
927 void MetamodSource::ConPrintf(const char *fmt, ...)
933 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
936 provider->ConsolePrint(buffer);
939 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
941 major = METAMOD_API_MAJOR;
942 minor = METAMOD_API_MINOR;
943 plvers = METAMOD_PLAPI_VERSION;
944 plmin = PLAPI_MIN_VERSION;
947 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
949 shvers = SH_IFACE_VERSION;
950 shimpl = SH_IMPL_VERSION;
953 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
955 int length = (int)strlen(iface);
959 for (i = length - 1; i >= 0; i--)
961 if (!isdigit(iface[i]))
971 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
978 num = atoi(&(iface[++i]));
983 snprintf(&(iface[i]), 4, "%03d", num);
988 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
990 char _if[256]; /* assume no interface goes beyond this */
991 size_t len = strlen(iface);
1000 if (len + 4 > sizeof(_if))
1004 *ret = IFACE_FAILED;
1013 if ((pf = (fn)(_if, ret)) != NULL)
1021 } while ((num = FormatIface(_if, len+1)));
1026 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
1028 char buffer[256]; /* assume no interface will go beyond this */
1029 size_t len = strlen(iface);
1030 int ret; /* just in case something doesn't handle NULL properly */
1032 if (len > sizeof(buffer) - 4)
1037 strcpy(buffer, iface);
1041 char *ptr = &buffer[len - 1];
1043 while (isdigit(*ptr) && digits <=3)
1051 /* for now, assume this is an error */
1052 strcpy(buffer, iface);
1057 min = (min == 0) ? 1 : min;
1058 snprintf(num, sizeof(num), "%03d", min);
1059 strcat(buffer, num);
1063 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1066 const char *MetamodSource::GetBaseDir()
1068 return mod_path.c_str();
1071 size_t MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1075 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1078 for (size_t i = 0; i < mylen; i++)
1080 if (buffer[i] == ALT_SEP_CHAR)
1082 buffer[i] = PATH_SEP_CHAR;
1089 void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
1095 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1098 provider->ClientConsolePrint(client, buffer);
1101 void MetamodSource::EnableVSPListener()
1103 if (is_game_init && !vsp_load_requested && !vsp_loaded)
1108 vsp_load_requested = true;
1111 int MetamodSource::GetVSPVersion()
1116 int MetamodSource::GetGameDLLVersion()
1118 return gamedll_version;
1121 bool MetamodSource::RemotePrintingAvailable()
1123 return provider->IsRemotePrintingAvailable();
1126 void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
1138 /* First check ours... we get first chance! */
1139 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
1145 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
1147 else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
1153 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
1155 else if (strcmp(iface, MMIFACE_SH_HOOKMANAUTOGEN) == 0)
1161 return static_cast<void *>(static_cast<SourceHook::IHookManagerAutoGen *>(&g_SH_HookManagerAutoGen));
1164 CPluginManager::CPlugin *pl;
1165 List<IMetamodListener *>::iterator event;
1166 IMetamodListener *api;
1170 for (PluginIter iter = g_PluginMngr._begin();
1171 iter != g_PluginMngr._end();
1175 for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
1178 subret = IFACE_FAILED;
1179 if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
1196 *ret = IFACE_FAILED;
1202 void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
1204 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
1206 pl->m_Events.push_back(pListener);
1209 const char *MetamodSource::GetGameBinaryPath()
1211 return full_bin_path.c_str();
1214 const char *MetamodSource::GetPluginsFile()
1216 return provider->GetConVarString(mm_pluginsfile);
1219 IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
1221 return provider->GetConCommandBaseAccessor();
1224 bool MetamodSource::RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1226 if (provider->IsConCommandBaseACommand(pCommand))
1228 g_PluginMngr.AddPluginCmd(plugin, pCommand);
1232 g_PluginMngr.AddPluginCvar(plugin, pCommand);
1235 return provider->RegisterConCommandBase(pCommand);
1238 void MetamodSource::UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1240 if (provider->IsConCommandBaseACommand(pCommand))
1242 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1246 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1249 CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1250 UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1253 void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1256 CPluginManager::CPlugin *pPlugin;
1257 List<IMetamodListener *>::iterator event;
1258 IMetamodListener *pML;
1259 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1262 if (pPlugin->m_Status < Pl_Paused)
1266 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1267 if (pPlugin->m_API->GetApiVersion() < 12)
1271 for (event=pPlugin->m_Events.begin();
1272 event!=pPlugin->m_Events.end();
1276 pML->OnUnlinkConCommandBase(id, pCommand);
1280 return provider->UnregisterConCommandBase(pCommand);
1283 int MetamodSource::GetUserMessageCount()
1285 return provider->GetUserMessageCount();
1288 int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1290 return provider->FindUserMessage(name, size);
1293 const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1295 return provider->GetUserMessage(index, size);
1298 int MetamodSource::GetSourceEngineBuild()
1300 return engine_build;
1303 void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks)
1305 ITER_EVENT(OnVSPListening, (callbacks));
1308 IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1312 *pVersion = vsp_version;
1315 return vsp_callbacks;
1318 size_t MetamodSource::Format(char *buffer, size_t maxlength, const char *format, ...)
1323 va_start(ap, format);
1324 result = FormatArgs(buffer, maxlength, format, ap);
1330 size_t MetamodSource::FormatArgs(char *buffer, size_t maxlength, const char *format, va_list ap)
1332 return UTIL_FormatArgs(buffer, maxlength, format, ap);
1335 bool MetamodSource::IsLoadedAsGameDLL()
1337 return is_gamedll_loaded;
1340 void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version)
1342 gamedll_info.factory = serverFactory;
1343 gamedll_version = version;
1346 bool MetamodSource::IsAlternateLoadComplete()
1348 return were_plugins_loaded;