core/systems/ExtensionSys.cpp
changeset 1952 b7aa2b626807
child 1993 8c08aaff4d39
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/systems/ExtensionSys.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,1371 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include <stdlib.h>
    1.36 +#include "ExtensionSys.h"
    1.37 +#include "LibrarySys.h"
    1.38 +#include "ShareSys.h"
    1.39 +#include "Logger.h"
    1.40 +#include "sourcemm_api.h"
    1.41 +#include "sm_srvcmds.h"
    1.42 +#include "sm_stringutil.h"
    1.43 +
    1.44 +CExtensionManager g_Extensions;
    1.45 +IdentityType_t g_ExtType;
    1.46 +
    1.47 +void CExtension::Initialize(const char *filename, const char *path)
    1.48 +{
    1.49 +	m_pAPI = NULL;
    1.50 +	m_pIdentToken = NULL;
    1.51 +	unload_code = 0;
    1.52 +	m_bFullyLoaded = false;
    1.53 +	m_File.assign(filename);
    1.54 +	m_Path.assign(path);
    1.55 +}
    1.56 +
    1.57 +CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
    1.58 +{
    1.59 +	Initialize(filename, path);
    1.60 +	m_pAPI = pAPI;
    1.61 +}
    1.62 +
    1.63 +CLocalExtension::CLocalExtension(const char *filename)
    1.64 +{
    1.65 +	m_PlId = 0;
    1.66 +	m_pLib = NULL;
    1.67 +
    1.68 +	char path[PLATFORM_MAX_PATH];
    1.69 +
    1.70 +	/* First see if there is an engine specific build! */
    1.71 +	g_SourceMod.BuildPath(Path_SM, 
    1.72 +		path, 
    1.73 +		PLATFORM_MAX_PATH, 
    1.74 +#if defined METAMOD_PLAPI_VERSION
    1.75 +#if defined ORANGEBOX_BUILD
    1.76 +		"extensions/auto.2.ep2/%s",
    1.77 +#else
    1.78 +		"extensions/auto.2.ep1/%s",
    1.79 +#endif //ORANGEBOX_BUILD
    1.80 +#else  //METAMOD_PLAPI_VERSION
    1.81 +		"extensions/auto.1.ep1/%s",
    1.82 +#endif //METAMOD_PLAPI_VERSION
    1.83 +		filename);
    1.84 +
    1.85 +	/* Try the "normal" version */
    1.86 +	if (!g_LibSys.IsPathFile(path))
    1.87 +	{
    1.88 +		g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
    1.89 +	}
    1.90 +
    1.91 +	Initialize(filename, path);
    1.92 +}
    1.93 +
    1.94 +bool CRemoteExtension::Load(char *error, size_t maxlength)
    1.95 +{
    1.96 +	if (!PerformAPICheck(error, maxlength))
    1.97 +	{
    1.98 +		m_pAPI = NULL;
    1.99 +		return false;
   1.100 +	}
   1.101 +
   1.102 +	if (!CExtension::Load(error, maxlength))
   1.103 +	{
   1.104 +		m_pAPI = NULL;
   1.105 +		return false;
   1.106 +	}
   1.107 +
   1.108 +	return true;
   1.109 +}
   1.110 +
   1.111 +bool CLocalExtension::Load(char *error, size_t maxlength)
   1.112 +{
   1.113 +	m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
   1.114 +
   1.115 +	if (m_pLib == NULL)
   1.116 +	{
   1.117 +		return false;
   1.118 +	}
   1.119 +
   1.120 +	typedef IExtensionInterface *(*GETAPI)();
   1.121 +	GETAPI pfnGetAPI = NULL;
   1.122 +
   1.123 +	if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
   1.124 +	{
   1.125 +		m_pLib->CloseLibrary();
   1.126 +		m_pLib = NULL;
   1.127 +		snprintf(error, maxlength, "Unable to find extension entry point");
   1.128 +		return false;
   1.129 +	}
   1.130 +
   1.131 +	m_pAPI = pfnGetAPI();
   1.132 +
   1.133 +	/* Check pointer and version */
   1.134 +	if (!PerformAPICheck(error, maxlength))
   1.135 +	{
   1.136 +		m_pLib->CloseLibrary();
   1.137 +		m_pLib = NULL;
   1.138 +		m_pAPI = NULL;
   1.139 +		return false;
   1.140 +	}
   1.141 +
   1.142 +	/* Load as MM:S */
   1.143 +	if (m_pAPI->IsMetamodExtension())
   1.144 +	{
   1.145 +		bool already;
   1.146 +		m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
   1.147 +	}
   1.148 +
   1.149 +	if (!CExtension::Load(error, maxlength))
   1.150 +	{
   1.151 +		if (m_pAPI->IsMetamodExtension())
   1.152 +		{
   1.153 +			if (m_PlId)
   1.154 +			{
   1.155 +				char dummy[255];
   1.156 +				g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
   1.157 +				m_PlId = 0;
   1.158 +			}
   1.159 +		}
   1.160 +		m_pLib->CloseLibrary();
   1.161 +		m_pLib = NULL;
   1.162 +		m_pAPI = NULL;
   1.163 +		return false;
   1.164 +	}
   1.165 +
   1.166 +	return true;
   1.167 +}
   1.168 +
   1.169 +void CRemoteExtension::Unload()
   1.170 +{
   1.171 +}
   1.172 +
   1.173 +void CLocalExtension::Unload()
   1.174 +{
   1.175 +	if (m_pAPI != NULL && m_PlId)
   1.176 +	{
   1.177 +		char temp_buffer[255];
   1.178 +		g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
   1.179 +		m_PlId = 0;
   1.180 +	}
   1.181 +
   1.182 +	if (m_pLib != NULL)
   1.183 +	{
   1.184 +		m_pLib->CloseLibrary();
   1.185 +		m_pLib = NULL;
   1.186 +	}
   1.187 +}
   1.188 +
   1.189 +bool CRemoteExtension::IsExternal()
   1.190 +{
   1.191 +	return true;
   1.192 +}
   1.193 +
   1.194 +bool CLocalExtension::IsExternal()
   1.195 +{
   1.196 +	return false;
   1.197 +}
   1.198 +
   1.199 +CExtension::~CExtension()
   1.200 +{
   1.201 +	DestroyIdentity();
   1.202 +}
   1.203 +
   1.204 +bool CExtension::PerformAPICheck(char *error, size_t maxlength)
   1.205 +{
   1.206 +	if (!m_pAPI)
   1.207 +	{
   1.208 +		snprintf(error, maxlength, "No IExtensionInterface instance provided");
   1.209 +		return false;
   1.210 +	}
   1.211 +	
   1.212 +	if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
   1.213 +	{
   1.214 +		snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
   1.215 +		return false;
   1.216 +	}
   1.217 +
   1.218 +	return true;
   1.219 +}
   1.220 +
   1.221 +bool CExtension::Load(char *error, size_t maxlength)
   1.222 +{
   1.223 +	CreateIdentity();
   1.224 +	if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
   1.225 +	{
   1.226 +		DestroyIdentity();
   1.227 +		return false;
   1.228 +	}
   1.229 +	else
   1.230 +	{
   1.231 +		/* Check if we're past load time */
   1.232 +		if (!g_SourceMod.IsMapLoading())
   1.233 +		{
   1.234 +			m_pAPI->OnExtensionsAllLoaded();
   1.235 +		}
   1.236 +	}
   1.237 +
   1.238 +	return true;
   1.239 +}
   1.240 +
   1.241 +void CExtension::CreateIdentity()
   1.242 +{
   1.243 +	if (m_pIdentToken != NULL)
   1.244 +	{
   1.245 +		return;
   1.246 +	}
   1.247 +
   1.248 +	m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
   1.249 +}
   1.250 +
   1.251 +void CExtension::DestroyIdentity()
   1.252 +{
   1.253 +	if (m_pIdentToken == NULL)
   1.254 +	{
   1.255 +		return;
   1.256 +	}
   1.257 +
   1.258 +	g_ShareSys.DestroyIdentity(m_pIdentToken);
   1.259 +	m_pIdentToken = NULL;
   1.260 +}
   1.261 +
   1.262 +void CExtension::MarkAllLoaded()
   1.263 +{
   1.264 +	assert(m_bFullyLoaded == false);
   1.265 +	if (!m_bFullyLoaded)
   1.266 +	{
   1.267 +		m_bFullyLoaded = true;
   1.268 +		m_pAPI->OnExtensionsAllLoaded();
   1.269 +	}
   1.270 +}
   1.271 +
   1.272 +void CExtension::AddPlugin(IPlugin *pPlugin)
   1.273 +{
   1.274 +	/* Unfortunately we have to do this :( */
   1.275 +	if (m_Plugins.find(pPlugin) != m_Plugins.end())
   1.276 +	{
   1.277 +		m_Plugins.push_back(pPlugin);
   1.278 +	}
   1.279 +}
   1.280 +
   1.281 +void CExtension::RemovePlugin(IPlugin *pPlugin)
   1.282 +{
   1.283 +	m_Plugins.remove(pPlugin);
   1.284 +
   1.285 +	List<WeakNative>::iterator iter = m_WeakNatives.begin();
   1.286 +	while (iter != m_WeakNatives.end())
   1.287 +	{
   1.288 +		if ((*iter).pl == pPlugin)
   1.289 +		{
   1.290 +			iter = m_WeakNatives.erase(iter);
   1.291 +		}
   1.292 +		else
   1.293 +		{
   1.294 +			iter++;
   1.295 +		}
   1.296 +	}
   1.297 +
   1.298 +	iter = m_ReplacedNatives.begin();
   1.299 +	while (iter != m_ReplacedNatives.end())
   1.300 +	{
   1.301 +		if ((*iter).pl == pPlugin)
   1.302 +		{
   1.303 +			iter = m_ReplacedNatives.erase(iter);
   1.304 +		}
   1.305 +		else
   1.306 +		{
   1.307 +			iter++;
   1.308 +		}
   1.309 +	}
   1.310 +}
   1.311 +
   1.312 +void CExtension::SetError(const char *error)
   1.313 +{
   1.314 +	m_Error.assign(error);
   1.315 +}
   1.316 +
   1.317 +IExtensionInterface *CExtension::GetAPI()
   1.318 +{
   1.319 +	return m_pAPI;
   1.320 +}
   1.321 +
   1.322 +const char *CExtension::GetFilename()
   1.323 +{
   1.324 +	return m_File.c_str();
   1.325 +}
   1.326 +
   1.327 +IdentityToken_t *CExtension::GetIdentity()
   1.328 +{
   1.329 +	return m_pIdentToken;
   1.330 +}
   1.331 +
   1.332 +bool CRemoteExtension::IsLoaded()
   1.333 +{
   1.334 +	return (m_pAPI != NULL);
   1.335 +}
   1.336 +
   1.337 +bool CLocalExtension::IsLoaded()
   1.338 +{
   1.339 +	return (m_pLib != NULL);
   1.340 +}
   1.341 +
   1.342 +void CExtension::AddDependency(IfaceInfo *pInfo)
   1.343 +{
   1.344 +	if (m_Deps.find(*pInfo) == m_Deps.end())
   1.345 +	{
   1.346 +		m_Deps.push_back(*pInfo);
   1.347 +	}
   1.348 +}
   1.349 +
   1.350 +bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
   1.351 +{
   1.352 +	return (i1.iface == i2.iface) && (i1.owner == i2.owner);
   1.353 +}
   1.354 +
   1.355 +void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
   1.356 +{
   1.357 +	IfaceInfo info;
   1.358 +	info.iface = iface;
   1.359 +	info.owner = pOther;
   1.360 +
   1.361 +	List<IfaceInfo>::iterator iter;
   1.362 +	for (iter = m_ChildDeps.begin();
   1.363 +		 iter != m_ChildDeps.end();
   1.364 +		 iter++)
   1.365 +	{
   1.366 +		IfaceInfo &other = (*iter);
   1.367 +		if (other == info)
   1.368 +		{
   1.369 +			return;
   1.370 +		}
   1.371 +	}
   1.372 +
   1.373 +	m_ChildDeps.push_back(info);
   1.374 +}
   1.375 +
   1.376 +ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
   1.377 +{
   1.378 +	List<IfaceInfo>::iterator iter = m_Deps.begin();
   1.379 +
   1.380 +	if (iter == m_Deps.end())
   1.381 +	{
   1.382 +		return NULL;
   1.383 +	}
   1.384 +
   1.385 +	if (pOwner)
   1.386 +	{
   1.387 +		*pOwner = (*iter).owner;
   1.388 +	}
   1.389 +	if (pInterface)
   1.390 +	{
   1.391 +		*pInterface = (*iter).iface;
   1.392 +	}
   1.393 +
   1.394 +	List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
   1.395 +
   1.396 +	return (ITERATOR *)pIter;
   1.397 +}
   1.398 +
   1.399 +bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
   1.400 +{
   1.401 +	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   1.402 +	List<IfaceInfo>::iterator _iter;
   1.403 +
   1.404 +	if (_iter == m_Deps.end())
   1.405 +	{
   1.406 +		return false;
   1.407 +	}
   1.408 +
   1.409 +	_iter++;
   1.410 +
   1.411 +	if (pOwner)
   1.412 +	{
   1.413 +		*pOwner = (*_iter).owner;
   1.414 +	}
   1.415 +	if (pInterface)
   1.416 +	{
   1.417 +		*pInterface = (*_iter).iface;
   1.418 +	}
   1.419 +
   1.420 +	*pIter = _iter;
   1.421 +
   1.422 +	if (_iter == m_Deps.end())
   1.423 +	{
   1.424 +		return false;
   1.425 +	}
   1.426 +
   1.427 +	return true;
   1.428 +}
   1.429 +
   1.430 +void CExtension::FreeDependencyIterator(ITERATOR *iter)
   1.431 +{
   1.432 +	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   1.433 +
   1.434 +	delete pIter;
   1.435 +}
   1.436 +
   1.437 +void CExtension::AddInterface(SMInterface *pInterface)
   1.438 +{
   1.439 +	m_Interfaces.push_back(pInterface);
   1.440 +}
   1.441 +
   1.442 +bool CExtension::IsRunning(char *error, size_t maxlength)
   1.443 +{
   1.444 +	if (!IsLoaded())
   1.445 +	{
   1.446 +		if (error)
   1.447 +		{
   1.448 +			snprintf(error, maxlength, "%s", m_Error.c_str());
   1.449 +		}
   1.450 +		return false;
   1.451 +	}
   1.452 +
   1.453 +	return m_pAPI->QueryRunning(error, maxlength);
   1.454 +}
   1.455 +
   1.456 +void CExtension::AddLibrary(const char *library)
   1.457 +{
   1.458 +	m_Libraries.push_back(library);
   1.459 +}
   1.460 +
   1.461 +/*********************
   1.462 + * EXTENSION MANAGER *
   1.463 + *********************/
   1.464 +
   1.465 +void CExtensionManager::OnSourceModAllInitialized()
   1.466 +{
   1.467 +	g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
   1.468 +	g_PluginSys.AddPluginsListener(this);
   1.469 +	g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
   1.470 +	g_ShareSys.AddInterface(NULL, this);
   1.471 +}
   1.472 +
   1.473 +void CExtensionManager::OnSourceModShutdown()
   1.474 +{
   1.475 +	g_RootMenu.RemoveRootConsoleCommand("exts", this);
   1.476 +	g_ShareSys.DestroyIdentType(g_ExtType);
   1.477 +}
   1.478 +
   1.479 +void CExtensionManager::Shutdown()
   1.480 +{
   1.481 +	List<CExtension *>::iterator iter;
   1.482 +
   1.483 +	while ((iter = m_Libs.begin()) != m_Libs.end())
   1.484 +	{
   1.485 +		UnloadExtension((*iter));
   1.486 +	}
   1.487 +}
   1.488 +
   1.489 +void CExtensionManager::TryAutoload()
   1.490 +{
   1.491 +	char path[PLATFORM_MAX_PATH];
   1.492 +
   1.493 +	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
   1.494 +
   1.495 +	IDirectory *pDir = g_LibSys.OpenDirectory(path);
   1.496 +	if (!pDir)
   1.497 +	{
   1.498 +		return;
   1.499 +	}
   1.500 +
   1.501 +	const char *lfile;
   1.502 +	size_t len;
   1.503 +	while (pDir->MoreFiles())
   1.504 +	{
   1.505 +		if (pDir->IsEntryDirectory())
   1.506 +		{
   1.507 +			pDir->NextEntry();
   1.508 +			continue;
   1.509 +		}
   1.510 +
   1.511 +		lfile = pDir->GetEntryName();
   1.512 +		len = strlen(lfile);
   1.513 +		if (len <= 9) /* size of ".autoload" */
   1.514 +		{
   1.515 +			pDir->NextEntry();
   1.516 +			continue;
   1.517 +		}
   1.518 +
   1.519 +		if (strcmp(&lfile[len - 9], ".autoload") != 0)
   1.520 +		{
   1.521 +			pDir->NextEntry();
   1.522 +			continue;
   1.523 +		}
   1.524 +
   1.525 +		char file[PLATFORM_MAX_PATH];
   1.526 +		len = UTIL_Format(file, sizeof(file), "%s", lfile);
   1.527 +		strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
   1.528 +
   1.529 +		LoadAutoExtension(file);
   1.530 +		
   1.531 +		pDir->NextEntry();
   1.532 +	}
   1.533 +}
   1.534 +
   1.535 +IExtension *CExtensionManager::LoadAutoExtension(const char *path)
   1.536 +{
   1.537 +	if (!strstr(path, "." PLATFORM_LIB_EXT))
   1.538 +	{
   1.539 +		char newpath[PLATFORM_MAX_PATH];
   1.540 +		g_LibSys.PathFormat(newpath, sizeof(newpath), "%s.%s", path, PLATFORM_LIB_EXT);
   1.541 +		return LoadAutoExtension(newpath);
   1.542 +	}
   1.543 +
   1.544 +	IExtension *pAlready;
   1.545 +	if ((pAlready=FindExtensionByFile(path)) != NULL)
   1.546 +	{
   1.547 +		return pAlready;
   1.548 +	}
   1.549 +
   1.550 +	char error[256];
   1.551 +	CExtension *p = new CLocalExtension(path);
   1.552 +
   1.553 +	/* We put us in the list beforehand so extensions that check for each other
   1.554 +	 * won't recursively load each other.
   1.555 +	 */
   1.556 +	m_Libs.push_back(p);
   1.557 +
   1.558 +	if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
   1.559 +	{
   1.560 +		g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
   1.561 +		p->SetError(error);
   1.562 +	}
   1.563 +
   1.564 +	return p;
   1.565 +}
   1.566 +
   1.567 +IExtension *CExtensionManager::FindExtensionByFile(const char *file)
   1.568 +{
   1.569 +	List<CExtension *>::iterator iter;
   1.570 +	CExtension *pExt;
   1.571 +
   1.572 +	/* Chomp off the path */
   1.573 +	char lookup[PLATFORM_MAX_PATH];
   1.574 +	g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
   1.575 +
   1.576 +	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.577 +	{
   1.578 +		pExt = (*iter);
   1.579 +		char short_file[PLATFORM_MAX_PATH];
   1.580 +		g_LibSys.GetFileFromPath(short_file, sizeof(short_file), pExt->GetFilename());
   1.581 +		if (strcmp(lookup, short_file) == 0)
   1.582 +		{
   1.583 +			return pExt;
   1.584 +		}
   1.585 +	}
   1.586 +
   1.587 +	/* If we got no results, test if there was a platform extension.
   1.588 +	 * If not, add one.
   1.589 +	 */
   1.590 +	if (!strstr(file, "." PLATFORM_LIB_EXT))
   1.591 +	{
   1.592 +		char path[PLATFORM_MAX_PATH];
   1.593 +		UTIL_Format(path, sizeof(path), "%s.%s", file, PLATFORM_LIB_EXT);
   1.594 +		return FindExtensionByFile(path);
   1.595 +	}
   1.596 +
   1.597 +	return NULL;
   1.598 +}
   1.599 +
   1.600 +IExtension *CExtensionManager::FindExtensionByName(const char *ext)
   1.601 +{
   1.602 +	List<CExtension *>::iterator iter;
   1.603 +	CExtension *pExt;
   1.604 +	IExtensionInterface *pAPI;
   1.605 +	const char *name;
   1.606 +
   1.607 +	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.608 +	{
   1.609 +		pExt = (*iter);
   1.610 +		if (!pExt->IsLoaded())
   1.611 +		{
   1.612 +			continue;
   1.613 +		}
   1.614 +		if ((pAPI = pExt->GetAPI()) == NULL)
   1.615 +		{
   1.616 +			continue;
   1.617 +		}
   1.618 +		name = pAPI->GetExtensionName();
   1.619 +		if (!name)
   1.620 +		{
   1.621 +			continue;
   1.622 +		}
   1.623 +		if (strcmp(name, ext) == 0)
   1.624 +		{
   1.625 +			return pExt;
   1.626 +		}
   1.627 +	}
   1.628 +
   1.629 +	return NULL;
   1.630 +}
   1.631 +
   1.632 +IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
   1.633 +{
   1.634 +	IExtension *pAlready;
   1.635 +	if ((pAlready=FindExtensionByFile(file)) != NULL)
   1.636 +	{
   1.637 +		return pAlready;
   1.638 +	}
   1.639 +
   1.640 +	CExtension *pExt = new CLocalExtension(file);
   1.641 +
   1.642 +	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
   1.643 +	{
   1.644 +		pExt->Unload();
   1.645 +		delete pExt;
   1.646 +		return NULL;
   1.647 +	}
   1.648 +
   1.649 +	/* :TODO: do QueryRunning check if the map is loaded */
   1.650 +
   1.651 +	m_Libs.push_back(pExt);
   1.652 +
   1.653 +	return pExt;
   1.654 +}
   1.655 +
   1.656 +void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
   1.657 +{
   1.658 +	CExtension *pExt = (CExtension *)pRequester;
   1.659 +	CExtension *pOwner = (CExtension *)pInfo->owner;
   1.660 +
   1.661 +	pExt->AddDependency(pInfo);
   1.662 +
   1.663 +	IExtensionInterface *pAPI = pExt->GetAPI();
   1.664 +	if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
   1.665 +	{
   1.666 +		pOwner->AddChildDependent(pExt, pInfo->iface);
   1.667 +	}
   1.668 +}
   1.669 +
   1.670 +void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
   1.671 +{
   1.672 +	CExtension *pExt = (CExtension *)pOwner;
   1.673 +
   1.674 +	pExt->AddInterface(pInterface);
   1.675 +}
   1.676 +
   1.677 +void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
   1.678 +{
   1.679 +	CExtension *pExt = (CExtension *)pParent;
   1.680 +
   1.681 +	pExt->AddPlugin(pPlugin);
   1.682 +}
   1.683 +
   1.684 +void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
   1.685 +{
   1.686 +	List<CExtension *>::iterator iter;
   1.687 +
   1.688 +	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.689 +	{
   1.690 +		(*iter)->RemovePlugin(plugin);
   1.691 +	}
   1.692 +}
   1.693 +
   1.694 +CExtension *CExtensionManager::FindByOrder(unsigned int num)
   1.695 +{
   1.696 +	if (num < 1 || num > m_Libs.size())
   1.697 +	{
   1.698 +		return NULL;
   1.699 +	}
   1.700 +
   1.701 +	List<CExtension *>::iterator iter = m_Libs.begin();
   1.702 +
   1.703 +	while (iter != m_Libs.end())
   1.704 +	{
   1.705 +		if (--num == 0)
   1.706 +		{
   1.707 +			return (*iter);
   1.708 +		}
   1.709 +		iter++;
   1.710 +	}
   1.711 +
   1.712 +	return NULL;
   1.713 +}
   1.714 +
   1.715 +void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
   1.716 +{
   1.717 +	IPluginContext *pContext = pPlugin->GetBaseContext();
   1.718 +
   1.719 +	List<CExtension *> exts;
   1.720 +
   1.721 +	uint32_t natives = pContext->GetNativesNum();
   1.722 +	sp_native_t *native;
   1.723 +	sm_extnative_t *x_native;
   1.724 +	sm_repnative_t *r_native;
   1.725 +	for (uint32_t i=0; i<natives; i++)
   1.726 +	{
   1.727 +		/* Make sure the native is valid */
   1.728 +		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
   1.729 +		{
   1.730 +			continue;
   1.731 +		}
   1.732 +
   1.733 +		/* Make sure the native is not already bound */
   1.734 +		if (native->status == SP_NATIVE_BOUND)
   1.735 +		{
   1.736 +			/* If it is bound, see if there is a replacement. */
   1.737 +			if ((r_native = m_RepNatives.retrieve(native->name)) == NULL)
   1.738 +			{
   1.739 +				continue;
   1.740 +			}
   1.741 +
   1.742 +			/* Rewrite the address.  Whee! */
   1.743 +			native->pfn = r_native->info.func;
   1.744 +
   1.745 +			/* Make sure this will unload safely */
   1.746 +			WeakNative wn((CPlugin *)pPlugin, i);
   1.747 +			r_native->owner->m_ReplacedNatives.push_back(wn);
   1.748 +
   1.749 +			continue;
   1.750 +		}
   1.751 +
   1.752 +		/* See if we've got this native in our cache */
   1.753 +		if ((x_native = m_ExtNatives.retrieve(native->name)) == NULL)
   1.754 +		{
   1.755 +			continue;
   1.756 +		}
   1.757 +		/* Try and bind it */
   1.758 +		if (pContext->BindNativeToIndex(i, x_native->info->func) != SP_ERROR_NONE)
   1.759 +		{
   1.760 +			continue;
   1.761 +		}
   1.762 +		/* See if it's optional */
   1.763 +		if (native->flags & SP_NTVFLAG_OPTIONAL)
   1.764 +		{
   1.765 +			WeakNative wkn = WeakNative((CPlugin *)pPlugin, i);
   1.766 +			x_native->owner->m_WeakNatives.push_back(wkn);
   1.767 +		}
   1.768 +		else if (exts.find(x_native->owner) == exts.end())
   1.769 +		{
   1.770 +			exts.push_back(x_native->owner);
   1.771 +		}
   1.772 +	}
   1.773 +
   1.774 +	List<CExtension *>::iterator iter;
   1.775 +	for (iter = exts.begin(); iter != exts.end(); iter++)
   1.776 +	{
   1.777 +		CExtension *pExt = (*iter);
   1.778 +		if (pExt->m_Plugins.find(pPlugin) != pExt->m_Plugins.end())
   1.779 +		{
   1.780 +			continue;
   1.781 +		}
   1.782 +		pExt->m_Plugins.push_back(pPlugin);
   1.783 +	}
   1.784 +}
   1.785 +
   1.786 +bool CExtensionManager::UnloadExtension(IExtension *_pExt)
   1.787 +{
   1.788 +	if (!_pExt)
   1.789 +	{
   1.790 +		return false;
   1.791 +	}
   1.792 +
   1.793 +	CExtension *pExt = (CExtension *)_pExt;
   1.794 +
   1.795 +	if (m_Libs.find(pExt) == m_Libs.end())
   1.796 +	{
   1.797 +		return false;
   1.798 +	}
   1.799 +
   1.800 +	/* First remove us from internal lists */
   1.801 +	g_ShareSys.RemoveInterfaces(_pExt);
   1.802 +	m_Libs.remove(pExt);
   1.803 +
   1.804 +	List<CExtension *> UnloadQueue;
   1.805 +
   1.806 +	/* Handle dependencies */
   1.807 +	if (pExt->IsLoaded())
   1.808 +	{
   1.809 +		/* Unload any dependent plugins */
   1.810 +		List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
   1.811 +		while (p_iter != pExt->m_Plugins.end())
   1.812 +		{
   1.813 +			/* We have to manually unlink ourselves here, since we're no longer being managed */
   1.814 +			g_PluginSys.UnloadPlugin((*p_iter));
   1.815 +			p_iter = pExt->m_Plugins.erase(p_iter);
   1.816 +		}
   1.817 +
   1.818 +		List<String>::iterator s_iter;
   1.819 +		for (s_iter = pExt->m_Libraries.begin();
   1.820 +			 s_iter != pExt->m_Libraries.end();
   1.821 +			 s_iter++)
   1.822 +		{
   1.823 +			g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
   1.824 +		}
   1.825 +
   1.826 +		/* Unbind weak natives */
   1.827 +		List<WeakNative>::iterator wkn_iter;
   1.828 +		for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++)
   1.829 +		{
   1.830 +			WeakNative & wkn = (*wkn_iter);
   1.831 +			sp_context_t *ctx = wkn.pl->GetContext();
   1.832 +			ctx->natives[wkn.idx].status = SP_NATIVE_UNBOUND;
   1.833 +		}
   1.834 +
   1.835 +		/* Unbind replacement natives, link them back to their originals */
   1.836 +		for (wkn_iter = pExt->m_ReplacedNatives.begin();
   1.837 +			 wkn_iter != pExt->m_ReplacedNatives.end();
   1.838 +			 wkn_iter++)
   1.839 +		{
   1.840 +			WeakNative & wkn = (*wkn_iter);
   1.841 +			sp_context_t *ctx = wkn.pl->GetContext();
   1.842 +			sm_repnative_t *r_native = m_RepNatives.retrieve(ctx->natives[wkn.idx].name);
   1.843 +			if (r_native == NULL || ctx->natives[wkn.idx].pfn != r_native->info.func)
   1.844 +			{
   1.845 +				continue;
   1.846 +			}
   1.847 +			ctx->natives[wkn.idx].pfn = r_native->original;
   1.848 +		}
   1.849 +
   1.850 +		/* Notify and/or unload all dependencies */
   1.851 +		List<CExtension *>::iterator c_iter;
   1.852 +		CExtension *pDep;
   1.853 +		IExtensionInterface *pAPI;
   1.854 +		for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
   1.855 +		{
   1.856 +			pDep = (*c_iter);
   1.857 +			if ((pAPI=pDep->GetAPI()) == NULL)
   1.858 +			{
   1.859 +				continue;
   1.860 +			}
   1.861 +			if (pDep == pExt)
   1.862 +			{
   1.863 +				continue;
   1.864 +			}
   1.865 +			/* Now, get its dependency list */
   1.866 +			bool dropped = false;
   1.867 +			List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
   1.868 +			while (i_iter != pDep->m_Deps.end())
   1.869 +			{
   1.870 +				if ((*i_iter).owner == _pExt)
   1.871 +				{
   1.872 +					if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
   1.873 +					{
   1.874 +						if (!dropped)
   1.875 +						{
   1.876 +							dropped = true;
   1.877 +							UnloadQueue.push_back(pDep);
   1.878 +						}
   1.879 +					}
   1.880 +					pAPI->NotifyInterfaceDrop((*i_iter).iface);
   1.881 +					i_iter = pDep->m_Deps.erase(i_iter);
   1.882 +				}
   1.883 +				else
   1.884 +				{
   1.885 +					i_iter++;
   1.886 +				}
   1.887 +			}
   1.888 +			/* Flush out any back references to this plugin */
   1.889 +			i_iter = pDep->m_ChildDeps.begin();
   1.890 +			while (i_iter != pDep->m_ChildDeps.end())
   1.891 +			{
   1.892 +				if ((*i_iter).owner == pExt)
   1.893 +				{
   1.894 +					i_iter = pDep->m_ChildDeps.erase(i_iter);
   1.895 +				}
   1.896 +				else
   1.897 +				{
   1.898 +					i_iter++;
   1.899 +				}
   1.900 +			}
   1.901 +		}
   1.902 +
   1.903 +		/* Unbind our natives from Core */
   1.904 +		List<const sp_nativeinfo_t *>::iterator native_iter;
   1.905 +		for (native_iter = pExt->m_Natives.begin();
   1.906 +			 native_iter != pExt->m_Natives.end();
   1.907 +			 native_iter++)
   1.908 +		{
   1.909 +			const sp_nativeinfo_t *natives = (*native_iter);
   1.910 +			sm_extnative_t *x_native;
   1.911 +			for (unsigned int n = 0;
   1.912 +				 natives[n].name != NULL && natives[n].func != NULL;
   1.913 +				 n++)
   1.914 +			{
   1.915 +				x_native = m_ExtNatives.retrieve(natives[n].name);
   1.916 +				if (x_native && x_native->owner == pExt)
   1.917 +				{
   1.918 +					m_ExtNatives.remove(natives[n].name);
   1.919 +				}
   1.920 +			}
   1.921 +		}
   1.922 +
   1.923 +		/* Unbind our replacement natives */
   1.924 +		List<sm_repnative_t>::iterator rep_iter = m_RepNativeList.begin();
   1.925 +		while (rep_iter != m_RepNativeList.end())
   1.926 +		{
   1.927 +			sm_repnative_t & r_native = (*rep_iter);
   1.928 +			if (r_native.owner == pExt)
   1.929 +			{
   1.930 +				m_RepNatives.remove(r_native.info.name);
   1.931 +				rep_iter = m_RepNativeList.erase(rep_iter);
   1.932 +			}
   1.933 +			else
   1.934 +			{
   1.935 +				rep_iter++;
   1.936 +			}
   1.937 +		}
   1.938 +
   1.939 +		/* Tell it to unload */
   1.940 +		pAPI = pExt->GetAPI();
   1.941 +		pAPI->OnExtensionUnload();
   1.942 +	}
   1.943 +
   1.944 +	IdentityToken_t *pIdentity;
   1.945 +	if ((pIdentity = pExt->GetIdentity()) != NULL)
   1.946 +	{
   1.947 +		SMGlobalClass *glob = SMGlobalClass::head;
   1.948 +		while (glob)
   1.949 +		{
   1.950 +			glob->OnSourceModIdentityDropped(pIdentity);
   1.951 +			glob = glob->m_pGlobalClassNext;
   1.952 +		}
   1.953 +	}
   1.954 +
   1.955 +	pExt->Unload();
   1.956 +	delete pExt;
   1.957 +
   1.958 +	List<CExtension *>::iterator iter;
   1.959 +	for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
   1.960 +	{
   1.961 +		/* NOTE: This is safe because the unload function backs out of anything not present */
   1.962 +		UnloadExtension((*iter));
   1.963 +	}
   1.964 +
   1.965 +	return true;
   1.966 +}
   1.967 +
   1.968 +void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
   1.969 +{
   1.970 +	CExtension *pExt = (CExtension *)pOwner;
   1.971 +
   1.972 +	pExt->m_Natives.push_back(natives);
   1.973 +
   1.974 +	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
   1.975 +	{
   1.976 +		if (m_ExtNatives.retrieve(natives[i].name) != NULL)
   1.977 +		{
   1.978 +			continue;
   1.979 +		}
   1.980 +		sm_extnative_t x_native;
   1.981 +		x_native.info = &natives[i];
   1.982 +		x_native.owner = pExt;
   1.983 +		m_ExtNatives.insert(natives[i].name, x_native);
   1.984 +	}
   1.985 +}
   1.986 +
   1.987 +void CExtensionManager::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   1.988 +{
   1.989 +	SPVM_NATIVE_FUNC orig;
   1.990 +
   1.991 +	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
   1.992 +	{
   1.993 +		if ((orig = g_PluginSys.FindCoreNative(natives[i].name)) == NULL)
   1.994 +		{
   1.995 +			continue;
   1.996 +		}
   1.997 +		sm_repnative_t rep;
   1.998 +		rep.info = natives[i];
   1.999 +		rep.owner = (CExtension *)myself;
  1.1000 +		rep.original = orig;
  1.1001 +		m_RepNativeList.push_back(rep);
  1.1002 +		m_RepNatives.insert(natives[i].name, rep);
  1.1003 +	}
  1.1004 +}
  1.1005 +
  1.1006 +void CExtensionManager::MarkAllLoaded()
  1.1007 +{
  1.1008 +	List<CExtension *>::iterator iter;
  1.1009 +	CExtension *pExt;
  1.1010 +
  1.1011 +	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1.1012 +	{
  1.1013 +		pExt = (*iter);
  1.1014 +		if (!pExt->IsLoaded())
  1.1015 +		{
  1.1016 +			continue;
  1.1017 +		}
  1.1018 +		if (pExt->m_bFullyLoaded)
  1.1019 +		{
  1.1020 +			continue;
  1.1021 +		}
  1.1022 +		pExt->MarkAllLoaded();
  1.1023 +	}
  1.1024 +}
  1.1025 +
  1.1026 +void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
  1.1027 +{
  1.1028 +	/* This function doesn't really need to do anything now.  We make sure the 
  1.1029 +	 * other extension is loaded, but handling of dependencies is really done 
  1.1030 +	 * by the interface fetcher.
  1.1031 +	 */
  1.1032 +	if (required || autoload)
  1.1033 +	{
  1.1034 +		LoadAutoExtension(file);
  1.1035 +	}
  1.1036 +}
  1.1037 +
  1.1038 +void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
  1.1039 +{
  1.1040 +	int argcount = command.ArgC();
  1.1041 +	if (argcount >= 3)
  1.1042 +	{
  1.1043 +		const char *cmd = command.Arg(2);
  1.1044 +		if (strcmp(cmd, "list") == 0)
  1.1045 +		{
  1.1046 +			List<CExtension *>::iterator iter;
  1.1047 +			CExtension *pExt;
  1.1048 +			unsigned int num = 1;
  1.1049 +			switch (m_Libs.size())
  1.1050 +			{
  1.1051 +			case 1:
  1.1052 +				{
  1.1053 +					g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
  1.1054 +					break;
  1.1055 +				}
  1.1056 +			case 0:
  1.1057 +				{
  1.1058 +					g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
  1.1059 +					break;
  1.1060 +				}
  1.1061 +			default:
  1.1062 +				{
  1.1063 +					g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
  1.1064 +					break;
  1.1065 +				}
  1.1066 +			}
  1.1067 +			for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
  1.1068 +			{
  1.1069 +				pExt = (*iter);
  1.1070 +				if (pExt->IsLoaded())
  1.1071 +				{
  1.1072 +					char error[255];
  1.1073 +					if (!pExt->IsRunning(error, sizeof(error)))
  1.1074 +					{
  1.1075 +						g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
  1.1076 +					}
  1.1077 +					else
  1.1078 +					{
  1.1079 +						IExtensionInterface *pAPI = pExt->GetAPI();
  1.1080 +						const char *name = pAPI->GetExtensionName();
  1.1081 +						const char *version = pAPI->GetExtensionVerString();
  1.1082 +						const char *descr = pAPI->GetExtensionDescription();
  1.1083 +						g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
  1.1084 +					}
  1.1085 +				} else {
  1.1086 +					g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
  1.1087 +				}
  1.1088 +			}
  1.1089 +			return;
  1.1090 +		}
  1.1091 +		else if (strcmp(cmd, "info") == 0)
  1.1092 +		{
  1.1093 +			if (argcount < 4)
  1.1094 +			{
  1.1095 +				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
  1.1096 +				return;
  1.1097 +			}
  1.1098 +
  1.1099 +			const char *sId = command.Arg(3);
  1.1100 +			unsigned int id = atoi(sId);
  1.1101 +			if (id <= 0)
  1.1102 +			{
  1.1103 +				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
  1.1104 +				return;
  1.1105 +			}
  1.1106 +
  1.1107 +			if (m_Libs.size() == 0)
  1.1108 +			{
  1.1109 +				g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
  1.1110 +				return;
  1.1111 +			}
  1.1112 +
  1.1113 +			if (id > m_Libs.size())
  1.1114 +			{
  1.1115 +				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1.1116 +				return;
  1.1117 +			}
  1.1118 +
  1.1119 +			List<CExtension *>::iterator iter = m_Libs.begin();
  1.1120 +			CExtension *pExt = NULL;
  1.1121 +			while (iter != m_Libs.end())
  1.1122 +			{
  1.1123 +				if (--id == 0)
  1.1124 +				{
  1.1125 +					pExt = (*iter);
  1.1126 +					break;
  1.1127 +				}
  1.1128 +				iter++;
  1.1129 +			}
  1.1130 +			/* This should never happen */
  1.1131 +			if (!pExt)
  1.1132 +			{
  1.1133 +				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1.1134 +				return;
  1.1135 +			}
  1.1136 +
  1.1137 +			if (!pExt->IsLoaded())
  1.1138 +			{
  1.1139 +				g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1140 +				g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
  1.1141 +			}
  1.1142 +			else
  1.1143 +			{
  1.1144 +				char error[255];
  1.1145 +				if (!pExt->IsRunning(error, sizeof(error)))
  1.1146 +				{
  1.1147 +					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1148 +					g_RootMenu.ConsolePrint(" Loaded: Yes");
  1.1149 +					g_RootMenu.ConsolePrint(" Running: No (%s)", error);
  1.1150 +				}
  1.1151 +				else
  1.1152 +				{
  1.1153 +					IExtensionInterface *pAPI = pExt->GetAPI();
  1.1154 +					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1155 +					g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
  1.1156 +					g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
  1.1157 +					g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
  1.1158 +					g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
  1.1159 +
  1.1160 +					if (pExt->IsExternal())
  1.1161 +					{
  1.1162 +						g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
  1.1163 +					}
  1.1164 +					else if (pAPI->IsMetamodExtension())
  1.1165 +					{
  1.1166 +						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
  1.1167 +					}
  1.1168 +					else
  1.1169 +					{
  1.1170 +						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
  1.1171 +					}
  1.1172 +				}
  1.1173 +			}
  1.1174 +			return;
  1.1175 +		}
  1.1176 +		else if (strcmp(cmd, "unload") == 0)
  1.1177 +		{
  1.1178 +			if (argcount < 4)
  1.1179 +			{
  1.1180 +				g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
  1.1181 +				return;
  1.1182 +			}
  1.1183 +
  1.1184 +			const char *arg = command.Arg(3);
  1.1185 +			unsigned int num = atoi(arg);
  1.1186 +			CExtension *pExt = FindByOrder(num);
  1.1187 +
  1.1188 +			if (!pExt)
  1.1189 +			{
  1.1190 +				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
  1.1191 +				return;
  1.1192 +			}
  1.1193 +
  1.1194 +			if (argcount > 4 && pExt->unload_code)
  1.1195 +			{
  1.1196 +				const char *unload = command.Arg(4);
  1.1197 +				if (pExt->unload_code == (unsigned)atoi(unload))
  1.1198 +				{
  1.1199 +					char filename[PLATFORM_MAX_PATH];
  1.1200 +					snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1.1201 +					UnloadExtension(pExt);
  1.1202 +					g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1.1203 +				}
  1.1204 +				else
  1.1205 +				{
  1.1206 +					g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
  1.1207 +				}
  1.1208 +				return;
  1.1209 +			}
  1.1210 +
  1.1211 +			if (!pExt->IsLoaded() 
  1.1212 +				|| (!pExt->m_ChildDeps.size() && !pExt->m_Plugins.size()))
  1.1213 +			{
  1.1214 +				char filename[PLATFORM_MAX_PATH];
  1.1215 +				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1.1216 +				UnloadExtension(pExt);
  1.1217 +				g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1.1218 +				return;
  1.1219 +			}
  1.1220 +			else
  1.1221 +			{
  1.1222 +				List<IPlugin *> plugins;
  1.1223 +				if (pExt->m_ChildDeps.size())
  1.1224 +				{
  1.1225 +					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
  1.1226 +					List<CExtension *>::iterator iter;
  1.1227 +					CExtension *pOther;
  1.1228 +					/* Get list of all extensions */
  1.1229 +					for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1.1230 +					{
  1.1231 +						List<IfaceInfo>::iterator i_iter;
  1.1232 +						pOther = (*iter);
  1.1233 +						if (!pOther->IsLoaded() || pOther == pExt)
  1.1234 +						{
  1.1235 +							continue;
  1.1236 +						}
  1.1237 +						/* Get their dependencies */
  1.1238 +						for (i_iter=pOther->m_Deps.begin();
  1.1239 +							 i_iter!=pOther->m_Deps.end();
  1.1240 +							 i_iter++)
  1.1241 +						{
  1.1242 +							/* Is this dependency to us? */
  1.1243 +							if ((*i_iter).owner != pExt)
  1.1244 +							{
  1.1245 +								continue;
  1.1246 +							}
  1.1247 +							/* Will our dependent care? */
  1.1248 +							if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
  1.1249 +							{
  1.1250 +								g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
  1.1251 +								/* Add to plugin unload list */
  1.1252 +								List<IPlugin *>::iterator p_iter;
  1.1253 +								for (p_iter=pOther->m_Plugins.begin();
  1.1254 +									 p_iter!=pOther->m_Plugins.end();
  1.1255 +									 p_iter++)
  1.1256 +								{
  1.1257 +									if (plugins.find((*p_iter)) == plugins.end())
  1.1258 +									{
  1.1259 +										plugins.push_back((*p_iter));
  1.1260 +									}
  1.1261 +								}
  1.1262 +							}
  1.1263 +						}
  1.1264 +					}
  1.1265 +				}
  1.1266 +				if (pExt->m_Plugins.size())
  1.1267 +				{
  1.1268 +					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
  1.1269 +					List<IPlugin *>::iterator iter;
  1.1270 +					IPlugin *pPlugin;
  1.1271 +					for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
  1.1272 +					{
  1.1273 +						pPlugin = (*iter);
  1.1274 +						if (plugins.find(pPlugin) == plugins.end())
  1.1275 +						{
  1.1276 +							plugins.push_back(pPlugin);
  1.1277 +						}
  1.1278 +					}
  1.1279 +					for (iter = plugins.begin(); iter != plugins.end(); iter++)
  1.1280 +					{
  1.1281 +						pPlugin = (*iter);
  1.1282 +						g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
  1.1283 +					}
  1.1284 +				}
  1.1285 +				srand(static_cast<int>(time(NULL)));
  1.1286 +				pExt->unload_code = (rand() % 877) + 123;	//123 to 999
  1.1287 +				g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
  1.1288 +				g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
  1.1289 +
  1.1290 +				return;
  1.1291 +			}
  1.1292 +		}
  1.1293 +	}
  1.1294 +
  1.1295 +	g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
  1.1296 +	g_RootMenu.DrawGenericOption("info", "Extra extension information");
  1.1297 +	g_RootMenu.DrawGenericOption("list", "List extensions");
  1.1298 +	g_RootMenu.DrawGenericOption("unload", "Unload an extension");
  1.1299 +}
  1.1300 +
  1.1301 +CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
  1.1302 +{
  1.1303 +	if (ptr->type == g_ExtType)
  1.1304 +	{
  1.1305 +		return (CExtension *)(ptr->ptr);
  1.1306 +	}
  1.1307 +
  1.1308 +	return NULL;
  1.1309 +}
  1.1310 +
  1.1311 +CExtensionManager::CExtensionManager()
  1.1312 +{
  1.1313 +}
  1.1314 +
  1.1315 +CExtensionManager::~CExtensionManager()
  1.1316 +{
  1.1317 +}
  1.1318 +
  1.1319 +void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
  1.1320 +{
  1.1321 +	CExtension *pExt = (CExtension *)pSource;
  1.1322 +	pExt->AddLibrary(library);
  1.1323 +	g_PluginSys.OnLibraryAction(library, false, false);
  1.1324 +}
  1.1325 +
  1.1326 +bool CExtensionManager::LibraryExists(const char *library)
  1.1327 +{
  1.1328 +	CExtension *pExt;
  1.1329 +
  1.1330 +	for (List<CExtension *>::iterator iter = m_Libs.begin();
  1.1331 +		 iter != m_Libs.end();
  1.1332 +		 iter++)
  1.1333 +	{
  1.1334 +		pExt = (*iter);
  1.1335 +		for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
  1.1336 +			 s_iter != pExt->m_Libraries.end();
  1.1337 +			 s_iter++)
  1.1338 +		{
  1.1339 +			if ((*s_iter).compare(library) == 0)
  1.1340 +			{
  1.1341 +				return true;
  1.1342 +			}
  1.1343 +		}
  1.1344 +	}
  1.1345 +
  1.1346 +	return false;
  1.1347 +}
  1.1348 +
  1.1349 +IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
  1.1350 +											const char *filepath,
  1.1351 +											const char *filename,
  1.1352 +											char *error,
  1.1353 +											size_t maxlength)
  1.1354 +{
  1.1355 +	IExtension *pAlready;
  1.1356 +	if ((pAlready=FindExtensionByFile(filename)) != NULL)
  1.1357 +	{
  1.1358 +		return pAlready;
  1.1359 +	}
  1.1360 +
  1.1361 +	CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
  1.1362 +
  1.1363 +	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
  1.1364 +	{
  1.1365 +		pExt->Unload();
  1.1366 +		delete pExt;
  1.1367 +		return NULL;
  1.1368 +	}
  1.1369 +
  1.1370 +	m_Libs.push_back(pExt);
  1.1371 +
  1.1372 +	return pExt;
  1.1373 +}
  1.1374 +