MyGUI 3.4.1
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
16namespace MyGUI
17{
18
20
22 mIsInitialise(false),
23 mCategoryName("Resource"),
24 mXmlListTagName("List"),
25 mSingletonHolder(this)
26 {
27 }
28
30 {
31 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33
35 registerLoadXmlDelegate(mXmlListTagName) = newDelegate(this, &ResourceManager::_loadList);
36
37 // регестрируем дефолтные ресурсы
39
40 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
41 mIsInitialise = true;
42 }
43
45 {
46 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
47 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
48
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, const std::string& _file, Version _version)
67 {
69
70 // берем детей и крутимся, основной цикл
72 while (root.next(mCategoryName))
73 {
74 // парсим атрибуты
75 std::string type, name;
76 root->findAttribute("type", type);
77 root->findAttribute("name", name);
78
79 if (name.empty())
80 continue;
81
82 IObject* object = factory.createObject(mCategoryName, type);
83 if (object == nullptr)
84 {
85 MYGUI_LOG(Error, "resource type '" << type << "' not found");
86 continue;
87 }
88
89 MapResource::iterator item = mResources.find(name);
90 if (item != mResources.end())
91 {
92 MYGUI_LOG(Warning, "duplicate resource name '" << name << "'");
93
94 // ресурсами могут пользоваться
95 mRemovedResoures.push_back((*item).second);
96 mResources.erase(item);
97 }
98
99 IResourcePtr resource = object->castType<IResource>();
100 resource->deserialization(root.current(), _version);
101
102 mResources[name] = resource;
103 }
104 }
105
106 void ResourceManager::_loadList(xml::ElementPtr _node, const std::string& _file, Version _version)
107 {
108 // берем детей и крутимся, основной цикл
110 while (node.next(mXmlListTagName))
111 {
112 std::string source;
113 if (!node->findAttribute("file", source)) continue;
114 MYGUI_LOG(Info, "Load ini file '" << source << "'");
115 _loadImplement(source, false, "", getClassTypeName());
116 }
117 }
118
120 {
121 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
122 MYGUI_ASSERT(iter == mMapLoadXmlDelegate.end(), "name delegate is exist");
123 return (mMapLoadXmlDelegate[_key] = LoadXmlDelegate());
124 }
125
126 void ResourceManager::unregisterLoadXmlDelegate(const std::string& _key)
127 {
128 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
129 if (iter != mMapLoadXmlDelegate.end()) mMapLoadXmlDelegate.erase(iter);
130 }
131
132 bool ResourceManager::_loadImplement(const std::string& _file, bool _match, const std::string& _type, const std::string& _instance)
133 {
135 if (data.getData() == nullptr)
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 xml::ElementPtr root = doc.getRoot();
149 if ( (nullptr == root) || (root->getName() != "MyGUI") )
150 {
151 MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'MyGUI' not found");
152 return false;
153 }
154
155 std::string type;
156 if (root->findAttribute("type", type))
157 {
158 Version version = Version::parse(root->findAttribute("version"));
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 // предпологаем что будут вложенные
177 else if (!_match)
178 {
179 xml::ElementEnumerator node = root->getElementEnumerator();
180 while (node.next("MyGUI"))
181 {
182 if (node->findAttribute("type", type))
183 {
184 Version version = Version::parse(root->findAttribute("version"));
185 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
186 if (iter != mMapLoadXmlDelegate.end())
187 {
188 (*iter).second(node.current(), _file, version);
189 }
190 else
191 {
192 MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
193 }
194 }
195 else
196 {
197 MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'type' not found");
198 }
199 }
200 }
201
202 return true;
203 }
204
206 {
207 if (!_item->getResourceName().empty())
208 mResources[_item->getResourceName()] = _item;
209 }
210
212 {
213 if (_item == nullptr)
214 return;
215
216 if (!_item->getResourceName().empty())
217 {
218 MapResource::iterator item = mResources.find(_item->getResourceName());
219 if (item != mResources.end())
220 mResources.erase(item);
221 }
222 }
223
224 bool ResourceManager::isExist(const std::string& _name) const
225 {
226 return mResources.find(_name) != mResources.end();
227 }
228
229 IResource* ResourceManager::findByName(const std::string& _name) const
230 {
231 MapResource::const_iterator item = mResources.find(_name);
232 return (item == mResources.end()) ? nullptr : item->second;
233 }
234
235 IResource* ResourceManager::getByName(const std::string& _name, bool _throw) const
236 {
237 IResource* result = findByName(_name);
238 MYGUI_ASSERT(result || !_throw, "Resource '" << _name << "' not found");
239 return result;
240 }
241
242 bool ResourceManager::removeByName(const std::string& _name)
243 {
244 MapResource::const_iterator item = mResources.find(_name);
245 if (item != mResources.end())
246 {
247 delete item->second;
248 mResources.erase(item->first);
249 return true;
250 }
251 return false;
252 }
253
255 {
256 for (MapResource::iterator item = mResources.begin(); item != mResources.end(); ++ item)
257 delete item->second;
258 mResources.clear();
259
260 for (VectorResource::iterator item = mRemovedResoures.begin(); item != mRemovedResoures.end(); ++ item)
261 delete (*item);
262 mRemovedResoures.clear();
263 }
264
266 {
267 return EnumeratorPtr(mResources);
268 }
269
271 {
272 return mResources.size();
273 }
274
275 const std::string& ResourceManager::getCategoryName() const
276 {
277 return mCategoryName;
278 }
279
280} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
virtual IDataStream * getData(const std::string &_name) const =0
static DataManager & getInstance()
void unregisterFactory(const std::string &_category, const std::string &_type)
static FactoryManager & getInstance()
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
IObject * createObject(const std::string &_category, const std::string &_type)
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
const std::string & getResourceName() const
void deserialization(xml::ElementPtr _node, Version _version) override
const std::string & getCategoryName() const
Enumerator< MapResource > EnumeratorPtr
IResource * findByName(const std::string &_name) const
void loadFromXmlNode(xml::ElementPtr _node, const std::string &_file, Version _version)
delegates::CDelegate3< xml::ElementPtr, const std::string &, Version > LoadXmlDelegate
void removeResource(IResourcePtr _item)
IResource * getByName(const std::string &_name, bool _throw=true) const
EnumeratorPtr getEnumerator() const
bool removeByName(const std::string &_name)
bool load(const std::string &_file)
void unregisterLoadXmlDelegate(const std::string &_key)
bool isExist(const std::string &_name) const
static const char * getClassTypeName()
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
void addResource(IResourcePtr _item)
static Version parse(const std::string &_value)
Definition: MyGUI_Version.h:94
ElementEnumerator getElementEnumerator()
bool findAttribute(const std::string &_name, std::string &_value)
Element * ElementPtr
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
MYGUI_SINGLETON_DEFINITION(ClipboardManager)