• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_ResourceManager.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009     
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014     
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019     
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_ResourceManager.h"
00025 #include "MyGUI_XmlDocument.h"
00026 #include "MyGUI_IResource.h"
00027 #include "MyGUI_DataManager.h"
00028 #include "MyGUI_FactoryManager.h"
00029 
00030 #include "MyGUI_ResourceImageSet.h"
00031 
00032 namespace MyGUI
00033 {
00034 
00035     const std::string XML_TYPE("Resource");
00036     const std::string XML_TYPE_LIST("List");
00037     const std::string XML_TYPE_SECTION("Section");
00038 
00039     MYGUI_INSTANCE_IMPLEMENT(ResourceManager);
00040 
00041     void ResourceManager::initialise()
00042     {
00043         MYGUI_ASSERT(false == mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
00044         MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
00045 
00046         registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &ResourceManager::_load);
00047         registerLoadXmlDelegate(XML_TYPE_LIST) = newDelegate(this, &ResourceManager::_loadList);
00048         registerLoadXmlDelegate(XML_TYPE_SECTION) = newDelegate(this, &ResourceManager::_loadSection);
00049 
00050         // регестрируем дефолтные ресурсы
00051         FactoryManager::getInstance().registryFactory<ResourceImageSet>(XML_TYPE);
00052 
00053         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
00054         mIsInitialise = true;
00055     }
00056 
00057     void ResourceManager::shutdown()
00058     {
00059         if (false == mIsInitialise) return;
00060         MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
00061 
00062         FactoryManager::getInstance().unregistryFactory<ResourceImageSet>(XML_TYPE);
00063 
00064         clear();
00065         unregisterLoadXmlDelegate(XML_TYPE);
00066         unregisterLoadXmlDelegate(XML_TYPE_LIST);
00067         unregisterLoadXmlDelegate(XML_TYPE_SECTION);
00068 
00069         mMapLoadXmlDelegate.clear();
00070 
00071         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
00072         mIsInitialise = false;
00073     }
00074 
00075     bool ResourceManager::load(const std::string& _file)
00076     {
00077         return _loadImplement(_file, false, "", INSTANCE_TYPE_NAME);
00078     }
00079 
00080     void ResourceManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00081     {
00082         FactoryManager& factory = FactoryManager::getInstance();
00083 
00084         VectorGuid vector_guid;
00085         // берем детей и крутимся, основной цикл
00086         xml::ElementEnumerator root = _node->getElementEnumerator();
00087         while (root.next(XML_TYPE))
00088         {
00089             // парсим атрибуты
00090             std::string id, type, name;
00091             root->findAttribute("type", type);
00092             root->findAttribute("name", name);
00093             root->findAttribute("id", id);
00094 
00095             Guid guid(id);
00096             if (!guid.empty())
00097             {
00098                 if (mResourcesID.find(guid) != mResourcesID.end())
00099                 {
00100                     MYGUI_LOG(Warning, "dublicate resource id " << guid.print());
00101                 }
00102             }
00103 
00104             if (mResources.find(name) != mResources.end())
00105             {
00106                 MYGUI_LOG(Warning, "dublicate resource name '" << name << "'");
00107             }
00108 
00109             vector_guid.push_back(guid);
00110 
00111             IObject* object = factory.createObject(XML_TYPE, type);
00112             if (object == nullptr)
00113             {
00114                 MYGUI_LOG(Error, "resource type '" << type << "' not found");
00115                 continue;
00116             }
00117 
00118             IResourcePtr resource = object->castType<IResource>();
00119             resource->deserialization(root.current(), _version);
00120 
00121             if (!guid.empty()) mResourcesID[guid] = resource;
00122             if (!name.empty()) mResources[name] = resource;
00123         }
00124 
00125         if (!vector_guid.empty())
00126         {
00127             mListFileGuid[_file] = vector_guid;
00128         }
00129 
00130     }
00131 
00132     std::string ResourceManager::getFileNameByID(const Guid& _id)
00133     {
00134         for (MapVectorString::iterator item=mListFileGuid.begin(); item!=mListFileGuid.end(); ++item)
00135         {
00136             for (VectorGuid::iterator item2=item->second.begin(); item2!=item->second.end(); ++item2)
00137             {
00138                 if (*item2 == _id)
00139                 {
00140                     return item->first;
00141                 }
00142             }
00143         }
00144         return "";
00145     }
00146 
00147     void ResourceManager::_loadList(xml::ElementPtr _node, const std::string& _file, Version _version)
00148     {
00149         // берем детей и крутимся, основной цикл
00150         xml::ElementEnumerator node = _node->getElementEnumerator();
00151         while (node.next(XML_TYPE_LIST))
00152         {
00153             std::string source;
00154             if (false == node->findAttribute("file", source)) continue;
00155             MYGUI_LOG(Info, "Load ini file '" << source << "'");
00156             _loadImplement(source, false, "", INSTANCE_TYPE_NAME);
00157         };
00158     }
00159 
00160     ResourceManager::LoadXmlDelegate& ResourceManager::registerLoadXmlDelegate(const std::string& _key)
00161     {
00162         MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
00163         MYGUI_ASSERT(iter == mMapLoadXmlDelegate.end(), "name delegate is exist");
00164         return (mMapLoadXmlDelegate[_key] = LoadXmlDelegate());
00165     }
00166 
00167     void ResourceManager::unregisterLoadXmlDelegate(const std::string& _key)
00168     {
00169         MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(_key);
00170         if (iter != mMapLoadXmlDelegate.end()) mMapLoadXmlDelegate.erase(iter);
00171     }
00172 
00173     bool ResourceManager::_loadImplement(const std::string& _file, bool _match, const std::string& _type, const std::string& _instance)
00174     {
00175         IDataStream* data = DataManager::getInstance().getData(_file);
00176         if (data == nullptr)
00177         {
00178             MYGUI_LOG(Error, _instance << " : '" << _file << "', not found");
00179             return false;
00180         }
00181 
00182         xml::Document doc;
00183         if (false == doc.open(data))
00184         {
00185             MYGUI_LOG(Error, _instance << " : '" << _file << "', " << doc.getLastError());
00186 
00187             // FIXME
00188             delete data;
00189 
00190             return false;
00191         }
00192 
00193         // FIXME
00194         delete data;
00195 
00196         xml::ElementPtr root = doc.getRoot();
00197         if ( (nullptr == root) || (root->getName() != "MyGUI") )
00198         {
00199             MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'MyGUI' not found");
00200             return false;
00201         }
00202 
00203         std::string type;
00204         if (root->findAttribute("type", type))
00205         {
00206             Version version = Version::parse(root->findAttribute("version"));
00207             MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
00208             if (iter != mMapLoadXmlDelegate.end())
00209             {
00210                 if ((false == _match) || (type == _type)) (*iter).second(root, _file, version);
00211                 else
00212                 {
00213                     MYGUI_LOG(Error, _instance << " : '" << _file << "', type '" << _type << "' not found");
00214                     return false;
00215                 }
00216             }
00217             else
00218             {
00219                 MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
00220                 return false;
00221             }
00222         }
00223         // предпологаем что будут вложенные
00224         else if (false == _match)
00225         {
00226             xml::ElementEnumerator node = root->getElementEnumerator();
00227             while (node.next("MyGUI"))
00228             {
00229                 if (node->findAttribute("type", type))
00230                 {
00231                     Version version = Version::parse(root->findAttribute("version"));
00232                     MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
00233                     if (iter != mMapLoadXmlDelegate.end())
00234                     {
00235                         (*iter).second(node.current(), _file, version);
00236                     }
00237                     else
00238                     {
00239                         MYGUI_LOG(Error, _instance << " : '" << _file << "', delegate for type '" << type << "'not found");
00240                     }
00241                 }
00242                 else
00243                 {
00244                     MYGUI_LOG(Error, _instance << " : '" << _file << "', tag 'type' not found");
00245                 }
00246             }
00247         }
00248 
00249         return true;
00250     }
00251 
00252     IResourcePtr ResourceManager::getByID(const Guid& _id, bool _throw)
00253     {
00254         MapResourceID::iterator iter = mResourcesID.find(_id);
00255         if (iter == mResourcesID.end())
00256         {
00257             if (_throw) MYGUI_EXCEPT("resource '" << _id.print() << "' not found");
00258             MYGUI_LOG(Warning, "resource '" << _id.print() << "' not found");
00259             return nullptr;
00260         }
00261         return iter->second;
00262     }
00263 
00264     void ResourceManager::addResource(IResourcePtr _item)
00265     {
00266         if (!_item->getResourceName().empty())
00267             mResources[_item->getResourceName()] = _item;
00268         if (!_item->getResourceID().empty())
00269             mResourcesID[_item->getResourceID()] = _item;
00270     }
00271 
00272     void ResourceManager::removeResource(IResourcePtr _item)
00273     {
00274         if (_item == nullptr) return;
00275 
00276         if (!_item->getResourceName().empty())
00277         {
00278             MapResource::iterator item = mResources.find(_item->getResourceName());
00279             if (item != mResources.end())
00280                 mResources.erase(item);
00281         }
00282 
00283         if (!_item->getResourceID().empty())
00284         {
00285             MapResourceID::iterator id = mResourcesID.find(_item->getResourceID());
00286             if (id != mResourcesID.end())
00287                 mResourcesID.erase(id);
00288         }
00289     }
00290 
00291     void ResourceManager::_loadSection(xml::ElementPtr _node, const std::string& _file, Version _version)
00292     {
00293         // берем детей и крутимся, основной цикл
00294         xml::ElementEnumerator node = _node->getElementEnumerator();
00295         while (node.next(XML_TYPE_SECTION))
00296         {
00297             std::string type;
00298             if (false == node->findAttribute("type", type))
00299                 continue;
00300 
00301             Version version = Version::parse(node->findAttribute("version"));
00302             MapLoadXmlDelegate::iterator iter = mMapLoadXmlDelegate.find(type);
00303             if (iter != mMapLoadXmlDelegate.end())
00304             {
00305                 (*iter).second(node.current(), _file, version);
00306             }
00307             else
00308             {
00309                 MYGUI_LOG(Error, INSTANCE_TYPE_NAME << " : '" << _file << "', delegate for type '" << type << "'not found");
00310                 return;
00311             }
00312 
00313         }
00314     }
00315 
00316 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1