1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/CPlugin.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,618 @@
1.4 +/* ======== SourceMM ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * ============================
1.12 + */
1.13 +
1.14 +#include "CPlugin.h"
1.15 +#include "CSmmAPI.h"
1.16 +#include "sourcemm.h"
1.17 +#include "concommands.h"
1.18 +
1.19 +/**
1.20 + * @brief Implements functions from CPlugin.h
1.21 + * @file CPlugin.cpp
1.22 + */
1.23 +
1.24 +using namespace SourceMM;
1.25 +
1.26 +#define ITER_PLEVENT(evn, plid) \
1.27 + CPluginManager::CPlugin *_Xpl; \
1.28 + SourceHook::List<IMetamodListener *>::iterator event; \
1.29 + IMetamodListener *api; \
1.30 + for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.31 + _Xpl = (*iter); \
1.32 + if (_Xpl->m_Id == plid) \
1.33 + continue; \
1.34 + for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
1.35 + api = (*event); \
1.36 + api->evn(plid); \
1.37 + } \
1.38 + }
1.39 +
1.40 +CPluginManager g_PluginMngr;
1.41 +
1.42 +CPluginManager::CPluginManager()
1.43 +{
1.44 + m_LastId = Pl_MinId;
1.45 + m_AllLoaded = false;
1.46 +}
1.47 +
1.48 +CPluginManager::~CPluginManager()
1.49 +{
1.50 + SourceHook::List<CNameAlias *>::iterator iter;
1.51 +
1.52 + for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
1.53 + {
1.54 + delete (*iter);
1.55 + }
1.56 +
1.57 + m_Aliases.clear();
1.58 +}
1.59 +
1.60 +const char *CPluginManager::LookupAlias(const char *alias)
1.61 +{
1.62 + SourceHook::List<CNameAlias *>::iterator iter;
1.63 + CNameAlias *p;
1.64 +
1.65 + for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
1.66 + {
1.67 + p = (*iter);
1.68 + if (p->alias.compare(alias) == 0)
1.69 + {
1.70 + return p->value.c_str();
1.71 + }
1.72 + }
1.73 +
1.74 + return NULL;
1.75 +}
1.76 +
1.77 +SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_begin()
1.78 +{
1.79 + return m_Aliases.begin();
1.80 +}
1.81 +
1.82 +SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_end()
1.83 +{
1.84 + return m_Aliases.end();
1.85 +}
1.86 +
1.87 +void CPluginManager::SetAlias(const char *alias, const char *value)
1.88 +{
1.89 + SourceHook::List<CNameAlias *>::iterator iter;
1.90 + CNameAlias *p;
1.91 +
1.92 + for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
1.93 + {
1.94 + p = (*iter);
1.95 + if (p->alias.compare(alias) == 0)
1.96 + {
1.97 + if (value[0] == '\0')
1.98 + {
1.99 + iter = m_Aliases.erase(iter);
1.100 + return;
1.101 + } else {
1.102 + p->value.assign(value);
1.103 + return;
1.104 + }
1.105 + }
1.106 + }
1.107 +
1.108 + if (value[0] != '\0')
1.109 + {
1.110 + p = new CNameAlias;
1.111 +
1.112 + p->alias.assign(alias);
1.113 + p->value.assign(value);
1.114 +
1.115 + m_Aliases.push_back(p);
1.116 + }
1.117 +}
1.118 +
1.119 +CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL)
1.120 +{
1.121 +
1.122 +}
1.123 +
1.124 +PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
1.125 +{
1.126 + already = false;
1.127 + //Check if we're about to reload an old plugin
1.128 + PluginIter i = m_Plugins.begin();
1.129 + while (i != m_Plugins.end())
1.130 + {
1.131 + if ( (*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()) )
1.132 + {
1.133 + if ( (*i)->m_Status < Pl_Paused )
1.134 + {
1.135 + //Attempt to load the plugin again
1.136 + already = true;
1.137 + i = m_Plugins.erase(i);
1.138 + continue;
1.139 + } else {
1.140 + //No need to load it
1.141 + already = true;
1.142 + return (*i)->m_Id;
1.143 + }
1.144 + }
1.145 + i++;
1.146 + }
1.147 +
1.148 + CPlugin *pl = _Load(file, source, error, maxlen);
1.149 +
1.150 + if (!pl)
1.151 + return Pl_BadLoad;
1.152 +
1.153 + ITER_PLEVENT(OnPluginLoad, pl->m_Id);
1.154 +
1.155 + return pl->m_Id;
1.156 +}
1.157 +
1.158 +CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
1.159 +{
1.160 + PluginIter i;
1.161 +
1.162 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.163 + {
1.164 + if ( (*i)->m_Id == id )
1.165 + return (*i);
1.166 + }
1.167 +
1.168 + return NULL;
1.169 +}
1.170 +
1.171 +void CPluginManager::SetAllLoaded()
1.172 +{
1.173 + m_AllLoaded = true;
1.174 + PluginIter i;
1.175 +
1.176 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.177 + {
1.178 + if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
1.179 + {
1.180 + //API 4 is when we added this callback
1.181 + //Min version is now 5, so we ignore this check
1.182 + //if ( (*i)->m_API->GetApiVersion() >= 004 )
1.183 + (*i)->m_API->AllPluginsLoaded();
1.184 + }
1.185 + }
1.186 +}
1.187 +
1.188 +bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
1.189 +{
1.190 + CPlugin *pl = FindById(id);
1.191 +
1.192 + if (!pl)
1.193 + {
1.194 + UTIL_Format(error, maxlen, "Plugin id not found");
1.195 + return false;
1.196 + }
1.197 +
1.198 + bool ret;
1.199 +
1.200 + if ( (ret=_Pause(pl, error, maxlen)) == true )
1.201 + {
1.202 + ITER_PLEVENT(OnPluginPause, pl->m_Id);
1.203 + }
1.204 +
1.205 + return ret;
1.206 +}
1.207 +
1.208 +bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
1.209 +{
1.210 + CPlugin *pl = FindById(id);
1.211 +
1.212 + if (!pl)
1.213 + {
1.214 + UTIL_Format(error, maxlen, "Plugin id not found");
1.215 + return false;
1.216 + }
1.217 +
1.218 + bool ret;
1.219 +
1.220 + if ( (ret=_Unpause(pl, error, maxlen)) == true )
1.221 + {
1.222 + ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
1.223 + }
1.224 +
1.225 + return ret;
1.226 +}
1.227 +
1.228 +bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
1.229 +{
1.230 + CPlugin *pl = FindById(id);
1.231 +
1.232 + if (!pl)
1.233 + {
1.234 + UTIL_Format(error, maxlen, "Plugin %d not found", id);
1.235 + return false;
1.236 + }
1.237 +
1.238 + bool ret;
1.239 + PluginId old_id = pl->m_Id;
1.240 + if ( (ret=_Unload(pl, force, error, maxlen)) == true )
1.241 + {
1.242 + ITER_PLEVENT(OnPluginUnload, old_id);
1.243 + }
1.244 +
1.245 + return ret;
1.246 +}
1.247 +
1.248 +bool CPluginManager::Retry(PluginId id, char *error, size_t len)
1.249 +{
1.250 + PluginIter i;
1.251 + char buffer[64];
1.252 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.253 + {
1.254 + if ( (*i) && (*i)->m_Id == id )
1.255 + {
1.256 + if ( (*i)->m_Status >= Pl_Paused)
1.257 + {
1.258 + UTIL_Format(error, len, "Plugin %d is already running.", id);
1.259 + return false;
1.260 + }
1.261 + CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
1.262 + if (!pl)
1.263 + return false;
1.264 + if (pl->m_Status >= Pl_Paused)
1.265 + {
1.266 + //Now it gets crazy... unload the original copy.
1.267 + _Unload( (*i), true, buffer, sizeof(buffer)-1 );
1.268 +
1.269 + //Set the new copy's id
1.270 + pl->m_Id = id;
1.271 +
1.272 + //We just wasted an id... reclaim it
1.273 + m_LastId--;
1.274 +
1.275 + return true;
1.276 + } else {
1.277 + //don't really care about the buffer here
1.278 + _Unload(pl, true, buffer, sizeof(buffer)-1);
1.279 +
1.280 + //We just wasted an id... reclaim it
1.281 + m_LastId--;
1.282 + return false;
1.283 + }
1.284 + }
1.285 + }
1.286 +
1.287 + UTIL_Format(error, len, "Plugin %d not found,", id);
1.288 + return false;
1.289 +}
1.290 +
1.291 +CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
1.292 +{
1.293 + PluginIter i;
1.294 +
1.295 + //don't find bad plugins!
1.296 + if (!api)
1.297 + return NULL;
1.298 +
1.299 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.300 + {
1.301 + if ( (*i)->m_API == api )
1.302 + return (*i);
1.303 + }
1.304 +
1.305 + return NULL;
1.306 +}
1.307 +
1.308 +CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
1.309 +{
1.310 + FILE *fp;
1.311 + CPlugin *pl;
1.312 +
1.313 + pl = new CPlugin();
1.314 + *error = '\0';
1.315 +
1.316 + //Add plugin to list
1.317 + pl->m_Id = m_LastId;
1.318 + pl->m_File.assign(file);
1.319 + m_Plugins.push_back(pl);
1.320 + m_LastId++;
1.321 +
1.322 + //Check if the file even exists
1.323 + fp = fopen(file, "r");
1.324 + if (!fp)
1.325 + {
1.326 + if (error)
1.327 + UTIL_Format(error, maxlen, "File not found: %s", file);
1.328 + pl->m_Status = Pl_NotFound;
1.329 + }
1.330 +
1.331 + if (fp)
1.332 + {
1.333 + fclose(fp);
1.334 + fp = NULL;
1.335 +
1.336 + //Load the file
1.337 + pl->m_Lib = dlmount(file);
1.338 + if (!pl->m_Lib)
1.339 + {
1.340 + if (error)
1.341 + UTIL_Format(error, maxlen, "%s", dlerror());
1.342 + pl->m_Status = Pl_Error;
1.343 + } else {
1.344 + CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
1.345 + if (!pfn)
1.346 + {
1.347 + if (error)
1.348 + UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
1.349 + pl->m_Status = Pl_Error;
1.350 + } else {
1.351 + pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
1.352 + if (!pl->m_API)
1.353 + {
1.354 + if (error)
1.355 + UTIL_Format(error, maxlen, "Failed to get API");
1.356 + pl->m_Status = Pl_Error;
1.357 + } else {
1.358 + int api = pl->m_API->GetApiVersion();
1.359 + if (api < PLAPI_MIN_VERSION)
1.360 + {
1.361 + if (error)
1.362 + UTIL_Format(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
1.363 + pl->m_Status = Pl_Error;
1.364 + } else if (api > PLAPI_VERSION) {
1.365 + if (error)
1.366 + UTIL_Format(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
1.367 + pl->m_Status = Pl_Error;
1.368 + } else {
1.369 + if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), error, maxlen, m_AllLoaded))
1.370 + {
1.371 + pl->m_Status = Pl_Running;
1.372 + if (m_AllLoaded)
1.373 + {
1.374 + //API 4 is when we added this callback
1.375 + //Removing this code as the min version is now 5
1.376 + //if (pl->m_API->GetApiVersion() >= 4)
1.377 + pl->m_API->AllPluginsLoaded();
1.378 + }
1.379 + } else {
1.380 + pl->m_Status = Pl_Refused;
1.381 + }
1.382 + }
1.383 + }
1.384 + }
1.385 + }
1.386 + }
1.387 +
1.388 + if (pl->m_Lib && (pl->m_Status < Pl_Paused))
1.389 + {
1.390 + pl->m_Events.clear();
1.391 + g_SourceHook.UnloadPlugin(pl->m_Id);
1.392 + UnregAllConCmds(pl);
1.393 +
1.394 + dlclose(pl->m_Lib);
1.395 + pl->m_Lib = NULL;
1.396 + pl->m_API = NULL;
1.397 + }
1.398 +
1.399 + return pl;
1.400 +}
1.401 +
1.402 +bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
1.403 +{
1.404 + if (error)
1.405 + *error = '\0';
1.406 + if (pl->m_API && pl->m_Lib)
1.407 + {
1.408 + //Note, we'll always tell the plugin it will be unloading...
1.409 + if (pl->m_API->Unload(error, maxlen) || force)
1.410 + {
1.411 + //Make sure to detach it from sourcehook!
1.412 + g_SourceHook.UnloadPlugin(pl->m_Id);
1.413 +
1.414 + UnregAllConCmds(pl);
1.415 +
1.416 + //Clean up the DLL
1.417 + dlclose(pl->m_Lib);
1.418 + pl->m_Lib = NULL;
1.419 + pl->m_API = NULL;
1.420 +
1.421 + //Remove the plugin from the list
1.422 + PluginIter i;
1.423 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.424 + {
1.425 + if ( (*i)->m_Id == pl->m_Id )
1.426 + {
1.427 + i = m_Plugins.erase(i);
1.428 + break;
1.429 + }
1.430 + }
1.431 + //Free its memory
1.432 + delete pl;
1.433 +
1.434 + return true;
1.435 + }
1.436 + } else {
1.437 + //The plugin is not valid, and let's just remove it from the list anyway
1.438 + PluginIter i;
1.439 + for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
1.440 + {
1.441 + if ( (*i)->m_Id == pl->m_Id )
1.442 + {
1.443 + i = m_Plugins.erase(i);
1.444 + break;
1.445 + }
1.446 + }
1.447 + delete pl;
1.448 +
1.449 + return true;
1.450 + }
1.451 +
1.452 + return false;
1.453 +}
1.454 +
1.455 +bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
1.456 +{
1.457 + if (error)
1.458 + *error = '\0';
1.459 + if (pl->m_Status != Pl_Running || !pl->m_API)
1.460 + {
1.461 + if (error)
1.462 + UTIL_Format(error, maxlen, "Plugin cannot be paused");
1.463 + } else {
1.464 + if (pl->m_API->Pause(error, maxlen))
1.465 + {
1.466 + g_SourceHook.PausePlugin(pl->m_Id);
1.467 + pl->m_Status = Pl_Paused;
1.468 +
1.469 + return true;
1.470 + }
1.471 + }
1.472 +
1.473 + return false;
1.474 +}
1.475 +
1.476 +bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
1.477 +{
1.478 + if (error)
1.479 + *error = '\0';
1.480 +
1.481 + if (pl->m_Status != Pl_Paused || !pl->m_API)
1.482 + {
1.483 + if (error)
1.484 + UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
1.485 + } else {
1.486 + if (pl->m_API->Unpause(error, maxlen))
1.487 + {
1.488 + g_SourceHook.UnpausePlugin(pl->m_Id);
1.489 + pl->m_Status = Pl_Running;
1.490 +
1.491 + return true;
1.492 + }
1.493 + }
1.494 +
1.495 + return false;
1.496 +}
1.497 +
1.498 +bool CPluginManager::UnloadAll()
1.499 +{
1.500 + PluginIter i;
1.501 +
1.502 + char error[128];
1.503 + bool status = true;
1.504 +
1.505 + while ((i = m_Plugins.begin()) != m_Plugins.end())
1.506 + {
1.507 + if ( !_Unload( (*i), true, error, sizeof(error)) )
1.508 + {
1.509 + status = false;
1.510 + }
1.511 + }
1.512 +
1.513 + return status;
1.514 +}
1.515 +
1.516 +bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
1.517 +{
1.518 + CPlugin *pl = FindById(id);
1.519 +
1.520 + if (!pl)
1.521 + return false;
1.522 +
1.523 + file = pl->m_File.c_str();
1.524 + status = pl->m_Status;
1.525 + source = pl->m_Source;
1.526 +
1.527 + return true;
1.528 +}
1.529 +
1.530 +bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
1.531 +{
1.532 + CPlugin *pl = FindById(id);
1.533 +
1.534 + if (!pl || !pl->m_API)
1.535 + {
1.536 + if (error)
1.537 + UTIL_Format(error, maxlength, "Plugin not valid");
1.538 + return false;
1.539 + }
1.540 +
1.541 + return pl->m_API->QueryRunning(error, maxlength);
1.542 +}
1.543 +
1.544 +bool CPluginManager::QueryHandle(PluginId id, void *&handle)
1.545 +{
1.546 + CPlugin *pl = FindById(id);
1.547 +
1.548 + if (!pl)
1.549 + {
1.550 + return false;
1.551 + }
1.552 +
1.553 + handle = static_cast<void *>(pl->m_Lib);
1.554 +
1.555 + return true;
1.556 +}
1.557 +
1.558 +PluginIter CPluginManager::_begin()
1.559 +{
1.560 + return m_Plugins.begin();
1.561 +}
1.562 +
1.563 +PluginIter CPluginManager::_end()
1.564 +{
1.565 + return m_Plugins.end();
1.566 +}
1.567 +
1.568 +void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
1.569 +{
1.570 + CPlugin *pl = FindByAPI(api);
1.571 +
1.572 + if (!pl)
1.573 + return;
1.574 +
1.575 + pl->m_Cvars.push_back(pCvar);
1.576 +}
1.577 +
1.578 +void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
1.579 +{
1.580 + CPlugin *pl = FindByAPI(api);
1.581 +
1.582 + if (!pl)
1.583 + return;
1.584 +
1.585 + pl->m_Cmds.push_back(pCmd);
1.586 +}
1.587 +
1.588 +void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
1.589 +{
1.590 + CPlugin *pl = FindByAPI(api);
1.591 +
1.592 + if (!pl)
1.593 + return;
1.594 +
1.595 + pl->m_Cvars.remove(pCvar);
1.596 +}
1.597 +
1.598 +void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
1.599 +{
1.600 + CPlugin *pl = FindByAPI(api);
1.601 +
1.602 + if (!pl)
1.603 + return;
1.604 +
1.605 + pl->m_Cmds.remove(pCmd);
1.606 +}
1.607 +
1.608 +void CPluginManager::UnregAllConCmds(CPlugin *pl)
1.609 +{
1.610 + SourceHook::List<ConCommandBase *>::iterator i;
1.611 +
1.612 + for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
1.613 + g_SMConVarAccessor.Unregister( (*i) );
1.614 +
1.615 + pl->m_Cvars.clear();
1.616 +
1.617 + for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
1.618 + g_SMConVarAccessor.Unregister( (*i) );
1.619 +
1.620 + pl->m_Cmds.clear();
1.621 +}