Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/util/loading/resource_manager.cc

Last change on this file 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
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: Patrick Boenzli
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
17
18#include "resource_manager.h"
19#include "debug.h"
20
21#include <algorithm>
22#include <cassert>
23
24
25namespace Resources
26{
27  /// Definition of the ResourceManager's ObjectList.
28  ObjectListDefinition(ResourceManager);
29  //! Singleton Reference to the ResourceManager
30  ResourceManager* ResourceManager::_singletonRef = NULL;
31
32
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  }
43
44
45  /**
46   * @brief standard destructor
47  */
48  ResourceManager::~ResourceManager ()
49  {
50    this->unloadAllBelowKeepLevel(this->_keepLevelNames.size());
51    ResourceManager::_singletonRef = NULL;
52  }
53
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)
59  {
60    this->_resourceTypes.push_back(type);
61    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
62  }
63
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)
69  {
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);
74      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
75    }
76  }
77
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)
84  {
85    this->_mainGlobalPath = directory;
86    this->_mainGlobalPath.open();
87  }
88
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)
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  }
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)
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  }
115
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)
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  }
131
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)
138  {
139    return (this->_mainGlobalPath + fileInside).exists();
140  }
141
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)
148  {
149    return (this->_mainGlobalPath + File(fileName)).name();
150  }
151
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)
158  {
159    this->_keepLevelNames.push_back(keepLevelName);
160    return _keepLevelNames.size()-1;
161  }
162
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
168  {
169    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
170      if (this->_keepLevelNames[i] == keepLevelName)
171        return i;
172
173    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
174    return 0;
175  }
176
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
182  {
183    assert(keepLevelID < this->_keepLevelNames.size());
184    return this->_keepLevelNames[keepLevelID];
185  }
186
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   */
193  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel)
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      {
200        (*it)->createFromString(loadString, keepLevel);
201        /// TODO check if the resource was allocated!!
202        return ;
203      }
204    }
205    return ;
206  }
207
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)
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
225  /**
226   * @brief outputs debug information about the ResourceManager
227   */
228  void ResourceManager::debug() const
229  {
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())
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());
253    PRINT(0)("\n");
254    PRINT(0)("=================================RM==/\n");
255  }
256}
Note: See TracBrowser for help on using the repository browser.