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