added the loading changes from the 1.4.3 branch. they're a bit different here but the theory is the same
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;
258 const char *game_dir = NULL;
260 if (!DetectGameInformation())
262 provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
266 char temp_path[PATH_SIZE];
268 /* Path to gameinfo.txt */
269 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
271 FILE *fp = fopen(temp_path, "rt");
275 provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
280 char key[128], val[128];
282 bool gamebin = false;
285 char cur_path[PATH_SIZE];
287 getcwd(cur_path, PATH_SIZE);
289 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
291 UTIL_TrimComments(buffer);
292 UTIL_TrimLeft(buffer);
293 UTIL_TrimRight(buffer);
295 if (stricmp(buffer, "SearchPaths") == 0)
305 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
306 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
308 if (stricmp(key, "Game") == 0)
317 if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
319 ptr = &(val[sizeof("|gameinfo_path|") - 1]);
324 lptr = mod_path.c_str();
330 size_t ptr_len = strlen(ptr);
331 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
333 ptr[--ptr_len] = '\0';
336 /* No need to append "bin" if key is GameBin */
339 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
343 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
347 g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
350 /* If not path to SourceMM... */
351 if (!UTIL_PathCmp(metamod_path.c_str(), temp_path))
353 FILE *temp_fp = fopen(temp_path, "rb");
360 /* Search for a matching game dll */
361 List<game_dll_t *>::iterator iter;
364 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
367 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
369 if (UTIL_PathCmp(temp_path, buffer))
382 HINSTANCE gamedll = dlmount(temp_path);
388 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
395 game_dll_t *pInfo = new game_dll_t;
397 pInfo->lib = gamedll;
398 gamedll_list.push_back(pInfo);
406 if (!is_gamedll_loaded)
408 if (strncmp(iface, "ServerGameDLL", 13) == 0)
410 List<game_dll_t *>::iterator iter;
412 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
415 if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
417 if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
419 /* We have a match. Erase us from the list and save our info. */
420 gamedll_list.erase(iter);
421 gamedll_info = *pInfo;
423 is_gamedll_loaded = true;
426 /* :TODO: error otherwise? */
430 if (is_gamedll_loaded)
446 /* wtf do we do... */
447 /* :TODO: .. something a bit more intelligent? */
448 provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
453 /* If we got here, there's definitely a GameDLL */
454 IFACE_MACRO(gamedll_info.factory, GameDLL);
457 void ClearGamedllList()
459 List<game_dll_t *>::iterator iter;
462 for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
469 gamedll_list.clear();
472 int LoadPluginsFromFile(const char *_file)
475 int total = 0, skipped=0;
479 fp = fopen(_file, "rt");
482 LogMessage("[META] Could not open plugins file %s\n", _file);
486 char buffer[255], error[255], full_path[255];
487 const char *ptr, *ext, *file;
489 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
491 UTIL_TrimLeft(buffer);
492 UTIL_TrimRight(buffer);
494 length = strlen(buffer);
500 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
506 if (buffer[0] == '"')
529 while (*cptr && isspace(*cptr))
534 UTIL_TrimRight(cptr);
535 if (*cptr && isalpha(*cptr))
537 g_PluginMngr.SetAlias(buffer, cptr);
549 /* First find if it's an absolute path or not... */
550 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
552 /* If we're in an absolute path, ignore our normal heuristics */
553 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
554 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
556 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
572 /* Attempt to find a file extension */
573 ptr = UTIL_GetExtension(file);
574 /* Add an extension if there's none there */
577 #if defined WIN32 || defined _WIN32
587 /* Format the new path */
588 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
589 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
590 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
592 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
611 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
615 LogMessage("[META] Loaded %d plugins from file.", total);
623 if (provider->IsAlternatelyLoaded())
629 char engine_file[PATH_SIZE];
630 char engine_path[PATH_SIZE];
631 char rel_path[PATH_SIZE * 2];
633 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
635 /* Chop off the "engine" file part */
636 len = strlen(engine_file);
637 for (size_t i = len - 1; i >= 0 && i < len; i--)
639 if (engine_file[i] == '/' || engine_file[i] == '\\')
641 engine_file[i] = '\0';
645 abspath(engine_path, engine_file);
647 const char *usepath = metamod_path.c_str();
648 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
653 char command[PATH_SIZE * 2];
654 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
655 provider->ServerCommand(command);
658 /* Wrapper function. This is called when the GameDLL thinks it's using
659 * the engine's real engineFactory.
661 void *EngineFactory(const char *iface, int *ret)
663 IFACE_MACRO(engine_factory, Engine);
666 /* Wrapper function. This is called when the GameDLL thinks it's using
667 * the engine's real physicsFactory.
669 void *PhysicsFactory(const char *iface, int *ret)
671 IFACE_MACRO(physics_factory, Physics);
674 /* Wrapper function. This is called when the GameDLL thinks it's using
675 * the engine's real fileSystemFactory.
677 void *FileSystemFactory(const char *iface, int *ret)
679 IFACE_MACRO(filesystem_factory, FileSystem);
682 void LogMessage(const char *msg, ...)
685 static char buffer[2048];
688 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
691 buffer[len++] = '\n';
694 if (!provider->LogMessage(buffer))
696 fprintf(stdout, "%s", buffer);
700 void DoInitialPluginLoads()
702 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
705 pluginFile = provider->GetConVarString(mm_pluginsfile);
706 if (pluginFile == NULL)
708 pluginFile = "addons/metamod/metaplugins.ini";
713 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
715 LoadPluginsFromFile(full_path);
718 void StartupMetamod(bool bWaitForGameInit)
720 metamod_version = provider->CreateConVar("metamod_version",
722 "Metamod:Source Version",
723 ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
725 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
726 #if defined WIN32 || defined _WIN32
727 "addons\\metamod\\metaplugins.ini",
729 "addons/metamod/metaplugins.ini",
731 "Metamod:Source Plugins File",
734 if (!bWaitForGameInit)
736 DoInitialPluginLoads();
737 in_first_level = true;
741 void InitializeGlobals(CreateInterfaceFn engineFactory,
742 CreateInterfaceFn physicsFactory,
743 CreateInterfaceFn filesystemFactory,
744 CGlobalVars *pGlobals)
746 engine_factory = engineFactory;
747 physics_factory = physicsFactory;
748 filesystem_factory = filesystemFactory;
749 gpGlobals = pGlobals;
750 provider->Notify_DLLInit_Pre(engineFactory, gamedll_info.factory);
753 bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
755 InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
756 StartupMetamod(false);
758 RETURN_META_VALUE(MRES_IGNORED, true);
761 bool Handler_GameInit()
768 if (vsp_load_requested)
773 if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
775 DoInitialPluginLoads();
776 g_PluginMngr.SetAllLoaded();
777 were_plugins_loaded = true;
782 RETURN_META_VALUE(MRES_IGNORED, true);
785 bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
787 g_PluginMngr.SetAllLoaded();
788 RETURN_META_VALUE(MRES_IGNORED, true);
794 g_PluginMngr.UnloadAll();
796 provider->Notify_DLLShutdown_Pre();
798 if (is_gamedll_loaded)
800 SH_CALL(server, &IServerGameDLL::DLLShutdown)();
803 g_SourceHook.CompleteShutdown();
805 if (is_gamedll_loaded && gamedll_info.lib)
807 dlclose(gamedll_info.lib);
808 is_gamedll_loaded = false;
812 void Handler_DLLShutdown()
815 RETURN_META(MRES_SUPERCEDE);
818 void Handler_LevelShutdown(void)
820 if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
822 g_PluginMngr.SetAllLoaded();
823 DoInitialPluginLoads();
824 were_plugins_loaded = true;
825 in_first_level = true;
831 g_Metamod.PathFormat(full_path,
835 provider->GetConVarString(mm_pluginsfile));
837 LoadPluginsFromFile(full_path);
841 in_first_level = false;
844 ITER_EVENT(OnLevelShutdown, ());
846 RETURN_META(MRES_IGNORED);
849 bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
851 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
853 RETURN_META_VALUE(MRES_IGNORED, false);
856 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
862 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
865 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
868 CreateInterfaceFn MetamodSource::GetEngineFactory(bool syn/* =true */)
872 return EngineFactory;
875 return engine_factory;
878 CreateInterfaceFn MetamodSource::GetPhysicsFactory(bool syn/* =true */)
882 return PhysicsFactory;
885 return physics_factory;
888 CreateInterfaceFn MetamodSource::GetFileSystemFactory(bool syn/* =true */)
892 return FileSystemFactory;
895 return filesystem_factory;
898 CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
902 return CreateInterface;
905 return gamedll_info.factory;
908 CGlobalVars *MetamodSource::GetCGlobals()
913 void MetamodSource::SetLastMetaReturn(META_RES res)
918 META_RES MetamodSource::GetLastMetaReturn()
920 return last_meta_res;
923 void MetamodSource::ConPrint(const char *str)
925 provider->ConsolePrint(str);
928 void MetamodSource::ConPrintf(const char *fmt, ...)
934 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
937 provider->ConsolePrint(buffer);
940 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
942 major = METAMOD_API_MAJOR;
943 minor = METAMOD_API_MINOR;
944 plvers = METAMOD_PLAPI_VERSION;
945 plmin = PLAPI_MIN_VERSION;
948 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
950 shvers = SH_IFACE_VERSION;
951 shimpl = SH_IMPL_VERSION;
954 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
956 int length = (int)strlen(iface);
960 for (i = length - 1; i >= 0; i--)
962 if (!isdigit(iface[i]))
972 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
979 num = atoi(&(iface[++i]));
984 snprintf(&(iface[i]), 4, "%03d", num);
989 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
991 char _if[256]; /* assume no interface goes beyond this */
992 size_t len = strlen(iface);
1001 if (len + 4 > sizeof(_if))
1005 *ret = IFACE_FAILED;
1014 if ((pf = (fn)(_if, ret)) != NULL)
1022 } while ((num = FormatIface(_if, len+1)));
1027 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
1029 char buffer[256]; /* assume no interface will go beyond this */
1030 size_t len = strlen(iface);
1031 int ret; /* just in case something doesn't handle NULL properly */
1033 if (len > sizeof(buffer) - 4)
1038 strcpy(buffer, iface);
1042 char *ptr = &buffer[len - 1];
1044 while (isdigit(*ptr) && digits <=3)
1052 /* for now, assume this is an error */
1053 strcpy(buffer, iface);
1058 min = (min == 0) ? 1 : min;
1059 snprintf(num, sizeof(num), "%03d", min);
1060 strcat(buffer, num);
1064 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1067 const char *MetamodSource::GetBaseDir()
1069 return mod_path.c_str();
1072 size_t MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1076 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1079 for (size_t i = 0; i < mylen; i++)
1081 if (buffer[i] == ALT_SEP_CHAR)
1083 buffer[i] = PATH_SEP_CHAR;
1090 void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
1096 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1099 provider->ClientConsolePrint(client, buffer);
1102 void MetamodSource::EnableVSPListener()
1104 if (is_game_init && !vsp_load_requested && !vsp_loaded)
1109 vsp_load_requested = true;
1112 int MetamodSource::GetVSPVersion()
1117 int MetamodSource::GetGameDLLVersion()
1119 return gamedll_version;
1122 bool MetamodSource::RemotePrintingAvailable()
1124 return provider->IsRemotePrintingAvailable();
1127 void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
1139 /* First check ours... we get first chance! */
1140 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
1146 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
1148 else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
1154 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
1156 else if (strcmp(iface, MMIFACE_SH_HOOKMANAUTOGEN) == 0)
1162 return static_cast<void *>(static_cast<SourceHook::IHookManagerAutoGen *>(&g_SH_HookManagerAutoGen));
1165 CPluginManager::CPlugin *pl;
1166 List<IMetamodListener *>::iterator event;
1167 IMetamodListener *api;
1171 for (PluginIter iter = g_PluginMngr._begin();
1172 iter != g_PluginMngr._end();
1176 for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
1179 subret = IFACE_FAILED;
1180 if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
1197 *ret = IFACE_FAILED;
1203 void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
1205 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
1207 pl->m_Events.push_back(pListener);
1210 const char *MetamodSource::GetGameBinaryPath()
1212 return full_bin_path.c_str();
1215 const char *MetamodSource::GetPluginsFile()
1217 return provider->GetConVarString(mm_pluginsfile);
1220 IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
1222 return provider->GetConCommandBaseAccessor();
1225 bool MetamodSource::RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1227 if (provider->IsConCommandBaseACommand(pCommand))
1229 g_PluginMngr.AddPluginCmd(plugin, pCommand);
1233 g_PluginMngr.AddPluginCvar(plugin, pCommand);
1236 return provider->RegisterConCommandBase(pCommand);
1239 void MetamodSource::UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1241 if (provider->IsConCommandBaseACommand(pCommand))
1243 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1247 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1250 CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1251 UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1254 void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1257 CPluginManager::CPlugin *pPlugin;
1258 List<IMetamodListener *>::iterator event;
1259 IMetamodListener *pML;
1260 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1263 if (pPlugin->m_Status < Pl_Paused)
1267 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1268 if (pPlugin->m_API->GetApiVersion() < 12)
1272 for (event=pPlugin->m_Events.begin();
1273 event!=pPlugin->m_Events.end();
1277 pML->OnUnlinkConCommandBase(id, pCommand);
1281 return provider->UnregisterConCommandBase(pCommand);
1284 int MetamodSource::GetUserMessageCount()
1286 return provider->GetUserMessageCount();
1289 int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1291 return provider->FindUserMessage(name, size);
1294 const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1296 return provider->GetUserMessage(index, size);
1299 int MetamodSource::GetSourceEngineBuild()
1301 return engine_build;
1304 void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks)
1306 ITER_EVENT(OnVSPListening, (callbacks));
1309 IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1313 *pVersion = vsp_version;
1316 return vsp_callbacks;
1319 size_t MetamodSource::Format(char *buffer, size_t maxlength, const char *format, ...)
1324 va_start(ap, format);
1325 result = FormatArgs(buffer, maxlength, format, ap);
1331 size_t MetamodSource::FormatArgs(char *buffer, size_t maxlength, const char *format, va_list ap)
1333 return UTIL_FormatArgs(buffer, maxlength, format, ap);
1336 bool MetamodSource::IsLoadedAsGameDLL()
1338 return is_gamedll_loaded;
1341 void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version)
1343 gamedll_info.factory = serverFactory;
1344 gamedll_version = version;
1347 bool MetamodSource::IsAlternateLoadComplete()
1349 return were_plugins_loaded;