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"
81 #elif SOURCE_ENGINE == SE_PORTAL2
82 #define GAMEFIX "2.portal2"
83 #elif SOURCE_ENGINE == SE_CSGO
84 #define GAMEFIX "2.csgo"
86 #define GAMEFIX "2.ep1"
87 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
88 #else //METAMOD_PLAPI_VERSION
89 #define GAMEFIX "1.ep1"
90 #endif //METAMOD_PLAPI_VERSION
92 CLocalExtension::CLocalExtension(const char *filename)
97 char path[PLATFORM_MAX_PATH];
99 /* Special case for new bintools binary */
100 if (strcmp(filename, "bintools.ext") == 0)
105 /* Zeroth, see if there is an engine specific build in the new place. */
106 g_SourceMod.BuildPath(Path_SM,
109 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
112 if (g_LibSys.IsPathFile(path))
117 /* First see if there is an engine specific build! */
118 g_SourceMod.BuildPath(Path_SM,
121 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
125 /* Try the "normal" version */
126 if (!g_LibSys.IsPathFile(path))
128 g_SourceMod.BuildPath(Path_SM,
131 "extensions/%s." PLATFORM_LIB_EXT,
136 Initialize(filename, path);
139 bool CRemoteExtension::Load(char *error, size_t maxlength)
141 if (!PerformAPICheck(error, maxlength))
147 if (!CExtension::Load(error, maxlength))
156 bool CLocalExtension::Load(char *error, size_t maxlength)
158 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
165 typedef IExtensionInterface *(*GETAPI)();
166 GETAPI pfnGetAPI = NULL;
168 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
170 m_pLib->CloseLibrary();
172 snprintf(error, maxlength, "Unable to find extension entry point");
176 m_pAPI = pfnGetAPI();
178 /* Check pointer and version */
179 if (!PerformAPICheck(error, maxlength))
181 m_pLib->CloseLibrary();
188 if (m_pAPI->IsMetamodExtension())
191 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
193 // Check the plugin didn't refuse load
196 #ifndef METAMOD_PLAPI_VERSION
202 #ifndef METAMOD_PLAPI_VERSION
203 g_pMMPlugins->Query(m_PlId, file, status, source)
205 g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
207 && status < Pl_Paused))
209 m_pLib->CloseLibrary();
216 if (!CExtension::Load(error, maxlength))
218 if (m_pAPI->IsMetamodExtension())
223 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
227 m_pLib->CloseLibrary();
236 void CRemoteExtension::Unload()
240 void CLocalExtension::Unload()
242 if (m_pAPI != NULL && m_PlId)
244 char temp_buffer[255];
245 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
251 m_pLib->CloseLibrary();
256 bool CRemoteExtension::Reload(char *error, size_t maxlength)
258 snprintf(error, maxlength, "Remote extensions do not support reloading");
262 bool CLocalExtension::Reload(char *error, size_t maxlength)
264 if (m_pLib == NULL) // FIXME: just load it instead?
267 m_pAPI->OnExtensionUnload();
270 return Load(error, maxlength);
273 bool CRemoteExtension::IsExternal()
278 bool CLocalExtension::IsExternal()
283 CExtension::~CExtension()
288 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
292 snprintf(error, maxlength, "No IExtensionInterface instance provided");
296 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
298 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
305 bool CExtension::Load(char *error, size_t maxlength)
308 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
315 /* Check if we're past load time */
316 if (!g_SourceMod.IsMapLoading())
318 m_pAPI->OnExtensionsAllLoaded();
325 void CExtension::CreateIdentity()
327 if (m_pIdentToken != NULL)
332 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
335 void CExtension::DestroyIdentity()
337 if (m_pIdentToken == NULL)
342 g_ShareSys.DestroyIdentity(m_pIdentToken);
343 m_pIdentToken = NULL;
346 void CExtension::MarkAllLoaded()
348 assert(m_bFullyLoaded == false);
351 m_bFullyLoaded = true;
352 m_pAPI->OnExtensionsAllLoaded();
356 void CExtension::AddPlugin(CPlugin *pPlugin)
358 /* Unfortunately we have to do this :( */
359 if (m_Dependents.find(pPlugin) != m_Dependents.end())
361 m_Dependents.push_back(pPlugin);
365 void CExtension::SetError(const char *error)
367 m_Error.assign(error);
370 IExtensionInterface *CExtension::GetAPI()
375 const char *CExtension::GetFilename()
377 return m_RealFile.c_str();
380 const char *CExtension::GetPath() const
382 return m_Path.c_str();
385 IdentityToken_t *CExtension::GetIdentity()
387 return m_pIdentToken;
390 bool CRemoteExtension::IsLoaded()
392 return (m_pAPI != NULL);
395 bool CLocalExtension::IsLoaded()
397 return (m_pLib != NULL);
400 void CExtension::AddDependency(const IfaceInfo *pInfo)
402 if (m_Deps.find(*pInfo) == m_Deps.end())
404 m_Deps.push_back(*pInfo);
408 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
410 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
413 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
419 List<IfaceInfo>::iterator iter;
420 for (iter = m_ChildDeps.begin();
421 iter != m_ChildDeps.end();
424 IfaceInfo &other = (*iter);
431 m_ChildDeps.push_back(info);
434 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
436 List<IfaceInfo>::iterator iter = m_Deps.begin();
438 if (iter == m_Deps.end())
445 *pOwner = (*iter).owner;
449 *pInterface = (*iter).iface;
452 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
454 return (ITERATOR *)pIter;
457 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
459 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
460 List<IfaceInfo>::iterator _iter;
462 if (_iter == m_Deps.end())
471 *pOwner = (*_iter).owner;
475 *pInterface = (*_iter).iface;
480 if (_iter == m_Deps.end())
488 void CExtension::FreeDependencyIterator(ITERATOR *iter)
490 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
495 void CExtension::AddInterface(SMInterface *pInterface)
497 m_Interfaces.push_back(pInterface);
500 bool CExtension::IsRunning(char *error, size_t maxlength)
506 snprintf(error, maxlength, "%s", m_Error.c_str());
511 return m_pAPI->QueryRunning(error, maxlength);
514 void CExtension::AddLibrary(const char *library)
516 m_Libraries.push_back(library);
519 /*********************
520 * EXTENSION MANAGER *
521 *********************/
523 void CExtensionManager::OnSourceModAllInitialized()
525 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
526 g_PluginSys.AddPluginsListener(this);
527 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
528 g_ShareSys.AddInterface(NULL, this);
531 void CExtensionManager::OnSourceModShutdown()
533 g_RootMenu.RemoveRootConsoleCommand("exts", this);
534 g_ShareSys.DestroyIdentType(g_ExtType);
537 void CExtensionManager::Shutdown()
539 List<CExtension *>::iterator iter;
541 while ((iter = m_Libs.begin()) != m_Libs.end())
543 UnloadExtension((*iter));
547 void CExtensionManager::TryAutoload()
549 char path[PLATFORM_MAX_PATH];
551 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
553 IDirectory *pDir = g_LibSys.OpenDirectory(path);
561 while (pDir->MoreFiles())
563 if (pDir->IsEntryDirectory())
569 lfile = pDir->GetEntryName();
571 if (len <= 9) /* size of ".autoload" */
577 if (strcmp(&lfile[len - 9], ".autoload") != 0)
583 char file[PLATFORM_MAX_PATH];
584 len = UTIL_Format(file, sizeof(file), "%s", lfile);
585 strcpy(&file[len - 9], ".ext");
587 LoadAutoExtension(file);
593 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
595 /* Remove platform extension if it's there. Compat hack. */
596 const char *ext = g_LibSys.GetFileExtension(path);
597 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
599 char path2[PLATFORM_MAX_PATH];
600 UTIL_Format(path2, sizeof(path2), "%s", path);
601 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
602 return LoadAutoExtension(path2, bErrorOnMissing);
605 IExtension *pAlready;
606 if ((pAlready=FindExtensionByFile(path)) != NULL)
612 CExtension *p = new CLocalExtension(path);
614 /* We put us in the list beforehand so extensions that check for each other
615 * won't recursively load each other.
619 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
621 if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
623 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
632 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
634 List<CExtension *>::iterator iter;
637 /* Chomp off the path */
638 char lookup[PLATFORM_MAX_PATH];
639 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
641 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
644 if (pExt->IsSameFile(lookup))
653 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
655 List<CExtension *>::iterator iter;
657 IExtensionInterface *pAPI;
660 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
663 if (!pExt->IsLoaded())
667 if ((pAPI = pExt->GetAPI()) == NULL)
671 name = pAPI->GetExtensionName();
676 if (strcmp(name, ext) == 0)
685 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
687 /* Remove platform extension if it's there. Compat hack. */
688 const char *ext = g_LibSys.GetFileExtension(file);
689 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
691 char path2[PLATFORM_MAX_PATH];
692 UTIL_Format(path2, sizeof(path2), "%s", file);
693 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
694 return LoadExtension(path2, error, maxlength);
697 IExtension *pAlready;
698 if ((pAlready=FindExtensionByFile(file)) != NULL)
703 CExtension *pExt = new CLocalExtension(file);
705 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
712 /* :TODO: do QueryRunning check if the map is loaded */
714 m_Libs.push_back(pExt);
719 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
721 CExtension *pExt = (CExtension *)pRequester;
722 CExtension *pOwner = (CExtension *)pInfo->owner;
724 pExt->AddDependency(pInfo);
726 IExtensionInterface *pAPI = pExt->GetAPI();
727 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
729 pOwner->AddChildDependent(pExt, pInfo->iface);
733 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
735 CExtension *pExt = (CExtension *)ext;
736 CExtension *pOwner = GetExtensionFromIdent(other);
740 info.iface = (SMInterface *)iface;
743 pExt->AddDependency(&info);
744 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
747 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
749 CExtension *pExt = (CExtension *)pOwner;
751 pExt->AddInterface(pInterface);
754 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
756 CExtension *pExt = (CExtension *)pParent;
758 pExt->AddPlugin(pPlugin);
761 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
763 List<CExtension *>::iterator iter;
765 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
767 (*iter)->DropRefsTo((CPlugin *)plugin);
771 CExtension *CExtensionManager::FindByOrder(unsigned int num)
773 if (num < 1 || num > m_Libs.size())
778 List<CExtension *>::iterator iter = m_Libs.begin();
780 while (iter != m_Libs.end())
792 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
799 CExtension *pExt = (CExtension *)_pExt;
801 if (m_Libs.find(pExt) == m_Libs.end())
806 /* First remove us from internal lists */
807 g_ShareSys.RemoveInterfaces(_pExt);
810 List<CExtension *> UnloadQueue;
812 /* Handle dependencies */
813 if (pExt->IsLoaded())
815 /* Unload any dependent plugins */
816 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
817 while (p_iter != pExt->m_Dependents.end())
819 /* We have to manually unlink ourselves here, since we're no longer being managed */
820 g_PluginSys.UnloadPlugin((*p_iter));
821 p_iter = pExt->m_Dependents.erase(p_iter);
824 List<String>::iterator s_iter;
825 for (s_iter = pExt->m_Libraries.begin();
826 s_iter != pExt->m_Libraries.end();
829 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
832 /* Notify and/or unload all dependencies */
833 List<CExtension *>::iterator c_iter;
835 IExtensionInterface *pAPI;
836 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
839 if ((pAPI=pDep->GetAPI()) == NULL)
847 /* Now, get its dependency list */
848 bool dropped = false;
849 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
850 while (i_iter != pDep->m_Deps.end())
852 if ((*i_iter).owner == _pExt)
854 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
859 UnloadQueue.push_back(pDep);
862 pAPI->NotifyInterfaceDrop((*i_iter).iface);
863 i_iter = pDep->m_Deps.erase(i_iter);
870 /* Flush out any back references to this plugin */
871 i_iter = pDep->m_ChildDeps.begin();
872 while (i_iter != pDep->m_ChildDeps.end())
874 if ((*i_iter).owner == pExt)
876 i_iter = pDep->m_ChildDeps.erase(i_iter);
885 /* Unbind our natives from Core */
886 pExt->DropEverything();
889 IdentityToken_t *pIdentity;
890 if ((pIdentity = pExt->GetIdentity()) != NULL)
892 SMGlobalClass *glob = SMGlobalClass::head;
895 glob->OnSourceModIdentityDropped(pIdentity);
896 glob = glob->m_pGlobalClassNext;
900 /* Tell it to unload */
901 if (pExt->IsLoaded())
903 IExtensionInterface *pAPI = pExt->GetAPI();
904 pAPI->OnExtensionUnload();
910 List<CExtension *>::iterator iter;
911 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
913 /* NOTE: This is safe because the unload function backs out of anything not present */
914 UnloadExtension((*iter));
920 void CExtensionManager::MarkAllLoaded()
922 List<CExtension *>::iterator iter;
925 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
928 if (!pExt->IsLoaded())
932 if (pExt->m_bFullyLoaded)
936 pExt->MarkAllLoaded();
940 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
942 /* This function doesn't really need to do anything now. We make sure the
943 * other extension is loaded, but handling of dependencies is really done
944 * by the interface fetcher.
946 if (required || autoload)
948 LoadAutoExtension(file);
952 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
954 int argcount = command.ArgC();
957 const char *cmd = command.Arg(2);
958 if (strcmp(cmd, "list") == 0)
960 List<CExtension *>::iterator iter;
962 unsigned int num = 1;
963 switch (m_Libs.size())
967 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
972 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
977 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
981 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
984 if (pExt->IsLoaded())
987 if (!pExt->IsRunning(error, sizeof(error)))
989 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
993 IExtensionInterface *pAPI = pExt->GetAPI();
994 const char *name = pAPI->GetExtensionName();
995 const char *version = pAPI->GetExtensionVerString();
996 const char *descr = pAPI->GetExtensionDescription();
997 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
1000 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1005 else if (strcmp(cmd, "load") == 0)
1009 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
1013 const char *filename = command.Arg(3);
1014 char path[PLATFORM_MAX_PATH];
1017 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1018 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1020 if (FindExtensionByFile(path) != NULL)
1022 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1026 if (LoadExtension(path, error, sizeof(error)))
1028 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1031 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1036 else if (strcmp(cmd, "info") == 0)
1040 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1044 const char *sId = command.Arg(3);
1045 unsigned int id = atoi(sId);
1048 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1052 if (m_Libs.size() == 0)
1054 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1058 if (id > m_Libs.size())
1060 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1064 List<CExtension *>::iterator iter = m_Libs.begin();
1065 CExtension *pExt = NULL;
1066 while (iter != m_Libs.end())
1075 /* This should never happen */
1078 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1082 if (!pExt->IsLoaded())
1084 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1085 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1090 if (!pExt->IsRunning(error, sizeof(error)))
1092 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1093 g_RootMenu.ConsolePrint(" Loaded: Yes");
1094 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1098 IExtensionInterface *pAPI = pExt->GetAPI();
1099 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1100 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1101 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1102 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1103 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1105 if (pExt->IsExternal())
1107 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1109 else if (pAPI->IsMetamodExtension())
1111 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1115 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1121 else if (strcmp(cmd, "unload") == 0)
1125 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1129 const char *arg = command.Arg(3);
1130 unsigned int num = atoi(arg);
1131 CExtension *pExt = FindByOrder(num);
1135 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1139 if (argcount > 4 && pExt->unload_code)
1141 const char *unload = command.Arg(4);
1142 if (pExt->unload_code == (unsigned)atoi(unload))
1144 char filename[PLATFORM_MAX_PATH];
1145 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1146 UnloadExtension(pExt);
1147 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1151 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1156 if (!pExt->IsLoaded()
1157 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1159 char filename[PLATFORM_MAX_PATH];
1160 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1161 UnloadExtension(pExt);
1162 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1167 List<CPlugin *> plugins;
1168 if (pExt->m_ChildDeps.size())
1170 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1171 List<CExtension *>::iterator iter;
1173 /* Get list of all extensions */
1174 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1176 List<IfaceInfo>::iterator i_iter;
1178 if (!pOther->IsLoaded() || pOther == pExt)
1182 /* Get their dependencies */
1183 for (i_iter=pOther->m_Deps.begin();
1184 i_iter!=pOther->m_Deps.end();
1187 /* Is this dependency to us? */
1188 if ((*i_iter).owner != pExt)
1192 /* Will our dependent care? */
1193 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1195 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1196 /* Add to plugin unload list */
1197 List<CPlugin *>::iterator p_iter;
1198 for (p_iter=pOther->m_Dependents.begin();
1199 p_iter!=pOther->m_Dependents.end();
1202 if (plugins.find((*p_iter)) == plugins.end())
1204 plugins.push_back((*p_iter));
1211 if (pExt->m_Dependents.size())
1213 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1214 List<CPlugin *>::iterator iter;
1216 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1219 if (plugins.find(pPlugin) == plugins.end())
1221 plugins.push_back(pPlugin);
1224 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1227 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1230 srand(static_cast<int>(time(NULL)));
1231 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1232 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1233 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1238 else if (strcmp(cmd, "reload") == 0)
1242 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1246 const char *arg = command.Arg(3);
1247 unsigned int num = atoi(arg);
1248 CExtension *pExt = FindByOrder(num);
1252 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1256 if (pExt->IsLoaded())
1258 char filename[PLATFORM_MAX_PATH];
1261 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1263 if (pExt->Reload(error, sizeof(error)))
1265 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1269 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1276 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1284 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1285 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1286 g_RootMenu.DrawGenericOption("list", "List extensions");
1287 g_RootMenu.DrawGenericOption("load", "Load an extension");
1288 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1289 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1292 void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
1295 int numExtensions = m_Libs.size();
1296 edict_t *edict = player->GetEdict();
1297 unsigned int id = 0;
1298 unsigned int start = 0;
1302 ClientConsolePrint(edict, "[SM] No extensions found.");
1306 if (args.ArgC() > 2)
1308 start = atoi(args.Arg(2));
1312 SourceHook::List<CExtension *>::iterator iter;
1314 for (iter = m_Libs.begin();
1315 iter != m_Libs.end();
1321 if (!ext->IsRunning(error, sizeof(error)))
1332 if (id - start > 10)
1337 IExtensionInterface *api = ext->GetAPI();
1339 const char *name = api->GetExtensionName();
1340 const char *version = api->GetExtensionVerString();
1341 const char *author = api->GetExtensionAuthor();
1342 const char *description = api->GetExtensionDescription();
1344 size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
1346 if (version != NULL && IS_STR_FILLED(version))
1348 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
1351 if (author != NULL && IS_STR_FILLED(author))
1353 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
1356 if (description != NULL && IS_STR_FILLED(description))
1358 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
1362 ClientConsolePrint(edict, "%s", buffer);
1365 while (iter != m_Libs.end())
1368 if ((*iter)->IsRunning(error, sizeof(error)))
1374 if (iter != m_Libs.end())
1376 ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
1380 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1382 if (ptr->type == g_ExtType)
1384 return (CExtension *)(ptr->ptr);
1390 CExtensionManager::CExtensionManager()
1394 CExtensionManager::~CExtensionManager()
1398 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1400 CExtension *pExt = (CExtension *)pSource;
1401 pExt->AddLibrary(library);
1402 g_PluginSys.OnLibraryAction(library, false, false);
1405 bool CExtensionManager::LibraryExists(const char *library)
1409 for (List<CExtension *>::iterator iter = m_Libs.begin();
1410 iter != m_Libs.end();
1414 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1415 s_iter != pExt->m_Libraries.end();
1418 if ((*s_iter).compare(library) == 0)
1428 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1429 const char *filepath,
1430 const char *filename,
1434 IExtension *pAlready;
1435 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1440 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1442 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1449 m_Libs.push_back(pExt);
1454 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1456 IExtensionInterface *pAPI;
1457 List<CExtension *>::iterator iter;
1459 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1461 if ((pAPI = (*iter)->GetAPI()) == NULL)
1465 if (pAPI->GetExtensionVersion() > 3)
1467 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1472 bool CLocalExtension::IsSameFile(const char *file)
1474 /* Only care about the shortened name. */
1475 return strcmp(file, m_File.c_str()) == 0;
1478 bool CRemoteExtension::IsSameFile(const char *file)
1480 /* :TODO: this could be better, but no one uses this API anyway. */
1481 return strcmp(file, m_Path.c_str()) == 0;