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