1
/* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
26
#include "common/scummsys.h"
27
#include "common/endian.h"
28
#include "common/file.h"
29
#include "common/stack.h"
30
#include "common/util.h"
31
#include "tsage/resources.h"
32
#include "tsage/tsage.h"
37
MemoryManager::MemoryManager() {
38
_memoryPool = new MemoryHeader*[MEMORY_POOL_SIZE];
39
Common::set_to(&_memoryPool[0], &_memoryPool[MEMORY_POOL_SIZE], (MemoryHeader *)NULL);
42
MemoryManager::~MemoryManager() {
43
for (int i = 0; i < MEMORY_POOL_SIZE; ++i) {
44
if (_memoryPool[i] != NULL)
50
uint16 MemoryManager::allocate(uint32 size) {
52
while ((idx < MEMORY_POOL_SIZE) && (_memoryPool[idx] != NULL))
54
if (idx == MEMORY_POOL_SIZE)
55
error("Out of memory handles");
57
// Create the new entry
58
_memoryPool[idx] = (MemoryHeader *)malloc(sizeof(MemoryHeader) + size);
59
_memoryPool[idx]->id = MEMORY_ENTRY_ID;
60
_memoryPool[idx]->index = idx;
61
_memoryPool[idx]->lockCtr = 0;
62
_memoryPool[idx]->criticalCtr = 0;
63
_memoryPool[idx]->tag = 0;
64
_memoryPool[idx]->size = size;
70
byte *MemoryManager::allocate2(uint32 size) {
71
uint32 idx = allocate(size);
75
byte *MemoryManager::lock(uint32 handle) {
76
assert((int)handle < MEMORY_POOL_SIZE);
77
return (byte *)_memoryPool[handle] + sizeof(MemoryHeader);
80
int MemoryManager::indexOf(const byte *p) {
81
for (int idx = 0; idx < MEMORY_POOL_SIZE; ++idx) {
82
if (((byte *)_memoryPool[idx] + sizeof(MemoryHeader)) == p)
89
void MemoryManager::deallocate(const byte *p) {
95
if (_memoryPool[idx]->lockCtr-- == 0) {
96
free(_memoryPool[idx]);
97
_memoryPool[idx] = NULL;
101
uint32 MemoryManager::getSize(const byte *p) {
102
int idx = indexOf(p);
104
return _memoryPool[idx]->size;
107
void MemoryManager::incLocks(const byte *p) {
108
int idx = indexOf(p);
110
_memoryPool[idx]->lockCtr++;
113
/*-------------------------------------------------------------------------*/
115
static uint16 bitMasks[4] = {0x1ff, 0x3ff, 0x7ff, 0xfff};
117
uint16 BitReader::readToken() {
118
assert((numBits >= 9) && (numBits <= 12));
119
uint16 result = _remainder;
120
int bitsLeft = numBits - _bitsLeft;
121
int bitOffset = _bitsLeft;
124
while (bitsLeft >= 0) {
125
_remainder = readByte();
126
result |= _remainder << bitOffset;
131
_bitsLeft = -bitsLeft;
132
_remainder >>= 8 - _bitsLeft;
133
return result & bitMasks[numBits - 9];
136
/*-------------------------------------------------------------------------*/
138
TLib::TLib(MemoryManager &memManager, const Common::String &filename) :
139
_memoryManager(memManager) {
141
// If the resource strings list isn't yet loaded, load them
142
if (_resStrings.size() == 0) {
144
if (f.open("tsage.cfg")) {
146
_resStrings.push_back(f.readLine());
152
if (!_file.open(filename))
153
error("Missing file %s", filename.c_str());
162
void TLib::loadSection(uint32 fileOffset) {
164
_file.seek(fileOffset);
165
_sections.fileOffset = fileOffset;
167
if (_file.readUint32BE() != 0x544D492D)
168
error("Data block is not valid Rlb data");
170
/*uint8 unknown1 = */_file.readByte();
171
uint16 numEntries = _file.readByte();
173
for (uint i = 0; i < numEntries; ++i) {
174
uint16 id = _file.readUint16LE();
175
uint16 size = _file.readUint16LE();
176
uint16 uncSize = _file.readUint16LE();
177
uint8 sizeHi = _file.readByte();
178
uint8 type = _file.readByte() >> 5;
180
uint32 offset = _file.readUint32LE();
184
re.fileOffset = offset;
185
re.isCompressed = type != 0;
186
re.size = ((sizeHi & 0xF) << 16) | size;
187
re.uncompressedSize = ((sizeHi & 0xF0) << 12) | uncSize;
189
_resources.push_back(re);
193
struct DecodeReference {
199
* Gets a resource from the currently loaded section
201
byte *TLib::getResource(uint16 id, bool suppressErrors) {
202
// Scan for an entry for the given Id
203
ResourceEntry *re= NULL;
204
ResourceList::iterator i;
205
for (i = _resources.begin(); i != _resources.end(); ++i) {
214
error("Could not find resource Id #%d", id);
217
if (!re->isCompressed) {
218
// Read in the resource data and return it
219
byte *dataP = _memoryManager.allocate2(re->size);
220
_file.seek(_sections.fileOffset + re->fileOffset);
221
_file.read(dataP, re->size);
227
* Decompress the data block
230
_file.seek(_sections.fileOffset + re->fileOffset);
231
Common::ReadStream *compStream = _file.readStream(re->size);
232
BitReader bitReader(*compStream);
234
byte *dataOut = _memoryManager.allocate2(re->uncompressedSize);
235
byte *destP = dataOut;
236
uint bytesWritten = 0;
238
uint16 ctrCurrent = 0x102, ctrMax = 0x200;
239
uint16 word_48050 = 0, currentToken = 0, word_48054 =0;
240
byte byte_49068 = 0, byte_49069 = 0;
241
DecodeReference table[0x1000];
242
for (int i = 0; i < 0x1000; ++i) {
243
table[i].vByte = table[i].vWord = 0;
245
Common::Stack<uint16> tokenList;
248
// Get the next decode token
249
uint16 token = bitReader.readToken();
252
if (token == 0x101) {
253
// End of compressed stream
255
} else if (token == 0x100) {
257
bitReader.numBits = 9;
261
// Set variables with next token
262
currentToken = word_48050 = bitReader.readToken();
263
byte_49069 = byte_49068 = (byte)currentToken;
266
assert(bytesWritten <= re->uncompressedSize);
267
*destP++ = byte_49069;
269
word_48054 = word_48050 = token;
271
if (token >= ctrCurrent) {
272
word_48050 = currentToken;
273
tokenList.push(byte_49068);
276
while (word_48050 >= 0x100) {
277
assert(word_48050 < 0x1000);
278
tokenList.push(table[word_48050].vByte);
279
word_48050 = table[word_48050].vWord;
282
byte_49069 = byte_49068 = (byte)word_48050;
283
tokenList.push(word_48050);
285
// Write out any cached tokens
286
while (!tokenList.empty()) {
288
assert(bytesWritten <= re->uncompressedSize);
289
*destP++ = tokenList.pop();
292
assert(ctrCurrent < 0x1000);
293
table[ctrCurrent].vByte = byte_49069;
294
table[ctrCurrent].vWord = currentToken;
297
currentToken = word_48054;
298
if ((ctrCurrent >= ctrMax) && (bitReader.numBits != 12)) {
299
// Move to the next higher bit-rate
306
assert(bytesWritten == re->uncompressedSize);
312
* Finds the correct section and loads the specified resource within it
314
byte *TLib::getResource(ResourceType resType, uint16 resNum, uint16 rlbNum, bool suppressErrors) {
315
SectionList::iterator i = _sections.begin();
316
while ((i != _sections.end()) && ((*i).resType != resType || (*i).resNum != resNum))
318
if (i == _sections.end()) {
321
error("Unknown resource type %d num %d", resType, resNum);
324
loadSection((*i).fileOffset);
326
return getResource(rlbNum, suppressErrors);
329
void TLib::loadIndex() {
330
uint16 resNum, configId, fileOffset;
332
// Load the root resources section
335
// Get the single resource from it
336
const byte *pData = getResource(0);
337
const byte *p = pData;
341
// Loop through reading the entries
342
while ((resNum = READ_LE_UINT16(p)) != 0xffff) {
343
configId = READ_LE_UINT16(p + 2);
344
fileOffset = READ_LE_UINT16(p + 4);
349
se.resType = (ResourceType)(configId & 0x1f);
350
se.fileOffset = (((configId >> 5) & 0x7ff) << 16) | fileOffset;
352
_sections.push_back(se);
355
_memoryManager.deallocate(pData);
359
* Retrieves the specified palette resource and returns it's data
361
* @paletteNum Specefies the palette number
363
bool TLib::getPalette(int paletteNum, byte *palData, uint *startNum, uint *numEntries) {
364
// Get the specified palette
365
byte *dataIn = getResource(RES_PALETTE, 0, paletteNum, true);
369
*startNum = READ_LE_UINT16(dataIn);
370
*numEntries = READ_LE_UINT16(dataIn + 2);
371
assert((*startNum < 256) && ((*startNum + *numEntries) <= 256));
373
// Copy over the data
374
Common::copy(&dataIn[6], &dataIn[6 + *numEntries * 3], palData);
376
_memoryManager.deallocate(dataIn);
380
byte *TLib::getSubResource(int resNum, int rlbNum, int index, uint *size, bool suppressErrors) {
381
// Get the specified image set
382
byte *dataIn = getResource(RES_VISAGE, resNum, rlbNum);
387
error("Unknown sub resource %d/%d index %d", resNum, rlbNum, index);
390
int numEntries = READ_LE_UINT16(dataIn);
391
uint32 entryOffset = READ_LE_UINT32(dataIn + 2 + (index - 1) * 4);
392
uint32 nextOffset = (index == numEntries) ?
393
_memoryManager.getSize(dataIn) : READ_LE_UINT32(dataIn + 2 + index * 4);
394
*size = nextOffset - entryOffset;
395
assert(*size < (1024 * 1024));
397
byte *entry = _memoryManager.allocate2(*size);
398
Common::copy(&dataIn[entryOffset], &dataIn[nextOffset], entry);
400
_memoryManager.deallocate(dataIn);
405
* Retrieves a given message resource, and returns the specified message number
407
Common::String TLib::getMessage(int resNum, int lineNum, bool suppressErrors) {
408
byte *msgData = getResource(RES_MESSAGE, resNum, 0);
411
return Common::String();
413
error("Unknown message %d line %d", resNum, lineNum);
416
const char *srcP = (const char *)msgData;
417
while (lineNum-- > 0)
418
srcP += strlen(srcP) + 1;
420
Common::String result(srcP);
421
_memoryManager.deallocate(msgData);
425
/*--------------------------------------------------------------------------*/
427
ResourceManager::~ResourceManager() {
428
for (uint idx = 0; idx < _libList.size(); ++idx)
429
delete _libList[idx];
432
void ResourceManager::addLib(const Common::String &libName) {
433
assert(_libList.size() < 5);
435
_libList.push_back(new TLib(_vm->_memoryManager, libName));
438
byte *ResourceManager::getResource(uint16 id, bool suppressErrors) {
440
for (uint idx = 0; idx < _libList.size(); ++idx) {
441
result = _libList[idx]->getResource(id, true);
446
if (!result && !suppressErrors)
447
error("Could not find resource Id #%d", id);
451
byte *ResourceManager::getResource(ResourceType resType, uint16 resNum, uint16 rlbNum, bool suppressErrors) {
453
for (uint idx = 0; idx < _libList.size(); ++idx) {
454
result = _libList[idx]->getResource(resType, resNum, rlbNum, true);
459
if (!result && !suppressErrors)
460
error("Unknown resource type %d num %d", resType, resNum);
464
void ResourceManager::getPalette(int paletteNum, byte *palData, uint *startNum, uint *numEntries, bool suppressErrors) {
465
for (uint idx = 0; idx < _libList.size(); ++idx) {
466
if (_libList[idx]->getPalette(paletteNum, palData, startNum, numEntries))
471
error("Unknown palette resource %d", paletteNum);
475
byte *ResourceManager::getSubResource(int resNum, int rlbNum, int index, uint *size, bool suppressErrors) {
477
for (uint idx = 0; idx < _libList.size(); ++idx) {
478
result = _libList[idx]->getSubResource(resNum, rlbNum, index, size, true);
483
if (!result && !suppressErrors)
484
error("Unknown resource %d/%d index %d", resNum, rlbNum, index);
488
Common::String ResourceManager::getMessage(int resNum, int lineNum, bool suppressErrors) {
489
Common::String result;
491
for (uint idx = 0; idx < _libList.size(); ++idx) {
492
result = _libList[idx]->getMessage(resNum, lineNum, true);
498
error("Unknown message %d line %d", resNum, lineNum);
502
} // end of namespace tSage