aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/scicore/resource.h
blob: 83015c6f636f675943f04feeeed30051a3cb7279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#ifndef SCI_SCICORE_RESOURCE_H
#define SCI_SCICORE_RESOURCE_H

#include "common/str.h"
#include "common/file.h"
#include "common/archive.h"

namespace Common {
	class ReadStream;
}

namespace Sci {

/** The maximum allowed size for a compressed or decompressed resource */
#define SCI_MAX_RESOURCE_SIZE 0x0400000

/*** RESOURCE STATUS TYPES ***/
#define SCI_STATUS_NOMALLOC 0
#define SCI_STATUS_ALLOCATED 1
#define SCI_STATUS_ENQUEUED 2 /* In the LRU queue */
#define SCI_STATUS_LOCKED 3 /* Allocated and in use */

#define SCI_RES_FILE_NR_PATCH -1 /* Resource was read from a patch file rather than from a resource */


/*** INITIALIZATION RESULT TYPES ***/
#define SCI_ERROR_IO_ERROR 1
#define SCI_ERROR_EMPTY_OBJECT 2
#define SCI_ERROR_INVALID_RESMAP_ENTRY 3
/* Invalid resource.map entry */
#define SCI_ERROR_RESMAP_NOT_FOUND 4
#define SCI_ERROR_NO_RESOURCE_FILES_FOUND 5
/* No resource at all was found */
#define SCI_ERROR_UNKNOWN_COMPRESSION 6
#define SCI_ERROR_DECOMPRESSION_OVERFLOW 7
/* decompression failed: Buffer overflow (wrong SCI version?)  */
#define SCI_ERROR_DECOMPRESSION_INSANE 8
/* sanity checks failed during decompression */
#define SCI_ERROR_RESOURCE_TOO_BIG 9
/* Resource size exceeds SCI_MAX_RESOURCE_SIZE */
#define SCI_ERROR_UNSUPPORTED_VERSION 10
#define SCI_ERROR_INVALID_SCRIPT_VERSION 11

#define SCI_ERROR_CRITICAL SCI_ERROR_NO_RESOURCE_FILES_FOUND
/* the first critical error number */

/*** SCI VERSION NUMBERS ***/
#define SCI_VERSION_AUTODETECT 0
#define SCI_VERSION_0 1
#define SCI_VERSION_01 2
#define SCI_VERSION_01_VGA 3
#define SCI_VERSION_01_VGA_ODD 4
#define SCI_VERSION_1_EARLY 5
#define SCI_VERSION_1_LATE 6
#define SCI_VERSION_1_1 7
#define SCI_VERSION_32 8
#define SCI_VERSION_LAST SCI_VERSION_1_LATE /* The last supported SCI version */

#define SCI_VERSION_1 SCI_VERSION_1_EARLY

enum ResSourceType {
	kSourceDirectory = 0,
	kSourceVolume = 2,
	kSourceExtMap = 3,
	kSourceIntMap = 4,
	kSourceMask = 127
};

#define RESSOURCE_ADDRESSING_BASIC 0
#define RESSOURCE_ADDRESSING_EXTENDED 128
#define RESSOURCE_ADDRESSING_MASK 128

#define RESOURCE_HASH(type, number) (uint32)((type<<16) | number) 

extern const char *sci_error_types[];
extern const char *sci_version_types[];
extern const int sci_max_resource_nr[]; /* Highest possible resource numbers */


enum ResourceType {
	kResourceTypeView = 0,
	kResourceTypePic,
	kResourceTypeScript,
	kResourceTypeText,
	kResourceTypeSound,
	kResourceTypeMemory,
	kResourceTypeVocab,
	kResourceTypeFont,
	kResourceTypeCursor,
	kResourceTypePatch,
	kResourceTypeBitmap,
	kResourceTypePalette,
	kResourceTypeCdAudio,
	kResourceTypeAudio,
	kResourceTypeSync,
	kResourceTypeMessage,
	kResourceTypeMap,
	kResourceTypeHeap,
	kResourceTypeInvalid
};

const char *getResourceTypeName(ResourceType restype);
// Suffixes for SCI1 patch files
const char *getResourceTypeSuffix(ResourceType restype);

#define sci0_last_resource kResourceTypePatch
#define sci1_last_resource kResourceTypeHeap
/* Used for autodetection */


#if 0
struct resource_index_struct {
	unsigned short resource_id;
	unsigned int resource_location;
}; /* resource type as stored in the resource.map file */

typedef struct resource_index_struct resource_index_t;
#endif

struct ResourceSource {
	ResSourceType source_type;
	bool scanned;
	Common::String location_name;	// FIXME: Replace by FSNode ?
	Common::String location_dir_name;	// FIXME: Get rid of this again, only a temporary HACK!
	int volume_number;
	ResourceSource *associated_map;
	ResourceSource *next;
};

/** Class for storing resources in memory */
class Resource {

public:
	Resource::Resource();
	Resource::~Resource();
	void unalloc();

// NOTE : Currently all member data has the same name and public visibility
// to let the rest of the engine compile without changes
public:
	byte *data;
	uint16 number;
	ResourceType type;
	uint16 id; /* contains number and type */
	unsigned int size;
	unsigned int file_offset; /* Offset in file */
	byte status;
	unsigned short lockers; /* Number of places where this resource was locked */
	Resource *next; /* Position marker for the LRU queue */
	Resource *prev;
	ResourceSource *source;
};


class ResourceManager {
public:
	int _sciVersion; /* SCI resource version to use */

	/**
	 * Creates a new FreeSCI resource manager.
	 * @param version		The SCI version to look for; use SCI_VERSION_AUTODETECT
	 *						in the default case.
	 * @param maxMemory		Maximum number of bytes to allow allocated for resources
	 *
	 * @note maxMemory will not be interpreted as a hard limit, only as a restriction
	 *    for resources which are not explicitly locked. However, a warning will be
	 *    issued whenever this limit is exceeded.
	 */
	ResourceManager(int version, int maxMemory);
	~ResourceManager();

	/* Add a path to the resource manager's list of sources.
	** Returns: A pointer to the added source structure, or NULL if an error occurred.
	*/
	ResourceSource *addPatchDir(const char *path);

	ResourceSource *getVolume(ResourceSource *map, int volume_nr);

	//! Add a volume to the resource manager's list of sources.
	/** @param map		The map associated with this volume
	 *	@param filename	The name of the volume to add
	 *	@param extended_addressing	1 if this volume uses extended addressing,
	 *                                        0 otherwise.
	 *	@return A pointer to the added source structure, or NULL if an error occurred.
	 */
	ResourceSource *addVolume(ResourceSource *map, const char *filename,
		int number, int extended_addressing);

	//! Add an external (i.e. separate file) map resource to the resource manager's list of sources.
	/**	@param file_name	 The name of the volume to add
	 *	@return		A pointer to the added source structure, or NULL if an error occurred.
	 */
	ResourceSource *addExternalMap(const char *file_name);

	//! Scans newly registered resource sources for resources, earliest addition first.
	/** @param detected_version: Pointer to the detected version number,
	 *					 used during startup. May be NULL.
	 * @return One of SCI_ERROR_*.
	 */
	int scanNewSources(int *detected_version, ResourceSource *source);

	//! Looks up a resource's data
	/**	@param type: The resource type to look for
	 *	@param number: The resource number to search
	 *	@param lock: non-zero iff the resource should be locked
	 *	@return (Resource *): The resource, or NULL if it doesn't exist
	 *	@note Locked resources are guaranteed not to have their contents freed until
	 *		they are unlocked explicitly (by unlockResource).
	 */
	Resource *findResource(ResourceType type, int number, int lock);

	/* Unlocks a previously locked resource
	**             (Resource *) res: The resource to free
	**             (int) number: Number of the resource to check (ditto)
	**             (ResourceType) type: Type of the resource to check (for error checking)
	** Returns   : (void)
	*/
	void unlockResource(Resource *res, int restype, ResourceType resnum);

	/* Tests whether a resource exists
	**             (ResourceType) type: Type of the resource to check
	**             (int) number: Number of the resource to check
	** Returns   : (Resource *) non-NULL if the resource exists, NULL otherwise
	** This function may often be much faster than finding the resource
	** and should be preferred for simple tests.
	** The resource object returned is, indeed, the resource in question, but
	** it should be used with care, as it may be unallocated.
	** Use scir_find_resource() if you want to use the data contained in the resource.
	*/
	Resource *testResource(ResourceType type, int number);

protected:
	int _maxMemory; /* Config option: Maximum total byte number allocated */
	//int _resourcesNr;
	ResourceSource *_sources;
	//Resource *_resources;
	int _memoryLocked;	// Amount of resource bytes in locked memory
	int _memoryLRU;		// Amount of resource bytes under LRU control
	Resource *lru_first, *lru_last;	// Pointers to the first and last LRU queue entries
										// LRU queue: lru_first points to the most recent entry
	Common::HashMap<uint32, Resource *> _resMap;

	int addAppropriateSources();
	void freeResourceSources(ResourceSource *rss);

	void loadResource(Resource *res, bool protect);
	bool loadFromPatchFile(Resource *res);
	void freeOldResources(int last_invulnerable);

	/**--- Resource map decoding functions ---*/

	/* Reads the SCI0 resource.map file from a local directory
	** Parameters: (char *) path: (unused)
	**             (int) sci_version: SCI resource version
	** Returns   : (int) 0 on success, an SCI_ERROR_* code otherwise
	*/
	int readResourceMapSCI0(ResourceSource *map, int *sci_version);

	/* Reads the SCI1 resource.map file from a local directory
	** Parameters: (char *) path: (unused)
	**             (int) sci_version: SCI resource version
	** Returns   : (int) 0 on success, an SCI_ERROR_* code otherwise
	*/
	int readResourceMapSCI1(ResourceSource *map, ResourceSource *vol, int *sci_version);

	int isSCI10or11(int *types);
	int detectOddSCI01(Common::File &file);
	int resReadEntry(ResourceSource *map, byte *buf, Resource *res, int sci_version);
	ResourceType resTypeSCI1(int ofs, int *types, ResourceType lastrt);
	int parseHeaderSCI1(Common::ReadStream &stream, int *types, ResourceType *lastrt);

	/**--- Patch management functions ---*/

	//! Reads SCI0 patch files from a local directory
	/** @paramParameters: ResourceSource *source
	  * @return   : (int) 0 on success, an SCI_ERROR_* code otherwise
	  */
	int readResourcePatchesSCI0(ResourceSource *source);

	//! Reads SCI1 patch files from a local directory
	/** @paramParameters: ResourceSource *source
	  * @return   : (int) 0 on success, an SCI_ERROR_* code otherwise
	  */
	int readResourcePatchesSCI1(ResourceSource *source);

	void process_patch(ResourceSource *source, Common::ArchiveMember &member,
		ResourceType restype, int resnumber);

	void printLRU();
	void addToLRU(Resource *res);
	void removeFromLRU(Resource *res);
};


	/* Prints the name of a matching patch to a string buffer
	** Parameters: (char *) string: The buffer to print to
	**             (Resource *) res: Resource containing the number and type of the
	**                                 resource whose name is to be print
	** Returns   : (void)
	*/
	void sci0_sprintf_patch_file_name(char *string, Resource *res);

	/* Prints the name of a matching patch to a string buffer
	** Parameters: (char *) string: The buffer to print to
	**             (Resource *) res: Resource containing the number and type of the
	**                                 resource whose name is to be print
	** Returns   : (void)
	*/
	void sci1_sprintf_patch_file_name(char *string, Resource *res);

	/**--- Decompression functions ---**/
	int decompress0(Resource *result, Common::ReadStream &stream, int sci_version);
	/* Decrypts resource data and stores the result for SCI0-style compression.
	** Parameters : result: The Resource the decompressed data is stored in.
	**              stream: Stream of the resource file
	**              sci_version : Actual SCI resource version
	** Returns    : (int) 0 on success, one of SCI_ERROR_* if a problem was
	**               encountered.
	*/

	int decompress01(Resource *result, Common::ReadStream &stream, int sci_version);
	/* Decrypts resource data and stores the result for SCI01-style compression.
	** Parameters : result: The Resource the decompressed data is stored in.
	**              stream: Stream of the resource file
	**              sci_version : Actual SCI resource version
	** Returns    : (int) 0 on success, one of SCI_ERROR_* if a problem was
	**               encountered.
	*/

	int decompress1(Resource *result, Common::ReadStream &stream, int sci_version);
	/* Decrypts resource data and stores the result for SCI1.1-style compression.
	** Parameters : result: The Resource the decompressed data is stored in.
	**              sci_version : Actual SCI resource version
	**              stream: Stream of the resource file
	** Returns    : (int) 0 on success, one of SCI_ERROR_* if a problem was
	**               encountered.
	*/

	int decompress11(Resource *result, Common::ReadStream &stream, int sci_version);
	/* Decrypts resource data and stores the result for SCI1.1-style compression.
	** Parameters : result: The Resource the decompressed data is stored in.
	**              sci_version : Actual SCI resource version
	**              stream: Stream of the resource file
	** Returns    : (int) 0 on success, one of SCI_ERROR_* if a problem was
	**               encountered.
	*/

	int unpackHuffman(uint8* dest, uint8* src, int length, int complength);
	/* Huffman token decryptor - defined in decompress0.c and used in decompress01.c
	*/

	int unpackDCL(uint8* dest, uint8* src, int length, int complength);
	/* DCL inflate- implemented in decompress1.c
	*/

	byte *view_reorder(byte *inbuffer, int dsize);
	/* SCI1 style view compression */

	byte *pic_reorder(byte *inbuffer, int dsize);
	/* SCI1 style pic compression */

} // End of namespace Sci

#endif // SCI_SCICORE_RESOURCE_H