1 /* ScummVM - Graphic Adventure Engine
2  *
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.
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22 
23 #include "common/str.h"
24 #ifndef MACOSX
25 #include "common/config-manager.h"
26 #endif
27 
28 #include "scumm/charset.h"
29 #include "scumm/dialogs.h"
30 #include "scumm/file.h"
31 #include "scumm/imuse/imuse.h"
32 #include "scumm/imuse_digi/dimuse.h"
33 #include "scumm/he/intern_he.h"
34 #include "scumm/object.h"
35 #include "scumm/resource.h"
36 #include "scumm/scumm.h"
37 #include "scumm/scumm_v5.h"
38 #include "scumm/scumm_v8.h"
39 #include "scumm/sound.h"
40 #include "scumm/util.h"
41 #include "scumm/verbs.h"
42 
43 namespace Scumm {
44 
45 enum {
46 	RF_LOCK = 0x80,
47 	RF_USAGE = 0x7F,
48 	RF_USAGE_MAX = RF_USAGE,
49 
50 	RS_MODIFIED = 0x10,
51 	RF_OFFHEAP = 0x40
52 };
53 
54 
55 
56 extern const char *nameOfResType(ResType type);
57 
58 static uint16 newTag2Old(uint32 newTag);
59 static const byte *findResourceSmall(uint32 tag, const byte *searchin);
60 
61 static bool checkTryMedia(BaseScummFile *handle);
62 
63 
64 /* Open a room */
openRoom(const int room)65 void ScummEngine::openRoom(const int room) {
66 	bool result;
67 	byte encByte = 0;
68 
69 	debugC(DEBUG_GENERAL, "openRoom(%d)", room);
70 	assert(room >= 0);
71 
72 	/* Don't load the same room again */
73 	if (_lastLoadedRoom == room)
74 		return;
75 	_lastLoadedRoom = room;
76 
77 	/* Room -1 means close file */
78 	if (room == -1) {
79 		deleteRoomOffsets();
80 		_fileHandle->close();
81 		return;
82 	}
83 
84 	// Load the disk numer / room offs (special case for room 0 exists because
85 	// room 0 contains the data which is used to create the roomno / roomoffs
86 	// tables -- hence obviously we mustn't use those when loading room 0.
87 	const uint32 diskNumber = room ? _res->_types[rtRoom][room]._roomno : 0;
88 	const uint32 room_offs = room ? _res->_types[rtRoom][room]._roomoffs : 0;
89 
90 	// FIXME: Since room_offs is const, clearly the following loop either
91 	// is never entered, or loops forever (if it wasn't for the return/error
92 	// statements in it, that is). -> This should be cleaned up!
93 	// Maybe we should re-enabled the looping properly, to deal with disc
94 	// changes in COMI ?
95 	while (room_offs != RES_INVALID_OFFSET) {
96 
97 		if (room_offs != 0 && room != 0 && _game.heversion < 98) {
98 			_fileOffset = _res->_types[rtRoom][room]._roomoffs;
99 			return;
100 		}
101 
102 		Common::String filename(generateFilename(room));
103 
104 		// Determine the encryption, if any.
105 		if (_game.features & GF_USE_KEY) {
106 			if (_game.version <= 3)
107 				encByte = 0xFF;
108 			else if ((_game.version == 4) && (room == 0 || room >= 900))
109 				encByte = 0;
110 			else
111 				encByte = 0x69;
112 		} else
113 			encByte = 0;
114 
115 		if (room > 0 && (_game.version == 8))
116 			VAR(VAR_CURRENTDISK) = diskNumber;
117 
118 		// Try to open the file
119 		result = openResourceFile(filename, encByte);
120 
121 		if (result) {
122 			if (room == 0)
123 				return;
124 			deleteRoomOffsets();
125 			readRoomsOffsets();
126 			_fileOffset = _res->_types[rtRoom][room]._roomoffs;
127 
128 			if (_fileOffset != 8)
129 				return;
130 
131 			error("Room %d not in %s", room, filename.c_str());
132 			return;
133 		}
134 		askForDisk(filename.c_str(), diskNumber);
135 	}
136 
137 	do {
138 		char buf[16];
139 		snprintf(buf, sizeof(buf), "%.3d.lfl", room);
140 		encByte = 0;
141 		if (openResourceFile(buf, encByte))
142 			break;
143 		askForDisk(buf, diskNumber);
144 	} while (1);
145 
146 	deleteRoomOffsets();
147 	_fileOffset = 0;		// start of file
148 }
149 
closeRoom()150 void ScummEngine::closeRoom() {
151 	if (_lastLoadedRoom != -1) {
152 		_lastLoadedRoom = -1;
153 		deleteRoomOffsets();
154 		_fileHandle->close();
155 	}
156 }
157 
158 /** Delete the currently loaded room offsets. */
deleteRoomOffsets()159 void ScummEngine::deleteRoomOffsets() {
160 	for (int i = 0; i < _numRooms; i++) {
161 		if (_res->_types[rtRoom][i]._roomoffs != RES_INVALID_OFFSET)
162 			_res->_types[rtRoom][i]._roomoffs = 0;
163 	}
164 }
165 
166 /** Read room offsets */
readRoomsOffsets()167 void ScummEngine::readRoomsOffsets() {
168 	if (_game.features & GF_SMALL_HEADER) {
169 		_fileHandle->seek(12, SEEK_SET);	// Directly searching for the room offset block would be more generic...
170 	} else {
171 		_fileHandle->seek(16, SEEK_SET);
172 	}
173 
174 	int num = _fileHandle->readByte();
175 	while (num--) {
176 		int room = _fileHandle->readByte();
177 		int offset =  _fileHandle->readUint32LE();
178 		if (_res->_types[rtRoom][room]._roomoffs != RES_INVALID_OFFSET) {
179 			_res->_types[rtRoom][room]._roomoffs = offset;
180 		}
181 	}
182 }
183 
openFile(BaseScummFile & file,const Common::String & filename,bool resourceFile)184 bool ScummEngine::openFile(BaseScummFile &file, const Common::String &filename, bool resourceFile) {
185 	bool result = false;
186 
187 	if (!_containerFile.empty()) {
188 		file.close();
189 		file.open(_containerFile);
190 		assert(file.isOpen());
191 
192 		result = file.openSubFile(filename);
193 	}
194 
195 	if (!result) {
196 		file.close();
197 		result = file.open(filename);
198 	}
199 
200 	return result;
201 }
202 
openResourceFile(const Common::String & filename,byte encByte)203 bool ScummEngine::openResourceFile(const Common::String &filename, byte encByte) {
204 	debugC(DEBUG_GENERAL, "openResourceFile(%s)", filename.c_str());
205 
206 	if (openFile(*_fileHandle, filename, true)) {
207 		_fileHandle->setEnc(encByte);
208 		return true;
209 	}
210 	return false;
211 }
212 
askForDisk(const char * filename,int disknum)213 void ScummEngine::askForDisk(const char *filename, int disknum) {
214 	char buf[128];
215 
216 	if (_game.version == 8) {
217 #ifdef ENABLE_SCUMM_7_8
218 		char result;
219 
220 		_imuseDigital->stopAllSounds();
221 
222 #ifdef MACOSX
223 		sprintf(buf, "Cannot find file: '%s'\nPlease insert disc %d.\nPress OK to retry, Quit to exit", filename, disknum);
224 #else
225 		sprintf(buf, "Cannot find file: '%s'\nInsert disc %d into drive %s\nPress OK to retry, Quit to exit", filename, disknum, ConfMan.get("path").c_str());
226 #endif
227 
228 		result = displayMessage("Quit", "%s", buf);
229 		if (!result) {
230 			error("Cannot find file: '%s'", filename);
231 		}
232 #endif
233 	} else {
234 		sprintf(buf, "Cannot find file: '%s'", filename);
235 		InfoDialog dialog(this, (char *)buf);
236 		runDialog(dialog);
237 		error("Cannot find file: '%s'", filename);
238 	}
239 }
240 
readIndexFile()241 void ScummEngine::readIndexFile() {
242 	uint32 blocktype, itemsize;
243 	int numblock = 0;
244 
245 	debugC(DEBUG_GENERAL, "readIndexFile()");
246 
247 	closeRoom();
248 	openRoom(0);
249 
250 	if (_game.version <= 5) {
251 		// Figure out the sizes of various resources
252 		while (true) {
253 			blocktype = _fileHandle->readUint32BE();
254 			itemsize = _fileHandle->readUint32BE();
255 			if (_fileHandle->eos() || _fileHandle->err())
256 				break;
257 			switch (blocktype) {
258 			case MKTAG('D','O','B','J'):
259 				_numGlobalObjects = _fileHandle->readUint16LE();
260 				itemsize -= 2;
261 				break;
262 			case MKTAG('D','R','O','O'):
263 				_numRooms = _fileHandle->readUint16LE();
264 				itemsize -= 2;
265 				break;
266 
267 			case MKTAG('D','S','C','R'):
268 				_numScripts = _fileHandle->readUint16LE();
269 				itemsize -= 2;
270 				break;
271 
272 			case MKTAG('D','C','O','S'):
273 				_numCostumes = _fileHandle->readUint16LE();
274 				itemsize -= 2;
275 				break;
276 
277 			case MKTAG('D','S','O','U'):
278 				_numSounds = _fileHandle->readUint16LE();
279 				itemsize -= 2;
280 				break;
281 			}
282 			_fileHandle->seek(itemsize - 8, SEEK_CUR);
283 		}
284 		_fileHandle->seek(0, SEEK_SET);
285 	}
286 
287 	if (checkTryMedia(_fileHandle)) {
288 		displayMessage(NULL, "You're trying to run game encrypted by ActiveMark. This is not supported.");
289 		quitGame();
290 
291 		return;
292 	}
293 
294 	while (true) {
295 		blocktype = _fileHandle->readUint32BE();
296 		itemsize = _fileHandle->readUint32BE();
297 
298 		if (_fileHandle->eos() || _fileHandle->err())
299 			break;
300 
301 		numblock++;
302 		debug(2, "Reading index block of type '%s', size %d", tag2str(blocktype), itemsize);
303 		readIndexBlock(blocktype, itemsize);
304 	}
305 
306 //  if (numblock!=9)
307 //    error("Not enough blocks read from directory");
308 
309 	closeRoom();
310 }
311 
312 
313 
314 #define TRYMEDIA_MARK_LEN 6
315 
checkTryMedia(BaseScummFile * handle)316 bool checkTryMedia(BaseScummFile *handle) {
317 	byte buf[TRYMEDIA_MARK_LEN];
318 	bool matched = true;
319 	const byte magic[2][TRYMEDIA_MARK_LEN] =
320 		{{ 0x00,  'T', 'M', 'S', 'A', 'M' },
321 		 { 'i',   '=', '$', ':', '(', '$' }};  // Same but 0x69 xored
322 
323 	handle->read(buf, TRYMEDIA_MARK_LEN);
324 
325 	for (int i = 0; i < 2; i++) {
326 		matched = true;
327 		for (int j = 0; j < TRYMEDIA_MARK_LEN; j++)
328 			if (buf[j] != magic[i][j]) {
329 				matched = false;
330 				break;
331 			}
332 
333 		if (matched)
334 			break;
335 	}
336 
337 	if (matched)
338 		return true;
339 
340 	handle->seek(0, SEEK_SET);
341 
342 	return false;
343 }
344 
345 
346 #ifdef ENABLE_SCUMM_7_8
readIndexBlock(uint32 blocktype,uint32 itemsize)347 void ScummEngine_v7::readIndexBlock(uint32 blocktype, uint32 itemsize) {
348 	int num;
349 	char *ptr;
350 	switch (blocktype) {
351 	case MKTAG('A','N','A','M'):		// Used by: The Dig, FT
352 		num = _fileHandle->readUint16LE();
353 		ptr = (char *)malloc(num * 9);
354 		_fileHandle->read(ptr, num * 9);
355 		_imuseDigital->setAudioNames(num, ptr);
356 		break;
357 
358 	case MKTAG('D','R','S','C'):		// Used by: COMI
359 		readResTypeList(rtRoomScripts);
360 		break;
361 
362 	default:
363 		ScummEngine::readIndexBlock(blocktype, itemsize);
364 	}
365 }
366 #endif
367 
readIndexBlock(uint32 blocktype,uint32 itemsize)368 void ScummEngine_v70he::readIndexBlock(uint32 blocktype, uint32 itemsize) {
369 	int i;
370 	switch (blocktype) {
371 	case MKTAG('D','I','R','I'):
372 		readResTypeList(rtRoomImage);
373 		break;
374 
375 	case MKTAG('D','I','R','M'):
376 		readResTypeList(rtImage);
377 		break;
378 
379 	case MKTAG('D','I','R','T'):
380 		readResTypeList(rtTalkie);
381 		break;
382 
383 	case MKTAG('D','L','F','L'):
384 		i = _fileHandle->readUint16LE();
385 		_fileHandle->seek(-2, SEEK_CUR);
386 		_heV7RoomOffsets = (byte *)calloc(2 + (i * 4), 1);
387 		_fileHandle->read(_heV7RoomOffsets, (2 + (i * 4)) );
388 		break;
389 
390 	case MKTAG('D','I','S','K'):
391 		i = _fileHandle->readUint16LE();
392 		_heV7DiskOffsets = (byte *)calloc(i, 1);
393 		_fileHandle->read(_heV7DiskOffsets, i);
394 		break;
395 
396 	case MKTAG('S','V','E','R'):
397 		// Index version number
398 		_fileHandle->seek(itemsize - 8, SEEK_CUR);
399 		break;
400 
401 	case MKTAG('I','N','I','B'):
402 		_fileHandle->seek(itemsize - 8, SEEK_CUR);
403 		debug(2, "INIB index block not yet handled, skipping");
404 		break;
405 
406 	default:
407 		ScummEngine::readIndexBlock(blocktype, itemsize);
408 	}
409 }
410 
readIndexBlock(uint32 blocktype,uint32 itemsize)411 void ScummEngine::readIndexBlock(uint32 blocktype, uint32 itemsize) {
412 	int i;
413 	switch (blocktype) {
414 	case MKTAG('D','C','H','R'):
415 	case MKTAG('D','I','R','F'):
416 		readResTypeList(rtCharset);
417 		break;
418 
419 	case MKTAG('D','O','B','J'):
420 		readGlobalObjects();
421 		break;
422 
423 	case MKTAG('R','N','A','M'):
424 		// Names of rooms. Maybe we should put them into a table, for use by the debugger?
425 		if (_game.heversion >= 80) {
426 			for (int room; (room = _fileHandle->readUint16LE()); ) {
427 				char buf[100];
428 				i = 0;
429 				for (byte s; (s = _fileHandle->readByte()) && i < ARRAYSIZE(buf) - 1; ) {
430 					buf[i++] = s;
431 				}
432 				buf[i] = 0;
433 				debug(5, "Room %d: '%s'", room, buf);
434 			}
435 		} else {
436 			for (int room; (room = _fileHandle->readByte()); ) {
437 				char buf[10];
438 				_fileHandle->read(buf, 9);
439 				buf[9] = 0;
440 				for (i = 0; i < 9; i++)
441 					buf[i] ^= 0xFF;
442 				debug(5, "Room %d: '%s'", room, buf);
443 			}
444 		}
445 		break;
446 
447 	case MKTAG('D','R','O','O'):
448 	case MKTAG('D','I','R','R'):
449 		readResTypeList(rtRoom);
450 		break;
451 
452 	case MKTAG('D','S','C','R'):
453 	case MKTAG('D','I','R','S'):
454 		readResTypeList(rtScript);
455 		break;
456 
457 	case MKTAG('D','C','O','S'):
458 	case MKTAG('D','I','R','C'):
459 		readResTypeList(rtCostume);
460 		break;
461 
462 	case MKTAG('M','A','X','S'):
463 		readMAXS(itemsize);
464 		allocateArrays();
465 		break;
466 
467 	case MKTAG('D','I','R','N'):
468 	case MKTAG('D','S','O','U'):
469 		readResTypeList(rtSound);
470 		break;
471 
472 	case MKTAG('A','A','R','Y'):
473 		readArrayFromIndexFile();
474 		break;
475 
476 	default:
477 		error("Bad ID %04X('%s') found in index file directory", blocktype,
478 				tag2str(blocktype));
479 	}
480 }
481 
readArrayFromIndexFile()482 void ScummEngine::readArrayFromIndexFile() {
483 	error("readArrayFromIndexFile() not supported in pre-V6 games");
484 }
485 
readResTypeList(ResType type)486 int ScummEngine::readResTypeList(ResType type) {
487 	uint num;
488 	ResId idx;
489 
490 	if (_game.version == 8)
491 		num = _fileHandle->readUint32LE();
492 	else
493 		num = _fileHandle->readUint16LE();
494 
495 	if (num != _res->_types[type].size()) {
496 		error("Invalid number of %ss (%d) in directory", nameOfResType(type), num);
497 	}
498 
499 	debug(2, "  readResTypeList(%s): %d entries", nameOfResType(type), num);
500 
501 
502 	for (idx = 0; idx < num; idx++) {
503 		_res->_types[type][idx]._roomno = _fileHandle->readByte();
504 	}
505 	for (idx = 0; idx < num; idx++) {
506 		_res->_types[type][idx]._roomoffs = _fileHandle->readUint32LE();
507 	}
508 
509 	return num;
510 }
511 
readResTypeList(ResType type)512 int ScummEngine_v70he::readResTypeList(ResType type) {
513 	uint num;
514 	ResId idx;
515 
516 	num = ScummEngine::readResTypeList(type);
517 
518 	if (type == rtRoom)
519 		for (idx = 0; idx < num; idx++) {
520 			_heV7RoomIntOffsets[idx] = _res->_types[rtRoom][idx]._roomoffs;
521 		}
522 
523 	for (idx = 0; idx < num; idx++) {
524 		// The globsize is currently not being used
525 		/*_res->_types[type][idx]._globsize =*/ _fileHandle->readUint32LE();
526 	}
527 
528 	return num;
529 }
530 
allocResTypeData(ResType type,uint32 tag,int num,ResTypeMode mode)531 void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTypeMode mode) {
532 	debug(2, "allocResTypeData(%s,%s,%d,%d)", nameOfResType(type), tag2str(TO_BE_32(tag)), num, mode);
533 	assert(type >= 0 && type < (int)(ARRAYSIZE(_types)));
534 
535 	if (num >= 8000)
536 		error("Too many %s resources (%d) in directory", nameOfResType(type), num);
537 
538 	_types[type]._mode = mode;
539 	_types[type]._tag = tag;
540 
541 	// If there was data in there, let's clear it out completely. This is important
542 	// in case we are restarting the game.
543 	_types[type].clear();
544 	_types[type].resize(num);
545 
546 /*
547 	TODO: Use multiple Resource subclasses, one for each res mode; then,
548 	given them serializability.
549 	if (mode) {
550 		_types[type].roomno = (byte *)calloc(num, sizeof(byte));
551 		_types[type].roomoffs = (uint32 *)calloc(num, sizeof(uint32));
552 	}
553 
554 	if (_vm->_game.heversion >= 70) {
555 		_types[type].globsize = (uint32 *)calloc(num, sizeof(uint32));
556 	}
557 */
558 }
559 
loadCharset(int no)560 void ScummEngine::loadCharset(int no) {
561 	int i;
562 	byte *ptr;
563 
564 	debugC(DEBUG_GENERAL, "loadCharset(%d)", no);
565 
566 	/* FIXME - hack around crash in Indy4 (occurs if you try to load after dieing) */
567 	if (_game.id == GID_INDY4 && no == 0)
568 		no = 1;
569 
570 	/* for Humongous catalogs */
571 	if (_game.heversion >= 70 && _numCharsets == 1) {
572 		debug(0, "Not loading charset as it doesn't seem to exist?");
573 		return;
574 	}
575 
576 	assert(no < (int)sizeof(_charsetData) / 16);
577 	assertRange(1, no, _numCharsets - 1, "charset");
578 
579 	ptr = getResourceAddress(rtCharset, no);
580 
581 	for (i = 0; i < 15; i++) {
582 		_charsetData[no][i + 1] = ptr[i + 14];
583 	}
584 }
585 
nukeCharset(int i)586 void ScummEngine::nukeCharset(int i) {
587 	assertRange(1, i, _numCharsets - 1, "charset");
588 	_res->nukeResource(rtCharset, i);
589 }
590 
ensureResourceLoaded(ResType type,ResId idx)591 void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) {
592 	debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", nameOfResType(type), idx);
593 
594 	if ((type == rtRoom) && idx > 0x7F && _game.version < 7 && _game.heversion <= 71) {
595 		idx = _resourceMapper[idx & 0x7F];
596 	}
597 
598 	// FIXME: This check used to be "idx==0". However, that causes
599 	// problems when using this function to ensure charset 0 is loaded.
600 	// This is done for many games, e.g. Zak256 or Indy3 (EGA and VGA).
601 	// For now we restrict the check to anything which is not a charset.
602 	// Question: Why was this check like that in the first place?
603 	// Answer: costumes with an index of zero in the newer games at least.
604 	// TODO: determine why the heck anything would try to load a costume
605 	// with id 0. Is that "normal", or is it caused by yet another bug in
606 	// our code base? After all we also have to add special cases for many
607 	// of our script opcodes that check for the (invalid) actor 0... so
608 	// maybe both issues are related...
609 	if (type != rtCharset && idx == 0)
610 		return;
611 
612 	if (idx <= _res->_types[type].size() && _res->_types[type][idx]._address)
613 		return;
614 
615 	loadResource(type, idx);
616 
617 	if (_game.version == 5 && type == rtRoom && (int)idx == _roomResource)
618 		VAR(VAR_ROOM_FLAG) = 1;
619 }
620 
loadResource(ResType type,ResId idx)621 int ScummEngine::loadResource(ResType type, ResId idx) {
622 	int roomNr;
623 	uint32 fileOffs;
624 	uint32 size, tag;
625 
626 	debugC(DEBUG_RESOURCE, "loadResource(%s,%d)", nameOfResType(type), idx);
627 
628 	if (type == rtCharset && (_game.features & GF_SMALL_HEADER)) {
629 		loadCharset(idx);
630 		return 1;
631 	}
632 
633 	roomNr = getResourceRoomNr(type, idx);
634 
635 	if (idx >= _res->_types[type].size())
636 		error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type].size(), roomNr);
637 
638 	if (roomNr == 0)
639 		roomNr = _roomResource;
640 
641 	fileOffs = getResourceRoomOffset(type, idx);
642 	if (fileOffs == RES_INVALID_OFFSET)
643 		return 0;
644 
645 	openRoom(roomNr);
646 
647 	_fileHandle->seek(fileOffs + _fileOffset, SEEK_SET);
648 
649 	if (_game.features & GF_OLD_BUNDLE) {
650 		if ((_game.version == 3) && !(_game.platform == Common::kPlatformAmiga) && (type == rtSound)) {
651 			return readSoundResourceSmallHeader(idx);
652 		} else {
653 			size = _fileHandle->readUint16LE();
654 			_fileHandle->seek(-2, SEEK_CUR);
655 		}
656 	} else if (_game.features & GF_SMALL_HEADER) {
657 		if (_game.version == 4)
658 			_fileHandle->seek(8, SEEK_CUR);
659 		size = _fileHandle->readUint32LE();
660 		tag = _fileHandle->readUint16LE();
661 		_fileHandle->seek(-6, SEEK_CUR);
662 		if ((type == rtSound) && !(_game.platform == Common::kPlatformAmiga) && !(_game.platform == Common::kPlatformFMTowns)) {
663 			return readSoundResourceSmallHeader(idx);
664 		}
665 	} else {
666 		if (type == rtSound) {
667 			return readSoundResource(idx);
668 		}
669 
670 		// Sanity check: Is this the right tag for this resource type?
671 		//
672 		// Currently disabled for newer HE games because they use different
673 		// tags. For example, for rtRoom, 'ROOM' changed to 'RMDA'; and for
674 		// rtImage, 'AWIZ' and 'MULT' can both occur simultaneously.
675 		// On the long run, it would be preferable to not turn this check off,
676 		// but instead to explicitly support the variations in the HE games.
677 		tag = _fileHandle->readUint32BE();
678 		if (tag != _res->_types[type]._tag && _game.heversion < 70) {
679 			error("Unknown res tag '%s' encountered (expected '%s') "
680 			        "while trying to load res (%s,%d) in room %d at %d+%d in file %s",
681 			        tag2str(tag), tag2str(_res->_types[type]._tag),
682 					nameOfResType(type), idx, roomNr,
683 					_fileOffset, fileOffs, _fileHandle->getName());
684 		}
685 
686 		size = _fileHandle->readUint32BE();
687 		_fileHandle->seek(-8, SEEK_CUR);
688 	}
689 	_fileHandle->read(_res->createResource(type, idx, size), size);
690 
691 	// dump the resource if requested
692 	if (_dumpScripts && type == rtScript) {
693 		dumpResource("script-", idx, getResourceAddress(rtScript, idx));
694 	}
695 
696 	if (_fileHandle->err() || _fileHandle->eos()) {
697 		error("Cannot read resource");
698 	}
699 
700 	return 1;
701 }
702 
getResourceRoomNr(ResType type,ResId idx)703 int ScummEngine::getResourceRoomNr(ResType type, ResId idx) {
704 	if (type == rtRoom && _game.heversion < 70)
705 		return idx;
706 	return _res->_types[type][idx]._roomno;
707 }
708 
getResourceRoomOffset(ResType type,ResId idx)709 uint32 ScummEngine::getResourceRoomOffset(ResType type, ResId idx) {
710 	if (type == rtRoom) {
711 		return (_game.version == 8) ? 8 : 0;
712 	}
713 	return _res->_types[type][idx]._roomoffs;
714 }
715 
getResourceRoomOffset(ResType type,ResId idx)716 uint32 ScummEngine_v70he::getResourceRoomOffset(ResType type, ResId idx) {
717 	if (type == rtRoom) {
718 		return _heV7RoomIntOffsets[idx];
719 	}
720 	return _res->_types[type][idx]._roomoffs;
721 }
722 
getResourceSize(ResType type,ResId idx)723 int ScummEngine::getResourceSize(ResType type, ResId idx) {
724 	byte *ptr = getResourceAddress(type, idx);
725 	assert(ptr);
726 	return _res->_types[type][idx]._size;
727 }
728 
getResourceAddress(ResType type,ResId idx)729 byte *ScummEngine::getResourceAddress(ResType type, ResId idx) {
730 	byte *ptr;
731 
732 	if (_game.heversion >= 80 && type == rtString)
733 		idx &= ~0x33539000;
734 
735 	if (!_res->validateResource("getResourceAddress", type, idx))
736 		return NULL;
737 
738 	// If the resource is missing, but loadable from the game data files, try to do so.
739 	if (!_res->_types[type][idx]._address && _res->_types[type]._mode != kDynamicResTypeMode) {
740 		ensureResourceLoaded(type, idx);
741 	}
742 
743 	ptr = (byte *)_res->_types[type][idx]._address;
744 	if (!ptr) {
745 		debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", nameOfResType(type), idx);
746 		return NULL;
747 	}
748 
749 	_res->setResourceCounter(type, idx, 1);
750 
751 	debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", nameOfResType(type), idx, (void *)ptr);
752 	return ptr;
753 }
754 
getStringAddress(ResId idx)755 byte *ScummEngine::getStringAddress(ResId idx) {
756 	byte *addr = getResourceAddress(rtString, idx);
757 	return addr;
758 }
759 
getStringAddress(ResId idx)760 byte *ScummEngine_v6::getStringAddress(ResId idx) {
761 	byte *addr = getResourceAddress(rtString, idx);
762 	if (addr == NULL)
763 		return NULL;
764 	// Skip over the ArrayHeader
765 	return addr + 6;
766 }
767 
getStringAddressVar(int i)768 byte *ScummEngine::getStringAddressVar(int i) {
769 	return getStringAddress(_scummVars[i]);
770 }
771 
increaseExpireCounter()772 void ResourceManager::increaseExpireCounter() {
773 	++_expireCounter;
774 	if (_expireCounter == 0) {	// overflow?
775 		increaseResourceCounters();
776 	}
777 }
778 
increaseResourceCounters()779 void ResourceManager::increaseResourceCounters() {
780 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
781 		ResId idx = _types[type].size();
782 		while (idx-- > 0) {
783 			byte counter = _types[type][idx].getResourceCounter();
784 			if (counter && counter < RF_USAGE_MAX) {
785 				setResourceCounter(type, idx, counter + 1);
786 			}
787 		}
788 	}
789 }
790 
setResourceCounter(ResType type,ResId idx,byte counter)791 void ResourceManager::setResourceCounter(ResType type, ResId idx, byte counter) {
792 	_types[type][idx].setResourceCounter(counter);
793 }
794 
setResourceCounter(byte counter)795 void ResourceManager::Resource::setResourceCounter(byte counter) {
796 	_flags &= RF_LOCK;	// Clear lower 7 bits, preserve the lock bit.
797 	_flags |= counter;	// Update the usage counter
798 }
799 
getResourceCounter() const800 byte ResourceManager::Resource::getResourceCounter() const {
801 	return _flags & RF_USAGE;
802 }
803 
804 /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */
805 #define SAFETY_AREA 2
806 
createResource(ResType type,ResId idx,uint32 size)807 byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) {
808 	debugC(DEBUG_RESOURCE, "_res->createResource(%s,%d,%d)", nameOfResType(type), idx, size);
809 
810 	if (!validateResource("allocating", type, idx))
811 		return NULL;
812 
813 	if (_vm->_game.version <= 2) {
814 		// Nuking and reloading a resource can be harmful in some
815 		// cases. For instance, Zak tries to reload the intro music
816 		// while it's playing. See bug #1253171.
817 
818 		if (_types[type][idx]._address && (type == rtSound || type == rtScript || type == rtCostume))
819 			return _types[type][idx]._address;
820 	}
821 
822 	nukeResource(type, idx);
823 
824 	expireResources(size);
825 
826 	byte *ptr = new byte[size + SAFETY_AREA];
827 	if (ptr == NULL) {
828 		error("createResource(%s,%d): Out of memory while allocating %d", nameOfResType(type), idx, size);
829 	}
830 
831 	memset(ptr, 0, size + SAFETY_AREA);
832 	_allocatedSize += size;
833 
834 	_types[type][idx]._address = ptr;
835 	_types[type][idx]._size = size;
836 	setResourceCounter(type, idx, 1);
837 	return ptr;
838 }
839 
Resource()840 ResourceManager::Resource::Resource() {
841 	_address = 0;
842 	_size = 0;
843 	_flags = 0;
844 	_status = 0;
845 	_roomno = 0;
846 	_roomoffs = 0;
847 }
848 
~Resource()849 ResourceManager::Resource::~Resource() {
850 	delete[] _address;
851 	_address = 0;
852 }
853 
nuke()854 void ResourceManager::Resource::nuke() {
855 	delete[] _address;
856 	_address = 0;
857 	_size = 0;
858 	_flags = 0;
859 	_status &= ~RS_MODIFIED;
860 }
861 
ResTypeData()862 ResourceManager::ResTypeData::ResTypeData() {
863 	_mode = kDynamicResTypeMode;
864 	_tag = 0;
865 }
866 
~ResTypeData()867 ResourceManager::ResTypeData::~ResTypeData() {
868 }
869 
ResourceManager(ScummEngine * vm)870 ResourceManager::ResourceManager(ScummEngine *vm) : _vm(vm) {
871 	_allocatedSize = 0;
872 	_maxHeapThreshold = 0;
873 	_minHeapThreshold = 0;
874 	_expireCounter = 0;
875 }
876 
~ResourceManager()877 ResourceManager::~ResourceManager() {
878 	freeResources();
879 }
880 
setHeapThreshold(int min,int max)881 void ResourceManager::setHeapThreshold(int min, int max) {
882 	assert(0 < max);
883 	assert(min <= max);
884 	_maxHeapThreshold = max;
885 	_minHeapThreshold = min;
886 }
887 
validateResource(const char * str,ResType type,ResId idx) const888 bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const {
889 	if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type].size()) {
890 		warning("%s Illegal Glob type %s (%d) num %d", str, nameOfResType(type), type, idx);
891 		return false;
892 	}
893 	return true;
894 }
895 
nukeResource(ResType type,ResId idx)896 void ResourceManager::nukeResource(ResType type, ResId idx) {
897 	byte *ptr = _types[type][idx]._address;
898 	if (ptr != NULL) {
899 		debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", nameOfResType(type), idx);
900 		_allocatedSize -= _types[type][idx]._size;
901 		_types[type][idx].nuke();
902 	}
903 }
904 
findResourceData(uint32 tag,const byte * ptr)905 const byte *ScummEngine::findResourceData(uint32 tag, const byte *ptr) {
906 	if (_game.features & GF_OLD_BUNDLE)
907 		error("findResourceData must not be used in GF_OLD_BUNDLE games");
908 	else if (_game.features & GF_SMALL_HEADER)
909 		ptr = findResourceSmall(tag, ptr);
910 	else
911 		ptr = findResource(tag, ptr);
912 
913 	if (ptr == NULL)
914 		return NULL;
915 	return ptr + _resourceHeaderSize;
916 }
917 
getResourceDataSize(const byte * ptr) const918 int ScummEngine::getResourceDataSize(const byte *ptr) const {
919 	if (ptr == NULL)
920 		return 0;
921 
922 	if (_game.features & GF_OLD_BUNDLE)
923 		return READ_LE_UINT16(ptr) - _resourceHeaderSize;
924 	else if (_game.features & GF_SMALL_HEADER)
925 		return READ_LE_UINT32(ptr) - _resourceHeaderSize;
926 	else
927 		return READ_BE_UINT32(ptr - 4) - _resourceHeaderSize;
928 }
929 
lock(ResType type,ResId idx)930 void ResourceManager::lock(ResType type, ResId idx) {
931 	if (!validateResource("Locking", type, idx))
932 		return;
933 	_types[type][idx].lock();
934 }
935 
unlock(ResType type,ResId idx)936 void ResourceManager::unlock(ResType type, ResId idx) {
937 	if (!validateResource("Unlocking", type, idx))
938 		return;
939 	_types[type][idx].unlock();
940 }
941 
isLocked(ResType type,ResId idx) const942 bool ResourceManager::isLocked(ResType type, ResId idx) const {
943 	if (!validateResource("isLocked", type, idx))
944 		return false;
945 	return _types[type][idx].isLocked();
946 }
947 
lock()948 void ResourceManager::Resource::lock() {
949 	_flags |= RF_LOCK;
950 }
951 
unlock()952 void ResourceManager::Resource::unlock() {
953 	_flags &= ~RF_LOCK;
954 }
955 
isLocked() const956 bool ResourceManager::Resource::isLocked() const {
957 	return (_flags & RF_LOCK) != 0;
958 }
959 
isResourceInUse(ResType type,ResId idx) const960 bool ScummEngine::isResourceInUse(ResType type, ResId idx) const {
961 	if (!_res->validateResource("isResourceInUse", type, idx))
962 		return false;
963 	switch (type) {
964 	case rtRoom:
965 		return _roomResource == (byte)idx;
966 	case rtRoomImage:
967 		return _roomResource == (byte)idx;
968 	case rtRoomScripts:
969 		return _roomResource == (byte)idx;
970 	case rtScript:
971 		return isScriptInUse(idx);
972 	case rtCostume:
973 		return isCostumeInUse(idx);
974 	case rtSound:
975 		// Sound resource 1 is used for queued speech
976 		if (_game.heversion >= 60 && idx == 1)
977 			return true;
978 		else
979 			return _sound->isSoundInUse(idx);
980 	case rtCharset:
981 		return _charset->getCurID() == (int)idx;
982 	case rtImage:
983 		return _res->isModified(type, idx) != 0;
984 	case rtSpoolBuffer:
985 		return _sound->isSoundRunning(10000 + idx) != 0;
986 	default:
987 		return false;
988 	}
989 }
990 
setModified(ResType type,ResId idx)991 void ResourceManager::setModified(ResType type, ResId idx) {
992 	if (!validateResource("Modified", type, idx))
993 		return;
994 	_types[type][idx].setModified();
995 }
996 
setOffHeap(ResType type,ResId idx)997 void ResourceManager::setOffHeap(ResType type, ResId idx) {
998 	if (!validateResource("setOffHeap", type, idx))
999 		return;
1000 	_types[type][idx].setOffHeap();
1001 }
1002 
setOnHeap(ResType type,ResId idx)1003 void ResourceManager::setOnHeap(ResType type, ResId idx) {
1004 	if (!validateResource("setOnHeap", type, idx))
1005 		return;
1006 	_types[type][idx].setOnHeap();
1007 }
1008 
isModified(ResType type,ResId idx) const1009 bool ResourceManager::isModified(ResType type, ResId idx) const {
1010 	if (!validateResource("isModified", type, idx))
1011 		return false;
1012 	return _types[type][idx].isModified();
1013 }
1014 
isModified() const1015 bool ResourceManager::Resource::isModified() const {
1016 	return (_status & RS_MODIFIED) != 0;
1017 }
1018 
isOffHeap() const1019 bool ResourceManager::Resource::isOffHeap() const {
1020 	return (_status & RF_OFFHEAP) != 0;
1021 }
1022 
setModified()1023 void ResourceManager::Resource::setModified() {
1024 	_status |= RS_MODIFIED;
1025 }
1026 
setOffHeap()1027 void ResourceManager::Resource::setOffHeap() {
1028 	_status |= RF_OFFHEAP;
1029 }
1030 
setOnHeap()1031 void ResourceManager::Resource::setOnHeap() {
1032 	_status &= ~RF_OFFHEAP;
1033 }
1034 
expireResources(uint32 size)1035 void ResourceManager::expireResources(uint32 size) {
1036 	byte best_counter;
1037 	ResType best_type;
1038 	int best_res = 0;
1039 	uint32 oldAllocatedSize;
1040 
1041 	if (_expireCounter != 0xFF) {
1042 		_expireCounter = 0xFF;
1043 		increaseResourceCounters();
1044 	}
1045 
1046 	if (size + _allocatedSize < _maxHeapThreshold)
1047 		return;
1048 
1049 	oldAllocatedSize = _allocatedSize;
1050 
1051 	do {
1052 		best_type = rtInvalid;
1053 		best_counter = 2;
1054 
1055 		for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1056 			if (_types[type]._mode != kDynamicResTypeMode) {
1057 				// Resources of this type can be reloaded from the data files,
1058 				// so we can potentially unload them to free memory.
1059 				ResId idx = _types[type].size();
1060 				while (idx-- > 0) {
1061 					Resource &tmp = _types[type][idx];
1062 					byte counter = tmp.getResourceCounter();
1063 					if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(type, idx) && !tmp.isOffHeap()) {
1064 						best_counter = counter;
1065 						best_type = type;
1066 						best_res = idx;
1067 					}
1068 				}
1069 			}
1070 		}
1071 
1072 		if (!best_type)
1073 			break;
1074 		nukeResource(best_type, best_res);
1075 	} while (size + _allocatedSize > _minHeapThreshold);
1076 
1077 	increaseResourceCounters();
1078 
1079 	debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize);
1080 }
1081 
freeResources()1082 void ResourceManager::freeResources() {
1083 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1084 		ResId idx = _types[type].size();
1085 		while (idx-- > 0) {
1086 			if (isResourceLoaded(type, idx))
1087 				nukeResource(type, idx);
1088 		}
1089 		_types[type].clear();
1090 	}
1091 }
1092 
loadPtrToResource(ResType type,ResId idx,const byte * source)1093 void ScummEngine::loadPtrToResource(ResType type, ResId idx, const byte *source) {
1094 	byte *alloced;
1095 	int len;
1096 
1097 	_res->nukeResource(type, idx);
1098 
1099 	len = resStrLen(source) + 1;
1100 	if (len <= 0)
1101 		return;
1102 
1103 	alloced = _res->createResource(type, idx, len);
1104 
1105 	if (!source) {
1106 		// Need to refresh the script pointer, since createResource may
1107 		// have caused the script resource to expire.
1108 		refreshScriptPointer();
1109 		memcpy(alloced, _scriptPointer, len);
1110 		_scriptPointer += len;
1111 	} else {
1112 		memcpy(alloced, source, len);
1113 	}
1114 }
1115 
isResourceLoaded(ResType type,ResId idx) const1116 bool ResourceManager::isResourceLoaded(ResType type, ResId idx) const {
1117 	if (!validateResource("isResourceLoaded", type, idx))
1118 		return false;
1119 	return _types[type][idx]._address != NULL;
1120 }
1121 
resourceStats()1122 void ResourceManager::resourceStats() {
1123 	uint32 lockedSize = 0, lockedNum = 0;
1124 
1125 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1126 		ResId idx = _types[type].size();
1127 		while (idx-- > 0) {
1128 			Resource &tmp = _types[type][idx];
1129 			if (tmp.isLocked() && tmp._address) {
1130 				lockedSize += tmp._size;
1131 				lockedNum++;
1132 			}
1133 		}
1134 	}
1135 
1136 	debug(1, "Total allocated size=%d, locked=%d(%d)", _allocatedSize, lockedSize, lockedNum);
1137 }
1138 
readMAXS(int blockSize)1139 void ScummEngine_v5::readMAXS(int blockSize) {
1140 	_numVariables = _fileHandle->readUint16LE();      // 800
1141 	_fileHandle->readUint16LE();                      // 16
1142 	_numBitVariables = _fileHandle->readUint16LE();   // 2048
1143 	_numLocalObjects = _fileHandle->readUint16LE();   // 200
1144 	_numArray = 50;
1145 	_numVerbs = 100;
1146 	// Used to be 50, which wasn't enough for MI2 and FOA. See bugs
1147 	// #933610, #936323 and #941275.
1148 	_numNewNames = 150;
1149 	_objectRoomTable = NULL;
1150 
1151 	_fileHandle->readUint16LE();                      // 50
1152 	_numCharsets = _fileHandle->readUint16LE();       // 9
1153 	_fileHandle->readUint16LE();                      // 100
1154 	_fileHandle->readUint16LE();                      // 50
1155 	_numInventory = _fileHandle->readUint16LE();      // 80
1156 	_numGlobalScripts = 200;
1157 
1158 	_shadowPaletteSize = 256;
1159 
1160 	_numFlObject = 50;
1161 
1162 	if (_shadowPaletteSize)
1163 		_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1164 }
1165 
1166 #ifdef ENABLE_SCUMM_7_8
readMAXS(int blockSize)1167 void ScummEngine_v8::readMAXS(int blockSize) {
1168 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over SCUMM engine version
1169 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over data file version
1170 	_numVariables = _fileHandle->readUint32LE();     // 1500
1171 	_numBitVariables = _fileHandle->readUint32LE();  // 2048
1172 	_fileHandle->readUint32LE();                     // 40
1173 	_numScripts = _fileHandle->readUint32LE();       // 458
1174 	_numSounds = _fileHandle->readUint32LE();        // 789
1175 	_numCharsets = _fileHandle->readUint32LE();      // 1
1176 	_numCostumes = _fileHandle->readUint32LE();      // 446
1177 	_numRooms = _fileHandle->readUint32LE();         // 95
1178 	_fileHandle->readUint32LE();                     // 80
1179 	_numGlobalObjects = _fileHandle->readUint32LE(); // 1401
1180 	_fileHandle->readUint32LE();                     // 60
1181 	_numLocalObjects = _fileHandle->readUint32LE();  // 200
1182 	_numNewNames = _fileHandle->readUint32LE();      // 100
1183 	_numFlObject = _fileHandle->readUint32LE();      // 128
1184 	_numInventory = _fileHandle->readUint32LE();     // 80
1185 	_numArray = _fileHandle->readUint32LE();         // 200
1186 	_numVerbs = _fileHandle->readUint32LE();         // 50
1187 
1188 	_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1189 	_numGlobalScripts = 2000;
1190 
1191 	_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
1192 	_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1193 }
1194 
readMAXS(int blockSize)1195 void ScummEngine_v7::readMAXS(int blockSize) {
1196 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over SCUMM engine version
1197 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over data file version
1198 	_numVariables = _fileHandle->readUint16LE();
1199 	_numBitVariables = _fileHandle->readUint16LE();
1200 	_fileHandle->readUint16LE();
1201 	_numGlobalObjects = _fileHandle->readUint16LE();
1202 	_numLocalObjects = _fileHandle->readUint16LE();
1203 	_numNewNames = _fileHandle->readUint16LE();
1204 	_numVerbs = _fileHandle->readUint16LE();
1205 	_numFlObject = _fileHandle->readUint16LE();
1206 	_numInventory = _fileHandle->readUint16LE();
1207 	_numArray = _fileHandle->readUint16LE();
1208 	_numRooms = _fileHandle->readUint16LE();
1209 	_numScripts = _fileHandle->readUint16LE();
1210 	_numSounds = _fileHandle->readUint16LE();
1211 	_numCharsets = _fileHandle->readUint16LE();
1212 	_numCostumes = _fileHandle->readUint16LE();
1213 
1214 	_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1215 
1216 	if ((_game.id == GID_FT) && (_game.features & GF_DEMO) &&
1217 		(_game.platform == Common::kPlatformDOS))
1218 		_numGlobalScripts = 300;
1219 	else
1220 		_numGlobalScripts = 2000;
1221 
1222 	_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
1223 	_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1224 }
1225 #endif
1226 
readMAXS(int blockSize)1227 void ScummEngine_v6::readMAXS(int blockSize) {
1228 	if (blockSize == 38) {
1229 		_numVariables = _fileHandle->readUint16LE();
1230 		_fileHandle->readUint16LE();
1231 		_numBitVariables = _fileHandle->readUint16LE();
1232 		_numLocalObjects = _fileHandle->readUint16LE();
1233 		_numArray = _fileHandle->readUint16LE();
1234 		_fileHandle->readUint16LE();
1235 		_numVerbs = _fileHandle->readUint16LE();
1236 		_numFlObject = _fileHandle->readUint16LE();
1237 		_numInventory = _fileHandle->readUint16LE();
1238 		_numRooms = _fileHandle->readUint16LE();
1239 		_numScripts = _fileHandle->readUint16LE();
1240 		_numSounds = _fileHandle->readUint16LE();
1241 		_numCharsets = _fileHandle->readUint16LE();
1242 		_numCostumes = _fileHandle->readUint16LE();
1243 		_numGlobalObjects = _fileHandle->readUint16LE();
1244 		_numNewNames = 50;
1245 
1246 		_objectRoomTable = NULL;
1247 		_numGlobalScripts = 200;
1248 
1249 		if (_game.heversion >= 70) {
1250 			_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1251 		}
1252 
1253 		if (_game.heversion <= 70) {
1254 			_shadowPaletteSize = 256;
1255 			_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1256 		}
1257 	} else
1258 		error("readMAXS(%d) failed to read MAXS data", blockSize);
1259 }
1260 
readGlobalObjects()1261 void ScummEngine::readGlobalObjects() {
1262 	int i;
1263 	int num = _fileHandle->readUint16LE();
1264 	assert(num == _numGlobalObjects);
1265 	assert(_objectStateTable);
1266 	assert(_objectOwnerTable);
1267 
1268 	_fileHandle->read(_objectOwnerTable, num);
1269 	for (i = 0; i < num; i++) {
1270 		_objectStateTable[i] = _objectOwnerTable[i] >> OF_STATE_SHL;
1271 		_objectOwnerTable[i] &= OF_OWNER_MASK;
1272 	}
1273 
1274 	_fileHandle->read(_classData, num * sizeof(uint32));
1275 
1276 #if defined(SCUMM_BIG_ENDIAN)
1277 	// Correct the endianess if necessary
1278 	for (i = 0; i != num; i++)
1279 		_classData[i] = FROM_LE_32(_classData[i]);
1280 #endif
1281 }
1282 
1283 #ifdef ENABLE_SCUMM_7_8
readGlobalObjects()1284 void ScummEngine_v8::readGlobalObjects() {
1285 	int i;
1286 	int num = _fileHandle->readUint32LE();
1287 	assert(num == _numGlobalObjects);
1288 	assert(_objectStateTable);
1289 	assert(_objectOwnerTable);
1290 
1291 	_objectIDMap = new ObjectNameId[num];
1292 	_objectIDMapSize = num;
1293 	for (i = 0; i < num; i++) {
1294 		// Add to object name-to-id map
1295 		_fileHandle->read(_objectIDMap[i].name, 40);
1296 		_objectIDMap[i].id = i;
1297 
1298 		_objectStateTable[i] = _fileHandle->readByte();
1299 		_objectRoomTable[i] = _fileHandle->readByte();
1300 		_classData[i] = _fileHandle->readUint32LE();
1301 	}
1302 	memset(_objectOwnerTable, 0xFF, num);
1303 
1304 	// Finally, sort the object name->ID map, so we can later use
1305 	// bsearch on it. For this we (ab)use strcmp, which works fine
1306 	// since the table entries start with a string.
1307 	qsort(_objectIDMap, _objectIDMapSize, sizeof(ObjectNameId),
1308 			(int (*)(const void*, const void*))strcmp);
1309 }
1310 
readGlobalObjects()1311 void ScummEngine_v7::readGlobalObjects() {
1312 	int num = _fileHandle->readUint16LE();
1313 	assert(num == _numGlobalObjects);
1314 	assert(_objectStateTable);
1315 	assert(_objectOwnerTable);
1316 
1317 	_fileHandle->read(_objectStateTable, num);
1318 	_fileHandle->read(_objectRoomTable, num);
1319 	memset(_objectOwnerTable, 0xFF, num);
1320 
1321 	_fileHandle->read(_classData, num * sizeof(uint32));
1322 
1323 #if defined(SCUMM_BIG_ENDIAN)
1324 	// Correct the endianess if necessary
1325 	for (int i = 0; i != num; i++)
1326 		_classData[i] = FROM_LE_32(_classData[i]);
1327 #endif
1328 }
1329 #endif
1330 
allocateArrays()1331 void ScummEngine::allocateArrays() {
1332 	// Note: Buffers are now allocated in scummMain to allow for
1333 	//     early GUI init.
1334 
1335 	_objectOwnerTable = (byte *)calloc(_numGlobalObjects, 1);
1336 	_objectStateTable = (byte *)calloc(_numGlobalObjects, 1);
1337 	_classData = (uint32 *)calloc(_numGlobalObjects, sizeof(uint32));
1338 	_newNames = (uint16 *)calloc(_numNewNames, sizeof(uint16));
1339 
1340 	_inventory = (uint16 *)calloc(_numInventory, sizeof(uint16));
1341 	_verbs = (VerbSlot *)calloc(_numVerbs, sizeof(VerbSlot));
1342 	_objs = (ObjectData *)calloc(_numLocalObjects, sizeof(ObjectData));
1343 	_roomVars = (int32 *)calloc(_numRoomVariables, sizeof(int32));
1344 	_scummVars = (int32 *)calloc(_numVariables, sizeof(int32));
1345 	_bitVars = (byte *)calloc(_numBitVariables >> 3, 1);
1346 	if (_game.heversion >= 60) {
1347 		_arraySlot = (byte *)calloc(_numArray, 1);
1348 	}
1349 
1350 	_res->allocResTypeData(rtCostume, (_game.features & GF_NEW_COSTUMES) ? MKTAG('A','K','O','S') : MKTAG('C','O','S','T'),
1351 								_numCostumes, kStaticResTypeMode);
1352 	_res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, kStaticResTypeMode);
1353 	_res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, kStaticResTypeMode);
1354 	_res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, kStaticResTypeMode);
1355 	_res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, kSoundResTypeMode);
1356 	_res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, kStaticResTypeMode);
1357 	_res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, kStaticResTypeMode);
1358 	_res->allocResTypeData(rtObjectName, 0, _numNewNames, kDynamicResTypeMode);
1359 	_res->allocResTypeData(rtInventory, 0, _numInventory, kDynamicResTypeMode);
1360 	_res->allocResTypeData(rtTemp, 0, 10, kDynamicResTypeMode);
1361 	_res->allocResTypeData(rtScaleTable, 0, 5, kDynamicResTypeMode);
1362 	_res->allocResTypeData(rtActorName, 0, _numActors, kDynamicResTypeMode);
1363 	_res->allocResTypeData(rtVerb, 0, _numVerbs, kDynamicResTypeMode);
1364 	_res->allocResTypeData(rtString, 0, _numArray, kDynamicResTypeMode);
1365 	_res->allocResTypeData(rtFlObject, 0, _numFlObject, kDynamicResTypeMode);
1366 	_res->allocResTypeData(rtMatrix, 0, 10, kDynamicResTypeMode);
1367 	_res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, kStaticResTypeMode);
1368 	_res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, kStaticResTypeMode);
1369 }
1370 
allocateArrays()1371 void ScummEngine_v70he::allocateArrays() {
1372 	ScummEngine::allocateArrays();
1373 
1374 	_res->allocResTypeData(rtSpoolBuffer, 0, 9, kStaticResTypeMode);
1375 	_heV7RoomIntOffsets = (uint32 *)calloc(_numRooms, sizeof(uint32));
1376 }
1377 
1378 
dumpResource(const char * tag,int id,const byte * ptr,int length)1379 void ScummEngine::dumpResource(const char *tag, int id, const byte *ptr, int length) {
1380 	char buf[256];
1381 	Common::DumpFile out;
1382 
1383 	uint32 size;
1384 	if (length >= 0)
1385 		size = length;
1386 	else if (_game.features & GF_OLD_BUNDLE)
1387 		size = READ_LE_UINT16(ptr);
1388 	else if (_game.features & GF_SMALL_HEADER)
1389 		size = READ_LE_UINT32(ptr);
1390 	else
1391 		size = READ_BE_UINT32(ptr + 4);
1392 
1393 	sprintf(buf, "dumps/%s%d.dmp", tag, id);
1394 
1395 	out.open(buf);
1396 	if (out.isOpen() == false)
1397 		return;
1398 	out.write(ptr, size);
1399 	out.close();
1400 }
1401 
ResourceIterator(const byte * searchin,bool smallHeader)1402 ResourceIterator::ResourceIterator(const byte *searchin, bool smallHeader)
1403 	: _ptr(searchin), _smallHeader(smallHeader) {
1404 	assert(searchin);
1405 	if (_smallHeader) {
1406 		_size = READ_LE_UINT32(searchin);
1407 		_pos = 6;
1408 		_ptr = searchin + 6;
1409 	} else {
1410 		_size = READ_BE_UINT32(searchin + 4);
1411 		_pos = 8;
1412 		_ptr = searchin + 8;
1413 	}
1414 
1415 }
1416 
findNext(uint32 tag)1417 const byte *ResourceIterator::findNext(uint32 tag) {
1418 	uint32 size = 0;
1419 	const byte *result = 0;
1420 
1421 	if (_smallHeader) {
1422 		uint16 smallTag = newTag2Old(tag);
1423 		do {
1424 			if (_pos >= _size)
1425 				return 0;
1426 
1427 			result = _ptr;
1428 			size = READ_LE_UINT32(result);
1429 			if ((int32)size <= 0)
1430 				return 0;	// Avoid endless loop
1431 
1432 			_pos += size;
1433 			_ptr += size;
1434 		} while (READ_LE_UINT16(result + 4) != smallTag);
1435 	} else {
1436 		do {
1437 			if (_pos >= _size)
1438 				return 0;
1439 
1440 			result = _ptr;
1441 			size = READ_BE_UINT32(result + 4);
1442 			if ((int32)size <= 0)
1443 				return 0;	// Avoid endless loop
1444 
1445 			_pos += size;
1446 			_ptr += size;
1447 		} while (READ_BE_UINT32(result) != tag);
1448 	}
1449 
1450 	return result;
1451 }
1452 
findResource(uint32 tag,const byte * searchin)1453 const byte *ScummEngine::findResource(uint32 tag, const byte *searchin) {
1454 	uint32 curpos, totalsize, size;
1455 
1456 	debugC(DEBUG_RESOURCE, "findResource(%s, %p)", tag2str(tag), (const void *)searchin);
1457 
1458 	if (!searchin) {
1459 		if (_game.heversion >= 70) {
1460 			searchin = _resourceLastSearchBuf;
1461 			totalsize = _resourceLastSearchSize;
1462 			curpos = 0;
1463 		} else {
1464 			assert(searchin);
1465 			return NULL;
1466 		}
1467 	} else {
1468 		searchin += 4;
1469 		_resourceLastSearchSize = totalsize = READ_BE_UINT32(searchin);
1470 		curpos = 8;
1471 		searchin += 4;
1472 	}
1473 
1474 	while (curpos < totalsize) {
1475 		if (READ_BE_UINT32(searchin) == tag) {
1476 			_resourceLastSearchBuf = searchin;
1477 			return searchin;
1478 		}
1479 
1480 		size = READ_BE_UINT32(searchin + 4);
1481 		if ((int32)size <= 0) {
1482 			error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
1483 			return NULL;
1484 		}
1485 
1486 		curpos += size;
1487 		searchin += size;
1488 	}
1489 
1490 	return NULL;
1491 }
1492 
findResourceSmall(uint32 tag,const byte * searchin)1493 const byte *findResourceSmall(uint32 tag, const byte *searchin) {
1494 	uint32 curpos, totalsize, size;
1495 	uint16 smallTag;
1496 
1497 	smallTag = newTag2Old(tag);
1498 	if (smallTag == 0)
1499 		return NULL;
1500 
1501 	assert(searchin);
1502 
1503 	totalsize = READ_LE_UINT32(searchin);
1504 	searchin += 6;
1505 	curpos = 6;
1506 
1507 	while (curpos < totalsize) {
1508 		size = READ_LE_UINT32(searchin);
1509 
1510 		if (READ_LE_UINT16(searchin + 4) == smallTag)
1511 			return searchin;
1512 
1513 		if ((int32)size <= 0) {
1514 			error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
1515 			return NULL;
1516 		}
1517 
1518 		curpos += size;
1519 		searchin += size;
1520 	}
1521 
1522 	return NULL;
1523 }
1524 
newTag2Old(uint32 newTag)1525 uint16 newTag2Old(uint32 newTag) {
1526 	switch (newTag) {
1527 	case (MKTAG('R','M','H','D')):
1528 		return (0x4448);	// HD
1529 	case (MKTAG('I','M','0','0')):
1530 		return (0x4D42);	// BM
1531 	case (MKTAG('E','X','C','D')):
1532 		return (0x5845);	// EX
1533 	case (MKTAG('E','N','C','D')):
1534 		return (0x4E45);	// EN
1535 	case (MKTAG('S','C','A','L')):
1536 		return (0x4153);	// SA
1537 	case (MKTAG('L','S','C','R')):
1538 		return (0x534C);	// LS
1539 	case (MKTAG('O','B','C','D')):
1540 		return (0x434F);	// OC
1541 	case (MKTAG('O','B','I','M')):
1542 		return (0x494F);	// OI
1543 	case (MKTAG('S','M','A','P')):
1544 		return (0x4D42);	// BM
1545 	case (MKTAG('C','L','U','T')):
1546 		return (0x4150);	// PA
1547 	case (MKTAG('B','O','X','D')):
1548 		return (0x5842);	// BX
1549 	case (MKTAG('C','Y','C','L')):
1550 		return (0x4343);	// CC
1551 	case (MKTAG('E','P','A','L')):
1552 		return (0x5053);	// SP
1553 	case (MKTAG('T','I','L','E')):
1554 		return (0x4C54);	// TL
1555 	case (MKTAG('Z','P','0','0')):
1556 		return (0x505A);	// ZP
1557 	default:
1558 		return (0);
1559 	}
1560 }
1561 
nameOfResType(ResType type)1562 const char *nameOfResType(ResType type) {
1563 	static char buf[100];
1564 
1565 	switch (type) {
1566 	case rtRoom:
1567 		return "Room";
1568 	case rtScript:
1569 		return "Script";
1570 	case rtCostume:
1571 		return "Costume";
1572 	case rtSound:
1573 		return "Sound";
1574 	case rtInventory:
1575 		return "Inventory";
1576 	case rtCharset:
1577 		return "Charset";
1578 	case rtString:
1579 		return "String";
1580 	case rtVerb:
1581 		return "Verb";
1582 	case rtActorName:
1583 		return "ActorName";
1584 	case rtBuffer:
1585 		return "Buffer";
1586 	case rtScaleTable:
1587 		return "ScaleTable";
1588 	case rtTemp:
1589 		return "Temp";
1590 	case rtFlObject:
1591 		return "FlObject";
1592 	case rtMatrix:
1593 		return "Matrix";
1594 	case rtBox:
1595 		return "Box";
1596 	case rtObjectName:
1597 		return "ObjectName";
1598 	case rtRoomScripts:
1599 		return "RoomScripts";
1600 	case rtRoomImage:
1601 		return "RoomImage";
1602 	case rtImage:
1603 		return "Image";
1604 	case rtTalkie:
1605 		return "Talkie";
1606 	case rtSpoolBuffer:
1607 		return "SpoolBuffer";
1608 	default:
1609 		sprintf(buf, "rt%d", type);
1610 		return buf;
1611 	}
1612 }
1613 
1614 } // End of namespace Scumm
1615