aboutsummaryrefslogtreecommitdiff
path: root/engines/kyra/resource.cpp
blob: b8d7eb3f6433b69d9199308f9cdc4444b6593052 (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
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
/* 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$
 *
 */

#include "common/stdafx.h"
#include "common/config-manager.h"
#include "common/endian.h"
#include "common/file.h"
#include "common/fs.h"
#include "common/hash-str.h"
#include "common/func.h"
#include "common/algorithm.h"

#include "gui/message.h"

#include "kyra/resource.h"
#include "kyra/script.h"
#include "kyra/wsamovie.h"
#include "kyra/screen.h"

namespace Kyra {

namespace {
struct ResFilenameEqual : public Common::UnaryFunction<ResourceFile*, bool> {
	uint _filename;
	ResFilenameEqual(uint file) : _filename(file) {}

	bool operator()(const ResourceFile *f) {
		return f->filename() == _filename;
	}
};
} // end of anonymous namespace

Resource::Resource(KyraEngine *vm) {
	_vm = vm;

	if (_vm->game() == GI_KYRA1) {
		// we're loading KYRA.DAT here too (but just for Kyrandia 1)
		if (!loadPakFile("KYRA.DAT") || !StaticResource::checkKyraDat()) {
			GUI::MessageDialog errorMsg("You're missing the 'KYRA.DAT' file or it got corrupted, (re)get it from the ScummVM website");
			errorMsg.runModal();
			error("You're missing the 'KYRA.DAT' file or it got corrupted, (re)get it from the ScummVM website");
		}

		// We only need kyra.dat for the demo.
		if (_vm->gameFlags().isDemo)
			return;

		// only VRM file we need in the *whole* game for kyra1
		if (_vm->gameFlags().isTalkie)
			loadPakFile("CHAPTER1.VRM");
	} else if (_vm->game() == GI_KYRA3) {
		// load the installation package file for kyra3
		INSFile *insFile = new INSFile("WESTWOOD.001");
		assert(insFile);
		if (!insFile->isValid())
			error("'WESTWOOD.001' file not found or corrupt");
		_pakfiles.push_back(insFile);
	}

	FSList fslist;
	FilesystemNode dir(ConfMan.get("path"));

	if (!dir.listDir(fslist, FilesystemNode::kListFilesOnly))
		error("invalid game path '%s'", dir.path().c_str());

	if (_vm->game() == GI_KYRA1 && _vm->gameFlags().isTalkie) {
		static const char *list[] = {
			"ADL.PAK", "CHAPTER1.VRM", "COL.PAK", "FINALE.PAK", "INTRO1.PAK", "INTRO2.PAK",
			"INTRO3.PAK", "INTRO4.PAK", "MISC.PAK",	"SND.PAK", "STARTUP.PAK", "XMI.PAK",
			"CAVE.APK", "DRAGON1.APK", "DRAGON2.APK", "LAGOON.APK"
		};

		Common::for_each(list, list + ARRAYSIZE(list), Common::bind1st(Common::mem_fun(&Resource::loadPakFile), this));
		Common::for_each(_pakfiles.begin(), _pakfiles.end(), Common::bind2nd(Common::mem_fun(&ResourceFile::protect), true));
	} else {
		for (FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
			Common::String filename = file->name();
			filename.toUppercase();

			// No real PAK file!
			if (filename == "TWMUSIC.PAK")
				continue;

			if (filename.hasSuffix("PAK") || filename.hasSuffix("APK")) {
				if (!loadPakFile(file->name()))
					error("couldn't open pakfile '%s'", file->name().c_str());
			}
		}

		if (_vm->gameFlags().platform == Common::kPlatformFMTowns) {
			uint unloadHash = (_vm->gameFlags().lang == Common::EN_ANY) ? Common::hashit_lower("JMC.PAK") : Common::hashit_lower("EMC.PAK");

			ResIterator file = Common::find_if(_pakfiles.begin(), _pakfiles.end(), ResFilenameEqual(unloadHash));
			if (file != _pakfiles.end()) {
				delete *file;
				_pakfiles.erase(file);
			}
		}
	}
}

Resource::~Resource() {
	for (ResIterator start = _pakfiles.begin() ;start != _pakfiles.end(); ++start) {
		delete *start;
		*start = 0;
	}
}

bool Resource::loadPakFile(const Common::String &filename) {
	ResIterator listFile = Common::find_if(_pakfiles.begin(), _pakfiles.end(), ResFilenameEqual(Common::hashit_lower(filename)));
	if (listFile != _pakfiles.end()) {
		(*listFile)->open();
		return true;
	}

	const bool isKyraDat = filename.equalsIgnoreCase("KYRA.DAT");
	uint32 size = 0;
	
	Common::File handle;
	if (!getFileHandle(filename.c_str(), &size, handle)) {
		(!isKyraDat ? error : warning)("couldn't load file: '%s'", filename.c_str());
		return false;
	}

	PAKFile *file = new PAKFile(filename.c_str(), handle.name(), handle, (_vm->gameFlags().platform == Common::kPlatformAmiga) && !isKyraDat);
	handle.close();

	if (!file)
		return false;

	if (!file->isValid()) {
		error("'%s' is no valid pak file", filename.c_str());
		delete file;
		return false;
	}

	_pakfiles.push_back(file);
	return true;
}

void Resource::unloadPakFile(const Common::String &filename) {
	ResIterator pak = Common::find_if(_pakfiles.begin(), _pakfiles.end(), ResFilenameEqual(Common::hashit_lower(filename)));
	if (pak != _pakfiles.end())
		(*pak)->close();
}

bool Resource::isInPakList(const Common::String &filename) const {
	return (Common::find_if(_pakfiles.begin(), _pakfiles.end(), ResFilenameEqual(Common::hashit_lower(filename))) != _pakfiles.end());
}

uint8 *Resource::fileData(const char *file, uint32 *size) const {
	Common::File fileHandle;

	if (size)
		*size = 0;

	// test to open it in the main dir
	if (fileHandle.open(file)) {
		uint32 fileSize = fileHandle.size();
		uint8 *buffer = new uint8[fileSize];
		assert(buffer);

		fileHandle.read(buffer, fileSize);

		if (size)
			*size = fileSize;

		return buffer;
	} else {
		// opens the file inside a PAK File
		uint fileHash = Common::hashit_lower(file);
		for (ConstResIterator cur = _pakfiles.begin(); cur != _pakfiles.end(); ++cur) {
			if (!(*cur)->isOpen())
				continue;


			uint8* result = (*cur)->getFile(fileHash);

			if (result) {
				uint32 fileSize = (*cur)->getFileSize(fileHash);

				if (!fileSize)
					continue;

				if (size)
					*size = fileSize;

				return result;
			}


		}
	}

	return 0;
}

bool Resource::getFileHandle(const char *file, uint32 *size, Common::File &filehandle) {
	filehandle.close();

	if (filehandle.open(file)) {
		if (size)
			*size = filehandle.size();
		return true;
	}

	uint fileHash = Common::hashit_lower(file);
	for (ResIterator start = _pakfiles.begin() ;start != _pakfiles.end(); ++start) {
		if (!(*start)->isOpen())
			continue;

		if ((*start)->getFileHandle(fileHash, filehandle)) {
			uint32 tSize = (*start)->getFileSize(fileHash);
		
			if (!tSize)
				continue;

			if (size)
				*size = tSize;

			return true;
		}
	}
	
	return false;
}

uint32 Resource::getFileSize(const char *file) const {
	Common::File temp;
	if (temp.open(file))
		return temp.size();

	uint fileHash = Common::hashit_lower(file);
	for (ConstResIterator start = _pakfiles.begin() ;start != _pakfiles.end(); ++start) {
		if (!(*start)->isOpen())
			continue;

		uint32 size = (*start)->getFileSize(fileHash);
		
		if (size)
			return size;
	}

	return 0;
}

bool Resource::loadFileToBuf(const char *file, void *buf, uint32 maxSize) {
	Common::File tempHandle;
	uint32 size = 0;

	if (!getFileHandle(file, &size, tempHandle))
		return false;

	if (size > maxSize)
		return false;

	memset(buf, 0, maxSize);
	tempHandle.read(buf, size);

	return true;
}

///////////////////////////////////////////
// Pak file manager
PAKFile::PAKFile(const char *file, const char *physfile, Common::File &pakfile, bool isAmiga) : ResourceFile() {
	_open = false;

	if (!pakfile.isOpen()) {
		debug(3, "couldn't open pakfile '%s'\n", file);
		return;
	}

	uint32 off = pakfile.pos();
	uint32 filesize = pakfile.size();

	uint32 pos = 0, startoffset = 0, endoffset = 0;

	if (isAmiga)
		startoffset = pakfile.readUint32BE();
	else
		startoffset = pakfile.readUint32LE();

	if (startoffset > filesize) {
		warning("PAK file '%s' is corrupted", file);
		return;
	}

	pos += 4;

	while (pos < filesize) {
		PakChunk chunk;
		uint8 buffer[64];
		uint32 nameLength;
		
		// Move to the position of the next file entry
		pakfile.seek(pos);
		
		// Read in the header
		if (pakfile.read(&buffer, 64) < 5) {
			warning("PAK file '%s' is corrupted", file);
			return;
		}
		
		// Quit now if we encounter an empty string
		if (!(*((const char*)buffer)))
			break;

		chunk._name = Common::hashit_lower((const char*)buffer);
		nameLength = strlen((const char*)buffer) + 1; 

		if (nameLength > 60) {
			warning("PAK file '%s' is corrupted", file);
			return;
		}

		if (isAmiga)
			endoffset = READ_BE_UINT32(buffer + nameLength);
		else
			endoffset = READ_LE_UINT32(buffer + nameLength);

		if (!endoffset) {
			endoffset = filesize;
		} else if (endoffset > filesize || startoffset > endoffset) {
			warning("PAK file '%s' is corrupted", file);
			return;
		}

		if (startoffset != endoffset) {
			chunk._start = startoffset;
			chunk._size = endoffset - startoffset;

			_files.push_back(chunk);
		}

		if (endoffset == filesize)
			break;

		startoffset = endoffset;
		pos += nameLength + 4;
	}

	_open = true;	
	_filename = Common::hashit_lower(file);
	_physfile = physfile;
	_physOffset = off;
}


PAKFile::~PAKFile() {
	_physfile.clear();
	_open = false;

	_files.clear();
}

uint8 *PAKFile::getFile(uint hash) const {
	ConstPakIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));
	if (file == _files.end())
		return 0;

	Common::File pakfile;
	if (!openFile(pakfile))
		return false;

	pakfile.seek(file->_start, SEEK_CUR);
	uint8 *buffer = new uint8[file->_size];
	assert(buffer);
	pakfile.read(buffer, file->_size);
	return buffer;
}

bool PAKFile::getFileHandle(uint hash, Common::File &filehandle) const {
	filehandle.close();

	ConstPakIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));
	if (file == _files.end())
		return false;

	if (!openFile(filehandle))
		return false;

	filehandle.seek(file->_start, SEEK_CUR);
	return true;
}

uint32 PAKFile::getFileSize(uint hash) const {
	ConstPakIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));
	return (file != _files.end()) ? file->_size : 0;
}

bool PAKFile::openFile(Common::File &filehandle) const {
	filehandle.close();

	if (!filehandle.open(_physfile))
		return false;

	filehandle.seek(_physOffset, SEEK_CUR);
	return true;
}

///////////////////////////////////////////
// Ins file manager
INSFile::INSFile(const char *file) : ResourceFile(), _files() {
	Common::File pakfile;
	_open = false;

	if (!pakfile.open(file)) {
		debug(3, "couldn't open insfile '%s'\n", file);
		return;
	}

	// thanks to eriktorbjorn for this code (a bit modified though)

	// skip first three bytes
	pakfile.seek(3);

	// first file is the index table
	uint32 filesize = pakfile.readUint32LE();

	Common::String temp = "";

	for (uint i = 0; i < filesize; ++i) {
		byte c = pakfile.readByte();

		if (c == '\\') {
			temp = "";
		} else if (c == 0x0D) {
			// line endings are CRLF
			c = pakfile.readByte();
			assert(c == 0x0A);
			++i;

			FileEntry newEntry;
			newEntry._name = Common::hashit_lower(temp.c_str());
			newEntry._start = 0;
			newEntry._size = 0;
			_files.push_back(newEntry);

			temp = "";
		} else {
			temp += (char)c;
		}
	}

	pakfile.seek(3);

	for (FileIterator start = _files.begin(); start != _files.end(); ++start) {
		filesize = pakfile.readUint32LE();
		start->_size = filesize;
		start->_start = pakfile.pos();
		pakfile.seek(filesize, SEEK_CUR);
	}

	_filename = Common::hashit_lower(file);
	_physfile = file;
	_open = true;
}

INSFile::~INSFile() {
	_open = false;

	_files.clear();
}

uint8 *INSFile::getFile(uint hash) const {
	ConstFileIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));
	if (file == _files.end())
		return 0;

	Common::File pakfile;
	if (!pakfile.open(_physfile))
		return false;

	pakfile.seek(file->_start);
	uint8 *buffer = new uint8[file->_size];
	assert(buffer);
	pakfile.read(buffer, file->_size);
	return buffer;
}

bool INSFile::getFileHandle(uint hash, Common::File &filehandle) const {
	ConstFileIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));

	if (file == _files.end())
		return false;

	if (!filehandle.open(_physfile)) 
		return false;

	filehandle.seek(file->_start, SEEK_CUR);
	return true;
}

uint32 INSFile::getFileSize(uint hash) const {
	ConstFileIterator file = Common::find_if(_files.begin(), _files.end(), Common::bind2nd(Common::EqualTo<uint>(), hash));
	return (file != _files.end()) ? file->_size : 0;
}

} // end of namespace Kyra