2005-04-16 21:59:26 +02:00
|
|
|
/* ======== SourceMM ========
|
2007-02-10 01:31:34 +01:00
|
|
|
* Copyright (C) 2004-2007 Metamod:Source Development Team
|
|
|
|
* No warranties of any kind
|
|
|
|
*
|
|
|
|
* License: zlib/libpng
|
|
|
|
*
|
|
|
|
* Author(s): David "BAILOPAN" Anderson
|
|
|
|
* ============================
|
|
|
|
*/
|
2005-04-16 21:59:26 +02:00
|
|
|
|
|
|
|
#include "CPlugin.h"
|
2005-04-17 01:33:39 +02:00
|
|
|
#include "CSmmAPI.h"
|
|
|
|
#include "sourcemm.h"
|
2005-05-26 10:06:23 +02:00
|
|
|
#include "concommands.h"
|
2005-04-16 21:59:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Implements functions from CPlugin.h
|
|
|
|
* @file CPlugin.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
using namespace SourceMM;
|
|
|
|
|
2005-10-21 09:18:53 +02:00
|
|
|
#define ITER_PLEVENT(evn, plid) \
|
|
|
|
CPluginManager::CPlugin *_Xpl; \
|
|
|
|
SourceHook::List<IMetamodListener *>::iterator event; \
|
|
|
|
IMetamodListener *api; \
|
|
|
|
for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
|
|
|
|
_Xpl = (*iter); \
|
|
|
|
if (_Xpl->m_Id == plid) \
|
|
|
|
continue; \
|
|
|
|
for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
|
|
|
|
api = (*event); \
|
|
|
|
api->evn(plid); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
CPluginManager g_PluginMngr;
|
|
|
|
|
|
|
|
CPluginManager::CPluginManager()
|
|
|
|
{
|
|
|
|
m_LastId = Pl_MinId;
|
2005-04-18 20:21:57 +02:00
|
|
|
m_AllLoaded = false;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CPluginManager::~CPluginManager()
|
|
|
|
{
|
2006-05-25 06:49:47 +02:00
|
|
|
SourceHook::List<CNameAlias *>::iterator iter;
|
|
|
|
|
|
|
|
for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
|
|
|
|
{
|
|
|
|
delete (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Aliases.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CPluginManager::LookupAlias(const char *alias)
|
|
|
|
{
|
|
|
|
SourceHook::List<CNameAlias *>::iterator iter;
|
|
|
|
CNameAlias *p;
|
|
|
|
|
|
|
|
for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
|
|
|
|
{
|
|
|
|
p = (*iter);
|
|
|
|
if (p->alias.compare(alias) == 0)
|
|
|
|
{
|
|
|
|
return p->value.c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_begin()
|
|
|
|
{
|
|
|
|
return m_Aliases.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_end()
|
|
|
|
{
|
|
|
|
return m_Aliases.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginManager::SetAlias(const char *alias, const char *value)
|
|
|
|
{
|
|
|
|
SourceHook::List<CNameAlias *>::iterator iter;
|
|
|
|
CNameAlias *p;
|
|
|
|
|
|
|
|
for (iter=m_Aliases.begin(); iter!=m_Aliases.end(); iter++)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
2006-05-25 06:49:47 +02:00
|
|
|
p = (*iter);
|
|
|
|
if (p->alias.compare(alias) == 0)
|
|
|
|
{
|
|
|
|
if (value[0] == '\0')
|
|
|
|
{
|
|
|
|
iter = m_Aliases.erase(iter);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
p->value.assign(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value[0] != '\0')
|
|
|
|
{
|
|
|
|
p = new CNameAlias;
|
|
|
|
|
|
|
|
p->alias.assign(alias);
|
|
|
|
p->value.assign(value);
|
|
|
|
|
|
|
|
m_Aliases.push_back(p);
|
|
|
|
}
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
2006-08-13 13:34:30 +02:00
|
|
|
CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
2005-10-20 16:37:47 +02:00
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
already = false;
|
|
|
|
//Check if we're about to reload an old plugin
|
2005-12-22 22:00:54 +01:00
|
|
|
PluginIter i = m_Plugins.begin();
|
|
|
|
while (i != m_Plugins.end())
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
2006-02-09 07:55:02 +01:00
|
|
|
if ( (*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()) )
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
if ( (*i)->m_Status < Pl_Paused )
|
|
|
|
{
|
|
|
|
//Attempt to load the plugin again
|
|
|
|
already = true;
|
|
|
|
i = m_Plugins.erase(i);
|
2005-12-22 14:04:08 +01:00
|
|
|
continue;
|
2005-04-16 21:59:26 +02:00
|
|
|
} else {
|
|
|
|
//No need to load it
|
|
|
|
already = true;
|
|
|
|
return (*i)->m_Id;
|
|
|
|
}
|
|
|
|
}
|
2005-12-22 22:00:54 +01:00
|
|
|
i++;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CPlugin *pl = _Load(file, source, error, maxlen);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return Pl_BadLoad;
|
|
|
|
|
2005-10-21 09:18:53 +02:00
|
|
|
ITER_PLEVENT(OnPluginLoad, pl->m_Id);
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
return pl->m_Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
|
|
|
|
{
|
|
|
|
PluginIter i;
|
|
|
|
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i)->m_Id == id )
|
|
|
|
return (*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-18 20:21:57 +02:00
|
|
|
void CPluginManager::SetAllLoaded()
|
|
|
|
{
|
|
|
|
m_AllLoaded = true;
|
|
|
|
PluginIter i;
|
|
|
|
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
|
|
|
|
{
|
2005-05-01 20:24:48 +02:00
|
|
|
//API 4 is when we added this callback
|
|
|
|
//Min version is now 5, so we ignore this check
|
|
|
|
//if ( (*i)->m_API->GetApiVersion() >= 004 )
|
|
|
|
(*i)->m_API->AllPluginsLoaded();
|
2005-04-18 20:21:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
{
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin id not found");
|
2005-04-16 21:59:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-21 09:18:53 +02:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if ( (ret=_Pause(pl, error, maxlen)) == true )
|
|
|
|
{
|
|
|
|
ITER_PLEVENT(OnPluginPause, pl->m_Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
{
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin id not found");
|
2005-04-16 21:59:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-21 09:18:53 +02:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if ( (ret=_Unpause(pl, error, maxlen)) == true )
|
|
|
|
{
|
|
|
|
ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
2005-04-17 01:33:39 +02:00
|
|
|
bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
{
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin %d not found", id);
|
2005-04-16 21:59:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-21 09:18:53 +02:00
|
|
|
bool ret;
|
|
|
|
PluginId old_id = pl->m_Id;
|
|
|
|
if ( (ret=_Unload(pl, force, error, maxlen)) == true )
|
|
|
|
{
|
|
|
|
ITER_PLEVENT(OnPluginUnload, old_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
2005-04-19 09:41:07 +02:00
|
|
|
bool CPluginManager::Retry(PluginId id, char *error, size_t len)
|
|
|
|
{
|
|
|
|
PluginIter i;
|
|
|
|
char buffer[64];
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i) && (*i)->m_Id == id )
|
|
|
|
{
|
|
|
|
if ( (*i)->m_Status >= Pl_Paused)
|
|
|
|
{
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, len, "Plugin %d is already running.", id);
|
2005-04-19 09:41:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
|
|
|
|
if (!pl)
|
|
|
|
return false;
|
|
|
|
if (pl->m_Status >= Pl_Paused)
|
|
|
|
{
|
|
|
|
//Now it gets crazy... unload the original copy.
|
|
|
|
_Unload( (*i), true, buffer, sizeof(buffer)-1 );
|
|
|
|
|
|
|
|
//Set the new copy's id
|
|
|
|
pl->m_Id = id;
|
|
|
|
|
|
|
|
//We just wasted an id... reclaim it
|
|
|
|
m_LastId--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
//don't really care about the buffer here
|
|
|
|
_Unload(pl, true, buffer, sizeof(buffer)-1);
|
|
|
|
|
|
|
|
//We just wasted an id... reclaim it
|
|
|
|
m_LastId--;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, len, "Plugin %d not found,", id);
|
2005-04-19 09:41:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-26 10:06:23 +02:00
|
|
|
CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
|
|
|
|
{
|
|
|
|
PluginIter i;
|
|
|
|
|
|
|
|
//don't find bad plugins!
|
|
|
|
if (!api)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i)->m_API == api )
|
|
|
|
return (*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-11 01:43:16 +02:00
|
|
|
int CPluginManager::GetPluginCount()
|
|
|
|
{
|
|
|
|
return (int)m_Plugins.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CPluginManager::GetStatusText(CPlugin *pl)
|
|
|
|
{
|
|
|
|
switch (pl->m_Status)
|
|
|
|
{
|
|
|
|
case Pl_NotFound:
|
|
|
|
return "NOFILE";
|
|
|
|
case Pl_Error:
|
|
|
|
return "ERROR";
|
|
|
|
case Pl_Refused:
|
|
|
|
return "FAILED";
|
|
|
|
case Pl_Paused:
|
|
|
|
return "PAUSED";
|
|
|
|
case Pl_Running:
|
|
|
|
{
|
|
|
|
if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
|
|
|
|
{
|
|
|
|
return "STOPPED";
|
|
|
|
} else {
|
|
|
|
return "RUNNING";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "-";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
CPlugin *pl;
|
|
|
|
|
|
|
|
pl = new CPlugin();
|
|
|
|
*error = '\0';
|
|
|
|
|
|
|
|
//Add plugin to list
|
|
|
|
pl->m_Id = m_LastId;
|
|
|
|
pl->m_File.assign(file);
|
|
|
|
m_Plugins.push_back(pl);
|
|
|
|
m_LastId++;
|
|
|
|
|
|
|
|
//Check if the file even exists
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "File not found: %s", file);
|
2005-04-16 21:59:26 +02:00
|
|
|
pl->m_Status = Pl_NotFound;
|
2006-05-25 06:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
2005-04-16 21:59:26 +02:00
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
//Load the file
|
|
|
|
pl->m_Lib = dlmount(file);
|
|
|
|
if (!pl->m_Lib)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "%s", dlerror());
|
2005-04-16 21:59:26 +02:00
|
|
|
pl->m_Status = Pl_Error;
|
|
|
|
} else {
|
2006-08-09 03:52:49 +02:00
|
|
|
CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
|
2005-04-16 21:59:26 +02:00
|
|
|
if (!pfn)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
|
2005-04-16 21:59:26 +02:00
|
|
|
pl->m_Status = Pl_Error;
|
|
|
|
} else {
|
|
|
|
pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
|
|
|
|
if (!pl->m_API)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Failed to get API");
|
2005-04-16 21:59:26 +02:00
|
|
|
pl->m_Status = Pl_Error;
|
|
|
|
} else {
|
2005-06-25 00:26:19 +02:00
|
|
|
int api = pl->m_API->GetApiVersion();
|
|
|
|
if (api < PLAPI_MIN_VERSION)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
|
2005-06-25 00:26:19 +02:00
|
|
|
pl->m_Status = Pl_Error;
|
|
|
|
} else if (api > PLAPI_VERSION) {
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
|
2005-04-16 21:59:26 +02:00
|
|
|
pl->m_Status = Pl_Error;
|
|
|
|
} else {
|
2005-10-20 16:37:47 +02:00
|
|
|
if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), error, maxlen, m_AllLoaded))
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
pl->m_Status = Pl_Running;
|
2005-04-18 20:21:57 +02:00
|
|
|
if (m_AllLoaded)
|
|
|
|
{
|
2005-05-01 20:24:48 +02:00
|
|
|
//API 4 is when we added this callback
|
|
|
|
//Removing this code as the min version is now 5
|
|
|
|
//if (pl->m_API->GetApiVersion() >= 4)
|
|
|
|
pl->m_API->AllPluginsLoaded();
|
2005-04-18 20:21:57 +02:00
|
|
|
}
|
2005-04-16 21:59:26 +02:00
|
|
|
} else {
|
|
|
|
pl->m_Status = Pl_Refused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pl->m_Lib && (pl->m_Status < Pl_Paused))
|
|
|
|
{
|
2006-07-20 12:53:06 +02:00
|
|
|
pl->m_Events.clear();
|
|
|
|
g_SourceHook.UnloadPlugin(pl->m_Id);
|
|
|
|
UnregAllConCmds(pl);
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
dlclose(pl->m_Lib);
|
|
|
|
pl->m_Lib = NULL;
|
|
|
|
pl->m_API = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
2005-04-17 01:33:39 +02:00
|
|
|
bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = '\0';
|
|
|
|
if (pl->m_API && pl->m_Lib)
|
|
|
|
{
|
2005-04-17 01:33:39 +02:00
|
|
|
//Note, we'll always tell the plugin it will be unloading...
|
|
|
|
if (pl->m_API->Unload(error, maxlen) || force)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
2005-04-17 01:33:39 +02:00
|
|
|
//Make sure to detach it from sourcehook!
|
|
|
|
g_SourceHook.UnloadPlugin(pl->m_Id);
|
|
|
|
|
2005-05-26 10:06:23 +02:00
|
|
|
UnregAllConCmds(pl);
|
|
|
|
|
2005-04-17 01:33:39 +02:00
|
|
|
//Clean up the DLL
|
2005-04-16 21:59:26 +02:00
|
|
|
dlclose(pl->m_Lib);
|
|
|
|
pl->m_Lib = NULL;
|
|
|
|
pl->m_API = NULL;
|
|
|
|
|
|
|
|
//Remove the plugin from the list
|
|
|
|
PluginIter i;
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i)->m_Id == pl->m_Id )
|
|
|
|
{
|
|
|
|
i = m_Plugins.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 01:33:39 +02:00
|
|
|
//Free its memory
|
2005-04-16 21:59:26 +02:00
|
|
|
delete pl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//The plugin is not valid, and let's just remove it from the list anyway
|
|
|
|
PluginIter i;
|
|
|
|
for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
|
|
|
|
{
|
|
|
|
if ( (*i)->m_Id == pl->m_Id )
|
|
|
|
{
|
|
|
|
i = m_Plugins.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete pl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = '\0';
|
|
|
|
if (pl->m_Status != Pl_Running || !pl->m_API)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin cannot be paused");
|
2005-04-16 21:59:26 +02:00
|
|
|
} else {
|
2005-04-17 01:33:39 +02:00
|
|
|
if (pl->m_API->Pause(error, maxlen))
|
|
|
|
{
|
|
|
|
g_SourceHook.PausePlugin(pl->m_Id);
|
2005-04-17 05:37:50 +02:00
|
|
|
pl->m_Status = Pl_Paused;
|
|
|
|
|
|
|
|
return true;
|
2005-04-17 01:33:39 +02:00
|
|
|
}
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = '\0';
|
|
|
|
|
|
|
|
if (pl->m_Status != Pl_Paused || !pl->m_API)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
|
2005-04-16 21:59:26 +02:00
|
|
|
} else {
|
2005-04-17 01:33:39 +02:00
|
|
|
if (pl->m_API->Unpause(error, maxlen))
|
|
|
|
{
|
|
|
|
g_SourceHook.UnpausePlugin(pl->m_Id);
|
2005-04-17 05:37:50 +02:00
|
|
|
pl->m_Status = Pl_Running;
|
|
|
|
|
|
|
|
return true;
|
2005-04-17 01:33:39 +02:00
|
|
|
}
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPluginManager::UnloadAll()
|
|
|
|
{
|
|
|
|
PluginIter i;
|
|
|
|
|
2005-06-26 21:15:24 +02:00
|
|
|
char error[128];
|
|
|
|
bool status = true;
|
2005-04-17 01:33:39 +02:00
|
|
|
|
2007-06-26 19:57:18 +02:00
|
|
|
while ((i = m_Plugins.begin()) != m_Plugins.end())
|
2005-06-26 21:15:24 +02:00
|
|
|
{
|
2007-03-21 18:45:28 +01:00
|
|
|
if ( !_Unload( (*i), true, error, sizeof(error)) )
|
2005-06-26 21:15:24 +02:00
|
|
|
status = false;
|
2005-04-16 21:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-10-20 16:37:47 +02:00
|
|
|
bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
|
2005-04-16 21:59:26 +02:00
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
file = pl->m_File.c_str();
|
|
|
|
status = pl->m_Status;
|
|
|
|
source = pl->m_Source;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-10-06 02:59:01 +02:00
|
|
|
bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl || !pl->m_API)
|
|
|
|
{
|
|
|
|
if (error)
|
2007-03-21 18:45:28 +01:00
|
|
|
UTIL_Format(error, maxlength, "Plugin not valid");
|
2005-10-06 02:59:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl->m_API->QueryRunning(error, maxlength);
|
|
|
|
}
|
|
|
|
|
2006-08-01 04:09:19 +02:00
|
|
|
bool CPluginManager::QueryHandle(PluginId id, void *&handle)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindById(id);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = static_cast<void *>(pl->m_Lib);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-16 21:59:26 +02:00
|
|
|
PluginIter CPluginManager::_begin()
|
|
|
|
{
|
|
|
|
return m_Plugins.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginIter CPluginManager::_end()
|
|
|
|
{
|
|
|
|
return m_Plugins.end();
|
|
|
|
}
|
2005-05-26 10:06:23 +02:00
|
|
|
|
|
|
|
void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindByAPI(api);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pl->m_Cvars.push_back(pCvar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindByAPI(api);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pl->m_Cmds.push_back(pCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindByAPI(api);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pl->m_Cvars.remove(pCvar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
|
|
|
|
{
|
|
|
|
CPlugin *pl = FindByAPI(api);
|
|
|
|
|
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pl->m_Cmds.remove(pCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginManager::UnregAllConCmds(CPlugin *pl)
|
|
|
|
{
|
2005-08-12 19:44:53 +02:00
|
|
|
SourceHook::List<ConCommandBase *>::iterator i;
|
2005-05-26 10:06:23 +02:00
|
|
|
|
|
|
|
for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
|
2007-07-23 20:19:02 +02:00
|
|
|
g_SMConVarAccessor.Unregister(pl->m_Id, (*i) );
|
2005-05-26 10:06:23 +02:00
|
|
|
|
|
|
|
pl->m_Cvars.clear();
|
|
|
|
|
|
|
|
for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
|
2007-07-23 20:19:02 +02:00
|
|
|
g_SMConVarAccessor.Unregister(pl->m_Id, (*i) );
|
2005-05-26 10:06:23 +02:00
|
|
|
|
|
|
|
pl->m_Cmds.clear();
|
|
|
|
}
|