Don't allocate/delete across static CRTs (no bug, irc r=fyren+psychonic).
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 <ILibrarySys.h>
35 #include <ISourceMod.h>
36 #include "common_logic.h"
37 #include "PluginSys.h"
39 CExtensionManager g_Extensions;
40 IdentityType_t g_ExtType;
42 void CExtension::Initialize(const char *filename, const char *path)
47 m_bFullyLoaded = false;
48 m_File.assign(filename);
50 char real_name[PLATFORM_MAX_PATH];
51 libsys->GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
52 m_RealFile.assign(real_name);
55 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
57 Initialize(filename, path);
61 #if defined METAMOD_PLAPI_VERSION
62 #if SOURCE_ENGINE == SE_LEFT4DEAD
63 #define GAMEFIX "2.l4d"
64 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
65 #define GAMEFIX "2.l4d2"
66 #elif SOURCE_ENGINE == SE_ALIENSWARM
67 #define GAMEFIX "2.swarm"
68 #elif SOURCE_ENGINE == SE_ORANGEBOX
69 #define GAMEFIX "2.ep2"
70 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
71 #define GAMEFIX "2.bgt"
72 #elif SOURCE_ENGINE == SE_EYE
73 #define GAMEFIX "2.eye"
74 #elif SOURCE_ENGINE == SE_CSS
75 #define GAMEFIX "2.css"
76 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
77 #define GAMEFIX "2.ep2v"
78 #elif SOURCE_ENGINE == SE_DARKMESSIAH
79 #define GAMEFIX "2.darkm"
80 #elif SOURCE_ENGINE == SE_PORTAL2
81 #define GAMEFIX "2.portal2"
82 #elif SOURCE_ENGINE == SE_CSGO
83 #define GAMEFIX "2.csgo"
84 #elif SOURCE_ENGINE == SE_DOTA
85 #define GAMEFIX "2.dota"
87 #define GAMEFIX "2.ep1"
88 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
89 #else //METAMOD_PLAPI_VERSION
90 #define GAMEFIX "1.ep1"
91 #endif //METAMOD_PLAPI_VERSION
93 CLocalExtension::CLocalExtension(const char *filename)
98 char path[PLATFORM_MAX_PATH];
100 /* Special case for new bintools binary */
101 if (strcmp(filename, "bintools.ext") == 0)
106 /* Zeroth, see if there is an engine specific build in the new place. */
107 g_pSM->BuildPath(Path_SM,
110 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
113 if (libsys->IsPathFile(path))
118 /* First see if there is an engine specific build! */
119 g_pSM->BuildPath(Path_SM,
122 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
125 /* Try the "normal" version */
126 if (!libsys->IsPathFile(path))
129 g_pSM->BuildPath(Path_SM,
132 "extensions/%s." PLATFORM_LIB_EXT,
137 Initialize(filename, path);
140 bool CRemoteExtension::Load(char *error, size_t maxlength)
142 if (!PerformAPICheck(error, maxlength))
148 if (!CExtension::Load(error, maxlength))
157 bool CLocalExtension::Load(char *error, size_t maxlength)
159 m_pLib = libsys->OpenLibrary(m_Path.c_str(), error, maxlength);
166 typedef IExtensionInterface *(*GETAPI)();
167 GETAPI pfnGetAPI = NULL;
169 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
171 m_pLib->CloseLibrary();
173 snprintf(error, maxlength, "Unable to find extension entry point");
177 m_pAPI = pfnGetAPI();
179 /* Check pointer and version */
180 if (!PerformAPICheck(error, maxlength))
182 m_pLib->CloseLibrary();
189 if (m_pAPI->IsMetamodExtension())
192 m_PlId = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
196 m_pLib->CloseLibrary();
203 if (!CExtension::Load(error, maxlength))
205 if (m_pAPI->IsMetamodExtension())
209 smcore.UnloadMMSPlugin(m_PlId);
213 m_pLib->CloseLibrary();
222 void CRemoteExtension::Unload()
226 void CLocalExtension::Unload()
228 if (m_pAPI != NULL && m_PlId)
230 smcore.UnloadMMSPlugin(m_PlId);
236 m_pLib->CloseLibrary();
241 bool CRemoteExtension::Reload(char *error, size_t maxlength)
243 snprintf(error, maxlength, "Remote extensions do not support reloading");
247 bool CLocalExtension::Reload(char *error, size_t maxlength)
249 if (m_pLib == NULL) // FIXME: just load it instead?
252 m_pAPI->OnExtensionUnload();
255 return Load(error, maxlength);
258 bool CRemoteExtension::IsExternal()
263 bool CLocalExtension::IsExternal()
268 CExtension::~CExtension()
273 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
277 snprintf(error, maxlength, "No IExtensionInterface instance provided");
281 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
283 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
290 bool CExtension::Load(char *error, size_t maxlength)
293 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
300 /* Check if we're past load time */
301 if (!smcore.IsMapLoading())
303 m_pAPI->OnExtensionsAllLoaded();
310 void CExtension::CreateIdentity()
312 if (m_pIdentToken != NULL)
317 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
320 void CExtension::DestroyIdentity()
322 if (m_pIdentToken == NULL)
327 g_ShareSys.DestroyIdentity(m_pIdentToken);
328 m_pIdentToken = NULL;
331 void CExtension::MarkAllLoaded()
333 assert(m_bFullyLoaded == false);
336 m_bFullyLoaded = true;
337 m_pAPI->OnExtensionsAllLoaded();
341 void CExtension::AddPlugin(CPlugin *pPlugin)
343 /* Unfortunately we have to do this :( */
344 if (m_Dependents.find(pPlugin) != m_Dependents.end())
346 m_Dependents.push_back(pPlugin);
350 void CExtension::SetError(const char *error)
352 m_Error.assign(error);
355 IExtensionInterface *CExtension::GetAPI()
360 const char *CExtension::GetFilename()
362 return m_RealFile.c_str();
365 const char *CExtension::GetPath() const
367 return m_Path.c_str();
370 IdentityToken_t *CExtension::GetIdentity()
372 return m_pIdentToken;
375 bool CRemoteExtension::IsLoaded()
377 return (m_pAPI != NULL);
380 bool CLocalExtension::IsLoaded()
382 return (m_pLib != NULL);
385 void CExtension::AddDependency(const IfaceInfo *pInfo)
387 if (m_Deps.find(*pInfo) == m_Deps.end())
389 m_Deps.push_back(*pInfo);
393 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
395 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
398 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
404 List<IfaceInfo>::iterator iter;
405 for (iter = m_ChildDeps.begin();
406 iter != m_ChildDeps.end();
409 IfaceInfo &other = (*iter);
416 m_ChildDeps.push_back(info);
419 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
421 List<IfaceInfo>::iterator iter = m_Deps.begin();
423 if (iter == m_Deps.end())
430 *pOwner = (*iter).owner;
434 *pInterface = (*iter).iface;
437 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
439 return (ITERATOR *)pIter;
442 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
444 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
445 List<IfaceInfo>::iterator _iter;
447 if (_iter == m_Deps.end())
456 *pOwner = (*_iter).owner;
460 *pInterface = (*_iter).iface;
465 if (_iter == m_Deps.end())
473 void CExtension::FreeDependencyIterator(ITERATOR *iter)
475 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
480 void CExtension::AddInterface(SMInterface *pInterface)
482 m_Interfaces.push_back(pInterface);
485 bool CExtension::IsRunning(char *error, size_t maxlength)
491 snprintf(error, maxlength, "%s", m_Error.c_str());
496 return m_pAPI->QueryRunning(error, maxlength);
499 void CExtension::AddLibrary(const char *library)
501 m_Libraries.push_back(library);
504 /*********************
505 * EXTENSION MANAGER *
506 *********************/
508 void CExtensionManager::OnSourceModAllInitialized()
510 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
511 pluginsys->AddPluginsListener(this);
512 g_ShareSys.AddInterface(NULL, this);
515 void CExtensionManager::OnSourceModShutdown()
517 pluginsys->RemovePluginsListener(this);
518 g_ShareSys.DestroyIdentType(g_ExtType);
521 void CExtensionManager::Shutdown()
523 List<CExtension *>::iterator iter;
525 while ((iter = m_Libs.begin()) != m_Libs.end())
527 UnloadExtension((*iter));
531 void CExtensionManager::TryAutoload()
533 char path[PLATFORM_MAX_PATH];
535 g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
537 IDirectory *pDir = libsys->OpenDirectory(path);
545 while (pDir->MoreFiles())
547 if (pDir->IsEntryDirectory())
553 lfile = pDir->GetEntryName();
555 if (len <= 9) /* size of ".autoload" */
561 if (strcmp(&lfile[len - 9], ".autoload") != 0)
567 char file[PLATFORM_MAX_PATH];
568 len = smcore.Format(file, sizeof(file), "%s", lfile);
569 strcpy(&file[len - 9], ".ext");
571 LoadAutoExtension(file);
577 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
579 /* Remove platform extension if it's there. Compat hack. */
580 const char *ext = libsys->GetFileExtension(path);
581 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
583 char path2[PLATFORM_MAX_PATH];
584 smcore.Format(path2, sizeof(path2), "%s", path);
585 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
586 return LoadAutoExtension(path2);
589 IExtension *pAlready;
590 if ((pAlready=FindExtensionByFile(path)) != NULL)
596 CExtension *p = new CLocalExtension(path);
598 /* We put us in the list beforehand so extensions that check for each other
599 * won't recursively load each other.
603 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
605 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
612 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
614 List<CExtension *>::iterator iter;
617 /* Chomp off the path */
618 char lookup[PLATFORM_MAX_PATH];
619 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
621 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
624 if (pExt->IsSameFile(lookup))
633 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
635 List<CExtension *>::iterator iter;
637 IExtensionInterface *pAPI;
640 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
643 if (!pExt->IsLoaded())
647 if ((pAPI = pExt->GetAPI()) == NULL)
651 name = pAPI->GetExtensionName();
656 if (strcmp(name, ext) == 0)
665 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
667 /* Remove platform extension if it's there. Compat hack. */
668 const char *ext = libsys->GetFileExtension(file);
669 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
671 char path2[PLATFORM_MAX_PATH];
672 smcore.Format(path2, sizeof(path2), "%s", file);
673 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
674 return LoadExtension(path2, error, maxlength);
677 IExtension *pAlready;
678 if ((pAlready=FindExtensionByFile(file)) != NULL)
683 CExtension *pExt = new CLocalExtension(file);
685 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
692 /* :TODO: do QueryRunning check if the map is loaded */
694 m_Libs.push_back(pExt);
699 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
701 CExtension *pExt = (CExtension *)pRequester;
702 CExtension *pOwner = (CExtension *)pInfo->owner;
704 pExt->AddDependency(pInfo);
706 IExtensionInterface *pAPI = pExt->GetAPI();
707 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
709 pOwner->AddChildDependent(pExt, pInfo->iface);
713 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
715 CExtension *pExt = (CExtension *)ext;
716 CExtension *pOwner = GetExtensionFromIdent(other);
720 info.iface = (SMInterface *)iface;
723 pExt->AddDependency(&info);
724 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
727 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
729 CExtension *pExt = (CExtension *)pOwner;
731 pExt->AddInterface(pInterface);
734 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
736 CExtension *pExt = (CExtension *)pParent;
738 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
741 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
743 List<CExtension *>::iterator iter;
745 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
747 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
751 CExtension *CExtensionManager::FindByOrder(unsigned int num)
753 if (num < 1 || num > m_Libs.size())
758 List<CExtension *>::iterator iter = m_Libs.begin();
760 while (iter != m_Libs.end())
772 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
779 CExtension *pExt = (CExtension *)_pExt;
781 if (m_Libs.find(pExt) == m_Libs.end())
786 /* Tell it to unload */
787 if (pExt->IsLoaded())
789 IExtensionInterface *pAPI = pExt->GetAPI();
790 pAPI->OnExtensionUnload();
793 /* First remove us from internal lists */
794 g_ShareSys.RemoveInterfaces(_pExt);
797 List<CExtension *> UnloadQueue;
799 /* Handle dependencies */
800 if (pExt->IsLoaded())
802 /* Unload any dependent plugins */
803 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
804 while (p_iter != pExt->m_Dependents.end())
806 /* We have to manually unlink ourselves here, since we're no longer being managed */
807 scripts->UnloadPlugin((*p_iter));
808 p_iter = pExt->m_Dependents.erase(p_iter);
811 List<String>::iterator s_iter;
812 for (s_iter = pExt->m_Libraries.begin();
813 s_iter != pExt->m_Libraries.end();
816 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
819 /* Notify and/or unload all dependencies */
820 List<CExtension *>::iterator c_iter;
822 IExtensionInterface *pAPI;
823 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
826 if ((pAPI=pDep->GetAPI()) == NULL)
834 /* Now, get its dependency list */
835 bool dropped = false;
836 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
837 while (i_iter != pDep->m_Deps.end())
839 if ((*i_iter).owner == _pExt)
841 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
846 UnloadQueue.push_back(pDep);
849 pAPI->NotifyInterfaceDrop((*i_iter).iface);
850 i_iter = pDep->m_Deps.erase(i_iter);
857 /* Flush out any back references to this plugin */
858 i_iter = pDep->m_ChildDeps.begin();
859 while (i_iter != pDep->m_ChildDeps.end())
861 if ((*i_iter).owner == pExt)
863 i_iter = pDep->m_ChildDeps.erase(i_iter);
872 /* Unbind our natives from Core */
873 pExt->DropEverything();
876 IdentityToken_t *pIdentity;
877 if ((pIdentity = pExt->GetIdentity()) != NULL)
879 SMGlobalClass *glob = SMGlobalClass::head;
882 glob->OnSourceModIdentityDropped(pIdentity);
883 glob = glob->m_pGlobalClassNext;
890 List<CExtension *>::iterator iter;
891 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
893 /* NOTE: This is safe because the unload function backs out of anything not present */
894 UnloadExtension((*iter));
900 void CExtensionManager::MarkAllLoaded()
902 List<CExtension *>::iterator iter;
905 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
908 if (!pExt->IsLoaded())
912 if (pExt->m_bFullyLoaded)
916 pExt->MarkAllLoaded();
920 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
922 /* This function doesn't really need to do anything now. We make sure the
923 * other extension is loaded, but handling of dependencies is really done
924 * by the interface fetcher.
926 if (required || autoload)
928 LoadAutoExtension(file);
932 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
934 int argcount = smcore.Argc(command);
937 const char *cmd = smcore.Arg(command, 2);
938 if (strcmp(cmd, "list") == 0)
940 List<CExtension *>::iterator iter;
942 unsigned int num = 1;
943 switch (m_Libs.size())
947 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
952 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
957 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
961 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
964 if (pExt->IsLoaded())
967 if (!pExt->IsRunning(error, sizeof(error)))
969 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
973 IExtensionInterface *pAPI = pExt->GetAPI();
974 const char *name = pAPI->GetExtensionName();
975 const char *version = pAPI->GetExtensionVerString();
976 const char *descr = pAPI->GetExtensionDescription();
977 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
980 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
985 else if (strcmp(cmd, "load") == 0)
989 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
993 const char *filename = smcore.Arg(command, 3);
994 char path[PLATFORM_MAX_PATH];
997 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
998 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1000 if (FindExtensionByFile(path) != NULL)
1002 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1006 if (LoadExtension(path, error, sizeof(error)))
1008 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1011 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1016 else if (strcmp(cmd, "info") == 0)
1020 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1024 const char *sId = smcore.Arg(command, 3);
1025 unsigned int id = atoi(sId);
1028 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1032 if (m_Libs.size() == 0)
1034 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1038 if (id > m_Libs.size())
1040 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1044 List<CExtension *>::iterator iter = m_Libs.begin();
1045 CExtension *pExt = NULL;
1046 while (iter != m_Libs.end())
1055 /* This should never happen */
1058 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1062 if (!pExt->IsLoaded())
1064 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1065 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1070 if (!pExt->IsRunning(error, sizeof(error)))
1072 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1073 rootmenu->ConsolePrint(" Loaded: Yes");
1074 rootmenu->ConsolePrint(" Running: No (%s)", error);
1078 IExtensionInterface *pAPI = pExt->GetAPI();
1079 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1080 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1081 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1082 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1083 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1085 if (pExt->IsExternal())
1087 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1089 else if (pAPI->IsMetamodExtension())
1091 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1095 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1101 else if (strcmp(cmd, "unload") == 0)
1105 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1109 const char *arg = smcore.Arg(command, 3);
1110 unsigned int num = atoi(arg);
1111 CExtension *pExt = FindByOrder(num);
1115 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1119 if (argcount > 4 && pExt->unload_code)
1121 const char *unload = smcore.Arg(command, 4);
1122 if (pExt->unload_code == (unsigned)atoi(unload))
1124 char filename[PLATFORM_MAX_PATH];
1125 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1126 UnloadExtension(pExt);
1127 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1131 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1136 if (!pExt->IsLoaded()
1137 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1139 char filename[PLATFORM_MAX_PATH];
1140 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1141 UnloadExtension(pExt);
1142 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1147 List<CPlugin *> plugins;
1148 if (pExt->m_ChildDeps.size())
1150 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1151 List<CExtension *>::iterator iter;
1153 /* Get list of all extensions */
1154 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1156 List<IfaceInfo>::iterator i_iter;
1158 if (!pOther->IsLoaded() || pOther == pExt)
1162 /* Get their dependencies */
1163 for (i_iter=pOther->m_Deps.begin();
1164 i_iter!=pOther->m_Deps.end();
1167 /* Is this dependency to us? */
1168 if ((*i_iter).owner != pExt)
1172 /* Will our dependent care? */
1173 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1175 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1176 /* Add to plugin unload list */
1177 List<CPlugin *>::iterator p_iter;
1178 for (p_iter=pOther->m_Dependents.begin();
1179 p_iter!=pOther->m_Dependents.end();
1182 if (plugins.find((*p_iter)) == plugins.end())
1184 plugins.push_back((*p_iter));
1191 if (pExt->m_Dependents.size())
1193 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1194 List<CPlugin *>::iterator iter;
1196 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1199 if (plugins.find(pPlugin) == plugins.end())
1201 plugins.push_back(pPlugin);
1204 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1207 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1210 srand(static_cast<int>(time(NULL)));
1211 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1212 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1213 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1218 else if (strcmp(cmd, "reload") == 0)
1222 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1226 const char *arg = smcore.Arg(command, 3);
1227 unsigned int num = atoi(arg);
1228 CExtension *pExt = FindByOrder(num);
1232 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1236 if (pExt->IsLoaded())
1238 char filename[PLATFORM_MAX_PATH];
1241 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1243 if (pExt->Reload(error, sizeof(error)))
1245 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1249 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1256 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1264 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1265 rootmenu->DrawGenericOption("info", "Extra extension information");
1266 rootmenu->DrawGenericOption("list", "List extensions");
1267 rootmenu->DrawGenericOption("load", "Load an extension");
1268 rootmenu->DrawGenericOption("reload", "Reload an extension");
1269 rootmenu->DrawGenericOption("unload", "Unload an extension");
1272 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1274 if (ptr->type == g_ExtType)
1276 return (CExtension *)(ptr->ptr);
1282 CExtensionManager::CExtensionManager()
1286 CExtensionManager::~CExtensionManager()
1290 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1292 CExtension *pExt = (CExtension *)pSource;
1293 pExt->AddLibrary(library);
1294 scripts->OnLibraryAction(library, LibraryAction_Added);
1297 bool CExtensionManager::LibraryExists(const char *library)
1301 for (List<CExtension *>::iterator iter = m_Libs.begin();
1302 iter != m_Libs.end();
1306 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1307 s_iter != pExt->m_Libraries.end();
1310 if ((*s_iter).compare(library) == 0)
1320 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1321 const char *filepath,
1322 const char *filename,
1326 IExtension *pAlready;
1327 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1332 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1334 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1341 m_Libs.push_back(pExt);
1346 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1348 IExtensionInterface *pAPI;
1349 List<CExtension *>::iterator iter;
1351 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1353 if ((pAPI = (*iter)->GetAPI()) == NULL)
1357 if (pAPI->GetExtensionVersion() > 3)
1359 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1364 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1366 CVector<IExtension *> *list = new CVector<IExtension *>();
1367 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1368 list->push_back(*iter);
1372 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1374 delete const_cast<CVector<IExtension *> *>(list);
1377 bool CLocalExtension::IsSameFile(const char *file)
1379 /* Only care about the shortened name. */
1380 return strcmp(file, m_File.c_str()) == 0;
1383 bool CRemoteExtension::IsSameFile(const char *file)
1385 /* :TODO: this could be better, but no one uses this API anyway. */
1386 return strcmp(file, m_Path.c_str()) == 0;