Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource_manager.cc @ 9852

Last change on this file since 9852 was 9852, checked in by bensch, 18 years ago

orxonox/new_class_id: more doxy-tags

File size: 8.4 KB
RevLine 
[4597]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[3655]12   main-programmer: Benjamin Grauer
[3672]13   co-programmer: Patrick Boenzli
[1853]14*/
15
[3655]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
[1853]17
[9836]18#include "resource_manager.h"
[4642]19#include "debug.h"
20
[6222]21#include <algorithm>
[9797]22#include <cassert>
[6222]23
[1853]24
[9800]25namespace Resources
26{
[9852]27  /// Definition of the ResourceManager's ObjectList.
[9836]28  ObjectListDefinition(ResourceManager);
29  //! Singleton Reference to the ResourceManager
30  ResourceManager* ResourceManager::_singletonRef = NULL;
[9791]31
32
[9800]33  /**
34   * @brief standard constructor
35  */
[9836]36  ResourceManager::ResourceManager ()
[9800]37  {
[9836]38    this->registerObject(this, ResourceManager::_objectList);
39    this->setName("ResourceManager");
[9802]40    this->_mainGlobalPath = Directory("./");
[9800]41  }
[4597]42
[1853]43
[9800]44  /**
45   * @brief standard destructor
46  */
[9836]47  ResourceManager::~ResourceManager ()
[9800]48  {
49    // deleting the Resources-List
50    //this->unloadAllByPriority(RP_GAME);
[3655]51
[9800]52    //   if (!this->resourceList.empty())
53    //     PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
[5303]54
[9836]55    ResourceManager::_singletonRef = NULL;
[9800]56  }
[5303]57
[9852]58  /**
59   * @brief Registers a new Type to the ResourceManager.
60   * @param type the Type to register.
61   */
[9836]62  void ResourceManager::registerType(Resources::Type* type)
[9791]63  {
[9845]64    this->_resourceTypes.push_back(type);
65    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
[9791]66  }
67
[9852]68  /**
69   * @brief Unregisters a new Type to the ResourceManager.
70   * @param type the Type to unregister.
71   */
[9836]72  void ResourceManager::unregisterType(Resources::Type* type)
[9799]73  {
[9800]74    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
75    if (it != this->_resourceTypes.end())
76    {
77      this->_resourceTypes.erase(it);
[9845]78      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
[9800]79    }
[9799]80  }
81
[9852]82  /**
83   * @brief Sets the main Global path (the main path Resources are searched for)
84   * @param directory the directory to set.
85   * @see Resource::locateFile
86   */
[9836]87  void ResourceManager::setMainGlobalPath(const Directory& directory)
[9800]88  {
89    this->_mainGlobalPath = directory;
90    this->_mainGlobalPath.open();
91  }
[9791]92
[9852]93  /**
94   * @brief add a Global search path. (global paths besided the main path.)
95   * @param directory a directory to add.
96   */
[9836]97  void ResourceManager::addGlobalPath(const Directory& directory)
[9800]98  {
99    std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory);
100    if (it == this->_globalPaths.end())
101      this->_globalPaths.push_back(directory);
102  }
[9791]103
[9852]104  /**
105   * @brief add a ResourcePath to a Type's Paths.
106   * @param resourceName the Type's name of Resource to add the path to.
107   * @param pathName pathName the Name of the path to add.
108   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
109   */
[9836]110  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
[9800]111  {
112    std::vector<Resources::Type*>::iterator it;
113    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
114      if (*(*it) == resourceName)
115        return (*it)->addResourcePath(pathName);
116    PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
117    return false;
118  }
[9791]119
[9852]120  /**
121   * @brief add a ResourcePath to a Type's SubPaths.
122   * @param resourceName the Type's name of Resource to add the subpath to.
123   * @param pathName pathName the Name of the path to add.
124   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
125   */
[9836]126  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
[9800]127  {
128    std::vector<Resources::Type*>::iterator it;
129    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
130      if (*(*it) == resourceName)
131        return (*it)->addResourceSubPath(pathName);
132    PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
133    return false;
134  }
[9791]135
[9852]136  /**
137   * @brief checks wether a File is inside of the MainPath.
138   * @param fileInside the file to check
139   * @return true if the file is inside.
140   */
[9836]141  bool ResourceManager::checkFileInMainPath(const File& fileInside)
[9800]142  {
143    return (this->_mainGlobalPath + fileInside).exists();
144  }
[9795]145
[9852]146  /**
147   * @brief prepends the fileName by the MainGlobalPath (same as mainGlobalPath + '/' + fileName).
148   * @param fileName The FileName to prepend
149   * @returns the prepended file-name
150   */
[9836]151  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
[9833]152  {
153    return (this->_mainGlobalPath + File(fileName)).name();
154  }
[9795]155
[9852]156  /**
157   * @brief add a KeepLevelName (this function just counts upwards).
158   * @param keepLevelName the Name of the KeepLevel to set.
159   * @returns the Level the Name was set to.
160   */
[9836]161  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
[9800]162  {
163    this->_keepLevelNames.push_back(keepLevelName);
164    return _keepLevelNames.size()-1;
165  }
[9798]166
[9852]167  /**
168   * @param keepLevelName the Name of the KeepLevel.
169   * @returns the ID of the KeepLevel named keepLevelName
170   */
[9836]171  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
[9800]172  {
173    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
174      if (this->_keepLevelNames[i] == keepLevelName)
175        return i;
[9798]176
[9800]177    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
178    return 0;
179  }
[9798]180
[9852]181  /**
182   * @param keepLevelID the ID to check.
183   * @return the name of the KeepLevel.
184   */
[9836]185  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
[9800]186  {
187    assert(keepLevelID < this->_keepLevelNames.size());
188    return this->_keepLevelNames[keepLevelID];
189  }
[9798]190
[9852]191
192  /**
193   * @brief loads a Resource from a TypeName and a loadString.
194   * @param resourceTypeName The Name of the Type to what to load a Resource from.
195   * @param loadString the loadString to load in the Type.
196   */
[9848]197  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString)
198  {
199    std::vector<Resources::Type*>::const_iterator it;
200    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
201    {
202      if (*(*it) == resourceTypeName)
203      {
204        (*it)->createFromString(loadString);
205        /// TODO check if the resource was allocated!!
206        return ;
207      }
208    }
209    return ;
210  }
[9798]211
[9852]212  /**
213   * @brief unloads all Resources below a certain threshhold.
214   * @param keepLevel the KeepLevel below which to erase.
215   *
216   * @not Resources will only be erased, if th keepLevel is below, and the resources are not
217   * referenced anymore.
218   */
[9845]219  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
220  {
221    std::vector<Resources::Type*>::const_iterator it;
222    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
223    {
224      (*it)->unloadAllBelowKeepLevel(keepLevel);
225    }
226  }
227
228
[9800]229  /**
[9836]230   * @brief outputs debug information about the ResourceManager
[9800]231   */
[9836]232  void ResourceManager::debug() const
[9798]233  {
[9800]234    PRINT(0)("=RM===================================\n");
235    PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
236    PRINT(0)("======================================\n");
237    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
238    if(!this->_globalPaths.empty())
239    {
240      PRINT(0)(" Additional Global search Paths are: ");
241      for (unsigned int i = 0; i < this->_globalPaths.size(); ++i)
242        PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str());
243      PRINT(0)("\n");
244    }
245    PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size());
246    std::vector<Resources::Type*>::const_iterator it;
247    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
248    {
249      (*it)->debug();
250      if (it != --this->_resourceTypes.end())
251        PRINT(0)(" ------------------------------------\n ");
252    }
253
254    PRINT(0)("KeepLevels are: ");
255    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
256      PRINT(0)("%d:'%s'  ", i, this->_keepLevelNames[i].c_str());
[9798]257    PRINT(0)("\n");
[9800]258    PRINT(0)("==================================RM==\n");
[9798]259  }
[3676]260}
Note: See TracBrowser for help on using the repository browser.