MyGUI 3.5.3
MyGUI_ResourceManager.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"
9#include "MyGUI_XmlDocument.h"
10#include "MyGUI_IResource.h"
11#include "MyGUI_DataManager.h"
15
16#include <pugixml.hpp>
17
18namespace MyGUI
19{
20
22
23 ResourceManager::ResourceManager() :
24 mSingletonHolder(this),
25 mCategoryName("Resource"),
26 mXmlListTagName("List")
27 {
28 }
29
30 void ResourceManager::initialise()
31 {
32 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34
35 registerLoadXmlDelegate(mCategoryName) = newDelegate(this, &ResourceManager::loadFromXmlNode);
36 registerLoadXmlDelegate(mXmlListTagName) = newDelegate(this, &ResourceManager::_loadList);
37
38 FactoryManager::getInstance().registerFactory<ResourceImageSet>(mCategoryName);
39
40 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
41 mIsInitialise = true;
42 }
43
44 void ResourceManager::shutdown()
45 {
46 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
47 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
48
49 FactoryManager::getInstance().unregisterFactory<ResourceImageSet>(mCategoryName);
50
51 clear();
52 unregisterLoadXmlDelegate(mCategoryName);
53 unregisterLoadXmlDelegate(mXmlListTagName);
54
55 mMapLoadXmlDelegate.clear();
56
57 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
58 mIsInitialise = false;
59 }
60
61 bool ResourceManager::load(const std::string& _file)
62 {
63 return _loadImplement(_file, false, {}, getClassTypeName());
64 }
65
66 void ResourceManager::loadFromXmlNode(xml::ElementPtr _node, std::string_view /*unused*/, Version _version)
67 {
68 FactoryManager& factory = FactoryManager::getInstance();
69
70 for (auto root : _node.node().children(mCategoryName.c_str()))
71 {
72 std::string_view type = root.attribute("type").value();
73 std::string name{root.attribute("name").value()};
74
75 if (name.empty())
76 continue;
77
78 IObject* object = factory.createObject(mCategoryName, type);
79 if (object == nullptr)
80 {
81 MYGUI_LOG(Error, "resource type '" << type << "' not found");
82 continue;
83 }
84
85 MapResource::iterator item = mResources.find(name);
86 if (item != mResources.end())
87 {
88 MYGUI_LOG(Warning, "duplicate resource name '" << name << "'");
89
90 // resources may be in use, keep for later cleanup
91 mRemovedResources.push_back((*item).second);
92 mResources.erase(item);
93 }
94
95 IResourcePtr resource = object->castType<IResource>();
96 resource->deserialization(root, _version);
97
98 mResources[name] = resource;
99 }
100 }
101
102 void ResourceManager::_loadList(xml::ElementPtr _node, std::string_view /*unused*/, Version _version)
103 {
104 for (auto child : _node.node().children(mXmlListTagName.c_str()))
105 {
106 std::string source{child.attribute("file").value()};
107 if (source.empty())
108 continue;
109 MYGUI_LOG(Info, "Load ini file '" << source << "'");
110 _loadImplement(source, false, {}, getClassTypeName());
111 }
112 }
113
114 ResourceManager::LoadXmlDelegate& ResourceManager::registerLoadXmlDelegate(std::string_view _key)
115 {
116 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
117 MYGUI_ASSERT(iter == mMapLoadXmlDelegate.end(), "name delegate is exist");
118 return mMapLoadXmlDelegate.emplace(_key, LoadXmlDelegate()).first->second;
119 }
120
121 void ResourceManager::unregisterLoadXmlDelegate(std::string_view _key)
122 {
123 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
124 if (iter != mMapLoadXmlDelegate.end())
125 mMapLoadXmlDelegate.erase(iter);
126 }
127
128 bool ResourceManager::_loadImplement(
129 const std::string& _file,
130 bool _match,
131 std::string_view _type,
132 std::string_view _instance)
133 {
134 auto data = DataManager::getInstance().getDataHolder(_file);
135 if (!data)
136 {
137 MYGUI_LOG(Error, _instance << " : '" << _file << "', not found");
138 return false;
139 }
140
141 xml::Document doc;
142 if (!doc.open(data.getData()))
143 {
144 MYGUI_LOG(Error, _instance << " : '" << _file << "', " << doc.getLastError());
145 return false;
146 }
147
148 pugi::xml_node root = doc.getRoot();
149 if ((!root) || (root.name() != std::string_view("MyGUI")))
150 {
151 MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'MyGUI' not found");
152 return false;
153 }
154
155 std::string_view type = root.attribute("type").value();
156 if (!type.empty())
157 {
158 Version version = Version::parse(root.attribute("version").value());
159 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
160 if (iter != mMapLoadXmlDelegate.end())
161 {
162 if ((!_match) || (type == _type))
163 (*iter).second(root, _file, version);
164 else
165 {
166 MYGUI_LOG(Error, _instance << " : '" << _file << "', type '" << _type << "' not found");
167 return false;
168 }
169 }
170 else
171 {
172 MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
173 return false;
174 }
175 }
176 // assume there will be nested elements
177 else if (!_match)
178 {
179 for (auto node : root.children("MyGUI"))
180 {
181 type = node.attribute("type").value();
182 if (!type.empty())
183 {
184 Version version = Version::parse(root.attribute("version").value());
185 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
186 if (iter != mMapLoadXmlDelegate.end())
187 {
188 (*iter).second(node, _file, version);
189 }
190 else
191 {
192 MYGUI_LOG(
193 Error,
194 _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
195 }
196 }
197 else
198 {
199 MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'type' not found");
200 }
201 }
202 }
203
204 return true;
205 }
206
207 void ResourceManager::addResource(IResourcePtr _item)
208 {
209 if (!_item->getResourceName().empty())
210 mResources[_item->getResourceName()] = _item;
211 }
212
213 void ResourceManager::removeResource(IResourcePtr _item)
214 {
215 if (_item == nullptr)
216 return;
217
218 if (!_item->getResourceName().empty())
219 {
220 MapResource::iterator item = mResources.find(_item->getResourceName());
221 if (item != mResources.end())
222 mResources.erase(item);
223 }
224 }
225
226 bool ResourceManager::isExist(std::string_view _name) const
227 {
228 return mResources.find(_name) != mResources.end();
229 }
230
231 IResource* ResourceManager::findByName(std::string_view _name) const
232 {
233 MapResource::const_iterator item = mResources.find(_name);
234 return (item == mResources.end()) ? nullptr : item->second;
235 }
236
237 IResource* ResourceManager::getByName(std::string_view _name, bool _throw) const
238 {
239 IResource* result = findByName(_name);
240 MYGUI_ASSERT(result || !_throw, "Resource '" << _name << "' not found");
241 return result;
242 }
243
244 bool ResourceManager::removeByName(std::string_view _name)
245 {
246 MapResource::const_iterator item = mResources.find(_name);
247 if (item != mResources.end())
248 {
249 delete item->second;
250 mResources.erase(item->first);
251 return true;
252 }
253 return false;
254 }
255
256 void ResourceManager::clear()
257 {
258 for (auto& resource : mResources)
259 delete resource.second;
260 mResources.clear();
261
262 for (auto& removedResource : mRemovedResources)
263 delete removedResource;
264 mRemovedResources.clear();
265 }
266
267 const ResourceManager::MapResource& ResourceManager::getResources() const
268 {
269 return mResources;
270 }
271
272 size_t ResourceManager::getCount() const
273 {
274 return mResources.size();
275 }
276
277 const std::string& ResourceManager::getCategoryName() const
278 {
279 return mCategoryName;
280 }
281
282} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
IResource * IResourcePtr
MYGUI_SINGLETON_DEFINITION(ClipboardManager)