Update CS:GO team score 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"
38 #include <am-utility.h>
40 CExtensionManager g_Extensions;
41 IdentityType_t g_ExtType;
43 void CExtension::Initialize(const char *filename, const char *path)
48 m_bFullyLoaded = false;
49 m_File.assign(filename);
51 char real_name[PLATFORM_MAX_PATH];
52 libsys->GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
53 m_RealFile.assign(real_name);
56 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
58 Initialize(filename, path);
62 CLocalExtension::CLocalExtension(const char *filename)
67 char path[PLATFORM_MAX_PATH];
69 /* Special case for new bintools binary */
70 if (strcmp(filename, "bintools.ext") == 0)
75 /* Zeroth, see if there is an engine specific build in the new place. */
76 g_pSM->BuildPath(Path_SM,
79 "extensions/%s.%s." PLATFORM_LIB_EXT,
83 if (libsys->IsPathFile(path))
88 /* COMPAT HACK: One-halfth, if ep2v, see if there is an engine specific build in the new place with old naming */
89 if (strcmp(smcore.gamesuffix, "2.tf2") == 0
90 || strcmp(smcore.gamesuffix, "2.dods") == 0
91 || strcmp(smcore.gamesuffix, "2.hl2dm") == 0
94 g_pSM->BuildPath(Path_SM,
97 "extensions/%s.2.ep2v." PLATFORM_LIB_EXT,
100 if (libsys->IsPathFile(path))
105 else if (strcmp(smcore.gamesuffix, "2.nd") == 0)
107 g_pSM->BuildPath(Path_SM,
110 "extensions/%s.2.l4d2." PLATFORM_LIB_EXT,
113 if (libsys->IsPathFile(path))
119 /* First see if there is an engine specific build! */
120 g_pSM->BuildPath(Path_SM,
123 "extensions/auto.%s/%s." PLATFORM_LIB_EXT,
127 /* Try the "normal" version */
128 if (!libsys->IsPathFile(path))
131 g_pSM->BuildPath(Path_SM,
134 "extensions/%s." PLATFORM_LIB_EXT,
139 Initialize(filename, path);
142 bool CRemoteExtension::Load(char *error, size_t maxlength)
144 if (!PerformAPICheck(error, maxlength))
150 if (!CExtension::Load(error, maxlength))
159 bool CLocalExtension::Load(char *error, size_t maxlength)
161 m_pLib = libsys->OpenLibrary(m_Path.c_str(), error, maxlength);
168 typedef IExtensionInterface *(*GETAPI)();
169 GETAPI pfnGetAPI = NULL;
171 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
173 m_pLib->CloseLibrary();
175 snprintf(error, maxlength, "Unable to find extension entry point");
179 m_pAPI = pfnGetAPI();
181 /* Check pointer and version */
182 if (!PerformAPICheck(error, maxlength))
184 m_pLib->CloseLibrary();
191 if (m_pAPI->IsMetamodExtension())
194 m_PlId = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
198 m_pLib->CloseLibrary();
205 if (!CExtension::Load(error, maxlength))
207 if (m_pAPI->IsMetamodExtension())
211 smcore.UnloadMMSPlugin(m_PlId);
215 m_pLib->CloseLibrary();
224 void CRemoteExtension::Unload()
228 void CLocalExtension::Unload()
230 if (m_pAPI != NULL && m_PlId)
232 smcore.UnloadMMSPlugin(m_PlId);
238 m_pLib->CloseLibrary();
243 bool CRemoteExtension::Reload(char *error, size_t maxlength)
245 snprintf(error, maxlength, "Remote extensions do not support reloading");
249 bool CLocalExtension::Reload(char *error, size_t maxlength)
251 if (m_pLib == NULL) // FIXME: just load it instead?
254 m_pAPI->OnExtensionUnload();
257 return Load(error, maxlength);
260 bool CRemoteExtension::IsExternal()
265 bool CLocalExtension::IsExternal()
270 CExtension::~CExtension()
275 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
279 snprintf(error, maxlength, "No IExtensionInterface instance provided");
283 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
285 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
292 bool CExtension::Load(char *error, size_t maxlength)
295 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
302 /* Check if we're past load time */
303 if (!smcore.IsMapLoading())
305 m_pAPI->OnExtensionsAllLoaded();
312 void CExtension::CreateIdentity()
314 if (m_pIdentToken != NULL)
319 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
322 void CExtension::DestroyIdentity()
324 if (m_pIdentToken == NULL)
329 g_ShareSys.DestroyIdentity(m_pIdentToken);
330 m_pIdentToken = NULL;
333 void CExtension::MarkAllLoaded()
335 assert(m_bFullyLoaded == false);
338 m_bFullyLoaded = true;
339 m_pAPI->OnExtensionsAllLoaded();
343 void CExtension::AddPlugin(CPlugin *pPlugin)
345 /* Unfortunately we have to do this :( */
346 if (m_Dependents.find(pPlugin) == m_Dependents.end())
348 m_Dependents.push_back(pPlugin);
352 void CExtension::SetError(const char *error)
354 m_Error.assign(error);
357 IExtensionInterface *CExtension::GetAPI()
362 const char *CExtension::GetFilename()
364 return m_RealFile.c_str();
367 const char *CExtension::GetPath() const
369 return m_Path.c_str();
372 IdentityToken_t *CExtension::GetIdentity()
374 return m_pIdentToken;
377 bool CRemoteExtension::IsLoaded()
379 return (m_pAPI != NULL);
382 bool CLocalExtension::IsLoaded()
384 return (m_pLib != NULL);
387 void CExtension::AddDependency(const IfaceInfo *pInfo)
389 if (m_Deps.find(*pInfo) == m_Deps.end())
391 m_Deps.push_back(*pInfo);
395 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
397 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
400 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
406 List<IfaceInfo>::iterator iter;
407 for (iter = m_ChildDeps.begin();
408 iter != m_ChildDeps.end();
411 IfaceInfo &other = (*iter);
418 m_ChildDeps.push_back(info);
421 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
423 List<IfaceInfo>::iterator iter = m_Deps.begin();
425 if (iter == m_Deps.end())
432 *pOwner = (*iter).owner;
436 *pInterface = (*iter).iface;
439 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
441 return (ITERATOR *)pIter;
444 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
446 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
447 List<IfaceInfo>::iterator _iter;
449 if (_iter == m_Deps.end())
458 *pOwner = (*_iter).owner;
462 *pInterface = (*_iter).iface;
467 if (_iter == m_Deps.end())
475 void CExtension::FreeDependencyIterator(ITERATOR *iter)
477 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
482 void CExtension::AddInterface(SMInterface *pInterface)
484 m_Interfaces.push_back(pInterface);
487 bool CExtension::IsRunning(char *error, size_t maxlength)
493 snprintf(error, maxlength, "%s", m_Error.c_str());
498 return m_pAPI->QueryRunning(error, maxlength);
501 void CExtension::AddLibrary(const char *library)
503 m_Libraries.push_back(library);
506 /*********************
507 * EXTENSION MANAGER *
508 *********************/
510 void CExtensionManager::OnSourceModAllInitialized()
512 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
513 pluginsys->AddPluginsListener(this);
514 rootmenu->AddRootConsoleCommand("exts", "Manage extensions", this);
515 g_ShareSys.AddInterface(NULL, this);
518 void CExtensionManager::OnSourceModShutdown()
520 rootmenu->RemoveRootConsoleCommand("exts", this);
521 pluginsys->RemovePluginsListener(this);
522 g_ShareSys.DestroyIdentType(g_ExtType);
525 void CExtensionManager::Shutdown()
527 List<CExtension *>::iterator iter;
529 while ((iter = m_Libs.begin()) != m_Libs.end())
531 UnloadExtension((*iter));
535 void CExtensionManager::TryAutoload()
537 char path[PLATFORM_MAX_PATH];
539 g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
541 ke::AutoPtr<IDirectory> pDir(libsys->OpenDirectory(path));
547 while (pDir->MoreFiles())
549 if (pDir->IsEntryDirectory())
555 lfile = pDir->GetEntryName();
557 if (len <= 9) /* size of ".autoload" */
563 if (strcmp(&lfile[len - 9], ".autoload") != 0)
569 char file[PLATFORM_MAX_PATH];
570 len = smcore.Format(file, sizeof(file), "%s", lfile);
571 strcpy(&file[len - 9], ".ext");
573 LoadAutoExtension(file);
579 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
581 /* Remove platform extension if it's there. Compat hack. */
582 const char *ext = libsys->GetFileExtension(path);
583 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
585 char path2[PLATFORM_MAX_PATH];
586 smcore.Format(path2, sizeof(path2), "%s", path);
587 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
588 return LoadAutoExtension(path2, bErrorOnMissing);
591 IExtension *pAlready;
592 if ((pAlready=FindExtensionByFile(path)) != NULL)
598 CExtension *p = new CLocalExtension(path);
600 /* We put us in the list beforehand so extensions that check for each other
601 * won't recursively load each other.
605 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
607 if (bErrorOnMissing || libsys->IsPathFile(p->GetPath()))
609 smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
618 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
620 List<CExtension *>::iterator iter;
623 /* Chomp off the path */
624 char lookup[PLATFORM_MAX_PATH];
625 libsys->GetFileFromPath(lookup, sizeof(lookup), file);
627 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
630 if (pExt->IsSameFile(lookup))
639 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
641 List<CExtension *>::iterator iter;
643 IExtensionInterface *pAPI;
646 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
649 if (!pExt->IsLoaded())
653 if ((pAPI = pExt->GetAPI()) == NULL)
657 name = pAPI->GetExtensionName();
662 if (strcmp(name, ext) == 0)
671 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
673 /* Remove platform extension if it's there. Compat hack. */
674 const char *ext = libsys->GetFileExtension(file);
675 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
677 char path2[PLATFORM_MAX_PATH];
678 smcore.Format(path2, sizeof(path2), "%s", file);
679 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
680 return LoadExtension(path2, error, maxlength);
683 IExtension *pAlready;
684 if ((pAlready=FindExtensionByFile(file)) != NULL)
689 CExtension *pExt = new CLocalExtension(file);
691 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
698 /* :TODO: do QueryRunning check if the map is loaded */
700 m_Libs.push_back(pExt);
705 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
707 CExtension *pExt = (CExtension *)pRequester;
708 CExtension *pOwner = (CExtension *)pInfo->owner;
710 pExt->AddDependency(pInfo);
712 IExtensionInterface *pAPI = pExt->GetAPI();
713 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
715 pOwner->AddChildDependent(pExt, pInfo->iface);
719 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
721 CExtension *pExt = (CExtension *)ext;
722 CExtension *pOwner = GetExtensionFromIdent(other);
726 info.iface = (SMInterface *)iface;
729 pExt->AddDependency(&info);
730 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
733 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
735 CExtension *pExt = (CExtension *)pOwner;
737 pExt->AddInterface(pInterface);
740 void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
742 CExtension *pExt = (CExtension *)pParent;
744 pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
747 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
749 List<CExtension *>::iterator iter;
751 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
753 (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
757 CExtension *CExtensionManager::FindByOrder(unsigned int num)
759 if (num < 1 || num > m_Libs.size())
764 List<CExtension *>::iterator iter = m_Libs.begin();
766 while (iter != m_Libs.end())
778 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
783 CExtension *pExt = (CExtension *)_pExt;
785 if (m_Libs.find(pExt) == m_Libs.end())
788 /* Tell it to unload */
789 if (pExt->IsLoaded())
790 pExt->GetAPI()->OnExtensionUnload();
792 // Remove us from internal lists. Note that because we do this, it's
793 // possible that our extension could be added back if another plugin
794 // tries to load during this process. If we ever find this to happen,
795 // we can just block plugin loading.
796 g_ShareSys.RemoveInterfaces(_pExt);
799 List<CExtension *> UnloadQueue;
801 /* Handle dependencies */
802 if (pExt->IsLoaded())
804 /* Unload any dependent plugins */
805 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
806 while (p_iter != pExt->m_Dependents.end())
808 /* We have to manually unlink ourselves here, since we're no longer being managed */
809 scripts->UnloadPlugin((*p_iter));
810 p_iter = pExt->m_Dependents.erase(p_iter);
813 List<String>::iterator s_iter;
814 for (s_iter = pExt->m_Libraries.begin();
815 s_iter != pExt->m_Libraries.end();
818 scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
821 /* Notify and/or unload all dependencies */
822 List<CExtension *>::iterator c_iter;
824 IExtensionInterface *pAPI;
825 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
828 if ((pAPI=pDep->GetAPI()) == NULL)
832 /* Now, get its dependency list */
833 bool dropped = false;
834 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
835 while (i_iter != pDep->m_Deps.end())
837 if ((*i_iter).owner == _pExt)
839 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
844 UnloadQueue.push_back(pDep);
847 pAPI->NotifyInterfaceDrop((*i_iter).iface);
848 i_iter = pDep->m_Deps.erase(i_iter);
855 /* Flush out any back references to this plugin */
856 i_iter = pDep->m_ChildDeps.begin();
857 while (i_iter != pDep->m_ChildDeps.end())
859 if ((*i_iter).owner == pExt)
860 i_iter = pDep->m_ChildDeps.erase(i_iter);
866 /* Unbind our natives from Core */
867 pExt->DropEverything();
870 IdentityToken_t *pIdentity;
871 if ((pIdentity = pExt->GetIdentity()) != NULL)
873 SMGlobalClass *glob = SMGlobalClass::head;
876 glob->OnSourceModIdentityDropped(pIdentity);
877 glob = glob->m_pGlobalClassNext;
881 // Everything has been informed that we're unloading, so give the
882 // extension one last notification.
883 if (pExt->IsLoaded() && pExt->GetAPI()->GetExtensionVersion() >= 7)
884 pExt->GetAPI()->OnDependenciesDropped();
889 List<CExtension *>::iterator iter;
890 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
892 /* NOTE: This is safe because the unload function backs out of anything not present */
893 UnloadExtension((*iter));
899 void CExtensionManager::MarkAllLoaded()
901 List<CExtension *>::iterator iter;
904 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
907 if (!pExt->IsLoaded())
911 if (pExt->m_bFullyLoaded)
915 pExt->MarkAllLoaded();
919 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
921 /* This function doesn't really need to do anything now. We make sure the
922 * other extension is loaded, but handling of dependencies is really done
923 * by the interface fetcher.
925 if (required || autoload)
927 LoadAutoExtension(file);
931 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
933 int argcount = smcore.Argc(command);
936 const char *cmd = smcore.Arg(command, 2);
937 if (strcmp(cmd, "list") == 0)
939 List<CExtension *>::iterator iter;
941 unsigned int num = 1;
942 switch (m_Libs.size())
946 rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
951 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
956 rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
960 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
963 if (pExt->IsLoaded())
966 if (!pExt->IsRunning(error, sizeof(error)))
968 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
972 IExtensionInterface *pAPI = pExt->GetAPI();
973 const char *name = pAPI->GetExtensionName();
974 const char *version = pAPI->GetExtensionVerString();
975 const char *descr = pAPI->GetExtensionDescription();
976 rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
979 rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
984 else if (strcmp(cmd, "load") == 0)
988 rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
992 const char *filename = smcore.Arg(command, 3);
993 char path[PLATFORM_MAX_PATH];
996 smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
997 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
999 if (FindExtensionByFile(path) != NULL)
1001 rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1005 if (LoadExtension(path, error, sizeof(error)))
1007 rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1010 rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1015 else if (strcmp(cmd, "info") == 0)
1019 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1023 const char *sId = smcore.Arg(command, 3);
1024 unsigned int id = atoi(sId);
1027 rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1031 if (m_Libs.size() == 0)
1033 rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1037 if (id > m_Libs.size())
1039 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1043 List<CExtension *>::iterator iter = m_Libs.begin();
1044 CExtension *pExt = NULL;
1045 while (iter != m_Libs.end())
1054 /* This should never happen */
1057 rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1061 if (!pExt->IsLoaded())
1063 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1064 rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1069 if (!pExt->IsRunning(error, sizeof(error)))
1071 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1072 rootmenu->ConsolePrint(" Loaded: Yes");
1073 rootmenu->ConsolePrint(" Running: No (%s)", error);
1077 IExtensionInterface *pAPI = pExt->GetAPI();
1078 rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1079 rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1080 rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1081 rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1082 rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1084 if (pExt->IsExternal())
1086 rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1088 else if (pAPI->IsMetamodExtension())
1090 rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1094 rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1100 else if (strcmp(cmd, "unload") == 0)
1104 rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1108 const char *arg = smcore.Arg(command, 3);
1109 unsigned int num = atoi(arg);
1110 CExtension *pExt = FindByOrder(num);
1114 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1118 if (argcount > 4 && pExt->unload_code)
1120 const char *unload = smcore.Arg(command, 4);
1121 if (pExt->unload_code == (unsigned)atoi(unload))
1123 char filename[PLATFORM_MAX_PATH];
1124 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1125 UnloadExtension(pExt);
1126 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1130 rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1135 if (!pExt->IsLoaded()
1136 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1138 char filename[PLATFORM_MAX_PATH];
1139 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1140 UnloadExtension(pExt);
1141 rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1146 List<CPlugin *> plugins;
1147 if (pExt->m_ChildDeps.size())
1149 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1150 List<CExtension *>::iterator iter;
1152 /* Get list of all extensions */
1153 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1155 List<IfaceInfo>::iterator i_iter;
1157 if (!pOther->IsLoaded() || pOther == pExt)
1161 /* Get their dependencies */
1162 for (i_iter=pOther->m_Deps.begin();
1163 i_iter!=pOther->m_Deps.end();
1166 /* Is this dependency to us? */
1167 if ((*i_iter).owner != pExt)
1171 /* Will our dependent care? */
1172 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1174 rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1175 /* Add to plugin unload list */
1176 List<CPlugin *>::iterator p_iter;
1177 for (p_iter=pOther->m_Dependents.begin();
1178 p_iter!=pOther->m_Dependents.end();
1181 if (plugins.find((*p_iter)) == plugins.end())
1183 plugins.push_back((*p_iter));
1190 if (pExt->m_Dependents.size())
1192 rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1193 List<CPlugin *>::iterator iter;
1195 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1198 if (plugins.find(pPlugin) == plugins.end())
1200 plugins.push_back(pPlugin);
1203 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1206 rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1209 srand(static_cast<int>(time(NULL)));
1210 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1211 rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1212 rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1217 else if (strcmp(cmd, "reload") == 0)
1221 rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1225 const char *arg = smcore.Arg(command, 3);
1226 unsigned int num = atoi(arg);
1227 CExtension *pExt = FindByOrder(num);
1231 rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1235 if (pExt->IsLoaded())
1237 char filename[PLATFORM_MAX_PATH];
1240 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1242 if (pExt->Reload(error, sizeof(error)))
1244 rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1248 rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1255 rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1263 rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1264 rootmenu->DrawGenericOption("info", "Extra extension information");
1265 rootmenu->DrawGenericOption("list", "List extensions");
1266 rootmenu->DrawGenericOption("load", "Load an extension");
1267 rootmenu->DrawGenericOption("reload", "Reload an extension");
1268 rootmenu->DrawGenericOption("unload", "Unload an extension");
1271 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1273 if (ptr->type == g_ExtType)
1275 return (CExtension *)(ptr->ptr);
1281 CExtensionManager::CExtensionManager()
1285 CExtensionManager::~CExtensionManager()
1289 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1291 CExtension *pExt = (CExtension *)pSource;
1292 pExt->AddLibrary(library);
1293 scripts->OnLibraryAction(library, LibraryAction_Added);
1296 bool CExtensionManager::LibraryExists(const char *library)
1300 for (List<CExtension *>::iterator iter = m_Libs.begin();
1301 iter != m_Libs.end();
1305 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1306 s_iter != pExt->m_Libraries.end();
1309 if ((*s_iter).compare(library) == 0)
1319 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1320 const char *filepath,
1321 const char *filename,
1325 IExtension *pAlready;
1326 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1331 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1333 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1340 m_Libs.push_back(pExt);
1345 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1347 IExtensionInterface *pAPI;
1348 List<CExtension *>::iterator iter;
1350 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1352 if ((pAPI = (*iter)->GetAPI()) == NULL)
1356 if (pAPI->GetExtensionVersion() > 3)
1358 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1363 const CVector<IExtension *> *CExtensionManager::ListExtensions()
1365 CVector<IExtension *> *list = new CVector<IExtension *>();
1366 for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1367 list->push_back(*iter);
1371 void CExtensionManager::FreeExtensionList(const CVector<IExtension *> *list)
1373 delete const_cast<CVector<IExtension *> *>(list);
1376 bool CLocalExtension::IsSameFile(const char *file)
1378 /* Only care about the shortened name. */
1379 return strcmp(file, m_File.c_str()) == 0;
1382 bool CRemoteExtension::IsSameFile(const char *file)
1384 /* :TODO: this could be better, but no one uses this API anyway. */
1385 return strcmp(file, m_Path.c_str()) == 0;