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_ORANGEBOXVALVE
78 #define GAMEFIX "2.ep2v"
79 #elif SOURCE_ENGINE == SE_DARKMESSIAH
80 #define GAMEFIX "2.darkm"
82 #define GAMEFIX "2.ep1"
83 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
84 #else //METAMOD_PLAPI_VERSION
85 #define GAMEFIX "1.ep1"
86 #endif //METAMOD_PLAPI_VERSION
88 CLocalExtension::CLocalExtension(const char *filename)
93 char path[PLATFORM_MAX_PATH];
95 /* Special case for new bintools binary */
96 if (strcmp(filename, "bintools.ext") == 0)
101 /* Zeroth, see if there is an engine specific build in the new place. */
102 g_SourceMod.BuildPath(Path_SM,
105 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
108 if (g_LibSys.IsPathFile(path))
113 /* First see if there is an engine specific build! */
114 g_SourceMod.BuildPath(Path_SM,
117 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
121 /* Try the "normal" version */
122 if (!g_LibSys.IsPathFile(path))
124 g_SourceMod.BuildPath(Path_SM,
127 "extensions/%s." PLATFORM_LIB_EXT,
132 Initialize(filename, path);
135 bool CRemoteExtension::Load(char *error, size_t maxlength)
137 if (!PerformAPICheck(error, maxlength))
143 if (!CExtension::Load(error, maxlength))
152 bool CLocalExtension::Load(char *error, size_t maxlength)
154 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
161 typedef IExtensionInterface *(*GETAPI)();
162 GETAPI pfnGetAPI = NULL;
164 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
166 m_pLib->CloseLibrary();
168 snprintf(error, maxlength, "Unable to find extension entry point");
172 m_pAPI = pfnGetAPI();
174 /* Check pointer and version */
175 if (!PerformAPICheck(error, maxlength))
177 m_pLib->CloseLibrary();
184 if (m_pAPI->IsMetamodExtension())
187 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
189 // Check the plugin didn't refuse load
192 #ifndef METAMOD_PLAPI_VERSION
198 #ifndef METAMOD_PLAPI_VERSION
199 g_pMMPlugins->Query(m_PlId, file, status, source)
201 g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
203 && status < Pl_Paused))
205 m_pLib->CloseLibrary();
212 if (!CExtension::Load(error, maxlength))
214 if (m_pAPI->IsMetamodExtension())
219 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
223 m_pLib->CloseLibrary();
232 void CRemoteExtension::Unload()
236 void CLocalExtension::Unload()
238 if (m_pAPI != NULL && m_PlId)
240 char temp_buffer[255];
241 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
247 m_pLib->CloseLibrary();
252 bool CRemoteExtension::Reload(char *error, size_t maxlength)
254 snprintf(error, maxlength, "Remote extensions do not support reloading");
258 bool CLocalExtension::Reload(char *error, size_t maxlength)
260 if (m_pLib == NULL) // FIXME: just load it instead?
263 m_pAPI->OnExtensionUnload();
266 return Load(error, maxlength);
269 bool CRemoteExtension::IsExternal()
274 bool CLocalExtension::IsExternal()
279 CExtension::~CExtension()
284 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
288 snprintf(error, maxlength, "No IExtensionInterface instance provided");
292 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
294 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
301 bool CExtension::Load(char *error, size_t maxlength)
304 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
311 /* Check if we're past load time */
312 if (!g_SourceMod.IsMapLoading())
314 m_pAPI->OnExtensionsAllLoaded();
321 void CExtension::CreateIdentity()
323 if (m_pIdentToken != NULL)
328 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
331 void CExtension::DestroyIdentity()
333 if (m_pIdentToken == NULL)
338 g_ShareSys.DestroyIdentity(m_pIdentToken);
339 m_pIdentToken = NULL;
342 void CExtension::MarkAllLoaded()
344 assert(m_bFullyLoaded == false);
347 m_bFullyLoaded = true;
348 m_pAPI->OnExtensionsAllLoaded();
352 void CExtension::AddPlugin(CPlugin *pPlugin)
354 /* Unfortunately we have to do this :( */
355 if (m_Dependents.find(pPlugin) != m_Dependents.end())
357 m_Dependents.push_back(pPlugin);
361 void CExtension::SetError(const char *error)
363 m_Error.assign(error);
366 IExtensionInterface *CExtension::GetAPI()
371 const char *CExtension::GetFilename()
373 return m_RealFile.c_str();
376 const char *CExtension::GetPath() const
378 return m_Path.c_str();
381 IdentityToken_t *CExtension::GetIdentity()
383 return m_pIdentToken;
386 bool CRemoteExtension::IsLoaded()
388 return (m_pAPI != NULL);
391 bool CLocalExtension::IsLoaded()
393 return (m_pLib != NULL);
396 void CExtension::AddDependency(const IfaceInfo *pInfo)
398 if (m_Deps.find(*pInfo) == m_Deps.end())
400 m_Deps.push_back(*pInfo);
404 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
406 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
409 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
415 List<IfaceInfo>::iterator iter;
416 for (iter = m_ChildDeps.begin();
417 iter != m_ChildDeps.end();
420 IfaceInfo &other = (*iter);
427 m_ChildDeps.push_back(info);
430 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
432 List<IfaceInfo>::iterator iter = m_Deps.begin();
434 if (iter == m_Deps.end())
441 *pOwner = (*iter).owner;
445 *pInterface = (*iter).iface;
448 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
450 return (ITERATOR *)pIter;
453 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
455 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
456 List<IfaceInfo>::iterator _iter;
458 if (_iter == m_Deps.end())
467 *pOwner = (*_iter).owner;
471 *pInterface = (*_iter).iface;
476 if (_iter == m_Deps.end())
484 void CExtension::FreeDependencyIterator(ITERATOR *iter)
486 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
491 void CExtension::AddInterface(SMInterface *pInterface)
493 m_Interfaces.push_back(pInterface);
496 bool CExtension::IsRunning(char *error, size_t maxlength)
502 snprintf(error, maxlength, "%s", m_Error.c_str());
507 return m_pAPI->QueryRunning(error, maxlength);
510 void CExtension::AddLibrary(const char *library)
512 m_Libraries.push_back(library);
515 /*********************
516 * EXTENSION MANAGER *
517 *********************/
519 void CExtensionManager::OnSourceModAllInitialized()
521 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
522 g_PluginSys.AddPluginsListener(this);
523 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
524 g_ShareSys.AddInterface(NULL, this);
527 void CExtensionManager::OnSourceModShutdown()
529 g_RootMenu.RemoveRootConsoleCommand("exts", this);
530 g_ShareSys.DestroyIdentType(g_ExtType);
533 void CExtensionManager::Shutdown()
535 List<CExtension *>::iterator iter;
537 while ((iter = m_Libs.begin()) != m_Libs.end())
539 UnloadExtension((*iter));
543 void CExtensionManager::TryAutoload()
545 char path[PLATFORM_MAX_PATH];
547 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
549 IDirectory *pDir = g_LibSys.OpenDirectory(path);
557 while (pDir->MoreFiles())
559 if (pDir->IsEntryDirectory())
565 lfile = pDir->GetEntryName();
567 if (len <= 9) /* size of ".autoload" */
573 if (strcmp(&lfile[len - 9], ".autoload") != 0)
579 char file[PLATFORM_MAX_PATH];
580 len = UTIL_Format(file, sizeof(file), "%s", lfile);
581 strcpy(&file[len - 9], ".ext");
583 LoadAutoExtension(file);
589 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
591 /* Remove platform extension if it's there. Compat hack. */
592 const char *ext = g_LibSys.GetFileExtension(path);
593 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
595 char path2[PLATFORM_MAX_PATH];
596 UTIL_Format(path2, sizeof(path2), "%s", path);
597 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
598 return LoadAutoExtension(path2, bErrorOnMissing);
601 IExtension *pAlready;
602 if ((pAlready=FindExtensionByFile(path)) != NULL)
608 CExtension *p = new CLocalExtension(path);
610 /* We put us in the list beforehand so extensions that check for each other
611 * won't recursively load each other.
615 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
617 if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
619 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
628 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
630 List<CExtension *>::iterator iter;
633 /* Chomp off the path */
634 char lookup[PLATFORM_MAX_PATH];
635 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
637 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
640 if (pExt->IsSameFile(lookup))
649 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
651 List<CExtension *>::iterator iter;
653 IExtensionInterface *pAPI;
656 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
659 if (!pExt->IsLoaded())
663 if ((pAPI = pExt->GetAPI()) == NULL)
667 name = pAPI->GetExtensionName();
672 if (strcmp(name, ext) == 0)
681 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
683 /* Remove platform extension if it's there. Compat hack. */
684 const char *ext = g_LibSys.GetFileExtension(file);
685 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
687 char path2[PLATFORM_MAX_PATH];
688 UTIL_Format(path2, sizeof(path2), "%s", file);
689 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
690 return LoadExtension(path2, error, maxlength);
693 IExtension *pAlready;
694 if ((pAlready=FindExtensionByFile(file)) != NULL)
699 CExtension *pExt = new CLocalExtension(file);
701 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
708 /* :TODO: do QueryRunning check if the map is loaded */
710 m_Libs.push_back(pExt);
715 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
717 CExtension *pExt = (CExtension *)pRequester;
718 CExtension *pOwner = (CExtension *)pInfo->owner;
720 pExt->AddDependency(pInfo);
722 IExtensionInterface *pAPI = pExt->GetAPI();
723 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
725 pOwner->AddChildDependent(pExt, pInfo->iface);
729 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
731 CExtension *pExt = (CExtension *)ext;
732 CExtension *pOwner = GetExtensionFromIdent(other);
736 info.iface = (SMInterface *)iface;
739 pExt->AddDependency(&info);
740 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
743 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
745 CExtension *pExt = (CExtension *)pOwner;
747 pExt->AddInterface(pInterface);
750 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
752 CExtension *pExt = (CExtension *)pParent;
754 pExt->AddPlugin(pPlugin);
757 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
759 List<CExtension *>::iterator iter;
761 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
763 (*iter)->DropRefsTo((CPlugin *)plugin);
767 CExtension *CExtensionManager::FindByOrder(unsigned int num)
769 if (num < 1 || num > m_Libs.size())
774 List<CExtension *>::iterator iter = m_Libs.begin();
776 while (iter != m_Libs.end())
788 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
795 CExtension *pExt = (CExtension *)_pExt;
797 if (m_Libs.find(pExt) == m_Libs.end())
802 /* First remove us from internal lists */
803 g_ShareSys.RemoveInterfaces(_pExt);
806 List<CExtension *> UnloadQueue;
808 /* Handle dependencies */
809 if (pExt->IsLoaded())
811 /* Unload any dependent plugins */
812 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
813 while (p_iter != pExt->m_Dependents.end())
815 /* We have to manually unlink ourselves here, since we're no longer being managed */
816 g_PluginSys.UnloadPlugin((*p_iter));
817 p_iter = pExt->m_Dependents.erase(p_iter);
820 List<String>::iterator s_iter;
821 for (s_iter = pExt->m_Libraries.begin();
822 s_iter != pExt->m_Libraries.end();
825 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
828 /* Notify and/or unload all dependencies */
829 List<CExtension *>::iterator c_iter;
831 IExtensionInterface *pAPI;
832 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
835 if ((pAPI=pDep->GetAPI()) == NULL)
843 /* Now, get its dependency list */
844 bool dropped = false;
845 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
846 while (i_iter != pDep->m_Deps.end())
848 if ((*i_iter).owner == _pExt)
850 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
855 UnloadQueue.push_back(pDep);
858 pAPI->NotifyInterfaceDrop((*i_iter).iface);
859 i_iter = pDep->m_Deps.erase(i_iter);
866 /* Flush out any back references to this plugin */
867 i_iter = pDep->m_ChildDeps.begin();
868 while (i_iter != pDep->m_ChildDeps.end())
870 if ((*i_iter).owner == pExt)
872 i_iter = pDep->m_ChildDeps.erase(i_iter);
881 /* Unbind our natives from Core */
882 pExt->DropEverything();
885 IdentityToken_t *pIdentity;
886 if ((pIdentity = pExt->GetIdentity()) != NULL)
888 SMGlobalClass *glob = SMGlobalClass::head;
891 glob->OnSourceModIdentityDropped(pIdentity);
892 glob = glob->m_pGlobalClassNext;
896 /* Tell it to unload */
897 if (pExt->IsLoaded())
899 IExtensionInterface *pAPI = pExt->GetAPI();
900 pAPI->OnExtensionUnload();
906 List<CExtension *>::iterator iter;
907 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
909 /* NOTE: This is safe because the unload function backs out of anything not present */
910 UnloadExtension((*iter));
916 void CExtensionManager::MarkAllLoaded()
918 List<CExtension *>::iterator iter;
921 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
924 if (!pExt->IsLoaded())
928 if (pExt->m_bFullyLoaded)
932 pExt->MarkAllLoaded();
936 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
938 /* This function doesn't really need to do anything now. We make sure the
939 * other extension is loaded, but handling of dependencies is really done
940 * by the interface fetcher.
942 if (required || autoload)
944 LoadAutoExtension(file);
948 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
950 int argcount = command.ArgC();
953 const char *cmd = command.Arg(2);
954 if (strcmp(cmd, "list") == 0)
956 List<CExtension *>::iterator iter;
958 unsigned int num = 1;
959 switch (m_Libs.size())
963 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
968 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
973 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
977 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
980 if (pExt->IsLoaded())
983 if (!pExt->IsRunning(error, sizeof(error)))
985 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
989 IExtensionInterface *pAPI = pExt->GetAPI();
990 const char *name = pAPI->GetExtensionName();
991 const char *version = pAPI->GetExtensionVerString();
992 const char *descr = pAPI->GetExtensionDescription();
993 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
996 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1001 else if (strcmp(cmd, "load") == 0)
1005 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
1009 const char *filename = command.Arg(3);
1010 char path[PLATFORM_MAX_PATH];
1013 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1014 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1016 if (FindExtensionByFile(path) != NULL)
1018 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1022 if (LoadExtension(path, error, sizeof(error)))
1024 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1027 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1032 else if (strcmp(cmd, "info") == 0)
1036 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1040 const char *sId = command.Arg(3);
1041 unsigned int id = atoi(sId);
1044 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1048 if (m_Libs.size() == 0)
1050 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1054 if (id > m_Libs.size())
1056 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1060 List<CExtension *>::iterator iter = m_Libs.begin();
1061 CExtension *pExt = NULL;
1062 while (iter != m_Libs.end())
1071 /* This should never happen */
1074 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1078 if (!pExt->IsLoaded())
1080 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1081 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1086 if (!pExt->IsRunning(error, sizeof(error)))
1088 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1089 g_RootMenu.ConsolePrint(" Loaded: Yes");
1090 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1094 IExtensionInterface *pAPI = pExt->GetAPI();
1095 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1096 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1097 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1098 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1099 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1101 if (pExt->IsExternal())
1103 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1105 else if (pAPI->IsMetamodExtension())
1107 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1111 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1117 else if (strcmp(cmd, "unload") == 0)
1121 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1125 const char *arg = command.Arg(3);
1126 unsigned int num = atoi(arg);
1127 CExtension *pExt = FindByOrder(num);
1131 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1135 if (argcount > 4 && pExt->unload_code)
1137 const char *unload = command.Arg(4);
1138 if (pExt->unload_code == (unsigned)atoi(unload))
1140 char filename[PLATFORM_MAX_PATH];
1141 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1142 UnloadExtension(pExt);
1143 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1147 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1152 if (!pExt->IsLoaded()
1153 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1155 char filename[PLATFORM_MAX_PATH];
1156 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1157 UnloadExtension(pExt);
1158 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1163 List<CPlugin *> plugins;
1164 if (pExt->m_ChildDeps.size())
1166 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1167 List<CExtension *>::iterator iter;
1169 /* Get list of all extensions */
1170 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1172 List<IfaceInfo>::iterator i_iter;
1174 if (!pOther->IsLoaded() || pOther == pExt)
1178 /* Get their dependencies */
1179 for (i_iter=pOther->m_Deps.begin();
1180 i_iter!=pOther->m_Deps.end();
1183 /* Is this dependency to us? */
1184 if ((*i_iter).owner != pExt)
1188 /* Will our dependent care? */
1189 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1191 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1192 /* Add to plugin unload list */
1193 List<CPlugin *>::iterator p_iter;
1194 for (p_iter=pOther->m_Dependents.begin();
1195 p_iter!=pOther->m_Dependents.end();
1198 if (plugins.find((*p_iter)) == plugins.end())
1200 plugins.push_back((*p_iter));
1207 if (pExt->m_Dependents.size())
1209 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1210 List<CPlugin *>::iterator iter;
1212 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1215 if (plugins.find(pPlugin) == plugins.end())
1217 plugins.push_back(pPlugin);
1220 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1223 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1226 srand(static_cast<int>(time(NULL)));
1227 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1228 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1229 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1234 else if (strcmp(cmd, "reload") == 0)
1238 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1242 const char *arg = command.Arg(3);
1243 unsigned int num = atoi(arg);
1244 CExtension *pExt = FindByOrder(num);
1248 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1252 if (pExt->IsLoaded())
1254 char filename[PLATFORM_MAX_PATH];
1257 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1259 if (pExt->Reload(error, sizeof(error)))
1261 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1265 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1272 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1280 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1281 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1282 g_RootMenu.DrawGenericOption("list", "List extensions");
1283 g_RootMenu.DrawGenericOption("load", "Load an extension");
1284 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1285 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1288 void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
1291 int numExtensions = m_Libs.size();
1292 edict_t *edict = player->GetEdict();
1293 unsigned int id = 0;
1294 unsigned int start = 0;
1298 ClientConsolePrint(edict, "[SM] No extensions found.");
1302 if (args.ArgC() > 2)
1304 start = atoi(args.Arg(2));
1308 SourceHook::List<CExtension *>::iterator iter;
1310 for (iter = m_Libs.begin();
1311 iter != m_Libs.end();
1317 if (!ext->IsRunning(error, sizeof(error)))
1328 if (id - start > 10)
1333 IExtensionInterface *api = ext->GetAPI();
1335 const char *name = api->GetExtensionName();
1336 const char *version = api->GetExtensionVerString();
1337 const char *author = api->GetExtensionAuthor();
1338 const char *description = api->GetExtensionDescription();
1340 size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
1342 if (version != NULL && IS_STR_FILLED(version))
1344 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
1347 if (author != NULL && IS_STR_FILLED(author))
1349 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
1352 if (description != NULL && IS_STR_FILLED(description))
1354 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
1358 ClientConsolePrint(edict, "%s", buffer);
1361 while (iter != m_Libs.end())
1364 if ((*iter)->IsRunning(error, sizeof(error)))
1370 if (iter != m_Libs.end())
1372 ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
1376 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1378 if (ptr->type == g_ExtType)
1380 return (CExtension *)(ptr->ptr);
1386 CExtensionManager::CExtensionManager()
1390 CExtensionManager::~CExtensionManager()
1394 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1396 CExtension *pExt = (CExtension *)pSource;
1397 pExt->AddLibrary(library);
1398 g_PluginSys.OnLibraryAction(library, false, false);
1401 bool CExtensionManager::LibraryExists(const char *library)
1405 for (List<CExtension *>::iterator iter = m_Libs.begin();
1406 iter != m_Libs.end();
1410 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1411 s_iter != pExt->m_Libraries.end();
1414 if ((*s_iter).compare(library) == 0)
1424 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1425 const char *filepath,
1426 const char *filename,
1430 IExtension *pAlready;
1431 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1436 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1438 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1445 m_Libs.push_back(pExt);
1450 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1452 IExtensionInterface *pAPI;
1453 List<CExtension *>::iterator iter;
1455 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1457 if ((pAPI = (*iter)->GetAPI()) == NULL)
1461 if (pAPI->GetExtensionVersion() > 3)
1463 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1468 bool CLocalExtension::IsSameFile(const char *file)
1470 /* Only care about the shortened name. */
1471 return strcmp(file, m_File.c_str()) == 0;
1474 bool CRemoteExtension::IsSameFile(const char *file)
1476 /* :TODO: this could be better, but no one uses this API anyway. */
1477 return strcmp(file, m_Path.c_str()) == 0;