MyGUI  3.0.1
MyGUI_ResourceManager.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_ResourceManager.h"
25 #include "MyGUI_XmlDocument.h"
26 #include "MyGUI_IResource.h"
27 #include "MyGUI_DataManager.h"
28 #include "MyGUI_FactoryManager.h"
29 
30 #include "MyGUI_ResourceImageSet.h"
31 
32 namespace MyGUI
33 {
34 
35  const std::string XML_TYPE("Resource");
36  const std::string XML_TYPE_LIST("List");
37 
38  MYGUI_INSTANCE_IMPLEMENT( ResourceManager )
39 
40  void ResourceManager::initialise()
41  {
42  MYGUI_ASSERT(!mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
43  MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
44 
45  registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &ResourceManager::_load);
46  registerLoadXmlDelegate(XML_TYPE_LIST) = newDelegate(this, &ResourceManager::_loadList);
47 
48  // регестрируем дефолтные ресурсы
50 
51  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
52  mIsInitialise = true;
53  }
54 
56  {
57  if (!mIsInitialise) return;
58  MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
59 
61 
62  clear();
65 
66  mMapLoadXmlDelegate.clear();
67 
68  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
69  mIsInitialise = false;
70  }
71 
72  bool ResourceManager::load(const std::string& _file)
73  {
74  return _loadImplement(_file, false, "", INSTANCE_TYPE_NAME);
75  }
76 
77  void ResourceManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
78  {
80 
81  VectorGuid vector_guid;
82  // берем детей и крутимся, основной цикл
84  while (root.next(XML_TYPE))
85  {
86  // парсим атрибуты
87  std::string id, type, name;
88  root->findAttribute("type", type);
89  root->findAttribute("name", name);
90  root->findAttribute("id", id);
91 
92  Guid guid(id);
93  if (!guid.empty())
94  {
95  if (mResourcesID.find(guid) != mResourcesID.end())
96  {
97  MYGUI_LOG(Warning, "dublicate resource id " << guid.print());
98  }
99  }
100 
101  if (mResources.find(name) != mResources.end())
102  {
103  MYGUI_LOG(Warning, "dublicate resource name '" << name << "'");
104  }
105 
106  vector_guid.push_back(guid);
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  if (!guid.empty()) mResourcesID[guid] = resource;
119  if (!name.empty()) mResources[name] = resource;
120  }
121 
122  if (!vector_guid.empty())
123  {
124  mListFileGuid[_file] = vector_guid;
125  }
126 
127  }
128 
129  std::string ResourceManager::getFileNameByID(const Guid& _id)
130  {
131  for (MapVectorString::iterator item=mListFileGuid.begin(); item!=mListFileGuid.end(); ++item)
132  {
133  for (VectorGuid::iterator item2=item->second.begin(); item2!=item->second.end(); ++item2)
134  {
135  if (*item2 == _id)
136  {
137  return item->first;
138  }
139  }
140  }
141  return "";
142  }
143 
144  void ResourceManager::_loadList(xml::ElementPtr _node, const std::string& _file, Version _version)
145  {
146  // берем детей и крутимся, основной цикл
148  while (node.next(XML_TYPE_LIST))
149  {
150  std::string source;
151  if (!node->findAttribute("file", source)) continue;
152  MYGUI_LOG(Info, "Load ini file '" << source << "'");
153  _loadImplement(source, false, "", INSTANCE_TYPE_NAME);
154  }
155  }
156 
158  {
159  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
160  MYGUI_ASSERT(iter == mMapLoadXmlDelegate.end(), "name delegate is exist");
161  return (mMapLoadXmlDelegate[_key] = LoadXmlDelegate());
162  }
163 
164  void ResourceManager::unregisterLoadXmlDelegate(const std::string& _key)
165  {
166  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
167  if (iter != mMapLoadXmlDelegate.end()) mMapLoadXmlDelegate.erase(iter);
168  }
169 
170  bool ResourceManager::_loadImplement(const std::string& _file, bool _match, const std::string& _type, const std::string& _instance)
171  {
172  IDataStream* data = DataManager::getInstance().getData(_file);
173  if (data == nullptr)
174  {
175  MYGUI_LOG(Error, _instance << " : '" << _file << "', not found");
176  return false;
177  }
178 
179  xml::Document doc;
180  if (!doc.open(data))
181  {
182  MYGUI_LOG(Error, _instance << " : '" << _file << "', " << doc.getLastError());
183 
184  // FIXME
185  delete data;
186 
187  return false;
188  }
189 
190  // FIXME
191  delete data;
192 
193  xml::ElementPtr root = doc.getRoot();
194  if ( (nullptr == root) || (root->getName() != "MyGUI") )
195  {
196  MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'MyGUI' not found");
197  return false;
198  }
199 
200  std::string type;
201  if (root->findAttribute("type", type))
202  {
203  Version version = Version::parse(root->findAttribute("version"));
204  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
205  if (iter != mMapLoadXmlDelegate.end())
206  {
207  if ((!_match) || (type == _type)) (*iter).second(root, _file, version);
208  else
209  {
210  MYGUI_LOG(Error, _instance << " : '" << _file << "', type '" << _type << "' not found");
211  return false;
212  }
213  }
214  else
215  {
216  MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
217  return false;
218  }
219  }
220  // предпологаем что будут вложенные
221  else if (!_match)
222  {
224  while (node.next("MyGUI"))
225  {
226  if (node->findAttribute("type", type))
227  {
228  Version version = Version::parse(root->findAttribute("version"));
229  MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
230  if (iter != mMapLoadXmlDelegate.end())
231  {
232  (*iter).second(node.current(), _file, version);
233  }
234  else
235  {
236  MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
237  }
238  }
239  else
240  {
241  MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'type' not found");
242  }
243  }
244  }
245 
246  return true;
247  }
248 
249  IResourcePtr ResourceManager::getByID(const Guid& _id, bool _throw)
250  {
251  MapResourceID::iterator iter = mResourcesID.find(_id);
252  if (iter == mResourcesID.end())
253  {
254  if (_throw) MYGUI_EXCEPT("resource '" << _id.print() << "' not found");
255  MYGUI_LOG(Warning, "resource '" << _id.print() << "' not found");
256  return nullptr;
257  }
258  return iter->second;
259  }
260 
262  {
263  if (!_item->getResourceName().empty())
264  mResources[_item->getResourceName()] = _item;
265  if (!_item->getResourceID().empty())
266  mResourcesID[_item->getResourceID()] = _item;
267  }
268 
270  {
271  if (_item == nullptr) return;
272 
273  if (!_item->getResourceName().empty())
274  {
275  MapResource::iterator item = mResources.find(_item->getResourceName());
276  if (item != mResources.end())
277  mResources.erase(item);
278  }
279 
280  if (!_item->getResourceID().empty())
281  {
282  MapResourceID::iterator id = mResourcesID.find(_item->getResourceID());
283  if (id != mResourcesID.end())
284  mResourcesID.erase(id);
285  }
286  }
287 
288 } // namespace MyGUI