MyGUI  3.2.0
MyGUI_ResourceManager.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_ResourceManager.h"
24 #include "MyGUI_XmlDocument.h"
25 #include "MyGUI_IResource.h"
26 #include "MyGUI_DataManager.h"
27 #include "MyGUI_FactoryManager.h"
28 
29 #include "MyGUI_ResourceImageSet.h"
30 
31 namespace MyGUI
32 {
33 
34  const std::string XML_TYPE("Resource");
35  const std::string XML_TYPE_LIST("List");
36 
37  template <> ResourceManager* Singleton<ResourceManager>::msInstance = nullptr;
38  template <> const char* Singleton<ResourceManager>::mClassTypeName("ResourceManager");
39 
41  mIsInitialise(false)
42  {
43  }
44 
46  {
47  MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
48  MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
49 
51  registerLoadXmlDelegate(XML_TYPE_LIST) = newDelegate(this, &ResourceManager::_loadList);
52 
53  // регестрируем дефолтные ресурсы
55 
56  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
57  mIsInitialise = true;
58  }
59 
61  {
62  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
63  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
64 
66 
67  clear();
70 
71  mMapLoadXmlDelegate.clear();
72 
73  MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
74  mIsInitialise = false;
75  }
76 
77  bool ResourceManager::load(const std::string& _file)
78  {
79  return _loadImplement(_file, false, "", getClassTypeName());
80  }
81 
82  void ResourceManager::loadFromXmlNode(xml::ElementPtr _node, const std::string& _file, Version _version)
83  {
85 
86  // берем детей и крутимся, основной цикл
88  while (root.next(XML_TYPE))
89  {
90  // парсим атрибуты
91  std::string type, name;
92  root->findAttribute("type", type);
93  root->findAttribute("name", name);
94 
95  if (name.empty())
96  continue;
97 
98  MapResource::iterator item = mResources.find(name);
99  if (item != mResources.end())
100  {
101  MYGUI_LOG(Warning, "dublicate resource name '" << name << "'");
102 
103  // ресурсами могут пользоваться
104  mRemovedResoures.push_back((*item).second);
105  mResources.erase(item);
106  }
107 
108  IObject* object = factory.createObject(XML_TYPE, type);
109  if (object == nullptr)
110  {
111  MYGUI_LOG(Error, "resource type '" << type << "' not found");
112  continue;
113  }
114 
115  IResourcePtr resource = object->castType<IResource>();
116  resource->deserialization(root.current(), _version);
117 
118  mResources[name] = resource;
119  }
120  }
121 
122  void ResourceManager::_loadList(xml::ElementPtr _node, const std::string& _file, Version _version)
123  {
124  // берем детей и крутимся, основной цикл
126  while (node.next(XML_TYPE_LIST))
127  {
128  std::string source;
129  if (!node->findAttribute("file", source)) continue;
130  MYGUI_LOG(Info, "Load ini file '" << source << "'");
131  _loadImplement(source, false, "", getClassTypeName());
132  }
133  }
134 
136  {
137  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
138  MYGUI_ASSERT(iter == mMapLoadXmlDelegate.end(), "name delegate is exist");
139  return (mMapLoadXmlDelegate[_key] = LoadXmlDelegate());
140  }
141 
142  void ResourceManager::unregisterLoadXmlDelegate(const std::string& _key)
143  {
144  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
145  if (iter != mMapLoadXmlDelegate.end()) mMapLoadXmlDelegate.erase(iter);
146  }
147 
148  bool ResourceManager::_loadImplement(const std::string& _file, bool _match, const std::string& _type, const std::string& _instance)
149  {
151  if (data.getData() == nullptr)
152  {
153  MYGUI_LOG(Error, _instance << " : '" << _file << "', not found");
154  return false;
155  }
156 
157  xml::Document doc;
158  if (!doc.open(data.getData()))
159  {
160  MYGUI_LOG(Error, _instance << " : '" << _file << "', " << doc.getLastError());
161  return false;
162  }
163 
164  xml::ElementPtr root = doc.getRoot();
165  if ( (nullptr == root) || (root->getName() != "MyGUI") )
166  {
167  MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'MyGUI' not found");
168  return false;
169  }
170 
171  std::string type;
172  if (root->findAttribute("type", type))
173  {
174  Version version = Version::parse(root->findAttribute("version"));
175  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
176  if (iter != mMapLoadXmlDelegate.end())
177  {
178  if ((!_match) || (type == _type))
179  (*iter).second(root, _file, version);
180  else
181  {
182  MYGUI_LOG(Error, _instance << " : '" << _file << "', type '" << _type << "' not found");
183  return false;
184  }
185  }
186  else
187  {
188  MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
189  return false;
190  }
191  }
192  // предпологаем что будут вложенные
193  else if (!_match)
194  {
195  xml::ElementEnumerator node = root->getElementEnumerator();
196  while (node.next("MyGUI"))
197  {
198  if (node->findAttribute("type", type))
199  {
200  Version version = Version::parse(root->findAttribute("version"));
201  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
202  if (iter != mMapLoadXmlDelegate.end())
203  {
204  (*iter).second(node.current(), _file, version);
205  }
206  else
207  {
208  MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
209  }
210  }
211  else
212  {
213  MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'type' not found");
214  }
215  }
216  }
217 
218  return true;
219  }
220 
222  {
223  if (!_item->getResourceName().empty())
224  mResources[_item->getResourceName()] = _item;
225  }
226 
228  {
229  if (_item == nullptr)
230  return;
231 
232  if (!_item->getResourceName().empty())
233  {
234  MapResource::iterator item = mResources.find(_item->getResourceName());
235  if (item != mResources.end())
236  mResources.erase(item);
237  }
238  }
239 
240  bool ResourceManager::isExist(const std::string& _name) const
241  {
242  return mResources.find(_name) != mResources.end();
243  }
244 
245  IResource* ResourceManager::findByName(const std::string& _name) const
246  {
247  MapResource::const_iterator item = mResources.find(_name);
248  return (item == mResources.end()) ? nullptr : item->second;
249  }
250 
251  IResource* ResourceManager::getByName(const std::string& _name, bool _throw) const
252  {
253  IResource* result = findByName(_name);
254  MYGUI_ASSERT(result || !_throw, "Resource '" << _name << "' not found");
255  return result;
256  }
257 
258  bool ResourceManager::removeByName(const std::string& _name)
259  {
260  MapResource::const_iterator item = mResources.find(_name);
261  if (item != mResources.end())
262  {
263  delete item->second;
264  mResources.erase(item->first);
265  return true;
266  }
267  return false;
268  }
269 
271  {
272  for (MapResource::iterator item = mResources.begin(); item != mResources.end(); ++ item)
273  delete item->second;
274  mResources.clear();
275 
276  for (VectorResource::iterator item = mRemovedResoures.begin(); item != mRemovedResoures.end(); ++ item)
277  delete (*item);
278  mRemovedResoures.clear();
279  }
280 
282  {
283  return EnumeratorPtr(mResources);
284  }
285 
287  {
288  return mResources.size();
289  }
290 
291 } // namespace MyGUI