MyGUI_PluginManager.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_PluginManager.h"
#include "MyGUI_DynLibManager.h"
#include "MyGUI_ResourceManager.h"
namespace MyGUI
{
MYGUI_SINGLETON_DEFINITION(PluginManager);
using DLL_START_PLUGIN = void (*)();
using DLL_STOP_PLUGIN = void (*)();
PluginManager::PluginManager() :
mXmlPluginTagName("Plugin"),
mSingletonHolder(this)
{
}
void PluginManager::initialise()
{
MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
ResourceManager::getInstance().registerLoadXmlDelegate(mXmlPluginTagName) =
newDelegate(this, &PluginManager::_load);
MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
mIsInitialise = true;
}
void PluginManager::shutdown()
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
unloadAllPlugins();
ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlPluginTagName);
MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
mIsInitialise = false;
}
bool PluginManager::loadPlugin(std::string_view _file)
{
#ifdef EMSCRIPTEN
return false;
#endif
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
// Load plugin library
DynLib* lib = DynLibManager::getInstance().load(_file);
if (!lib)
{
MYGUI_LOG(Error, "Plugin '" << _file << "' not found");
return false;
}
// Call startup function
DLL_START_PLUGIN pFunc = reinterpret_cast<DLL_START_PLUGIN>(lib->getSymbol("dllStartPlugin"));
if (!pFunc)
{
MYGUI_LOG(Error, "Cannot find symbol 'dllStartPlugin' in library " << _file);
return false;
}
// Store for later unload
mLibs[lib->getName()] = lib;
// This must call installPlugin
pFunc();
return true;
}
void PluginManager::unloadPlugin(std::string_view _file)
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
DynLibList::iterator it = mLibs.find(_file);
if (it != mLibs.end())
{
// Call plugin shutdown
DLL_STOP_PLUGIN pFunc = reinterpret_cast<DLL_STOP_PLUGIN>((*it).second->getSymbol("dllStopPlugin"));
MYGUI_ASSERT(
nullptr != pFunc,
getClassTypeName() << "Cannot find symbol 'dllStopPlugin' in library " << _file);
// this must call uninstallPlugin
pFunc();
// Unload library (destroyed by DynLibManager)
DynLibManager::getInstance().unload((*it).second);
mLibs.erase(it);
}
}
void PluginManager::_load(xml::ElementPtr _node, std::string_view /*_file*/, Version _version)
{
xml::ElementEnumerator node = _node->getElementEnumerator();
while (node.next())
{
if (node->getName() == "path")
{
std::string source;
if (node->findAttribute("source", source))
loadPlugin(source);
}
else if (node->getName() == "Plugin")
{
std::string_view source;
xml::ElementEnumerator source_node = node->getElementEnumerator();
while (source_node.next("Source"))
{
std::string_view build = source_node->findAttribute("build");
#if MYGUI_DEBUG_MODE == 1
if (build == "Debug")
source = source_node->getContent();
#else
if (build != "Debug")
source = source_node->getContent();
#endif
}
if (!source.empty())
loadPlugin(source);
}
}
}
void PluginManager::installPlugin(IPlugin* _plugin)
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
MYGUI_LOG(Info, "Installing plugin: " << _plugin->getName());
mPlugins.insert(_plugin);
_plugin->install();
_plugin->initialize();
MYGUI_LOG(Info, "Plugin successfully installed");
}
void PluginManager::uninstallPlugin(IPlugin* _plugin)
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
MYGUI_LOG(Info, "Uninstalling plugin: " << _plugin->getName());
PluginList::iterator it = mPlugins.find(_plugin);
if (it != mPlugins.end())
{
_plugin->shutdown();
_plugin->uninstall();
mPlugins.erase(it);
}
MYGUI_LOG(Info, "Plugin successfully uninstalled");
}
void PluginManager::unloadAllPlugins()
{
while (!mLibs.empty())
unloadPlugin((*mLibs.begin()).first);
}
} // namespace MyGUI
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_DynLibManager.h
MyGUI_PluginManager.h
MyGUI_Precompiled.h
MyGUI_ResourceManager.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI::DynLib
Resource holding data about a dynamic library.
Definition MyGUI_DynLib.h:35
MyGUI::DynLib::getSymbol
void * getSymbol(const char *strName) const noexcept
Definition MyGUI_DynLib.cpp:85
MyGUI::DynLib::getName
const std::string & getName() const
Get the name of the library.
Definition MyGUI_DynLib.cpp:124
MyGUI::DynLibManager::unload
void unload(DynLib *library)
Unload library.
Definition MyGUI_DynLibManager.cpp:67
MyGUI::DynLibManager::load
DynLib * load(std::string_view fileName)
Load library.
Definition MyGUI_DynLibManager.cpp:47
MyGUI::DynLibManager::getInstance
static DynLibManager & getInstance()
MyGUI::IPlugin
Base plugin class.
Definition MyGUI_Plugin.h:19
MyGUI::IPlugin::install
virtual void install()=0
MyGUI::IPlugin::initialize
virtual void initialize()=0
MyGUI::IPlugin::getName
virtual const std::string & getName() const =0
MyGUI::IPlugin::shutdown
virtual void shutdown()=0
MyGUI::IPlugin::uninstall
virtual void uninstall()=0
MyGUI::PluginManager
Plugin manager. Load/unload and register plugins.
Definition MyGUI_PluginManager.h:25
MyGUI::PluginManager::installPlugin
void installPlugin(IPlugin *_plugin)
Definition MyGUI_PluginManager.cpp:136
MyGUI::PluginManager::unloadPlugin
void unloadPlugin(std::string_view _file)
Unload plugin.
Definition MyGUI_PluginManager.cpp:81
MyGUI::PluginManager::uninstallPlugin
void uninstallPlugin(IPlugin *_plugin)
Definition MyGUI_PluginManager.cpp:150
MyGUI::PluginManager::initialise
void initialise()
Definition MyGUI_PluginManager.cpp:25
MyGUI::PluginManager::PluginManager
PluginManager()
Definition MyGUI_PluginManager.cpp:19
MyGUI::PluginManager::unloadAllPlugins
void unloadAllPlugins()
Unload all plugins.
Definition MyGUI_PluginManager.cpp:165
MyGUI::PluginManager::loadPlugin
bool loadPlugin(std::string_view _file)
Load plugin.
Definition MyGUI_PluginManager.cpp:49
MyGUI::PluginManager::shutdown
void shutdown()
Definition MyGUI_PluginManager.cpp:37
MyGUI::PluginManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:127
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:120
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::Element::findAttribute
bool findAttribute(std::string_view _name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:242
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:328
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::DLL_STOP_PLUGIN
void(*)() DLL_STOP_PLUGIN
Definition MyGUI_PluginManager.cpp:17
MyGUI::DLL_START_PLUGIN
void(*)() DLL_START_PLUGIN
Definition MyGUI_PluginManager.cpp:16
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100