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