1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /*
24 * This code is based on Broken Sword 2.5 engine
25 *
26 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
27 *
28 * Licensed under GNU GPL v2
29 *
30 */
31
32 #include "sword25/sword25.h" // for kDebugResource
33 #include "sword25/kernel/resmanager.h"
34 #include "sword25/kernel/resource.h"
35 #include "sword25/kernel/resservice.h"
36 #include "sword25/package/packagemanager.h"
37
38 namespace Sword25 {
39
40 // Sets the amount of resources that are simultaneously loaded.
41 // This needs to be a relatively high number, as all the animation
42 // frames in each scene are loaded as separate resources.
43 // Also, George's walk states are all loaded here (150 files)
44 #define SWORD25_RESOURCECACHE_MIN 400
45 // The maximum number of loaded resources. If more than these resources
46 // are loaded, the resource manager will start purging resources till it
47 // hits the minimum limit above
48 #define SWORD25_RESOURCECACHE_MAX 500
49
~ResourceManager()50 ResourceManager::~ResourceManager() {
51 // Clear all unlocked resources
52 emptyCache();
53
54 // All remaining resources are not released, so print warnings and release
55 Common::List<Resource *>::iterator iter = _resources.begin();
56 for (; iter != _resources.end(); ++iter) {
57 warning("Resource \"%s\" was not released.", (*iter)->getFileName().c_str());
58
59 // Set the lock count to zero
60 while ((*iter)->getLockCount() > 0) {
61 (*iter)->release();
62 };
63
64 // Delete the resource
65 delete(*iter);
66 }
67 }
68
69 /**
70 * Registers a RegisterResourceService. This method is the constructor of
71 * BS_ResourceService, and thus helps all resource services in the ResourceManager list
72 * @param pService Which service
73 */
registerResourceService(ResourceService * pService)74 bool ResourceManager::registerResourceService(ResourceService *pService) {
75 if (!pService) {
76 error("Can't register NULL resource service.");
77 return false;
78 }
79
80 _resourceServices.push_back(pService);
81
82 return true;
83 }
84
85 /**
86 * Deletes resources as necessary until the specified memory limit is not being exceeded.
87 */
deleteResourcesIfNecessary()88 void ResourceManager::deleteResourcesIfNecessary() {
89 // If enough memory is available, or no resources are loaded, then the function can immediately end
90 if (_resources.size() < SWORD25_RESOURCECACHE_MAX)
91 return;
92
93 // Keep deleting resources until the memory usage of the process falls below the set maximum limit.
94 // The list is processed backwards in order to first release those resources that have been
95 // not been accessed for the longest
96 Common::List<Resource *>::iterator iter = _resources.end();
97 do {
98 --iter;
99
100 // The resource may be released only if it isn't locked
101 if ((*iter)->getLockCount() == 0)
102 iter = deleteResource(*iter);
103 } while (iter != _resources.begin() && _resources.size() >= SWORD25_RESOURCECACHE_MIN);
104
105 // Are we still above the minimum? If yes, then start releasing locked resources
106 // FIXME: This code shouldn't be needed at all, but it seems like there is a bug
107 // in the resource lock code, and resources are not unlocked when changing rooms.
108 // Only image/animation resources are unlocked forcibly, thus this shouldn't have
109 // any impact on the game itself.
110 if (_resources.size() <= SWORD25_RESOURCECACHE_MIN)
111 return;
112
113 iter = _resources.end();
114 do {
115 --iter;
116
117 // Only unlock image/animation resources
118 if ((*iter)->getFileName().hasSuffix(".swf") ||
119 (*iter)->getFileName().hasSuffix(".png")) {
120
121 warning("Forcibly unlocking %s", (*iter)->getFileName().c_str());
122
123 // Forcibly unlock the resource
124 while ((*iter)->getLockCount() > 0)
125 (*iter)->release();
126
127 iter = deleteResource(*iter);
128 }
129 } while (iter != _resources.begin() && _resources.size() >= SWORD25_RESOURCECACHE_MIN);
130 }
131
132 /**
133 * Releases all resources that are not locked.
134 */
emptyCache()135 void ResourceManager::emptyCache() {
136 // Scan through the resource list
137 Common::List<Resource *>::iterator iter = _resources.begin();
138 while (iter != _resources.end()) {
139 if ((*iter)->getLockCount() == 0) {
140 // Delete the resource
141 iter = deleteResource(*iter);
142 } else
143 ++iter;
144 }
145 }
146
emptyThumbnailCache()147 void ResourceManager::emptyThumbnailCache() {
148 // Scan through the resource list
149 Common::List<Resource *>::iterator iter = _resources.begin();
150 while (iter != _resources.end()) {
151 if ((*iter)->getFileName().hasPrefix("/saves")) {
152 // Unlock the thumbnail
153 while ((*iter)->getLockCount() > 0)
154 (*iter)->release();
155 // Delete the thumbnail
156 iter = deleteResource(*iter);
157 } else
158 ++iter;
159 }
160 }
161
162 /**
163 * Returns a requested resource. If any error occurs, returns NULL
164 * @param FileName Filename of resource
165 */
requestResource(const Common::String & fileName)166 Resource *ResourceManager::requestResource(const Common::String &fileName) {
167 // Get the absolute path to the file
168 Common::String uniqueFileName = getUniqueFileName(fileName);
169 if (uniqueFileName.empty())
170 return NULL;
171
172 // Determine whether the resource is already loaded
173 // If the resource is found, it will be placed at the head of the resource list and returned
174 Resource *pResource = getResource(uniqueFileName);
175 if (!pResource)
176 pResource = loadResource(uniqueFileName);
177 if (pResource) {
178 moveToFront(pResource);
179 (pResource)->addReference();
180 return pResource;
181 }
182
183 return NULL;
184 }
185
186 #ifdef PRECACHE_RESOURCES
187
188 /**
189 * Loads a resource into the cache
190 * @param FileName The filename of the resource to be cached
191 * @param ForceReload Indicates whether the file should be reloaded if it's already in the cache.
192 * This is useful for files that may have changed in the interim
193 */
precacheResource(const Common::String & fileName,bool forceReload)194 bool ResourceManager::precacheResource(const Common::String &fileName, bool forceReload) {
195 // Get the absolute path to the file
196 Common::String uniqueFileName = getUniqueFileName(fileName);
197 if (uniqueFileName.empty())
198 return false;
199
200 Resource *resourcePtr = getResource(uniqueFileName);
201
202 if (forceReload && resourcePtr) {
203 if (resourcePtr->getLockCount()) {
204 error("Could not force precaching of \"%s\". The resource is locked.", fileName.c_str());
205 return false;
206 } else {
207 deleteResource(resourcePtr);
208 resourcePtr = 0;
209 }
210 }
211
212 if (!resourcePtr && loadResource(uniqueFileName) == NULL) {
213 // This isn't fatal - e.g. it can happen when loading saved games
214 debugC(kDebugResource, "Could not precache \"%s\",", fileName.c_str());
215 return false;
216 }
217
218 return true;
219 }
220
221 #endif
222
223 /**
224 * Moves a resource to the top of the resource list
225 * @param pResource The resource
226 */
moveToFront(Resource * pResource)227 void ResourceManager::moveToFront(Resource *pResource) {
228 // Erase the resource from it's current position
229 _resources.erase(pResource->_iterator);
230 // Re-add the resource at the front of the list
231 _resources.push_front(pResource);
232 // Reset the resource iterator to the repositioned item
233 pResource->_iterator = _resources.begin();
234 }
235
236 /**
237 * Loads a resource and updates the m_UsedMemory total
238 *
239 * The resource must not already be loaded
240 * @param FileName The unique filename of the resource to be loaded
241 */
loadResource(const Common::String & fileName)242 Resource *ResourceManager::loadResource(const Common::String &fileName) {
243 // ResourceService finden, der die Resource laden kann.
244 for (uint i = 0; i < _resourceServices.size(); ++i) {
245 if (_resourceServices[i]->canLoadResource(fileName)) {
246 // If more memory is desired, memory must be released
247 deleteResourcesIfNecessary();
248
249 // Load the resource
250 Resource *pResource = _resourceServices[i]->loadResource(fileName);
251 if (!pResource) {
252 error("Responsible service could not load resource \"%s\".", fileName.c_str());
253 return NULL;
254 }
255
256 // Add the resource to the front of the list
257 _resources.push_front(pResource);
258 pResource->_iterator = _resources.begin();
259
260 // Also store the resource in the hash table for quick lookup
261 _resourceHashMap[pResource->getFileName()] = pResource;
262
263 return pResource;
264 }
265 }
266
267 // This isn't fatal - e.g. it can happen when loading saved games
268 debugC(kDebugResource, "Could not find a service that can load \"%s\".", fileName.c_str());
269 return NULL;
270 }
271
272 /**
273 * Returns the full path of a given resource filename.
274 * It will return an empty string if a path could not be created.
275 */
getUniqueFileName(const Common::String & fileName) const276 Common::String ResourceManager::getUniqueFileName(const Common::String &fileName) const {
277 // Get a pointer to the package manager
278 PackageManager *pPackage = (PackageManager *)_kernelPtr->getPackage();
279 if (!pPackage) {
280 error("Could not get package manager.");
281 return Common::String();
282 }
283
284 // Absoluten Pfad der Datei bekommen und somit die Eindeutigkeit des Dateinamens sicherstellen
285 Common::String uniquefileName = pPackage->getAbsolutePath(fileName);
286 if (uniquefileName.empty())
287 error("Could not create absolute file name for \"%s\".", fileName.c_str());
288
289 return uniquefileName;
290 }
291
292 /**
293 * Deletes a resource, removes it from the lists, and updates m_UsedMemory
294 */
deleteResource(Resource * pResource)295 Common::List<Resource *>::iterator ResourceManager::deleteResource(Resource *pResource) {
296 // Remove the resource from the hash table
297 _resourceHashMap.erase(pResource->_fileName);
298
299 // Delete the resource from the resource list
300 Common::List<Resource *>::iterator result = _resources.erase(pResource->_iterator);
301
302 // Delete the resource
303 delete pResource;
304
305 // Return the iterator
306 return result;
307 }
308
309 /**
310 * Returns a pointer to a loaded resource. If any error occurs, NULL will be returned.
311 * @param UniquefileName The absolute path and filename
312 */
getResource(const Common::String & uniquefileName) const313 Resource *ResourceManager::getResource(const Common::String &uniquefileName) const {
314 // Determine whether the resource is already loaded
315 ResMap::iterator it = _resourceHashMap.find(uniquefileName);
316 if (it != _resourceHashMap.end())
317 return it->_value;
318
319 // Resource was not found, i.e. has not yet been loaded.
320 return NULL;
321 }
322
323 /**
324 * Writes the names of all currently locked resources to the log file
325 */
dumpLockedResources()326 void ResourceManager::dumpLockedResources() {
327 for (Common::List<Resource *>::iterator iter = _resources.begin(); iter != _resources.end(); ++iter) {
328 if ((*iter)->getLockCount() > 0) {
329 debugC(kDebugResource, "%s", (*iter)->getFileName().c_str());
330 }
331 }
332 }
333
334 } // End of namespace Sword25
335