core/ExtensionSys.cpp
author Ryan Stecker <voidedweasel@gmail.com>
Sun May 27 01:51:08 2012 +0100 (2012-05-27)
changeset 3511 c9fd86b6bec3
parent 3402 a459183ce707
child 3515 c14c14e06cc1
permissions -rw-r--r--
Exposed extensions list to clients (bug 5221, r=asherkin).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdlib.h>
    33 #include "ExtensionSys.h"
    34 #include "LibrarySys.h"
    35 #include "ShareSys.h"
    36 #include "Logger.h"
    37 #include "sourcemm_api.h"
    38 #include "sm_srvcmds.h"
    39 #include "sm_stringutil.h"
    40 #include "PlayerManager.h"
    41 
    42 CExtensionManager g_Extensions;
    43 IdentityType_t g_ExtType;
    44 
    45 void CExtension::Initialize(const char *filename, const char *path)
    46 {
    47 	m_pAPI = NULL;
    48 	m_pIdentToken = NULL;
    49 	unload_code = 0;
    50 	m_bFullyLoaded = false;
    51 	m_File.assign(filename);
    52 	m_Path.assign(path);
    53 	char real_name[PLATFORM_MAX_PATH];
    54 	g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
    55 	m_RealFile.assign(real_name);
    56 }
    57 
    58 CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
    59 {
    60 	Initialize(filename, path);
    61 	m_pAPI = pAPI;
    62 }
    63 
    64 #if defined METAMOD_PLAPI_VERSION
    65 #if SOURCE_ENGINE == SE_LEFT4DEAD
    66 #define GAMEFIX "2.l4d"
    67 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
    68 #define GAMEFIX "2.l4d2"
    69 #elif SOURCE_ENGINE == SE_ALIENSWARM
    70 #define GAMEFIX "2.swarm"
    71 #elif SOURCE_ENGINE == SE_ORANGEBOX
    72 #define GAMEFIX "2.ep2"
    73 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
    74 #define GAMEFIX "2.bgt"
    75 #elif SOURCE_ENGINE == SE_EYE
    76 #define GAMEFIX "2.eye"
    77 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
    78 #define GAMEFIX "2.ep2v"
    79 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    80 #define GAMEFIX "2.darkm"
    81 #else
    82 #define GAMEFIX "2.ep1"
    83 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
    84 #else  //METAMOD_PLAPI_VERSION
    85 #define GAMEFIX "1.ep1"
    86 #endif //METAMOD_PLAPI_VERSION
    87 
    88 CLocalExtension::CLocalExtension(const char *filename)
    89 {
    90 	m_PlId = 0;
    91 	m_pLib = NULL;
    92 
    93 	char path[PLATFORM_MAX_PATH];
    94 
    95 	/* Special case for new bintools binary */
    96 	if (strcmp(filename, "bintools.ext") == 0)
    97 	{
    98 		goto normal;
    99 	}
   100 
   101 	/* Zeroth, see if there is an engine specific build in the new place. */
   102 	g_SourceMod.BuildPath(Path_SM,
   103 		path,
   104 		PLATFORM_MAX_PATH,
   105 		"extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
   106 		filename);
   107 
   108 	if (g_LibSys.IsPathFile(path))
   109 	{
   110 		goto found;
   111 	}
   112 
   113 	/* First see if there is an engine specific build! */
   114 	g_SourceMod.BuildPath(Path_SM, 
   115 		path, 
   116 		PLATFORM_MAX_PATH,
   117 		"extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
   118 		filename);
   119 
   120 normal:
   121 	/* Try the "normal" version */
   122 	if (!g_LibSys.IsPathFile(path))
   123 	{
   124 		g_SourceMod.BuildPath(Path_SM,
   125 			path,
   126 			PLATFORM_MAX_PATH,
   127 			"extensions/%s." PLATFORM_LIB_EXT,
   128 			filename);
   129 	}
   130 
   131 found:
   132 	Initialize(filename, path);
   133 }
   134 
   135 bool CRemoteExtension::Load(char *error, size_t maxlength)
   136 {
   137 	if (!PerformAPICheck(error, maxlength))
   138 	{
   139 		m_pAPI = NULL;
   140 		return false;
   141 	}
   142 
   143 	if (!CExtension::Load(error, maxlength))
   144 	{
   145 		m_pAPI = NULL;
   146 		return false;
   147 	}
   148 
   149 	return true;
   150 }
   151 
   152 bool CLocalExtension::Load(char *error, size_t maxlength)
   153 {
   154 	m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
   155 
   156 	if (m_pLib == NULL)
   157 	{
   158 		return false;
   159 	}
   160 
   161 	typedef IExtensionInterface *(*GETAPI)();
   162 	GETAPI pfnGetAPI = NULL;
   163 
   164 	if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
   165 	{
   166 		m_pLib->CloseLibrary();
   167 		m_pLib = NULL;
   168 		snprintf(error, maxlength, "Unable to find extension entry point");
   169 		return false;
   170 	}
   171 
   172 	m_pAPI = pfnGetAPI();
   173 
   174 	/* Check pointer and version */
   175 	if (!PerformAPICheck(error, maxlength))
   176 	{
   177 		m_pLib->CloseLibrary();
   178 		m_pLib = NULL;
   179 		m_pAPI = NULL;
   180 		return false;
   181 	}
   182 
   183 	/* Load as MM:S */
   184 	if (m_pAPI->IsMetamodExtension())
   185 	{
   186 		bool already;
   187 		m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
   188 
   189 		// Check the plugin didn't refuse load
   190 		Pl_Status status;
   191 		
   192 #ifndef METAMOD_PLAPI_VERSION
   193 		const char *file;
   194 		PluginId source;
   195 #endif
   196 		
   197 		if (!m_PlId || (
   198 #ifndef METAMOD_PLAPI_VERSION
   199 			g_pMMPlugins->Query(m_PlId, file, status, source)
   200 #else
   201 			g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
   202 #endif			
   203 			&& status < Pl_Paused))
   204 		{
   205 			m_pLib->CloseLibrary();
   206 			m_pLib = NULL;
   207 			m_pAPI = NULL;
   208 			return false;
   209 		}
   210 	}
   211 
   212 	if (!CExtension::Load(error, maxlength))
   213 	{
   214 		if (m_pAPI->IsMetamodExtension())
   215 		{
   216 			if (m_PlId)
   217 			{
   218 				char dummy[255];
   219 				g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
   220 				m_PlId = 0;
   221 			}
   222 		}
   223 		m_pLib->CloseLibrary();
   224 		m_pLib = NULL;
   225 		m_pAPI = NULL;
   226 		return false;
   227 	}
   228 
   229 	return true;
   230 }
   231 
   232 void CRemoteExtension::Unload()
   233 {
   234 }
   235 
   236 void CLocalExtension::Unload()
   237 {
   238 	if (m_pAPI != NULL && m_PlId)
   239 	{
   240 		char temp_buffer[255];
   241 		g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
   242 		m_PlId = 0;
   243 	}
   244 
   245 	if (m_pLib != NULL)
   246 	{
   247 		m_pLib->CloseLibrary();
   248 		m_pLib = NULL;
   249 	}
   250 }
   251 
   252 bool CRemoteExtension::Reload(char *error, size_t maxlength)
   253 {
   254 	snprintf(error, maxlength, "Remote extensions do not support reloading");
   255 	return false;
   256 }
   257 
   258 bool CLocalExtension::Reload(char *error, size_t maxlength)
   259 {
   260 	if (m_pLib == NULL) // FIXME: just load it instead?
   261 		return false;
   262 	
   263 	m_pAPI->OnExtensionUnload();
   264 	Unload();
   265 	
   266 	return Load(error, maxlength);
   267 }
   268 
   269 bool CRemoteExtension::IsExternal()
   270 {
   271 	return true;
   272 }
   273 
   274 bool CLocalExtension::IsExternal()
   275 {
   276 	return false;
   277 }
   278 
   279 CExtension::~CExtension()
   280 {
   281 	DestroyIdentity();
   282 }
   283 
   284 bool CExtension::PerformAPICheck(char *error, size_t maxlength)
   285 {
   286 	if (!m_pAPI)
   287 	{
   288 		snprintf(error, maxlength, "No IExtensionInterface instance provided");
   289 		return false;
   290 	}
   291 	
   292 	if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
   293 	{
   294 		snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
   295 		return false;
   296 	}
   297 
   298 	return true;
   299 }
   300 
   301 bool CExtension::Load(char *error, size_t maxlength)
   302 {
   303 	CreateIdentity();
   304 	if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
   305 	{
   306 		DestroyIdentity();
   307 		return false;
   308 	}
   309 	else
   310 	{
   311 		/* Check if we're past load time */
   312 		if (!g_SourceMod.IsMapLoading())
   313 		{
   314 			m_pAPI->OnExtensionsAllLoaded();
   315 		}
   316 	}
   317 
   318 	return true;
   319 }
   320 
   321 void CExtension::CreateIdentity()
   322 {
   323 	if (m_pIdentToken != NULL)
   324 	{
   325 		return;
   326 	}
   327 
   328 	m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
   329 }
   330 
   331 void CExtension::DestroyIdentity()
   332 {
   333 	if (m_pIdentToken == NULL)
   334 	{
   335 		return;
   336 	}
   337 
   338 	g_ShareSys.DestroyIdentity(m_pIdentToken);
   339 	m_pIdentToken = NULL;
   340 }
   341 
   342 void CExtension::MarkAllLoaded()
   343 {
   344 	assert(m_bFullyLoaded == false);
   345 	if (!m_bFullyLoaded)
   346 	{
   347 		m_bFullyLoaded = true;
   348 		m_pAPI->OnExtensionsAllLoaded();
   349 	}
   350 }
   351 
   352 void CExtension::AddPlugin(CPlugin *pPlugin)
   353 {
   354 	/* Unfortunately we have to do this :( */
   355 	if (m_Dependents.find(pPlugin) != m_Dependents.end())
   356 	{
   357 		m_Dependents.push_back(pPlugin);
   358 	}
   359 }
   360 
   361 void CExtension::SetError(const char *error)
   362 {
   363 	m_Error.assign(error);
   364 }
   365 
   366 IExtensionInterface *CExtension::GetAPI()
   367 {
   368 	return m_pAPI;
   369 }
   370 
   371 const char *CExtension::GetFilename()
   372 {
   373 	return m_RealFile.c_str();
   374 }
   375 
   376 const char *CExtension::GetPath() const
   377 {
   378 	return m_Path.c_str();
   379 }
   380 
   381 IdentityToken_t *CExtension::GetIdentity()
   382 {
   383 	return m_pIdentToken;
   384 }
   385 
   386 bool CRemoteExtension::IsLoaded()
   387 {
   388 	return (m_pAPI != NULL);
   389 }
   390 
   391 bool CLocalExtension::IsLoaded()
   392 {
   393 	return (m_pLib != NULL);
   394 }
   395 
   396 void CExtension::AddDependency(const IfaceInfo *pInfo)
   397 {
   398 	if (m_Deps.find(*pInfo) == m_Deps.end())
   399 	{
   400 		m_Deps.push_back(*pInfo);
   401 	}
   402 }
   403 
   404 bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
   405 {
   406 	return (i1.iface == i2.iface) && (i1.owner == i2.owner);
   407 }
   408 
   409 void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
   410 {
   411 	IfaceInfo info;
   412 	info.iface = iface;
   413 	info.owner = pOther;
   414 
   415 	List<IfaceInfo>::iterator iter;
   416 	for (iter = m_ChildDeps.begin();
   417 		 iter != m_ChildDeps.end();
   418 		 iter++)
   419 	{
   420 		IfaceInfo &other = (*iter);
   421 		if (other == info)
   422 		{
   423 			return;
   424 		}
   425 	}
   426 
   427 	m_ChildDeps.push_back(info);
   428 }
   429 
   430 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
   431 {
   432 	List<IfaceInfo>::iterator iter = m_Deps.begin();
   433 
   434 	if (iter == m_Deps.end())
   435 	{
   436 		return NULL;
   437 	}
   438 
   439 	if (pOwner)
   440 	{
   441 		*pOwner = (*iter).owner;
   442 	}
   443 	if (pInterface)
   444 	{
   445 		*pInterface = (*iter).iface;
   446 	}
   447 
   448 	List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
   449 
   450 	return (ITERATOR *)pIter;
   451 }
   452 
   453 bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
   454 {
   455 	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   456 	List<IfaceInfo>::iterator _iter;
   457 
   458 	if (_iter == m_Deps.end())
   459 	{
   460 		return false;
   461 	}
   462 
   463 	_iter++;
   464 
   465 	if (pOwner)
   466 	{
   467 		*pOwner = (*_iter).owner;
   468 	}
   469 	if (pInterface)
   470 	{
   471 		*pInterface = (*_iter).iface;
   472 	}
   473 
   474 	*pIter = _iter;
   475 
   476 	if (_iter == m_Deps.end())
   477 	{
   478 		return false;
   479 	}
   480 
   481 	return true;
   482 }
   483 
   484 void CExtension::FreeDependencyIterator(ITERATOR *iter)
   485 {
   486 	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   487 
   488 	delete pIter;
   489 }
   490 
   491 void CExtension::AddInterface(SMInterface *pInterface)
   492 {
   493 	m_Interfaces.push_back(pInterface);
   494 }
   495 
   496 bool CExtension::IsRunning(char *error, size_t maxlength)
   497 {
   498 	if (!IsLoaded())
   499 	{
   500 		if (error)
   501 		{
   502 			snprintf(error, maxlength, "%s", m_Error.c_str());
   503 		}
   504 		return false;
   505 	}
   506 
   507 	return m_pAPI->QueryRunning(error, maxlength);
   508 }
   509 
   510 void CExtension::AddLibrary(const char *library)
   511 {
   512 	m_Libraries.push_back(library);
   513 }
   514 
   515 /*********************
   516  * EXTENSION MANAGER *
   517  *********************/
   518 
   519 void CExtensionManager::OnSourceModAllInitialized()
   520 {
   521 	g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
   522 	g_PluginSys.AddPluginsListener(this);
   523 	g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
   524 	g_ShareSys.AddInterface(NULL, this);
   525 }
   526 
   527 void CExtensionManager::OnSourceModShutdown()
   528 {
   529 	g_RootMenu.RemoveRootConsoleCommand("exts", this);
   530 	g_ShareSys.DestroyIdentType(g_ExtType);
   531 }
   532 
   533 void CExtensionManager::Shutdown()
   534 {
   535 	List<CExtension *>::iterator iter;
   536 
   537 	while ((iter = m_Libs.begin()) != m_Libs.end())
   538 	{
   539 		UnloadExtension((*iter));
   540 	}
   541 }
   542 
   543 void CExtensionManager::TryAutoload()
   544 {
   545 	char path[PLATFORM_MAX_PATH];
   546 
   547 	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
   548 
   549 	IDirectory *pDir = g_LibSys.OpenDirectory(path);
   550 	if (!pDir)
   551 	{
   552 		return;
   553 	}
   554 
   555 	const char *lfile;
   556 	size_t len;
   557 	while (pDir->MoreFiles())
   558 	{
   559 		if (pDir->IsEntryDirectory())
   560 		{
   561 			pDir->NextEntry();
   562 			continue;
   563 		}
   564 
   565 		lfile = pDir->GetEntryName();
   566 		len = strlen(lfile);
   567 		if (len <= 9) /* size of ".autoload" */
   568 		{
   569 			pDir->NextEntry();
   570 			continue;
   571 		}
   572 
   573 		if (strcmp(&lfile[len - 9], ".autoload") != 0)
   574 		{
   575 			pDir->NextEntry();
   576 			continue;
   577 		}
   578 
   579 		char file[PLATFORM_MAX_PATH];
   580 		len = UTIL_Format(file, sizeof(file), "%s", lfile);
   581 		strcpy(&file[len - 9], ".ext");
   582 
   583 		LoadAutoExtension(file);
   584 		
   585 		pDir->NextEntry();
   586 	}
   587 }
   588 
   589 IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
   590 {
   591 	/* Remove platform extension if it's there. Compat hack. */
   592 	const char *ext = g_LibSys.GetFileExtension(path);
   593 	if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
   594 	{
   595 		char path2[PLATFORM_MAX_PATH];
   596 		UTIL_Format(path2, sizeof(path2), "%s", path);
   597 		path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
   598 		return LoadAutoExtension(path2, bErrorOnMissing);
   599 	}
   600 
   601 	IExtension *pAlready;
   602 	if ((pAlready=FindExtensionByFile(path)) != NULL)
   603 	{
   604 		return pAlready;
   605 	}
   606 
   607 	char error[256];
   608 	CExtension *p = new CLocalExtension(path);
   609 
   610 	/* We put us in the list beforehand so extensions that check for each other
   611 	 * won't recursively load each other.
   612 	 */
   613 	m_Libs.push_back(p);
   614 
   615 	if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
   616 	{
   617 		if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
   618 		{
   619 			g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
   620 		}
   621 
   622 		p->SetError(error);
   623 	}
   624 
   625 	return p;
   626 }
   627 
   628 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
   629 {
   630 	List<CExtension *>::iterator iter;
   631 	CExtension *pExt;
   632 
   633 	/* Chomp off the path */
   634 	char lookup[PLATFORM_MAX_PATH];
   635 	g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
   636 
   637 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   638 	{
   639 		pExt = (*iter);
   640 		if (pExt->IsSameFile(lookup))
   641 		{
   642 			return pExt;
   643 		}
   644 	}
   645 
   646 	return NULL;
   647 }
   648 
   649 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
   650 {
   651 	List<CExtension *>::iterator iter;
   652 	CExtension *pExt;
   653 	IExtensionInterface *pAPI;
   654 	const char *name;
   655 
   656 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   657 	{
   658 		pExt = (*iter);
   659 		if (!pExt->IsLoaded())
   660 		{
   661 			continue;
   662 		}
   663 		if ((pAPI = pExt->GetAPI()) == NULL)
   664 		{
   665 			continue;
   666 		}
   667 		name = pAPI->GetExtensionName();
   668 		if (!name)
   669 		{
   670 			continue;
   671 		}
   672 		if (strcmp(name, ext) == 0)
   673 		{
   674 			return pExt;
   675 		}
   676 	}
   677 
   678 	return NULL;
   679 }
   680 
   681 IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
   682 {
   683 	/* Remove platform extension if it's there. Compat hack. */
   684 	const char *ext = g_LibSys.GetFileExtension(file);
   685 	if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
   686 	{
   687 		char path2[PLATFORM_MAX_PATH];
   688 		UTIL_Format(path2, sizeof(path2), "%s", file);
   689 		path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
   690 		return LoadExtension(path2, error, maxlength);
   691 	}
   692 
   693 	IExtension *pAlready;
   694 	if ((pAlready=FindExtensionByFile(file)) != NULL)
   695 	{
   696 		return pAlready;
   697 	}
   698 
   699 	CExtension *pExt = new CLocalExtension(file);
   700 
   701 	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
   702 	{
   703 		pExt->Unload();
   704 		delete pExt;
   705 		return NULL;
   706 	}
   707 
   708 	/* :TODO: do QueryRunning check if the map is loaded */
   709 
   710 	m_Libs.push_back(pExt);
   711 
   712 	return pExt;
   713 }
   714 
   715 void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
   716 {
   717 	CExtension *pExt = (CExtension *)pRequester;
   718 	CExtension *pOwner = (CExtension *)pInfo->owner;
   719 
   720 	pExt->AddDependency(pInfo);
   721 
   722 	IExtensionInterface *pAPI = pExt->GetAPI();
   723 	if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
   724 	{
   725 		pOwner->AddChildDependent(pExt, pInfo->iface);
   726 	}
   727 }
   728 
   729 void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
   730 {
   731 	CExtension *pExt = (CExtension *)ext;
   732 	CExtension *pOwner = GetExtensionFromIdent(other);
   733 
   734 	IfaceInfo info;
   735 
   736 	info.iface = (SMInterface *)iface;
   737 	info.owner = pOwner;
   738 
   739 	pExt->AddDependency(&info);
   740 	pOwner->AddChildDependent(pExt, (SMInterface *)iface);
   741 }
   742 
   743 void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
   744 {
   745 	CExtension *pExt = (CExtension *)pOwner;
   746 
   747 	pExt->AddInterface(pInterface);
   748 }
   749 
   750 void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
   751 {
   752 	CExtension *pExt = (CExtension *)pParent;
   753 
   754 	pExt->AddPlugin(pPlugin);
   755 }
   756 
   757 void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
   758 {
   759 	List<CExtension *>::iterator iter;
   760 
   761 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   762 	{
   763 		(*iter)->DropRefsTo((CPlugin *)plugin);
   764 	}
   765 }
   766 
   767 CExtension *CExtensionManager::FindByOrder(unsigned int num)
   768 {
   769 	if (num < 1 || num > m_Libs.size())
   770 	{
   771 		return NULL;
   772 	}
   773 
   774 	List<CExtension *>::iterator iter = m_Libs.begin();
   775 
   776 	while (iter != m_Libs.end())
   777 	{
   778 		if (--num == 0)
   779 		{
   780 			return (*iter);
   781 		}
   782 		iter++;
   783 	}
   784 
   785 	return NULL;
   786 }
   787 
   788 bool CExtensionManager::UnloadExtension(IExtension *_pExt)
   789 {
   790 	if (!_pExt)
   791 	{
   792 		return false;
   793 	}
   794 
   795 	CExtension *pExt = (CExtension *)_pExt;
   796 
   797 	if (m_Libs.find(pExt) == m_Libs.end())
   798 	{
   799 		return false;
   800 	}
   801 
   802 	/* First remove us from internal lists */
   803 	g_ShareSys.RemoveInterfaces(_pExt);
   804 	m_Libs.remove(pExt);
   805 
   806 	List<CExtension *> UnloadQueue;
   807 
   808 	/* Handle dependencies */
   809 	if (pExt->IsLoaded())
   810 	{
   811 		/* Unload any dependent plugins */
   812 		List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
   813 		while (p_iter != pExt->m_Dependents.end())
   814 		{
   815 			/* We have to manually unlink ourselves here, since we're no longer being managed */
   816 			g_PluginSys.UnloadPlugin((*p_iter));
   817 			p_iter = pExt->m_Dependents.erase(p_iter);
   818 		}
   819 
   820 		List<String>::iterator s_iter;
   821 		for (s_iter = pExt->m_Libraries.begin();
   822 			 s_iter != pExt->m_Libraries.end();
   823 			 s_iter++)
   824 		{
   825 			g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
   826 		}
   827 
   828 		/* Notify and/or unload all dependencies */
   829 		List<CExtension *>::iterator c_iter;
   830 		CExtension *pDep;
   831 		IExtensionInterface *pAPI;
   832 		for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
   833 		{
   834 			pDep = (*c_iter);
   835 			if ((pAPI=pDep->GetAPI()) == NULL)
   836 			{
   837 				continue;
   838 			}
   839 			if (pDep == pExt)
   840 			{
   841 				continue;
   842 			}
   843 			/* Now, get its dependency list */
   844 			bool dropped = false;
   845 			List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
   846 			while (i_iter != pDep->m_Deps.end())
   847 			{
   848 				if ((*i_iter).owner == _pExt)
   849 				{
   850 					if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
   851 					{
   852 						if (!dropped)
   853 						{
   854 							dropped = true;
   855 							UnloadQueue.push_back(pDep);
   856 						}
   857 					}
   858 					pAPI->NotifyInterfaceDrop((*i_iter).iface);
   859 					i_iter = pDep->m_Deps.erase(i_iter);
   860 				}
   861 				else
   862 				{
   863 					i_iter++;
   864 				}
   865 			}
   866 			/* Flush out any back references to this plugin */
   867 			i_iter = pDep->m_ChildDeps.begin();
   868 			while (i_iter != pDep->m_ChildDeps.end())
   869 			{
   870 				if ((*i_iter).owner == pExt)
   871 				{
   872 					i_iter = pDep->m_ChildDeps.erase(i_iter);
   873 				}
   874 				else
   875 				{
   876 					i_iter++;
   877 				}
   878 			}
   879 		}
   880 
   881 		/* Unbind our natives from Core */
   882 		pExt->DropEverything();
   883 	}
   884 
   885 	IdentityToken_t *pIdentity;
   886 	if ((pIdentity = pExt->GetIdentity()) != NULL)
   887 	{
   888 		SMGlobalClass *glob = SMGlobalClass::head;
   889 		while (glob)
   890 		{
   891 			glob->OnSourceModIdentityDropped(pIdentity);
   892 			glob = glob->m_pGlobalClassNext;
   893 		}
   894 	}
   895 
   896 	/* Tell it to unload */
   897 	if (pExt->IsLoaded())
   898 	{
   899 		IExtensionInterface *pAPI = pExt->GetAPI();
   900 		pAPI->OnExtensionUnload();
   901 	}
   902 
   903 	pExt->Unload();
   904 	delete pExt;
   905 
   906 	List<CExtension *>::iterator iter;
   907 	for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
   908 	{
   909 		/* NOTE: This is safe because the unload function backs out of anything not present */
   910 		UnloadExtension((*iter));
   911 	}
   912 
   913 	return true;
   914 }
   915 
   916 void CExtensionManager::MarkAllLoaded()
   917 {
   918 	List<CExtension *>::iterator iter;
   919 	CExtension *pExt;
   920 
   921 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   922 	{
   923 		pExt = (*iter);
   924 		if (!pExt->IsLoaded())
   925 		{
   926 			continue;
   927 		}
   928 		if (pExt->m_bFullyLoaded)
   929 		{
   930 			continue;
   931 		}
   932 		pExt->MarkAllLoaded();
   933 	}
   934 }
   935 
   936 void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
   937 {
   938 	/* This function doesn't really need to do anything now.  We make sure the 
   939 	 * other extension is loaded, but handling of dependencies is really done 
   940 	 * by the interface fetcher.
   941 	 */
   942 	if (required || autoload)
   943 	{
   944 		LoadAutoExtension(file);
   945 	}
   946 }
   947 
   948 void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   949 {
   950 	int argcount = command.ArgC();
   951 	if (argcount >= 3)
   952 	{
   953 		const char *cmd = command.Arg(2);
   954 		if (strcmp(cmd, "list") == 0)
   955 		{
   956 			List<CExtension *>::iterator iter;
   957 			CExtension *pExt;
   958 			unsigned int num = 1;
   959 			switch (m_Libs.size())
   960 			{
   961 			case 1:
   962 				{
   963 					g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
   964 					break;
   965 				}
   966 			case 0:
   967 				{
   968 					g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
   969 					break;
   970 				}
   971 			default:
   972 				{
   973 					g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
   974 					break;
   975 				}
   976 			}
   977 			for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
   978 			{
   979 				pExt = (*iter);
   980 				if (pExt->IsLoaded())
   981 				{
   982 					char error[255];
   983 					if (!pExt->IsRunning(error, sizeof(error)))
   984 					{
   985 						g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
   986 					}
   987 					else
   988 					{
   989 						IExtensionInterface *pAPI = pExt->GetAPI();
   990 						const char *name = pAPI->GetExtensionName();
   991 						const char *version = pAPI->GetExtensionVerString();
   992 						const char *descr = pAPI->GetExtensionDescription();
   993 						g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
   994 					}
   995 				} else {
   996 					g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
   997 				}
   998 			}
   999 			return;
  1000 		}
  1001 		else if (strcmp(cmd, "load") == 0)
  1002 		{
  1003 			if (argcount < 4)
  1004 			{
  1005 				g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
  1006 				return;
  1007 			}
  1008 
  1009 			const char *filename = command.Arg(3);
  1010 			char path[PLATFORM_MAX_PATH];
  1011 			char error[256];
  1012 
  1013 			UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
  1014 				!strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
  1015 			
  1016 			if (FindExtensionByFile(path) != NULL)
  1017 			{
  1018 				g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
  1019 				return;
  1020 			}
  1021 			
  1022 			if (LoadExtension(path, error, sizeof(error)))
  1023 			{
  1024 				g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
  1025 			} else
  1026 			{
  1027 				g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
  1028 			}
  1029 			
  1030 			return;
  1031 		}
  1032 		else if (strcmp(cmd, "info") == 0)
  1033 		{
  1034 			if (argcount < 4)
  1035 			{
  1036 				g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
  1037 				return;
  1038 			}
  1039 
  1040 			const char *sId = command.Arg(3);
  1041 			unsigned int id = atoi(sId);
  1042 			if (id <= 0)
  1043 			{
  1044 				g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
  1045 				return;
  1046 			}
  1047 
  1048 			if (m_Libs.size() == 0)
  1049 			{
  1050 				g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
  1051 				return;
  1052 			}
  1053 
  1054 			if (id > m_Libs.size())
  1055 			{
  1056 				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1057 				return;
  1058 			}
  1059 
  1060 			List<CExtension *>::iterator iter = m_Libs.begin();
  1061 			CExtension *pExt = NULL;
  1062 			while (iter != m_Libs.end())
  1063 			{
  1064 				if (--id == 0)
  1065 				{
  1066 					pExt = (*iter);
  1067 					break;
  1068 				}
  1069 				iter++;
  1070 			}
  1071 			/* This should never happen */
  1072 			if (!pExt)
  1073 			{
  1074 				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1075 				return;
  1076 			}
  1077 
  1078 			if (!pExt->IsLoaded())
  1079 			{
  1080 				g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1081 				g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
  1082 			}
  1083 			else
  1084 			{
  1085 				char error[255];
  1086 				if (!pExt->IsRunning(error, sizeof(error)))
  1087 				{
  1088 					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1089 					g_RootMenu.ConsolePrint(" Loaded: Yes");
  1090 					g_RootMenu.ConsolePrint(" Running: No (%s)", error);
  1091 				}
  1092 				else
  1093 				{
  1094 					IExtensionInterface *pAPI = pExt->GetAPI();
  1095 					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1096 					g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
  1097 					g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
  1098 					g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
  1099 					g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
  1100 
  1101 					if (pExt->IsExternal())
  1102 					{
  1103 						g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
  1104 					}
  1105 					else if (pAPI->IsMetamodExtension())
  1106 					{
  1107 						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
  1108 					}
  1109 					else
  1110 					{
  1111 						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
  1112 					}
  1113 				}
  1114 			}
  1115 			return;
  1116 		}
  1117 		else if (strcmp(cmd, "unload") == 0)
  1118 		{
  1119 			if (argcount < 4)
  1120 			{
  1121 				g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
  1122 				return;
  1123 			}
  1124 
  1125 			const char *arg = command.Arg(3);
  1126 			unsigned int num = atoi(arg);
  1127 			CExtension *pExt = FindByOrder(num);
  1128 
  1129 			if (!pExt)
  1130 			{
  1131 				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
  1132 				return;
  1133 			}
  1134 
  1135 			if (argcount > 4 && pExt->unload_code)
  1136 			{
  1137 				const char *unload = command.Arg(4);
  1138 				if (pExt->unload_code == (unsigned)atoi(unload))
  1139 				{
  1140 					char filename[PLATFORM_MAX_PATH];
  1141 					snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1142 					UnloadExtension(pExt);
  1143 					g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1144 				}
  1145 				else
  1146 				{
  1147 					g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
  1148 				}
  1149 				return;
  1150 			}
  1151 
  1152 			if (!pExt->IsLoaded() 
  1153 				|| (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
  1154 			{
  1155 				char filename[PLATFORM_MAX_PATH];
  1156 				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1157 				UnloadExtension(pExt);
  1158 				g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1159 				return;
  1160 			}
  1161 			else
  1162 			{
  1163 				List<CPlugin *> plugins;
  1164 				if (pExt->m_ChildDeps.size())
  1165 				{
  1166 					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
  1167 					List<CExtension *>::iterator iter;
  1168 					CExtension *pOther;
  1169 					/* Get list of all extensions */
  1170 					for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1171 					{
  1172 						List<IfaceInfo>::iterator i_iter;
  1173 						pOther = (*iter);
  1174 						if (!pOther->IsLoaded() || pOther == pExt)
  1175 						{
  1176 							continue;
  1177 						}
  1178 						/* Get their dependencies */
  1179 						for (i_iter=pOther->m_Deps.begin();
  1180 							 i_iter!=pOther->m_Deps.end();
  1181 							 i_iter++)
  1182 						{
  1183 							/* Is this dependency to us? */
  1184 							if ((*i_iter).owner != pExt)
  1185 							{
  1186 								continue;
  1187 							}
  1188 							/* Will our dependent care? */
  1189 							if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
  1190 							{
  1191 								g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
  1192 								/* Add to plugin unload list */
  1193 								List<CPlugin *>::iterator p_iter;
  1194 								for (p_iter=pOther->m_Dependents.begin();
  1195 									 p_iter!=pOther->m_Dependents.end();
  1196 									 p_iter++)
  1197 								{
  1198 									if (plugins.find((*p_iter)) == plugins.end())
  1199 									{
  1200 										plugins.push_back((*p_iter));
  1201 									}
  1202 								}
  1203 							}
  1204 						}
  1205 					}
  1206 				}
  1207 				if (pExt->m_Dependents.size())
  1208 				{
  1209 					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
  1210 					List<CPlugin *>::iterator iter;
  1211 					CPlugin *pPlugin;
  1212 					for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
  1213 					{
  1214 						pPlugin = (*iter);
  1215 						if (plugins.find(pPlugin) == plugins.end())
  1216 						{
  1217 							plugins.push_back(pPlugin);
  1218 						}
  1219 					}
  1220 					for (iter = plugins.begin(); iter != plugins.end(); iter++)
  1221 					{
  1222 						pPlugin = (*iter);
  1223 						g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
  1224 					}
  1225 				}
  1226 				srand(static_cast<int>(time(NULL)));
  1227 				pExt->unload_code = (rand() % 877) + 123;	//123 to 999
  1228 				g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
  1229 				g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
  1230 
  1231 				return;
  1232 			}
  1233 		}
  1234 		else if (strcmp(cmd, "reload") == 0)
  1235 		{
  1236 			if (argcount < 4)
  1237 			{
  1238 				g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
  1239 				return;
  1240 			}
  1241 			
  1242 			const char *arg = command.Arg(3);
  1243 			unsigned int num = atoi(arg);
  1244 			CExtension *pExt = FindByOrder(num);
  1245 
  1246 			if (!pExt)
  1247 			{
  1248 				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
  1249 				return;
  1250 			}
  1251 			
  1252 			if (pExt->IsLoaded())
  1253 			{
  1254 				char filename[PLATFORM_MAX_PATH];
  1255 				char error[255];
  1256 				
  1257 				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1258 				
  1259 				if (pExt->Reload(error, sizeof(error)))
  1260 				{
  1261 					g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
  1262 				}
  1263 				else
  1264 				{
  1265 					g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
  1266 				}
  1267 					
  1268 				return;
  1269 			} 
  1270 			else
  1271 			{
  1272 				g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
  1273 				
  1274 				return;
  1275 			}
  1276 			
  1277 		}
  1278 	}
  1279 
  1280 	g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
  1281 	g_RootMenu.DrawGenericOption("info", "Extra extension information");
  1282 	g_RootMenu.DrawGenericOption("list", "List extensions");
  1283 	g_RootMenu.DrawGenericOption("load", "Load an extension");
  1284 	g_RootMenu.DrawGenericOption("reload", "Reload an extension");
  1285 	g_RootMenu.DrawGenericOption("unload", "Unload an extension");
  1286 }
  1287 
  1288 void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
  1289 {
  1290 	char buffer[256];
  1291 	int numExtensions = m_Libs.size();
  1292 	edict_t *edict = player->GetEdict();
  1293 	unsigned int id = 0;
  1294 	unsigned int start = 0;
  1295 
  1296 	if (!numExtensions)
  1297 	{
  1298 		ClientConsolePrint(edict, "[SM] No extensions found.");
  1299 		return;
  1300 	}
  1301 
  1302 	if (args.ArgC() > 2)
  1303 	{
  1304 		start = atoi(args.Arg(2));
  1305 	}
  1306 
  1307 	CExtension *ext;
  1308 	SourceHook::List<CExtension *>::iterator iter;
  1309 
  1310 	for (iter = m_Libs.begin();
  1311 		 iter != m_Libs.end();
  1312 		 iter++)
  1313 	{
  1314 		ext = (*iter);
  1315 
  1316 		char error[255];
  1317 		if (!ext->IsRunning(error, sizeof(error)))
  1318 		{
  1319 			continue;
  1320 		}
  1321 
  1322 		id++;
  1323 		if (id < start)
  1324 		{
  1325 			continue;
  1326 		}
  1327 
  1328 		if (id - start > 10)
  1329 		{
  1330 			break;
  1331 		}
  1332 
  1333 		IExtensionInterface *api = ext->GetAPI();
  1334 
  1335 		const char *name = api->GetExtensionName();
  1336 		const char *version = api->GetExtensionVerString();
  1337 		const char *author = api->GetExtensionAuthor();
  1338 		const char *description = api->GetExtensionDescription();
  1339 
  1340 		size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
  1341 
  1342 		if (version != NULL && IS_STR_FILLED(version))
  1343 		{
  1344 			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
  1345 		}
  1346 
  1347 		if (author != NULL && IS_STR_FILLED(author))
  1348 		{
  1349 			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
  1350 		}
  1351 
  1352 		if (description != NULL && IS_STR_FILLED(description))
  1353 		{
  1354 			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
  1355 		}
  1356 
  1357 
  1358 		ClientConsolePrint(edict, "%s", buffer);
  1359 	}
  1360 
  1361 	while (iter != m_Libs.end())
  1362 	{
  1363 		char error[255];
  1364 		if ((*iter)->IsRunning(error, sizeof(error)))
  1365 		{
  1366 			break;
  1367 		}
  1368 	}
  1369 
  1370 	if (iter != m_Libs.end())
  1371 	{
  1372 		ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
  1373 	}
  1374 }
  1375 
  1376 CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
  1377 {
  1378 	if (ptr->type == g_ExtType)
  1379 	{
  1380 		return (CExtension *)(ptr->ptr);
  1381 	}
  1382 
  1383 	return NULL;
  1384 }
  1385 
  1386 CExtensionManager::CExtensionManager()
  1387 {
  1388 }
  1389 
  1390 CExtensionManager::~CExtensionManager()
  1391 {
  1392 }
  1393 
  1394 void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
  1395 {
  1396 	CExtension *pExt = (CExtension *)pSource;
  1397 	pExt->AddLibrary(library);
  1398 	g_PluginSys.OnLibraryAction(library, false, false);
  1399 }
  1400 
  1401 bool CExtensionManager::LibraryExists(const char *library)
  1402 {
  1403 	CExtension *pExt;
  1404 
  1405 	for (List<CExtension *>::iterator iter = m_Libs.begin();
  1406 		 iter != m_Libs.end();
  1407 		 iter++)
  1408 	{
  1409 		pExt = (*iter);
  1410 		for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
  1411 			 s_iter != pExt->m_Libraries.end();
  1412 			 s_iter++)
  1413 		{
  1414 			if ((*s_iter).compare(library) == 0)
  1415 			{
  1416 				return true;
  1417 			}
  1418 		}
  1419 	}
  1420 
  1421 	return false;
  1422 }
  1423 
  1424 IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
  1425 											const char *filepath,
  1426 											const char *filename,
  1427 											char *error,
  1428 											size_t maxlength)
  1429 {
  1430 	IExtension *pAlready;
  1431 	if ((pAlready=FindExtensionByFile(filename)) != NULL)
  1432 	{
  1433 		return pAlready;
  1434 	}
  1435 
  1436 	CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
  1437 
  1438 	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
  1439 	{
  1440 		pExt->Unload();
  1441 		delete pExt;
  1442 		return NULL;
  1443 	}
  1444 
  1445 	m_Libs.push_back(pExt);
  1446 
  1447 	return pExt;
  1448 }
  1449 
  1450 void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
  1451 {
  1452 	IExtensionInterface *pAPI;
  1453 	List<CExtension *>::iterator iter;
  1454 
  1455 	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1456 	{
  1457 		if ((pAPI = (*iter)->GetAPI()) == NULL)
  1458 		{
  1459 			continue;
  1460 		}
  1461 		if (pAPI->GetExtensionVersion() > 3)
  1462 		{
  1463 			pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
  1464 		}
  1465 	}
  1466 }
  1467 
  1468 bool CLocalExtension::IsSameFile(const char *file)
  1469 {
  1470 	/* Only care about the shortened name. */
  1471 	return strcmp(file, m_File.c_str()) == 0;
  1472 }
  1473 
  1474 bool CRemoteExtension::IsSameFile(const char *file)
  1475 {
  1476 	/* :TODO: this could be better, but no one uses this API anyway. */
  1477 	return strcmp(file, m_Path.c_str()) == 0;
  1478 }
  1479