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_ORANGEBOXVALVE
73 #define GAMEFIX "2.ep2v"
74 #elif SOURCE_ENGINE == SE_DARKMESSIAH
75 #define GAMEFIX "2.darkm"
77 #define GAMEFIX "2.ep1"
78 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
79 #else //METAMOD_PLAPI_VERSION
80 #define GAMEFIX "1.ep1"
81 #endif //METAMOD_PLAPI_VERSION
83 CLocalExtension::CLocalExtension(const char *filename)
88 char path[PLATFORM_MAX_PATH];
90 /* Special case for new bintools binary */
91 if (strcmp(filename, "bintools.ext") == 0)
96 /* Zeroth, see if there is an engine specific build in the new place. */
97 g_SourceMod.BuildPath(Path_SM,
100 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
103 if (g_LibSys.IsPathFile(path))
108 /* First see if there is an engine specific build! */
109 g_SourceMod.BuildPath(Path_SM,
112 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
116 /* Try the "normal" version */
117 if (!g_LibSys.IsPathFile(path))
119 g_SourceMod.BuildPath(Path_SM,
122 "extensions/%s." PLATFORM_LIB_EXT,
127 Initialize(filename, path);
130 bool CRemoteExtension::Load(char *error, size_t maxlength)
132 if (!PerformAPICheck(error, maxlength))
138 if (!CExtension::Load(error, maxlength))
147 bool CLocalExtension::Load(char *error, size_t maxlength)
149 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
156 typedef IExtensionInterface *(*GETAPI)();
157 GETAPI pfnGetAPI = NULL;
159 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
161 m_pLib->CloseLibrary();
163 snprintf(error, maxlength, "Unable to find extension entry point");
167 m_pAPI = pfnGetAPI();
169 /* Check pointer and version */
170 if (!PerformAPICheck(error, maxlength))
172 m_pLib->CloseLibrary();
179 if (m_pAPI->IsMetamodExtension())
182 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
185 if (!CExtension::Load(error, maxlength))
187 if (m_pAPI->IsMetamodExtension())
192 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
196 m_pLib->CloseLibrary();
205 void CRemoteExtension::Unload()
209 void CLocalExtension::Unload()
211 if (m_pAPI != NULL && m_PlId)
213 char temp_buffer[255];
214 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
220 m_pLib->CloseLibrary();
225 bool CRemoteExtension::Reload(char *error, size_t maxlength)
227 snprintf(error, maxlength, "Remote extensions do not support reloading");
231 bool CLocalExtension::Reload(char *error, size_t maxlength)
233 if (m_pLib == NULL) // FIXME: just load it instead?
236 m_pAPI->OnExtensionUnload();
239 return Load(error, maxlength);
242 bool CRemoteExtension::IsExternal()
247 bool CLocalExtension::IsExternal()
252 CExtension::~CExtension()
257 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
261 snprintf(error, maxlength, "No IExtensionInterface instance provided");
265 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
267 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
274 bool CExtension::Load(char *error, size_t maxlength)
277 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
284 /* Check if we're past load time */
285 if (!g_SourceMod.IsMapLoading())
287 m_pAPI->OnExtensionsAllLoaded();
294 void CExtension::CreateIdentity()
296 if (m_pIdentToken != NULL)
301 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
304 void CExtension::DestroyIdentity()
306 if (m_pIdentToken == NULL)
311 g_ShareSys.DestroyIdentity(m_pIdentToken);
312 m_pIdentToken = NULL;
315 void CExtension::MarkAllLoaded()
317 assert(m_bFullyLoaded == false);
320 m_bFullyLoaded = true;
321 m_pAPI->OnExtensionsAllLoaded();
325 void CExtension::AddPlugin(CPlugin *pPlugin)
327 /* Unfortunately we have to do this :( */
328 if (m_Dependents.find(pPlugin) != m_Dependents.end())
330 m_Dependents.push_back(pPlugin);
334 void CExtension::SetError(const char *error)
336 m_Error.assign(error);
339 IExtensionInterface *CExtension::GetAPI()
344 const char *CExtension::GetFilename()
346 return m_RealFile.c_str();
349 IdentityToken_t *CExtension::GetIdentity()
351 return m_pIdentToken;
354 bool CRemoteExtension::IsLoaded()
356 return (m_pAPI != NULL);
359 bool CLocalExtension::IsLoaded()
361 return (m_pLib != NULL);
364 void CExtension::AddDependency(const IfaceInfo *pInfo)
366 if (m_Deps.find(*pInfo) == m_Deps.end())
368 m_Deps.push_back(*pInfo);
372 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
374 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
377 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
383 List<IfaceInfo>::iterator iter;
384 for (iter = m_ChildDeps.begin();
385 iter != m_ChildDeps.end();
388 IfaceInfo &other = (*iter);
395 m_ChildDeps.push_back(info);
398 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
400 List<IfaceInfo>::iterator iter = m_Deps.begin();
402 if (iter == m_Deps.end())
409 *pOwner = (*iter).owner;
413 *pInterface = (*iter).iface;
416 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
418 return (ITERATOR *)pIter;
421 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
423 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
424 List<IfaceInfo>::iterator _iter;
426 if (_iter == m_Deps.end())
435 *pOwner = (*_iter).owner;
439 *pInterface = (*_iter).iface;
444 if (_iter == m_Deps.end())
452 void CExtension::FreeDependencyIterator(ITERATOR *iter)
454 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
459 void CExtension::AddInterface(SMInterface *pInterface)
461 m_Interfaces.push_back(pInterface);
464 bool CExtension::IsRunning(char *error, size_t maxlength)
470 snprintf(error, maxlength, "%s", m_Error.c_str());
475 return m_pAPI->QueryRunning(error, maxlength);
478 void CExtension::AddLibrary(const char *library)
480 m_Libraries.push_back(library);
483 /*********************
484 * EXTENSION MANAGER *
485 *********************/
487 void CExtensionManager::OnSourceModAllInitialized()
489 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
490 g_PluginSys.AddPluginsListener(this);
491 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
492 g_ShareSys.AddInterface(NULL, this);
495 void CExtensionManager::OnSourceModShutdown()
497 g_RootMenu.RemoveRootConsoleCommand("exts", this);
498 g_ShareSys.DestroyIdentType(g_ExtType);
501 void CExtensionManager::Shutdown()
503 List<CExtension *>::iterator iter;
505 while ((iter = m_Libs.begin()) != m_Libs.end())
507 UnloadExtension((*iter));
511 void CExtensionManager::TryAutoload()
513 char path[PLATFORM_MAX_PATH];
515 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
517 IDirectory *pDir = g_LibSys.OpenDirectory(path);
525 while (pDir->MoreFiles())
527 if (pDir->IsEntryDirectory())
533 lfile = pDir->GetEntryName();
535 if (len <= 9) /* size of ".autoload" */
541 if (strcmp(&lfile[len - 9], ".autoload") != 0)
547 char file[PLATFORM_MAX_PATH];
548 len = UTIL_Format(file, sizeof(file), "%s", lfile);
549 strcpy(&file[len - 9], ".ext");
551 LoadAutoExtension(file);
557 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
559 /* Remove platform extension if it's there. Compat hack. */
560 const char *ext = g_LibSys.GetFileExtension(path);
561 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
563 char path2[PLATFORM_MAX_PATH];
564 UTIL_Format(path2, sizeof(path2), "%s", path);
565 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
566 return LoadAutoExtension(path2);
569 IExtension *pAlready;
570 if ((pAlready=FindExtensionByFile(path)) != NULL)
576 CExtension *p = new CLocalExtension(path);
578 /* We put us in the list beforehand so extensions that check for each other
579 * won't recursively load each other.
583 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
585 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
592 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
594 List<CExtension *>::iterator iter;
597 /* Chomp off the path */
598 char lookup[PLATFORM_MAX_PATH];
599 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
601 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
604 if (pExt->IsSameFile(lookup))
613 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
615 List<CExtension *>::iterator iter;
617 IExtensionInterface *pAPI;
620 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
623 if (!pExt->IsLoaded())
627 if ((pAPI = pExt->GetAPI()) == NULL)
631 name = pAPI->GetExtensionName();
636 if (strcmp(name, ext) == 0)
645 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
647 /* Remove platform extension if it's there. Compat hack. */
648 const char *ext = g_LibSys.GetFileExtension(file);
649 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
651 char path2[PLATFORM_MAX_PATH];
652 UTIL_Format(path2, sizeof(path2), "%s", file);
653 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
654 return LoadExtension(path2, error, maxlength);
657 IExtension *pAlready;
658 if ((pAlready=FindExtensionByFile(file)) != NULL)
663 CExtension *pExt = new CLocalExtension(file);
665 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
672 /* :TODO: do QueryRunning check if the map is loaded */
674 m_Libs.push_back(pExt);
679 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
681 CExtension *pExt = (CExtension *)pRequester;
682 CExtension *pOwner = (CExtension *)pInfo->owner;
684 pExt->AddDependency(pInfo);
686 IExtensionInterface *pAPI = pExt->GetAPI();
687 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
689 pOwner->AddChildDependent(pExt, pInfo->iface);
693 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
695 CExtension *pExt = (CExtension *)ext;
696 CExtension *pOwner = GetExtensionFromIdent(other);
700 info.iface = (SMInterface *)iface;
703 pExt->AddDependency(&info);
704 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
707 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
709 CExtension *pExt = (CExtension *)pOwner;
711 pExt->AddInterface(pInterface);
714 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
716 CExtension *pExt = (CExtension *)pParent;
718 pExt->AddPlugin(pPlugin);
721 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
723 List<CExtension *>::iterator iter;
725 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
727 (*iter)->DropRefsTo((CPlugin *)plugin);
731 CExtension *CExtensionManager::FindByOrder(unsigned int num)
733 if (num < 1 || num > m_Libs.size())
738 List<CExtension *>::iterator iter = m_Libs.begin();
740 while (iter != m_Libs.end())
752 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
759 CExtension *pExt = (CExtension *)_pExt;
761 if (m_Libs.find(pExt) == m_Libs.end())
766 /* First remove us from internal lists */
767 g_ShareSys.RemoveInterfaces(_pExt);
770 List<CExtension *> UnloadQueue;
772 /* Handle dependencies */
773 if (pExt->IsLoaded())
775 /* Unload any dependent plugins */
776 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
777 while (p_iter != pExt->m_Dependents.end())
779 /* We have to manually unlink ourselves here, since we're no longer being managed */
780 g_PluginSys.UnloadPlugin((*p_iter));
781 p_iter = pExt->m_Dependents.erase(p_iter);
784 List<String>::iterator s_iter;
785 for (s_iter = pExt->m_Libraries.begin();
786 s_iter != pExt->m_Libraries.end();
789 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
792 /* Notify and/or unload all dependencies */
793 List<CExtension *>::iterator c_iter;
795 IExtensionInterface *pAPI;
796 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
799 if ((pAPI=pDep->GetAPI()) == NULL)
807 /* Now, get its dependency list */
808 bool dropped = false;
809 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
810 while (i_iter != pDep->m_Deps.end())
812 if ((*i_iter).owner == _pExt)
814 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
819 UnloadQueue.push_back(pDep);
822 pAPI->NotifyInterfaceDrop((*i_iter).iface);
823 i_iter = pDep->m_Deps.erase(i_iter);
830 /* Flush out any back references to this plugin */
831 i_iter = pDep->m_ChildDeps.begin();
832 while (i_iter != pDep->m_ChildDeps.end())
834 if ((*i_iter).owner == pExt)
836 i_iter = pDep->m_ChildDeps.erase(i_iter);
845 /* Unbind our natives from Core */
846 pExt->DropEverything();
849 IdentityToken_t *pIdentity;
850 if ((pIdentity = pExt->GetIdentity()) != NULL)
852 SMGlobalClass *glob = SMGlobalClass::head;
855 glob->OnSourceModIdentityDropped(pIdentity);
856 glob = glob->m_pGlobalClassNext;
860 /* Tell it to unload */
861 if (pExt->IsLoaded())
863 IExtensionInterface *pAPI = pExt->GetAPI();
864 pAPI->OnExtensionUnload();
870 List<CExtension *>::iterator iter;
871 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
873 /* NOTE: This is safe because the unload function backs out of anything not present */
874 UnloadExtension((*iter));
880 void CExtensionManager::MarkAllLoaded()
882 List<CExtension *>::iterator iter;
885 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
888 if (!pExt->IsLoaded())
892 if (pExt->m_bFullyLoaded)
896 pExt->MarkAllLoaded();
900 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
902 /* This function doesn't really need to do anything now. We make sure the
903 * other extension is loaded, but handling of dependencies is really done
904 * by the interface fetcher.
906 if (required || autoload)
908 LoadAutoExtension(file);
912 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
914 int argcount = command.ArgC();
917 const char *cmd = command.Arg(2);
918 if (strcmp(cmd, "list") == 0)
920 List<CExtension *>::iterator iter;
922 unsigned int num = 1;
923 switch (m_Libs.size())
927 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
932 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
937 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
941 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
944 if (pExt->IsLoaded())
947 if (!pExt->IsRunning(error, sizeof(error)))
949 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
953 IExtensionInterface *pAPI = pExt->GetAPI();
954 const char *name = pAPI->GetExtensionName();
955 const char *version = pAPI->GetExtensionVerString();
956 const char *descr = pAPI->GetExtensionDescription();
957 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
960 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
965 else if (strcmp(cmd, "load") == 0)
969 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
973 const char *filename = command.Arg(3);
974 char path[PLATFORM_MAX_PATH];
977 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
978 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
980 if (FindExtensionByFile(path) != NULL)
982 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
986 if (LoadExtension(path, error, sizeof(error)))
988 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
991 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
996 else if (strcmp(cmd, "info") == 0)
1000 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1004 const char *sId = command.Arg(3);
1005 unsigned int id = atoi(sId);
1008 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1012 if (m_Libs.size() == 0)
1014 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1018 if (id > m_Libs.size())
1020 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1024 List<CExtension *>::iterator iter = m_Libs.begin();
1025 CExtension *pExt = NULL;
1026 while (iter != m_Libs.end())
1035 /* This should never happen */
1038 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1042 if (!pExt->IsLoaded())
1044 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1045 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1050 if (!pExt->IsRunning(error, sizeof(error)))
1052 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1053 g_RootMenu.ConsolePrint(" Loaded: Yes");
1054 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1058 IExtensionInterface *pAPI = pExt->GetAPI();
1059 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1060 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1061 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1062 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1063 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1065 if (pExt->IsExternal())
1067 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1069 else if (pAPI->IsMetamodExtension())
1071 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1075 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1081 else if (strcmp(cmd, "unload") == 0)
1085 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1089 const char *arg = command.Arg(3);
1090 unsigned int num = atoi(arg);
1091 CExtension *pExt = FindByOrder(num);
1095 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1099 if (argcount > 4 && pExt->unload_code)
1101 const char *unload = command.Arg(4);
1102 if (pExt->unload_code == (unsigned)atoi(unload))
1104 char filename[PLATFORM_MAX_PATH];
1105 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1106 UnloadExtension(pExt);
1107 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1111 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1116 if (!pExt->IsLoaded()
1117 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1119 char filename[PLATFORM_MAX_PATH];
1120 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1121 UnloadExtension(pExt);
1122 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1127 List<CPlugin *> plugins;
1128 if (pExt->m_ChildDeps.size())
1130 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1131 List<CExtension *>::iterator iter;
1133 /* Get list of all extensions */
1134 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1136 List<IfaceInfo>::iterator i_iter;
1138 if (!pOther->IsLoaded() || pOther == pExt)
1142 /* Get their dependencies */
1143 for (i_iter=pOther->m_Deps.begin();
1144 i_iter!=pOther->m_Deps.end();
1147 /* Is this dependency to us? */
1148 if ((*i_iter).owner != pExt)
1152 /* Will our dependent care? */
1153 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1155 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1156 /* Add to plugin unload list */
1157 List<CPlugin *>::iterator p_iter;
1158 for (p_iter=pOther->m_Dependents.begin();
1159 p_iter!=pOther->m_Dependents.end();
1162 if (plugins.find((*p_iter)) == plugins.end())
1164 plugins.push_back((*p_iter));
1171 if (pExt->m_Dependents.size())
1173 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1174 List<CPlugin *>::iterator iter;
1176 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1179 if (plugins.find(pPlugin) == plugins.end())
1181 plugins.push_back(pPlugin);
1184 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1187 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1190 srand(static_cast<int>(time(NULL)));
1191 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1192 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1193 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1198 else if (strcmp(cmd, "reload") == 0)
1202 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1206 const char *arg = command.Arg(3);
1207 unsigned int num = atoi(arg);
1208 CExtension *pExt = FindByOrder(num);
1212 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1216 if (pExt->IsLoaded())
1218 char filename[PLATFORM_MAX_PATH];
1221 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1223 if (pExt->Reload(error, sizeof(error)))
1225 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1229 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1236 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1244 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1245 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1246 g_RootMenu.DrawGenericOption("list", "List extensions");
1247 g_RootMenu.DrawGenericOption("load", "Load an extension");
1248 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1249 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1252 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1254 if (ptr->type == g_ExtType)
1256 return (CExtension *)(ptr->ptr);
1262 CExtensionManager::CExtensionManager()
1266 CExtensionManager::~CExtensionManager()
1270 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1272 CExtension *pExt = (CExtension *)pSource;
1273 pExt->AddLibrary(library);
1274 g_PluginSys.OnLibraryAction(library, false, false);
1277 bool CExtensionManager::LibraryExists(const char *library)
1281 for (List<CExtension *>::iterator iter = m_Libs.begin();
1282 iter != m_Libs.end();
1286 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1287 s_iter != pExt->m_Libraries.end();
1290 if ((*s_iter).compare(library) == 0)
1300 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1301 const char *filepath,
1302 const char *filename,
1306 IExtension *pAlready;
1307 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1312 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1314 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1321 m_Libs.push_back(pExt);
1326 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1328 IExtensionInterface *pAPI;
1329 List<CExtension *>::iterator iter;
1331 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1333 if ((pAPI = (*iter)->GetAPI()) == NULL)
1337 if (pAPI->GetExtensionVersion() > 3)
1339 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1344 bool CLocalExtension::IsSameFile(const char *file)
1346 /* Only care about the shortened name. */
1347 return strcmp(file, m_File.c_str()) == 0;
1350 bool CRemoteExtension::IsSameFile(const char *file)
1352 /* :TODO: this could be better, but no one uses this API anyway. */
1353 return strcmp(file, m_Path.c_str()) == 0;