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