2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-02-22 22:40:53 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2006-02-22 22:40:53 +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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-12-07 20:45:51 +00:00
|
|
|
#include "common/endian.h"
|
|
|
|
|
2006-02-22 22:40:53 +00:00
|
|
|
#include "cine/cine.h"
|
2006-02-25 01:18:01 +00:00
|
|
|
#include "cine/unpack.h"
|
|
|
|
#include "cine/various.h"
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-02-25 00:26:14 +00:00
|
|
|
namespace Cine {
|
|
|
|
|
2008-08-11 20:41:13 +00:00
|
|
|
Common::Array<PartBuffer> partBuffer;
|
2006-02-22 22:40:53 +00:00
|
|
|
|
|
|
|
void loadPart(const char *partName) {
|
2008-08-11 20:41:13 +00:00
|
|
|
partBuffer.clear();
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
g_cine->_partFileHandle.close();
|
2006-02-22 22:40:53 +00:00
|
|
|
|
|
|
|
checkDataDisk(-1);
|
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
if (!g_cine->_partFileHandle.open(partName))
|
|
|
|
error("loadPart(): Cannot open file %s", partName);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-16 20:29:07 +00:00
|
|
|
setMouseCursor(MOUSE_CURSOR_DISK);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-08-11 20:41:13 +00:00
|
|
|
uint16 numElementInPart = g_cine->_partFileHandle.readUint16BE();
|
|
|
|
partBuffer.resize(numElementInPart);
|
2007-05-30 18:43:28 +00:00
|
|
|
g_cine->_partFileHandle.readUint16BE(); // entry size
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-06-08 19:29:48 +00:00
|
|
|
if (currentPartName != partName)
|
|
|
|
strcpy(currentPartName, partName);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-08-11 20:41:13 +00:00
|
|
|
for (uint16 i = 0; i < partBuffer.size(); i++) {
|
2007-05-30 18:43:28 +00:00
|
|
|
g_cine->_partFileHandle.read(partBuffer[i].partName, 14);
|
|
|
|
partBuffer[i].offset = g_cine->_partFileHandle.readUint32BE();
|
|
|
|
partBuffer[i].packedSize = g_cine->_partFileHandle.readUint32BE();
|
|
|
|
partBuffer[i].unpackedSize = g_cine->_partFileHandle.readUint32BE();
|
|
|
|
g_cine->_partFileHandle.readUint32BE(); // unused
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-13 16:07:33 +00:00
|
|
|
if (g_cine->getGameType() == Cine::GType_FW && g_cine->getPlatform() == Common::kPlatformPC && strcmp(partName, "BASESON.SND") != 0)
|
2006-02-22 22:40:53 +00:00
|
|
|
loadPal(partName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void closePart(void) {
|
2007-05-29 20:22:20 +00:00
|
|
|
// TODO
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-08 15:22:04 +00:00
|
|
|
static Common::String fixVolCnfFileName(const uint8 *src, uint len) {
|
|
|
|
assert(len == 11 || len == 13);
|
|
|
|
// Copy source to a temporary buffer and force a trailing zero for string manipulation
|
|
|
|
char tmp[14];
|
|
|
|
memcpy(tmp, src, len);
|
|
|
|
tmp[len] = 0;
|
|
|
|
|
|
|
|
if (len == 11) {
|
|
|
|
// Filenames of length 11 have no separation of the extension and the basename
|
|
|
|
// so that's why we have to convert them first. There's no trailing zero in them
|
|
|
|
// either and they're always of the full length 11 with padding spaces. Extension
|
|
|
|
// can be always found at offset 8 onwards.
|
|
|
|
//
|
|
|
|
// Examples of filename mappings:
|
|
|
|
// "AEROPORTMSG" -> "AEROPORT.MSG"
|
|
|
|
// "MITRAILLHP " -> "MITRAILL.HP" (Notice the trailing space after the extension)
|
|
|
|
// "BOND10 " -> "BOND10"
|
|
|
|
// "GIRL SET" -> "GIRL.SET"
|
|
|
|
|
|
|
|
// Replace all space characters with zeroes
|
|
|
|
for (uint i = 0; i < len; i++)
|
|
|
|
if (tmp[i] == ' ')
|
|
|
|
tmp[i] = 0;
|
|
|
|
// Extract the filename's extension
|
|
|
|
Common::String extension(tmp + 8);
|
|
|
|
tmp[8] = 0; // Force separation of extension and basename
|
|
|
|
Common::String basename(tmp);
|
|
|
|
if (extension.empty()) {
|
|
|
|
return basename;
|
|
|
|
} else {
|
|
|
|
return basename + "." + extension;
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
2008-06-08 15:22:04 +00:00
|
|
|
} else {
|
|
|
|
// Filenames of length 13 are okay as they are, no need for conversion
|
|
|
|
return Common::String(tmp);
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CineEngine::readVolCnf() {
|
|
|
|
Common::File f;
|
|
|
|
if (!f.open("vol.cnf")) {
|
|
|
|
error("Unable to open 'vol.cnf'");
|
|
|
|
}
|
2007-12-08 09:51:24 +00:00
|
|
|
uint32 unpackedSize, packedSize;
|
|
|
|
char hdr[8];
|
|
|
|
f.read(hdr, 8);
|
2008-06-08 15:22:04 +00:00
|
|
|
bool compressed = (memcmp(hdr, "ABASECP", 7) == 0);
|
|
|
|
if (compressed) {
|
2007-12-08 09:51:24 +00:00
|
|
|
unpackedSize = f.readUint32BE();
|
|
|
|
packedSize = f.readUint32BE();
|
|
|
|
} else {
|
|
|
|
f.seek(0);
|
|
|
|
unpackedSize = packedSize = f.size();
|
|
|
|
}
|
2008-06-08 15:22:04 +00:00
|
|
|
uint8 *buf = new uint8[unpackedSize];
|
2008-08-19 11:55:20 +00:00
|
|
|
uint8 *packedBuf = new uint8[packedSize];
|
|
|
|
f.read(packedBuf, packedSize);
|
|
|
|
CineUnpacker cineUnpacker;
|
|
|
|
if (!cineUnpacker.unpack(packedBuf, packedSize, buf, unpackedSize)) {
|
|
|
|
error("Error while unpacking 'vol.cnf' data");
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
2008-08-19 11:55:20 +00:00
|
|
|
delete[] packedBuf;
|
2007-12-07 20:45:51 +00:00
|
|
|
uint8 *p = buf;
|
|
|
|
int resourceFilesCount = READ_BE_UINT16(p); p += 2;
|
|
|
|
int entrySize = READ_BE_UINT16(p); p += 2;
|
|
|
|
for (int i = 0; i < resourceFilesCount; ++i) {
|
|
|
|
char volumeResourceFile[9];
|
|
|
|
memcpy(volumeResourceFile, p, 8);
|
|
|
|
volumeResourceFile[8] = 0;
|
|
|
|
_volumeResourceFiles.push_back(volumeResourceFile);
|
|
|
|
p += entrySize;
|
|
|
|
}
|
|
|
|
|
2008-06-08 15:22:04 +00:00
|
|
|
// Check file name blocks' sizes
|
|
|
|
bool fileNameLenMod11, fileNameLenMod13;
|
|
|
|
fileNameLenMod11 = fileNameLenMod13 = true;
|
2007-12-07 20:45:51 +00:00
|
|
|
for (int i = 0; i < resourceFilesCount; ++i) {
|
|
|
|
int size = READ_BE_UINT32(p); p += 4;
|
2008-06-08 15:22:04 +00:00
|
|
|
fileNameLenMod11 &= ((size % 11) == 0);
|
|
|
|
fileNameLenMod13 &= ((size % 13) == 0);
|
2007-12-07 20:45:51 +00:00
|
|
|
p += size;
|
|
|
|
}
|
2008-06-08 15:22:04 +00:00
|
|
|
// Make sure at least one of the candidates for file name length fits the data
|
|
|
|
assert(fileNameLenMod11 || fileNameLenMod13);
|
|
|
|
|
|
|
|
// File name length used to be deduced from the fact whether the file
|
|
|
|
// was compressed or not. Compressed files used file name length 11,
|
|
|
|
// uncompressed files used file name length 13. It worked almost always,
|
|
|
|
// but not with the game entry that's detected as the Operation Stealth's
|
|
|
|
// US Amiga release. It uses a compressed 'vol.cnf' file but still uses
|
|
|
|
// file names of length 13. So we try to deduce the file name length from
|
|
|
|
// the data in the 'vol.cnf' file.
|
|
|
|
int fileNameLength;
|
|
|
|
if (fileNameLenMod11 != fileNameLenMod13) {
|
|
|
|
// All file name blocks' sizes were divisible by either 11 or 13, but not with both.
|
|
|
|
fileNameLength = (fileNameLenMod11 ? 11 : 13);
|
|
|
|
} else {
|
|
|
|
warning("Couldn't deduce file name length from data in 'vol.cnf', using a backup deduction scheme.");
|
|
|
|
// Here we use the former file name length detection method
|
|
|
|
// if we couldn't deduce the file name length from the data.
|
|
|
|
fileNameLength = (compressed ? 11 : 13);
|
|
|
|
}
|
2007-12-07 20:45:51 +00:00
|
|
|
|
|
|
|
p = buf + 4 + resourceFilesCount * entrySize;
|
|
|
|
for (int i = 0; i < resourceFilesCount; ++i) {
|
2007-12-08 09:51:24 +00:00
|
|
|
int count = READ_BE_UINT32(p) / fileNameLength; p += 4;
|
2007-12-07 20:45:51 +00:00
|
|
|
while (count--) {
|
2008-06-08 15:22:04 +00:00
|
|
|
Common::String volumeEntryName = fixVolCnfFileName(p, fileNameLength);
|
2007-12-07 20:45:51 +00:00
|
|
|
_volumeEntriesMap.setVal(volumeEntryName, _volumeResourceFiles[i].c_str());
|
2008-06-08 15:22:04 +00:00
|
|
|
debugC(5, kCineDebugPart, "Added volume entry name '%s' resource file '%s'", volumeEntryName.c_str(), _volumeResourceFiles[i].c_str());
|
2007-12-08 09:51:24 +00:00
|
|
|
p += fileNameLength;
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-06-08 15:22:04 +00:00
|
|
|
delete[] buf;
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int16 findFileInBundle(const char *fileName) {
|
2006-10-15 01:06:44 +00:00
|
|
|
if (g_cine->getGameType() == Cine::GType_OS) {
|
2007-12-07 20:45:51 +00:00
|
|
|
// look first in currently loaded resource file
|
2008-08-11 20:41:13 +00:00
|
|
|
for (uint i = 0; i < partBuffer.size(); i++) {
|
2007-05-20 11:48:53 +00:00
|
|
|
if (!scumm_stricmp(fileName, partBuffer[i].partName)) {
|
2006-02-22 22:40:53 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2007-12-07 20:45:51 +00:00
|
|
|
// not found, open the required resource file
|
2007-12-08 09:51:24 +00:00
|
|
|
StringPtrHashMap::const_iterator it = g_cine->_volumeEntriesMap.find(fileName);
|
|
|
|
if (it == g_cine->_volumeEntriesMap.end()) {
|
|
|
|
warning("Unable to find part file for filename '%s'", fileName);
|
2007-12-07 20:45:51 +00:00
|
|
|
return -1;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2007-12-08 09:51:24 +00:00
|
|
|
const char *part = (*it)._value;
|
|
|
|
loadPart(part);
|
2007-12-07 20:45:51 +00:00
|
|
|
}
|
2008-08-11 20:41:13 +00:00
|
|
|
for (uint i = 0; i < partBuffer.size(); i++) {
|
2007-12-07 20:45:51 +00:00
|
|
|
if (!scumm_stricmp(fileName, partBuffer[i].partName)) {
|
|
|
|
return i;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-11 20:18:33 +00:00
|
|
|
void readFromPart(int16 idx, byte *dataPtr, uint32 maxSize) {
|
2008-08-19 11:55:20 +00:00
|
|
|
assert(maxSize >= partBuffer[idx].packedSize);
|
2006-03-16 20:29:07 +00:00
|
|
|
setMouseCursor(MOUSE_CURSOR_DISK);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
g_cine->_partFileHandle.seek(partBuffer[idx].offset, SEEK_SET);
|
2008-08-19 11:55:20 +00:00
|
|
|
g_cine->_partFileHandle.read(dataPtr, partBuffer[idx].packedSize);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-08-12 22:45:38 +00:00
|
|
|
byte *readBundleFile(int16 foundFileIdx, uint32 *size) {
|
2008-08-11 20:41:13 +00:00
|
|
|
assert(foundFileIdx >= 0 && foundFileIdx < (int32)partBuffer.size());
|
2008-08-11 20:09:03 +00:00
|
|
|
bool error = false;
|
2007-12-07 20:45:51 +00:00
|
|
|
byte *dataPtr = (byte *)calloc(partBuffer[foundFileIdx].unpackedSize, 1);
|
2008-08-19 11:55:20 +00:00
|
|
|
byte *packedData = (byte *)calloc(partBuffer[foundFileIdx].packedSize, 1);
|
|
|
|
assert(dataPtr && packedData);
|
|
|
|
readFromPart(foundFileIdx, packedData, partBuffer[foundFileIdx].packedSize);
|
|
|
|
CineUnpacker cineUnpacker;
|
|
|
|
error = !cineUnpacker.unpack(packedData, partBuffer[foundFileIdx].packedSize, dataPtr, partBuffer[foundFileIdx].unpackedSize);
|
|
|
|
free(packedData);
|
2008-08-11 20:09:03 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
warning("Error unpacking '%s' from bundle file '%s'", partBuffer[foundFileIdx].partName, currentPartName);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-08-12 22:45:38 +00:00
|
|
|
// Set the size variable if a pointer to it has been given
|
|
|
|
if (size != NULL) {
|
|
|
|
*size = partBuffer[foundFileIdx].unpackedSize;
|
|
|
|
}
|
|
|
|
|
2006-02-22 22:40:53 +00:00
|
|
|
return dataPtr;
|
|
|
|
}
|
2006-02-25 00:26:14 +00:00
|
|
|
|
2007-05-20 11:48:53 +00:00
|
|
|
byte *readBundleSoundFile(const char *entryName, uint32 *size) {
|
2007-05-13 16:07:33 +00:00
|
|
|
int16 index;
|
|
|
|
byte *data = 0;
|
|
|
|
char previousPartName[15] = "";
|
|
|
|
|
|
|
|
if (g_cine->getGameType() == Cine::GType_FW) {
|
|
|
|
strcpy(previousPartName, currentPartName);
|
|
|
|
loadPart("BASESON.SND");
|
|
|
|
}
|
|
|
|
index = findFileInBundle((const char *)entryName);
|
|
|
|
if (index != -1) {
|
|
|
|
data = readBundleFile(index);
|
2007-05-20 11:48:53 +00:00
|
|
|
if (size) {
|
|
|
|
*size = partBuffer[index].unpackedSize;
|
|
|
|
}
|
2007-05-13 16:07:33 +00:00
|
|
|
}
|
|
|
|
if (g_cine->getGameType() == Cine::GType_FW) {
|
|
|
|
loadPart(previousPartName);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2008-08-09 10:44:12 +00:00
|
|
|
/*! \brief Rotate byte value to the left by n bits */
|
|
|
|
byte rolByte(byte value, uint n) {
|
|
|
|
n %= 8;
|
2008-08-08 18:28:13 +00:00
|
|
|
return (byte) ((value << n) | (value >> (8 - n)));
|
|
|
|
}
|
|
|
|
|
|
|
|
byte *readFile(const char *filename, bool crypted) {
|
2006-03-23 03:15:39 +00:00
|
|
|
Common::File in;
|
|
|
|
|
|
|
|
in.open(filename);
|
|
|
|
|
|
|
|
if (!in.isOpen())
|
|
|
|
error("readFile(): Cannot open file %s", filename);
|
|
|
|
|
|
|
|
uint32 size = in.size();
|
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
byte *dataPtr = (byte *)malloc(size);
|
2006-03-23 03:15:39 +00:00
|
|
|
in.read(dataPtr, size);
|
|
|
|
|
2008-08-08 18:28:13 +00:00
|
|
|
// The Sony published CD version of Future Wars has its
|
|
|
|
// AUTO00.PRC file's bytes rotated to the right by one.
|
|
|
|
// So we decode the so called crypting by rotating all
|
|
|
|
// the bytes to the left by one.
|
|
|
|
if (crypted) {
|
|
|
|
for (uint index = 0; index < size; index++) {
|
2008-08-09 10:44:12 +00:00
|
|
|
dataPtr[index] = rolByte(dataPtr[index], 1);
|
2008-08-08 18:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 03:15:39 +00:00
|
|
|
return dataPtr;
|
|
|
|
}
|
|
|
|
|
2007-05-13 16:07:33 +00:00
|
|
|
void checkDataDisk(int16 param) {
|
|
|
|
}
|
|
|
|
|
2006-11-23 06:47:41 +00:00
|
|
|
void dumpBundle(const char *fileName) {
|
|
|
|
char tmpPart[15];
|
|
|
|
|
|
|
|
strcpy(tmpPart, currentPartName);
|
|
|
|
|
|
|
|
loadPart(fileName);
|
2008-08-11 20:41:13 +00:00
|
|
|
for (uint i = 0; i < partBuffer.size(); i++) {
|
2006-11-23 06:47:41 +00:00
|
|
|
byte *data = readBundleFile(i);
|
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
debug(0, "%s", partBuffer[i].partName);
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
Common::DumpFile out;
|
|
|
|
if (out.open(Common::String("dumps/") + partBuffer[i].partName)) {
|
2007-05-30 18:43:28 +00:00
|
|
|
out.write(data, partBuffer[i].unpackedSize);
|
|
|
|
out.close();
|
|
|
|
}
|
2006-11-23 06:47:41 +00:00
|
|
|
|
2007-05-30 18:43:28 +00:00
|
|
|
free(data);
|
2006-11-23 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadPart(tmpPart);
|
|
|
|
}
|
|
|
|
|
2006-02-25 00:26:14 +00:00
|
|
|
} // End of namespace Cine
|