Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

includes

File size: 8.3 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 ()
[9854]37  : _defaultKeepLevel(0)
[9800]38  {
[9836]39    this->registerObject(this, ResourceManager::_objectList);
40    this->setName("ResourceManager");
[9802]41    this->_mainGlobalPath = Directory("./");
[9800]42  }
[4597]43
[1853]44
[9800]45  /**
46   * @brief standard destructor
47  */
[9836]48  ResourceManager::~ResourceManager ()
[9800]49  {
[9855]50    this->unloadAllBelowKeepLevel(this->_keepLevelNames.size());
[9836]51    ResourceManager::_singletonRef = NULL;
[9800]52  }
[5303]53
[9852]54  /**
55   * @brief Registers a new Type to the ResourceManager.
56   * @param type the Type to register.
57   */
[9836]58  void ResourceManager::registerType(Resources::Type* type)
[9791]59  {
[9845]60    this->_resourceTypes.push_back(type);
61    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
[9791]62  }
63
[9852]64  /**
65   * @brief Unregisters a new Type to the ResourceManager.
66   * @param type the Type to unregister.
67   */
[9836]68  void ResourceManager::unregisterType(Resources::Type* type)
[9799]69  {
[9800]70    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
71    if (it != this->_resourceTypes.end())
72    {
73      this->_resourceTypes.erase(it);
[9845]74      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
[9800]75    }
[9799]76  }
77
[9852]78  /**
79   * @brief Sets the main Global path (the main path Resources are searched for)
80   * @param directory the directory to set.
81   * @see Resource::locateFile
82   */
[9836]83  void ResourceManager::setMainGlobalPath(const Directory& directory)
[9800]84  {
85    this->_mainGlobalPath = directory;
86    this->_mainGlobalPath.open();
87  }
[9791]88
[9852]89  /**
90   * @brief add a Global search path. (global paths besided the main path.)
91   * @param directory a directory to add.
92   */
[9836]93  void ResourceManager::addGlobalPath(const Directory& directory)
[9800]94  {
95    std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory);
96    if (it == this->_globalPaths.end())
97      this->_globalPaths.push_back(directory);
98  }
[9791]99
[9852]100  /**
101   * @brief add a ResourcePath to a Type's Paths.
102   * @param resourceName the Type's name of Resource to add the path to.
103   * @param pathName pathName the Name of the path to add.
104   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
105   */
[9836]106  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
[9800]107  {
108    std::vector<Resources::Type*>::iterator it;
109    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
110      if (*(*it) == resourceName)
111        return (*it)->addResourcePath(pathName);
112    PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
113    return false;
114  }
[9791]115
[9852]116  /**
117   * @brief add a ResourcePath to a Type's SubPaths.
118   * @param resourceName the Type's name of Resource to add the subpath to.
119   * @param pathName pathName the Name of the path to add.
120   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
121   */
[9836]122  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
[9800]123  {
124    std::vector<Resources::Type*>::iterator it;
125    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
126      if (*(*it) == resourceName)
127        return (*it)->addResourceSubPath(pathName);
128    PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
129    return false;
130  }
[9791]131
[9852]132  /**
133   * @brief checks wether a File is inside of the MainPath.
134   * @param fileInside the file to check
135   * @return true if the file is inside.
136   */
[9836]137  bool ResourceManager::checkFileInMainPath(const File& fileInside)
[9800]138  {
139    return (this->_mainGlobalPath + fileInside).exists();
140  }
[9795]141
[9852]142  /**
143   * @brief prepends the fileName by the MainGlobalPath (same as mainGlobalPath + '/' + fileName).
144   * @param fileName The FileName to prepend
145   * @returns the prepended file-name
146   */
[9836]147  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
[9833]148  {
149    return (this->_mainGlobalPath + File(fileName)).name();
150  }
[9795]151
[9852]152  /**
153   * @brief add a KeepLevelName (this function just counts upwards).
154   * @param keepLevelName the Name of the KeepLevel to set.
155   * @returns the Level the Name was set to.
156   */
[9836]157  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
[9800]158  {
159    this->_keepLevelNames.push_back(keepLevelName);
160    return _keepLevelNames.size()-1;
161  }
[9798]162
[9852]163  /**
164   * @param keepLevelName the Name of the KeepLevel.
165   * @returns the ID of the KeepLevel named keepLevelName
166   */
[9836]167  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
[9800]168  {
169    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
170      if (this->_keepLevelNames[i] == keepLevelName)
171        return i;
[9798]172
[9800]173    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
174    return 0;
175  }
[9798]176
[9852]177  /**
178   * @param keepLevelID the ID to check.
179   * @return the name of the KeepLevel.
180   */
[9836]181  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
[9800]182  {
183    assert(keepLevelID < this->_keepLevelNames.size());
184    return this->_keepLevelNames[keepLevelID];
185  }
[9798]186
[9852]187
188  /**
189   * @brief loads a Resource from a TypeName and a loadString.
190   * @param resourceTypeName The Name of the Type to what to load a Resource from.
191   * @param loadString the loadString to load in the Type.
192   */
[9854]193  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel)
[9848]194  {
195    std::vector<Resources::Type*>::const_iterator it;
196    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
197    {
198      if (*(*it) == resourceTypeName)
199      {
[9854]200        (*it)->createFromString(loadString, keepLevel);
[9848]201        /// TODO check if the resource was allocated!!
202        return ;
203      }
204    }
205    return ;
206  }
[9798]207
[9852]208  /**
209   * @brief unloads all Resources below a certain threshhold.
210   * @param keepLevel the KeepLevel below which to erase.
211   *
212   * @not Resources will only be erased, if th keepLevel is below, and the resources are not
213   * referenced anymore.
214   */
[9845]215  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
216  {
217    std::vector<Resources::Type*>::const_iterator it;
218    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
219    {
220      (*it)->unloadAllBelowKeepLevel(keepLevel);
221    }
222  }
223
224
[9800]225  /**
[9836]226   * @brief outputs debug information about the ResourceManager
[9800]227   */
[9836]228  void ResourceManager::debug() const
[9798]229  {
[9853]230    PRINT(0)("/==RM================================\\\n");
231    PRINT(0)("| RESOURCE-MANAGER DEBUG INFORMATION |\n");
232    PRINT(0)("\\====================================/\n");
[9800]233    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
234    if(!this->_globalPaths.empty())
235    {
236      PRINT(0)(" Additional Global search Paths are: ");
237      for (unsigned int i = 0; i < this->_globalPaths.size(); ++i)
238        PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str());
239      PRINT(0)("\n");
240    }
241    PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size());
242    std::vector<Resources::Type*>::const_iterator it;
243    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
244    {
245      (*it)->debug();
246      if (it != --this->_resourceTypes.end())
247        PRINT(0)(" ------------------------------------\n ");
248    }
249
250    PRINT(0)("KeepLevels are: ");
251    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
252      PRINT(0)("%d:'%s'  ", i, this->_keepLevelNames[i].c_str());
[9798]253    PRINT(0)("\n");
[9853]254    PRINT(0)("=================================RM==/\n");
[9798]255  }
[3676]256}
Note: See TracBrowser for help on using the repository browser.