mirror of
https://github.com/libretro/scummvm.git
synced 2025-01-07 10:21:31 +00:00
be698249f1
Most of these changes resolve inconsistencies between the three loaders. Formatting, error handling, logging, etc. V2 and V3 parsers no longer assume that directory files are multiples of three. Some Mac games have padding. This caused uninitialized memory to be parsed and stored in the directory table.
328 lines
8.9 KiB
C++
328 lines
8.9 KiB
C++
/* ScummVM - Graphic Adventure Engine
|
|
*
|
|
* ScummVM is the legal property of its developers, whose names
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
* file distributed with this source distribution.
|
|
*
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
#include "agi/agi.h"
|
|
#include "agi/words.h"
|
|
|
|
#include "common/md5.h"
|
|
|
|
#define IMAGE_SIZE 368640 // = 40 * 2 * 9 * 512 = tracks * sides * sectors * sector size
|
|
#define SECTOR_OFFSET(s) ((s) * 512)
|
|
|
|
#define DDP_BASE_SECTOR 0x1C2
|
|
#define DDP_LOGDIR_SEC SECTOR_OFFSET(171) + 5
|
|
#define DDP_LOGDIR_MAX 43
|
|
#define DDP_PICDIR_SEC SECTOR_OFFSET(180) + 5
|
|
#define DDP_PICDIR_MAX 30
|
|
#define DDP_VIEWDIR_SEC SECTOR_OFFSET(189) + 5
|
|
#define DDP_VIEWDIR_MAX 171
|
|
#define DDP_SNDDIR_SEC SECTOR_OFFSET(198) + 5
|
|
#define DDP_SNDDIR_MAX 64
|
|
|
|
#define BC_LOGDIR_SEC SECTOR_OFFSET(90) + 5
|
|
#define BC_LOGDIR_MAX 118
|
|
#define BC_VIEWDIR_SEC SECTOR_OFFSET(96) + 5
|
|
#define BC_VIEWDIR_MAX 180
|
|
#define BC_PICDIR_SEC SECTOR_OFFSET(93) + 8
|
|
#define BC_PICDIR_MAX 117
|
|
#define BC_SNDDIR_SEC SECTOR_OFFSET(99) + 5
|
|
#define BC_SNDDIR_MAX 29
|
|
#define BC_WORDS SECTOR_OFFSET(0x26D) + 5
|
|
#define BC_OBJECTS SECTOR_OFFSET(0x1E6) + 3
|
|
|
|
namespace Agi {
|
|
|
|
|
|
AgiLoader_v1::AgiLoader_v1(AgiEngine *vm) {
|
|
_vm = vm;
|
|
}
|
|
|
|
int AgiLoader_v1::detectGame() {
|
|
// Find filenames for the disk images
|
|
_filenameDisk0 = _vm->getDiskName(BooterDisk1);
|
|
_filenameDisk1 = _vm->getDiskName(BooterDisk2);
|
|
|
|
return errOK;
|
|
}
|
|
|
|
int AgiLoader_v1::loadDir_DDP(AgiDir *agid, int offset, int max) {
|
|
Common::File fp;
|
|
|
|
if (!fp.open(_filenameDisk0))
|
|
return errBadFileOpen;
|
|
|
|
// initialize directory entries to empty
|
|
for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++) {
|
|
agid[i].volume = 0xFF;
|
|
agid[i].offset = _EMPTY;
|
|
}
|
|
|
|
fp.seek(offset, SEEK_SET);
|
|
for (int i = 0; i <= max; i++) {
|
|
int b0 = fp.readByte();
|
|
int b1 = fp.readByte();
|
|
int b2 = fp.readByte();
|
|
|
|
if (b0 == 0xFF && b1 == 0xFF && b2 == 0xFF) {
|
|
agid[i].volume = 0xFF;
|
|
agid[i].offset = _EMPTY;
|
|
} else {
|
|
int sec = (DDP_BASE_SECTOR + (((b0 & 0xF) << 8) | b1)) >> 1;
|
|
int off = ((b1 & 0x1) << 8) | b2;
|
|
agid[i].volume = 0;
|
|
agid[i].offset = SECTOR_OFFSET(sec) + off;
|
|
}
|
|
}
|
|
|
|
return errOK;
|
|
}
|
|
|
|
int AgiLoader_v1::loadDir_BC(AgiDir *agid, int offset, int max) {
|
|
Common::File fp;
|
|
|
|
if (!fp.open(_filenameDisk0))
|
|
return errBadFileOpen;
|
|
|
|
// initialize directory entries to empty
|
|
for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++) {
|
|
agid[i].volume = 0xFF;
|
|
agid[i].offset = _EMPTY;
|
|
}
|
|
|
|
fp.seek(offset, SEEK_SET);
|
|
for (int i = 0; i <= max; i++) {
|
|
int b0 = fp.readByte();
|
|
int b1 = fp.readByte();
|
|
int b2 = fp.readByte();
|
|
|
|
if (b0 == 0xFF && b1 == 0xFF && b2 == 0xFF) {
|
|
agid[i].volume = 0xFF;
|
|
agid[i].offset = _EMPTY;
|
|
} else {
|
|
int sec = (b0 & 0x3F) * 18 + ((b1 >> 1) & 0x1) * 9 + ((b1 >> 2) & 0x1F) - 1;
|
|
int off = ((b1 & 0x1) << 8) | b2;
|
|
int vol = (b0 & 0xC0) >> 6;
|
|
agid[i].volume = 0;
|
|
agid[i].offset = (vol == 2) * IMAGE_SIZE + SECTOR_OFFSET(sec) + off;
|
|
}
|
|
}
|
|
|
|
return errOK;
|
|
}
|
|
|
|
int AgiLoader_v1::init() {
|
|
int ec = errOK;
|
|
|
|
switch (_vm->getGameID()) {
|
|
case GID_DDP:
|
|
ec = loadDir_DDP(_vm->_game.dirLogic, DDP_LOGDIR_SEC, DDP_LOGDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_DDP(_vm->_game.dirPic, DDP_PICDIR_SEC, DDP_PICDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_DDP(_vm->_game.dirView, DDP_VIEWDIR_SEC, DDP_VIEWDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_DDP(_vm->_game.dirSound, DDP_SNDDIR_SEC, DDP_SNDDIR_MAX);
|
|
break;
|
|
|
|
case GID_BC:
|
|
ec = loadDir_BC(_vm->_game.dirLogic, BC_LOGDIR_SEC, BC_LOGDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_BC(_vm->_game.dirPic, BC_PICDIR_SEC, BC_PICDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_BC(_vm->_game.dirView, BC_VIEWDIR_SEC, BC_VIEWDIR_MAX);
|
|
if (ec == errOK)
|
|
ec = loadDir_BC(_vm->_game.dirSound, BC_SNDDIR_SEC, BC_SNDDIR_MAX);
|
|
break;
|
|
|
|
default:
|
|
ec = errUnk;
|
|
break;
|
|
}
|
|
|
|
return ec;
|
|
}
|
|
|
|
uint8 *AgiLoader_v1::loadVolRes(struct AgiDir *agid) {
|
|
Common::File fp;
|
|
int offset = agid->offset;
|
|
|
|
if (offset == _EMPTY)
|
|
return nullptr;
|
|
|
|
if (offset > IMAGE_SIZE) {
|
|
if (!fp.open(_filenameDisk1)) {
|
|
warning("AgiLoader_v1::loadVolRes: could not open %s", _filenameDisk1.toString().c_str());
|
|
return nullptr;
|
|
}
|
|
offset -= IMAGE_SIZE;
|
|
} else {
|
|
if (!fp.open(_filenameDisk0)) {
|
|
warning("AgiLoader_v1::loadVolRes: could not open %s", _filenameDisk0.toString().c_str());
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
fp.seek(offset, SEEK_SET);
|
|
|
|
uint16 signature = fp.readUint16BE();
|
|
if (signature != 0x1234) {
|
|
warning("AgiLoader_v1::loadVolRes: bad signature %04x", signature);
|
|
return nullptr;
|
|
}
|
|
|
|
fp.readByte(); // volume number
|
|
agid->len = fp.readUint16LE();
|
|
uint8 *data = (uint8 *)calloc(1, agid->len + 32); // why the extra 32 bytes?
|
|
fp.read(data, agid->len);
|
|
|
|
return data;
|
|
}
|
|
|
|
int AgiLoader_v1::loadResource(int16 resourceType, int16 resourceNr) {
|
|
int ec = errOK;
|
|
uint8 *data = nullptr;
|
|
|
|
debugC(3, kDebugLevelResources, "(t = %d, n = %d)", resourceType, resourceNr);
|
|
if (resourceNr >= MAX_DIRECTORY_ENTRIES)
|
|
return errBadResource;
|
|
|
|
switch (resourceType) {
|
|
case RESOURCETYPE_LOGIC:
|
|
if (~_vm->_game.dirLogic[resourceNr].flags & RES_LOADED) {
|
|
debugC(3, kDebugLevelResources, "loading logic resource %d", resourceNr);
|
|
unloadResource(RESOURCETYPE_LOGIC, resourceNr);
|
|
|
|
// load raw resource into data
|
|
data = loadVolRes(&_vm->_game.dirLogic[resourceNr]);
|
|
|
|
_vm->_game.logics[resourceNr].data = data;
|
|
ec = data ? _vm->decodeLogic(resourceNr) : errBadResource;
|
|
|
|
_vm->_game.logics[resourceNr].sIP = 2;
|
|
}
|
|
|
|
// reset code pointer in case logic was cached
|
|
_vm->_game.logics[resourceNr].cIP = _vm->_game.logics[resourceNr].sIP;
|
|
break;
|
|
case RESOURCETYPE_PICTURE:
|
|
// if picture is currently NOT loaded *OR* cacheing is off,
|
|
// unload the resource (caching == off) and reload it
|
|
|
|
debugC(3, kDebugLevelResources, "loading picture resource %d", resourceNr);
|
|
if (_vm->_game.dirPic[resourceNr].flags & RES_LOADED)
|
|
break;
|
|
|
|
// if loaded but not cached, unload it
|
|
// if cached but not loaded, etc
|
|
unloadResource(RESOURCETYPE_PICTURE, resourceNr);
|
|
data = loadVolRes(&_vm->_game.dirPic[resourceNr]);
|
|
|
|
if (data != nullptr) {
|
|
_vm->_game.pictures[resourceNr].rdata = data;
|
|
_vm->_game.dirPic[resourceNr].flags |= RES_LOADED;
|
|
} else {
|
|
ec = errBadResource;
|
|
}
|
|
break;
|
|
case RESOURCETYPE_SOUND:
|
|
debugC(3, kDebugLevelResources, "loading sound resource %d", resourceNr);
|
|
if (_vm->_game.dirSound[resourceNr].flags & RES_LOADED)
|
|
break;
|
|
|
|
data = loadVolRes(&_vm->_game.dirSound[resourceNr]);
|
|
|
|
// "data" is freed by objects created by createFromRawResource on success
|
|
_vm->_game.sounds[resourceNr] = AgiSound::createFromRawResource(data, _vm->_game.dirSound[resourceNr].len, resourceNr, _vm->_soundemu);
|
|
if (_vm->_game.sounds[resourceNr] != nullptr) {
|
|
_vm->_game.dirSound[resourceNr].flags |= RES_LOADED;
|
|
} else {
|
|
free(data);
|
|
ec = errBadResource;
|
|
}
|
|
break;
|
|
case RESOURCETYPE_VIEW:
|
|
// Load a VIEW resource into memory...
|
|
// Since VIEWS alter the view table ALL the time
|
|
// can we cache the view? or must we reload it all
|
|
// the time?
|
|
if (_vm->_game.dirView[resourceNr].flags & RES_LOADED)
|
|
break;
|
|
|
|
debugC(3, kDebugLevelResources, "loading view resource %d", resourceNr);
|
|
unloadResource(RESOURCETYPE_VIEW, resourceNr);
|
|
data = loadVolRes(&_vm->_game.dirView[resourceNr]);
|
|
if (data) {
|
|
_vm->_game.dirView[resourceNr].flags |= RES_LOADED;
|
|
ec = _vm->decodeView(data, _vm->_game.dirView[resourceNr].len, resourceNr);
|
|
free(data);
|
|
} else {
|
|
ec = errBadResource;
|
|
}
|
|
break;
|
|
default:
|
|
ec = errBadResource;
|
|
break;
|
|
}
|
|
|
|
return ec;
|
|
}
|
|
|
|
void AgiLoader_v1::unloadResource(int16 resourceType, int16 resourceNr) {
|
|
switch (resourceType) {
|
|
case RESOURCETYPE_LOGIC:
|
|
_vm->unloadLogic(resourceNr);
|
|
break;
|
|
case RESOURCETYPE_PICTURE:
|
|
_vm->_picture->unloadPicture(resourceNr);
|
|
break;
|
|
case RESOURCETYPE_VIEW:
|
|
_vm->unloadView(resourceNr);
|
|
break;
|
|
case RESOURCETYPE_SOUND:
|
|
_vm->_sound->unloadSound(resourceNr);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
int AgiLoader_v1::loadObjects(const char *fname) {
|
|
if (_vm->getGameID() == GID_BC) {
|
|
Common::File f;
|
|
f.open(_filenameDisk0);
|
|
f.seek(BC_OBJECTS, SEEK_SET);
|
|
return _vm->loadObjects(f);
|
|
}
|
|
return errOK;
|
|
}
|
|
|
|
int AgiLoader_v1::loadWords(const char *fname) {
|
|
if (_vm->getGameID() == GID_BC) {
|
|
Common::File f;
|
|
f.open(_filenameDisk0);
|
|
f.seek(BC_WORDS, SEEK_SET);
|
|
return _vm->_words->loadDictionary_v1(f);
|
|
}
|
|
return errOK;
|
|
}
|
|
|
|
}
|