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 | /// Definition of the ResourceManager's ObjectList. |
---|
28 | ObjectListDefinition(ResourceManager); |
---|
29 | //! Singleton Reference to the ResourceManager |
---|
30 | ResourceManager* ResourceManager::_singletonRef = NULL; |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | * @brief standard constructor |
---|
35 | */ |
---|
36 | ResourceManager::ResourceManager () |
---|
37 | { |
---|
38 | this->registerObject(this, ResourceManager::_objectList); |
---|
39 | this->setName("ResourceManager"); |
---|
40 | this->_mainGlobalPath = Directory("./"); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * @brief standard destructor |
---|
46 | */ |
---|
47 | ResourceManager::~ResourceManager () |
---|
48 | { |
---|
49 | // deleting the Resources-List |
---|
50 | //this->unloadAllByPriority(RP_GAME); |
---|
51 | |
---|
52 | // if (!this->resourceList.empty()) |
---|
53 | // PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size()); |
---|
54 | |
---|
55 | ResourceManager::_singletonRef = NULL; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief Registers a new Type to the ResourceManager. |
---|
60 | * @param type the Type to register. |
---|
61 | */ |
---|
62 | void ResourceManager::registerType(Resources::Type* type) |
---|
63 | { |
---|
64 | this->_resourceTypes.push_back(type); |
---|
65 | PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str()); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * @brief Unregisters a new Type to the ResourceManager. |
---|
70 | * @param type the Type to unregister. |
---|
71 | */ |
---|
72 | void ResourceManager::unregisterType(Resources::Type* type) |
---|
73 | { |
---|
74 | std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type); |
---|
75 | if (it != this->_resourceTypes.end()) |
---|
76 | { |
---|
77 | this->_resourceTypes.erase(it); |
---|
78 | PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str()); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief Sets the main Global path (the main path Resources are searched for) |
---|
84 | * @param directory the directory to set. |
---|
85 | * @see Resource::locateFile |
---|
86 | */ |
---|
87 | void ResourceManager::setMainGlobalPath(const Directory& directory) |
---|
88 | { |
---|
89 | this->_mainGlobalPath = directory; |
---|
90 | this->_mainGlobalPath.open(); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * @brief add a Global search path. (global paths besided the main path.) |
---|
95 | * @param directory a directory to add. |
---|
96 | */ |
---|
97 | void ResourceManager::addGlobalPath(const Directory& directory) |
---|
98 | { |
---|
99 | std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory); |
---|
100 | if (it == this->_globalPaths.end()) |
---|
101 | this->_globalPaths.push_back(directory); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief add a ResourcePath to a Type's Paths. |
---|
106 | * @param resourceName the Type's name of Resource to add the path to. |
---|
107 | * @param pathName pathName the Name of the path to add. |
---|
108 | * @return true on success. (if a path was added (no duplicate, and resourceName existed). |
---|
109 | */ |
---|
110 | bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName) |
---|
111 | { |
---|
112 | std::vector<Resources::Type*>::iterator it; |
---|
113 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
114 | if (*(*it) == resourceName) |
---|
115 | return (*it)->addResourcePath(pathName); |
---|
116 | PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str()); |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief add a ResourcePath to a Type's SubPaths. |
---|
122 | * @param resourceName the Type's name of Resource to add the subpath to. |
---|
123 | * @param pathName pathName the Name of the path to add. |
---|
124 | * @return true on success. (if a path was added (no duplicate, and resourceName existed). |
---|
125 | */ |
---|
126 | bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName) |
---|
127 | { |
---|
128 | std::vector<Resources::Type*>::iterator it; |
---|
129 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
130 | if (*(*it) == resourceName) |
---|
131 | return (*it)->addResourceSubPath(pathName); |
---|
132 | PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str()); |
---|
133 | return false; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * @brief checks wether a File is inside of the MainPath. |
---|
138 | * @param fileInside the file to check |
---|
139 | * @return true if the file is inside. |
---|
140 | */ |
---|
141 | bool ResourceManager::checkFileInMainPath(const File& fileInside) |
---|
142 | { |
---|
143 | return (this->_mainGlobalPath + fileInside).exists(); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * @brief prepends the fileName by the MainGlobalPath (same as mainGlobalPath + '/' + fileName). |
---|
148 | * @param fileName The FileName to prepend |
---|
149 | * @returns the prepended file-name |
---|
150 | */ |
---|
151 | std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName) |
---|
152 | { |
---|
153 | return (this->_mainGlobalPath + File(fileName)).name(); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief add a KeepLevelName (this function just counts upwards). |
---|
158 | * @param keepLevelName the Name of the KeepLevel to set. |
---|
159 | * @returns the Level the Name was set to. |
---|
160 | */ |
---|
161 | unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName) |
---|
162 | { |
---|
163 | this->_keepLevelNames.push_back(keepLevelName); |
---|
164 | return _keepLevelNames.size()-1; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * @param keepLevelName the Name of the KeepLevel. |
---|
169 | * @returns the ID of the KeepLevel named keepLevelName |
---|
170 | */ |
---|
171 | unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const |
---|
172 | { |
---|
173 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
174 | if (this->_keepLevelNames[i] == keepLevelName) |
---|
175 | return i; |
---|
176 | |
---|
177 | PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str()); |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * @param keepLevelID the ID to check. |
---|
183 | * @return the name of the KeepLevel. |
---|
184 | */ |
---|
185 | const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const |
---|
186 | { |
---|
187 | assert(keepLevelID < this->_keepLevelNames.size()); |
---|
188 | return this->_keepLevelNames[keepLevelID]; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief loads a Resource from a TypeName and a loadString. |
---|
194 | * @param resourceTypeName The Name of the Type to what to load a Resource from. |
---|
195 | * @param loadString the loadString to load in the Type. |
---|
196 | */ |
---|
197 | void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString) |
---|
198 | { |
---|
199 | std::vector<Resources::Type*>::const_iterator it; |
---|
200 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
201 | { |
---|
202 | if (*(*it) == resourceTypeName) |
---|
203 | { |
---|
204 | (*it)->createFromString(loadString); |
---|
205 | /// TODO check if the resource was allocated!! |
---|
206 | return ; |
---|
207 | } |
---|
208 | } |
---|
209 | return ; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * @brief unloads all Resources below a certain threshhold. |
---|
214 | * @param keepLevel the KeepLevel below which to erase. |
---|
215 | * |
---|
216 | * @not Resources will only be erased, if th keepLevel is below, and the resources are not |
---|
217 | * referenced anymore. |
---|
218 | */ |
---|
219 | void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel) |
---|
220 | { |
---|
221 | std::vector<Resources::Type*>::const_iterator it; |
---|
222 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
223 | { |
---|
224 | (*it)->unloadAllBelowKeepLevel(keepLevel); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | * @brief outputs debug information about the ResourceManager |
---|
231 | */ |
---|
232 | void ResourceManager::debug() const |
---|
233 | { |
---|
234 | PRINT(0)("=RM===================================\n"); |
---|
235 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
236 | PRINT(0)("======================================\n"); |
---|
237 | PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str()); |
---|
238 | if(!this->_globalPaths.empty()) |
---|
239 | { |
---|
240 | PRINT(0)(" Additional Global search Paths are: "); |
---|
241 | for (unsigned int i = 0; i < this->_globalPaths.size(); ++i) |
---|
242 | PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str()); |
---|
243 | PRINT(0)("\n"); |
---|
244 | } |
---|
245 | PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size()); |
---|
246 | std::vector<Resources::Type*>::const_iterator it; |
---|
247 | for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it) |
---|
248 | { |
---|
249 | (*it)->debug(); |
---|
250 | if (it != --this->_resourceTypes.end()) |
---|
251 | PRINT(0)(" ------------------------------------\n "); |
---|
252 | } |
---|
253 | |
---|
254 | PRINT(0)("KeepLevels are: "); |
---|
255 | for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i) |
---|
256 | PRINT(0)("%d:'%s' ", i, this->_keepLevelNames[i].c_str()); |
---|
257 | PRINT(0)("\n"); |
---|
258 | PRINT(0)("==================================RM==\n"); |
---|
259 | } |
---|
260 | } |
---|