sourcemm/CPlugin.cpp
author Christian Hammacher <bmasterc@gmail.com>
Sun Feb 12 21:07:31 2006 +0000 (2006-02-12)
changeset 182 f60d482a6e07
parent 180 6c991ab4d967
child 205 08964c08d894
permissions -rw-r--r--
Changed version number to 1.2.1
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #include "CPlugin.h"
    12 #include "CSmmAPI.h"
    13 #include "sourcemm.h"
    14 #include "concommands.h"
    15 
    16 /** 
    17  * @brief Implements functions from CPlugin.h
    18  * @file CPlugin.cpp
    19  */
    20 
    21 using namespace SourceMM;
    22 
    23 #define ITER_PLEVENT(evn, plid) \
    24 	CPluginManager::CPlugin *_Xpl; \
    25 	SourceHook::List<IMetamodListener *>::iterator event; \
    26 	IMetamodListener *api; \
    27 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    28 		_Xpl = (*iter); \
    29 		if (_Xpl->m_Id == plid) \
    30 			continue; \
    31 		for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
    32 			api = (*event); \
    33 			api->evn(plid); \
    34 		} \
    35 	}
    36 
    37 CPluginManager g_PluginMngr;
    38 
    39 CPluginManager::CPluginManager()
    40 {
    41 	m_LastId = Pl_MinId;
    42 	m_AllLoaded = false;
    43 }
    44 
    45 CPluginManager::~CPluginManager()
    46 {
    47 	/*if (m_Plugins.size())
    48 	{
    49 		UnloadAll();
    50 		m_Plugins.clear();
    51 	}*/
    52 }
    53 
    54 CPluginManager::CPlugin::CPlugin() : m_Lib(NULL), m_API(NULL), m_Id(0), m_Source(0)
    55 {
    56 
    57 }
    58 
    59 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
    60 {
    61 	already = false;
    62 	//Check if we're about to reload an old plugin
    63 	PluginIter i = m_Plugins.begin();
    64 	while (i != m_Plugins.end())
    65 	{
    66 		if ( (*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()) )
    67 		{
    68 			if ( (*i)->m_Status < Pl_Paused )
    69 			{
    70 				//Attempt to load the plugin again
    71 				already = true;
    72 				i = m_Plugins.erase(i);
    73 				continue;
    74 			} else {
    75 				//No need to load it
    76 				already = true;
    77 				return (*i)->m_Id;
    78 			}
    79 		}
    80 		i++;
    81 	}
    82 
    83 	CPlugin *pl = _Load(file, source, error, maxlen);
    84 
    85 	if (!pl)
    86 		return Pl_BadLoad;
    87 
    88 	ITER_PLEVENT(OnPluginLoad, pl->m_Id);
    89 
    90 	return pl->m_Id;
    91 }
    92 
    93 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
    94 {
    95 	PluginIter i;
    96 
    97 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
    98 	{
    99 		if ( (*i)->m_Id == id )
   100 			return (*i);
   101 	}
   102 
   103 	return NULL;
   104 }
   105 
   106 void CPluginManager::SetAllLoaded()
   107 {
   108 	m_AllLoaded = true;
   109 	PluginIter i;
   110 
   111 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   112 	{
   113 		if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
   114 		{
   115 			//API 4 is when we added this callback
   116 			//Min version is now 5, so we ignore this check
   117 			//if ( (*i)->m_API->GetApiVersion() >= 004 )
   118 			(*i)->m_API->AllPluginsLoaded();
   119 		}
   120 	}
   121 }
   122 
   123 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
   124 {
   125 	CPlugin *pl = FindById(id);
   126 	
   127 	if (!pl)
   128 	{
   129 		snprintf(error, maxlen, "Plugin id not found");
   130 		return false;
   131 	}
   132 
   133 	bool ret;
   134 	
   135 	if ( (ret=_Pause(pl, error, maxlen)) == true )
   136 	{
   137 		ITER_PLEVENT(OnPluginPause, pl->m_Id);
   138 	}
   139 
   140 	return ret;
   141 }
   142 
   143 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
   144 {
   145 	CPlugin *pl = FindById(id);
   146 	
   147 	if (!pl)
   148 	{
   149 		snprintf(error, maxlen, "Plugin id not found");
   150 		return false;
   151 	}
   152 
   153 	bool ret;
   154 	
   155 	if ( (ret=_Unpause(pl, error, maxlen)) == true )
   156 	{
   157 		ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
   158 	}
   159 
   160 	return ret;
   161 }
   162 
   163 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
   164 {
   165 	CPlugin *pl = FindById(id);
   166 	
   167 	if (!pl)
   168 	{
   169 		snprintf(error, maxlen, "Plugin %d not found", id);
   170 		return false;
   171 	}
   172 
   173 	bool ret;
   174 	PluginId old_id = pl->m_Id;
   175 	if ( (ret=_Unload(pl, force, error, maxlen)) == true )
   176 	{
   177 		ITER_PLEVENT(OnPluginUnload, old_id);
   178 	}
   179 
   180 	return ret;
   181 }
   182 
   183 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
   184 {
   185 	PluginIter i;
   186 	char buffer[64];
   187 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   188 	{
   189 		if ( (*i) && (*i)->m_Id == id )
   190 		{
   191 			if ( (*i)->m_Status >= Pl_Paused)
   192 			{
   193 				snprintf(error, len, "Plugin %d is already running.", id);
   194 				return false;
   195 			}
   196 			CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
   197 			if (!pl)
   198 				return false;
   199 			if (pl->m_Status >= Pl_Paused)
   200 			{
   201 				//Now it gets crazy... unload the original copy.
   202 				_Unload( (*i), true, buffer, sizeof(buffer)-1 );
   203 				
   204 				//Set the new copy's id
   205 				pl->m_Id = id;
   206 
   207 				//We just wasted an id... reclaim it
   208 				m_LastId--;
   209 				
   210 				return true;
   211 			} else {
   212 				//don't really care about the buffer here
   213 				_Unload(pl, true, buffer, sizeof(buffer)-1);
   214 
   215 				//We just wasted an id... reclaim it
   216 				m_LastId--;
   217 				return false;
   218 			}
   219 		}
   220 	}
   221 
   222 	snprintf(error, len, "Plugin %d not found,", id);
   223 	return false;
   224 }
   225 
   226 CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
   227 {
   228 	PluginIter i;
   229 
   230 	//don't find bad plugins!
   231 	if (!api)
   232 		return NULL;
   233 
   234 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   235 	{
   236 		if ( (*i)->m_API == api )
   237 			return (*i);
   238 	}
   239 
   240 	return NULL;
   241 }
   242 
   243 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
   244 {
   245 	FILE *fp;
   246 	CPlugin *pl;
   247 
   248 	pl = new CPlugin();
   249 	*error = '\0';
   250 	
   251 	//Add plugin to list
   252 	pl->m_Id = m_LastId;
   253 	pl->m_File.assign(file);
   254 	m_Plugins.push_back(pl);
   255 	m_LastId++;
   256 
   257 	//Check if the file even exists
   258 	fp = fopen(file, "r");
   259 	if (!fp)
   260 	{
   261 		if (error)
   262 			snprintf(error, maxlen, "File not found: %s", file);
   263 		pl->m_Status = Pl_NotFound;
   264 	} else {
   265 		fclose(fp);
   266 		fp = NULL;
   267 		
   268 		//Load the file
   269 		pl->m_Lib = dlmount(file);
   270 		if (!pl->m_Lib)
   271 		{
   272 			if (error)
   273 				snprintf(error, maxlen, "%s", dlerror());
   274 			pl->m_Status = Pl_Error;
   275 		} else {
   276 			CreateInterfaceFn pfn = reinterpret_cast<CreateInterfaceFn>(dlsym(pl->m_Lib, PL_EXPOSURE_C));
   277 			if (!pfn)
   278 			{
   279 				if (error)
   280 					snprintf(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
   281 				pl->m_Status = Pl_Error;
   282 			} else {
   283 				pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
   284 				if (!pl->m_API)
   285 				{
   286 					if (error)
   287 						snprintf(error, maxlen, "Failed to get API");
   288 					pl->m_Status = Pl_Error;
   289 				} else {
   290 					int api = pl->m_API->GetApiVersion();
   291 					if (api < PLAPI_MIN_VERSION)
   292 					{
   293 						if (error)
   294 							snprintf(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
   295 						pl->m_Status = Pl_Error;
   296 					} else if (api > PLAPI_VERSION) {
   297 						if (error)
   298 							snprintf(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
   299 						pl->m_Status = Pl_Error;
   300 					} else {
   301 						if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), error, maxlen, m_AllLoaded))
   302 						{
   303 							pl->m_Status = Pl_Running;
   304 							if (m_AllLoaded)
   305 							{
   306 								//API 4 is when we added this callback
   307 								//Removing this code as the min version is now 5
   308 								//if (pl->m_API->GetApiVersion() >= 4)
   309 								pl->m_API->AllPluginsLoaded();
   310 							}
   311 						} else {
   312 							pl->m_Status = Pl_Refused;
   313 						}
   314 					}
   315 				}
   316 			}
   317 		}
   318 	}
   319 
   320 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   321 	{
   322 		dlclose(pl->m_Lib);
   323 		pl->m_Lib = NULL;
   324 		pl->m_API = NULL;
   325 	}
   326 
   327 	return pl;
   328 }
   329 
   330 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   331 {
   332 	if (error)
   333 		*error = '\0';
   334 	if (pl->m_API && pl->m_Lib)
   335 	{
   336 		//Note, we'll always tell the plugin it will be unloading...
   337 		if (pl->m_API->Unload(error, maxlen) || force)
   338 		{
   339 			//Make sure to detach it from sourcehook!
   340 			g_SourceHook.UnloadPlugin(pl->m_Id);
   341 
   342 			UnregAllConCmds(pl);
   343 
   344 			//Clean up the DLL
   345 			dlclose(pl->m_Lib);
   346 			pl->m_Lib = NULL;
   347 			pl->m_API = NULL;
   348 
   349 			//Remove the plugin from the list
   350 			PluginIter i;
   351 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   352 			{
   353 				if ( (*i)->m_Id == pl->m_Id )
   354 				{
   355 					i = m_Plugins.erase(i);
   356 					break;
   357 				}
   358 			}
   359 			//Free its memory
   360 			delete pl;
   361 
   362 			return true;
   363 		}
   364 	} else {
   365 		//The plugin is not valid, and let's just remove it from the list anyway
   366 		PluginIter i;
   367 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   368 		{
   369 			if ( (*i)->m_Id == pl->m_Id )
   370 			{
   371 				i = m_Plugins.erase(i);
   372 				break;
   373 			}
   374 		}
   375 		delete pl;
   376 
   377 		return true;
   378 	}
   379 
   380 	return false;
   381 }
   382 
   383 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   384 {
   385 	if (error)
   386 		*error = '\0';
   387 	if (pl->m_Status != Pl_Running || !pl->m_API)
   388 	{
   389 		if (error)
   390 			snprintf(error, maxlen, "Plugin cannot be paused");
   391 	} else {
   392 		if (pl->m_API->Pause(error, maxlen))
   393 		{
   394 			g_SourceHook.PausePlugin(pl->m_Id);
   395 			pl->m_Status = Pl_Paused;
   396 
   397 			return true;
   398 		}
   399 	}
   400 
   401 	return false;
   402 }
   403 
   404 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   405 {
   406 	if (error)
   407 		*error = '\0';
   408 
   409 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   410 	{
   411 		if (error)
   412 			snprintf(error, maxlen, "Plugin cannot be unpaused");
   413 	} else {
   414 		if (pl->m_API->Unpause(error, maxlen))
   415 		{
   416 			g_SourceHook.UnpausePlugin(pl->m_Id);
   417 			pl->m_Status = Pl_Running;
   418 
   419 			return true;
   420 		}
   421 	}
   422 
   423 	return false;
   424 }
   425 
   426 bool CPluginManager::UnloadAll()
   427 {
   428 	PluginIter i;
   429 
   430 	SourceHook::List<SourceMM::CPluginManager::CPlugin *> remqueue;
   431 
   432 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   433 		remqueue.push_back( (*i) );
   434 
   435 	char error[128];
   436 	bool status = true;
   437 
   438 	for (i=remqueue.begin(); i!=remqueue.end(); i++)
   439 	{
   440 		if ( !_Unload( (*i), true, error, sizeof(error)-1) )
   441 			status = false;
   442 	}
   443 
   444 	m_Plugins.clear();
   445 	remqueue.clear();
   446 
   447 	return status;
   448 }
   449 
   450 bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
   451 {
   452 	CPlugin *pl = FindById(id);
   453 
   454 	if (!pl)
   455 		return false;
   456 
   457 	file = pl->m_File.c_str();
   458 	status = pl->m_Status;
   459 	source = pl->m_Source;
   460 
   461 	return true;
   462 }
   463 
   464 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   465 {
   466 	CPlugin *pl = FindById(id);
   467 
   468 	if (!pl || !pl->m_API)
   469 	{
   470 		if (error)
   471 			snprintf(error, maxlength, "Plugin not valid");
   472 		return false;
   473 	}
   474 
   475 	return pl->m_API->QueryRunning(error, maxlength);
   476 }
   477 
   478 PluginIter CPluginManager::_begin()
   479 {
   480 	return m_Plugins.begin();
   481 }
   482 
   483 PluginIter CPluginManager::_end()
   484 {
   485 	return m_Plugins.end();
   486 }
   487 
   488 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   489 {
   490 	CPlugin *pl = FindByAPI(api);
   491 
   492 	if (!pl)
   493 		return;
   494 
   495 	pl->m_Cvars.push_back(pCvar);
   496 }
   497 
   498 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   499 {
   500 	CPlugin *pl = FindByAPI(api);
   501 
   502 	if (!pl)
   503 		return;
   504 
   505 	pl->m_Cmds.push_back(pCmd);
   506 }
   507 
   508 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   509 {
   510 	CPlugin *pl = FindByAPI(api);
   511 
   512 	if (!pl)
   513 		return;
   514 
   515 	pl->m_Cvars.remove(pCvar);
   516 }
   517 
   518 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   519 {
   520 	CPlugin *pl = FindByAPI(api);
   521 
   522 	if (!pl)
   523 		return;
   524 
   525 	pl->m_Cmds.remove(pCmd);
   526 }
   527 
   528 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   529 {
   530 	SourceHook::List<ConCommandBase *>::iterator i;
   531 
   532 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   533 		g_SMConVarAccessor.Unregister( (*i) );
   534 
   535 	pl->m_Cvars.clear();
   536 
   537 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   538 		g_SMConVarAccessor.Unregister( (*i) );
   539 
   540 	pl->m_Cmds.clear();
   541 }