File: resource.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (561 lines) | stat: -rw-r--r-- 19,396 bytes parent folder | download | duplicates (2)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

// RSC Resource file management module

#include "saga/saga.h"

#include "saga/actor.h"
#include "saga/animation.h"
#include "saga/interface.h"
#include "saga/music.h"
#include "saga/resource.h"
#include "saga/scene.h"
#include "saga/sndres.h"

#include "engines/advancedDetector.h"
#include "common/compression/powerpacker.h"

namespace Saga {

// Patch files. Files not found will be ignored
static const GamePatchDescription ITEPatch_Files[] = {
	{       "cave.mid", GAME_RESOURCEFILE,    9},
	{      "intro.mid", GAME_RESOURCEFILE,   10},
	{   "fvillage.mid", GAME_RESOURCEFILE,   11},
	{    "elkhall.mid", GAME_RESOURCEFILE,   12},
	{      "mouse.mid", GAME_RESOURCEFILE,   13},
	{   "darkclaw.mid", GAME_RESOURCEFILE,   14},
	{   "birdchrp.mid", GAME_RESOURCEFILE,   15},
	{   "orbtempl.mid", GAME_RESOURCEFILE,   16},
	{     "spooky.mid", GAME_RESOURCEFILE,   17},
	{    "catfest.mid", GAME_RESOURCEFILE,   18},
	{ "elkfanfare.mid", GAME_RESOURCEFILE,   19},
	{     "bcexpl.mid", GAME_RESOURCEFILE,   20},
	{   "boargtnt.mid", GAME_RESOURCEFILE,   21},
	{   "boarking.mid", GAME_RESOURCEFILE,   22},
	{   "explorea.mid", GAME_RESOURCEFILE,   23},
	{   "exploreb.mid", GAME_RESOURCEFILE,   24},
	{   "explorec.mid", GAME_RESOURCEFILE,   25},
	{   "sunstatm.mid", GAME_RESOURCEFILE,   26},
	{   "nitstrlm.mid", GAME_RESOURCEFILE,   27},
	{   "humruinm.mid", GAME_RESOURCEFILE,   28},
	{   "damexplm.mid", GAME_RESOURCEFILE,   29},
	{     "tychom.mid", GAME_RESOURCEFILE,   30},
	{     "kitten.mid", GAME_RESOURCEFILE,   31},
	{      "sweet.mid", GAME_RESOURCEFILE,   32},
	{   "brutalmt.mid", GAME_RESOURCEFILE,   33},
	{     "shiala.mid", GAME_RESOURCEFILE,   34},

	{       "wyrm.pak", GAME_RESOURCEFILE, 1529},
	{      "wyrm1.dlt", GAME_RESOURCEFILE, 1530},
	{      "wyrm2.dlt", GAME_RESOURCEFILE, 1531},
	{      "wyrm3.dlt", GAME_RESOURCEFILE, 1532},
	{      "wyrm4.dlt", GAME_RESOURCEFILE, 1533},
	{   "credit3n.dlt", GAME_RESOURCEFILE, 1796}, // PC
	{   "credit3m.dlt", GAME_RESOURCEFILE, 1796}, // Macintosh
	{   "credit4n.dlt", GAME_RESOURCEFILE, 1797}, // PC
	{   "credit4m.dlt", GAME_RESOURCEFILE, 1797}, // Macintosh
	{       "p2_a.voc", GAME_VOICEFILE,       4},
	{       "p2_a.iaf", GAME_VOICEFILE,       4},
	{             NULL,              0,       0}
};

static const GamePatchDescription ITEMacPatch_Files[] = {
	{       "wyrm.pak", GAME_RESOURCEFILE, 1529},
	{      "wyrm1.dlt", GAME_RESOURCEFILE, 1530},
	{      "wyrm2.dlt", GAME_RESOURCEFILE, 1531},
	{      "wyrm3.dlt", GAME_RESOURCEFILE, 1532},
	{      "wyrm4.dlt", GAME_RESOURCEFILE, 1533},
	{   "credit3m.dlt", GAME_RESOURCEFILE, 1796},
	{   "credit4m.dlt", GAME_RESOURCEFILE, 1797},
	{       "p2_a.iaf", GAME_VOICEFILE,       4},
	{             NULL,              0,       0}
};

static const GamePatchDescription *PatchLists[PATCHLIST_MAX] = {
	/* PATCHLIST_NONE */    nullptr,
	/* PATCHLIST_ITE */     ITEPatch_Files,
	/* PATCHLIST_ITE_MAC */ ITEMacPatch_Files
};

struct ITEAmigaIndex {
	uint32 fileOffset;
	uint32 numEntries;
};

struct ITEAmigaEXEDescriptor {
	ITEAmigaIndex voiceIndex;
	ITEAmigaIndex soundIndex;
};

bool ResourceContext::loadResIteAmigaSound(SagaEngine *_vm, int type) {
	Common::String exeName;

	for (const ADGameFileDescription *gameFileDescription = _vm->getFilesDescriptions();
		gameFileDescription->fileName; gameFileDescription++) {
		if (Common::String(gameFileDescription->fileName).hasSuffix(".exe"))
			exeName = gameFileDescription->fileName;
	}

	if (exeName.empty())
		return false;

	// Right now German and English ECS version have same offsets to
	// offset tables, no need to distinguish them
	static const ITEAmigaEXEDescriptor ecsDesc = {
		{ 0x56a8, 3730 },
		{ 0x90f0,   44 },
	};

	// Right now German and English ECS version have same offsets to
	// offset tables, no need to distinguish them
	static const ITEAmigaEXEDescriptor agaDesc = {
		{ 0x53a8, 3730 },
		{ 0x8df0,   44 },
	};
 
	const ITEAmigaEXEDescriptor *exedesc = _vm->isECS() ? &ecsDesc : &agaDesc;
	const ITEAmigaIndex& amigaIdx = _fileType & GAME_VOICEFILE ? exedesc->voiceIndex : exedesc->soundIndex;

	if (amigaIdx.numEntries <= 1)
		return false;

	_table.resize(amigaIdx.numEntries - 1);

	Common::File f;

	if(!f.open(exeName.c_str()))
		return false;

	f.seek(amigaIdx.fileOffset);

	for (uint32 i = 0; i < amigaIdx.numEntries - 1; i++) {
		ResourceData *resourceData = &_table[i];
		resourceData->offset = f.readUint32BE();
		resourceData->diskNum = -1;
	}

	uint32 lastEntry = f.readUint32BE();

	for (uint32 i = 0; i < amigaIdx.numEntries - 2; i++) {
		_table[i].size = _table[i + 1].offset - _table[i].offset;
	}

	_table[amigaIdx.numEntries - 2].size = lastEntry - _table[amigaIdx.numEntries - 2].offset;

	return true;
}

bool ResourceContext::loadResIteAmiga(SagaEngine *_vm, int type, bool isFloppy) {
	if (_fileType & (GAME_VOICEFILE | GAME_SOUNDFILE))
		return loadResIteAmigaSound(_vm, type);
	_file->seek(0);
	uint16 resourceCount = _file->readUint16BE();
	uint16 scriptCount = _file->readUint16BE();
	uint32 count = (type &  GAME_SCRIPTFILE) ? scriptCount : resourceCount;
	uint32 extraOffset = isFloppy ? 1024 : 0;

	if (type &  GAME_SCRIPTFILE)
		_file->seek(resourceCount * 10, SEEK_CUR);

	_table.resize(count);

	for (uint32 i = 0; i < count; i++) {
		ResourceData *resourceData = &_table[i];
		resourceData->offset = _file->readUint32BE() + extraOffset;
		resourceData->size = _file->readUint32BE();
		resourceData->diskNum = _file->readUint16BE();
	}

	return true;
}

bool ResourceContext::loadResV1() {
	size_t i;
	bool result;
	byte tableInfo[RSC_TABLEINFO_SIZE];
	ByteArray tableBuffer;
	uint32 count;
	uint32 resourceTableOffset;
	ResourceData *resourceData;

	if (_fileSize < RSC_MIN_FILESIZE) {
		warning("ResourceContext::loadResV1(): Incorrect contextSize: %d < %d", (int) _fileSize, RSC_MIN_FILESIZE);
		return false;
	}

	_file->seek(-RSC_TABLEINFO_SIZE, SEEK_END);

	if (_file->read(tableInfo, RSC_TABLEINFO_SIZE) != RSC_TABLEINFO_SIZE) {
		warning("ResourceContext::loadResV1(): Incorrect table size: %d for %s", RSC_TABLEINFO_SIZE, _fileName);
		return false;
	}

	Common::MemoryReadStreamEndian readS(tableInfo, RSC_TABLEINFO_SIZE, _isBigEndian);

	resourceTableOffset = readS.readUint32();
	count = readS.readUint32();

	// Check for sane table offset
	if (resourceTableOffset != _fileSize - RSC_TABLEINFO_SIZE - RSC_TABLEENTRY_SIZE * count) {
		warning("ResourceContext::loadResV1(): Incorrect tables offset: %d != %d for %s, endian is %d",
			resourceTableOffset, (int)_fileSize - RSC_TABLEINFO_SIZE - RSC_TABLEENTRY_SIZE * count,
			_fileName, _isBigEndian);
		return false;
	}

	// Load resource table
	tableBuffer.resize(RSC_TABLEENTRY_SIZE * count);

	_file->seek(resourceTableOffset, SEEK_SET);

	result = (_file->read(tableBuffer.getBuffer(), tableBuffer.size()) == tableBuffer.size());
	if (result) {
		_table.resize(count);

		Common::MemoryReadStreamEndian readS1(tableBuffer.getBuffer(), tableBuffer.size(), _isBigEndian);

		for (i = 0; i < count; i++) {
			resourceData = &_table[i];
			resourceData->offset = readS1.readUint32();
			resourceData->size = readS1.readUint32();
			// Sanity check
			if ((resourceData->offset > (uint)_fileSize) || (resourceData->size > (uint)_fileSize)) {
				result = false;
				break;
			}
		}
	}

	return result;
}

bool ResourceContext::load(SagaEngine *vm, Resource *resource) {
	if (_fileName == nullptr) // IHNM special case
		return true;

	_file.reset(Common::MacResManager::openFileOrDataFork(_fileName));
	if (!_file)
		return false;

	_fileSize = _file->size();
	_isBigEndian = vm->isBigEndian();

	if (_fileType & GAME_SWAPENDIAN)
		_isBigEndian = !_isBigEndian;

	if ((_fileType & (GAME_MACBINARY | GAME_MUSICFILE_GM)) == (GAME_MACBINARY | GAME_MUSICFILE_GM)) {
		_macRes.reset(new Common::MacResManager());
		if (!_macRes->open(_fileName))
			return false;
		// Unpacking MacBinary packed MIDI files happens on-demand
		return true;
	}


	if (!loadRes(vm, _fileType))
		return false;

	GamePatchList index = vm->getPatchList();
	if (index < PATCHLIST_MAX && index > PATCHLIST_NONE)
		processPatches(resource, PatchLists[index]);

	// Close the file if it's part of a series of files.
	// This prevents having all voice files open in IHNM for no reason, as each chapter uses
	// a different voice file.
	if (_serial > 0)
		closeFile();

	return true;
}

Resource::Resource(SagaEngine *vm): _vm(vm) {
}

Resource::~Resource() {
	clearContexts();
}

void Resource::addContext(const char *fileName, uint16 fileType, bool isCompressed, int serial) {
	ResourceContext *context;
	context = createContext();
	context->_fileName = fileName;
	context->_fileType = fileType;
	context->_isCompressed = isCompressed;
	context->_serial = serial;
	_contexts.push_back(context);
}

bool Resource::createContexts() {
	bool soundFileInArray = false;
	bool voiceFileInArray = false;

	_vm->_voiceFilesExist = true;

	struct SoundFileInfo {
		int gameId;
		char fileName[40];
		bool isCompressed;
		uint16 voiceFileAddType;
	};

	for (const ADGameFileDescription *gameFileDescription = _vm->getFilesDescriptions();
		gameFileDescription->fileName; gameFileDescription++) {
		if (gameFileDescription->fileType > 0)
			addContext(gameFileDescription->fileName, gameFileDescription->fileType);
		if ((gameFileDescription->fileType & GAME_RESOURCEFILE) && _vm->getPlatform() == Common::kPlatformAmiga && _vm->getGameId() == GID_ITE)
			addContext(gameFileDescription->fileName, (gameFileDescription->fileType & ~GAME_RESOURCEFILE) | GAME_SCRIPTFILE | GAME_SWAPENDIAN);
		if ((gameFileDescription->fileType & GAME_RESOURCEFILE) && _vm->getPlatform() == Common::kPlatformAmiga && _vm->getGameId() == GID_ITE)
			addContext(gameFileDescription->fileName, (gameFileDescription->fileType & ~GAME_RESOURCEFILE) | GAME_MUSICFILE_FM);
		if (gameFileDescription->fileType == GAME_SOUNDFILE) {
			soundFileInArray = true;
		}
		if (gameFileDescription->fileType == GAME_VOICEFILE) {
			voiceFileInArray = true;
		}
	}

	//// Detect and add SFX files ////////////////////////////////////////////////
	SoundFileInfo sfxFiles[] = {
		{	GID_ITE,	"sounds.rsc",		false,	0	},
		{	GID_ITE,	"sounds.cmp",		true,	0	},
		{	GID_ITE,	"soundsd.rsc",		false,	0	},
		{	GID_ITE,	"soundsd.cmp",		true,	0	},
#ifdef ENABLE_IHNM
		{	GID_IHNM,	"sfx.res",			false,	0	},
		{	GID_IHNM,	"sfx.cmp",			true,	0	},
#endif
		{	-1,			"",				false,	0	}
	};

	_soundFileName[0] = 0;
	if (!soundFileInArray) {
		for (SoundFileInfo *curSoundFile = sfxFiles; (curSoundFile->gameId != -1); curSoundFile++) {
			if (curSoundFile->gameId != _vm->getGameId()) continue;
			if (!Common::File::exists(curSoundFile->fileName)) continue;
			Common::strcpy_s(_soundFileName, curSoundFile->fileName);
			uint32 flags = GAME_SOUNDFILE;

			if (_vm->getFeatures() & GF_SOME_MAC_RESOURCES)
				flags |= GAME_SWAPENDIAN;
			addContext(_soundFileName, flags, curSoundFile->isCompressed);
			break;
		}
	}

	//// Detect and add voice files /////////////////////////////////////////////
	SoundFileInfo voiceFiles[] = {
		{	GID_ITE,	"voices.rsc",					false	,	(uint16)((_soundFileName[0] == 0) ? GAME_SOUNDFILE : 0)},
		{	GID_ITE,	"voices.cmp",					true	,	(uint16)((_soundFileName[0] == 0) ? GAME_SOUNDFILE : 0)},
		{	GID_ITE,	"voicesd.rsc",					false	,	(uint16)((_soundFileName[0] == 0) ? GAME_SOUNDFILE : 0)},
		{	GID_ITE,	"voicesd.cmp",					true	,	(uint16)((_soundFileName[0] == 0) ? GAME_SOUNDFILE : 0)},
		// The resources in the Wyrmkeep combined Windows/Mac/Linux CD version are little endian, but
		// the voice file is big endian. If we got such a version with mixed files, mark this voice file
		// as big endian
		{	GID_ITE,	"inherit the earth voices",		false	,	(uint16)(_vm->isBigEndian() ? 0 : GAME_SWAPENDIAN)},
		{	GID_ITE,	"inherit the earth voices.cmp",	true	,	(uint16)(_vm->isBigEndian() ? 0 : GAME_SWAPENDIAN)},
		{	GID_ITE,	"ite voices",				false	,	GAME_MACBINARY},
#ifdef ENABLE_IHNM
		{	GID_IHNM,	"voicess.res",					false	,	0},
		{	GID_IHNM,	"voicess.cmp",					true	,	0},
		{	GID_IHNM,	"voicesd.res",					false	,	0},
		{	GID_IHNM,	"voicesd.cmp",					true	,	0},
#endif
		{	-1,			"",							false	,	0}
	};

	// Detect and add voice files
	_voicesFileName[0][0] = 0;
	if (!voiceFileInArray) {
		for (SoundFileInfo *curSoundFile = voiceFiles; (curSoundFile->gameId != -1); curSoundFile++) {
			if (curSoundFile->gameId != _vm->getGameId()) continue;
			bool exists = curSoundFile->voiceFileAddType & GAME_MACBINARY ? Common::MacResManager::exists(curSoundFile->fileName) : Common::File::exists(curSoundFile->fileName);
			if (!exists) continue;

			Common::strcpy_s(_voicesFileName[0], curSoundFile->fileName);
			addContext(_voicesFileName[0], GAME_VOICEFILE | curSoundFile->voiceFileAddType, curSoundFile->isCompressed);

			// Special cases
			if (!scumm_stricmp(curSoundFile->fileName, "voicess.res") ||
			    !scumm_stricmp(curSoundFile->fileName, "voicess.cmp")) {
				// IHNM has multiple voice files
				for (size_t i = 1; i <= 6; i++) { // voices1-voices6
					Common::sprintf_s(_voicesFileName[i], "voices%i.%s", (uint)i, curSoundFile->isCompressed ? "cmp" : "res");
					if (i == 4) {
						// The German and French versions of IHNM don't have Nimdok's chapter,
						// therefore the voices file for that chapter is missing
						if (!Common::File::exists(_voicesFileName[i])) {
							continue;
						}
					}
					addContext(_voicesFileName[i], GAME_VOICEFILE, curSoundFile->isCompressed, i);
				}
			}
			break;
		}
	}

	if (!voiceFileInArray && _voicesFileName[0][0] == 0) {
#ifdef ENABLE_IHNM
		if (_vm->getGameId() == GID_IHNM && _vm->isMacResources()) {
			// The Macintosh version of IHNM has no voices.res, and it has all
			// its voice files in subdirectories, so don't do anything here
			_contexts.push_back(new VoiceResourceContext_RES());
		} else {
#endif
			warning("No voice file found, voices will be disabled");
			_vm->_voicesEnabled = false;
			_vm->_subtitlesEnabled = true;
			_vm->_voiceFilesExist = false;
#ifdef ENABLE_IHNM
		}
#endif
	}

	//// Detect and add music files /////////////////////////////////////////
	SoundFileInfo musicFiles[] = {
		{	GID_ITE,	"music.rsc",	false,	0	},
		{	GID_ITE,	"music.cmp",	true,	0	},
		{	GID_ITE,	"musicd.rsc",	false,	0	},
		{	GID_ITE,	"musicd.cmp",	true,	0	},
		{	-1,			"",			false	,	0}
	};

	// Check for digital music in ITE

	for (SoundFileInfo *curSoundFile = musicFiles; (curSoundFile->gameId != -1); curSoundFile++) {
		if (curSoundFile->gameId != _vm->getGameId()) continue;
		if (!Common::File::exists(curSoundFile->fileName)) continue;
		Common::strcpy_s(_musicFileName, curSoundFile->fileName);
		uint32 flags = GAME_DIGITALMUSICFILE;

		if (_vm->getFeatures() & GF_SOME_MAC_RESOURCES)
			flags |= GAME_SWAPENDIAN;
		addContext(_musicFileName, flags, curSoundFile->isCompressed);
		break;
	}

	for (ResourceContextList::iterator i = _contexts.begin(); i != _contexts.end(); ++i) {
		if (!(*i)->load(_vm, this)) {
			warning("Cannot load context %s", (*i)->_fileName);
			return false;
		}
	}
	return true;
}

void Resource::clearContexts() {
	ResourceContextList::iterator i = _contexts.begin();
	while (i != _contexts.end()) {
		ResourceContext * context = *i;
		i = _contexts.erase(i);
		delete context;
	}
}

#define ID_MIDI     MKTAG('M','i','d','i')

void Resource::loadResource(ResourceContext *context, uint32 resourceId, ByteArray &resourceBuffer) {
	if ((context->_fileType & (GAME_MACBINARY | GAME_MUSICFILE_GM)) == (GAME_MACBINARY | GAME_MUSICFILE_GM) && context->_macRes) {
		Common::SeekableReadStream *s = context->_macRes->getResource(ID_MIDI, resourceId);
		if (!s)
			return;
		resourceBuffer.resize(s->size());
		s->read(resourceBuffer.getBuffer(), s->size());

		delete s;
		
		return;
	}

	ResourceData *resourceData = context->getResourceData(resourceId);
	Common::SeekableReadStream *file = nullptr;
	uint32 resourceOffset = resourceData->offset;
	Common::File actualFile;

	if (resourceData->diskNum == -1)
		file = context->getFile(resourceData);
	else {
		Common::String fileName = context->_fileName;
		int sz = fileName.size();
		while(sz > 0 && fileName[sz - 1] != '.')
			sz--;
		if (sz > 0)
			sz--;
		Common::Path filePath;
		if (_vm->getFeatures() & GF_ITE_FLOPPY)
			filePath = Common::Path(Common::String::format("%s%02d.adf", fileName.substr(0, sz).c_str(), resourceData->diskNum + 1));
		else
			filePath = Common::Path(Common::String::format("%s.%03d", fileName.substr(0, sz).c_str(), resourceData->diskNum));
		if (!actualFile.open(filePath))
			error("Resource::loadResource() failed to open %s", filePath.toString().c_str());
		file = &actualFile;
	}

	debug(8, "loadResource %d 0x%X:0x%X", resourceId, resourceOffset, uint(resourceData->size));
	resourceBuffer.resize(resourceData->size);

	file->seek((long)resourceOffset, SEEK_SET);

	if (file->read(resourceBuffer.getBuffer(), resourceBuffer.size()) != resourceBuffer.size()) {
		error("Resource::loadResource() failed to read");
	}

	if (_vm->getPlatform() == Common::Platform::kPlatformAmiga &&
	    resourceBuffer.size() >= 16 && READ_BE_UINT32(resourceBuffer.getBuffer()) == MKTAG('H', 'E', 'A', 'D')
	    && READ_BE_UINT32(resourceBuffer.getBuffer() + 12) == MKTAG('P', 'A', 'C', 'K')) {
		uint32 unpackedLen = READ_BE_UINT32(resourceBuffer.getBuffer() + 4);
		uint32 packedLen = READ_BE_UINT32(resourceBuffer.getBuffer() + 8);
		uint32 actualUncompressedLen = 0;
		if (packedLen != resourceBuffer.size() - 20) {
			warning("Compressed size mismatch in resource %d: %d vs %d", resourceId, packedLen, resourceBuffer.size() - 20);
		}
		byte *uncompressed = Common::PowerPackerStream::unpackBuffer(resourceBuffer.getBuffer() + 12, packedLen + 8, actualUncompressedLen);
		if (uncompressed == nullptr || unpackedLen != actualUncompressedLen) {
			warning("Uncompressed size mismatch in resource %d: %d vs %d", resourceId, unpackedLen, actualUncompressedLen);
		}

		if (context->fileType() & GAME_MUSICFILE_FM) {
			byte b = 0;
			for (uint32 i = 0; i < unpackedLen; i++) {
				b += uncompressed[i];
				uncompressed[i] = b;
			}
		}

		// TODO: Use move semantics
		resourceBuffer = ByteArray(uncompressed, actualUncompressedLen);
		delete[] uncompressed;
	}
}

ResourceContext *Resource::getContext(uint16 fileType, int serial) {
	for (ResourceContextList::const_iterator i = _contexts.begin(); i != _contexts.end(); ++i) {
		ResourceContext * context = *i;
		if ((context->fileType() & fileType) && (context->serial() == serial)) {
			return context;
		}
	}
	return nullptr;
}

} // End of namespace Saga