3 * ======================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
33 #include "metamod_plugins.h"
34 #include "metamod_util.h"
41 * @brief Implements functions from CPlugin.h
45 using namespace SourceMM;
47 #define ITER_PLEVENT(evn, plid) \
48 CPluginManager::CPlugin *_Xpl; \
49 SourceHook::List<IMetamodListener *>::iterator event; \
50 IMetamodListener *api; \
51 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
53 if (_Xpl->m_Id == plid) \
55 for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
61 CPluginManager g_PluginMngr;
63 CPluginManager::CPluginManager()
69 CPluginManager::~CPluginManager()
71 SourceHook::List<CNameAlias *>::iterator iter;
73 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
81 const char *CPluginManager::LookupAlias(const char *alias)
83 SourceHook::List<CNameAlias *>::iterator iter;
86 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
89 if (p->alias.compare(alias) == 0)
91 return p->value.c_str();
98 SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_begin()
100 return m_Aliases.begin();
103 SourceHook::List<SourceMM::CNameAlias *>::iterator CPluginManager::_alias_end()
105 return m_Aliases.end();
108 void CPluginManager::SetAlias(const char *alias, const char *value)
110 SourceHook::List<CNameAlias *>::iterator iter;
113 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
116 if (p->alias.compare(alias) == 0)
118 if (value[0] == '\0')
120 iter = m_Aliases.erase(iter);
125 p->value.assign(value);
131 if (value[0] != '\0')
135 p->alias.assign(alias);
136 p->value.assign(value);
138 m_Aliases.push_back(p);
142 CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL)
147 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
150 //Check if we're about to reload an old plugin
151 PluginIter i = m_Plugins.begin();
152 while (i != m_Plugins.end())
154 if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
156 if ((*i)->m_Status < Pl_Paused)
158 //Attempt to load the plugin again
160 i = m_Plugins.erase(i);
173 CPlugin *pl = _Load(file, source, error, maxlen);
180 ITER_PLEVENT(OnPluginLoad, pl->m_Id);
185 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
189 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
191 if ( (*i)->m_Id == id )\
200 void CPluginManager::SetAllLoaded()
205 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
207 if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
209 //API 4 is when we added this callback
210 //Min version is now 5, so we ignore this check
211 //if ( (*i)->m_API->GetApiVersion() >= 004 )
212 (*i)->m_API->AllPluginsLoaded();
217 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
219 CPlugin *pl = FindById(id);
223 UTIL_Format(error, maxlen, "Plugin id not found");
229 if ((ret=_Pause(pl, error, maxlen)) == true)
231 ITER_PLEVENT(OnPluginPause, pl->m_Id);
237 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
239 CPlugin *pl = FindById(id);
243 UTIL_Format(error, maxlen, "Plugin id not found");
249 if ( (ret=_Unpause(pl, error, maxlen)) == true )
251 ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
257 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
259 CPlugin *pl = FindById(id);
263 UTIL_Format(error, maxlen, "Plugin %d not found", id);
268 PluginId old_id = pl->m_Id;
269 if ( (ret=_Unload(pl, force, error, maxlen)) == true )
271 ITER_PLEVENT(OnPluginUnload, old_id);
277 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
281 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
283 if ( (*i) && (*i)->m_Id == id )
285 if ( (*i)->m_Status >= Pl_Paused)
287 UTIL_Format(error, len, "Plugin %d is already running.", id);
290 CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
295 if (pl->m_Status >= Pl_Paused)
297 //Now it gets crazy... unload the original copy.
298 _Unload( (*i), true, buffer, sizeof(buffer)-1 );
300 //Set the new copy's id
303 //We just wasted an id... reclaim it
310 //don't really care about the buffer here
311 _Unload(pl, true, buffer, sizeof(buffer)-1);
313 //We just wasted an id... reclaim it
320 UTIL_Format(error, len, "Plugin %d not found,", id);
324 CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
328 //don't find bad plugins!
334 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
336 if ((*i)->m_API == api)
345 int CPluginManager::GetPluginCount()
347 return (int)m_Plugins.size();
350 const char *CPluginManager::GetStatusText(CPlugin *pl)
352 switch (pl->m_Status)
364 if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
376 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
386 pl->m_File.assign(file);
387 m_Plugins.push_back(pl);
390 //Check if the file even exists
391 fp = fopen(file, "r");
396 UTIL_Format(error, maxlen, "File not found: %s", file);
398 pl->m_Status = Pl_NotFound;
407 pl->m_Lib = dlmount(file);
412 UTIL_Format(error, maxlen, "%s", dlerror());
414 pl->m_Status = Pl_Error;
418 CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
423 UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
425 pl->m_Status = Pl_Error;
429 pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
434 UTIL_Format(error, maxlen, "Failed to get API");
436 pl->m_Status = Pl_Error;
440 int api = pl->m_API->GetApiVersion();
441 if (api < PLAPI_MIN_VERSION)
445 UTIL_Format(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", api, PLAPI_MIN_VERSION);
447 pl->m_Status = Pl_Error;
449 else if (api > PLAPI_VERSION)
453 UTIL_Format(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
455 pl->m_Status = Pl_Error;
459 if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
461 pl->m_Status = Pl_Running;
464 //API 4 is when we added this callback
465 //Removing this code as the min version is now 5
466 //if (pl->m_API->GetApiVersion() >= 4)
467 pl->m_API->AllPluginsLoaded();
472 pl->m_Status = Pl_Refused;
480 if (pl->m_Lib && (pl->m_Status < Pl_Paused))
482 pl->m_Events.clear();
483 g_SourceHook.UnloadPlugin(pl->m_Id);
494 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
501 if (pl->m_API && pl->m_Lib)
503 //Note, we'll always tell the plugin it will be unloading...
504 if (pl->m_API->Unload(error, maxlen) || force)
506 //Make sure to detach it from sourcehook!
507 g_SourceHook.UnloadPlugin(pl->m_Id);
509 pl->m_Events.clear();
518 //Remove the plugin from the list
520 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
522 if ( (*i)->m_Id == pl->m_Id )
524 i = m_Plugins.erase(i);
534 //The plugin is not valid, and let's just remove it from the list anyway
536 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
538 if ( (*i)->m_Id == pl->m_Id )
540 i = m_Plugins.erase(i);
552 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
559 if (pl->m_Status != Pl_Running || !pl->m_API)
563 UTIL_Format(error, maxlen, "Plugin cannot be paused");
568 if (pl->m_API->Pause(error, maxlen))
570 g_SourceHook.PausePlugin(pl->m_Id);
571 pl->m_Status = Pl_Paused;
580 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
587 if (pl->m_Status != Pl_Paused || !pl->m_API)
591 UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
596 if (pl->m_API->Unpause(error, maxlen))
598 g_SourceHook.UnpausePlugin(pl->m_Id);
599 pl->m_Status = Pl_Running;
608 bool CPluginManager::UnloadAll()
615 while ((i = m_Plugins.begin()) != m_Plugins.end())
617 if ( !_Unload( (*i), true, error, sizeof(error)) )
626 bool CPluginManager::Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source)
628 CPlugin *pl = FindById(id);
635 file = pl->m_File.c_str();
636 status = pl->m_Status;
637 source = pl->m_Source;
642 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
644 CPlugin *pl = FindById(id);
646 if (!pl || !pl->m_API)
650 UTIL_Format(error, maxlength, "Plugin not valid");
655 return pl->m_API->QueryRunning(error, maxlength);
658 bool CPluginManager::QueryHandle(PluginId id, void *&handle)
660 CPlugin *pl = FindById(id);
667 handle = static_cast<void *>(pl->m_Lib);
672 PluginIter CPluginManager::_begin()
674 return m_Plugins.begin();
677 PluginIter CPluginManager::_end()
679 return m_Plugins.end();
682 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
684 CPlugin *pl = FindByAPI(api);
691 pl->m_Cvars.push_back(pCvar);
694 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
696 CPlugin *pl = FindByAPI(api);
703 pl->m_Cmds.push_back(pCmd);
706 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
708 CPlugin *pl = FindByAPI(api);
715 pl->m_Cvars.remove(pCvar);
718 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
720 CPlugin *pl = FindByAPI(api);
727 pl->m_Cmds.remove(pCmd);
730 void CPluginManager::UnregAllConCmds(CPlugin *pl)
732 SourceHook::List<ConCommandBase *>::iterator i;
735 for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
737 g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
741 for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
743 g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );