Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed NewResource to Resource

File size: 5.4 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  ObjectListDefinition(Resource);
27
28
29  /**
30   * standard constructor
31  */
32  Resource::Resource (Type* type)
33      : _pointer(NULL), _type(type)
34  {
35    this->registerObject(this, Resource::_objectList);
36  }
37
38  /**
39   * standard deconstructor
40   */
41  Resource::~Resource ()
42  {
43    // delete what has to be deleted here
44  }
45
46
47  std::string Resource::locateFile(const std::string& fileName) const
48  {
49    if ((ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() )
50      return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
51
52    std::string locatedFile;
53    locatedFile = locateFileInSubDir(ResourceManager::getInstance()->mainGlobalPath(), fileName);
54    if (!locatedFile.empty())
55    {
56      return locatedFile;
57    }
58
59    if (File(fileName).exists())
60      return fileName;
61
62    return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
63  }
64
65  /**
66   * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists.
67   * @param directory the directory to in what to search for all subdirectories for.
68   * @param fileName the Name of the File to query for
69   * @return true on success.
70   */
71  std::string Resource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const
72  {
73    std::vector<Directory>::const_iterator it;
74    for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it)
75    {
76      Directory dir = directory + (*it);
77      File file = dir + File(fileName);
78      if ((dir+ File(fileName)).exists())
79        return (dir+File(fileName)).name();
80    }
81    return "";
82  }
83
84
85
86  StorePointer* Resource::acquireResource(const std::string& loadString)
87  {
88    //const Type* const type = _resourceTypes[this->_type->id()];
89
90    for (unsigned int i = 0; i < _type->storedResources().size(); ++i)
91    {
92      if (_type->storedResources()[i]->loadString() == loadString)
93        return _type->storedResources()[i];
94    }
95
96    return NULL;
97  }
98
99
100  void Resource::addResource(StorePointer* pointer)
101  {
102    this->_type->addResource(pointer);
103  }
104
105
106
107
108
109
110  ///////////////////
111  //// KEEPLEVEL ////
112  ///////////////////
113  KeepLevel::KeepLevel(const std::string& keepLevelName)
114  {
115    this->_keepLevel = ResourceManager::getInstance()->getKeepLevelID(keepLevelName);
116  }
117
118  const std::string& KeepLevel::name() const
119  {
120    return ResourceManager::getInstance()->getKeepLevelName(this->_keepLevel);
121  }
122
123
124
125  ///////////////////////
126  //// STORE POINTER ////
127  ///////////////////////
128  StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel)
129      : _loadString(loadString), _keepLevel(keeplevel)
130  {}
131
132
133
134
135  //////////////
136  //// TYPE ////
137  //////////////
138  Type::Type(const std::string& typeName)
139      : _typeName(typeName)
140  {
141    ResourceManager::getInstance()->registerType(this);
142    PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str());
143  }
144
145  Type::~Type()
146  {
147    ResourceManager::getInstance()->unregisterType(this);
148  }
149
150  void Type::addResource(StorePointer* resource)
151  {
152    this->_storedResources.push_back(resource);
153  }
154
155  bool Type::addResourcePath(const std::string& path)
156  {
157    std::vector<Directory>::const_iterator it;
158    for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it)
159      if ((*it) == path)
160        return false;
161    this->_resourcePaths.push_back(path);
162    return true;
163
164  }
165
166  bool Type::addResourceSubPath(const std::string& subPath)
167  {
168    std::vector<Directory>::const_iterator it;
169    for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it)
170      if ((*it) == subPath)
171        return false;
172    this->_resourceSubPaths.push_back(subPath);
173    return true;
174  }
175
176  void Type::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
177  {
178    std::vector<Resources::StorePointer*>::iterator it;
179    for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it)
180      if((*it)->keepLevel() < keepLevel && (*it)->last())
181    {
182      delete (*it);
183      this->_storedResources.erase(it);
184      it = this->_storedResources.begin();
185    }
186  }
187
188
189  void Type::debug() const
190  {
191    PRINT(0)(" ResourceType '%s' stores %d Resources\n", this->_typeName.c_str(), this->_storedResources.size());
192    PRINT(0)("  Paths:\n");
193    for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i)
194      PRINT(0)("    %s\n", this->_resourcePaths[i].name().c_str());
195    PRINT(0)("  Sub-Paths:");
196    for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i)
197      PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str());
198    PRINT(0)("\n");
199
200    PRINT(0)("  Loaded Resources:\n");
201    std::vector<Resources::StorePointer*>::const_iterator it;
202    for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it)
203      PRINT(0)("    '%s' : KeepLevel '%s'\n", (*it)->loadString().c_str(), (*it)->keepLevel().name().c_str());
204  }
205}
Note: See TracBrowser for help on using the repository browser.