sourcemm/metamod_plugins.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue Sep 16 04:41:32 2008 -0500 (2008-09-16)
changeset 533 591b88a78e35
parent 523 6f1630519b81
child 534 eb2d7d18441c
permissions -rw-r--r--
Fixed tags for mmsource-central

mmsource-1.00-RC1 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:41:34 2006 UTC
mmsource-1.00-RC2 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:35:20 2006 UTC
mmsource-1.00 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:30:40 2006 UTC
mmsource-1.1 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:25:09 2006 UTC
mmsource-1.1.1 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:22:23 2006 UTC
mmsource-1.1.2 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:14:00 2006 UTC
mmsource-1.2 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:10:47 2006 UTC
mmsource-1.2.1 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 08:01:35 2006 UTC
mmsource-1.2.2 - Not tagged originally, but eventually done by Scott Ehlert <[email protected]> on Wed Jul 19 07:57:26 2006 UTC
mmsource-1.2.3 - Not tagged originally, but eventually done by David Anderson <[email protected]> on Wed Jul 12 17:33:59 2006 UTC
mmsource-1.3 - Originally tagged by Scott Ehlert <[email protected]> on Thu Aug 17 03:19:55 2006 UTC
mmsource-1.3a - Originally tagged by Scott Ehlert <[email protected]> on Wed Sep 6 07:17:37 2006 UTC
mmsource-1.3d - Originally tagged by Scott Ehlert <[email protected]> on Fri Feb 2 05:30:56 2007 UTC
mmsource-1.4 - Originally tagged by Scott Ehlert <[email protected]> on Sat Apr 7 05:00:28 2007 UTC
mmsource-1.6.0 - Originally tagged by David Anderson <[email protected]> on Mon Feb 18 23:31:29 2008 UTC
mmsource-1.6.1 - Originally tagged by David Anderson <[email protected]> on Mon Feb 25 17:15:44 2008 UTC
mmsource-1.6.2 - Originally tagged by David Anderson <[email protected]> on Sat Jul 26 05:04:35 2008 UTC
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 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  * Version: $Id$
    26  */
    27 
    28 #include "metamod_oslink.h"
    29 #include "metamod.h"
    30 #include "metamod_plugins.h"
    31 #include "metamod_util.h"
    32 
    33 /** 
    34  * @brief Implements functions from CPlugin.h
    35  * @file CPlugin.cpp
    36  */
    37 
    38 using namespace SourceMM;
    39 
    40 #define ITER_PLEVENT(evn, plid) \
    41 	CPluginManager::CPlugin *_Xpl; \
    42 	SourceHook::List<IMetamodListener *>::iterator event; \
    43 	IMetamodListener *api; \
    44 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    45 		_Xpl = (*iter); \
    46 		if (_Xpl->m_Id == plid) \
    47 			continue; \
    48 		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    49 			api = (*event); \
    50 			api->evn(plid); \
    51 		} \
    52 	}
    53 
    54 CPluginManager g_PluginMngr;
    55 
    56 MetamodVersionInfo GlobVersionInfo = 
    57 {
    58 	METAMOD_API_MAJOR,
    59 	METAMOD_API_MINOR,
    60 	SH_IFACE_VERSION,
    61 	SH_IMPL_VERSION,
    62 	PLAPI_MIN_VERSION,
    63 	METAMOD_PLAPI_VERSION,
    64 	SOURCE_ENGINE_UNKNOWN
    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 
   440 				/* Build path information */
   441 				char file_path[256];
   442 				size_t len = g_Metamod.PathFormat(file_path, sizeof(file_path), "%s", file);
   443 
   444 				for (size_t i = len - 1; i >= 0 && i < len; i--)
   445 				{
   446 					if (_IsPathSepChar(file_path[i]))
   447 					{
   448 						file_path[i] = '\0';
   449 						break;
   450 					}
   451 				}
   452 
   453 				MetamodLoaderInfo info;
   454 				info.pl_file = file;
   455 				info.pl_path = file_path;
   456 
   457 				pl->m_API = fnLoad(&GlobVersionInfo, &info);
   458 				pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
   459 			}
   460 
   461 			/**
   462 			 * If we didn't get anything, try the normal/simple interface.
   463 			 */
   464 			if (pl->m_API == NULL)
   465 			{
   466 				CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   467 				if (!pfn)
   468 				{
   469 					if (error)
   470 					{
   471 						UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   472 					}
   473 					pl->m_Status = Pl_Error;
   474 				}
   475 				else
   476 				{
   477 					pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
   478 
   479 					if (!pl->m_API)
   480 					{
   481 						if (error)
   482 						{
   483 							UTIL_Format(error, maxlen, "Failed to get API");
   484 						}
   485 						pl->m_Status = Pl_Error;
   486 					}
   487 				}
   488 			}
   489 
   490 			if (pl->m_API != NULL)
   491 			{
   492 				int api = pl->m_API->GetApiVersion();
   493 				if (api < PLAPI_MIN_VERSION)
   494 				{
   495 					if (error)
   496 					{
   497 						if (api == 13)
   498 						{
   499 							UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
   500 						}
   501 						else if (api <= 12 && api >= 7)
   502 						{
   503 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
   504 						}
   505 						else
   506 						{
   507 							UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
   508 						}
   509 					}
   510 					pl->m_Status = Pl_Error;
   511 				}
   512 				else if (api > METAMOD_PLAPI_VERSION)
   513 				{
   514 					if (error)
   515 					{
   516 						UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
   517 					}
   518 					pl->m_Status = Pl_Error;
   519 				}
   520 				else
   521 				{
   522 					if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   523 					{
   524 						pl->m_Status = Pl_Running;
   525 						if (m_AllLoaded)
   526 						{
   527 							pl->m_API->AllPluginsLoaded();
   528 						}
   529 					}
   530 					else
   531 					{
   532 						pl->m_Status = Pl_Refused;
   533 					}
   534 				}
   535 			}
   536 		}
   537 	}
   538 
   539 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   540 	{
   541 		pl->m_Events.clear();
   542 		g_SourceHook.UnloadPlugin(pl->m_Id);
   543 		UnregAllConCmds(pl);
   544 
   545 		if (pl->m_UnloadFn != NULL)
   546 		{
   547 			pl->m_UnloadFn();
   548 		}
   549 
   550 		dlclose(pl->m_Lib);
   551 		pl->m_Lib = NULL;
   552 		pl->m_API = NULL;
   553 	}
   554 
   555 	return pl;
   556 }
   557 
   558 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   559 {
   560 	if (error)
   561 	{
   562 		*error = '\0';
   563 	}
   564 
   565 	if (pl->m_API && pl->m_Lib)
   566 	{
   567 		//Note, we'll always tell the plugin it will be unloading...
   568 		if (pl->m_API->Unload(error, maxlen) || force)
   569 		{
   570 			//Make sure to detach it from sourcehook!
   571 			g_SourceHook.UnloadPlugin(pl->m_Id);
   572 
   573 			pl->m_Events.clear();
   574 
   575 			UnregAllConCmds(pl);
   576 
   577 			if (pl->m_UnloadFn != NULL)
   578 			{
   579 				pl->m_UnloadFn();
   580 			}
   581 
   582 			//Clean up the DLL
   583 			dlclose(pl->m_Lib);
   584 			pl->m_Lib = NULL;
   585 			pl->m_API = NULL;
   586 
   587 			//Remove the plugin from the list
   588 			PluginIter i;
   589 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   590 			{
   591 				if ( (*i)->m_Id == pl->m_Id )
   592 				{
   593 					i = m_Plugins.erase(i);
   594 					break;
   595 				}
   596 			}
   597 			//Free its memory
   598 			delete pl;
   599 
   600 			return true;
   601 		}
   602 	} else {
   603 		//The plugin is not valid, and let's just remove it from the list anyway
   604 		PluginIter i;
   605 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   606 		{
   607 			if ( (*i)->m_Id == pl->m_Id )
   608 			{
   609 				i = m_Plugins.erase(i);
   610 				break;
   611 			}
   612 		}
   613 		delete pl;
   614 
   615 		return true;
   616 	}
   617 
   618 	return false;
   619 }
   620 
   621 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   622 {
   623 	if (error)
   624 	{
   625 		*error = '\0';
   626 	}
   627 
   628 	if (pl->m_Status != Pl_Running || !pl->m_API)
   629 	{
   630 		if (error)
   631 		{
   632 			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   633 		}
   634 	}
   635 	else
   636 	{
   637 		if (pl->m_API->Pause(error, maxlen))
   638 		{
   639 			g_SourceHook.PausePlugin(pl->m_Id);
   640 			pl->m_Status = Pl_Paused;
   641 
   642 			return true;
   643 		}
   644 	}
   645 
   646 	return false;
   647 }
   648 
   649 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   650 {
   651 	if (error)
   652 	{
   653 		*error = '\0';
   654 	}
   655 
   656 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   657 	{
   658 		if (error)
   659 		{
   660 			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   661 		}
   662 	}
   663 	else
   664 	{
   665 		if (pl->m_API->Unpause(error, maxlen))
   666 		{
   667 			g_SourceHook.UnpausePlugin(pl->m_Id);
   668 			pl->m_Status = Pl_Running;
   669 
   670 			return true;
   671 		}
   672 	}
   673 
   674 	return false;
   675 }
   676 
   677 bool CPluginManager::UnloadAll()
   678 {
   679 	PluginIter i;
   680 
   681 	char error[128];
   682 	bool status = true;
   683 
   684 	while ((i = m_Plugins.begin()) != m_Plugins.end())
   685 	{
   686 		if ( !_Unload( (*i), true, error, sizeof(error)) )
   687 		{
   688 			status = false;
   689 		}
   690 	}
   691 
   692 	return status;
   693 }
   694 
   695 bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
   696 {
   697 	CPlugin *pl = FindById(id);
   698 
   699 	if (!pl)
   700 	{
   701 		return false;
   702 	}
   703 
   704 	if (file != NULL)
   705 	{
   706 		*file = pl->m_File.c_str();
   707 	}
   708 
   709 	if (status != NULL)
   710 	{
   711 		*status = pl->m_Status;
   712 	}
   713 
   714 	if (source != NULL)
   715 	{
   716 		*source = pl->m_Source;
   717 	}
   718 
   719 	return true;
   720 }
   721 
   722 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   723 {
   724 	CPlugin *pl = FindById(id);
   725 
   726 	if (!pl || !pl->m_API)
   727 	{
   728 		if (error)
   729 		{
   730 			UTIL_Format(error, maxlength, "Plugin not valid");
   731 		}
   732 		return false;
   733 	}
   734 
   735 	return pl->m_API->QueryRunning(error, maxlength);
   736 }
   737 
   738 bool CPluginManager::QueryHandle(PluginId id, void **handle)
   739 {
   740 	CPlugin *pl = FindById(id);
   741 
   742 	if (!pl)
   743 	{
   744 		return false;
   745 	}
   746 
   747 	if (handle)
   748 	{
   749 		*handle = static_cast<void *>(pl->m_Lib);
   750 	}
   751 
   752 	return true;
   753 }
   754 
   755 PluginIter CPluginManager::_begin()
   756 {
   757 	return m_Plugins.begin();
   758 }
   759 
   760 PluginIter CPluginManager::_end()
   761 {
   762 	return m_Plugins.end();
   763 }
   764 
   765 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   766 {
   767 	CPlugin *pl = FindByAPI(api);
   768 
   769 	if (!pl)
   770 	{
   771 		return;
   772 	}
   773 
   774 	pl->m_Cvars.push_back(pCvar);
   775 }
   776 
   777 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   778 {
   779 	CPlugin *pl = FindByAPI(api);
   780 
   781 	if (!pl)
   782 	{
   783 		return;
   784 	}
   785 
   786 	pl->m_Cmds.push_back(pCmd);
   787 }
   788 
   789 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   790 {
   791 	CPlugin *pl = FindByAPI(api);
   792 
   793 	if (!pl)
   794 	{
   795 		return;
   796 	}
   797 
   798 	pl->m_Cvars.remove(pCvar);
   799 }
   800 
   801 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   802 {
   803 	CPlugin *pl = FindByAPI(api);
   804 
   805 	if (!pl)
   806 	{
   807 		return;
   808 	}
   809 
   810 	pl->m_Cmds.remove(pCmd);
   811 }
   812 
   813 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   814 {
   815 	SourceHook::List<ConCommandBase *>::iterator i;
   816 
   817 	/* :TODO: */
   818 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   819 	{
   820 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   821 	}
   822 	pl->m_Cvars.clear();
   823 
   824 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   825 	{
   826 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   827 	}
   828 	pl->m_Cmds.clear();
   829 }
   830