core/ExtensionSys.cpp
changeset 3872 4c93f74cae51
parent 3871 4edce76aefe3
child 3873 a496ff7c2aad
     1.1 --- a/core/ExtensionSys.cpp	Fri Mar 29 12:28:02 2013 -0400
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1487 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 sw=4 tw=99 noet :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2010 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 -#include "PlayerManager.h"
    1.44 -
    1.45 -CExtensionManager g_Extensions;
    1.46 -IdentityType_t g_ExtType;
    1.47 -
    1.48 -void CExtension::Initialize(const char *filename, const char *path)
    1.49 -{
    1.50 -	m_pAPI = NULL;
    1.51 -	m_pIdentToken = NULL;
    1.52 -	unload_code = 0;
    1.53 -	m_bFullyLoaded = false;
    1.54 -	m_File.assign(filename);
    1.55 -	m_Path.assign(path);
    1.56 -	char real_name[PLATFORM_MAX_PATH];
    1.57 -	g_LibSys.GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
    1.58 -	m_RealFile.assign(real_name);
    1.59 -}
    1.60 -
    1.61 -CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
    1.62 -{
    1.63 -	Initialize(filename, path);
    1.64 -	m_pAPI = pAPI;
    1.65 -}
    1.66 -
    1.67 -#if defined METAMOD_PLAPI_VERSION
    1.68 -#if SOURCE_ENGINE == SE_LEFT4DEAD
    1.69 -#define GAMEFIX "2.l4d"
    1.70 -#elif SOURCE_ENGINE == SE_LEFT4DEAD2
    1.71 -#define GAMEFIX "2.l4d2"
    1.72 -#elif SOURCE_ENGINE == SE_ALIENSWARM
    1.73 -#define GAMEFIX "2.swarm"
    1.74 -#elif SOURCE_ENGINE == SE_ORANGEBOX
    1.75 -#define GAMEFIX "2.ep2"
    1.76 -#elif SOURCE_ENGINE == SE_BLOODYGOODTIME
    1.77 -#define GAMEFIX "2.bgt"
    1.78 -#elif SOURCE_ENGINE == SE_EYE
    1.79 -#define GAMEFIX "2.eye"
    1.80 -#elif SOURCE_ENGINE == SE_CSS
    1.81 -#define GAMEFIX "2.css"
    1.82 -#elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
    1.83 -#define GAMEFIX "2.ep2v"
    1.84 -#elif SOURCE_ENGINE == SE_DARKMESSIAH
    1.85 -#define GAMEFIX "2.darkm"
    1.86 -#elif SOURCE_ENGINE == SE_PORTAL2
    1.87 -#define GAMEFIX "2.portal2"
    1.88 -#elif SOURCE_ENGINE == SE_CSGO
    1.89 -#define GAMEFIX "2.csgo"
    1.90 -#elif SOURCE_ENGINE == SE_DOTA
    1.91 -#define GAMEFIX "2.dota"
    1.92 -#else
    1.93 -#define GAMEFIX "2.ep1"
    1.94 -#endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
    1.95 -#else  //METAMOD_PLAPI_VERSION
    1.96 -#define GAMEFIX "1.ep1"
    1.97 -#endif //METAMOD_PLAPI_VERSION
    1.98 -
    1.99 -CLocalExtension::CLocalExtension(const char *filename)
   1.100 -{
   1.101 -	m_PlId = 0;
   1.102 -	m_pLib = NULL;
   1.103 -
   1.104 -	char path[PLATFORM_MAX_PATH];
   1.105 -
   1.106 -	/* Special case for new bintools binary */
   1.107 -	if (strcmp(filename, "bintools.ext") == 0)
   1.108 -	{
   1.109 -		goto normal;
   1.110 -	}
   1.111 -
   1.112 -	/* Zeroth, see if there is an engine specific build in the new place. */
   1.113 -	g_SourceMod.BuildPath(Path_SM,
   1.114 -		path,
   1.115 -		PLATFORM_MAX_PATH,
   1.116 -		"extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
   1.117 -		filename);
   1.118 -
   1.119 -	if (g_LibSys.IsPathFile(path))
   1.120 -	{
   1.121 -		goto found;
   1.122 -	}
   1.123 -
   1.124 -	/* First see if there is an engine specific build! */
   1.125 -	g_SourceMod.BuildPath(Path_SM, 
   1.126 -		path, 
   1.127 -		PLATFORM_MAX_PATH,
   1.128 -		"extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
   1.129 -		filename);
   1.130 -
   1.131 -	/* Try the "normal" version */
   1.132 -	if (!g_LibSys.IsPathFile(path))
   1.133 -	{
   1.134 -		normal:
   1.135 -		g_SourceMod.BuildPath(Path_SM,
   1.136 -			path,
   1.137 -			PLATFORM_MAX_PATH,
   1.138 -			"extensions/%s." PLATFORM_LIB_EXT,
   1.139 -			filename);
   1.140 -	}
   1.141 -
   1.142 -found:
   1.143 -	Initialize(filename, path);
   1.144 -}
   1.145 -
   1.146 -bool CRemoteExtension::Load(char *error, size_t maxlength)
   1.147 -{
   1.148 -	if (!PerformAPICheck(error, maxlength))
   1.149 -	{
   1.150 -		m_pAPI = NULL;
   1.151 -		return false;
   1.152 -	}
   1.153 -
   1.154 -	if (!CExtension::Load(error, maxlength))
   1.155 -	{
   1.156 -		m_pAPI = NULL;
   1.157 -		return false;
   1.158 -	}
   1.159 -
   1.160 -	return true;
   1.161 -}
   1.162 -
   1.163 -bool CLocalExtension::Load(char *error, size_t maxlength)
   1.164 -{
   1.165 -	m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
   1.166 -
   1.167 -	if (m_pLib == NULL)
   1.168 -	{
   1.169 -		return false;
   1.170 -	}
   1.171 -
   1.172 -	typedef IExtensionInterface *(*GETAPI)();
   1.173 -	GETAPI pfnGetAPI = NULL;
   1.174 -
   1.175 -	if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
   1.176 -	{
   1.177 -		m_pLib->CloseLibrary();
   1.178 -		m_pLib = NULL;
   1.179 -		snprintf(error, maxlength, "Unable to find extension entry point");
   1.180 -		return false;
   1.181 -	}
   1.182 -
   1.183 -	m_pAPI = pfnGetAPI();
   1.184 -
   1.185 -	/* Check pointer and version */
   1.186 -	if (!PerformAPICheck(error, maxlength))
   1.187 -	{
   1.188 -		m_pLib->CloseLibrary();
   1.189 -		m_pLib = NULL;
   1.190 -		m_pAPI = NULL;
   1.191 -		return false;
   1.192 -	}
   1.193 -
   1.194 -	/* Load as MM:S */
   1.195 -	if (m_pAPI->IsMetamodExtension())
   1.196 -	{
   1.197 -		bool already;
   1.198 -		m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
   1.199 -
   1.200 -		// Check the plugin didn't refuse load
   1.201 -		Pl_Status status;
   1.202 -		
   1.203 -#ifndef METAMOD_PLAPI_VERSION
   1.204 -		const char *file;
   1.205 -		PluginId source;
   1.206 -#endif
   1.207 -		
   1.208 -		if (!m_PlId || (
   1.209 -#ifndef METAMOD_PLAPI_VERSION
   1.210 -			g_pMMPlugins->Query(m_PlId, file, status, source)
   1.211 -#else
   1.212 -			g_pMMPlugins->Query(m_PlId, NULL, &status, NULL)
   1.213 -#endif			
   1.214 -			&& status < Pl_Paused))
   1.215 -		{
   1.216 -			m_pLib->CloseLibrary();
   1.217 -			m_pLib = NULL;
   1.218 -			m_pAPI = NULL;
   1.219 -			return false;
   1.220 -		}
   1.221 -	}
   1.222 -
   1.223 -	if (!CExtension::Load(error, maxlength))
   1.224 -	{
   1.225 -		if (m_pAPI->IsMetamodExtension())
   1.226 -		{
   1.227 -			if (m_PlId)
   1.228 -			{
   1.229 -				char dummy[255];
   1.230 -				g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
   1.231 -				m_PlId = 0;
   1.232 -			}
   1.233 -		}
   1.234 -		m_pLib->CloseLibrary();
   1.235 -		m_pLib = NULL;
   1.236 -		m_pAPI = NULL;
   1.237 -		return false;
   1.238 -	}
   1.239 -
   1.240 -	return true;
   1.241 -}
   1.242 -
   1.243 -void CRemoteExtension::Unload()
   1.244 -{
   1.245 -}
   1.246 -
   1.247 -void CLocalExtension::Unload()
   1.248 -{
   1.249 -	if (m_pAPI != NULL && m_PlId)
   1.250 -	{
   1.251 -		char temp_buffer[255];
   1.252 -		g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
   1.253 -		m_PlId = 0;
   1.254 -	}
   1.255 -
   1.256 -	if (m_pLib != NULL)
   1.257 -	{
   1.258 -		m_pLib->CloseLibrary();
   1.259 -		m_pLib = NULL;
   1.260 -	}
   1.261 -}
   1.262 -
   1.263 -bool CRemoteExtension::Reload(char *error, size_t maxlength)
   1.264 -{
   1.265 -	snprintf(error, maxlength, "Remote extensions do not support reloading");
   1.266 -	return false;
   1.267 -}
   1.268 -
   1.269 -bool CLocalExtension::Reload(char *error, size_t maxlength)
   1.270 -{
   1.271 -	if (m_pLib == NULL) // FIXME: just load it instead?
   1.272 -		return false;
   1.273 -	
   1.274 -	m_pAPI->OnExtensionUnload();
   1.275 -	Unload();
   1.276 -	
   1.277 -	return Load(error, maxlength);
   1.278 -}
   1.279 -
   1.280 -bool CRemoteExtension::IsExternal()
   1.281 -{
   1.282 -	return true;
   1.283 -}
   1.284 -
   1.285 -bool CLocalExtension::IsExternal()
   1.286 -{
   1.287 -	return false;
   1.288 -}
   1.289 -
   1.290 -CExtension::~CExtension()
   1.291 -{
   1.292 -	DestroyIdentity();
   1.293 -}
   1.294 -
   1.295 -bool CExtension::PerformAPICheck(char *error, size_t maxlength)
   1.296 -{
   1.297 -	if (!m_pAPI)
   1.298 -	{
   1.299 -		snprintf(error, maxlength, "No IExtensionInterface instance provided");
   1.300 -		return false;
   1.301 -	}
   1.302 -	
   1.303 -	if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
   1.304 -	{
   1.305 -		snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
   1.306 -		return false;
   1.307 -	}
   1.308 -
   1.309 -	return true;
   1.310 -}
   1.311 -
   1.312 -bool CExtension::Load(char *error, size_t maxlength)
   1.313 -{
   1.314 -	CreateIdentity();
   1.315 -	if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
   1.316 -	{
   1.317 -		DestroyIdentity();
   1.318 -		return false;
   1.319 -	}
   1.320 -	else
   1.321 -	{
   1.322 -		/* Check if we're past load time */
   1.323 -		if (!g_SourceMod.IsMapLoading())
   1.324 -		{
   1.325 -			m_pAPI->OnExtensionsAllLoaded();
   1.326 -		}
   1.327 -	}
   1.328 -
   1.329 -	return true;
   1.330 -}
   1.331 -
   1.332 -void CExtension::CreateIdentity()
   1.333 -{
   1.334 -	if (m_pIdentToken != NULL)
   1.335 -	{
   1.336 -		return;
   1.337 -	}
   1.338 -
   1.339 -	m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
   1.340 -}
   1.341 -
   1.342 -void CExtension::DestroyIdentity()
   1.343 -{
   1.344 -	if (m_pIdentToken == NULL)
   1.345 -	{
   1.346 -		return;
   1.347 -	}
   1.348 -
   1.349 -	g_ShareSys.DestroyIdentity(m_pIdentToken);
   1.350 -	m_pIdentToken = NULL;
   1.351 -}
   1.352 -
   1.353 -void CExtension::MarkAllLoaded()
   1.354 -{
   1.355 -	assert(m_bFullyLoaded == false);
   1.356 -	if (!m_bFullyLoaded)
   1.357 -	{
   1.358 -		m_bFullyLoaded = true;
   1.359 -		m_pAPI->OnExtensionsAllLoaded();
   1.360 -	}
   1.361 -}
   1.362 -
   1.363 -void CExtension::AddPlugin(CPlugin *pPlugin)
   1.364 -{
   1.365 -	/* Unfortunately we have to do this :( */
   1.366 -	if (m_Dependents.find(pPlugin) != m_Dependents.end())
   1.367 -	{
   1.368 -		m_Dependents.push_back(pPlugin);
   1.369 -	}
   1.370 -}
   1.371 -
   1.372 -void CExtension::SetError(const char *error)
   1.373 -{
   1.374 -	m_Error.assign(error);
   1.375 -}
   1.376 -
   1.377 -IExtensionInterface *CExtension::GetAPI()
   1.378 -{
   1.379 -	return m_pAPI;
   1.380 -}
   1.381 -
   1.382 -const char *CExtension::GetFilename()
   1.383 -{
   1.384 -	return m_RealFile.c_str();
   1.385 -}
   1.386 -
   1.387 -const char *CExtension::GetPath() const
   1.388 -{
   1.389 -	return m_Path.c_str();
   1.390 -}
   1.391 -
   1.392 -IdentityToken_t *CExtension::GetIdentity()
   1.393 -{
   1.394 -	return m_pIdentToken;
   1.395 -}
   1.396 -
   1.397 -bool CRemoteExtension::IsLoaded()
   1.398 -{
   1.399 -	return (m_pAPI != NULL);
   1.400 -}
   1.401 -
   1.402 -bool CLocalExtension::IsLoaded()
   1.403 -{
   1.404 -	return (m_pLib != NULL);
   1.405 -}
   1.406 -
   1.407 -void CExtension::AddDependency(const IfaceInfo *pInfo)
   1.408 -{
   1.409 -	if (m_Deps.find(*pInfo) == m_Deps.end())
   1.410 -	{
   1.411 -		m_Deps.push_back(*pInfo);
   1.412 -	}
   1.413 -}
   1.414 -
   1.415 -bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
   1.416 -{
   1.417 -	return (i1.iface == i2.iface) && (i1.owner == i2.owner);
   1.418 -}
   1.419 -
   1.420 -void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
   1.421 -{
   1.422 -	IfaceInfo info;
   1.423 -	info.iface = iface;
   1.424 -	info.owner = pOther;
   1.425 -
   1.426 -	List<IfaceInfo>::iterator iter;
   1.427 -	for (iter = m_ChildDeps.begin();
   1.428 -		 iter != m_ChildDeps.end();
   1.429 -		 iter++)
   1.430 -	{
   1.431 -		IfaceInfo &other = (*iter);
   1.432 -		if (other == info)
   1.433 -		{
   1.434 -			return;
   1.435 -		}
   1.436 -	}
   1.437 -
   1.438 -	m_ChildDeps.push_back(info);
   1.439 -}
   1.440 -
   1.441 -ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
   1.442 -{
   1.443 -	List<IfaceInfo>::iterator iter = m_Deps.begin();
   1.444 -
   1.445 -	if (iter == m_Deps.end())
   1.446 -	{
   1.447 -		return NULL;
   1.448 -	}
   1.449 -
   1.450 -	if (pOwner)
   1.451 -	{
   1.452 -		*pOwner = (*iter).owner;
   1.453 -	}
   1.454 -	if (pInterface)
   1.455 -	{
   1.456 -		*pInterface = (*iter).iface;
   1.457 -	}
   1.458 -
   1.459 -	List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
   1.460 -
   1.461 -	return (ITERATOR *)pIter;
   1.462 -}
   1.463 -
   1.464 -bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
   1.465 -{
   1.466 -	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   1.467 -	List<IfaceInfo>::iterator _iter;
   1.468 -
   1.469 -	if (_iter == m_Deps.end())
   1.470 -	{
   1.471 -		return false;
   1.472 -	}
   1.473 -
   1.474 -	_iter++;
   1.475 -
   1.476 -	if (pOwner)
   1.477 -	{
   1.478 -		*pOwner = (*_iter).owner;
   1.479 -	}
   1.480 -	if (pInterface)
   1.481 -	{
   1.482 -		*pInterface = (*_iter).iface;
   1.483 -	}
   1.484 -
   1.485 -	*pIter = _iter;
   1.486 -
   1.487 -	if (_iter == m_Deps.end())
   1.488 -	{
   1.489 -		return false;
   1.490 -	}
   1.491 -
   1.492 -	return true;
   1.493 -}
   1.494 -
   1.495 -void CExtension::FreeDependencyIterator(ITERATOR *iter)
   1.496 -{
   1.497 -	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
   1.498 -
   1.499 -	delete pIter;
   1.500 -}
   1.501 -
   1.502 -void CExtension::AddInterface(SMInterface *pInterface)
   1.503 -{
   1.504 -	m_Interfaces.push_back(pInterface);
   1.505 -}
   1.506 -
   1.507 -bool CExtension::IsRunning(char *error, size_t maxlength)
   1.508 -{
   1.509 -	if (!IsLoaded())
   1.510 -	{
   1.511 -		if (error)
   1.512 -		{
   1.513 -			snprintf(error, maxlength, "%s", m_Error.c_str());
   1.514 -		}
   1.515 -		return false;
   1.516 -	}
   1.517 -
   1.518 -	return m_pAPI->QueryRunning(error, maxlength);
   1.519 -}
   1.520 -
   1.521 -void CExtension::AddLibrary(const char *library)
   1.522 -{
   1.523 -	m_Libraries.push_back(library);
   1.524 -}
   1.525 -
   1.526 -/*********************
   1.527 - * EXTENSION MANAGER *
   1.528 - *********************/
   1.529 -
   1.530 -void CExtensionManager::OnSourceModAllInitialized()
   1.531 -{
   1.532 -	g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
   1.533 -	g_PluginSys.AddPluginsListener(this);
   1.534 -	g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
   1.535 -	g_ShareSys.AddInterface(NULL, this);
   1.536 -}
   1.537 -
   1.538 -void CExtensionManager::OnSourceModShutdown()
   1.539 -{
   1.540 -	g_RootMenu.RemoveRootConsoleCommand("exts", this);
   1.541 -	g_ShareSys.DestroyIdentType(g_ExtType);
   1.542 -}
   1.543 -
   1.544 -void CExtensionManager::Shutdown()
   1.545 -{
   1.546 -	List<CExtension *>::iterator iter;
   1.547 -
   1.548 -	while ((iter = m_Libs.begin()) != m_Libs.end())
   1.549 -	{
   1.550 -		UnloadExtension((*iter));
   1.551 -	}
   1.552 -}
   1.553 -
   1.554 -void CExtensionManager::TryAutoload()
   1.555 -{
   1.556 -	char path[PLATFORM_MAX_PATH];
   1.557 -
   1.558 -	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
   1.559 -
   1.560 -	IDirectory *pDir = g_LibSys.OpenDirectory(path);
   1.561 -	if (!pDir)
   1.562 -	{
   1.563 -		return;
   1.564 -	}
   1.565 -
   1.566 -	const char *lfile;
   1.567 -	size_t len;
   1.568 -	while (pDir->MoreFiles())
   1.569 -	{
   1.570 -		if (pDir->IsEntryDirectory())
   1.571 -		{
   1.572 -			pDir->NextEntry();
   1.573 -			continue;
   1.574 -		}
   1.575 -
   1.576 -		lfile = pDir->GetEntryName();
   1.577 -		len = strlen(lfile);
   1.578 -		if (len <= 9) /* size of ".autoload" */
   1.579 -		{
   1.580 -			pDir->NextEntry();
   1.581 -			continue;
   1.582 -		}
   1.583 -
   1.584 -		if (strcmp(&lfile[len - 9], ".autoload") != 0)
   1.585 -		{
   1.586 -			pDir->NextEntry();
   1.587 -			continue;
   1.588 -		}
   1.589 -
   1.590 -		char file[PLATFORM_MAX_PATH];
   1.591 -		len = UTIL_Format(file, sizeof(file), "%s", lfile);
   1.592 -		strcpy(&file[len - 9], ".ext");
   1.593 -
   1.594 -		LoadAutoExtension(file);
   1.595 -		
   1.596 -		pDir->NextEntry();
   1.597 -	}
   1.598 -}
   1.599 -
   1.600 -IExtension *CExtensionManager::LoadAutoExtension(const char *path, bool bErrorOnMissing)
   1.601 -{
   1.602 -	/* Remove platform extension if it's there. Compat hack. */
   1.603 -	const char *ext = g_LibSys.GetFileExtension(path);
   1.604 -	if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
   1.605 -	{
   1.606 -		char path2[PLATFORM_MAX_PATH];
   1.607 -		UTIL_Format(path2, sizeof(path2), "%s", path);
   1.608 -		path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
   1.609 -		return LoadAutoExtension(path2, bErrorOnMissing);
   1.610 -	}
   1.611 -
   1.612 -	IExtension *pAlready;
   1.613 -	if ((pAlready=FindExtensionByFile(path)) != NULL)
   1.614 -	{
   1.615 -		return pAlready;
   1.616 -	}
   1.617 -
   1.618 -	char error[256];
   1.619 -	CExtension *p = new CLocalExtension(path);
   1.620 -
   1.621 -	/* We put us in the list beforehand so extensions that check for each other
   1.622 -	 * won't recursively load each other.
   1.623 -	 */
   1.624 -	m_Libs.push_back(p);
   1.625 -
   1.626 -	if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
   1.627 -	{
   1.628 -		if (bErrorOnMissing || g_LibSys.IsPathFile(p->GetPath()))
   1.629 -		{
   1.630 -			g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
   1.631 -		}
   1.632 -
   1.633 -		p->SetError(error);
   1.634 -	}
   1.635 -
   1.636 -	return p;
   1.637 -}
   1.638 -
   1.639 -IExtension *CExtensionManager::FindExtensionByFile(const char *file)
   1.640 -{
   1.641 -	List<CExtension *>::iterator iter;
   1.642 -	CExtension *pExt;
   1.643 -
   1.644 -	/* Chomp off the path */
   1.645 -	char lookup[PLATFORM_MAX_PATH];
   1.646 -	g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
   1.647 -
   1.648 -	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.649 -	{
   1.650 -		pExt = (*iter);
   1.651 -		if (pExt->IsSameFile(lookup))
   1.652 -		{
   1.653 -			return pExt;
   1.654 -		}
   1.655 -	}
   1.656 -
   1.657 -	return NULL;
   1.658 -}
   1.659 -
   1.660 -IExtension *CExtensionManager::FindExtensionByName(const char *ext)
   1.661 -{
   1.662 -	List<CExtension *>::iterator iter;
   1.663 -	CExtension *pExt;
   1.664 -	IExtensionInterface *pAPI;
   1.665 -	const char *name;
   1.666 -
   1.667 -	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.668 -	{
   1.669 -		pExt = (*iter);
   1.670 -		if (!pExt->IsLoaded())
   1.671 -		{
   1.672 -			continue;
   1.673 -		}
   1.674 -		if ((pAPI = pExt->GetAPI()) == NULL)
   1.675 -		{
   1.676 -			continue;
   1.677 -		}
   1.678 -		name = pAPI->GetExtensionName();
   1.679 -		if (!name)
   1.680 -		{
   1.681 -			continue;
   1.682 -		}
   1.683 -		if (strcmp(name, ext) == 0)
   1.684 -		{
   1.685 -			return pExt;
   1.686 -		}
   1.687 -	}
   1.688 -
   1.689 -	return NULL;
   1.690 -}
   1.691 -
   1.692 -IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
   1.693 -{
   1.694 -	/* Remove platform extension if it's there. Compat hack. */
   1.695 -	const char *ext = g_LibSys.GetFileExtension(file);
   1.696 -	if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
   1.697 -	{
   1.698 -		char path2[PLATFORM_MAX_PATH];
   1.699 -		UTIL_Format(path2, sizeof(path2), "%s", file);
   1.700 -		path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
   1.701 -		return LoadExtension(path2, error, maxlength);
   1.702 -	}
   1.703 -
   1.704 -	IExtension *pAlready;
   1.705 -	if ((pAlready=FindExtensionByFile(file)) != NULL)
   1.706 -	{
   1.707 -		return pAlready;
   1.708 -	}
   1.709 -
   1.710 -	CExtension *pExt = new CLocalExtension(file);
   1.711 -
   1.712 -	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
   1.713 -	{
   1.714 -		pExt->Unload();
   1.715 -		delete pExt;
   1.716 -		return NULL;
   1.717 -	}
   1.718 -
   1.719 -	/* :TODO: do QueryRunning check if the map is loaded */
   1.720 -
   1.721 -	m_Libs.push_back(pExt);
   1.722 -
   1.723 -	return pExt;
   1.724 -}
   1.725 -
   1.726 -void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
   1.727 -{
   1.728 -	CExtension *pExt = (CExtension *)pRequester;
   1.729 -	CExtension *pOwner = (CExtension *)pInfo->owner;
   1.730 -
   1.731 -	pExt->AddDependency(pInfo);
   1.732 -
   1.733 -	IExtensionInterface *pAPI = pExt->GetAPI();
   1.734 -	if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
   1.735 -	{
   1.736 -		pOwner->AddChildDependent(pExt, pInfo->iface);
   1.737 -	}
   1.738 -}
   1.739 -
   1.740 -void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
   1.741 -{
   1.742 -	CExtension *pExt = (CExtension *)ext;
   1.743 -	CExtension *pOwner = GetExtensionFromIdent(other);
   1.744 -
   1.745 -	IfaceInfo info;
   1.746 -
   1.747 -	info.iface = (SMInterface *)iface;
   1.748 -	info.owner = pOwner;
   1.749 -
   1.750 -	pExt->AddDependency(&info);
   1.751 -	pOwner->AddChildDependent(pExt, (SMInterface *)iface);
   1.752 -}
   1.753 -
   1.754 -void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
   1.755 -{
   1.756 -	CExtension *pExt = (CExtension *)pOwner;
   1.757 -
   1.758 -	pExt->AddInterface(pInterface);
   1.759 -}
   1.760 -
   1.761 -void CExtensionManager::BindChildPlugin( IExtension *pParent, CPlugin *pPlugin )
   1.762 -{
   1.763 -	CExtension *pExt = (CExtension *)pParent;
   1.764 -
   1.765 -	pExt->AddPlugin(pPlugin);
   1.766 -}
   1.767 -
   1.768 -void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
   1.769 -{
   1.770 -	List<CExtension *>::iterator iter;
   1.771 -
   1.772 -	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.773 -	{
   1.774 -		(*iter)->DropRefsTo((CPlugin *)plugin);
   1.775 -	}
   1.776 -}
   1.777 -
   1.778 -CExtension *CExtensionManager::FindByOrder(unsigned int num)
   1.779 -{
   1.780 -	if (num < 1 || num > m_Libs.size())
   1.781 -	{
   1.782 -		return NULL;
   1.783 -	}
   1.784 -
   1.785 -	List<CExtension *>::iterator iter = m_Libs.begin();
   1.786 -
   1.787 -	while (iter != m_Libs.end())
   1.788 -	{
   1.789 -		if (--num == 0)
   1.790 -		{
   1.791 -			return (*iter);
   1.792 -		}
   1.793 -		iter++;
   1.794 -	}
   1.795 -
   1.796 -	return NULL;
   1.797 -}
   1.798 -
   1.799 -bool CExtensionManager::UnloadExtension(IExtension *_pExt)
   1.800 -{
   1.801 -	if (!_pExt)
   1.802 -	{
   1.803 -		return false;
   1.804 -	}
   1.805 -
   1.806 -	CExtension *pExt = (CExtension *)_pExt;
   1.807 -
   1.808 -	if (m_Libs.find(pExt) == m_Libs.end())
   1.809 -	{
   1.810 -		return false;
   1.811 -	}
   1.812 -
   1.813 -	/* Tell it to unload */
   1.814 -	if (pExt->IsLoaded())
   1.815 -	{
   1.816 -		IExtensionInterface *pAPI = pExt->GetAPI();
   1.817 -		pAPI->OnExtensionUnload();
   1.818 -	}
   1.819 -
   1.820 -	/* First remove us from internal lists */
   1.821 -	g_ShareSys.RemoveInterfaces(_pExt);
   1.822 -	m_Libs.remove(pExt);
   1.823 -
   1.824 -	List<CExtension *> UnloadQueue;
   1.825 -
   1.826 -	/* Handle dependencies */
   1.827 -	if (pExt->IsLoaded())
   1.828 -	{
   1.829 -		/* Unload any dependent plugins */
   1.830 -		List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
   1.831 -		while (p_iter != pExt->m_Dependents.end())
   1.832 -		{
   1.833 -			/* We have to manually unlink ourselves here, since we're no longer being managed */
   1.834 -			g_PluginSys.UnloadPlugin((*p_iter));
   1.835 -			p_iter = pExt->m_Dependents.erase(p_iter);
   1.836 -		}
   1.837 -
   1.838 -		List<String>::iterator s_iter;
   1.839 -		for (s_iter = pExt->m_Libraries.begin();
   1.840 -			 s_iter != pExt->m_Libraries.end();
   1.841 -			 s_iter++)
   1.842 -		{
   1.843 -			g_PluginSys.OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
   1.844 -		}
   1.845 -
   1.846 -		/* Notify and/or unload all dependencies */
   1.847 -		List<CExtension *>::iterator c_iter;
   1.848 -		CExtension *pDep;
   1.849 -		IExtensionInterface *pAPI;
   1.850 -		for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
   1.851 -		{
   1.852 -			pDep = (*c_iter);
   1.853 -			if ((pAPI=pDep->GetAPI()) == NULL)
   1.854 -			{
   1.855 -				continue;
   1.856 -			}
   1.857 -			if (pDep == pExt)
   1.858 -			{
   1.859 -				continue;
   1.860 -			}
   1.861 -			/* Now, get its dependency list */
   1.862 -			bool dropped = false;
   1.863 -			List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
   1.864 -			while (i_iter != pDep->m_Deps.end())
   1.865 -			{
   1.866 -				if ((*i_iter).owner == _pExt)
   1.867 -				{
   1.868 -					if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
   1.869 -					{
   1.870 -						if (!dropped)
   1.871 -						{
   1.872 -							dropped = true;
   1.873 -							UnloadQueue.push_back(pDep);
   1.874 -						}
   1.875 -					}
   1.876 -					pAPI->NotifyInterfaceDrop((*i_iter).iface);
   1.877 -					i_iter = pDep->m_Deps.erase(i_iter);
   1.878 -				}
   1.879 -				else
   1.880 -				{
   1.881 -					i_iter++;
   1.882 -				}
   1.883 -			}
   1.884 -			/* Flush out any back references to this plugin */
   1.885 -			i_iter = pDep->m_ChildDeps.begin();
   1.886 -			while (i_iter != pDep->m_ChildDeps.end())
   1.887 -			{
   1.888 -				if ((*i_iter).owner == pExt)
   1.889 -				{
   1.890 -					i_iter = pDep->m_ChildDeps.erase(i_iter);
   1.891 -				}
   1.892 -				else
   1.893 -				{
   1.894 -					i_iter++;
   1.895 -				}
   1.896 -			}
   1.897 -		}
   1.898 -
   1.899 -		/* Unbind our natives from Core */
   1.900 -		pExt->DropEverything();
   1.901 -	}
   1.902 -
   1.903 -	IdentityToken_t *pIdentity;
   1.904 -	if ((pIdentity = pExt->GetIdentity()) != NULL)
   1.905 -	{
   1.906 -		SMGlobalClass *glob = SMGlobalClass::head;
   1.907 -		while (glob)
   1.908 -		{
   1.909 -			glob->OnSourceModIdentityDropped(pIdentity);
   1.910 -			glob = glob->m_pGlobalClassNext;
   1.911 -		}
   1.912 -	}
   1.913 -
   1.914 -	pExt->Unload();
   1.915 -	delete pExt;
   1.916 -
   1.917 -	List<CExtension *>::iterator iter;
   1.918 -	for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
   1.919 -	{
   1.920 -		/* NOTE: This is safe because the unload function backs out of anything not present */
   1.921 -		UnloadExtension((*iter));
   1.922 -	}
   1.923 -
   1.924 -	return true;
   1.925 -}
   1.926 -
   1.927 -void CExtensionManager::MarkAllLoaded()
   1.928 -{
   1.929 -	List<CExtension *>::iterator iter;
   1.930 -	CExtension *pExt;
   1.931 -
   1.932 -	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
   1.933 -	{
   1.934 -		pExt = (*iter);
   1.935 -		if (!pExt->IsLoaded())
   1.936 -		{
   1.937 -			continue;
   1.938 -		}
   1.939 -		if (pExt->m_bFullyLoaded)
   1.940 -		{
   1.941 -			continue;
   1.942 -		}
   1.943 -		pExt->MarkAllLoaded();
   1.944 -	}
   1.945 -}
   1.946 -
   1.947 -void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
   1.948 -{
   1.949 -	/* This function doesn't really need to do anything now.  We make sure the 
   1.950 -	 * other extension is loaded, but handling of dependencies is really done 
   1.951 -	 * by the interface fetcher.
   1.952 -	 */
   1.953 -	if (required || autoload)
   1.954 -	{
   1.955 -		LoadAutoExtension(file);
   1.956 -	}
   1.957 -}
   1.958 -
   1.959 -void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   1.960 -{
   1.961 -	int argcount = command.ArgC();
   1.962 -	if (argcount >= 3)
   1.963 -	{
   1.964 -		const char *cmd = command.Arg(2);
   1.965 -		if (strcmp(cmd, "list") == 0)
   1.966 -		{
   1.967 -			List<CExtension *>::iterator iter;
   1.968 -			CExtension *pExt;
   1.969 -			unsigned int num = 1;
   1.970 -			switch (m_Libs.size())
   1.971 -			{
   1.972 -			case 1:
   1.973 -				{
   1.974 -					g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
   1.975 -					break;
   1.976 -				}
   1.977 -			case 0:
   1.978 -				{
   1.979 -					g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
   1.980 -					break;
   1.981 -				}
   1.982 -			default:
   1.983 -				{
   1.984 -					g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
   1.985 -					break;
   1.986 -				}
   1.987 -			}
   1.988 -			for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
   1.989 -			{
   1.990 -				pExt = (*iter);
   1.991 -				if (pExt->IsLoaded())
   1.992 -				{
   1.993 -					char error[255];
   1.994 -					if (!pExt->IsRunning(error, sizeof(error)))
   1.995 -					{
   1.996 -						g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
   1.997 -					}
   1.998 -					else
   1.999 -					{
  1.1000 -						IExtensionInterface *pAPI = pExt->GetAPI();
  1.1001 -						const char *name = pAPI->GetExtensionName();
  1.1002 -						const char *version = pAPI->GetExtensionVerString();
  1.1003 -						const char *descr = pAPI->GetExtensionDescription();
  1.1004 -						g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
  1.1005 -					}
  1.1006 -				} else {
  1.1007 -					g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
  1.1008 -				}
  1.1009 -			}
  1.1010 -			return;
  1.1011 -		}
  1.1012 -		else if (strcmp(cmd, "load") == 0)
  1.1013 -		{
  1.1014 -			if (argcount < 4)
  1.1015 -			{
  1.1016 -				g_RootMenu.ConsolePrint("[SM] Usage: sm exts load <file>");
  1.1017 -				return;
  1.1018 -			}
  1.1019 -
  1.1020 -			const char *filename = command.Arg(3);
  1.1021 -			char path[PLATFORM_MAX_PATH];
  1.1022 -			char error[256];
  1.1023 -
  1.1024 -			UTIL_Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
  1.1025 -				!strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
  1.1026 -			
  1.1027 -			if (FindExtensionByFile(path) != NULL)
  1.1028 -			{
  1.1029 -				g_RootMenu.ConsolePrint("[SM] Extension %s is already loaded.", path);
  1.1030 -				return;
  1.1031 -			}
  1.1032 -			
  1.1033 -			if (LoadExtension(path, error, sizeof(error)))
  1.1034 -			{
  1.1035 -				g_RootMenu.ConsolePrint("[SM] Loaded extension %s successfully.", path);
  1.1036 -			} else
  1.1037 -			{
  1.1038 -				g_RootMenu.ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
  1.1039 -			}
  1.1040 -			
  1.1041 -			return;
  1.1042 -		}
  1.1043 -		else if (strcmp(cmd, "info") == 0)
  1.1044 -		{
  1.1045 -			if (argcount < 4)
  1.1046 -			{
  1.1047 -				g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
  1.1048 -				return;
  1.1049 -			}
  1.1050 -
  1.1051 -			const char *sId = command.Arg(3);
  1.1052 -			unsigned int id = atoi(sId);
  1.1053 -			if (id <= 0)
  1.1054 -			{
  1.1055 -				g_RootMenu.ConsolePrint("[SM] Usage: sm exts info <#>");
  1.1056 -				return;
  1.1057 -			}
  1.1058 -
  1.1059 -			if (m_Libs.size() == 0)
  1.1060 -			{
  1.1061 -				g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
  1.1062 -				return;
  1.1063 -			}
  1.1064 -
  1.1065 -			if (id > m_Libs.size())
  1.1066 -			{
  1.1067 -				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1.1068 -				return;
  1.1069 -			}
  1.1070 -
  1.1071 -			List<CExtension *>::iterator iter = m_Libs.begin();
  1.1072 -			CExtension *pExt = NULL;
  1.1073 -			while (iter != m_Libs.end())
  1.1074 -			{
  1.1075 -				if (--id == 0)
  1.1076 -				{
  1.1077 -					pExt = (*iter);
  1.1078 -					break;
  1.1079 -				}
  1.1080 -				iter++;
  1.1081 -			}
  1.1082 -			/* This should never happen */
  1.1083 -			if (!pExt)
  1.1084 -			{
  1.1085 -				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
  1.1086 -				return;
  1.1087 -			}
  1.1088 -
  1.1089 -			if (!pExt->IsLoaded())
  1.1090 -			{
  1.1091 -				g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1092 -				g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
  1.1093 -			}
  1.1094 -			else
  1.1095 -			{
  1.1096 -				char error[255];
  1.1097 -				if (!pExt->IsRunning(error, sizeof(error)))
  1.1098 -				{
  1.1099 -					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1100 -					g_RootMenu.ConsolePrint(" Loaded: Yes");
  1.1101 -					g_RootMenu.ConsolePrint(" Running: No (%s)", error);
  1.1102 -				}
  1.1103 -				else
  1.1104 -				{
  1.1105 -					IExtensionInterface *pAPI = pExt->GetAPI();
  1.1106 -					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
  1.1107 -					g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
  1.1108 -					g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
  1.1109 -					g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
  1.1110 -					g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
  1.1111 -
  1.1112 -					if (pExt->IsExternal())
  1.1113 -					{
  1.1114 -						g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
  1.1115 -					}
  1.1116 -					else if (pAPI->IsMetamodExtension())
  1.1117 -					{
  1.1118 -						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
  1.1119 -					}
  1.1120 -					else
  1.1121 -					{
  1.1122 -						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
  1.1123 -					}
  1.1124 -				}
  1.1125 -			}
  1.1126 -			return;
  1.1127 -		}
  1.1128 -		else if (strcmp(cmd, "unload") == 0)
  1.1129 -		{
  1.1130 -			if (argcount < 4)
  1.1131 -			{
  1.1132 -				g_RootMenu.ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
  1.1133 -				return;
  1.1134 -			}
  1.1135 -
  1.1136 -			const char *arg = command.Arg(3);
  1.1137 -			unsigned int num = atoi(arg);
  1.1138 -			CExtension *pExt = FindByOrder(num);
  1.1139 -
  1.1140 -			if (!pExt)
  1.1141 -			{
  1.1142 -				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
  1.1143 -				return;
  1.1144 -			}
  1.1145 -
  1.1146 -			if (argcount > 4 && pExt->unload_code)
  1.1147 -			{
  1.1148 -				const char *unload = command.Arg(4);
  1.1149 -				if (pExt->unload_code == (unsigned)atoi(unload))
  1.1150 -				{
  1.1151 -					char filename[PLATFORM_MAX_PATH];
  1.1152 -					snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1.1153 -					UnloadExtension(pExt);
  1.1154 -					g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1.1155 -				}
  1.1156 -				else
  1.1157 -				{
  1.1158 -					g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
  1.1159 -				}
  1.1160 -				return;
  1.1161 -			}
  1.1162 -
  1.1163 -			if (!pExt->IsLoaded() 
  1.1164 -				|| (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
  1.1165 -			{
  1.1166 -				char filename[PLATFORM_MAX_PATH];
  1.1167 -				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1.1168 -				UnloadExtension(pExt);
  1.1169 -				g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
  1.1170 -				return;
  1.1171 -			}
  1.1172 -			else
  1.1173 -			{
  1.1174 -				List<CPlugin *> plugins;
  1.1175 -				if (pExt->m_ChildDeps.size())
  1.1176 -				{
  1.1177 -					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
  1.1178 -					List<CExtension *>::iterator iter;
  1.1179 -					CExtension *pOther;
  1.1180 -					/* Get list of all extensions */
  1.1181 -					for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1.1182 -					{
  1.1183 -						List<IfaceInfo>::iterator i_iter;
  1.1184 -						pOther = (*iter);
  1.1185 -						if (!pOther->IsLoaded() || pOther == pExt)
  1.1186 -						{
  1.1187 -							continue;
  1.1188 -						}
  1.1189 -						/* Get their dependencies */
  1.1190 -						for (i_iter=pOther->m_Deps.begin();
  1.1191 -							 i_iter!=pOther->m_Deps.end();
  1.1192 -							 i_iter++)
  1.1193 -						{
  1.1194 -							/* Is this dependency to us? */
  1.1195 -							if ((*i_iter).owner != pExt)
  1.1196 -							{
  1.1197 -								continue;
  1.1198 -							}
  1.1199 -							/* Will our dependent care? */
  1.1200 -							if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
  1.1201 -							{
  1.1202 -								g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
  1.1203 -								/* Add to plugin unload list */
  1.1204 -								List<CPlugin *>::iterator p_iter;
  1.1205 -								for (p_iter=pOther->m_Dependents.begin();
  1.1206 -									 p_iter!=pOther->m_Dependents.end();
  1.1207 -									 p_iter++)
  1.1208 -								{
  1.1209 -									if (plugins.find((*p_iter)) == plugins.end())
  1.1210 -									{
  1.1211 -										plugins.push_back((*p_iter));
  1.1212 -									}
  1.1213 -								}
  1.1214 -							}
  1.1215 -						}
  1.1216 -					}
  1.1217 -				}
  1.1218 -				if (pExt->m_Dependents.size())
  1.1219 -				{
  1.1220 -					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
  1.1221 -					List<CPlugin *>::iterator iter;
  1.1222 -					CPlugin *pPlugin;
  1.1223 -					for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
  1.1224 -					{
  1.1225 -						pPlugin = (*iter);
  1.1226 -						if (plugins.find(pPlugin) == plugins.end())
  1.1227 -						{
  1.1228 -							plugins.push_back(pPlugin);
  1.1229 -						}
  1.1230 -					}
  1.1231 -					for (iter = plugins.begin(); iter != plugins.end(); iter++)
  1.1232 -					{
  1.1233 -						pPlugin = (*iter);
  1.1234 -						g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
  1.1235 -					}
  1.1236 -				}
  1.1237 -				srand(static_cast<int>(time(NULL)));
  1.1238 -				pExt->unload_code = (rand() % 877) + 123;	//123 to 999
  1.1239 -				g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
  1.1240 -				g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
  1.1241 -
  1.1242 -				return;
  1.1243 -			}
  1.1244 -		}
  1.1245 -		else if (strcmp(cmd, "reload") == 0)
  1.1246 -		{
  1.1247 -			if (argcount < 4)
  1.1248 -			{
  1.1249 -				g_RootMenu.ConsolePrint("[SM] Usage: sm exts reload <#>");
  1.1250 -				return;
  1.1251 -			}
  1.1252 -			
  1.1253 -			const char *arg = command.Arg(3);
  1.1254 -			unsigned int num = atoi(arg);
  1.1255 -			CExtension *pExt = FindByOrder(num);
  1.1256 -
  1.1257 -			if (!pExt)
  1.1258 -			{
  1.1259 -				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
  1.1260 -				return;
  1.1261 -			}
  1.1262 -			
  1.1263 -			if (pExt->IsLoaded())
  1.1264 -			{
  1.1265 -				char filename[PLATFORM_MAX_PATH];
  1.1266 -				char error[255];
  1.1267 -				
  1.1268 -				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
  1.1269 -				
  1.1270 -				if (pExt->Reload(error, sizeof(error)))
  1.1271 -				{
  1.1272 -					g_RootMenu.ConsolePrint("[SM] Extension %s is now reloaded.", filename);
  1.1273 -				}
  1.1274 -				else
  1.1275 -				{
  1.1276 -					g_RootMenu.ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
  1.1277 -				}
  1.1278 -					
  1.1279 -				return;
  1.1280 -			} 
  1.1281 -			else
  1.1282 -			{
  1.1283 -				g_RootMenu.ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
  1.1284 -				
  1.1285 -				return;
  1.1286 -			}
  1.1287 -			
  1.1288 -		}
  1.1289 -	}
  1.1290 -
  1.1291 -	g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
  1.1292 -	g_RootMenu.DrawGenericOption("info", "Extra extension information");
  1.1293 -	g_RootMenu.DrawGenericOption("list", "List extensions");
  1.1294 -	g_RootMenu.DrawGenericOption("load", "Load an extension");
  1.1295 -	g_RootMenu.DrawGenericOption("reload", "Reload an extension");
  1.1296 -	g_RootMenu.DrawGenericOption("unload", "Unload an extension");
  1.1297 -}
  1.1298 -
  1.1299 -void CExtensionManager::ListExtensionsToClient(CPlayer *player, const CCommand &args)
  1.1300 -{
  1.1301 -	char buffer[256];
  1.1302 -	int numExtensions = m_Libs.size();
  1.1303 -	edict_t *edict = player->GetEdict();
  1.1304 -	unsigned int id = 0;
  1.1305 -	unsigned int start = 0;
  1.1306 -
  1.1307 -	if (!numExtensions)
  1.1308 -	{
  1.1309 -		ClientConsolePrint(edict, "[SM] No extensions found.");
  1.1310 -		return;
  1.1311 -	}
  1.1312 -
  1.1313 -	if (args.ArgC() > 2)
  1.1314 -	{
  1.1315 -		start = atoi(args.Arg(2));
  1.1316 -	}
  1.1317 -
  1.1318 -	CExtension *ext;
  1.1319 -	SourceHook::List<CExtension *>::iterator iter;
  1.1320 -
  1.1321 -	for (iter = m_Libs.begin();
  1.1322 -		 iter != m_Libs.end();
  1.1323 -		 iter++)
  1.1324 -	{
  1.1325 -		ext = (*iter);
  1.1326 -
  1.1327 -		char error[255];
  1.1328 -		if (!ext->IsRunning(error, sizeof(error)))
  1.1329 -		{
  1.1330 -			continue;
  1.1331 -		}
  1.1332 -
  1.1333 -		id++;
  1.1334 -		if (id < start)
  1.1335 -		{
  1.1336 -			continue;
  1.1337 -		}
  1.1338 -
  1.1339 -		if (id - start > 10)
  1.1340 -		{
  1.1341 -			break;
  1.1342 -		}
  1.1343 -
  1.1344 -		IExtensionInterface *api = ext->GetAPI();
  1.1345 -
  1.1346 -		const char *name = api->GetExtensionName();
  1.1347 -		const char *version = api->GetExtensionVerString();
  1.1348 -		const char *author = api->GetExtensionAuthor();
  1.1349 -		const char *description = api->GetExtensionDescription();
  1.1350 -
  1.1351 -		size_t len = UTIL_Format(buffer, sizeof(buffer), " \"%s\"", name);
  1.1352 -
  1.1353 -		if (version != NULL && IS_STR_FILLED(version))
  1.1354 -		{
  1.1355 -			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", version);
  1.1356 -		}
  1.1357 -
  1.1358 -		if (author != NULL && IS_STR_FILLED(author))
  1.1359 -		{
  1.1360 -			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", author);
  1.1361 -		}
  1.1362 -
  1.1363 -		if (description != NULL && IS_STR_FILLED(description))
  1.1364 -		{
  1.1365 -			len += UTIL_Format(&buffer[len], sizeof(buffer)-len, ": %s", description);
  1.1366 -		}
  1.1367 -
  1.1368 -
  1.1369 -		ClientConsolePrint(edict, "%s", buffer);
  1.1370 -	}
  1.1371 -
  1.1372 -	while (iter != m_Libs.end())
  1.1373 -	{
  1.1374 -		char error[255];
  1.1375 -		if ((*iter)->IsRunning(error, sizeof(error)))
  1.1376 -		{
  1.1377 -			break;
  1.1378 -		}
  1.1379 -	}
  1.1380 -
  1.1381 -	if (iter != m_Libs.end())
  1.1382 -	{
  1.1383 -		ClientConsolePrint(edict, "To see more, type \"sm exts %d\"", id);
  1.1384 -	}
  1.1385 -}
  1.1386 -
  1.1387 -CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
  1.1388 -{
  1.1389 -	if (ptr->type == g_ExtType)
  1.1390 -	{
  1.1391 -		return (CExtension *)(ptr->ptr);
  1.1392 -	}
  1.1393 -
  1.1394 -	return NULL;
  1.1395 -}
  1.1396 -
  1.1397 -CExtensionManager::CExtensionManager()
  1.1398 -{
  1.1399 -}
  1.1400 -
  1.1401 -CExtensionManager::~CExtensionManager()
  1.1402 -{
  1.1403 -}
  1.1404 -
  1.1405 -void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
  1.1406 -{
  1.1407 -	CExtension *pExt = (CExtension *)pSource;
  1.1408 -	pExt->AddLibrary(library);
  1.1409 -	g_PluginSys.OnLibraryAction(library, LibraryAction_Added);
  1.1410 -}
  1.1411 -
  1.1412 -bool CExtensionManager::LibraryExists(const char *library)
  1.1413 -{
  1.1414 -	CExtension *pExt;
  1.1415 -
  1.1416 -	for (List<CExtension *>::iterator iter = m_Libs.begin();
  1.1417 -		 iter != m_Libs.end();
  1.1418 -		 iter++)
  1.1419 -	{
  1.1420 -		pExt = (*iter);
  1.1421 -		for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
  1.1422 -			 s_iter != pExt->m_Libraries.end();
  1.1423 -			 s_iter++)
  1.1424 -		{
  1.1425 -			if ((*s_iter).compare(library) == 0)
  1.1426 -			{
  1.1427 -				return true;
  1.1428 -			}
  1.1429 -		}
  1.1430 -	}
  1.1431 -
  1.1432 -	return false;
  1.1433 -}
  1.1434 -
  1.1435 -IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
  1.1436 -											const char *filepath,
  1.1437 -											const char *filename,
  1.1438 -											char *error,
  1.1439 -											size_t maxlength)
  1.1440 -{
  1.1441 -	IExtension *pAlready;
  1.1442 -	if ((pAlready=FindExtensionByFile(filename)) != NULL)
  1.1443 -	{
  1.1444 -		return pAlready;
  1.1445 -	}
  1.1446 -
  1.1447 -	CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
  1.1448 -
  1.1449 -	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
  1.1450 -	{
  1.1451 -		pExt->Unload();
  1.1452 -		delete pExt;
  1.1453 -		return NULL;
  1.1454 -	}
  1.1455 -
  1.1456 -	m_Libs.push_back(pExt);
  1.1457 -
  1.1458 -	return pExt;
  1.1459 -}
  1.1460 -
  1.1461 -void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
  1.1462 -{
  1.1463 -	IExtensionInterface *pAPI;
  1.1464 -	List<CExtension *>::iterator iter;
  1.1465 -
  1.1466 -	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
  1.1467 -	{
  1.1468 -		if ((pAPI = (*iter)->GetAPI()) == NULL)
  1.1469 -		{
  1.1470 -			continue;
  1.1471 -		}
  1.1472 -		if (pAPI->GetExtensionVersion() > 3)
  1.1473 -		{
  1.1474 -			pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
  1.1475 -		}
  1.1476 -	}
  1.1477 -}
  1.1478 -
  1.1479 -bool CLocalExtension::IsSameFile(const char *file)
  1.1480 -{
  1.1481 -	/* Only care about the shortened name. */
  1.1482 -	return strcmp(file, m_File.c_str()) == 0;
  1.1483 -}
  1.1484 -
  1.1485 -bool CRemoteExtension::IsSameFile(const char *file)
  1.1486 -{
  1.1487 -	/* :TODO: this could be better, but no one uses this API anyway. */
  1.1488 -	return strcmp(file, m_Path.c_str()) == 0;
  1.1489 -}
  1.1490 -