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))
104 else if (strcmp(smcore.gamesuffix, "2.nd") == 0)
106 g_pSM->BuildPath(Path_SM,
109 "extensions/%s.2.l4d2." PLATFORM_LIB_EXT,
112 if (libsys->IsPathFile(path))
118 /* First see if there is an engine specific build! */
119 g_pSM->BuildPath(Path_SM,
122 "extensions/auto.%s/%s." PLATFORM_LIB_EXT,
126 /* Try the "normal" version */
127 if (!libsys->IsPathFile(path))
130 g_pSM->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 = 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 = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
197 m_pLib->CloseLibrary();
204 if (!CExtension::Load(error, maxlength))
206 if (m_pAPI->IsMetamodExtension())
210 smcore.UnloadMMSPlugin(m_PlId);
214 m_pLib->CloseLibrary();
223 void CRemoteExtension::Unload()
227 void CLocalExtension::Unload()
229 if (m_pAPI != NULL && m_PlId)
231 smcore.UnloadMMSPlugin(m_PlId);
237 m_pLib->CloseLibrary();
242 bool CRemoteExtension::Reload(char *error, size_t maxlength)
244 snprintf(error, maxlength, "Remote extensions do not support reloading");
248 bool CLocalExtension::Reload(char *error, size_t maxlength)
250 if (m_pLib == NULL) // FIXME: just load it instead?
253 m_pAPI->OnExtensionUnload();
256 return Load(error, maxlength);
259 bool CRemoteExtension::IsExternal()
264 bool CLocalExtension::IsExternal()
269 CExtension::~CExtension()
274 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
278 snprintf(error, maxlength, "No IExtensionInterface instance provided");
282 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
284 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
291 bool CExtension::Load(char *error, size_t maxlength)
294 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
301 /* Check if we're past load time */
302 if (!smcore.IsMapLoading())
304 m_pAPI->OnExtensionsAllLoaded();
311 void CExtension::CreateIdentity()
313 if (m_pIdentToken != NULL)
318 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
321 void CExtension::DestroyIdentity()
323 if (m_pIdentToken == NULL)
328 g_ShareSys.DestroyIdentity(m_pIdentToken);
329 m_pIdentToken = NULL;
332 void CExtension::MarkAllLoaded()
334 assert(m_bFullyLoaded == false);
337 m_bFullyLoaded = true;
338 m_pAPI->OnExtensionsAllLoaded();
342 void CExtension::AddPlugin(CPlugin *pPlugin)
344 /* Unfortunately we have to do this :( */
345 if (m_Dependents.find(pPlugin) != m_Dependents.end())
347 m_Dependents.push_back(pPlugin);
351 void CExtension::SetError(const char *error)
353 m_Error.assign(error);
356 IExtensionInterface *CExtension::GetAPI()
361 const char *CExtension::GetFilename()
363 return m_RealFile.c_str();
366 const char *CExtension::GetPath() const
368 return m_Path.c_str();
371 IdentityToken_t *CExtension::GetIdentity()
373 return m_pIdentToken;
376 bool CRemoteExtension::IsLoaded()
378 return (m_pAPI != NULL);
381 bool CLocalExtension::IsLoaded()
383 return (m_pLib != NULL);
386 void CExtension::AddDependency(const IfaceInfo *pInfo)
388 if (m_Deps.find(*pInfo) == m_Deps.end())
390 m_Deps.push_back(*pInfo);
394 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
396 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
399 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
405 List<IfaceInfo>::iterator iter;
406 for (iter = m_ChildDeps.begin();
407 iter != m_ChildDeps.end();
410 IfaceInfo &other = (*iter);
417 m_ChildDeps.push_back(info);
420 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
422 List<IfaceInfo>::iterator iter = m_Deps.begin();
424 if (iter == m_Deps.end())
431 *pOwner = (*iter).owner;
435 *pInterface = (*iter).iface;
438 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
440 return (ITERATOR *)pIter;
443 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
445 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
446 List<IfaceInfo>::iterator _iter;
448 if (_iter == m_Deps.end())
457 *pOwner = (*_iter).owner;
461 *pInterface = (*_iter).iface;
466 if (_iter == m_Deps.end())
474 void CExtension::FreeDependencyIterator(ITERATOR *iter)
476 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
481 void CExtension::AddInterface(SMInterface *pInterface)
483 m_Interfaces.push_back(pInterface);
486 bool CExtension::IsRunning(char *error, size_t maxlength)
492 snprintf(error, maxlength, "%s", m_Error.c_str());
497 return m_pAPI->QueryRunning(error, maxlength);
500 void CExtension::AddLibrary(const char *library)
502 m_Libraries.push_back(library);
505 /*********************
506 * EXTENSION MANAGER *
507 *********************/
509 void CExtensionManager::OnSourceModAllInitialized()
511 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
512 pluginsys->AddPluginsListener(this);
513 rootmenu->AddRootConsoleCommand("exts", "Manage extensions", this);
514 g_ShareSys.AddInterface(NULL, this);
517 void CExtensionManager::OnSourceModShutdown()
519 rootmenu->RemoveRootConsoleCommand("exts", this);
520 pluginsys->RemovePluginsListener(this);
521 g_ShareSys.DestroyIdentType(g_ExtType);
524 void CExtensionManager::Shutdown()
526 List<CExtension *>::iterator iter;
528 while ((iter = m_Libs.begin()) != m_Libs.end())
530 UnloadExtension((*iter));
534 void CExtensionManager::TryAutoload()
536 char path[PLATFORM_MAX_PATH];
538 g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
540 IDirectory *pDir = libsys->OpenDirectory(path);
548 while (pDir->MoreFiles())
550 if (pDir->IsEntryDirectory())
556 lfile = pDir->GetEntryName();
558 if (len <= 9) /* size of ".autoload" */
564 if (strcmp(&lfile[len - 9], ".autoload") != 0)
570 char file[PLATFORM_MAX_PATH];
571 len = smcore.Format(file, sizeof(file), "%s", lfile);
572 strcpy(&file[len - 9], ".ext");
574 LoadAutoExtension(file);
580 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
582 /* Remove platform extension if it's there. Compat hack. */
583 const char *ext = libsys->GetFileExtension(path);
584 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
586 char path2[PLATFORM_MAX_PATH];
587 smcore.Format(path2, sizeof(path2), "%s", path);
588 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
589 return LoadAutoExtension(path2);
592 IExtension *pAlready;
593 if ((pAlready=FindExtensionByFile(path)) != NULL)
599 CExtension *p = new CLocalExtension(path);
601 /* We put us in the list beforehand so extensions that check for each other
602 * won't recursively load each other.
606 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
608 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
615 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
617 List<CExtension *>::iterator iter;
620 /* Chomp off the path */
621 char lookup[PLATFORM_MAX_PATH];
622 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
624 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
627 if (pExt->IsSameFile(lookup))
636 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
638 List<CExtension *>::iterator iter;
640 IExtensionInterface *pAPI;
643 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
646 if (!pExt->IsLoaded())
650 if ((pAPI = pExt->GetAPI()) == NULL)
654 name = pAPI->GetExtensionName();
659 if (strcmp(name, ext) == 0)
668 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
670 /* Remove platform extension if it's there. Compat hack. */
671 const char *ext = libsys->GetFileExtension(file);
672 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
674 char path2[PLATFORM_MAX_PATH];
675 smcore.Format(path2, sizeof(path2), "%s", file);
676 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
677 return LoadExtension(path2, error, maxlength);
680 IExtension *pAlready;
681 if ((pAlready=FindExtensionByFile(file)) != NULL)
686 CExtension *pExt = new CLocalExtension(file);
688 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
695 /* :TODO: do QueryRunning check if the map is loaded */
697 m_Libs.push_back(pExt);
702 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
704 CExtension *pExt = (CExtension *)pRequester;
705 CExtension *pOwner = (CExtension *)pInfo->owner;
707 pExt->AddDependency(pInfo);
709 IExtensionInterface *pAPI = pExt->GetAPI();
710 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
712 pOwner->AddChildDependent(pExt, pInfo->iface);
716 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
718 CExtension *pExt = (CExtension *)ext;
719 CExtension *pOwner = GetExtensionFromIdent(other);
723 info.iface = (SMInterface *)iface;
726 pExt->AddDependency(&info);
727 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
730 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
732 CExtension *pExt = (CExtension *)pOwner;
734 pExt->AddInterface(pInterface);
737 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
739 CExtension *pExt = (CExtension *)pParent;
741 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
744 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
746 List<CExtension *>::iterator iter;
748 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
750 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
754 CExtension *CExtensionManager::FindByOrder(unsigned int num)
756 if (num < 1 || num > m_Libs.size())
761 List<CExtension *>::iterator iter = m_Libs.begin();
763 while (iter != m_Libs.end())
775 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
780 CExtension *pExt = (CExtension *)_pExt;
782 if (m_Libs.find(pExt) == m_Libs.end())
785 /* Tell it to unload */
786 if (pExt->IsLoaded())
787 pExt->GetAPI()->OnExtensionUnload();
789 // Remove us from internal lists. Note that because we do this, it's
790 // possible that our extension could be added back if another plugin
791 // tries to load during this process. If we ever find this to happen,
792 // we can just block plugin loading.
793 g_ShareSys.RemoveInterfaces(_pExt);
796 List<CExtension *> UnloadQueue;
798 /* Handle dependencies */
799 if (pExt->IsLoaded())
801 /* Unload any dependent plugins */
802 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
803 while (p_iter != pExt->m_Dependents.end())
805 /* We have to manually unlink ourselves here, since we're no longer being managed */
806 scripts->UnloadPlugin((*p_iter));
807 p_iter = pExt->m_Dependents.erase(p_iter);
810 List<String>::iterator s_iter;
811 for (s_iter = pExt->m_Libraries.begin();
812 s_iter != pExt->m_Libraries.end();
815 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
818 /* Notify and/or unload all dependencies */
819 List<CExtension *>::iterator c_iter;
821 IExtensionInterface *pAPI;
822 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
825 if ((pAPI=pDep->GetAPI()) == NULL)
829 /* Now, get its dependency list */
830 bool dropped = false;
831 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
832 while (i_iter != pDep->m_Deps.end())
834 if ((*i_iter).owner == _pExt)
836 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
841 UnloadQueue.push_back(pDep);
844 pAPI->NotifyInterfaceDrop((*i_iter).iface);
845 i_iter = pDep->m_Deps.erase(i_iter);
852 /* Flush out any back references to this plugin */
853 i_iter = pDep->m_ChildDeps.begin();
854 while (i_iter != pDep->m_ChildDeps.end())
856 if ((*i_iter).owner == pExt)
857 i_iter = pDep->m_ChildDeps.erase(i_iter);
863 /* Unbind our natives from Core */
864 pExt->DropEverything();
867 IdentityToken_t *pIdentity;
868 if ((pIdentity = pExt->GetIdentity()) != NULL)
870 SMGlobalClass *glob = SMGlobalClass::head;
873 glob->OnSourceModIdentityDropped(pIdentity);
874 glob = glob->m_pGlobalClassNext;
878 // Everything has been informed that we're unloading, so give the
879 // extension one last notification.
880 if (pExt->IsLoaded() && pExt->GetAPI()->GetExtensionVersion() >= 7)
881 pExt->GetAPI()->OnDependenciesDropped();
886 List<CExtension *>::iterator iter;
887 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
889 /* NOTE: This is safe because the unload function backs out of anything not present */
890 UnloadExtension((*iter));
896 void CExtensionManager::MarkAllLoaded()
898 List<CExtension *>::iterator iter;
901 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
904 if (!pExt->IsLoaded())
908 if (pExt->m_bFullyLoaded)
912 pExt->MarkAllLoaded();
916 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
918 /* This function doesn't really need to do anything now. We make sure the
919 * other extension is loaded, but handling of dependencies is really done
920 * by the interface fetcher.
922 if (required || autoload)
924 LoadAutoExtension(file);
928 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
930 int argcount = smcore.Argc(command);
933 const char *cmd = smcore.Arg(command, 2);
934 if (strcmp(cmd, "list") == 0)
936 List<CExtension *>::iterator iter;
938 unsigned int num = 1;
939 switch (m_Libs.size())
943 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
948 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
953 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
957 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
960 if (pExt->IsLoaded())
963 if (!pExt->IsRunning(error, sizeof(error)))
965 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
969 IExtensionInterface *pAPI = pExt->GetAPI();
970 const char *name = pAPI->GetExtensionName();
971 const char *version = pAPI->GetExtensionVerString();
972 const char *descr = pAPI->GetExtensionDescription();
973 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
976 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
981 else if (strcmp(cmd, "load") == 0)
985 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
989 const char *filename = smcore.Arg(command, 3);
990 char path[PLATFORM_MAX_PATH];
993 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
994 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
996 if (FindExtensionByFile(path) != NULL)
998 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1002 if (LoadExtension(path, error, sizeof(error)))
1004 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1007 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1012 else if (strcmp(cmd, "info") == 0)
1016 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1020 const char *sId = smcore.Arg(command, 3);
1021 unsigned int id = atoi(sId);
1024 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1028 if (m_Libs.size() == 0)
1030 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1034 if (id > m_Libs.size())
1036 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1040 List<CExtension *>::iterator iter = m_Libs.begin();
1041 CExtension *pExt = NULL;
1042 while (iter != m_Libs.end())
1051 /* This should never happen */
1054 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1058 if (!pExt->IsLoaded())
1060 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1061 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1066 if (!pExt->IsRunning(error, sizeof(error)))
1068 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1069 rootmenu->ConsolePrint(" Loaded: Yes");
1070 rootmenu->ConsolePrint(" Running: No (%s)", error);
1074 IExtensionInterface *pAPI = pExt->GetAPI();
1075 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1076 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1077 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1078 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1079 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1081 if (pExt->IsExternal())
1083 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1085 else if (pAPI->IsMetamodExtension())
1087 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1091 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1097 else if (strcmp(cmd, "unload") == 0)
1101 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1105 const char *arg = smcore.Arg(command, 3);
1106 unsigned int num = atoi(arg);
1107 CExtension *pExt = FindByOrder(num);
1111 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1115 if (argcount > 4 && pExt->unload_code)
1117 const char *unload = smcore.Arg(command, 4);
1118 if (pExt->unload_code == (unsigned)atoi(unload))
1120 char filename[PLATFORM_MAX_PATH];
1121 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1122 UnloadExtension(pExt);
1123 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1127 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1132 if (!pExt->IsLoaded()
1133 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1135 char filename[PLATFORM_MAX_PATH];
1136 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1137 UnloadExtension(pExt);
1138 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1143 List<CPlugin *> plugins;
1144 if (pExt->m_ChildDeps.size())
1146 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1147 List<CExtension *>::iterator iter;
1149 /* Get list of all extensions */
1150 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1152 List<IfaceInfo>::iterator i_iter;
1154 if (!pOther->IsLoaded() || pOther == pExt)
1158 /* Get their dependencies */
1159 for (i_iter=pOther->m_Deps.begin();
1160 i_iter!=pOther->m_Deps.end();
1163 /* Is this dependency to us? */
1164 if ((*i_iter).owner != pExt)
1168 /* Will our dependent care? */
1169 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1171 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1172 /* Add to plugin unload list */
1173 List<CPlugin *>::iterator p_iter;
1174 for (p_iter=pOther->m_Dependents.begin();
1175 p_iter!=pOther->m_Dependents.end();
1178 if (plugins.find((*p_iter)) == plugins.end())
1180 plugins.push_back((*p_iter));
1187 if (pExt->m_Dependents.size())
1189 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1190 List<CPlugin *>::iterator iter;
1192 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1195 if (plugins.find(pPlugin) == plugins.end())
1197 plugins.push_back(pPlugin);
1200 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1203 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1206 srand(static_cast<int>(time(NULL)));
1207 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1208 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1209 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1214 else if (strcmp(cmd, "reload") == 0)
1218 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1222 const char *arg = smcore.Arg(command, 3);
1223 unsigned int num = atoi(arg);
1224 CExtension *pExt = FindByOrder(num);
1228 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1232 if (pExt->IsLoaded())
1234 char filename[PLATFORM_MAX_PATH];
1237 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1239 if (pExt->Reload(error, sizeof(error)))
1241 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1245 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1252 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1260 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1261 rootmenu->DrawGenericOption("info", "Extra extension information");
1262 rootmenu->DrawGenericOption("list", "List extensions");
1263 rootmenu->DrawGenericOption("load", "Load an extension");
1264 rootmenu->DrawGenericOption("reload", "Reload an extension");
1265 rootmenu->DrawGenericOption("unload", "Unload an extension");
1268 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1270 if (ptr->type == g_ExtType)
1272 return (CExtension *)(ptr->ptr);
1278 CExtensionManager::CExtensionManager()
1282 CExtensionManager::~CExtensionManager()
1286 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1288 CExtension *pExt = (CExtension *)pSource;
1289 pExt->AddLibrary(library);
1290 scripts->OnLibraryAction(library, LibraryAction_Added);
1293 bool CExtensionManager::LibraryExists(const char *library)
1297 for (List<CExtension *>::iterator iter = m_Libs.begin();
1298 iter != m_Libs.end();
1302 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1303 s_iter != pExt->m_Libraries.end();
1306 if ((*s_iter).compare(library) == 0)
1316 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1317 const char *filepath,
1318 const char *filename,
1322 IExtension *pAlready;
1323 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1328 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1330 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1337 m_Libs.push_back(pExt);
1342 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1344 IExtensionInterface *pAPI;
1345 List<CExtension *>::iterator iter;
1347 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1349 if ((pAPI = (*iter)->GetAPI()) == NULL)
1353 if (pAPI->GetExtensionVersion() > 3)
1355 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1360 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1362 CVector<IExtension *> *list = new CVector<IExtension *>();
1363 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1364 list->push_back(*iter);
1368 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1370 delete const_cast<CVector<IExtension *> *>(list);
1373 bool CLocalExtension::IsSameFile(const char *file)
1375 /* Only care about the shortened name. */
1376 return strcmp(file, m_File.c_str()) == 0;
1379 bool CRemoteExtension::IsSameFile(const char *file)
1381 /* :TODO: this could be better, but no one uses this API anyway. */
1382 return strcmp(file, m_Path.c_str()) == 0;