Updated some CS:GO gamedata.
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 CLocalExtension::CLocalExtension(const char *filename)
66 char path[PLATFORM_MAX_PATH];
68 /* Special case for new bintools binary */
69 if (strcmp(filename, "bintools.ext") == 0)
74 /* Zeroth, see if there is an engine specific build in the new place. */
75 g_pSM->BuildPath(Path_SM,
78 "extensions/%s.%s." PLATFORM_LIB_EXT,
82 if (libsys->IsPathFile(path))
87 /* COMPAT HACK: One-halfth, if ep2v, see if there is an engine specific build in the new place with old naming */
88 if (strcmp(smcore.gamesuffix, "2.tf2") == 0
89 || strcmp(smcore.gamesuffix, "2.dods") == 0
90 || strcmp(smcore.gamesuffix, "2.hl2dm") == 0
93 g_pSM->BuildPath(Path_SM,
96 "extensions/%s.2.ep2v." PLATFORM_LIB_EXT,
99 if (libsys->IsPathFile(path))
104 else if (strcmp(smcore.gamesuffix, "2.nd") == 0)
106 g_pSM->BuildPath(Path_SM,
109 "extensions/%s.2.l4d2." PLATFORM_LIB_EXT,
112 if (libsys->IsPathFile(path))
118 /* First see if there is an engine specific build! */
119 g_pSM->BuildPath(Path_SM,
122 "extensions/auto.%s/%s." PLATFORM_LIB_EXT,
126 /* Try the "normal" version */
127 if (!libsys->IsPathFile(path))
130 g_pSM->BuildPath(Path_SM,
133 "extensions/%s." PLATFORM_LIB_EXT,
138 Initialize(filename, path);
141 bool CRemoteExtension::Load(char *error, size_t maxlength)
143 if (!PerformAPICheck(error, maxlength))
149 if (!CExtension::Load(error, maxlength))
158 bool CLocalExtension::Load(char *error, size_t maxlength)
160 m_pLib = libsys->OpenLibrary(m_Path.c_str(), error, maxlength);
167 typedef IExtensionInterface *(*GETAPI)();
168 GETAPI pfnGetAPI = NULL;
170 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
172 m_pLib->CloseLibrary();
174 snprintf(error, maxlength, "Unable to find extension entry point");
178 m_pAPI = pfnGetAPI();
180 /* Check pointer and version */
181 if (!PerformAPICheck(error, maxlength))
183 m_pLib->CloseLibrary();
190 if (m_pAPI->IsMetamodExtension())
193 m_PlId = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
197 m_pLib->CloseLibrary();
204 if (!CExtension::Load(error, maxlength))
206 if (m_pAPI->IsMetamodExtension())
210 smcore.UnloadMMSPlugin(m_PlId);
214 m_pLib->CloseLibrary();
223 void CRemoteExtension::Unload()
227 void CLocalExtension::Unload()
229 if (m_pAPI != NULL && m_PlId)
231 smcore.UnloadMMSPlugin(m_PlId);
237 m_pLib->CloseLibrary();
242 bool CRemoteExtension::Reload(char *error, size_t maxlength)
244 snprintf(error, maxlength, "Remote extensions do not support reloading");
248 bool CLocalExtension::Reload(char *error, size_t maxlength)
250 if (m_pLib == NULL) // FIXME: just load it instead?
253 m_pAPI->OnExtensionUnload();
256 return Load(error, maxlength);
259 bool CRemoteExtension::IsExternal()
264 bool CLocalExtension::IsExternal()
269 CExtension::~CExtension()
274 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
278 snprintf(error, maxlength, "No IExtensionInterface instance provided");
282 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
284 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
291 bool CExtension::Load(char *error, size_t maxlength)
294 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
301 /* Check if we're past load time */
302 if (!smcore.IsMapLoading())
304 m_pAPI->OnExtensionsAllLoaded();
311 void CExtension::CreateIdentity()
313 if (m_pIdentToken != NULL)
318 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
321 void CExtension::DestroyIdentity()
323 if (m_pIdentToken == NULL)
328 g_ShareSys.DestroyIdentity(m_pIdentToken);
329 m_pIdentToken = NULL;
332 void CExtension::MarkAllLoaded()
334 assert(m_bFullyLoaded == false);
337 m_bFullyLoaded = true;
338 m_pAPI->OnExtensionsAllLoaded();
342 void CExtension::AddPlugin(CPlugin *pPlugin)
344 /* Unfortunately we have to do this :( */
345 if (m_Dependents.find(pPlugin) != m_Dependents.end())
347 m_Dependents.push_back(pPlugin);
351 void CExtension::SetError(const char *error)
353 m_Error.assign(error);
356 IExtensionInterface *CExtension::GetAPI()
361 const char *CExtension::GetFilename()
363 return m_RealFile.c_str();
366 const char *CExtension::GetPath() const
368 return m_Path.c_str();
371 IdentityToken_t *CExtension::GetIdentity()
373 return m_pIdentToken;
376 bool CRemoteExtension::IsLoaded()
378 return (m_pAPI != NULL);
381 bool CLocalExtension::IsLoaded()
383 return (m_pLib != NULL);
386 void CExtension::AddDependency(const IfaceInfo *pInfo)
388 if (m_Deps.find(*pInfo) == m_Deps.end())
390 m_Deps.push_back(*pInfo);
394 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
396 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
399 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
405 List<IfaceInfo>::iterator iter;
406 for (iter = m_ChildDeps.begin();
407 iter != m_ChildDeps.end();
410 IfaceInfo &other = (*iter);
417 m_ChildDeps.push_back(info);
420 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
422 List<IfaceInfo>::iterator iter = m_Deps.begin();
424 if (iter == m_Deps.end())
431 *pOwner = (*iter).owner;
435 *pInterface = (*iter).iface;
438 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
440 return (ITERATOR *)pIter;
443 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
445 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
446 List<IfaceInfo>::iterator _iter;
448 if (_iter == m_Deps.end())
457 *pOwner = (*_iter).owner;
461 *pInterface = (*_iter).iface;
466 if (_iter == m_Deps.end())
474 void CExtension::FreeDependencyIterator(ITERATOR *iter)
476 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
481 void CExtension::AddInterface(SMInterface *pInterface)
483 m_Interfaces.push_back(pInterface);
486 bool CExtension::IsRunning(char *error, size_t maxlength)
492 snprintf(error, maxlength, "%s", m_Error.c_str());
497 return m_pAPI->QueryRunning(error, maxlength);
500 void CExtension::AddLibrary(const char *library)
502 m_Libraries.push_back(library);
505 /*********************
506 * EXTENSION MANAGER *
507 *********************/
509 void CExtensionManager::OnSourceModAllInitialized()
511 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
512 pluginsys->AddPluginsListener(this);
513 rootmenu->AddRootConsoleCommand("exts", "Manage extensions", this);
514 g_ShareSys.AddInterface(NULL, this);
517 void CExtensionManager::OnSourceModShutdown()
519 rootmenu->RemoveRootConsoleCommand("exts", this);
520 pluginsys->RemovePluginsListener(this);
521 g_ShareSys.DestroyIdentType(g_ExtType);
524 void CExtensionManager::Shutdown()
526 List<CExtension *>::iterator iter;
528 while ((iter = m_Libs.begin()) != m_Libs.end())
530 UnloadExtension((*iter));
534 void CExtensionManager::TryAutoload()
536 char path[PLATFORM_MAX_PATH];
538 g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
540 IDirectory *pDir = libsys->OpenDirectory(path);
548 while (pDir->MoreFiles())
550 if (pDir->IsEntryDirectory())
556 lfile = pDir->GetEntryName();
558 if (len <= 9) /* size of ".autoload" */
564 if (strcmp(&lfile[len - 9], ".autoload") != 0)
570 char file[PLATFORM_MAX_PATH];
571 len = smcore.Format(file, sizeof(file), "%s", lfile);
572 strcpy(&file[len - 9], ".ext");
574 LoadAutoExtension(file);
580 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
582 /* Remove platform extension if it's there. Compat hack. */
583 const char *ext = libsys->GetFileExtension(path);
584 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
586 char path2[PLATFORM_MAX_PATH];
587 smcore.Format(path2, sizeof(path2), "%s", path);
588 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
589 return LoadAutoExtension(path2);
592 IExtension *pAlready;
593 if ((pAlready=FindExtensionByFile(path)) != NULL)
599 CExtension *p = new CLocalExtension(path);
601 /* We put us in the list beforehand so extensions that check for each other
602 * won't recursively load each other.
606 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
608 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
615 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
617 List<CExtension *>::iterator iter;
620 /* Chomp off the path */
621 char lookup[PLATFORM_MAX_PATH];
622 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
624 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
627 if (pExt->IsSameFile(lookup))
636 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
638 List<CExtension *>::iterator iter;
640 IExtensionInterface *pAPI;
643 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
646 if (!pExt->IsLoaded())
650 if ((pAPI = pExt->GetAPI()) == NULL)
654 name = pAPI->GetExtensionName();
659 if (strcmp(name, ext) == 0)
668 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
670 /* Remove platform extension if it's there. Compat hack. */
671 const char *ext = libsys->GetFileExtension(file);
672 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
674 char path2[PLATFORM_MAX_PATH];
675 smcore.Format(path2, sizeof(path2), "%s", file);
676 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
677 return LoadExtension(path2, error, maxlength);
680 IExtension *pAlready;
681 if ((pAlready=FindExtensionByFile(file)) != NULL)
686 CExtension *pExt = new CLocalExtension(file);
688 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
695 /* :TODO: do QueryRunning check if the map is loaded */
697 m_Libs.push_back(pExt);
702 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
704 CExtension *pExt = (CExtension *)pRequester;
705 CExtension *pOwner = (CExtension *)pInfo->owner;
707 pExt->AddDependency(pInfo);
709 IExtensionInterface *pAPI = pExt->GetAPI();
710 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
712 pOwner->AddChildDependent(pExt, pInfo->iface);
716 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
718 CExtension *pExt = (CExtension *)ext;
719 CExtension *pOwner = GetExtensionFromIdent(other);
723 info.iface = (SMInterface *)iface;
726 pExt->AddDependency(&info);
727 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
730 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
732 CExtension *pExt = (CExtension *)pOwner;
734 pExt->AddInterface(pInterface);
737 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
739 CExtension *pExt = (CExtension *)pParent;
741 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
744 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
746 List<CExtension *>::iterator iter;
748 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
750 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
754 CExtension *CExtensionManager::FindByOrder(unsigned int num)
756 if (num < 1 || num > m_Libs.size())
761 List<CExtension *>::iterator iter = m_Libs.begin();
763 while (iter != m_Libs.end())
775 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
782 CExtension *pExt = (CExtension *)_pExt;
784 if (m_Libs.find(pExt) == m_Libs.end())
789 /* Tell it to unload */
790 if (pExt->IsLoaded())
792 IExtensionInterface *pAPI = pExt->GetAPI();
793 pAPI->OnExtensionUnload();
796 /* First remove us from internal lists */
797 g_ShareSys.RemoveInterfaces(_pExt);
800 List<CExtension *> UnloadQueue;
802 /* Handle dependencies */
803 if (pExt->IsLoaded())
805 /* Unload any dependent plugins */
806 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
807 while (p_iter != pExt->m_Dependents.end())
809 /* We have to manually unlink ourselves here, since we're no longer being managed */
810 scripts->UnloadPlugin((*p_iter));
811 p_iter = pExt->m_Dependents.erase(p_iter);
814 List<String>::iterator s_iter;
815 for (s_iter = pExt->m_Libraries.begin();
816 s_iter != pExt->m_Libraries.end();
819 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
822 /* Notify and/or unload all dependencies */
823 List<CExtension *>::iterator c_iter;
825 IExtensionInterface *pAPI;
826 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
829 if ((pAPI=pDep->GetAPI()) == NULL)
837 /* Now, get its dependency list */
838 bool dropped = false;
839 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
840 while (i_iter != pDep->m_Deps.end())
842 if ((*i_iter).owner == _pExt)
844 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
849 UnloadQueue.push_back(pDep);
852 pAPI->NotifyInterfaceDrop((*i_iter).iface);
853 i_iter = pDep->m_Deps.erase(i_iter);
860 /* Flush out any back references to this plugin */
861 i_iter = pDep->m_ChildDeps.begin();
862 while (i_iter != pDep->m_ChildDeps.end())
864 if ((*i_iter).owner == pExt)
866 i_iter = pDep->m_ChildDeps.erase(i_iter);
875 /* Unbind our natives from Core */
876 pExt->DropEverything();
879 IdentityToken_t *pIdentity;
880 if ((pIdentity = pExt->GetIdentity()) != NULL)
882 SMGlobalClass *glob = SMGlobalClass::head;
885 glob->OnSourceModIdentityDropped(pIdentity);
886 glob = glob->m_pGlobalClassNext;
893 List<CExtension *>::iterator iter;
894 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
896 /* NOTE: This is safe because the unload function backs out of anything not present */
897 UnloadExtension((*iter));
903 void CExtensionManager::MarkAllLoaded()
905 List<CExtension *>::iterator iter;
908 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
911 if (!pExt->IsLoaded())
915 if (pExt->m_bFullyLoaded)
919 pExt->MarkAllLoaded();
923 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
925 /* This function doesn't really need to do anything now. We make sure the
926 * other extension is loaded, but handling of dependencies is really done
927 * by the interface fetcher.
929 if (required || autoload)
931 LoadAutoExtension(file);
935 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
937 int argcount = smcore.Argc(command);
940 const char *cmd = smcore.Arg(command, 2);
941 if (strcmp(cmd, "list") == 0)
943 List<CExtension *>::iterator iter;
945 unsigned int num = 1;
946 switch (m_Libs.size())
950 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
955 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
960 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
964 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
967 if (pExt->IsLoaded())
970 if (!pExt->IsRunning(error, sizeof(error)))
972 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
976 IExtensionInterface *pAPI = pExt->GetAPI();
977 const char *name = pAPI->GetExtensionName();
978 const char *version = pAPI->GetExtensionVerString();
979 const char *descr = pAPI->GetExtensionDescription();
980 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
983 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
988 else if (strcmp(cmd, "load") == 0)
992 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
996 const char *filename = smcore.Arg(command, 3);
997 char path[PLATFORM_MAX_PATH];
1000 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1001 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1003 if (FindExtensionByFile(path) != NULL)
1005 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1009 if (LoadExtension(path, error, sizeof(error)))
1011 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1014 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1019 else if (strcmp(cmd, "info") == 0)
1023 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1027 const char *sId = smcore.Arg(command, 3);
1028 unsigned int id = atoi(sId);
1031 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1035 if (m_Libs.size() == 0)
1037 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1041 if (id > m_Libs.size())
1043 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1047 List<CExtension *>::iterator iter = m_Libs.begin();
1048 CExtension *pExt = NULL;
1049 while (iter != m_Libs.end())
1058 /* This should never happen */
1061 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1065 if (!pExt->IsLoaded())
1067 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1068 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1073 if (!pExt->IsRunning(error, sizeof(error)))
1075 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1076 rootmenu->ConsolePrint(" Loaded: Yes");
1077 rootmenu->ConsolePrint(" Running: No (%s)", error);
1081 IExtensionInterface *pAPI = pExt->GetAPI();
1082 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1083 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1084 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1085 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1086 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1088 if (pExt->IsExternal())
1090 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1092 else if (pAPI->IsMetamodExtension())
1094 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1098 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1104 else if (strcmp(cmd, "unload") == 0)
1108 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1112 const char *arg = smcore.Arg(command, 3);
1113 unsigned int num = atoi(arg);
1114 CExtension *pExt = FindByOrder(num);
1118 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1122 if (argcount > 4 && pExt->unload_code)
1124 const char *unload = smcore.Arg(command, 4);
1125 if (pExt->unload_code == (unsigned)atoi(unload))
1127 char filename[PLATFORM_MAX_PATH];
1128 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1129 UnloadExtension(pExt);
1130 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1134 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1139 if (!pExt->IsLoaded()
1140 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1142 char filename[PLATFORM_MAX_PATH];
1143 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1144 UnloadExtension(pExt);
1145 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1150 List<CPlugin *> plugins;
1151 if (pExt->m_ChildDeps.size())
1153 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1154 List<CExtension *>::iterator iter;
1156 /* Get list of all extensions */
1157 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1159 List<IfaceInfo>::iterator i_iter;
1161 if (!pOther->IsLoaded() || pOther == pExt)
1165 /* Get their dependencies */
1166 for (i_iter=pOther->m_Deps.begin();
1167 i_iter!=pOther->m_Deps.end();
1170 /* Is this dependency to us? */
1171 if ((*i_iter).owner != pExt)
1175 /* Will our dependent care? */
1176 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1178 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1179 /* Add to plugin unload list */
1180 List<CPlugin *>::iterator p_iter;
1181 for (p_iter=pOther->m_Dependents.begin();
1182 p_iter!=pOther->m_Dependents.end();
1185 if (plugins.find((*p_iter)) == plugins.end())
1187 plugins.push_back((*p_iter));
1194 if (pExt->m_Dependents.size())
1196 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1197 List<CPlugin *>::iterator iter;
1199 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1202 if (plugins.find(pPlugin) == plugins.end())
1204 plugins.push_back(pPlugin);
1207 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1210 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1213 srand(static_cast<int>(time(NULL)));
1214 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1215 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1216 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1221 else if (strcmp(cmd, "reload") == 0)
1225 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1229 const char *arg = smcore.Arg(command, 3);
1230 unsigned int num = atoi(arg);
1231 CExtension *pExt = FindByOrder(num);
1235 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1239 if (pExt->IsLoaded())
1241 char filename[PLATFORM_MAX_PATH];
1244 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1246 if (pExt->Reload(error, sizeof(error)))
1248 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1252 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1259 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1267 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1268 rootmenu->DrawGenericOption("info", "Extra extension information");
1269 rootmenu->DrawGenericOption("list", "List extensions");
1270 rootmenu->DrawGenericOption("load", "Load an extension");
1271 rootmenu->DrawGenericOption("reload", "Reload an extension");
1272 rootmenu->DrawGenericOption("unload", "Unload an extension");
1275 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1277 if (ptr->type == g_ExtType)
1279 return (CExtension *)(ptr->ptr);
1285 CExtensionManager::CExtensionManager()
1289 CExtensionManager::~CExtensionManager()
1293 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1295 CExtension *pExt = (CExtension *)pSource;
1296 pExt->AddLibrary(library);
1297 scripts->OnLibraryAction(library, LibraryAction_Added);
1300 bool CExtensionManager::LibraryExists(const char *library)
1304 for (List<CExtension *>::iterator iter = m_Libs.begin();
1305 iter != m_Libs.end();
1309 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1310 s_iter != pExt->m_Libraries.end();
1313 if ((*s_iter).compare(library) == 0)
1323 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1324 const char *filepath,
1325 const char *filename,
1329 IExtension *pAlready;
1330 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1335 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1337 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1344 m_Libs.push_back(pExt);
1349 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1351 IExtensionInterface *pAPI;
1352 List<CExtension *>::iterator iter;
1354 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1356 if ((pAPI = (*iter)->GetAPI()) == NULL)
1360 if (pAPI->GetExtensionVersion() > 3)
1362 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1367 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1369 CVector<IExtension *> *list = new CVector<IExtension *>();
1370 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1371 list->push_back(*iter);
1375 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1377 delete const_cast<CVector<IExtension *> *>(list);
1380 bool CLocalExtension::IsSameFile(const char *file)
1382 /* Only care about the shortened name. */
1383 return strcmp(file, m_File.c_str()) == 0;
1386 bool CRemoteExtension::IsSameFile(const char *file)
1388 /* :TODO: this could be better, but no one uses this API anyway. */
1389 return strcmp(file, m_Path.c_str()) == 0;