Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource.cc @ 9851

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

orxonox/new_class_id: Resources now get Loaded and Unloaded correctly by will alone

File size: 8.0 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: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
17
18#include "resource.h"
19#include "resource_manager.h"
20
21#include "debug.h"
22
23
24namespace Resources
25{
26  //! Define an ObjectList for the Resources
27  ObjectListDefinition(Resource);
28
29
30  /**
31   * @brief standard constructor
32   * @param type the Type this resource belongs to.
33   */
34  Resource::Resource (Type* type)
35      : _pointer(NULL), _type(type)
36  {
37    this->registerObject(this, Resource::_objectList);
38  }
39
40  /**
41   * @brief standard deconstructor
42   */
43  Resource::~Resource ()
44  {
45    // delete what has to be deleted here
46  }
47
48  /**
49   * @brief Locates a File inside of the Resources Paths and returns the appended path.
50   *
51   * @param fileName the Name of the file to look for.
52   * @returns the Name of the File prepended with the PAth it is in, if found, empty String ("") otherwise.
53   *
54   * This Function searches in (ordered):
55   * 1. mainGlobalPath (from ResourceManger)
56   * 2. all of the global Paths (from ResourceManger)
57   * 3. all of the Resources Paths (from Resources::Type)
58   *
59   * in each of these directory, first "./" is searched, and afterwards all of the subDirs (from Resources::Type) are searched.
60   *
61   * @todo finish it!!
62   */
63  std::string Resource::locateFile(const std::string& fileName) const
64  {
65    if ((ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() )
66      return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
67
68    std::string locatedFile;
69    locatedFile = locateFileInSubDir(ResourceManager::getInstance()->mainGlobalPath(), fileName);
70    if (!locatedFile.empty())
71    {
72      return locatedFile;
73    }
74
75    if (File(fileName).exists())
76      return fileName;
77
78    return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
79  }
80
81  /**
82   * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists.
83   * @param directory the directory to in what to search for all subdirectories for.
84   * @param fileName the Name of the File to query for
85   * @return true on success.
86   */
87  std::string Resource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const
88  {
89    std::vector<Directory>::const_iterator it;
90    for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it)
91    {
92      Directory dir = directory + (*it);
93      File file = dir + File(fileName);
94      if ((dir+ File(fileName)).exists())
95        return (dir+File(fileName)).name();
96    }
97    return "";
98  }
99
100
101  /**
102   * @param loadString the Identifier of the Resource.
103   * @returns a Store-Pointer to the Resource if found, NULL otherwise
104   */
105  StorePointer* Resource::acquireResource(const std::string& loadString)
106  {
107    //const Type* const type = _resourceTypes[this->_type->id()];
108
109    for (unsigned int i = 0; i < _type->storedResources().size(); ++i)
110    {
111      if (_type->storedResources()[i]->loadString() == loadString)
112        return _type->storedResources()[i];
113    }
114
115    return NULL;
116  }
117
118  /**
119   * @brief registers a StorePointer to a Resource's Type.
120   * @param pointer the StorePointer to register.
121   */
122  void Resource::addResource(StorePointer* pointer)
123  {
124    assert(pointer != NULL);
125    this->_type->addResource(pointer);
126  }
127
128
129
130
131
132
133  ///////////////////
134  //// KEEPLEVEL ////
135  ///////////////////
136  /**
137   * @brief constructor of a KeepLevel.
138   * @param keepLevelName the Name of the KeepLevel. Must be one Name of the defined Names in the ResourceManager.
139   *
140   * @note the Name is transformed into an Integer for fast interpretation.
141   */
142  KeepLevel::KeepLevel(const std::string& keepLevelName)
143  {
144    this->_keepLevel = ResourceManager::getInstance()->getKeepLevelID(keepLevelName);
145  }
146
147  /**
148   * @returns the name of the KeepLevel.
149   */
150  const std::string& KeepLevel::name() const
151  {
152    return ResourceManager::getInstance()->getKeepLevelName(this->_keepLevel);
153  }
154
155
156
157  ///////////////////////
158  //// STORE POINTER ////
159  ///////////////////////
160  /**
161   * @brief allocates a StorePointer.
162   * @param loadString An identifier String that is unique between all resources of this type.
163   * @param keepLevel the KeepLevel at wich to keep this resource.
164   */
165  StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel)
166      : _loadString(loadString), _keepLevel(keeplevel)
167  {
168    PRINTF(4)("Acquired a Resource with LoadString '%s' and KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str());
169  }
170
171  StorePointer::~StorePointer()
172  {
173    PRINTF(4)("Deleting Stored Resource '%s' from KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str());
174  };
175
176
177
178  //////////////
179  //// TYPE ////
180  //////////////
181  /**
182   * @brief allocates a Type.
183   * @param typeName the Name of the Type to be stored in this Container.
184   */
185  Type::Type(const std::string& typeName)
186      : _typeName(typeName)
187  {
188    ResourceManager::getInstance()->registerType(this);
189    PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str());
190  }
191
192  //! Destructs a Type.
193  Type::~Type()
194  {
195    ResourceManager::getInstance()->unregisterType(this);
196  }
197
198  /**
199   * @brief adds a Resource to this Resource's type.
200   * @param resource the Resource to add.
201   */
202  void Type::addResource(StorePointer* resource)
203  {
204    this->_storedResources.push_back(resource);
205  }
206
207  /**
208   * @brief adds a Path to the Type's resource-paths.
209   * @param path the path-name to add.
210   */
211  bool Type::addResourcePath(const std::string& path)
212  {
213    std::vector<Directory>::const_iterator it;
214    for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it)
215      if ((*it) == path)
216        return false;
217    this->_resourcePaths.push_back(path);
218    return true;
219
220  }
221
222  /**
223   * @brief Adds a SubPath to the Type's resource-subpaths.
224   * @param subPath the subpath to add.
225   */
226  bool Type::addResourceSubPath(const std::string& subPath)
227  {
228    std::vector<Directory>::const_iterator it;
229    for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it)
230      if ((*it) == subPath)
231        return false;
232    this->_resourceSubPaths.push_back(subPath);
233    return true;
234  }
235
236  /**
237   * @brief Unloads all Resources below a certain Level.
238   * @param keepLevel the KeepLevel at what to remove the Resources from.
239   */
240  void Type::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
241  {
242    std::vector<Resources::StorePointer*>::iterator it, it2;
243    bool finished = false;
244
245    while (!finished)
246    {
247      finished = true;
248      for (it = this->_storedResources.begin(); it != this->_storedResources.end();++it)
249        if((*it)->keepLevel() < keepLevel && (*it)->last())
250        {
251          delete (*it);
252          this->_storedResources.erase(it);
253          finished = false;
254          break;
255        }
256    }
257  }
258
259  /**
260   * @brief print out some nice Debug information in a beatifully designed style
261   */
262  void Type::debug() const
263  {
264    PRINT(0)(" ResourceType '%s' stores %d Resources\n", this->_typeName.c_str(), this->_storedResources.size());
265    PRINT(0)("  Paths:\n");
266    for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i)
267      PRINT(0)("    %s\n", this->_resourcePaths[i].name().c_str());
268    PRINT(0)("  Sub-Paths:");
269    for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i)
270      PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str());
271    PRINT(0)("\n");
272
273    PRINT(0)("  Loaded Resources:\n");
274    std::vector<Resources::StorePointer*>::const_iterator it;
275    for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it)
276      PRINT(0)("    '%s' : KeepLevel '%s'\n", (*it)->loadString().c_str(), (*it)->keepLevel().name().c_str());
277  }
278}
Note: See TracBrowser for help on using the repository browser.