Triggering build.
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"
40 #include "PlayerManager.h"
42 CExtensionManager g_Extensions;
43 IdentityType_t g_ExtType;
45 void CExtension::Initialize(const char *filename, const char *path)
50 m_bFullyLoaded = false;
51 m_File.assign(filename);
53 char real_name[PLATFORM_MAX_PATH];
54 g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
55 m_RealFile.assign(real_name);
58 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
60 Initialize(filename, path);
64 #if defined METAMOD_PLAPI_VERSION
65 #if SOURCE_ENGINE == SE_LEFT4DEAD
66 #define GAMEFIX "2.l4d"
67 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
68 #define GAMEFIX "2.l4d2"
69 #elif SOURCE_ENGINE == SE_ALIENSWARM
70 #define GAMEFIX "2.swarm"
71 #elif SOURCE_ENGINE == SE_ORANGEBOX
72 #define GAMEFIX "2.ep2"
73 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
74 #define GAMEFIX "2.bgt"
75 #elif SOURCE_ENGINE == SE_EYE
76 #define GAMEFIX "2.eye"
77 #elif SOURCE_ENGINE == SE_CSS
78 #define GAMEFIX "2.css"
79 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
80 #define GAMEFIX "2.ep2v"
81 #elif SOURCE_ENGINE == SE_DARKMESSIAH
82 #define GAMEFIX "2.darkm"
83 #elif SOURCE_ENGINE == SE_PORTAL2
84 #define GAMEFIX "2.portal2"
85 #elif SOURCE_ENGINE == SE_CSGO
86 #define GAMEFIX "2.csgo"
87 #elif SOURCE_ENGINE == SE_DOTA
88 #define GAMEFIX "2.dota"
90 #define GAMEFIX "2.ep1"
91 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
92 #else //METAMOD_PLAPI_VERSION
93 #define GAMEFIX "1.ep1"
94 #endif //METAMOD_PLAPI_VERSION
96 CLocalExtension::CLocalExtension(const char *filename)
101 char path[PLATFORM_MAX_PATH];
103 /* Special case for new bintools binary */
104 if (strcmp(filename, "bintools.ext") == 0)
109 /* Zeroth, see if there is an engine specific build in the new place. */
110 g_SourceMod.BuildPath(Path_SM,
113 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
116 if (g_LibSys.IsPathFile(path))
121 /* First see if there is an engine specific build! */
122 g_SourceMod.BuildPath(Path_SM,
125 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
128 /* Try the "normal" version */
129 if (!g_LibSys.IsPathFile(path))
132 g_SourceMod.BuildPath(Path_SM,
135 "extensions/%s." PLATFORM_LIB_EXT,
140 Initialize(filename, path);
143 bool CRemoteExtension::Load(char *error, size_t maxlength)
145 if (!PerformAPICheck(error, maxlength))
151 if (!CExtension::Load(error, maxlength))
160 bool CLocalExtension::Load(char *error, size_t maxlength)
162 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
169 typedef IExtensionInterface *(*GETAPI)();
170 GETAPI pfnGetAPI = NULL;
172 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
174 m_pLib->CloseLibrary();
176 snprintf(error, maxlength, "Unable to find extension entry point");
180 m_pAPI = pfnGetAPI();
182 /* Check pointer and version */
183 if (!PerformAPICheck(error, maxlength))
185 m_pLib->CloseLibrary();
192 if (m_pAPI->IsMetamodExtension())
195 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
197 // Check the plugin didn't refuse load
200 #ifndef METAMOD_PLAPI_VERSION
206 #ifndef METAMOD_PLAPI_VERSION
207 g_pMMPlugins->Query(m_PlId, file, status, source)
209 g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
211 && status < Pl_Paused))
213 m_pLib->CloseLibrary();
220 if (!CExtension::Load(error, maxlength))
222 if (m_pAPI->IsMetamodExtension())
227 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
231 m_pLib->CloseLibrary();
240 void CRemoteExtension::Unload()
244 void CLocalExtension::Unload()
246 if (m_pAPI != NULL && m_PlId)
248 char temp_buffer[255];
249 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
255 m_pLib->CloseLibrary();
260 bool CRemoteExtension::Reload(char *error, size_t maxlength)
262 snprintf(error, maxlength, "Remote extensions do not support reloading");
266 bool CLocalExtension::Reload(char *error, size_t maxlength)
268 if (m_pLib == NULL) // FIXME: just load it instead?
271 m_pAPI->OnExtensionUnload();
274 return Load(error, maxlength);
277 bool CRemoteExtension::IsExternal()
282 bool CLocalExtension::IsExternal()
287 CExtension::~CExtension()
292 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
296 snprintf(error, maxlength, "No IExtensionInterface instance provided");
300 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
302 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
309 bool CExtension::Load(char *error, size_t maxlength)
312 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
319 /* Check if we're past load time */
320 if (!g_SourceMod.IsMapLoading())
322 m_pAPI->OnExtensionsAllLoaded();
329 void CExtension::CreateIdentity()
331 if (m_pIdentToken != NULL)
336 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
339 void CExtension::DestroyIdentity()
341 if (m_pIdentToken == NULL)
346 g_ShareSys.DestroyIdentity(m_pIdentToken);
347 m_pIdentToken = NULL;
350 void CExtension::MarkAllLoaded()
352 assert(m_bFullyLoaded == false);
355 m_bFullyLoaded = true;
356 m_pAPI->OnExtensionsAllLoaded();
360 void CExtension::AddPlugin(CPlugin *pPlugin)
362 /* Unfortunately we have to do this :( */
363 if (m_Dependents.find(pPlugin) != m_Dependents.end())
365 m_Dependents.push_back(pPlugin);
369 void CExtension::SetError(const char *error)
371 m_Error.assign(error);
374 IExtensionInterface *CExtension::GetAPI()
379 const char *CExtension::GetFilename()
381 return m_RealFile.c_str();
384 const char *CExtension::GetPath() const
386 return m_Path.c_str();
389 IdentityToken_t *CExtension::GetIdentity()
391 return m_pIdentToken;
394 bool CRemoteExtension::IsLoaded()
396 return (m_pAPI != NULL);
399 bool CLocalExtension::IsLoaded()
401 return (m_pLib != NULL);
404 void CExtension::AddDependency(const IfaceInfo *pInfo)
406 if (m_Deps.find(*pInfo) == m_Deps.end())
408 m_Deps.push_back(*pInfo);
412 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
414 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
417 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
423 List<IfaceInfo>::iterator iter;
424 for (iter = m_ChildDeps.begin();
425 iter != m_ChildDeps.end();
428 IfaceInfo &other = (*iter);
435 m_ChildDeps.push_back(info);
438 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
440 List<IfaceInfo>::iterator iter = m_Deps.begin();
442 if (iter == m_Deps.end())
449 *pOwner = (*iter).owner;
453 *pInterface = (*iter).iface;
456 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
458 return (ITERATOR *)pIter;
461 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
463 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
464 List<IfaceInfo>::iterator _iter;
466 if (_iter == m_Deps.end())
475 *pOwner = (*_iter).owner;
479 *pInterface = (*_iter).iface;
484 if (_iter == m_Deps.end())
492 void CExtension::FreeDependencyIterator(ITERATOR *iter)
494 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
499 void CExtension::AddInterface(SMInterface *pInterface)
501 m_Interfaces.push_back(pInterface);
504 bool CExtension::IsRunning(char *error, size_t maxlength)
510 snprintf(error, maxlength, "%s", m_Error.c_str());
515 return m_pAPI->QueryRunning(error, maxlength);
518 void CExtension::AddLibrary(const char *library)
520 m_Libraries.push_back(library);
523 /*********************
524 * EXTENSION MANAGER *
525 *********************/
527 void CExtensionManager::OnSourceModAllInitialized()
529 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
530 g_PluginSys.AddPluginsListener(this);
531 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
532 g_ShareSys.AddInterface(NULL, this);
535 void CExtensionManager::OnSourceModShutdown()
537 g_RootMenu.RemoveRootConsoleCommand("exts", this);
538 g_ShareSys.DestroyIdentType(g_ExtType);
541 void CExtensionManager::Shutdown()
543 List<CExtension *>::iterator iter;
545 while ((iter = m_Libs.begin()) != m_Libs.end())
547 UnloadExtension((*iter));
551 void CExtensionManager::TryAutoload()
553 char path[PLATFORM_MAX_PATH];
555 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
557 IDirectory *pDir = g_LibSys.OpenDirectory(path);
565 while (pDir->MoreFiles())
567 if (pDir->IsEntryDirectory())
573 lfile = pDir->GetEntryName();
575 if (len <= 9) /* size of ".autoload" */
581 if (strcmp(&lfile[len - 9], ".autoload") != 0)
587 char file[PLATFORM_MAX_PATH];
588 len = UTIL_Format(file, sizeof(file), "%s", lfile);
589 strcpy(&file[len - 9], ".ext");
591 LoadAutoExtension(file);
597 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
599 /* Remove platform extension if it's there. Compat hack. */
600 const char *ext = g_LibSys.GetFileExtension(path);
601 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
603 char path2[PLATFORM_MAX_PATH];
604 UTIL_Format(path2, sizeof(path2), "%s", path);
605 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
606 return LoadAutoExtension(path2, bErrorOnMissing);
609 IExtension *pAlready;
610 if ((pAlready=FindExtensionByFile(path)) != NULL)
616 CExtension *p = new CLocalExtension(path);
618 /* We put us in the list beforehand so extensions that check for each other
619 * won't recursively load each other.
623 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
625 if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
627 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
636 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
638 List<CExtension *>::iterator iter;
641 /* Chomp off the path */
642 char lookup[PLATFORM_MAX_PATH];
643 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
645 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
648 if (pExt->IsSameFile(lookup))
657 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
659 List<CExtension *>::iterator iter;
661 IExtensionInterface *pAPI;
664 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
667 if (!pExt->IsLoaded())
671 if ((pAPI = pExt->GetAPI()) == NULL)
675 name = pAPI->GetExtensionName();
680 if (strcmp(name, ext) == 0)
689 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
691 /* Remove platform extension if it's there. Compat hack. */
692 const char *ext = g_LibSys.GetFileExtension(file);
693 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
695 char path2[PLATFORM_MAX_PATH];
696 UTIL_Format(path2, sizeof(path2), "%s", file);
697 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
698 return LoadExtension(path2, error, maxlength);
701 IExtension *pAlready;
702 if ((pAlready=FindExtensionByFile(file)) != NULL)
707 CExtension *pExt = new CLocalExtension(file);
709 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
716 /* :TODO: do QueryRunning check if the map is loaded */
718 m_Libs.push_back(pExt);
723 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
725 CExtension *pExt = (CExtension *)pRequester;
726 CExtension *pOwner = (CExtension *)pInfo->owner;
728 pExt->AddDependency(pInfo);
730 IExtensionInterface *pAPI = pExt->GetAPI();
731 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
733 pOwner->AddChildDependent(pExt, pInfo->iface);
737 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
739 CExtension *pExt = (CExtension *)ext;
740 CExtension *pOwner = GetExtensionFromIdent(other);
744 info.iface = (SMInterface *)iface;
747 pExt->AddDependency(&info);
748 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
751 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
753 CExtension *pExt = (CExtension *)pOwner;
755 pExt->AddInterface(pInterface);
758 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
760 CExtension *pExt = (CExtension *)pParent;
762 pExt->AddPlugin(pPlugin);
765 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
767 List<CExtension *>::iterator iter;
769 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
771 (*iter)->DropRefsTo((CPlugin *)plugin);
775 CExtension *CExtensionManager::FindByOrder(unsigned int num)
777 if (num < 1 || num > m_Libs.size())
782 List<CExtension *>::iterator iter = m_Libs.begin();
784 while (iter != m_Libs.end())
796 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
803 CExtension *pExt = (CExtension *)_pExt;
805 if (m_Libs.find(pExt) == m_Libs.end())
810 /* Tell it to unload */
811 if (pExt->IsLoaded())
813 IExtensionInterface *pAPI = pExt->GetAPI();
814 pAPI->OnExtensionUnload();
817 /* First remove us from internal lists */
818 g_ShareSys.RemoveInterfaces(_pExt);
821 List<CExtension *> UnloadQueue;
823 /* Handle dependencies */
824 if (pExt->IsLoaded())
826 /* Unload any dependent plugins */
827 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
828 while (p_iter != pExt->m_Dependents.end())
830 /* We have to manually unlink ourselves here, since we're no longer being managed */
831 g_PluginSys.UnloadPlugin((*p_iter));
832 p_iter = pExt->m_Dependents.erase(p_iter);
835 List<String>::iterator s_iter;
836 for (s_iter = pExt->m_Libraries.begin();
837 s_iter != pExt->m_Libraries.end();
840 g_PluginSys.OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
843 /* Notify and/or unload all dependencies */
844 List<CExtension *>::iterator c_iter;
846 IExtensionInterface *pAPI;
847 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
850 if ((pAPI=pDep->GetAPI()) == NULL)
858 /* Now, get its dependency list */
859 bool dropped = false;
860 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
861 while (i_iter != pDep->m_Deps.end())
863 if ((*i_iter).owner == _pExt)
865 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
870 UnloadQueue.push_back(pDep);
873 pAPI->NotifyInterfaceDrop((*i_iter).iface);
874 i_iter = pDep->m_Deps.erase(i_iter);
881 /* Flush out any back references to this plugin */
882 i_iter = pDep->m_ChildDeps.begin();
883 while (i_iter != pDep->m_ChildDeps.end())
885 if ((*i_iter).owner == pExt)
887 i_iter = pDep->m_ChildDeps.erase(i_iter);
896 /* Unbind our natives from Core */
897 pExt->DropEverything();
900 IdentityToken_t *pIdentity;
901 if ((pIdentity = pExt->GetIdentity()) != NULL)
903 SMGlobalClass *glob = SMGlobalClass::head;
906 glob->OnSourceModIdentityDropped(pIdentity);
907 glob = glob->m_pGlobalClassNext;
914 List<CExtension *>::iterator iter;
915 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
917 /* NOTE: This is safe because the unload function backs out of anything not present */
918 UnloadExtension((*iter));
924 void CExtensionManager::MarkAllLoaded()
926 List<CExtension *>::iterator iter;
929 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
932 if (!pExt->IsLoaded())
936 if (pExt->m_bFullyLoaded)
940 pExt->MarkAllLoaded();
944 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
946 /* This function doesn't really need to do anything now. We make sure the
947 * other extension is loaded, but handling of dependencies is really done
948 * by the interface fetcher.
950 if (required || autoload)
952 LoadAutoExtension(file);
956 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
958 int argcount = command.ArgC();
961 const char *cmd = command.Arg(2);
962 if (strcmp(cmd, "list") == 0)
964 List<CExtension *>::iterator iter;
966 unsigned int num = 1;
967 switch (m_Libs.size())
971 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
976 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
981 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
985 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
988 if (pExt->IsLoaded())
991 if (!pExt->IsRunning(error, sizeof(error)))
993 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
997 IExtensionInterface *pAPI = pExt->GetAPI();
998 const char *name = pAPI->GetExtensionName();
999 const char *version = pAPI->GetExtensionVerString();
1000 const char *descr = pAPI->GetExtensionDescription();
1001 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
1004 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1009 else if (strcmp(cmd, "load") == 0)
1013 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
1017 const char *filename = command.Arg(3);
1018 char path[PLATFORM_MAX_PATH];
1021 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1022 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1024 if (FindExtensionByFile(path) != NULL)
1026 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1030 if (LoadExtension(path, error, sizeof(error)))
1032 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1035 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1040 else if (strcmp(cmd, "info") == 0)
1044 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1048 const char *sId = command.Arg(3);
1049 unsigned int id = atoi(sId);
1052 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1056 if (m_Libs.size() == 0)
1058 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1062 if (id > m_Libs.size())
1064 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1068 List<CExtension *>::iterator iter = m_Libs.begin();
1069 CExtension *pExt = NULL;
1070 while (iter != m_Libs.end())
1079 /* This should never happen */
1082 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1086 if (!pExt->IsLoaded())
1088 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1089 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1094 if (!pExt->IsRunning(error, sizeof(error)))
1096 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1097 g_RootMenu.ConsolePrint(" Loaded: Yes");
1098 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1102 IExtensionInterface *pAPI = pExt->GetAPI();
1103 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1104 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1105 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1106 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1107 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1109 if (pExt->IsExternal())
1111 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1113 else if (pAPI->IsMetamodExtension())
1115 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1119 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1125 else if (strcmp(cmd, "unload") == 0)
1129 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1133 const char *arg = command.Arg(3);
1134 unsigned int num = atoi(arg);
1135 CExtension *pExt = FindByOrder(num);
1139 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1143 if (argcount > 4 && pExt->unload_code)
1145 const char *unload = command.Arg(4);
1146 if (pExt->unload_code == (unsigned)atoi(unload))
1148 char filename[PLATFORM_MAX_PATH];
1149 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1150 UnloadExtension(pExt);
1151 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1155 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1160 if (!pExt->IsLoaded()
1161 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1163 char filename[PLATFORM_MAX_PATH];
1164 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1165 UnloadExtension(pExt);
1166 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1171 List<CPlugin *> plugins;
1172 if (pExt->m_ChildDeps.size())
1174 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1175 List<CExtension *>::iterator iter;
1177 /* Get list of all extensions */
1178 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1180 List<IfaceInfo>::iterator i_iter;
1182 if (!pOther->IsLoaded() || pOther == pExt)
1186 /* Get their dependencies */
1187 for (i_iter=pOther->m_Deps.begin();
1188 i_iter!=pOther->m_Deps.end();
1191 /* Is this dependency to us? */
1192 if ((*i_iter).owner != pExt)
1196 /* Will our dependent care? */
1197 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1199 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1200 /* Add to plugin unload list */
1201 List<CPlugin *>::iterator p_iter;
1202 for (p_iter=pOther->m_Dependents.begin();
1203 p_iter!=pOther->m_Dependents.end();
1206 if (plugins.find((*p_iter)) == plugins.end())
1208 plugins.push_back((*p_iter));
1215 if (pExt->m_Dependents.size())
1217 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1218 List<CPlugin *>::iterator iter;
1220 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1223 if (plugins.find(pPlugin) == plugins.end())
1225 plugins.push_back(pPlugin);
1228 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1231 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1234 srand(static_cast<int>(time(NULL)));
1235 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1236 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1237 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1242 else if (strcmp(cmd, "reload") == 0)
1246 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1250 const char *arg = command.Arg(3);
1251 unsigned int num = atoi(arg);
1252 CExtension *pExt = FindByOrder(num);
1256 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1260 if (pExt->IsLoaded())
1262 char filename[PLATFORM_MAX_PATH];
1265 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1267 if (pExt->Reload(error, sizeof(error)))
1269 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1273 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1280 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1288 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1289 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1290 g_RootMenu.DrawGenericOption("list", "List extensions");
1291 g_RootMenu.DrawGenericOption("load", "Load an extension");
1292 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1293 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1296 void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
1299 int numExtensions = m_Libs.size();
1300 edict_t *edict = player->GetEdict();
1301 unsigned int id = 0;
1302 unsigned int start = 0;
1306 ClientConsolePrint(edict, "[SM] No extensions found.");
1310 if (args.ArgC() > 2)
1312 start = atoi(args.Arg(2));
1316 SourceHook::List<CExtension *>::iterator iter;
1318 for (iter = m_Libs.begin();
1319 iter != m_Libs.end();
1325 if (!ext->IsRunning(error, sizeof(error)))
1336 if (id - start > 10)
1341 IExtensionInterface *api = ext->GetAPI();
1343 const char *name = api->GetExtensionName();
1344 const char *version = api->GetExtensionVerString();
1345 const char *author = api->GetExtensionAuthor();
1346 const char *description = api->GetExtensionDescription();
1348 size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
1350 if (version != NULL && IS_STR_FILLED(version))
1352 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
1355 if (author != NULL && IS_STR_FILLED(author))
1357 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
1360 if (description != NULL && IS_STR_FILLED(description))
1362 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
1366 ClientConsolePrint(edict, "%s", buffer);
1369 while (iter != m_Libs.end())
1372 if ((*iter)->IsRunning(error, sizeof(error)))
1378 if (iter != m_Libs.end())
1380 ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
1384 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1386 if (ptr->type == g_ExtType)
1388 return (CExtension *)(ptr->ptr);
1394 CExtensionManager::CExtensionManager()
1398 CExtensionManager::~CExtensionManager()
1402 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1404 CExtension *pExt = (CExtension *)pSource;
1405 pExt->AddLibrary(library);
1406 g_PluginSys.OnLibraryAction(library, LibraryAction_Added);
1409 bool CExtensionManager::LibraryExists(const char *library)
1413 for (List<CExtension *>::iterator iter = m_Libs.begin();
1414 iter != m_Libs.end();
1418 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1419 s_iter != pExt->m_Libraries.end();
1422 if ((*s_iter).compare(library) == 0)
1432 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1433 const char *filepath,
1434 const char *filename,
1438 IExtension *pAlready;
1439 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1444 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1446 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1453 m_Libs.push_back(pExt);
1458 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1460 IExtensionInterface *pAPI;
1461 List<CExtension *>::iterator iter;
1463 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1465 if ((pAPI = (*iter)->GetAPI()) == NULL)
1469 if (pAPI->GetExtensionVersion() > 3)
1471 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1476 bool CLocalExtension::IsSameFile(const char *file)
1478 /* Only care about the shortened name. */
1479 return strcmp(file, m_File.c_str()) == 0;
1482 bool CRemoteExtension::IsSameFile(const char *file)
1484 /* :TODO: this could be better, but no one uses this API anyway. */
1485 return strcmp(file, m_Path.c_str()) == 0;