2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2009 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_ORANGEBOX
69 #define GAMEFIX "2.ep2"
70 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
71 #define GAMEFIX "2.ep2v"
72 #elif SOURCE_ENGINE == SE_DARKMESSIAH
73 #define GAMEFIX "2.darkm"
75 #define GAMEFIX "2.ep1"
76 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
77 #else //METAMOD_PLAPI_VERSION
78 #define GAMEFIX "1.ep1"
79 #endif //METAMOD_PLAPI_VERSION
81 CLocalExtension::CLocalExtension(const char *filename)
86 char path[PLATFORM_MAX_PATH];
88 /* Zeroth, see if there is an engine specific build in the new place. */
89 g_SourceMod.BuildPath(Path_SM,
92 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
95 if (g_LibSys.IsPathFile(path))
100 /* First see if there is an engine specific build! */
101 g_SourceMod.BuildPath(Path_SM,
104 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
107 /* Try the "normal" version */
108 if (!g_LibSys.IsPathFile(path))
110 g_SourceMod.BuildPath(Path_SM,
113 "extensions/%s." PLATFORM_LIB_EXT,
118 Initialize(filename, path);
121 bool CRemoteExtension::Load(char *error, size_t maxlength)
123 if (!PerformAPICheck(error, maxlength))
129 if (!CExtension::Load(error, maxlength))
138 bool CLocalExtension::Load(char *error, size_t maxlength)
140 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
147 typedef IExtensionInterface *(*GETAPI)();
148 GETAPI pfnGetAPI = NULL;
150 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
152 m_pLib->CloseLibrary();
154 snprintf(error, maxlength, "Unable to find extension entry point");
158 m_pAPI = pfnGetAPI();
160 /* Check pointer and version */
161 if (!PerformAPICheck(error, maxlength))
163 m_pLib->CloseLibrary();
170 if (m_pAPI->IsMetamodExtension())
173 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
176 if (!CExtension::Load(error, maxlength))
178 if (m_pAPI->IsMetamodExtension())
183 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
187 m_pLib->CloseLibrary();
196 void CRemoteExtension::Unload()
200 void CLocalExtension::Unload()
202 if (m_pAPI != NULL && m_PlId)
204 char temp_buffer[255];
205 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
211 m_pLib->CloseLibrary();
216 bool CRemoteExtension::Reload(char *error, size_t maxlength)
218 snprintf(error, maxlength, "Remote extensions do not support reloading");
222 bool CLocalExtension::Reload(char *error, size_t maxlength)
224 if (m_pLib == NULL) // FIXME: just load it instead?
227 m_pAPI->OnExtensionUnload();
230 return Load(error, maxlength);
233 bool CRemoteExtension::IsExternal()
238 bool CLocalExtension::IsExternal()
243 CExtension::~CExtension()
248 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
252 snprintf(error, maxlength, "No IExtensionInterface instance provided");
256 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
258 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
265 bool CExtension::Load(char *error, size_t maxlength)
268 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
275 /* Check if we're past load time */
276 if (!g_SourceMod.IsMapLoading())
278 m_pAPI->OnExtensionsAllLoaded();
285 void CExtension::CreateIdentity()
287 if (m_pIdentToken != NULL)
292 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
295 void CExtension::DestroyIdentity()
297 if (m_pIdentToken == NULL)
302 g_ShareSys.DestroyIdentity(m_pIdentToken);
303 m_pIdentToken = NULL;
306 void CExtension::MarkAllLoaded()
308 assert(m_bFullyLoaded == false);
311 m_bFullyLoaded = true;
312 m_pAPI->OnExtensionsAllLoaded();
316 void CExtension::AddPlugin(CPlugin *pPlugin)
318 /* Unfortunately we have to do this :( */
319 if (m_Dependents.find(pPlugin) != m_Dependents.end())
321 m_Dependents.push_back(pPlugin);
325 void CExtension::SetError(const char *error)
327 m_Error.assign(error);
330 IExtensionInterface *CExtension::GetAPI()
335 const char *CExtension::GetFilename()
337 return m_RealFile.c_str();
340 IdentityToken_t *CExtension::GetIdentity()
342 return m_pIdentToken;
345 bool CRemoteExtension::IsLoaded()
347 return (m_pAPI != NULL);
350 bool CLocalExtension::IsLoaded()
352 return (m_pLib != NULL);
355 void CExtension::AddDependency(const IfaceInfo *pInfo)
357 if (m_Deps.find(*pInfo) == m_Deps.end())
359 m_Deps.push_back(*pInfo);
363 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
365 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
368 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
374 List<IfaceInfo>::iterator iter;
375 for (iter = m_ChildDeps.begin();
376 iter != m_ChildDeps.end();
379 IfaceInfo &other = (*iter);
386 m_ChildDeps.push_back(info);
389 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
391 List<IfaceInfo>::iterator iter = m_Deps.begin();
393 if (iter == m_Deps.end())
400 *pOwner = (*iter).owner;
404 *pInterface = (*iter).iface;
407 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
409 return (ITERATOR *)pIter;
412 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
414 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
415 List<IfaceInfo>::iterator _iter;
417 if (_iter == m_Deps.end())
426 *pOwner = (*_iter).owner;
430 *pInterface = (*_iter).iface;
435 if (_iter == m_Deps.end())
443 void CExtension::FreeDependencyIterator(ITERATOR *iter)
445 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
450 void CExtension::AddInterface(SMInterface *pInterface)
452 m_Interfaces.push_back(pInterface);
455 bool CExtension::IsRunning(char *error, size_t maxlength)
461 snprintf(error, maxlength, "%s", m_Error.c_str());
466 return m_pAPI->QueryRunning(error, maxlength);
469 void CExtension::AddLibrary(const char *library)
471 m_Libraries.push_back(library);
474 /*********************
475 * EXTENSION MANAGER *
476 *********************/
478 void CExtensionManager::OnSourceModAllInitialized()
480 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
481 g_PluginSys.AddPluginsListener(this);
482 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
483 g_ShareSys.AddInterface(NULL, this);
486 void CExtensionManager::OnSourceModShutdown()
488 g_RootMenu.RemoveRootConsoleCommand("exts", this);
489 g_ShareSys.DestroyIdentType(g_ExtType);
492 void CExtensionManager::Shutdown()
494 List<CExtension *>::iterator iter;
496 while ((iter = m_Libs.begin()) != m_Libs.end())
498 UnloadExtension((*iter));
502 void CExtensionManager::TryAutoload()
504 char path[PLATFORM_MAX_PATH];
506 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
508 IDirectory *pDir = g_LibSys.OpenDirectory(path);
516 while (pDir->MoreFiles())
518 if (pDir->IsEntryDirectory())
524 lfile = pDir->GetEntryName();
526 if (len <= 9) /* size of ".autoload" */
532 if (strcmp(&lfile[len - 9], ".autoload") != 0)
538 char file[PLATFORM_MAX_PATH];
539 len = UTIL_Format(file, sizeof(file), "%s", lfile);
540 strcpy(&file[len - 9], ".ext");
542 LoadAutoExtension(file);
548 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
550 /* Remove platform extension if it's there. Compat hack. */
551 const char *ext = g_LibSys.GetFileExtension(path);
552 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
554 char path2[PLATFORM_MAX_PATH];
555 UTIL_Format(path2, sizeof(path2), "%s", path);
556 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
557 return LoadAutoExtension(path2);
560 IExtension *pAlready;
561 if ((pAlready=FindExtensionByFile(path)) != NULL)
567 CExtension *p = new CLocalExtension(path);
569 /* We put us in the list beforehand so extensions that check for each other
570 * won't recursively load each other.
574 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
576 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
583 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
585 List<CExtension *>::iterator iter;
588 /* Chomp off the path */
589 char lookup[PLATFORM_MAX_PATH];
590 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
592 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
595 if (pExt->IsSameFile(lookup))
604 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
606 List<CExtension *>::iterator iter;
608 IExtensionInterface *pAPI;
611 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
614 if (!pExt->IsLoaded())
618 if ((pAPI = pExt->GetAPI()) == NULL)
622 name = pAPI->GetExtensionName();
627 if (strcmp(name, ext) == 0)
636 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
638 /* Remove platform extension if it's there. Compat hack. */
639 const char *ext = g_LibSys.GetFileExtension(file);
640 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
642 char path2[PLATFORM_MAX_PATH];
643 UTIL_Format(path2, sizeof(path2), "%s", file);
644 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
645 return LoadExtension(path2, error, maxlength);
648 IExtension *pAlready;
649 if ((pAlready=FindExtensionByFile(file)) != NULL)
654 CExtension *pExt = new CLocalExtension(file);
656 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
663 /* :TODO: do QueryRunning check if the map is loaded */
665 m_Libs.push_back(pExt);
670 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
672 CExtension *pExt = (CExtension *)pRequester;
673 CExtension *pOwner = (CExtension *)pInfo->owner;
675 pExt->AddDependency(pInfo);
677 IExtensionInterface *pAPI = pExt->GetAPI();
678 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
680 pOwner->AddChildDependent(pExt, pInfo->iface);
684 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
686 CExtension *pExt = (CExtension *)ext;
687 CExtension *pOwner = GetExtensionFromIdent(other);
691 info.iface = (SMInterface *)iface;
694 pExt->AddDependency(&info);
695 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
698 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
700 CExtension *pExt = (CExtension *)pOwner;
702 pExt->AddInterface(pInterface);
705 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
707 CExtension *pExt = (CExtension *)pParent;
709 pExt->AddPlugin(pPlugin);
712 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
714 List<CExtension *>::iterator iter;
716 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
718 (*iter)->DropRefsTo((CPlugin *)plugin);
722 CExtension *CExtensionManager::FindByOrder(unsigned int num)
724 if (num < 1 || num > m_Libs.size())
729 List<CExtension *>::iterator iter = m_Libs.begin();
731 while (iter != m_Libs.end())
743 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
750 CExtension *pExt = (CExtension *)_pExt;
752 if (m_Libs.find(pExt) == m_Libs.end())
757 /* First remove us from internal lists */
758 g_ShareSys.RemoveInterfaces(_pExt);
761 List<CExtension *> UnloadQueue;
763 /* Handle dependencies */
764 if (pExt->IsLoaded())
766 /* Unload any dependent plugins */
767 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
768 while (p_iter != pExt->m_Dependents.end())
770 /* We have to manually unlink ourselves here, since we're no longer being managed */
771 g_PluginSys.UnloadPlugin((*p_iter));
772 p_iter = pExt->m_Dependents.erase(p_iter);
775 List<String>::iterator s_iter;
776 for (s_iter = pExt->m_Libraries.begin();
777 s_iter != pExt->m_Libraries.end();
780 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
783 /* Notify and/or unload all dependencies */
784 List<CExtension *>::iterator c_iter;
786 IExtensionInterface *pAPI;
787 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
790 if ((pAPI=pDep->GetAPI()) == NULL)
798 /* Now, get its dependency list */
799 bool dropped = false;
800 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
801 while (i_iter != pDep->m_Deps.end())
803 if ((*i_iter).owner == _pExt)
805 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
810 UnloadQueue.push_back(pDep);
813 pAPI->NotifyInterfaceDrop((*i_iter).iface);
814 i_iter = pDep->m_Deps.erase(i_iter);
821 /* Flush out any back references to this plugin */
822 i_iter = pDep->m_ChildDeps.begin();
823 while (i_iter != pDep->m_ChildDeps.end())
825 if ((*i_iter).owner == pExt)
827 i_iter = pDep->m_ChildDeps.erase(i_iter);
836 /* Unbind our natives from Core */
837 pExt->DropEverything();
840 IdentityToken_t *pIdentity;
841 if ((pIdentity = pExt->GetIdentity()) != NULL)
843 SMGlobalClass *glob = SMGlobalClass::head;
846 glob->OnSourceModIdentityDropped(pIdentity);
847 glob = glob->m_pGlobalClassNext;
851 /* Tell it to unload */
852 if (pExt->IsLoaded())
854 IExtensionInterface *pAPI = pExt->GetAPI();
855 pAPI->OnExtensionUnload();
861 List<CExtension *>::iterator iter;
862 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
864 /* NOTE: This is safe because the unload function backs out of anything not present */
865 UnloadExtension((*iter));
871 void CExtensionManager::MarkAllLoaded()
873 List<CExtension *>::iterator iter;
876 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
879 if (!pExt->IsLoaded())
883 if (pExt->m_bFullyLoaded)
887 pExt->MarkAllLoaded();
891 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
893 /* This function doesn't really need to do anything now. We make sure the
894 * other extension is loaded, but handling of dependencies is really done
895 * by the interface fetcher.
897 if (required || autoload)
899 LoadAutoExtension(file);
903 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
905 int argcount = command.ArgC();
908 const char *cmd = command.Arg(2);
909 if (strcmp(cmd, "list") == 0)
911 List<CExtension *>::iterator iter;
913 unsigned int num = 1;
914 switch (m_Libs.size())
918 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
923 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
928 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
932 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
935 if (pExt->IsLoaded())
938 if (!pExt->IsRunning(error, sizeof(error)))
940 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
944 IExtensionInterface *pAPI = pExt->GetAPI();
945 const char *name = pAPI->GetExtensionName();
946 const char *version = pAPI->GetExtensionVerString();
947 const char *descr = pAPI->GetExtensionDescription();
948 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
951 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
956 else if (strcmp(cmd, "load") == 0)
960 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
964 const char *filename = command.Arg(3);
965 char path[PLATFORM_MAX_PATH];
968 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
969 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
971 if (FindExtensionByFile(path) != NULL)
973 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
977 if (LoadExtension(path, error, sizeof(error)))
979 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
982 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
987 else if (strcmp(cmd, "info") == 0)
991 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
995 const char *sId = command.Arg(3);
996 unsigned int id = atoi(sId);
999 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
1003 if (m_Libs.size() == 0)
1005 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1009 if (id > m_Libs.size())
1011 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1015 List<CExtension *>::iterator iter = m_Libs.begin();
1016 CExtension *pExt = NULL;
1017 while (iter != m_Libs.end())
1026 /* This should never happen */
1029 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1033 if (!pExt->IsLoaded())
1035 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1036 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1041 if (!pExt->IsRunning(error, sizeof(error)))
1043 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1044 g_RootMenu.ConsolePrint(" Loaded: Yes");
1045 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1049 IExtensionInterface *pAPI = pExt->GetAPI();
1050 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1051 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1052 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1053 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1054 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1056 if (pExt->IsExternal())
1058 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1060 else if (pAPI->IsMetamodExtension())
1062 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1066 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1072 else if (strcmp(cmd, "unload") == 0)
1076 g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
1080 const char *arg = command.Arg(3);
1081 unsigned int num = atoi(arg);
1082 CExtension *pExt = FindByOrder(num);
1086 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1090 if (argcount > 4 && pExt->unload_code)
1092 const char *unload = command.Arg(4);
1093 if (pExt->unload_code == (unsigned)atoi(unload))
1095 char filename[PLATFORM_MAX_PATH];
1096 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1097 UnloadExtension(pExt);
1098 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1102 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1107 if (!pExt->IsLoaded()
1108 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1110 char filename[PLATFORM_MAX_PATH];
1111 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1112 UnloadExtension(pExt);
1113 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1118 List<CPlugin *> plugins;
1119 if (pExt->m_ChildDeps.size())
1121 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1122 List<CExtension *>::iterator iter;
1124 /* Get list of all extensions */
1125 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1127 List<IfaceInfo>::iterator i_iter;
1129 if (!pOther->IsLoaded() || pOther == pExt)
1133 /* Get their dependencies */
1134 for (i_iter=pOther->m_Deps.begin();
1135 i_iter!=pOther->m_Deps.end();
1138 /* Is this dependency to us? */
1139 if ((*i_iter).owner != pExt)
1143 /* Will our dependent care? */
1144 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1146 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1147 /* Add to plugin unload list */
1148 List<CPlugin *>::iterator p_iter;
1149 for (p_iter=pOther->m_Dependents.begin();
1150 p_iter!=pOther->m_Dependents.end();
1153 if (plugins.find((*p_iter)) == plugins.end())
1155 plugins.push_back((*p_iter));
1162 if (pExt->m_Dependents.size())
1164 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1165 List<CPlugin *>::iterator iter;
1167 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1170 if (plugins.find(pPlugin) == plugins.end())
1172 plugins.push_back(pPlugin);
1175 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1178 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1181 srand(static_cast<int>(time(NULL)));
1182 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1183 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1184 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1189 else if (strcmp(cmd, "reload") == 0)
1193 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1197 const char *arg = command.Arg(3);
1198 unsigned int num = atoi(arg);
1199 CExtension *pExt = FindByOrder(num);
1203 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1207 if (pExt->IsLoaded())
1209 char filename[PLATFORM_MAX_PATH];
1212 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1214 if (pExt->Reload(error, sizeof(error)))
1216 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1220 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1227 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1235 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1236 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1237 g_RootMenu.DrawGenericOption("list", "List extensions");
1238 g_RootMenu.DrawGenericOption("load", "Load an extension");
1239 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1240 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1243 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1245 if (ptr->type == g_ExtType)
1247 return (CExtension *)(ptr->ptr);
1253 CExtensionManager::CExtensionManager()
1257 CExtensionManager::~CExtensionManager()
1261 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1263 CExtension *pExt = (CExtension *)pSource;
1264 pExt->AddLibrary(library);
1265 g_PluginSys.OnLibraryAction(library, false, false);
1268 bool CExtensionManager::LibraryExists(const char *library)
1272 for (List<CExtension *>::iterator iter = m_Libs.begin();
1273 iter != m_Libs.end();
1277 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1278 s_iter != pExt->m_Libraries.end();
1281 if ((*s_iter).compare(library) == 0)
1291 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1292 const char *filepath,
1293 const char *filename,
1297 IExtension *pAlready;
1298 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1303 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1305 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1312 m_Libs.push_back(pExt);
1317 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1319 IExtensionInterface *pAPI;
1320 List<CExtension *>::iterator iter;
1322 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1324 if ((pAPI = (*iter)->GetAPI()) == NULL)
1328 if (pAPI->GetExtensionVersion() > 3)
1330 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1335 bool CLocalExtension::IsSameFile(const char *file)
1337 /* Only care about the shortened name. */
1338 return strcmp(file, m_File.c_str()) == 0;
1341 bool CRemoteExtension::IsSameFile(const char *file)
1343 /* :TODO: this could be better, but no one uses this API anyway. */
1344 return strcmp(file, m_Path.c_str()) == 0;