2002-05-22 12:20:36 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2001-10-09 14:30:12 +00:00
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2004-01-06 12:45:34 +00:00
|
|
|
* Copyright (C) 2001-2004 The ScummVM project
|
2001-10-09 14:30:12 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
2001-11-06 20:00:47 +00:00
|
|
|
* $Header$
|
2001-10-09 14:30:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2002-12-24 16:10:31 +00:00
|
|
|
#include "common/str.h"
|
2003-09-11 10:32:15 +00:00
|
|
|
#include "scumm/dialogs.h"
|
|
|
|
#include "scumm/imuse.h"
|
2004-01-06 17:28:29 +00:00
|
|
|
#include "scumm/imuse_digi/dimuse.h"
|
2003-09-11 10:32:15 +00:00
|
|
|
#include "scumm/object.h"
|
|
|
|
#include "scumm/resource.h"
|
|
|
|
#include "scumm/scumm.h"
|
|
|
|
#include "scumm/sound.h"
|
|
|
|
#include "scumm/verbs.h"
|
|
|
|
#include "sound/mididrv.h" // Need MD_ enum values
|
2002-07-16 21:03:14 +00:00
|
|
|
|
2003-10-03 18:33:57 +00:00
|
|
|
namespace Scumm {
|
|
|
|
|
2003-06-25 23:49:54 +00:00
|
|
|
static uint16 newTag2Old(uint32 oldTag);
|
|
|
|
static const char *resTypeFromId(int id);
|
2002-07-16 21:03:14 +00:00
|
|
|
|
2004-07-26 17:14:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
ScummFile::ScummFile() : _encbyte(0), _subFileStart(0), _subFileLen(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScummFile::setEnc(byte value) {
|
|
|
|
_encbyte = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScummFile::setSubfileRange(uint32 start, uint32 len) {
|
|
|
|
// TODO: Add sanity checks
|
|
|
|
const uint32 fileSize = File::size();
|
|
|
|
assert(start <= fileSize);
|
|
|
|
assert(start + len <= fileSize);
|
|
|
|
_subFileStart = start;
|
|
|
|
_subFileLen = len;
|
|
|
|
seek(0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScummFile::resetSubfile() {
|
|
|
|
_subFileStart = 0;
|
|
|
|
_subFileLen = 0;
|
|
|
|
seek(0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScummFile::open(const char *filename, AccessMode mode, const char *directory) {
|
|
|
|
if (File::open(filename, mode, directory)) {
|
|
|
|
resetSubfile();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScummFile::openSubFile(const char *filename) {
|
|
|
|
assert(isOpen());
|
|
|
|
|
|
|
|
// Disable the XOR encryption and reset any current subfile range
|
|
|
|
setEnc(0);
|
|
|
|
resetSubfile();
|
|
|
|
|
|
|
|
// Read in the filename table and look for the specified file
|
|
|
|
|
|
|
|
unsigned long file_off, file_len;
|
|
|
|
char file_name[0x20+1];
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
// Get the length of the data file to use for consistency checks
|
|
|
|
const uint32 data_file_len = size();
|
|
|
|
|
|
|
|
// Read offset and length to the file records */
|
|
|
|
const uint32 file_record_off = readUint32BE();
|
|
|
|
const uint32 file_record_len = readUint32BE();
|
|
|
|
|
|
|
|
// Do a quick check to make sure the offset and length are good
|
|
|
|
if (file_record_off + file_record_len > data_file_len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a little consistancy check on file_record_length
|
|
|
|
if (file_record_len % 0x28) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan through the files
|
|
|
|
for (i = 0; i < file_record_len; i += 0x28) {
|
|
|
|
// read a file record
|
|
|
|
seek(file_record_off + i, SEEK_SET);
|
|
|
|
file_off = readUint32BE();
|
|
|
|
file_len = readUint32BE();
|
|
|
|
read(file_name, 0x20);
|
|
|
|
file_name[0x20] = 0;
|
|
|
|
|
|
|
|
assert(file_name[0]);
|
2004-07-26 23:15:01 +00:00
|
|
|
//debug(7, " extracting \'%s\'", file_name);
|
2004-07-26 17:14:57 +00:00
|
|
|
|
|
|
|
// Consistency check. make sure the file data is in the file
|
|
|
|
if (file_off + file_len > data_file_len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scumm_stricmp(file_name, filename) == 0) {
|
|
|
|
// We got a match!
|
|
|
|
setSubfileRange(file_off, file_len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ScummFile::eof() {
|
|
|
|
return _subFileLen ? (pos() >= _subFileLen) : File::eof();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 ScummFile::pos() {
|
|
|
|
return File::pos() - _subFileStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 ScummFile::size() {
|
|
|
|
return _subFileLen ? _subFileLen : File::size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScummFile::seek(int32 offs, int whence) {
|
|
|
|
if (_subFileLen) {
|
|
|
|
// Constrain the seek to the subfile
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_END:
|
|
|
|
offs = _subFileStart + _subFileLen - offs;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
offs += _subFileStart;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
offs += File::pos();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert((int32)_subFileStart <= offs && offs <= (int32)(_subFileStart + _subFileLen));
|
|
|
|
whence = SEEK_SET;
|
|
|
|
}
|
|
|
|
File::seek(offs, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 ScummFile::read(void *ptr, uint32 len) {
|
|
|
|
uint32 realLen;
|
|
|
|
|
|
|
|
if (_subFileLen) {
|
|
|
|
// Limit the amount we read by the subfile boundaries.
|
|
|
|
const uint32 curPos = pos();
|
|
|
|
assert(_subFileLen >= curPos);
|
|
|
|
uint32 newPos = curPos + len;
|
|
|
|
if (newPos > _subFileLen) {
|
|
|
|
len = _subFileLen - curPos;
|
|
|
|
_ioFailed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
realLen = File::read(ptr, len);
|
|
|
|
|
|
|
|
|
|
|
|
// If an encryption byte was specified, XOR the data we just read by it.
|
|
|
|
// This simple kind of "encryption" was used by some of the older SCUMM
|
|
|
|
// games.
|
|
|
|
if (_encbyte) {
|
|
|
|
byte *p = (byte *)ptr;
|
|
|
|
byte *end = p + realLen;
|
|
|
|
while (p < end)
|
|
|
|
*p++ ^= _encbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
return realLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 ScummFile::write(const void *, uint32) {
|
|
|
|
error("ScummFile does not support writing!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
/* Open a room */
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::openRoom(int room) {
|
2002-02-24 17:25:03 +00:00
|
|
|
int room_offs, roomlimit;
|
2003-04-29 03:32:08 +00:00
|
|
|
bool result;
|
|
|
|
char buf[128];
|
|
|
|
char buf2[128] = "";
|
2003-07-28 16:55:06 +00:00
|
|
|
byte encByte = 0;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_GENERAL, "openRoom(%d)", room);
|
2002-12-22 21:58:16 +00:00
|
|
|
assert(room >= 0);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
/* Don't load the same room again */
|
|
|
|
if (_lastLoadedRoom == room)
|
|
|
|
return;
|
|
|
|
_lastLoadedRoom = room;
|
|
|
|
|
|
|
|
/* Room -1 means close file */
|
2002-04-11 17:19:16 +00:00
|
|
|
if (room == -1) {
|
2001-10-09 14:30:12 +00:00
|
|
|
deleteRoomOffsets();
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.close();
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either xxx.lfl or monkey.xxx file name */
|
2002-12-21 12:34:17 +00:00
|
|
|
while (1) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_features & GF_SMALL_NAMES)
|
|
|
|
roomlimit = 98;
|
|
|
|
else
|
|
|
|
roomlimit = 900;
|
|
|
|
if (_features & GF_EXTERNAL_CHARSET && room >= roomlimit)
|
|
|
|
room_offs = 0;
|
|
|
|
else
|
|
|
|
room_offs = room ? _roomFileOffsets[room] : 0;
|
|
|
|
|
2002-12-21 12:34:17 +00:00
|
|
|
if (room_offs == -1)
|
2001-10-09 14:30:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (room_offs != 0 && room != 0) {
|
|
|
|
_fileOffset = _roomFileOffsets[room];
|
|
|
|
return;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
if (!(_features & GF_SMALL_HEADER)) {
|
2002-06-27 10:40:37 +00:00
|
|
|
|
2004-06-06 02:20:53 +00:00
|
|
|
if (_heversion >= 70) { // Windows titles
|
2004-08-26 14:35:38 +00:00
|
|
|
if (_heversion >= 98)
|
|
|
|
sprintf(buf, "%s.%s", _gameName.c_str(), room == 0 ? "he0" : "(a)");
|
|
|
|
else
|
|
|
|
sprintf(buf, "%s.he%.1d", _gameName.c_str(), room == 0 ? 0 : 1);
|
2003-08-29 02:39:05 +00:00
|
|
|
} else if (_version >= 7) {
|
2003-06-07 00:13:26 +00:00
|
|
|
if (room > 0 && (_version == 8))
|
2003-05-08 15:48:50 +00:00
|
|
|
VAR(VAR_CURRENTDISK) = res.roomno[rtRoom][room];
|
2003-10-12 19:10:17 +00:00
|
|
|
sprintf(buf, "%s.la%d", _gameName.c_str(), room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-11-13 10:51:02 +00:00
|
|
|
|
2003-11-28 22:29:56 +00:00
|
|
|
sprintf(buf2, "%s.%.3d", _gameName.c_str(), room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-01-02 10:36:17 +00:00
|
|
|
} else if (_features & GF_HUMONGOUS)
|
2003-10-12 19:10:17 +00:00
|
|
|
sprintf(buf, "%s.he%.1d", _gameName.c_str(), room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-04-29 03:32:08 +00:00
|
|
|
else {
|
2003-10-12 19:10:17 +00:00
|
|
|
sprintf(buf, "%s.%.3d", _gameName.c_str(), room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-04-29 03:32:08 +00:00
|
|
|
if (_gameId == GID_SAMNMAX)
|
2003-10-12 19:10:17 +00:00
|
|
|
sprintf(buf2, "%s.sm%.1d", _gameName.c_str(), room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-04-29 03:32:08 +00:00
|
|
|
}
|
2002-06-27 10:40:37 +00:00
|
|
|
|
2003-07-28 16:55:06 +00:00
|
|
|
encByte = (_features & GF_USE_KEY) ? 0x69 : 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
} else if (!(_features & GF_SMALL_NAMES)) {
|
|
|
|
if (room == 0 || room >= 900) {
|
2002-09-15 19:28:34 +00:00
|
|
|
sprintf(buf, "%.3d.lfl", room);
|
2003-07-28 16:55:06 +00:00
|
|
|
encByte = 0;
|
|
|
|
if (openResourceFile(buf, encByte)) {
|
2002-02-12 21:28:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-02 10:36:17 +00:00
|
|
|
askForDisk(buf, room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2002-02-12 21:28:07 +00:00
|
|
|
|
|
|
|
} else {
|
2003-01-02 10:36:17 +00:00
|
|
|
sprintf(buf, "disk%.2d.lec", room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2003-07-28 16:55:06 +00:00
|
|
|
encByte = 0x69;
|
2002-02-12 21:28:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-09-15 19:28:34 +00:00
|
|
|
sprintf(buf, "%.2d.lfl", room);
|
2004-02-29 00:18:45 +00:00
|
|
|
// Maniac Mansion demo has .man instead of .lfl
|
|
|
|
if (_gameId == GID_MANIAC)
|
|
|
|
sprintf(buf2, "%.2d.man", room);
|
2003-07-28 16:55:06 +00:00
|
|
|
encByte = (_features & GF_USE_KEY) ? 0xFF : 0;
|
2002-02-12 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
2003-07-28 16:55:06 +00:00
|
|
|
result = openResourceFile(buf, encByte);
|
2004-02-29 00:18:45 +00:00
|
|
|
if ((result == false) && (buf2[0])) {
|
2003-07-28 16:55:06 +00:00
|
|
|
result = openResourceFile(buf2, encByte);
|
2004-02-29 00:18:45 +00:00
|
|
|
// We have .man files so set demo mode
|
|
|
|
if (_gameId == GID_MANIAC)
|
|
|
|
_demoMode = true;
|
|
|
|
}
|
2003-04-29 03:32:08 +00:00
|
|
|
|
|
|
|
if (result) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (room == 0)
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_features & GF_EXTERNAL_CHARSET && room >= roomlimit)
|
2002-02-20 21:56:38 +00:00
|
|
|
return;
|
2001-10-09 14:30:12 +00:00
|
|
|
readRoomsOffsets();
|
|
|
|
_fileOffset = _roomFileOffsets[room];
|
|
|
|
|
|
|
|
if (_fileOffset != 8)
|
|
|
|
return;
|
|
|
|
|
|
|
|
error("Room %d not in %s", room, buf);
|
|
|
|
return;
|
|
|
|
}
|
2003-01-02 10:36:17 +00:00
|
|
|
askForDisk(buf, room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
sprintf(buf, "%.3d.lfl", room);
|
2003-07-28 16:55:06 +00:00
|
|
|
encByte = 0;
|
|
|
|
if (openResourceFile(buf, encByte))
|
2001-10-09 14:30:12 +00:00
|
|
|
break;
|
2003-01-02 10:36:17 +00:00
|
|
|
askForDisk(buf, room == 0 ? 0 : res.roomno[rtRoom][room]);
|
2002-04-11 17:19:16 +00:00
|
|
|
} while (1);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
deleteRoomOffsets();
|
2002-12-22 21:58:16 +00:00
|
|
|
_fileOffset = 0; // start of file
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::closeRoom() {
|
2002-12-22 21:58:16 +00:00
|
|
|
if (_lastLoadedRoom != -1) {
|
|
|
|
_lastLoadedRoom = -1;
|
|
|
|
deleteRoomOffsets();
|
|
|
|
_fileHandle.close();
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-05-30 20:13:29 +00:00
|
|
|
/** Delete the currently loaded room offsets. */
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::deleteRoomOffsets() {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (!(_features & GF_SMALL_HEADER) && !_dynamicRoomOffsets)
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-12-26 23:11:35 +00:00
|
|
|
for (int i = 0; i < _numRooms; i++) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_roomFileOffsets[i] != 0xFFFFFFFF)
|
2001-10-09 14:30:12 +00:00
|
|
|
_roomFileOffsets[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-30 20:13:29 +00:00
|
|
|
/** Read room offsets */
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::readRoomsOffsets() {
|
2003-05-16 16:09:16 +00:00
|
|
|
int num, room, i;
|
|
|
|
byte *ptr;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
debug(9, "readRoomOffsets()");
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
deleteRoomOffsets();
|
|
|
|
if (_features & GF_SMALL_NAMES)
|
|
|
|
return;
|
|
|
|
|
2004-06-06 02:20:53 +00:00
|
|
|
if (_heversion >= 70) { // Windows titles
|
2003-05-16 16:09:16 +00:00
|
|
|
num = READ_LE_UINT16(_HEV7RoomOffsets);
|
|
|
|
ptr = _HEV7RoomOffsets + 2;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
_roomFileOffsets[i] = READ_LE_UINT32(ptr);
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (!(_features & GF_SMALL_HEADER)) {
|
|
|
|
if (!_dynamicRoomOffsets)
|
|
|
|
return;
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(16, SEEK_SET);
|
2002-04-11 17:19:16 +00:00
|
|
|
} else {
|
2002-10-23 14:02:46 +00:00
|
|
|
_fileHandle.seek(12, SEEK_SET); // Directly searching for the room offset block would be more generic...
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-09-10 07:34:27 +00:00
|
|
|
num = _fileHandle.readByte();
|
2002-12-24 02:18:02 +00:00
|
|
|
while (num--) {
|
2002-09-10 07:34:27 +00:00
|
|
|
room = _fileHandle.readByte();
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_roomFileOffsets[room] != 0xFFFFFFFF) {
|
2002-10-21 13:23:25 +00:00
|
|
|
_roomFileOffsets[room] = _fileHandle.readUint32LE();
|
2001-10-09 14:30:12 +00:00
|
|
|
} else {
|
2002-10-21 13:23:25 +00:00
|
|
|
_fileHandle.readUint32LE();
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-26 23:15:01 +00:00
|
|
|
bool ScummEngine::openFile(ScummFile &file, const char *filename) {
|
2004-07-26 17:14:57 +00:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (!_containerFile.isEmpty()) {
|
2004-07-26 23:15:01 +00:00
|
|
|
file.close();
|
|
|
|
file.open(_containerFile.c_str());
|
|
|
|
assert(file.isOpen());
|
2004-07-26 17:14:57 +00:00
|
|
|
|
2004-07-26 23:15:01 +00:00
|
|
|
result = file.openSubFile(filename);
|
2004-07-26 17:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
2004-07-26 23:15:01 +00:00
|
|
|
file.close();
|
|
|
|
result = file.open(filename);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2004-07-26 23:15:01 +00:00
|
|
|
|
2004-07-26 17:14:57 +00:00
|
|
|
return result;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2004-07-26 23:15:01 +00:00
|
|
|
bool ScummEngine::openResourceFile(const char *filename, byte encByte) {
|
|
|
|
debugC(DEBUG_GENERAL, "openResourceFile(%s)", filename);
|
|
|
|
|
|
|
|
if (openFile(_fileHandle, filename)) {
|
|
|
|
_fileHandle.setEnc(encByte);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::askForDisk(const char *filename, int disknum) {
|
2003-01-02 10:36:17 +00:00
|
|
|
char buf[128];
|
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8) {
|
2003-01-02 10:36:17 +00:00
|
|
|
char result;
|
|
|
|
|
2004-04-26 07:47:12 +00:00
|
|
|
_imuseDigital->stopAllSounds();
|
2003-04-20 00:56:23 +00:00
|
|
|
|
2003-05-24 22:17:45 +00:00
|
|
|
#ifdef MACOSX
|
2004-02-05 22:39:13 +00:00
|
|
|
sprintf(buf, "Cannot find file: '%s'\nPlease insert disc %d.\nPress OK to retry, Quit to exit", filename, disknum);
|
2003-05-24 22:17:45 +00:00
|
|
|
#else
|
2004-06-28 00:00:33 +00:00
|
|
|
sprintf(buf, "Cannot find file: '%s'\nInsert disc %d into drive %s\nPress OK to retry, Quit to exit", filename, disknum, _gameDataPath.c_str());
|
2003-05-24 22:17:45 +00:00
|
|
|
#endif
|
2003-01-02 10:36:17 +00:00
|
|
|
|
2004-03-28 13:28:44 +00:00
|
|
|
result = displayMessage("Quit", buf);
|
2004-08-11 21:49:58 +00:00
|
|
|
if (!result) {
|
2004-04-08 11:48:35 +00:00
|
|
|
#ifdef __PALM_OS__
|
|
|
|
error("Cannot find file: '%s'", filename);
|
|
|
|
#else
|
2004-04-04 12:36:50 +00:00
|
|
|
_system->quit();
|
2004-04-08 11:48:35 +00:00
|
|
|
#endif
|
2004-04-04 12:36:50 +00:00
|
|
|
}
|
2003-01-02 10:36:17 +00:00
|
|
|
} else {
|
|
|
|
sprintf(buf, "Cannot find file: '%s'", filename);
|
2003-11-02 02:18:16 +00:00
|
|
|
InfoDialog dialog(this, (char*)buf);
|
2003-09-11 10:32:15 +00:00
|
|
|
runDialog(dialog);
|
2004-04-08 11:48:35 +00:00
|
|
|
#ifdef __PALM_OS__
|
|
|
|
error("Cannot find file: '%s'", filename);
|
|
|
|
#else
|
2004-04-04 12:36:50 +00:00
|
|
|
_system->quit();
|
2004-04-08 11:48:35 +00:00
|
|
|
#endif
|
2003-01-02 10:36:17 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::readIndexFile() {
|
2002-04-11 17:19:16 +00:00
|
|
|
uint32 blocktype, itemsize;
|
2001-10-09 14:30:12 +00:00
|
|
|
int numblock = 0;
|
2001-11-26 19:57:57 +00:00
|
|
|
int num, i;
|
2003-04-28 05:54:54 +00:00
|
|
|
bool stop = false;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_GENERAL, "readIndexFile()");
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-12-22 21:58:16 +00:00
|
|
|
closeRoom();
|
2001-10-09 14:30:12 +00:00
|
|
|
openRoom(0);
|
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version <= 5) {
|
2001-11-26 19:57:57 +00:00
|
|
|
/* Figure out the sizes of various resources */
|
2002-09-10 07:34:27 +00:00
|
|
|
while (!_fileHandle.eof()) {
|
2001-11-26 19:57:57 +00:00
|
|
|
blocktype = fileReadDword();
|
2002-10-21 13:23:25 +00:00
|
|
|
itemsize = _fileHandle.readUint32BE();
|
2002-09-10 07:34:27 +00:00
|
|
|
if (_fileHandle.ioFailed())
|
2001-11-26 19:57:57 +00:00
|
|
|
break;
|
2002-04-11 17:19:16 +00:00
|
|
|
switch (blocktype) {
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DOBJ'):
|
2002-10-21 13:23:25 +00:00
|
|
|
_numGlobalObjects = _fileHandle.readUint16LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
itemsize -= 2;
|
2001-11-26 19:57:57 +00:00
|
|
|
break;
|
|
|
|
case MKID('DROO'):
|
2002-10-21 13:23:25 +00:00
|
|
|
_numRooms = _fileHandle.readUint16LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
itemsize -= 2;
|
2001-11-26 19:57:57 +00:00
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DSCR'):
|
2002-10-21 13:23:25 +00:00
|
|
|
_numScripts = _fileHandle.readUint16LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
itemsize -= 2;
|
2001-11-26 19:57:57 +00:00
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DCOS'):
|
2002-10-21 13:23:25 +00:00
|
|
|
_numCostumes = _fileHandle.readUint16LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
itemsize -= 2;
|
2001-11-26 19:57:57 +00:00
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DSOU'):
|
2002-10-21 13:23:25 +00:00
|
|
|
_numSounds = _fileHandle.readUint16LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
itemsize -= 2;
|
2001-10-09 14:30:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(itemsize - 8, SEEK_CUR);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.clearIOFailed();
|
|
|
|
_fileHandle.seek(0, SEEK_SET);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-04-28 05:54:54 +00:00
|
|
|
while (!stop) {
|
2001-10-16 10:01:48 +00:00
|
|
|
blocktype = fileReadDword();
|
|
|
|
|
2002-09-10 07:34:27 +00:00
|
|
|
if (_fileHandle.ioFailed())
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
2002-10-21 13:23:25 +00:00
|
|
|
itemsize = _fileHandle.readUint32BE();
|
2001-10-16 10:01:48 +00:00
|
|
|
|
|
|
|
numblock++;
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
switch (blocktype) {
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DCHR'):
|
2003-04-28 05:54:54 +00:00
|
|
|
case MKID('DIRF'):
|
2003-05-16 16:09:16 +00:00
|
|
|
readResTypeList(rtCharset, MKID('CHAR'), "charset");
|
2003-04-28 05:54:54 +00:00
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DOBJ'):
|
2004-06-25 08:18:51 +00:00
|
|
|
debug(9, "found DOBJ block, reading object table");
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-10-21 13:23:25 +00:00
|
|
|
num = _fileHandle.readUint32LE();
|
2002-07-04 15:48:17 +00:00
|
|
|
else
|
2002-10-21 13:23:25 +00:00
|
|
|
num = _fileHandle.readUint16LE();
|
2001-11-26 19:57:57 +00:00
|
|
|
assert(num == _numGlobalObjects);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8) { /* FIXME: Not sure.. */
|
2002-12-24 16:10:31 +00:00
|
|
|
char buffer[40];
|
2002-07-07 20:32:26 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2002-12-24 16:10:31 +00:00
|
|
|
_fileHandle.read(buffer, 40);
|
|
|
|
if (buffer[0]) {
|
|
|
|
// Add to object name-to-id map
|
|
|
|
_objectIDMap[buffer] = i;
|
|
|
|
}
|
2002-09-10 07:34:27 +00:00
|
|
|
_objectStateTable[i] = _fileHandle.readByte();
|
|
|
|
_objectRoomTable[i] = _fileHandle.readByte();
|
2002-10-21 13:23:25 +00:00
|
|
|
_classData[i] = _fileHandle.readUint32LE();
|
2002-07-04 15:48:17 +00:00
|
|
|
}
|
|
|
|
memset(_objectOwnerTable, 0xFF, num);
|
2003-06-07 00:13:26 +00:00
|
|
|
} else if (_version == 7) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(_objectStateTable, num);
|
|
|
|
_fileHandle.read(_objectRoomTable, num);
|
2001-11-26 19:57:57 +00:00
|
|
|
memset(_objectOwnerTable, 0xFF, num);
|
2004-08-26 14:07:19 +00:00
|
|
|
} else if (_heversion >= 70) { // HE Windows titles
|
2004-06-06 02:20:53 +00:00
|
|
|
_fileHandle.read(_objectStateTable, num);
|
|
|
|
_fileHandle.read(_objectOwnerTable, num);
|
|
|
|
_fileHandle.read(_objectRoomTable, num);
|
2004-08-26 14:07:19 +00:00
|
|
|
// FIXME more DOBJ bits left to sort out here
|
2001-11-26 19:57:57 +00:00
|
|
|
} else {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(_objectOwnerTable, num);
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
_objectStateTable[i] = _objectOwnerTable[i] >> OF_STATE_SHL;
|
2001-11-26 19:57:57 +00:00
|
|
|
_objectOwnerTable[i] &= OF_OWNER_MASK;
|
|
|
|
}
|
|
|
|
}
|
2002-12-24 16:10:31 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version != 8) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(_classData, num * sizeof(uint32));
|
2002-07-04 15:48:17 +00:00
|
|
|
|
|
|
|
// Swap flag endian where applicable
|
2002-04-11 15:22:02 +00:00
|
|
|
#if defined(SCUMM_BIG_ENDIAN)
|
2002-07-04 15:48:17 +00:00
|
|
|
for (i = 0; i != num; i++)
|
|
|
|
_classData[i] = FROM_LE_32(_classData[i]);
|
2002-04-11 15:22:02 +00:00
|
|
|
#endif
|
2002-07-04 15:48:17 +00:00
|
|
|
}
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('RNAM'):
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(itemsize - 8, SEEK_CUR);
|
2004-06-25 08:18:51 +00:00
|
|
|
debug(9, "found RNAM block, skipping");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
2003-05-16 16:09:16 +00:00
|
|
|
|
|
|
|
case MKID('DLFL'):
|
2003-08-28 14:25:29 +00:00
|
|
|
i = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.seek(-2, SEEK_CUR);
|
|
|
|
_HEV7RoomOffsets = (byte *)calloc(2 + (i * 4), 1);
|
|
|
|
_fileHandle.read(_HEV7RoomOffsets, (2 + (i * 4)) );
|
2004-06-25 08:18:51 +00:00
|
|
|
debug(9, "found DLFL block, offsets read");
|
2003-05-16 16:09:16 +00:00
|
|
|
break;
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2003-08-29 02:39:05 +00:00
|
|
|
case MKID('DIRM'):
|
2004-08-24 02:02:56 +00:00
|
|
|
readResTypeList(rtImage, MKID('AWIZ'), "images");
|
2003-08-29 02:39:05 +00:00
|
|
|
break;
|
|
|
|
|
2004-08-23 08:35:35 +00:00
|
|
|
case MKID('DISK'):
|
|
|
|
_fileHandle.seek(itemsize - 8, SEEK_CUR);
|
|
|
|
warning("DISK index block not yet handled, skipping");
|
|
|
|
break;
|
|
|
|
|
2004-08-26 14:35:38 +00:00
|
|
|
case MKID('INIB'):
|
|
|
|
_fileHandle.seek(itemsize - 8, SEEK_CUR);
|
|
|
|
warning("INIB index block not yet handled, skipping");
|
|
|
|
break;
|
|
|
|
|
2004-08-24 02:02:56 +00:00
|
|
|
case MKID('DIRI'):
|
|
|
|
readResTypeList(rtRoom, MKID('RMIM'), "room image");
|
2003-04-28 05:54:54 +00:00
|
|
|
break;
|
|
|
|
|
2002-10-11 08:35:12 +00:00
|
|
|
case MKID('ANAM'):
|
2004-06-25 08:18:51 +00:00
|
|
|
debug(9, "found ANAM block, reading audio names");
|
2002-10-21 13:23:25 +00:00
|
|
|
_numAudioNames = _fileHandle.readUint16LE();
|
2002-10-11 08:35:12 +00:00
|
|
|
_audioNames = (char*)malloc(_numAudioNames * 9);
|
|
|
|
_fileHandle.read(_audioNames, _numAudioNames * 9);
|
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DROO'):
|
2002-04-11 17:19:16 +00:00
|
|
|
readResTypeList(rtRoom, MKID('ROOM'), "room");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
2003-04-28 05:54:54 +00:00
|
|
|
case MKID('DIRR'):
|
2003-05-16 16:09:16 +00:00
|
|
|
readResTypeList(rtRoom, MKID('RMDA'), "room");
|
2003-04-28 05:54:54 +00:00
|
|
|
break;
|
|
|
|
|
2002-07-07 20:32:26 +00:00
|
|
|
case MKID('DRSC'): // FIXME: Verify
|
|
|
|
readResTypeList(rtRoomScripts, MKID('RMSC'), "room script");
|
2002-07-04 15:48:17 +00:00
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DSCR'):
|
2003-04-28 05:54:54 +00:00
|
|
|
case MKID('DIRS'):
|
2003-05-16 16:09:16 +00:00
|
|
|
readResTypeList(rtScript, MKID('SCRP'), "script");
|
2003-04-28 05:54:54 +00:00
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DCOS'):
|
2003-04-28 05:54:54 +00:00
|
|
|
case MKID('DIRC'):
|
2003-05-16 16:09:16 +00:00
|
|
|
readResTypeList(rtCostume, MKID('COST'), "costume");
|
2003-04-28 05:54:54 +00:00
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('MAXS'):
|
2004-06-25 10:39:35 +00:00
|
|
|
readMAXS(itemsize);
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DSOU'):
|
2002-04-11 17:19:16 +00:00
|
|
|
readResTypeList(rtSound, MKID('SOUN'), "sound");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
2003-04-28 05:54:54 +00:00
|
|
|
case MKID('DIRN'):
|
|
|
|
readResTypeList(rtSound, MKID('DIRN'), "sound");
|
|
|
|
break;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('AARY'):
|
|
|
|
readArrayFromIndexFile();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-06-25 10:39:35 +00:00
|
|
|
error("Bad ID %04X('%s') found in index file directory!", blocktype,
|
|
|
|
tag2str(blocktype));
|
2001-10-16 10:01:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
// if (numblock!=9)
|
|
|
|
// error("Not enough blocks read from directory");
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2002-12-22 21:58:16 +00:00
|
|
|
closeRoom();
|
2001-10-16 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::readArrayFromIndexFile() {
|
2004-01-19 20:27:31 +00:00
|
|
|
error("readArrayFromIndexFile() not supported in pre-V6 games");
|
2001-10-16 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::readResTypeList(int id, uint32 tag, const char *name) {
|
2001-10-16 10:01:48 +00:00
|
|
|
int num;
|
|
|
|
int i;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2004-06-05 15:14:19 +00:00
|
|
|
debug(9, "readResTypeList(%s,%s,%s)", resTypeFromId(id), tag2str(TO_BE_32(tag)), name);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-10-21 13:23:25 +00:00
|
|
|
num = _fileHandle.readUint32LE();
|
2002-08-29 16:57:43 +00:00
|
|
|
else if (!(_features & GF_OLD_BUNDLE))
|
2002-10-21 13:23:25 +00:00
|
|
|
num = _fileHandle.readUint16LE();
|
2002-08-29 16:57:43 +00:00
|
|
|
else
|
2002-09-10 07:34:27 +00:00
|
|
|
num = _fileHandle.readByte();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-04-11 11:40:35 +00:00
|
|
|
if (_features & GF_OLD_BUNDLE) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (num >= 0xFF) {
|
2001-10-23 19:51:50 +00:00
|
|
|
error("Too many %ss (%d) in directory", name, num);
|
|
|
|
}
|
2003-04-11 11:40:35 +00:00
|
|
|
} else {
|
|
|
|
if (num != res.num[id]) {
|
|
|
|
error("Invalid number of %ss (%d) in directory", name, num);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2002-08-29 16:57:43 +00:00
|
|
|
if (_features & GF_OLD_BUNDLE) {
|
2002-12-22 21:58:16 +00:00
|
|
|
if (id == rtRoom) {
|
2002-08-29 16:57:43 +00:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
res.roomno[id][i] = i;
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(num, SEEK_CUR);
|
2002-08-29 16:57:43 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < num; i++)
|
2002-09-10 07:34:27 +00:00
|
|
|
res.roomno[id][i] = _fileHandle.readByte();
|
2002-08-29 16:57:43 +00:00
|
|
|
}
|
2003-05-01 22:19:10 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2002-10-21 13:23:25 +00:00
|
|
|
res.roomoffs[id][i] = _fileHandle.readUint16LE();
|
2003-05-01 22:26:30 +00:00
|
|
|
if (res.roomoffs[id][i] == 0xFFFF)
|
|
|
|
res.roomoffs[id][i] = 0xFFFFFFFF;
|
2003-05-01 22:19:10 +00:00
|
|
|
}
|
|
|
|
|
2002-08-29 16:57:43 +00:00
|
|
|
} else if (_features & GF_SMALL_HEADER) {
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2002-09-10 07:34:27 +00:00
|
|
|
res.roomno[id][i] = _fileHandle.readByte();
|
2002-11-28 23:47:58 +00:00
|
|
|
res.roomoffs[id][i] = _fileHandle.readUint32LE();
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-05-01 22:19:10 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
res.roomno[id][i] = _fileHandle.readByte();
|
|
|
|
}
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
res.roomoffs[id][i] = _fileHandle.readUint32LE();
|
2004-06-22 13:52:39 +00:00
|
|
|
|
|
|
|
if (id == rtRoom && _heversion >= 70)
|
|
|
|
_HEV7RoomIntOffsets[i] = res.roomoffs[id][i];
|
2003-05-01 22:19:10 +00:00
|
|
|
}
|
2004-06-06 02:20:53 +00:00
|
|
|
|
2004-06-06 02:36:44 +00:00
|
|
|
if (_heversion >= 70) {
|
2004-06-07 00:26:08 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
res.globsize[id][i] = _fileHandle.readUint32LE();
|
|
|
|
}
|
2004-06-06 02:36:44 +00:00
|
|
|
}
|
2002-11-28 23:47:58 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::allocResTypeData(int id, uint32 tag, int num, const char *name, int mode) {
|
2004-06-05 15:14:19 +00:00
|
|
|
debug(9, "allocResTypeData(%s/%s,%s,%d,%d)", resTypeFromId(id), name, tag2str(TO_BE_32(tag)), num, mode);
|
2003-05-18 21:03:57 +00:00
|
|
|
assert(id >= 0 && id < (int)(ARRAYSIZE(res.mode)));
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (num >= 2000) {
|
2004-06-25 10:39:35 +00:00
|
|
|
/* FIXME: this used to be an error but it seems the newer humongous titles
|
|
|
|
* exceed this presumably old limit, need to determine a new ceiling
|
|
|
|
*/
|
|
|
|
warning("Too many %ss (%d) in directory", name, num);
|
2001-10-16 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
res.mode[id] = mode;
|
|
|
|
res.num[id] = num;
|
|
|
|
res.tags[id] = tag;
|
|
|
|
res.name[id] = name;
|
2002-05-01 17:16:47 +00:00
|
|
|
res.address[id] = (byte **)calloc(num, sizeof(void *));
|
|
|
|
res.flags[id] = (byte *)calloc(num, sizeof(byte));
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
if (mode) {
|
2002-05-01 17:16:47 +00:00
|
|
|
res.roomno[id] = (byte *)calloc(num, sizeof(byte));
|
|
|
|
res.roomoffs[id] = (uint32 *)calloc(num, sizeof(uint32));
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2004-06-07 00:26:08 +00:00
|
|
|
|
2004-06-22 13:52:39 +00:00
|
|
|
if (_heversion >= 70) {
|
2004-06-07 00:26:08 +00:00
|
|
|
res.globsize[id] = (uint32 *)calloc(num, sizeof(uint32));
|
2004-06-22 13:52:39 +00:00
|
|
|
|
|
|
|
if (id == rtRoom)
|
|
|
|
_HEV7RoomIntOffsets = (uint32 *)calloc(num, sizeof(uint32));
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::loadCharset(int no) {
|
2002-04-13 04:39:04 +00:00
|
|
|
int i;
|
2001-10-09 14:30:12 +00:00
|
|
|
byte *ptr;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_GENERAL, "loadCharset(%d)", no);
|
2002-03-31 09:06:51 +00:00
|
|
|
|
2002-05-11 20:00:32 +00:00
|
|
|
/* FIXME - hack around crash in Indy4 (occurs if you try to load after dieing) */
|
|
|
|
if (_gameId == GID_INDY4 && no == 0)
|
|
|
|
no = 1;
|
2002-04-11 15:22:02 +00:00
|
|
|
|
2004-06-26 05:08:02 +00:00
|
|
|
/* for Humongous catalogs */
|
|
|
|
if (_heversion >= 70 && _numCharsets == 1) {
|
|
|
|
warning("not loading charset as it doesn't seem to exist?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-07 20:32:26 +00:00
|
|
|
assert(no < (int)sizeof(_charsetData) / 16);
|
2003-12-26 23:11:35 +00:00
|
|
|
checkRange(_numCharsets - 1, 1, no, "Loading illegal charset %d");
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-08-29 20:01:27 +00:00
|
|
|
// ensureResourceLoaded(rtCharset, no);
|
|
|
|
ptr = getResourceAddress(rtCharset, no);
|
2002-12-04 15:00:35 +00:00
|
|
|
if (_features & GF_SMALL_HEADER)
|
|
|
|
ptr -= 12;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < 15; i++) {
|
|
|
|
_charsetData[no][i + 1] = ptr[i + 14];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::nukeCharset(int i) {
|
2003-12-26 23:11:35 +00:00
|
|
|
checkRange(_numCharsets - 1, 1, i, "Nuking illegal charset %d");
|
2001-11-05 19:21:49 +00:00
|
|
|
nukeResource(rtCharset, i);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::ensureResourceLoaded(int type, int i) {
|
2003-04-30 13:23:31 +00:00
|
|
|
void *addr = NULL;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", resTypeFromId(type), i);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-06-23 09:13:25 +00:00
|
|
|
if ((type == rtRoom) && i > 0x7F && _version < 7) {
|
2002-11-23 13:53:46 +00:00
|
|
|
i = _resourceMapper[i & 0x7F];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 11:03:41 +00:00
|
|
|
// FIXME - TODO: This check used to be "i==0". However, that causes
|
|
|
|
// problems when using this function to ensure charset 0 is loaded.
|
2003-05-01 14:35:21 +00:00
|
|
|
// This is done for many games, e.g. Zak256 or Indy3 (EGA and VGA).
|
|
|
|
// For now we restrict the check to anything which is not a charset.
|
2003-05-01 11:40:21 +00:00
|
|
|
// Question: Why was this check like that in the first place?
|
2003-05-01 14:35:21 +00:00
|
|
|
// Answer: costumes with an index of zero in the newer games at least.
|
|
|
|
// TODO: determine why the heck anything would try to load a costume
|
|
|
|
// with id 0. Is that "normal", or is it caused by yet another bug in
|
|
|
|
// our code base? After all we also have to add special cases for many
|
|
|
|
// of our script opcodes that check for the (invalid) actor 0... so
|
|
|
|
// maybe both issues are related...
|
|
|
|
if (type != rtCharset && i == 0)
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
|
|
|
|
2003-04-30 13:23:31 +00:00
|
|
|
if (i <= res.num[type])
|
|
|
|
addr = res.address[type][i];
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
if (addr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
loadResource(type, i);
|
|
|
|
|
2004-08-14 03:16:32 +00:00
|
|
|
if (_version == 5 && type == rtRoom && i == _roomResource)
|
2004-07-01 04:45:56 +00:00
|
|
|
VAR(VAR_ROOM_FLAG) = 1;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::loadResource(int type, int idx) {
|
2002-11-28 23:47:58 +00:00
|
|
|
int roomNr;
|
2001-10-09 14:30:12 +00:00
|
|
|
uint32 fileOffs;
|
|
|
|
uint32 size, tag;
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "loadResource(%s,%d)", resTypeFromId(type),idx);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (type == rtCharset && (_features & GF_SMALL_HEADER)) {
|
|
|
|
loadCharset(idx);
|
|
|
|
return (1);
|
|
|
|
}
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
roomNr = getResourceRoomNr(type, idx);
|
2002-05-08 06:57:27 +00:00
|
|
|
|
|
|
|
if (idx >= res.num[type])
|
|
|
|
error("%s %d undefined %d %d", res.name[type], idx, res.num[type], roomNr);
|
|
|
|
|
|
|
|
if (roomNr == 0)
|
|
|
|
roomNr = _roomResource;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-06-22 13:52:39 +00:00
|
|
|
if (type == rtRoom) {
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-12-24 02:18:02 +00:00
|
|
|
fileOffs = 8;
|
2004-06-24 05:55:04 +00:00
|
|
|
else if (_heversion >= 70)
|
2004-06-22 13:52:39 +00:00
|
|
|
fileOffs = _HEV7RoomIntOffsets[idx];
|
2002-12-24 02:18:02 +00:00
|
|
|
else
|
|
|
|
fileOffs = 0;
|
2001-10-09 14:30:12 +00:00
|
|
|
} else {
|
2002-03-08 08:42:11 +00:00
|
|
|
fileOffs = res.roomoffs[type][idx];
|
2002-04-11 17:19:16 +00:00
|
|
|
if (fileOffs == 0xFFFFFFFF)
|
2001-10-09 14:30:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
openRoom(roomNr);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
_fileHandle.seek(fileOffs + _fileOffset, SEEK_SET);
|
2002-08-29 20:01:27 +00:00
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
if (_features & GF_OLD_BUNDLE) {
|
2003-08-12 16:09:41 +00:00
|
|
|
if ((_version == 3) && !(_features & GF_AMIGA) && (type == rtSound)) {
|
|
|
|
return readSoundResourceSmallHeader(type, idx);
|
|
|
|
} else {
|
|
|
|
size = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.seek(-2, SEEK_CUR);
|
|
|
|
}
|
2002-11-28 23:47:58 +00:00
|
|
|
} else if (_features & GF_SMALL_HEADER) {
|
|
|
|
if (!(_features & GF_SMALL_NAMES))
|
|
|
|
_fileHandle.seek(8, SEEK_CUR);
|
|
|
|
size = _fileHandle.readUint32LE();
|
|
|
|
tag = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.seek(-6, SEEK_CUR);
|
2003-07-18 05:49:21 +00:00
|
|
|
if ((type == rtSound) && !(_features & GF_AMIGA) && !(_features & GF_FMTOWNS)) {
|
2002-11-28 23:47:58 +00:00
|
|
|
return readSoundResourceSmallHeader(type, idx);
|
2003-05-03 12:48:18 +00:00
|
|
|
}
|
2002-11-28 23:47:58 +00:00
|
|
|
} else {
|
|
|
|
if (type == rtSound) {
|
|
|
|
return readSoundResource(type, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = fileReadDword();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-06-22 13:52:39 +00:00
|
|
|
if (tag != res.tags[type] && _heversion < 70) {
|
2002-12-24 02:03:35 +00:00
|
|
|
error("%s %d not in room %d at %d+%d in file %s",
|
|
|
|
res.name[type], idx, roomNr,
|
|
|
|
_fileOffset, fileOffs, _fileHandle.name());
|
2002-08-29 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
size = _fileHandle.readUint32BE();
|
|
|
|
_fileHandle.seek(-8, SEEK_CUR);
|
|
|
|
}
|
|
|
|
_fileHandle.read(createResource(type, idx, size), size);
|
|
|
|
|
2002-12-31 02:09:57 +00:00
|
|
|
// dump the resource if requested
|
|
|
|
if (_dumpScripts && type == rtScript) {
|
2002-11-28 23:47:58 +00:00
|
|
|
dumpResource("script-", idx, getResourceAddress(rtScript, idx));
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
if (!_fileHandle.ioFailed()) {
|
|
|
|
return 1;
|
2002-08-29 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
2002-11-28 23:47:58 +00:00
|
|
|
nukeResource(type, idx);
|
|
|
|
|
2002-08-29 20:01:27 +00:00
|
|
|
error("Cannot read resource");
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::readSoundResource(int type, int idx) {
|
2003-09-30 17:53:42 +00:00
|
|
|
uint32 pos, total_size, size, tag, basetag, max_total_size;
|
2001-12-01 17:06:13 +00:00
|
|
|
int pri, best_pri;
|
2002-04-11 17:19:16 +00:00
|
|
|
uint32 best_size = 0, best_offs = 0;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "readSoundResource(%s,%d)", resTypeFromId(type), idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-12-01 17:06:13 +00:00
|
|
|
pos = 0;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-09-30 17:53:42 +00:00
|
|
|
_fileHandle.readUint32LE();
|
|
|
|
max_total_size = _fileHandle.readUint32BE() - 8;
|
2002-03-16 18:58:51 +00:00
|
|
|
basetag = fileReadDword();
|
2002-10-21 13:23:25 +00:00
|
|
|
total_size = _fileHandle.readUint32BE();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, " basetag: %s, total_size=%d", tag2str(TO_BE_32(basetag)), total_size);
|
2002-05-17 21:58:59 +00:00
|
|
|
|
2002-05-13 08:25:58 +00:00
|
|
|
if (basetag == MKID('MIDI') || basetag == MKID('iMUS')) {
|
2003-08-15 07:45:21 +00:00
|
|
|
if (_midiDriver != MD_PCSPK && _midiDriver != MD_PCJR) {
|
|
|
|
_fileHandle.seek(-8, SEEK_CUR);
|
|
|
|
_fileHandle.read(createResource(type, idx, total_size + 8), total_size + 8);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-03-16 05:33:25 +00:00
|
|
|
} else if (basetag == MKID('SOU ')) {
|
2002-03-05 22:23:48 +00:00
|
|
|
best_pri = -1;
|
|
|
|
while (pos < total_size) {
|
|
|
|
tag = fileReadDword();
|
2002-10-21 13:23:25 +00:00
|
|
|
size = _fileHandle.readUint32BE() + 8;
|
2002-03-05 22:23:48 +00:00
|
|
|
pos += size;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-04-13 18:34:11 +00:00
|
|
|
pri = -1;
|
2002-07-07 20:32:26 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
switch (tag) {
|
2003-09-30 17:53:42 +00:00
|
|
|
case MKID('TOWS'):
|
|
|
|
pri = 16;
|
|
|
|
break;
|
2002-08-19 12:56:12 +00:00
|
|
|
case MKID('SBL '):
|
|
|
|
pri = 15;
|
|
|
|
break;
|
2002-04-11 17:19:16 +00:00
|
|
|
case MKID('ADL '):
|
2003-05-16 22:00:33 +00:00
|
|
|
pri = 1;
|
2003-08-13 14:08:21 +00:00
|
|
|
if (_midiDriver == MD_ADLIB)
|
2002-04-13 18:34:11 +00:00
|
|
|
pri = 10;
|
2002-04-11 17:19:16 +00:00
|
|
|
break;
|
2003-05-28 11:56:50 +00:00
|
|
|
case MKID('AMI '):
|
|
|
|
pri = 3;
|
|
|
|
break;
|
2002-04-11 17:19:16 +00:00
|
|
|
case MKID('ROL '):
|
2003-05-16 22:00:33 +00:00
|
|
|
pri = 3;
|
2003-10-02 14:37:40 +00:00
|
|
|
if (_native_mt32)
|
|
|
|
pri = 5;
|
2002-04-11 17:19:16 +00:00
|
|
|
break;
|
|
|
|
case MKID('GMD '):
|
2003-05-16 22:00:33 +00:00
|
|
|
pri = 4;
|
2002-04-26 16:52:00 +00:00
|
|
|
break;
|
|
|
|
case MKID('MAC '):
|
2003-05-16 22:00:33 +00:00
|
|
|
pri = 2;
|
2002-04-11 17:19:16 +00:00
|
|
|
break;
|
2002-05-05 20:01:54 +00:00
|
|
|
case MKID('SPK '):
|
2003-05-07 19:24:14 +00:00
|
|
|
pri = -1;
|
2003-08-13 14:08:21 +00:00
|
|
|
// if (_midiDriver == MD_PCSPK)
|
|
|
|
// pri = 11;
|
2002-05-05 20:01:54 +00:00
|
|
|
break;
|
2002-03-05 22:23:48 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-08-13 14:08:21 +00:00
|
|
|
if ((_midiDriver == MD_PCSPK || _midiDriver == MD_PCJR) && pri != 11)
|
|
|
|
pri = -1;
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, " tag: %s, total_size=%d, pri=%d", tag2str(TO_BE_32(tag)), size, pri);
|
2002-07-07 20:32:26 +00:00
|
|
|
|
2002-05-17 21:58:59 +00:00
|
|
|
|
2002-03-05 22:23:48 +00:00
|
|
|
if (pri > best_pri) {
|
|
|
|
best_pri = pri;
|
|
|
|
best_size = size;
|
2002-09-10 07:34:27 +00:00
|
|
|
best_offs = _fileHandle.pos();
|
2002-03-05 22:23:48 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(size - 8, SEEK_CUR);
|
2001-12-01 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
2002-03-05 22:23:48 +00:00
|
|
|
if (best_pri != -1) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(best_offs - 8, SEEK_SET);
|
|
|
|
_fileHandle.read(createResource(type, idx, best_size), best_size);
|
2002-03-05 22:23:48 +00:00
|
|
|
return 1;
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2002-04-26 16:52:00 +00:00
|
|
|
} else if (basetag == MKID('Mac0')) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
2003-09-09 00:13:19 +00:00
|
|
|
total_size = _fileHandle.readUint32BE() - 8;
|
|
|
|
byte *ptr = (byte *)calloc(total_size, 1);
|
|
|
|
_fileHandle.read(ptr, total_size);
|
|
|
|
// dumpResource("sound-", idx, ptr);
|
|
|
|
convertMac0Resource(type, idx, ptr, total_size);
|
|
|
|
free(ptr);
|
2002-05-17 21:58:59 +00:00
|
|
|
return 1;
|
2002-04-26 16:52:00 +00:00
|
|
|
} else if (basetag == MKID('Mac1')) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
2002-10-21 13:23:25 +00:00
|
|
|
total_size = _fileHandle.readUint32BE();
|
2004-08-24 05:04:14 +00:00
|
|
|
_fileHandle.read(createResource(type, idx, total_size), total_size - 8);
|
|
|
|
return 1;
|
|
|
|
} else if (basetag == MKID('TALK')) {
|
|
|
|
debugC(DEBUG_SOUND, "Found base tag TALK in sound %d, size %d", idx, total_size);
|
|
|
|
debugC(DEBUG_SOUND, "It was at position %d", _fileHandle.pos());
|
|
|
|
|
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
|
|
|
total_size = _fileHandle.readUint32BE();
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(createResource(type, idx, total_size), total_size - 8);
|
2002-07-02 21:28:57 +00:00
|
|
|
return 1;
|
|
|
|
} else if (basetag == MKID('DIGI')) {
|
|
|
|
// Use in Putt-Putt Demo
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "Found base tag DIGI in sound %d, size %d", idx, total_size);
|
|
|
|
debugC(DEBUG_SOUND, "It was at position %d", _fileHandle.pos());
|
2002-07-02 21:28:57 +00:00
|
|
|
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
2002-10-21 13:23:25 +00:00
|
|
|
total_size = _fileHandle.readUint32BE();
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(createResource(type, idx, total_size), total_size - 8);
|
2002-08-20 09:58:35 +00:00
|
|
|
return 1;
|
2003-07-06 12:04:37 +00:00
|
|
|
} else if (basetag == MKID('FMUS')) {
|
|
|
|
// Used in 3DO version of puttputt joins the parade and probably others
|
2003-11-07 02:11:41 +00:00
|
|
|
// Specifies a separate file to be used for music from what I gather.
|
2003-07-06 12:04:37 +00:00
|
|
|
int tmpsize;
|
2003-07-06 15:02:13 +00:00
|
|
|
File dmuFile;
|
2003-07-06 12:04:37 +00:00
|
|
|
char buffer[128];
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "Found base tag FMUS in sound %d, size %d", idx, total_size);
|
|
|
|
debugC(DEBUG_SOUND, "It was at position %d", _fileHandle.pos());
|
2003-07-06 12:04:37 +00:00
|
|
|
|
|
|
|
_fileHandle.seek(4, SEEK_CUR);
|
|
|
|
// HSHD size
|
|
|
|
tmpsize = _fileHandle.readUint32BE();
|
|
|
|
// skip to size part of the SDAT block
|
|
|
|
_fileHandle.seek(tmpsize - 4, SEEK_CUR);
|
|
|
|
// SDAT size
|
|
|
|
tmpsize = _fileHandle.readUint32BE();
|
|
|
|
|
|
|
|
// SDAT contains name of file we want
|
2004-03-29 11:02:45 +00:00
|
|
|
_fileHandle.read(buffer, tmpsize - 8);
|
|
|
|
// files seem to be 11 chars (8.3) unused space is replaced by spaces
|
|
|
|
*(strstr(buffer, " ")) = '\0';
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "FMUS file %s", buffer);
|
2004-06-27 21:52:25 +00:00
|
|
|
if (dmuFile.open(buffer) == false) {
|
2003-07-06 15:02:13 +00:00
|
|
|
warning("Can't open music file %s*", buffer);
|
|
|
|
res.roomoffs[type][idx] = 0xFFFFFFFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dmuFile.seek(4, SEEK_SET);
|
|
|
|
total_size = dmuFile.readUint32BE();
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "dmu file size %d", total_size);
|
2003-07-06 15:02:13 +00:00
|
|
|
dmuFile.seek(-8, SEEK_CUR);
|
|
|
|
dmuFile.read(createResource(type, idx, total_size), total_size);
|
|
|
|
dmuFile.close();
|
|
|
|
return 1;
|
2002-08-20 09:58:35 +00:00
|
|
|
} else if (basetag == MKID('Crea')) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
2002-10-21 13:23:25 +00:00
|
|
|
total_size = _fileHandle.readUint32BE();
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.read(createResource(type, idx, total_size), total_size - 8);
|
2002-05-17 21:58:59 +00:00
|
|
|
return 1;
|
2003-09-30 17:53:42 +00:00
|
|
|
} else if (FROM_LE_32(basetag) == max_total_size) {
|
|
|
|
_fileHandle.seek(-12, SEEK_CUR);
|
|
|
|
total_size = _fileHandle.readUint32BE();
|
|
|
|
_fileHandle.seek(-8, SEEK_CUR);
|
|
|
|
_fileHandle.read(createResource(type, idx, total_size), total_size);
|
|
|
|
return 1;
|
2002-03-16 05:33:25 +00:00
|
|
|
} else {
|
2003-07-13 16:22:17 +00:00
|
|
|
warning("Unrecognized base tag 0x%08x in sound %d", basetag, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-03-08 08:42:11 +00:00
|
|
|
res.roomoffs[type][idx] = 0xFFFFFFFF;
|
2001-10-09 14:30:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-08-05 05:21:57 +00:00
|
|
|
// Adlib MIDI-SYSEX to set MIDI instruments for small header games.
|
|
|
|
static byte ADLIB_INSTR_MIDI_HACK[95] = {
|
|
|
|
0x00, 0xf0, 0x14, 0x7d, 0x00, // sysex 00: part on/off
|
|
|
|
0x00, 0x00, 0x03, // part/channel (offset 5)
|
|
|
|
0x00, 0x00, 0x07, 0x0f, 0x00, 0x00, 0x08, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xf7,
|
2003-08-17 16:32:36 +00:00
|
|
|
0x00, 0xf0, 0x41, 0x7d, 0x10, // sysex 16: set instrument
|
2003-08-12 16:09:41 +00:00
|
|
|
0x00, 0x01, // part/channel (offset 28)
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0xf7,
|
2003-08-05 05:21:57 +00:00
|
|
|
0x00, 0xb0, 0x07, 0x64 // Controller 7 = 100 (offset 92)
|
|
|
|
};
|
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
static const byte map_param[7] = {
|
|
|
|
0, 2, 3, 4, 8, 9, 0,
|
|
|
|
};
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
static const byte freq2note[128] = {
|
|
|
|
/*128*/ 6, 6, 6, 6,
|
|
|
|
/*132*/ 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
/*139*/ 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
|
|
/*148*/ 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
|
|
/*157*/ 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
|
|
|
/*166*/ 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
|
|
|
|
/*176*/ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
|
|
|
/*186*/ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
|
|
|
/*197*/ 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
|
|
|
/*209*/ 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
/*222*/ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
|
|
/*235*/ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
|
|
|
/*249*/ 18, 18, 18, 18, 18, 18, 18
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16 num_steps_table[] = {
|
|
|
|
1, 2, 4, 5,
|
|
|
|
6, 7, 8, 9,
|
|
|
|
10, 12, 14, 16,
|
|
|
|
18, 21, 24, 30,
|
|
|
|
36, 50, 64, 82,
|
|
|
|
100, 136, 160, 192,
|
|
|
|
240, 276, 340, 460,
|
|
|
|
600, 860, 1200, 1600
|
|
|
|
};
|
2004-04-17 16:29:03 +00:00
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::convert_extraflags(byte * ptr, byte * src_ptr) {
|
2003-08-12 16:09:41 +00:00
|
|
|
int flags = src_ptr[0];
|
|
|
|
|
|
|
|
int t1, t2, t3, t4, time;
|
|
|
|
int v1, v2, v3;
|
|
|
|
|
|
|
|
if (!(flags & 0x80))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
t1 = (src_ptr[1] & 0xf0) >> 3;
|
|
|
|
t2 = (src_ptr[2] & 0xf0) >> 3;
|
|
|
|
t3 = (src_ptr[3] & 0xf0) >> 3 | (flags & 0x40 ? 0x80 : 0);
|
|
|
|
t4 = (src_ptr[3] & 0x0f) << 1;
|
|
|
|
v1 = (src_ptr[1] & 0x0f);
|
|
|
|
v2 = (src_ptr[2] & 0x0f);
|
|
|
|
v3 = 31;
|
|
|
|
if ((flags & 0x7) == 0) {
|
|
|
|
v1 = v1 + 31 + 8;
|
|
|
|
v2 = v2 + 31 + 8;
|
|
|
|
} else {
|
|
|
|
v1 = v1 * 2 + 31;
|
|
|
|
v2 = v2 * 2 + 31;
|
|
|
|
}
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* flags a */
|
|
|
|
if ((flags & 0x7) == 6)
|
|
|
|
ptr[0] = 0;
|
|
|
|
else {
|
|
|
|
ptr[0] = (flags >> 4) & 0xb;
|
|
|
|
ptr[1] = map_param[flags & 0x7];
|
2003-08-05 05:21:57 +00:00
|
|
|
}
|
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* extra a */
|
|
|
|
ptr[2] = 0;
|
|
|
|
ptr[3] = 0;
|
|
|
|
ptr[4] = t1 >> 4;
|
|
|
|
ptr[5] = t1 & 0xf;
|
|
|
|
ptr[6] = v1 >> 4;
|
|
|
|
ptr[7] = v1 & 0xf;
|
|
|
|
ptr[8] = t2 >> 4;
|
|
|
|
ptr[9] = t2 & 0xf;
|
|
|
|
ptr[10] = v2 >> 4;
|
|
|
|
ptr[11] = v2 & 0xf;
|
|
|
|
ptr[12] = t3 >> 4;
|
|
|
|
ptr[13] = t3 & 0xf;
|
|
|
|
ptr[14] = t4 >> 4;
|
|
|
|
ptr[15] = t4 & 0xf;
|
|
|
|
ptr[16] = v3 >> 4;
|
|
|
|
ptr[17] = v3 & 0xf;
|
|
|
|
|
|
|
|
time = num_steps_table[t1] + num_steps_table[t2]
|
|
|
|
+ num_steps_table[t3 & 0x7f] + num_steps_table[t4];
|
|
|
|
if (flags & 0x20) {
|
|
|
|
int playtime = ((src_ptr[4] >> 4) & 0xf) * 118 +
|
|
|
|
(src_ptr[4] & 0xf) * 8;
|
|
|
|
if (playtime > time)
|
|
|
|
time = playtime;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
time = ((src_ptr[4] >> 4) & 0xf) * 118 +
|
|
|
|
(src_ptr[4] & 0xf) * 8;
|
|
|
|
*/
|
|
|
|
return time;
|
|
|
|
}
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-09-09 01:21:42 +00:00
|
|
|
#define kMIDIHeaderSize 46
|
|
|
|
static inline byte *writeMIDIHeader(byte *ptr, const char *type, int ppqn, int total_size) {
|
|
|
|
uint32 dw = TO_BE_32(total_size);
|
|
|
|
|
|
|
|
memcpy(ptr, type, 4); ptr += 4;
|
|
|
|
memcpy(ptr, &dw, 4); ptr += 4;
|
|
|
|
memcpy(ptr, "MDhd", 4); ptr += 4;
|
|
|
|
ptr[0] = 0; ptr[1] = 0; ptr[2] = 0; ptr[3] = 8;
|
|
|
|
ptr += 4;
|
|
|
|
memset(ptr, 0, 8), ptr += 8;
|
|
|
|
memcpy(ptr, "MThd", 4); ptr += 4;
|
|
|
|
ptr[0] = 0; ptr[1] = 0; ptr[2] = 0; ptr[3] = 6;
|
|
|
|
ptr += 4;
|
|
|
|
ptr[0] = 0; ptr[1] = 0; ptr[2] = 0; ptr[3] = 1; // MIDI format 0 with 1 track
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
*ptr++ = ppqn >> 8;
|
|
|
|
*ptr++ = ppqn & 0xFF;
|
|
|
|
|
|
|
|
memcpy(ptr, "MTrk", 4); ptr += 4;
|
|
|
|
memcpy(ptr, &dw, 4); ptr += 4;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *writeVLQ(byte *ptr, int value) {
|
|
|
|
if (value > 0x7f) {
|
|
|
|
if (value > 0x3fff) {
|
|
|
|
*ptr++ = (value >> 14) | 0x80;
|
|
|
|
value &= 0x3fff;
|
|
|
|
}
|
|
|
|
*ptr++ = (value >> 7) | 0x80;
|
|
|
|
value &= 0x7f;
|
|
|
|
}
|
|
|
|
*ptr++ = value;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2003-10-11 15:18:49 +00:00
|
|
|
static inline byte Mac0ToGMInstrument(uint32 type, int &transpose) {
|
|
|
|
transpose = 0;
|
2003-09-09 01:21:42 +00:00
|
|
|
switch (type) {
|
2003-10-11 15:18:49 +00:00
|
|
|
case MKID('MARI'): return 12;
|
|
|
|
case MKID('PLUC'): return 45;
|
|
|
|
case MKID('HARM'): return 22;
|
|
|
|
case MKID('PIPE'): return 19;
|
|
|
|
case MKID('TROM'): transpose = -12; return 57;
|
|
|
|
case MKID('STRI'): return 48;
|
|
|
|
case MKID('HORN'): return 60;
|
|
|
|
case MKID('VIBE'): return 11;
|
|
|
|
case MKID('SHAK'): return 77;
|
|
|
|
case MKID('PANP'): return 75;
|
|
|
|
case MKID('WHIS'): return 76;
|
|
|
|
case MKID('ORGA'): return 17;
|
|
|
|
case MKID('BONG'): return 115;
|
|
|
|
case MKID('BASS'): transpose = -24; return 35;
|
2003-09-09 01:21:42 +00:00
|
|
|
default:
|
2003-10-17 16:28:29 +00:00
|
|
|
error("Unknown Mac0 instrument %s found", tag2str(type));
|
2003-09-09 01:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::convertMac0Resource(int type, int idx, byte *src_ptr, int size) {
|
2003-09-09 00:13:19 +00:00
|
|
|
/*
|
|
|
|
From Markus Magnuson (superqult) we got this information:
|
|
|
|
Mac0
|
|
|
|
---
|
|
|
|
4 bytes - 'SOUN'
|
|
|
|
BE 4 bytes - block length
|
|
|
|
|
|
|
|
4 bytes - 'Mac0'
|
|
|
|
BE 4 bytes - (blockLength - 27)
|
|
|
|
28 bytes - ???
|
|
|
|
|
|
|
|
do this three times (once for each channel):
|
|
|
|
4 bytes - 'Chan'
|
|
|
|
BE 4 bytes - channel length
|
|
|
|
4 bytes - instrument name (e.g. 'MARI')
|
|
|
|
|
|
|
|
do this for ((chanLength-24)/4) times:
|
|
|
|
2 bytes - note duration
|
|
|
|
1 byte - note value
|
|
|
|
1 byte - note velocity
|
|
|
|
|
|
|
|
4 bytes - ???
|
|
|
|
4 bytes - 'Loop'/'Done'
|
|
|
|
4 bytes - ???
|
|
|
|
|
|
|
|
1 byte - 0x09
|
|
|
|
---
|
|
|
|
|
|
|
|
Instruments (General Midi):
|
2003-10-11 15:18:49 +00:00
|
|
|
"MARI" - Marimba (12)
|
|
|
|
"PLUC" - Pizzicato Strings (45)
|
|
|
|
"HARM" - Harmonica (22)
|
|
|
|
"PIPE" - Church Organ? (19) or Flute? (73) or Bag Pipe (109)
|
|
|
|
"TROM" - Trombone (57)
|
|
|
|
"STRI" - String Ensemble (48 or 49)
|
|
|
|
"HORN" - French Horn? (60) or English Horn? (69)
|
|
|
|
"VIBE" - Vibraphone (11)
|
|
|
|
"SHAK" - Shakuhachi? (77)
|
|
|
|
"PANP" - Pan Flute (75)
|
|
|
|
"WHIS" - Whistle (78) / Bottle (76)
|
|
|
|
"ORGA" - Drawbar Organ (16; but could also be 17-20)
|
|
|
|
"BONG" - Woodblock? (115)
|
|
|
|
"BASS" - Bass (32-39)
|
2003-09-09 00:13:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
Now the task could be to convert this into MIDI, to be fed into iMuse.
|
|
|
|
Or we do something similiar to what is done in Player_V3, assuming
|
|
|
|
we can identify SFX in the MI datafiles for each of the instruments
|
|
|
|
listed above.
|
|
|
|
*/
|
|
|
|
|
2003-10-10 21:40:25 +00:00
|
|
|
#if 0
|
2003-09-09 00:13:19 +00:00
|
|
|
byte *ptr = createResource(type, idx, size);
|
|
|
|
memcpy(ptr, src_ptr, size);
|
2003-09-09 01:21:42 +00:00
|
|
|
#else
|
|
|
|
const int ppqn = 480;
|
|
|
|
byte *ptr, *start_ptr;
|
2003-09-09 00:13:19 +00:00
|
|
|
|
2003-09-09 01:21:42 +00:00
|
|
|
int total_size = 0;
|
2003-10-10 21:40:25 +00:00
|
|
|
total_size += kMIDIHeaderSize; // Header
|
|
|
|
total_size += 7; // Tempo META
|
|
|
|
total_size += 3 * 3; // Three program change mesages
|
|
|
|
total_size += 22; // Possible jump SysEx
|
|
|
|
total_size += 5; // EOT META
|
2003-09-09 00:13:19 +00:00
|
|
|
|
2003-09-09 01:21:42 +00:00
|
|
|
int i, len;
|
|
|
|
byte track_instr[3];
|
|
|
|
byte *track_data[3];
|
|
|
|
int track_len[3];
|
2003-10-11 15:18:49 +00:00
|
|
|
int track_transpose[3];
|
2003-09-09 01:21:42 +00:00
|
|
|
bool looped = false;
|
|
|
|
|
2003-10-11 15:18:49 +00:00
|
|
|
src_ptr += 8;
|
2003-09-09 01:21:42 +00:00
|
|
|
// TODO: Decipher the unknown bytes in the header. For now, skip 'em
|
2003-10-11 15:18:49 +00:00
|
|
|
src_ptr += 28;
|
2003-09-09 01:21:42 +00:00
|
|
|
|
|
|
|
// Parse the three channels
|
|
|
|
for (i = 0; i < 3; i++) {
|
2003-10-10 21:40:25 +00:00
|
|
|
assert(*((uint32*)src_ptr) == MKID('Chan'));
|
2003-09-09 01:21:42 +00:00
|
|
|
len = READ_BE_UINT32(src_ptr + 4);
|
2003-10-10 21:40:25 +00:00
|
|
|
track_len[i] = len - 24;
|
2003-10-11 15:18:49 +00:00
|
|
|
track_instr[i] = Mac0ToGMInstrument(*(uint32*)(src_ptr + 8), track_transpose[i]);
|
2003-10-10 21:40:25 +00:00
|
|
|
track_data[i] = src_ptr + 12;
|
2003-09-09 01:21:42 +00:00
|
|
|
src_ptr += len;
|
2003-10-10 21:40:25 +00:00
|
|
|
looped = (*((uint32*)(src_ptr - 8)) == MKID('Loop'));
|
2003-09-09 01:21:42 +00:00
|
|
|
|
2003-10-10 21:40:25 +00:00
|
|
|
// For each note event, we need up to 6 bytes for the
|
|
|
|
// Note On (3 VLQ, 3 event), and 6 bytes for the Note
|
|
|
|
// Off (3 VLQ, 3 event). So 12 bytes total.
|
|
|
|
total_size += 12 * track_len[i];
|
2003-09-09 01:21:42 +00:00
|
|
|
}
|
|
|
|
assert(*src_ptr == 0x09);
|
|
|
|
|
|
|
|
// Create sound resource
|
|
|
|
start_ptr = createResource(type, idx, total_size);
|
|
|
|
|
|
|
|
// Insert MIDI header
|
|
|
|
ptr = writeMIDIHeader(start_ptr, "GMD ", ppqn, total_size);
|
|
|
|
|
|
|
|
// Write a tempo change Meta event
|
|
|
|
// 473 / 4 Hz, convert to micro seconds.
|
2003-10-10 21:40:25 +00:00
|
|
|
uint32 dw = 1000000 * 437 / 4 / ppqn; // 1000000 * ppqn * 4 / 473;
|
2003-09-09 01:21:42 +00:00
|
|
|
memcpy(ptr, "\x00\xFF\x51\x03", 4); ptr += 4;
|
|
|
|
*ptr++ = (byte)((dw >> 16) & 0xFF);
|
|
|
|
*ptr++ = (byte)((dw >> 8) & 0xFF);
|
|
|
|
*ptr++ = (byte)(dw & 0xFF);
|
|
|
|
|
2003-10-10 21:40:25 +00:00
|
|
|
// Insert program change messages
|
|
|
|
*ptr++ = 0; // VLQ
|
|
|
|
*ptr++ = 0xC0;
|
|
|
|
*ptr++ = track_instr[0];
|
|
|
|
*ptr++ = 0; // VLQ
|
|
|
|
*ptr++ = 0xC1;
|
|
|
|
*ptr++ = track_instr[1];
|
|
|
|
*ptr++ = 0; // VLQ
|
|
|
|
*ptr++ = 0xC2;
|
|
|
|
*ptr++ = track_instr[2];
|
|
|
|
|
|
|
|
// And now, the actual composition. Please turn all cell phones
|
|
|
|
// and pagers off during the performance. Thank you.
|
|
|
|
uint16 nextTime[3] = { 1, 1, 1 };
|
|
|
|
int stage[3] = { 0, 0, 0 };
|
|
|
|
|
|
|
|
while (track_len[0] | track_len[1] | track_len[2]) {
|
|
|
|
int best = -1;
|
|
|
|
uint16 bestTime = 0xFFFF;
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
if (track_len[i] && nextTime[i] < bestTime) {
|
|
|
|
bestTime = nextTime[i];
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (best != -1);
|
|
|
|
|
|
|
|
if (!stage[best]) {
|
|
|
|
// We are STARTING this event.
|
|
|
|
if (track_data[best][2] > 1) {
|
|
|
|
// Note On
|
|
|
|
ptr = writeVLQ(ptr, nextTime[best]);
|
|
|
|
*ptr++ = 0x90 | best;
|
2003-10-11 15:18:49 +00:00
|
|
|
*ptr++ = track_data[best][2] + track_transpose[best];
|
2003-10-10 21:40:25 +00:00
|
|
|
*ptr++ = track_data[best][3] * 127 / 100; // Scale velocity
|
|
|
|
for (i = 0; i < 3; ++i)
|
|
|
|
nextTime[i] -= bestTime;
|
|
|
|
}
|
|
|
|
nextTime[best] += READ_BE_UINT16 (track_data[best]);
|
|
|
|
stage[best] = 1;
|
|
|
|
} else {
|
|
|
|
// We are ENDING this event.
|
|
|
|
if (track_data[best][2] > 1) {
|
|
|
|
// There was a Note On, so do a Note Off
|
|
|
|
ptr = writeVLQ(ptr, nextTime[best]);
|
|
|
|
*ptr++ = 0x80 | best;
|
2003-10-11 15:18:49 +00:00
|
|
|
*ptr++ = track_data[best][2] + track_transpose[best];
|
2003-10-10 21:40:25 +00:00
|
|
|
*ptr++ = track_data[best][3] * 127 / 100; // Scale velocity
|
|
|
|
for (i = 0; i < 3; ++i)
|
|
|
|
nextTime[i] -= bestTime;
|
|
|
|
}
|
|
|
|
track_data[best] += 4;
|
|
|
|
track_len[best] -= 4;
|
|
|
|
stage[best] = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-09-09 01:21:42 +00:00
|
|
|
|
2003-10-10 21:40:25 +00:00
|
|
|
// Is this a looped song? If so, effect a loop by
|
|
|
|
// using the S&M maybe_jump SysEx command.
|
|
|
|
// FIXME: Jamieson630: The jump seems to be happening
|
|
|
|
// too quickly! There should maybe be a pause after
|
|
|
|
// the last Note Off? But I couldn't find one in the
|
|
|
|
// MI1 Lookout music, where I was hearing problems.
|
|
|
|
if (looped) {
|
|
|
|
memcpy(ptr, "\x00\xf0\x13\x7d\x30\00", 6); ptr += 6; // maybe_jump
|
|
|
|
memcpy(ptr, "\x00\x00", 2); ptr += 2; // cmd -> 0 means always jump
|
|
|
|
memcpy(ptr, "\x00\x00\x00\x00", 4); ptr += 4; // track -> 0 (only track)
|
|
|
|
memcpy(ptr, "\x00\x00\x00\x01", 4); ptr += 4; // beat -> 1 (first beat)
|
|
|
|
memcpy(ptr, "\x00\x00\x00\x01", 4); ptr += 4; // tick -> 1
|
|
|
|
memcpy(ptr, "\x00\xf7", 2); ptr += 2; // SysEx end marker
|
|
|
|
}
|
2003-09-09 00:13:19 +00:00
|
|
|
|
2003-10-10 21:40:25 +00:00
|
|
|
// Insert end of song META
|
2003-09-09 01:21:42 +00:00
|
|
|
memcpy(ptr, "\x00\xff\x2f\x00\x00", 5); ptr += 5;
|
|
|
|
|
|
|
|
assert(ptr <= start_ptr + total_size);
|
|
|
|
|
|
|
|
// Rewrite MIDI header, this time with true size
|
|
|
|
total_size = ptr - start_ptr;
|
|
|
|
ptr = writeMIDIHeader(start_ptr, "GMD ", ppqn, total_size);
|
|
|
|
#endif
|
2003-09-09 00:13:19 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::convertADResource(int type, int idx, byte *src_ptr, int size) {
|
2003-09-09 00:13:19 +00:00
|
|
|
|
2003-09-08 11:29:57 +00:00
|
|
|
// We will ignore the PPQN in the original resource, because
|
|
|
|
// it's invalid anyway. We use a constant PPQN of 480.
|
2003-09-09 01:21:42 +00:00
|
|
|
const int ppqn = 480;
|
2003-09-09 00:13:19 +00:00
|
|
|
uint32 dw;
|
2003-08-12 16:09:41 +00:00
|
|
|
int i, ch;
|
2003-09-09 00:20:55 +00:00
|
|
|
byte *ptr;
|
2003-09-09 01:21:42 +00:00
|
|
|
int total_size = kMIDIHeaderSize + 7 + 8 * sizeof(ADLIB_INSTR_MIDI_HACK) + size;
|
2003-08-05 05:21:57 +00:00
|
|
|
total_size += 24; // Up to 24 additional bytes are needed for the jump sysex
|
2003-09-09 01:21:42 +00:00
|
|
|
|
2003-08-05 05:21:57 +00:00
|
|
|
ptr = createResource(type, idx, total_size);
|
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
src_ptr += 2;
|
|
|
|
size -= 2;
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
// 0x80 marks a music resource. Otherwise it's a SFX
|
2003-08-12 16:09:41 +00:00
|
|
|
if (*src_ptr == 0x80) {
|
2003-09-09 00:20:55 +00:00
|
|
|
byte ticks, play_once;
|
|
|
|
byte num_instr;
|
|
|
|
byte *channel, *instr, *track;
|
|
|
|
|
2003-09-09 00:13:19 +00:00
|
|
|
ptr = writeMIDIHeader(ptr, "ADL ", ppqn, total_size);
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// The "speed" of the song
|
|
|
|
ticks = *(src_ptr + 1);
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// Flag that tells us whether we should loop the song (0) or play it only once (1)
|
|
|
|
play_once = *(src_ptr + 2);
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// Number of instruments used
|
|
|
|
num_instr = *(src_ptr + 8); // Normally 8
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// copy the pointer to instrument data
|
|
|
|
channel = src_ptr + 9;
|
|
|
|
instr = src_ptr + 0x11;
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// skip over the rest of the header and copy the MIDI data into a buffer
|
|
|
|
src_ptr += 0x11 + 8 * 16;
|
|
|
|
size -= 0x11 + 8 * 16;
|
|
|
|
|
|
|
|
CHECK_HEAP
|
|
|
|
|
|
|
|
track = src_ptr;
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-16 14:30:23 +00:00
|
|
|
// Convert the ticks into a MIDI tempo.
|
2003-08-17 16:32:36 +00:00
|
|
|
// Unfortunate LOOM and INDY3 have different interpretation
|
|
|
|
// of the ticks value.
|
|
|
|
if (_gameId == GID_INDY3) {
|
2003-09-08 11:29:57 +00:00
|
|
|
// Note: since we fix ppqn at 480, ppqn/473 is almost 1
|
2003-09-08 16:48:34 +00:00
|
|
|
dw = 500000 * 256 / 473 * ppqn / ticks;
|
2003-08-20 15:19:23 +00:00
|
|
|
} else if (_gameId == GID_LOOM) {
|
2003-09-08 11:29:57 +00:00
|
|
|
dw = 500000 * ppqn / 4 / ticks;
|
2003-08-20 15:19:23 +00:00
|
|
|
} else {
|
2003-09-08 11:29:57 +00:00
|
|
|
dw = 500000 * 256 / ticks;
|
2003-08-17 16:32:36 +00:00
|
|
|
}
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, " ticks = %d, speed = %ld", ticks, dw);
|
2003-08-12 16:09:41 +00:00
|
|
|
|
2003-08-16 17:05:04 +00:00
|
|
|
// Write a tempo change Meta event
|
2003-08-12 16:09:41 +00:00
|
|
|
memcpy(ptr, "\x00\xFF\x51\x03", 4); ptr += 4;
|
|
|
|
*ptr++ = (byte)((dw >> 16) & 0xFF);
|
|
|
|
*ptr++ = (byte)((dw >> 8) & 0xFF);
|
|
|
|
*ptr++ = (byte)(dw & 0xFF);
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// Copy our hardcoded instrument table into it
|
|
|
|
// Then, convert the instrument table as given in this song resource
|
|
|
|
// And write it *over* the hardcoded table.
|
|
|
|
// Note: we deliberately.
|
2003-08-05 05:21:57 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* now fill in the instruments */
|
|
|
|
for (i = 0; i < num_instr; i++) {
|
|
|
|
ch = channel[i] - 1;
|
2003-08-17 16:32:36 +00:00
|
|
|
if (ch < 0 || ch > 15)
|
|
|
|
continue;
|
2003-09-14 15:03:12 +00:00
|
|
|
|
|
|
|
if (instr[i*16 + 13])
|
|
|
|
warning("Sound %d instrument %d uses percussion", idx, i);
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "Sound %d: instrument %d on channel %d.", idx, i, ch);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
|
|
|
memcpy(ptr, ADLIB_INSTR_MIDI_HACK, sizeof(ADLIB_INSTR_MIDI_HACK));
|
2003-08-12 16:09:41 +00:00
|
|
|
|
|
|
|
ptr[5] += ch;
|
|
|
|
ptr[28] += ch;
|
|
|
|
ptr[92] += ch;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* flags_1 */
|
|
|
|
ptr[30 + 0] = (instr[i * 16 + 3] >> 4) & 0xf;
|
|
|
|
ptr[30 + 1] = instr[i * 16 + 3] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* oplvl_1 */
|
|
|
|
ptr[30 + 2] = (instr[i * 16 + 4] >> 4) & 0xf;
|
|
|
|
ptr[30 + 3] = instr[i * 16 + 4] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* atdec_1 */
|
|
|
|
ptr[30 + 4] = ((~instr[i * 16 + 5]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 5] = (~instr[i * 16 + 5]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* sustrel_1 */
|
|
|
|
ptr[30 + 6] = ((~instr[i * 16 + 6]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 7] = (~instr[i * 16 + 6]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* waveform_1 */
|
|
|
|
ptr[30 + 8] = (instr[i * 16 + 7] >> 4) & 0xf;
|
|
|
|
ptr[30 + 9] = instr[i * 16 + 7] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* flags_2 */
|
|
|
|
ptr[30 + 10] = (instr[i * 16 + 8] >> 4) & 0xf;
|
|
|
|
ptr[30 + 11] = instr[i * 16 + 8] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* oplvl_2 */
|
|
|
|
ptr[30 + 12] = (instr[i * 16 + 9] >> 4) & 0xf;
|
|
|
|
ptr[30 + 13] = instr[i * 16 + 9] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* atdec_2 */
|
|
|
|
ptr[30 + 14] = ((~instr[i * 16 + 10]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 15] = (~instr[i * 16 + 10]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* sustrel_2 */
|
|
|
|
ptr[30 + 16] = ((~instr[i * 16 + 11]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 17] = (~instr[i * 16 + 11]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* waveform_2 */
|
|
|
|
ptr[30 + 18] = (instr[i * 16 + 12] >> 4) & 0xf;
|
|
|
|
ptr[30 + 19] = instr[i * 16 + 12] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
/* feedback */
|
|
|
|
ptr[30 + 20] = (instr[i * 16 + 2] >> 4) & 0xf;
|
|
|
|
ptr[30 + 21] = instr[i * 16 + 2] & 0xf;
|
|
|
|
ptr += sizeof(ADLIB_INSTR_MIDI_HACK);
|
|
|
|
}
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-17 16:32:36 +00:00
|
|
|
// There is a constant delay of ppqn/3 before the music starts.
|
|
|
|
if (ppqn / 3 >= 128)
|
2003-09-14 15:03:12 +00:00
|
|
|
*ptr++ = (ppqn / 3 >> 7) | 0x80;
|
2003-08-17 16:32:36 +00:00
|
|
|
*ptr++ = ppqn / 3 & 0x7f;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// Now copy the actual music data
|
|
|
|
memcpy(ptr, track, size);
|
|
|
|
ptr += size;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
if (!play_once) {
|
|
|
|
// The song is meant to be looped. We achieve this by inserting just
|
|
|
|
// before the song end a jump to the song start. More precisely we abuse
|
|
|
|
// a S&M sysex, "maybe_jump" to achieve this effect. We could also
|
|
|
|
// use a set_loop sysex, but it's a bit longer, a little more complicated,
|
|
|
|
// and has no advantage either.
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
// First, find the track end
|
|
|
|
byte *end = ptr;
|
|
|
|
ptr -= size;
|
|
|
|
for (; ptr < end; ptr++) {
|
|
|
|
if (*ptr == 0xff && *(ptr + 1) == 0x2f)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(ptr < end);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-17 16:32:36 +00:00
|
|
|
// Now insert the jump. The jump offset is measured in ticks.
|
|
|
|
// We have ppqn/3 ticks before the first note.
|
|
|
|
|
|
|
|
const int jump_offset = ppqn / 3;
|
2003-08-12 16:09:41 +00:00
|
|
|
memcpy(ptr, "\xf0\x13\x7d\x30\00", 5); ptr += 5; // maybe_jump
|
|
|
|
memcpy(ptr, "\x00\x00", 2); ptr += 2; // cmd -> 0 means always jump
|
|
|
|
memcpy(ptr, "\x00\x00\x00\x00", 4); ptr += 4; // track -> there is only one track, 0
|
|
|
|
memcpy(ptr, "\x00\x00\x00\x01", 4); ptr += 4; // beat -> for now, 1 (first beat)
|
|
|
|
// Ticks
|
|
|
|
*ptr++ = (byte)((jump_offset >> 12) & 0x0F);
|
|
|
|
*ptr++ = (byte)((jump_offset >> 8) & 0x0F);
|
|
|
|
*ptr++ = (byte)((jump_offset >> 4) & 0x0F);
|
|
|
|
*ptr++ = (byte)(jump_offset & 0x0F);
|
|
|
|
memcpy(ptr, "\x00\xf7", 2); ptr += 2; // sysex end marker
|
|
|
|
}
|
2003-09-09 00:20:55 +00:00
|
|
|
} else {
|
2003-08-12 16:09:41 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* This is a sfx resource. First parse it quickly to find the parallel
|
|
|
|
* tracks.
|
|
|
|
*/
|
|
|
|
ptr = writeMIDIHeader(ptr, "ASFX", ppqn, total_size);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
byte current_instr[3][14];
|
|
|
|
int current_note[3];
|
|
|
|
int track_time[3];
|
2003-09-09 01:21:42 +00:00
|
|
|
byte *track_data[3];
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
int track_ctr = 0;
|
|
|
|
byte chunk_type = 0;
|
|
|
|
int delay, delay2, olddelay;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
// Write a tempo change Meta event
|
|
|
|
// 473 / 4 Hz, convert to micro seconds.
|
|
|
|
dw = 1000000 * ppqn * 4 / 473;
|
|
|
|
memcpy(ptr, "\x00\xFF\x51\x03", 4); ptr += 4;
|
|
|
|
*ptr++ = (byte)((dw >> 16) & 0xFF);
|
|
|
|
*ptr++ = (byte)((dw >> 8) & 0xFF);
|
|
|
|
*ptr++ = (byte)(dw & 0xFF);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-12 16:09:41 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2003-09-09 00:20:55 +00:00
|
|
|
track_time[i] = -1;
|
|
|
|
current_note[i] = -1;
|
2003-08-12 16:09:41 +00:00
|
|
|
}
|
2003-09-09 00:20:55 +00:00
|
|
|
while (size > 0) {
|
|
|
|
assert(track_ctr < 3);
|
2003-09-09 01:21:42 +00:00
|
|
|
track_data[track_ctr] = src_ptr;
|
2003-09-09 00:20:55 +00:00
|
|
|
track_time[track_ctr] = 0;
|
|
|
|
track_ctr++;
|
|
|
|
while (size > 0) {
|
|
|
|
chunk_type = *(src_ptr);
|
|
|
|
if (chunk_type == 1) {
|
|
|
|
src_ptr += 15;
|
|
|
|
size -= 15;
|
|
|
|
} else if (chunk_type == 2) {
|
|
|
|
src_ptr += 11;
|
|
|
|
size -= 11;
|
|
|
|
} else if (chunk_type == 0x80) {
|
|
|
|
src_ptr ++;
|
|
|
|
size --;
|
|
|
|
} else {
|
|
|
|
break;
|
2003-08-12 16:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-09 00:20:55 +00:00
|
|
|
if (chunk_type == 0xff)
|
|
|
|
break;
|
|
|
|
src_ptr++;
|
2003-08-05 05:21:57 +00:00
|
|
|
}
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
int curtime = 0;
|
|
|
|
for (;;) {
|
|
|
|
int mintime = -1;
|
|
|
|
ch = -1;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (track_time[i] >= 0 &&
|
|
|
|
(mintime == -1 || mintime > track_time[i])) {
|
|
|
|
mintime = track_time[i];
|
|
|
|
ch = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mintime < 0)
|
|
|
|
break;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 01:21:42 +00:00
|
|
|
src_ptr = track_data[ch];
|
2003-09-09 00:20:55 +00:00
|
|
|
chunk_type = *src_ptr;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
if (current_note[ch] >= 0) {
|
|
|
|
delay = mintime - curtime;
|
|
|
|
curtime = mintime;
|
2003-09-09 01:21:42 +00:00
|
|
|
ptr = writeVLQ(ptr, delay);
|
2003-09-09 00:20:55 +00:00
|
|
|
*ptr++ = 0x80 + ch; // key off channel;
|
|
|
|
*ptr++ = current_note[ch];
|
|
|
|
*ptr++ = 0;
|
|
|
|
current_note[ch] = -1;
|
|
|
|
}
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
switch (chunk_type) {
|
|
|
|
case 1:
|
|
|
|
/* Instrument definition */
|
2003-11-08 21:59:32 +00:00
|
|
|
memcpy(current_instr[ch], src_ptr + 1, 14);
|
2003-09-09 00:20:55 +00:00
|
|
|
src_ptr += 15;
|
|
|
|
break;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
case 2:
|
|
|
|
/* tone/parammodulation */
|
|
|
|
memcpy(ptr, ADLIB_INSTR_MIDI_HACK,
|
|
|
|
sizeof(ADLIB_INSTR_MIDI_HACK));
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
ptr[5] += ch;
|
|
|
|
ptr[28] += ch;
|
|
|
|
ptr[92] += ch;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* flags_1 */
|
|
|
|
ptr[30 + 0] = (current_instr[ch][3] >> 4) & 0xf;
|
|
|
|
ptr[30 + 1] = current_instr[ch][3] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* oplvl_1 */
|
|
|
|
ptr[30 + 2] = (current_instr[ch][4] >> 4) & 0xf;
|
|
|
|
ptr[30 + 3] = current_instr[ch][4] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* atdec_1 */
|
|
|
|
ptr[30 + 4] = ((~current_instr[ch][5]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 5] = (~current_instr[ch][5]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* sustrel_1 */
|
|
|
|
ptr[30 + 6] = ((~current_instr[ch][6]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 7] = (~current_instr[ch][6]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* waveform_1 */
|
|
|
|
ptr[30 + 8] = (current_instr[ch][7] >> 4) & 0xf;
|
|
|
|
ptr[30 + 9] = current_instr[ch][7] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* flags_2 */
|
|
|
|
ptr[30 + 10] = (current_instr[ch][8] >> 4) & 0xf;
|
|
|
|
ptr[30 + 11] = current_instr[ch][8] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* oplvl_2 */
|
|
|
|
ptr[30 + 12] = ((current_instr[ch][9]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 13] = (current_instr[ch][9]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* atdec_2 */
|
|
|
|
ptr[30 + 14] = ((~current_instr[ch][10]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 15] = (~current_instr[ch][10]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* sustrel_2 */
|
|
|
|
ptr[30 + 16] = ((~current_instr[ch][11]) >> 4) & 0xf;
|
|
|
|
ptr[30 + 17] = (~current_instr[ch][11]) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* waveform_2 */
|
|
|
|
ptr[30 + 18] = (current_instr[ch][12] >> 4) & 0xf;
|
|
|
|
ptr[30 + 19] = current_instr[ch][12] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* feedback */
|
|
|
|
ptr[30 + 20] = (current_instr[ch][2] >> 4) & 0xf;
|
|
|
|
ptr[30 + 21] = current_instr[ch][2] & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
delay = mintime - curtime;
|
|
|
|
curtime = mintime;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
{
|
|
|
|
delay = convert_extraflags(ptr + 30 + 22, src_ptr + 1);
|
|
|
|
delay2 = convert_extraflags(ptr + 30 + 40, src_ptr + 6);
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "delays: %d / %d", delay, delay2);
|
2003-09-09 00:20:55 +00:00
|
|
|
if (delay2 >= 0 && delay2 < delay)
|
|
|
|
delay = delay2;
|
|
|
|
if (delay == -1)
|
|
|
|
delay = 0;
|
2003-08-12 16:09:41 +00:00
|
|
|
}
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
/* duration */
|
|
|
|
ptr[30 + 58] = 0; // ((delay * 17 / 63) >> 4) & 0xf;
|
|
|
|
ptr[30 + 59] = 0; // (delay * 17 / 63) & 0xf;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
ptr += sizeof(ADLIB_INSTR_MIDI_HACK);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
olddelay = mintime - curtime;
|
|
|
|
curtime = mintime;
|
2003-09-09 01:21:42 +00:00
|
|
|
ptr = writeVLQ(ptr, olddelay);
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
{
|
|
|
|
int freq = ((current_instr[ch][1] & 3) << 8)
|
|
|
|
| current_instr[ch][0];
|
2003-09-14 15:03:12 +00:00
|
|
|
if (!freq)
|
|
|
|
freq = 0x80;
|
2003-09-09 00:20:55 +00:00
|
|
|
freq <<= ((current_instr[ch][1] >> 2) & 7) + 1;
|
|
|
|
int note = -11;
|
|
|
|
while (freq >= 0x100) {
|
|
|
|
note += 12;
|
|
|
|
freq >>= 1;
|
|
|
|
}
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "Freq: %d (%x) Note: %d", freq, freq, note);
|
2003-09-09 00:20:55 +00:00
|
|
|
if (freq < 0x80)
|
|
|
|
note = 0;
|
|
|
|
else
|
|
|
|
note += freq2note[freq - 0x80];
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_SOUND, "Note: %d", note);
|
2003-09-09 00:20:55 +00:00
|
|
|
if (note <= 0)
|
|
|
|
note = 1;
|
|
|
|
else if (note > 127)
|
|
|
|
note = 127;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
// Insert a note on event
|
|
|
|
*ptr++ = 0x90 + ch; // key on channel
|
|
|
|
*ptr++ = note;
|
|
|
|
*ptr++ = 63;
|
|
|
|
current_note[ch] = note;
|
2003-11-08 21:59:32 +00:00
|
|
|
track_time[ch] = curtime + delay;
|
2003-09-09 00:20:55 +00:00
|
|
|
}
|
|
|
|
src_ptr += 11;
|
|
|
|
break;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
case 0x80:
|
|
|
|
track_time[ch] = -1;
|
|
|
|
src_ptr ++;
|
|
|
|
break;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
default:
|
|
|
|
track_time[ch] = -1;
|
2003-08-12 16:09:41 +00:00
|
|
|
}
|
2003-09-09 01:21:42 +00:00
|
|
|
track_data[ch] = src_ptr;
|
2003-08-12 16:09:41 +00:00
|
|
|
}
|
2003-08-05 05:21:57 +00:00
|
|
|
}
|
2003-08-12 16:09:41 +00:00
|
|
|
|
2003-09-09 00:20:55 +00:00
|
|
|
// Insert end of song sysex
|
2003-08-05 05:21:57 +00:00
|
|
|
memcpy(ptr, "\x00\xff\x2f\x00\x00", 5); ptr += 5;
|
|
|
|
}
|
|
|
|
|
2002-09-21 13:48:03 +00:00
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::readSoundResourceSmallHeader(int type, int idx) {
|
2003-08-05 05:21:57 +00:00
|
|
|
uint32 pos, total_size, size, tag;
|
|
|
|
uint32 ad_size = 0, ad_offs = 0;
|
2003-08-14 06:28:54 +00:00
|
|
|
uint32 ro_size = 0, ro_offs = 0;
|
2003-08-05 05:21:57 +00:00
|
|
|
uint32 wa_size = 0, wa_offs = 0;
|
2002-09-21 13:48:03 +00:00
|
|
|
|
|
|
|
debug(4, "readSoundResourceSmallHeader(%s,%d)", resTypeFromId(type), idx);
|
|
|
|
|
2003-11-18 09:44:58 +00:00
|
|
|
if ((_gameId == GID_LOOM) && (_features & GF_PC) && VAR(VAR_SOUNDCARD) == 4) {
|
2003-08-16 06:01:10 +00:00
|
|
|
// Roland resources in Loom are tagless
|
|
|
|
// So we add an RO tag to allow imuse to detect format
|
2003-08-14 10:28:16 +00:00
|
|
|
byte *ptr, *src_ptr;
|
|
|
|
ro_offs = _fileHandle.pos();
|
|
|
|
ro_size = _fileHandle.readUint16LE();
|
|
|
|
|
|
|
|
src_ptr = (byte *) calloc(ro_size - 4, 1);
|
2003-09-10 03:50:03 +00:00
|
|
|
_fileHandle.seek(ro_offs + 4, SEEK_SET);
|
2003-08-14 10:28:16 +00:00
|
|
|
_fileHandle.read(src_ptr, ro_size -4);
|
|
|
|
|
|
|
|
ptr = createResource(type, idx, ro_size + 2);
|
|
|
|
memcpy(ptr, "RO", 2); ptr += 2;
|
|
|
|
memcpy(ptr, src_ptr, ro_size - 4); ptr += ro_size - 4;
|
|
|
|
return 1;
|
|
|
|
} else if (_features & GF_OLD_BUNDLE) {
|
2003-08-05 05:21:57 +00:00
|
|
|
wa_offs = _fileHandle.pos();
|
|
|
|
wa_size = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.seek(wa_size - 2, SEEK_CUR);
|
2003-08-13 16:21:35 +00:00
|
|
|
|
2003-09-05 07:36:05 +00:00
|
|
|
if (!(_features & GF_ATARI_ST || _features & GF_MACINTOSH)) {
|
2003-08-23 07:27:59 +00:00
|
|
|
ad_offs = _fileHandle.pos();
|
|
|
|
ad_size = _fileHandle.readUint16LE();
|
|
|
|
}
|
2003-08-05 05:21:57 +00:00
|
|
|
_fileHandle.seek(4, SEEK_CUR);
|
|
|
|
total_size = wa_size + ad_size;
|
|
|
|
} else {
|
|
|
|
total_size = size = _fileHandle.readUint32LE();
|
2002-10-21 13:23:25 +00:00
|
|
|
tag = _fileHandle.readUint16LE();
|
2003-11-08 21:59:32 +00:00
|
|
|
debug(4, " tag='%c%c', size=%d", (char) (tag & 0xff),
|
|
|
|
(char) ((tag >> 8) & 0xff), size);
|
|
|
|
|
2003-08-14 08:26:59 +00:00
|
|
|
if (tag == 0x4F52) { // RO
|
2003-08-14 06:28:54 +00:00
|
|
|
ro_offs = _fileHandle.pos();
|
2003-09-10 03:50:03 +00:00
|
|
|
ro_size = size;
|
2003-08-14 06:28:54 +00:00
|
|
|
} else {
|
|
|
|
pos = 6;
|
|
|
|
while (pos < total_size) {
|
|
|
|
size = _fileHandle.readUint32LE();
|
|
|
|
tag = _fileHandle.readUint16LE();
|
2003-11-08 21:59:32 +00:00
|
|
|
debug(4, " tag='%c%c', size=%d", (char) (tag & 0xff),
|
|
|
|
(char) ((tag >> 8) & 0xff), size);
|
2003-08-14 06:28:54 +00:00
|
|
|
pos += size;
|
2003-11-08 21:59:32 +00:00
|
|
|
|
2003-08-14 06:28:54 +00:00
|
|
|
// MI1 and Indy3 uses one or more nested SO resources, which contains AD and WA
|
|
|
|
// resources.
|
|
|
|
if ((tag == 0x4441) && !(ad_offs)) { // AD
|
|
|
|
ad_size = size;
|
|
|
|
ad_offs = _fileHandle.pos();
|
|
|
|
} else if ((tag == 0x4157) && !(wa_offs)) { // WA
|
|
|
|
wa_size = size;
|
|
|
|
wa_offs = _fileHandle.pos();
|
|
|
|
} else { // other AD, WA and nested SO resources
|
|
|
|
if (tag == 0x4F53) { // SO
|
|
|
|
pos -= size;
|
|
|
|
size = 6;
|
|
|
|
pos += 6;
|
|
|
|
}
|
2003-08-05 05:21:57 +00:00
|
|
|
}
|
2003-08-14 06:28:54 +00:00
|
|
|
_fileHandle.seek(size - 6, SEEK_CUR);
|
2002-09-21 13:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-14 08:47:47 +00:00
|
|
|
if ((_midiDriver == MD_ADLIB) && ad_offs != 0) {
|
2003-08-14 08:26:59 +00:00
|
|
|
// AD resources have a header, instrument definitions and one MIDI track.
|
|
|
|
// We build an 'ADL ' resource from that:
|
|
|
|
// 8 bytes resource header
|
|
|
|
// 16 bytes MDhd header
|
|
|
|
// 14 bytes MThd header
|
|
|
|
// 8 bytes MTrk header
|
|
|
|
// 7 bytes MIDI tempo sysex
|
|
|
|
// + some default instruments
|
2003-08-05 05:21:57 +00:00
|
|
|
byte *ptr;
|
|
|
|
if (_features & GF_OLD_BUNDLE) {
|
|
|
|
ptr = (byte *) calloc(ad_size - 4, 1);
|
|
|
|
_fileHandle.seek(ad_offs + 4, SEEK_SET);
|
|
|
|
_fileHandle.read(ptr, ad_size - 4);
|
2003-09-09 00:13:19 +00:00
|
|
|
convertADResource(type, idx, ptr, ad_size - 4);
|
|
|
|
free(ptr);
|
|
|
|
return 1;
|
2003-08-05 05:21:57 +00:00
|
|
|
} else {
|
|
|
|
ptr = (byte *) calloc(ad_size - 6, 1);
|
|
|
|
_fileHandle.seek(ad_offs, SEEK_SET);
|
|
|
|
_fileHandle.read(ptr, ad_size - 6);
|
2003-09-09 00:13:19 +00:00
|
|
|
convertADResource(type, idx, ptr, ad_size - 6);
|
|
|
|
free(ptr);
|
|
|
|
return 1;
|
2003-08-05 05:21:57 +00:00
|
|
|
}
|
2003-08-14 08:47:47 +00:00
|
|
|
} else if (((_midiDriver == MD_PCJR) || (_midiDriver == MD_PCSPK)) && wa_offs != 0) {
|
|
|
|
if (_features & GF_OLD_BUNDLE) {
|
|
|
|
_fileHandle.seek(wa_offs, SEEK_SET);
|
|
|
|
_fileHandle.read(createResource(type, idx, wa_size), wa_size);
|
|
|
|
} else {
|
|
|
|
_fileHandle.seek(wa_offs - 6, SEEK_SET);
|
|
|
|
_fileHandle.read(createResource(type, idx, wa_size + 6), wa_size + 6);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else if (ro_offs != 0) {
|
|
|
|
_fileHandle.seek(ro_offs - 2, SEEK_SET);
|
2003-09-10 03:50:03 +00:00
|
|
|
_fileHandle.read(createResource(type, idx, ro_size - 4), ro_size - 4);
|
2003-08-14 08:47:47 +00:00
|
|
|
return 1;
|
2002-09-21 13:48:03 +00:00
|
|
|
}
|
|
|
|
res.roomoffs[type][idx] = 0xFFFFFFFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::getResourceRoomNr(int type, int idx) {
|
2004-06-21 10:06:52 +00:00
|
|
|
if (type == rtRoom && _heversion < 70)
|
2002-03-08 08:42:11 +00:00
|
|
|
return idx;
|
|
|
|
return res.roomno[type][idx];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::getResourceSize(int type, int idx) {
|
2003-07-21 21:28:47 +00:00
|
|
|
byte *ptr = getResourceAddress(type, idx);
|
|
|
|
MemBlkHeader *hdr = (MemBlkHeader *)(ptr - sizeof(MemBlkHeader));
|
|
|
|
|
|
|
|
return hdr->size;
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
byte *ScummEngine::getResourceAddress(int type, int idx) {
|
2001-10-09 14:30:12 +00:00
|
|
|
byte *ptr;
|
2002-07-07 20:32:26 +00:00
|
|
|
|
2002-09-25 03:09:00 +00:00
|
|
|
CHECK_HEAP
|
|
|
|
if (!validateResource("getResourceAddress", type, idx))
|
2002-09-25 03:04:28 +00:00
|
|
|
return NULL;
|
|
|
|
|
2002-04-26 14:13:39 +00:00
|
|
|
if (!res.address[type]) {
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), res.address[type] == NULL", resTypeFromId(type), idx);
|
2002-03-16 18:03:28 +00:00
|
|
|
return NULL;
|
2002-04-26 14:13:39 +00:00
|
|
|
}
|
2002-03-16 18:03:28 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
if (res.mode[type] && !res.address[type][idx]) {
|
|
|
|
ensureResourceLoaded(type, idx);
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-04-26 14:13:39 +00:00
|
|
|
if (!(ptr = (byte *)res.address[type][idx])) {
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
return NULL;
|
2002-04-26 14:13:39 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
setResourceCounter(type, idx, 1);
|
2001-11-14 18:40:39 +00:00
|
|
|
|
2004-04-04 23:29:10 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", resTypeFromId(type), idx, ptr + sizeof(MemBlkHeader));
|
2001-12-11 13:34:15 +00:00
|
|
|
return ptr + sizeof(MemBlkHeader);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
byte *ScummEngine::getStringAddress(int i) {
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *b = getResourceAddress(rtString, i);
|
2001-10-24 20:12:52 +00:00
|
|
|
if (!b)
|
2003-01-26 06:17:25 +00:00
|
|
|
return NULL;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2004-08-25 02:12:03 +00:00
|
|
|
if (_heversion >= 72)
|
|
|
|
return (b + 0x14); // ArrayHeader->data
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
if (_features & GF_NEW_OPCODES)
|
2002-04-11 17:19:16 +00:00
|
|
|
return ((ArrayHeader *)b)->data;
|
2001-10-24 20:12:52 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
byte *ScummEngine::getStringAddressVar(int i) {
|
2002-04-21 19:38:00 +00:00
|
|
|
byte *addr;
|
|
|
|
|
2003-05-08 15:48:50 +00:00
|
|
|
addr = getResourceAddress(rtString, _scummVars[i]);
|
2002-07-07 20:32:26 +00:00
|
|
|
if (addr == NULL)
|
2003-05-19 08:42:10 +00:00
|
|
|
// as this is used for string mapping in the gui
|
|
|
|
// it must be allowed to return NULL
|
|
|
|
// error("NULL string var %d slot %d", i, _scummVars[i]);
|
|
|
|
return NULL;
|
2002-04-21 19:38:00 +00:00
|
|
|
|
2004-08-25 02:12:03 +00:00
|
|
|
if (_heversion >= 72)
|
|
|
|
return (addr + 0x14); // ArrayHeader->data
|
|
|
|
|
2002-04-21 19:38:00 +00:00
|
|
|
if (_features & GF_NEW_OPCODES)
|
2002-07-07 20:32:26 +00:00
|
|
|
return ((ArrayHeader *)addr)->data;
|
2002-04-21 19:38:00 +00:00
|
|
|
|
2002-07-07 20:32:26 +00:00
|
|
|
return (addr);
|
2002-04-21 19:38:00 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::setResourceCounter(int type, int idx, byte flag) {
|
2002-03-08 08:42:11 +00:00
|
|
|
res.flags[type][idx] &= ~RF_USAGE;
|
|
|
|
res.flags[type][idx] |= flag;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
/* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */
|
|
|
|
#define SAFETY_AREA 2
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
byte *ScummEngine::createResource(int type, int idx, uint32 size) {
|
2001-10-09 14:30:12 +00:00
|
|
|
byte *ptr;
|
|
|
|
|
2002-09-25 03:09:00 +00:00
|
|
|
CHECK_HEAP
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "createResource(%s,%d,%d)", resTypeFromId(type), idx, size);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-09-25 03:04:28 +00:00
|
|
|
if (!validateResource("allocating", type, idx))
|
|
|
|
return NULL;
|
2002-03-08 08:42:11 +00:00
|
|
|
nukeResource(type, idx);
|
2001-10-10 10:02:33 +00:00
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
expireResources(size);
|
|
|
|
|
2002-09-25 03:09:00 +00:00
|
|
|
CHECK_HEAP
|
|
|
|
ptr = (byte *)calloc(size + sizeof(MemBlkHeader) + SAFETY_AREA, 1);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (ptr == NULL) {
|
2001-10-09 14:30:12 +00:00
|
|
|
error("Out of memory while allocating %d", size);
|
|
|
|
}
|
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
_allocatedSize += size;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
res.address[type][idx] = ptr;
|
2002-04-11 17:19:16 +00:00
|
|
|
((MemBlkHeader *)ptr)->size = size;
|
2002-03-08 08:42:11 +00:00
|
|
|
setResourceCounter(type, idx, 1);
|
2002-04-11 17:19:16 +00:00
|
|
|
return ptr + sizeof(MemBlkHeader); /* skip header */
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
bool ScummEngine::validateResource(const char *str, int type, int idx) const {
|
2004-08-23 11:44:29 +00:00
|
|
|
if (type < rtFirst || type > 19 || (uint) idx >= (uint) res.num[type]) {
|
2002-04-26 14:13:39 +00:00
|
|
|
warning("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx);
|
2002-09-25 03:04:28 +00:00
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-09-25 03:04:28 +00:00
|
|
|
return true;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::nukeResource(int type, int idx) {
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *ptr;
|
|
|
|
|
2002-09-25 03:09:00 +00:00
|
|
|
CHECK_HEAP
|
|
|
|
if (!res.address[type])
|
2002-08-29 20:01:27 +00:00
|
|
|
return;
|
2002-03-14 13:57:28 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
assert(idx >= 0 && idx < res.num[type]);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
if ((ptr = res.address[type][idx]) != NULL) {
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx);
|
2002-03-08 08:42:11 +00:00
|
|
|
res.address[type][idx] = 0;
|
|
|
|
res.flags[type][idx] = 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
_allocatedSize -= ((MemBlkHeader *)ptr)->size;
|
2001-11-05 19:21:49 +00:00
|
|
|
free(ptr);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
const byte *ScummEngine::findResourceData(uint32 tag, const byte *ptr) {
|
2003-04-28 13:16:37 +00:00
|
|
|
if (_features & GF_OLD_BUNDLE)
|
|
|
|
error("findResourceData must not be used in GF_OLD_BUNDLE games");
|
|
|
|
else if (_features & GF_SMALL_HEADER)
|
2003-06-26 00:47:39 +00:00
|
|
|
ptr = findResourceSmall(tag, ptr);
|
2002-08-29 20:01:27 +00:00
|
|
|
else
|
2003-06-26 00:47:39 +00:00
|
|
|
ptr = findResource(tag, ptr);
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (ptr == NULL)
|
2001-11-26 19:57:57 +00:00
|
|
|
return NULL;
|
2002-08-29 20:01:27 +00:00
|
|
|
return ptr + _resourceHeaderSize;
|
2001-11-26 19:57:57 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
int ScummEngine::getResourceDataSize(const byte *ptr) const {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (ptr == NULL)
|
2001-12-27 17:51:58 +00:00
|
|
|
return 0;
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2003-04-12 20:12:52 +00:00
|
|
|
if (_features & GF_OLD_BUNDLE)
|
|
|
|
return READ_LE_UINT16(ptr) - 4;
|
|
|
|
else if (_features & GF_SMALL_HEADER)
|
2002-04-11 17:19:16 +00:00
|
|
|
return READ_LE_UINT32(ptr) - 6;
|
|
|
|
else
|
2003-06-14 18:52:30 +00:00
|
|
|
return READ_BE_UINT32(ptr - 4) - 8;
|
2001-12-27 17:51:58 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::lock(int type, int i) {
|
2002-09-25 03:04:28 +00:00
|
|
|
if (!validateResource("Locking", type, i))
|
|
|
|
return;
|
2001-11-14 20:09:39 +00:00
|
|
|
res.flags[type][i] |= RF_LOCK;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::unlock(int type, int i) {
|
2002-09-25 03:04:28 +00:00
|
|
|
if (!validateResource("Unlocking", type, i))
|
|
|
|
return;
|
2001-11-14 20:09:39 +00:00
|
|
|
res.flags[type][i] &= ~RF_LOCK;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
bool ScummEngine::isResourceInUse(int type, int i) const {
|
2002-09-25 03:04:28 +00:00
|
|
|
if (!validateResource("isResourceInUse", type, i))
|
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
switch (type) {
|
2001-11-05 19:21:49 +00:00
|
|
|
case rtRoom:
|
|
|
|
return _roomResource == (byte)i;
|
2002-12-27 19:48:30 +00:00
|
|
|
case rtRoomScripts:
|
|
|
|
return _roomResource == (byte)i;
|
2001-11-05 19:21:49 +00:00
|
|
|
case rtScript:
|
|
|
|
return isScriptInUse(i);
|
|
|
|
case rtCostume:
|
|
|
|
return isCostumeInUse(i);
|
|
|
|
case rtSound:
|
2003-09-07 17:14:56 +00:00
|
|
|
return _sound->isSoundInUse(i);
|
2001-11-05 19:21:49 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::increaseResourceCounter() {
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
2001-11-05 19:21:49 +00:00
|
|
|
byte counter;
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = rtFirst; i <= rtLast; i++) {
|
|
|
|
for (j = res.num[i]; --j >= 0;) {
|
2001-11-14 20:09:39 +00:00
|
|
|
counter = res.flags[i][j] & RF_USAGE;
|
|
|
|
if (counter && counter < RF_USAGE_MAX) {
|
2002-04-11 17:19:16 +00:00
|
|
|
setResourceCounter(i, j, counter + 1);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::expireResources(uint32 size) {
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
2001-11-05 19:21:49 +00:00
|
|
|
byte flag;
|
|
|
|
byte best_counter;
|
2002-04-13 04:39:04 +00:00
|
|
|
int best_type, best_res = 0;
|
2001-11-06 21:29:23 +00:00
|
|
|
uint32 oldAllocatedSize;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
if (_expire_counter != 0xFF) {
|
|
|
|
_expire_counter = 0xFF;
|
|
|
|
increaseResourceCounter();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size + _allocatedSize < _maxHeapThreshold)
|
|
|
|
return;
|
|
|
|
|
2001-11-06 21:29:23 +00:00
|
|
|
oldAllocatedSize = _allocatedSize;
|
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
do {
|
|
|
|
best_type = 0;
|
|
|
|
best_counter = 2;
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = rtFirst; i <= rtLast; i++)
|
2001-11-05 19:21:49 +00:00
|
|
|
if (res.mode[i]) {
|
2002-04-11 17:19:16 +00:00
|
|
|
for (j = res.num[i]; --j >= 0;) {
|
2001-11-05 19:21:49 +00:00
|
|
|
flag = res.flags[i][j];
|
2002-11-23 13:53:46 +00:00
|
|
|
if (!(flag & RF_LOCK) && flag >= best_counter && res.address[i][j] && !isResourceInUse(i, j)) {
|
2001-11-05 19:21:49 +00:00
|
|
|
best_counter = flag;
|
|
|
|
best_type = i;
|
|
|
|
best_res = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!best_type)
|
|
|
|
break;
|
|
|
|
nukeResource(best_type, best_res);
|
|
|
|
} while (size + _allocatedSize > _minHeapThreshold);
|
2001-11-06 21:29:23 +00:00
|
|
|
|
2001-11-07 18:10:52 +00:00
|
|
|
increaseResourceCounter();
|
|
|
|
|
2004-01-10 11:01:47 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::freeResources() {
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
|
|
|
for (i = rtFirst; i <= rtLast; i++) {
|
|
|
|
for (j = res.num[i]; --j >= 0;) {
|
|
|
|
if (isResourceLoaded(i, j))
|
|
|
|
nukeResource(i, j);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
free(res.address[i]);
|
|
|
|
free(res.flags[i]);
|
|
|
|
free(res.roomno[i]);
|
|
|
|
free(res.roomoffs[i]);
|
2004-06-07 00:26:08 +00:00
|
|
|
|
|
|
|
if (_heversion >= 70)
|
|
|
|
free(res.globsize[i]);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
2004-06-22 13:52:39 +00:00
|
|
|
if (_heversion >= 70) {
|
|
|
|
free(_HEV7RoomIntOffsets);
|
|
|
|
free(_HEV7RoomOffsets);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) {
|
2002-02-24 17:25:03 +00:00
|
|
|
byte *alloced;
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, len;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
nukeResource(type, resindex);
|
|
|
|
|
2002-12-28 01:57:19 +00:00
|
|
|
len = resStrLen(source) + 1;
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2001-11-09 22:45:19 +00:00
|
|
|
if (len <= 0)
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
alloced = createResource(type, resindex, len);
|
|
|
|
|
|
|
|
if (!source) {
|
|
|
|
alloced[0] = fetchScriptByte();
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 1; i < len; i++)
|
2001-10-09 14:30:12 +00:00
|
|
|
alloced[i] = *_scriptPointer++;
|
|
|
|
} else {
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < len; i++)
|
2001-10-09 14:30:12 +00:00
|
|
|
alloced[i] = source[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
bool ScummEngine::isResourceLoaded(int type, int idx) const {
|
2004-07-31 23:20:37 +00:00
|
|
|
if (!validateResource("isResourceLoaded", type, idx))
|
2002-09-25 03:04:28 +00:00
|
|
|
return false;
|
2002-03-08 08:42:11 +00:00
|
|
|
return res.address[type][idx] != NULL;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::resourceStats() {
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
2001-11-07 18:10:52 +00:00
|
|
|
uint32 lockedSize = 0, lockedNum = 0;
|
|
|
|
byte flag;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
for (i = rtFirst; i <= rtLast; i++)
|
|
|
|
for (j = res.num[i]; --j >= 0;) {
|
2001-11-07 18:10:52 +00:00
|
|
|
flag = res.flags[i][j];
|
2002-11-23 13:53:46 +00:00
|
|
|
if (flag & RF_LOCK && res.address[i][j]) {
|
2002-04-11 17:19:16 +00:00
|
|
|
lockedSize += ((MemBlkHeader *)res.address[i][j])->size;
|
2001-11-07 18:10:52 +00:00
|
|
|
lockedNum++;
|
|
|
|
}
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-07-13 16:22:17 +00:00
|
|
|
debug(1, "Total allocated size=%d, locked=%d(%d)", _allocatedSize, lockedSize, lockedNum);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2004-06-25 10:39:35 +00:00
|
|
|
void ScummEngine::readMAXS(int blockSize) {
|
2004-08-23 06:30:34 +00:00
|
|
|
debug(9, "readMAXS: MAXS has blocksize %d", blockSize);
|
2004-06-26 12:57:11 +00:00
|
|
|
// FIXME: trying to detect multiple targets probably a better way to do this
|
|
|
|
if (_heversion >= 70 && blockSize > 38 && _heversion < 72)
|
|
|
|
_heversion = 72;
|
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8) { // CMI
|
2003-01-19 04:10:13 +00:00
|
|
|
_fileHandle.seek(50 + 50, SEEK_CUR); // 176 - 8
|
|
|
|
_numVariables = _fileHandle.readUint32LE(); // 1500
|
|
|
|
_numBitVariables = _fileHandle.readUint32LE(); // 2048
|
|
|
|
_fileHandle.readUint32LE(); // 40
|
|
|
|
_numScripts = _fileHandle.readUint32LE(); // 458
|
|
|
|
_numSounds = _fileHandle.readUint32LE(); // 789
|
|
|
|
_numCharsets = _fileHandle.readUint32LE(); // 1
|
|
|
|
_numCostumes = _fileHandle.readUint32LE(); // 446
|
|
|
|
_numRooms = _fileHandle.readUint32LE(); // 95
|
|
|
|
_fileHandle.readUint32LE(); // 80
|
|
|
|
_numGlobalObjects = _fileHandle.readUint32LE(); // 1401
|
|
|
|
_fileHandle.readUint32LE(); // 60
|
|
|
|
_numLocalObjects = _fileHandle.readUint32LE(); // 200
|
|
|
|
_numNewNames = _fileHandle.readUint32LE(); // 100
|
|
|
|
_numFlObject = _fileHandle.readUint32LE(); // 128
|
|
|
|
_numInventory = _fileHandle.readUint32LE(); // 80
|
|
|
|
_numArray = _fileHandle.readUint32LE(); // 200
|
|
|
|
_numVerbs = _fileHandle.readUint32LE(); // 50
|
2002-07-04 15:48:17 +00:00
|
|
|
|
2002-07-07 20:32:26 +00:00
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
|
2002-07-04 15:48:17 +00:00
|
|
|
_numGlobalScripts = 2000;
|
|
|
|
|
|
|
|
_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
|
2003-06-07 00:13:26 +00:00
|
|
|
} else if (_version == 7) {
|
2002-09-10 07:34:27 +00:00
|
|
|
_fileHandle.seek(50 + 50, SEEK_CUR);
|
2002-10-21 13:23:25 +00:00
|
|
|
_numVariables = _fileHandle.readUint16LE();
|
|
|
|
_numBitVariables = _fileHandle.readUint16LE();
|
2003-01-19 04:10:13 +00:00
|
|
|
_fileHandle.readUint16LE(); // 40 in FT; 16 in Dig
|
2002-10-21 13:23:25 +00:00
|
|
|
_numGlobalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numLocalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numNewNames = _fileHandle.readUint16LE();
|
|
|
|
_numVerbs = _fileHandle.readUint16LE();
|
|
|
|
_numFlObject = _fileHandle.readUint16LE();
|
|
|
|
_numInventory = _fileHandle.readUint16LE();
|
|
|
|
_numArray = _fileHandle.readUint16LE();
|
|
|
|
_numRooms = _fileHandle.readUint16LE();
|
|
|
|
_numScripts = _fileHandle.readUint16LE();
|
|
|
|
_numSounds = _fileHandle.readUint16LE();
|
|
|
|
_numCharsets = _fileHandle.readUint16LE();
|
|
|
|
_numCostumes = _fileHandle.readUint16LE();
|
2001-11-26 19:57:57 +00:00
|
|
|
|
2002-05-01 17:16:47 +00:00
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
|
2003-11-13 10:51:02 +00:00
|
|
|
|
2004-02-06 21:05:58 +00:00
|
|
|
if ((_gameId == GID_FT) && (_features & GF_DEMO) &&
|
|
|
|
(_features & GF_PC))
|
2003-11-13 10:51:02 +00:00
|
|
|
_numGlobalScripts = 300;
|
|
|
|
else
|
|
|
|
_numGlobalScripts = 2000;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
|
|
|
_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
|
2004-06-26 12:57:11 +00:00
|
|
|
} else if (_heversion >= 70 && (blockSize == 38 + 8)) { // Scummsys.9x
|
|
|
|
_numVariables = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.readUint16LE(); // not used in spydemo
|
|
|
|
_fileHandle.readUint16LE(); // _numLocalVariables ?
|
|
|
|
_numLocalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numArray = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.readUint16LE(); // unknown
|
|
|
|
_fileHandle.readUint16LE(); // unknown
|
|
|
|
_numFlObject = _fileHandle.readUint16LE();
|
|
|
|
_numInventory = _fileHandle.readUint16LE();
|
|
|
|
_numRooms = _fileHandle.readUint16LE();
|
|
|
|
_numScripts = _fileHandle.readUint16LE();
|
|
|
|
_numSounds = _fileHandle.readUint16LE();
|
|
|
|
_numCharsets = _fileHandle.readUint16LE();
|
|
|
|
_numCostumes = _fileHandle.readUint16LE();
|
|
|
|
_numGlobalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numImages = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.readUint16LE(); // unknown
|
|
|
|
_fileHandle.readUint16LE(); // _numLocalScripts?
|
|
|
|
_fileHandle.readUint16LE(); // unknown
|
2004-08-26 14:07:19 +00:00
|
|
|
|
|
|
|
/* TODO check these values */
|
|
|
|
|
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
|
|
|
|
|
|
|
|
_numNewNames = 10;
|
|
|
|
|
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects * 4, 1);
|
|
|
|
|
|
|
|
_numGlobalScripts = 200;
|
|
|
|
_shadowPaletteSize = 256;
|
2004-06-26 12:57:11 +00:00
|
|
|
} else if (_heversion >= 70 && (blockSize == 44 + 8)) { // C++ based engine
|
2004-08-23 06:30:34 +00:00
|
|
|
error("MAXS blocks from C++ based games not yet supported");
|
2004-06-26 12:57:11 +00:00
|
|
|
} else if (_heversion >= 70 && blockSize > 38) { // sputm7.2
|
|
|
|
if (blockSize != 32 + 8)
|
2004-06-25 10:58:47 +00:00
|
|
|
error("MAXS block of size %d not supported, please report", blockSize);
|
2003-08-29 02:39:05 +00:00
|
|
|
_numVariables = _fileHandle.readUint16LE();
|
2004-08-23 07:10:16 +00:00
|
|
|
_fileHandle.readUint16LE();
|
2003-08-29 02:39:05 +00:00
|
|
|
_numBitVariables = _fileHandle.readUint16LE();
|
|
|
|
_numLocalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numArray = _fileHandle.readUint16LE();
|
|
|
|
_fileHandle.readUint16LE();
|
2004-08-23 11:28:28 +00:00
|
|
|
_numVerbs = _fileHandle.readUint16LE();
|
2003-08-29 02:39:05 +00:00
|
|
|
_numFlObject = _fileHandle.readUint16LE();
|
|
|
|
_numInventory = _fileHandle.readUint16LE();
|
|
|
|
_numRooms = _fileHandle.readUint16LE();
|
|
|
|
_numScripts = _fileHandle.readUint16LE();
|
|
|
|
_numSounds = _fileHandle.readUint16LE();
|
|
|
|
_numCharsets = _fileHandle.readUint16LE();
|
|
|
|
_numCostumes = _fileHandle.readUint16LE();
|
|
|
|
_numGlobalObjects = _fileHandle.readUint16LE();
|
2004-08-23 11:28:28 +00:00
|
|
|
_numImages = _fileHandle.readUint16LE();
|
2004-08-23 08:41:23 +00:00
|
|
|
|
2004-06-06 02:20:53 +00:00
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
|
2003-08-29 02:39:05 +00:00
|
|
|
|
2004-08-23 11:47:51 +00:00
|
|
|
_numNewNames = 10;
|
2004-02-17 17:45:22 +00:00
|
|
|
|
2003-08-29 02:39:05 +00:00
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects * 4, 1);
|
2004-06-06 02:20:53 +00:00
|
|
|
|
2003-08-29 02:39:05 +00:00
|
|
|
_numGlobalScripts = 200;
|
|
|
|
_shadowPaletteSize = 256;
|
2004-03-23 01:59:02 +00:00
|
|
|
} else if (_version == 6) {
|
2004-06-25 10:39:35 +00:00
|
|
|
if (blockSize != 30 + 8)
|
|
|
|
error("MAXS block of size %d not supported", blockSize);
|
2002-10-21 13:23:25 +00:00
|
|
|
_numVariables = _fileHandle.readUint16LE();
|
2003-01-19 04:10:13 +00:00
|
|
|
_fileHandle.readUint16LE(); // 16 in Sam/DOTT
|
2002-10-21 13:23:25 +00:00
|
|
|
_numBitVariables = _fileHandle.readUint16LE();
|
|
|
|
_numLocalObjects = _fileHandle.readUint16LE();
|
|
|
|
_numArray = _fileHandle.readUint16LE();
|
2003-01-19 04:10:13 +00:00
|
|
|
_fileHandle.readUint16LE(); // 0 in Sam/DOTT
|
2002-10-21 13:23:25 +00:00
|
|
|
_numVerbs = _fileHandle.readUint16LE();
|
|
|
|
_numFlObject = _fileHandle.readUint16LE();
|
|
|
|
_numInventory = _fileHandle.readUint16LE();
|
|
|
|
_numRooms = _fileHandle.readUint16LE();
|
|
|
|
_numScripts = _fileHandle.readUint16LE();
|
|
|
|
_numSounds = _fileHandle.readUint16LE();
|
|
|
|
_numCharsets = _fileHandle.readUint16LE();
|
|
|
|
_numCostumes = _fileHandle.readUint16LE();
|
|
|
|
_numGlobalObjects = _fileHandle.readUint16LE();
|
2001-11-26 19:57:57 +00:00
|
|
|
_numNewNames = 50;
|
|
|
|
|
|
|
|
_objectRoomTable = NULL;
|
|
|
|
_numGlobalScripts = 200;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
|
|
|
_shadowPaletteSize = 256;
|
2004-06-06 02:20:53 +00:00
|
|
|
|
|
|
|
if (_heversion >= 70) {
|
|
|
|
_objectRoomTable = (byte *)calloc(_numGlobalObjects, 1);
|
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
} else {
|
2003-01-19 04:10:13 +00:00
|
|
|
_numVariables = _fileHandle.readUint16LE(); // 800
|
|
|
|
_fileHandle.readUint16LE(); // 16
|
|
|
|
_numBitVariables = _fileHandle.readUint16LE(); // 2048
|
|
|
|
_numLocalObjects = _fileHandle.readUint16LE(); // 200
|
2001-11-26 19:57:57 +00:00
|
|
|
_numArray = 50;
|
|
|
|
_numVerbs = 100;
|
2004-04-20 07:21:25 +00:00
|
|
|
// Used to be 50, which wasn't enough for MI2 and FOA. See bugs
|
2004-04-24 12:54:02 +00:00
|
|
|
// #933610, #936323 and #941275.
|
|
|
|
_numNewNames = 150;
|
2001-11-26 19:57:57 +00:00
|
|
|
_objectRoomTable = NULL;
|
|
|
|
|
2003-01-19 04:10:13 +00:00
|
|
|
_fileHandle.readUint16LE(); // 50
|
|
|
|
_numCharsets = _fileHandle.readUint16LE(); // 9
|
|
|
|
_fileHandle.readUint16LE(); // 100
|
|
|
|
_fileHandle.readUint16LE(); // 50
|
|
|
|
_numInventory = _fileHandle.readUint16LE(); // 80
|
2001-11-26 19:57:57 +00:00
|
|
|
_numGlobalScripts = 200;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
|
|
|
_shadowPaletteSize = 256;
|
|
|
|
|
|
|
|
_numFlObject = 50;
|
2001-11-26 19:57:57 +00:00
|
|
|
}
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2001-12-27 17:51:58 +00:00
|
|
|
if (_shadowPaletteSize)
|
2002-05-01 17:16:47 +00:00
|
|
|
_shadowPalette = (byte *)calloc(_shadowPaletteSize, 1);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-11-06 21:41:56 +00:00
|
|
|
allocateArrays();
|
2003-07-28 16:55:06 +00:00
|
|
|
_dynamicRoomOffsets = true;
|
2001-10-16 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::allocateArrays() {
|
2002-03-14 13:57:28 +00:00
|
|
|
// Note: Buffers are now allocated in scummMain to allow for
|
2002-04-11 17:19:16 +00:00
|
|
|
// early GUI init.
|
|
|
|
|
2002-05-01 17:16:47 +00:00
|
|
|
_objectOwnerTable = (byte *)calloc(_numGlobalObjects, 1);
|
|
|
|
_objectStateTable = (byte *)calloc(_numGlobalObjects, 1);
|
|
|
|
_classData = (uint32 *)calloc(_numGlobalObjects, sizeof(uint32));
|
|
|
|
_newNames = (uint16 *)calloc(_numNewNames, sizeof(uint16));
|
|
|
|
|
|
|
|
_inventory = (uint16 *)calloc(_numInventory, sizeof(uint16));
|
|
|
|
_verbs = (VerbSlot *)calloc(_numVerbs, sizeof(VerbSlot));
|
|
|
|
_objs = (ObjectData *)calloc(_numLocalObjects, sizeof(ObjectData));
|
2004-08-26 23:15:39 +00:00
|
|
|
_roomVars = (int32 *)calloc(_numBitVariables, sizeof(int32));
|
2003-05-08 15:48:50 +00:00
|
|
|
_scummVars = (int32 *)calloc(_numVariables, sizeof(int32));
|
2002-05-01 17:16:47 +00:00
|
|
|
_bitVars = (byte *)calloc(_numBitVariables >> 3, 1);
|
2004-06-26 12:57:11 +00:00
|
|
|
_images = (uint16 *)calloc(_numImages, sizeof(uint16));
|
2004-08-02 05:09:53 +00:00
|
|
|
if (_features & GF_HUMONGOUS)
|
|
|
|
_arraySlot = (byte *)calloc(_numArray, 1);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-12-24 02:18:02 +00:00
|
|
|
allocResTypeData(rtCostume, (_features & GF_NEW_COSTUMES) ? MKID('AKOS') : MKID('COST'),
|
|
|
|
_numCostumes, "costume", 1);
|
2001-11-26 19:57:57 +00:00
|
|
|
allocResTypeData(rtRoom, MKID('ROOM'), _numRooms, "room", 1);
|
2002-07-04 15:48:17 +00:00
|
|
|
allocResTypeData(rtRoomScripts, MKID('RMSC'), _numRooms, "room script", 1);
|
2003-09-14 20:34:48 +00:00
|
|
|
allocResTypeData(rtSound, MKID('SOUN'), _numSounds, "sound", 2);
|
2001-11-26 19:57:57 +00:00
|
|
|
allocResTypeData(rtScript, MKID('SCRP'), _numScripts, "script", 1);
|
|
|
|
allocResTypeData(rtCharset, MKID('CHAR'), _numCharsets, "charset", 1);
|
2002-04-11 17:19:16 +00:00
|
|
|
allocResTypeData(rtObjectName, MKID('NONE'), _numNewNames, "new name", 0);
|
|
|
|
allocResTypeData(rtInventory, MKID('NONE'), _numInventory, "inventory", 0);
|
|
|
|
allocResTypeData(rtTemp, MKID('NONE'), 10, "temp", 0);
|
|
|
|
allocResTypeData(rtScaleTable, MKID('NONE'), 5, "scale table", 0);
|
2003-05-13 23:42:41 +00:00
|
|
|
allocResTypeData(rtActorName, MKID('NONE'), _numActors, "actor name", 0);
|
2002-04-11 17:19:16 +00:00
|
|
|
allocResTypeData(rtVerb, MKID('NONE'), _numVerbs, "verb", 0);
|
|
|
|
allocResTypeData(rtString, MKID('NONE'), _numArray, "array", 0);
|
|
|
|
allocResTypeData(rtFlObject, MKID('NONE'), _numFlObject, "flobject", 0);
|
|
|
|
allocResTypeData(rtMatrix, MKID('NONE'), 10, "boxes", 0);
|
2004-06-26 12:57:11 +00:00
|
|
|
allocResTypeData(rtImage, MKID('AWIZ'), _numImages, "images", 1);
|
2004-08-24 02:02:56 +00:00
|
|
|
allocResTypeData(rtRoomImage, MKID('RMIM'), _numRooms, "room image", 1);
|
2001-11-06 21:41:56 +00:00
|
|
|
}
|
2002-02-12 18:20:37 +00:00
|
|
|
|
2003-10-02 22:42:03 +00:00
|
|
|
void ScummEngine::dumpResource(const char *tag, int idx, const byte *ptr, int length) {
|
2003-07-19 18:18:01 +00:00
|
|
|
char buf[256];
|
|
|
|
File out;
|
|
|
|
|
|
|
|
uint32 size;
|
|
|
|
if (length >= 0)
|
|
|
|
size = length;
|
|
|
|
else if (_features & GF_OLD_BUNDLE)
|
|
|
|
size = READ_LE_UINT16(ptr);
|
|
|
|
else if (_features & GF_SMALL_HEADER)
|
|
|
|
size = READ_LE_UINT32(ptr);
|
|
|
|
else
|
|
|
|
size = READ_BE_UINT32(ptr + 4);
|
|
|
|
|
|
|
|
#if defined(MACOS_CARBON)
|
|
|
|
sprintf(buf, ":dumps:%s%d.dmp", tag, idx);
|
|
|
|
#else
|
|
|
|
sprintf(buf, "dumps/%s%d.dmp", tag, idx);
|
|
|
|
#endif
|
|
|
|
|
2004-06-27 22:14:35 +00:00
|
|
|
out.open(buf, File::kFileWriteMode, "");
|
2003-12-25 15:09:23 +00:00
|
|
|
if (out.isOpen() == false)
|
|
|
|
return;
|
|
|
|
out.write(ptr, size);
|
2003-07-19 18:18:01 +00:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2003-06-26 13:54:29 +00:00
|
|
|
ResourceIterator::ResourceIterator(const byte *searchin, bool smallHeader)
|
|
|
|
: _ptr(searchin), _smallHeader(smallHeader) {
|
|
|
|
assert(searchin);
|
|
|
|
if (_smallHeader) {
|
|
|
|
_size = READ_LE_UINT32(searchin);
|
|
|
|
_pos = 6;
|
|
|
|
_ptr = searchin + 6;
|
|
|
|
} else {
|
|
|
|
_size = READ_BE_UINT32(searchin + 4);
|
|
|
|
_pos = 8;
|
|
|
|
_ptr = searchin + 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte *ResourceIterator::findNext(uint32 tag) {
|
|
|
|
uint32 size = 0;
|
|
|
|
const byte *result = 0;
|
|
|
|
|
|
|
|
if (_smallHeader) {
|
|
|
|
uint16 smallTag = newTag2Old(tag);
|
|
|
|
do {
|
|
|
|
if (_pos >= _size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
result = _ptr;
|
|
|
|
size = READ_LE_UINT32(result);
|
|
|
|
if ((int32)size <= 0)
|
|
|
|
return 0; // Avoid endless loop
|
|
|
|
|
|
|
|
_pos += size;
|
|
|
|
_ptr += size;
|
|
|
|
} while (READ_LE_UINT16(result + 4) != smallTag);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (_pos >= _size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
result = _ptr;
|
|
|
|
size = READ_BE_UINT32(result + 4);
|
|
|
|
if ((int32)size <= 0)
|
|
|
|
return 0; // Avoid endless loop
|
|
|
|
|
|
|
|
_pos += size;
|
|
|
|
_ptr += size;
|
|
|
|
} while (READ_UINT32(result) != tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-06-23 01:36:57 +00:00
|
|
|
const byte *ScummEngine::findResource(uint32 tag, const byte *searchin) {
|
2003-06-26 13:54:29 +00:00
|
|
|
uint32 curpos, totalsize, size;
|
|
|
|
|
2004-06-23 01:36:57 +00:00
|
|
|
debugC(DEBUG_RESOURCE, "findResource(%s, %lx)", tag2str(tag), searchin);
|
2003-06-26 13:54:29 +00:00
|
|
|
|
2004-06-23 01:36:57 +00:00
|
|
|
if (!searchin) {
|
|
|
|
if (_heversion >= 70) {
|
|
|
|
searchin = _resourceLastSearchBuf;
|
|
|
|
totalsize = _resourceLastSearchSize;
|
|
|
|
curpos = 0;
|
|
|
|
} else {
|
|
|
|
assert(searchin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
searchin += 4;
|
|
|
|
_resourceLastSearchSize = totalsize = READ_BE_UINT32(searchin);
|
|
|
|
curpos = 8;
|
|
|
|
searchin += 4;
|
|
|
|
}
|
2003-06-26 13:54:29 +00:00
|
|
|
|
|
|
|
while (curpos < totalsize) {
|
2004-06-23 01:36:57 +00:00
|
|
|
if (READ_UINT32(searchin) == tag) {
|
|
|
|
_resourceLastSearchBuf = searchin;
|
2003-06-26 13:54:29 +00:00
|
|
|
return searchin;
|
2004-06-23 01:36:57 +00:00
|
|
|
}
|
2003-06-26 13:54:29 +00:00
|
|
|
|
|
|
|
size = READ_BE_UINT32(searchin + 4);
|
|
|
|
if ((int32)size <= 0) {
|
2003-10-17 16:28:29 +00:00
|
|
|
error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
|
2003-06-26 13:54:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
curpos += size;
|
|
|
|
searchin += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte *findResourceSmall(uint32 tag, const byte *searchin) {
|
|
|
|
uint32 curpos, totalsize, size;
|
|
|
|
uint16 smallTag;
|
|
|
|
|
|
|
|
smallTag = newTag2Old(tag);
|
2003-07-17 14:03:15 +00:00
|
|
|
if (smallTag == 0)
|
|
|
|
return NULL;
|
2003-06-26 13:54:29 +00:00
|
|
|
|
|
|
|
assert(searchin);
|
|
|
|
|
|
|
|
totalsize = READ_LE_UINT32(searchin);
|
|
|
|
searchin += 6;
|
|
|
|
curpos = 6;
|
|
|
|
|
|
|
|
while (curpos < totalsize) {
|
|
|
|
size = READ_LE_UINT32(searchin);
|
|
|
|
|
|
|
|
if (READ_LE_UINT16(searchin + 4) == smallTag)
|
|
|
|
return searchin;
|
|
|
|
|
|
|
|
if ((int32)size <= 0) {
|
2003-10-17 16:28:29 +00:00
|
|
|
error("(%s) Not found in %d... illegal block len %d", tag2str(tag), 0, size);
|
2003-06-26 13:54:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
curpos += size;
|
|
|
|
searchin += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
uint16 newTag2Old(uint32 oldTag) {
|
2002-04-11 17:19:16 +00:00
|
|
|
switch (oldTag) {
|
|
|
|
case (MKID('RMHD')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4448); // HD
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('IM00')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4D42); // BM
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('EXCD')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x5845); // EX
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('ENCD')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4E45); // EN
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('SCAL')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4153); // SA
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('LSCR')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x534C); // LS
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('OBCD')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x434F); // OC
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('OBIM')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x494F); // OI
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('SMAP')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4D42); // BM
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('CLUT')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x4150); // PA
|
2002-04-11 17:19:16 +00:00
|
|
|
case (MKID('BOXD')):
|
2002-07-28 11:57:33 +00:00
|
|
|
return (0x5842); // BX
|
2003-08-28 01:10:36 +00:00
|
|
|
case (MKID('CYCL')):
|
|
|
|
return (0x4343); // CC
|
2004-08-22 06:16:16 +00:00
|
|
|
case (MKID('EPAL')):
|
|
|
|
return (0x5053); // SP
|
2002-04-11 17:19:16 +00:00
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
2002-02-12 18:20:37 +00:00
|
|
|
}
|
2002-04-21 19:38:00 +00:00
|
|
|
|
2003-06-25 23:49:54 +00:00
|
|
|
const char *resTypeFromId(int id) {
|
2002-04-26 14:13:39 +00:00
|
|
|
static char buf[100];
|
|
|
|
|
2002-07-07 20:32:26 +00:00
|
|
|
switch (id) {
|
|
|
|
case rtRoom:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Room";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtScript:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Script";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtCostume:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Costume";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtSound:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Sound";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtInventory:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Inventory";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtCharset:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Charset";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtString:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "String";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtVerb:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Verb";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtActorName:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "ActorName";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtBuffer:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Buffer";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtScaleTable:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "ScaleTable";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtTemp:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Temp";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtFlObject:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "FlObject";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtMatrix:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Matrix";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtBox:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Box";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtLast:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "Last";
|
2002-07-07 20:32:26 +00:00
|
|
|
case rtNumTypes:
|
2003-06-25 23:49:54 +00:00
|
|
|
return "NumTypes";
|
2004-06-26 12:57:11 +00:00
|
|
|
case rtImage:
|
|
|
|
return "Image";
|
2002-07-07 20:32:26 +00:00
|
|
|
default:
|
|
|
|
sprintf(buf, "%d", id);
|
2003-06-25 23:49:54 +00:00
|
|
|
return buf;
|
2002-04-26 14:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-03 18:33:57 +00:00
|
|
|
|
|
|
|
} // End of namespace Scumm
|