[4744] | 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: |
---|
[7195] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[9790] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
[1853] | 17 | |
---|
[7193] | 18 | #include "resource.h" |
---|
[9836] | 19 | #include "resource_manager.h" |
---|
[9791] | 20 | |
---|
[9790] | 21 | #include "debug.h" |
---|
[1853] | 22 | |
---|
[9785] | 23 | |
---|
[9800] | 24 | namespace Resources |
---|
[3365] | 25 | { |
---|
[9850] | 26 | //! Define an ObjectList for the Resources |
---|
[9847] | 27 | ObjectListDefinition(Resource); |
---|
[1853] | 28 | |
---|
[9783] | 29 | |
---|
[9800] | 30 | /** |
---|
[9850] | 31 | * @brief standard constructor |
---|
| 32 | * @param type the Type this resource belongs to. |
---|
| 33 | */ |
---|
[9847] | 34 | Resource::Resource (Type* type) |
---|
[9800] | 35 | : _pointer(NULL), _type(type) |
---|
| 36 | { |
---|
[9847] | 37 | this->registerObject(this, Resource::_objectList); |
---|
[9800] | 38 | } |
---|
[9783] | 39 | |
---|
[9800] | 40 | /** |
---|
[9850] | 41 | * @brief standard deconstructor |
---|
[9800] | 42 | */ |
---|
[9847] | 43 | Resource::~Resource () |
---|
[9800] | 44 | { |
---|
| 45 | // delete what has to be deleted here |
---|
| 46 | } |
---|
| 47 | |
---|
[9850] | 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 | */ |
---|
[9847] | 63 | std::string Resource::locateFile(const std::string& fileName) const |
---|
[9800] | 64 | { |
---|
[9836] | 65 | if ((ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() ) |
---|
| 66 | return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name(); |
---|
[9800] | 67 | |
---|
| 68 | std::string locatedFile; |
---|
[9836] | 69 | locatedFile = locateFileInSubDir(ResourceManager::getInstance()->mainGlobalPath(), fileName); |
---|
[9800] | 70 | if (!locatedFile.empty()) |
---|
| 71 | { |
---|
| 72 | return locatedFile; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if (File(fileName).exists()) |
---|
| 76 | return fileName; |
---|
| 77 | |
---|
[9836] | 78 | return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name(); |
---|
[9800] | 79 | } |
---|
[9783] | 80 | |
---|
[9800] | 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 | */ |
---|
[9847] | 87 | std::string Resource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const |
---|
[9793] | 88 | { |
---|
[9800] | 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 ""; |
---|
[9793] | 98 | } |
---|
[9795] | 99 | |
---|
[9799] | 100 | |
---|
[9850] | 101 | /** |
---|
| 102 | * @param loadString the Identifier of the Resource. |
---|
| 103 | * @returns a Store-Pointer to the Resource if found, NULL otherwise |
---|
| 104 | */ |
---|
[9847] | 105 | StorePointer* Resource::acquireResource(const std::string& loadString) |
---|
[9790] | 106 | { |
---|
[9800] | 107 | //const Type* const type = _resourceTypes[this->_type->id()]; |
---|
[9785] | 108 | |
---|
[9800] | 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 | } |
---|
[9785] | 114 | |
---|
[9800] | 115 | return NULL; |
---|
| 116 | } |
---|
[9790] | 117 | |
---|
[9850] | 118 | /** |
---|
| 119 | * @brief registers a StorePointer to a Resource's Type. |
---|
| 120 | * @param pointer the StorePointer to register. |
---|
| 121 | */ |
---|
[9847] | 122 | void Resource::addResource(StorePointer* pointer) |
---|
[9786] | 123 | { |
---|
[9850] | 124 | assert(pointer != NULL); |
---|
[9800] | 125 | this->_type->addResource(pointer); |
---|
[9786] | 126 | } |
---|
[9783] | 127 | |
---|
| 128 | |
---|
| 129 | |
---|
[9784] | 130 | |
---|
[9785] | 131 | |
---|
| 132 | |
---|
[9800] | 133 | /////////////////// |
---|
| 134 | //// KEEPLEVEL //// |
---|
| 135 | /////////////////// |
---|
[9854] | 136 | |
---|
| 137 | //! Constructs a default KeepLevel as Set in the ResourceManager via setDefaultKeepLevel. |
---|
| 138 | KeepLevel::KeepLevel() |
---|
| 139 | { |
---|
| 140 | this->_keepLevel = ResourceManager::getInstance()->defaultKeepLevel().keepLevel(); |
---|
| 141 | } |
---|
[9850] | 142 | /** |
---|
[9854] | 143 | * @param keepLevel the level to set. |
---|
| 144 | */ |
---|
| 145 | KeepLevel::KeepLevel(unsigned int keepLevel) |
---|
| 146 | { |
---|
| 147 | _keepLevel = keepLevel; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
[9850] | 151 | * @brief constructor of a KeepLevel. |
---|
| 152 | * @param keepLevelName the Name of the KeepLevel. Must be one Name of the defined Names in the ResourceManager. |
---|
| 153 | * |
---|
| 154 | * @note the Name is transformed into an Integer for fast interpretation. |
---|
| 155 | */ |
---|
[9800] | 156 | KeepLevel::KeepLevel(const std::string& keepLevelName) |
---|
| 157 | { |
---|
[9836] | 158 | this->_keepLevel = ResourceManager::getInstance()->getKeepLevelID(keepLevelName); |
---|
[9800] | 159 | } |
---|
[9785] | 160 | |
---|
[9850] | 161 | /** |
---|
| 162 | * @returns the name of the KeepLevel. |
---|
| 163 | */ |
---|
[9800] | 164 | const std::string& KeepLevel::name() const |
---|
| 165 | { |
---|
[9836] | 166 | return ResourceManager::getInstance()->getKeepLevelName(this->_keepLevel); |
---|
[9800] | 167 | } |
---|
[9785] | 168 | |
---|
| 169 | |
---|
[9790] | 170 | |
---|
[9800] | 171 | /////////////////////// |
---|
| 172 | //// STORE POINTER //// |
---|
| 173 | /////////////////////// |
---|
[9850] | 174 | /** |
---|
| 175 | * @brief allocates a StorePointer. |
---|
| 176 | * @param loadString An identifier String that is unique between all resources of this type. |
---|
| 177 | * @param keepLevel the KeepLevel at wich to keep this resource. |
---|
| 178 | */ |
---|
[9800] | 179 | StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel) |
---|
| 180 | : _loadString(loadString), _keepLevel(keeplevel) |
---|
[9851] | 181 | { |
---|
| 182 | PRINTF(4)("Acquired a Resource with LoadString '%s' and KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str()); |
---|
| 183 | } |
---|
[9790] | 184 | |
---|
[9851] | 185 | StorePointer::~StorePointer() |
---|
| 186 | { |
---|
| 187 | PRINTF(4)("Deleting Stored Resource '%s' from KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str()); |
---|
| 188 | }; |
---|
[9798] | 189 | |
---|
[9799] | 190 | |
---|
[9790] | 191 | |
---|
[9800] | 192 | ////////////// |
---|
| 193 | //// TYPE //// |
---|
| 194 | ////////////// |
---|
[9850] | 195 | /** |
---|
| 196 | * @brief allocates a Type. |
---|
| 197 | * @param typeName the Name of the Type to be stored in this Container. |
---|
| 198 | */ |
---|
[9800] | 199 | Type::Type(const std::string& typeName) |
---|
[9845] | 200 | : _typeName(typeName) |
---|
[9800] | 201 | { |
---|
[9836] | 202 | ResourceManager::getInstance()->registerType(this); |
---|
[9800] | 203 | PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str()); |
---|
| 204 | } |
---|
[9790] | 205 | |
---|
[9850] | 206 | //! Destructs a Type. |
---|
[9800] | 207 | Type::~Type() |
---|
| 208 | { |
---|
[9836] | 209 | ResourceManager::getInstance()->unregisterType(this); |
---|
[9800] | 210 | } |
---|
[9790] | 211 | |
---|
[9850] | 212 | /** |
---|
| 213 | * @brief adds a Resource to this Resource's type. |
---|
| 214 | * @param resource the Resource to add. |
---|
| 215 | */ |
---|
[9800] | 216 | void Type::addResource(StorePointer* resource) |
---|
| 217 | { |
---|
| 218 | this->_storedResources.push_back(resource); |
---|
| 219 | } |
---|
[9785] | 220 | |
---|
[9850] | 221 | /** |
---|
| 222 | * @brief adds a Path to the Type's resource-paths. |
---|
| 223 | * @param path the path-name to add. |
---|
| 224 | */ |
---|
[9800] | 225 | bool Type::addResourcePath(const std::string& path) |
---|
| 226 | { |
---|
| 227 | std::vector<Directory>::const_iterator it; |
---|
| 228 | for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it) |
---|
| 229 | if ((*it) == path) |
---|
| 230 | return false; |
---|
| 231 | this->_resourcePaths.push_back(path); |
---|
| 232 | return true; |
---|
[9799] | 233 | |
---|
[9800] | 234 | } |
---|
[9785] | 235 | |
---|
[9850] | 236 | /** |
---|
| 237 | * @brief Adds a SubPath to the Type's resource-subpaths. |
---|
| 238 | * @param subPath the subpath to add. |
---|
| 239 | */ |
---|
[9800] | 240 | bool Type::addResourceSubPath(const std::string& subPath) |
---|
| 241 | { |
---|
| 242 | std::vector<Directory>::const_iterator it; |
---|
| 243 | for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it) |
---|
| 244 | if ((*it) == subPath) |
---|
| 245 | return false; |
---|
| 246 | this->_resourceSubPaths.push_back(subPath); |
---|
| 247 | return true; |
---|
| 248 | } |
---|
[9785] | 249 | |
---|
[9850] | 250 | /** |
---|
| 251 | * @brief Unloads all Resources below a certain Level. |
---|
| 252 | * @param keepLevel the KeepLevel at what to remove the Resources from. |
---|
| 253 | */ |
---|
[9845] | 254 | void Type::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel) |
---|
[9800] | 255 | { |
---|
[9851] | 256 | std::vector<Resources::StorePointer*>::iterator it, it2; |
---|
| 257 | bool finished = false; |
---|
| 258 | |
---|
| 259 | while (!finished) |
---|
| 260 | { |
---|
| 261 | finished = true; |
---|
| 262 | for (it = this->_storedResources.begin(); it != this->_storedResources.end();++it) |
---|
| 263 | if((*it)->keepLevel() < keepLevel && (*it)->last()) |
---|
| 264 | { |
---|
| 265 | delete (*it); |
---|
| 266 | this->_storedResources.erase(it); |
---|
| 267 | finished = false; |
---|
| 268 | break; |
---|
| 269 | } |
---|
| 270 | } |
---|
[9800] | 271 | } |
---|
[9790] | 272 | |
---|
[9850] | 273 | /** |
---|
| 274 | * @brief print out some nice Debug information in a beatifully designed style |
---|
| 275 | */ |
---|
[9800] | 276 | void Type::debug() const |
---|
| 277 | { |
---|
[9845] | 278 | PRINT(0)(" ResourceType '%s' stores %d Resources\n", this->_typeName.c_str(), this->_storedResources.size()); |
---|
[9800] | 279 | PRINT(0)(" Paths:\n"); |
---|
| 280 | for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i) |
---|
| 281 | PRINT(0)(" %s\n", this->_resourcePaths[i].name().c_str()); |
---|
| 282 | PRINT(0)(" Sub-Paths:"); |
---|
| 283 | for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i) |
---|
| 284 | PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str()); |
---|
| 285 | PRINT(0)("\n"); |
---|
[9790] | 286 | |
---|
[9843] | 287 | PRINT(0)(" Loaded Resources:\n"); |
---|
| 288 | std::vector<Resources::StorePointer*>::const_iterator it; |
---|
| 289 | for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it) |
---|
| 290 | PRINT(0)(" '%s' : KeepLevel '%s'\n", (*it)->loadString().c_str(), (*it)->keepLevel().name().c_str()); |
---|
[9800] | 291 | } |
---|
[9785] | 292 | } |
---|