Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/resource_manager.cc @ 9954

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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
[9869]18#include "resource_manager.h"
[4642]19#include "debug.h"
20
[6222]21#include <algorithm>
[9869]22#include <cassert>
[6222]23
[1853]24
[9869]25namespace Resources
[3365]26{
[9869]27  /// Definition of the ResourceManager's ObjectList.
28  ObjectListDefinition(ResourceManager);
29  //! Singleton Reference to the ResourceManager
30  ResourceManager* ResourceManager::_singletonRef = NULL;
[4597]31
[1853]32
[9869]33  /**
34   * @brief standard constructor
35  */
36  ResourceManager::ResourceManager ()
37  : _defaultKeepLevel(0)
38  {
39    this->registerObject(this, ResourceManager::_objectList);
40    this->setName("ResourceManager");
41    this->_mainGlobalPath = Directory("./");
42  }
[3655]43
[5303]44
[9869]45  /**
46   * @brief standard destructor
47  */
48  ResourceManager::~ResourceManager ()
[5480]49  {
[9869]50    this->unloadAllBelowKeepLevel(this->_keepLevelNames.size());
51    ResourceManager::_singletonRef = NULL;
52  }
[7661]53
[9869]54  /**
55   * @brief Registers a new Type to the ResourceManager.
56   * @param type the Type to register.
57   */
58  void ResourceManager::registerType(Resources::Type* type)
[5480]59  {
[9869]60    this->_resourceTypes.push_back(type);
61    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
[5480]62  }
63
[9869]64  /**
65   * @brief Unregisters a new Type to the ResourceManager.
66   * @param type the Type to unregister.
67   */
68  void ResourceManager::unregisterType(Resources::Type* type)
[5480]69  {
[9869]70    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
71    if (it != this->_resourceTypes.end())
[3655]72    {
[9869]73      this->_resourceTypes.erase(it);
74      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
[5480]75    }
76  }
[1853]77
[9869]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   */
83  void ResourceManager::setMainGlobalPath(const Directory& directory)
[6640]84  {
[9869]85    this->_mainGlobalPath = directory;
86    this->_mainGlobalPath.open();
[6640]87  }
[4597]88
[9869]89  /**
90   * @brief add a Global search path. (global paths besided the main path.)
91   * @param directory a directory to add.
92   */
93  void ResourceManager::addGlobalPath(const Directory& directory)
[5335]94  {
[9869]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);
[5335]98  }
[9869]99
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   */
106  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
[6640]107  {
[9869]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());
[6640]113    return false;
114  }
[3658]115
[9869]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   */
122  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
[6650]123  {
[9869]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;
[6650]130  }
[6641]131
[9869]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   */
137  bool ResourceManager::checkFileInMainPath(const File& fileInside)
[6651]138  {
[9869]139    return (this->_mainGlobalPath + fileInside).exists();
[6651]140  }
[6641]141
[9869]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   */
147  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
[6648]148  {
[9869]149    return (this->_mainGlobalPath + File(fileName)).name();
[6648]150  }
151
[9869]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   */
157  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
[6640]158  {
[9869]159    this->_keepLevelNames.push_back(keepLevelName);
160    return _keepLevelNames.size()-1;
[6640]161  }
162
[9869]163  /**
164   * @param keepLevelName the Name of the KeepLevel.
165   * @returns the ID of the KeepLevel named keepLevelName
166   */
167  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
[6640]168  {
[9869]169    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
170      if (this->_keepLevelNames[i] == keepLevelName)
171        return i;
[3658]172
[9869]173    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
174    return 0;
[5366]175  }
[3660]176
[9869]177  /**
178   * @param keepLevelID the ID to check.
179   * @return the name of the KeepLevel.
180   */
181  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
[6640]182  {
[9869]183    assert(keepLevelID < this->_keepLevelNames.size());
184    return this->_keepLevelNames[keepLevelID];
[6640]185  }
[3655]186
[3660]187
[9869]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   */
193  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel)
[6222]194  {
[9869]195    std::vector<Resources::Type*>::const_iterator it;
196    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
[3660]197    {
[9869]198      if (*(*it) == resourceTypeName)
[6640]199      {
[9869]200        (*it)->createFromString(loadString, keepLevel);
201        /// TODO check if the resource was allocated!!
202        return ;
[6640]203      }
204    }
[9869]205    return ;
[6222]206  }
[3660]207
[9869]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   */
215  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
[6222]216  {
[9869]217    std::vector<Resources::Type*>::const_iterator it;
218    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
[6640]219    {
[9869]220      (*it)->unloadAllBelowKeepLevel(keepLevel);
[3658]221    }
[6640]222  }
[3658]223
224
[9869]225  /**
226   * @brief outputs debug information about the ResourceManager
227   */
228  void ResourceManager::debug() const
[6648]229  {
[9869]230    PRINT(0)("/==RM================================\\\n");
231    PRINT(0)("| RESOURCE-MANAGER DEBUG INFORMATION |\n");
232    PRINT(0)("\\====================================/\n");
233    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
234    if(!this->_globalPaths.empty())
[3676]235    {
[9869]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");
[3676]240    }
[9869]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());
253    PRINT(0)("\n");
254    PRINT(0)("=================================RM==/\n");
[6640]255  }
[3676]256}
Note: See TracBrowser for help on using the repository browser.