More consistency of some sort...
Oh god, what has possessed me to do this?
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "ExtensionSys.h"
17 #include "LibrarySys.h"
20 #include "sourcemm_api.h"
21 #include "PluginSys.h"
22 #include "sm_srvcmds.h"
23 #include "sm_stringutil.h"
25 CExtensionManager g_Extensions;
26 IdentityType_t g_ExtType;
28 CExtension::CExtension(const char *filename, char *error, size_t err_max)
30 m_File.assign(filename);
35 m_FullyLoaded = false;
37 char path[PLATFORM_MAX_PATH];
38 g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
40 m_pLib = g_LibSys.OpenLibrary(path, error, err_max);
47 typedef IExtensionInterface *(*GETAPI)();
48 GETAPI pfnGetAPI = NULL;
50 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
52 m_pLib->CloseLibrary();
54 snprintf(error, err_max, "Unable to find extension entry point");
59 if (!m_pAPI || m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
61 m_pLib->CloseLibrary();
63 snprintf(error, err_max, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
67 if (m_pAPI->IsMetamodExtension())
70 m_PlId = g_pMMPlugins->Load(path, g_PLID, already, error, err_max);
73 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType);
75 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, err_max, !g_SourceMod.IsMapLoading()))
77 if (m_pAPI->IsMetamodExtension())
82 g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
87 m_pLib->CloseLibrary();
89 g_ShareSys.DestroyIdentity(m_pIdentToken);
93 /* Check if we're past load time */
94 if (!g_SourceMod.IsMapLoading())
96 m_pAPI->OnExtensionsAllLoaded();
101 CExtension::~CExtension()
105 m_pAPI->OnExtensionUnload();
108 g_pMMPlugins->Unload(m_PlId, true, NULL, 0);
114 g_ShareSys.DestroyIdentity(m_pIdentToken);
119 m_pLib->CloseLibrary();
123 void CExtension::MarkAllLoaded()
125 assert(m_FullyLoaded == false);
128 m_FullyLoaded = true;
129 m_pAPI->OnExtensionsAllLoaded();
133 void CExtension::AddPlugin(IPlugin *pPlugin)
135 m_Plugins.push_back(pPlugin);
138 void CExtension::RemovePlugin(IPlugin *pPlugin)
140 m_Plugins.remove(pPlugin);
143 void CExtension::SetError(const char *error)
145 m_Error.assign(error);
148 IExtensionInterface *CExtension::GetAPI()
153 const char *CExtension::GetFilename()
155 return m_File.c_str();
158 IdentityToken_t *CExtension::GetIdentity()
160 return m_pIdentToken;
163 bool CExtension::IsLoaded()
165 return (m_pLib != NULL);
168 void CExtension::AddDependency(IfaceInfo *pInfo)
170 m_Deps.push_back(*pInfo);
173 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
175 List<IfaceInfo>::iterator iter = m_Deps.begin();
177 if (iter == m_Deps.end())
184 *pOwner = (*iter).owner;
188 *pInterface = (*iter).iface;
191 List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
193 return (ITERATOR *)pIter;
196 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
198 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
199 List<IfaceInfo>::iterator _iter;
201 if (_iter == m_Deps.end())
210 *pOwner = (*_iter).owner;
214 *pInterface = (*_iter).iface;
219 if (_iter == m_Deps.end())
227 void CExtension::FreeDependencyIterator(ITERATOR *iter)
229 List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
234 void CExtension::AddInterface(SMInterface *pInterface)
236 m_Interfaces.push_back(pInterface);
239 bool CExtension::IsRunning(char *error, size_t maxlength)
245 snprintf(error, maxlength, "%s", m_Error.c_str());
250 return m_pAPI->QueryRunning(error, maxlength);
253 /*********************
254 * EXTENSION MANAGER *
255 *********************/
257 void CExtensionManager::OnSourceModAllInitialized()
259 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
260 g_PluginSys.AddPluginsListener(this);
261 g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
262 g_ShareSys.AddInterface(NULL, this);
265 void CExtensionManager::OnSourceModShutdown()
267 g_RootMenu.RemoveRootConsoleCommand("exts", this);
268 g_ShareSys.DestroyIdentType(g_ExtType);
271 void CExtensionManager::TryAutoload()
273 char path[PLATFORM_MAX_PATH];
275 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
277 IDirectory *pDir = g_LibSys.OpenDirectory(path);
285 while (pDir->MoreFiles())
287 if (pDir->IsEntryDirectory())
293 lfile = pDir->GetEntryName();
295 if (len <= 9) /* size of ".autoload" */
301 if (strcmp(&lfile[len - 9], ".autoload") != 0)
307 char file[PLATFORM_MAX_PATH];
308 len = UTIL_Format(file, sizeof(file), "%s", lfile);
309 strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
311 LoadAutoExtension(file);
317 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
319 if (!strstr(path, "." PLATFORM_LIB_EXT))
321 char newpath[PLATFORM_MAX_PATH];
322 snprintf(newpath, PLATFORM_MAX_PATH, "%s.%s", path, PLATFORM_LIB_EXT);
323 return LoadAutoExtension(newpath);
326 IExtension *pAlready;
327 if ((pAlready=FindExtensionByFile(path)) != NULL)
333 CExtension *p = new CExtension(path, error, sizeof(error));
337 g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
346 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
348 List<CExtension *>::iterator iter;
351 if (!strstr(file, "." PLATFORM_LIB_EXT))
353 char path[PLATFORM_MAX_PATH];
354 snprintf(path, PLATFORM_MAX_PATH, "%s.%s", file, PLATFORM_LIB_EXT);
355 return FindExtensionByFile(path);
358 /* Make sure the file direction is right */
359 char path[PLATFORM_MAX_PATH];
360 g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
362 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
365 if (strcmp(pExt->GetFilename(), path) == 0)
374 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
376 List<CExtension *>::iterator iter;
378 IExtensionInterface *pAPI;
381 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
384 if (!pExt->IsLoaded())
388 if ((pAPI = pExt->GetAPI()) == NULL)
392 name = pAPI->GetExtensionName();
397 if (strcmp(name, ext) == 0)
406 IExtension *CExtensionManager::LoadExtension(const char *file, ExtensionLifetime lifetime, char *error, size_t err_max)
408 IExtension *pAlready;
409 if ((pAlready=FindExtensionByFile(file)) != NULL)
414 CExtension *pExt = new CExtension(file, error, err_max);
416 /* :NOTE: lifetime is currently ignored */
418 if (!pExt->IsLoaded())
424 /* :TODO: do QueryRunning check if the map is loaded */
426 m_Libs.push_back(pExt);
431 void CExtensionManager::BindDependency(IExtension *pOwner, IfaceInfo *pInfo)
433 CExtension *pExt = (CExtension *)pOwner;
435 pExt->AddDependency(pInfo);
438 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
440 CExtension *pExt = (CExtension *)pOwner;
442 pExt->AddInterface(pInterface);
445 void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
447 CExtension *pExt = (CExtension *)pParent;
449 pExt->AddPlugin(pPlugin);
452 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
454 List<CExtension *>::iterator iter;
456 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
458 (*iter)->RemovePlugin(plugin);
462 CExtension *CExtensionManager::FindByOrder(unsigned int num)
464 if (num < 1 || num > m_Libs.size())
469 List<CExtension *>::iterator iter = m_Libs.begin();
471 while (iter != m_Libs.end())
483 void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
485 List<CExtension *>::iterator iter;
487 IPluginContext *pContext = pPlugin->GetBaseContext();
488 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
491 if (!pExt->IsLoaded())
495 if (!pExt->m_Natives.size())
500 List<const sp_nativeinfo_t *>::iterator n_iter;
501 const sp_nativeinfo_t *natives;
502 for (n_iter = pExt->m_Natives.begin();
503 n_iter != pExt->m_Natives.end();
508 while (natives[i].func != NULL && natives[i].name != NULL)
510 if (pContext->BindNative(&natives[i++]) == SP_ERROR_NONE)
516 if (set && (pExt->m_Plugins.find(pPlugin) == pExt->m_Plugins.end()))
518 /* For now, mark this plugin as a requirement. Later we'll introduce native filtering. */
519 pExt->m_Plugins.push_back(pPlugin);
524 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
531 CExtension *pExt = (CExtension *)_pExt;
533 if (m_Libs.find(pExt) == m_Libs.end())
538 /* First remove us from internal lists */
539 g_ShareSys.RemoveInterfaces(_pExt);
542 List<CExtension *> UnloadQueue;
544 /* Handle dependencies */
545 if (pExt->IsLoaded())
547 /* Unload any dependent plugins */
548 List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
549 while (p_iter != pExt->m_Plugins.end())
551 /* We have to manually unlink ourselves here, since we're no longer being managed */
552 g_PluginSys.UnloadPlugin((*p_iter));
553 p_iter = pExt->m_Plugins.erase(p_iter);
556 /* Notify and/or unload all dependencies */
557 List<CExtension *>::iterator c_iter;
559 IExtensionInterface *pAPI;
560 for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
563 if ((pAPI=pDep->GetAPI()) == NULL)
571 /* Now, get its dependency list */
572 bool dropped = false;
573 List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
574 while (i_iter != pDep->m_Deps.end())
576 if ((*i_iter).owner == _pExt)
578 if (!dropped && !pAPI->QueryInterfaceDrop((*i_iter).iface))
582 pAPI->NotifyInterfaceDrop((*i_iter).iface);
583 i_iter = pDep->m_Deps.erase(i_iter);
591 List<CExtension *>::iterator iter;
592 for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
594 /* NOTE: This is safe because the unload function backs out of anything not present */
595 UnloadExtension((*iter));
601 void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
603 CExtension *pExt = (CExtension *)pOwner;
605 pExt->m_Natives.push_back(natives);
608 void CExtensionManager::MarkAllLoaded()
610 List<CExtension *>::iterator iter;
613 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
616 if (!pExt->IsLoaded())
620 if (pExt->m_FullyLoaded)
624 pExt->MarkAllLoaded();
628 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
630 /* :TODO: implement */
634 void CExtensionManager::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
638 const char *cmd = g_RootMenu.GetArgument(2);
639 if (strcmp(cmd, "list") == 0)
641 List<CExtension *>::iterator iter;
643 unsigned int num = 1;
644 switch (m_Libs.size())
648 g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
653 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
658 g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
662 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
665 if (pExt->IsLoaded())
668 if (!pExt->IsRunning(error, sizeof(error)))
670 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
672 IExtensionInterface *pAPI = pExt->GetAPI();
673 const char *name = pAPI->GetExtensionName();
674 const char *version = pAPI->GetExtensionVerString();
675 const char *descr = pAPI->GetExtensionDescription();
676 g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
679 g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
683 } else if (strcmp(cmd, "info") == 0) {
686 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
690 const char *sId = g_RootMenu.GetArgument(3);
691 unsigned int id = atoi(sId);
694 g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
698 if (m_Libs.size() == 0)
700 g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
704 if (id > m_Libs.size())
706 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
710 List<CExtension *>::iterator iter = m_Libs.begin();
711 CExtension *pExt = NULL;
712 while (iter != m_Libs.end())
721 /* This should never happen */
724 g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
728 if (!pExt->IsLoaded())
730 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
731 g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
734 if (!pExt->IsRunning(error, sizeof(error)))
736 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
737 g_RootMenu.ConsolePrint(" Loaded: Yes");
738 g_RootMenu.ConsolePrint(" Running: No (%s)", error);
740 IExtensionInterface *pAPI = pExt->GetAPI();
741 g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
742 g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
743 g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
744 g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
745 g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
746 g_RootMenu.ConsolePrint(" Metamod enabled: %s", pAPI->IsMetamodExtension() ? "yes" : "no");
750 } else if (strcmp(cmd, "unload") == 0) {
753 g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
757 const char *arg = g_RootMenu.GetArgument(3);
758 unsigned int num = atoi(arg);
759 CExtension *pExt = FindByOrder(num);
763 g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
767 if (argcount > 4 && pExt->unload_code)
769 const char *unload = g_RootMenu.GetArgument(4);
770 if (pExt->unload_code == (unsigned)atoi(unload))
772 char filename[PLATFORM_MAX_PATH];
773 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
774 UnloadExtension(pExt);
775 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
777 g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
782 if (!pExt->IsLoaded()
783 || (!pExt->m_Deps.size() && !pExt->m_Plugins.size()))
785 char filename[PLATFORM_MAX_PATH];
786 snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
787 UnloadExtension(pExt);
788 g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
791 List<IPlugin *> plugins;
792 if (pExt->m_Deps.size())
794 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
795 List<CExtension *>::iterator iter;
797 /* Get list of all extensions */
798 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
800 List<IfaceInfo>::iterator i_iter;
802 if (!pOther->IsLoaded() || pOther == pExt)
806 /* Get their dependencies */
807 for (i_iter=pOther->m_Deps.begin();
808 i_iter!=pOther->m_Deps.end();
811 /* Is this dependency to us? */
812 if ((*i_iter).owner != pExt)
816 /* Will our dependent care? */
817 if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
819 g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
820 /* Add to plugin unload list */
821 List<IPlugin *>::iterator p_iter;
822 for (p_iter=pOther->m_Plugins.begin();
823 p_iter!=pOther->m_Plugins.end();
826 if (plugins.find((*p_iter)) == plugins.end())
828 plugins.push_back((*p_iter));
835 if (pExt->m_Plugins.size())
837 g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
838 List<IPlugin *>::iterator iter;
840 for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
843 if (plugins.find(pPlugin) == plugins.end())
845 plugins.push_back(pPlugin);
848 for (iter = plugins.begin(); iter != plugins.end(); iter++)
851 g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
854 srand(static_cast<int>(time(NULL)));
855 pExt->unload_code = (rand() % 877) + 123; //123 to 999
856 g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
857 g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
864 g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
865 g_RootMenu.DrawGenericOption("info", "Extra extension information");
866 g_RootMenu.DrawGenericOption("list", "List extensions");
867 g_RootMenu.DrawGenericOption("unload", "Unload an extension");