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