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: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
17 | |
---|
18 | #include "resource_manager.h" |
---|
19 | #include "debug.h" |
---|
20 | |
---|
21 | #include <algorithm> |
---|
22 | #include <cassert> |
---|
23 | |
---|
24 | |
---|
25 | namespace Resources |
---|
26 | { |
---|
27 | ObjectListDefinition(ResourceManager); |
---|
28 | //! Singleton Reference to the ResourceManager |
---|
29 | ResourceManager* ResourceManager::_singletonRef = NULL; |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * @brief standard constructor |
---|
34 | */ |
---|
35 | ResourceManager::ResourceManager () |
---|
36 | { |
---|
37 | this->registerObject(this, ResourceManager::_objectList); |
---|
38 | this->setName("ResourceManager"); |
---|
39 | this->_mainGlobalPath = Directory("./"); |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | * @brief standard destructor |
---|
45 | */ |
---|
46 | ResourceManager::~ResourceManager () |
---|
47 | { |
---|
48 | // deleting the Resources-List |
---|
49 | //this->unloadAllByPriority(RP_GAME); |
---|
50 | |
---|
51 | // if (!this->resourceList.empty()) |
---|
52 | // PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size()); |
---|
53 | |
---|
54 | ResourceManager::_singletonRef = NULL; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | void ResourceManager::registerType(Resources::Type* type) |
---|
61 | { |
---|
62 | this->_resourceTypes.push_back(type); |
---|
63 | PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str()); |
---|
64 | } |
---|
65 | |
---|
66 | void ResourceManager::unregisterType(Resources::Type* type) |
---|
67 | { |
---|
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); |
---|
72 | PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str()); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void ResourceManager::setMainGlobalPath(const Directory& directory) |
---|
78 | { |
---|
79 | this->_mainGlobalPath = directory; |
---|
80 | this->_mainGlobalPath.open(); |
---|
81 | } |
---|
82 | |
---|
83 | void ResourceManager::addGlobalPath(const Directory& directory) |
---|
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 | } |
---|
89 | |
---|
90 | |
---|
91 | bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName) |
---|
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 | } |
---|
100 | |
---|
101 | bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName) |
---|
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 | } |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | bool ResourceManager::checkFileInMainPath(const File& fileInside) |
---|
114 | { |
---|
115 | return (this->_mainGlobalPath + fileInside).exists(); |
---|
116 | } |
---|
117 | |
---|
118 | std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName) |
---|
119 | { |
---|
120 | return (this->_mainGlobalPath + File(fileName)).name(); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName) |
---|
125 | { |
---|
126 | this->_keepLevelNames.push_back(keepLevelName); |
---|
127 | return _keepLevelNames.size()-1; |
---|
128 | } |
---|
129 | |
---|
130 | unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const |
---|
131 | { |
---|
132 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
133 | if (this->_keepLevelNames[i] == keepLevelName) |
---|
134 | return i; |
---|
135 | |
---|
136 | PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str()); |
---|
137 | return 0; |
---|
138 | } |
---|
139 | |
---|
140 | const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const |
---|
141 | { |
---|
142 | assert(keepLevelID < this->_keepLevelNames.size()); |
---|
143 | return this->_keepLevelNames[keepLevelID]; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel) |
---|
148 | { |
---|
149 | std::vector<Resources::Type*>::const_iterator it; |
---|
150 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
151 | { |
---|
152 | (*it)->unloadAllBelowKeepLevel(keepLevel); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief outputs debug information about the ResourceManager |
---|
159 | */ |
---|
160 | void ResourceManager::debug() const |
---|
161 | { |
---|
162 | PRINT(0)("=RM===================================\n"); |
---|
163 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
164 | PRINT(0)("======================================\n"); |
---|
165 | PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str()); |
---|
166 | if(!this->_globalPaths.empty()) |
---|
167 | { |
---|
168 | PRINT(0)(" Additional Global search Paths are: "); |
---|
169 | for (unsigned int i = 0; i < this->_globalPaths.size(); ++i) |
---|
170 | PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str()); |
---|
171 | PRINT(0)("\n"); |
---|
172 | } |
---|
173 | PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size()); |
---|
174 | std::vector<Resources::Type*>::const_iterator it; |
---|
175 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
176 | { |
---|
177 | (*it)->debug(); |
---|
178 | if (it != --this->_resourceTypes.end()) |
---|
179 | PRINT(0)(" ------------------------------------\n "); |
---|
180 | } |
---|
181 | |
---|
182 | PRINT(0)("KeepLevels are: "); |
---|
183 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
184 | PRINT(0)("%d:'%s' ", i, this->_keepLevelNames[i].c_str()); |
---|
185 | PRINT(0)("\n"); |
---|
186 | PRINT(0)("==================================RM==\n"); |
---|
187 | } |
---|
188 | } |
---|