sourcemm/CPlugin.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 06 00:59:01 2005 +0000 (2005-10-06)
changeset 122 7c36121e2dfe
parent 98 4c46acf7e76f
child 138 dfa3b31cc042
permissions -rw-r--r--
Initial import of new GameDLL loading code
     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 					int api = pl->m_API->GetApiVersion();
   253 					if (api < PLAPI_MIN_VERSION)
   254 					{
   255 						if (error)
   256 							snprintf(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
   257 						pl->m_Status = Pl_Error;
   258 					} else if (api > PLAPI_VERSION) {
   259 						if (error)
   260 							snprintf(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
   261 						pl->m_Status = Pl_Error;
   262 					} else {
   263 						if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), &(pl->fac_list), error, maxlen, m_AllLoaded))
   264 						{
   265 							pl->m_Status = Pl_Running;
   266 							if (m_AllLoaded)
   267 							{
   268 								//API 4 is when we added this callback
   269 								//Removing this code as the min version is now 5
   270 								//if (pl->m_API->GetApiVersion() >= 4)
   271 								pl->m_API->AllPluginsLoaded();
   272 							}
   273 						} else {
   274 							pl->m_Status = Pl_Refused;
   275 						}
   276 					}
   277 				}
   278 			}
   279 		}
   280 	}
   281 
   282 	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
   283 	{
   284 		dlclose(pl->m_Lib);
   285 		pl->m_Lib = NULL;
   286 		pl->m_API = NULL;
   287 	}
   288 
   289 	return pl;
   290 }
   291 
   292 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
   293 {
   294 	if (error)
   295 		*error = '\0';
   296 	if (pl->m_API && pl->m_Lib)
   297 	{
   298 		//Note, we'll always tell the plugin it will be unloading...
   299 		if (pl->m_API->Unload(error, maxlen) || force)
   300 		{
   301 			//Make sure to detach it from sourcehook!
   302 			g_SourceHook.UnloadPlugin(pl->m_Id);
   303 
   304 			UnregAllConCmds(pl);
   305 
   306 			//Clean up the DLL
   307 			dlclose(pl->m_Lib);
   308 			pl->m_Lib = NULL;
   309 			pl->m_API = NULL;
   310 
   311 			//Remove the plugin from the list
   312 			PluginIter i;
   313 			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   314 			{
   315 				if ( (*i)->m_Id == pl->m_Id )
   316 				{
   317 					i = m_Plugins.erase(i);
   318 					break;
   319 				}
   320 			}
   321 			//Free its memory
   322 			delete pl;
   323 
   324 			return true;
   325 		}
   326 	} else {
   327 		//The plugin is not valid, and let's just remove it from the list anyway
   328 		PluginIter i;
   329 		for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   330 		{
   331 			if ( (*i)->m_Id == pl->m_Id )
   332 			{
   333 				i = m_Plugins.erase(i);
   334 				break;
   335 			}
   336 		}
   337 		delete pl;
   338 
   339 		return true;
   340 	}
   341 
   342 	return false;
   343 }
   344 
   345 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   346 {
   347 	if (error)
   348 		*error = '\0';
   349 	if (pl->m_Status != Pl_Running || !pl->m_API)
   350 	{
   351 		if (error)
   352 			snprintf(error, maxlen, "Plugin cannot be paused");
   353 	} else {
   354 		if (pl->m_API->Pause(error, maxlen))
   355 		{
   356 			g_SourceHook.PausePlugin(pl->m_Id);
   357 			pl->m_Status = Pl_Paused;
   358 
   359 			return true;
   360 		}
   361 	}
   362 
   363 	return false;
   364 }
   365 
   366 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
   367 {
   368 	if (error)
   369 		*error = '\0';
   370 
   371 	if (pl->m_Status != Pl_Paused || !pl->m_API)
   372 	{
   373 		if (error)
   374 			snprintf(error, maxlen, "Plugin cannot be unpaused");
   375 	} else {
   376 		if (pl->m_API->Unpause(error, maxlen))
   377 		{
   378 			g_SourceHook.UnpausePlugin(pl->m_Id);
   379 			pl->m_Status = Pl_Running;
   380 
   381 			return true;
   382 		}
   383 	}
   384 
   385 	return false;
   386 }
   387 
   388 bool CPluginManager::UnloadAll()
   389 {
   390 	PluginIter i;
   391 
   392 	SourceHook::List<SourceMM::CPluginManager::CPlugin *> remqueue;
   393 
   394 	for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
   395 		remqueue.push_back( (*i) );
   396 
   397 	char error[128];
   398 	bool status = true;
   399 
   400 	for (i=remqueue.begin(); i!=remqueue.end(); i++)
   401 	{
   402 		if ( !_Unload( (*i), true, error, sizeof(error)-1) )
   403 			status = false;
   404 	}
   405 
   406 	m_Plugins.clear();
   407 	remqueue.clear();
   408 
   409 	return status;
   410 }
   411 
   412 bool CPluginManager::Query(PluginId id, const char *&file, factories *&list, Pl_Status &status, PluginId &source)
   413 {
   414 	CPlugin *pl = FindById(id);
   415 
   416 	if (!pl)
   417 		return false;
   418 
   419 	file = pl->m_File.c_str();
   420 	list = &(pl->fac_list);
   421 	status = pl->m_Status;
   422 	source = pl->m_Source;
   423 
   424 	return true;
   425 }
   426 
   427 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
   428 {
   429 	CPlugin *pl = FindById(id);
   430 
   431 	if (!pl || !pl->m_API)
   432 	{
   433 		if (error)
   434 			snprintf(error, maxlength, "Plugin not valid");
   435 		return false;
   436 	}
   437 
   438 	return pl->m_API->QueryRunning(error, maxlength);
   439 }
   440 
   441 PluginIter CPluginManager::_begin()
   442 {
   443 	return m_Plugins.begin();
   444 }
   445 
   446 PluginIter CPluginManager::_end()
   447 {
   448 	return m_Plugins.end();
   449 }
   450 
   451 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   452 {
   453 	CPlugin *pl = FindByAPI(api);
   454 
   455 	if (!pl)
   456 		return;
   457 
   458 	pl->m_Cvars.push_back(pCvar);
   459 }
   460 
   461 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   462 {
   463 	CPlugin *pl = FindByAPI(api);
   464 
   465 	if (!pl)
   466 		return;
   467 
   468 	pl->m_Cmds.push_back(pCmd);
   469 }
   470 
   471 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
   472 {
   473 	CPlugin *pl = FindByAPI(api);
   474 
   475 	if (!pl)
   476 		return;
   477 
   478 	pl->m_Cvars.remove(pCvar);
   479 }
   480 
   481 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
   482 {
   483 	CPlugin *pl = FindByAPI(api);
   484 
   485 	if (!pl)
   486 		return;
   487 
   488 	pl->m_Cmds.remove(pCmd);
   489 }
   490 
   491 void CPluginManager::UnregAllConCmds(CPlugin *pl)
   492 {
   493 	SourceHook::List<ConCommandBase *>::iterator i;
   494 
   495 	for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
   496 		g_SMConVarAccessor.Unregister( (*i) );
   497 
   498 	pl->m_Cvars.clear();
   499 
   500 	for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
   501 		g_SMConVarAccessor.Unregister( (*i) );
   502 
   503 	pl->m_Cmds.clear();
   504 }