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