2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "ExtensionSys.h"
34 #include "LibrarySys.h"
37 #include "sourcemm_api.h"
38 #include "sm_srvcmds.h"
39 #include "sm_stringutil.h"
40 #include "PlayerManager.h"
42 CExtensionManager g_Extensions;
43 IdentityType_t g_ExtType;
45 void CExtension::Initialize(const char *filename, const char *path)
50 m_bFullyLoaded = false;
51 m_File.assign(filename);
53 char real_name[PLATFORM_MAX_PATH];
54 g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
55 m_RealFile.assign(real_name);
58 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
60 Initialize(filename, path);
64 #if defined METAMOD_PLAPI_VERSION
65 #if SOURCE_ENGINE == SE_LEFT4DEAD
66 #define GAMEFIX "2.l4d"
67 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
68 #define GAMEFIX "2.l4d2"
69 #elif SOURCE_ENGINE == SE_ALIENSWARM
70 #define GAMEFIX "2.swarm"
71 #elif SOURCE_ENGINE == SE_ORANGEBOX
72 #define GAMEFIX "2.ep2"
73 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
74 #define GAMEFIX "2.bgt"
75 #elif SOURCE_ENGINE == SE_EYE
76 #define GAMEFIX "2.eye"
77 #elif SOURCE_ENGINE == SE_CSS
78 #define GAMEFIX "2.css"
79 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
80 #define GAMEFIX "2.ep2v"
81 #elif SOURCE_ENGINE == SE_DARKMESSIAH
82 #define GAMEFIX "2.darkm"
83 #elif SOURCE_ENGINE == SE_PORTAL2
84 #define GAMEFIX "2.portal2"
85 #elif SOURCE_ENGINE == SE_CSGO
86 #define GAMEFIX "2.csgo"
88 #define GAMEFIX "2.ep1"
89 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
90 #else //METAMOD_PLAPI_VERSION
91 #define GAMEFIX "1.ep1"
92 #endif //METAMOD_PLAPI_VERSION
94 CLocalExtension::CLocalExtension(const char *filename)
99 char path[PLATFORM_MAX_PATH];
101 /* Special case for new bintools binary */
102 if (strcmp(filename, "bintools.ext") == 0)
107 /* Zeroth, see if there is an engine specific build in the new place. */
108 g_SourceMod.BuildPath(Path_SM,
111 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
114 if (g_LibSys.IsPathFile(path))
119 /* First see if there is an engine specific build! */
120 g_SourceMod.BuildPath(Path_SM,
123 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
127 /* Try the "normal" version */
128 if (!g_LibSys.IsPathFile(path))
130 g_SourceMod.BuildPath(Path_SM,
133 "extensions/%s." PLATFORM_LIB_EXT,
138 Initialize(filename, path);
141 bool CRemoteExtension::Load(char *error, size_t maxlength)
143 if (!PerformAPICheck(error, maxlength))
149 if (!CExtension::Load(error, maxlength))
158 bool CLocalExtension::Load(char *error, size_t maxlength)
160 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
167 typedef IExtensionInterface *(*GETAPI)();
168 GETAPI pfnGetAPI = NULL;
170 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
172 m_pLib->CloseLibrary();
174 snprintf(error, maxlength, "Unable to find extension entry point");
178 m_pAPI = pfnGetAPI();
180 /* Check pointer and version */
181 if (!PerformAPICheck(error, maxlength))
183 m_pLib->CloseLibrary();
190 if (m_pAPI->IsMetamodExtension())
193 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
195 // Check the plugin didn't refuse load
198 #ifndef METAMOD_PLAPI_VERSION
204 #ifndef METAMOD_PLAPI_VERSION
205 g_pMMPlugins->Query(m_PlId, file, status, source)
207 g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
209 && status < Pl_Paused))
211 m_pLib->CloseLibrary();
218 if (!CExtension::Load(error, maxlength))
220 if (m_pAPI->IsMetamodExtension())
225 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
229 m_pLib->CloseLibrary();
238 void CRemoteExtension::Unload()
242 void CLocalExtension::Unload()
244 if (m_pAPI != NULL && m_PlId)
246 char temp_buffer[255];
247 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
253 m_pLib->CloseLibrary();
258 bool CRemoteExtension::Reload(char *error, size_t maxlength)
260 snprintf(error, maxlength, "Remote extensions do not support reloading");
264 bool CLocalExtension::Reload(char *error, size_t maxlength)
266 if (m_pLib == NULL) // FIXME: just load it instead?
269 m_pAPI->OnExtensionUnload();
272 return Load(error, maxlength);
275 bool CRemoteExtension::IsExternal()
280 bool CLocalExtension::IsExternal()
285 CExtension::~CExtension()
290 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
294 snprintf(error, maxlength, "No IExtensionInterface instance provided");
298 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
300 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
307 bool CExtension::Load(char *error, size_t maxlength)
310 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
317 /* Check if we're past load time */
318 if (!g_SourceMod.IsMapLoading())
320 m_pAPI->OnExtensionsAllLoaded();
327 void CExtension::CreateIdentity()
329 if (m_pIdentToken != NULL)
334 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
337 void CExtension::DestroyIdentity()
339 if (m_pIdentToken == NULL)
344 g_ShareSys.DestroyIdentity(m_pIdentToken);
345 m_pIdentToken = NULL;
348 void CExtension::MarkAllLoaded()
350 assert(m_bFullyLoaded == false);
353 m_bFullyLoaded = true;
354 m_pAPI->OnExtensionsAllLoaded();
358 void CExtension::AddPlugin(CPlugin *pPlugin)
360 /* Unfortunately we have to do this :( */
361 if (m_Dependents.find(pPlugin) != m_Dependents.end())
363 m_Dependents.push_back(pPlugin);
367 void CExtension::SetError(const char *error)
369 m_Error.assign(error);
372 IExtensionInterface *CExtension::GetAPI()
377 const char *CExtension::GetFilename()
379 return m_RealFile.c_str();
382 const char *CExtension::GetPath() const
384 return m_Path.c_str();
387 IdentityToken_t *CExtension::GetIdentity()
389 return m_pIdentToken;
392 bool CRemoteExtension::IsLoaded()
394 return (m_pAPI != NULL);
397 bool CLocalExtension::IsLoaded()
399 return (m_pLib != NULL);
402 void CExtension::AddDependency(const IfaceInfo *pInfo)
404 if (m_Deps.find(*pInfo) == m_Deps.end())
406 m_Deps.push_back(*pInfo);
410 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
412 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
415 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
421 List<IfaceInfo>::iterator iter;
422 for (iter = m_ChildDeps.begin();
423 iter != m_ChildDeps.end();
426 IfaceInfo &other = (*iter);
433 m_ChildDeps.push_back(info);
436 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
438 List<IfaceInfo>::iterator iter = m_Deps.begin();
440 if (iter == m_Deps.end())
447 *pOwner = (*iter).owner;
451 *pInterface = (*iter).iface;
454 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
456 return (ITERATOR *)pIter;
459 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
461 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
462 List<IfaceInfo>::iterator _iter;
464 if (_iter == m_Deps.end())
473 *pOwner = (*_iter).owner;
477 *pInterface = (*_iter).iface;
482 if (_iter == m_Deps.end())
490 void CExtension::FreeDependencyIterator(ITERATOR *iter)
492 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
497 void CExtension::AddInterface(SMInterface *pInterface)
499 m_Interfaces.push_back(pInterface);
502 bool CExtension::IsRunning(char *error, size_t maxlength)
508 snprintf(error, maxlength, "%s", m_Error.c_str());
513 return m_pAPI->QueryRunning(error, maxlength);
516 void CExtension::AddLibrary(const char *library)
518 m_Libraries.push_back(library);
521 /*********************
522 * EXTENSION MANAGER *
523 *********************/
525 void CExtensionManager::OnSourceModAllInitialized()
527 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
528 g_PluginSys.AddPluginsListener(this);
529 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
530 g_ShareSys.AddInterface(NULL, this);
533 void CExtensionManager::OnSourceModShutdown()
535 g_RootMenu.RemoveRootConsoleCommand("exts", this);
536 g_ShareSys.DestroyIdentType(g_ExtType);
539 void CExtensionManager::Shutdown()
541 List<CExtension *>::iterator iter;
543 while ((iter = m_Libs.begin()) != m_Libs.end())
545 UnloadExtension((*iter));
549 void CExtensionManager::TryAutoload()
551 char path[PLATFORM_MAX_PATH];
553 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
555 IDirectory *pDir = g_LibSys.OpenDirectory(path);
563 while (pDir->MoreFiles())
565 if (pDir->IsEntryDirectory())
571 lfile = pDir->GetEntryName();
573 if (len <= 9) /* size of ".autoload" */
579 if (strcmp(&lfile[len - 9], ".autoload") != 0)
585 char file[PLATFORM_MAX_PATH];
586 len = UTIL_Format(file, sizeof(file), "%s", lfile);
587 strcpy(&file[len - 9], ".ext");
589 LoadAutoExtension(file);
595 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
597 /* Remove platform extension if it's there. Compat hack. */
598 const char *ext = g_LibSys.GetFileExtension(path);
599 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
601 char path2[PLATFORM_MAX_PATH];
602 UTIL_Format(path2, sizeof(path2), "%s", path);
603 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
604 return LoadAutoExtension(path2, bErrorOnMissing);
607 IExtension *pAlready;
608 if ((pAlready=FindExtensionByFile(path)) != NULL)
614 CExtension *p = new CLocalExtension(path);
616 /* We put us in the list beforehand so extensions that check for each other
617 * won't recursively load each other.
621 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
623 if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
625 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
634 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
636 List<CExtension *>::iterator iter;
639 /* Chomp off the path */
640 char lookup[PLATFORM_MAX_PATH];
641 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
643 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
646 if (pExt->IsSameFile(lookup))
655 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
657 List<CExtension *>::iterator iter;
659 IExtensionInterface *pAPI;
662 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
665 if (!pExt->IsLoaded())
669 if ((pAPI = pExt->GetAPI()) == NULL)
673 name = pAPI->GetExtensionName();
678 if (strcmp(name, ext) == 0)
687 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
689 /* Remove platform extension if it's there. Compat hack. */
690 const char *ext = g_LibSys.GetFileExtension(file);
691 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
693 char path2[PLATFORM_MAX_PATH];
694 UTIL_Format(path2, sizeof(path2), "%s", file);
695 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
696 return LoadExtension(path2, error, maxlength);
699 IExtension *pAlready;
700 if ((pAlready=FindExtensionByFile(file)) != NULL)
705 CExtension *pExt = new CLocalExtension(file);
707 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
714 /* :TODO: do QueryRunning check if the map is loaded */
716 m_Libs.push_back(pExt);
721 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
723 CExtension *pExt = (CExtension *)pRequester;
724 CExtension *pOwner = (CExtension *)pInfo->owner;
726 pExt->AddDependency(pInfo);
728 IExtensionInterface *pAPI = pExt->GetAPI();
729 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
731 pOwner->AddChildDependent(pExt, pInfo->iface);
735 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
737 CExtension *pExt = (CExtension *)ext;
738 CExtension *pOwner = GetExtensionFromIdent(other);
742 info.iface = (SMInterface *)iface;
745 pExt->AddDependency(&info);
746 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
749 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
751 CExtension *pExt = (CExtension *)pOwner;
753 pExt->AddInterface(pInterface);
756 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
758 CExtension *pExt = (CExtension *)pParent;
760 pExt->AddPlugin(pPlugin);
763 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
765 List<CExtension *>::iterator iter;
767 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
769 (*iter)->DropRefsTo((CPlugin *)plugin);
773 CExtension *CExtensionManager::FindByOrder(unsigned int num)
775 if (num < 1 || num > m_Libs.size())
780 List<CExtension *>::iterator iter = m_Libs.begin();
782 while (iter != m_Libs.end())
794 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
801 CExtension *pExt = (CExtension *)_pExt;
803 if (m_Libs.find(pExt) == m_Libs.end())
808 /* First remove us from internal lists */
809 g_ShareSys.RemoveInterfaces(_pExt);
812 List<CExtension *> UnloadQueue;
814 /* Handle dependencies */
815 if (pExt->IsLoaded())
817 /* Unload any dependent plugins */
818 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
819 while (p_iter != pExt->m_Dependents.end())
821 /* We have to manually unlink ourselves here, since we're no longer being managed */
822 g_PluginSys.UnloadPlugin((*p_iter));
823 p_iter = pExt->m_Dependents.erase(p_iter);
826 List<String>::iterator s_iter;
827 for (s_iter = pExt->m_Libraries.begin();
828 s_iter != pExt->m_Libraries.end();
831 g_PluginSys.OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
834 /* Notify and/or unload all dependencies */
835 List<CExtension *>::iterator c_iter;
837 IExtensionInterface *pAPI;
838 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
841 if ((pAPI=pDep->GetAPI()) == NULL)
849 /* Now, get its dependency list */
850 bool dropped = false;
851 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
852 while (i_iter != pDep->m_Deps.end())
854 if ((*i_iter).owner == _pExt)
856 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
861 UnloadQueue.push_back(pDep);
864 pAPI->NotifyInterfaceDrop((*i_iter).iface);
865 i_iter = pDep->m_Deps.erase(i_iter);
872 /* Flush out any back references to this plugin */
873 i_iter = pDep->m_ChildDeps.begin();
874 while (i_iter != pDep->m_ChildDeps.end())
876 if ((*i_iter).owner == pExt)
878 i_iter = pDep->m_ChildDeps.erase(i_iter);
887 /* Unbind our natives from Core */
888 pExt->DropEverything();
891 IdentityToken_t *pIdentity;
892 if ((pIdentity = pExt->GetIdentity()) != NULL)
894 SMGlobalClass *glob = SMGlobalClass::head;
897 glob->OnSourceModIdentityDropped(pIdentity);
898 glob = glob->m_pGlobalClassNext;
902 /* Tell it to unload */
903 if (pExt->IsLoaded())
905 IExtensionInterface *pAPI = pExt->GetAPI();
906 pAPI->OnExtensionUnload();
912 List<CExtension *>::iterator iter;
913 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
915 /* NOTE: This is safe because the unload function backs out of anything not present */
916 UnloadExtension((*iter));
922 void CExtensionManager::MarkAllLoaded()
924 List<CExtension *>::iterator iter;
927 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
930 if (!pExt->IsLoaded())
934 if (pExt->m_bFullyLoaded)
938 pExt->MarkAllLoaded();
942 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
944 /* This function doesn't really need to do anything now. We make sure the
945 * other extension is loaded, but handling of dependencies is really done
946 * by the interface fetcher.
948 if (required || autoload)
950 LoadAutoExtension(file);
954 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
956 int argcount = command.ArgC();
959 const char *cmd = command.Arg(2);
960 if (strcmp(cmd, "list") == 0)
962 List<CExtension *>::iterator iter;
964 unsigned int num = 1;
965 switch (m_Libs.size())
969 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
974 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
979 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
983 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
986 if (pExt->IsLoaded())
989 if (!pExt->IsRunning(error, sizeof(error)))
991 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
995 IExtensionInterface *pAPI = pExt->GetAPI();
996 const char *name = pAPI->GetExtensionName();
997 const char *version = pAPI->GetExtensionVerString();
998 const char *descr = pAPI->GetExtensionDescription();
999 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
1002 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1007 else if (strcmp(cmd, "load") == 0)
1011 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
1015 const char *filename = command.Arg(3);
1016 char path[PLATFORM_MAX_PATH];
1019 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1020 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1022 if (FindExtensionByFile(path) != NULL)
1024 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1028 if (LoadExtension(path, error, sizeof(error)))
1030 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1033 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1038 else if (strcmp(cmd, "info") == 0)
1042 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1046 const char *sId = command.Arg(3);
1047 unsigned int id = atoi(sId);
1050 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1054 if (m_Libs.size() == 0)
1056 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1060 if (id > m_Libs.size())
1062 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1066 List<CExtension *>::iterator iter = m_Libs.begin();
1067 CExtension *pExt = NULL;
1068 while (iter != m_Libs.end())
1077 /* This should never happen */
1080 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1084 if (!pExt->IsLoaded())
1086 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1087 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1092 if (!pExt->IsRunning(error, sizeof(error)))
1094 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1095 g_RootMenu.ConsolePrint(" Loaded: Yes");
1096 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1100 IExtensionInterface *pAPI = pExt->GetAPI();
1101 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1102 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1103 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1104 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1105 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1107 if (pExt->IsExternal())
1109 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1111 else if (pAPI->IsMetamodExtension())
1113 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1117 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1123 else if (strcmp(cmd, "unload") == 0)
1127 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1131 const char *arg = command.Arg(3);
1132 unsigned int num = atoi(arg);
1133 CExtension *pExt = FindByOrder(num);
1137 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1141 if (argcount > 4 && pExt->unload_code)
1143 const char *unload = command.Arg(4);
1144 if (pExt->unload_code == (unsigned)atoi(unload))
1146 char filename[PLATFORM_MAX_PATH];
1147 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1148 UnloadExtension(pExt);
1149 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1153 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1158 if (!pExt->IsLoaded()
1159 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1161 char filename[PLATFORM_MAX_PATH];
1162 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1163 UnloadExtension(pExt);
1164 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1169 List<CPlugin *> plugins;
1170 if (pExt->m_ChildDeps.size())
1172 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1173 List<CExtension *>::iterator iter;
1175 /* Get list of all extensions */
1176 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1178 List<IfaceInfo>::iterator i_iter;
1180 if (!pOther->IsLoaded() || pOther == pExt)
1184 /* Get their dependencies */
1185 for (i_iter=pOther->m_Deps.begin();
1186 i_iter!=pOther->m_Deps.end();
1189 /* Is this dependency to us? */
1190 if ((*i_iter).owner != pExt)
1194 /* Will our dependent care? */
1195 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1197 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1198 /* Add to plugin unload list */
1199 List<CPlugin *>::iterator p_iter;
1200 for (p_iter=pOther->m_Dependents.begin();
1201 p_iter!=pOther->m_Dependents.end();
1204 if (plugins.find((*p_iter)) == plugins.end())
1206 plugins.push_back((*p_iter));
1213 if (pExt->m_Dependents.size())
1215 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1216 List<CPlugin *>::iterator iter;
1218 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1221 if (plugins.find(pPlugin) == plugins.end())
1223 plugins.push_back(pPlugin);
1226 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1229 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1232 srand(static_cast<int>(time(NULL)));
1233 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1234 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1235 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1240 else if (strcmp(cmd, "reload") == 0)
1244 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1248 const char *arg = command.Arg(3);
1249 unsigned int num = atoi(arg);
1250 CExtension *pExt = FindByOrder(num);
1254 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1258 if (pExt->IsLoaded())
1260 char filename[PLATFORM_MAX_PATH];
1263 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1265 if (pExt->Reload(error, sizeof(error)))
1267 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1271 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1278 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1286 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1287 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1288 g_RootMenu.DrawGenericOption("list", "List extensions");
1289 g_RootMenu.DrawGenericOption("load", "Load an extension");
1290 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1291 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1294 void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
1297 int numExtensions = m_Libs.size();
1298 edict_t *edict = player->GetEdict();
1299 unsigned int id = 0;
1300 unsigned int start = 0;
1304 ClientConsolePrint(edict, "[SM] No extensions found.");
1308 if (args.ArgC() > 2)
1310 start = atoi(args.Arg(2));
1314 SourceHook::List<CExtension *>::iterator iter;
1316 for (iter = m_Libs.begin();
1317 iter != m_Libs.end();
1323 if (!ext->IsRunning(error, sizeof(error)))
1334 if (id - start > 10)
1339 IExtensionInterface *api = ext->GetAPI();
1341 const char *name = api->GetExtensionName();
1342 const char *version = api->GetExtensionVerString();
1343 const char *author = api->GetExtensionAuthor();
1344 const char *description = api->GetExtensionDescription();
1346 size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
1348 if (version != NULL && IS_STR_FILLED(version))
1350 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
1353 if (author != NULL && IS_STR_FILLED(author))
1355 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
1358 if (description != NULL && IS_STR_FILLED(description))
1360 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
1364 ClientConsolePrint(edict, "%s", buffer);
1367 while (iter != m_Libs.end())
1370 if ((*iter)->IsRunning(error, sizeof(error)))
1376 if (iter != m_Libs.end())
1378 ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
1382 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1384 if (ptr->type == g_ExtType)
1386 return (CExtension *)(ptr->ptr);
1392 CExtensionManager::CExtensionManager()
1396 CExtensionManager::~CExtensionManager()
1400 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1402 CExtension *pExt = (CExtension *)pSource;
1403 pExt->AddLibrary(library);
1404 g_PluginSys.OnLibraryAction(library, LibraryAction_Added);
1407 bool CExtensionManager::LibraryExists(const char *library)
1411 for (List<CExtension *>::iterator iter = m_Libs.begin();
1412 iter != m_Libs.end();
1416 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1417 s_iter != pExt->m_Libraries.end();
1420 if ((*s_iter).compare(library) == 0)
1430 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1431 const char *filepath,
1432 const char *filename,
1436 IExtension *pAlready;
1437 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1442 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1444 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1451 m_Libs.push_back(pExt);
1456 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1458 IExtensionInterface *pAPI;
1459 List<CExtension *>::iterator iter;
1461 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1463 if ((pAPI = (*iter)->GetAPI()) == NULL)
1467 if (pAPI->GetExtensionVersion() > 3)
1469 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1474 bool CLocalExtension::IsSameFile(const char *file)
1476 /* Only care about the shortened name. */
1477 return strcmp(file, m_File.c_str()) == 0;
1480 bool CRemoteExtension::IsSameFile(const char *file)
1482 /* :TODO: this could be better, but no one uses this API anyway. */
1483 return strcmp(file, m_Path.c_str()) == 0;