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