23 ResourceManager::ResourceManager() :
24 mSingletonHolder(this),
25 mCategoryName(
"Resource"),
26 mXmlListTagName(
"List")
30 void ResourceManager::initialise()
32 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() <<
" initialised twice");
33 MYGUI_LOG(Info,
"* Initialise: " << getClassTypeName());
35 registerLoadXmlDelegate(mCategoryName) = newDelegate(
this, &ResourceManager::loadFromXmlNode);
36 registerLoadXmlDelegate(mXmlListTagName) = newDelegate(
this, &ResourceManager::_loadList);
38 FactoryManager::getInstance().registerFactory<ResourceImageSet>(mCategoryName);
40 MYGUI_LOG(Info, getClassTypeName() <<
" successfully initialized");
44 void ResourceManager::shutdown()
46 MYGUI_ASSERT(mIsInitialise, getClassTypeName() <<
" is not initialised");
47 MYGUI_LOG(Info,
"* Shutdown: " << getClassTypeName());
49 FactoryManager::getInstance().unregisterFactory<ResourceImageSet>(mCategoryName);
52 unregisterLoadXmlDelegate(mCategoryName);
53 unregisterLoadXmlDelegate(mXmlListTagName);
55 mMapLoadXmlDelegate.clear();
57 MYGUI_LOG(Info, getClassTypeName() <<
" successfully shutdown");
58 mIsInitialise =
false;
61 bool ResourceManager::load(
const std::string& _file)
63 return _loadImplement(_file,
false, {}, getClassTypeName());
66 void ResourceManager::loadFromXmlNode(xml::ElementPtr _node, std::string_view , Version _version)
68 FactoryManager& factory = FactoryManager::getInstance();
70 for (
auto root : _node.node().children(mCategoryName.c_str()))
72 std::string_view type = root.attribute(
"type").value();
73 std::string name{root.attribute(
"name").value()};
78 IObject*
object = factory.createObject(mCategoryName, type);
79 if (
object ==
nullptr)
81 MYGUI_LOG(Error,
"resource type '" << type <<
"' not found");
85 MapResource::iterator item = mResources.find(name);
86 if (item != mResources.end())
88 MYGUI_LOG(Warning,
"duplicate resource name '" << name <<
"'");
91 mRemovedResources.push_back((*item).second);
92 mResources.erase(item);
96 resource->deserialization(root, _version);
98 mResources[name] = resource;
102 void ResourceManager::_loadList(xml::ElementPtr _node, std::string_view , Version _version)
104 for (
auto child : _node.node().children(mXmlListTagName.c_str()))
106 std::string source{child.attribute(
"file").value()};
109 MYGUI_LOG(Info,
"Load ini file '" << source <<
"'");
110 _loadImplement(source,
false, {}, getClassTypeName());
114 ResourceManager::LoadXmlDelegate& ResourceManager::registerLoadXmlDelegate(std::string_view _key)
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;
121 void ResourceManager::unregisterLoadXmlDelegate(std::string_view _key)
123 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
124 if (iter != mMapLoadXmlDelegate.end())
125 mMapLoadXmlDelegate.erase(iter);
128 bool ResourceManager::_loadImplement(
129 const std::string& _file,
131 std::string_view _type,
132 std::string_view _instance)
134 auto data = DataManager::getInstance().getDataHolder(_file);
137 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', not found");
142 if (!doc.open(data.getData()))
144 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', " << doc.getLastError());
148 pugi::xml_node root = doc.getRoot();
149 if ((!root) || (root.name() != std::string_view(
"MyGUI")))
151 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', tag 'MyGUI' not found");
155 std::string_view type = root.attribute(
"type").value();
158 Version version = Version::parse(root.attribute(
"version").value());
159 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
160 if (iter != mMapLoadXmlDelegate.end())
162 if ((!_match) || (type == _type))
163 (*iter).second(root, _file, version);
166 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', type '" << _type <<
"' not found");
172 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', delegate for type '" << type <<
"'not found");
179 for (
auto node : root.children(
"MyGUI"))
181 type = node.attribute(
"type").value();
184 Version version = Version::parse(root.attribute(
"version").value());
185 MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
186 if (iter != mMapLoadXmlDelegate.end())
188 (*iter).second(node, _file, version);
194 _instance <<
" : '" << _file <<
"', delegate for type '" << type <<
"'not found");
199 MYGUI_LOG(Error, _instance <<
" : '" << _file <<
"', tag 'type' not found");
207 void ResourceManager::addResource(IResourcePtr _item)
209 if (!_item->getResourceName().empty())
210 mResources[_item->getResourceName()] = _item;
213 void ResourceManager::removeResource(IResourcePtr _item)
215 if (_item ==
nullptr)
218 if (!_item->getResourceName().empty())
220 MapResource::iterator item = mResources.find(_item->getResourceName());
221 if (item != mResources.end())
222 mResources.erase(item);
226 bool ResourceManager::isExist(std::string_view _name)
const
228 return mResources.find(_name) != mResources.end();
231 IResource* ResourceManager::findByName(std::string_view _name)
const
233 MapResource::const_iterator item = mResources.find(_name);
234 return (item == mResources.end()) ? nullptr : item->second;
237 IResource* ResourceManager::getByName(std::string_view _name,
bool _throw)
const
239 IResource* result = findByName(_name);
240 MYGUI_ASSERT(result || !_throw,
"Resource '" << _name <<
"' not found");
244 bool ResourceManager::removeByName(std::string_view _name)
246 MapResource::const_iterator item = mResources.find(_name);
247 if (item != mResources.end())
250 mResources.erase(item->first);
256 void ResourceManager::clear()
258 for (
auto& resource : mResources)
259 delete resource.second;
262 for (
auto& removedResource : mRemovedResources)
263 delete removedResource;
264 mRemovedResources.clear();
267 const ResourceManager::MapResource& ResourceManager::getResources()
const
272 size_t ResourceManager::getCount()
const
274 return mResources.size();
277 const std::string& ResourceManager::getCategoryName()
const
279 return mCategoryName;
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
MYGUI_SINGLETON_DEFINITION(ClipboardManager)