core/metamod_plugins.cpp
author Fyren <fyrenmoo@gmail.com>
Wed Apr 28 23:19:40 2010 -0700 (2010-04-28)
changeset 699 dbe0a8a879e2
parent 677 3ec9092c5dc8
child 722 78f730f91439
permissions -rw-r--r--
Updated Makefile for latest OB Valve (bug 4372, r=dvander, a=blocking).
     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 >= 0 && 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 				}
   469 #endif
   470 				pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
   471 			}
   472 
   473 			/**
   474 			 * If we didn't get anything, try the normal/simple interface.
   475 			 */
   476 			if (pl->m_API == NULL)
   477 			{
   478 				CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   479 				if (!pfn)
   480 				{
   481 					if (error)
   482 					{
   483 						UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   484 					}
   485 					pl->m_Status = Pl_Error;
   486 				}
   487 				else
   488 				{
   489 					pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
   490 
   491 					if (!pl->m_API)
   492 					{
   493 						if (error)
   494 						{
   495 							UTIL_Format(error, maxlen, "Failed to get API");
   496 						}
   497 						pl->m_Status = Pl_Error;
   498 					}
   499 				}
   500 			}
   501 
   502 			if (pl->m_API != NULL)
   503 			{
   504 				int api = pl->m_API->GetApiVersion();
   505 				if (api < PLAPI_MIN_VERSION)
   506 				{
   507 					if (error)
   508 					{
   509 						if (api == 13)
   510 						{
   511 							UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
   512 						}
   513 						else if (api <= 12 && api >= 7)
   514 						{
   515 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
   516 						}
   517 						else
   518 						{
   519 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
   520 						}
   521 					}
   522 					pl->m_Status = Pl_Error;
   523 				}
   524 				else if (api > METAMOD_PLAPI_VERSION)
   525 				{
   526 					if (error)
   527 					{
   528 						UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
   529 					}
   530 					pl->m_Status = Pl_Error;
   531 				}
   532 				else
   533 				{
   534 					if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   535 					{
   536 						pl->m_Status = Pl_Running;
   537 						if (m_AllLoaded)
   538 						{
   539 							pl->m_API->AllPluginsLoaded();
   540 						}
   541 					}
   542 					else
   543 					{
   544 						pl->m_Status = Pl_Refused;
   545 					}
   546 				}
   547 			}
   548 		}
   549 	}
   550 
   551 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   552 	{
   553 		pl->m_Events.clear();
   554 		g_SourceHook.UnloadPlugin(pl->m_Id);
   555 		UnregAllConCmds(pl);
   556 
   557 		if (pl->m_UnloadFn != NULL)
   558 		{
   559 			pl->m_UnloadFn();
   560 		}
   561 
   562 		dlclose(pl->m_Lib);
   563 		pl->m_Lib = NULL;
   564 		pl->m_API = NULL;
   565 	}
   566 
   567 	return pl;
   568 }
   569 
   570 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   571 {
   572 	if (error)
   573 	{
   574 		*error = '\0';
   575 	}
   576 
   577 	if (pl->m_API && pl->m_Lib)
   578 	{
   579 		//Note, we'll always tell the plugin it will be unloading...
   580 		if (pl->m_API->Unload(error, maxlen) || force)
   581 		{
   582 			//Make sure to detach it from sourcehook!
   583 			g_SourceHook.UnloadPlugin(pl->m_Id);
   584 
   585 			pl->m_Events.clear();
   586 
   587 			UnregAllConCmds(pl);
   588 
   589 			if (pl->m_UnloadFn != NULL)
   590 			{
   591 				pl->m_UnloadFn();
   592 			}
   593 
   594 			//Clean up the DLL
   595 			dlclose(pl->m_Lib);
   596 			pl->m_Lib = NULL;
   597 			pl->m_API = NULL;
   598 
   599 			//Remove the plugin from the list
   600 			PluginIter i;
   601 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   602 			{
   603 				if ( (*i)->m_Id == pl->m_Id )
   604 				{
   605 					i = m_Plugins.erase(i);
   606 					break;
   607 				}
   608 			}
   609 			//Free its memory
   610 			delete pl;
   611 
   612 			return true;
   613 		}
   614 	} else {
   615 		//The plugin is not valid, and let's just remove it from the list anyway
   616 		PluginIter i;
   617 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   618 		{
   619 			if ( (*i)->m_Id == pl->m_Id )
   620 			{
   621 				i = m_Plugins.erase(i);
   622 				break;
   623 			}
   624 		}
   625 		delete pl;
   626 
   627 		return true;
   628 	}
   629 
   630 	return false;
   631 }
   632 
   633 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   634 {
   635 	if (error)
   636 	{
   637 		*error = '\0';
   638 	}
   639 
   640 	if (pl->m_Status != Pl_Running || !pl->m_API)
   641 	{
   642 		if (error)
   643 		{
   644 			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   645 		}
   646 	}
   647 	else
   648 	{
   649 		if (pl->m_API->Pause(error, maxlen))
   650 		{
   651 			g_SourceHook.PausePlugin(pl->m_Id);
   652 			pl->m_Status = Pl_Paused;
   653 
   654 			return true;
   655 		}
   656 	}
   657 
   658 	return false;
   659 }
   660 
   661 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   662 {
   663 	if (error)
   664 	{
   665 		*error = '\0';
   666 	}
   667 
   668 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   669 	{
   670 		if (error)
   671 		{
   672 			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   673 		}
   674 	}
   675 	else
   676 	{
   677 		if (pl->m_API->Unpause(error, maxlen))
   678 		{
   679 			g_SourceHook.UnpausePlugin(pl->m_Id);
   680 			pl->m_Status = Pl_Running;
   681 
   682 			return true;
   683 		}
   684 	}
   685 
   686 	return false;
   687 }
   688 
   689 bool CPluginManager::UnloadAll()
   690 {
   691 	PluginIter i;
   692 
   693 	char error[128];
   694 	bool status = true;
   695 
   696 	while ((i = m_Plugins.begin()) != m_Plugins.end())
   697 	{
   698 		if ( !_Unload( (*i), true, error, sizeof(error)) )
   699 		{
   700 			status = false;
   701 		}
   702 	}
   703 
   704 	return status;
   705 }
   706 
   707 bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
   708 {
   709 	CPlugin *pl = FindById(id);
   710 
   711 	if (!pl)
   712 	{
   713 		return false;
   714 	}
   715 
   716 	if (file != NULL)
   717 	{
   718 		*file = pl->m_File.c_str();
   719 	}
   720 
   721 	if (status != NULL)
   722 	{
   723 		*status = pl->m_Status;
   724 	}
   725 
   726 	if (source != NULL)
   727 	{
   728 		*source = pl->m_Source;
   729 	}
   730 
   731 	return true;
   732 }
   733 
   734 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   735 {
   736 	CPlugin *pl = FindById(id);
   737 
   738 	if (!pl || !pl->m_API)
   739 	{
   740 		if (error)
   741 		{
   742 			UTIL_Format(error, maxlength, "Plugin not valid");
   743 		}
   744 		return false;
   745 	}
   746 
   747 	return pl->m_API->QueryRunning(error, maxlength);
   748 }
   749 
   750 bool CPluginManager::QueryHandle(PluginId id, void **handle)
   751 {
   752 	CPlugin *pl = FindById(id);
   753 
   754 	if (!pl)
   755 	{
   756 		return false;
   757 	}
   758 
   759 	if (handle)
   760 	{
   761 		*handle = static_cast<void *>(pl->m_Lib);
   762 	}
   763 
   764 	return true;
   765 }
   766 
   767 PluginIter CPluginManager::_begin()
   768 {
   769 	return m_Plugins.begin();
   770 }
   771 
   772 PluginIter CPluginManager::_end()
   773 {
   774 	return m_Plugins.end();
   775 }
   776 
   777 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   778 {
   779 	CPlugin *pl = FindByAPI(api);
   780 
   781 	if (!pl)
   782 	{
   783 		return;
   784 	}
   785 
   786 	pl->m_Cvars.push_back(pCvar);
   787 }
   788 
   789 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   790 {
   791 	CPlugin *pl = FindByAPI(api);
   792 
   793 	if (!pl)
   794 	{
   795 		return;
   796 	}
   797 
   798 	pl->m_Cmds.push_back(pCmd);
   799 }
   800 
   801 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   802 {
   803 	CPlugin *pl = FindByAPI(api);
   804 
   805 	if (!pl)
   806 	{
   807 		return;
   808 	}
   809 
   810 	pl->m_Cvars.remove(pCvar);
   811 }
   812 
   813 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   814 {
   815 	CPlugin *pl = FindByAPI(api);
   816 
   817 	if (!pl)
   818 	{
   819 		return;
   820 	}
   821 
   822 	pl->m_Cmds.remove(pCmd);
   823 }
   824 
   825 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   826 {
   827 	SourceHook::List<ConCommandBase *>::iterator i;
   828 
   829 	/* :TODO: */
   830 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   831 	{
   832 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   833 	}
   834 	pl->m_Cvars.clear();
   835 
   836 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   837 	{
   838 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   839 	}
   840 	pl->m_Cmds.clear();
   841 }
   842