sourcemm/metamod_plugins.cpp
author David Anderson <dvander@alliedmods.net>
Sat Oct 06 16:47:58 2007 +0000 (2007-10-06)
changeset 376 f2d55f90f697
parent 371 751179604513
child 396 ed81bca8944a
permissions -rw-r--r--
added format/formatargs api
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2007 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 #if defined _DEBUG
    29 #define DEBUG2
    30 #undef _DEBUG
    31 #endif
    32 #include "metamod.h"
    33 #include "metamod_plugins.h"
    34 #include "metamod_util.h"
    35 #if defined DEBUG2
    36 #undef DEBUG2
    37 #define _DEBUG
    38 #endif
    39 
    40 /** 
    41  * @brief Implements functions from CPlugin.h
    42  * @file CPlugin.cpp
    43  */
    44 
    45 using namespace SourceMM;
    46 
    47 #define ITER_PLEVENT(evn, plid) \
    48 	CPluginManager::CPlugin *_Xpl; \
    49 	SourceHook::List<IMetamodListener *>::iterator event; \
    50 	IMetamodListener *api; \
    51 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    52 		_Xpl = (*iter); \
    53 		if (_Xpl->m_Id == plid) \
    54 			continue; \
    55 		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    56 			api = (*event); \
    57 			api->evn(plid); \
    58 		} \
    59 	}
    60 
    61 CPluginManager g_PluginMngr;
    62 
    63 CPluginManager::CPluginManager()
    64 {
    65 	m_LastId = Pl_MinId;
    66 	m_AllLoaded = false;
    67 }
    68 
    69 CPluginManager::~CPluginManager()
    70 {
    71 	SourceHook::List<CNameAlias *>::iterator iter;
    72 
    73 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    74 	{
    75 		delete (*iter);
    76 	}
    77 
    78 	m_Aliases.clear();
    79 }
    80 
    81 const char *CPluginManager::LookupAlias(const char *alias)
    82 {
    83 	SourceHook::List<CNameAlias *>::iterator iter;
    84 	CNameAlias *p;
    85 
    86 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
    87 	{
    88 		p = (*iter);
    89 		if (p->alias.compare(alias) == 0)
    90 		{
    91 			return p->value.c_str();
    92 		}
    93 	}
    94 
    95 	return NULL;
    96 }
    97 
    98 SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_begin()
    99 {
   100 	return m_Aliases.begin();
   101 }
   102 
   103 SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_end()
   104 {
   105 	return m_Aliases.end();
   106 }
   107 
   108 void CPluginManager::SetAlias(const char *alias, const char *value)
   109 {
   110 	SourceHook::List<CNameAlias *>::iterator iter;
   111 	CNameAlias *p;
   112 
   113 	for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
   114 	{
   115 		p = (*iter);
   116 		if (p->alias.compare(alias) == 0)
   117 		{
   118 			if (value[0] == '\0')
   119 			{
   120 				iter = m_Aliases.erase(iter);
   121 				return;
   122 			}
   123 			else
   124 			{
   125 				p->value.assign(value);
   126 				return;
   127 			}
   128 		}
   129 	}
   130 
   131 	if (value[0] != '\0')
   132 	{
   133 		p = new CNameAlias;
   134 	
   135 		p->alias.assign(alias);
   136 		p->value.assign(value);
   137 	
   138 		m_Aliases.push_back(p);
   139 	}
   140 }
   141 
   142 CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL)
   143 {
   144 
   145 }
   146 
   147 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
   148 {
   149 	already = false;
   150 	//Check if we're about to reload an old plugin
   151 	PluginIter i = m_Plugins.begin();
   152 	while (i != m_Plugins.end())
   153 	{
   154 		if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
   155 		{
   156 			if ((*i)->m_Status < Pl_Paused)
   157 			{
   158 				//Attempt to load the plugin again
   159 				already = true;
   160 				i = m_Plugins.erase(i);
   161 				continue;
   162 			}
   163 			else
   164 			{
   165 				//No need to load it
   166 				already = true;
   167 				return (*i)->m_Id;
   168 			}
   169 		}
   170 		i++;
   171 	}
   172 
   173 	CPlugin *pl = _Load(file, source, error, maxlen);
   174 
   175 	if (!pl)
   176 	{
   177 		return Pl_BadLoad;
   178 	}
   179 
   180 	ITER_PLEVENT(OnPluginLoad, pl->m_Id);
   181 
   182 	return pl->m_Id;
   183 }
   184 
   185 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
   186 {
   187 	PluginIter i;
   188 
   189 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   190 	{
   191 		if ( (*i)->m_Id == id )\
   192 		{
   193 			return (*i);
   194 		}
   195 	}
   196 
   197 	return NULL;
   198 }
   199 
   200 void CPluginManager::SetAllLoaded()
   201 {
   202 	m_AllLoaded = true;
   203 	PluginIter i;
   204 
   205 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   206 	{
   207 		if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
   208 		{
   209 			//API 4 is when we added this callback
   210 			//Min version is now 5, so we ignore this check
   211 			//if ( (*i)->m_API->GetApiVersion() >= 004 )
   212 			(*i)->m_API->AllPluginsLoaded();
   213 		}
   214 	}
   215 }
   216 
   217 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
   218 {
   219 	CPlugin *pl = FindById(id);
   220 	
   221 	if (!pl)
   222 	{
   223 		UTIL_Format(error, maxlen, "Plugin id not found");
   224 		return false;
   225 	}
   226 
   227 	bool ret;
   228 	
   229 	if ((ret=_Pause(pl, error, maxlen)) == true)
   230 	{
   231 		ITER_PLEVENT(OnPluginPause, pl->m_Id);
   232 	}
   233 
   234 	return ret;
   235 }
   236 
   237 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
   238 {
   239 	CPlugin *pl = FindById(id);
   240 	
   241 	if (!pl)
   242 	{
   243 		UTIL_Format(error, maxlen, "Plugin id not found");
   244 		return false;
   245 	}
   246 
   247 	bool ret;
   248 	
   249 	if ( (ret=_Unpause(pl, error, maxlen)) == true )
   250 	{
   251 		ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
   252 	}
   253 
   254 	return ret;
   255 }
   256 
   257 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
   258 {
   259 	CPlugin *pl = FindById(id);
   260 	
   261 	if (!pl)
   262 	{
   263 		UTIL_Format(error, maxlen, "Plugin %d not found", id);
   264 		return false;
   265 	}
   266 
   267 	bool ret;
   268 	PluginId old_id = pl->m_Id;
   269 	if ( (ret=_Unload(pl, force, error, maxlen)) == true )
   270 	{
   271 		ITER_PLEVENT(OnPluginUnload, old_id);
   272 	}
   273 
   274 	return ret;
   275 }
   276 
   277 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
   278 {
   279 	PluginIter i;
   280 	char buffer[64];
   281 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   282 	{
   283 		if ( (*i) && (*i)->m_Id == id )
   284 		{
   285 			if ( (*i)->m_Status >= Pl_Paused)
   286 			{
   287 				UTIL_Format(error, len, "Plugin %d is already running.", id);
   288 				return false;
   289 			}
   290 			CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
   291 			if (!pl)
   292 			{
   293 				return false;
   294 			}
   295 			if (pl->m_Status >= Pl_Paused)
   296 			{
   297 				//Now it gets crazy... unload the original copy.
   298 				_Unload( (*i), true, buffer, sizeof(buffer)-1 );
   299 				
   300 				//Set the new copy's id
   301 				pl->m_Id = id;
   302 
   303 				//We just wasted an id... reclaim it
   304 				m_LastId--;
   305 				
   306 				return true;
   307 			}
   308 			else
   309 			{
   310 				//don't really care about the buffer here
   311 				_Unload(pl, true, buffer, sizeof(buffer)-1);
   312 
   313 				//We just wasted an id... reclaim it
   314 				m_LastId--;
   315 				return false;
   316 			}
   317 		}
   318 	}
   319 
   320 	UTIL_Format(error, len, "Plugin %d not found,", id);
   321 	return false;
   322 }
   323 
   324 CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
   325 {
   326 	PluginIter i;
   327 
   328 	//don't find bad plugins!
   329 	if (!api)
   330 	{
   331 		return NULL;
   332 	}
   333 
   334 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   335 	{
   336 		if ((*i)->m_API == api)
   337 		{
   338 			return (*i);
   339 		}
   340 	}
   341 
   342 	return NULL;
   343 }
   344 
   345 int CPluginManager::GetPluginCount()
   346 {
   347 	return (int)m_Plugins.size();
   348 }
   349 
   350 const char *CPluginManager::GetStatusText(CPlugin *pl)
   351 {
   352 	switch (pl->m_Status)
   353 	{
   354 	case Pl_NotFound:
   355 		return "NOFILE";
   356 	case Pl_Error:
   357 		return "ERROR";
   358 	case Pl_Refused:
   359 		return "FAILED";
   360 	case Pl_Paused:
   361 		return "PAUSED";
   362 	case Pl_Running:
   363 		{
   364 			if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   365 			{
   366 				return "STOPPED";
   367 			} else {
   368 				return "RUNNING";
   369 			}
   370 		}
   371 	default:
   372 		return "-";
   373 	}
   374 }
   375 
   376 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   377 {
   378 	FILE *fp;
   379 	CPlugin *pl;
   380 
   381 	pl = new CPlugin();
   382 	*error = '\0';
   383 	
   384 	//Add plugin to list
   385 	pl->m_Id = m_LastId;
   386 	pl->m_File.assign(file);
   387 	m_Plugins.push_back(pl);
   388 	m_LastId++;
   389 
   390 	//Check if the file even exists
   391 	fp = fopen(file, "r");
   392 	if (!fp)
   393 	{
   394 		if (error)
   395 		{
   396 			UTIL_Format(error, maxlen, "File not found: %s", file);
   397 		}
   398 		pl->m_Status = Pl_NotFound;
   399 	}
   400 
   401 	if (fp)
   402 	{
   403 		fclose(fp);
   404 		fp = NULL;
   405 		
   406 		//Load the file
   407 		pl->m_Lib = dlmount(file);
   408 		if (!pl->m_Lib)
   409 		{
   410 			if (error)
   411 			{
   412 				UTIL_Format(error, maxlen, "%s", dlerror());
   413 			}
   414 			pl->m_Status = Pl_Error;
   415 		}
   416 		else
   417 		{
   418 			CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   419 			if (!pfn)
   420 			{
   421 				if (error)
   422 				{
   423 					UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   424 				}
   425 				pl->m_Status = Pl_Error;
   426 			}
   427 			else
   428 			{
   429 				pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
   430 				if (!pl->m_API)
   431 				{
   432 					if (error)
   433 					{
   434 						UTIL_Format(error, maxlen, "Failed to get API");
   435 					}
   436 					pl->m_Status = Pl_Error;
   437 				}
   438 				else
   439 				{
   440 					int api = pl->m_API->GetApiVersion();
   441 					if (api < PLAPI_MIN_VERSION)
   442 					{
   443 						if (error)
   444 						{
   445 							UTIL_Format(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
   446 						}
   447 						pl->m_Status = Pl_Error;
   448 					}
   449 					else if (api > PLAPI_VERSION)
   450 					{
   451 						if (error)
   452 						{
   453 							UTIL_Format(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
   454 						}
   455 						pl->m_Status = Pl_Error;
   456 					}
   457 					else
   458 					{
   459 						if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
   460 						{
   461 							pl->m_Status = Pl_Running;
   462 							if (m_AllLoaded)
   463 							{
   464 								//API 4 is when we added this callback
   465 								//Removing this code as the min version is now 5
   466 								//if (pl->m_API->GetApiVersion() >= 4)
   467 								pl->m_API->AllPluginsLoaded();
   468 							}
   469 						}
   470 						else
   471 						{
   472 							pl->m_Status = Pl_Refused;
   473 						}
   474 					}
   475 				}
   476 			}
   477 		}
   478 	}
   479 
   480 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   481 	{
   482 		pl->m_Events.clear();
   483 		g_SourceHook.UnloadPlugin(pl->m_Id);
   484 		UnregAllConCmds(pl);
   485 
   486 		dlclose(pl->m_Lib);
   487 		pl->m_Lib = NULL;
   488 		pl->m_API = NULL;
   489 	}
   490 
   491 	return pl;
   492 }
   493 
   494 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   495 {
   496 	if (error)
   497 	{
   498 		*error = '\0';
   499 	}
   500 
   501 	if (pl->m_API && pl->m_Lib)
   502 	{
   503 		//Note, we'll always tell the plugin it will be unloading...
   504 		if (pl->m_API->Unload(error, maxlen) || force)
   505 		{
   506 			//Make sure to detach it from sourcehook!
   507 			g_SourceHook.UnloadPlugin(pl->m_Id);
   508 
   509 			pl->m_Events.clear();
   510 
   511 			UnregAllConCmds(pl);
   512 
   513 			//Clean up the DLL
   514 			dlclose(pl->m_Lib);
   515 			pl->m_Lib = NULL;
   516 			pl->m_API = NULL;
   517 
   518 			//Remove the plugin from the list
   519 			PluginIter i;
   520 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   521 			{
   522 				if ( (*i)->m_Id == pl->m_Id )
   523 				{
   524 					i = m_Plugins.erase(i);
   525 					break;
   526 				}
   527 			}
   528 			//Free its memory
   529 			delete pl;
   530 
   531 			return true;
   532 		}
   533 	} else {
   534 		//The plugin is not valid, and let's just remove it from the list anyway
   535 		PluginIter i;
   536 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   537 		{
   538 			if ( (*i)->m_Id == pl->m_Id )
   539 			{
   540 				i = m_Plugins.erase(i);
   541 				break;
   542 			}
   543 		}
   544 		delete pl;
   545 
   546 		return true;
   547 	}
   548 
   549 	return false;
   550 }
   551 
   552 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   553 {
   554 	if (error)
   555 	{
   556 		*error = '\0';
   557 	}
   558 
   559 	if (pl->m_Status != Pl_Running || !pl->m_API)
   560 	{
   561 		if (error)
   562 		{
   563 			UTIL_Format(error, maxlen, "Plugin cannot be paused");
   564 		}
   565 	}
   566 	else
   567 	{
   568 		if (pl->m_API->Pause(error, maxlen))
   569 		{
   570 			g_SourceHook.PausePlugin(pl->m_Id);
   571 			pl->m_Status = Pl_Paused;
   572 
   573 			return true;
   574 		}
   575 	}
   576 
   577 	return false;
   578 }
   579 
   580 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   581 {
   582 	if (error)
   583 	{
   584 		*error = '\0';
   585 	}
   586 
   587 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   588 	{
   589 		if (error)
   590 		{
   591 			UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
   592 		}
   593 	}
   594 	else
   595 	{
   596 		if (pl->m_API->Unpause(error, maxlen))
   597 		{
   598 			g_SourceHook.UnpausePlugin(pl->m_Id);
   599 			pl->m_Status = Pl_Running;
   600 
   601 			return true;
   602 		}
   603 	}
   604 
   605 	return false;
   606 }
   607 
   608 bool CPluginManager::UnloadAll()
   609 {
   610 	PluginIter i;
   611 
   612 	char error[128];
   613 	bool status = true;
   614 
   615 	while ((i = m_Plugins.begin()) != m_Plugins.end())
   616 	{
   617 		if ( !_Unload( (*i), true, error, sizeof(error)) )
   618 		{
   619 			status = false;
   620 		}
   621 	}
   622 
   623 	return status;
   624 }
   625 
   626 bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
   627 {
   628 	CPlugin *pl = FindById(id);
   629 
   630 	if (!pl)
   631 	{
   632 		return false;
   633 	}
   634 
   635 	file = pl->m_File.c_str();
   636 	status = pl->m_Status;
   637 	source = pl->m_Source;
   638 
   639 	return true;
   640 }
   641 
   642 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   643 {
   644 	CPlugin *pl = FindById(id);
   645 
   646 	if (!pl || !pl->m_API)
   647 	{
   648 		if (error)
   649 		{
   650 			UTIL_Format(error, maxlength, "Plugin not valid");
   651 		}
   652 		return false;
   653 	}
   654 
   655 	return pl->m_API->QueryRunning(error, maxlength);
   656 }
   657 
   658 bool CPluginManager::QueryHandle(PluginId id, void *&handle)
   659 {
   660 	CPlugin *pl = FindById(id);
   661 
   662 	if (!pl)
   663 	{
   664 		return false;
   665 	}
   666 
   667 	handle = static_cast<void *>(pl->m_Lib);
   668 
   669 	return true;
   670 }
   671 
   672 PluginIter CPluginManager::_begin()
   673 {
   674 	return m_Plugins.begin();
   675 }
   676 
   677 PluginIter CPluginManager::_end()
   678 {
   679 	return m_Plugins.end();
   680 }
   681 
   682 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   683 {
   684 	CPlugin *pl = FindByAPI(api);
   685 
   686 	if (!pl)
   687 	{
   688 		return;
   689 	}
   690 
   691 	pl->m_Cvars.push_back(pCvar);
   692 }
   693 
   694 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   695 {
   696 	CPlugin *pl = FindByAPI(api);
   697 
   698 	if (!pl)
   699 	{
   700 		return;
   701 	}
   702 
   703 	pl->m_Cmds.push_back(pCmd);
   704 }
   705 
   706 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   707 {
   708 	CPlugin *pl = FindByAPI(api);
   709 
   710 	if (!pl)
   711 	{
   712 		return;
   713 	}
   714 
   715 	pl->m_Cvars.remove(pCvar);
   716 }
   717 
   718 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   719 {
   720 	CPlugin *pl = FindByAPI(api);
   721 
   722 	if (!pl)
   723 	{
   724 		return;
   725 	}
   726 
   727 	pl->m_Cmds.remove(pCmd);
   728 }
   729 
   730 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   731 {
   732 	SourceHook::List<ConCommandBase *>::iterator i;
   733 
   734 	/* :TODO: */
   735 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   736 	{
   737 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   738 	}
   739 	pl->m_Cvars.clear();
   740 
   741 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   742 	{
   743 		g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
   744 	}
   745 	pl->m_Cmds.clear();
   746 }
   747