2010-08-17 09:28:20 +00:00
|
|
|
/* 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code is based on original Hugo Trilogy source code
|
|
|
|
*
|
|
|
|
* Copyright (c) 1989-1995 David P. Gray
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/system.h"
|
|
|
|
#include "common/savefile.h"
|
2010-11-29 17:42:08 +00:00
|
|
|
#include "common/config-manager.h"
|
|
|
|
#include "graphics/thumbnail.h"
|
|
|
|
#include "gui/saveload.h"
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
#include "hugo/hugo.h"
|
|
|
|
#include "hugo/file.h"
|
|
|
|
#include "hugo/schedule.h"
|
|
|
|
#include "hugo/display.h"
|
|
|
|
#include "hugo/util.h"
|
2010-10-21 17:09:57 +00:00
|
|
|
#include "hugo/object.h"
|
2011-01-25 00:32:48 +00:00
|
|
|
#include "hugo/text.h"
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
namespace Hugo {
|
2010-10-21 17:09:57 +00:00
|
|
|
FileManager::FileManager(HugoEngine *vm) : _vm(vm) {
|
2011-02-03 18:25:38 +00:00
|
|
|
has_read_header = false;
|
|
|
|
firstUIFFl = true;
|
2010-09-05 20:37:38 +00:00
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-09-05 20:37:38 +00:00
|
|
|
FileManager::~FileManager() {
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Convert 4 planes (RGBI) data to 8-bit DIB format
|
|
|
|
* Return original plane data ptr
|
|
|
|
*/
|
2011-02-03 18:25:38 +00:00
|
|
|
byte *FileManager::convertPCC(byte *p, const uint16 y, const uint16 bpl, image_pt dataPtr) const {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(2, kDebugFile, "convertPCC(byte *p, %d, %d, image_pt data_p)", y, bpl);
|
|
|
|
|
|
|
|
dataPtr += y * bpl * 8; // Point to correct DIB line
|
2010-10-03 08:08:42 +00:00
|
|
|
for (int16 r = 0, g = bpl, b = g + bpl, i = b + bpl; r < bpl; r++, g++, b++, i++) { // Each byte in all planes
|
|
|
|
for (int8 bit = 7; bit >= 0; bit--) { // Each bit in byte
|
2010-08-17 09:28:20 +00:00
|
|
|
*dataPtr++ = (((p[r] >> bit & 1) << 0) |
|
|
|
|
((p[g] >> bit & 1) << 1) |
|
|
|
|
((p[b] >> bit & 1) << 2) |
|
|
|
|
((p[i] >> bit & 1) << 3));
|
2010-10-03 08:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read a pcx file of length len. Use supplied seq_p and image_p or
|
|
|
|
* allocate space if NULL. Name used for errors. Returns address of seq_p
|
|
|
|
* Set first TRUE to initialize b_index (i.e. not reading a sequential image in file).
|
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
seq_t *FileManager::readPCX(Common::File &f, seq_t *seqPtr, byte *imagePtr, const bool firstFl, const char *name) {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(1, kDebugFile, "readPCX(..., %s)", name);
|
|
|
|
|
|
|
|
// Read in the PCC header and check consistency
|
|
|
|
PCC_header.mfctr = f.readByte();
|
|
|
|
PCC_header.vers = f.readByte();
|
|
|
|
PCC_header.enc = f.readByte();
|
|
|
|
PCC_header.bpx = f.readByte();
|
|
|
|
PCC_header.x1 = f.readUint16LE();
|
|
|
|
PCC_header.y1 = f.readUint16LE();
|
|
|
|
PCC_header.x2 = f.readUint16LE();
|
|
|
|
PCC_header.y2 = f.readUint16LE();
|
|
|
|
PCC_header.xres = f.readUint16LE();
|
|
|
|
PCC_header.yres = f.readUint16LE();
|
|
|
|
f.read(PCC_header.palette, sizeof(PCC_header.palette));
|
|
|
|
PCC_header.vmode = f.readByte();
|
|
|
|
PCC_header.planes = f.readByte();
|
|
|
|
PCC_header.bytesPerLine = f.readUint16LE();
|
|
|
|
f.read(PCC_header.fill2, sizeof(PCC_header.fill2));
|
|
|
|
|
|
|
|
if (PCC_header.mfctr != 10)
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Bad data file format: %s", name);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-03 08:08:42 +00:00
|
|
|
// Allocate memory for seq_t if 0
|
|
|
|
if (seqPtr == 0) {
|
|
|
|
if ((seqPtr = (seq_t *)malloc(sizeof(seq_t))) == 0)
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Insufficient memory to run game.");
|
2010-10-03 08:08:42 +00:00
|
|
|
}
|
2010-10-25 13:31:01 +00:00
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
// Find size of image data in 8-bit DIB format
|
|
|
|
// Note save of x2 - marks end of valid data before garbage
|
2010-10-03 08:08:42 +00:00
|
|
|
uint16 bytesPerLine4 = PCC_header.bytesPerLine * 4; // 4-bit bpl
|
2010-08-17 09:28:20 +00:00
|
|
|
seqPtr->bytesPerLine8 = bytesPerLine4 * 2; // 8-bit bpl
|
|
|
|
seqPtr->lines = PCC_header.y2 - PCC_header.y1 + 1;
|
|
|
|
seqPtr->x2 = PCC_header.x2 - PCC_header.x1 + 1;
|
2010-10-03 08:08:42 +00:00
|
|
|
// Size of the image
|
|
|
|
uint16 size = seqPtr->lines * seqPtr->bytesPerLine8;
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Allocate memory for image data if NULL
|
2011-02-02 21:12:51 +00:00
|
|
|
if (imagePtr == 0)
|
2010-11-11 10:36:10 +00:00
|
|
|
imagePtr = (byte *)malloc((size_t) size);
|
2011-02-02 21:12:51 +00:00
|
|
|
|
2010-11-11 10:36:10 +00:00
|
|
|
assert(imagePtr);
|
2010-10-03 08:08:42 +00:00
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
seqPtr->imagePtr = imagePtr;
|
|
|
|
|
|
|
|
// Process the image data, converting to 8-bit DIB format
|
2010-10-03 08:08:42 +00:00
|
|
|
uint16 y = 0; // Current line index
|
2011-01-23 00:05:52 +00:00
|
|
|
byte pline[kXPix]; // Hold 4 planes of data
|
2010-10-03 08:08:42 +00:00
|
|
|
byte *p = pline; // Ptr to above
|
2010-08-17 09:28:20 +00:00
|
|
|
while (y < seqPtr->lines) {
|
2010-10-03 08:08:42 +00:00
|
|
|
byte c = f.readByte();
|
2011-01-23 00:05:52 +00:00
|
|
|
if ((c & kRepeatMask) == kRepeatMask) {
|
2010-10-03 08:08:42 +00:00
|
|
|
byte d = f.readByte(); // Read data byte
|
2011-01-23 00:05:52 +00:00
|
|
|
for (int i = 0; i < (c & kLengthMask); i++) {
|
2010-08-17 09:28:20 +00:00
|
|
|
*p++ = d;
|
|
|
|
if ((uint16)(p - pline) == bytesPerLine4)
|
|
|
|
p = convertPCC(pline, y++, PCC_header.bytesPerLine, imagePtr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*p++ = c;
|
|
|
|
if ((uint16)(p - pline) == bytesPerLine4)
|
|
|
|
p = convertPCC(pline, y++, PCC_header.bytesPerLine, imagePtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return seqPtr;
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read object file of PCC images into object supplied
|
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
void FileManager::readImage(const int objNum, object_t *objPtr) {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(1, kDebugFile, "readImage(%d, object_t *objPtr)", objNum);
|
|
|
|
|
2010-11-22 19:50:57 +00:00
|
|
|
/**
|
|
|
|
* Structure of object file lookup entry
|
|
|
|
*/
|
|
|
|
struct objBlock_t {
|
|
|
|
uint32 objOffset;
|
|
|
|
uint32 objLength;
|
|
|
|
};
|
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
if (!objPtr->seqNumb) // This object has no images
|
|
|
|
return;
|
|
|
|
|
2010-10-21 17:09:57 +00:00
|
|
|
if (_vm->isPacked()) {
|
2010-08-17 09:28:20 +00:00
|
|
|
_objectsArchive.seek((uint32)objNum * sizeof(objBlock_t), SEEK_SET);
|
|
|
|
|
2010-10-03 08:08:42 +00:00
|
|
|
objBlock_t objBlock; // Info on file within database
|
2010-08-17 09:28:20 +00:00
|
|
|
objBlock.objOffset = _objectsArchive.readUint32LE();
|
|
|
|
objBlock.objLength = _objectsArchive.readUint32LE();
|
|
|
|
|
|
|
|
_objectsArchive.seek(objBlock.objOffset, SEEK_SET);
|
|
|
|
} else {
|
2011-01-30 23:08:05 +00:00
|
|
|
Common::String buf;
|
2011-01-31 18:22:20 +00:00
|
|
|
buf = _vm->_picDir + Common::String(_vm->_text->getNoun(objPtr->nounIndex, 0)) + ".PIX";
|
2010-08-17 09:28:20 +00:00
|
|
|
if (!_objectsArchive.open(buf)) {
|
2011-01-31 18:22:20 +00:00
|
|
|
buf = Common::String(_vm->_text->getNoun(objPtr->nounIndex, 0)) + ".PIX";
|
2010-08-17 09:28:20 +00:00
|
|
|
if (!_objectsArchive.open(buf))
|
2011-01-30 23:19:30 +00:00
|
|
|
error("File not found: %s", buf.c_str());
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 18:25:38 +00:00
|
|
|
bool firstImgFl = true; // Initializes pcx read function
|
2010-10-03 08:08:42 +00:00
|
|
|
seq_t *seqPtr = 0; // Ptr to sequence structure
|
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
// Now read the images into an images list
|
2010-10-03 08:08:42 +00:00
|
|
|
for (int j = 0; j < objPtr->seqNumb; j++) { // for each sequence
|
|
|
|
for (int k = 0; k < objPtr->seqList[j].imageNbr; k++) { // each image
|
2010-08-17 09:28:20 +00:00
|
|
|
if (k == 0) { // First image
|
|
|
|
// Read this image - allocate both seq and image memory
|
2011-02-03 18:25:38 +00:00
|
|
|
seqPtr = readPCX(_objectsArchive, 0, 0, firstImgFl, _vm->_text->getNoun(objPtr->nounIndex, 0));
|
2010-08-17 09:28:20 +00:00
|
|
|
objPtr->seqList[j].seqPtr = seqPtr;
|
2011-02-03 18:25:38 +00:00
|
|
|
firstImgFl = false;
|
2010-08-17 09:28:20 +00:00
|
|
|
} else { // Subsequent image
|
|
|
|
// Read this image - allocate both seq and image memory
|
2011-02-03 18:25:38 +00:00
|
|
|
seqPtr->nextSeqPtr = readPCX(_objectsArchive, 0, 0, firstImgFl, _vm->_text->getNoun(objPtr->nounIndex, 0));
|
2010-08-17 09:28:20 +00:00
|
|
|
seqPtr = seqPtr->nextSeqPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the bounding box - x1, x2, y1, y2
|
|
|
|
// Note use of x2 - marks end of valid data in row
|
2010-10-03 08:08:42 +00:00
|
|
|
uint16 x2 = seqPtr->x2;
|
2010-08-17 09:28:20 +00:00
|
|
|
seqPtr->x1 = seqPtr->x2;
|
|
|
|
seqPtr->x2 = 0;
|
|
|
|
seqPtr->y1 = seqPtr->lines;
|
|
|
|
seqPtr->y2 = 0;
|
2010-10-03 08:08:42 +00:00
|
|
|
|
|
|
|
image_pt dibPtr = seqPtr->imagePtr;
|
|
|
|
for (int y = 0; y < seqPtr->lines; y++, dibPtr += seqPtr->bytesPerLine8 - x2) {
|
|
|
|
for (int x = 0; x < x2; x++) {
|
|
|
|
if (*dibPtr++) { // Some data found
|
2010-08-17 09:28:20 +00:00
|
|
|
if (x < seqPtr->x1)
|
|
|
|
seqPtr->x1 = x;
|
|
|
|
if (x > seqPtr->x2)
|
|
|
|
seqPtr->x2 = x;
|
|
|
|
if (y < seqPtr->y1)
|
|
|
|
seqPtr->y1 = y;
|
|
|
|
if (y > seqPtr->y2)
|
|
|
|
seqPtr->y2 = y;
|
|
|
|
}
|
2010-10-03 08:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
2010-10-15 06:08:42 +00:00
|
|
|
assert(seqPtr);
|
2010-08-17 09:28:20 +00:00
|
|
|
seqPtr->nextSeqPtr = objPtr->seqList[j].seqPtr; // loop linked list to head
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current image sequence to first or last
|
|
|
|
switch (objPtr->cycling) {
|
2011-01-23 00:05:52 +00:00
|
|
|
case kCycleInvisible: // (May become visible later)
|
|
|
|
case kCycleAlmostInvisible:
|
|
|
|
case kCycleNotCycling:
|
|
|
|
case kCycleForward:
|
2010-08-17 09:28:20 +00:00
|
|
|
objPtr->currImagePtr = objPtr->seqList[0].seqPtr;
|
|
|
|
break;
|
2011-01-23 00:05:52 +00:00
|
|
|
case kCycleBackward:
|
2010-08-17 09:28:20 +00:00
|
|
|
objPtr->currImagePtr = seqPtr;
|
|
|
|
break;
|
2010-09-16 23:03:03 +00:00
|
|
|
default:
|
|
|
|
warning("Unexpected cycling: %d", objPtr->cycling);
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 17:09:57 +00:00
|
|
|
if (!_vm->isPacked())
|
2010-08-17 09:28:20 +00:00
|
|
|
_objectsArchive.close();
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read sound (or music) file data. Call with SILENCE to free-up
|
|
|
|
* any allocated memory. Also returns size of data
|
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
sound_pt FileManager::getSound(const int16 sound, uint16 *size) {
|
2010-12-05 21:52:42 +00:00
|
|
|
debugC(1, kDebugFile, "getSound(%d)", sound);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// No more to do if SILENCE (called for cleanup purposes)
|
2010-10-21 17:09:57 +00:00
|
|
|
if (sound == _vm->_soundSilence)
|
2010-10-03 08:08:42 +00:00
|
|
|
return 0;
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Open sounds file
|
2010-10-03 08:08:42 +00:00
|
|
|
Common::File fp; // Handle to SOUND_FILE
|
2011-01-23 22:51:12 +00:00
|
|
|
if (!fp.open(getSoundFilename())) {
|
|
|
|
warning("Hugo Error: File not found %s", getSoundFilename());
|
2010-10-03 08:08:42 +00:00
|
|
|
return 0;
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_read_header) {
|
|
|
|
if (fp.read(s_hdr, sizeof(s_hdr)) != sizeof(s_hdr))
|
2011-01-23 22:51:12 +00:00
|
|
|
error("Wrong sound file format");
|
2010-08-17 09:28:20 +00:00
|
|
|
has_read_header = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = s_hdr[sound].size;
|
|
|
|
if (*size == 0)
|
2011-01-23 22:51:12 +00:00
|
|
|
error("Wrong sound file format or missing sound %d", sound);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Allocate memory for sound or music, if possible
|
2010-10-03 08:08:42 +00:00
|
|
|
sound_pt soundPtr = (byte *)malloc(s_hdr[sound].size); // Ptr to sound data
|
2010-11-11 10:36:10 +00:00
|
|
|
assert(soundPtr);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Seek to data and read it
|
|
|
|
fp.seek(s_hdr[sound].offset, SEEK_SET);
|
|
|
|
if (fp.read(soundPtr, s_hdr[sound].size) != s_hdr[sound].size)
|
2011-01-23 22:51:12 +00:00
|
|
|
error("Wrong sound file format");
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
return soundPtr;
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Return whether file exists or not
|
|
|
|
*/
|
2011-01-31 18:22:20 +00:00
|
|
|
bool FileManager::fileExists(const Common::String filename) const {
|
2010-08-17 09:28:20 +00:00
|
|
|
Common::File f;
|
2010-11-19 23:49:04 +00:00
|
|
|
return(f.exists(filename));
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
2010-11-26 23:17:16 +00:00
|
|
|
* Save game to supplied slot
|
2010-11-01 20:20:21 +00:00
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
bool FileManager::saveGame(const int16 slot, const Common::String descrip) {
|
2010-11-29 18:03:05 +00:00
|
|
|
debugC(1, kDebugFile, "saveGame(%d, %s)", slot, descrip.c_str());
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-29 17:42:08 +00:00
|
|
|
const EnginePlugin *plugin = NULL;
|
|
|
|
int16 savegameId;
|
|
|
|
Common::String savegameDescription;
|
|
|
|
EngineMan.findGame(_vm->getGameId(), &plugin);
|
|
|
|
|
|
|
|
if (slot == -1) {
|
|
|
|
GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Save game:", "Save");
|
|
|
|
dialog->setSaveMode(true);
|
2011-02-07 22:58:22 +00:00
|
|
|
savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName());
|
2010-11-29 17:42:08 +00:00
|
|
|
savegameDescription = dialog->getResultString();
|
|
|
|
delete dialog;
|
|
|
|
} else {
|
|
|
|
savegameId = slot;
|
|
|
|
if (!descrip.empty()) {
|
|
|
|
savegameDescription = descrip;
|
|
|
|
} else {
|
|
|
|
savegameDescription = Common::String::format("Quick save #%d", slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savegameId < 0) // dialog aborted
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Common::String savegameFile = Common::String::format(_vm->_saveFilename.c_str(), savegameId);
|
|
|
|
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
|
|
|
|
Common::OutSaveFile *out = saveMan->openForSaving(savegameFile);
|
|
|
|
|
2010-10-10 23:24:57 +00:00
|
|
|
if (!out) {
|
2010-11-29 17:42:08 +00:00
|
|
|
warning("Can't create file '%s', game not saved", savegameFile.c_str());
|
|
|
|
return false;
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write version. We can't restore from obsolete versions
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte(kSavegameVersion);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-29 17:42:08 +00:00
|
|
|
if (savegameDescription == "") {
|
|
|
|
savegameDescription = "Untitled savegame";
|
|
|
|
}
|
|
|
|
|
|
|
|
out->writeSint16BE(savegameDescription.size() + 1);
|
|
|
|
out->write(savegameDescription.c_str(), savegameDescription.size() + 1);
|
|
|
|
|
|
|
|
Graphics::saveThumbnail(*out);
|
|
|
|
|
|
|
|
TimeDate curTime;
|
|
|
|
_vm->_system->getTimeAndDate(curTime);
|
|
|
|
|
|
|
|
uint32 saveDate = (curTime.tm_mday & 0xFF) << 24 | ((curTime.tm_mon + 1) & 0xFF) << 16 | ((curTime.tm_year + 1900) & 0xFFFF);
|
|
|
|
uint16 saveTime = (curTime.tm_hour & 0xFF) << 8 | ((curTime.tm_min) & 0xFF);
|
|
|
|
|
|
|
|
out->writeUint32BE(saveDate);
|
|
|
|
out->writeUint16BE(saveTime);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-11 23:04:04 +00:00
|
|
|
_vm->_object->saveObjects(out);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-21 17:09:57 +00:00
|
|
|
const status_t &gameStatus = _vm->getGameStatus();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save whether hero image is swapped
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte(_vm->_heroImage);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save score
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeSint16BE(_vm->getScore());
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save story mode
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte((gameStatus.storyModeFl) ? 1 : 0);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save jumpexit mode
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte((gameStatus.jumpExitFl) ? 1 : 0);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save gameover status
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte((gameStatus.gameOverFl) ? 1 : 0);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save screen states
|
2010-11-19 23:49:04 +00:00
|
|
|
for (int i = 0; i < _vm->_numScreens; i++)
|
|
|
|
out->writeByte(_vm->_screenStates[i]);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save points table
|
2010-11-19 23:49:04 +00:00
|
|
|
for (int i = 0; i < _vm->_numBonuses; i++) {
|
|
|
|
out->writeByte(_vm->_points[i].score);
|
|
|
|
out->writeByte((_vm->_points[i].scoredFl) ? 1 : 0);
|
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Now save current time and all current events in event queue
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->_scheduler->saveEvents(out);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2011-01-26 00:21:54 +00:00
|
|
|
// Now save current actions
|
|
|
|
_vm->_scheduler->saveActions(out);
|
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
// Save palette table
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->_screen->savePal(out);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Save maze status
|
2010-11-19 23:49:04 +00:00
|
|
|
out->writeByte((_maze.enabledFl) ? 1 : 0);
|
|
|
|
out->writeByte(_maze.size);
|
|
|
|
out->writeSint16BE(_maze.x1);
|
|
|
|
out->writeSint16BE(_maze.y1);
|
|
|
|
out->writeSint16BE(_maze.x2);
|
|
|
|
out->writeSint16BE(_maze.y2);
|
|
|
|
out->writeSint16BE(_maze.x3);
|
|
|
|
out->writeSint16BE(_maze.x4);
|
|
|
|
out->writeByte(_maze.firstScreenIndex);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
out->finalize();
|
|
|
|
|
|
|
|
delete out;
|
2010-11-29 17:42:08 +00:00
|
|
|
|
|
|
|
return true;
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
2010-11-26 23:17:16 +00:00
|
|
|
* Restore game from supplied slot number
|
2010-11-01 20:20:21 +00:00
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
bool FileManager::restoreGame(const int16 slot) {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(1, kDebugFile, "restoreGame(%d)", slot);
|
|
|
|
|
2010-11-29 17:42:08 +00:00
|
|
|
const EnginePlugin *plugin = NULL;
|
|
|
|
int16 savegameId;
|
|
|
|
EngineMan.findGame(_vm->getGameId(), &plugin);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-29 17:42:08 +00:00
|
|
|
if (slot == -1) {
|
|
|
|
GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Restore game:", "Restore");
|
|
|
|
dialog->setSaveMode(false);
|
2011-02-07 22:58:22 +00:00
|
|
|
savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName());
|
2010-11-29 17:42:08 +00:00
|
|
|
delete dialog;
|
|
|
|
} else {
|
|
|
|
savegameId = slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savegameId < 0) // dialog aborted
|
|
|
|
return false;
|
2010-10-10 23:24:57 +00:00
|
|
|
|
2010-11-29 17:42:08 +00:00
|
|
|
Common::String savegameFile = Common::String::format(_vm->_saveFilename.c_str(), savegameId);
|
|
|
|
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
|
|
|
|
Common::InSaveFile *in = saveMan->openForLoading(savegameFile);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-10 23:24:57 +00:00
|
|
|
if (!in)
|
2010-11-29 17:42:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Initialize new-game status
|
|
|
|
_vm->initStatus();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Check version, can't restore from different versions
|
2010-11-19 23:49:04 +00:00
|
|
|
int saveVersion = in->readByte();
|
2010-08-17 09:28:20 +00:00
|
|
|
if (saveVersion != kSavegameVersion) {
|
2010-11-29 17:42:08 +00:00
|
|
|
warning("Savegame of incompatible version");
|
|
|
|
delete in;
|
|
|
|
return false;
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over description
|
2010-11-29 17:42:08 +00:00
|
|
|
int32 saveGameNameSize = in->readSint16BE();
|
|
|
|
in->skip(saveGameNameSize);
|
|
|
|
|
|
|
|
Graphics::skipThumbnail(*in);
|
|
|
|
|
|
|
|
in->skip(6); // Skip date & time
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// If hero image is currently swapped, swap it back before restore
|
2011-01-23 00:05:52 +00:00
|
|
|
if (_vm->_heroImage != kHeroIndex)
|
|
|
|
_vm->_object->swapImages(kHeroIndex, _vm->_heroImage);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-11 23:04:04 +00:00
|
|
|
_vm->_object->restoreObjects(in);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-19 23:49:04 +00:00
|
|
|
_vm->_heroImage = in->readByte();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// If hero swapped in saved game, swap it
|
2010-11-19 23:49:04 +00:00
|
|
|
byte heroImg = _vm->_heroImage;
|
2011-01-23 00:05:52 +00:00
|
|
|
if (heroImg != kHeroIndex)
|
|
|
|
_vm->_object->swapImages(kHeroIndex, _vm->_heroImage);
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->_heroImage = heroImg;
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-21 17:09:57 +00:00
|
|
|
status_t &gameStatus = _vm->getGameStatus();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-22 22:49:24 +00:00
|
|
|
int score = in->readSint16BE();
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->setScore(score);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-19 23:49:04 +00:00
|
|
|
gameStatus.storyModeFl = (in->readByte() == 1);
|
|
|
|
gameStatus.jumpExitFl = (in->readByte() == 1);
|
|
|
|
gameStatus.gameOverFl = (in->readByte() == 1);
|
|
|
|
for (int i = 0; i < _vm->_numScreens; i++)
|
|
|
|
_vm->_screenStates[i] = in->readByte();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Restore points table
|
2010-11-19 23:49:04 +00:00
|
|
|
for (int i = 0; i < _vm->_numBonuses; i++) {
|
|
|
|
_vm->_points[i].score = in->readByte();
|
|
|
|
_vm->_points[i].scoredFl = (in->readByte() == 1);
|
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-11-11 23:04:04 +00:00
|
|
|
_vm->_object->restoreAllSeq();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Now restore time of the save and the event queue
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->_scheduler->restoreEvents(in);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2011-01-26 00:21:54 +00:00
|
|
|
// Now restore actions
|
|
|
|
_vm->_scheduler->restoreActions(in);
|
|
|
|
|
2010-08-17 09:28:20 +00:00
|
|
|
// Restore palette and change it if necessary
|
2010-10-21 17:09:57 +00:00
|
|
|
_vm->_screen->restorePal(in);
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Restore maze status
|
2010-11-19 23:49:04 +00:00
|
|
|
_maze.enabledFl = (in->readByte() == 1);
|
|
|
|
_maze.size = in->readByte();
|
|
|
|
_maze.x1 = in->readSint16BE();
|
|
|
|
_maze.y1 = in->readSint16BE();
|
|
|
|
_maze.x2 = in->readSint16BE();
|
|
|
|
_maze.y2 = in->readSint16BE();
|
|
|
|
_maze.x3 = in->readSint16BE();
|
|
|
|
_maze.x4 = in->readSint16BE();
|
|
|
|
_maze.firstScreenIndex = in->readByte();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
delete in;
|
2010-11-29 17:42:08 +00:00
|
|
|
return true;
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read the encrypted text from the boot file and print it
|
|
|
|
*/
|
2010-08-17 09:28:20 +00:00
|
|
|
void FileManager::printBootText() {
|
2011-01-23 22:51:12 +00:00
|
|
|
debugC(1, kDebugFile, "printBootText()");
|
2011-01-23 00:05:52 +00:00
|
|
|
static const char *cypher = getBootCypher();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-03 08:08:42 +00:00
|
|
|
Common::File ofp;
|
2011-01-23 22:51:12 +00:00
|
|
|
if (!ofp.open(getBootFilename())) {
|
2011-01-18 18:26:33 +00:00
|
|
|
if (_vm->_gameVariant == kGameVariantH1Dos) {
|
2010-08-18 15:33:59 +00:00
|
|
|
//TODO initialize properly _boot structure
|
|
|
|
warning("printBootText - Skipping as H1 Dos may be a freeware");
|
|
|
|
return;
|
2010-10-03 08:08:42 +00:00
|
|
|
} else {
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Missing startup file");
|
2010-10-03 08:08:42 +00:00
|
|
|
}
|
2010-08-18 17:51:44 +00:00
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Allocate space for the text and print it
|
2010-10-03 08:08:42 +00:00
|
|
|
char *buf = (char *)malloc(_boot.exit_len + 1);
|
2010-08-17 09:28:20 +00:00
|
|
|
if (buf) {
|
|
|
|
// Skip over the boot structure (already read) and read exit text
|
|
|
|
ofp.seek((long)sizeof(_boot), SEEK_SET);
|
|
|
|
if (ofp.read(buf, _boot.exit_len) != (size_t)_boot.exit_len)
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Error while reading startup file");
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Decrypt the exit text, using CRYPT substring
|
2010-10-03 08:08:42 +00:00
|
|
|
int i;
|
2010-08-17 09:28:20 +00:00
|
|
|
for (i = 0; i < _boot.exit_len; i++)
|
2011-01-23 00:05:52 +00:00
|
|
|
buf[i] ^= cypher[i % strlen(cypher)];
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
buf[i] = '\0';
|
2011-01-23 00:05:52 +00:00
|
|
|
Utils::Box(kBoxOk, "%s", buf);
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
ofp.close();
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Reads boot file for program environment. Fatal error if not there or
|
|
|
|
* file checksum is bad. De-crypts structure while checking checksum
|
|
|
|
*/
|
2010-08-17 09:28:20 +00:00
|
|
|
void FileManager::readBootFile() {
|
2011-01-23 22:51:12 +00:00
|
|
|
debugC(1, kDebugFile, "readBootFile()");
|
2011-01-23 00:05:52 +00:00
|
|
|
static const char *cypher = getBootCypher();
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2010-10-03 08:08:42 +00:00
|
|
|
Common::File ofp;
|
2011-01-23 22:51:12 +00:00
|
|
|
if (!ofp.open(getBootFilename())) {
|
2011-01-18 18:26:33 +00:00
|
|
|
if (_vm->_gameVariant == kGameVariantH1Dos) {
|
2010-08-18 15:33:59 +00:00
|
|
|
//TODO initialize properly _boot structure
|
|
|
|
warning("readBootFile - Skipping as H1 Dos may be a freeware");
|
|
|
|
return;
|
2010-10-03 08:08:42 +00:00
|
|
|
} else {
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Missing startup file");
|
2010-10-03 08:08:42 +00:00
|
|
|
}
|
2010-08-18 17:51:44 +00:00
|
|
|
}
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
if (ofp.size() < (int32)sizeof(_boot))
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Corrupted startup file");
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
_boot.checksum = ofp.readByte();
|
|
|
|
_boot.registered = ofp.readByte();
|
|
|
|
ofp.read(_boot.pbswitch, sizeof(_boot.pbswitch));
|
|
|
|
ofp.read(_boot.distrib, sizeof(_boot.distrib));
|
|
|
|
_boot.exit_len = ofp.readUint16LE();
|
|
|
|
|
2010-10-03 08:08:42 +00:00
|
|
|
byte *p = (byte *)&_boot;
|
|
|
|
|
|
|
|
byte checksum = 0;
|
|
|
|
for (uint32 i = 0; i < sizeof(_boot); i++) {
|
2010-08-17 09:28:20 +00:00
|
|
|
checksum ^= p[i];
|
2011-01-23 00:05:52 +00:00
|
|
|
p[i] ^= cypher[i % strlen(cypher)];
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
ofp.close();
|
|
|
|
|
|
|
|
if (checksum)
|
2010-11-11 10:36:10 +00:00
|
|
|
error("Corrupted startup file");
|
2010-08-17 09:28:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Returns address of uif_hdr[id], reading it in if first call
|
2011-01-23 22:51:12 +00:00
|
|
|
* This file contains, between others, the bitmaps of the fonts used in the application
|
|
|
|
* UIF means User interface database (Windows Only)
|
2010-11-01 20:20:21 +00:00
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
uif_hdr_t *FileManager::getUIFHeader(const uif_t id) {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(1, kDebugFile, "getUIFHeader(%d)", id);
|
|
|
|
|
|
|
|
// Initialize offset lookup if not read yet
|
2011-02-03 18:25:38 +00:00
|
|
|
if (firstUIFFl) {
|
|
|
|
firstUIFFl = false;
|
2010-08-17 09:28:20 +00:00
|
|
|
// Open unbuffered to do far read
|
2010-10-03 08:08:42 +00:00
|
|
|
Common::File ip; // Image data file
|
2011-01-23 22:51:12 +00:00
|
|
|
if (!ip.open(getUifFilename()))
|
|
|
|
error("File not found: %s", getUifFilename());
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
if (ip.size() < (int32)sizeof(UIFHeader))
|
2011-01-23 22:51:12 +00:00
|
|
|
error("Wrong UIF file format");
|
2010-08-17 09:28:20 +00:00
|
|
|
|
2011-01-23 00:05:52 +00:00
|
|
|
for (int i = 0; i < kMaxUifs; ++i) {
|
2010-08-17 09:28:20 +00:00
|
|
|
UIFHeader[i].size = ip.readUint16LE();
|
|
|
|
UIFHeader[i].offset = ip.readUint32LE();
|
|
|
|
}
|
|
|
|
|
|
|
|
ip.close();
|
|
|
|
}
|
|
|
|
return &UIFHeader[id];
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read uif item into supplied buffer.
|
|
|
|
*/
|
2011-02-02 21:12:51 +00:00
|
|
|
void FileManager::readUIFItem(const int16 id, byte *buf) {
|
2010-08-17 09:28:20 +00:00
|
|
|
debugC(1, kDebugFile, "readUIFItem(%d, ...)", id);
|
|
|
|
|
|
|
|
// Open uif file to read data
|
2010-10-03 08:08:42 +00:00
|
|
|
Common::File ip; // UIF_FILE handle
|
2011-01-23 22:51:12 +00:00
|
|
|
if (!ip.open(getUifFilename()))
|
|
|
|
error("File not found: %s", getUifFilename());
|
2010-08-17 09:28:20 +00:00
|
|
|
|
|
|
|
// Seek to data
|
2010-10-03 08:08:42 +00:00
|
|
|
uif_hdr_t *UIFHeaderPtr = getUIFHeader((uif_t)id);
|
2010-08-17 09:28:20 +00:00
|
|
|
ip.seek(UIFHeaderPtr->offset, SEEK_SET);
|
|
|
|
|
|
|
|
// We support pcx images and straight data
|
2010-11-09 20:26:12 +00:00
|
|
|
seq_t *dummySeq; // Dummy seq_t for image data
|
2010-08-17 09:28:20 +00:00
|
|
|
switch (id) {
|
|
|
|
case UIF_IMAGES: // Read uif images file
|
2011-01-23 22:51:12 +00:00
|
|
|
dummySeq = readPCX(ip, 0, buf, true, getUifFilename());
|
2010-11-09 20:26:12 +00:00
|
|
|
free(dummySeq);
|
2010-08-17 09:28:20 +00:00
|
|
|
break;
|
|
|
|
default: // Read file data into supplied array
|
|
|
|
if (ip.read(buf, UIFHeaderPtr->size) != UIFHeaderPtr->size)
|
2011-01-23 22:51:12 +00:00
|
|
|
error("Wrong UIF file format");
|
2010-08-17 09:28:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip.close();
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* Read the uif image file (inventory icons)
|
|
|
|
*/
|
2010-10-21 17:09:57 +00:00
|
|
|
void FileManager::readUIFImages() {
|
2011-01-23 22:51:12 +00:00
|
|
|
debugC(1, kDebugFile, "readUIFImages()");
|
2010-10-21 17:09:57 +00:00
|
|
|
|
|
|
|
readUIFItem(UIF_IMAGES, _vm->_screen->getGUIBuffer()); // Read all uif images
|
|
|
|
}
|
|
|
|
|
2011-02-02 21:12:51 +00:00
|
|
|
const char *FileManager::getBootCypher() const {
|
2011-01-23 00:05:52 +00:00
|
|
|
return "Copyright 1992, David P Gray, Gray Design Associates";
|
|
|
|
}
|
2010-08-27 09:48:53 +00:00
|
|
|
} // End of namespace Hugo
|
2010-09-05 20:37:38 +00:00
|
|
|
|