MyGUI 3.2.0

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

- File List
- Globals

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
40 ResourceManager::ResourceManager() :
41 mIsInitialise(false)
42 {
43 }
44
45 void ResourceManager::initialise()
46 {
47 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
48 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
49
50 registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &ResourceManager::loadFromXmlNode);
51 registerLoadXmlDelegate(XML_TYPE_LIST) = newDelegate(this, &ResourceManager::_loadList);
52
53 // регестрируем дефолтные ресурсы
54 FactoryManager::getInstance().registerFactory<ResourceImageSet>(XML_TYPE);
55
56 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
57 mIsInitialise = true;
58 }
59
60 void ResourceManager::shutdown()
61 {
62 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
63 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
64
65 FactoryManager::getInstance().unregisterFactory<ResourceImageSet>(XML_TYPE);
66
67 clear();
68 unregisterLoadXmlDelegate(XML_TYPE);
69 unregisterLoadXmlDelegate(XML_TYPE_LIST);
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 {
84 FactoryManager& factory = FactoryManager::getInstance();
85
86 // берем детей и крутимся, основной цикл
87 xml::ElementEnumerator root = _node->getElementEnumerator();
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 // берем детей и крутимся, основной цикл
125 xml::ElementEnumerator node = _node->getElementEnumerator();
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
135 ResourceManager::LoadXmlDelegate& ResourceManager::registerLoadXmlDelegate(const std::string& _key)
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 {
150 DataStreamHolder data = DataManager::getInstance().getData(_file);
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
221 void ResourceManager::addResource(IResourcePtr _item)
222 {
223 if (!_item->getResourceName().empty())
224 mResources[_item->getResourceName()] = _item;
225 }
226
227 void ResourceManager::removeResource(IResourcePtr _item)
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
270 void ResourceManager::clear()
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
281 ResourceManager::EnumeratorPtr ResourceManager::getEnumerator() const
282 {
283 return EnumeratorPtr(mResources);
284 }
285
286 size_t ResourceManager::getCount() const
287 {
288 return mResources.size();
289 }
290
291 } // namespace MyGUI
MyGUI::DataStreamHolder
Definition: MyGUI_IDataStream.h:42
MyGUI_FactoryManager.h
MyGUI::IResource::deserialization
virtual void deserialization(xml::ElementPtr _node, Version _version)
Definition: MyGUI_IResource.h:61
MyGUI::ResourceManager::shutdown
void shutdown()
Definition: MyGUI_ResourceManager.cpp:60
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:142
MyGUI_Precompiled.h
MyGUI_DataManager.h
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:135
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition: MyGUI_XmlDocument.h:114
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:150
MyGUI::Singleton< FactoryManager >::getInstance
static FactoryManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::ResourceManager::loadFromXmlNode
void loadFromXmlNode(xml::ElementPtr _node, const std::string &_file, Version _version)
Definition: MyGUI_ResourceManager.cpp:82
MyGUI::ResourceManager::removeByName
bool removeByName(const std::string &_name)
Definition: MyGUI_ResourceManager.cpp:258
MyGUI_ResourceImageSet.h
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:261
MyGUI::Singleton< ResourceManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI::IResource::getResourceName
const std::string & getResourceName() const
Definition: MyGUI_IResource.h:47
MyGUI::delegates::CDelegate3
Definition: MyGUI_Delegate.h:169
MyGUI::XML_TYPE
const std::string XML_TYPE("Font")
MyGUI::ResourceManager::LoadXmlDelegate
delegates::CDelegate3< xml::ElementPtr, const std::string &, Version > LoadXmlDelegate
Definition: MyGUI_ResourceManager.h:58
MyGUI::ResourceManager::getEnumerator
EnumeratorPtr getEnumerator() const
Definition: MyGUI_ResourceManager.cpp:281
MyGUI::ResourceImageSet
Definition: MyGUI_ResourceImageSet.h:41
MyGUI::FactoryManager
Definition: MyGUI_FactoryManager.h:33
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition: MyGUI_ResourceManager.cpp:251
MyGUI::Enumerator
Definition: MyGUI_Enumerator.h:63
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:124
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI::ResourceManager::getCount
size_t getCount() const
Definition: MyGUI_ResourceManager.cpp:286
MyGUI_XmlDocument.h
MyGUI::Version::parse
static Version parse(const std::string &_value)
Definition: MyGUI_Version.h:109
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:115
MyGUI::ResourceManager::EnumeratorPtr
Enumerator< MapResource > EnumeratorPtr
Definition: MyGUI_ResourceManager.h:80
MyGUI::ResourceManager::load
bool load(const std::string &_file)
Definition: MyGUI_ResourceManager.cpp:77
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::ResourceManager::findByName
IResource * findByName(const std::string &_name) const
Definition: MyGUI_ResourceManager.cpp:245
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:168
MyGUI::XML_TYPE_LIST
const std::string XML_TYPE_LIST("List")
MyGUI::DataManager::getData
virtual IDataStream * getData(const std::string &_name)=0
MyGUI_IResource.h
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
MyGUI::Version
Definition: MyGUI_Version.h:32
MyGUI_ResourceManager.h
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:61
MyGUI::IObject
Definition: MyGUI_IObject.h:31
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:55
MyGUI::ResourceManager::isExist
bool isExist(const std::string &_name) const
Definition: MyGUI_ResourceManager.cpp:240
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:367
MyGUI::DataStreamHolder::getData
IDataStream * getData()
Definition: MyGUI_IDataStream.h:55
MyGUI::ResourceManager::addResource
void addResource(IResourcePtr _item)
Definition: MyGUI_ResourceManager.cpp:221
MyGUI::ResourceManager::clear
void clear()
Definition: MyGUI_ResourceManager.cpp:270
MyGUI::IResource
Definition: MyGUI_IResource.h:38
MyGUI::ResourceManager::removeResource
void removeResource(IResourcePtr _item)
Definition: MyGUI_ResourceManager.cpp:227
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI::FactoryManager::createObject
IObject * createObject(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:87
MyGUI::ResourceManager::ResourceManager
ResourceManager()
Definition: MyGUI_ResourceManager.cpp:40
MyGUI::ResourceManager::initialise
void initialise()
Definition: MyGUI_ResourceManager.cpp:45
MyGUI::xml::ElementEnumerator::current
ElementPtr current()
Definition: MyGUI_XmlDocument.cpp:146