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/md5.h"
24 #include "common/str.h"
25 #include "common/memstream.h"
26 #include "common/macresman.h"
27 #ifndef MACOSX
28 #include "common/config-manager.h"
29 #endif
30 
31 #include "scumm/charset.h"
32 #include "scumm/dialogs.h"
33 #include "scumm/file.h"
34 #include "scumm/imuse/imuse.h"
35 #include "scumm/imuse_digi/dimuse.h"
36 #include "scumm/he/intern_he.h"
37 #include "scumm/object.h"
38 #include "scumm/resource.h"
39 #include "scumm/scumm.h"
40 #include "scumm/scumm_v5.h"
41 #include "scumm/scumm_v8.h"
42 #include "scumm/sound.h"
43 #include "scumm/util.h"
44 #include "scumm/verbs.h"
45 
46 namespace Scumm {
47 
48 enum {
49 	RF_LOCK = 0x80,
50 	RF_USAGE = 0x7F,
51 	RF_USAGE_MAX = RF_USAGE,
52 
53 	RS_MODIFIED = 0x10,
54 	RF_OFFHEAP = 0x40
55 };
56 
57 
58 
59 extern const char *nameOfResType(ResType type);
60 
61 static uint16 newTag2Old(uint32 newTag);
62 static const byte *findResourceSmall(uint32 tag, const byte *searchin);
63 
64 static bool checkTryMedia(BaseScummFile *handle);
65 
66 
67 /* Open a room */
openRoom(const int room)68 void ScummEngine::openRoom(const int room) {
69 	bool result;
70 	byte encByte = 0;
71 
72 	debugC(DEBUG_GENERAL, "openRoom(%d)", room);
73 	assert(room >= 0);
74 
75 	/* Don't load the same room again */
76 	if (_lastLoadedRoom == room)
77 		return;
78 	_lastLoadedRoom = room;
79 
80 	/* Room -1 means close file */
81 	if (room == -1) {
82 		deleteRoomOffsets();
83 		_fileHandle->close();
84 		return;
85 	}
86 
87 	// Load the disk numer / room offs (special case for room 0 exists because
88 	// room 0 contains the data which is used to create the roomno / roomoffs
89 	// tables -- hence obviously we mustn't use those when loading room 0.
90 	const uint32 diskNumber = room ? _res->_types[rtRoom][room]._roomno : 0;
91 	const uint32 room_offs = room ? _res->_types[rtRoom][room]._roomoffs : 0;
92 
93 	// FIXME: Since room_offs is const, clearly the following loop either
94 	// is never entered, or loops forever (if it wasn't for the return/error
95 	// statements in it, that is). -> This should be cleaned up!
96 	// Maybe we should re-enabled the looping properly, to deal with disc
97 	// changes in COMI ?
98 	while (room_offs != RES_INVALID_OFFSET) {
99 
100 		if (room_offs != 0 && room != 0 && _game.heversion < 98) {
101 			_fileOffset = _res->_types[rtRoom][room]._roomoffs;
102 			return;
103 		}
104 
105 		Common::String filename(generateFilename(room));
106 
107 		// Determine the encryption, if any.
108 		if (_game.features & GF_USE_KEY) {
109 			if (_game.version <= 3)
110 				encByte = 0xFF;
111 			else if ((_game.version == 4) && (room == 0 || room >= 900))
112 				encByte = 0;
113 			else
114 				encByte = 0x69;
115 		} else
116 			encByte = 0;
117 
118 		if (room > 0 && (_game.version == 8))
119 			VAR(VAR_CURRENTDISK) = diskNumber;
120 
121 		// Try to open the file
122 		result = openResourceFile(filename, encByte);
123 
124 		if (result) {
125 			if (room == 0)
126 				return;
127 			deleteRoomOffsets();
128 			readRoomsOffsets();
129 			_fileOffset = _res->_types[rtRoom][room]._roomoffs;
130 
131 			if (_fileOffset != 8)
132 				return;
133 
134 			error("Room %d not in %s", room, filename.c_str());
135 			return;
136 		}
137 		askForDisk(filename.c_str(), diskNumber);
138 	}
139 
140 	do {
141 		char buf[16];
142 		snprintf(buf, sizeof(buf), "%.3d.lfl", room);
143 		encByte = 0;
144 		if (openResourceFile(buf, encByte))
145 			break;
146 		askForDisk(buf, diskNumber);
147 	} while (1);
148 
149 	deleteRoomOffsets();
150 	_fileOffset = 0;		// start of file
151 }
152 
closeRoom()153 void ScummEngine::closeRoom() {
154 	if (_lastLoadedRoom != -1) {
155 		_lastLoadedRoom = -1;
156 		deleteRoomOffsets();
157 		_fileHandle->close();
158 	}
159 }
160 
161 /** Delete the currently loaded room offsets. */
deleteRoomOffsets()162 void ScummEngine::deleteRoomOffsets() {
163 	for (int i = 0; i < _numRooms; i++) {
164 		if (_res->_types[rtRoom][i]._roomoffs != RES_INVALID_OFFSET)
165 			_res->_types[rtRoom][i]._roomoffs = 0;
166 	}
167 }
168 
169 /** Read room offsets */
readRoomsOffsets()170 void ScummEngine::readRoomsOffsets() {
171 	if (_game.features & GF_SMALL_HEADER) {
172 		_fileHandle->seek(12, SEEK_SET);	// Directly searching for the room offset block would be more generic...
173 	} else {
174 		_fileHandle->seek(16, SEEK_SET);
175 	}
176 
177 	int num = _fileHandle->readByte();
178 	while (num--) {
179 		int room = _fileHandle->readByte();
180 		int offset =  _fileHandle->readUint32LE();
181 		if (_res->_types[rtRoom][room]._roomoffs != RES_INVALID_OFFSET) {
182 			_res->_types[rtRoom][room]._roomoffs = offset;
183 		}
184 	}
185 }
186 
openFile(BaseScummFile & file,const Common::String & filename,bool resourceFile)187 bool ScummEngine::openFile(BaseScummFile &file, const Common::String &filename, bool resourceFile) {
188 	bool result = false;
189 
190 	if (!_containerFile.empty()) {
191 		file.close();
192 		file.open(_containerFile);
193 		assert(file.isOpen());
194 
195 		result = file.openSubFile(filename);
196 	}
197 
198 	if (!result) {
199 		file.close();
200 		result = file.open(filename);
201 	}
202 
203 	return result;
204 }
205 
openResourceFile(const Common::String & filename,byte encByte)206 bool ScummEngine::openResourceFile(const Common::String &filename, byte encByte) {
207 	debugC(DEBUG_GENERAL, "openResourceFile(%s)", filename.c_str());
208 
209 	if (openFile(*_fileHandle, filename, true)) {
210 		_fileHandle->setEnc(encByte);
211 		return true;
212 	}
213 	return false;
214 }
215 
askForDisk(const char * filename,int disknum)216 void ScummEngine::askForDisk(const char *filename, int disknum) {
217 	char buf[128];
218 
219 	if (_game.version == 8) {
220 #ifdef ENABLE_SCUMM_7_8
221 		char result;
222 
223 		_imuseDigital->stopAllSounds();
224 
225 #ifdef MACOSX
226 		sprintf(buf, "Cannot find file: '%s'\nPlease insert disc %d.\nPress OK to retry, Quit to exit", filename, disknum);
227 #else
228 		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());
229 #endif
230 
231 		result = displayMessage("Quit", "%s", buf);
232 		if (!result) {
233 			error("Cannot find file: '%s'", filename);
234 		}
235 #endif
236 	} else {
237 		sprintf(buf, "Cannot find file: '%s'", filename);
238 		InfoDialog dialog(this, Common::U32String(buf));
239 		runDialog(dialog);
240 		error("Cannot find file: '%s'", filename);
241 	}
242 }
243 
readIndexFile()244 void ScummEngine::readIndexFile() {
245 	uint32 blocktype, itemsize;
246 	int numblock = 0;
247 
248 	debugC(DEBUG_GENERAL, "readIndexFile()");
249 
250 	closeRoom();
251 	openRoom(0);
252 
253 	if (_game.version <= 5) {
254 		// Figure out the sizes of various resources
255 		while (true) {
256 			blocktype = _fileHandle->readUint32BE();
257 			itemsize = _fileHandle->readUint32BE();
258 			if (_fileHandle->eos() || _fileHandle->err())
259 				break;
260 			switch (blocktype) {
261 			case MKTAG('D','O','B','J'):
262 				_numGlobalObjects = _fileHandle->readUint16LE();
263 				itemsize -= 2;
264 				break;
265 			case MKTAG('D','R','O','O'):
266 				_numRooms = _fileHandle->readUint16LE();
267 				itemsize -= 2;
268 				break;
269 
270 			case MKTAG('D','S','C','R'):
271 				_numScripts = _fileHandle->readUint16LE();
272 				itemsize -= 2;
273 				break;
274 
275 			case MKTAG('D','C','O','S'):
276 				_numCostumes = _fileHandle->readUint16LE();
277 				itemsize -= 2;
278 				break;
279 
280 			case MKTAG('D','S','O','U'):
281 				_numSounds = _fileHandle->readUint16LE();
282 				itemsize -= 2;
283 				break;
284 
285 			default:
286 				break;
287 			}
288 			_fileHandle->seek(itemsize - 8, SEEK_CUR);
289 		}
290 		_fileHandle->seek(0, SEEK_SET);
291 	}
292 
293 	if (checkTryMedia(_fileHandle)) {
294 		displayMessage(NULL, "You're trying to run game encrypted by ActiveMark. This is not supported.");
295 		quitGame();
296 
297 		return;
298 	}
299 
300 	while (true) {
301 		blocktype = _fileHandle->readUint32BE();
302 		itemsize = _fileHandle->readUint32BE();
303 
304 		if (_fileHandle->eos() || _fileHandle->err())
305 			break;
306 
307 		numblock++;
308 		debug(2, "Reading index block of type '%s', size %d", tag2str(blocktype), itemsize);
309 		readIndexBlock(blocktype, itemsize);
310 	}
311 
312 //  if (numblock!=9)
313 //    error("Not enough blocks read from directory");
314 
315 	closeRoom();
316 }
317 
318 
319 
320 #define TRYMEDIA_MARK_LEN 6
321 
checkTryMedia(BaseScummFile * handle)322 bool checkTryMedia(BaseScummFile *handle) {
323 	byte buf[TRYMEDIA_MARK_LEN];
324 	bool matched = true;
325 	const byte magic[2][TRYMEDIA_MARK_LEN] =
326 		{{ 0x00,  'T', 'M', 'S', 'A', 'M' },
327 		 { 'i',   '=', '$', ':', '(', '$' }};  // Same but 0x69 xored
328 
329 	handle->read(buf, TRYMEDIA_MARK_LEN);
330 
331 	for (int i = 0; i < 2; i++) {
332 		matched = true;
333 		for (int j = 0; j < TRYMEDIA_MARK_LEN; j++)
334 			if (buf[j] != magic[i][j]) {
335 				matched = false;
336 				break;
337 			}
338 
339 		if (matched)
340 			break;
341 	}
342 
343 	if (matched)
344 		return true;
345 
346 	handle->seek(0, SEEK_SET);
347 
348 	return false;
349 }
350 
351 
352 #ifdef ENABLE_SCUMM_7_8
readIndexBlock(uint32 blocktype,uint32 itemsize)353 void ScummEngine_v7::readIndexBlock(uint32 blocktype, uint32 itemsize) {
354 	int num;
355 	char *ptr;
356 	switch (blocktype) {
357 	case MKTAG('A','N','A','M'):		// Used by: The Dig, FT
358 		num = _fileHandle->readUint16LE();
359 		ptr = (char *)malloc(num * 9);
360 		_fileHandle->read(ptr, num * 9);
361 		_imuseDigital->setAudioNames(num, ptr);
362 		break;
363 
364 	case MKTAG('D','R','S','C'):		// Used by: COMI
365 		readResTypeList(rtRoomScripts);
366 		break;
367 
368 	default:
369 		ScummEngine::readIndexBlock(blocktype, itemsize);
370 	}
371 }
372 #endif
373 
readIndexBlock(uint32 blocktype,uint32 itemsize)374 void ScummEngine_v70he::readIndexBlock(uint32 blocktype, uint32 itemsize) {
375 	int i;
376 	switch (blocktype) {
377 	case MKTAG('D','I','R','I'):
378 		readResTypeList(rtRoomImage);
379 		break;
380 
381 	case MKTAG('D','I','R','M'):
382 		readResTypeList(rtImage);
383 		break;
384 
385 	case MKTAG('D','I','R','T'):
386 		readResTypeList(rtTalkie);
387 		break;
388 
389 	case MKTAG('D','L','F','L'):
390 		i = _fileHandle->readUint16LE();
391 		_fileHandle->seek(-2, SEEK_CUR);
392 		_heV7RoomOffsets = (byte *)calloc(2 + (i * 4), 1);
393 		_fileHandle->read(_heV7RoomOffsets, (2 + (i * 4)) );
394 		break;
395 
396 	case MKTAG('D','I','S','K'):
397 		i = _fileHandle->readUint16LE();
398 		_heV7DiskOffsets = (byte *)calloc(i, 1);
399 		_fileHandle->read(_heV7DiskOffsets, i);
400 		break;
401 
402 	case MKTAG('S','V','E','R'):
403 		// Index version number
404 		_fileHandle->seek(itemsize - 8, SEEK_CUR);
405 		break;
406 
407 	case MKTAG('I','N','I','B'):
408 		_fileHandle->seek(itemsize - 8, SEEK_CUR);
409 		debug(2, "INIB index block not yet handled, skipping");
410 		break;
411 
412 	default:
413 		ScummEngine::readIndexBlock(blocktype, itemsize);
414 	}
415 }
416 
readIndexBlock(uint32 blocktype,uint32 itemsize)417 void ScummEngine::readIndexBlock(uint32 blocktype, uint32 itemsize) {
418 	int i;
419 	switch (blocktype) {
420 	case MKTAG('D','C','H','R'):
421 	case MKTAG('D','I','R','F'):
422 		readResTypeList(rtCharset);
423 		break;
424 
425 	case MKTAG('D','O','B','J'):
426 		readGlobalObjects();
427 		break;
428 
429 	case MKTAG('R','N','A','M'):
430 		// Names of rooms. Maybe we should put them into a table, for use by the debugger?
431 		if (_game.heversion >= 80) {
432 			for (int room; (room = _fileHandle->readUint16LE()); ) {
433 				char buf[100];
434 				i = 0;
435 				for (byte s; (s = _fileHandle->readByte()) && i < ARRAYSIZE(buf) - 1; ) {
436 					buf[i++] = s;
437 				}
438 				buf[i] = 0;
439 				debug(5, "Room %d: '%s'", room, buf);
440 			}
441 		} else {
442 			for (int room; (room = _fileHandle->readByte()); ) {
443 				char buf[10];
444 				_fileHandle->read(buf, 9);
445 				buf[9] = 0;
446 				for (i = 0; i < 9; i++)
447 					buf[i] ^= 0xFF;
448 				debug(5, "Room %d: '%s'", room, buf);
449 			}
450 		}
451 		break;
452 
453 	case MKTAG('D','R','O','O'):
454 	case MKTAG('D','I','R','R'):
455 		readResTypeList(rtRoom);
456 		break;
457 
458 	case MKTAG('D','S','C','R'):
459 	case MKTAG('D','I','R','S'):
460 		readResTypeList(rtScript);
461 		break;
462 
463 	case MKTAG('D','C','O','S'):
464 	case MKTAG('D','I','R','C'):
465 		readResTypeList(rtCostume);
466 		break;
467 
468 	case MKTAG('M','A','X','S'):
469 		readMAXS(itemsize);
470 		allocateArrays();
471 		break;
472 
473 	case MKTAG('D','I','R','N'):
474 	case MKTAG('D','S','O','U'):
475 		readResTypeList(rtSound);
476 		break;
477 
478 	case MKTAG('A','A','R','Y'):
479 		readArrayFromIndexFile();
480 		break;
481 
482 	default:
483 		error("Bad ID %04X('%s') found in index file directory", blocktype,
484 				tag2str(blocktype));
485 	}
486 }
487 
readArrayFromIndexFile()488 void ScummEngine::readArrayFromIndexFile() {
489 	error("readArrayFromIndexFile() not supported in pre-V6 games");
490 }
491 
readResTypeList(ResType type)492 int ScummEngine::readResTypeList(ResType type) {
493 	uint num;
494 	ResId idx;
495 
496 	if (_game.version == 8)
497 		num = _fileHandle->readUint32LE();
498 	else
499 		num = _fileHandle->readUint16LE();
500 
501 	if (num != _res->_types[type].size()) {
502 		error("Invalid number of %ss (%d) in directory", nameOfResType(type), num);
503 	}
504 
505 	debug(2, "  readResTypeList(%s): %d entries", nameOfResType(type), num);
506 
507 
508 	for (idx = 0; idx < num; idx++) {
509 		_res->_types[type][idx]._roomno = _fileHandle->readByte();
510 	}
511 	for (idx = 0; idx < num; idx++) {
512 		_res->_types[type][idx]._roomoffs = _fileHandle->readUint32LE();
513 	}
514 
515 	return num;
516 }
517 
readResTypeList(ResType type)518 int ScummEngine_v70he::readResTypeList(ResType type) {
519 	uint num;
520 	ResId idx;
521 
522 	num = ScummEngine::readResTypeList(type);
523 
524 	if (type == rtRoom)
525 		for (idx = 0; idx < num; idx++) {
526 			_heV7RoomIntOffsets[idx] = _res->_types[rtRoom][idx]._roomoffs;
527 		}
528 
529 	for (idx = 0; idx < num; idx++) {
530 		// The globsize is currently not being used
531 		/*_res->_types[type][idx]._globsize =*/ _fileHandle->readUint32LE();
532 	}
533 
534 	return num;
535 }
536 
allocResTypeData(ResType type,uint32 tag,int num,ResTypeMode mode)537 void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTypeMode mode) {
538 	debug(2, "allocResTypeData(%s,%s,%d,%d)", nameOfResType(type), tag2str(TO_BE_32(tag)), num, mode);
539 	assert(type >= 0 && type < (int)(ARRAYSIZE(_types)));
540 
541 	if (num >= 8000)
542 		error("Too many %s resources (%d) in directory", nameOfResType(type), num);
543 
544 	_types[type]._mode = mode;
545 	_types[type]._tag = tag;
546 
547 	// If there was data in there, let's clear it out completely. This is important
548 	// in case we are restarting the game.
549 	_types[type].clear();
550 	_types[type].resize(num);
551 
552 /*
553 	TODO: Use multiple Resource subclasses, one for each res mode; then,
554 	given them serializability.
555 	if (mode) {
556 		_types[type].roomno = (byte *)calloc(num, sizeof(byte));
557 		_types[type].roomoffs = (uint32 *)calloc(num, sizeof(uint32));
558 	}
559 
560 	if (_vm->_game.heversion >= 70) {
561 		_types[type].globsize = (uint32 *)calloc(num, sizeof(uint32));
562 	}
563 */
564 }
565 
loadCharset(int no)566 void ScummEngine::loadCharset(int no) {
567 	int i;
568 	byte *ptr;
569 
570 	debugC(DEBUG_GENERAL, "loadCharset(%d)", no);
571 
572 	/* FIXME - hack around crash in Indy4 (occurs if you try to load after dieing) */
573 	if (_game.id == GID_INDY4 && no == 0)
574 		no = 1;
575 
576 	/* for Humongous catalogs */
577 	if (_game.heversion >= 70 && _numCharsets == 1) {
578 		debug(0, "Not loading charset as it doesn't seem to exist?");
579 		return;
580 	}
581 
582 	assert(no < (int)sizeof(_charsetData) / 16);
583 	assertRange(1, no, _numCharsets - 1, "charset");
584 
585 	ptr = getResourceAddress(rtCharset, no);
586 
587 	for (i = 0; i < 15; i++) {
588 		_charsetData[no][i + 1] = ptr[i + 14];
589 	}
590 }
591 
nukeCharset(int i)592 void ScummEngine::nukeCharset(int i) {
593 	assertRange(1, i, _numCharsets - 1, "charset");
594 	_res->nukeResource(rtCharset, i);
595 }
596 
ensureResourceLoaded(ResType type,ResId idx)597 void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) {
598 	debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", nameOfResType(type), idx);
599 
600 	if ((type == rtRoom) && idx > 0x7F && _game.version < 7 && _game.heversion <= 71) {
601 		idx = _resourceMapper[idx & 0x7F];
602 	}
603 
604 	// FIXME: This check used to be "idx==0". However, that causes
605 	// problems when using this function to ensure charset 0 is loaded.
606 	// This is done for many games, e.g. Zak256 or Indy3 (EGA and VGA).
607 	// For now we restrict the check to anything which is not a charset.
608 	// Question: Why was this check like that in the first place?
609 	// Answer: costumes with an index of zero in the newer games at least.
610 	// TODO: determine why the heck anything would try to load a costume
611 	// with id 0. Is that "normal", or is it caused by yet another bug in
612 	// our code base? After all we also have to add special cases for many
613 	// of our script opcodes that check for the (invalid) actor 0... so
614 	// maybe both issues are related...
615 	if (type != rtCharset && idx == 0)
616 		return;
617 
618 	if (idx <= _res->_types[type].size() && _res->_types[type][idx]._address)
619 		return;
620 
621 	loadResource(type, idx);
622 
623 	if (_game.version == 5 && type == rtRoom && (int)idx == _roomResource)
624 		VAR(VAR_ROOM_FLAG) = 1;
625 }
626 
loadResource(ResType type,ResId idx)627 int ScummEngine::loadResource(ResType type, ResId idx) {
628 	int roomNr;
629 	uint32 fileOffs;
630 	uint32 size, tag;
631 
632 	debugC(DEBUG_RESOURCE, "loadResource(%s,%d)", nameOfResType(type), idx);
633 
634 	if (type == rtCharset && (_game.features & GF_SMALL_HEADER)) {
635 		loadCharset(idx);
636 		return 1;
637 	}
638 
639 	roomNr = getResourceRoomNr(type, idx);
640 
641 	if (idx >= _res->_types[type].size())
642 		error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type].size(), roomNr);
643 
644 	if (roomNr == 0)
645 		roomNr = _roomResource;
646 
647 	fileOffs = getResourceRoomOffset(type, idx);
648 	if (fileOffs == RES_INVALID_OFFSET)
649 		return 0;
650 
651 	openRoom(roomNr);
652 
653 	_fileHandle->seek(fileOffs + _fileOffset, SEEK_SET);
654 
655 	if (_game.features & GF_OLD_BUNDLE) {
656 		if ((_game.version == 3) && !(_game.platform == Common::kPlatformAmiga) && (type == rtSound)) {
657 			return readSoundResourceSmallHeader(idx);
658 		} else {
659 			// WORKAROUND: Apple //gs MM has malformed sound resource #68
660 			if (_fileHandle->pos() + 2 > _fileHandle->size()) {
661 				warning("loadResource(%s,%d): resource is too short", nameOfResType(type), idx);
662 				size = 0;
663 			} else {
664 				size = _fileHandle->readUint16LE();
665 				_fileHandle->seek(-2, SEEK_CUR);
666 			}
667 		}
668 	} else if (_game.features & GF_SMALL_HEADER) {
669 		if (_game.version == 4)
670 			_fileHandle->seek(8, SEEK_CUR);
671 		size = _fileHandle->readUint32LE();
672 		tag = _fileHandle->readUint16LE();
673 		_fileHandle->seek(-6, SEEK_CUR);
674 		if ((type == rtSound) && !(_game.platform == Common::kPlatformAmiga) && !(_game.platform == Common::kPlatformFMTowns)) {
675 			return readSoundResourceSmallHeader(idx);
676 		}
677 	} else {
678 		if (type == rtSound) {
679 			return readSoundResource(idx);
680 		}
681 
682 		// Sanity check: Is this the right tag for this resource type?
683 		//
684 		// Currently disabled for newer HE games because they use different
685 		// tags. For example, for rtRoom, 'ROOM' changed to 'RMDA'; and for
686 		// rtImage, 'AWIZ' and 'MULT' can both occur simultaneously.
687 		// On the long run, it would be preferable to not turn this check off,
688 		// but instead to explicitly support the variations in the HE games.
689 		tag = _fileHandle->readUint32BE();
690 		if (tag != _res->_types[type]._tag && _game.heversion < 70) {
691 			error("Unknown res tag '%s' encountered (expected '%s') "
692 			        "while trying to load res (%s,%d) in room %d at %d+%d in file %s",
693 			        tag2str(tag), tag2str(_res->_types[type]._tag),
694 					nameOfResType(type), idx, roomNr,
695 					_fileOffset, fileOffs, _fileHandle->getName());
696 		}
697 
698 		size = _fileHandle->readUint32BE();
699 		_fileHandle->seek(-8, SEEK_CUR);
700 	}
701 	_fileHandle->read(_res->createResource(type, idx, size), size);
702 
703 	applyWorkaroundIfNeeded(type, idx);
704 
705 	// NB: The workaround may have changed the resource size, so don't rely on 'size' after this.
706 
707 	// dump the resource if requested
708 	if (_dumpScripts && type == rtScript) {
709 		dumpResource("script-", idx, getResourceAddress(rtScript, idx));
710 	}
711 
712 	if (_fileHandle->err() || _fileHandle->eos()) {
713 		error("Cannot read resource");
714 	}
715 
716 	return 1;
717 }
718 
getResourceRoomNr(ResType type,ResId idx)719 int ScummEngine::getResourceRoomNr(ResType type, ResId idx) {
720 	if (type == rtRoom && _game.heversion < 70)
721 		return idx;
722 	return _res->_types[type][idx]._roomno;
723 }
724 
getResourceRoomOffset(ResType type,ResId idx)725 uint32 ScummEngine::getResourceRoomOffset(ResType type, ResId idx) {
726 	if (type == rtRoom) {
727 		return (_game.version == 8) ? 8 : 0;
728 	}
729 	return _res->_types[type][idx]._roomoffs;
730 }
731 
getResourceRoomOffset(ResType type,ResId idx)732 uint32 ScummEngine_v70he::getResourceRoomOffset(ResType type, ResId idx) {
733 	if (type == rtRoom) {
734 		return _heV7RoomIntOffsets[idx];
735 	}
736 	return _res->_types[type][idx]._roomoffs;
737 }
738 
getResourceSize(ResType type,ResId idx)739 int ScummEngine::getResourceSize(ResType type, ResId idx) {
740 	byte *ptr = getResourceAddress(type, idx);
741 	assert(ptr);
742 	return _res->_types[type][idx]._size;
743 }
744 
getResourceAddress(ResType type,ResId idx)745 byte *ScummEngine::getResourceAddress(ResType type, ResId idx) {
746 	byte *ptr;
747 
748 	if (_game.heversion >= 80 && type == rtString)
749 		idx &= ~0x33539000;
750 
751 	if (!_res->validateResource("getResourceAddress", type, idx))
752 		return NULL;
753 
754 	// If the resource is missing, but loadable from the game data files, try to do so.
755 	if (!_res->_types[type][idx]._address && _res->_types[type]._mode != kDynamicResTypeMode) {
756 		ensureResourceLoaded(type, idx);
757 	}
758 
759 	ptr = (byte *)_res->_types[type][idx]._address;
760 	if (!ptr) {
761 		debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", nameOfResType(type), idx);
762 		return NULL;
763 	}
764 
765 	_res->setResourceCounter(type, idx, 1);
766 
767 	debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", nameOfResType(type), idx, (void *)ptr);
768 	return ptr;
769 }
770 
getStringAddress(ResId idx)771 byte *ScummEngine::getStringAddress(ResId idx) {
772 	byte *addr = getResourceAddress(rtString, idx);
773 	return addr;
774 }
775 
getStringAddress(ResId idx)776 byte *ScummEngine_v6::getStringAddress(ResId idx) {
777 	byte *addr = getResourceAddress(rtString, idx);
778 	if (addr == NULL)
779 		return NULL;
780 	// Skip over the ArrayHeader
781 	return addr + 6;
782 }
783 
getStringAddressVar(int i)784 byte *ScummEngine::getStringAddressVar(int i) {
785 	return getStringAddress(_scummVars[i]);
786 }
787 
increaseExpireCounter()788 void ResourceManager::increaseExpireCounter() {
789 	++_expireCounter;
790 	if (_expireCounter == 0) {	// overflow?
791 		increaseResourceCounters();
792 	}
793 }
794 
increaseResourceCounters()795 void ResourceManager::increaseResourceCounters() {
796 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
797 		ResId idx = _types[type].size();
798 		while (idx-- > 0) {
799 			byte counter = _types[type][idx].getResourceCounter();
800 			if (counter && counter < RF_USAGE_MAX) {
801 				setResourceCounter(type, idx, counter + 1);
802 			}
803 		}
804 	}
805 }
806 
setResourceCounter(ResType type,ResId idx,byte counter)807 void ResourceManager::setResourceCounter(ResType type, ResId idx, byte counter) {
808 	_types[type][idx].setResourceCounter(counter);
809 }
810 
setResourceCounter(byte counter)811 void ResourceManager::Resource::setResourceCounter(byte counter) {
812 	_flags &= RF_LOCK;	// Clear lower 7 bits, preserve the lock bit.
813 	_flags |= counter;	// Update the usage counter
814 }
815 
getResourceCounter() const816 byte ResourceManager::Resource::getResourceCounter() const {
817 	return _flags & RF_USAGE;
818 }
819 
820 /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */
821 #define SAFETY_AREA 2
822 
createResource(ResType type,ResId idx,uint32 size)823 byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) {
824 	debugC(DEBUG_RESOURCE, "_res->createResource(%s,%d,%d)", nameOfResType(type), idx, size);
825 
826 	if (!validateResource("allocating", type, idx))
827 		return NULL;
828 
829 	if (_vm->_game.version <= 2) {
830 		// Nuking and reloading a resource can be harmful in some
831 		// cases. For instance, Zak tries to reload the intro music
832 		// while it's playing. See bug #2115.
833 
834 		if (_types[type][idx]._address && (type == rtSound || type == rtScript || type == rtCostume))
835 			return _types[type][idx]._address;
836 	}
837 
838 	nukeResource(type, idx);
839 
840 	expireResources(size);
841 
842 	byte *ptr = new byte[size + SAFETY_AREA];
843 	if (ptr == NULL) {
844 		error("createResource(%s,%d): Out of memory while allocating %d", nameOfResType(type), idx, size);
845 	}
846 
847 	memset(ptr, 0, size + SAFETY_AREA);
848 	_allocatedSize += size;
849 
850 	_types[type][idx]._address = ptr;
851 	_types[type][idx]._size = size;
852 	setResourceCounter(type, idx, 1);
853 	return ptr;
854 }
855 
Resource()856 ResourceManager::Resource::Resource() {
857 	_address = 0;
858 	_size = 0;
859 	_flags = 0;
860 	_status = 0;
861 	_roomno = 0;
862 	_roomoffs = 0;
863 }
864 
~Resource()865 ResourceManager::Resource::~Resource() {
866 	delete[] _address;
867 	_address = 0;
868 }
869 
nuke()870 void ResourceManager::Resource::nuke() {
871 	delete[] _address;
872 	_address = 0;
873 	_size = 0;
874 	_flags = 0;
875 	_status &= ~RS_MODIFIED;
876 }
877 
ResTypeData()878 ResourceManager::ResTypeData::ResTypeData() {
879 	_mode = kDynamicResTypeMode;
880 	_tag = 0;
881 }
882 
~ResTypeData()883 ResourceManager::ResTypeData::~ResTypeData() {
884 }
885 
ResourceManager(ScummEngine * vm)886 ResourceManager::ResourceManager(ScummEngine *vm) : _vm(vm) {
887 	_allocatedSize = 0;
888 	_maxHeapThreshold = 0;
889 	_minHeapThreshold = 0;
890 	_expireCounter = 0;
891 }
892 
~ResourceManager()893 ResourceManager::~ResourceManager() {
894 	freeResources();
895 }
896 
setHeapThreshold(int min,int max)897 void ResourceManager::setHeapThreshold(int min, int max) {
898 	assert(0 < max);
899 	assert(min <= max);
900 	_maxHeapThreshold = max;
901 	_minHeapThreshold = min;
902 }
903 
validateResource(const char * str,ResType type,ResId idx) const904 bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const {
905 	if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type].size()) {
906 		warning("%s Illegal Glob type %s (%d) num %d", str, nameOfResType(type), type, idx);
907 		return false;
908 	}
909 	return true;
910 }
911 
nukeResource(ResType type,ResId idx)912 void ResourceManager::nukeResource(ResType type, ResId idx) {
913 	byte *ptr = _types[type][idx]._address;
914 	if (ptr != NULL) {
915 		debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", nameOfResType(type), idx);
916 		_allocatedSize -= _types[type][idx]._size;
917 		_types[type][idx].nuke();
918 	}
919 }
920 
findResourceData(uint32 tag,const byte * ptr)921 const byte *ScummEngine::findResourceData(uint32 tag, const byte *ptr) {
922 	if (_game.features & GF_OLD_BUNDLE)
923 		error("findResourceData must not be used in GF_OLD_BUNDLE games");
924 	else if (_game.features & GF_SMALL_HEADER)
925 		ptr = findResourceSmall(tag, ptr);
926 	else
927 		ptr = findResource(tag, ptr);
928 
929 	if (ptr == NULL)
930 		return NULL;
931 	return ptr + _resourceHeaderSize;
932 }
933 
getResourceDataSize(const byte * ptr) const934 int ScummEngine::getResourceDataSize(const byte *ptr) const {
935 	if (ptr == NULL)
936 		return 0;
937 
938 	if (_game.features & GF_OLD_BUNDLE)
939 		return READ_LE_UINT16(ptr) - _resourceHeaderSize;
940 	else if (_game.features & GF_SMALL_HEADER)
941 		return READ_LE_UINT32(ptr) - _resourceHeaderSize;
942 	else
943 		return READ_BE_UINT32(ptr - 4) - _resourceHeaderSize;
944 }
945 
lock(ResType type,ResId idx)946 void ResourceManager::lock(ResType type, ResId idx) {
947 	if (!validateResource("Locking", type, idx))
948 		return;
949 	_types[type][idx].lock();
950 }
951 
unlock(ResType type,ResId idx)952 void ResourceManager::unlock(ResType type, ResId idx) {
953 	if (!validateResource("Unlocking", type, idx))
954 		return;
955 	_types[type][idx].unlock();
956 }
957 
isLocked(ResType type,ResId idx) const958 bool ResourceManager::isLocked(ResType type, ResId idx) const {
959 	if (!validateResource("isLocked", type, idx))
960 		return false;
961 	return _types[type][idx].isLocked();
962 }
963 
lock()964 void ResourceManager::Resource::lock() {
965 	_flags |= RF_LOCK;
966 }
967 
unlock()968 void ResourceManager::Resource::unlock() {
969 	_flags &= ~RF_LOCK;
970 }
971 
isLocked() const972 bool ResourceManager::Resource::isLocked() const {
973 	return (_flags & RF_LOCK) != 0;
974 }
975 
isResourceInUse(ResType type,ResId idx) const976 bool ScummEngine::isResourceInUse(ResType type, ResId idx) const {
977 	if (!_res->validateResource("isResourceInUse", type, idx))
978 		return false;
979 	switch (type) {
980 	case rtRoom:
981 		return _roomResource == (byte)idx;
982 	case rtRoomImage:
983 		return _roomResource == (byte)idx;
984 	case rtRoomScripts:
985 		return _roomResource == (byte)idx;
986 	case rtScript:
987 		return isScriptInUse(idx);
988 	case rtCostume:
989 		return isCostumeInUse(idx);
990 	case rtSound:
991 		// Sound resource 1 is used for queued speech
992 		if (_game.heversion >= 60 && idx == 1)
993 			return true;
994 		else
995 			return _sound->isSoundInUse(idx);
996 	case rtCharset:
997 		return _charset->getCurID() == (int)idx;
998 	case rtImage:
999 		return _res->isModified(type, idx) != 0;
1000 	case rtSpoolBuffer:
1001 		return _sound->isSoundRunning(10000 + idx) != 0;
1002 	default:
1003 		return false;
1004 	}
1005 }
1006 
setModified(ResType type,ResId idx)1007 void ResourceManager::setModified(ResType type, ResId idx) {
1008 	if (!validateResource("Modified", type, idx))
1009 		return;
1010 	_types[type][idx].setModified();
1011 }
1012 
setOffHeap(ResType type,ResId idx)1013 void ResourceManager::setOffHeap(ResType type, ResId idx) {
1014 	if (!validateResource("setOffHeap", type, idx))
1015 		return;
1016 	_types[type][idx].setOffHeap();
1017 }
1018 
setOnHeap(ResType type,ResId idx)1019 void ResourceManager::setOnHeap(ResType type, ResId idx) {
1020 	if (!validateResource("setOnHeap", type, idx))
1021 		return;
1022 	_types[type][idx].setOnHeap();
1023 }
1024 
isModified(ResType type,ResId idx) const1025 bool ResourceManager::isModified(ResType type, ResId idx) const {
1026 	if (!validateResource("isModified", type, idx))
1027 		return false;
1028 	return _types[type][idx].isModified();
1029 }
1030 
isModified() const1031 bool ResourceManager::Resource::isModified() const {
1032 	return (_status & RS_MODIFIED) != 0;
1033 }
1034 
isOffHeap() const1035 bool ResourceManager::Resource::isOffHeap() const {
1036 	return (_status & RF_OFFHEAP) != 0;
1037 }
1038 
setModified()1039 void ResourceManager::Resource::setModified() {
1040 	_status |= RS_MODIFIED;
1041 }
1042 
setOffHeap()1043 void ResourceManager::Resource::setOffHeap() {
1044 	_status |= RF_OFFHEAP;
1045 }
1046 
setOnHeap()1047 void ResourceManager::Resource::setOnHeap() {
1048 	_status &= ~RF_OFFHEAP;
1049 }
1050 
expireResources(uint32 size)1051 void ResourceManager::expireResources(uint32 size) {
1052 	byte best_counter;
1053 	ResType best_type;
1054 	int best_res = 0;
1055 	uint32 oldAllocatedSize;
1056 
1057 	if (_expireCounter != 0xFF) {
1058 		_expireCounter = 0xFF;
1059 		increaseResourceCounters();
1060 	}
1061 
1062 	if (size + _allocatedSize < _maxHeapThreshold)
1063 		return;
1064 
1065 	oldAllocatedSize = _allocatedSize;
1066 
1067 	do {
1068 		best_type = rtInvalid;
1069 		best_counter = 2;
1070 
1071 		for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1072 			if (_types[type]._mode != kDynamicResTypeMode) {
1073 				// Resources of this type can be reloaded from the data files,
1074 				// so we can potentially unload them to free memory.
1075 				ResId idx = _types[type].size();
1076 				while (idx-- > 0) {
1077 					Resource &tmp = _types[type][idx];
1078 					byte counter = tmp.getResourceCounter();
1079 					if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(type, idx) && !tmp.isOffHeap()) {
1080 						best_counter = counter;
1081 						best_type = type;
1082 						best_res = idx;
1083 					}
1084 				}
1085 			}
1086 		}
1087 
1088 		if (!best_type)
1089 			break;
1090 		nukeResource(best_type, best_res);
1091 	} while (size + _allocatedSize > _minHeapThreshold);
1092 
1093 	increaseResourceCounters();
1094 
1095 	debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize);
1096 }
1097 
freeResources()1098 void ResourceManager::freeResources() {
1099 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1100 		ResId idx = _types[type].size();
1101 		while (idx-- > 0) {
1102 			if (isResourceLoaded(type, idx))
1103 				nukeResource(type, idx);
1104 		}
1105 		_types[type].clear();
1106 	}
1107 }
1108 
loadPtrToResource(ResType type,ResId idx,const byte * source)1109 void ScummEngine::loadPtrToResource(ResType type, ResId idx, const byte *source) {
1110 	byte *alloced;
1111 	int len;
1112 
1113 	bool sourceWasNull = !source;
1114 	int originalLen;
1115 
1116 	_res->nukeResource(type, idx);
1117 
1118 	len = resStrLen(source) + 1;
1119 	if (len <= 0)
1120 		return;
1121 
1122 	originalLen = len;
1123 
1124 	// Translate resource text
1125 	byte translateBuffer[512];
1126 	if (isScummvmKorTarget()) {
1127 		if (!source) {
1128 			refreshScriptPointer();
1129 			source = _scriptPointer;
1130 		}
1131 		translateText(source, translateBuffer);
1132 
1133 		source = translateBuffer;
1134 		len = resStrLen(source) + 1;
1135 	}
1136 
1137 	alloced = _res->createResource(type, idx, len);
1138 
1139 	if (!source) {
1140 		// Need to refresh the script pointer, since createResource may
1141 		// have caused the script resource to expire.
1142 		refreshScriptPointer();
1143 		memcpy(alloced, _scriptPointer, originalLen);
1144 		_scriptPointer += originalLen;
1145 	} else if (sourceWasNull) {
1146 		refreshScriptPointer();
1147 		memcpy(alloced, source, len);
1148 		_scriptPointer += originalLen;
1149 	} else {
1150 		memcpy(alloced, source, len);
1151 	}
1152 }
1153 
isResourceLoaded(ResType type,ResId idx) const1154 bool ResourceManager::isResourceLoaded(ResType type, ResId idx) const {
1155 	if (!validateResource("isResourceLoaded", type, idx))
1156 		return false;
1157 	return _types[type][idx]._address != NULL;
1158 }
1159 
resourceStats()1160 void ResourceManager::resourceStats() {
1161 	uint32 lockedSize = 0, lockedNum = 0;
1162 
1163 	for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) {
1164 		ResId idx = _types[type].size();
1165 		while (idx-- > 0) {
1166 			Resource &tmp = _types[type][idx];
1167 			if (tmp.isLocked() && tmp._address) {
1168 				lockedSize += tmp._size;
1169 				lockedNum++;
1170 			}
1171 		}
1172 	}
1173 
1174 	debug(1, "Total allocated size=%d, locked=%d(%d)", _allocatedSize, lockedSize, lockedNum);
1175 }
1176 
readMAXS(int blockSize)1177 void ScummEngine_v5::readMAXS(int blockSize) {
1178 	_numVariables = _fileHandle->readUint16LE();      // 800
1179 	_fileHandle->readUint16LE();                      // 16
1180 	_numBitVariables = _fileHandle->readUint16LE();   // 2048
1181 	_numLocalObjects = _fileHandle->readUint16LE();   // 200
1182 	_numArray = 50;
1183 	_numVerbs = 100;
1184 	// Used to be 50, which wasn't enough for MI2 and FOA. See bugs
1185 	// #1591, #1600 and #1607.
1186 	_numNewNames = 150;
1187 	_objectRoomTable = NULL;
1188 
1189 	_fileHandle->readUint16LE();                      // 50
1190 	_numCharsets = _fileHandle->readUint16LE();       // 9
1191 	_fileHandle->readUint16LE();                      // 100
1192 	_fileHandle->readUint16LE();                      // 50
1193 	_numInventory = _fileHandle->readUint16LE();      // 80
1194 	_numGlobalScripts = 200;
1195 
1196 	_shadowPaletteSize = 256;
1197 
1198 	_numFlObject = 50;
1199 
1200 	if (_shadowPaletteSize)
1201 		_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1202 }
1203 
1204 #ifdef ENABLE_SCUMM_7_8
readMAXS(int blockSize)1205 void ScummEngine_v8::readMAXS(int blockSize) {
1206 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over SCUMM engine version
1207 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over data file version
1208 	_numVariables = _fileHandle->readUint32LE();     // 1500
1209 	_numBitVariables = _fileHandle->readUint32LE();  // 2048
1210 	_fileHandle->readUint32LE();                     // 40
1211 	_numScripts = _fileHandle->readUint32LE();       // 458
1212 	_numSounds = _fileHandle->readUint32LE();        // 789
1213 	_numCharsets = _fileHandle->readUint32LE();      // 1
1214 	_numCostumes = _fileHandle->readUint32LE();      // 446
1215 	_numRooms = _fileHandle->readUint32LE();         // 95
1216 	_fileHandle->readUint32LE();                     // 80
1217 	_numGlobalObjects = _fileHandle->readUint32LE(); // 1401
1218 	_fileHandle->readUint32LE();                     // 60
1219 	_numLocalObjects = _fileHandle->readUint32LE();  // 200
1220 	_numNewNames = _fileHandle->readUint32LE();      // 100
1221 	_numFlObject = _fileHandle->readUint32LE();      // 128
1222 	_numInventory = _fileHandle->readUint32LE();     // 80
1223 	_numArray = _fileHandle->readUint32LE();         // 200
1224 	_numVerbs = _fileHandle->readUint32LE();         // 50
1225 
1226 	_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1227 	_numGlobalScripts = 2000;
1228 
1229 	_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
1230 	_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1231 }
1232 
readMAXS(int blockSize)1233 void ScummEngine_v7::readMAXS(int blockSize) {
1234 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over SCUMM engine version
1235 	_fileHandle->seek(50, SEEK_CUR);                 // Skip over data file version
1236 	_numVariables = _fileHandle->readUint16LE();
1237 	_numBitVariables = _fileHandle->readUint16LE();
1238 	_fileHandle->readUint16LE();
1239 	_numGlobalObjects = _fileHandle->readUint16LE();
1240 	_numLocalObjects = _fileHandle->readUint16LE();
1241 	_numNewNames = _fileHandle->readUint16LE();
1242 	_numVerbs = _fileHandle->readUint16LE();
1243 	_numFlObject = _fileHandle->readUint16LE();
1244 	_numInventory = _fileHandle->readUint16LE();
1245 	_numArray = _fileHandle->readUint16LE();
1246 	_numRooms = _fileHandle->readUint16LE();
1247 	_numScripts = _fileHandle->readUint16LE();
1248 	_numSounds = _fileHandle->readUint16LE();
1249 	_numCharsets = _fileHandle->readUint16LE();
1250 	_numCostumes = _fileHandle->readUint16LE();
1251 
1252 	_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1253 
1254 	if ((_game.id == GID_FT) && (_game.features & GF_DEMO) &&
1255 		(_game.platform == Common::kPlatformDOS))
1256 		_numGlobalScripts = 300;
1257 	else
1258 		_numGlobalScripts = 2000;
1259 
1260 	_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
1261 	_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1262 }
1263 #endif
1264 
readMAXS(int blockSize)1265 void ScummEngine_v6::readMAXS(int blockSize) {
1266 	if (blockSize == 38) {
1267 		_numVariables = _fileHandle->readUint16LE();
1268 		_fileHandle->readUint16LE();
1269 		_numBitVariables = _fileHandle->readUint16LE();
1270 		_numLocalObjects = _fileHandle->readUint16LE();
1271 		_numArray = _fileHandle->readUint16LE();
1272 		_fileHandle->readUint16LE();
1273 		_numVerbs = _fileHandle->readUint16LE();
1274 		_numFlObject = _fileHandle->readUint16LE();
1275 		_numInventory = _fileHandle->readUint16LE();
1276 		_numRooms = _fileHandle->readUint16LE();
1277 		_numScripts = _fileHandle->readUint16LE();
1278 		_numSounds = _fileHandle->readUint16LE();
1279 		_numCharsets = _fileHandle->readUint16LE();
1280 		_numCostumes = _fileHandle->readUint16LE();
1281 		_numGlobalObjects = _fileHandle->readUint16LE();
1282 		_numNewNames = 50;
1283 
1284 		_objectRoomTable = NULL;
1285 		_numGlobalScripts = 200;
1286 
1287 		if (_game.heversion >= 70) {
1288 			_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
1289 		}
1290 
1291 		if (_game.heversion <= 70) {
1292 			_shadowPaletteSize = 256;
1293 			_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
1294 		}
1295 	} else
1296 		error("readMAXS(%d) failed to read MAXS data", blockSize);
1297 }
1298 
readGlobalObjects()1299 void ScummEngine::readGlobalObjects() {
1300 	int i;
1301 	int num = _fileHandle->readUint16LE();
1302 	assert(num == _numGlobalObjects);
1303 	assert(_objectStateTable);
1304 	assert(_objectOwnerTable);
1305 
1306 	_fileHandle->read(_objectOwnerTable, num);
1307 	for (i = 0; i < num; i++) {
1308 		_objectStateTable[i] = _objectOwnerTable[i] >> OF_STATE_SHL;
1309 		_objectOwnerTable[i] &= OF_OWNER_MASK;
1310 	}
1311 
1312 	_fileHandle->read(_classData, num * sizeof(uint32));
1313 
1314 #if defined(SCUMM_BIG_ENDIAN)
1315 	// Correct the endianess if necessary
1316 	for (i = 0; i != num; i++)
1317 		_classData[i] = FROM_LE_32(_classData[i]);
1318 #endif
1319 }
1320 
1321 #ifdef ENABLE_SCUMM_7_8
readGlobalObjects()1322 void ScummEngine_v8::readGlobalObjects() {
1323 	int i;
1324 	int num = _fileHandle->readUint32LE();
1325 	assert(num == _numGlobalObjects);
1326 	assert(_objectStateTable);
1327 	assert(_objectOwnerTable);
1328 
1329 	_objectIDMap = new ObjectNameId[num];
1330 	_objectIDMapSize = num;
1331 	for (i = 0; i < num; i++) {
1332 		// Add to object name-to-id map
1333 		_fileHandle->read(_objectIDMap[i].name, 40);
1334 		_objectIDMap[i].id = i;
1335 
1336 		_objectStateTable[i] = _fileHandle->readByte();
1337 		_objectRoomTable[i] = _fileHandle->readByte();
1338 		_classData[i] = _fileHandle->readUint32LE();
1339 	}
1340 	memset(_objectOwnerTable, 0xFF, num);
1341 
1342 	// Finally, sort the object name->ID map, so we can later use
1343 	// bsearch on it. For this we (ab)use strcmp, which works fine
1344 	// since the table entries start with a string.
1345 	qsort(_objectIDMap, _objectIDMapSize, sizeof(ObjectNameId),
1346 			(int (*)(const void*, const void*))strcmp);
1347 }
1348 
readGlobalObjects()1349 void ScummEngine_v7::readGlobalObjects() {
1350 	int num = _fileHandle->readUint16LE();
1351 	assert(num == _numGlobalObjects);
1352 	assert(_objectStateTable);
1353 	assert(_objectOwnerTable);
1354 
1355 	_fileHandle->read(_objectStateTable, num);
1356 	_fileHandle->read(_objectRoomTable, num);
1357 	memset(_objectOwnerTable, 0xFF, num);
1358 
1359 	_fileHandle->read(_classData, num * sizeof(uint32));
1360 
1361 #if defined(SCUMM_BIG_ENDIAN)
1362 	// Correct the endianess if necessary
1363 	for (int i = 0; i != num; i++)
1364 		_classData[i] = FROM_LE_32(_classData[i]);
1365 #endif
1366 }
1367 #endif
1368 
allocateArrays()1369 void ScummEngine::allocateArrays() {
1370 	// Note: Buffers are now allocated in scummMain to allow for
1371 	//     early GUI init.
1372 
1373 	_objectOwnerTable = (byte *)calloc(_numGlobalObjects, 1);
1374 	_objectStateTable = (byte *)calloc(_numGlobalObjects, 1);
1375 	_classData = (uint32 *)calloc(_numGlobalObjects, sizeof(uint32));
1376 	_newNames = (uint16 *)calloc(_numNewNames, sizeof(uint16));
1377 
1378 	_inventory = (uint16 *)calloc(_numInventory, sizeof(uint16));
1379 	_verbs = (VerbSlot *)calloc(_numVerbs, sizeof(VerbSlot));
1380 	_objs = (ObjectData *)calloc(_numLocalObjects, sizeof(ObjectData));
1381 	_roomVars = (int32 *)calloc(_numRoomVariables, sizeof(int32));
1382 	_scummVars = (int32 *)calloc(_numVariables, sizeof(int32));
1383 	_bitVars = (byte *)calloc(_numBitVariables >> 3, 1);
1384 	if (_game.heversion >= 60) {
1385 		_arraySlot = (byte *)calloc(_numArray, 1);
1386 	}
1387 
1388 	_res->allocResTypeData(rtCostume, (_game.features & GF_NEW_COSTUMES) ? MKTAG('A','K','O','S') : MKTAG('C','O','S','T'),
1389 								_numCostumes, kStaticResTypeMode);
1390 	_res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, kStaticResTypeMode);
1391 	_res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, kStaticResTypeMode);
1392 	_res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, kStaticResTypeMode);
1393 	_res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, kSoundResTypeMode);
1394 	_res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, kStaticResTypeMode);
1395 	_res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, kStaticResTypeMode);
1396 	_res->allocResTypeData(rtObjectName, 0, _numNewNames, kDynamicResTypeMode);
1397 	_res->allocResTypeData(rtInventory, 0, _numInventory, kDynamicResTypeMode);
1398 	_res->allocResTypeData(rtTemp, 0, 10, kDynamicResTypeMode);
1399 	_res->allocResTypeData(rtScaleTable, 0, 5, kDynamicResTypeMode);
1400 	_res->allocResTypeData(rtActorName, 0, _numActors, kDynamicResTypeMode);
1401 	_res->allocResTypeData(rtVerb, 0, _numVerbs, kDynamicResTypeMode);
1402 	_res->allocResTypeData(rtString, 0, _numArray, kDynamicResTypeMode);
1403 	_res->allocResTypeData(rtFlObject, 0, _numFlObject, kDynamicResTypeMode);
1404 	_res->allocResTypeData(rtMatrix, 0, 10, kDynamicResTypeMode);
1405 	_res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, kStaticResTypeMode);
1406 	_res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, kStaticResTypeMode);
1407 }
1408 
allocateArrays()1409 void ScummEngine_v70he::allocateArrays() {
1410 	ScummEngine::allocateArrays();
1411 
1412 	_res->allocResTypeData(rtSpoolBuffer, 0, 9, kStaticResTypeMode);
1413 	_heV7RoomIntOffsets = (uint32 *)calloc(_numRooms, sizeof(uint32));
1414 }
1415 
1416 
dumpResource(const char * tag,int id,const byte * ptr,int length)1417 void ScummEngine::dumpResource(const char *tag, int id, const byte *ptr, int length) {
1418 	char buf[256];
1419 	Common::DumpFile out;
1420 
1421 	uint32 size;
1422 	if (length >= 0)
1423 		size = length;
1424 	else if (_game.features & GF_OLD_BUNDLE)
1425 		size = READ_LE_UINT16(ptr);
1426 	else if (_game.features & GF_SMALL_HEADER)
1427 		size = READ_LE_UINT32(ptr);
1428 	else
1429 		size = READ_BE_UINT32(ptr + 4);
1430 
1431 	sprintf(buf, "dumps/%s%d.dmp", tag, id);
1432 
1433 	out.open(buf);
1434 	if (out.isOpen() == false)
1435 		return;
1436 	out.write(ptr, size);
1437 	out.close();
1438 }
1439 
ResourceIterator(const byte * searchin,bool smallHeader)1440 ResourceIterator::ResourceIterator(const byte *searchin, bool smallHeader)
1441 	: _ptr(searchin), _smallHeader(smallHeader) {
1442 	assert(searchin);
1443 	if (_smallHeader) {
1444 		_size = READ_LE_UINT32(searchin);
1445 		_pos = 6;
1446 		_ptr = searchin + 6;
1447 	} else {
1448 		_size = READ_BE_UINT32(searchin + 4);
1449 		_pos = 8;
1450 		_ptr = searchin + 8;
1451 	}
1452 
1453 }
1454 
findNext(uint32 tag)1455 const byte *ResourceIterator::findNext(uint32 tag) {
1456 	uint32 size = 0;
1457 	const byte *result = 0;
1458 
1459 	if (_smallHeader) {
1460 		uint16 smallTag = newTag2Old(tag);
1461 		do {
1462 			if (_pos >= _size)
1463 				return 0;
1464 
1465 			result = _ptr;
1466 			size = READ_LE_UINT32(result);
1467 			if ((int32)size <= 0)
1468 				return 0;	// Avoid endless loop
1469 
1470 			_pos += size;
1471 			_ptr += size;
1472 		} while (READ_LE_UINT16(result + 4) != smallTag);
1473 	} else {
1474 		do {
1475 			if (_pos >= _size)
1476 				return 0;
1477 
1478 			result = _ptr;
1479 			size = READ_BE_UINT32(result + 4);
1480 			if ((int32)size <= 0)
1481 				return 0;	// Avoid endless loop
1482 
1483 			_pos += size;
1484 			_ptr += size;
1485 		} while (READ_BE_UINT32(result) != tag);
1486 	}
1487 
1488 	return result;
1489 }
1490 
findResource(uint32 tag,const byte * searchin)1491 const byte *ScummEngine::findResource(uint32 tag, const byte *searchin) {
1492 	uint32 curpos, totalsize, size;
1493 
1494 	debugC(DEBUG_RESOURCE, "findResource(%s, %p)", tag2str(tag), (const void *)searchin);
1495 
1496 	if (!searchin) {
1497 		if (_game.heversion >= 70) {
1498 			searchin = _resourceLastSearchBuf;
1499 			totalsize = _resourceLastSearchSize;
1500 			curpos = 0;
1501 		} else {
1502 			assert(searchin);
1503 			return NULL;
1504 		}
1505 	} else {
1506 		searchin += 4;
1507 		_resourceLastSearchSize = totalsize = READ_BE_UINT32(searchin);
1508 		curpos = 8;
1509 		searchin += 4;
1510 	}
1511 
1512 	while (curpos < totalsize) {
1513 		if (READ_BE_UINT32(searchin) == tag) {
1514 			_resourceLastSearchBuf = searchin;
1515 			return searchin;
1516 		}
1517 
1518 		size = READ_BE_UINT32(searchin + 4);
1519 		if ((int32)size <= 0) {
1520 			error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
1521 			return NULL;
1522 		}
1523 
1524 		curpos += size;
1525 		searchin += size;
1526 	}
1527 
1528 	return NULL;
1529 }
1530 
findResourceSmall(uint32 tag,const byte * searchin)1531 const byte *findResourceSmall(uint32 tag, const byte *searchin) {
1532 	uint32 curpos, totalsize, size;
1533 	uint16 smallTag;
1534 
1535 	smallTag = newTag2Old(tag);
1536 	if (smallTag == 0)
1537 		return NULL;
1538 
1539 	assert(searchin);
1540 
1541 	totalsize = READ_LE_UINT32(searchin);
1542 	searchin += 6;
1543 	curpos = 6;
1544 
1545 	while (curpos < totalsize) {
1546 		size = READ_LE_UINT32(searchin);
1547 
1548 		if (READ_LE_UINT16(searchin + 4) == smallTag)
1549 			return searchin;
1550 
1551 		if ((int32)size <= 0) {
1552 			error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
1553 			return NULL;
1554 		}
1555 
1556 		curpos += size;
1557 		searchin += size;
1558 	}
1559 
1560 	return NULL;
1561 }
1562 
newTag2Old(uint32 newTag)1563 uint16 newTag2Old(uint32 newTag) {
1564 	switch (newTag) {
1565 	case (MKTAG('R','M','H','D')):
1566 		return (0x4448);	// HD
1567 	case (MKTAG('I','M','0','0')):
1568 		return (0x4D42);	// BM
1569 	case (MKTAG('E','X','C','D')):
1570 		return (0x5845);	// EX
1571 	case (MKTAG('E','N','C','D')):
1572 		return (0x4E45);	// EN
1573 	case (MKTAG('S','C','A','L')):
1574 		return (0x4153);	// SA
1575 	case (MKTAG('L','S','C','R')):
1576 		return (0x534C);	// LS
1577 	case (MKTAG('O','B','C','D')):
1578 		return (0x434F);	// OC
1579 	case (MKTAG('O','B','I','M')):
1580 		return (0x494F);	// OI
1581 	case (MKTAG('S','M','A','P')):
1582 		return (0x4D42);	// BM
1583 	case (MKTAG('C','L','U','T')):
1584 		return (0x4150);	// PA
1585 	case (MKTAG('B','O','X','D')):
1586 		return (0x5842);	// BX
1587 	case (MKTAG('C','Y','C','L')):
1588 		return (0x4343);	// CC
1589 	case (MKTAG('E','P','A','L')):
1590 		return (0x5053);	// SP
1591 	case (MKTAG('T','I','L','E')):
1592 		return (0x4C54);	// TL
1593 	case (MKTAG('Z','P','0','0')):
1594 		return (0x505A);	// ZP
1595 	default:
1596 		return (0);
1597 	}
1598 }
1599 
nameOfResType(ResType type)1600 const char *nameOfResType(ResType type) {
1601 	static char buf[100];
1602 
1603 	switch (type) {
1604 	case rtRoom:
1605 		return "Room";
1606 	case rtScript:
1607 		return "Script";
1608 	case rtCostume:
1609 		return "Costume";
1610 	case rtSound:
1611 		return "Sound";
1612 	case rtInventory:
1613 		return "Inventory";
1614 	case rtCharset:
1615 		return "Charset";
1616 	case rtString:
1617 		return "String";
1618 	case rtVerb:
1619 		return "Verb";
1620 	case rtActorName:
1621 		return "ActorName";
1622 	case rtBuffer:
1623 		return "Buffer";
1624 	case rtScaleTable:
1625 		return "ScaleTable";
1626 	case rtTemp:
1627 		return "Temp";
1628 	case rtFlObject:
1629 		return "FlObject";
1630 	case rtMatrix:
1631 		return "Matrix";
1632 	case rtBox:
1633 		return "Box";
1634 	case rtObjectName:
1635 		return "ObjectName";
1636 	case rtRoomScripts:
1637 		return "RoomScripts";
1638 	case rtRoomImage:
1639 		return "RoomImage";
1640 	case rtImage:
1641 		return "Image";
1642 	case rtTalkie:
1643 		return "Talkie";
1644 	case rtSpoolBuffer:
1645 		return "SpoolBuffer";
1646 	default:
1647 		sprintf(buf, "rt%d", type);
1648 		return buf;
1649 	}
1650 }
1651 
applyWorkaroundIfNeeded(ResType type,int idx)1652 void ScummEngine::applyWorkaroundIfNeeded(ResType type, int idx) {
1653 	// The resource isn't always loaded into memory, in which case no
1654 	// workaround is needed. This happens when loading some HE savegames
1655 	// where sound resource 1 isn't loaded. Possibly other cases as well.
1656 	if (!_res->isResourceLoaded(type, idx))
1657 		return;
1658 
1659 	int size = getResourceSize(type, idx);
1660 
1661 	// WORKAROUND: FM-TOWNS Zak used the extra 40 pixels at the bottom to increase the inventory to 10 items
1662 	// if we trim to 200 pixels, we can show only 6 items
1663 	// therefore we patch the inventory script (20)
1664 	// replacing the 5 occurences of 10 as limit to 6
1665 	if (_game.platform == Common::kPlatformFMTowns && _game.id == GID_ZAK && ConfMan.getBool("trim_fmtowns_to_200_pixels")) {
1666 		if (type == rtScript && idx == 20) {
1667 			byte *ptr = getResourceAddress(rtScript, idx);
1668 			for (int cnt = 5; cnt; ptr++) {
1669 				if (*ptr == 10) {
1670 					*ptr = 6;
1671 					cnt--;
1672 				}
1673 			}
1674 		}
1675 	}
1676 
1677 	// WORKAROUND: The Mac version of Monkey Island 2 that was distributed
1678 	// on CD as the LucasArts Adventure Game Pack II is missing the part of
1679 	// the boot script that shows the copy protection and difficulty
1680 	// selection screen. Presumably it didn't include the code wheel. In
1681 	// fact, none of the games on this CD have any copy protection.
1682 	//
1683 	// The games on the first Game Pack CD does have copy protection, but
1684 	// since I only own the discs I can neither confirm nor deny if the
1685 	// necessary documentation was included.
1686 	//
1687 	// However, this means that there is no way to pick the difficulty
1688 	// level. Since ScummVM bypasses the copy protection check, there is
1689 	// no harm in showing the screen by simply re-inserging the missing
1690 	// part of the script.
1691 
1692 	else if (_game.id == GID_MONKEY2 && _game.platform == Common::kPlatformMacintosh && type == rtScript && idx == 1 && size == 6718) {
1693 		byte *unpatchedScript = getResourceAddress(type, idx);
1694 
1695 		const byte patch[] = {
1696 0x48, 0x00, 0x40, 0x00, 0x00, 0x13, 0x00, // if (Local[0] == 0) {
1697 0x33, 0x03, 0x00, 0x00, 0xc8, 0x00,       //     SetScreen(0,200);
1698 0x0a, 0x82, 0xff,                         //     startScript(130,[]);
1699 0x80,                                     //     breakHere();
1700 0x68, 0x00, 0x00, 0x82,                   //     VAR_RESULT = isScriptRunning(130);
1701 0x28, 0x00, 0x00, 0xf6, 0xff,             //     unless (!VAR_RESULT) goto 0955;
1702                                           // }
1703 0x48, 0x00, 0x40, 0x3f, 0xe1, 0x1d, 0x00, // if (Local[0] == -7873) [
1704 0x1a, 0x32, 0x00, 0x3f, 0x01,             //     VAR_MAINMENU_KEY = 319;
1705 0x33, 0x03, 0x00, 0x00, 0xc8, 0x00,       //     SetScreen(0,200);
1706 0x0a, 0x82, 0xff,                         //     startScript(130,[]);
1707 0x80,                                     //     breakHere();
1708 0x68, 0x00, 0x00, 0x82,                   //     VAR_RESULT = isScriptRunning(130);
1709 0x28, 0x00, 0x00, 0xf6, 0xff,             //     unless (!VAR_RESULT) goto 0955;
1710 0x1a, 0x00, 0x40, 0x00, 0x00              //     Local[0] = 0;
1711                                           // }
1712 		};
1713 
1714 		byte *patchedScript = new byte[6780];
1715 
1716 		memcpy(patchedScript, unpatchedScript, 2350);
1717 		memcpy(patchedScript + 2350, patch, sizeof(patch));
1718 		memcpy(patchedScript + 2350 + sizeof(patch), unpatchedScript + 2350, 6718 - 2350);
1719 
1720 		WRITE_BE_UINT32(patchedScript + 4, 6780);
1721 
1722 		// Just to be completely safe, check that the patched script now
1723 		// matches the boot script from the other known Mac version.
1724 		// Only if it does can we replace the unpatched script.
1725 
1726 		if (verifyMI2MacBootScript(patchedScript, 6780)) {
1727 			byte *newResource = _res->createResource(type, idx, 6780);
1728 			memcpy(newResource, patchedScript, 6780);
1729 		} else
1730 			warning("Could not patch MI2 Mac boot script");
1731 
1732 		delete[] patchedScript;
1733 	} else
1734 
1735 	// For some reason, the CD version of Monkey Island 1 removes some of
1736 	// the text when giving the wimpy idol to the cannibals. It looks like
1737 	// a mistake, because one of the text that is printed is immediately
1738 	// overwritten. This probably affects all CD versions, so we just have
1739 	// to add further patches as they are reported.
1740 
1741 	if (_game.id == GID_MONKEY && type == rtRoom && idx == 25) {
1742 		tryPatchMI1CannibalScript(getResourceAddress(type, idx), size);
1743 	} else
1744 
1745 	// There is a cracked version of Maniac Mansion v2 that attempts to
1746 	// remove the security door copy protection. With it, any code is
1747 	// accepted as long as you get the last digit wrong. Unfortunately,
1748 	// it changes a script that is used by all keypads in the game, which
1749 	// means some puzzles are completely nerfed.
1750 	//
1751 	// Even worse, this is the version that GOG and Steam are selling. No,
1752 	// seriously! I've reported this as a bug, but it remains unclear
1753 	// whether or not they will fix it.
1754 
1755 	if (_game.id == GID_MANIAC && _game.version == 2 && _game.platform == Common::kPlatformDOS && type == rtScript && idx == 44 && size == 199) {
1756 		byte *data = getResourceAddress(type, idx);
1757 
1758 		if (data[184] == 0) {
1759 			Common::MemoryReadStream stream(data, size);
1760 			Common::String md5 = Common::computeStreamMD5AsString(stream);
1761 
1762 			if (md5 == "11adc9b47497b26ac2b9627e0982b3fe") {
1763 				warning("Removing bad copy protection crack from keypad script");
1764 				data[184] = 1;
1765 			}
1766 		}
1767 	}
1768 }
1769 
verifyMI2MacBootScript()1770 bool ScummEngine::verifyMI2MacBootScript() {
1771 	return verifyMI2MacBootScript(getResourceAddress(rtScript, 1), getResourceSize(rtScript, 1));
1772 }
1773 
verifyMI2MacBootScript(byte * buf,int size)1774 bool ScummEngine::verifyMI2MacBootScript(byte *buf, int size) {
1775 	if (size == 6780) {
1776 		Common::MemoryReadStream stream(buf, size);
1777 		Common::String md5 = Common::computeStreamMD5AsString(stream);
1778 
1779 		if (md5 != "92b1cb7902b57d02b8e7434903d8508b") {
1780 			warning("Unexpected MI2 Mac boot script checksum: %s", md5.c_str());
1781 			return false;
1782 		}
1783 	} else {
1784 		warning("Unexpected MI2 Mac boot script length: %d", size);
1785 		return false;
1786 	}
1787 	return true;
1788 }
1789 
tryPatchMI1CannibalScript(byte * buf,int size)1790 bool ScummEngine::tryPatchMI1CannibalScript(byte *buf, int size) {
1791 	assert(_game.id == GID_MONKEY);
1792 
1793 	// The room resource is a collection of resources. We need to know the
1794 	// offset to the initial LSCR tag of the room-25-205 script, and its
1795 	// length up to (but not including) the LSCR tag of the next script.
1796 	// Furthermore we need to know the offset and length of the part of
1797 	// the script that we are going to replace. As an illustration, this
1798 	// is what that part of script looks like in the English CD version:
1799 	//
1800 	// [009C] (AE) WaitForMessage();
1801 	// [009E] (14) print(3,[Text("Oooh, that's nice.")]);
1802 	// [00B4] (14) print(3,[Text("And it says, `Made by Lemonhead`^" +
1803 	//             wait() + "^just like one of mine!" + wait() +
1804 	//             "We should take this to the Great Monkey.")]);
1805 	// [011C] (AE) WaitForMessage();
1806 	//
1807 	// What we want to do is make it behave like the script from the VGA
1808 	// floppy version:
1809 	//
1810 	// [009E] (AE) WaitForMessage();
1811 	// [00A0] (14) print(3,[Text("Oooh, that's nice." + wait() +
1812 	//             "Simple.  Just like one of mine." + wait() +
1813 	//             "And little.  Like mine.")]);
1814 	// [00F0] (AE) WaitForMessage();
1815 	// [00F2] (14) print(3,[Text("And it says, `Made by Lemonhead`^" +
1816 	//             wait() + "^just like one of mine!" + wait() +
1817 	//             "We should take this to the Great Monkey.")]);
1818 	// [015A] (AE) WaitForMessage();
1819 	//
1820 	// So we want to adjust the message, and insert a WaitForMessage().
1821 	// Unfortunately there isn't enough space to do that, and rather than
1822 	// modifying the length of the whole resource (which is easy to get
1823 	// wrong), we insert a placeholder message that gets replaced by
1824 	// decodeParseString().
1825 	//
1826 	// There should be enough space to do this even if we only change the
1827 	// first message. Any leftover space in the message is padded with
1828 	// spaces, since I can't find any NOP opcode.
1829 
1830 	int expectedSize = -1;
1831 	int scriptOffset = -1;
1832 	int scriptLength = -1;
1833 	Common::String expectedMd5;
1834 	int patchOffset = -1;
1835 	int patchLength = -1;
1836 	byte lang[3];
1837 
1838 	switch (_language) {
1839 	case Common::EN_ANY:
1840 		expectedSize = 82906;
1841 		scriptOffset = 73883;
1842 		scriptLength = 607;
1843 		expectedMd5 = "98b1126a836ef5bfefff10b605b20555";
1844 		patchOffset = 167;
1845 		patchLength = 22;
1846 		lang[0] = 'E';
1847 		lang[1] = 'N';
1848 		lang[2] = 'G';
1849 
1850 		// The Macintosh resource is 4 bytes shorter, which affects
1851 		// the script offset as well. Otherwise, both Mac versions
1852 		// that I have are identical to the DOS CD version in this
1853 		// particular case.
1854 
1855 		if (_game.platform == Common::kPlatformMacintosh) {
1856 			expectedSize -= 4;
1857 			scriptOffset -= 4;
1858 		} else if (_game.platform == Common::kPlatformFMTowns) {
1859 			expectedSize = 82817;
1860 			scriptOffset = 73794;
1861 		} else if (_game.platform == Common::kPlatformSegaCD) {
1862 			expectedSize = 61844;
1863 			scriptOffset = 51703;
1864 		}
1865 		break;
1866 	case Common::DE_DEU:
1867 		expectedSize = 83554;
1868 		scriptOffset = 74198;
1869 		scriptLength = 632;
1870 		expectedMd5 = "27d6d8eab4e0f66792e10769090ae047";
1871 		patchOffset = 170;
1872 		patchLength = 23;
1873 		lang[0] = 'D';
1874 		lang[1] = 'E';
1875 		lang[2] = 'U';
1876 		break;
1877 	case Common::IT_ITA:
1878 		expectedSize = 83211;
1879 		scriptOffset = 73998;
1880 		scriptLength = 602;
1881 		expectedMd5 = "39eb6116d67f2318f31d6fa98df2e931";
1882 		patchOffset = 161;
1883 		patchLength = 20;
1884 		lang[0] = 'I';
1885 		lang[1] = 'T';
1886 		lang[2] = 'A';
1887 		break;
1888 	case Common::ES_ESP:
1889 		expectedSize = 82829;
1890 		scriptOffset = 73905;
1891 		scriptLength = 579;
1892 		expectedMd5 = "0e282d86f80d4e062a9a145601e6fed3";
1893 		patchOffset = 161;
1894 		patchLength = 21;
1895 		lang[0] = 'E';
1896 		lang[1] = 'S';
1897 		lang[2] = 'P';
1898 		break;
1899 	default:
1900 		return false;
1901 	}
1902 
1903 	// Note that the patch will not apply to the "Ultimate Talkie" edition
1904 	// since that script has been patched to a different length.
1905 
1906 	if (size == expectedSize) {
1907 		// There isn't enough space in the script for the revised
1908 		// texts, so these abbreviations will be expanded in
1909 		// decodeParseString().
1910 		const byte patchData[] = {
1911 			0x14, 0x03, 0x0F,       // print(3,[Text("/LH.$$$/");
1912 			0x2F, 0x4C, 0x48, 0x2E,
1913 			0x24, 0x24, 0x24, 0x2F  // No terminating 0x00!
1914 		};
1915 
1916 		byte *scriptPtr = buf + scriptOffset;
1917 
1918 		// Check that the data is a local script.
1919 		if (READ_BE_UINT32(scriptPtr) != MKTAG('L','S','C','R'))
1920 			return false;
1921 
1922 		// Check that the first instruction to be patched is o5_print
1923 		if (scriptPtr[patchOffset] != 0x14)
1924 			return false;
1925 
1926 		// Check that the MD5 sum matches a known patchable script.
1927 		Common::MemoryReadStream stream(buf + scriptOffset, scriptLength);
1928 		Common::String md5 = Common::computeStreamMD5AsString(stream);
1929 
1930 		if (md5 != expectedMd5)
1931 			return false;
1932 
1933 		// Insert the script patch and tag it with the appropriate
1934 		// language.
1935 
1936 		memcpy(scriptPtr + patchOffset, patchData, sizeof(patchData));
1937 		memcpy(scriptPtr + patchOffset + 7, lang, sizeof(lang));
1938 
1939 		// Pad the rest of the replaced script part with spaces before
1940 		// terminating the string. Finally, add WaitForMessage().
1941 
1942 		memset(scriptPtr + patchOffset + sizeof(patchData), 32, patchLength - sizeof(patchData) - 3);
1943 		scriptPtr[patchOffset + patchLength - 3] = 0;
1944 		scriptPtr[patchOffset + patchLength - 2] = 0xAE;
1945 		scriptPtr[patchOffset + patchLength - 1] = 0x02;
1946 	}
1947 
1948 	return true;
1949 }
1950 
1951 
1952 } // End of namespace Scumm
1953