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 const char *CExtension::GetPath() const
377 return m_Path.c_str();
380 IdentityToken_t *CExtension::GetIdentity()
382 return m_pIdentToken;
385 bool CRemoteExtension::IsLoaded()
387 return (m_pAPI != NULL);
390 bool CLocalExtension::IsLoaded()
392 return (m_pLib != NULL);
395 void CExtension::AddDependency(const IfaceInfo *pInfo)
397 if (m_Deps.find(*pInfo) == m_Deps.end())
399 m_Deps.push_back(*pInfo);
403 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
405 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
408 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
414 List<IfaceInfo>::iterator iter;
415 for (iter = m_ChildDeps.begin();
416 iter != m_ChildDeps.end();
419 IfaceInfo &other = (*iter);
426 m_ChildDeps.push_back(info);
429 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
431 List<IfaceInfo>::iterator iter = m_Deps.begin();
433 if (iter == m_Deps.end())
440 *pOwner = (*iter).owner;
444 *pInterface = (*iter).iface;
447 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
449 return (ITERATOR *)pIter;
452 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
454 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
455 List<IfaceInfo>::iterator _iter;
457 if (_iter == m_Deps.end())
466 *pOwner = (*_iter).owner;
470 *pInterface = (*_iter).iface;
475 if (_iter == m_Deps.end())
483 void CExtension::FreeDependencyIterator(ITERATOR *iter)
485 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
490 void CExtension::AddInterface(SMInterface *pInterface)
492 m_Interfaces.push_back(pInterface);
495 bool CExtension::IsRunning(char *error, size_t maxlength)
501 snprintf(error, maxlength, "%s", m_Error.c_str());
506 return m_pAPI->QueryRunning(error, maxlength);
509 void CExtension::AddLibrary(const char *library)
511 m_Libraries.push_back(library);
514 /*********************
515 * EXTENSION MANAGER *
516 *********************/
518 void CExtensionManager::OnSourceModAllInitialized()
520 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
521 g_PluginSys.AddPluginsListener(this);
522 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
523 g_ShareSys.AddInterface(NULL, this);
526 void CExtensionManager::OnSourceModShutdown()
528 g_RootMenu.RemoveRootConsoleCommand("exts", this);
529 g_ShareSys.DestroyIdentType(g_ExtType);
532 void CExtensionManager::Shutdown()
534 List<CExtension *>::iterator iter;
536 while ((iter = m_Libs.begin()) != m_Libs.end())
538 UnloadExtension((*iter));
542 void CExtensionManager::TryAutoload()
544 char path[PLATFORM_MAX_PATH];
546 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
548 IDirectory *pDir = g_LibSys.OpenDirectory(path);
556 while (pDir->MoreFiles())
558 if (pDir->IsEntryDirectory())
564 lfile = pDir->GetEntryName();
566 if (len <= 9) /* size of ".autoload" */
572 if (strcmp(&lfile[len - 9], ".autoload") != 0)
578 char file[PLATFORM_MAX_PATH];
579 len = UTIL_Format(file, sizeof(file), "%s", lfile);
580 strcpy(&file[len - 9], ".ext");
582 LoadAutoExtension(file);
588 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
590 /* Remove platform extension if it's there. Compat hack. */
591 const char *ext = g_LibSys.GetFileExtension(path);
592 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
594 char path2[PLATFORM_MAX_PATH];
595 UTIL_Format(path2, sizeof(path2), "%s", path);
596 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
597 return LoadAutoExtension(path2, bErrorOnMissing);
600 IExtension *pAlready;
601 if ((pAlready=FindExtensionByFile(path)) != NULL)
607 CExtension *p = new CLocalExtension(path);
609 /* We put us in the list beforehand so extensions that check for each other
610 * won't recursively load each other.
614 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
616 if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
618 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
627 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
629 List<CExtension *>::iterator iter;
632 /* Chomp off the path */
633 char lookup[PLATFORM_MAX_PATH];
634 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
636 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
639 if (pExt->IsSameFile(lookup))
648 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
650 List<CExtension *>::iterator iter;
652 IExtensionInterface *pAPI;
655 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
658 if (!pExt->IsLoaded())
662 if ((pAPI = pExt->GetAPI()) == NULL)
666 name = pAPI->GetExtensionName();
671 if (strcmp(name, ext) == 0)
680 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
682 /* Remove platform extension if it's there. Compat hack. */
683 const char *ext = g_LibSys.GetFileExtension(file);
684 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
686 char path2[PLATFORM_MAX_PATH];
687 UTIL_Format(path2, sizeof(path2), "%s", file);
688 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
689 return LoadExtension(path2, error, maxlength);
692 IExtension *pAlready;
693 if ((pAlready=FindExtensionByFile(file)) != NULL)
698 CExtension *pExt = new CLocalExtension(file);
700 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
707 /* :TODO: do QueryRunning check if the map is loaded */
709 m_Libs.push_back(pExt);
714 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
716 CExtension *pExt = (CExtension *)pRequester;
717 CExtension *pOwner = (CExtension *)pInfo->owner;
719 pExt->AddDependency(pInfo);
721 IExtensionInterface *pAPI = pExt->GetAPI();
722 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
724 pOwner->AddChildDependent(pExt, pInfo->iface);
728 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
730 CExtension *pExt = (CExtension *)ext;
731 CExtension *pOwner = GetExtensionFromIdent(other);
735 info.iface = (SMInterface *)iface;
738 pExt->AddDependency(&info);
739 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
742 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
744 CExtension *pExt = (CExtension *)pOwner;
746 pExt->AddInterface(pInterface);
749 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
751 CExtension *pExt = (CExtension *)pParent;
753 pExt->AddPlugin(pPlugin);
756 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
758 List<CExtension *>::iterator iter;
760 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
762 (*iter)->DropRefsTo((CPlugin *)plugin);
766 CExtension *CExtensionManager::FindByOrder(unsigned int num)
768 if (num < 1 || num > m_Libs.size())
773 List<CExtension *>::iterator iter = m_Libs.begin();
775 while (iter != m_Libs.end())
787 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
794 CExtension *pExt = (CExtension *)_pExt;
796 if (m_Libs.find(pExt) == m_Libs.end())
801 /* First remove us from internal lists */
802 g_ShareSys.RemoveInterfaces(_pExt);
805 List<CExtension *> UnloadQueue;
807 /* Handle dependencies */
808 if (pExt->IsLoaded())
810 /* Unload any dependent plugins */
811 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
812 while (p_iter != pExt->m_Dependents.end())
814 /* We have to manually unlink ourselves here, since we're no longer being managed */
815 g_PluginSys.UnloadPlugin((*p_iter));
816 p_iter = pExt->m_Dependents.erase(p_iter);
819 List<String>::iterator s_iter;
820 for (s_iter = pExt->m_Libraries.begin();
821 s_iter != pExt->m_Libraries.end();
824 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
827 /* Notify and/or unload all dependencies */
828 List<CExtension *>::iterator c_iter;
830 IExtensionInterface *pAPI;
831 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
834 if ((pAPI=pDep->GetAPI()) == NULL)
842 /* Now, get its dependency list */
843 bool dropped = false;
844 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
845 while (i_iter != pDep->m_Deps.end())
847 if ((*i_iter).owner == _pExt)
849 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
854 UnloadQueue.push_back(pDep);
857 pAPI->NotifyInterfaceDrop((*i_iter).iface);
858 i_iter = pDep->m_Deps.erase(i_iter);
865 /* Flush out any back references to this plugin */
866 i_iter = pDep->m_ChildDeps.begin();
867 while (i_iter != pDep->m_ChildDeps.end())
869 if ((*i_iter).owner == pExt)
871 i_iter = pDep->m_ChildDeps.erase(i_iter);
880 /* Unbind our natives from Core */
881 pExt->DropEverything();
884 IdentityToken_t *pIdentity;
885 if ((pIdentity = pExt->GetIdentity()) != NULL)
887 SMGlobalClass *glob = SMGlobalClass::head;
890 glob->OnSourceModIdentityDropped(pIdentity);
891 glob = glob->m_pGlobalClassNext;
895 /* Tell it to unload */
896 if (pExt->IsLoaded())
898 IExtensionInterface *pAPI = pExt->GetAPI();
899 pAPI->OnExtensionUnload();
905 List<CExtension *>::iterator iter;
906 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
908 /* NOTE: This is safe because the unload function backs out of anything not present */
909 UnloadExtension((*iter));
915 void CExtensionManager::MarkAllLoaded()
917 List<CExtension *>::iterator iter;
920 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
923 if (!pExt->IsLoaded())
927 if (pExt->m_bFullyLoaded)
931 pExt->MarkAllLoaded();
935 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
937 /* This function doesn't really need to do anything now. We make sure the
938 * other extension is loaded, but handling of dependencies is really done
939 * by the interface fetcher.
941 if (required || autoload)
943 LoadAutoExtension(file);
947 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
949 int argcount = command.ArgC();
952 const char *cmd = command.Arg(2);
953 if (strcmp(cmd, "list") == 0)
955 List<CExtension *>::iterator iter;
957 unsigned int num = 1;
958 switch (m_Libs.size())
962 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
967 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
972 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
976 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
979 if (pExt->IsLoaded())
982 if (!pExt->IsRunning(error, sizeof(error)))
984 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
988 IExtensionInterface *pAPI = pExt->GetAPI();
989 const char *name = pAPI->GetExtensionName();
990 const char *version = pAPI->GetExtensionVerString();
991 const char *descr = pAPI->GetExtensionDescription();
992 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
995 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1000 else if (strcmp(cmd, "load") == 0)
1004 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
1008 const char *filename = command.Arg(3);
1009 char path[PLATFORM_MAX_PATH];
1012 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1013 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1015 if (FindExtensionByFile(path) != NULL)
1017 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
1021 if (LoadExtension(path, error, sizeof(error)))
1023 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
1026 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1031 else if (strcmp(cmd, "info") == 0)
1035 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1039 const char *sId = command.Arg(3);
1040 unsigned int id = atoi(sId);
1043 g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
1047 if (m_Libs.size() == 0)
1049 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1053 if (id > m_Libs.size())
1055 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1059 List<CExtension *>::iterator iter = m_Libs.begin();
1060 CExtension *pExt = NULL;
1061 while (iter != m_Libs.end())
1070 /* This should never happen */
1073 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1077 if (!pExt->IsLoaded())
1079 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1080 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1085 if (!pExt->IsRunning(error, sizeof(error)))
1087 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1088 g_RootMenu.ConsolePrint(" Loaded: Yes");
1089 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1093 IExtensionInterface *pAPI = pExt->GetAPI();
1094 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1095 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1096 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1097 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1098 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1100 if (pExt->IsExternal())
1102 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1104 else if (pAPI->IsMetamodExtension())
1106 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1110 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1116 else if (strcmp(cmd, "unload") == 0)
1120 g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1124 const char *arg = command.Arg(3);
1125 unsigned int num = atoi(arg);
1126 CExtension *pExt = FindByOrder(num);
1130 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1134 if (argcount > 4 && pExt->unload_code)
1136 const char *unload = command.Arg(4);
1137 if (pExt->unload_code == (unsigned)atoi(unload))
1139 char filename[PLATFORM_MAX_PATH];
1140 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1141 UnloadExtension(pExt);
1142 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1146 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1151 if (!pExt->IsLoaded()
1152 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1154 char filename[PLATFORM_MAX_PATH];
1155 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1156 UnloadExtension(pExt);
1157 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1162 List<CPlugin *> plugins;
1163 if (pExt->m_ChildDeps.size())
1165 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1166 List<CExtension *>::iterator iter;
1168 /* Get list of all extensions */
1169 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1171 List<IfaceInfo>::iterator i_iter;
1173 if (!pOther->IsLoaded() || pOther == pExt)
1177 /* Get their dependencies */
1178 for (i_iter=pOther->m_Deps.begin();
1179 i_iter!=pOther->m_Deps.end();
1182 /* Is this dependency to us? */
1183 if ((*i_iter).owner != pExt)
1187 /* Will our dependent care? */
1188 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1190 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1191 /* Add to plugin unload list */
1192 List<CPlugin *>::iterator p_iter;
1193 for (p_iter=pOther->m_Dependents.begin();
1194 p_iter!=pOther->m_Dependents.end();
1197 if (plugins.find((*p_iter)) == plugins.end())
1199 plugins.push_back((*p_iter));
1206 if (pExt->m_Dependents.size())
1208 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1209 List<CPlugin *>::iterator iter;
1211 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1214 if (plugins.find(pPlugin) == plugins.end())
1216 plugins.push_back(pPlugin);
1219 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1222 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1225 srand(static_cast<int>(time(NULL)));
1226 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1227 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1228 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1233 else if (strcmp(cmd, "reload") == 0)
1237 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1241 const char *arg = command.Arg(3);
1242 unsigned int num = atoi(arg);
1243 CExtension *pExt = FindByOrder(num);
1247 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1251 if (pExt->IsLoaded())
1253 char filename[PLATFORM_MAX_PATH];
1256 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1258 if (pExt->Reload(error, sizeof(error)))
1260 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1264 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1271 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1279 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1280 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1281 g_RootMenu.DrawGenericOption("list", "List extensions");
1282 g_RootMenu.DrawGenericOption("load", "Load an extension");
1283 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1284 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1287 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1289 if (ptr->type == g_ExtType)
1291 return (CExtension *)(ptr->ptr);
1297 CExtensionManager::CExtensionManager()
1301 CExtensionManager::~CExtensionManager()
1305 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1307 CExtension *pExt = (CExtension *)pSource;
1308 pExt->AddLibrary(library);
1309 g_PluginSys.OnLibraryAction(library, false, false);
1312 bool CExtensionManager::LibraryExists(const char *library)
1316 for (List<CExtension *>::iterator iter = m_Libs.begin();
1317 iter != m_Libs.end();
1321 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1322 s_iter != pExt->m_Libraries.end();
1325 if ((*s_iter).compare(library) == 0)
1335 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1336 const char *filepath,
1337 const char *filename,
1341 IExtension *pAlready;
1342 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1347 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1349 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1356 m_Libs.push_back(pExt);
1361 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1363 IExtensionInterface *pAPI;
1364 List<CExtension *>::iterator iter;
1366 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1368 if ((pAPI = (*iter)->GetAPI()) == NULL)
1372 if (pAPI->GetExtensionVersion() > 3)
1374 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1379 bool CLocalExtension::IsSameFile(const char *file)
1381 /* Only care about the shortened name. */
1382 return strcmp(file, m_File.c_str()) == 0;
1385 bool CRemoteExtension::IsSameFile(const char *file)
1387 /* :TODO: this could be better, but no one uses this API anyway. */
1388 return strcmp(file, m_Path.c_str()) == 0;