core/metamod_plugins.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sun Jul 22 09:10:36 2012 -0400 (2012-07-22)
changeset 789 53d15f6faed7
parent 768 e6394d70ced0
child 850 3d9258ef9a30
permissions -rw-r--r--
Triggering another build for bug 5391.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2009 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  */
    25 
    26 #include <stdio.h>
    27 #include "metamod_oslink.h"
    28 #include "metamod.h"
    29 #include "metamod_plugins.h"
    30 #include "metamod_util.h"
    31 
    32 /** 
    33  * @brief Implements functions from CPlugin.h
    34  * @file CPlugin.cpp
    35  */
    36 
    37 using namespace SourceMM;
    38 
    39 #define ITER_PLEVENT(evn, plid) \
    40 	CPluginManager::CPlugin *_Xpl; \
    41 	SourceHook::List<IMetamodListener *>::iterator event; \
    42 	IMetamodListener *api; \
    43 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    44 		_Xpl = (*iter); \
    45 		if (_Xpl->m_Id == plid) \
    46 			continue; \
    47 		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    48 			api = (*event); \
    49 			api->evn(plid); \
    50 		} \
    51 	}
    52 
    53 CPluginManager g_PluginMngr;
    54 
    55 MetamodVersionInfo GlobVersionInfo = 
    56 {
    57 	METAMOD_API_MAJOR,
    58 	METAMOD_API_MINOR,
    59 	SH_IFACE_VERSION,
    60 	SH_IMPL_VERSION,
    61 	PLAPI_MIN_VERSION,
    62 	METAMOD_PLAPI_VERSION,
    63 	SOURCE_ENGINE_UNKNOWN,
    64 	NULL
    65 };
    66 
    67 CPluginManager::CPluginManager()
    68 {
    69 	m_LastId = Pl_MinId;
    70 	m_AllLoaded = false;
    71 }
    72 
    73 CPluginManager::~CPluginManager()
    74 {
    75 	SourceHook::List<CNameAlias *>::iterator iter;
    76 
    77 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    78 	{
    79 		delete (*iter);
    80 	}
    81 
    82 	m_Aliases.clear();
    83 }
    84 
    85 const char *CPluginManager::LookupAlias(const char *alias)
    86 {
    87 	SourceHook::List<CNameAlias *>::iterator iter;
    88 	CNameAlias *p;
    89 
    90 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    91 	{
    92 		p = (*iter);
    93 		if (p->alias.compare(alias) == 0)
    94 		{
    95 			return p->value.c_str();
    96 		}
    97 	}
    98 
    99 	return NULL;
   100 }
   101 
   102 SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_begin()
   103 {
   104 	return m_Aliases.begin();
   105 }
   106 
   107 SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_end()
   108 {
   109 	return m_Aliases.end();
   110 }
   111 
   112 void CPluginManager::SetAlias(const char *alias, const char *value)
   113 {
   114 	SourceHook::List<CNameAlias *>::iterator iter;
   115 	CNameAlias *p;
   116 
   117 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
   118 	{
   119 		p = (*iter);
   120 		if (p->alias.compare(alias) == 0)
   121 		{
   122 			if (value[0] == '\0')
   123 			{
   124 				iter = m_Aliases.erase(iter);
   125 				return;
   126 			}
   127 			else
   128 			{
   129 				p->value.assign(value);
   130 				return;
   131 			}
   132 		}
   133 	}
   134 
   135 	if (value[0] != '\0')
   136 	{
   137 		p = new CNameAlias;
   138 	
   139 		p->alias.assign(alias);
   140 		p->value.assign(value);
   141 	
   142 		m_Aliases.push_back(p);
   143 	}
   144 }
   145 
   146 CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL), m_UnloadFn(NULL)
   147 {
   148 
   149 }
   150 
   151 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
   152 {
   153 	already = false;
   154 	//Check if we're about to reload an old plugin
   155 	PluginIter i = m_Plugins.begin();
   156 	while (i != m_Plugins.end())
   157 	{
   158 		if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
   159 		{
   160 			if ((*i)->m_Status < Pl_Paused)
   161 			{
   162 				//Attempt to load the plugin again
   163 				already = true;
   164 				i = m_Plugins.erase(i);
   165 				continue;
   166 			}
   167 			else
   168 			{
   169 				//No need to load it
   170 				already = true;
   171 				return (*i)->m_Id;
   172 			}
   173 		}
   174 		i++;
   175 	}
   176 
   177 	CPlugin *pl = _Load(file, source, error, maxlen);
   178 
   179 	if (!pl)
   180 	{
   181 		return Pl_BadLoad;
   182 	}
   183 
   184 	ITER_PLEVENT(OnPluginLoad, pl->m_Id);
   185 
   186 	return pl->m_Id;
   187 }
   188 
   189 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
   190 {
   191 	PluginIter i;
   192 
   193 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   194 	{
   195 		if ( (*i)->m_Id == id )\
   196 		{
   197 			return (*i);
   198 		}
   199 	}
   200 
   201 	return NULL;
   202 }
   203 
   204 void CPluginManager::SetAllLoaded()
   205 {
   206 	if (m_AllLoaded)
   207 	{
   208 		return;
   209 	}
   210 
   211 	m_AllLoaded = true;
   212 	PluginIter i;
   213 
   214 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   215 	{
   216 		if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
   217 		{
   218 			//API 4 is when we added this callback
   219 			//Min version is now 5, so we ignore this check
   220 			//if ( (*i)->m_API->GetApiVersion() >= 004 )
   221 			(*i)->m_API->AllPluginsLoaded();
   222 		}
   223 	}
   224 }
   225 
   226 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
   227 {
   228 	CPlugin *pl = FindById(id);
   229 	
   230 	if (!pl)
   231 	{
   232 		UTIL_Format(error, maxlen, "Plugin id not found");
   233 		return false;
   234 	}
   235 
   236 	bool ret;
   237 	
   238 	if ((ret=_Pause(pl, error, maxlen)) == true)
   239 	{
   240 		ITER_PLEVENT(OnPluginPause, pl->m_Id);
   241 	}
   242 
   243 	return ret;
   244 }
   245 
   246 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
   247 {
   248 	CPlugin *pl = FindById(id);
   249 	
   250 	if (!pl)
   251 	{
   252 		UTIL_Format(error, maxlen, "Plugin id not found");
   253 		return false;
   254 	}
   255 
   256 	bool ret;
   257 	
   258 	if ( (ret=_Unpause(pl, error, maxlen)) == true )
   259 	{
   260 		ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
   261 	}
   262 
   263 	return ret;
   264 }
   265 
   266 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
   267 {
   268 	CPlugin *pl = FindById(id);
   269 	
   270 	if (!pl)
   271 	{
   272 		UTIL_Format(error, maxlen, "Plugin %d not found", id);
   273 		return false;
   274 	}
   275 
   276 	bool ret;
   277 	PluginId old_id = pl->m_Id;
   278 	if ( (ret=_Unload(pl, force, error, maxlen)) == true )
   279 	{
   280 		ITER_PLEVENT(OnPluginUnload, old_id);
   281 	}
   282 
   283 	return ret;
   284 }
   285 
   286 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
   287 {
   288 	PluginIter i;
   289 	char buffer[64];
   290 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   291 	{
   292 		if ( (*i) && (*i)->m_Id == id )
   293 		{
   294 			if ( (*i)->m_Status >= Pl_Paused)
   295 			{
   296 				UTIL_Format(error, len, "Plugin %d is already running.", id);
   297 				return false;
   298 			}
   299 			CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
   300 			if (!pl)
   301 			{
   302 				return false;
   303 			}
   304 			if (pl->m_Status >= Pl_Paused)
   305 			{
   306 				//Now it gets crazy... unload the original copy.
   307 				_Unload( (*i), true, buffer, sizeof(buffer)-1 );
   308 				
   309 				//Set the new copy's id
   310 				pl->m_Id = id;
   311 
   312 				//We just wasted an id... reclaim it
   313 				m_LastId--;
   314 				
   315 				return true;
   316 			}
   317 			else
   318 			{
   319 				//don't really care about the buffer here
   320 				_Unload(pl, true, buffer, sizeof(buffer)-1);
   321 
   322 				//We just wasted an id... reclaim it
   323 				m_LastId--;
   324 				return false;
   325 			}
   326 		}
   327 	}
   328 
   329 	UTIL_Format(error, len, "Plugin %d not found,", id);
   330 	return false;
   331 }
   332 
   333 CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
   334 {
   335 	PluginIter i;
   336 
   337 	//don't find bad plugins!
   338 	if (!api)
   339 	{
   340 		return NULL;
   341 	}
   342 
   343 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   344 	{
   345 		if ((*i)->m_API == api)
   346 		{
   347 			return (*i);
   348 		}
   349 	}
   350 
   351 	return NULL;
   352 }
   353 
   354 int CPluginManager::GetPluginCount()
   355 {
   356 	return (int)m_Plugins.size();
   357 }
   358 
   359 const char *CPluginManager::GetStatusText(CPlugin *pl)
   360 {
   361 	switch (pl->m_Status)
   362 	{
   363 	case Pl_NotFound:
   364 		return "NOFILE";
   365 	case Pl_Error:
   366 		return "ERROR";
   367 	case Pl_Refused:
   368 		return "FAILED";
   369 	case Pl_Paused:
   370 		return "PAUSED";
   371 	case Pl_Running:
   372 		{
   373 			if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   374 			{
   375 				return "STOPPED";
   376 			} else {
   377 				return "RUNNING";
   378 			}
   379 		}
   380 	default:
   381 		return "-";
   382 	}
   383 }
   384 
   385 struct Unloader : public SourceHook::Impl::UnloadListener
   386 {
   387 	CPluginManager::CPlugin *plugin_;
   388 	bool destroy_;
   389 
   390 	Unloader(CPluginManager::CPlugin *plugin, bool destroy)
   391 	  : plugin_(plugin), destroy_(destroy)
   392 	{ }
   393 
   394 	void ReadyToUnload(SourceHook::Plugin plug)
   395 	{
   396 		if (plugin_->m_UnloadFn != NULL)
   397 			plugin_->m_UnloadFn();
   398 
   399 		dlclose(plugin_->m_Lib);
   400 
   401 		if (destroy_)
   402 		{
   403 			delete plugin_;
   404 		}
   405 		else
   406 		{
   407 			plugin_->m_Lib = NULL;
   408 			plugin_->m_API = NULL;
   409 		}
   410 
   411 		delete this;
   412 	}
   413 };
   414 
   415 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   416 {
   417 	FILE *fp;
   418 	CPlugin *pl;
   419 
   420 	pl = new CPlugin();
   421 	*error = '\0';
   422 	
   423 	//Add plugin to list
   424 	pl->m_Id = m_LastId;
   425 	pl->m_File.assign(file);
   426 	m_Plugins.push_back(pl);
   427 	m_LastId++;
   428 
   429 	//Check if the file even exists
   430 	fp = fopen(file, "r");
   431 	if (!fp)
   432 	{
   433 		if (error)
   434 		{
   435 			UTIL_Format(error, maxlen, "File not found: %s", file);
   436 		}
   437 		pl->m_Status = Pl_NotFound;
   438 	}
   439 
   440 	if (fp)
   441 	{
   442 		fclose(fp);
   443 		fp = NULL;
   444 		
   445 		//Load the file
   446 		pl->m_Lib = dlmount(file);
   447 		if (!pl->m_Lib)
   448 		{
   449 			if (error)
   450 			{
   451 				UTIL_Format(error, maxlen, "%s", dlerror());
   452 			}
   453 			pl->m_Status = Pl_Error;
   454 		}
   455 		else
   456 		{
   457 			pl->m_API = NULL;
   458 			
   459 			/**
   460 			 * First, try the new "advanced" interface
   461 			 */
   462 			METAMOD_FN_LOAD fnLoad = (METAMOD_FN_LOAD)dlsym(pl->m_Lib, "CreateInterface_MMS");
   463 			if (fnLoad != NULL)
   464 			{
   465 				if (GlobVersionInfo.source_engine == SOURCE_ENGINE_UNKNOWN)
   466 				{
   467 					GlobVersionInfo.source_engine = g_Metamod.GetSourceEngineBuild();
   468 				}
   469 				if (GlobVersionInfo.game_dir == NULL)
   470 				{
   471 					GlobVersionInfo.game_dir = strrchr(g_Metamod.GetBaseDir(), PATH_SEP_CHAR) + 1;
   472 				}
   473 
   474 				/* Build path information */
   475 				char file_path[256];
   476 				size_t len = g_Metamod.PathFormat(file_path, sizeof(file_path), "%s", file);
   477 
   478 				for (size_t i = len - 1; i < len; i--)
   479 				{
   480 					if (_IsPathSepChar(file_path[i]))
   481 					{
   482 						file_path[i] = '\0';
   483 						break;
   484 					}
   485 				}
   486 
   487 				MetamodLoaderInfo info;
   488 				info.pl_file = file;
   489 				info.pl_path = file_path;
   490 
   491 				pl->m_API = fnLoad(&GlobVersionInfo, &info);
   492 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
   493 				/* For plugin compat - try loading again using original OB if OB-Valve has failed */
   494 				if (pl->m_API == NULL)
   495 				{
   496 					GlobVersionInfo.source_engine = SOURCE_ENGINE_ORANGEBOX;
   497 					pl->m_API = fnLoad(&GlobVersionInfo, &info);
   498 					GlobVersionInfo.source_engine = SOURCE_ENGINE_ORANGEBOXVALVE;
   499 				}
   500 #endif
   501 				pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
   502 			}
   503 
   504 			/**
   505 			 * If we didn't get anything, try the normal/simple interface.
   506 			 */
   507 			if (pl->m_API == NULL)
   508 			{
   509 				CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   510 				if (!pfn)
   511 				{
   512 					if (error)
   513 					{
   514 						UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   515 					}
   516 					pl->m_Status = Pl_Error;
   517 				}
   518 				else
   519 				{
   520 					pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
   521 
   522 					if (!pl->m_API)
   523 					{
   524 						if (error)
   525 						{
   526 							UTIL_Format(error, maxlen, "Failed to get API");
   527 						}
   528 						pl->m_Status = Pl_Error;
   529 					}
   530 				}
   531 			}
   532 
   533 			if (pl->m_API != NULL)
   534 			{
   535 				int api = pl->m_API->GetApiVersion();
   536 				if (api < PLAPI_MIN_VERSION)
   537 				{
   538 					if (error)
   539 					{
   540 						if (api == 13)
   541 						{
   542 							UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
   543 						}
   544 						else if (api <= 12 && api >= 7)
   545 						{
   546 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
   547 						}
   548 						else
   549 						{
   550 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
   551 						}
   552 					}
   553 					pl->m_Status = Pl_Error;
   554 				}
   555 				else if (api > METAMOD_PLAPI_VERSION)
   556 				{
   557 					if (error)
   558 					{
   559 						UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
   560 					}
   561 					pl->m_Status = Pl_Error;
   562 				}
   563 				else
   564 				{
   565 					if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   566 					{
   567 						pl->m_Status = Pl_Running;
   568 						if (m_AllLoaded)
   569 						{
   570 							pl->m_API->AllPluginsLoaded();
   571 						}
   572 					}
   573 					else
   574 					{
   575 						pl->m_Status = Pl_Refused;
   576 					}
   577 				}
   578 			}
   579 		}
   580 	}
   581 
   582 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   583 	{
   584 		pl->m_Events.clear();
   585 		UnregAllConCmds(pl);
   586 		g_SourceHook.UnloadPlugin(pl->m_Id, new Unloader(pl, false));
   587 	}
   588 
   589 	return pl;
   590 }
   591 
   592 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   593 {
   594 	if (error)
   595 	{
   596 		*error = '\0';
   597 	}
   598 
   599 	if (pl->m_API && pl->m_Lib)
   600 	{
   601 		//Note, we'll always tell the plugin it will be unloading...
   602 		if (pl->m_API->Unload(error, maxlen) || force)
   603 		{
   604 			pl->m_Events.clear();
   605 			UnregAllConCmds(pl);
   606 
   607 			//Remove the plugin from the list
   608 			PluginIter i;
   609 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   610 			{
   611 				if ( (*i)->m_Id == pl->m_Id )
   612 				{
   613 					i = m_Plugins.erase(i);
   614 					break;
   615 				}
   616 			}
   617 
   618 			//Make sure to detach it from sourcehook!
   619 			g_SourceHook.UnloadPlugin(pl->m_Id, new Unloader(pl, true));
   620 			return true;
   621 		}
   622 	} else {
   623 		//The plugin is not valid, and let's just remove it from the list anyway
   624 		PluginIter i;
   625 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   626 		{
   627 			if ( (*i)->m_Id == pl->m_Id )
   628 			{
   629 				i = m_Plugins.erase(i);
   630 				break;
   631 			}
   632 		}
   633 		delete pl;
   634 
   635 		return true;
   636 	}
   637 
   638 	return false;
   639 }
   640 
   641 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   642 {
   643 	if (error)
   644 	{
   645 		*error = '\0';
   646 	}
   647 
   648 	if (pl->m_Status != Pl_Running || !pl->m_API)
   649 	{
   650 		if (error)
   651 		{
   652 			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   653 		}
   654 	}
   655 	else
   656 	{
   657 		if (pl->m_API->Pause(error, maxlen))
   658 		{
   659 			g_SourceHook.PausePlugin(pl->m_Id);
   660 			pl->m_Status = Pl_Paused;
   661 
   662 			return true;
   663 		}
   664 	}
   665 
   666 	return false;
   667 }
   668 
   669 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   670 {
   671 	if (error)
   672 	{
   673 		*error = '\0';
   674 	}
   675 
   676 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   677 	{
   678 		if (error)
   679 		{
   680 			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   681 		}
   682 	}
   683 	else
   684 	{
   685 		if (pl->m_API->Unpause(error, maxlen))
   686 		{
   687 			g_SourceHook.UnpausePlugin(pl->m_Id);
   688 			pl->m_Status = Pl_Running;
   689 
   690 			return true;
   691 		}
   692 	}
   693 
   694 	return false;
   695 }
   696 
   697 bool CPluginManager::UnloadAll()
   698 {
   699 	PluginIter i;
   700 
   701 	char error[128];
   702 	bool status = true;
   703 
   704 	while ((i = m_Plugins.begin()) != m_Plugins.end())
   705 	{
   706 		if ( !_Unload( (*i), true, error, sizeof(error)) )
   707 		{
   708 			status = false;
   709 		}
   710 	}
   711 
   712 	return status;
   713 }
   714 
   715 bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
   716 {
   717 	CPlugin *pl = FindById(id);
   718 
   719 	if (!pl)
   720 	{
   721 		return false;
   722 	}
   723 
   724 	if (file != NULL)
   725 	{
   726 		*file = pl->m_File.c_str();
   727 	}
   728 
   729 	if (status != NULL)
   730 	{
   731 		*status = pl->m_Status;
   732 	}
   733 
   734 	if (source != NULL)
   735 	{
   736 		*source = pl->m_Source;
   737 	}
   738 
   739 	return true;
   740 }
   741 
   742 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   743 {
   744 	CPlugin *pl = FindById(id);
   745 
   746 	if (!pl || !pl->m_API)
   747 	{
   748 		if (error)
   749 		{
   750 			UTIL_Format(error, maxlength, "Plugin not valid");
   751 		}
   752 		return false;
   753 	}
   754 
   755 	return pl->m_API->QueryRunning(error, maxlength);
   756 }
   757 
   758 bool CPluginManager::QueryHandle(PluginId id, void **handle)
   759 {
   760 	CPlugin *pl = FindById(id);
   761 
   762 	if (!pl)
   763 	{
   764 		return false;
   765 	}
   766 
   767 	if (handle)
   768 	{
   769 		*handle = static_cast<void *>(pl->m_Lib);
   770 	}
   771 
   772 	return true;
   773 }
   774 
   775 PluginIter CPluginManager::_begin()
   776 {
   777 	return m_Plugins.begin();
   778 }
   779 
   780 PluginIter CPluginManager::_end()
   781 {
   782 	return m_Plugins.end();
   783 }
   784 
   785 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   786 {
   787 	CPlugin *pl = FindByAPI(api);
   788 
   789 	if (!pl)
   790 	{
   791 		return;
   792 	}
   793 
   794 	pl->m_Cvars.push_back(pCvar);
   795 }
   796 
   797 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   798 {
   799 	CPlugin *pl = FindByAPI(api);
   800 
   801 	if (!pl)
   802 	{
   803 		return;
   804 	}
   805 
   806 	pl->m_Cmds.push_back(pCmd);
   807 }
   808 
   809 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   810 {
   811 	CPlugin *pl = FindByAPI(api);
   812 
   813 	if (!pl)
   814 	{
   815 		return;
   816 	}
   817 
   818 	pl->m_Cvars.remove(pCvar);
   819 }
   820 
   821 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   822 {
   823 	CPlugin *pl = FindByAPI(api);
   824 
   825 	if (!pl)
   826 	{
   827 		return;
   828 	}
   829 
   830 	pl->m_Cmds.remove(pCmd);
   831 }
   832 
   833 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   834 {
   835 	SourceHook::List<ConCommandBase *>::iterator i;
   836 
   837 	/* :TODO: */
   838 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   839 	{
   840 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   841 	}
   842 	pl->m_Cvars.clear();
   843 
   844 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   845 	{
   846 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   847 	}
   848 	pl->m_Cmds.clear();
   849 }
   850