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, bool bErrorOnMissing)
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, bErrorOnMissing);
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 if (bErrorOnMissing || libsys->IsPathFile(p->GetPath()))
610 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
619 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
621 List<CExtension *>::iterator iter;
624 /* Chomp off the path */
625 char lookup[PLATFORM_MAX_PATH];
626 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
628 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
631 if (pExt->IsSameFile(lookup))
640 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
642 List<CExtension *>::iterator iter;
644 IExtensionInterface *pAPI;
647 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
650 if (!pExt->IsLoaded())
654 if ((pAPI = pExt->GetAPI()) == NULL)
658 name = pAPI->GetExtensionName();
663 if (strcmp(name, ext) == 0)
672 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
674 /* Remove platform extension if it's there. Compat hack. */
675 const char *ext = libsys->GetFileExtension(file);
676 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
678 char path2[PLATFORM_MAX_PATH];
679 smcore.Format(path2, sizeof(path2), "%s", file);
680 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
681 return LoadExtension(path2, error, maxlength);
684 IExtension *pAlready;
685 if ((pAlready=FindExtensionByFile(file)) != NULL)
690 CExtension *pExt = new CLocalExtension(file);
692 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
699 /* :TODO: do QueryRunning check if the map is loaded */
701 m_Libs.push_back(pExt);
706 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
708 CExtension *pExt = (CExtension *)pRequester;
709 CExtension *pOwner = (CExtension *)pInfo->owner;
711 pExt->AddDependency(pInfo);
713 IExtensionInterface *pAPI = pExt->GetAPI();
714 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
716 pOwner->AddChildDependent(pExt, pInfo->iface);
720 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
722 CExtension *pExt = (CExtension *)ext;
723 CExtension *pOwner = GetExtensionFromIdent(other);
727 info.iface = (SMInterface *)iface;
730 pExt->AddDependency(&info);
731 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
734 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
736 CExtension *pExt = (CExtension *)pOwner;
738 pExt->AddInterface(pInterface);
741 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
743 CExtension *pExt = (CExtension *)pParent;
745 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
748 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
750 List<CExtension *>::iterator iter;
752 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
754 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
758 CExtension *CExtensionManager::FindByOrder(unsigned int num)
760 if (num < 1 || num > m_Libs.size())
765 List<CExtension *>::iterator iter = m_Libs.begin();
767 while (iter != m_Libs.end())
779 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
784 CExtension *pExt = (CExtension *)_pExt;
786 if (m_Libs.find(pExt) == m_Libs.end())
789 /* Tell it to unload */
790 if (pExt->IsLoaded())
791 pExt->GetAPI()->OnExtensionUnload();
793 // Remove us from internal lists. Note that because we do this, it's
794 // possible that our extension could be added back if another plugin
795 // tries to load during this process. If we ever find this to happen,
796 // we can just block plugin loading.
797 g_ShareSys.RemoveInterfaces(_pExt);
800 List<CExtension *> UnloadQueue;
802 /* Handle dependencies */
803 if (pExt->IsLoaded())
805 /* Unload any dependent plugins */
806 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
807 while (p_iter != pExt->m_Dependents.end())
809 /* We have to manually unlink ourselves here, since we're no longer being managed */
810 scripts->UnloadPlugin((*p_iter));
811 p_iter = pExt->m_Dependents.erase(p_iter);
814 List<String>::iterator s_iter;
815 for (s_iter = pExt->m_Libraries.begin();
816 s_iter != pExt->m_Libraries.end();
819 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
822 /* Notify and/or unload all dependencies */
823 List<CExtension *>::iterator c_iter;
825 IExtensionInterface *pAPI;
826 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
829 if ((pAPI=pDep->GetAPI()) == NULL)
833 /* Now, get its dependency list */
834 bool dropped = false;
835 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
836 while (i_iter != pDep->m_Deps.end())
838 if ((*i_iter).owner == _pExt)
840 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
845 UnloadQueue.push_back(pDep);
848 pAPI->NotifyInterfaceDrop((*i_iter).iface);
849 i_iter = pDep->m_Deps.erase(i_iter);
856 /* Flush out any back references to this plugin */
857 i_iter = pDep->m_ChildDeps.begin();
858 while (i_iter != pDep->m_ChildDeps.end())
860 if ((*i_iter).owner == pExt)
861 i_iter = pDep->m_ChildDeps.erase(i_iter);
867 /* Unbind our natives from Core */
868 pExt->DropEverything();
871 IdentityToken_t *pIdentity;
872 if ((pIdentity = pExt->GetIdentity()) != NULL)
874 SMGlobalClass *glob = SMGlobalClass::head;
877 glob->OnSourceModIdentityDropped(pIdentity);
878 glob = glob->m_pGlobalClassNext;
882 // Everything has been informed that we're unloading, so give the
883 // extension one last notification.
884 if (pExt->IsLoaded() && pExt->GetAPI()->GetExtensionVersion() >= 7)
885 pExt->GetAPI()->OnDependenciesDropped();
890 List<CExtension *>::iterator iter;
891 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
893 /* NOTE: This is safe because the unload function backs out of anything not present */
894 UnloadExtension((*iter));
900 void CExtensionManager::MarkAllLoaded()
902 List<CExtension *>::iterator iter;
905 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
908 if (!pExt->IsLoaded())
912 if (pExt->m_bFullyLoaded)
916 pExt->MarkAllLoaded();
920 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
922 /* This function doesn't really need to do anything now. We make sure the
923 * other extension is loaded, but handling of dependencies is really done
924 * by the interface fetcher.
926 if (required || autoload)
928 LoadAutoExtension(file);
932 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
934 int argcount = smcore.Argc(command);
937 const char *cmd = smcore.Arg(command, 2);
938 if (strcmp(cmd, "list") == 0)
940 List<CExtension *>::iterator iter;
942 unsigned int num = 1;
943 switch (m_Libs.size())
947 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
952 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
957 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
961 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
964 if (pExt->IsLoaded())
967 if (!pExt->IsRunning(error, sizeof(error)))
969 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
973 IExtensionInterface *pAPI = pExt->GetAPI();
974 const char *name = pAPI->GetExtensionName();
975 const char *version = pAPI->GetExtensionVerString();
976 const char *descr = pAPI->GetExtensionDescription();
977 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
980 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
985 else if (strcmp(cmd, "load") == 0)
989 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
993 const char *filename = smcore.Arg(command, 3);
994 char path[PLATFORM_MAX_PATH];
997 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
998 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1000 if (FindExtensionByFile(path) != NULL)
1002 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1006 if (LoadExtension(path, error, sizeof(error)))
1008 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1011 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1016 else if (strcmp(cmd, "info") == 0)
1020 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1024 const char *sId = smcore.Arg(command, 3);
1025 unsigned int id = atoi(sId);
1028 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1032 if (m_Libs.size() == 0)
1034 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1038 if (id > m_Libs.size())
1040 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1044 List<CExtension *>::iterator iter = m_Libs.begin();
1045 CExtension *pExt = NULL;
1046 while (iter != m_Libs.end())
1055 /* This should never happen */
1058 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1062 if (!pExt->IsLoaded())
1064 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1065 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1070 if (!pExt->IsRunning(error, sizeof(error)))
1072 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1073 rootmenu->ConsolePrint(" Loaded: Yes");
1074 rootmenu->ConsolePrint(" Running: No (%s)", error);
1078 IExtensionInterface *pAPI = pExt->GetAPI();
1079 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1080 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1081 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1082 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1083 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1085 if (pExt->IsExternal())
1087 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1089 else if (pAPI->IsMetamodExtension())
1091 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1095 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1101 else if (strcmp(cmd, "unload") == 0)
1105 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1109 const char *arg = smcore.Arg(command, 3);
1110 unsigned int num = atoi(arg);
1111 CExtension *pExt = FindByOrder(num);
1115 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1119 if (argcount > 4 && pExt->unload_code)
1121 const char *unload = smcore.Arg(command, 4);
1122 if (pExt->unload_code == (unsigned)atoi(unload))
1124 char filename[PLATFORM_MAX_PATH];
1125 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1126 UnloadExtension(pExt);
1127 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1131 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1136 if (!pExt->IsLoaded()
1137 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1139 char filename[PLATFORM_MAX_PATH];
1140 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1141 UnloadExtension(pExt);
1142 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1147 List<CPlugin *> plugins;
1148 if (pExt->m_ChildDeps.size())
1150 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1151 List<CExtension *>::iterator iter;
1153 /* Get list of all extensions */
1154 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1156 List<IfaceInfo>::iterator i_iter;
1158 if (!pOther->IsLoaded() || pOther == pExt)
1162 /* Get their dependencies */
1163 for (i_iter=pOther->m_Deps.begin();
1164 i_iter!=pOther->m_Deps.end();
1167 /* Is this dependency to us? */
1168 if ((*i_iter).owner != pExt)
1172 /* Will our dependent care? */
1173 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1175 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1176 /* Add to plugin unload list */
1177 List<CPlugin *>::iterator p_iter;
1178 for (p_iter=pOther->m_Dependents.begin();
1179 p_iter!=pOther->m_Dependents.end();
1182 if (plugins.find((*p_iter)) == plugins.end())
1184 plugins.push_back((*p_iter));
1191 if (pExt->m_Dependents.size())
1193 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1194 List<CPlugin *>::iterator iter;
1196 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1199 if (plugins.find(pPlugin) == plugins.end())
1201 plugins.push_back(pPlugin);
1204 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1207 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1210 srand(static_cast<int>(time(NULL)));
1211 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1212 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1213 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1218 else if (strcmp(cmd, "reload") == 0)
1222 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1226 const char *arg = smcore.Arg(command, 3);
1227 unsigned int num = atoi(arg);
1228 CExtension *pExt = FindByOrder(num);
1232 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1236 if (pExt->IsLoaded())
1238 char filename[PLATFORM_MAX_PATH];
1241 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1243 if (pExt->Reload(error, sizeof(error)))
1245 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1249 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1256 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1264 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1265 rootmenu->DrawGenericOption("info", "Extra extension information");
1266 rootmenu->DrawGenericOption("list", "List extensions");
1267 rootmenu->DrawGenericOption("load", "Load an extension");
1268 rootmenu->DrawGenericOption("reload", "Reload an extension");
1269 rootmenu->DrawGenericOption("unload", "Unload an extension");
1272 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1274 if (ptr->type == g_ExtType)
1276 return (CExtension *)(ptr->ptr);
1282 CExtensionManager::CExtensionManager()
1286 CExtensionManager::~CExtensionManager()
1290 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1292 CExtension *pExt = (CExtension *)pSource;
1293 pExt->AddLibrary(library);
1294 scripts->OnLibraryAction(library, LibraryAction_Added);
1297 bool CExtensionManager::LibraryExists(const char *library)
1301 for (List<CExtension *>::iterator iter = m_Libs.begin();
1302 iter != m_Libs.end();
1306 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1307 s_iter != pExt->m_Libraries.end();
1310 if ((*s_iter).compare(library) == 0)
1320 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1321 const char *filepath,
1322 const char *filename,
1326 IExtension *pAlready;
1327 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1332 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1334 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1341 m_Libs.push_back(pExt);
1346 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1348 IExtensionInterface *pAPI;
1349 List<CExtension *>::iterator iter;
1351 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1353 if ((pAPI = (*iter)->GetAPI()) == NULL)
1357 if (pAPI->GetExtensionVersion() > 3)
1359 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1364 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1366 CVector<IExtension *> *list = new CVector<IExtension *>();
1367 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1368 list->push_back(*iter);
1372 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1374 delete const_cast<CVector<IExtension *> *>(list);
1377 bool CLocalExtension::IsSameFile(const char *file)
1379 /* Only care about the shortened name. */
1380 return strcmp(file, m_File.c_str()) == 0;
1383 bool CRemoteExtension::IsSameFile(const char *file)
1385 /* :TODO: this could be better, but no one uses this API anyway. */
1386 return strcmp(file, m_Path.c_str()) == 0;