3 * ======================================================
5 * Copyright (C) 2004-2008 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.
29 #include "metamod_oslink.h"
31 #include <interface.h>
33 #include "metamod_provider.h"
34 #include "metamod_plugins.h"
35 #include "metamod_util.h"
36 #include "metamod_console.h"
37 #include "provider/provider_ep2.h"
39 using namespace SourceMM;
40 using namespace SourceHook;
41 using namespace SourceHook::Impl;
44 * @brief Implementation of main SourceMM GameDLL functionality
48 SH_DECL_MANUALHOOK0(SGD_GameInit, 0, 0, 0, bool);
49 SH_DECL_MANUALHOOK6(SGD_LevelInit, 0, 0, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
50 SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
53 Handler_LevelShutdown();
56 Handler_LevelInit(char const *pMapName,
57 char const *pMapEntities,
58 char const *pOldLevel,
59 char const *pLandmarkName,
70 LoadPluginsFromFile(const char *filepath, int &skipped);
73 LoadVDFPluginsFromDir(const char *dir, int &skipped);
77 CreateInterfaceFn factory;
80 static String mod_path;
81 static String metamod_path;
82 static String full_bin_path;
83 static int vsp_version = 0;
84 static int gamedll_version = 0;
85 static int engine_build = SOURCE_ENGINE_UNKNOWN;
86 static List<game_dll_t *> gamedll_list;
87 static bool is_gamedll_loaded = false;
88 static bool in_first_level = true;
89 static bool is_game_init = false;
90 static bool vsp_load_requested = false;
91 static bool vsp_loaded = false;
92 static game_dll_t gamedll_info;
93 static ConVar *metamod_version = NULL;
94 static ConVar *mm_pluginsfile = NULL;
95 static ConVar *mm_basedir = NULL;
96 static CreateInterfaceFn engine_factory = NULL;
97 static CreateInterfaceFn physics_factory = NULL;
98 static CreateInterfaceFn filesystem_factory = NULL;
99 static CGlobalVars *gpGlobals = NULL;
100 static CHookManagerAutoGen g_SH_HookManagerAutoGen(&g_SourceHook);
101 static META_RES last_meta_res;
102 static IServerPluginCallbacks *vsp_callbacks = NULL;
103 static bool were_plugins_loaded = false;
104 static bool g_bIsVspBridged = false;
106 MetamodSource g_Metamod;
107 PluginId g_PLID = Pl_Console;
108 CSourceHookImpl g_SourceHook;
109 ISourceHook *g_SHPtr = &g_SourceHook;
110 SourceMM::ISmmAPI *g_pMetamod = &g_Metamod;
113 #define IFACE_MACRO(orig,nam) \
114 CPluginManager::CPlugin *pl; \
115 SourceHook::List<IMetamodListener *>::iterator event; \
116 IMetamodListener *api; \
119 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
121 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
123 mret = IFACE_FAILED; \
124 if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
125 if (ret) *ret = mret; \
130 return (orig)(iface, ret);
132 #define ITER_EVENT(evn, args) \
133 CPluginManager::CPlugin *pl; \
134 SourceHook::List<IMetamodListener *>::iterator event; \
135 IMetamodListener *api; \
136 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
138 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
144 /* Initialize everything here */
146 mm_InitializeForLoad()
148 char full_path[PATH_SIZE] = {0};
149 GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
150 full_bin_path.assign(full_path);
152 /* Like Metamod, reload plugins at the end of the map.
153 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
154 * if the server is shut down (silly, but rare case).
156 in_first_level = true;
158 SourceHook::MemFuncInfo info;
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);
183 mm_DetectGameInformation()
185 char mm_path[PATH_SIZE];
186 char game_path[PATH_SIZE];
188 /* Get path to SourceMM DLL */
189 if (!GetFileOfAddress((void *)mm_InitializeForLoad, mm_path, sizeof(mm_path)))
194 metamod_path.assign(mm_path);
196 /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
197 const char *game_dir = provider->GetCommandLineValue("-game");
201 /* Get absolute path */
202 abspath(game_path, game_dir);
206 /* Get absolute path for current directory */
207 abspath(game_path, ".");
210 mod_path.assign(game_path);
212 engine_build = provider->DetermineSourceEngine(game_dir);
218 ServerFactory(const char *iface, int *ret)
220 IFACE_MACRO(gamedll_info.factory, GameDLL);
224 CreateInterface(const char *iface, int *ret)
228 if (!mm_IsVspBridged() && strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
230 if (vsp_callbacks != NULL)
237 vsp_version = atoi(&iface[22]);
238 ptr = provider->GetVSPCallbacks(vsp_version);
245 *ret = (ptr != NULL) ? IFACE_OK : IFACE_FAILED;
251 LoadPluginsFromFile(const char *filepath, int &skipped)
260 fp = fopen(filepath, "rt");
266 char buffer[255], error[255], full_path[PATH_SIZE];
267 const char *ptr, *ext, *file;
269 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
271 UTIL_TrimLeft(buffer);
272 UTIL_TrimRight(buffer);
274 length = strlen(buffer);
280 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
286 if (buffer[0] == '"')
309 while (*cptr && isspace(*cptr))
314 UTIL_TrimRight(cptr);
315 if (*cptr && isalpha(*cptr))
317 g_PluginMngr.SetAlias(buffer, cptr);
329 /* First find if it's an absolute path or not... */
330 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
332 /* If we're in an absolute path, ignore our normal heuristics */
333 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
334 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
336 mm_LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
348 /* Attempt to find a file extension */
349 ptr = UTIL_GetExtension(file);
350 /* Add an extension if there's none there */
353 #if defined WIN32 || defined _WIN32
363 /* Format the new path */
364 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
365 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
366 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
368 mm_LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
390 char engine_file[PATH_SIZE];
391 char engine_path[PATH_SIZE];
392 char rel_path[PATH_SIZE * 2];
394 GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
396 /* Chop off the "engine" file part */
397 len = strlen(engine_file);
398 for (size_t i = len - 1; i >= 0 && i < len; i--)
400 if (engine_file[i] == '/' || engine_file[i] == '\\')
402 engine_file[i] = '\0';
406 abspath(engine_path, engine_file);
408 const char *usepath = metamod_path.c_str();
409 if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
414 char command[PATH_SIZE * 2];
415 UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
416 provider->ServerCommand(command);
419 /* Wrapper function. This is called when the GameDLL thinks it's using
420 * the engine's real engineFactory.
423 EngineFactory(const char *iface, int *ret)
425 IFACE_MACRO(engine_factory, Engine);
428 /* Wrapper function. This is called when the GameDLL thinks it's using
429 * the engine's real physicsFactory.
432 PhysicsFactory(const char *iface, int *ret)
434 IFACE_MACRO(physics_factory, Physics);
437 /* Wrapper function. This is called when the GameDLL thinks it's using
438 * the engine's real fileSystemFactory.
441 FileSystemFactory(const char *iface, int *ret)
443 IFACE_MACRO(filesystem_factory, FileSystem);
447 mm_LogMessage(const char *msg, ...)
450 static char buffer[2048];
453 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
456 buffer[len++] = '\n';
459 if (!provider->LogMessage(buffer))
461 fprintf(stdout, "%s", buffer);
466 DoInitialPluginLoads()
468 const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
469 const char *mmBaseDir = provider->GetCommandLineValue("mm_basedir", NULL);
472 pluginFile = provider->GetConVarString(mm_pluginsfile);
473 if (pluginFile == NULL)
475 pluginFile = "addons/metamod/metaplugins.ini";
480 mmBaseDir = provider->GetConVarString(mm_basedir);
481 if (mmBaseDir == NULL)
483 mmBaseDir = "addons/metamod";
487 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
489 g_Metamod.PathFormat(filepath, sizeof(filepath), "%s/%s", mod_path.c_str(), pluginFile);
490 g_Metamod.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", mod_path.c_str(), mmBaseDir);
491 mm_LoadPlugins(filepath, vdfpath);
495 mm_StartupMetamod(bool is_vsp_load)
503 is_vsp_load ? "V" : "");
505 metamod_version = provider->CreateConVar("metamod_version",
507 "Metamod:Source Version",
508 ConVarFlag_Notify|ConVarFlag_SpOnly);
510 provider->SetConVarString(metamod_version, buffer);
512 mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
513 #if defined WIN32 || defined _WIN32
514 "addons\\metamod\\metaplugins.ini",
516 "addons/metamod/metaplugins.ini",
518 "Metamod:Source Plugins File",
521 mm_basedir = provider->CreateConVar("mm_basedir",
522 #if defined __linux__
527 "Metamod:Source Base Folder",
530 g_bIsVspBridged = is_vsp_load;
534 DoInitialPluginLoads();
535 in_first_level = true;
540 mm_InitializeGlobals(CreateInterfaceFn engineFactory,
541 CreateInterfaceFn physicsFactory,
542 CreateInterfaceFn filesystemFactory,
543 CGlobalVars *pGlobals)
545 engine_factory = engineFactory;
546 physics_factory = physicsFactory;
547 filesystem_factory = filesystemFactory;
548 gpGlobals = pGlobals;
549 provider->Notify_DLLInit_Pre(engineFactory, gamedll_info.factory);
558 if (vsp_load_requested)
561 if (g_bIsVspBridged && !were_plugins_loaded)
563 DoInitialPluginLoads();
564 g_PluginMngr.SetAllLoaded();
565 were_plugins_loaded = true;
570 RETURN_META_VALUE(MRES_IGNORED, true);
577 g_PluginMngr.UnloadAll();
579 provider->Notify_DLLShutdown_Pre();
581 g_SourceHook.CompleteShutdown();
585 Handler_LevelShutdown(void)
587 if (g_bIsVspBridged && !were_plugins_loaded)
589 DoInitialPluginLoads();
590 g_PluginMngr.SetAllLoaded();
591 were_plugins_loaded = true;
592 in_first_level = true;
597 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
599 g_Metamod.PathFormat(filepath,
603 provider->GetConVarString(mm_pluginsfile));
604 g_Metamod.PathFormat(vdfpath,
608 provider->GetConVarString(mm_basedir));
609 mm_LoadPlugins(filepath, vdfpath);
613 in_first_level = false;
616 ITER_EVENT(OnLevelShutdown, ());
618 RETURN_META(MRES_IGNORED);
622 Handler_LevelInit(char const *pMapName,
623 char const *pMapEntities,
624 char const *pOldLevel,
625 char const *pLandmarkName,
629 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
631 RETURN_META_VALUE(MRES_IGNORED, false);
634 void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
640 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
643 mm_LogMessage("[%s] %s", pl->GetLogTag(), buffer);
646 CreateInterfaceFn MetamodSource::GetEngineFactory(bool syn/* =true */)
649 return EngineFactory;
650 return engine_factory;
653 CreateInterfaceFn MetamodSource::GetPhysicsFactory(bool syn/* =true */)
656 return PhysicsFactory;
657 return physics_factory;
660 CreateInterfaceFn MetamodSource::GetFileSystemFactory(bool syn/* =true */)
663 return FileSystemFactory;
664 return filesystem_factory;
667 CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
670 return ServerFactory;
671 return gamedll_info.factory;
674 CGlobalVars *MetamodSource::GetCGlobals()
679 void MetamodSource::SetLastMetaReturn(META_RES res)
684 META_RES MetamodSource::GetLastMetaReturn()
686 return last_meta_res;
689 void MetamodSource::ConPrint(const char *str)
691 provider->ConsolePrint(str);
694 void MetamodSource::ConPrintf(const char *fmt, ...)
700 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
703 provider->ConsolePrint(buffer);
706 void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
708 major = METAMOD_API_MAJOR;
709 minor = METAMOD_API_MINOR;
710 plvers = METAMOD_PLAPI_VERSION;
711 plmin = PLAPI_MIN_VERSION;
714 void MetamodSource::GetShVersions(int &shvers, int &shimpl)
716 shvers = SH_IFACE_VERSION;
717 shimpl = SH_IMPL_VERSION;
720 int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
722 int length = (int)strlen(iface);
726 for (i = length - 1; i >= 0; i--)
728 if (!isdigit(iface[i]))
738 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
745 num = atoi(&(iface[++i]));
750 snprintf(&(iface[i]), 4, "%03d", num);
755 void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
757 char _if[256]; /* assume no interface goes beyond this */
758 size_t len = strlen(iface);
767 if (len + 4 > sizeof(_if))
780 if ((pf = (fn)(_if, ret)) != NULL)
788 } while ((num = FormatIface(_if, len+1)));
793 void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
795 char buffer[256]; /* assume no interface will go beyond this */
796 size_t len = strlen(iface);
797 int ret; /* just in case something doesn't handle NULL properly */
799 if (len > sizeof(buffer) - 4)
804 strcpy(buffer, iface);
808 char *ptr = &buffer[len - 1];
810 while (isdigit(*ptr) && digits <=3)
818 /* for now, assume this is an error */
819 strcpy(buffer, iface);
824 min = (min == 0) ? 1 : min;
825 snprintf(num, sizeof(num), "%03d", min);
830 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
833 const char *MetamodSource::GetBaseDir()
835 return mod_path.c_str();
838 size_t MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
842 size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
845 for (size_t i = 0; i < mylen; i++)
847 if (buffer[i] == ALT_SEP_CHAR)
849 buffer[i] = PATH_SEP_CHAR;
856 void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
862 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
865 provider->ClientConsolePrint(client, buffer);
868 void MetamodSource::EnableVSPListener()
870 if (is_game_init && !vsp_load_requested && !vsp_loaded)
873 vsp_load_requested = true;
876 int MetamodSource::GetVSPVersion()
881 int MetamodSource::GetGameDLLVersion()
883 return gamedll_version;
886 bool MetamodSource::RemotePrintingAvailable()
888 return provider->IsRemotePrintingAvailable();
891 void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
903 /* First check ours... we get first chance! */
904 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
910 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
912 else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
918 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
920 else if (strcmp(iface, MMIFACE_SH_HOOKMANAUTOGEN) == 0)
926 return static_cast<void *>(static_cast<SourceHook::IHookManagerAutoGen *>(&g_SH_HookManagerAutoGen));
929 CPluginManager::CPlugin *pl;
930 List<IMetamodListener *>::iterator event;
931 IMetamodListener *api;
935 for (PluginIter iter = g_PluginMngr._begin();
936 iter != g_PluginMngr._end();
940 for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
943 subret = IFACE_FAILED;
944 if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
967 void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
969 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
971 pl->m_Events.push_back(pListener);
974 const char *MetamodSource::GetGameBinaryPath()
976 return full_bin_path.c_str();
979 const char *MetamodSource::GetPluginsFile()
981 return provider->GetConVarString(mm_pluginsfile);
984 const char *MetamodSource::GetVDFDir()
986 return provider->GetConVarString(mm_basedir);
989 IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
991 return provider->GetConCommandBaseAccessor();
994 bool MetamodSource::RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
996 if (provider->IsConCommandBaseACommand(pCommand))
998 g_PluginMngr.AddPluginCmd(plugin, pCommand);
1002 g_PluginMngr.AddPluginCvar(plugin, pCommand);
1005 return provider->RegisterConCommandBase(pCommand);
1008 void MetamodSource::UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1010 if (provider->IsConCommandBaseACommand(pCommand))
1012 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1016 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1019 CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1020 UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1023 void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1026 CPluginManager::CPlugin *pPlugin;
1027 List<IMetamodListener *>::iterator event;
1028 IMetamodListener *pML;
1029 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1032 if (pPlugin->m_Status < Pl_Paused)
1036 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1037 if (pPlugin->m_API->GetApiVersion() < 12)
1041 for (event=pPlugin->m_Events.begin();
1042 event!=pPlugin->m_Events.end();
1046 pML->OnUnlinkConCommandBase(id, pCommand);
1050 return provider->UnregisterConCommandBase(pCommand);
1053 int MetamodSource::GetUserMessageCount()
1055 return provider->GetUserMessageCount();
1058 int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1060 return provider->FindUserMessage(name, size);
1063 const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1065 return provider->GetUserMessage(index, size);
1068 int MetamodSource::GetSourceEngineBuild()
1070 return engine_build;
1073 void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks, int version)
1076 vsp_version = version;
1078 vsp_callbacks = callbacks;
1079 ITER_EVENT(OnVSPListening, (callbacks));
1082 IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1086 *pVersion = vsp_version;
1089 return vsp_callbacks;
1092 size_t MetamodSource::Format(char *buffer, size_t maxlength, const char *format, ...)
1097 va_start(ap, format);
1098 result = FormatArgs(buffer, maxlength, format, ap);
1104 size_t MetamodSource::FormatArgs(char *buffer, size_t maxlength, const char *format, va_list ap)
1106 return UTIL_FormatArgs(buffer, maxlength, format, ap);
1109 bool MetamodSource::IsLoadedAsGameDLL()
1111 return is_gamedll_loaded;
1114 void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version, bool loaded)
1116 gamedll_info.factory = serverFactory;
1117 gamedll_version = version;
1118 is_gamedll_loaded = loaded;
1122 ProcessVDF(const char *path, bool &skipped)
1126 char alias[24], file[255], full_path[255], error[255];
1128 if (!provider->ProcessVDF(path, file, sizeof(file), alias, sizeof(alias)))
1134 if (alias[0] != '\0')
1135 g_PluginMngr.SetAlias(alias, file);
1137 /* Attempt to find a file extension */
1138 if (UTIL_GetExtension(file) == NULL)
1140 g_pMetamod->PathFormat(full_path,
1143 g_pMetamod->GetBaseDir(),
1145 #if defined WIN32 || defined _WIN32
1154 g_pMetamod->PathFormat(full_path,
1157 g_pMetamod->GetBaseDir(),
1161 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
1163 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1165 mm_LogMessage("[META] Failed to load plugin %s: %s", file, error);
1173 LoadVDFPluginsFromDir(const char *dir, int &skipped)
1177 char path[MAX_PATH];
1178 char relpath[MAX_PATH * 2];
1182 #if defined _MSC_VER
1187 g_Metamod.PathFormat(path, sizeof(path), "%s\\*.vdf", dir);
1188 if ((hFind = FindFirstFile(path, &fd)) == INVALID_HANDLE_VALUE)
1190 DWORD dw = GetLastError();
1191 if (dw == ERROR_FILE_NOT_FOUND)
1194 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
1197 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1201 mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, error);
1207 g_Metamod.PathFormat(path, sizeof(path), "%s\\%s", dir, fd.cFileName);
1208 UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
1209 success = ProcessVDF(relpath, skip);
1214 } while (FindNextFile(hFind, &fd));
1219 struct dirent *pEnt;
1222 if ((pDir = opendir(dir)) == NULL)
1224 mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, strerror(errno));
1228 while ((pEnt = readdir(pDir)) != NULL)
1230 if (strcmp(pEnt->d_name, ".") == 0
1231 || strcmp(pEnt->d_name, "..") == 0)
1235 extidx = strlen(pEnt->d_name) - 4;
1236 if (extidx < 0 || stricmp(&pEnt->d_name[extidx], ".vdf"))
1240 g_Metamod.PathFormat(path, sizeof(path), "%s/%s", dir, pEnt->d_name);
1241 UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
1242 success = ProcessVDF(relpath, skip);
1256 mm_LoadPlugins(const char *filepath, const char *vdfpath)
1258 int total, skipped, fskipped, vskipped;
1261 total = LoadPluginsFromFile(filepath, fskipped);
1262 total += LoadVDFPluginsFromDir(vdfpath, vskipped);
1263 skipped = fskipped + vskipped;
1265 if (total == 0 || total > 1)
1269 mm_LogMessage("[META] Loaded %d plugin%s (%d already loaded)", total, s, skipped);
1271 mm_LogMessage("[META] Loaded %d plugin%s.", total, s);
1279 return g_bIsVspBridged;
1283 mm_IsVspLoadComplete()
1285 return were_plugins_loaded;