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