sourcemm/metamod_plugins.cpp
branchsourcemm-1.6.0
changeset 354 341f867fdad8
child 363 b3db5c1dd016
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcemm/metamod_plugins.cpp	Sat Sep 22 21:31:45 2007 +0000
     1.3 @@ -0,0 +1,731 @@
     1.4 +/* ======== SourceMM ========
     1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
     1.6 + * No warranties of any kind
     1.7 + *
     1.8 + * License: zlib/libpng
     1.9 + *
    1.10 + * Author(s): David "BAILOPAN" Anderson
    1.11 + * ============================
    1.12 + */
    1.13 +
    1.14 +#if defined _DEBUG
    1.15 +#define DEBUG2
    1.16 +#undef _DEBUG
    1.17 +#endif
    1.18 +#include "metamod.h"
    1.19 +#include "metamod_plugins.h"
    1.20 +#include "metamod_util.h"
    1.21 +#if defined DEBUG2
    1.22 +#undef DEBUG2
    1.23 +#define _DEBUG
    1.24 +#endif
    1.25 +
    1.26 +/** 
    1.27 + * @brief Implements functions from CPlugin.h
    1.28 + * @file CPlugin.cpp
    1.29 + */
    1.30 +
    1.31 +using namespace SourceMM;
    1.32 +
    1.33 +#define ITER_PLEVENT(evn, plid) \
    1.34 +	CPluginManager::CPlugin *_Xpl; \
    1.35 +	SourceHook::List<IMetamodListener *>::iterator event; \
    1.36 +	IMetamodListener *api; \
    1.37 +	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    1.38 +		_Xpl = (*iter); \
    1.39 +		if (_Xpl->m_Id == plid) \
    1.40 +			continue; \
    1.41 +		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    1.42 +			api = (*event); \
    1.43 +			api->evn(plid); \
    1.44 +		} \
    1.45 +	}
    1.46 +
    1.47 +CPluginManager g_PluginMngr;
    1.48 +
    1.49 +CPluginManager::CPluginManager()
    1.50 +{
    1.51 +	m_LastId = Pl_MinId;
    1.52 +	m_AllLoaded = false;
    1.53 +}
    1.54 +
    1.55 +CPluginManager::~CPluginManager()
    1.56 +{
    1.57 +	SourceHook::List<CNameAlias *>::iterator iter;
    1.58 +
    1.59 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    1.60 +	{
    1.61 +		delete (*iter);
    1.62 +	}
    1.63 +
    1.64 +	m_Aliases.clear();
    1.65 +}
    1.66 +
    1.67 +const char *CPluginManager::LookupAlias(const char *alias)
    1.68 +{
    1.69 +	SourceHook::List<CNameAlias *>::iterator iter;
    1.70 +	CNameAlias *p;
    1.71 +
    1.72 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    1.73 +	{
    1.74 +		p = (*iter);
    1.75 +		if (p->alias.compare(alias) == 0)
    1.76 +		{
    1.77 +			return p->value.c_str();
    1.78 +		}
    1.79 +	}
    1.80 +
    1.81 +	return NULL;
    1.82 +}
    1.83 +
    1.84 +SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_begin()
    1.85 +{
    1.86 +	return m_Aliases.begin();
    1.87 +}
    1.88 +
    1.89 +SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_end()
    1.90 +{
    1.91 +	return m_Aliases.end();
    1.92 +}
    1.93 +
    1.94 +void CPluginManager::SetAlias(const char *alias, const char *value)
    1.95 +{
    1.96 +	SourceHook::List<CNameAlias *>::iterator iter;
    1.97 +	CNameAlias *p;
    1.98 +
    1.99 +	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
   1.100 +	{
   1.101 +		p = (*iter);
   1.102 +		if (p->alias.compare(alias) == 0)
   1.103 +		{
   1.104 +			if (value[0] == '\0')
   1.105 +			{
   1.106 +				iter = m_Aliases.erase(iter);
   1.107 +				return;
   1.108 +			}
   1.109 +			else
   1.110 +			{
   1.111 +				p->value.assign(value);
   1.112 +				return;
   1.113 +			}
   1.114 +		}
   1.115 +	}
   1.116 +
   1.117 +	if (value[0] != '\0')
   1.118 +	{
   1.119 +		p = new CNameAlias;
   1.120 +	
   1.121 +		p->alias.assign(alias);
   1.122 +		p->value.assign(value);
   1.123 +	
   1.124 +		m_Aliases.push_back(p);
   1.125 +	}
   1.126 +}
   1.127 +
   1.128 +CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL)
   1.129 +{
   1.130 +
   1.131 +}
   1.132 +
   1.133 +PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
   1.134 +{
   1.135 +	already = false;
   1.136 +	//Check if we're about to reload an old plugin
   1.137 +	PluginIter i = m_Plugins.begin();
   1.138 +	while (i != m_Plugins.end())
   1.139 +	{
   1.140 +		if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
   1.141 +		{
   1.142 +			if ((*i)->m_Status < Pl_Paused)
   1.143 +			{
   1.144 +				//Attempt to load the plugin again
   1.145 +				already = true;
   1.146 +				i = m_Plugins.erase(i);
   1.147 +				continue;
   1.148 +			}
   1.149 +			else
   1.150 +			{
   1.151 +				//No need to load it
   1.152 +				already = true;
   1.153 +				return (*i)->m_Id;
   1.154 +			}
   1.155 +		}
   1.156 +		i++;
   1.157 +	}
   1.158 +
   1.159 +	CPlugin *pl = _Load(file, source, error, maxlen);
   1.160 +
   1.161 +	if (!pl)
   1.162 +	{
   1.163 +		return Pl_BadLoad;
   1.164 +	}
   1.165 +
   1.166 +	ITER_PLEVENT(OnPluginLoad, pl->m_Id);
   1.167 +
   1.168 +	return pl->m_Id;
   1.169 +}
   1.170 +
   1.171 +CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
   1.172 +{
   1.173 +	PluginIter i;
   1.174 +
   1.175 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.176 +	{
   1.177 +		if ( (*i)->m_Id == id )\
   1.178 +		{
   1.179 +			return (*i);
   1.180 +		}
   1.181 +	}
   1.182 +
   1.183 +	return NULL;
   1.184 +}
   1.185 +
   1.186 +void CPluginManager::SetAllLoaded()
   1.187 +{
   1.188 +	m_AllLoaded = true;
   1.189 +	PluginIter i;
   1.190 +
   1.191 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.192 +	{
   1.193 +		if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
   1.194 +		{
   1.195 +			//API 4 is when we added this callback
   1.196 +			//Min version is now 5, so we ignore this check
   1.197 +			//if ( (*i)->m_API->GetApiVersion() >= 004 )
   1.198 +			(*i)->m_API->AllPluginsLoaded();
   1.199 +		}
   1.200 +	}
   1.201 +}
   1.202 +
   1.203 +bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
   1.204 +{
   1.205 +	CPlugin *pl = FindById(id);
   1.206 +	
   1.207 +	if (!pl)
   1.208 +	{
   1.209 +		UTIL_Format(error, maxlen, "Plugin id not found");
   1.210 +		return false;
   1.211 +	}
   1.212 +
   1.213 +	bool ret;
   1.214 +	
   1.215 +	if ((ret=_Pause(pl, error, maxlen)) == true)
   1.216 +	{
   1.217 +		ITER_PLEVENT(OnPluginPause, pl->m_Id);
   1.218 +	}
   1.219 +
   1.220 +	return ret;
   1.221 +}
   1.222 +
   1.223 +bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
   1.224 +{
   1.225 +	CPlugin *pl = FindById(id);
   1.226 +	
   1.227 +	if (!pl)
   1.228 +	{
   1.229 +		UTIL_Format(error, maxlen, "Plugin id not found");
   1.230 +		return false;
   1.231 +	}
   1.232 +
   1.233 +	bool ret;
   1.234 +	
   1.235 +	if ( (ret=_Unpause(pl, error, maxlen)) == true )
   1.236 +	{
   1.237 +		ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
   1.238 +	}
   1.239 +
   1.240 +	return ret;
   1.241 +}
   1.242 +
   1.243 +bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
   1.244 +{
   1.245 +	CPlugin *pl = FindById(id);
   1.246 +	
   1.247 +	if (!pl)
   1.248 +	{
   1.249 +		UTIL_Format(error, maxlen, "Plugin %d not found", id);
   1.250 +		return false;
   1.251 +	}
   1.252 +
   1.253 +	bool ret;
   1.254 +	PluginId old_id = pl->m_Id;
   1.255 +	if ( (ret=_Unload(pl, force, error, maxlen)) == true )
   1.256 +	{
   1.257 +		ITER_PLEVENT(OnPluginUnload, old_id);
   1.258 +	}
   1.259 +
   1.260 +	return ret;
   1.261 +}
   1.262 +
   1.263 +bool CPluginManager::Retry(PluginId id, char *error, size_t len)
   1.264 +{
   1.265 +	PluginIter i;
   1.266 +	char buffer[64];
   1.267 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.268 +	{
   1.269 +		if ( (*i) && (*i)->m_Id == id )
   1.270 +		{
   1.271 +			if ( (*i)->m_Status >= Pl_Paused)
   1.272 +			{
   1.273 +				UTIL_Format(error, len, "Plugin %d is already running.", id);
   1.274 +				return false;
   1.275 +			}
   1.276 +			CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
   1.277 +			if (!pl)
   1.278 +			{
   1.279 +				return false;
   1.280 +			}
   1.281 +			if (pl->m_Status >= Pl_Paused)
   1.282 +			{
   1.283 +				//Now it gets crazy... unload the original copy.
   1.284 +				_Unload( (*i), true, buffer, sizeof(buffer)-1 );
   1.285 +				
   1.286 +				//Set the new copy's id
   1.287 +				pl->m_Id = id;
   1.288 +
   1.289 +				//We just wasted an id... reclaim it
   1.290 +				m_LastId--;
   1.291 +				
   1.292 +				return true;
   1.293 +			}
   1.294 +			else
   1.295 +			{
   1.296 +				//don't really care about the buffer here
   1.297 +				_Unload(pl, true, buffer, sizeof(buffer)-1);
   1.298 +
   1.299 +				//We just wasted an id... reclaim it
   1.300 +				m_LastId--;
   1.301 +				return false;
   1.302 +			}
   1.303 +		}
   1.304 +	}
   1.305 +
   1.306 +	UTIL_Format(error, len, "Plugin %d not found,", id);
   1.307 +	return false;
   1.308 +}
   1.309 +
   1.310 +CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
   1.311 +{
   1.312 +	PluginIter i;
   1.313 +
   1.314 +	//don't find bad plugins!
   1.315 +	if (!api)
   1.316 +	{
   1.317 +		return NULL;
   1.318 +	}
   1.319 +
   1.320 +	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.321 +	{
   1.322 +		if ((*i)->m_API == api)
   1.323 +		{
   1.324 +			return (*i);
   1.325 +		}
   1.326 +	}
   1.327 +
   1.328 +	return NULL;
   1.329 +}
   1.330 +
   1.331 +int CPluginManager::GetPluginCount()
   1.332 +{
   1.333 +	return (int)m_Plugins.size();
   1.334 +}
   1.335 +
   1.336 +const char *CPluginManager::GetStatusText(CPlugin *pl)
   1.337 +{
   1.338 +	switch (pl->m_Status)
   1.339 +	{
   1.340 +	case Pl_NotFound:
   1.341 +		return "NOFILE";
   1.342 +	case Pl_Error:
   1.343 +		return "ERROR";
   1.344 +	case Pl_Refused:
   1.345 +		return "FAILED";
   1.346 +	case Pl_Paused:
   1.347 +		return "PAUSED";
   1.348 +	case Pl_Running:
   1.349 +		{
   1.350 +			if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   1.351 +			{
   1.352 +				return "STOPPED";
   1.353 +			} else {
   1.354 +				return "RUNNING";
   1.355 +			}
   1.356 +		}
   1.357 +	default:
   1.358 +		return "-";
   1.359 +	}
   1.360 +}
   1.361 +
   1.362 +CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   1.363 +{
   1.364 +	FILE *fp;
   1.365 +	CPlugin *pl;
   1.366 +
   1.367 +	pl = new CPlugin();
   1.368 +	*error = '\0';
   1.369 +	
   1.370 +	//Add plugin to list
   1.371 +	pl->m_Id = m_LastId;
   1.372 +	pl->m_File.assign(file);
   1.373 +	m_Plugins.push_back(pl);
   1.374 +	m_LastId++;
   1.375 +
   1.376 +	//Check if the file even exists
   1.377 +	fp = fopen(file, "r");
   1.378 +	if (!fp)
   1.379 +	{
   1.380 +		if (error)
   1.381 +		{
   1.382 +			UTIL_Format(error, maxlen, "File not found: %s", file);
   1.383 +		}
   1.384 +		pl->m_Status = Pl_NotFound;
   1.385 +	}
   1.386 +
   1.387 +	if (fp)
   1.388 +	{
   1.389 +		fclose(fp);
   1.390 +		fp = NULL;
   1.391 +		
   1.392 +		//Load the file
   1.393 +		pl->m_Lib = dlmount(file);
   1.394 +		if (!pl->m_Lib)
   1.395 +		{
   1.396 +			if (error)
   1.397 +			{
   1.398 +				UTIL_Format(error, maxlen, "%s", dlerror());
   1.399 +			}
   1.400 +			pl->m_Status = Pl_Error;
   1.401 +		}
   1.402 +		else
   1.403 +		{
   1.404 +			CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   1.405 +			if (!pfn)
   1.406 +			{
   1.407 +				if (error)
   1.408 +				{
   1.409 +					UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   1.410 +				}
   1.411 +				pl->m_Status = Pl_Error;
   1.412 +			}
   1.413 +			else
   1.414 +			{
   1.415 +				pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
   1.416 +				if (!pl->m_API)
   1.417 +				{
   1.418 +					if (error)
   1.419 +					{
   1.420 +						UTIL_Format(error, maxlen, "Failed to get API");
   1.421 +					}
   1.422 +					pl->m_Status = Pl_Error;
   1.423 +				}
   1.424 +				else
   1.425 +				{
   1.426 +					int api = pl->m_API->GetApiVersion();
   1.427 +					if (api < PLAPI_MIN_VERSION)
   1.428 +					{
   1.429 +						if (error)
   1.430 +						{
   1.431 +							UTIL_Format(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
   1.432 +						}
   1.433 +						pl->m_Status = Pl_Error;
   1.434 +					}
   1.435 +					else if (api > PLAPI_VERSION)
   1.436 +					{
   1.437 +						if (error)
   1.438 +						{
   1.439 +							UTIL_Format(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
   1.440 +						}
   1.441 +						pl->m_Status = Pl_Error;
   1.442 +					}
   1.443 +					else
   1.444 +					{
   1.445 +						if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   1.446 +						{
   1.447 +							pl->m_Status = Pl_Running;
   1.448 +							if (m_AllLoaded)
   1.449 +							{
   1.450 +								//API 4 is when we added this callback
   1.451 +								//Removing this code as the min version is now 5
   1.452 +								//if (pl->m_API->GetApiVersion() >= 4)
   1.453 +								pl->m_API->AllPluginsLoaded();
   1.454 +							}
   1.455 +						}
   1.456 +						else
   1.457 +						{
   1.458 +							pl->m_Status = Pl_Refused;
   1.459 +						}
   1.460 +					}
   1.461 +				}
   1.462 +			}
   1.463 +		}
   1.464 +	}
   1.465 +
   1.466 +	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   1.467 +	{
   1.468 +		pl->m_Events.clear();
   1.469 +		g_SourceHook.UnloadPlugin(pl->m_Id);
   1.470 +		UnregAllConCmds(pl);
   1.471 +
   1.472 +		dlclose(pl->m_Lib);
   1.473 +		pl->m_Lib = NULL;
   1.474 +		pl->m_API = NULL;
   1.475 +	}
   1.476 +
   1.477 +	return pl;
   1.478 +}
   1.479 +
   1.480 +bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   1.481 +{
   1.482 +	if (error)
   1.483 +	{
   1.484 +		*error = '\0';
   1.485 +	}
   1.486 +
   1.487 +	if (pl->m_API && pl->m_Lib)
   1.488 +	{
   1.489 +		//Note, we'll always tell the plugin it will be unloading...
   1.490 +		if (pl->m_API->Unload(error, maxlen) || force)
   1.491 +		{
   1.492 +			//Make sure to detach it from sourcehook!
   1.493 +			g_SourceHook.UnloadPlugin(pl->m_Id);
   1.494 +
   1.495 +			UnregAllConCmds(pl);
   1.496 +
   1.497 +			//Clean up the DLL
   1.498 +			dlclose(pl->m_Lib);
   1.499 +			pl->m_Lib = NULL;
   1.500 +			pl->m_API = NULL;
   1.501 +
   1.502 +			//Remove the plugin from the list
   1.503 +			PluginIter i;
   1.504 +			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.505 +			{
   1.506 +				if ( (*i)->m_Id == pl->m_Id )
   1.507 +				{
   1.508 +					i = m_Plugins.erase(i);
   1.509 +					break;
   1.510 +				}
   1.511 +			}
   1.512 +			//Free its memory
   1.513 +			delete pl;
   1.514 +
   1.515 +			return true;
   1.516 +		}
   1.517 +	} else {
   1.518 +		//The plugin is not valid, and let's just remove it from the list anyway
   1.519 +		PluginIter i;
   1.520 +		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   1.521 +		{
   1.522 +			if ( (*i)->m_Id == pl->m_Id )
   1.523 +			{
   1.524 +				i = m_Plugins.erase(i);
   1.525 +				break;
   1.526 +			}
   1.527 +		}
   1.528 +		delete pl;
   1.529 +
   1.530 +		return true;
   1.531 +	}
   1.532 +
   1.533 +	return false;
   1.534 +}
   1.535 +
   1.536 +bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   1.537 +{
   1.538 +	if (error)
   1.539 +	{
   1.540 +		*error = '\0';
   1.541 +	}
   1.542 +
   1.543 +	if (pl->m_Status != Pl_Running || !pl->m_API)
   1.544 +	{
   1.545 +		if (error)
   1.546 +		{
   1.547 +			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   1.548 +		}
   1.549 +	}
   1.550 +	else
   1.551 +	{
   1.552 +		if (pl->m_API->Pause(error, maxlen))
   1.553 +		{
   1.554 +			g_SourceHook.PausePlugin(pl->m_Id);
   1.555 +			pl->m_Status = Pl_Paused;
   1.556 +
   1.557 +			return true;
   1.558 +		}
   1.559 +	}
   1.560 +
   1.561 +	return false;
   1.562 +}
   1.563 +
   1.564 +bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   1.565 +{
   1.566 +	if (error)
   1.567 +	{
   1.568 +		*error = '\0';
   1.569 +	}
   1.570 +
   1.571 +	if (pl->m_Status != Pl_Paused || !pl->m_API)
   1.572 +	{
   1.573 +		if (error)
   1.574 +		{
   1.575 +			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   1.576 +		}
   1.577 +	}
   1.578 +	else
   1.579 +	{
   1.580 +		if (pl->m_API->Unpause(error, maxlen))
   1.581 +		{
   1.582 +			g_SourceHook.UnpausePlugin(pl->m_Id);
   1.583 +			pl->m_Status = Pl_Running;
   1.584 +
   1.585 +			return true;
   1.586 +		}
   1.587 +	}
   1.588 +
   1.589 +	return false;
   1.590 +}
   1.591 +
   1.592 +bool CPluginManager::UnloadAll()
   1.593 +{
   1.594 +	PluginIter i;
   1.595 +
   1.596 +	char error[128];
   1.597 +	bool status = true;
   1.598 +
   1.599 +	while ((i = m_Plugins.begin()) != m_Plugins.end())
   1.600 +	{
   1.601 +		if ( !_Unload( (*i), true, error, sizeof(error)) )
   1.602 +		{
   1.603 +			status = false;
   1.604 +		}
   1.605 +	}
   1.606 +
   1.607 +	return status;
   1.608 +}
   1.609 +
   1.610 +bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
   1.611 +{
   1.612 +	CPlugin *pl = FindById(id);
   1.613 +
   1.614 +	if (!pl)
   1.615 +	{
   1.616 +		return false;
   1.617 +	}
   1.618 +
   1.619 +	file = pl->m_File.c_str();
   1.620 +	status = pl->m_Status;
   1.621 +	source = pl->m_Source;
   1.622 +
   1.623 +	return true;
   1.624 +}
   1.625 +
   1.626 +bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   1.627 +{
   1.628 +	CPlugin *pl = FindById(id);
   1.629 +
   1.630 +	if (!pl || !pl->m_API)
   1.631 +	{
   1.632 +		if (error)
   1.633 +		{
   1.634 +			UTIL_Format(error, maxlength, "Plugin not valid");
   1.635 +		}
   1.636 +		return false;
   1.637 +	}
   1.638 +
   1.639 +	return pl->m_API->QueryRunning(error, maxlength);
   1.640 +}
   1.641 +
   1.642 +bool CPluginManager::QueryHandle(PluginId id, void *&handle)
   1.643 +{
   1.644 +	CPlugin *pl = FindById(id);
   1.645 +
   1.646 +	if (!pl)
   1.647 +	{
   1.648 +		return false;
   1.649 +	}
   1.650 +
   1.651 +	handle = static_cast<void *>(pl->m_Lib);
   1.652 +
   1.653 +	return true;
   1.654 +}
   1.655 +
   1.656 +PluginIter CPluginManager::_begin()
   1.657 +{
   1.658 +	return m_Plugins.begin();
   1.659 +}
   1.660 +
   1.661 +PluginIter CPluginManager::_end()
   1.662 +{
   1.663 +	return m_Plugins.end();
   1.664 +}
   1.665 +
   1.666 +void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   1.667 +{
   1.668 +	CPlugin *pl = FindByAPI(api);
   1.669 +
   1.670 +	if (!pl)
   1.671 +	{
   1.672 +		return;
   1.673 +	}
   1.674 +
   1.675 +	pl->m_Cvars.push_back(pCvar);
   1.676 +}
   1.677 +
   1.678 +void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   1.679 +{
   1.680 +	CPlugin *pl = FindByAPI(api);
   1.681 +
   1.682 +	if (!pl)
   1.683 +	{
   1.684 +		return;
   1.685 +	}
   1.686 +
   1.687 +	pl->m_Cmds.push_back(pCmd);
   1.688 +}
   1.689 +
   1.690 +void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   1.691 +{
   1.692 +	CPlugin *pl = FindByAPI(api);
   1.693 +
   1.694 +	if (!pl)
   1.695 +	{
   1.696 +		return;
   1.697 +	}
   1.698 +
   1.699 +	pl->m_Cvars.remove(pCvar);
   1.700 +}
   1.701 +
   1.702 +void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   1.703 +{
   1.704 +	CPlugin *pl = FindByAPI(api);
   1.705 +
   1.706 +	if (!pl)
   1.707 +	{
   1.708 +		return;
   1.709 +	}
   1.710 +
   1.711 +	pl->m_Cmds.remove(pCmd);
   1.712 +}
   1.713 +
   1.714 +void CPluginManager::UnregAllConCmds(CPlugin *pl)
   1.715 +{
   1.716 +#if 0
   1.717 +	SourceHook::List<ConCommandBase *>::iterator i;
   1.718 +
   1.719 +	/* :TODO: */
   1.720 +	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   1.721 +	{
   1.722 +		g_SMConVarAccessor.Unregister(pl->m_Id, (*i) );
   1.723 +	}
   1.724 +
   1.725 +	pl->m_Cvars.clear();
   1.726 +
   1.727 +	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   1.728 +	{
   1.729 +		g_SMConVarAccessor.Unregister(pl->m_Id, (*i) );
   1.730 +	}
   1.731 +
   1.732 +	pl->m_Cmds.clear();
   1.733 +#endif
   1.734 +}