MyGUI: MyGUI_PluginManager.cpp Source File

MyGUI 3.4.3

MyGUI_PluginManager.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_PluginManager.h"
9#include "MyGUI_DynLibManager.h"
10#include "MyGUI_ResourceManager.h"
11
12namespace MyGUI
13{
14 MYGUI_SINGLETON_DEFINITION(PluginManager);
15
16 using DLL_START_PLUGIN = void (*)();
17 using DLL_STOP_PLUGIN = void (*)();
18
19 PluginManager::PluginManager() :
20 mXmlPluginTagName("Plugin"),
21 mSingletonHolder(this)
22 {
23 }
24
25 void PluginManager::initialise()
26 {
27 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
28 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
29
30 ResourceManager::getInstance().registerLoadXmlDelegate(mXmlPluginTagName) =
31 newDelegate(this, &PluginManager::_load);
32
33 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
34 mIsInitialise = true;
35 }
36
37 void PluginManager::shutdown()
38 {
39 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
40 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
41
42 unloadAllPlugins();
43 ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlPluginTagName);
44
45 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
46 mIsInitialise = false;
47 }
48
49 bool PluginManager::loadPlugin(std::string_view _file)
50 {
51#ifdef EMSCRIPTEN
52 return false;
53#endif
54 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
55
56 // Load plugin library
57 DynLib* lib = DynLibManager::getInstance().load(_file);
58 if (!lib)
59 {
60 MYGUI_LOG(Error, "Plugin '" << _file << "' not found");
61 return false;
62 }
63
64 // Call startup function
65 DLL_START_PLUGIN pFunc = reinterpret_cast<DLL_START_PLUGIN>(lib->getSymbol("dllStartPlugin"));
66 if (!pFunc)
67 {
68 MYGUI_LOG(Error, "Cannot find symbol 'dllStartPlugin' in library " << _file);
69 return false;
70 }
71
72 // Store for later unload
73 mLibs[lib->getName()] = lib;
74
75 // This must call installPlugin
76 pFunc();
77
78 return true;
79 }
80
81 void PluginManager::unloadPlugin(std::string_view _file)
82 {
83 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
84
85 DynLibList::iterator it = mLibs.find(_file);
86 if (it != mLibs.end())
87 {
88 // Call plugin shutdown
89 DLL_STOP_PLUGIN pFunc = reinterpret_cast<DLL_STOP_PLUGIN>((*it).second->getSymbol("dllStopPlugin"));
90
91 MYGUI_ASSERT(
92 nullptr != pFunc,
93 getClassTypeName() << "Cannot find symbol 'dllStopPlugin' in library " << _file);
94
95 // this must call uninstallPlugin
96 pFunc();
97 // Unload library (destroyed by DynLibManager)
98 DynLibManager::getInstance().unload((*it).second);
99 mLibs.erase(it);
100 }
101 }
102
103 void PluginManager::_load(xml::ElementPtr _node, std::string_view /*_file*/, Version _version)
104 {
105 xml::ElementEnumerator node = _node->getElementEnumerator();
106 while (node.next())
107 {
108 if (node->getName() == "path")
109 {
110 std::string source;
111 if (node->findAttribute("source", source))
112 loadPlugin(source);
113 }
114 else if (node->getName() == "Plugin")
115 {
116 std::string_view source;
117
118 xml::ElementEnumerator source_node = node->getElementEnumerator();
119 while (source_node.next("Source"))
120 {
121 std::string_view build = source_node->findAttribute("build");
122#if MYGUI_DEBUG_MODE == 1
123 if (build == "Debug")
124 source = source_node->getContent();
125#else
126 if (build != "Debug")
127 source = source_node->getContent();
128#endif
129 }
130 if (!source.empty())
131 loadPlugin(source);
132 }
133 }
134 }
135
136 void PluginManager::installPlugin(IPlugin* _plugin)
137 {
138 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
139
140 MYGUI_LOG(Info, "Installing plugin: " << _plugin->getName());
141
142 mPlugins.insert(_plugin);
143 _plugin->install();
144
145 _plugin->initialize();
146
147 MYGUI_LOG(Info, "Plugin successfully installed");
148 }
149
150 void PluginManager::uninstallPlugin(IPlugin* _plugin)
151 {
152 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
153
154 MYGUI_LOG(Info, "Uninstalling plugin: " << _plugin->getName());
155 PluginList::iterator it = mPlugins.find(_plugin);
156 if (it != mPlugins.end())
157 {
158 _plugin->shutdown();
159 _plugin->uninstall();
160 mPlugins.erase(it);
161 }
162 MYGUI_LOG(Info, "Plugin successfully uninstalled");
163 }
164
165 void PluginManager::unloadAllPlugins()
166 {
167 while (!mLibs.empty())
168 unloadPlugin((*mLibs.begin()).first);
169 }
170
171} // 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