[4597] | 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: |
---|
[3655] | 12 | main-programmer: Benjamin Grauer |
---|
[3672] | 13 | co-programmer: Patrick Boenzli |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3655] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
[1853] | 17 | |
---|
[9836] | 18 | #include "resource_manager.h" |
---|
[4642] | 19 | #include "debug.h" |
---|
| 20 | |
---|
[6222] | 21 | #include <algorithm> |
---|
[9797] | 22 | #include <cassert> |
---|
[6222] | 23 | |
---|
[1853] | 24 | |
---|
[9800] | 25 | namespace Resources |
---|
| 26 | { |
---|
[9836] | 27 | ObjectListDefinition(ResourceManager); |
---|
| 28 | //! Singleton Reference to the ResourceManager |
---|
| 29 | ResourceManager* ResourceManager::_singletonRef = NULL; |
---|
[9791] | 30 | |
---|
| 31 | |
---|
[9800] | 32 | /** |
---|
| 33 | * @brief standard constructor |
---|
| 34 | */ |
---|
[9836] | 35 | ResourceManager::ResourceManager () |
---|
[9800] | 36 | { |
---|
[9836] | 37 | this->registerObject(this, ResourceManager::_objectList); |
---|
| 38 | this->setName("ResourceManager"); |
---|
[9802] | 39 | this->_mainGlobalPath = Directory("./"); |
---|
[9800] | 40 | } |
---|
[4597] | 41 | |
---|
[1853] | 42 | |
---|
[9800] | 43 | /** |
---|
| 44 | * @brief standard destructor |
---|
| 45 | */ |
---|
[9836] | 46 | ResourceManager::~ResourceManager () |
---|
[9800] | 47 | { |
---|
| 48 | // deleting the Resources-List |
---|
| 49 | //this->unloadAllByPriority(RP_GAME); |
---|
[3655] | 50 | |
---|
[9800] | 51 | // if (!this->resourceList.empty()) |
---|
| 52 | // PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size()); |
---|
[5303] | 53 | |
---|
[9836] | 54 | ResourceManager::_singletonRef = NULL; |
---|
[9800] | 55 | } |
---|
[5303] | 56 | |
---|
[1853] | 57 | |
---|
[7661] | 58 | |
---|
[5480] | 59 | |
---|
[9836] | 60 | void ResourceManager::registerType(Resources::Type* type) |
---|
[9791] | 61 | { |
---|
[9845] | 62 | this->_resourceTypes.push_back(type); |
---|
| 63 | PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str()); |
---|
[9791] | 64 | } |
---|
| 65 | |
---|
[9836] | 66 | void ResourceManager::unregisterType(Resources::Type* type) |
---|
[9799] | 67 | { |
---|
[9800] | 68 | std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type); |
---|
| 69 | if (it != this->_resourceTypes.end()) |
---|
| 70 | { |
---|
| 71 | this->_resourceTypes.erase(it); |
---|
[9845] | 72 | PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str()); |
---|
[9800] | 73 | } |
---|
[9799] | 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
[9836] | 77 | void ResourceManager::setMainGlobalPath(const Directory& directory) |
---|
[9800] | 78 | { |
---|
| 79 | this->_mainGlobalPath = directory; |
---|
| 80 | this->_mainGlobalPath.open(); |
---|
| 81 | } |
---|
[9791] | 82 | |
---|
[9836] | 83 | void ResourceManager::addGlobalPath(const Directory& directory) |
---|
[9800] | 84 | { |
---|
| 85 | std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory); |
---|
| 86 | if (it == this->_globalPaths.end()) |
---|
| 87 | this->_globalPaths.push_back(directory); |
---|
| 88 | } |
---|
[9791] | 89 | |
---|
| 90 | |
---|
[9836] | 91 | bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName) |
---|
[9800] | 92 | { |
---|
| 93 | std::vector<Resources::Type*>::iterator it; |
---|
| 94 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
| 95 | if (*(*it) == resourceName) |
---|
| 96 | return (*it)->addResourcePath(pathName); |
---|
| 97 | PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str()); |
---|
| 98 | return false; |
---|
| 99 | } |
---|
[9791] | 100 | |
---|
[9836] | 101 | bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName) |
---|
[9800] | 102 | { |
---|
| 103 | std::vector<Resources::Type*>::iterator it; |
---|
| 104 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
| 105 | if (*(*it) == resourceName) |
---|
| 106 | return (*it)->addResourceSubPath(pathName); |
---|
| 107 | PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str()); |
---|
| 108 | return false; |
---|
| 109 | } |
---|
[9791] | 110 | |
---|
| 111 | |
---|
[9795] | 112 | |
---|
[9836] | 113 | bool ResourceManager::checkFileInMainPath(const File& fileInside) |
---|
[9800] | 114 | { |
---|
| 115 | return (this->_mainGlobalPath + fileInside).exists(); |
---|
| 116 | } |
---|
[9795] | 117 | |
---|
[9836] | 118 | std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName) |
---|
[9833] | 119 | { |
---|
| 120 | return (this->_mainGlobalPath + File(fileName)).name(); |
---|
| 121 | } |
---|
[9795] | 122 | |
---|
[9798] | 123 | |
---|
[9836] | 124 | unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName) |
---|
[9800] | 125 | { |
---|
| 126 | this->_keepLevelNames.push_back(keepLevelName); |
---|
| 127 | return _keepLevelNames.size()-1; |
---|
| 128 | } |
---|
[9798] | 129 | |
---|
[9836] | 130 | unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const |
---|
[9800] | 131 | { |
---|
| 132 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
| 133 | if (this->_keepLevelNames[i] == keepLevelName) |
---|
| 134 | return i; |
---|
[9798] | 135 | |
---|
[9800] | 136 | PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str()); |
---|
| 137 | return 0; |
---|
| 138 | } |
---|
[9798] | 139 | |
---|
[9836] | 140 | const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const |
---|
[9800] | 141 | { |
---|
| 142 | assert(keepLevelID < this->_keepLevelNames.size()); |
---|
| 143 | return this->_keepLevelNames[keepLevelID]; |
---|
| 144 | } |
---|
[9798] | 145 | |
---|
[9848] | 146 | void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString) |
---|
| 147 | { |
---|
| 148 | std::vector<Resources::Type*>::const_iterator it; |
---|
| 149 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
| 150 | { |
---|
| 151 | if (*(*it) == resourceTypeName) |
---|
| 152 | { |
---|
| 153 | (*it)->createFromString(loadString); |
---|
| 154 | /// TODO check if the resource was allocated!! |
---|
| 155 | return ; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | return ; |
---|
| 159 | } |
---|
[9798] | 160 | |
---|
[9848] | 161 | |
---|
[9845] | 162 | void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel) |
---|
| 163 | { |
---|
| 164 | std::vector<Resources::Type*>::const_iterator it; |
---|
| 165 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
| 166 | { |
---|
| 167 | (*it)->unloadAllBelowKeepLevel(keepLevel); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
[9800] | 172 | /** |
---|
[9836] | 173 | * @brief outputs debug information about the ResourceManager |
---|
[9800] | 174 | */ |
---|
[9836] | 175 | void ResourceManager::debug() const |
---|
[9798] | 176 | { |
---|
[9800] | 177 | PRINT(0)("=RM===================================\n"); |
---|
| 178 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
| 179 | PRINT(0)("======================================\n"); |
---|
| 180 | PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str()); |
---|
| 181 | if(!this->_globalPaths.empty()) |
---|
| 182 | { |
---|
| 183 | PRINT(0)(" Additional Global search Paths are: "); |
---|
| 184 | for (unsigned int i = 0; i < this->_globalPaths.size(); ++i) |
---|
| 185 | PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str()); |
---|
| 186 | PRINT(0)("\n"); |
---|
| 187 | } |
---|
| 188 | PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size()); |
---|
| 189 | std::vector<Resources::Type*>::const_iterator it; |
---|
| 190 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
| 191 | { |
---|
| 192 | (*it)->debug(); |
---|
| 193 | if (it != --this->_resourceTypes.end()) |
---|
| 194 | PRINT(0)(" ------------------------------------\n "); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | PRINT(0)("KeepLevels are: "); |
---|
| 198 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
| 199 | PRINT(0)("%d:'%s' ", i, this->_keepLevelNames[i].c_str()); |
---|
[9798] | 200 | PRINT(0)("\n"); |
---|
[9800] | 201 | PRINT(0)("==================================RM==\n"); |
---|
[9798] | 202 | } |
---|
[3676] | 203 | } |
---|