core/metamod_plugins.cpp
changeset 539 db914129ae01
parent 534 eb2d7d18441c
child 674 c4ed595f6248
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/metamod_plugins.cpp	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,831 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     1.9 + * All rights reserved.
    1.10 + * ======================================================
    1.11 + *
    1.12 + * This software is provided 'as-is', without any express or implied warranty.
    1.13 + * In no event will the authors be held liable for any damages arising from 
    1.14 + * the use of this software.
    1.15 + * 
    1.16 + * Permission is granted to anyone to use this software for any purpose, 
    1.17 + * including commercial applications, and to alter it and redistribute it 
    1.18 + * freely, subject to the following restrictions:
    1.19 + *
    1.20 + * 1. The origin of this software must not be misrepresented; you must not 
    1.21 + * claim that you wrote the original software. If you use this software in a 
    1.22 + * product, an acknowledgment in the product documentation would be 
    1.23 + * appreciated but is not required.
    1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
    1.25 + * misrepresented as being the original software.
    1.26 + * 3. This notice may not be removed or altered from any source distribution.
    1.27 + *
    1.28 + * Version: $Id$
    1.29 + */
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include "metamod_oslink.h"
    1.33 +#include "metamod.h"
    1.34 +#include "metamod_plugins.h"
    1.35 +#include "metamod_util.h"
    1.36 +
    1.37 +/** 
    1.38 + * @brief Implements functions from CPlugin.h
    1.39 + * @file CPlugin.cpp
    1.40 + */
    1.41 +
    1.42 +using namespace SourceMM;
    1.43 +
    1.44 +#define ITER_PLEVENT(evn, plid) \
    1.45 +	CPluginManager::CPlugin *_Xpl; \
    1.46 +	SourceHook::List<IMetamodListener *>::iterator event; \
    1.47 +	IMetamodListener *api; \
    1.48 +	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    1.49 +		_Xpl = (*iter); \
    1.50 +		if (_Xpl->m_Id == plid) \
    1.51 +			continue; \
    1.52 +		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    1.53 +			api = (*event); \
    1.54 +			api->evn(plid); \
    1.55 +		} \
    1.56 +	}
    1.57 +
    1.58 +CPluginManager g_PluginMngr;
    1.59 +
    1.60 +MetamodVersionInfo GlobVersionInfo = 
    1.61 +{
    1.62 +	METAMOD_API_MAJOR,
    1.63 +	METAMOD_API_MINOR,
    1.64 +	SH_IFACE_VERSION,
    1.65 +	SH_IMPL_VERSION,
    1.66 +	PLAPI_MIN_VERSION,
    1.67 +	METAMOD_PLAPI_VERSION,
    1.68 +	SOURCE_ENGINE_UNKNOWN
    1.69 +};
    1.70 +
    1.71 +CPluginManager::CPluginManager()
    1.72 +{
    1.73 +	m_LastId = Pl_MinId;
    1.74 +	m_AllLoaded = false;
    1.75 +}
    1.76 +
    1.77 +CPluginManager::~CPluginManager()
    1.78 +{
    1.79 +	SourceHook::List<CNameAlias *>::iterator iter;
    1.80 +
    1.81 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    1.82 +	{
    1.83 +		delete (*iter);
    1.84 +	}
    1.85 +
    1.86 +	m_Aliases.clear();
    1.87 +}
    1.88 +
    1.89 +const char *CPluginManager::LookupAlias(const char *alias)
    1.90 +{
    1.91 +	SourceHook::List<CNameAlias *>::iterator iter;
    1.92 +	CNameAlias *p;
    1.93 +
    1.94 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    1.95 +	{
    1.96 +		p = (*iter);
    1.97 +		if (p->alias.compare(alias) == 0)
    1.98 +		{
    1.99 +			return p->value.c_str();
   1.100 +		}
   1.101 +	}
   1.102 +
   1.103 +	return NULL;
   1.104 +}
   1.105 +
   1.106 +SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_begin()
   1.107 +{
   1.108 +	return m_Aliases.begin();
   1.109 +}
   1.110 +
   1.111 +SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_end()
   1.112 +{
   1.113 +	return m_Aliases.end();
   1.114 +}
   1.115 +
   1.116 +void CPluginManager::SetAlias(const char *alias, const char *value)
   1.117 +{
   1.118 +	SourceHook::List<CNameAlias *>::iterator iter;
   1.119 +	CNameAlias *p;
   1.120 +
   1.121 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
   1.122 +	{
   1.123 +		p = (*iter);
   1.124 +		if (p->alias.compare(alias) == 0)
   1.125 +		{
   1.126 +			if (value[0] == '\0')
   1.127 +			{
   1.128 +				iter = m_Aliases.erase(iter);
   1.129 +				return;
   1.130 +			}
   1.131 +			else
   1.132 +			{
   1.133 +				p->value.assign(value);
   1.134 +				return;
   1.135 +			}
   1.136 +		}
   1.137 +	}
   1.138 +
   1.139 +	if (value[0] != '\0')
   1.140 +	{
   1.141 +		p = new CNameAlias;
   1.142 +	
   1.143 +		p->alias.assign(alias);
   1.144 +		p->value.assign(value);
   1.145 +	
   1.146 +		m_Aliases.push_back(p);
   1.147 +	}
   1.148 +}
   1.149 +
   1.150 +CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL), m_UnloadFn(NULL)
   1.151 +{
   1.152 +
   1.153 +}
   1.154 +
   1.155 +PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
   1.156 +{
   1.157 +	already = false;
   1.158 +	//Check if we're about to reload an old plugin
   1.159 +	PluginIter i = m_Plugins.begin();
   1.160 +	while (i != m_Plugins.end())
   1.161 +	{
   1.162 +		if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
   1.163 +		{
   1.164 +			if ((*i)->m_Status < Pl_Paused)
   1.165 +			{
   1.166 +				//Attempt to load the plugin again
   1.167 +				already = true;
   1.168 +				i = m_Plugins.erase(i);
   1.169 +				continue;
   1.170 +			}
   1.171 +			else
   1.172 +			{
   1.173 +				//No need to load it
   1.174 +				already = true;
   1.175 +				return (*i)->m_Id;
   1.176 +			}
   1.177 +		}
   1.178 +		i++;
   1.179 +	}
   1.180 +
   1.181 +	CPlugin *pl = _Load(file, source, error, maxlen);
   1.182 +
   1.183 +	if (!pl)
   1.184 +	{
   1.185 +		return Pl_BadLoad;
   1.186 +	}
   1.187 +
   1.188 +	ITER_PLEVENT(OnPluginLoad, pl->m_Id);
   1.189 +
   1.190 +	return pl->m_Id;
   1.191 +}
   1.192 +
   1.193 +CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
   1.194 +{
   1.195 +	PluginIter i;
   1.196 +
   1.197 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.198 +	{
   1.199 +		if ( (*i)->m_Id == id )\
   1.200 +		{
   1.201 +			return (*i);
   1.202 +		}
   1.203 +	}
   1.204 +
   1.205 +	return NULL;
   1.206 +}
   1.207 +
   1.208 +void CPluginManager::SetAllLoaded()
   1.209 +{
   1.210 +	if (m_AllLoaded)
   1.211 +	{
   1.212 +		return;
   1.213 +	}
   1.214 +
   1.215 +	m_AllLoaded = true;
   1.216 +	PluginIter i;
   1.217 +
   1.218 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.219 +	{
   1.220 +		if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
   1.221 +		{
   1.222 +			//API 4 is when we added this callback
   1.223 +			//Min version is now 5, so we ignore this check
   1.224 +			//if ( (*i)->m_API->GetApiVersion() >= 004 )
   1.225 +			(*i)->m_API->AllPluginsLoaded();
   1.226 +		}
   1.227 +	}
   1.228 +}
   1.229 +
   1.230 +bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
   1.231 +{
   1.232 +	CPlugin *pl = FindById(id);
   1.233 +	
   1.234 +	if (!pl)
   1.235 +	{
   1.236 +		UTIL_Format(error, maxlen, "Plugin id not found");
   1.237 +		return false;
   1.238 +	}
   1.239 +
   1.240 +	bool ret;
   1.241 +	
   1.242 +	if ((ret=_Pause(pl, error, maxlen)) == true)
   1.243 +	{
   1.244 +		ITER_PLEVENT(OnPluginPause, pl->m_Id);
   1.245 +	}
   1.246 +
   1.247 +	return ret;
   1.248 +}
   1.249 +
   1.250 +bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
   1.251 +{
   1.252 +	CPlugin *pl = FindById(id);
   1.253 +	
   1.254 +	if (!pl)
   1.255 +	{
   1.256 +		UTIL_Format(error, maxlen, "Plugin id not found");
   1.257 +		return false;
   1.258 +	}
   1.259 +
   1.260 +	bool ret;
   1.261 +	
   1.262 +	if ( (ret=_Unpause(pl, error, maxlen)) == true )
   1.263 +	{
   1.264 +		ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
   1.265 +	}
   1.266 +
   1.267 +	return ret;
   1.268 +}
   1.269 +
   1.270 +bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
   1.271 +{
   1.272 +	CPlugin *pl = FindById(id);
   1.273 +	
   1.274 +	if (!pl)
   1.275 +	{
   1.276 +		UTIL_Format(error, maxlen, "Plugin %d not found", id);
   1.277 +		return false;
   1.278 +	}
   1.279 +
   1.280 +	bool ret;
   1.281 +	PluginId old_id = pl->m_Id;
   1.282 +	if ( (ret=_Unload(pl, force, error, maxlen)) == true )
   1.283 +	{
   1.284 +		ITER_PLEVENT(OnPluginUnload, old_id);
   1.285 +	}
   1.286 +
   1.287 +	return ret;
   1.288 +}
   1.289 +
   1.290 +bool CPluginManager::Retry(PluginId id, char *error, size_t len)
   1.291 +{
   1.292 +	PluginIter i;
   1.293 +	char buffer[64];
   1.294 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.295 +	{
   1.296 +		if ( (*i) && (*i)->m_Id == id )
   1.297 +		{
   1.298 +			if ( (*i)->m_Status >= Pl_Paused)
   1.299 +			{
   1.300 +				UTIL_Format(error, len, "Plugin %d is already running.", id);
   1.301 +				return false;
   1.302 +			}
   1.303 +			CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
   1.304 +			if (!pl)
   1.305 +			{
   1.306 +				return false;
   1.307 +			}
   1.308 +			if (pl->m_Status >= Pl_Paused)
   1.309 +			{
   1.310 +				//Now it gets crazy... unload the original copy.
   1.311 +				_Unload( (*i), true, buffer, sizeof(buffer)-1 );
   1.312 +				
   1.313 +				//Set the new copy's id
   1.314 +				pl->m_Id = id;
   1.315 +
   1.316 +				//We just wasted an id... reclaim it
   1.317 +				m_LastId--;
   1.318 +				
   1.319 +				return true;
   1.320 +			}
   1.321 +			else
   1.322 +			{
   1.323 +				//don't really care about the buffer here
   1.324 +				_Unload(pl, true, buffer, sizeof(buffer)-1);
   1.325 +
   1.326 +				//We just wasted an id... reclaim it
   1.327 +				m_LastId--;
   1.328 +				return false;
   1.329 +			}
   1.330 +		}
   1.331 +	}
   1.332 +
   1.333 +	UTIL_Format(error, len, "Plugin %d not found,", id);
   1.334 +	return false;
   1.335 +}
   1.336 +
   1.337 +CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
   1.338 +{
   1.339 +	PluginIter i;
   1.340 +
   1.341 +	//don't find bad plugins!
   1.342 +	if (!api)
   1.343 +	{
   1.344 +		return NULL;
   1.345 +	}
   1.346 +
   1.347 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.348 +	{
   1.349 +		if ((*i)->m_API == api)
   1.350 +		{
   1.351 +			return (*i);
   1.352 +		}
   1.353 +	}
   1.354 +
   1.355 +	return NULL;
   1.356 +}
   1.357 +
   1.358 +int CPluginManager::GetPluginCount()
   1.359 +{
   1.360 +	return (int)m_Plugins.size();
   1.361 +}
   1.362 +
   1.363 +const char *CPluginManager::GetStatusText(CPlugin *pl)
   1.364 +{
   1.365 +	switch (pl->m_Status)
   1.366 +	{
   1.367 +	case Pl_NotFound:
   1.368 +		return "NOFILE";
   1.369 +	case Pl_Error:
   1.370 +		return "ERROR";
   1.371 +	case Pl_Refused:
   1.372 +		return "FAILED";
   1.373 +	case Pl_Paused:
   1.374 +		return "PAUSED";
   1.375 +	case Pl_Running:
   1.376 +		{
   1.377 +			if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   1.378 +			{
   1.379 +				return "STOPPED";
   1.380 +			} else {
   1.381 +				return "RUNNING";
   1.382 +			}
   1.383 +		}
   1.384 +	default:
   1.385 +		return "-";
   1.386 +	}
   1.387 +}
   1.388 +
   1.389 +CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   1.390 +{
   1.391 +	FILE *fp;
   1.392 +	CPlugin *pl;
   1.393 +
   1.394 +	pl = new CPlugin();
   1.395 +	*error = '\0';
   1.396 +	
   1.397 +	//Add plugin to list
   1.398 +	pl->m_Id = m_LastId;
   1.399 +	pl->m_File.assign(file);
   1.400 +	m_Plugins.push_back(pl);
   1.401 +	m_LastId++;
   1.402 +
   1.403 +	//Check if the file even exists
   1.404 +	fp = fopen(file, "r");
   1.405 +	if (!fp)
   1.406 +	{
   1.407 +		if (error)
   1.408 +		{
   1.409 +			UTIL_Format(error, maxlen, "File not found: %s", file);
   1.410 +		}
   1.411 +		pl->m_Status = Pl_NotFound;
   1.412 +	}
   1.413 +
   1.414 +	if (fp)
   1.415 +	{
   1.416 +		fclose(fp);
   1.417 +		fp = NULL;
   1.418 +		
   1.419 +		//Load the file
   1.420 +		pl->m_Lib = dlmount(file);
   1.421 +		if (!pl->m_Lib)
   1.422 +		{
   1.423 +			if (error)
   1.424 +			{
   1.425 +				UTIL_Format(error, maxlen, "%s", dlerror());
   1.426 +			}
   1.427 +			pl->m_Status = Pl_Error;
   1.428 +		}
   1.429 +		else
   1.430 +		{
   1.431 +			pl->m_API = NULL;
   1.432 +			
   1.433 +			/**
   1.434 +			 * First, try the new "advanced" interface
   1.435 +			 */
   1.436 +			METAMOD_FN_LOAD fnLoad = (METAMOD_FN_LOAD)dlsym(pl->m_Lib, "CreateInterface_MMS");
   1.437 +			if (fnLoad != NULL)
   1.438 +			{
   1.439 +				if (GlobVersionInfo.source_engine == SOURCE_ENGINE_UNKNOWN)
   1.440 +				{
   1.441 +					GlobVersionInfo.source_engine = g_Metamod.GetSourceEngineBuild();
   1.442 +				}
   1.443 +
   1.444 +				/* Build path information */
   1.445 +				char file_path[256];
   1.446 +				size_t len = g_Metamod.PathFormat(file_path, sizeof(file_path), "%s", file);
   1.447 +
   1.448 +				for (size_t i = len - 1; i >= 0 && i < len; i--)
   1.449 +				{
   1.450 +					if (_IsPathSepChar(file_path[i]))
   1.451 +					{
   1.452 +						file_path[i] = '\0';
   1.453 +						break;
   1.454 +					}
   1.455 +				}
   1.456 +
   1.457 +				MetamodLoaderInfo info;
   1.458 +				info.pl_file = file;
   1.459 +				info.pl_path = file_path;
   1.460 +
   1.461 +				pl->m_API = fnLoad(&GlobVersionInfo, &info);
   1.462 +				pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
   1.463 +			}
   1.464 +
   1.465 +			/**
   1.466 +			 * If we didn't get anything, try the normal/simple interface.
   1.467 +			 */
   1.468 +			if (pl->m_API == NULL)
   1.469 +			{
   1.470 +				CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   1.471 +				if (!pfn)
   1.472 +				{
   1.473 +					if (error)
   1.474 +					{
   1.475 +						UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   1.476 +					}
   1.477 +					pl->m_Status = Pl_Error;
   1.478 +				}
   1.479 +				else
   1.480 +				{
   1.481 +					pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
   1.482 +
   1.483 +					if (!pl->m_API)
   1.484 +					{
   1.485 +						if (error)
   1.486 +						{
   1.487 +							UTIL_Format(error, maxlen, "Failed to get API");
   1.488 +						}
   1.489 +						pl->m_Status = Pl_Error;
   1.490 +					}
   1.491 +				}
   1.492 +			}
   1.493 +
   1.494 +			if (pl->m_API != NULL)
   1.495 +			{
   1.496 +				int api = pl->m_API->GetApiVersion();
   1.497 +				if (api < PLAPI_MIN_VERSION)
   1.498 +				{
   1.499 +					if (error)
   1.500 +					{
   1.501 +						if (api == 13)
   1.502 +						{
   1.503 +							UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
   1.504 +						}
   1.505 +						else if (api <= 12 && api >= 7)
   1.506 +						{
   1.507 +							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
   1.508 +						}
   1.509 +						else
   1.510 +						{
   1.511 +							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
   1.512 +						}
   1.513 +					}
   1.514 +					pl->m_Status = Pl_Error;
   1.515 +				}
   1.516 +				else if (api > METAMOD_PLAPI_VERSION)
   1.517 +				{
   1.518 +					if (error)
   1.519 +					{
   1.520 +						UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
   1.521 +					}
   1.522 +					pl->m_Status = Pl_Error;
   1.523 +				}
   1.524 +				else
   1.525 +				{
   1.526 +					if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   1.527 +					{
   1.528 +						pl->m_Status = Pl_Running;
   1.529 +						if (m_AllLoaded)
   1.530 +						{
   1.531 +							pl->m_API->AllPluginsLoaded();
   1.532 +						}
   1.533 +					}
   1.534 +					else
   1.535 +					{
   1.536 +						pl->m_Status = Pl_Refused;
   1.537 +					}
   1.538 +				}
   1.539 +			}
   1.540 +		}
   1.541 +	}
   1.542 +
   1.543 +	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   1.544 +	{
   1.545 +		pl->m_Events.clear();
   1.546 +		g_SourceHook.UnloadPlugin(pl->m_Id);
   1.547 +		UnregAllConCmds(pl);
   1.548 +
   1.549 +		if (pl->m_UnloadFn != NULL)
   1.550 +		{
   1.551 +			pl->m_UnloadFn();
   1.552 +		}
   1.553 +
   1.554 +		dlclose(pl->m_Lib);
   1.555 +		pl->m_Lib = NULL;
   1.556 +		pl->m_API = NULL;
   1.557 +	}
   1.558 +
   1.559 +	return pl;
   1.560 +}
   1.561 +
   1.562 +bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   1.563 +{
   1.564 +	if (error)
   1.565 +	{
   1.566 +		*error = '\0';
   1.567 +	}
   1.568 +
   1.569 +	if (pl->m_API && pl->m_Lib)
   1.570 +	{
   1.571 +		//Note, we'll always tell the plugin it will be unloading...
   1.572 +		if (pl->m_API->Unload(error, maxlen) || force)
   1.573 +		{
   1.574 +			//Make sure to detach it from sourcehook!
   1.575 +			g_SourceHook.UnloadPlugin(pl->m_Id);
   1.576 +
   1.577 +			pl->m_Events.clear();
   1.578 +
   1.579 +			UnregAllConCmds(pl);
   1.580 +
   1.581 +			if (pl->m_UnloadFn != NULL)
   1.582 +			{
   1.583 +				pl->m_UnloadFn();
   1.584 +			}
   1.585 +
   1.586 +			//Clean up the DLL
   1.587 +			dlclose(pl->m_Lib);
   1.588 +			pl->m_Lib = NULL;
   1.589 +			pl->m_API = NULL;
   1.590 +
   1.591 +			//Remove the plugin from the list
   1.592 +			PluginIter i;
   1.593 +			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.594 +			{
   1.595 +				if ( (*i)->m_Id == pl->m_Id )
   1.596 +				{
   1.597 +					i = m_Plugins.erase(i);
   1.598 +					break;
   1.599 +				}
   1.600 +			}
   1.601 +			//Free its memory
   1.602 +			delete pl;
   1.603 +
   1.604 +			return true;
   1.605 +		}
   1.606 +	} else {
   1.607 +		//The plugin is not valid, and let's just remove it from the list anyway
   1.608 +		PluginIter i;
   1.609 +		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.610 +		{
   1.611 +			if ( (*i)->m_Id == pl->m_Id )
   1.612 +			{
   1.613 +				i = m_Plugins.erase(i);
   1.614 +				break;
   1.615 +			}
   1.616 +		}
   1.617 +		delete pl;
   1.618 +
   1.619 +		return true;
   1.620 +	}
   1.621 +
   1.622 +	return false;
   1.623 +}
   1.624 +
   1.625 +bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   1.626 +{
   1.627 +	if (error)
   1.628 +	{
   1.629 +		*error = '\0';
   1.630 +	}
   1.631 +
   1.632 +	if (pl->m_Status != Pl_Running || !pl->m_API)
   1.633 +	{
   1.634 +		if (error)
   1.635 +		{
   1.636 +			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   1.637 +		}
   1.638 +	}
   1.639 +	else
   1.640 +	{
   1.641 +		if (pl->m_API->Pause(error, maxlen))
   1.642 +		{
   1.643 +			g_SourceHook.PausePlugin(pl->m_Id);
   1.644 +			pl->m_Status = Pl_Paused;
   1.645 +
   1.646 +			return true;
   1.647 +		}
   1.648 +	}
   1.649 +
   1.650 +	return false;
   1.651 +}
   1.652 +
   1.653 +bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   1.654 +{
   1.655 +	if (error)
   1.656 +	{
   1.657 +		*error = '\0';
   1.658 +	}
   1.659 +
   1.660 +	if (pl->m_Status != Pl_Paused || !pl->m_API)
   1.661 +	{
   1.662 +		if (error)
   1.663 +		{
   1.664 +			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   1.665 +		}
   1.666 +	}
   1.667 +	else
   1.668 +	{
   1.669 +		if (pl->m_API->Unpause(error, maxlen))
   1.670 +		{
   1.671 +			g_SourceHook.UnpausePlugin(pl->m_Id);
   1.672 +			pl->m_Status = Pl_Running;
   1.673 +
   1.674 +			return true;
   1.675 +		}
   1.676 +	}
   1.677 +
   1.678 +	return false;
   1.679 +}
   1.680 +
   1.681 +bool CPluginManager::UnloadAll()
   1.682 +{
   1.683 +	PluginIter i;
   1.684 +
   1.685 +	char error[128];
   1.686 +	bool status = true;
   1.687 +
   1.688 +	while ((i = m_Plugins.begin()) != m_Plugins.end())
   1.689 +	{
   1.690 +		if ( !_Unload( (*i), true, error, sizeof(error)) )
   1.691 +		{
   1.692 +			status = false;
   1.693 +		}
   1.694 +	}
   1.695 +
   1.696 +	return status;
   1.697 +}
   1.698 +
   1.699 +bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
   1.700 +{
   1.701 +	CPlugin *pl = FindById(id);
   1.702 +
   1.703 +	if (!pl)
   1.704 +	{
   1.705 +		return false;
   1.706 +	}
   1.707 +
   1.708 +	if (file != NULL)
   1.709 +	{
   1.710 +		*file = pl->m_File.c_str();
   1.711 +	}
   1.712 +
   1.713 +	if (status != NULL)
   1.714 +	{
   1.715 +		*status = pl->m_Status;
   1.716 +	}
   1.717 +
   1.718 +	if (source != NULL)
   1.719 +	{
   1.720 +		*source = pl->m_Source;
   1.721 +	}
   1.722 +
   1.723 +	return true;
   1.724 +}
   1.725 +
   1.726 +bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   1.727 +{
   1.728 +	CPlugin *pl = FindById(id);
   1.729 +
   1.730 +	if (!pl || !pl->m_API)
   1.731 +	{
   1.732 +		if (error)
   1.733 +		{
   1.734 +			UTIL_Format(error, maxlength, "Plugin not valid");
   1.735 +		}
   1.736 +		return false;
   1.737 +	}
   1.738 +
   1.739 +	return pl->m_API->QueryRunning(error, maxlength);
   1.740 +}
   1.741 +
   1.742 +bool CPluginManager::QueryHandle(PluginId id, void **handle)
   1.743 +{
   1.744 +	CPlugin *pl = FindById(id);
   1.745 +
   1.746 +	if (!pl)
   1.747 +	{
   1.748 +		return false;
   1.749 +	}
   1.750 +
   1.751 +	if (handle)
   1.752 +	{
   1.753 +		*handle = static_cast<void *>(pl->m_Lib);
   1.754 +	}
   1.755 +
   1.756 +	return true;
   1.757 +}
   1.758 +
   1.759 +PluginIter CPluginManager::_begin()
   1.760 +{
   1.761 +	return m_Plugins.begin();
   1.762 +}
   1.763 +
   1.764 +PluginIter CPluginManager::_end()
   1.765 +{
   1.766 +	return m_Plugins.end();
   1.767 +}
   1.768 +
   1.769 +void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   1.770 +{
   1.771 +	CPlugin *pl = FindByAPI(api);
   1.772 +
   1.773 +	if (!pl)
   1.774 +	{
   1.775 +		return;
   1.776 +	}
   1.777 +
   1.778 +	pl->m_Cvars.push_back(pCvar);
   1.779 +}
   1.780 +
   1.781 +void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   1.782 +{
   1.783 +	CPlugin *pl = FindByAPI(api);
   1.784 +
   1.785 +	if (!pl)
   1.786 +	{
   1.787 +		return;
   1.788 +	}
   1.789 +
   1.790 +	pl->m_Cmds.push_back(pCmd);
   1.791 +}
   1.792 +
   1.793 +void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   1.794 +{
   1.795 +	CPlugin *pl = FindByAPI(api);
   1.796 +
   1.797 +	if (!pl)
   1.798 +	{
   1.799 +		return;
   1.800 +	}
   1.801 +
   1.802 +	pl->m_Cvars.remove(pCvar);
   1.803 +}
   1.804 +
   1.805 +void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   1.806 +{
   1.807 +	CPlugin *pl = FindByAPI(api);
   1.808 +
   1.809 +	if (!pl)
   1.810 +	{
   1.811 +		return;
   1.812 +	}
   1.813 +
   1.814 +	pl->m_Cmds.remove(pCmd);
   1.815 +}
   1.816 +
   1.817 +void CPluginManager::UnregAllConCmds(CPlugin *pl)
   1.818 +{
   1.819 +	SourceHook::List<ConCommandBase *>::iterator i;
   1.820 +
   1.821 +	/* :TODO: */
   1.822 +	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   1.823 +	{
   1.824 +		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   1.825 +	}
   1.826 +	pl->m_Cvars.clear();
   1.827 +
   1.828 +	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   1.829 +	{
   1.830 +		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   1.831 +	}
   1.832 +	pl->m_Cmds.clear();
   1.833 +}
   1.834 +