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 #ifndef SWORD25_RESOURCEMANAGER_H
33 #define SWORD25_RESOURCEMANAGER_H
34 
35 #include "common/list.h"
36 #include "common/hashmap.h"
37 #include "common/hash-str.h"
38 
39 #include "sword25/kernel/common.h"
40 
41 namespace Sword25 {
42 
43 //#define PRECACHE_RESOURCES
44 
45 class ResourceService;
46 class Resource;
47 class Kernel;
48 
49 class ResourceManager {
50 	friend class Kernel;
51 
52 public:
53 	/**
54 	 * Returns a requested resource. If any error occurs, returns NULL
55 	 * @param FileName      Filename of resource
56 	 */
57 	Resource *requestResource(const Common::String &fileName);
58 
59 #ifdef PRECACHE_RESOURCES
60 	/**
61 	 * Loads a resource into the cache
62 	 * @param FileName      The filename of the resource to be cached
63 	 * @param ForceReload   Indicates whether the file should be reloaded if it's already in the cache.
64 	 * This is useful for files that may have changed in the interim
65 	 */
66 	bool precacheResource(const Common::String &fileName, bool forceReload = false);
67 #endif
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 	 */
74 	bool registerResourceService(ResourceService *pService);
75 
76 	/**
77 	 * Releases all resources that are not locked.
78 	 **/
79 	void emptyCache();
80 
81 	/**
82 	 * Removes all the savegame thumbnails from the cache
83 	 **/
84 	void emptyThumbnailCache();
85 
86 	/**
87 	 * Writes the names of all currently locked resources to the log file
88 	 */
89 	void dumpLockedResources();
90 
91 private:
92 	/**
93 	 * Creates a new resource manager
94 	 * Only the BS_Kernel class can generate copies this class. Thus, the constructor is private
95 	 */
ResourceManager(Kernel * pKernel)96 	ResourceManager(Kernel *pKernel) :
97 		_kernelPtr(pKernel)
98 	{}
99 	virtual ~ResourceManager();
100 
101 	/**
102 	 * Moves a resource to the top of the resource list
103 	 * @param pResource     The resource
104 	 */
105 	void moveToFront(Resource *pResource);
106 
107 	/**
108 	 * Loads a resource and updates the m_UsedMemory total
109 	 *
110 	 * The resource must not already be loaded
111 	 * @param FileName      The unique filename of the resource to be loaded
112 	 */
113 	Resource *loadResource(const Common::String &fileName);
114 
115 	/**
116 	 * Returns the full path of a given resource filename.
117 	 * It will return an empty string if a path could not be created.
118 	*/
119 	Common::String getUniqueFileName(const Common::String &fileName) const;
120 
121 	/**
122 	 * Deletes a resource, removes it from the lists, and updates m_UsedMemory
123 	 */
124 	Common::List<Resource *>::iterator deleteResource(Resource *pResource);
125 
126 	/**
127 	 * Returns a pointer to a loaded resource. If any error occurs, NULL will be returned.
128 	 * @param UniqueFileName        The absolute path and filename
129 	 */
130 	Resource *getResource(const Common::String &uniqueFileName) const;
131 
132 	/**
133 	 * Deletes resources as necessary until the specified memory limit is not being exceeded.
134 	 */
135 	void deleteResourcesIfNecessary();
136 
137 	Kernel *_kernelPtr;
138 	Common::Array<ResourceService *> _resourceServices;
139 	Common::List<Resource *> _resources;
140 	typedef Common::HashMap<Common::String, Resource *> ResMap;
141 	ResMap _resourceHashMap;
142 };
143 
144 } // End of namespace Sword25
145 
146 #endif
147