MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_PluginManager.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_PluginManager.h"
24 #include "MyGUI_DynLibManager.h"
25 #include "MyGUI_ResourceManager.h"
26
27 namespace MyGUI
28 {
29 typedef void (*DLL_START_PLUGIN)(void);
30 typedef void (*DLL_STOP_PLUGIN)(void);
31
32 const std::string XML_TYPE("Plugin");
33
34 template <> PluginManager* Singleton<PluginManager>::msInstance = nullptr;
35 template <> const char* Singleton<PluginManager>::mClassTypeName("PluginManager");
36
37 PluginManager::PluginManager() :
38 mIsInitialise(false)
39 {
40 }
41
42 void PluginManager::initialise()
43 {
44 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
45 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
46
47 ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &PluginManager::_load);
48
49 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
50 mIsInitialise = true;
51 }
52
53 void PluginManager::shutdown()
54 {
55 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
56 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
57
58 unloadAllPlugins();
59 ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
60
61 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
62 mIsInitialise = false;
63 }
64
65 bool PluginManager::loadPlugin(const std::string& _file)
66 {
67 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
68
69 // Load plugin library
70 DynLib* lib = DynLibManager::getInstance().load(_file);
71 if (!lib)
72 {
73 MYGUI_LOG(Error, "Plugin '" << _file << "' not found");
74 return false;
75 }
76
77 // Call startup function
78 DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->getSymbol("dllStartPlugin");
79 if (!pFunc)
80 {
81 MYGUI_LOG(Error, "Cannot find symbol 'dllStartPlugin' in library " << _file);
82 return false;
83 }
84
85 // Store for later unload
86 mLibs[_file] = lib;
87
88 // This must call installPlugin
89 pFunc();
90
91 return true;
92 }
93
94 void PluginManager::unloadPlugin(const std::string& _file)
95 {
96 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
97
98 DynLibList::iterator it = mLibs.find(_file);
99 if (it != mLibs.end())
100 {
101 // Call plugin shutdown
102 DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)(*it).second->getSymbol("dllStopPlugin");
103
104 MYGUI_ASSERT(nullptr != pFunc, getClassTypeName() << "Cannot find symbol 'dllStopPlugin' in library " << _file);
105
106 // this must call uninstallPlugin
107 pFunc();
108 // Unload library (destroyed by DynLibManager)
109 DynLibManager::getInstance().unload((*it).second);
110 mLibs.erase(it);
111 }
112 }
113
114 void PluginManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
115 {
116 xml::ElementEnumerator node = _node->getElementEnumerator();
117 while (node.next())
118 {
119 if (node->getName() == "path")
120 {
121 std::string source;
122 if (node->findAttribute("source", source))
123 loadPlugin(source);
124 }
125 else if (node->getName() == "Plugin")
126 {
127 std::string source;
128
129 xml::ElementEnumerator source_node = node->getElementEnumerator();
130 while (source_node.next("Source"))
131 {
132 std::string build = source_node->findAttribute("build");
133 #if MYGUI_DEBUG_MODE == 1
134 if (build == "Debug")
135 source = source_node->getContent();
136 #else
137 if (build != "Debug")
138 source = source_node->getContent();
139 #endif
140 }
141 if (!source.empty())
142 loadPlugin(source);
143 }
144 }
145 }
146
147 void PluginManager::installPlugin(IPlugin* _plugin)
148 {
149 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
150
151 MYGUI_LOG(Info, "Installing plugin: " << _plugin->getName());
152
153 mPlugins.insert(_plugin);
154 _plugin->install();
155
156 _plugin->initialize();
157
158 MYGUI_LOG(Info, "Plugin successfully installed");
159 }
160
161 void PluginManager::uninstallPlugin(IPlugin* _plugin)
162 {
163 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " used but not initialised");
164
165 MYGUI_LOG(Info, "Uninstalling plugin: " << _plugin->getName());
166 PluginList::iterator it = mPlugins.find(_plugin);
167 if (it != mPlugins.end())
168 {
169 _plugin->shutdown();
170 _plugin->uninstall();
171 mPlugins.erase(it);
172 }
173 MYGUI_LOG(Info, "Plugin successfully uninstalled");
174 }
175
176 void PluginManager::unloadAllPlugins()
177 {
178 while (!mLibs.empty())
179 unloadPlugin((*mLibs.begin()).first);
180 }
181
182 } // namespace MyGUI
MyGUI::IPlugin
Base plugin class.
Definition: MyGUI_Plugin.h:33
MyGUI::PluginManager::shutdown
void shutdown()
Definition: MyGUI_PluginManager.cpp:53
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:142
MyGUI::DynLibManager::unload
void unload(DynLib *library)
Unload library.
Definition: MyGUI_DynLibManager.cpp:83
MyGUI_Precompiled.h
MyGUI::IPlugin::shutdown
virtual void shutdown()=0
MyGUI::PluginManager::uninstallPlugin
void uninstallPlugin(IPlugin *_plugin)
Definition: MyGUI_PluginManager.cpp:161
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:135
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:150
MyGUI::Singleton< ResourceManager >::getInstance
static ResourceManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::DLL_STOP_PLUGIN
void(* DLL_STOP_PLUGIN)(void)
Definition: MyGUI_PluginManager.cpp:30
MyGUI::DynLib
Resource holding data about a dynamic library.
Definition: MyGUI_DynLib.h:63
MyGUI::IPlugin::getName
virtual const std::string & getName() const =0
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:261
MyGUI::Singleton< PluginManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI::XML_TYPE
const std::string XML_TYPE("Font")
MyGUI::PluginManager::unloadPlugin
void unloadPlugin(const std::string &_file)
Unload plugin.
Definition: MyGUI_PluginManager.cpp:94
MyGUI::PluginManager
Plugin manager. Load/unload and register plugins.
Definition: MyGUI_PluginManager.h:39
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:124
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI::PluginManager::initialise
void initialise()
Definition: MyGUI_PluginManager.cpp:42
MyGUI::PluginManager::PluginManager
PluginManager()
Definition: MyGUI_PluginManager.cpp:37
MyGUI::PluginManager::unloadAllPlugins
void unloadAllPlugins()
Unload all plugins.
Definition: MyGUI_PluginManager.cpp:176
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:115
MyGUI::IPlugin::initialize
virtual void initialize()=0
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:168
MyGUI::Version
Definition: MyGUI_Version.h:32
MyGUI_ResourceManager.h
MyGUI_PluginManager.h
MyGUI::DynLibManager::load
DynLib * load(const std::string &fileName)
Load library.
Definition: MyGUI_DynLibManager.cpp:63
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:367
MyGUI::DLL_START_PLUGIN
void(* DLL_START_PLUGIN)(void)
Definition: MyGUI_PluginManager.cpp:29
MyGUI::xml::Element::getName
const std::string & getName() const
Definition: MyGUI_XmlDocument.cpp:347
MyGUI::PluginManager::loadPlugin
bool loadPlugin(const std::string &_file)
Load plugin.
Definition: MyGUI_PluginManager.cpp:65
MyGUI::Singleton
Definition: MyGUI_Singleton.h:35
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI::PluginManager::installPlugin
void installPlugin(IPlugin *_plugin)
Definition: MyGUI_PluginManager.cpp:147
MyGUI::IPlugin::uninstall
virtual void uninstall()=0
MyGUI::IPlugin::install
virtual void install()=0
MyGUI::DynLib::getSymbol
void * getSymbol(const std::string &strName) const
Definition: MyGUI_DynLib.cpp:75
MyGUI_DynLibManager.h