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
390
391
392
393
394
395
396
397
398
399
|
/* 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.
*
*/
#include "sherlock/resources.h"
#include "sherlock/decompress.h"
#include "sherlock/screen.h"
#include "sherlock/sherlock.h"
#include "common/debug.h"
namespace Sherlock {
Cache::Cache() {
}
/**
* Returns true if a given file is currently being cached
*/
bool Cache::isCached(const Common::String &filename) const {
return _resources.contains(filename);
}
/**
* Loads a file into the cache if it's not already present, and returns it.
* If the file is LZW compressed, automatically decompresses it and loads
* the uncompressed version into memory
*/
void Cache::load(const Common::String &name) {
// First check if the entry already exists
if (_resources.contains(name))
return;
// Open the file for reading
Common::File f;
if (!f.open(name))
error("Could not read file - %s", name.c_str());
load(name, f);
f.close();
}
/**
* Load a cache entry based on a passed stream
*/
void Cache::load(const Common::String &name, Common::SeekableReadStream &stream) {
// First check if the entry already exists
if (_resources.contains(name))
return;
// Check whether the file is compressed
const char LZW_HEADER[5] = { "LZV\x1a" };
char header[5];
stream.read(header, 5);
bool isCompressed = !strncmp(header, LZW_HEADER, 5);
stream.seek(0);
// Allocate a new cache entry
_resources[name] = CacheEntry();
CacheEntry &cacheEntry = _resources[name];
if (isCompressed) {
// It's compressed, so decompress the file and store it's data in the cache entry
Common::SeekableReadStream *decompressed = decompressLZ(stream);
cacheEntry.resize(decompressed->size());
decompressed->read(&cacheEntry[0], decompressed->size());
delete decompressed;
} else {
// It's not, so read the raw data of the file into the cache entry
cacheEntry.resize(stream.size());
stream.read(&cacheEntry[0], stream.size());
}
}
/**
* Get a file from the cache
*/
Common::SeekableReadStream *Cache::get(const Common::String &filename) const {
// Return a memory stream that encapsulates the data
const CacheEntry &cacheEntry = _resources[filename];
return new Common::MemoryReadStream(&cacheEntry[0], cacheEntry.size());
}
/*----------------------------------------------------------------*/
Resources::Resources() {
_resourceIndex = -1;
addToCache("vgs.lib");
addToCache("talk.lib");
addToCache("sequence.txt");
addToCache("journal.txt");
addToCache("portrait.lib");
}
/**
* Adds the specified file to the cache. If it's a library file, takes care of
* loading it's index for future use
*/
void Resources::addToCache(const Common::String &filename) {
_cache.load(filename);
// Check to see if the file is a library
Common::SeekableReadStream *stream = load(filename);
uint32 header = stream->readUint32BE();
if (header == MKTAG('L', 'I', 'B', 26))
loadLibraryIndex(filename, stream);
delete stream;
}
/**
* Adds a resource from a library file to the cache
*/
void Resources::addToCache(const Common::String &filename, const Common::String &libFilename) {
// Get the resource
Common::SeekableReadStream *stream = load(filename, libFilename);
_cache.load(filename, *stream);
delete stream;
}
/**
* Adds a given stream to the cache under the given name
*/
void Resources::addToCache(const Common::String &filename, Common::SeekableReadStream &stream) {
_cache.load(filename, stream);
}
/**
* Returns a stream for a given file
*/
Common::SeekableReadStream *Resources::load(const Common::String &filename) {
// First check if the file is directly in the cache
if (_cache.isCached(filename))
return _cache.get(filename);
// Secondly, iterate through any loaded library file looking for a resource
// that has the same name
LibraryIndexes::iterator i;
for (i = _indexes.begin(); i != _indexes.end(); ++i) {
if ((*i)._value.contains(filename)) {
// Get a stream reference to the given library file
Common::SeekableReadStream *stream = load((*i)._key);
LibraryEntry &entry = (*i)._value[filename];
_resourceIndex = entry._index;
stream->seek(entry._offset);
Common::SeekableReadStream *resStream = stream->readStream(entry._size);
delete stream;
return resStream;
}
}
// At this point, fall back on a physical file with the given name
Common::File f;
if (!f.open(filename))
error("Could not load file - %s", filename.c_str());
Common::SeekableReadStream *stream = f.readStream(f.size());
f.close();
return stream;
}
/**
* Loads a specific resource from a given library file
*/
Common::SeekableReadStream *Resources::load(const Common::String &filename, const Common::String &libraryFile) {
// Open up the library for access
Common::SeekableReadStream *libStream = load(libraryFile);
// Check if the library has already had it's index read, and if not, load it
if (!_indexes.contains(libraryFile))
loadLibraryIndex(libraryFile, libStream);
// Extract the data for the specified resource and return it
LibraryEntry &entry = _indexes[libraryFile][filename];
libStream->seek(entry._offset);
Common::SeekableReadStream *stream = libStream->readStream(entry._size);
delete libStream;
return stream;
}
/**
* Returns true if the given file exists on disk or in the cache
*/
bool Resources::exists(const Common::String &filename) const {
Common::File f;
return f.exists(filename) || _cache.isCached(filename);
}
/**
* Reads in the index from a library file, and caches it's index for later use
*/
void Resources::loadLibraryIndex(const Common::String &libFilename,
Common::SeekableReadStream *stream) {
uint32 offset, nextOffset;
// Create an index entry
_indexes[libFilename] = LibraryIndex();
LibraryIndex &index = _indexes[libFilename];
// Read in the number of resources
stream->seek(4);
int count = stream->readUint16LE();
// Loop through reading in the entries
for (int idx = 0; idx < count; ++idx) {
// Read the name of the resource
char resName[13];
stream->read(resName, 13);
resName[12] = '\0';
// Read the offset
offset = stream->readUint32LE();
if (idx == (count - 1)) {
nextOffset = stream->size();
} else {
// Read the size by jumping forward to read the next entry's offset
stream->seek(13, SEEK_CUR);
nextOffset = stream->readUint32LE();
stream->seek(-17, SEEK_CUR);
}
// Add the entry to the index
index[resName] = LibraryEntry(idx, offset, nextOffset - offset);
}
}
/**
* Returns the index of the last loaded resource in it's given library file.
* This will be used primarily when loading talk files, so the engine can
* update the given conversation number in the journal
*/
int Resources::resourceIndex() const {
return _resourceIndex;
}
/*----------------------------------------------------------------*/
SherlockEngine *ImageFile::_vm;
void ImageFile::setVm(SherlockEngine *vm) {
_vm = vm;
}
ImageFile::ImageFile(const Common::String &name, bool skipPal, bool animImages) {
Common::SeekableReadStream *stream = _vm->_res->load(name);
Common::fill(&_palette[0], &_palette[PALETTE_SIZE], 0);
load(*stream, skipPal, animImages);
delete stream;
}
ImageFile::ImageFile(Common::SeekableReadStream &stream, bool skipPal) {
Common::fill(&_palette[0], &_palette[PALETTE_SIZE], 0);
load(stream, skipPal, false);
}
ImageFile::~ImageFile() {
for (uint idx = 0; idx < size(); ++idx)
(*this)[idx]._frame.free();
}
/**
* Load the data of the sprite
*/
void ImageFile::load(Common::SeekableReadStream &stream, bool skipPalette, bool animImages) {
loadPalette(stream);
int streamSize = stream.size();
while (stream.pos() < streamSize) {
ImageFrame frame;
frame._width = stream.readUint16LE() + 1;
frame._height = stream.readUint16LE() + 1;
frame._paletteBase = stream.readByte();
if (animImages) {
// Animation cutscene image files use a 16-bit x offset
frame._offset.x = stream.readUint16LE();
frame._rleEncoded = (frame._offset.x & 0xff) == 1;
} else {
// Standard image files have a separate byte for the RLE flag, and an 8-bit X offset
frame._rleEncoded = stream.readByte() == 1;
frame._offset.x = stream.readByte();
}
frame._offset.y = stream.readByte();
frame._rleEncoded = !skipPalette && frame._rleEncoded;
if (frame._paletteBase) {
// Nibble packed frame data
frame._size = (frame._width * frame._height) / 2;
} else if (frame._rleEncoded) {
// This size includes the header size, which we subtract
frame._size = stream.readUint16LE() - 11;
frame._rleMarker = stream.readByte();
} else {
// Uncompressed data
frame._size = frame._width * frame._height;
}
// Load data for frame and decompress it
byte *data = new byte[frame._size];
stream.read(data, frame._size);
decompressFrame(frame, data);
delete[] data;
push_back(frame);
}
}
/**
* Gets the palette at the start of the sprite file
*/
void ImageFile::loadPalette(Common::SeekableReadStream &stream) {
// Check for palette
int v1 = stream.readUint16LE() + 1;
int v2 = stream.readUint16LE() + 1;
stream.skip(1); // Skip paletteBase byte
bool rleEncoded = stream.readByte() == 1;
int palSize = v1 * v2;
if ((palSize - 12) == PALETTE_SIZE && !rleEncoded) {
// Found palette, so read it in
stream.seek(2 + 12, SEEK_CUR);
for (int idx = 0; idx < PALETTE_SIZE; ++idx)
_palette[idx] = VGA_COLOR_TRANS(stream.readByte());
} else {
// Not a palette, so rewind to start of frame data for normal frame processing
stream.seek(-6, SEEK_CUR);
}
}
/**
* Decompress a single frame for the sprite
*/
void ImageFile::decompressFrame(ImageFrame &frame, const byte *src) {
frame._frame.create(frame._width, frame._height, Graphics::PixelFormat::createFormatCLUT8());
if (frame._paletteBase) {
// Nibble-packed
byte *pDest = (byte *)frame._frame.getPixels();
for (uint idx = 0; idx < frame._size; ++idx, ++src) {
*pDest++ = *src & 0xF;
*pDest++ = (*src >> 4);
}
} else if (frame._rleEncoded) {
// RLE encoded
byte *dst = (byte *)frame._frame.getPixels();
int frameSize = frame._width * frame._height;
while (frameSize > 0) {
if (*src == frame._rleMarker) {
byte rleColor = src[1];
byte rleCount = src[2];
src += 3;
frameSize -= rleCount;
while (rleCount--)
*dst++ = rleColor;
} else {
*dst++ = *src++;
--frameSize;
}
}
assert(frameSize == 0);
} else {
// Uncompressed frame
Common::copy(src, src + frame._width * frame._height,
(byte *)frame._frame.getPixels());
}
}
} // End of namespace Sherlock
|