1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
16 * @brief Implements functions from CPlugin.h
20 using namespace SourceMM;
22 CPluginManager g_PluginMngr;
24 CPluginManager::CPluginManager()
30 CPluginManager::~CPluginManager()
39 CPluginManager::CPlugin::CPlugin() : m_Lib(NULL), m_API(NULL), m_Id(0), m_Source(0)
41 memset(&fac_list, 0, sizeof(factories));
44 PluginId CPluginManager::Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen)
49 //Check if we're about to reload an old plugin
50 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
52 if ( (*i) && (*i)->m_File.compare(file)==0 )
54 if ( (*i)->m_Status < Pl_Paused )
56 //Attempt to load the plugin again
58 i = m_Plugins.erase(i);
67 CPlugin *pl = _Load(file, source, error, maxlen);
75 CPluginManager::CPlugin *CPluginManager::FindById(PluginId id)
79 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
81 if ( (*i)->m_Id == id )
88 void CPluginManager::SetAllLoaded()
93 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
95 if ( (*i) && (*i)->m_Status == Pl_Running && (*i)->m_API )
97 //004 is when we added this callback
98 if ( (*i)->m_API->GetApiVersion() >= 004 )
100 (*i)->m_API->AllPluginsLoaded();
106 bool CPluginManager::Pause(PluginId id, char *error, size_t maxlen)
108 CPlugin *pl = FindById(id);
112 snprintf(error, maxlen, "Plugin id not found");
116 return _Pause(pl, error, maxlen);
119 bool CPluginManager::Unpause(PluginId id, char *error, size_t maxlen)
121 CPlugin *pl = FindById(id);
125 snprintf(error, maxlen, "Plugin id not found");
129 return _Unpause(pl, error, maxlen);
132 bool CPluginManager::Unload(PluginId id, bool force, char *error, size_t maxlen)
134 CPlugin *pl = FindById(id);
138 snprintf(error, maxlen, "Plugin %d not found", id);
142 return _Unload(pl, force, error, maxlen);
145 bool CPluginManager::Retry(PluginId id, char *error, size_t len)
149 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
151 if ( (*i) && (*i)->m_Id == id )
153 if ( (*i)->m_Status >= Pl_Paused)
155 snprintf(error, len, "Plugin %d is already running.", id);
158 CPlugin *pl = _Load((*i)->m_File.c_str(), Pl_Console, error, len);
161 if (pl->m_Status >= Pl_Paused)
163 //Now it gets crazy... unload the original copy.
164 _Unload( (*i), true, buffer, sizeof(buffer)-1 );
166 //Set the new copy's id
169 //We just wasted an id... reclaim it
174 //don't really care about the buffer here
175 _Unload(pl, true, buffer, sizeof(buffer)-1);
177 //We just wasted an id... reclaim it
184 snprintf(error, len, "Plugin %d not found,", id);
188 CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
198 pl->m_File.assign(file);
199 m_Plugins.push_back(pl);
202 //Check if the file even exists
203 fp = fopen(file, "r");
207 snprintf(error, maxlen, "File not found: %s", file);
208 pl->m_Status = Pl_NotFound;
214 pl->m_Lib = dlmount(file);
218 snprintf(error, maxlen, "%s", dlerror());
219 pl->m_Status = Pl_Error;
221 CreateInterfaceFn pfn = reinterpret_cast<CreateInterfaceFn>(dlsym(pl->m_Lib, PL_EXPOSURE_C));
225 snprintf(error, maxlen, "Function %s not found", PL_EXPOSURE_C);
226 pl->m_Status = Pl_Error;
228 pl->m_API = static_cast<ISmmPlugin *>((pfn)(PLAPI_NAME, NULL));
232 snprintf(error, maxlen, "Failed to get API");
233 pl->m_Status = Pl_Error;
235 if (pl->m_API->GetApiVersion() < PLAPI_MIN_VERSION)
238 snprintf(error, maxlen, "Plugin API %d is out of date with required minimum (%d)", pl->m_API->GetApiVersion(), PLAPI_MIN_VERSION);
239 pl->m_Status = Pl_Error;
241 if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), &(pl->fac_list), error, maxlen))
243 pl->m_Status = Pl_Running;
246 //API 004 is when we added this callback
247 if (pl->m_API->GetApiVersion() >= 4)
248 pl->m_API->AllPluginsLoaded();
251 pl->m_Status = Pl_Refused;
259 if (pl->m_Lib && (pl->m_Status < Pl_Paused))
269 bool CPluginManager::_Unload(CPluginManager::CPlugin *pl, bool force, char *error, size_t maxlen)
273 if (pl->m_API && pl->m_Lib)
275 //Note, we'll always tell the plugin it will be unloading...
276 if (pl->m_API->Unload(error, maxlen) || force)
278 //Make sure to detach it from sourcehook!
279 g_SourceHook.UnloadPlugin(pl->m_Id);
286 //Remove the plugin from the list
288 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
290 if ( (*i)->m_Id == pl->m_Id )
292 i = m_Plugins.erase(i);
302 //The plugin is not valid, and let's just remove it from the list anyway
304 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
306 if ( (*i)->m_Id == pl->m_Id )
308 i = m_Plugins.erase(i);
320 bool CPluginManager::_Pause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
324 if (pl->m_Status != Pl_Running || !pl->m_API)
327 snprintf(error, maxlen, "Plugin cannot be paused");
329 if (pl->m_API->Pause(error, maxlen))
331 g_SourceHook.PausePlugin(pl->m_Id);
332 pl->m_Status = Pl_Paused;
341 bool CPluginManager::_Unpause(CPluginManager::CPlugin *pl, char *error, size_t maxlen)
346 if (pl->m_Status != Pl_Paused || !pl->m_API)
349 snprintf(error, maxlen, "Plugin cannot be unpaused");
351 if (pl->m_API->Unpause(error, maxlen))
353 g_SourceHook.UnpausePlugin(pl->m_Id);
354 pl->m_Status = Pl_Running;
363 bool CPluginManager::UnloadAll()
368 for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
374 if ( (*i)->m_API->Unload(NULL, 0) )
377 //Unlink from SourceHook
378 g_SourceHook.UnloadPlugin( (*i)->m_Id );
381 dlclose( (*i)->m_Lib );
386 i = m_Plugins.erase(i);
392 bool CPluginManager::Query(PluginId id, const char *&file, factories *&list, Pl_Status &status, PluginId &source)
394 CPlugin *pl = FindById(id);
399 file = pl->m_File.c_str();
400 list = &(pl->fac_list);
401 status = pl->m_Status;
402 source = pl->m_Source;
407 PluginIter CPluginManager::_begin()
409 return m_Plugins.begin();
412 PluginIter CPluginManager::_end()
414 return m_Plugins.end();