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_EYE
75 #define GAMEFIX "2.eye"
76 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
77 #define GAMEFIX "2.ep2v"
78 #elif SOURCE_ENGINE == SE_DARKMESSIAH
79 #define GAMEFIX "2.darkm"
81 #define GAMEFIX "2.ep1"
82 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
83 #else //METAMOD_PLAPI_VERSION
84 #define GAMEFIX "1.ep1"
85 #endif //METAMOD_PLAPI_VERSION
87 CLocalExtension::CLocalExtension(const char *filename)
92 char path[PLATFORM_MAX_PATH];
94 /* Special case for new bintools binary */
95 if (strcmp(filename, "bintools.ext") == 0)
100 /* Zeroth, see if there is an engine specific build in the new place. */
101 g_SourceMod.BuildPath(Path_SM,
104 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
107 if (g_LibSys.IsPathFile(path))
112 /* First see if there is an engine specific build! */
113 g_SourceMod.BuildPath(Path_SM,
116 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
120 /* Try the "normal" version */
121 if (!g_LibSys.IsPathFile(path))
123 g_SourceMod.BuildPath(Path_SM,
126 "extensions/%s." PLATFORM_LIB_EXT,
131 Initialize(filename, path);
134 bool CRemoteExtension::Load(char *error, size_t maxlength)
136 if (!PerformAPICheck(error, maxlength))
142 if (!CExtension::Load(error, maxlength))
151 bool CLocalExtension::Load(char *error, size_t maxlength)
153 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
160 typedef IExtensionInterface *(*GETAPI)();
161 GETAPI pfnGetAPI = NULL;
163 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
165 m_pLib->CloseLibrary();
167 snprintf(error, maxlength, "Unable to find extension entry point");
171 m_pAPI = pfnGetAPI();
173 /* Check pointer and version */
174 if (!PerformAPICheck(error, maxlength))
176 m_pLib->CloseLibrary();
183 if (m_pAPI->IsMetamodExtension())
186 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
188 // Check the plugin didn't refuse load
191 #ifndef METAMOD_PLAPI_VERSION
197 #ifndef METAMOD_PLAPI_VERSION
198 g_pMMPlugins->Query(m_PlId, file, status, source)
200 g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
202 && status < Pl_Paused))
204 m_pLib->CloseLibrary();
211 if (!CExtension::Load(error, maxlength))
213 if (m_pAPI->IsMetamodExtension())
218 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
222 m_pLib->CloseLibrary();
231 void CRemoteExtension::Unload()
235 void CLocalExtension::Unload()
237 if (m_pAPI != NULL && m_PlId)
239 char temp_buffer[255];
240 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
246 m_pLib->CloseLibrary();
251 bool CRemoteExtension::Reload(char *error, size_t maxlength)
253 snprintf(error, maxlength, "Remote extensions do not support reloading");
257 bool CLocalExtension::Reload(char *error, size_t maxlength)
259 if (m_pLib == NULL) // FIXME: just load it instead?
262 m_pAPI->OnExtensionUnload();
265 return Load(error, maxlength);
268 bool CRemoteExtension::IsExternal()
273 bool CLocalExtension::IsExternal()
278 CExtension::~CExtension()
283 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
287 snprintf(error, maxlength, "No IExtensionInterface instance provided");
291 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
293 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
300 bool CExtension::Load(char *error, size_t maxlength)
303 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
310 /* Check if we're past load time */
311 if (!g_SourceMod.IsMapLoading())
313 m_pAPI->OnExtensionsAllLoaded();
320 void CExtension::CreateIdentity()
322 if (m_pIdentToken != NULL)
327 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
330 void CExtension::DestroyIdentity()
332 if (m_pIdentToken == NULL)
337 g_ShareSys.DestroyIdentity(m_pIdentToken);
338 m_pIdentToken = NULL;
341 void CExtension::MarkAllLoaded()
343 assert(m_bFullyLoaded == false);
346 m_bFullyLoaded = true;
347 m_pAPI->OnExtensionsAllLoaded();
351 void CExtension::AddPlugin(CPlugin *pPlugin)
353 /* Unfortunately we have to do this :( */
354 if (m_Dependents.find(pPlugin) != m_Dependents.end())
356 m_Dependents.push_back(pPlugin);
360 void CExtension::SetError(const char *error)
362 m_Error.assign(error);
365 IExtensionInterface *CExtension::GetAPI()
370 const char *CExtension::GetFilename()
372 return m_RealFile.c_str();
375 IdentityToken_t *CExtension::GetIdentity()
377 return m_pIdentToken;
380 bool CRemoteExtension::IsLoaded()
382 return (m_pAPI != NULL);
385 bool CLocalExtension::IsLoaded()
387 return (m_pLib != NULL);
390 void CExtension::AddDependency(const IfaceInfo *pInfo)
392 if (m_Deps.find(*pInfo) == m_Deps.end())
394 m_Deps.push_back(*pInfo);
398 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
400 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
403 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
409 List<IfaceInfo>::iterator iter;
410 for (iter = m_ChildDeps.begin();
411 iter != m_ChildDeps.end();
414 IfaceInfo &other = (*iter);
421 m_ChildDeps.push_back(info);
424 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
426 List<IfaceInfo>::iterator iter = m_Deps.begin();
428 if (iter == m_Deps.end())
435 *pOwner = (*iter).owner;
439 *pInterface = (*iter).iface;
442 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
444 return (ITERATOR *)pIter;
447 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
449 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
450 List<IfaceInfo>::iterator _iter;
452 if (_iter == m_Deps.end())
461 *pOwner = (*_iter).owner;
465 *pInterface = (*_iter).iface;
470 if (_iter == m_Deps.end())
478 void CExtension::FreeDependencyIterator(ITERATOR *iter)
480 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
485 void CExtension::AddInterface(SMInterface *pInterface)
487 m_Interfaces.push_back(pInterface);
490 bool CExtension::IsRunning(char *error, size_t maxlength)
496 snprintf(error, maxlength, "%s", m_Error.c_str());
501 return m_pAPI->QueryRunning(error, maxlength);
504 void CExtension::AddLibrary(const char *library)
506 m_Libraries.push_back(library);
509 /*********************
510 * EXTENSION MANAGER *
511 *********************/
513 void CExtensionManager::OnSourceModAllInitialized()
515 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
516 g_PluginSys.AddPluginsListener(this);
517 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
518 g_ShareSys.AddInterface(NULL, this);
521 void CExtensionManager::OnSourceModShutdown()
523 g_RootMenu.RemoveRootConsoleCommand("exts", this);
524 g_ShareSys.DestroyIdentType(g_ExtType);
527 void CExtensionManager::Shutdown()
529 List<CExtension *>::iterator iter;
531 while ((iter = m_Libs.begin()) != m_Libs.end())
533 UnloadExtension((*iter));
537 void CExtensionManager::TryAutoload()
539 char path[PLATFORM_MAX_PATH];
541 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
543 IDirectory *pDir = g_LibSys.OpenDirectory(path);
551 while (pDir->MoreFiles())
553 if (pDir->IsEntryDirectory())
559 lfile = pDir->GetEntryName();
561 if (len <= 9) /* size of ".autoload" */
567 if (strcmp(&lfile[len - 9], ".autoload") != 0)
573 char file[PLATFORM_MAX_PATH];
574 len = UTIL_Format(file, sizeof(file), "%s", lfile);
575 strcpy(&file[len - 9], ".ext");
577 LoadAutoExtension(file);
583 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
585 /* Remove platform extension if it's there. Compat hack. */
586 const char *ext = g_LibSys.GetFileExtension(path);
587 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
589 char path2[PLATFORM_MAX_PATH];
590 UTIL_Format(path2, sizeof(path2), "%s", path);
591 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
592 return LoadAutoExtension(path2);
595 IExtension *pAlready;
596 if ((pAlready=FindExtensionByFile(path)) != NULL)
602 CExtension *p = new CLocalExtension(path);
604 /* We put us in the list beforehand so extensions that check for each other
605 * won't recursively load each other.
609 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
611 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
618 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
620 List<CExtension *>::iterator iter;
623 /* Chomp off the path */
624 char lookup[PLATFORM_MAX_PATH];
625 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
627 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
630 if (pExt->IsSameFile(lookup))
639 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
641 List<CExtension *>::iterator iter;
643 IExtensionInterface *pAPI;
646 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
649 if (!pExt->IsLoaded())
653 if ((pAPI = pExt->GetAPI()) == NULL)
657 name = pAPI->GetExtensionName();
662 if (strcmp(name, ext) == 0)
671 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
673 /* Remove platform extension if it's there. Compat hack. */
674 const char *ext = g_LibSys.GetFileExtension(file);
675 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
677 char path2[PLATFORM_MAX_PATH];
678 UTIL_Format(path2, sizeof(path2), "%s", file);
679 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
680 return LoadExtension(path2, error, maxlength);
683 IExtension *pAlready;
684 if ((pAlready=FindExtensionByFile(file)) != NULL)
689 CExtension *pExt = new CLocalExtension(file);
691 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
698 /* :TODO: do QueryRunning check if the map is loaded */
700 m_Libs.push_back(pExt);
705 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
707 CExtension *pExt = (CExtension *)pRequester;
708 CExtension *pOwner = (CExtension *)pInfo->owner;
710 pExt->AddDependency(pInfo);
712 IExtensionInterface *pAPI = pExt->GetAPI();
713 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
715 pOwner->AddChildDependent(pExt, pInfo->iface);
719 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
721 CExtension *pExt = (CExtension *)ext;
722 CExtension *pOwner = GetExtensionFromIdent(other);
726 info.iface = (SMInterface *)iface;
729 pExt->AddDependency(&info);
730 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
733 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
735 CExtension *pExt = (CExtension *)pOwner;
737 pExt->AddInterface(pInterface);
740 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
742 CExtension *pExt = (CExtension *)pParent;
744 pExt->AddPlugin(pPlugin);
747 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
749 List<CExtension *>::iterator iter;
751 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
753 (*iter)->DropRefsTo((CPlugin *)plugin);
757 CExtension *CExtensionManager::FindByOrder(unsigned int num)
759 if (num < 1 || num > m_Libs.size())
764 List<CExtension *>::iterator iter = m_Libs.begin();
766 while (iter != m_Libs.end())
778 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
785 CExtension *pExt = (CExtension *)_pExt;
787 if (m_Libs.find(pExt) == m_Libs.end())
792 /* First remove us from internal lists */
793 g_ShareSys.RemoveInterfaces(_pExt);
796 List<CExtension *> UnloadQueue;
798 /* Handle dependencies */
799 if (pExt->IsLoaded())
801 /* Unload any dependent plugins */
802 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
803 while (p_iter != pExt->m_Dependents.end())
805 /* We have to manually unlink ourselves here, since we're no longer being managed */
806 g_PluginSys.UnloadPlugin((*p_iter));
807 p_iter = pExt->m_Dependents.erase(p_iter);
810 List<String>::iterator s_iter;
811 for (s_iter = pExt->m_Libraries.begin();
812 s_iter != pExt->m_Libraries.end();
815 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
818 /* Notify and/or unload all dependencies */
819 List<CExtension *>::iterator c_iter;
821 IExtensionInterface *pAPI;
822 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
825 if ((pAPI=pDep->GetAPI()) == NULL)
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)
862 i_iter = pDep->m_ChildDeps.erase(i_iter);
871 /* Unbind our natives from Core */
872 pExt->DropEverything();
875 IdentityToken_t *pIdentity;
876 if ((pIdentity = pExt->GetIdentity()) != NULL)
878 SMGlobalClass *glob = SMGlobalClass::head;
881 glob->OnSourceModIdentityDropped(pIdentity);
882 glob = glob->m_pGlobalClassNext;
886 /* Tell it to unload */
887 if (pExt->IsLoaded())
889 IExtensionInterface *pAPI = pExt->GetAPI();
890 pAPI->OnExtensionUnload();
896 List<CExtension *>::iterator iter;
897 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
899 /* NOTE: This is safe because the unload function backs out of anything not present */
900 UnloadExtension((*iter));
906 void CExtensionManager::MarkAllLoaded()
908 List<CExtension *>::iterator iter;
911 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
914 if (!pExt->IsLoaded())
918 if (pExt->m_bFullyLoaded)
922 pExt->MarkAllLoaded();
926 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
928 /* This function doesn't really need to do anything now. We make sure the
929 * other extension is loaded, but handling of dependencies is really done
930 * by the interface fetcher.
932 if (required || autoload)
934 LoadAutoExtension(file);
938 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
940 int argcount = command.ArgC();
943 const char *cmd = command.Arg(2);
944 if (strcmp(cmd, "list") == 0)
946 List<CExtension *>::iterator iter;
948 unsigned int num = 1;
949 switch (m_Libs.size())
953 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
958 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
963 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
967 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
970 if (pExt->IsLoaded())
973 if (!pExt->IsRunning(error, sizeof(error)))
975 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
979 IExtensionInterface *pAPI = pExt->GetAPI();
980 const char *name = pAPI->GetExtensionName();
981 const char *version = pAPI->GetExtensionVerString();
982 const char *descr = pAPI->GetExtensionDescription();
983 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
986 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
991 else if (strcmp(cmd, "load") == 0)
995 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
999 const char *filename = command.Arg(3);
1000 char path[PLATFORM_MAX_PATH];
1003 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1004 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1006 if (FindExtensionByFile(path) != NULL)
1008 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1012 if (LoadExtension(path, error, sizeof(error)))
1014 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1017 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1022 else if (strcmp(cmd, "info") == 0)
1026 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1030 const char *sId = command.Arg(3);
1031 unsigned int id = atoi(sId);
1034 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1038 if (m_Libs.size() == 0)
1040 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1044 if (id > m_Libs.size())
1046 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1050 List<CExtension *>::iterator iter = m_Libs.begin();
1051 CExtension *pExt = NULL;
1052 while (iter != m_Libs.end())
1061 /* This should never happen */
1064 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1068 if (!pExt->IsLoaded())
1070 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1071 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1076 if (!pExt->IsRunning(error, sizeof(error)))
1078 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1079 g_RootMenu.ConsolePrint(" Loaded: Yes");
1080 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1084 IExtensionInterface *pAPI = pExt->GetAPI();
1085 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1086 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1087 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1088 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1089 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1091 if (pExt->IsExternal())
1093 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1095 else if (pAPI->IsMetamodExtension())
1097 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1101 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1107 else if (strcmp(cmd, "unload") == 0)
1111 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1115 const char *arg = command.Arg(3);
1116 unsigned int num = atoi(arg);
1117 CExtension *pExt = FindByOrder(num);
1121 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1125 if (argcount > 4 && pExt->unload_code)
1127 const char *unload = command.Arg(4);
1128 if (pExt->unload_code == (unsigned)atoi(unload))
1130 char filename[PLATFORM_MAX_PATH];
1131 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1132 UnloadExtension(pExt);
1133 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1137 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1142 if (!pExt->IsLoaded()
1143 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1145 char filename[PLATFORM_MAX_PATH];
1146 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1147 UnloadExtension(pExt);
1148 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1153 List<CPlugin *> plugins;
1154 if (pExt->m_ChildDeps.size())
1156 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1157 List<CExtension *>::iterator iter;
1159 /* Get list of all extensions */
1160 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1162 List<IfaceInfo>::iterator i_iter;
1164 if (!pOther->IsLoaded() || pOther == pExt)
1168 /* Get their dependencies */
1169 for (i_iter=pOther->m_Deps.begin();
1170 i_iter!=pOther->m_Deps.end();
1173 /* Is this dependency to us? */
1174 if ((*i_iter).owner != pExt)
1178 /* Will our dependent care? */
1179 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1181 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1182 /* Add to plugin unload list */
1183 List<CPlugin *>::iterator p_iter;
1184 for (p_iter=pOther->m_Dependents.begin();
1185 p_iter!=pOther->m_Dependents.end();
1188 if (plugins.find((*p_iter)) == plugins.end())
1190 plugins.push_back((*p_iter));
1197 if (pExt->m_Dependents.size())
1199 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1200 List<CPlugin *>::iterator iter;
1202 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1205 if (plugins.find(pPlugin) == plugins.end())
1207 plugins.push_back(pPlugin);
1210 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1213 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1216 srand(static_cast<int>(time(NULL)));
1217 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1218 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1219 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1224 else if (strcmp(cmd, "reload") == 0)
1228 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1232 const char *arg = command.Arg(3);
1233 unsigned int num = atoi(arg);
1234 CExtension *pExt = FindByOrder(num);
1238 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1242 if (pExt->IsLoaded())
1244 char filename[PLATFORM_MAX_PATH];
1247 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1249 if (pExt->Reload(error, sizeof(error)))
1251 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1255 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1262 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1270 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1271 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1272 g_RootMenu.DrawGenericOption("list", "List extensions");
1273 g_RootMenu.DrawGenericOption("load", "Load an extension");
1274 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1275 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1278 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1280 if (ptr->type == g_ExtType)
1282 return (CExtension *)(ptr->ptr);
1288 CExtensionManager::CExtensionManager()
1292 CExtensionManager::~CExtensionManager()
1296 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1298 CExtension *pExt = (CExtension *)pSource;
1299 pExt->AddLibrary(library);
1300 g_PluginSys.OnLibraryAction(library, false, false);
1303 bool CExtensionManager::LibraryExists(const char *library)
1307 for (List<CExtension *>::iterator iter = m_Libs.begin();
1308 iter != m_Libs.end();
1312 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1313 s_iter != pExt->m_Libraries.end();
1316 if ((*s_iter).compare(library) == 0)
1326 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1327 const char *filepath,
1328 const char *filename,
1332 IExtension *pAlready;
1333 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1338 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1340 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1347 m_Libs.push_back(pExt);
1352 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1354 IExtensionInterface *pAPI;
1355 List<CExtension *>::iterator iter;
1357 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1359 if ((pAPI = (*iter)->GetAPI()) == NULL)
1363 if (pAPI->GetExtensionVersion() > 3)
1365 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1370 bool CLocalExtension::IsSameFile(const char *file)
1372 /* Only care about the shortened name. */
1373 return strcmp(file, m_File.c_str()) == 0;
1376 bool CRemoteExtension::IsSameFile(const char *file)
1378 /* :TODO: this could be better, but no one uses this API anyway. */
1379 return strcmp(file, m_Path.c_str()) == 0;