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