3 * =============================================================================
5 * Copyright (C) 2004-2008 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);
54 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
56 Initialize(filename, path);
60 CLocalExtension::CLocalExtension(const char *filename)
65 char path[PLATFORM_MAX_PATH];
67 /* First see if there is an engine specific build! */
68 g_SourceMod.BuildPath(Path_SM,
71 #if defined METAMOD_PLAPI_VERSION
72 #if defined ORANGEBOX_BUILD
73 "extensions/auto.2.ep2/%s",
75 "extensions/auto.2.ep1/%s",
76 #endif //ORANGEBOX_BUILD
77 #else //METAMOD_PLAPI_VERSION
78 "extensions/auto.1.ep1/%s",
79 #endif //METAMOD_PLAPI_VERSION
82 /* Try the "normal" version */
83 if (!g_LibSys.IsPathFile(path))
85 g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
88 Initialize(filename, path);
91 bool CRemoteExtension::Load(char *error, size_t maxlength)
93 if (!PerformAPICheck(error, maxlength))
99 if (!CExtension::Load(error, maxlength))
108 bool CLocalExtension::Load(char *error, size_t maxlength)
110 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
117 typedef IExtensionInterface *(*GETAPI)();
118 GETAPI pfnGetAPI = NULL;
120 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
122 m_pLib->CloseLibrary();
124 snprintf(error, maxlength, "Unable to find extension entry point");
128 m_pAPI = pfnGetAPI();
130 /* Check pointer and version */
131 if (!PerformAPICheck(error, maxlength))
133 m_pLib->CloseLibrary();
140 if (m_pAPI->IsMetamodExtension())
143 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
146 if (!CExtension::Load(error, maxlength))
148 if (m_pAPI->IsMetamodExtension())
153 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
157 m_pLib->CloseLibrary();
166 void CRemoteExtension::Unload()
170 void CLocalExtension::Unload()
172 if (m_pAPI != NULL && m_PlId)
174 char temp_buffer[255];
175 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
181 m_pLib->CloseLibrary();
186 bool CRemoteExtension::IsExternal()
191 bool CLocalExtension::IsExternal()
196 CExtension::~CExtension()
201 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
205 snprintf(error, maxlength, "No IExtensionInterface instance provided");
209 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
211 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
218 bool CExtension::Load(char *error, size_t maxlength)
221 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
228 /* Check if we're past load time */
229 if (!g_SourceMod.IsMapLoading())
231 m_pAPI->OnExtensionsAllLoaded();
238 void CExtension::CreateIdentity()
240 if (m_pIdentToken != NULL)
245 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
248 void CExtension::DestroyIdentity()
250 if (m_pIdentToken == NULL)
255 g_ShareSys.DestroyIdentity(m_pIdentToken);
256 m_pIdentToken = NULL;
259 void CExtension::MarkAllLoaded()
261 assert(m_bFullyLoaded == false);
264 m_bFullyLoaded = true;
265 m_pAPI->OnExtensionsAllLoaded();
269 void CExtension::AddPlugin(IPlugin *pPlugin)
271 /* Unfortunately we have to do this :( */
272 if (m_Plugins.find(pPlugin) != m_Plugins.end())
274 m_Plugins.push_back(pPlugin);
278 void CExtension::RemovePlugin(IPlugin *pPlugin)
280 m_Plugins.remove(pPlugin);
282 List<WeakNative>::iterator iter = m_WeakNatives.begin();
283 while (iter != m_WeakNatives.end())
285 if ((*iter).pl == pPlugin)
287 iter = m_WeakNatives.erase(iter);
295 iter = m_ReplacedNatives.begin();
296 while (iter != m_ReplacedNatives.end())
298 if ((*iter).pl == pPlugin)
300 iter = m_ReplacedNatives.erase(iter);
309 void CExtension::SetError(const char *error)
311 m_Error.assign(error);
314 IExtensionInterface *CExtension::GetAPI()
319 const char *CExtension::GetFilename()
321 return m_File.c_str();
324 IdentityToken_t *CExtension::GetIdentity()
326 return m_pIdentToken;
329 bool CRemoteExtension::IsLoaded()
331 return (m_pAPI != NULL);
334 bool CLocalExtension::IsLoaded()
336 return (m_pLib != NULL);
339 void CExtension::AddDependency(IfaceInfo *pInfo)
341 if (m_Deps.find(*pInfo) == m_Deps.end())
343 m_Deps.push_back(*pInfo);
347 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
349 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
352 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
358 List<IfaceInfo>::iterator iter;
359 for (iter = m_ChildDeps.begin();
360 iter != m_ChildDeps.end();
363 IfaceInfo &other = (*iter);
370 m_ChildDeps.push_back(info);
373 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
375 List<IfaceInfo>::iterator iter = m_Deps.begin();
377 if (iter == m_Deps.end())
384 *pOwner = (*iter).owner;
388 *pInterface = (*iter).iface;
391 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
393 return (ITERATOR *)pIter;
396 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
398 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
399 List<IfaceInfo>::iterator _iter;
401 if (_iter == m_Deps.end())
410 *pOwner = (*_iter).owner;
414 *pInterface = (*_iter).iface;
419 if (_iter == m_Deps.end())
427 void CExtension::FreeDependencyIterator(ITERATOR *iter)
429 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
434 void CExtension::AddInterface(SMInterface *pInterface)
436 m_Interfaces.push_back(pInterface);
439 bool CExtension::IsRunning(char *error, size_t maxlength)
445 snprintf(error, maxlength, "%s", m_Error.c_str());
450 return m_pAPI->QueryRunning(error, maxlength);
453 void CExtension::AddLibrary(const char *library)
455 m_Libraries.push_back(library);
458 /*********************
459 * EXTENSION MANAGER *
460 *********************/
462 void CExtensionManager::OnSourceModAllInitialized()
464 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
465 g_PluginSys.AddPluginsListener(this);
466 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
467 g_ShareSys.AddInterface(NULL, this);
470 void CExtensionManager::OnSourceModShutdown()
472 g_RootMenu.RemoveRootConsoleCommand("exts", this);
473 g_ShareSys.DestroyIdentType(g_ExtType);
476 void CExtensionManager::Shutdown()
478 List<CExtension *>::iterator iter;
480 while ((iter = m_Libs.begin()) != m_Libs.end())
482 UnloadExtension((*iter));
486 void CExtensionManager::TryAutoload()
488 char path[PLATFORM_MAX_PATH];
490 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
492 IDirectory *pDir = g_LibSys.OpenDirectory(path);
500 while (pDir->MoreFiles())
502 if (pDir->IsEntryDirectory())
508 lfile = pDir->GetEntryName();
510 if (len <= 9) /* size of ".autoload" */
516 if (strcmp(&lfile[len - 9], ".autoload") != 0)
522 char file[PLATFORM_MAX_PATH];
523 len = UTIL_Format(file, sizeof(file), "%s", lfile);
524 strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
526 LoadAutoExtension(file);
532 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
534 if (!strstr(path, "." PLATFORM_LIB_EXT))
536 char newpath[PLATFORM_MAX_PATH];
537 g_LibSys.PathFormat(newpath, sizeof(newpath), "%s.%s", path, PLATFORM_LIB_EXT);
538 return LoadAutoExtension(newpath);
541 IExtension *pAlready;
542 if ((pAlready=FindExtensionByFile(path)) != NULL)
548 CExtension *p = new CLocalExtension(path);
550 /* We put us in the list beforehand so extensions that check for each other
551 * won't recursively load each other.
555 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
557 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
564 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
566 List<CExtension *>::iterator iter;
569 /* Chomp off the path */
570 char lookup[PLATFORM_MAX_PATH];
571 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
573 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
576 char short_file[PLATFORM_MAX_PATH];
577 g_LibSys.GetFileFromPath(short_file, sizeof(short_file), pExt->GetFilename());
578 if (strcmp(lookup, short_file) == 0)
584 /* If we got no results, test if there was a platform extension.
587 if (!strstr(file, "." PLATFORM_LIB_EXT))
589 char path[PLATFORM_MAX_PATH];
590 UTIL_Format(path, sizeof(path), "%s.%s", file, PLATFORM_LIB_EXT);
591 return FindExtensionByFile(path);
597 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
599 List<CExtension *>::iterator iter;
601 IExtensionInterface *pAPI;
604 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
607 if (!pExt->IsLoaded())
611 if ((pAPI = pExt->GetAPI()) == NULL)
615 name = pAPI->GetExtensionName();
620 if (strcmp(name, ext) == 0)
629 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
631 IExtension *pAlready;
632 if ((pAlready=FindExtensionByFile(file)) != NULL)
637 CExtension *pExt = new CLocalExtension(file);
639 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
646 /* :TODO: do QueryRunning check if the map is loaded */
648 m_Libs.push_back(pExt);
653 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
655 CExtension *pExt = (CExtension *)pRequester;
656 CExtension *pOwner = (CExtension *)pInfo->owner;
658 pExt->AddDependency(pInfo);
660 IExtensionInterface *pAPI = pExt->GetAPI();
661 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
663 pOwner->AddChildDependent(pExt, pInfo->iface);
667 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
669 CExtension *pExt = (CExtension *)pOwner;
671 pExt->AddInterface(pInterface);
674 void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
676 CExtension *pExt = (CExtension *)pParent;
678 pExt->AddPlugin(pPlugin);
681 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
683 List<CExtension *>::iterator iter;
685 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
687 (*iter)->RemovePlugin(plugin);
691 CExtension *CExtensionManager::FindByOrder(unsigned int num)
693 if (num < 1 || num > m_Libs.size())
698 List<CExtension *>::iterator iter = m_Libs.begin();
700 while (iter != m_Libs.end())
712 void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
714 IPluginContext *pContext = pPlugin->GetBaseContext();
716 List<CExtension *> exts;
718 uint32_t natives = pContext->GetNativesNum();
720 sm_extnative_t *x_native;
721 sm_repnative_t *r_native;
722 for (uint32_t i=0; i<natives; i++)
724 /* Make sure the native is valid */
725 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
730 /* Make sure the native is not already bound */
731 if (native->status == SP_NATIVE_BOUND)
733 /* If it is bound, see if there is a replacement. */
734 if ((r_native = m_RepNatives.retrieve(native->name)) == NULL)
739 /* Rewrite the address. Whee! */
740 native->pfn = r_native->info.func;
742 /* Make sure this will unload safely */
743 WeakNative wn((CPlugin *)pPlugin, i);
744 r_native->owner->m_ReplacedNatives.push_back(wn);
749 /* See if we've got this native in our cache */
750 if ((x_native = m_ExtNatives.retrieve(native->name)) == NULL)
754 /* Try and bind it */
755 if (pContext->BindNativeToIndex(i, x_native->info->func) != SP_ERROR_NONE)
759 /* See if it's optional */
760 if (native->flags & SP_NTVFLAG_OPTIONAL)
762 WeakNative wkn = WeakNative((CPlugin *)pPlugin, i);
763 x_native->owner->m_WeakNatives.push_back(wkn);
765 else if (exts.find(x_native->owner) == exts.end())
767 exts.push_back(x_native->owner);
771 List<CExtension *>::iterator iter;
772 for (iter = exts.begin(); iter != exts.end(); iter++)
774 CExtension *pExt = (*iter);
775 if (pExt->m_Plugins.find(pPlugin) != pExt->m_Plugins.end())
779 pExt->m_Plugins.push_back(pPlugin);
783 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
790 CExtension *pExt = (CExtension *)_pExt;
792 if (m_Libs.find(pExt) == m_Libs.end())
797 /* First remove us from internal lists */
798 g_ShareSys.RemoveInterfaces(_pExt);
801 List<CExtension *> UnloadQueue;
803 /* Handle dependencies */
804 if (pExt->IsLoaded())
806 /* Unload any dependent plugins */
807 List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
808 while (p_iter != pExt->m_Plugins.end())
810 /* We have to manually unlink ourselves here, since we're no longer being managed */
811 g_PluginSys.UnloadPlugin((*p_iter));
812 p_iter = pExt->m_Plugins.erase(p_iter);
815 List<String>::iterator s_iter;
816 for (s_iter = pExt->m_Libraries.begin();
817 s_iter != pExt->m_Libraries.end();
820 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
823 /* Unbind weak natives */
824 List<WeakNative>::iterator wkn_iter;
825 for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++)
827 WeakNative & wkn = (*wkn_iter);
828 sp_context_t *ctx = wkn.pl->GetContext();
829 ctx->natives[wkn.idx].status = SP_NATIVE_UNBOUND;
832 /* Unbind replacement natives, link them back to their originals */
833 for (wkn_iter = pExt->m_ReplacedNatives.begin();
834 wkn_iter != pExt->m_ReplacedNatives.end();
837 WeakNative & wkn = (*wkn_iter);
838 sp_context_t *ctx = wkn.pl->GetContext();
839 sm_repnative_t *r_native = m_RepNatives.retrieve(ctx->natives[wkn.idx].name);
840 if (r_native == NULL || ctx->natives[wkn.idx].pfn != r_native->info.func)
844 ctx->natives[wkn.idx].pfn = r_native->original;
847 /* Notify and/or unload all dependencies */
848 List<CExtension *>::iterator c_iter;
850 IExtensionInterface *pAPI;
851 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
854 if ((pAPI=pDep->GetAPI()) == NULL)
862 /* Now, get its dependency list */
863 bool dropped = false;
864 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
865 while (i_iter != pDep->m_Deps.end())
867 if ((*i_iter).owner == _pExt)
869 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
874 UnloadQueue.push_back(pDep);
877 pAPI->NotifyInterfaceDrop((*i_iter).iface);
878 i_iter = pDep->m_Deps.erase(i_iter);
885 /* Flush out any back references to this plugin */
886 i_iter = pDep->m_ChildDeps.begin();
887 while (i_iter != pDep->m_ChildDeps.end())
889 if ((*i_iter).owner == pExt)
891 i_iter = pDep->m_ChildDeps.erase(i_iter);
900 /* Unbind our natives from Core */
901 List<const sp_nativeinfo_t *>::iterator native_iter;
902 for (native_iter = pExt->m_Natives.begin();
903 native_iter != pExt->m_Natives.end();
906 const sp_nativeinfo_t *natives = (*native_iter);
907 sm_extnative_t *x_native;
908 for (unsigned int n = 0;
909 natives[n].name != NULL && natives[n].func != NULL;
912 x_native = m_ExtNatives.retrieve(natives[n].name);
913 if (x_native && x_native->owner == pExt)
915 m_ExtNatives.remove(natives[n].name);
920 /* Unbind our replacement natives */
921 List<sm_repnative_t>::iterator rep_iter = m_RepNativeList.begin();
922 while (rep_iter != m_RepNativeList.end())
924 sm_repnative_t & r_native = (*rep_iter);
925 if (r_native.owner == pExt)
927 m_RepNatives.remove(r_native.info.name);
928 rep_iter = m_RepNativeList.erase(rep_iter);
936 /* Tell it to unload */
937 pAPI = pExt->GetAPI();
938 pAPI->OnExtensionUnload();
941 IdentityToken_t *pIdentity;
942 if ((pIdentity = pExt->GetIdentity()) != NULL)
944 SMGlobalClass *glob = SMGlobalClass::head;
947 glob->OnSourceModIdentityDropped(pIdentity);
948 glob = glob->m_pGlobalClassNext;
955 List<CExtension *>::iterator iter;
956 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
958 /* NOTE: This is safe because the unload function backs out of anything not present */
959 UnloadExtension((*iter));
965 void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
967 CExtension *pExt = (CExtension *)pOwner;
969 pExt->m_Natives.push_back(natives);
971 for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
973 if (m_ExtNatives.retrieve(natives[i].name) != NULL)
977 sm_extnative_t x_native;
978 x_native.info = &natives[i];
979 x_native.owner = pExt;
980 m_ExtNatives.insert(natives[i].name, x_native);
984 void CExtensionManager::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
986 SPVM_NATIVE_FUNC orig;
988 for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
990 if ((orig = g_PluginSys.FindCoreNative(natives[i].name)) == NULL)
995 rep.info = natives[i];
996 rep.owner = (CExtension *)myself;
998 m_RepNativeList.push_back(rep);
999 m_RepNatives.insert(natives[i].name, rep);
1003 void CExtensionManager::MarkAllLoaded()
1005 List<CExtension *>::iterator iter;
1008 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1011 if (!pExt->IsLoaded())
1015 if (pExt->m_bFullyLoaded)
1019 pExt->MarkAllLoaded();
1023 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
1025 /* This function doesn't really need to do anything now. We make sure the
1026 * other extension is loaded, but handling of dependencies is really done
1027 * by the interface fetcher.
1029 if (required || autoload)
1031 LoadAutoExtension(file);
1035 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
1037 int argcount = command.ArgC();
1040 const char *cmd = command.Arg(2);
1041 if (strcmp(cmd, "list") == 0)
1043 List<CExtension *>::iterator iter;
1045 unsigned int num = 1;
1046 switch (m_Libs.size())
1050 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
1055 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1060 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
1064 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
1067 if (pExt->IsLoaded())
1070 if (!pExt->IsRunning(error, sizeof(error)))
1072 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
1076 IExtensionInterface *pAPI = pExt->GetAPI();
1077 const char *name = pAPI->GetExtensionName();
1078 const char *version = pAPI->GetExtensionVerString();
1079 const char *descr = pAPI->GetExtensionDescription();
1080 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
1083 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1088 else if (strcmp(cmd, "info") == 0)
1092 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
1096 const char *sId = command.Arg(3);
1097 unsigned int id = atoi(sId);
1100 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
1104 if (m_Libs.size() == 0)
1106 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1110 if (id > m_Libs.size())
1112 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1116 List<CExtension *>::iterator iter = m_Libs.begin();
1117 CExtension *pExt = NULL;
1118 while (iter != m_Libs.end())
1127 /* This should never happen */
1130 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1134 if (!pExt->IsLoaded())
1136 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1137 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1142 if (!pExt->IsRunning(error, sizeof(error)))
1144 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1145 g_RootMenu.ConsolePrint(" Loaded: Yes");
1146 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1150 IExtensionInterface *pAPI = pExt->GetAPI();
1151 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1152 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1153 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1154 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1155 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1157 if (pExt->IsExternal())
1159 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1161 else if (pAPI->IsMetamodExtension())
1163 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1167 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1173 else if (strcmp(cmd, "unload") == 0)
1177 g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
1181 const char *arg = command.Arg(3);
1182 unsigned int num = atoi(arg);
1183 CExtension *pExt = FindByOrder(num);
1187 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1191 if (argcount > 4 && pExt->unload_code)
1193 const char *unload = command.Arg(4);
1194 if (pExt->unload_code == (unsigned)atoi(unload))
1196 char filename[PLATFORM_MAX_PATH];
1197 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1198 UnloadExtension(pExt);
1199 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1203 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1208 if (!pExt->IsLoaded()
1209 || (!pExt->m_ChildDeps.size() && !pExt->m_Plugins.size()))
1211 char filename[PLATFORM_MAX_PATH];
1212 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1213 UnloadExtension(pExt);
1214 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1219 List<IPlugin *> plugins;
1220 if (pExt->m_ChildDeps.size())
1222 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1223 List<CExtension *>::iterator iter;
1225 /* Get list of all extensions */
1226 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1228 List<IfaceInfo>::iterator i_iter;
1230 if (!pOther->IsLoaded() || pOther == pExt)
1234 /* Get their dependencies */
1235 for (i_iter=pOther->m_Deps.begin();
1236 i_iter!=pOther->m_Deps.end();
1239 /* Is this dependency to us? */
1240 if ((*i_iter).owner != pExt)
1244 /* Will our dependent care? */
1245 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1247 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1248 /* Add to plugin unload list */
1249 List<IPlugin *>::iterator p_iter;
1250 for (p_iter=pOther->m_Plugins.begin();
1251 p_iter!=pOther->m_Plugins.end();
1254 if (plugins.find((*p_iter)) == plugins.end())
1256 plugins.push_back((*p_iter));
1263 if (pExt->m_Plugins.size())
1265 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1266 List<IPlugin *>::iterator iter;
1268 for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
1271 if (plugins.find(pPlugin) == plugins.end())
1273 plugins.push_back(pPlugin);
1276 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1279 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1282 srand(static_cast<int>(time(NULL)));
1283 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1284 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1285 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1292 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1293 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1294 g_RootMenu.DrawGenericOption("list", "List extensions");
1295 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1298 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1300 if (ptr->type == g_ExtType)
1302 return (CExtension *)(ptr->ptr);
1308 CExtensionManager::CExtensionManager()
1312 CExtensionManager::~CExtensionManager()
1316 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1318 CExtension *pExt = (CExtension *)pSource;
1319 pExt->AddLibrary(library);
1320 g_PluginSys.OnLibraryAction(library, false, false);
1323 bool CExtensionManager::LibraryExists(const char *library)
1327 for (List<CExtension *>::iterator iter = m_Libs.begin();
1328 iter != m_Libs.end();
1332 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1333 s_iter != pExt->m_Libraries.end();
1336 if ((*s_iter).compare(library) == 0)
1346 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1347 const char *filepath,
1348 const char *filename,
1352 IExtension *pAlready;
1353 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1358 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1360 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1367 m_Libs.push_back(pExt);
1372 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1374 List<CExtension *>::iterator iter;
1376 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1378 if (SMINTERFACE_EXTENSIONAPI_VERSION > 3)
1380 (*iter)->GetAPI()->OnCoreMapStart(pEdictList, edictCount, clientMax);