Re-enabled PDB files.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "ExtensionSys.h"
34 #include "LibrarySys.h"
37 #include "sourcemm_api.h"
38 #include "sm_srvcmds.h"
39 #include "sm_stringutil.h"
41 CExtensionManager g_Extensions;
42 IdentityType_t g_ExtType;
44 void CExtension::Initialize(const char *filename, const char *path)
49 m_bFullyLoaded = false;
50 m_File.assign(filename);
52 char real_name[PLATFORM_MAX_PATH];
53 g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
54 m_RealFile.assign(real_name);
57 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
59 Initialize(filename, path);
63 #if defined METAMOD_PLAPI_VERSION
64 #if SOURCE_ENGINE == SE_LEFT4DEAD
65 #define GAMEFIX "2.l4d"
66 #elif SOURCE_ENGINE == SE_ORANGEBOX
67 #define GAMEFIX "2.ep2"
68 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
69 #define GAMEFIX "2.ep2v"
70 #elif SOURCE_ENGINE == SE_DARKMESSIAH
71 #define GAMEFIX "2.darkm"
73 #define GAMEFIX "2.ep1"
74 #endif //SOURCE_ENGINE == SE_LEFT4DEAD
75 #else //METAMOD_PLAPI_VERSION
76 #define GAMEFIX "1.ep1"
77 #endif //METAMOD_PLAPI_VERSION
79 CLocalExtension::CLocalExtension(const char *filename)
84 char path[PLATFORM_MAX_PATH];
86 /* Zeroth, see if there is an engine specific build in the new place. */
87 g_SourceMod.BuildPath(Path_SM,
90 "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
93 if (g_LibSys.IsPathFile(path))
98 /* First see if there is an engine specific build! */
99 g_SourceMod.BuildPath(Path_SM,
102 "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
105 /* Try the "normal" version */
106 if (!g_LibSys.IsPathFile(path))
108 g_SourceMod.BuildPath(Path_SM,
111 "extensions/%s." PLATFORM_LIB_EXT,
116 Initialize(filename, path);
119 bool CRemoteExtension::Load(char *error, size_t maxlength)
121 if (!PerformAPICheck(error, maxlength))
127 if (!CExtension::Load(error, maxlength))
136 bool CLocalExtension::Load(char *error, size_t maxlength)
138 m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
145 typedef IExtensionInterface *(*GETAPI)();
146 GETAPI pfnGetAPI = NULL;
148 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
150 m_pLib->CloseLibrary();
152 snprintf(error, maxlength, "Unable to find extension entry point");
156 m_pAPI = pfnGetAPI();
158 /* Check pointer and version */
159 if (!PerformAPICheck(error, maxlength))
161 m_pLib->CloseLibrary();
168 if (m_pAPI->IsMetamodExtension())
171 m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
174 if (!CExtension::Load(error, maxlength))
176 if (m_pAPI->IsMetamodExtension())
181 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
185 m_pLib->CloseLibrary();
194 void CRemoteExtension::Unload()
198 void CLocalExtension::Unload()
200 if (m_pAPI != NULL && m_PlId)
202 char temp_buffer[255];
203 g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
209 m_pLib->CloseLibrary();
214 bool CRemoteExtension::Reload(char *error, size_t maxlength)
216 snprintf(error, maxlength, "Remote extensions do not support reloading");
220 bool CLocalExtension::Reload(char *error, size_t maxlength)
222 if (m_pLib == NULL) // FIXME: just load it instead?
225 m_pAPI->OnExtensionUnload();
228 return Load(error, maxlength);
231 bool CRemoteExtension::IsExternal()
236 bool CLocalExtension::IsExternal()
241 CExtension::~CExtension()
246 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
250 snprintf(error, maxlength, "No IExtensionInterface instance provided");
254 if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
256 snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
263 bool CExtension::Load(char *error, size_t maxlength)
266 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
273 /* Check if we're past load time */
274 if (!g_SourceMod.IsMapLoading())
276 m_pAPI->OnExtensionsAllLoaded();
283 void CExtension::CreateIdentity()
285 if (m_pIdentToken != NULL)
290 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
293 void CExtension::DestroyIdentity()
295 if (m_pIdentToken == NULL)
300 g_ShareSys.DestroyIdentity(m_pIdentToken);
301 m_pIdentToken = NULL;
304 void CExtension::MarkAllLoaded()
306 assert(m_bFullyLoaded == false);
309 m_bFullyLoaded = true;
310 m_pAPI->OnExtensionsAllLoaded();
314 void CExtension::AddPlugin(CPlugin *pPlugin)
316 /* Unfortunately we have to do this :( */
317 if (m_Dependents.find(pPlugin) != m_Dependents.end())
319 m_Dependents.push_back(pPlugin);
323 void CExtension::SetError(const char *error)
325 m_Error.assign(error);
328 IExtensionInterface *CExtension::GetAPI()
333 const char *CExtension::GetFilename()
335 return m_RealFile.c_str();
338 IdentityToken_t *CExtension::GetIdentity()
340 return m_pIdentToken;
343 bool CRemoteExtension::IsLoaded()
345 return (m_pAPI != NULL);
348 bool CLocalExtension::IsLoaded()
350 return (m_pLib != NULL);
353 void CExtension::AddDependency(const IfaceInfo *pInfo)
355 if (m_Deps.find(*pInfo) == m_Deps.end())
357 m_Deps.push_back(*pInfo);
361 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
363 return (i1.iface == i2.iface) && (i1.owner == i2.owner);
366 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
372 List<IfaceInfo>::iterator iter;
373 for (iter = m_ChildDeps.begin();
374 iter != m_ChildDeps.end();
377 IfaceInfo &other = (*iter);
384 m_ChildDeps.push_back(info);
387 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
389 List<IfaceInfo>::iterator iter = m_Deps.begin();
391 if (iter == m_Deps.end())
398 *pOwner = (*iter).owner;
402 *pInterface = (*iter).iface;
405 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
407 return (ITERATOR *)pIter;
410 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
412 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
413 List<IfaceInfo>::iterator _iter;
415 if (_iter == m_Deps.end())
424 *pOwner = (*_iter).owner;
428 *pInterface = (*_iter).iface;
433 if (_iter == m_Deps.end())
441 void CExtension::FreeDependencyIterator(ITERATOR *iter)
443 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
448 void CExtension::AddInterface(SMInterface *pInterface)
450 m_Interfaces.push_back(pInterface);
453 bool CExtension::IsRunning(char *error, size_t maxlength)
459 snprintf(error, maxlength, "%s", m_Error.c_str());
464 return m_pAPI->QueryRunning(error, maxlength);
467 void CExtension::AddLibrary(const char *library)
469 m_Libraries.push_back(library);
472 /*********************
473 * EXTENSION MANAGER *
474 *********************/
476 void CExtensionManager::OnSourceModAllInitialized()
478 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
479 g_PluginSys.AddPluginsListener(this);
480 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
481 g_ShareSys.AddInterface(NULL, this);
484 void CExtensionManager::OnSourceModShutdown()
486 g_RootMenu.RemoveRootConsoleCommand("exts", this);
487 g_ShareSys.DestroyIdentType(g_ExtType);
490 void CExtensionManager::Shutdown()
492 List<CExtension *>::iterator iter;
494 while ((iter = m_Libs.begin()) != m_Libs.end())
496 UnloadExtension((*iter));
500 void CExtensionManager::TryAutoload()
502 char path[PLATFORM_MAX_PATH];
504 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
506 IDirectory *pDir = g_LibSys.OpenDirectory(path);
514 while (pDir->MoreFiles())
516 if (pDir->IsEntryDirectory())
522 lfile = pDir->GetEntryName();
524 if (len <= 9) /* size of ".autoload" */
530 if (strcmp(&lfile[len - 9], ".autoload") != 0)
536 char file[PLATFORM_MAX_PATH];
537 len = UTIL_Format(file, sizeof(file), "%s", lfile);
538 strcpy(&file[len - 9], ".ext");
540 LoadAutoExtension(file);
546 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
548 /* Remove platform extension if it's there. Compat hack. */
549 const char *ext = g_LibSys.GetFileExtension(path);
550 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
552 char path2[PLATFORM_MAX_PATH];
553 UTIL_Format(path2, sizeof(path2), "%s", path);
554 path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
555 return LoadAutoExtension(path2);
558 IExtension *pAlready;
559 if ((pAlready=FindExtensionByFile(path)) != NULL)
565 CExtension *p = new CLocalExtension(path);
567 /* We put us in the list beforehand so extensions that check for each other
568 * won't recursively load each other.
572 if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
574 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
581 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
583 List<CExtension *>::iterator iter;
586 /* Chomp off the path */
587 char lookup[PLATFORM_MAX_PATH];
588 g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
590 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
593 if (pExt->IsSameFile(lookup))
602 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
604 List<CExtension *>::iterator iter;
606 IExtensionInterface *pAPI;
609 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
612 if (!pExt->IsLoaded())
616 if ((pAPI = pExt->GetAPI()) == NULL)
620 name = pAPI->GetExtensionName();
625 if (strcmp(name, ext) == 0)
634 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
636 /* Remove platform extension if it's there. Compat hack. */
637 const char *ext = g_LibSys.GetFileExtension(file);
638 if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
640 char path2[PLATFORM_MAX_PATH];
641 UTIL_Format(path2, sizeof(path2), "%s", file);
642 path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
643 return LoadExtension(path2, error, maxlength);
646 IExtension *pAlready;
647 if ((pAlready=FindExtensionByFile(file)) != NULL)
652 CExtension *pExt = new CLocalExtension(file);
654 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
661 /* :TODO: do QueryRunning check if the map is loaded */
663 m_Libs.push_back(pExt);
668 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
670 CExtension *pExt = (CExtension *)pRequester;
671 CExtension *pOwner = (CExtension *)pInfo->owner;
673 pExt->AddDependency(pInfo);
675 IExtensionInterface *pAPI = pExt->GetAPI();
676 if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
678 pOwner->AddChildDependent(pExt, pInfo->iface);
682 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
684 CExtension *pExt = (CExtension *)ext;
685 CExtension *pOwner = GetExtensionFromIdent(other);
689 info.iface = (SMInterface *)iface;
692 pExt->AddDependency(&info);
693 pOwner->AddChildDependent(pExt, (SMInterface *)iface);
696 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
698 CExtension *pExt = (CExtension *)pOwner;
700 pExt->AddInterface(pInterface);
703 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
705 CExtension *pExt = (CExtension *)pParent;
707 pExt->AddPlugin(pPlugin);
710 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
712 List<CExtension *>::iterator iter;
714 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
716 (*iter)->DropRefsTo((CPlugin *)plugin);
720 CExtension *CExtensionManager::FindByOrder(unsigned int num)
722 if (num < 1 || num > m_Libs.size())
727 List<CExtension *>::iterator iter = m_Libs.begin();
729 while (iter != m_Libs.end())
741 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
748 CExtension *pExt = (CExtension *)_pExt;
750 if (m_Libs.find(pExt) == m_Libs.end())
755 /* First remove us from internal lists */
756 g_ShareSys.RemoveInterfaces(_pExt);
759 List<CExtension *> UnloadQueue;
761 /* Handle dependencies */
762 if (pExt->IsLoaded())
764 /* Unload any dependent plugins */
765 List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
766 while (p_iter != pExt->m_Dependents.end())
768 /* We have to manually unlink ourselves here, since we're no longer being managed */
769 g_PluginSys.UnloadPlugin((*p_iter));
770 p_iter = pExt->m_Dependents.erase(p_iter);
773 List<String>::iterator s_iter;
774 for (s_iter = pExt->m_Libraries.begin();
775 s_iter != pExt->m_Libraries.end();
778 g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
781 /* Notify and/or unload all dependencies */
782 List<CExtension *>::iterator c_iter;
784 IExtensionInterface *pAPI;
785 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
788 if ((pAPI=pDep->GetAPI()) == NULL)
796 /* Now, get its dependency list */
797 bool dropped = false;
798 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
799 while (i_iter != pDep->m_Deps.end())
801 if ((*i_iter).owner == _pExt)
803 if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
808 UnloadQueue.push_back(pDep);
811 pAPI->NotifyInterfaceDrop((*i_iter).iface);
812 i_iter = pDep->m_Deps.erase(i_iter);
819 /* Flush out any back references to this plugin */
820 i_iter = pDep->m_ChildDeps.begin();
821 while (i_iter != pDep->m_ChildDeps.end())
823 if ((*i_iter).owner == pExt)
825 i_iter = pDep->m_ChildDeps.erase(i_iter);
834 /* Unbind our natives from Core */
835 pExt->DropEverything();
838 IdentityToken_t *pIdentity;
839 if ((pIdentity = pExt->GetIdentity()) != NULL)
841 SMGlobalClass *glob = SMGlobalClass::head;
844 glob->OnSourceModIdentityDropped(pIdentity);
845 glob = glob->m_pGlobalClassNext;
849 /* Tell it to unload */
850 if (pExt->IsLoaded())
852 IExtensionInterface *pAPI = pExt->GetAPI();
853 pAPI->OnExtensionUnload();
859 List<CExtension *>::iterator iter;
860 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
862 /* NOTE: This is safe because the unload function backs out of anything not present */
863 UnloadExtension((*iter));
869 void CExtensionManager::MarkAllLoaded()
871 List<CExtension *>::iterator iter;
874 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
877 if (!pExt->IsLoaded())
881 if (pExt->m_bFullyLoaded)
885 pExt->MarkAllLoaded();
889 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
891 /* This function doesn't really need to do anything now. We make sure the
892 * other extension is loaded, but handling of dependencies is really done
893 * by the interface fetcher.
895 if (required || autoload)
897 LoadAutoExtension(file);
901 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
903 int argcount = command.ArgC();
906 const char *cmd = command.Arg(2);
907 if (strcmp(cmd, "list") == 0)
909 List<CExtension *>::iterator iter;
911 unsigned int num = 1;
912 switch (m_Libs.size())
916 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
921 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
926 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
930 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
933 if (pExt->IsLoaded())
936 if (!pExt->IsRunning(error, sizeof(error)))
938 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
942 IExtensionInterface *pAPI = pExt->GetAPI();
943 const char *name = pAPI->GetExtensionName();
944 const char *version = pAPI->GetExtensionVerString();
945 const char *descr = pAPI->GetExtensionDescription();
946 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
949 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
954 else if (strcmp(cmd, "load") == 0)
958 g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
962 const char *filename = command.Arg(3);
963 char path[PLATFORM_MAX_PATH];
966 UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
967 !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
969 if (FindExtensionByFile(path) != NULL)
971 g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
975 if (LoadExtension(path, error, sizeof(error)))
977 g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
980 g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
985 else if (strcmp(cmd, "info") == 0)
989 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
993 const char *sId = command.Arg(3);
994 unsigned int id = atoi(sId);
997 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
1001 if (m_Libs.size() == 0)
1003 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
1007 if (id > m_Libs.size())
1009 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1013 List<CExtension *>::iterator iter = m_Libs.begin();
1014 CExtension *pExt = NULL;
1015 while (iter != m_Libs.end())
1024 /* This should never happen */
1027 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
1031 if (!pExt->IsLoaded())
1033 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1034 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1039 if (!pExt->IsRunning(error, sizeof(error)))
1041 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1042 g_RootMenu.ConsolePrint(" Loaded: Yes");
1043 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
1047 IExtensionInterface *pAPI = pExt->GetAPI();
1048 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
1049 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1050 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1051 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1052 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1054 if (pExt->IsExternal())
1056 g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1058 else if (pAPI->IsMetamodExtension())
1060 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1064 g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
1070 else if (strcmp(cmd, "unload") == 0)
1074 g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
1078 const char *arg = command.Arg(3);
1079 unsigned int num = atoi(arg);
1080 CExtension *pExt = FindByOrder(num);
1084 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1088 if (argcount > 4 && pExt->unload_code)
1090 const char *unload = command.Arg(4);
1091 if (pExt->unload_code == (unsigned)atoi(unload))
1093 char filename[PLATFORM_MAX_PATH];
1094 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1095 UnloadExtension(pExt);
1096 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1100 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1105 if (!pExt->IsLoaded()
1106 || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1108 char filename[PLATFORM_MAX_PATH];
1109 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1110 UnloadExtension(pExt);
1111 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1116 List<CPlugin *> plugins;
1117 if (pExt->m_ChildDeps.size())
1119 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1120 List<CExtension *>::iterator iter;
1122 /* Get list of all extensions */
1123 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1125 List<IfaceInfo>::iterator i_iter;
1127 if (!pOther->IsLoaded() || pOther == pExt)
1131 /* Get their dependencies */
1132 for (i_iter=pOther->m_Deps.begin();
1133 i_iter!=pOther->m_Deps.end();
1136 /* Is this dependency to us? */
1137 if ((*i_iter).owner != pExt)
1141 /* Will our dependent care? */
1142 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1144 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
1145 /* Add to plugin unload list */
1146 List<CPlugin *>::iterator p_iter;
1147 for (p_iter=pOther->m_Dependents.begin();
1148 p_iter!=pOther->m_Dependents.end();
1151 if (plugins.find((*p_iter)) == plugins.end())
1153 plugins.push_back((*p_iter));
1160 if (pExt->m_Dependents.size())
1162 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1163 List<CPlugin *>::iterator iter;
1165 for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1168 if (plugins.find(pPlugin) == plugins.end())
1170 plugins.push_back(pPlugin);
1173 for (iter = plugins.begin(); iter != plugins.end(); iter++)
1176 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
1179 srand(static_cast<int>(time(NULL)));
1180 pExt->unload_code = (rand() % 877) + 123; //123 to 999
1181 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1182 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1187 else if (strcmp(cmd, "reload") == 0)
1191 g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
1195 const char *arg = command.Arg(3);
1196 unsigned int num = atoi(arg);
1197 CExtension *pExt = FindByOrder(num);
1201 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
1205 if (pExt->IsLoaded())
1207 char filename[PLATFORM_MAX_PATH];
1210 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1212 if (pExt->Reload(error, sizeof(error)))
1214 g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1218 g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1225 g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1233 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
1234 g_RootMenu.DrawGenericOption("info", "Extra extension information");
1235 g_RootMenu.DrawGenericOption("list", "List extensions");
1236 g_RootMenu.DrawGenericOption("load", "Load an extension");
1237 g_RootMenu.DrawGenericOption("reload", "Reload an extension");
1238 g_RootMenu.DrawGenericOption("unload", "Unload an extension");
1241 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1243 if (ptr->type == g_ExtType)
1245 return (CExtension *)(ptr->ptr);
1251 CExtensionManager::CExtensionManager()
1255 CExtensionManager::~CExtensionManager()
1259 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1261 CExtension *pExt = (CExtension *)pSource;
1262 pExt->AddLibrary(library);
1263 g_PluginSys.OnLibraryAction(library, false, false);
1266 bool CExtensionManager::LibraryExists(const char *library)
1270 for (List<CExtension *>::iterator iter = m_Libs.begin();
1271 iter != m_Libs.end();
1275 for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1276 s_iter != pExt->m_Libraries.end();
1279 if ((*s_iter).compare(library) == 0)
1289 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1290 const char *filepath,
1291 const char *filename,
1295 IExtension *pAlready;
1296 if ((pAlready=FindExtensionByFile(filename)) != NULL)
1301 CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1303 if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1310 m_Libs.push_back(pExt);
1315 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1317 IExtensionInterface *pAPI;
1318 List<CExtension *>::iterator iter;
1320 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1322 if ((pAPI = (*iter)->GetAPI()) == NULL)
1326 if (pAPI->GetExtensionVersion() > 3)
1328 pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1333 bool CLocalExtension::IsSameFile(const char *file)
1335 /* Only care about the shortened name. */
1336 return strcmp(file, m_File.c_str()) == 0;
1339 bool CRemoteExtension::IsSameFile(const char *file)
1341 /* :TODO: this could be better, but no one uses this API anyway. */
1342 return strcmp(file, m_Path.c_str()) == 0;