MyGUI: MyGUI_ResourceManager.cpp Source File

MyGUI 3.4.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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"
8#include "MyGUI_ResourceManager.h"
9#include "MyGUI_XmlDocument.h"
10#include "MyGUI_IResource.h"
11#include "MyGUI_DataManager.h"
12#include "MyGUI_FactoryManager.h"
13#include "MyGUI_DataStreamHolder.h"
14#include "MyGUI_ResourceImageSet.h"
15
16namespace MyGUI
17{
18
19 MYGUI_SINGLETON_DEFINITION(ResourceManager);
20
21 ResourceManager::ResourceManager() :
22 mIsInitialise(false),
23 mCategoryName("Resource"),
24 mXmlListTagName("List"),
25 mSingletonHolder(this)
26 {
27 }
28
29 void ResourceManager::initialise()
30 {
31 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33
34 registerLoadXmlDelegate(mCategoryName) = newDelegate(this, &ResourceManager::loadFromXmlNode);
35 registerLoadXmlDelegate(mXmlListTagName) = newDelegate(this, &ResourceManager::_loadList);
36
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, const std::string& _file, Version _version)
67 {
68 FactoryManager& factory = FactoryManager::getInstance();
69
70 // берем детей и крутимся, основной цикл
71 xml::ElementEnumerator root = _node->getElementEnumerator();
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 // берем детей и крутимся, основной цикл
109 xml::ElementEnumerator node = _node->getElementEnumerator();
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
119 ResourceManager::LoadXmlDelegate& ResourceManager::registerLoadXmlDelegate(const std::string& _key)
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 {
134 DataStreamHolder data = DataManager::getInstance().getData(_file);
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
205 void ResourceManager::addResource(IResourcePtr _item)
206 {
207 if (!_item->getResourceName().empty())
208 mResources[_item->getResourceName()] = _item;
209 }
210
211 void ResourceManager::removeResource(IResourcePtr _item)
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
254 void ResourceManager::clear()
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
265 ResourceManager::EnumeratorPtr ResourceManager::getEnumerator() const
266 {
267 return EnumeratorPtr(mResources);
268 }
269
270 size_t ResourceManager::getCount() const
271 {
272 return mResources.size();
273 }
274
275 const std::string& ResourceManager::getCategoryName() const
276 {
277 return mCategoryName;
278 }
279
280} // namespace MyGUI
MyGUI_DataManager.h
MyGUI_DataStreamHolder.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:29
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_IResource.h
MyGUI_Precompiled.h
MyGUI_ResourceImageSet.h
MyGUI_ResourceManager.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:123
MyGUI_XmlDocument.h
MyGUI::DataManager::getData
virtual IDataStream * getData(const std::string &_name) const =0
MyGUI::DataManager::getInstance
static DataManager & getInstance()
MyGUI::DataStreamHolder
Definition MyGUI_DataStreamHolder.h:17
MyGUI::DataStreamHolder::getData
IDataStream * getData()
Definition MyGUI_DataStreamHolder.h:31
MyGUI::Enumerator
Definition MyGUI_Enumerator.h:49
MyGUI::FactoryManager
Definition MyGUI_FactoryManager.h:19
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition MyGUI_FactoryManager.cpp:46
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition MyGUI_FactoryManager.cpp:40
MyGUI::FactoryManager::createObject
IObject * createObject(const std::string &_category, const std::string &_type)
Definition MyGUI_FactoryManager.cpp:72
MyGUI::IObject
Definition MyGUI_IObject.h:17
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::IResource
Definition MyGUI_IResource.h:25
MyGUI::IResource::deserialization
void deserialization(xml::ElementPtr _node, Version) override
Definition MyGUI_IResource.h:48
MyGUI::IResource::getResourceName
const std::string & getResourceName() const
Definition MyGUI_IResource.h:32
MyGUI::ResourceImageSet
Definition MyGUI_ResourceImageSet.h:28
MyGUI::ResourceManager
Definition MyGUI_ResourceManager.h:23
MyGUI::ResourceManager::ResourceManager
ResourceManager()
Definition MyGUI_ResourceManager.cpp:21
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_ResourceManager.cpp:275
MyGUI::ResourceManager::initialise
void initialise()
Definition MyGUI_ResourceManager.cpp:29
MyGUI::ResourceManager::findByName
IResource * findByName(const std::string &_name) const
Definition MyGUI_ResourceManager.cpp:229
MyGUI::ResourceManager::loadFromXmlNode
void loadFromXmlNode(xml::ElementPtr _node, const std::string &_file, Version _version)
Definition MyGUI_ResourceManager.cpp:66
MyGUI::ResourceManager::getCount
size_t getCount() const
Definition MyGUI_ResourceManager.cpp:270
MyGUI::ResourceManager::removeResource
void removeResource(IResourcePtr _item)
Definition MyGUI_ResourceManager.cpp:211
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition MyGUI_ResourceManager.cpp:235
MyGUI::ResourceManager::EnumeratorPtr
Enumerator< MapResource > EnumeratorPtr
Definition MyGUI_ResourceManager.h:65
MyGUI::ResourceManager::LoadXmlDelegate
delegates::CDelegate3< xml::ElementPtr, const std::string &, Version > LoadXmlDelegate
Definition MyGUI_ResourceManager.h:43
MyGUI::ResourceManager::clear
void clear()
Definition MyGUI_ResourceManager.cpp:254
MyGUI::ResourceManager::getEnumerator
EnumeratorPtr getEnumerator() const
Definition MyGUI_ResourceManager.cpp:265
MyGUI::ResourceManager::removeByName
bool removeByName(const std::string &_name)
Definition MyGUI_ResourceManager.cpp:242
MyGUI::ResourceManager::load
bool load(const std::string &_file)
Definition MyGUI_ResourceManager.cpp:61
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition MyGUI_ResourceManager.cpp:126
MyGUI::ResourceManager::isExist
bool isExist(const std::string &_name) const
Definition MyGUI_ResourceManager.cpp:224
MyGUI::ResourceManager::shutdown
void shutdown()
Definition MyGUI_ResourceManager.cpp:44
MyGUI::ResourceManager::getClassTypeName
static const char * getClassTypeName()
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition MyGUI_ResourceManager.cpp:119
MyGUI::ResourceManager::addResource
void addResource(IResourcePtr _item)
Definition MyGUI_ResourceManager.cpp:205
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Version::parse
static Version parse(const std::string &_value)
Definition MyGUI_Version.h:94
MyGUI::delegates::Delegate
Definition MyGUI_Delegate.h:136
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:115
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:100
MyGUI::xml::ElementEnumerator::current
ElementPtr current()
Definition MyGUI_XmlDocument.cpp:131
MyGUI::xml::Element
Definition MyGUI_XmlDocument.h:159
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:352
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:246
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:106
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:99