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"
41 CExtensionManager g_Extensions;
42 IdentityType_t g_ExtType;
44 void CExtension::Initialize(const char *filename, const char *path)
49 m_bFullyLoaded = false;
50 m_File.assign(filename);
52 char real_name[PLATFORM_MAX_PATH];
53 g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
54 m_RealFile.assign(real_name);
57 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
59 Initialize(filename, path);
63 #if defined METAMOD_PLAPI_VERSION
64 #if SOURCE_ENGINE == SE_LEFT4DEAD
65 #define GAMEFIX "2.l4d"
66 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
67 #define GAMEFIX "2.l4d2"
68 #elif SOURCE_ENGINE == SE_ALIENSWARM
69 #define GAMEFIX "2.swarm"
70 #elif SOURCE_ENGINE == SE_ORANGEBOX
71 #define GAMEFIX "2.ep2"
72 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
73 #define GAMEFIX "2.bgt"
74 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
75 #define GAMEFIX "2.ep2v"
76 #elif SOURCE_ENGINE == SE_DARKMESSIAH
77 #define GAMEFIX "2.darkm"
79 #define GAMEFIX "2.ep1"
80 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
81 #else //METAMOD_PLAPI_VERSION
82 #define GAMEFIX "1.ep1"
83 #endif //METAMOD_PLAPI_VERSION
85 CLocalExtension::CLocalExtension(const char *filename)
90 char path[PLATFORM_MAX_PATH];
92 /* Special case for new bintools binary */
93 if (strcmp(filename, "bintools.ext") == 0)
98 /* Zeroth, see if there is an engine specific build in the new place. */
99 g_SourceMod.BuildPath(Path_SM,
102 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
105 if (g_LibSys.IsPathFile(path))
110 /* First see if there is an engine specific build! */
111 g_SourceMod.BuildPath(Path_SM,
114 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
118 /* Try the "normal" version */
119 if (!g_LibSys.IsPathFile(path))
121 g_SourceMod.BuildPath(Path_SM,
124 "extensions/%s." PLATFORM_LIB_EXT,
129 Initialize(filename, path);
132 bool CRemoteExtension::Load(char *error, size_t maxlength)
134 if (!PerformAPICheck(error, maxlength))
140 if (!CExtension::Load(error, maxlength))
149 bool CLocalExtension::Load(char *error, size_t maxlength)
151 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
158 typedef IExtensionInterface *(*GETAPI)();
159 GETAPI pfnGetAPI = NULL;
161 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
163 m_pLib->CloseLibrary();
165 snprintf(error, maxlength, "Unable to find extension entry point");
169 m_pAPI = pfnGetAPI();
171 /* Check pointer and version */
172 if (!PerformAPICheck(error, maxlength))
174 m_pLib->CloseLibrary();
181 if (m_pAPI->IsMetamodExtension())
184 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
187 if (!CExtension::Load(error, maxlength))
189 if (m_pAPI->IsMetamodExtension())
194 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
198 m_pLib->CloseLibrary();
207 void CRemoteExtension::Unload()
211 void CLocalExtension::Unload()
213 if (m_pAPI != NULL && m_PlId)
215 char temp_buffer[255];
216 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
222 m_pLib->CloseLibrary();
227 bool CRemoteExtension::Reload(char *error, size_t maxlength)
229 snprintf(error, maxlength, "Remote extensions do not support reloading");
233 bool CLocalExtension::Reload(char *error, size_t maxlength)
235 if (m_pLib == NULL) // FIXME: just load it instead?
238 m_pAPI->OnExtensionUnload();
241 return Load(error, maxlength);
244 bool CRemoteExtension::IsExternal()
249 bool CLocalExtension::IsExternal()
254 CExtension::~CExtension()
259 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
263 snprintf(error, maxlength, "No IExtensionInterface instance provided");
267 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
269 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
276 bool CExtension::Load(char *error, size_t maxlength)
279 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
286 /* Check if we're past load time */
287 if (!g_SourceMod.IsMapLoading())
289 m_pAPI->OnExtensionsAllLoaded();
296 void CExtension::CreateIdentity()
298 if (m_pIdentToken != NULL)
303 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
306 void CExtension::DestroyIdentity()
308 if (m_pIdentToken == NULL)
313 g_ShareSys.DestroyIdentity(m_pIdentToken);
314 m_pIdentToken = NULL;
317 void CExtension::MarkAllLoaded()
319 assert(m_bFullyLoaded == false);
322 m_bFullyLoaded = true;
323 m_pAPI->OnExtensionsAllLoaded();
327 void CExtension::AddPlugin(CPlugin *pPlugin)
329 /* Unfortunately we have to do this :( */
330 if (m_Dependents.find(pPlugin) != m_Dependents.end())
332 m_Dependents.push_back(pPlugin);
336 void CExtension::SetError(const char *error)
338 m_Error.assign(error);
341 IExtensionInterface *CExtension::GetAPI()
346 const char *CExtension::GetFilename()
348 return m_RealFile.c_str();
351 IdentityToken_t *CExtension::GetIdentity()
353 return m_pIdentToken;
356 bool CRemoteExtension::IsLoaded()
358 return (m_pAPI != NULL);
361 bool CLocalExtension::IsLoaded()
363 return (m_pLib != NULL);
366 void CExtension::AddDependency(const IfaceInfo *pInfo)
368 if (m_Deps.find(*pInfo) == m_Deps.end())
370 m_Deps.push_back(*pInfo);
374 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
376 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
379 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
385 List<IfaceInfo>::iterator iter;
386 for (iter = m_ChildDeps.begin();
387 iter != m_ChildDeps.end();
390 IfaceInfo &other = (*iter);
397 m_ChildDeps.push_back(info);
400 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
402 List<IfaceInfo>::iterator iter = m_Deps.begin();
404 if (iter == m_Deps.end())
411 *pOwner = (*iter).owner;
415 *pInterface = (*iter).iface;
418 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
420 return (ITERATOR *)pIter;
423 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
425 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
426 List<IfaceInfo>::iterator _iter;
428 if (_iter == m_Deps.end())
437 *pOwner = (*_iter).owner;
441 *pInterface = (*_iter).iface;
446 if (_iter == m_Deps.end())
454 void CExtension::FreeDependencyIterator(ITERATOR *iter)
456 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
461 void CExtension::AddInterface(SMInterface *pInterface)
463 m_Interfaces.push_back(pInterface);
466 bool CExtension::IsRunning(char *error, size_t maxlength)
472 snprintf(error, maxlength, "%s", m_Error.c_str());
477 return m_pAPI->QueryRunning(error, maxlength);
480 void CExtension::AddLibrary(const char *library)
482 m_Libraries.push_back(library);
485 /*********************
486 * EXTENSION MANAGER *
487 *********************/
489 void CExtensionManager::OnSourceModAllInitialized()
491 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
492 g_PluginSys.AddPluginsListener(this);
493 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
494 g_ShareSys.AddInterface(NULL, this);
497 void CExtensionManager::OnSourceModShutdown()
499 g_RootMenu.RemoveRootConsoleCommand("exts", this);
500 g_ShareSys.DestroyIdentType(g_ExtType);
503 void CExtensionManager::Shutdown()
505 List<CExtension *>::iterator iter;
507 while ((iter = m_Libs.begin()) != m_Libs.end())
509 UnloadExtension((*iter));
513 void CExtensionManager::TryAutoload()
515 char path[PLATFORM_MAX_PATH];
517 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
519 IDirectory *pDir = g_LibSys.OpenDirectory(path);
527 while (pDir->MoreFiles())
529 if (pDir->IsEntryDirectory())
535 lfile = pDir->GetEntryName();
537 if (len <= 9) /* size of ".autoload" */
543 if (strcmp(&lfile[len - 9], ".autoload") != 0)
549 char file[PLATFORM_MAX_PATH];
550 len = UTIL_Format(file, sizeof(file), "%s", lfile);
551 strcpy(&file[len - 9], ".ext");
553 LoadAutoExtension(file);
559 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
561 /* Remove platform extension if it's there. Compat hack. */
562 const char *ext = g_LibSys.GetFileExtension(path);
563 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
565 char path2[PLATFORM_MAX_PATH];
566 UTIL_Format(path2, sizeof(path2), "%s", path);
567 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
568 return LoadAutoExtension(path2);
571 IExtension *pAlready;
572 if ((pAlready=FindExtensionByFile(path)) != NULL)
578 CExtension *p = new CLocalExtension(path);
580 /* We put us in the list beforehand so extensions that check for each other
581 * won't recursively load each other.
585 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
587 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
594 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
596 List<CExtension *>::iterator iter;
599 /* Chomp off the path */
600 char lookup[PLATFORM_MAX_PATH];
601 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
603 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
606 if (pExt->IsSameFile(lookup))
615 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
617 List<CExtension *>::iterator iter;
619 IExtensionInterface *pAPI;
622 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
625 if (!pExt->IsLoaded())
629 if ((pAPI = pExt->GetAPI()) == NULL)
633 name = pAPI->GetExtensionName();
638 if (strcmp(name, ext) == 0)
647 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
649 /* Remove platform extension if it's there. Compat hack. */
650 const char *ext = g_LibSys.GetFileExtension(file);
651 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
653 char path2[PLATFORM_MAX_PATH];
654 UTIL_Format(path2, sizeof(path2), "%s", file);
655 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
656 return LoadExtension(path2, error, maxlength);
659 IExtension *pAlready;
660 if ((pAlready=FindExtensionByFile(file)) != NULL)
665 CExtension *pExt = new CLocalExtension(file);
667 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
674 /* :TODO: do QueryRunning check if the map is loaded */
676 m_Libs.push_back(pExt);
681 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
683 CExtension *pExt = (CExtension *)pRequester;
684 CExtension *pOwner = (CExtension *)pInfo->owner;
686 pExt->AddDependency(pInfo);
688 IExtensionInterface *pAPI = pExt->GetAPI();
689 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
691 pOwner->AddChildDependent(pExt, pInfo->iface);
695 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
697 CExtension *pExt = (CExtension *)ext;
698 CExtension *pOwner = GetExtensionFromIdent(other);
702 info.iface = (SMInterface *)iface;
705 pExt->AddDependency(&info);
706 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
709 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
711 CExtension *pExt = (CExtension *)pOwner;
713 pExt->AddInterface(pInterface);
716 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
718 CExtension *pExt = (CExtension *)pParent;
720 pExt->AddPlugin(pPlugin);
723 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
725 List<CExtension *>::iterator iter;
727 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
729 (*iter)->DropRefsTo((CPlugin *)plugin);
733 CExtension *CExtensionManager::FindByOrder(unsigned int num)
735 if (num < 1 || num > m_Libs.size())
740 List<CExtension *>::iterator iter = m_Libs.begin();
742 while (iter != m_Libs.end())
754 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
761 CExtension *pExt = (CExtension *)_pExt;
763 if (m_Libs.find(pExt) == m_Libs.end())
768 /* First remove us from internal lists */
769 g_ShareSys.RemoveInterfaces(_pExt);
772 List<CExtension *> UnloadQueue;
774 /* Handle dependencies */
775 if (pExt->IsLoaded())
777 /* Unload any dependent plugins */
778 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
779 while (p_iter != pExt->m_Dependents.end())
781 /* We have to manually unlink ourselves here, since we're no longer being managed */
782 g_PluginSys.UnloadPlugin((*p_iter));
783 p_iter = pExt->m_Dependents.erase(p_iter);
786 List<String>::iterator s_iter;
787 for (s_iter = pExt->m_Libraries.begin();
788 s_iter != pExt->m_Libraries.end();
791 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
794 /* Notify and/or unload all dependencies */
795 List<CExtension *>::iterator c_iter;
797 IExtensionInterface *pAPI;
798 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
801 if ((pAPI=pDep->GetAPI()) == NULL)
809 /* Now, get its dependency list */
810 bool dropped = false;
811 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
812 while (i_iter != pDep->m_Deps.end())
814 if ((*i_iter).owner == _pExt)
816 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
821 UnloadQueue.push_back(pDep);
824 pAPI->NotifyInterfaceDrop((*i_iter).iface);
825 i_iter = pDep->m_Deps.erase(i_iter);
832 /* Flush out any back references to this plugin */
833 i_iter = pDep->m_ChildDeps.begin();
834 while (i_iter != pDep->m_ChildDeps.end())
836 if ((*i_iter).owner == pExt)
838 i_iter = pDep->m_ChildDeps.erase(i_iter);
847 /* Unbind our natives from Core */
848 pExt->DropEverything();
851 IdentityToken_t *pIdentity;
852 if ((pIdentity = pExt->GetIdentity()) != NULL)
854 SMGlobalClass *glob = SMGlobalClass::head;
857 glob->OnSourceModIdentityDropped(pIdentity);
858 glob = glob->m_pGlobalClassNext;
862 /* Tell it to unload */
863 if (pExt->IsLoaded())
865 IExtensionInterface *pAPI = pExt->GetAPI();
866 pAPI->OnExtensionUnload();
872 List<CExtension *>::iterator iter;
873 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
875 /* NOTE: This is safe because the unload function backs out of anything not present */
876 UnloadExtension((*iter));
882 void CExtensionManager::MarkAllLoaded()
884 List<CExtension *>::iterator iter;
887 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
890 if (!pExt->IsLoaded())
894 if (pExt->m_bFullyLoaded)
898 pExt->MarkAllLoaded();
902 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
904 /* This function doesn't really need to do anything now. We make sure the
905 * other extension is loaded, but handling of dependencies is really done
906 * by the interface fetcher.
908 if (required || autoload)
910 LoadAutoExtension(file);
914 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
916 int argcount = command.ArgC();
919 const char *cmd = command.Arg(2);
920 if (strcmp(cmd, "list") == 0)
922 List<CExtension *>::iterator iter;
924 unsigned int num = 1;
925 switch (m_Libs.size())
929 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
934 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
939 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
943 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
946 if (pExt->IsLoaded())
949 if (!pExt->IsRunning(error, sizeof(error)))
951 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
955 IExtensionInterface *pAPI = pExt->GetAPI();
956 const char *name = pAPI->GetExtensionName();
957 const char *version = pAPI->GetExtensionVerString();
958 const char *descr = pAPI->GetExtensionDescription();
959 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
962 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
967 else if (strcmp(cmd, "load") == 0)
971 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
975 const char *filename = command.Arg(3);
976 char path[PLATFORM_MAX_PATH];
979 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
980 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
982 if (FindExtensionByFile(path) != NULL)
984 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
988 if (LoadExtension(path, error, sizeof(error)))
990 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
993 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
998 else if (strcmp(cmd, "info") == 0)
1002 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1006 const char *sId = command.Arg(3);
1007 unsigned int id = atoi(sId);
1010 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1014 if (m_Libs.size() == 0)
1016 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1020 if (id > m_Libs.size())
1022 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1026 List<CExtension *>::iterator iter = m_Libs.begin();
1027 CExtension *pExt = NULL;
1028 while (iter != m_Libs.end())
1037 /* This should never happen */
1040 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1044 if (!pExt->IsLoaded())
1046 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1047 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1052 if (!pExt->IsRunning(error, sizeof(error)))
1054 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1055 g_RootMenu.ConsolePrint(" Loaded: Yes");
1056 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1060 IExtensionInterface *pAPI = pExt->GetAPI();
1061 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1062 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1063 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1064 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1065 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1067 if (pExt->IsExternal())
1069 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1071 else if (pAPI->IsMetamodExtension())
1073 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1077 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1083 else if (strcmp(cmd, "unload") == 0)
1087 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1091 const char *arg = command.Arg(3);
1092 unsigned int num = atoi(arg);
1093 CExtension *pExt = FindByOrder(num);
1097 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1101 if (argcount > 4 && pExt->unload_code)
1103 const char *unload = command.Arg(4);
1104 if (pExt->unload_code == (unsigned)atoi(unload))
1106 char filename[PLATFORM_MAX_PATH];
1107 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1108 UnloadExtension(pExt);
1109 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1113 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1118 if (!pExt->IsLoaded()
1119 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1121 char filename[PLATFORM_MAX_PATH];
1122 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1123 UnloadExtension(pExt);
1124 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1129 List<CPlugin *> plugins;
1130 if (pExt->m_ChildDeps.size())
1132 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1133 List<CExtension *>::iterator iter;
1135 /* Get list of all extensions */
1136 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1138 List<IfaceInfo>::iterator i_iter;
1140 if (!pOther->IsLoaded() || pOther == pExt)
1144 /* Get their dependencies */
1145 for (i_iter=pOther->m_Deps.begin();
1146 i_iter!=pOther->m_Deps.end();
1149 /* Is this dependency to us? */
1150 if ((*i_iter).owner != pExt)
1154 /* Will our dependent care? */
1155 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1157 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1158 /* Add to plugin unload list */
1159 List<CPlugin *>::iterator p_iter;
1160 for (p_iter=pOther->m_Dependents.begin();
1161 p_iter!=pOther->m_Dependents.end();
1164 if (plugins.find((*p_iter)) == plugins.end())
1166 plugins.push_back((*p_iter));
1173 if (pExt->m_Dependents.size())
1175 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1176 List<CPlugin *>::iterator iter;
1178 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1181 if (plugins.find(pPlugin) == plugins.end())
1183 plugins.push_back(pPlugin);
1186 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1189 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1192 srand(static_cast<int>(time(NULL)));
1193 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1194 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1195 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1200 else if (strcmp(cmd, "reload") == 0)
1204 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1208 const char *arg = command.Arg(3);
1209 unsigned int num = atoi(arg);
1210 CExtension *pExt = FindByOrder(num);
1214 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1218 if (pExt->IsLoaded())
1220 char filename[PLATFORM_MAX_PATH];
1223 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1225 if (pExt->Reload(error, sizeof(error)))
1227 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1231 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1238 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1246 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1247 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1248 g_RootMenu.DrawGenericOption("list", "List extensions");
1249 g_RootMenu.DrawGenericOption("load", "Load an extension");
1250 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1251 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1254 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1256 if (ptr->type == g_ExtType)
1258 return (CExtension *)(ptr->ptr);
1264 CExtensionManager::CExtensionManager()
1268 CExtensionManager::~CExtensionManager()
1272 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1274 CExtension *pExt = (CExtension *)pSource;
1275 pExt->AddLibrary(library);
1276 g_PluginSys.OnLibraryAction(library, false, false);
1279 bool CExtensionManager::LibraryExists(const char *library)
1283 for (List<CExtension *>::iterator iter = m_Libs.begin();
1284 iter != m_Libs.end();
1288 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1289 s_iter != pExt->m_Libraries.end();
1292 if ((*s_iter).compare(library) == 0)
1302 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1303 const char *filepath,
1304 const char *filename,
1308 IExtension *pAlready;
1309 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1314 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1316 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1323 m_Libs.push_back(pExt);
1328 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1330 IExtensionInterface *pAPI;
1331 List<CExtension *>::iterator iter;
1333 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1335 if ((pAPI = (*iter)->GetAPI()) == NULL)
1339 if (pAPI->GetExtensionVersion() > 3)
1341 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1346 bool CLocalExtension::IsSameFile(const char *file)
1348 /* Only care about the shortened name. */
1349 return strcmp(file, m_File.c_str()) == 0;
1352 bool CRemoteExtension::IsSameFile(const char *file)
1354 /* :TODO: this could be better, but no one uses this API anyway. */
1355 return strcmp(file, m_Path.c_str()) == 0;