Made this more consistent or something...
2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "ExtensionSys.h"
16 #include "LibrarySys.h"
19 #include "sourcemm_api.h"
20 #include "PluginSys.h"
21 #include "sm_srvcmds.h"
22 #include "sm_stringutil.h"
24 CExtensionManager g_Extensions;
25 IdentityType_t g_ExtType;
27 CExtension::CExtension(const char *filename, char *error, size_t err_max)
29 m_File.assign(filename);
34 m_FullyLoaded = false;
36 char path[PLATFORM_MAX_PATH];
37 g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
39 m_pLib = g_LibSys.OpenLibrary(path, error, err_max);
46 typedef IExtensionInterface *(*GETAPI)();
47 GETAPI pfnGetAPI = NULL;
49 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
51 m_pLib->CloseLibrary();
53 snprintf(error, err_max, "Unable to find extension entry point");
58 if (!m_pAPI || m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
60 m_pLib->CloseLibrary();
62 snprintf(error, err_max, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
66 if (m_pAPI->IsMetamodExtension())
69 m_PlId = g_pMMPlugins->Load(path, g_PLID, already, error, err_max);
72 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType);
74 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, err_max, !g_SourceMod.IsMapLoading()))
76 if (m_pAPI->IsMetamodExtension())
81 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
86 m_pLib->CloseLibrary();
88 g_ShareSys.DestroyIdentity(m_pIdentToken);
92 /* Check if we're past load time */
93 if (!g_SourceMod.IsMapLoading())
95 m_pAPI->OnExtensionsAllLoaded();
100 CExtension::~CExtension()
104 m_pAPI->OnExtensionUnload();
107 g_pMMPlugins->Unload(m_PlId, true, NULL, 0);
113 g_ShareSys.DestroyIdentity(m_pIdentToken);
118 m_pLib->CloseLibrary();
122 void CExtension::MarkAllLoaded()
124 assert(m_FullyLoaded == false);
127 m_FullyLoaded = true;
128 m_pAPI->OnExtensionsAllLoaded();
132 void CExtension::AddPlugin(IPlugin *pPlugin)
134 m_Plugins.push_back(pPlugin);
137 void CExtension::RemovePlugin(IPlugin *pPlugin)
139 m_Plugins.remove(pPlugin);
142 void CExtension::SetError(const char *error)
144 m_Error.assign(error);
147 IExtensionInterface *CExtension::GetAPI()
152 const char *CExtension::GetFilename()
154 return m_File.c_str();
157 IdentityToken_t *CExtension::GetIdentity()
159 return m_pIdentToken;
162 bool CExtension::IsLoaded()
164 return (m_pLib != NULL);
167 void CExtension::AddDependency(IfaceInfo *pInfo)
169 m_Deps.push_back(*pInfo);
172 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
174 List<IfaceInfo>::iterator iter = m_Deps.begin();
176 if (iter == m_Deps.end())
183 *pOwner = (*iter).owner;
187 *pInterface = (*iter).iface;
190 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
192 return (ITERATOR *)pIter;
195 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
197 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
198 List<IfaceInfo>::iterator _iter;
200 if (_iter == m_Deps.end())
209 *pOwner = (*_iter).owner;
213 *pInterface = (*_iter).iface;
218 if (_iter == m_Deps.end())
226 void CExtension::FreeDependencyIterator(ITERATOR *iter)
228 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
233 void CExtension::AddInterface(SMInterface *pInterface)
235 m_Interfaces.push_back(pInterface);
238 bool CExtension::IsRunning(char *error, size_t maxlength)
244 snprintf(error, maxlength, "%s", m_Error.c_str());
249 return m_pAPI->QueryRunning(error, maxlength);
252 /*********************
253 * EXTENSION MANAGER *
254 *********************/
256 void CExtensionManager::OnSourceModAllInitialized()
258 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
259 g_PluginSys.AddPluginsListener(this);
260 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
261 g_ShareSys.AddInterface(NULL, this);
264 void CExtensionManager::OnSourceModShutdown()
266 g_RootMenu.RemoveRootConsoleCommand("exts", this);
267 g_ShareSys.DestroyIdentType(g_ExtType);
270 void CExtensionManager::TryAutoload()
272 char path[PLATFORM_MAX_PATH];
274 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
276 IDirectory *pDir = g_LibSys.OpenDirectory(path);
284 while (pDir->MoreFiles())
286 if (pDir->IsEntryDirectory())
292 lfile = pDir->GetEntryName();
294 if (len <= 9) /* size of ".autoload" */
300 if (strcmp(&lfile[len - 9], ".autoload") != 0)
306 char file[PLATFORM_MAX_PATH];
307 len = UTIL_Format(file, sizeof(file), "%s", lfile);
308 strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
310 LoadAutoExtension(file);
316 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
318 if (!strstr(path, "." PLATFORM_LIB_EXT))
320 char newpath[PLATFORM_MAX_PATH];
321 snprintf(newpath, PLATFORM_MAX_PATH, "%s.%s", path, PLATFORM_LIB_EXT);
322 return LoadAutoExtension(newpath);
325 IExtension *pAlready;
326 if ((pAlready=FindExtensionByFile(path)) != NULL)
332 CExtension *p = new CExtension(path, error, sizeof(error));
336 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
345 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
347 List<CExtension *>::iterator iter;
350 if (!strstr(file, "." PLATFORM_LIB_EXT))
352 char path[PLATFORM_MAX_PATH];
353 snprintf(path, PLATFORM_MAX_PATH, "%s.%s", file, PLATFORM_LIB_EXT);
354 return FindExtensionByFile(path);
357 /* Make sure the file direction is right */
358 char path[PLATFORM_MAX_PATH];
359 g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
361 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
364 if (strcmp(pExt->GetFilename(), path) == 0)
373 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
375 List<CExtension *>::iterator iter;
377 IExtensionInterface *pAPI;
380 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
383 if (!pExt->IsLoaded())
387 if ((pAPI = pExt->GetAPI()) == NULL)
391 name = pAPI->GetExtensionName();
396 if (strcmp(name, ext) == 0)
405 IExtension *CExtensionManager::LoadExtension(const char *file, ExtensionLifetime lifetime, char *error, size_t err_max)
407 IExtension *pAlready;
408 if ((pAlready=FindExtensionByFile(file)) != NULL)
413 CExtension *pExt = new CExtension(file, error, err_max);
415 /* :NOTE: lifetime is currently ignored */
417 if (!pExt->IsLoaded())
423 /* :TODO: do QueryRunning check if the map is loaded */
425 m_Libs.push_back(pExt);
430 void CExtensionManager::BindDependency(IExtension *pOwner, IfaceInfo *pInfo)
432 CExtension *pExt = (CExtension *)pOwner;
434 pExt->AddDependency(pInfo);
437 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
439 CExtension *pExt = (CExtension *)pOwner;
441 pExt->AddInterface(pInterface);
444 void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
446 CExtension *pExt = (CExtension *)pParent;
448 pExt->AddPlugin(pPlugin);
451 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
453 List<CExtension *>::iterator iter;
455 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
457 (*iter)->RemovePlugin(plugin);
461 CExtension *CExtensionManager::FindByOrder(unsigned int num)
463 if (num < 1 || num > m_Libs.size())
468 List<CExtension *>::iterator iter = m_Libs.begin();
470 while (iter != m_Libs.end())
482 void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
484 List<CExtension *>::iterator iter;
486 IPluginContext *pContext = pPlugin->GetBaseContext();
487 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
490 if (!pExt->IsLoaded())
494 if (!pExt->m_Natives.size())
499 List<const sp_nativeinfo_t *>::iterator n_iter;
500 const sp_nativeinfo_t *natives;
501 for (n_iter = pExt->m_Natives.begin();
502 n_iter != pExt->m_Natives.end();
507 while (natives[i].func != NULL && natives[i].name != NULL)
509 if (pContext->BindNative(&natives[i++]) == SP_ERROR_NONE)
515 if (set && (pExt->m_Plugins.find(pPlugin) == pExt->m_Plugins.end()))
517 /* For now, mark this plugin as a requirement. Later we'll introduce native filtering. */
518 pExt->m_Plugins.push_back(pPlugin);
523 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
530 CExtension *pExt = (CExtension *)_pExt;
532 if (m_Libs.find(pExt) == m_Libs.end())
537 /* First remove us from internal lists */
538 g_ShareSys.RemoveInterfaces(_pExt);
541 List<CExtension *> UnloadQueue;
543 /* Handle dependencies */
544 if (pExt->IsLoaded())
546 /* Unload any dependent plugins */
547 List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
548 while (p_iter != pExt->m_Plugins.end())
550 /* We have to manually unlink ourselves here, since we're no longer being managed */
551 g_PluginSys.UnloadPlugin((*p_iter));
552 p_iter = pExt->m_Plugins.erase(p_iter);
555 /* Notify and/or unload all dependencies */
556 List<CExtension *>::iterator c_iter;
558 IExtensionInterface *pAPI;
559 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
562 if ((pAPI=pDep->GetAPI()) == NULL)
570 /* Now, get its dependency list */
571 bool dropped = false;
572 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
573 while (i_iter != pDep->m_Deps.end())
575 if ((*i_iter).owner == _pExt)
577 if (!dropped && !pAPI->QueryInterfaceDrop((*i_iter).iface))
581 pAPI->NotifyInterfaceDrop((*i_iter).iface);
582 i_iter = pDep->m_Deps.erase(i_iter);
590 List<CExtension *>::iterator iter;
591 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
593 /* NOTE: This is safe because the unload function backs out of anything not present */
594 UnloadExtension((*iter));
600 void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
602 CExtension *pExt = (CExtension *)pOwner;
604 pExt->m_Natives.push_back(natives);
607 void CExtensionManager::MarkAllLoaded()
609 List<CExtension *>::iterator iter;
612 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
615 if (!pExt->IsLoaded())
619 if (pExt->m_FullyLoaded)
623 pExt->MarkAllLoaded();
627 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
629 /* :TODO: implement */
633 void CExtensionManager::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
637 const char *cmd = g_RootMenu.GetArgument(2);
638 if (strcmp(cmd, "list") == 0)
640 List<CExtension *>::iterator iter;
642 unsigned int num = 1;
643 switch (m_Libs.size())
647 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
652 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
657 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
661 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
664 if (pExt->IsLoaded())
667 if (!pExt->IsRunning(error, sizeof(error)))
669 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
671 IExtensionInterface *pAPI = pExt->GetAPI();
672 const char *name = pAPI->GetExtensionName();
673 const char *version = pAPI->GetExtensionVerString();
674 const char *descr = pAPI->GetExtensionDescription();
675 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
678 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
682 } else if (strcmp(cmd, "info") == 0) {
685 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
689 const char *sId = g_RootMenu.GetArgument(3);
690 unsigned int id = atoi(sId);
693 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
697 if (m_Libs.size() == 0)
699 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
703 if (id > m_Libs.size())
705 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
709 List<CExtension *>::iterator iter = m_Libs.begin();
710 CExtension *pExt = NULL;
711 while (iter != m_Libs.end())
720 /* This should never happen */
723 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
727 if (!pExt->IsLoaded())
729 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
730 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
733 if (!pExt->IsRunning(error, sizeof(error)))
735 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
736 g_RootMenu.ConsolePrint(" Loaded: Yes");
737 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
739 IExtensionInterface *pAPI = pExt->GetAPI();
740 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
741 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
742 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
743 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
744 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
745 g_RootMenu.ConsolePrint(" Metamod enabled: %s", pAPI->IsMetamodExtension() ? "yes" : "no");
749 } else if (strcmp(cmd, "unload") == 0) {
752 g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
756 const char *arg = g_RootMenu.GetArgument(3);
757 unsigned int num = atoi(arg);
758 CExtension *pExt = FindByOrder(num);
762 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
766 if (argcount > 4 && pExt->unload_code)
768 const char *unload = g_RootMenu.GetArgument(4);
769 if (pExt->unload_code == (unsigned)atoi(unload))
771 char filename[PLATFORM_MAX_PATH];
772 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
773 UnloadExtension(pExt);
774 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
776 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
781 if (!pExt->IsLoaded()
782 || (!pExt->m_Deps.size() && !pExt->m_Plugins.size()))
784 char filename[PLATFORM_MAX_PATH];
785 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
786 UnloadExtension(pExt);
787 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
790 List<IPlugin *> plugins;
791 if (pExt->m_Deps.size())
793 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
794 List<CExtension *>::iterator iter;
796 /* Get list of all extensions */
797 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
799 List<IfaceInfo>::iterator i_iter;
801 if (!pOther->IsLoaded() || pOther == pExt)
805 /* Get their dependencies */
806 for (i_iter=pOther->m_Deps.begin();
807 i_iter!=pOther->m_Deps.end();
810 /* Is this dependency to us? */
811 if ((*i_iter).owner != pExt)
815 /* Will our dependent care? */
816 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
818 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
819 /* Add to plugin unload list */
820 List<IPlugin *>::iterator p_iter;
821 for (p_iter=pOther->m_Plugins.begin();
822 p_iter!=pOther->m_Plugins.end();
825 if (plugins.find((*p_iter)) == plugins.end())
827 plugins.push_back((*p_iter));
834 if (pExt->m_Plugins.size())
836 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
837 List<IPlugin *>::iterator iter;
839 for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
842 if (plugins.find(pPlugin) == plugins.end())
844 plugins.push_back(pPlugin);
847 for (iter = plugins.begin(); iter != plugins.end(); iter++)
850 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
853 srand(static_cast<int>(time(NULL)));
854 pExt->unload_code = (rand() % 877) + 123; //123 to 999
855 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
856 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
863 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
864 g_RootMenu.DrawGenericOption("info", "Extra extension information");
865 g_RootMenu.DrawGenericOption("list", "List extensions");
866 g_RootMenu.DrawGenericOption("unload", "Unload an extension");