core/systems/ExtensionSys.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 652 d62a08ba6318
child 755 66c9fde7f632
permissions -rw-r--r--
More consistency of some sort...

Oh god, what has possessed me to do this?
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     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
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include <stdlib.h>
    16 #include "ExtensionSys.h"
    17 #include "LibrarySys.h"
    18 #include "ShareSys.h"
    19 #include "Logger.h"
    20 #include "sourcemm_api.h"
    21 #include "PluginSys.h"
    22 #include "sm_srvcmds.h"
    23 #include "sm_stringutil.h"
    24 
    25 CExtensionManager g_Extensions;
    26 IdentityType_t g_ExtType;
    27 
    28 CExtension::CExtension(const char *filename, char *error, size_t err_max)
    29 {
    30 	m_File.assign(filename);
    31 	m_pAPI = NULL;
    32 	m_pIdentToken = NULL;
    33 	m_PlId = 0;
    34 	unload_code = 0;
    35 	m_FullyLoaded = false;
    36 
    37 	char path[PLATFORM_MAX_PATH];
    38 	g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
    39 
    40 	m_pLib = g_LibSys.OpenLibrary(path, error, err_max);
    41 
    42 	if (m_pLib == NULL)
    43 	{
    44 		return;
    45 	}
    46 
    47 	typedef IExtensionInterface *(*GETAPI)();
    48 	GETAPI pfnGetAPI = NULL;
    49 
    50 	if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
    51 	{
    52 		m_pLib->CloseLibrary();
    53 		m_pLib = NULL;
    54 		snprintf(error, err_max, "Unable to find extension entry point");
    55 		return;
    56 	}
    57 
    58 	m_pAPI = pfnGetAPI();
    59 	if (!m_pAPI || m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
    60 	{
    61 		m_pLib->CloseLibrary();
    62 		m_pLib = NULL;
    63 		snprintf(error, err_max, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
    64 		return;
    65 	}
    66 
    67 	if (m_pAPI->IsMetamodExtension())
    68 	{
    69 		bool already;
    70 		m_PlId = g_pMMPlugins->Load(path, g_PLID, already, error, err_max);
    71 	}
    72 
    73 	m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType);
    74 
    75 	if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, err_max, !g_SourceMod.IsMapLoading()))
    76 	{
    77 		if (m_pAPI->IsMetamodExtension())
    78 		{
    79 			if (m_PlId)
    80 			{
    81 				char dummy[255];
    82 				g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
    83 				m_PlId = 0;
    84 			}
    85 		}
    86 		m_pAPI = NULL;
    87 		m_pLib->CloseLibrary();
    88 		m_pLib = NULL;
    89 		g_ShareSys.DestroyIdentity(m_pIdentToken);
    90 		m_pIdentToken = NULL;
    91 		return;
    92 	} else {
    93 		/* Check if we're past load time */
    94 		if (!g_SourceMod.IsMapLoading())
    95 		{
    96 			m_pAPI->OnExtensionsAllLoaded();
    97 		}
    98 	}
    99 }
   100 
   101 CExtension::~CExtension()
   102 {
   103 	if (m_pAPI)
   104 	{
   105 		m_pAPI->OnExtensionUnload();
   106 		if (m_PlId)
   107 		{
   108 			g_pMMPlugins->Unload(m_PlId, true, NULL, 0);
   109 		}
   110 	}
   111 
   112 	if (m_pIdentToken)
   113 	{
   114 		g_ShareSys.DestroyIdentity(m_pIdentToken);
   115 	}
   116 
   117 	if (m_pLib)
   118 	{
   119 		m_pLib->CloseLibrary();
   120 	}
   121 }
   122 
   123 void CExtension::MarkAllLoaded()
   124 {
   125 	assert(m_FullyLoaded == false);
   126 	if (!m_FullyLoaded)
   127 	{
   128 		m_FullyLoaded = true;
   129 		m_pAPI->OnExtensionsAllLoaded();
   130 	}
   131 }
   132 
   133 void CExtension::AddPlugin(IPlugin *pPlugin)
   134 {
   135 	m_Plugins.push_back(pPlugin);
   136 }
   137 
   138 void CExtension::RemovePlugin(IPlugin *pPlugin)
   139 {
   140 	m_Plugins.remove(pPlugin);
   141 }
   142 
   143 void CExtension::SetError(const char *error)
   144 {
   145 	m_Error.assign(error);
   146 }
   147 
   148 IExtensionInterface *CExtension::GetAPI()
   149 {
   150 	return m_pAPI;
   151 }
   152 
   153 const char *CExtension::GetFilename()
   154 {
   155 	return m_File.c_str();
   156 }
   157 
   158 IdentityToken_t *CExtension::GetIdentity()
   159 {
   160 	return m_pIdentToken;
   161 }
   162 
   163 bool CExtension::IsLoaded()
   164 {
   165 	return (m_pLib != NULL);
   166 }
   167 
   168 void CExtension::AddDependency(IfaceInfo *pInfo)
   169 {
   170 	m_Deps.push_back(*pInfo);
   171 }
   172 
   173 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
   174 {
   175 	List<IfaceInfo>::iterator iter = m_Deps.begin();
   176 
   177 	if (iter == m_Deps.end())
   178 	{
   179 		return NULL;
   180 	}
   181 
   182 	if (pOwner)
   183 	{
   184 		*pOwner = (*iter).owner;
   185 	}
   186 	if (pInterface)
   187 	{
   188 		*pInterface = (*iter).iface;
   189 	}
   190 
   191 	List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
   192 
   193 	return (ITERATOR *)pIter;
   194 }
   195 
   196 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
   197 {
   198 	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   199 	List<IfaceInfo>::iterator _iter;
   200 
   201 	if (_iter == m_Deps.end())
   202 	{
   203 		return false;
   204 	}
   205 
   206 	_iter++;
   207 
   208 	if (pOwner)
   209 	{
   210 		*pOwner = (*_iter).owner;
   211 	}
   212 	if (pInterface)
   213 	{
   214 		*pInterface = (*_iter).iface;
   215 	}
   216 
   217 	*pIter = _iter;
   218 
   219 	if (_iter == m_Deps.end())
   220 	{
   221 		return false;
   222 	}
   223 
   224 	return true;
   225 }
   226 
   227 void CExtension::FreeDependencyIterator(ITERATOR *iter)
   228 {
   229 	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   230 
   231 	delete pIter;
   232 }
   233 
   234 void CExtension::AddInterface(SMInterface *pInterface)
   235 {
   236 	m_Interfaces.push_back(pInterface);
   237 }
   238 
   239 bool CExtension::IsRunning(char *error, size_t maxlength)
   240 {
   241 	if (!IsLoaded())
   242 	{
   243 		if (error)
   244 		{
   245 			snprintf(error, maxlength, "%s", m_Error.c_str());
   246 		}
   247 		return false;
   248 	}
   249 
   250 	return m_pAPI->QueryRunning(error, maxlength);
   251 }
   252 
   253 /*********************
   254  * EXTENSION MANAGER *
   255  *********************/
   256 
   257 void CExtensionManager::OnSourceModAllInitialized()
   258 {
   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);
   263 }
   264 
   265 void CExtensionManager::OnSourceModShutdown()
   266 {
   267 	g_RootMenu.RemoveRootConsoleCommand("exts", this);
   268 	g_ShareSys.DestroyIdentType(g_ExtType);
   269 }
   270 
   271 void CExtensionManager::TryAutoload()
   272 {
   273 	char path[PLATFORM_MAX_PATH];
   274 
   275 	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
   276 
   277 	IDirectory *pDir = g_LibSys.OpenDirectory(path);
   278 	if (!pDir)
   279 	{
   280 		return;
   281 	}
   282 
   283 	const char *lfile;
   284 	size_t len;
   285 	while (pDir->MoreFiles())
   286 	{
   287 		if (pDir->IsEntryDirectory())
   288 		{
   289 			pDir->NextEntry();
   290 			continue;
   291 		}
   292 
   293 		lfile = pDir->GetEntryName();
   294 		len = strlen(lfile);
   295 		if (len <= 9) /* size of ".autoload" */
   296 		{
   297 			pDir->NextEntry();
   298 			continue;
   299 		}
   300 
   301 		if (strcmp(&lfile[len - 9], ".autoload") != 0)
   302 		{
   303 			pDir->NextEntry();
   304 			continue;
   305 		}
   306 
   307 		char file[PLATFORM_MAX_PATH];
   308 		len = UTIL_Format(file, sizeof(file), "%s", lfile);
   309 		strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
   310 
   311 		LoadAutoExtension(file);
   312 		
   313 		pDir->NextEntry();
   314 	}
   315 }
   316 
   317 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
   318 {
   319 	if (!strstr(path, "." PLATFORM_LIB_EXT))
   320 	{
   321 		char newpath[PLATFORM_MAX_PATH];
   322 		snprintf(newpath, PLATFORM_MAX_PATH, "%s.%s", path, PLATFORM_LIB_EXT);
   323 		return LoadAutoExtension(newpath);
   324 	}
   325 
   326 	IExtension *pAlready;
   327 	if ((pAlready=FindExtensionByFile(path)) != NULL)
   328 	{
   329 		return pAlready;
   330 	}
   331 
   332 	char error[256];
   333 	CExtension *p = new CExtension(path, error, sizeof(error));
   334 
   335 	if (!p->IsLoaded())
   336 	{
   337 		g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
   338 		p->SetError(error);
   339 	}
   340 
   341 	m_Libs.push_back(p);
   342 
   343 	return p;
   344 }
   345 
   346 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
   347 {
   348 	List<CExtension *>::iterator iter;
   349 	CExtension *pExt;
   350 
   351 	if (!strstr(file, "." PLATFORM_LIB_EXT))
   352 	{
   353 		char path[PLATFORM_MAX_PATH];
   354 		snprintf(path, PLATFORM_MAX_PATH, "%s.%s", file, PLATFORM_LIB_EXT);
   355 		return FindExtensionByFile(path);
   356 	}
   357 
   358 	/* Make sure the file direction is right */
   359 	char path[PLATFORM_MAX_PATH];
   360 	g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
   361 
   362 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   363 	{
   364 		pExt = (*iter);
   365 		if (strcmp(pExt->GetFilename(), path) == 0)
   366 		{
   367 			return pExt;
   368 		}
   369 	}
   370 
   371 	return NULL;
   372 }
   373 
   374 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
   375 {
   376 	List<CExtension *>::iterator iter;
   377 	CExtension *pExt;
   378 	IExtensionInterface *pAPI;
   379 	const char *name;
   380 
   381 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   382 	{
   383 		pExt = (*iter);
   384 		if (!pExt->IsLoaded())
   385 		{
   386 			continue;
   387 		}
   388 		if ((pAPI = pExt->GetAPI()) == NULL)
   389 		{
   390 			continue;
   391 		}
   392 		name = pAPI->GetExtensionName();
   393 		if (!name)
   394 		{
   395 			continue;
   396 		}
   397 		if (strcmp(name, ext) == 0)
   398 		{
   399 			return pExt;
   400 		}
   401 	}
   402 
   403 	return NULL;
   404 }
   405 
   406 IExtension *CExtensionManager::LoadExtension(const char *file, ExtensionLifetime lifetime, char *error, size_t err_max)
   407 {
   408 	IExtension *pAlready;
   409 	if ((pAlready=FindExtensionByFile(file)) != NULL)
   410 	{
   411 		return pAlready;
   412 	}
   413 
   414 	CExtension *pExt = new CExtension(file, error, err_max);
   415 
   416 	/* :NOTE: lifetime is currently ignored */
   417 
   418 	if (!pExt->IsLoaded())
   419 	{
   420 		delete pExt;
   421 		return NULL;
   422 	}
   423 
   424 	/* :TODO: do QueryRunning check if the map is loaded */
   425 
   426 	m_Libs.push_back(pExt);
   427 
   428 	return pExt;
   429 }
   430 
   431 void CExtensionManager::BindDependency(IExtension *pOwner, IfaceInfo *pInfo)
   432 {
   433 	CExtension *pExt = (CExtension *)pOwner;
   434 
   435 	pExt->AddDependency(pInfo);
   436 }
   437 
   438 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
   439 {
   440 	CExtension *pExt = (CExtension *)pOwner;
   441 
   442 	pExt->AddInterface(pInterface);
   443 }
   444 
   445 void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
   446 {
   447 	CExtension *pExt = (CExtension *)pParent;
   448 
   449 	pExt->AddPlugin(pPlugin);
   450 }
   451 
   452 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
   453 {
   454 	List<CExtension *>::iterator iter;
   455 
   456 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   457 	{
   458 		(*iter)->RemovePlugin(plugin);
   459 	}
   460 }
   461 
   462 CExtension *CExtensionManager::FindByOrder(unsigned int num)
   463 {
   464 	if (num < 1 || num > m_Libs.size())
   465 	{
   466 		return NULL;
   467 	}
   468 
   469 	List<CExtension *>::iterator iter = m_Libs.begin();
   470 
   471 	while (iter != m_Libs.end())
   472 	{
   473 		if (--num == 0)
   474 		{
   475 			return (*iter);
   476 		}
   477 		iter++;
   478 	}
   479 
   480 	return NULL;
   481 }
   482 
   483 void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
   484 {
   485 	List<CExtension *>::iterator iter;
   486 	CExtension *pExt;
   487 	IPluginContext *pContext = pPlugin->GetBaseContext();
   488 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   489 	{
   490 		pExt = (*iter);
   491 		if (!pExt->IsLoaded())
   492 		{
   493 			continue;
   494 		}
   495 		if (!pExt->m_Natives.size())
   496 		{
   497 			continue;
   498 		}
   499 		bool set = false;
   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();
   504 			 n_iter++)
   505 		{
   506 			natives = (*n_iter);
   507 			unsigned int i=0;
   508 			while (natives[i].func != NULL && natives[i].name != NULL)
   509 			{
   510 				if (pContext->BindNative(&natives[i++]) == SP_ERROR_NONE)
   511 				{
   512 					set = true;
   513 				}
   514 			}
   515 		}
   516 		if (set && (pExt->m_Plugins.find(pPlugin) == pExt->m_Plugins.end()))
   517 		{
   518 			/* For now, mark this plugin as a requirement.  Later we'll introduce native filtering. */
   519 			pExt->m_Plugins.push_back(pPlugin);
   520 		}
   521 	}
   522 }
   523 
   524 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
   525 {
   526 	if (!_pExt)
   527 	{
   528 		return false;
   529 	}
   530 
   531 	CExtension *pExt = (CExtension *)_pExt;
   532 
   533 	if (m_Libs.find(pExt) == m_Libs.end())
   534 	{
   535 		return false;
   536 	}
   537 
   538 	/* First remove us from internal lists */
   539 	g_ShareSys.RemoveInterfaces(_pExt);
   540 	m_Libs.remove(pExt);
   541 
   542 	List<CExtension *> UnloadQueue;
   543 
   544 	/* Handle dependencies */
   545 	if (pExt->IsLoaded())
   546 	{
   547 		/* Unload any dependent plugins */
   548 		List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
   549 		while (p_iter != pExt->m_Plugins.end())
   550 		{
   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);
   554 		}
   555 
   556 		/* Notify and/or unload all dependencies */
   557 		List<CExtension *>::iterator c_iter;
   558 		CExtension *pDep;
   559 		IExtensionInterface *pAPI;
   560 		for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
   561 		{
   562 			pDep = (*c_iter);
   563 			if ((pAPI=pDep->GetAPI()) == NULL)
   564 			{
   565 				continue;
   566 			}
   567 			if (pDep == pExt)
   568 			{
   569 				continue;
   570 			}
   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())
   575 			{
   576 				if ((*i_iter).owner == _pExt)
   577 				{
   578 					if (!dropped && !pAPI->QueryInterfaceDrop((*i_iter).iface))
   579 					{
   580 						dropped = true;
   581 					}
   582 					pAPI->NotifyInterfaceDrop((*i_iter).iface);
   583 					i_iter = pDep->m_Deps.erase(i_iter);
   584 				} else {
   585 					i_iter++;
   586 				}
   587 			}
   588 		}
   589 	}
   590 
   591 	List<CExtension *>::iterator iter;
   592 	for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
   593 	{
   594 		/* NOTE: This is safe because the unload function backs out of anything not present */
   595 		UnloadExtension((*iter));
   596 	}
   597 
   598 	return true;
   599 }
   600 
   601 void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
   602 {
   603 	CExtension *pExt = (CExtension *)pOwner;
   604 
   605 	pExt->m_Natives.push_back(natives);
   606 }
   607 
   608 void CExtensionManager::MarkAllLoaded()
   609 {
   610 	List<CExtension *>::iterator iter;
   611 	CExtension *pExt;
   612 
   613 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   614 	{
   615 		pExt = (*iter);
   616 		if (!pExt->IsLoaded())
   617 		{
   618 			continue;
   619 		}
   620 		if (pExt->m_FullyLoaded)
   621 		{
   622 			continue;
   623 		}
   624 		pExt->MarkAllLoaded();
   625 	}
   626 }
   627 
   628 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
   629 {
   630 	/* :TODO: implement */
   631 	return;
   632 }
   633 
   634 void CExtensionManager::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
   635 {
   636 	if (argcount >= 3)
   637 	{
   638 		const char *cmd = g_RootMenu.GetArgument(2);
   639 		if (strcmp(cmd, "list") == 0)
   640 		{
   641 			List<CExtension *>::iterator iter;
   642 			CExtension *pExt;
   643 			unsigned int num = 1;
   644 			switch (m_Libs.size())
   645 			{
   646 			case 1:
   647 				{
   648 					g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
   649 					break;
   650 				}
   651 			case 0:
   652 				{
   653 					g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
   654 					break;
   655 				}
   656 			default:
   657 				{
   658 					g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
   659 					break;
   660 				}
   661 			}
   662 			for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
   663 			{
   664 				pExt = (*iter);
   665 				if (pExt->IsLoaded())
   666 				{
   667 					char error[255];
   668 					if (!pExt->IsRunning(error, sizeof(error)))
   669 					{
   670 						g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
   671 					} else {
   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);
   677 					}
   678 				} else {
   679 					g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
   680 				}
   681 			}
   682 			return;
   683 		} else if (strcmp(cmd, "info") == 0) {
   684 			if (argcount < 4)
   685 			{
   686 				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
   687 				return;
   688 			}
   689 
   690 			const char *sId = g_RootMenu.GetArgument(3);
   691 			unsigned int id = atoi(sId);
   692 			if (id <= 0)
   693 			{
   694 				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
   695 				return;
   696 			}
   697 
   698 			if (m_Libs.size() == 0)
   699 			{
   700 				g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
   701 				return;
   702 			}
   703 
   704 			if (id > m_Libs.size())
   705 			{
   706 				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
   707 				return;
   708 			}
   709 
   710 			List<CExtension *>::iterator iter = m_Libs.begin();
   711 			CExtension *pExt = NULL;
   712 			while (iter != m_Libs.end())
   713 			{
   714 				if (--id == 0)
   715 				{
   716 					pExt = (*iter);
   717 					break;
   718 				}
   719 				iter++;
   720 			}
   721 			/* This should never happen */
   722 			if (!pExt)
   723 			{
   724 				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
   725 				return;
   726 			}
   727 
   728 			if (!pExt->IsLoaded())
   729 			{
   730 				g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
   731 				g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
   732 			} else {
   733 				char error[255];
   734 				if (!pExt->IsRunning(error, sizeof(error)))
   735 				{
   736 					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
   737 					g_RootMenu.ConsolePrint(" Loaded: Yes");
   738 					g_RootMenu.ConsolePrint(" Running: No (%s)", error);
   739 				} else {
   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");
   747 				}
   748 			}
   749 			return;
   750 		} else if (strcmp(cmd, "unload") == 0) {
   751 			if (argcount < 4)
   752 			{
   753 				g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
   754 				return;
   755 			}
   756 
   757 			const char *arg = g_RootMenu.GetArgument(3);
   758 			unsigned int num = atoi(arg);
   759 			CExtension *pExt = FindByOrder(num);
   760 
   761 			if (!pExt)
   762 			{
   763 				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
   764 				return;
   765 			}
   766 
   767 			if (argcount > 4 && pExt->unload_code)
   768 			{
   769 				const char *unload = g_RootMenu.GetArgument(4);
   770 				if (pExt->unload_code == (unsigned)atoi(unload))
   771 				{
   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);
   776 				} else {
   777 					g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
   778 				}
   779 				return;
   780 			}
   781 
   782 			if (!pExt->IsLoaded() 
   783 				|| (!pExt->m_Deps.size() && !pExt->m_Plugins.size()))
   784 			{
   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);
   789 				return;
   790 			} else {
   791 				List<IPlugin *> plugins;
   792 				if (pExt->m_Deps.size())
   793 				{
   794 					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
   795 					List<CExtension *>::iterator iter;
   796 					CExtension *pOther;
   797 					/* Get list of all extensions */
   798 					for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   799 					{
   800 						List<IfaceInfo>::iterator i_iter;
   801 						pOther = (*iter);
   802 						if (!pOther->IsLoaded() || pOther == pExt)
   803 						{
   804 							continue;
   805 						}
   806 						/* Get their dependencies */
   807 						for (i_iter=pOther->m_Deps.begin();
   808 							 i_iter!=pOther->m_Deps.end();
   809 							 i_iter++)
   810 						{
   811 							/* Is this dependency to us? */
   812 							if ((*i_iter).owner != pExt)
   813 							{
   814 								continue;
   815 							}
   816 							/* Will our dependent care? */
   817 							if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
   818 							{
   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();
   824 									 p_iter++)
   825 								{
   826 									if (plugins.find((*p_iter)) == plugins.end())
   827 									{
   828 										plugins.push_back((*p_iter));
   829 									}
   830 								}
   831 							}
   832 						}
   833 					}
   834 				}
   835 				if (pExt->m_Plugins.size())
   836 				{
   837 					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
   838 					List<IPlugin *>::iterator iter;
   839 					IPlugin *pPlugin;
   840 					for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
   841 					{
   842 						pPlugin = (*iter);
   843 						if (plugins.find(pPlugin) == plugins.end())
   844 						{
   845 							plugins.push_back(pPlugin);
   846 						}
   847 					}
   848 					for (iter = plugins.begin(); iter != plugins.end(); iter++)
   849 					{
   850 						pPlugin = (*iter);
   851 						g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
   852 					}
   853 				}
   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);
   858 
   859 				return;
   860 			}
   861 		}
   862 	}
   863 
   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");
   868 }