core/metamod_plugins.cpp
author Fyren <fyrenmoo@gmail.com>
Wed Sep 21 00:19:21 2011 -0700 (2011-09-21)
changeset 765 44703fe45c60
parent 747 9563de0165f7
child 768 e6394d70ced0
permissions -rw-r--r--
Triggering build.
     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 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   386 {
   387 	FILE *fp;
   388 	CPlugin *pl;
   389 
   390 	pl = new CPlugin();
   391 	*error = '\0';
   392 	
   393 	//Add plugin to list
   394 	pl->m_Id = m_LastId;
   395 	pl->m_File.assign(file);
   396 	m_Plugins.push_back(pl);
   397 	m_LastId++;
   398 
   399 	//Check if the file even exists
   400 	fp = fopen(file, "r");
   401 	if (!fp)
   402 	{
   403 		if (error)
   404 		{
   405 			UTIL_Format(error, maxlen, "File not found: %s", file);
   406 		}
   407 		pl->m_Status = Pl_NotFound;
   408 	}
   409 
   410 	if (fp)
   411 	{
   412 		fclose(fp);
   413 		fp = NULL;
   414 		
   415 		//Load the file
   416 		pl->m_Lib = dlmount(file);
   417 		if (!pl->m_Lib)
   418 		{
   419 			if (error)
   420 			{
   421 				UTIL_Format(error, maxlen, "%s", dlerror());
   422 			}
   423 			pl->m_Status = Pl_Error;
   424 		}
   425 		else
   426 		{
   427 			pl->m_API = NULL;
   428 			
   429 			/**
   430 			 * First, try the new "advanced" interface
   431 			 */
   432 			METAMOD_FN_LOAD fnLoad = (METAMOD_FN_LOAD)dlsym(pl->m_Lib, "CreateInterface_MMS");
   433 			if (fnLoad != NULL)
   434 			{
   435 				if (GlobVersionInfo.source_engine == SOURCE_ENGINE_UNKNOWN)
   436 				{
   437 					GlobVersionInfo.source_engine = g_Metamod.GetSourceEngineBuild();
   438 				}
   439 				if (GlobVersionInfo.game_dir == NULL)
   440 				{
   441 					GlobVersionInfo.game_dir = strrchr(g_Metamod.GetBaseDir(), PATH_SEP_CHAR) + 1;
   442 				}
   443 
   444 				/* Build path information */
   445 				char file_path[256];
   446 				size_t len = g_Metamod.PathFormat(file_path, sizeof(file_path), "%s", file);
   447 
   448 				for (size_t i = len - 1; i < len; i--)
   449 				{
   450 					if (_IsPathSepChar(file_path[i]))
   451 					{
   452 						file_path[i] = '\0';
   453 						break;
   454 					}
   455 				}
   456 
   457 				MetamodLoaderInfo info;
   458 				info.pl_file = file;
   459 				info.pl_path = file_path;
   460 
   461 				pl->m_API = fnLoad(&GlobVersionInfo, &info);
   462 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
   463 				/* For plugin compat - try loading again using original OB if OB-Valve has failed */
   464 				if (pl->m_API == NULL)
   465 				{
   466 					GlobVersionInfo.source_engine = SOURCE_ENGINE_ORANGEBOX;
   467 					pl->m_API = fnLoad(&GlobVersionInfo, &info);
   468 					GlobVersionInfo.source_engine = SOURCE_ENGINE_ORANGEBOXVALVE;
   469 				}
   470 #endif
   471 				pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
   472 			}
   473 
   474 			/**
   475 			 * If we didn't get anything, try the normal/simple interface.
   476 			 */
   477 			if (pl->m_API == NULL)
   478 			{
   479 				CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   480 				if (!pfn)
   481 				{
   482 					if (error)
   483 					{
   484 						UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   485 					}
   486 					pl->m_Status = Pl_Error;
   487 				}
   488 				else
   489 				{
   490 					pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
   491 
   492 					if (!pl->m_API)
   493 					{
   494 						if (error)
   495 						{
   496 							UTIL_Format(error, maxlen, "Failed to get API");
   497 						}
   498 						pl->m_Status = Pl_Error;
   499 					}
   500 				}
   501 			}
   502 
   503 			if (pl->m_API != NULL)
   504 			{
   505 				int api = pl->m_API->GetApiVersion();
   506 				if (api < PLAPI_MIN_VERSION)
   507 				{
   508 					if (error)
   509 					{
   510 						if (api == 13)
   511 						{
   512 							UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
   513 						}
   514 						else if (api <= 12 && api >= 7)
   515 						{
   516 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
   517 						}
   518 						else
   519 						{
   520 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
   521 						}
   522 					}
   523 					pl->m_Status = Pl_Error;
   524 				}
   525 				else if (api > METAMOD_PLAPI_VERSION)
   526 				{
   527 					if (error)
   528 					{
   529 						UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
   530 					}
   531 					pl->m_Status = Pl_Error;
   532 				}
   533 				else
   534 				{
   535 					if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   536 					{
   537 						pl->m_Status = Pl_Running;
   538 						if (m_AllLoaded)
   539 						{
   540 							pl->m_API->AllPluginsLoaded();
   541 						}
   542 					}
   543 					else
   544 					{
   545 						pl->m_Status = Pl_Refused;
   546 					}
   547 				}
   548 			}
   549 		}
   550 	}
   551 
   552 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   553 	{
   554 		pl->m_Events.clear();
   555 		g_SourceHook.UnloadPlugin(pl->m_Id);
   556 		UnregAllConCmds(pl);
   557 
   558 		if (pl->m_UnloadFn != NULL)
   559 		{
   560 			pl->m_UnloadFn();
   561 		}
   562 
   563 		dlclose(pl->m_Lib);
   564 		pl->m_Lib = NULL;
   565 		pl->m_API = NULL;
   566 	}
   567 
   568 	return pl;
   569 }
   570 
   571 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   572 {
   573 	if (error)
   574 	{
   575 		*error = '\0';
   576 	}
   577 
   578 	if (pl->m_API && pl->m_Lib)
   579 	{
   580 		//Note, we'll always tell the plugin it will be unloading...
   581 		if (pl->m_API->Unload(error, maxlen) || force)
   582 		{
   583 			//Make sure to detach it from sourcehook!
   584 			g_SourceHook.UnloadPlugin(pl->m_Id);
   585 
   586 			pl->m_Events.clear();
   587 
   588 			UnregAllConCmds(pl);
   589 
   590 			if (pl->m_UnloadFn != NULL)
   591 			{
   592 				pl->m_UnloadFn();
   593 			}
   594 
   595 			//Clean up the DLL
   596 			dlclose(pl->m_Lib);
   597 			pl->m_Lib = NULL;
   598 			pl->m_API = NULL;
   599 
   600 			//Remove the plugin from the list
   601 			PluginIter i;
   602 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   603 			{
   604 				if ( (*i)->m_Id == pl->m_Id )
   605 				{
   606 					i = m_Plugins.erase(i);
   607 					break;
   608 				}
   609 			}
   610 			//Free its memory
   611 			delete pl;
   612 
   613 			return true;
   614 		}
   615 	} else {
   616 		//The plugin is not valid, and let's just remove it from the list anyway
   617 		PluginIter i;
   618 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   619 		{
   620 			if ( (*i)->m_Id == pl->m_Id )
   621 			{
   622 				i = m_Plugins.erase(i);
   623 				break;
   624 			}
   625 		}
   626 		delete pl;
   627 
   628 		return true;
   629 	}
   630 
   631 	return false;
   632 }
   633 
   634 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   635 {
   636 	if (error)
   637 	{
   638 		*error = '\0';
   639 	}
   640 
   641 	if (pl->m_Status != Pl_Running || !pl->m_API)
   642 	{
   643 		if (error)
   644 		{
   645 			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   646 		}
   647 	}
   648 	else
   649 	{
   650 		if (pl->m_API->Pause(error, maxlen))
   651 		{
   652 			g_SourceHook.PausePlugin(pl->m_Id);
   653 			pl->m_Status = Pl_Paused;
   654 
   655 			return true;
   656 		}
   657 	}
   658 
   659 	return false;
   660 }
   661 
   662 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   663 {
   664 	if (error)
   665 	{
   666 		*error = '\0';
   667 	}
   668 
   669 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   670 	{
   671 		if (error)
   672 		{
   673 			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   674 		}
   675 	}
   676 	else
   677 	{
   678 		if (pl->m_API->Unpause(error, maxlen))
   679 		{
   680 			g_SourceHook.UnpausePlugin(pl->m_Id);
   681 			pl->m_Status = Pl_Running;
   682 
   683 			return true;
   684 		}
   685 	}
   686 
   687 	return false;
   688 }
   689 
   690 bool CPluginManager::UnloadAll()
   691 {
   692 	PluginIter i;
   693 
   694 	char error[128];
   695 	bool status = true;
   696 
   697 	while ((i = m_Plugins.begin()) != m_Plugins.end())
   698 	{
   699 		if ( !_Unload( (*i), true, error, sizeof(error)) )
   700 		{
   701 			status = false;
   702 		}
   703 	}
   704 
   705 	return status;
   706 }
   707 
   708 bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
   709 {
   710 	CPlugin *pl = FindById(id);
   711 
   712 	if (!pl)
   713 	{
   714 		return false;
   715 	}
   716 
   717 	if (file != NULL)
   718 	{
   719 		*file = pl->m_File.c_str();
   720 	}
   721 
   722 	if (status != NULL)
   723 	{
   724 		*status = pl->m_Status;
   725 	}
   726 
   727 	if (source != NULL)
   728 	{
   729 		*source = pl->m_Source;
   730 	}
   731 
   732 	return true;
   733 }
   734 
   735 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   736 {
   737 	CPlugin *pl = FindById(id);
   738 
   739 	if (!pl || !pl->m_API)
   740 	{
   741 		if (error)
   742 		{
   743 			UTIL_Format(error, maxlength, "Plugin not valid");
   744 		}
   745 		return false;
   746 	}
   747 
   748 	return pl->m_API->QueryRunning(error, maxlength);
   749 }
   750 
   751 bool CPluginManager::QueryHandle(PluginId id, void **handle)
   752 {
   753 	CPlugin *pl = FindById(id);
   754 
   755 	if (!pl)
   756 	{
   757 		return false;
   758 	}
   759 
   760 	if (handle)
   761 	{
   762 		*handle = static_cast<void *>(pl->m_Lib);
   763 	}
   764 
   765 	return true;
   766 }
   767 
   768 PluginIter CPluginManager::_begin()
   769 {
   770 	return m_Plugins.begin();
   771 }
   772 
   773 PluginIter CPluginManager::_end()
   774 {
   775 	return m_Plugins.end();
   776 }
   777 
   778 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   779 {
   780 	CPlugin *pl = FindByAPI(api);
   781 
   782 	if (!pl)
   783 	{
   784 		return;
   785 	}
   786 
   787 	pl->m_Cvars.push_back(pCvar);
   788 }
   789 
   790 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   791 {
   792 	CPlugin *pl = FindByAPI(api);
   793 
   794 	if (!pl)
   795 	{
   796 		return;
   797 	}
   798 
   799 	pl->m_Cmds.push_back(pCmd);
   800 }
   801 
   802 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   803 {
   804 	CPlugin *pl = FindByAPI(api);
   805 
   806 	if (!pl)
   807 	{
   808 		return;
   809 	}
   810 
   811 	pl->m_Cvars.remove(pCvar);
   812 }
   813 
   814 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   815 {
   816 	CPlugin *pl = FindByAPI(api);
   817 
   818 	if (!pl)
   819 	{
   820 		return;
   821 	}
   822 
   823 	pl->m_Cmds.remove(pCmd);
   824 }
   825 
   826 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   827 {
   828 	SourceHook::List<ConCommandBase *>::iterator i;
   829 
   830 	/* :TODO: */
   831 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   832 	{
   833 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   834 	}
   835 	pl->m_Cvars.clear();
   836 
   837 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   838 	{
   839 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   840 	}
   841 	pl->m_Cmds.clear();
   842 }
   843