2001-10-09 14:30:12 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "scumm.h"
|
|
|
|
|
|
|
|
/* Open a room */
|
|
|
|
void Scumm::openRoom(int room) {
|
2002-02-24 17:25:03 +00:00
|
|
|
int room_offs, roomlimit;
|
2001-10-09 14:30:12 +00:00
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
debug(9, "openRoom(%d)",room);
|
|
|
|
|
|
|
|
/* Don't load the same room again */
|
|
|
|
if (_lastLoadedRoom == room)
|
|
|
|
return;
|
|
|
|
_lastLoadedRoom = room;
|
|
|
|
|
|
|
|
/* Room -1 means close file */
|
|
|
|
if (room==-1) {
|
|
|
|
_encbyte = 0;
|
|
|
|
deleteRoomOffsets();
|
|
|
|
fileClose(_fileHandle);
|
|
|
|
_fileHandle = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either xxx.lfl or monkey.xxx file name */
|
|
|
|
while (!_resFilePrefix) {
|
|
|
|
#if REAL_CODE
|
|
|
|
room_offs = _roomFileOffsets[room];
|
|
|
|
#else
|
2002-02-17 10:50:34 +00:00
|
|
|
if( _features & GF_SMALL_NAMES) roomlimit = 98; else roomlimit=900;
|
|
|
|
if(_features & GF_EXTERNAL_CHARSET && room>=roomlimit)
|
2002-02-12 21:28:07 +00:00
|
|
|
room_offs = 0;
|
|
|
|
else
|
|
|
|
room_offs = room ? _roomFileOffsets[room] : 0;
|
2001-10-09 14:30:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (room_offs == 0xFFFFFFFF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (room_offs != 0 && room != 0) {
|
|
|
|
_fileOffset = _roomFileOffsets[room];
|
|
|
|
return;
|
|
|
|
}
|
2002-02-12 21:45:33 +00:00
|
|
|
if (!(_features & GF_SMALL_HEADER)) {
|
2002-02-23 23:23:28 +00:00
|
|
|
if(_features & GF_AFTER_V7)
|
2002-02-27 22:48:55 +00:00
|
|
|
sprintf(buf, "%s%s.la%d", _gameDataPath, _exe_name, room==0 ? 0 : res.roomno[rtRoom][room]);
|
2002-02-23 23:23:28 +00:00
|
|
|
else
|
2002-02-27 22:48:55 +00:00
|
|
|
sprintf(buf, "%s%s.%.3d", _gameDataPath, _exe_name, room==0 ? 0 : res.roomno[rtRoom][room]);
|
2002-02-12 21:45:33 +00:00
|
|
|
_encbyte = (_features & GF_USE_KEY) ? 0x69 : 0;
|
|
|
|
} else if(!(_features & GF_SMALL_NAMES)) {
|
2002-02-12 21:28:07 +00:00
|
|
|
if(room==0 || room>=900) {
|
2002-03-02 12:21:06 +00:00
|
|
|
sprintf(buf, "%s%.3d.lfl",_gameDataPath,room);
|
2002-02-12 21:28:07 +00:00
|
|
|
_encbyte = 0;
|
|
|
|
if (openResourceFile(buf)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
askForDisk(buf);
|
|
|
|
|
|
|
|
} else {
|
2002-02-27 22:48:55 +00:00
|
|
|
sprintf(buf, "%sdisk%.2d.lec",_gameDataPath,res.roomno[rtRoom][room]);
|
2002-02-12 21:28:07 +00:00
|
|
|
_encbyte = 0x69;
|
|
|
|
}
|
|
|
|
} else {
|
2002-02-27 22:48:55 +00:00
|
|
|
sprintf(buf, "%s%.2d.lfl",_gameDataPath,room);
|
2002-02-12 21:28:07 +00:00
|
|
|
if(_features & GF_OLD_BUNDLE)
|
|
|
|
_encbyte = 0xFF;
|
|
|
|
else
|
|
|
|
_encbyte = 0;
|
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
if (openResourceFile(buf)) {
|
|
|
|
if (room==0)
|
|
|
|
return;
|
2002-02-20 21:56:38 +00:00
|
|
|
if (_features & GF_EXTERNAL_CHARSET && room>=roomlimit)
|
|
|
|
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;
|
|
|
|
}
|
2001-10-23 19:51:50 +00:00
|
|
|
askForDisk(buf);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
sprintf(buf, "%.3d.lfl", room);
|
|
|
|
_encbyte = 0;
|
|
|
|
if (openResourceFile(buf))
|
|
|
|
break;
|
2001-10-23 19:51:50 +00:00
|
|
|
askForDisk(buf);
|
2001-10-09 14:30:12 +00:00
|
|
|
} while(1);
|
|
|
|
|
|
|
|
deleteRoomOffsets();
|
|
|
|
_fileOffset = 0; /* start of file */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the currently loaded room offsets */
|
|
|
|
void Scumm::deleteRoomOffsets() {
|
2002-02-12 18:20:37 +00:00
|
|
|
if (!(_features & GF_SMALL_HEADER) && !_dynamicRoomOffsets)
|
2001-10-09 14:30:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i=0; i<_maxRooms; i++) {
|
|
|
|
if (_roomFileOffsets[i]!=0xFFFFFFFF)
|
|
|
|
_roomFileOffsets[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read room offsets */
|
|
|
|
void Scumm::readRoomsOffsets() {
|
|
|
|
int num,room;
|
|
|
|
|
|
|
|
debug(9, "readRoomOffsets()");
|
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
deleteRoomOffsets();
|
|
|
|
if(_features & GF_SMALL_NAMES)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(_features & GF_SMALL_HEADER)) {
|
|
|
|
if (!_dynamicRoomOffsets)
|
|
|
|
return;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
fileSeek(_fileHandle, 16, SEEK_SET);
|
|
|
|
} else {
|
|
|
|
fileSeek(_fileHandle, 12, SEEK_SET); // Directlry searching for the room offset block would be more generic...
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
num = fileReadByte();
|
|
|
|
while (num) {
|
|
|
|
num--;
|
|
|
|
|
|
|
|
room = fileReadByte();
|
|
|
|
if (_roomFileOffsets[room]!=0xFFFFFFFF) {
|
|
|
|
_roomFileOffsets[room] = fileReadDwordLE();
|
|
|
|
} else {
|
|
|
|
fileReadDwordLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scumm::openResourceFile(const char *filename) {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
debug(9, "openResourceFile(%s)",filename);
|
|
|
|
|
|
|
|
if (_resFilePath) {
|
|
|
|
sprintf(buf, "%s.%d\\%s", _resFilePath, _resFilePathId, filename);
|
|
|
|
} else if (_resFilePrefix) {
|
|
|
|
sprintf(buf, "%s%s", _resFilePrefix, filename);
|
|
|
|
} else {
|
|
|
|
strcpy(buf, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fileHandle != NULL) {
|
|
|
|
fileClose(_fileHandle);
|
|
|
|
_fileHandle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fileHandle = fileOpen(buf, 1);
|
|
|
|
|
|
|
|
return _fileHandle != NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-23 19:51:50 +00:00
|
|
|
void Scumm::askForDisk(const char *filename) {
|
|
|
|
error("Cannot find '%s'", filename);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
void Scumm::readIndexFile() {
|
2001-10-09 14:30:12 +00:00
|
|
|
uint32 blocktype,itemsize;
|
|
|
|
int numblock = 0;
|
2001-11-26 19:57:57 +00:00
|
|
|
int num, i;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
debug(9, "readIndexFile()");
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
openRoom(-1);
|
|
|
|
openRoom(0);
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
if (!(_features & GF_AFTER_V6)) {
|
|
|
|
/* Figure out the sizes of various resources */
|
|
|
|
while (!fileEof(_fileHandle)) {
|
|
|
|
blocktype = fileReadDword();
|
|
|
|
itemsize = fileReadDwordBE();
|
|
|
|
if (fileReadFailed(_fileHandle))
|
|
|
|
break;
|
|
|
|
switch(blocktype) {
|
|
|
|
case MKID('DOBJ'):
|
|
|
|
_numGlobalObjects = fileReadWordLE();
|
|
|
|
itemsize-=2;
|
|
|
|
break;
|
|
|
|
case MKID('DROO'):
|
|
|
|
_numRooms = fileReadWordLE();
|
|
|
|
itemsize-=2;
|
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DSCR'):
|
|
|
|
_numScripts = fileReadWordLE();
|
|
|
|
itemsize-=2;
|
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DCOS'):
|
|
|
|
_numCostumes = fileReadWordLE();
|
|
|
|
itemsize-=2;
|
|
|
|
break;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('DSOU'):
|
|
|
|
_numSounds = fileReadWordLE();
|
|
|
|
itemsize-=2;
|
2001-10-09 14:30:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
fileSeek(_fileHandle, itemsize-8,SEEK_CUR);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
clearFileReadFailed(_fileHandle);
|
|
|
|
fileSeek(_fileHandle, 0, SEEK_SET);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
while (1) {
|
|
|
|
blocktype = fileReadDword();
|
|
|
|
|
|
|
|
if (fileReadFailed(_fileHandle))
|
|
|
|
break;
|
|
|
|
itemsize = fileReadDwordBE();
|
|
|
|
|
|
|
|
numblock++;
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
switch(blocktype) {
|
2001-10-16 10:01:48 +00:00
|
|
|
case MKID('DCHR'):
|
2001-11-10 19:12:32 +00:00
|
|
|
readResTypeList(rtCharset,MKID('CHAR'),"charset");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DOBJ'):
|
2001-11-26 19:57:57 +00:00
|
|
|
num = fileReadWordLE();
|
|
|
|
assert(num == _numGlobalObjects);
|
|
|
|
|
|
|
|
if (_features & GF_AFTER_V7) {
|
|
|
|
fileRead(_fileHandle, _objectStateTable, num);
|
|
|
|
fileRead(_fileHandle, _objectRoomTable, num);
|
|
|
|
memset(_objectOwnerTable, 0xFF, num);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fileRead(_fileHandle, _objectOwnerTable, num);
|
|
|
|
for (i=0; i<num; i++) {
|
|
|
|
_objectStateTable[i] = _objectOwnerTable[i]>>OF_STATE_SHL;
|
|
|
|
_objectOwnerTable[i] &= OF_OWNER_MASK;
|
|
|
|
}
|
|
|
|
}
|
2001-10-16 10:01:48 +00:00
|
|
|
fileRead(_fileHandle, _classData, num * sizeof(uint32));
|
2001-11-26 19:57:57 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
#if defined(SCUMM_BIG_ENDIAN)
|
2001-11-26 19:57:57 +00:00
|
|
|
for (i=0; i<num; i++) {
|
2001-10-16 10:01:48 +00:00
|
|
|
_classData[i] = FROM_LE_32(_classData[i]);
|
2001-11-26 19:57:57 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
#endif
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('RNAM'):
|
2001-11-26 19:57:57 +00:00
|
|
|
case MKID('ANAM'):
|
|
|
|
fileSeek(_fileHandle, itemsize-8,SEEK_CUR);
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DROO'):
|
2001-11-10 19:12:32 +00:00
|
|
|
readResTypeList(rtRoom,MKID('ROOM'),"room");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DSCR'):
|
2001-11-10 19:12:32 +00:00
|
|
|
readResTypeList(rtScript,MKID('SCRP'),"script");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DCOS'):
|
2001-11-10 19:12:32 +00:00
|
|
|
readResTypeList(rtCostume,MKID('COST'),"costume");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('MAXS'):
|
|
|
|
readMAXS();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('DSOU'):
|
2001-11-10 19:12:32 +00:00
|
|
|
readResTypeList(rtSound,MKID('SOUN'),"sound");
|
2001-10-16 10:01:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKID('AARY'):
|
|
|
|
readArrayFromIndexFile();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Bad ID %c%c%c%c found in directory!", blocktype&0xFF, blocktype>>8, blocktype>>16, blocktype>>24);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
// if (numblock!=9)
|
|
|
|
// error("Not enough blocks read from directory");
|
2001-10-16 10:01:48 +00:00
|
|
|
|
|
|
|
openRoom(-1);
|
|
|
|
}
|
|
|
|
|
2002-02-12 18:20:37 +00:00
|
|
|
void Scumm::readIndexFileSmall() {
|
|
|
|
uint16 blocktype;
|
|
|
|
uint32 itemsize;
|
|
|
|
int numblock = 0;
|
|
|
|
int num, i;
|
|
|
|
|
|
|
|
debug(9, "readIndexFile()");
|
|
|
|
|
|
|
|
openRoom(-1);
|
|
|
|
openRoom(0);
|
|
|
|
|
|
|
|
if (!(_features & GF_AFTER_V6)) {
|
|
|
|
while (!fileEof(_fileHandle)) {
|
|
|
|
itemsize = fileReadDwordLE();
|
|
|
|
blocktype = fileReadWordLE();
|
|
|
|
if (fileReadFailed(_fileHandle))
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(blocktype) {
|
|
|
|
case 0x4E52:
|
|
|
|
fileReadWordLE();
|
|
|
|
break;
|
|
|
|
case 0x5230:
|
|
|
|
_numRooms = fileReadWordLE();
|
|
|
|
break;
|
|
|
|
case 0x5330:
|
|
|
|
_numScripts = fileReadWordLE();
|
|
|
|
break;
|
|
|
|
case 0x4E30:
|
|
|
|
_numSounds = fileReadWordLE();
|
|
|
|
break;
|
|
|
|
case 0x4330:
|
|
|
|
_numCostumes = fileReadWordLE();
|
|
|
|
break;
|
|
|
|
case 0x4F30:
|
|
|
|
_numGlobalObjects = fileReadWordLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fileSeek(_fileHandle, itemsize-8,SEEK_CUR);
|
|
|
|
}
|
|
|
|
clearFileReadFailed(_fileHandle);
|
|
|
|
fileSeek(_fileHandle, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I'm not sure for those values yet, they will have to be rechecked */
|
|
|
|
|
|
|
|
_numVariables = 800; /* 800 */
|
|
|
|
_numBitVariables = 4096; /* 2048 */
|
|
|
|
_numLocalObjects = 200; /* 200 */
|
|
|
|
_numArray = 50;
|
|
|
|
_numVerbs = 100;
|
|
|
|
_numNewNames = 0;
|
|
|
|
_objectRoomTable = NULL;
|
|
|
|
_numCharsets = 9; /* 9 */
|
|
|
|
_numInventory = 80; /* 80 */
|
|
|
|
_numGlobalScripts = 200;
|
|
|
|
|
|
|
|
_shadowPaletteSize = 256;
|
|
|
|
_shadowPalette = (byte*)alloc(_shadowPaletteSize); // stupid for now. Need to be removed later
|
|
|
|
_numFlObject = 50;
|
|
|
|
|
|
|
|
allocateArrays();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
itemsize = fileReadDwordLE();
|
|
|
|
|
|
|
|
if (fileReadFailed(_fileHandle))
|
|
|
|
break;
|
|
|
|
|
|
|
|
blocktype = fileReadWordLE();
|
|
|
|
|
|
|
|
numblock++;
|
|
|
|
|
|
|
|
switch(blocktype) {
|
|
|
|
|
|
|
|
case 0x4E52:
|
|
|
|
fileSeek(_fileHandle, itemsize-6,SEEK_CUR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x5230:
|
|
|
|
readResTypeList(rtRoom,MKID('ROOM'),"room");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x5330:
|
|
|
|
readResTypeList(rtScript,MKID('SCRP'),"script");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x4E30:
|
|
|
|
readResTypeList(rtSound,MKID('SOUN'),"sound");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x4330:
|
|
|
|
readResTypeList(rtCostume,MKID('COST'),"costume");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x4F30:
|
|
|
|
num = fileReadWordLE();
|
|
|
|
assert(num == _numGlobalObjects);
|
|
|
|
for (i=0; i<num; i++) { /* not too sure about all that */
|
2002-02-13 17:33:52 +00:00
|
|
|
_classData[i] = fileReadByte() + 256*fileReadByte()+ 256*256*fileReadByte();
|
|
|
|
// fileReadByte();
|
2002-02-12 18:20:37 +00:00
|
|
|
_objectOwnerTable[i] = fileReadByte();
|
2002-02-15 00:46:51 +00:00
|
|
|
_objectStateTable[i] = _objectOwnerTable[i]>>OF_STATE_SHL;
|
|
|
|
_objectOwnerTable[i] &= OF_OWNER_MASK;
|
2002-02-12 18:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SCUMM_BIG_ENDIAN)
|
|
|
|
for (i=0; i<num; i++) {
|
|
|
|
_classData[i] = FROM_LE_32(_classData[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Bad ID %c%c found in directory!", blocktype&0xFF, blocktype>>8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openRoom(-1);
|
|
|
|
}
|
2001-10-16 10:01:48 +00:00
|
|
|
|
|
|
|
void Scumm::readArrayFromIndexFile() {
|
|
|
|
int num;
|
|
|
|
int a,b,c;
|
|
|
|
|
|
|
|
while ((num = fileReadWordLE()) != 0) {
|
|
|
|
a = fileReadWordLE();
|
|
|
|
b = fileReadWordLE();
|
|
|
|
c = fileReadWordLE();
|
|
|
|
if (c==1)
|
|
|
|
defineArray(num, 1, a, b);
|
|
|
|
else
|
|
|
|
defineArray(num, 5, a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::readResTypeList(int id, uint32 tag, const char *name) {
|
|
|
|
int num;
|
|
|
|
int i;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
debug(9, "readResTypeList(%d,%x,%s)",id,FROM_LE_32(tag),name);
|
|
|
|
|
|
|
|
num = fileReadWordLE();
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
if (1 || _features&GF_AFTER_V6) {
|
2001-10-23 19:51:50 +00:00
|
|
|
if (num != res.num[id]) {
|
|
|
|
error("Invalid number of %ss (%d) in directory", name, num);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (num>=0xFF) {
|
|
|
|
error("Too many %ss (%d) in directory", name, num);
|
|
|
|
}
|
|
|
|
allocResTypeData(id, tag, num, name, 1);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-02-12 21:28:07 +00:00
|
|
|
|
|
|
|
if (_features & GF_SMALL_HEADER) {
|
|
|
|
for (i=0; i<num; i++) {
|
|
|
|
res.roomno[id][i] = fileReadByte();
|
|
|
|
res.roomoffs[id][i]= fileReadDwordLE();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileRead(_fileHandle, res.roomno[id], num*sizeof(uint8));
|
|
|
|
fileRead(_fileHandle, res.roomoffs[id], num*sizeof(uint32));
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
#if defined(SCUMM_BIG_ENDIAN)
|
|
|
|
for (i=0; i<num; i++)
|
|
|
|
res.roomoffs[id][i] = FROM_LE_32(res.roomoffs[id][i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
void Scumm::allocResTypeData(int id, uint32 tag, int num, const char *name, int mode) {
|
|
|
|
debug(9, "allocResTypeData(%d,%x,%d,%s,%d)",id,FROM_LE_32(tag),num,name,mode);
|
2001-10-16 10:01:48 +00:00
|
|
|
assert(id>=0 && id<sizeof(res.mode)/sizeof(res.mode[0]));
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
if (num>=2000) {
|
2001-10-16 10:01:48 +00:00
|
|
|
error("Too many %ss (%d) in directory", name, num);
|
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
res.mode[id] = mode;
|
|
|
|
res.num[id] = num;
|
|
|
|
res.tags[id] = tag;
|
|
|
|
res.name[id] = name;
|
|
|
|
res.address[id] = (byte**)alloc(num*sizeof(void*));
|
|
|
|
res.flags[id] = (byte*)alloc(num*sizeof(byte));
|
|
|
|
|
|
|
|
if (mode) {
|
|
|
|
res.roomno[id] = (byte*)alloc(num*sizeof(byte));
|
|
|
|
res.roomoffs[id] = (uint32*)alloc(num*sizeof(uint32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::loadCharset(int no) {
|
|
|
|
int i;
|
|
|
|
byte *ptr;
|
|
|
|
|
|
|
|
debug(9, "loadCharset(%d)",no);
|
2002-02-12 21:28:07 +00:00
|
|
|
if(_features & GF_EXTERNAL_CHARSET) {
|
|
|
|
uint32 size;
|
|
|
|
checkRange(4 ,0 ,no , "Loading illegal charset %d");
|
|
|
|
openRoom(-1);
|
|
|
|
if( _features & GF_SMALL_NAMES)
|
|
|
|
openRoom(98+no);
|
|
|
|
else
|
|
|
|
openRoom(900+no);
|
2002-02-16 19:03:14 +00:00
|
|
|
|
|
|
|
if (_features & GF_OLD256)
|
|
|
|
size = fileReadWordLE();
|
|
|
|
else
|
|
|
|
size = fileReadDword();
|
2002-02-12 21:28:07 +00:00
|
|
|
fileRead(_fileHandle, createResource(6, no, size), size);
|
|
|
|
openRoom(-1);
|
|
|
|
} else {
|
|
|
|
checkRange(_maxCharsets-1, 1, no, "Loading illegal charset %d");
|
|
|
|
}
|
2001-11-05 19:21:49 +00:00
|
|
|
// ensureResourceLoaded(6, no);
|
2001-10-09 14:30:12 +00:00
|
|
|
ptr = getResourceAddress(6, no);
|
|
|
|
|
|
|
|
for (i=0; i<15; i++) {
|
|
|
|
_charsetData[no][i+1] = ptr[i+14];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::nukeCharset(int i) {
|
|
|
|
checkRange(_maxCharsets-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
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::ensureResourceLoaded(int type, int i) {
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
debug(9, "ensureResourceLoaded(%d,%d)", type, i);
|
|
|
|
|
2001-11-10 19:12:32 +00:00
|
|
|
if (type==rtRoom && i>127) {
|
2001-10-09 14:30:12 +00:00
|
|
|
i = _resourceMapper[i&127];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
addr = res.address[type][i];
|
|
|
|
if (addr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
loadResource(type, i);
|
|
|
|
|
2002-03-05 23:05:55 +00:00
|
|
|
if(!(_features & GF_AFTER_V7))
|
|
|
|
if (type==rtRoom && i==_roomResource)
|
|
|
|
_vars[VAR_ROOM_FLAG] = 1;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
int Scumm::loadResource(int type, int idx) {
|
2001-10-09 14:30:12 +00:00
|
|
|
int roomNr, i;
|
|
|
|
uint32 fileOffs;
|
|
|
|
uint32 size, tag;
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
// debug(1, "loadResource(%d,%d)", type,idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
if(type == rtCharset && (_features & GF_SMALL_HEADER)){
|
2002-03-08 08:42:11 +00:00
|
|
|
loadCharset(idx);
|
2002-02-12 21:28:07 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
roomNr = getResourceRoomNr(type, idx);
|
|
|
|
if (roomNr == 0 || idx >= res.num[type]) {
|
2001-10-09 14:30:12 +00:00
|
|
|
error("%s %d undefined",
|
2002-03-08 08:42:11 +00:00
|
|
|
res.name[type],idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2001-11-10 19:12:32 +00:00
|
|
|
if (type==rtRoom) {
|
2001-10-09 14:30:12 +00:00
|
|
|
fileOffs = 0;
|
|
|
|
} else {
|
2002-03-08 08:42:11 +00:00
|
|
|
fileOffs = res.roomoffs[type][idx];
|
2001-10-09 14:30:12 +00:00
|
|
|
if (fileOffs==0xFFFFFFFF)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
for (i=0; i<5; i++) {
|
|
|
|
openRoom(roomNr);
|
|
|
|
|
|
|
|
fileSeek(_fileHandle, fileOffs + _fileOffset, SEEK_SET);
|
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
if (_features & GF_SMALL_HEADER) {
|
|
|
|
if(!(_features & GF_SMALL_NAMES))
|
|
|
|
fileSeek(_fileHandle, 8, SEEK_CUR);
|
|
|
|
size = fileReadDwordLE();
|
|
|
|
tag = fileReadWordLE();
|
|
|
|
fileSeek(_fileHandle, -6, SEEK_CUR);
|
|
|
|
} else {
|
|
|
|
if (type==rtSound) {
|
|
|
|
fileReadDwordLE();
|
|
|
|
fileReadDwordLE();
|
2002-03-08 08:42:11 +00:00
|
|
|
return readSoundResource(type, idx);
|
2002-02-12 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tag = fileReadDword();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
if (tag != res.tags[type]) {
|
|
|
|
error("%s %d not in room %d at %d+%d",
|
|
|
|
res.name[type], type, roomNr, _fileOffset, fileOffs);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
size = fileReadDwordBE();
|
|
|
|
fileSeek(_fileHandle, -8, SEEK_CUR);
|
|
|
|
}
|
2002-03-08 08:42:11 +00:00
|
|
|
fileRead(_fileHandle, createResource(type, idx, size), size);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-02-12 21:28:07 +00:00
|
|
|
/* dump the resource */
|
2001-10-09 14:30:12 +00:00
|
|
|
#ifdef DUMP_SCRIPTS
|
2001-11-10 19:12:32 +00:00
|
|
|
if(type==rtScript) {
|
2002-03-08 08:42:11 +00:00
|
|
|
dumpResource("script-", idx, getResourceAddress(rtScript, idx));
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!fileReadFailed(_fileHandle)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
nukeResource(type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error("Cannot read resource");
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
int Scumm::readSoundResource(int type, int idx) {
|
2001-12-01 17:06:13 +00:00
|
|
|
uint32 pos, total_size, size, tag,basetag;
|
|
|
|
int pri, best_pri;
|
|
|
|
uint32 best_size, best_offs;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
debug(9, "readSoundResource(%d,%d)", 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
|
|
|
|
2001-11-11 16:54:45 +00:00
|
|
|
basetag = fileReadDwordLE();
|
2001-12-01 17:06:13 +00:00
|
|
|
total_size = fileReadDwordBE();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-06 00:18:22 +00:00
|
|
|
if (_gameId==GID_SAMNMAX || _features & GF_AFTER_V7) {
|
2002-03-05 22:23:48 +00:00
|
|
|
if (basetag == MKID('MIDI')) {
|
|
|
|
fileSeek(_fileHandle, -8, SEEK_CUR);
|
2002-03-08 08:42:11 +00:00
|
|
|
fileRead(_fileHandle,createResource(type, idx, total_size+8), total_size+8);
|
2002-03-05 22:23:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
best_pri = -1;
|
|
|
|
while (pos < total_size) {
|
|
|
|
tag = fileReadDword();
|
|
|
|
size = fileReadDwordBE() + 8;
|
|
|
|
pos += size;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-05 22:23:48 +00:00
|
|
|
switch(tag) {
|
2001-12-01 17:06:13 +00:00
|
|
|
#ifdef USE_ADLIB
|
2002-03-05 22:23:48 +00:00
|
|
|
case MKID('ADL '): pri = 10; break;
|
2001-12-01 17:06:13 +00:00
|
|
|
#else
|
2002-03-05 22:23:48 +00:00
|
|
|
case MKID('ROL '): pri = 1; break;
|
|
|
|
case MKID('GMD '): pri = 2; break;
|
2001-12-01 17:06:13 +00:00
|
|
|
#endif
|
2002-03-05 22:23:48 +00:00
|
|
|
default: pri = -1;
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-05 22:23:48 +00:00
|
|
|
if (pri > best_pri) {
|
|
|
|
best_pri = pri;
|
|
|
|
best_size = size;
|
|
|
|
best_offs = filePos(_fileHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSeek(_fileHandle, size - 8, SEEK_CUR);
|
2001-12-01 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
2002-03-05 22:23:48 +00:00
|
|
|
if (best_pri != -1) {
|
|
|
|
fileSeek(_fileHandle, best_offs - 8, SEEK_SET);
|
2002-03-08 08:42:11 +00:00
|
|
|
fileRead(_fileHandle,createResource(type, idx, best_size), best_size);
|
2002-03-05 22:23:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-12-01 17:06:13 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
int Scumm::getResourceRoomNr(int type, int idx) {
|
2001-11-10 19:12:32 +00:00
|
|
|
if (type==rtRoom)
|
2002-03-08 08:42:11 +00:00
|
|
|
return idx;
|
|
|
|
return res.roomno[type][idx];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
byte *Scumm::getResourceAddress(int type, int idx) {
|
2001-10-09 14:30:12 +00:00
|
|
|
byte *ptr;
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
debug(9, "getResourceAddress(%d,%d)", type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
CHECK_HEAP
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
validateResource("getResourceAddress", type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
if (res.mode[type] && !res.address[type][idx]) {
|
|
|
|
ensureResourceLoaded(type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
ptr=(byte*)res.address[type][idx];
|
2001-10-09 14:30:12 +00:00
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
setResourceCounter(type, idx, 1);
|
2001-11-14 18:40:39 +00:00
|
|
|
|
2001-12-11 13:34:15 +00:00
|
|
|
return ptr + sizeof(MemBlkHeader);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2001-10-24 20:12:52 +00:00
|
|
|
byte *Scumm::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)
|
|
|
|
return b;
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
if (_features & GF_NEW_OPCODES)
|
2001-10-24 20:12:52 +00:00
|
|
|
return ((ArrayHeader*)b)->data;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
void Scumm::setResourceCounter(int type, int idx, byte flag) {
|
|
|
|
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
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
byte *Scumm::createResource(int type, int idx, uint32 size) {
|
2001-10-09 14:30:12 +00:00
|
|
|
byte *ptr;
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
CHECK_HEAP
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
debug(9, "createResource(%d,%d,%d)", type, idx,size);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
if (size > 65536*4+37856)
|
2001-12-27 17:51:58 +00:00
|
|
|
warning("Probably invalid size allocating %d", size);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
validateResource("allocating", type, idx);
|
|
|
|
nukeResource(type, idx);
|
2001-10-10 10:02:33 +00:00
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
expireResources(size);
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
CHECK_HEAP
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-12-11 13:34:15 +00:00
|
|
|
ptr = (byte*)alloc(size + sizeof(MemBlkHeader) + SAFETY_AREA);
|
2001-10-09 14:30:12 +00:00
|
|
|
if (ptr==NULL) {
|
|
|
|
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;
|
2001-12-11 13:34:15 +00:00
|
|
|
((MemBlkHeader*)ptr)->size = size;
|
2002-03-08 08:42:11 +00:00
|
|
|
setResourceCounter(type, idx, 1);
|
2001-12-11 13:34:15 +00:00
|
|
|
return ptr + sizeof(MemBlkHeader); /* skip header */
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
void Scumm::validateResource(const char *str, int type, int idx) {
|
|
|
|
if (type<rtFirst || type>rtLast || (uint)idx >= (uint)res.num[type]) {
|
|
|
|
error("%s Illegal Glob type %d num %d", str, type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
void Scumm::nukeResource(int type, int idx) {
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *ptr;
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
debug(9, "nukeResource(%d,%d)", type, idx);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
CHECK_HEAP
|
2001-10-09 14:30:12 +00:00
|
|
|
assert( res.address[type] );
|
2002-03-08 08:42:11 +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) {
|
|
|
|
res.address[type][idx] = 0;
|
|
|
|
res.flags[type][idx] = 0;
|
2001-12-11 13:34:15 +00:00
|
|
|
_allocatedSize -= ((MemBlkHeader*)ptr)->size;
|
2001-11-05 19:21:49 +00:00
|
|
|
free(ptr);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
byte *Scumm::findResourceData(uint32 tag, byte *ptr) {
|
2002-02-12 21:28:07 +00:00
|
|
|
if(_features & GF_SMALL_HEADER) {
|
|
|
|
ptr = findResourceSmall(tag,ptr,0);
|
|
|
|
if (ptr==NULL)
|
|
|
|
return NULL;
|
|
|
|
return ptr + 6;
|
|
|
|
}
|
|
|
|
|
2001-12-27 17:51:58 +00:00
|
|
|
ptr = findResource(tag,ptr,0);
|
2001-11-26 19:57:57 +00:00
|
|
|
if (ptr==NULL)
|
|
|
|
return NULL;
|
|
|
|
return ptr + 8;
|
|
|
|
}
|
|
|
|
|
2001-12-27 17:51:58 +00:00
|
|
|
int Scumm::getResourceDataSize(byte *ptr) {
|
|
|
|
if (ptr==NULL)
|
|
|
|
return 0;
|
2002-02-12 21:28:07 +00:00
|
|
|
|
|
|
|
if (_features & GF_SMALL_HEADER)
|
|
|
|
return READ_LE_UINT32(ptr)-6;
|
|
|
|
else
|
|
|
|
return READ_BE_UINT32(ptr-4)-8;
|
2001-12-27 17:51:58 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
struct FindResourceState {
|
|
|
|
uint32 size,pos;
|
|
|
|
byte *ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* just O(N) complexity when iterating with this function */
|
|
|
|
byte *findResource(uint32 tag, byte *searchin) {
|
|
|
|
uint32 size;
|
|
|
|
static FindResourceState frs;
|
|
|
|
FindResourceState *f = &frs; /* easier to make it thread safe like this */
|
|
|
|
|
|
|
|
if (searchin) {
|
|
|
|
f->size = READ_BE_UINT32_UNALIGNED(searchin+4);
|
|
|
|
f->pos = 8;
|
|
|
|
f->ptr = searchin+8;
|
|
|
|
goto StartScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
size = READ_BE_UINT32_UNALIGNED(f->ptr+4);
|
|
|
|
if ((int32)size <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f->pos += size;
|
|
|
|
f->ptr += size;
|
|
|
|
|
|
|
|
StartScan:
|
|
|
|
if (f->pos >= f->size)
|
|
|
|
return NULL;
|
|
|
|
} while (READ_UINT32_UNALIGNED(f->ptr) != tag);
|
|
|
|
|
|
|
|
return f->ptr;
|
|
|
|
}
|
|
|
|
|
2002-02-12 18:20:37 +00:00
|
|
|
byte *findResourceSmall(uint32 tag, byte *searchin) {
|
|
|
|
uint32 size;
|
|
|
|
static FindResourceState frs;
|
|
|
|
FindResourceState *f = &frs; /* easier to make it thread safe like this */
|
|
|
|
uint16 smallTag;
|
|
|
|
|
|
|
|
smallTag=newTag2Old(tag);
|
|
|
|
|
|
|
|
if (searchin) {
|
|
|
|
f->size = READ_LE_UINT32(searchin);
|
|
|
|
f->pos = 6;
|
|
|
|
f->ptr = searchin+6;
|
|
|
|
goto StartScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
size = READ_LE_UINT32(f->ptr);
|
|
|
|
if ((int32)size <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f->pos += size;
|
|
|
|
f->ptr += size;
|
|
|
|
|
|
|
|
StartScan:
|
|
|
|
if (f->pos >= f->size)
|
|
|
|
return NULL;
|
|
|
|
} while (READ_LE_UINT16(f->ptr+4) != smallTag);
|
|
|
|
|
|
|
|
return f->ptr;
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
byte *findResource(uint32 tag, byte *searchin, int idx) {
|
2002-02-24 17:25:03 +00:00
|
|
|
uint32 curpos,totalsize,size;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-11 16:54:45 +00:00
|
|
|
assert(searchin);
|
|
|
|
|
2001-10-26 17:34:50 +00:00
|
|
|
searchin += 4;
|
|
|
|
totalsize = READ_BE_UINT32_UNALIGNED(searchin);
|
|
|
|
curpos = 8;
|
|
|
|
searchin += 4;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-10-26 17:34:50 +00:00
|
|
|
while (curpos<totalsize) {
|
2002-03-08 08:42:11 +00:00
|
|
|
if (READ_UINT32_UNALIGNED(searchin)==tag && !idx--)
|
2001-10-26 17:34:50 +00:00
|
|
|
return searchin;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-10-26 17:34:50 +00:00
|
|
|
size = READ_BE_UINT32_UNALIGNED(searchin+4);
|
2001-10-09 14:30:12 +00:00
|
|
|
if ((int32)size <= 0) {
|
|
|
|
error("(%c%c%c%c) Not found in %d... illegal block len %d",
|
|
|
|
tag&0xFF,(tag>>8)&0xFF,(tag>>16)&0xFF,(tag>>24)&0xFF,
|
|
|
|
0,
|
|
|
|
size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-26 17:34:50 +00:00
|
|
|
curpos += size;
|
|
|
|
searchin += size;
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-10-26 17:34:50 +00:00
|
|
|
return NULL;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
byte *findResourceSmall(uint32 tag, byte *searchin, int idx) {
|
2002-02-24 17:25:03 +00:00
|
|
|
uint32 curpos,totalsize,size;
|
2002-02-12 21:28:07 +00:00
|
|
|
uint16 smallTag;
|
|
|
|
|
|
|
|
smallTag=newTag2Old(tag);
|
|
|
|
|
|
|
|
assert(searchin);
|
|
|
|
|
|
|
|
totalsize = READ_LE_UINT32(searchin);
|
|
|
|
searchin += 6;
|
|
|
|
curpos = 6;
|
|
|
|
|
|
|
|
while (curpos<totalsize) {
|
|
|
|
size = READ_LE_UINT32(searchin);
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
if (READ_LE_UINT16(searchin+4)==smallTag && !idx--)
|
2002-02-12 21:28:07 +00:00
|
|
|
return searchin;
|
|
|
|
|
|
|
|
if ((int32)size <= 0) {
|
|
|
|
error("(%c%c%c%c) Not found in %d... illegal block len %d",
|
|
|
|
tag&0xFF,(tag>>8)&0xFF,(tag>>16)&0xFF,(tag>>24)&0xFF,
|
|
|
|
0, size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
curpos += size;
|
|
|
|
searchin += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
void Scumm::lock(int type, int i) {
|
|
|
|
validateResource("Locking", type, i);
|
2001-11-14 20:09:39 +00:00
|
|
|
res.flags[type][i] |= RF_LOCK;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2001-11-07 18:10:52 +00:00
|
|
|
// debug(1, "locking %d,%d", type, i);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::unlock(int type, int i) {
|
|
|
|
validateResource("Unlocking", type, i);
|
2001-11-14 20:09:39 +00:00
|
|
|
res.flags[type][i] &= ~RF_LOCK;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2001-11-07 18:10:52 +00:00
|
|
|
// debug(1, "unlocking %d,%d", type, i);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Scumm::isResourceInUse(int type, int i) {
|
|
|
|
validateResource("isResourceInUse", type, i);
|
|
|
|
switch(type) {
|
|
|
|
case rtRoom:
|
|
|
|
return _roomResource == (byte)i;
|
|
|
|
case rtScript:
|
|
|
|
return isScriptInUse(i);
|
|
|
|
case rtCostume:
|
|
|
|
return isCostumeInUse(i);
|
|
|
|
case rtSound:
|
|
|
|
return isSoundRunning(i)!=0;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::increaseResourceCounter() {
|
|
|
|
int i,j;
|
|
|
|
byte counter;
|
|
|
|
|
2001-11-10 19:12:32 +00:00
|
|
|
for (i=rtFirst; i<=rtLast; i++) {
|
2001-11-05 19:21:49 +00:00
|
|
|
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) {
|
2001-11-05 19:21:49 +00:00
|
|
|
setResourceCounter(i, j, counter+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::expireResources(uint32 size) {
|
|
|
|
int i,j;
|
|
|
|
byte flag;
|
|
|
|
byte best_counter;
|
|
|
|
int best_type, best_res;
|
2001-11-06 21:29:23 +00:00
|
|
|
uint32 oldAllocatedSize;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
return;
|
|
|
|
|
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;
|
|
|
|
|
2001-11-10 19:12:32 +00:00
|
|
|
for (i=rtFirst; i<=rtLast; i++)
|
2001-11-05 19:21:49 +00:00
|
|
|
if (res.mode[i]) {
|
|
|
|
for(j=res.num[i]; --j>=0;) {
|
|
|
|
flag = res.flags[i][j];
|
2001-11-10 23:30:12 +00:00
|
|
|
if (!(flag&0x80) && 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();
|
|
|
|
|
2001-11-06 21:29:23 +00:00
|
|
|
debug(1, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::freeResources() {
|
|
|
|
int i,j;
|
2001-11-10 19:12:32 +00:00
|
|
|
for (i=rtFirst; i<=rtLast; i++) {
|
2001-11-05 19:21:49 +00:00
|
|
|
for(j=res.num[i]; --j>=0;) {
|
|
|
|
if (isResourceLoaded(i,j))
|
|
|
|
nukeResource(i,j);
|
|
|
|
}
|
|
|
|
free(res.address[i]);
|
|
|
|
free(res.flags[i]);
|
|
|
|
free(res.roomno[i]);
|
|
|
|
free(res.roomoffs[i]);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::loadPtrToResource(int type, int resindex, byte *source) {
|
2002-02-24 17:25:03 +00:00
|
|
|
byte *alloced;
|
2001-10-09 14:30:12 +00:00
|
|
|
int i,len;
|
|
|
|
|
|
|
|
nukeResource(type, resindex);
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
len = getStringLen(source);
|
|
|
|
|
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();
|
|
|
|
for (i=1; i<len; i++)
|
|
|
|
alloced[i] = *_scriptPointer++;
|
|
|
|
} else {
|
|
|
|
for(i=0; i<len; i++)
|
|
|
|
alloced[i] = source[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-08 08:42:11 +00:00
|
|
|
bool Scumm::isResourceLoaded(int type, int idx) {
|
|
|
|
validateResource("isLoaded", type, idx);
|
|
|
|
return res.address[type][idx] != NULL;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::resourceStats() {
|
2001-11-07 18:10:52 +00:00
|
|
|
int i,j;
|
|
|
|
uint32 lockedSize = 0, lockedNum = 0;
|
|
|
|
byte flag;
|
|
|
|
|
2001-11-10 19:12:32 +00:00
|
|
|
for (i=rtFirst; i<=rtLast; i++)
|
2001-11-07 18:10:52 +00:00
|
|
|
for(j=res.num[i]; --j>=0;) {
|
|
|
|
flag = res.flags[i][j];
|
|
|
|
if (flag&0x80 && res.address[i][j]) {
|
2001-12-11 13:34:15 +00:00
|
|
|
lockedSize += ((MemBlkHeader*)res.address[i][j])->size;
|
2001-11-07 18:10:52 +00:00
|
|
|
lockedNum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Total allocated size=%d, locked=%d(%d)\n", _allocatedSize, lockedSize, lockedNum);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
void Scumm::heapClear(int mode) {
|
|
|
|
/* TODO: implement this */
|
|
|
|
warning("heapClear: not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::unkHeapProc2(int a, int b) {
|
|
|
|
warning("unkHeapProc2: not implemented");
|
|
|
|
}
|
|
|
|
|
2001-10-16 10:01:48 +00:00
|
|
|
void Scumm::readMAXS() {
|
2001-11-26 19:57:57 +00:00
|
|
|
if (_features & GF_AFTER_V7) {
|
|
|
|
fileSeek(_fileHandle, 50+50, SEEK_CUR);
|
|
|
|
_numVariables = fileReadWordLE();
|
|
|
|
_numBitVariables = fileReadWordLE();
|
|
|
|
fileReadWordLE();
|
|
|
|
_numGlobalObjects = fileReadWordLE();
|
|
|
|
_numLocalObjects = fileReadWordLE();
|
|
|
|
_numNewNames = fileReadWordLE();
|
|
|
|
_numVerbs = fileReadWordLE();
|
|
|
|
_numFlObject = fileReadWordLE();
|
|
|
|
_numInventory = fileReadWordLE();
|
|
|
|
_numArray = fileReadWordLE();
|
|
|
|
_numRooms = fileReadWordLE();
|
|
|
|
_numScripts = fileReadWordLE();
|
|
|
|
_numSounds = fileReadWordLE();
|
|
|
|
_numCharsets = fileReadWordLE();
|
|
|
|
_numCostumes = fileReadWordLE();
|
|
|
|
|
|
|
|
_objectRoomTable = (byte*)alloc(_numGlobalObjects);
|
|
|
|
_numGlobalScripts = 2000;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
|
|
|
_shadowPaletteSize = NUM_SHADOW_PALETTE * 256;
|
2001-11-26 19:57:57 +00:00
|
|
|
} else if (_features & GF_AFTER_V6) {
|
|
|
|
_numVariables = fileReadWordLE();
|
|
|
|
fileReadWordLE();
|
|
|
|
_numBitVariables = fileReadWordLE();
|
|
|
|
_numLocalObjects = fileReadWordLE();
|
|
|
|
_numArray = fileReadWordLE();
|
|
|
|
fileReadWordLE();
|
|
|
|
_numVerbs = fileReadWordLE();
|
|
|
|
_numFlObject = fileReadWordLE();
|
|
|
|
_numInventory = fileReadWordLE();
|
|
|
|
_numRooms = fileReadWordLE();
|
|
|
|
_numScripts = fileReadWordLE();
|
|
|
|
_numSounds = fileReadWordLE();
|
|
|
|
_numCharsets = fileReadWordLE();
|
|
|
|
_numCostumes = fileReadWordLE();
|
|
|
|
_numGlobalObjects = fileReadWordLE();
|
|
|
|
_numNewNames = 50;
|
|
|
|
|
|
|
|
_objectRoomTable = NULL;
|
|
|
|
_numGlobalScripts = 200;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
|
|
|
_shadowPaletteSize = 256;
|
2001-11-26 19:57:57 +00:00
|
|
|
} else {
|
|
|
|
_numVariables = fileReadWordLE(); /* 800 */
|
|
|
|
fileReadWordLE(); /* 16 */
|
|
|
|
_numBitVariables = fileReadWordLE(); /* 2048 */
|
|
|
|
_numLocalObjects = fileReadWordLE(); /* 200 */
|
|
|
|
_numArray = 50;
|
|
|
|
_numVerbs = 100;
|
|
|
|
_numNewNames = 0;
|
|
|
|
_objectRoomTable = NULL;
|
|
|
|
|
|
|
|
fileReadWordLE(); /* 50 */
|
|
|
|
_numCharsets = fileReadWordLE(); /* 9 */
|
|
|
|
fileReadWordLE(); /* 100 */
|
|
|
|
fileReadWordLE(); /* 50 */
|
|
|
|
_numInventory = fileReadWordLE(); /* 80 */
|
|
|
|
_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)
|
|
|
|
_shadowPalette = (byte*)alloc(_shadowPaletteSize);
|
2001-11-06 21:41:56 +00:00
|
|
|
|
|
|
|
allocateArrays();
|
2001-10-16 10:01:48 +00:00
|
|
|
_dynamicRoomOffsets = 1;
|
|
|
|
}
|
|
|
|
|
2001-11-06 21:41:56 +00:00
|
|
|
void Scumm::allocateArrays() {
|
2001-11-26 19:57:57 +00:00
|
|
|
_objectOwnerTable = (byte*)alloc(_numGlobalObjects);
|
|
|
|
_objectStateTable = (byte*)alloc(_numGlobalObjects);
|
|
|
|
_classData = (uint32*)alloc(_numGlobalObjects * sizeof(uint32));
|
|
|
|
_arrays = (byte*)alloc(_numArray);
|
|
|
|
_newNames = (uint16*)alloc(_numNewNames * sizeof(uint16));
|
|
|
|
|
2001-11-06 21:41:56 +00:00
|
|
|
_inventory = (uint16*)alloc(_numInventory * sizeof(uint16));
|
|
|
|
_verbs = (VerbSlot*)alloc(_numVerbs * sizeof(VerbSlot));
|
|
|
|
_objs = (ObjectData*)alloc(_numLocalObjects * sizeof(ObjectData));
|
|
|
|
_vars = (int16*)alloc(_numVariables * sizeof(int16));
|
|
|
|
_bitVars = (byte*)alloc(_numBitVariables >> 3);
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2001-12-27 17:51:58 +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);
|
|
|
|
allocResTypeData(rtSound, MKID('SOUN'), _numSounds, "sound", 1);
|
|
|
|
allocResTypeData(rtScript, MKID('SCRP'), _numScripts, "script", 1);
|
|
|
|
allocResTypeData(rtCharset, MKID('CHAR'), _numCharsets, "charset", 1);
|
|
|
|
allocResTypeData(rtObjectName, MKID('NONE'),_numNewNames,"new name", 0);
|
2001-11-06 21:41:56 +00:00
|
|
|
allocResTypeData(rtInventory, MKID('NONE'), _numInventory, "inventory", 0);
|
|
|
|
allocResTypeData(rtTemp,MKID('NONE'),10, "temp", 0);
|
|
|
|
allocResTypeData(rtScaleTable,MKID('NONE'),5, "scale table", 0);
|
2001-12-27 17:51:58 +00:00
|
|
|
allocResTypeData(rtActorName, MKID('NONE'),NUM_ACTORS,"actor name", 0);
|
2001-11-06 21:41:56 +00:00
|
|
|
allocResTypeData(rtBuffer, MKID('NONE'),10,"buffer", 0);
|
|
|
|
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);
|
|
|
|
}
|
2002-02-12 18:20:37 +00:00
|
|
|
|
|
|
|
uint16 newTag2Old(uint32 oldTag) {
|
|
|
|
switch(oldTag) {
|
|
|
|
case(MKID('RMHD')):
|
|
|
|
return(0x4448);
|
|
|
|
break;
|
|
|
|
case(MKID('IM00')):
|
|
|
|
return(0x4D42);
|
|
|
|
break;
|
|
|
|
case(MKID('EXCD')):
|
|
|
|
return(0x5845);
|
|
|
|
break;
|
|
|
|
case(MKID('ENCD')):
|
|
|
|
return(0x4E45);
|
|
|
|
break;
|
|
|
|
case(MKID('SCAL')):
|
|
|
|
return(0x4153);
|
|
|
|
break;
|
|
|
|
case(MKID('LSCR')):
|
|
|
|
return(0x534C);
|
|
|
|
break;
|
|
|
|
case(MKID('OBCD')):
|
|
|
|
return(0x434F);
|
|
|
|
break;
|
|
|
|
case(MKID('OBIM')):
|
|
|
|
return(0x494F);
|
|
|
|
break;
|
|
|
|
case(MKID('SMAP')):
|
|
|
|
return(0x4D42);
|
|
|
|
break;
|
|
|
|
case(MKID('CLUT')):
|
|
|
|
return(0x4150);
|
|
|
|
break;
|
|
|
|
case(MKID('BOXD')):
|
|
|
|
return(0x5842);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|