aboutsummaryrefslogtreecommitdiff
path: root/engines/sword2/resman.cpp
blob: fa9c396ef3ecdbb4141195b450a711b3bff137b4 (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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1994-1998 Revolution Software Ltd.
 *
 * 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/file.h"
#include "common/system.h"

#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/console.h"
#include "sword2/logic.h"
#include "sword2/memory.h"
#include "sword2/resman.h"
#include "sword2/router.h"
#include "sword2/screen.h"
#include "sword2/sound.h"

#define Debug_Printf _vm->_debugger->DebugPrintf

namespace Sword2 {

// Welcome to the easy resource manager - written in simple code for easy
// maintenance
//
// The resource compiler will create two files
//
//	resource.inf which is a list of ascii cluster file names
//	resource.tab which is a table which tells us which cluster a resource
//	is located in and the number within the cluster

enum {
	BOTH		= 0x0,		// Cluster is on both CDs
	CD1		= 0x1,		// Cluster is on CD1 only
	CD2		= 0x2,		// Cluster is on CD2 only
	LOCAL_CACHE	= 0x4,		// Cluster is cached on HDD
	LOCAL_PERM	= 0x8		// Cluster is on HDD.
};

struct CdInf {
	uint8 clusterName[20];	// Null terminated cluster name.
	uint8 cd;		// Cd cluster is on and whether it is on the local drive or not.
};

ResourceManager::ResourceManager(Sword2Engine *vm) {
	_vm = vm;

	_totalClusters = 0;
	_resList = NULL;
	_resConvTable = NULL;
	_cacheStart = NULL;
	_cacheEnd = NULL;
	_usedMem = 0;
}

ResourceManager::~ResourceManager() {
	Resource *res = _cacheStart;
	while (res) {
		_vm->_memory->memFree(res->ptr);
		res = res->next;
	}
	for (uint i = 0; i < _totalClusters; i++)
		free(_resFiles[i].entryTab);
	free(_resList);
	free(_resConvTable);
}


bool ResourceManager::init() {
	uint32 i, j;

	// Until proven differently, assume we're on CD 1. This is so the start
	// dialog will be able to play any music at all.

	setCD(1);

	// We read in the resource info which tells us the names of the
	// resource cluster files ultimately, although there might be groups
	// within the clusters at this point it makes no difference. We only
	// wish to know what resource files there are and what is in each

	Common::File file;

	if (!file.open("resource.inf")) {
		GUIErrorMessage("Broken Sword II: Cannot open resource.inf");
		return false;
	}

	// The resource.inf file is a simple text file containing the names of
	// all the resource files.

	while (1) {
		char *buf = _resFiles[_totalClusters].fileName;
		uint len = sizeof(_resFiles[_totalClusters].fileName);

		if (!file.readLine(buf, len))
			break;

		int pos = strlen(buf);
		if (buf[pos - 1] == 0x0A)
			buf[pos - 1] = 0;

		_resFiles[_totalClusters].numEntries = -1;
		_resFiles[_totalClusters].entryTab = NULL;
		if (++_totalClusters >= MAX_res_files) {
			GUIErrorMessage("Broken Sword II: Too many entries in resource.inf");
			return false;
		}
	}

	file.close();

	// Now load in the binary id to res conversion table
	if (!file.open("resource.tab")) {
		GUIErrorMessage("Broken Sword II: Cannot open resource.tab");
		return false;
	}

	// Find how many resources
	uint32 size = file.size();

	_totalResFiles = size / 4;

	// Table seems ok so malloc some space
	_resConvTable = (uint16 *)malloc(size);

	for (i = 0; i < size / 2; i++)
		_resConvTable[i] = file.readUint16LE();

	if (file.eos() || file.err()) {
		file.close();
		GUIErrorMessage("Broken Sword II: Cannot read resource.tab");
		return false;
	}

	file.close();

	// Check that we have cd.inf file, unless we are running PSX
	// version, which has all files on one disc.

	if (!file.open("cd.inf") && !Sword2Engine::isPsx()) {
		GUIErrorMessage("Broken Sword II: Cannot open cd.inf");
		return false;
	}

	CdInf *cdInf = new CdInf[_totalClusters];

	for (i = 0; i < _totalClusters; i++) {

		if (Sword2Engine::isPsx()) { // We are running PSX version, artificially fill CdInf structure
			cdInf[i].cd = CD1;
		} else { // We are running PC version, read cd.inf file
			file.read(cdInf[i].clusterName, sizeof(cdInf[i].clusterName));

			cdInf[i].cd = file.readByte();

			if (file.eos() || file.err()) {
				delete[] cdInf;
				file.close();
				GUIErrorMessage("Broken Sword II: Cannot read cd.inf");
				return false;
			}

		}

		// It has been reported that there are two different versions
		// of the cd.inf file: One where all clusters on CD also have
		// the LOCAL_CACHE bit set. This bit is no longer used. To
		// avoid future problems, let's normalize the flag once and for
		// all here.

		if (cdInf[i].cd & LOCAL_PERM)
			cdInf[i].cd = 0;
		else if (cdInf[i].cd & CD1)
			cdInf[i].cd = 1;
		else if (cdInf[i].cd & CD2)
			cdInf[i].cd = 2;
		else
			cdInf[i].cd = 0;

		// Any file on "CD 0" may be needed at all times. Verify that
		// it exists. Any other missing cluster will be requested with
		// an "insert CD" message. Of course, the file may still vanish
		// during game-play (oh, that wascally wabbit!) in which case
		// the resource manager will print a fatal error.

		if (cdInf[i].cd == 0 && !Common::File::exists((char *)cdInf[i].clusterName)) {
			GUIErrorMessage("Broken Sword II: Cannot find " + Common::String((char *)cdInf[i].clusterName));
			delete[] cdInf;
			return false;
		}
	}

	file.close();

	// We check the presence of resource files in cd.inf
	// This is ok in PC version, but in PSX version we don't
	// have cd.inf so we'll have to skip this.
	if (!Sword2Engine::isPsx()) {
		for (i = 0; i < _totalClusters; i++) {
			for (j = 0; j < _totalClusters; j++) {
				if (scumm_stricmp((char *)cdInf[j].clusterName, _resFiles[i].fileName) == 0)
					break;
			}

			if (j == _totalClusters) {
				delete[] cdInf;
				GUIErrorMessage(Common::String(_resFiles[i].fileName) + " is not in cd.inf");
				return false;
			}

			_resFiles[i].cd = cdInf[j].cd;
		}
	}

	delete[] cdInf;

	debug(1, "%d resources in %d cluster files", _totalResFiles, _totalClusters);
	for (i = 0; i < _totalClusters; i++)
		debug(2, "filename of cluster %d: -%s (%d)", i, _resFiles[i].fileName, _resFiles[i].cd);

	_resList = (Resource *)malloc(_totalResFiles * sizeof(Resource));

	for (i = 0; i < _totalResFiles; i++) {
		_resList[i].ptr = NULL;
		_resList[i].size = 0;
		_resList[i].refCount = 0;
		_resList[i].prev = _resList[i].next = NULL;
	}

	return true;
}

/**
 * Returns the address of a resource. Loads if not in memory. Retains a count.
 */
byte *ResourceManager::openResource(uint32 res, bool dump) {
	assert(res < _totalResFiles);


	// FIXME: In PSX edition, not all top menu icons are present (TOP menu is not used).
	// Though, at present state, the engine still ask for the resources.
	if (Sword2Engine::isPsx()) { // We need to "rewire" missing icons
		if (res == 342) res = 364; // Rewire RESTORE ICON to SAVE ICON
	}

	// Is the resource in memory already? If not, load it.

	if (!_resList[res].ptr) {
		// Fetch the correct file and read in the correct portion.
		uint16 cluFileNum = _resConvTable[res * 2]; // points to the number of the ascii filename

		assert(cluFileNum != 0xffff);

		// Relative resource within the file
		// First we have to find the file via the _resConvTable
		uint16 actual_res = _resConvTable[(res * 2) + 1];

		debug(5, "openResource %s res %d", _resFiles[cluFileNum].fileName, res);

		// If we're loading a cluster that's only available from one
		// of the CDs, remember which one so that we can play the
		// correct speech and music.

		if (Sword2Engine::isPsx()) // We have only one disk in PSX version
			setCD(CD1);
		else
			setCD(_resFiles[cluFileNum].cd);

		// Actually, as long as the file can be found we don't really
		// care which CD it's on. But if we can't find it, keep asking
		// for the CD until we do.

		Common::File *file = openCluFile(cluFileNum);

		if (_resFiles[cluFileNum].entryTab == NULL) {
			// we didn't read from this file before, get its index table
			readCluIndex(cluFileNum, file);
		}

		assert(_resFiles[cluFileNum].entryTab);

		uint32 pos = _resFiles[cluFileNum].entryTab[actual_res * 2 + 0];
		uint32 len = _resFiles[cluFileNum].entryTab[actual_res * 2 + 1];

		file->seek(pos, SEEK_SET);

		debug(6, "res len %d", len);

		// Ok, we know the length so try and allocate the memory.
		_resList[res].ptr = _vm->_memory->memAlloc(len, res);
		_resList[res].size = len;
		_resList[res].refCount = 0;

		file->read(_resList[res].ptr, len);

		debug(3, "Loaded resource '%s' (%d) from '%s' on CD %d (%d)", fetchName(_resList[res].ptr), res, _resFiles[cluFileNum].fileName, getCD(), _resFiles[cluFileNum].cd);

		if (dump) {
			char buf[256];
			const char *tag;

			switch (fetchType(_resList[res].ptr)) {
			case ANIMATION_FILE:
				tag = "anim";
				break;
			case SCREEN_FILE:
				tag = "layer";
				break;
			case GAME_OBJECT:
				tag = "object";
				break;
			case WALK_GRID_FILE:
				tag = "walkgrid";
				break;
			case GLOBAL_VAR_FILE:
				tag = "globals";
				break;
			case PARALLAX_FILE_null:
				tag = "parallax";	// Not used!
				break;
			case RUN_LIST:
				tag = "runlist";
				break;
			case TEXT_FILE:
				tag = "text";
				break;
			case SCREEN_MANAGER:
				tag = "screen";
				break;
			case MOUSE_FILE:
				tag = "mouse";
				break;
			case WAV_FILE:
				tag = "wav";
				break;
			case ICON_FILE:
				tag = "icon";
				break;
			case PALETTE_FILE:
				tag = "palette";
				break;
			default:
				tag = "unknown";
				break;
			}

			sprintf(buf, "dumps/%s-%d.dmp", tag, res);

			if (!Common::File::exists(buf)) {
				Common::DumpFile out;
				if (out.open(buf))
					out.write(_resList[res].ptr, len);
			}
		}

		// close the cluster
		file->close();
		delete file;

		_usedMem += len;
		checkMemUsage();
	} else if (_resList[res].refCount == 0)
		removeFromCacheList(_resList + res);

	_resList[res].refCount++;

	return _resList[res].ptr;
}

void ResourceManager::closeResource(uint32 res) {
	assert(res < _totalResFiles);

	// Don't try to close the resource if it has already been forcibly
	// closed, e.g. by fnResetGlobals().

	if (_resList[res].ptr == NULL)
		return;

	assert(_resList[res].refCount > 0);

	_resList[res].refCount--;
	if (_resList[res].refCount == 0)
		addToCacheList(_resList + res);

	// It's tempting to free the resource immediately when refCount
	// reaches zero, but that'd be a mistake. Closing a resource does not
	// mean "I'm not going to use this resource any more". It means that
	// "the next time I use this resource I'm going to ask for a new
	// pointer to it".
	//
	// Since the original memory manager had to deal with memory
	// fragmentation, keeping a resource open - and thus locked down to a
	// specific memory address - was considered a bad thing.
}

void ResourceManager::removeFromCacheList(Resource *res) {
	if (_cacheStart == res)
		_cacheStart = res->next;

	if (_cacheEnd == res)
		_cacheEnd = res->prev;

	if (res->prev)
		res->prev->next = res->next;
	if (res->next)
		res->next->prev = res->prev;
	res->prev = res->next = NULL;
}

void ResourceManager::addToCacheList(Resource *res) {
	res->prev = NULL;
	res->next = _cacheStart;
	if (_cacheStart)
		_cacheStart->prev = res;
	_cacheStart = res;
	if (!_cacheEnd)
		_cacheEnd = res;
}

Common::File *ResourceManager::openCluFile(uint16 fileNum) {
	Common::File *file = new Common::File;
	while (!file->open(_resFiles[fileNum].fileName)) {
		// HACK: We have to check for this, or it'll be impossible to
		// quit while the game is asking for the user to insert a CD.
		// But recovering from this situation gracefully is just too
		// much trouble, so quit now.
		if (_vm->shouldQuit())
			g_system->quit();

		// If the file is supposed to be on hard disk, or we're
		// playing a demo, then we're in trouble if the file
		// can't be found!

		if ((_vm->_features & GF_DEMO) || _resFiles[fileNum].cd == 0)
			error("Could not find '%s'", _resFiles[fileNum].fileName);

		askForCD(_resFiles[fileNum].cd);
	}
	return file;
}

void ResourceManager::readCluIndex(uint16 fileNum, Common::File *file) {
	// we didn't read from this file before, get its index table
	assert(_resFiles[fileNum].entryTab == NULL);
	assert(file);

	// 1st DWORD of a cluster is an offset to the look-up table
	uint32 table_offset = file->readUint32LE();
	debug(6, "table offset = %d", table_offset);
	uint32 tableSize = file->size() - table_offset; // the table is stored at the end of the file
	file->seek(table_offset);

	assert((tableSize % 8) == 0);
	_resFiles[fileNum].entryTab = (uint32 *)malloc(tableSize);
	_resFiles[fileNum].numEntries = tableSize / 8;

	assert(_resFiles[fileNum].entryTab);

	file->read(_resFiles[fileNum].entryTab, tableSize);
	if (file->eos() || file->err())
		error("unable to read index table from file %s", _resFiles[fileNum].fileName);

#ifdef SCUMM_BIG_ENDIAN
	for (int tabCnt = 0; tabCnt < _resFiles[fileNum].numEntries * 2; tabCnt++)
		_resFiles[fileNum].entryTab[tabCnt] = FROM_LE_32(_resFiles[fileNum].entryTab[tabCnt]);
#endif
}

/**
 * Returns true if resource is valid, otherwise false.
 */

bool ResourceManager::checkValid(uint32 res) {
	// Resource number out of range
	if (res >= _totalResFiles)
		return false;

	// Points to the number of the ascii filename
	uint16 parent_res_file = _resConvTable[res * 2];

	// Null & void resource
	if (parent_res_file == 0xffff)
		return false;

	return true;
}

/**
 * Fetch resource type
 */

uint8 ResourceManager::fetchType(byte *ptr) {
	if (!Sword2Engine::isPsx()) {
		return ptr[0];
	} else { // in PSX version, some files got a "garbled" resource header, with type stored in ninth byte
		if (ptr[0]) {
			return ptr[0];
		} else if (ptr[8]) {
			return ptr[8];
		} else  {            // In PSX version there is no resource header for audio files,
			return WAV_FILE; // but hopefully all audio files got first 16 bytes zeroed,
		}                    // Allowing us to check for this condition.
							 // Alas, this doesn't work with PSX DEMO audio files.

	}
}

/**
 * Returns the total file length of a resource - i.e. all headers are included
 * too.
 */

uint32 ResourceManager::fetchLen(uint32 res) {
	if (_resList[res].ptr)
		return _resList[res].size;

	// Does this ever happen?
	warning("fetchLen: Resource %u is not loaded; reading length from file", res);

	// Points to the number of the ascii filename
	uint16 parent_res_file = _resConvTable[res * 2];

	// relative resource within the file
	uint16 actual_res = _resConvTable[(res * 2) + 1];

	// first we have to find the file via the _resConvTable
	// open the cluster file

	if (_resFiles[parent_res_file].entryTab == NULL) {
		Common::File *file = openCluFile(parent_res_file);
		readCluIndex(parent_res_file, file);
		delete file;
	}
	return _resFiles[parent_res_file].entryTab[actual_res * 2 + 1];
}

void ResourceManager::checkMemUsage() {
	while (_usedMem > MAX_MEM_CACHE) {
		// we're using up more memory than we wanted to. free some old stuff.
		// Newly loaded objects are added to the start of the list,
		// we start freeing from the end, to free the oldest items first
		if (_cacheEnd) {
			Resource *tmp = _cacheEnd;
			assert((tmp->refCount == 0) && (tmp->ptr) && (tmp->next == NULL));
			removeFromCacheList(tmp);

			_vm->_memory->memFree(tmp->ptr);
			tmp->ptr = NULL;
			_usedMem -= tmp->size;
		} else {
			warning("%d bytes of memory used, but cache list is empty", _usedMem);
			return;
		}
	}
}

void ResourceManager::remove(int res) {
	if (_resList[res].ptr) {
		removeFromCacheList(_resList + res);

		_vm->_memory->memFree(_resList[res].ptr);
		_resList[res].ptr = NULL;
		_resList[res].refCount = 0;
		_usedMem -= _resList[res].size;
	}
}

/**
 * Remove all res files from memory - ready for a total restart. This includes
 * the player object and global variables resource.
 */

void ResourceManager::removeAll() {
	// We need to clear the FX queue, because otherwise the sound system
	// will still believe that the sound resources are in memory. We also
	// need to kill the movie lead-in/out.

	_vm->_sound->clearFxQueue(true);

	for (uint i = 0; i < _totalResFiles; i++)
		remove(i);
}

/**
 * Remove all resources from memory.
 */

void ResourceManager::killAll(bool wantInfo) {
	int nuked = 0;

	// We need to clear the FX queue, because otherwise the sound system
	// will still believe that the sound resources are in memory. We also
	// need to kill the movie lead-in/out.

	_vm->_sound->clearFxQueue(true);

	for (uint i = 0; i < _totalResFiles; i++) {
		// Don't nuke the global variables or the player object!
		if (i == 1 || i == CUR_PLAYER_ID)
			continue;

		if (_resList[i].ptr) {
			if (wantInfo)
				Debug_Printf("Nuked %5d: %s\n", i, fetchName(_resList[i].ptr));

			remove(i);
			nuked++;
		}
	}

	if (wantInfo)
		Debug_Printf("Expelled %d resources\n", nuked);
}

/**
 * Like killAll but only kills objects (except George & the variable table of
 * course) - ie. forcing them to reload & restart their scripts, which
 * simulates the effect of a save & restore, thus checking that each object's
 * re-entrant logic works correctly, and doesn't cause a statuette to
 * disappear forever, or some plaster-filled holes in sand to crash the game &
 * get James in trouble again.
 */

void ResourceManager::killAllObjects(bool wantInfo) {
	int nuked = 0;

	for (uint i = 0; i < _totalResFiles; i++) {
		// Don't nuke the global variables or the player object!
		if (i == 1 || i == CUR_PLAYER_ID)
			continue;

		if (_resList[i].ptr) {
			if (fetchType(_resList[i].ptr) == GAME_OBJECT) {
				if (wantInfo)
					Debug_Printf("Nuked %5d: %s\n", i, fetchName(_resList[i].ptr));

				remove(i);
				nuked++;
			}
		}
	}

	if (wantInfo)
		Debug_Printf("Expelled %d resources\n", nuked);
}

void ResourceManager::askForCD(int cd) {
	byte *textRes;

	// Stop any music from playing - so the system no longer needs the
	// current CD - otherwise when we take out the CD, Windows will
	// complain!

	_vm->_sound->stopMusic(true);

	textRes = openResource(2283);
	_vm->_screen->displayMsg(_vm->fetchTextLine(textRes, 5 + cd) + 2, 0);
	closeResource(2283);

	// The original code probably determined automagically when the correct
	// CD had been inserted, but our backend doesn't support that, and
	// anyway I don't know if all systems allow that sort of thing. So we
	// wait for the user to press any key instead, or click the mouse.
	//
	// But just in case we ever try to identify the CDs by their labels,
	// they should be:
	//
	// CD1: "RBSII1" (or "PCF76" for the PCF76 version, whatever that is)
	// CD2: "RBSII2"
}

} // End of namespace Sword2