More re-arranging. Not what we discussed in IRC but I think it works out better, Make can't deal with upward dependencies nicely.
3 * ======================================================
5 * Copyright (C) 2004-2008 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.
29 #include "metamod_oslink.h"
31 #include "metamod_plugins.h"
32 #include "metamod_util.h"
35 * @brief Implements functions from CPlugin.h
39 using namespace SourceMM;
41 #define ITER_PLEVENT(evn, plid) \
42 CPluginManager::CPlugin *_Xpl; \
43 SourceHook::List<IMetamodListener *>::iterator event; \
44 IMetamodListener *api; \
45 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
47 if (_Xpl->m_Id == plid) \
49 for (event=_Xpl->m_Events.begin(); event!=_Xpl->m_Events.end(); event++) { \
55 CPluginManager g_PluginMngr;
57 MetamodVersionInfo GlobVersionInfo =
64 METAMOD_PLAPI_VERSION,
68 CPluginManager::CPluginManager()
74 CPluginManager::~CPluginManager()
76 SourceHook::List<CNameAlias *>::iterator iter;
78 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
86 const char *CPluginManager::LookupAlias(const char *alias)
88 SourceHook::List<CNameAlias *>::iterator iter;
91 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
94 if (p->alias.compare(alias) == 0)
96 return p->value.c_str();
103 SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_begin()
105 return m_Aliases.begin();
108 SourceHook::List<CNameAlias *>::iterator CPluginManager::_alias_end()
110 return m_Aliases.end();
113 void CPluginManager::SetAlias(const char *alias, const char *value)
115 SourceHook::List<CNameAlias *>::iterator iter;
118 for (iter = m_Aliases.begin(); iter != m_Aliases.end(); iter++)
121 if (p->alias.compare(alias) == 0)
123 if (value[0] == '\0')
125 iter = m_Aliases.erase(iter);
130 p->value.assign(value);
136 if (value[0] != '\0')
140 p->alias.assign(alias);
141 p->value.assign(value);
143 m_Aliases.push_back(p);
147 CPluginManager::CPlugin::CPlugin() : m_Id(0), m_Source(0), m_API(NULL), m_Lib(NULL), m_UnloadFn(NULL)
152 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
155 //Check if we're about to reload an old plugin
156 PluginIter i = m_Plugins.begin();
157 while (i != m_Plugins.end())
159 if ((*i) && UTIL_PathCmp(file, (*i)->m_File.c_str()))
161 if ((*i)->m_Status < Pl_Paused)
163 //Attempt to load the plugin again
165 i = m_Plugins.erase(i);
178 CPlugin *pl = _Load(file, source, error, maxlen);
185 ITER_PLEVENT(OnPluginLoad, pl->m_Id);
190 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
194 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
196 if ( (*i)->m_Id == id )\
205 void CPluginManager::SetAllLoaded()
215 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
217 if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
219 //API 4 is when we added this callback
220 //Min version is now 5, so we ignore this check
221 //if ( (*i)->m_API->GetApiVersion() >= 004 )
222 (*i)->m_API->AllPluginsLoaded();
227 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
229 CPlugin *pl = FindById(id);
233 UTIL_Format(error, maxlen, "Plugin id not found");
239 if ((ret=_Pause(pl, error, maxlen)) == true)
241 ITER_PLEVENT(OnPluginPause, pl->m_Id);
247 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
249 CPlugin *pl = FindById(id);
253 UTIL_Format(error, maxlen, "Plugin id not found");
259 if ( (ret=_Unpause(pl, error, maxlen)) == true )
261 ITER_PLEVENT(OnPluginUnpause, pl->m_Id);
267 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
269 CPlugin *pl = FindById(id);
273 UTIL_Format(error, maxlen, "Plugin %d not found", id);
278 PluginId old_id = pl->m_Id;
279 if ( (ret=_Unload(pl, force, error, maxlen)) == true )
281 ITER_PLEVENT(OnPluginUnload, old_id);
287 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
291 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
293 if ( (*i) && (*i)->m_Id == id )
295 if ( (*i)->m_Status >= Pl_Paused)
297 UTIL_Format(error, len, "Plugin %d is already running.", id);
300 CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
305 if (pl->m_Status >= Pl_Paused)
307 //Now it gets crazy... unload the original copy.
308 _Unload( (*i), true, buffer, sizeof(buffer)-1 );
310 //Set the new copy's id
313 //We just wasted an id... reclaim it
320 //don't really care about the buffer here
321 _Unload(pl, true, buffer, sizeof(buffer)-1);
323 //We just wasted an id... reclaim it
330 UTIL_Format(error, len, "Plugin %d not found,", id);
334 CPluginManager::CPlugin *CPluginManager::FindByAPI(ISmmPlugin *api)
338 //don't find bad plugins!
344 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
346 if ((*i)->m_API == api)
355 int CPluginManager::GetPluginCount()
357 return (int)m_Plugins.size();
360 const char *CPluginManager::GetStatusText(CPlugin *pl)
362 switch (pl->m_Status)
374 if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
386 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
396 pl->m_File.assign(file);
397 m_Plugins.push_back(pl);
400 //Check if the file even exists
401 fp = fopen(file, "r");
406 UTIL_Format(error, maxlen, "File not found: %s", file);
408 pl->m_Status = Pl_NotFound;
417 pl->m_Lib = dlmount(file);
422 UTIL_Format(error, maxlen, "%s", dlerror());
424 pl->m_Status = Pl_Error;
431 * First, try the new "advanced" interface
433 METAMOD_FN_LOAD fnLoad = (METAMOD_FN_LOAD)dlsym(pl->m_Lib, "CreateInterface_MMS");
436 if (GlobVersionInfo.source_engine == SOURCE_ENGINE_UNKNOWN)
438 GlobVersionInfo.source_engine = g_Metamod.GetSourceEngineBuild();
441 /* Build path information */
443 size_t len = g_Metamod.PathFormat(file_path, sizeof(file_path), "%s", file);
445 for (size_t i = len - 1; i >= 0 && i < len; i--)
447 if (_IsPathSepChar(file_path[i]))
454 MetamodLoaderInfo info;
456 info.pl_path = file_path;
458 pl->m_API = fnLoad(&GlobVersionInfo, &info);
459 pl->m_UnloadFn = (METAMOD_FN_UNLOAD)dlsym(pl->m_Lib, "UnloadInterface_MMS");
463 * If we didn't get anything, try the normal/simple interface.
465 if (pl->m_API == NULL)
467 CreateInterfaceFn pfn = (CreateInterfaceFn)(dlsym(pl->m_Lib, PL_EXPOSURE_C));
472 UTIL_Format(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
474 pl->m_Status = Pl_Error;
478 pl->m_API = static_cast<ISmmPlugin *>((pfn)(METAMOD_PLAPI_NAME, NULL));
484 UTIL_Format(error, maxlen, "Failed to get API");
486 pl->m_Status = Pl_Error;
491 if (pl->m_API != NULL)
493 int api = pl->m_API->GetApiVersion();
494 if (api < PLAPI_MIN_VERSION)
500 UTIL_Format(error, maxlen, "Plugin uses experimental Metamod build, probably 1.6.x (%d < %d)", api, PLAPI_MIN_VERSION);
502 else if (api <= 12 && api >= 7)
504 UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.4.x (%d < %d)", api, PLAPI_MIN_VERSION);
508 UTIL_Format(error, maxlen, "Older Metamod version required, probably 1.0 (%d < %d)", api, PLAPI_MIN_VERSION);
511 pl->m_Status = Pl_Error;
513 else if (api > METAMOD_PLAPI_VERSION)
517 UTIL_Format(error, maxlen, "Plugin requires newer Metamod version (%d > %d)", api, METAMOD_PLAPI_VERSION);
519 pl->m_Status = Pl_Error;
523 if (pl->m_API->Load(pl->m_Id, &g_Metamod, error, maxlen, m_AllLoaded))
525 pl->m_Status = Pl_Running;
528 pl->m_API->AllPluginsLoaded();
533 pl->m_Status = Pl_Refused;
540 if (pl->m_Lib && (pl->m_Status < Pl_Paused))
542 pl->m_Events.clear();
543 g_SourceHook.UnloadPlugin(pl->m_Id);
546 if (pl->m_UnloadFn != NULL)
559 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
566 if (pl->m_API && pl->m_Lib)
568 //Note, we'll always tell the plugin it will be unloading...
569 if (pl->m_API->Unload(error, maxlen) || force)
571 //Make sure to detach it from sourcehook!
572 g_SourceHook.UnloadPlugin(pl->m_Id);
574 pl->m_Events.clear();
578 if (pl->m_UnloadFn != NULL)
588 //Remove the plugin from the list
590 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
592 if ( (*i)->m_Id == pl->m_Id )
594 i = m_Plugins.erase(i);
604 //The plugin is not valid, and let's just remove it from the list anyway
606 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
608 if ( (*i)->m_Id == pl->m_Id )
610 i = m_Plugins.erase(i);
622 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
629 if (pl->m_Status != Pl_Running || !pl->m_API)
633 UTIL_Format(error, maxlen, "Plugin cannot be paused");
638 if (pl->m_API->Pause(error, maxlen))
640 g_SourceHook.PausePlugin(pl->m_Id);
641 pl->m_Status = Pl_Paused;
650 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
657 if (pl->m_Status != Pl_Paused || !pl->m_API)
661 UTIL_Format(error, maxlen, "Plugin cannot be unpaused");
666 if (pl->m_API->Unpause(error, maxlen))
668 g_SourceHook.UnpausePlugin(pl->m_Id);
669 pl->m_Status = Pl_Running;
678 bool CPluginManager::UnloadAll()
685 while ((i = m_Plugins.begin()) != m_Plugins.end())
687 if ( !_Unload( (*i), true, error, sizeof(error)) )
696 bool CPluginManager::Query(PluginId id, const char **file, Pl_Status *status, PluginId *source)
698 CPlugin *pl = FindById(id);
707 *file = pl->m_File.c_str();
712 *status = pl->m_Status;
717 *source = pl->m_Source;
723 bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
725 CPlugin *pl = FindById(id);
727 if (!pl || !pl->m_API)
731 UTIL_Format(error, maxlength, "Plugin not valid");
736 return pl->m_API->QueryRunning(error, maxlength);
739 bool CPluginManager::QueryHandle(PluginId id, void **handle)
741 CPlugin *pl = FindById(id);
750 *handle = static_cast<void *>(pl->m_Lib);
756 PluginIter CPluginManager::_begin()
758 return m_Plugins.begin();
761 PluginIter CPluginManager::_end()
763 return m_Plugins.end();
766 void CPluginManager::AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
768 CPlugin *pl = FindByAPI(api);
775 pl->m_Cvars.push_back(pCvar);
778 void CPluginManager::AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
780 CPlugin *pl = FindByAPI(api);
787 pl->m_Cmds.push_back(pCmd);
790 void CPluginManager::RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar)
792 CPlugin *pl = FindByAPI(api);
799 pl->m_Cvars.remove(pCvar);
802 void CPluginManager::RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd)
804 CPlugin *pl = FindByAPI(api);
811 pl->m_Cmds.remove(pCmd);
814 void CPluginManager::UnregAllConCmds(CPlugin *pl)
816 SourceHook::List<ConCommandBase *>::iterator i;
819 for (i=pl->m_Cvars.begin(); i!=pl->m_Cvars.end(); i++)
821 g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );
825 for (i=pl->m_Cmds.begin(); i!=pl->m_Cmds.end(); i++)
827 g_Metamod.UnregisterConCommandBase(pl->m_Id, (*i) );