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 <ILibrarySys.h>
35 #include <ISourceMod.h>
36 #include "common_logic.h"
37 #include "PluginSys.h"
39 CExtensionManager g_Extensions;
40 IdentityType_t g_ExtType;
42 void CExtension::Initialize(const char *filename, const char *path)
47 m_bFullyLoaded = false;
48 m_File.assign(filename);
50 char real_name[PLATFORM_MAX_PATH];
51 libsys->GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
52 m_RealFile.assign(real_name);
55 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
57 Initialize(filename, path);
61 CLocalExtension::CLocalExtension(const char *filename)
66 char path[PLATFORM_MAX_PATH];
68 /* Special case for new bintools binary */
69 if (strcmp(filename, "bintools.ext") == 0)
74 /* Zeroth, see if there is an engine specific build in the new place. */
75 g_pSM->BuildPath(Path_SM,
78 "extensions/%s.%s." PLATFORM_LIB_EXT,
82 if (libsys->IsPathFile(path))
87 /* COMPAT HACK: One-halfth, if ep2v, see if there is an engine specific build in the new place with old naming */
88 if (strcmp(smcore.gamesuffix, "2.tf2") == 0
89 || strcmp(smcore.gamesuffix, "2.dods") == 0
90 || strcmp(smcore.gamesuffix, "2.hl2dm") == 0
93 g_pSM->BuildPath(Path_SM,
96 "extensions/%s.2.ep2v." PLATFORM_LIB_EXT,
99 if (libsys->IsPathFile(path))
105 /* First see if there is an engine specific build! */
106 g_pSM->BuildPath(Path_SM,
109 "extensions/auto.%s/%s." PLATFORM_LIB_EXT,
113 /* Try the "normal" version */
114 if (!libsys->IsPathFile(path))
117 g_pSM->BuildPath(Path_SM,
120 "extensions/%s." PLATFORM_LIB_EXT,
125 Initialize(filename, path);
128 bool CRemoteExtension::Load(char *error, size_t maxlength)
130 if (!PerformAPICheck(error, maxlength))
136 if (!CExtension::Load(error, maxlength))
145 bool CLocalExtension::Load(char *error, size_t maxlength)
147 m_pLib = libsys->OpenLibrary(m_Path.c_str(), error, maxlength);
154 typedef IExtensionInterface *(*GETAPI)();
155 GETAPI pfnGetAPI = NULL;
157 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
159 m_pLib->CloseLibrary();
161 snprintf(error, maxlength, "Unable to find extension entry point");
165 m_pAPI = pfnGetAPI();
167 /* Check pointer and version */
168 if (!PerformAPICheck(error, maxlength))
170 m_pLib->CloseLibrary();
177 if (m_pAPI->IsMetamodExtension())
180 m_PlId = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
184 m_pLib->CloseLibrary();
191 if (!CExtension::Load(error, maxlength))
193 if (m_pAPI->IsMetamodExtension())
197 smcore.UnloadMMSPlugin(m_PlId);
201 m_pLib->CloseLibrary();
210 void CRemoteExtension::Unload()
214 void CLocalExtension::Unload()
216 if (m_pAPI != NULL && m_PlId)
218 smcore.UnloadMMSPlugin(m_PlId);
224 m_pLib->CloseLibrary();
229 bool CRemoteExtension::Reload(char *error, size_t maxlength)
231 snprintf(error, maxlength, "Remote extensions do not support reloading");
235 bool CLocalExtension::Reload(char *error, size_t maxlength)
237 if (m_pLib == NULL) // FIXME: just load it instead?
240 m_pAPI->OnExtensionUnload();
243 return Load(error, maxlength);
246 bool CRemoteExtension::IsExternal()
251 bool CLocalExtension::IsExternal()
256 CExtension::~CExtension()
261 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
265 snprintf(error, maxlength, "No IExtensionInterface instance provided");
269 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
271 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
278 bool CExtension::Load(char *error, size_t maxlength)
281 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
288 /* Check if we're past load time */
289 if (!smcore.IsMapLoading())
291 m_pAPI->OnExtensionsAllLoaded();
298 void CExtension::CreateIdentity()
300 if (m_pIdentToken != NULL)
305 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
308 void CExtension::DestroyIdentity()
310 if (m_pIdentToken == NULL)
315 g_ShareSys.DestroyIdentity(m_pIdentToken);
316 m_pIdentToken = NULL;
319 void CExtension::MarkAllLoaded()
321 assert(m_bFullyLoaded == false);
324 m_bFullyLoaded = true;
325 m_pAPI->OnExtensionsAllLoaded();
329 void CExtension::AddPlugin(CPlugin *pPlugin)
331 /* Unfortunately we have to do this :( */
332 if (m_Dependents.find(pPlugin) != m_Dependents.end())
334 m_Dependents.push_back(pPlugin);
338 void CExtension::SetError(const char *error)
340 m_Error.assign(error);
343 IExtensionInterface *CExtension::GetAPI()
348 const char *CExtension::GetFilename()
350 return m_RealFile.c_str();
353 const char *CExtension::GetPath() const
355 return m_Path.c_str();
358 IdentityToken_t *CExtension::GetIdentity()
360 return m_pIdentToken;
363 bool CRemoteExtension::IsLoaded()
365 return (m_pAPI != NULL);
368 bool CLocalExtension::IsLoaded()
370 return (m_pLib != NULL);
373 void CExtension::AddDependency(const IfaceInfo *pInfo)
375 if (m_Deps.find(*pInfo) == m_Deps.end())
377 m_Deps.push_back(*pInfo);
381 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
383 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
386 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
392 List<IfaceInfo>::iterator iter;
393 for (iter = m_ChildDeps.begin();
394 iter != m_ChildDeps.end();
397 IfaceInfo &other = (*iter);
404 m_ChildDeps.push_back(info);
407 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
409 List<IfaceInfo>::iterator iter = m_Deps.begin();
411 if (iter == m_Deps.end())
418 *pOwner = (*iter).owner;
422 *pInterface = (*iter).iface;
425 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
427 return (ITERATOR *)pIter;
430 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
432 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
433 List<IfaceInfo>::iterator _iter;
435 if (_iter == m_Deps.end())
444 *pOwner = (*_iter).owner;
448 *pInterface = (*_iter).iface;
453 if (_iter == m_Deps.end())
461 void CExtension::FreeDependencyIterator(ITERATOR *iter)
463 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
468 void CExtension::AddInterface(SMInterface *pInterface)
470 m_Interfaces.push_back(pInterface);
473 bool CExtension::IsRunning(char *error, size_t maxlength)
479 snprintf(error, maxlength, "%s", m_Error.c_str());
484 return m_pAPI->QueryRunning(error, maxlength);
487 void CExtension::AddLibrary(const char *library)
489 m_Libraries.push_back(library);
492 /*********************
493 * EXTENSION MANAGER *
494 *********************/
496 void CExtensionManager::OnSourceModAllInitialized()
498 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
499 pluginsys->AddPluginsListener(this);
500 rootmenu->AddRootConsoleCommand("exts", "Manage extensions", this);
501 g_ShareSys.AddInterface(NULL, this);
504 void CExtensionManager::OnSourceModShutdown()
506 rootmenu->RemoveRootConsoleCommand("exts", this);
507 pluginsys->RemovePluginsListener(this);
508 g_ShareSys.DestroyIdentType(g_ExtType);
511 void CExtensionManager::Shutdown()
513 List<CExtension *>::iterator iter;
515 while ((iter = m_Libs.begin()) != m_Libs.end())
517 UnloadExtension((*iter));
521 void CExtensionManager::TryAutoload()
523 char path[PLATFORM_MAX_PATH];
525 g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
527 IDirectory *pDir = libsys->OpenDirectory(path);
535 while (pDir->MoreFiles())
537 if (pDir->IsEntryDirectory())
543 lfile = pDir->GetEntryName();
545 if (len <= 9) /* size of ".autoload" */
551 if (strcmp(&lfile[len - 9], ".autoload") != 0)
557 char file[PLATFORM_MAX_PATH];
558 len = smcore.Format(file, sizeof(file), "%s", lfile);
559 strcpy(&file[len - 9], ".ext");
561 LoadAutoExtension(file);
567 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
569 /* Remove platform extension if it's there. Compat hack. */
570 const char *ext = libsys->GetFileExtension(path);
571 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
573 char path2[PLATFORM_MAX_PATH];
574 smcore.Format(path2, sizeof(path2), "%s", path);
575 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
576 return LoadAutoExtension(path2);
579 IExtension *pAlready;
580 if ((pAlready=FindExtensionByFile(path)) != NULL)
586 CExtension *p = new CLocalExtension(path);
588 /* We put us in the list beforehand so extensions that check for each other
589 * won't recursively load each other.
593 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
595 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
602 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
604 List<CExtension *>::iterator iter;
607 /* Chomp off the path */
608 char lookup[PLATFORM_MAX_PATH];
609 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
611 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
614 if (pExt->IsSameFile(lookup))
623 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
625 List<CExtension *>::iterator iter;
627 IExtensionInterface *pAPI;
630 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
633 if (!pExt->IsLoaded())
637 if ((pAPI = pExt->GetAPI()) == NULL)
641 name = pAPI->GetExtensionName();
646 if (strcmp(name, ext) == 0)
655 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
657 /* Remove platform extension if it's there. Compat hack. */
658 const char *ext = libsys->GetFileExtension(file);
659 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
661 char path2[PLATFORM_MAX_PATH];
662 smcore.Format(path2, sizeof(path2), "%s", file);
663 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
664 return LoadExtension(path2, error, maxlength);
667 IExtension *pAlready;
668 if ((pAlready=FindExtensionByFile(file)) != NULL)
673 CExtension *pExt = new CLocalExtension(file);
675 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
682 /* :TODO: do QueryRunning check if the map is loaded */
684 m_Libs.push_back(pExt);
689 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
691 CExtension *pExt = (CExtension *)pRequester;
692 CExtension *pOwner = (CExtension *)pInfo->owner;
694 pExt->AddDependency(pInfo);
696 IExtensionInterface *pAPI = pExt->GetAPI();
697 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
699 pOwner->AddChildDependent(pExt, pInfo->iface);
703 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
705 CExtension *pExt = (CExtension *)ext;
706 CExtension *pOwner = GetExtensionFromIdent(other);
710 info.iface = (SMInterface *)iface;
713 pExt->AddDependency(&info);
714 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
717 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
719 CExtension *pExt = (CExtension *)pOwner;
721 pExt->AddInterface(pInterface);
724 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
726 CExtension *pExt = (CExtension *)pParent;
728 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
731 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
733 List<CExtension *>::iterator iter;
735 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
737 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
741 CExtension *CExtensionManager::FindByOrder(unsigned int num)
743 if (num < 1 || num > m_Libs.size())
748 List<CExtension *>::iterator iter = m_Libs.begin();
750 while (iter != m_Libs.end())
762 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
769 CExtension *pExt = (CExtension *)_pExt;
771 if (m_Libs.find(pExt) == m_Libs.end())
776 /* Tell it to unload */
777 if (pExt->IsLoaded())
779 IExtensionInterface *pAPI = pExt->GetAPI();
780 pAPI->OnExtensionUnload();
783 /* First remove us from internal lists */
784 g_ShareSys.RemoveInterfaces(_pExt);
787 List<CExtension *> UnloadQueue;
789 /* Handle dependencies */
790 if (pExt->IsLoaded())
792 /* Unload any dependent plugins */
793 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
794 while (p_iter != pExt->m_Dependents.end())
796 /* We have to manually unlink ourselves here, since we're no longer being managed */
797 scripts->UnloadPlugin((*p_iter));
798 p_iter = pExt->m_Dependents.erase(p_iter);
801 List<String>::iterator s_iter;
802 for (s_iter = pExt->m_Libraries.begin();
803 s_iter != pExt->m_Libraries.end();
806 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
809 /* Notify and/or unload all dependencies */
810 List<CExtension *>::iterator c_iter;
812 IExtensionInterface *pAPI;
813 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
816 if ((pAPI=pDep->GetAPI()) == NULL)
824 /* Now, get its dependency list */
825 bool dropped = false;
826 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
827 while (i_iter != pDep->m_Deps.end())
829 if ((*i_iter).owner == _pExt)
831 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
836 UnloadQueue.push_back(pDep);
839 pAPI->NotifyInterfaceDrop((*i_iter).iface);
840 i_iter = pDep->m_Deps.erase(i_iter);
847 /* Flush out any back references to this plugin */
848 i_iter = pDep->m_ChildDeps.begin();
849 while (i_iter != pDep->m_ChildDeps.end())
851 if ((*i_iter).owner == pExt)
853 i_iter = pDep->m_ChildDeps.erase(i_iter);
862 /* Unbind our natives from Core */
863 pExt->DropEverything();
866 IdentityToken_t *pIdentity;
867 if ((pIdentity = pExt->GetIdentity()) != NULL)
869 SMGlobalClass *glob = SMGlobalClass::head;
872 glob->OnSourceModIdentityDropped(pIdentity);
873 glob = glob->m_pGlobalClassNext;
880 List<CExtension *>::iterator iter;
881 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
883 /* NOTE: This is safe because the unload function backs out of anything not present */
884 UnloadExtension((*iter));
890 void CExtensionManager::MarkAllLoaded()
892 List<CExtension *>::iterator iter;
895 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
898 if (!pExt->IsLoaded())
902 if (pExt->m_bFullyLoaded)
906 pExt->MarkAllLoaded();
910 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
912 /* This function doesn't really need to do anything now. We make sure the
913 * other extension is loaded, but handling of dependencies is really done
914 * by the interface fetcher.
916 if (required || autoload)
918 LoadAutoExtension(file);
922 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
924 int argcount = smcore.Argc(command);
927 const char *cmd = smcore.Arg(command, 2);
928 if (strcmp(cmd, "list") == 0)
930 List<CExtension *>::iterator iter;
932 unsigned int num = 1;
933 switch (m_Libs.size())
937 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
942 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
947 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
951 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
954 if (pExt->IsLoaded())
957 if (!pExt->IsRunning(error, sizeof(error)))
959 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
963 IExtensionInterface *pAPI = pExt->GetAPI();
964 const char *name = pAPI->GetExtensionName();
965 const char *version = pAPI->GetExtensionVerString();
966 const char *descr = pAPI->GetExtensionDescription();
967 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
970 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
975 else if (strcmp(cmd, "load") == 0)
979 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
983 const char *filename = smcore.Arg(command, 3);
984 char path[PLATFORM_MAX_PATH];
987 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
988 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
990 if (FindExtensionByFile(path) != NULL)
992 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
996 if (LoadExtension(path, error, sizeof(error)))
998 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1001 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1006 else if (strcmp(cmd, "info") == 0)
1010 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1014 const char *sId = smcore.Arg(command, 3);
1015 unsigned int id = atoi(sId);
1018 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1022 if (m_Libs.size() == 0)
1024 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1028 if (id > m_Libs.size())
1030 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1034 List<CExtension *>::iterator iter = m_Libs.begin();
1035 CExtension *pExt = NULL;
1036 while (iter != m_Libs.end())
1045 /* This should never happen */
1048 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1052 if (!pExt->IsLoaded())
1054 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1055 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1060 if (!pExt->IsRunning(error, sizeof(error)))
1062 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1063 rootmenu->ConsolePrint(" Loaded: Yes");
1064 rootmenu->ConsolePrint(" Running: No (%s)", error);
1068 IExtensionInterface *pAPI = pExt->GetAPI();
1069 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1070 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1071 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1072 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1073 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1075 if (pExt->IsExternal())
1077 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1079 else if (pAPI->IsMetamodExtension())
1081 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1085 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1091 else if (strcmp(cmd, "unload") == 0)
1095 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1099 const char *arg = smcore.Arg(command, 3);
1100 unsigned int num = atoi(arg);
1101 CExtension *pExt = FindByOrder(num);
1105 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1109 if (argcount > 4 && pExt->unload_code)
1111 const char *unload = smcore.Arg(command, 4);
1112 if (pExt->unload_code == (unsigned)atoi(unload))
1114 char filename[PLATFORM_MAX_PATH];
1115 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1116 UnloadExtension(pExt);
1117 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1121 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1126 if (!pExt->IsLoaded()
1127 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1129 char filename[PLATFORM_MAX_PATH];
1130 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1131 UnloadExtension(pExt);
1132 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1137 List<CPlugin *> plugins;
1138 if (pExt->m_ChildDeps.size())
1140 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1141 List<CExtension *>::iterator iter;
1143 /* Get list of all extensions */
1144 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1146 List<IfaceInfo>::iterator i_iter;
1148 if (!pOther->IsLoaded() || pOther == pExt)
1152 /* Get their dependencies */
1153 for (i_iter=pOther->m_Deps.begin();
1154 i_iter!=pOther->m_Deps.end();
1157 /* Is this dependency to us? */
1158 if ((*i_iter).owner != pExt)
1162 /* Will our dependent care? */
1163 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1165 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1166 /* Add to plugin unload list */
1167 List<CPlugin *>::iterator p_iter;
1168 for (p_iter=pOther->m_Dependents.begin();
1169 p_iter!=pOther->m_Dependents.end();
1172 if (plugins.find((*p_iter)) == plugins.end())
1174 plugins.push_back((*p_iter));
1181 if (pExt->m_Dependents.size())
1183 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1184 List<CPlugin *>::iterator iter;
1186 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1189 if (plugins.find(pPlugin) == plugins.end())
1191 plugins.push_back(pPlugin);
1194 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1197 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1200 srand(static_cast<int>(time(NULL)));
1201 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1202 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1203 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1208 else if (strcmp(cmd, "reload") == 0)
1212 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1216 const char *arg = smcore.Arg(command, 3);
1217 unsigned int num = atoi(arg);
1218 CExtension *pExt = FindByOrder(num);
1222 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1226 if (pExt->IsLoaded())
1228 char filename[PLATFORM_MAX_PATH];
1231 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1233 if (pExt->Reload(error, sizeof(error)))
1235 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1239 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1246 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1254 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1255 rootmenu->DrawGenericOption("info", "Extra extension information");
1256 rootmenu->DrawGenericOption("list", "List extensions");
1257 rootmenu->DrawGenericOption("load", "Load an extension");
1258 rootmenu->DrawGenericOption("reload", "Reload an extension");
1259 rootmenu->DrawGenericOption("unload", "Unload an extension");
1262 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1264 if (ptr->type == g_ExtType)
1266 return (CExtension *)(ptr->ptr);
1272 CExtensionManager::CExtensionManager()
1276 CExtensionManager::~CExtensionManager()
1280 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1282 CExtension *pExt = (CExtension *)pSource;
1283 pExt->AddLibrary(library);
1284 scripts->OnLibraryAction(library, LibraryAction_Added);
1287 bool CExtensionManager::LibraryExists(const char *library)
1291 for (List<CExtension *>::iterator iter = m_Libs.begin();
1292 iter != m_Libs.end();
1296 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1297 s_iter != pExt->m_Libraries.end();
1300 if ((*s_iter).compare(library) == 0)
1310 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1311 const char *filepath,
1312 const char *filename,
1316 IExtension *pAlready;
1317 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1322 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1324 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1331 m_Libs.push_back(pExt);
1336 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1338 IExtensionInterface *pAPI;
1339 List<CExtension *>::iterator iter;
1341 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1343 if ((pAPI = (*iter)->GetAPI()) == NULL)
1347 if (pAPI->GetExtensionVersion() > 3)
1349 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1354 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1356 CVector<IExtension *> *list = new CVector<IExtension *>();
1357 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1358 list->push_back(*iter);
1362 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1364 delete const_cast<CVector<IExtension *> *>(list);
1367 bool CLocalExtension::IsSameFile(const char *file)
1369 /* Only care about the shortened name. */
1370 return strcmp(file, m_File.c_str()) == 0;
1373 bool CRemoteExtension::IsSameFile(const char *file)
1375 /* :TODO: this could be better, but no one uses this API anyway. */
1376 return strcmp(file, m_Path.c_str()) == 0;