2009-02-17 15:09:09 +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$
|
|
|
|
*
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Resource library
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-02-17 23:38:43 +00:00
|
|
|
#include "common/file.h"
|
2009-02-17 21:18:11 +00:00
|
|
|
|
2009-05-15 14:07:45 +00:00
|
|
|
#include "sci/resource.h"
|
2010-06-15 12:08:40 +00:00
|
|
|
#include "sci/resource_intern.h"
|
2010-05-18 04:17:58 +00:00
|
|
|
#include "sci/util.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2010-02-17 23:37:32 +00:00
|
|
|
enum {
|
|
|
|
SCI0_RESMAP_ENTRIES_SIZE = 6,
|
|
|
|
SCI1_RESMAP_ENTRIES_SIZE = 6,
|
|
|
|
SCI11_RESMAP_ENTRIES_SIZE = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
/** resource type for SCI1 resource.map file */
|
|
|
|
struct resource_index_t {
|
|
|
|
uint16 wOffset;
|
|
|
|
uint16 wSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SciVersion s_sciVersion = SCI_VERSION_NONE; // FIXME: Move this inside a suitable class, e.g. SciEngine
|
2009-09-23 12:12:37 +00:00
|
|
|
|
|
|
|
SciVersion getSciVersion() {
|
2010-01-03 22:50:39 +00:00
|
|
|
assert(s_sciVersion != SCI_VERSION_NONE);
|
2009-09-23 12:12:37 +00:00
|
|
|
return s_sciVersion;
|
|
|
|
}
|
|
|
|
|
2010-02-13 11:58:15 +00:00
|
|
|
const char *getSciVersionDesc(SciVersion version) {
|
|
|
|
switch (version) {
|
|
|
|
case SCI_VERSION_NONE:
|
|
|
|
return "Invalid SCI version";
|
|
|
|
case SCI_VERSION_0_EARLY:
|
|
|
|
return "Early SCI0";
|
|
|
|
case SCI_VERSION_0_LATE:
|
|
|
|
return "Late SCI0";
|
|
|
|
case SCI_VERSION_01:
|
|
|
|
return "SCI01";
|
|
|
|
case SCI_VERSION_1_EGA:
|
|
|
|
return "SCI1 EGA";
|
|
|
|
case SCI_VERSION_1_EARLY:
|
|
|
|
return "Early SCI1";
|
|
|
|
case SCI_VERSION_1_MIDDLE:
|
|
|
|
return "Middle SCI1";
|
|
|
|
case SCI_VERSION_1_LATE:
|
|
|
|
return "Late SCI1";
|
|
|
|
case SCI_VERSION_1_1:
|
|
|
|
return "SCI1.1";
|
|
|
|
case SCI_VERSION_2:
|
|
|
|
return "SCI2";
|
|
|
|
case SCI_VERSION_2_1:
|
|
|
|
return "SCI2.1";
|
|
|
|
case SCI_VERSION_3:
|
|
|
|
return "SCI3";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-17 23:37:32 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
#undef SCI_REQUIRE_RESOURCE_FILES
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2009-09-02 12:02:37 +00:00
|
|
|
//#define SCI_VERBOSE_resMan 1
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-04 22:16:31 +00:00
|
|
|
static const char *sci_error_types[] = {
|
2009-02-15 06:10:59 +00:00
|
|
|
"No error",
|
|
|
|
"I/O error",
|
|
|
|
"Resource is empty (size 0)",
|
|
|
|
"resource.map entry is invalid",
|
|
|
|
"resource.map file not found",
|
|
|
|
"No resource files found",
|
|
|
|
"Unknown compression method",
|
|
|
|
"Decompression failed: Decompression buffer overflow",
|
|
|
|
"Decompression failed: Sanity check failed",
|
|
|
|
"Decompression failed: Resource too big",
|
2009-02-15 22:28:50 +00:00
|
|
|
"SCI version is unsupported"
|
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-19 02:10:58 +00:00
|
|
|
// These are the 20 resource types supported by SCI1.1
|
2009-06-04 22:16:31 +00:00
|
|
|
static const char *resourceTypeNames[] = {
|
2009-02-28 23:46:50 +00:00
|
|
|
"view", "pic", "script", "text", "sound",
|
|
|
|
"memory", "vocab", "font", "cursor",
|
|
|
|
"patch", "bitmap", "palette", "cdaudio",
|
2009-05-19 02:10:58 +00:00
|
|
|
"audio", "sync", "message", "map", "heap",
|
2010-01-06 20:54:18 +00:00
|
|
|
"audio36", "sync36", "", "", "robot"
|
2009-02-28 23:46:50 +00:00
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-04 22:16:31 +00:00
|
|
|
static const char *resourceTypeSuffixes[] = {
|
2009-02-28 23:46:50 +00:00
|
|
|
"v56", "p56", "scr", "tex", "snd",
|
|
|
|
" ", "voc", "fon", "cur", "pat",
|
|
|
|
"bit", "pal", "cda", "aud", "syn",
|
2010-01-06 20:54:18 +00:00
|
|
|
"msg", "map", "hep", "aud", "syn",
|
|
|
|
"trn", " ", "rbt"
|
2009-02-28 23:46:50 +00:00
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 23:46:50 +00:00
|
|
|
const char *getResourceTypeName(ResourceType restype) {
|
2009-06-07 19:15:55 +00:00
|
|
|
if (restype != kResourceTypeInvalid)
|
|
|
|
return resourceTypeNames[restype];
|
|
|
|
else
|
|
|
|
return "invalid";
|
2009-02-28 23:46:50 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
//-- Resource main functions --
|
|
|
|
Resource::Resource() {
|
|
|
|
data = NULL;
|
|
|
|
size = 0;
|
2010-02-17 23:37:59 +00:00
|
|
|
_fileOffset = 0;
|
|
|
|
_status = kResStatusNoMalloc;
|
|
|
|
_lockers = 0;
|
|
|
|
_source = NULL;
|
|
|
|
_header = NULL;
|
|
|
|
_headerSize = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
Resource::~Resource() {
|
|
|
|
delete[] data;
|
2010-02-17 23:37:59 +00:00
|
|
|
if (_source && _source->source_type == kSourcePatch)
|
|
|
|
delete _source;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
void Resource::unalloc() {
|
|
|
|
delete[] data;
|
|
|
|
data = NULL;
|
2010-02-17 23:37:59 +00:00
|
|
|
_status = kResStatusNoMalloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::writeToStream(Common::WriteStream *stream) const {
|
2010-04-24 11:59:19 +00:00
|
|
|
stream->writeByte(_id.type | 0x80); // 0x80 is required by old sierra sci, otherwise it wont accept the patch file
|
2010-02-17 23:37:59 +00:00
|
|
|
stream->writeByte(_headerSize);
|
|
|
|
if (_headerSize > 0)
|
|
|
|
stream->write(_header, _headerSize);
|
|
|
|
stream->write(data, size);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 15:54:59 +00:00
|
|
|
uint32 Resource::getAudioCompressionType() {
|
|
|
|
return _source->audioCompressionType;
|
|
|
|
}
|
|
|
|
|
2009-09-02 12:02:37 +00:00
|
|
|
//-- resMan helper functions --
|
2009-03-03 23:07:06 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Resource source list management
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-01-27 04:26:28 +00:00
|
|
|
ResourceSource *ResourceManager::addExternalMap(const char *file_name, int volume_nr) {
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
newsrc->source_type = kSourceExtMap;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->location_name = file_name;
|
2010-01-27 04:26:28 +00:00
|
|
|
newsrc->volume_number = volume_nr;
|
2009-08-18 14:10:31 +00:00
|
|
|
|
|
|
|
_sources.push_back(newsrc);
|
|
|
|
return newsrc;
|
|
|
|
}
|
|
|
|
|
2010-06-01 22:06:52 +00:00
|
|
|
ResourceSource *ResourceManager::addExternalMap(const Common::FSNode *mapFile, int volume_nr) {
|
2009-08-18 14:10:31 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
|
|
|
|
|
|
|
newsrc->source_type = kSourceExtMap;
|
|
|
|
newsrc->location_name = mapFile->getName();
|
|
|
|
newsrc->resourceFile = mapFile;
|
2010-06-01 22:06:52 +00:00
|
|
|
newsrc->volume_number = volume_nr;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
_sources.push_back(newsrc);
|
2009-02-15 06:10:59 +00:00
|
|
|
return newsrc;
|
|
|
|
}
|
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
ResourceSource *ResourceManager::addSource(ResourceSource *map, ResSourceType type, const char *filename, int number) {
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
newsrc->source_type = type;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->location_name = filename;
|
2009-08-18 14:10:31 +00:00
|
|
|
newsrc->volume_number = number;
|
|
|
|
newsrc->associated_map = map;
|
2010-04-29 15:54:59 +00:00
|
|
|
if (type == kSourceAudioVolume)
|
|
|
|
checkIfAudioVolumeIsCompressed(newsrc);
|
2009-08-18 14:10:31 +00:00
|
|
|
|
|
|
|
_sources.push_back(newsrc);
|
|
|
|
return newsrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceSource *ResourceManager::addSource(ResourceSource *map, ResSourceType type, const Common::FSNode *resFile, int number) {
|
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
|
|
|
|
|
|
|
newsrc->source_type = type;
|
|
|
|
newsrc->location_name = resFile->getName();
|
|
|
|
newsrc->resourceFile = resFile;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->volume_number = number;
|
2009-02-15 06:10:59 +00:00
|
|
|
newsrc->associated_map = map;
|
2010-04-29 15:54:59 +00:00
|
|
|
if (type == kSourceAudioVolume)
|
|
|
|
checkIfAudioVolumeIsCompressed(newsrc);
|
2009-02-21 12:33:24 +00:00
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
_sources.push_back(newsrc);
|
2009-06-06 00:07:18 +00:00
|
|
|
return newsrc;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
ResourceSource *ResourceManager::addPatchDir(const char *dirname) {
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
newsrc->source_type = kSourceDirectory;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->location_name = dirname;
|
2009-02-21 12:33:24 +00:00
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
_sources.push_back(newsrc);
|
2009-02-15 12:29:09 +00:00
|
|
|
return 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
ResourceSource *ResourceManager::getVolume(ResourceSource *map, int volume_nr) {
|
2009-06-08 12:20:36 +00:00
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
|
|
|
ResourceSource *src = *it;
|
|
|
|
if ((src->source_type == kSourceVolume || src->source_type == kSourceAudioVolume)
|
|
|
|
&& src->associated_map == map && src->volume_number == volume_nr)
|
|
|
|
return src;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Resource manager constructors and operations
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
bool ResourceManager::loadPatch(Resource *res, Common::SeekableReadStream *file) {
|
2009-06-06 00:07:18 +00:00
|
|
|
// We assume that the resource type matches res->type
|
2010-05-01 14:51:57 +00:00
|
|
|
// We also assume that the current file position is right at the actual data (behind resourceid/headersize byte)
|
2009-06-06 00:07:18 +00:00
|
|
|
|
|
|
|
res->data = new byte[res->size];
|
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_headerSize > 0)
|
|
|
|
res->_header = new byte[res->_headerSize];
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if ((res->data == NULL) || ((res->_headerSize > 0) && (res->_header == NULL))) {
|
|
|
|
error("Can't allocate %d bytes needed for loading %s", res->size + res->_headerSize, res->_id.toString().c_str());
|
2009-06-06 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int really_read;
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_headerSize > 0) {
|
2010-06-02 13:17:36 +00:00
|
|
|
really_read = file->read(res->_header, res->_headerSize);
|
2010-02-17 23:37:59 +00:00
|
|
|
if (really_read != res->_headerSize)
|
|
|
|
error("Read %d bytes from %s but expected %d", really_read, res->_id.toString().c_str(), res->_headerSize);
|
2009-06-06 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
really_read = file->read(res->data, res->size);
|
2009-06-06 00:07:18 +00:00
|
|
|
if (really_read != res->size)
|
2010-02-17 23:37:59 +00:00
|
|
|
error("Read %d bytes from %s but expected %d", really_read, res->_id.toString().c_str(), res->size);
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_status = kResStatusAllocated;
|
2009-06-06 00:07:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
bool ResourceManager::loadFromPatchFile(Resource *res) {
|
|
|
|
Common::File file;
|
2010-02-17 23:37:59 +00:00
|
|
|
const char *filename = res->_source->location_name.c_str();
|
2009-03-03 23:07:06 +00:00
|
|
|
if (file.open(filename) == false) {
|
|
|
|
warning("Failed to open patch file %s", filename);
|
|
|
|
res->unalloc();
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 14:51:57 +00:00
|
|
|
// Skip resourceid and header size byte
|
|
|
|
file.seek(2, SEEK_SET);
|
2010-06-02 13:17:36 +00:00
|
|
|
return loadPatch(res, &file);
|
2009-06-06 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2010-06-06 13:04:24 +00:00
|
|
|
Common::SeekableReadStream *ResourceManager::getVolumeFile(ResourceSource *source) {
|
2009-03-11 20:15:42 +00:00
|
|
|
Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
|
|
|
Common::File *file;
|
|
|
|
|
2010-06-06 13:04:24 +00:00
|
|
|
if (source->resourceFile)
|
|
|
|
return source->resourceFile->createReadStream();
|
|
|
|
|
|
|
|
const char *filename = source->location_name.c_str();
|
|
|
|
|
2009-03-11 20:15:42 +00:00
|
|
|
// check if file is already opened
|
|
|
|
while (it != _volumeFiles.end()) {
|
|
|
|
file = *it;
|
|
|
|
if (scumm_stricmp(file->getName(), filename) == 0) {
|
|
|
|
// move file to top
|
|
|
|
if (it != _volumeFiles.begin()) {
|
|
|
|
_volumeFiles.erase(it);
|
|
|
|
_volumeFiles.push_front(file);
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
2010-01-13 23:58:25 +00:00
|
|
|
++it;
|
2009-03-11 20:15:42 +00:00
|
|
|
}
|
|
|
|
// adding a new file
|
|
|
|
file = new Common::File;
|
|
|
|
if (file->open(filename)) {
|
|
|
|
if (_volumeFiles.size() == MAX_OPENED_VOLUMES) {
|
|
|
|
it = --_volumeFiles.end();
|
|
|
|
delete *it;
|
|
|
|
_volumeFiles.erase(it);
|
|
|
|
}
|
|
|
|
_volumeFiles.push_front(file);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
// failed
|
|
|
|
delete file;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-13 13:32:12 +00:00
|
|
|
static uint32 resTypeToMacTag(ResourceType type);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-05-10 18:29:13 +00:00
|
|
|
void ResourceManager::loadResource(Resource *res) {
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_source->source_type == kSourcePatch && loadFromPatchFile(res))
|
2009-03-03 23:07:06 +00:00
|
|
|
return;
|
2009-06-08 12:20:36 +00:00
|
|
|
|
2010-05-10 18:29:13 +00:00
|
|
|
if (res->_source->source_type == kSourceMacResourceFork) {
|
|
|
|
Common::SeekableReadStream *stream = res->_source->macResMan.getResource(resTypeToMacTag(res->_id.type), res->_id.number);
|
|
|
|
|
|
|
|
if (!stream)
|
2010-05-24 17:21:11 +00:00
|
|
|
error("Could not get Mac resource fork resource: %d %d", res->_id.type, res->_id.number);
|
2010-05-10 18:29:13 +00:00
|
|
|
|
|
|
|
int error = decompress(res, stream);
|
|
|
|
if (error) {
|
|
|
|
warning("Error %d occured while reading %s from Mac resource file: %s",
|
|
|
|
error, res->_id.toString().c_str(), sci_error_types[error]);
|
|
|
|
res->unalloc();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-06 13:04:24 +00:00
|
|
|
Common::SeekableReadStream *fileStream = getVolumeFile(res->_source);
|
2010-06-02 13:17:36 +00:00
|
|
|
|
|
|
|
if (!fileStream) {
|
2010-02-17 23:37:59 +00:00
|
|
|
warning("Failed to open %s", res->_source->location_name.c_str());
|
2009-03-03 23:07:06 +00:00
|
|
|
res->unalloc();
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-04-29 15:54:59 +00:00
|
|
|
switch(res->_source->source_type) {
|
|
|
|
case kSourceWave:
|
2010-06-02 13:17:36 +00:00
|
|
|
fileStream->seek(res->_fileOffset, SEEK_SET);
|
|
|
|
loadFromWaveFile(res, fileStream);
|
2010-06-05 14:09:52 +00:00
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
2010-01-03 03:37:50 +00:00
|
|
|
return;
|
2009-03-10 21:44:03 +00:00
|
|
|
|
2010-04-29 15:54:59 +00:00
|
|
|
case kSourceAudioVolume:
|
|
|
|
if (res->_source->audioCompressionType) {
|
|
|
|
// this file is compressed, so lookup our offset in the offset-translation table and get the new offset
|
|
|
|
// also calculate the compressed size by using the next offset
|
|
|
|
int32 *mappingTable = res->_source->audioCompressionOffsetMapping;
|
|
|
|
int32 compressedOffset = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (*mappingTable == res->_fileOffset) {
|
|
|
|
mappingTable++;
|
|
|
|
compressedOffset = *mappingTable;
|
|
|
|
// Go to next compressed offset and use that to calculate size of compressed sample
|
2010-05-01 14:51:57 +00:00
|
|
|
switch (res->_id.type) {
|
|
|
|
case kResourceTypeSync:
|
|
|
|
case kResourceTypeSync36:
|
|
|
|
// we should already have a (valid) size
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mappingTable += 2;
|
|
|
|
res->size = *mappingTable - compressedOffset;
|
|
|
|
}
|
2010-04-29 15:54:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mappingTable += 2;
|
|
|
|
} while (*mappingTable);
|
|
|
|
|
|
|
|
if (!compressedOffset)
|
|
|
|
error("could not translate offset to compressed offset in audio volume");
|
2010-06-02 13:17:36 +00:00
|
|
|
fileStream->seek(compressedOffset, SEEK_SET);
|
2010-04-29 15:54:59 +00:00
|
|
|
|
|
|
|
switch (res->_id.type) {
|
|
|
|
case kResourceTypeAudio:
|
|
|
|
case kResourceTypeAudio36:
|
|
|
|
// Directly read the stream, compressed audio wont have resource type id and header size for SCI1.1
|
2010-06-02 13:17:36 +00:00
|
|
|
loadFromAudioVolumeSCI1(res, fileStream);
|
2010-06-05 14:09:52 +00:00
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
2010-04-29 15:54:59 +00:00
|
|
|
return;
|
2010-04-29 19:06:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
2010-04-29 15:54:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// original file, directly seek to given offset and get SCI1/SCI1.1 audio resource
|
2010-06-02 13:17:36 +00:00
|
|
|
fileStream->seek(res->_fileOffset, SEEK_SET);
|
2010-04-29 15:54:59 +00:00
|
|
|
}
|
2009-09-23 10:55:35 +00:00
|
|
|
if (getSciVersion() < SCI_VERSION_1_1)
|
2010-06-02 13:17:36 +00:00
|
|
|
loadFromAudioVolumeSCI1(res, fileStream);
|
2009-06-09 19:18:48 +00:00
|
|
|
else
|
2010-06-02 13:17:36 +00:00
|
|
|
loadFromAudioVolumeSCI11(res, fileStream);
|
2010-06-05 14:09:52 +00:00
|
|
|
|
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
2010-04-29 15:54:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2010-06-02 13:17:36 +00:00
|
|
|
fileStream->seek(res->_fileOffset, SEEK_SET);
|
|
|
|
int error = decompress(res, fileStream);
|
2010-06-05 14:09:52 +00:00
|
|
|
|
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
if (error) {
|
|
|
|
warning("Error %d occured while reading %s from resource file: %s",
|
2010-02-17 23:37:59 +00:00
|
|
|
error, res->_id.toString().c_str(), sci_error_types[error]);
|
2009-06-08 12:20:36 +00:00
|
|
|
res->unalloc();
|
|
|
|
}
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
Resource *ResourceManager::testResource(ResourceId id) {
|
2010-02-17 23:37:59 +00:00
|
|
|
return _resMap.getVal(id, NULL);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
int ResourceManager::addAppropriateSources() {
|
2010-01-27 04:26:28 +00:00
|
|
|
Common::ArchiveMemberList files;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-06-01 22:06:52 +00:00
|
|
|
if (Common::File::exists("resource.map")) {
|
2010-01-27 04:26:28 +00:00
|
|
|
// SCI0-SCI2 file naming scheme
|
2010-06-01 22:06:52 +00:00
|
|
|
ResourceSource *map = addExternalMap("resource.map");
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2010-06-01 22:06:52 +00:00
|
|
|
SearchMan.listMatchingMembers(files, "resource.0??");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-01-27 04:26:28 +00:00
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
|
|
|
const Common::String name = (*x)->getName();
|
|
|
|
const char *dot = strrchr(name.c_str(), '.');
|
|
|
|
int number = atoi(dot + 1);
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2010-01-27 04:26:28 +00:00
|
|
|
addSource(map, kSourceVolume, name.c_str(), number);
|
|
|
|
}
|
2009-08-21 18:12:13 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2010-01-29 16:25:18 +00:00
|
|
|
// GK1CD hires content
|
2010-06-01 22:06:52 +00:00
|
|
|
if (Common::File::exists("alt.map") && Common::File::exists("resource.alt"))
|
|
|
|
addSource(addExternalMap("alt.map", 10), kSourceVolume, "resource.alt", 10);
|
2010-05-10 18:29:13 +00:00
|
|
|
#endif
|
|
|
|
} else if (Common::File::exists("Data1")) {
|
|
|
|
// Mac SCI1.1+ file naming scheme
|
2010-05-27 03:15:24 +00:00
|
|
|
SearchMan.listMatchingMembers(files, "Data?*");
|
2010-05-10 18:29:13 +00:00
|
|
|
|
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
|
|
|
Common::String filename = (*x)->getName();
|
|
|
|
addSource(0, kSourceMacResourceFork, filename.c_str(), atoi(filename.c_str() + 4));
|
2010-01-28 22:26:13 +00:00
|
|
|
}
|
2010-05-10 18:29:13 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2010-05-18 04:17:58 +00:00
|
|
|
// Mac SCI32 games have extra folders for patches
|
|
|
|
addPatchDir("Robot Folder");
|
|
|
|
addPatchDir("Sound Folder");
|
|
|
|
addPatchDir("Voices Folder");
|
2010-06-10 15:06:25 +00:00
|
|
|
addPatchDir("Voices");
|
2010-05-18 04:17:58 +00:00
|
|
|
//addPatchDir("VMD Folder");
|
|
|
|
|
|
|
|
// There can also be a "Patches" resource fork with patches
|
|
|
|
if (Common::File::exists("Patches"))
|
|
|
|
addSource(0, kSourceMacResourceFork, "Patches", 100);
|
2010-01-27 04:26:28 +00:00
|
|
|
} else {
|
|
|
|
// SCI2.1-SCI3 file naming scheme
|
|
|
|
Common::ArchiveMemberList mapFiles;
|
2010-06-01 22:06:52 +00:00
|
|
|
SearchMan.listMatchingMembers(mapFiles, "resmap.0??");
|
|
|
|
SearchMan.listMatchingMembers(files, "ressci.0??");
|
2009-02-20 16:03:50 +00:00
|
|
|
|
2010-01-27 04:26:28 +00:00
|
|
|
// We need to have the same number of maps as resource archives
|
|
|
|
if (mapFiles.empty() || files.empty() || mapFiles.size() != files.size())
|
|
|
|
return 0;
|
|
|
|
|
2010-01-27 21:43:23 +00:00
|
|
|
for (Common::ArchiveMemberList::const_iterator mapIterator = mapFiles.begin(); mapIterator != mapFiles.end(); ++mapIterator) {
|
2010-01-27 04:26:28 +00:00
|
|
|
Common::String mapName = (*mapIterator)->getName();
|
2010-01-27 21:43:23 +00:00
|
|
|
int mapNumber = atoi(strrchr(mapName.c_str(), '.') + 1);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-01-27 21:43:23 +00:00
|
|
|
for (Common::ArchiveMemberList::const_iterator fileIterator = files.begin(); fileIterator != files.end(); ++fileIterator) {
|
|
|
|
Common::String resName = (*fileIterator)->getName();
|
|
|
|
int resNumber = atoi(strrchr(resName.c_str(), '.') + 1);
|
2010-01-27 04:26:28 +00:00
|
|
|
|
2010-01-27 21:43:23 +00:00
|
|
|
if (mapNumber == resNumber) {
|
|
|
|
addSource(addExternalMap(mapName.c_str(), mapNumber), kSourceVolume, resName.c_str(), mapNumber);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-27 04:26:28 +00:00
|
|
|
}
|
2010-01-28 11:19:27 +00:00
|
|
|
|
|
|
|
// SCI2.1 resource patches
|
2010-06-01 22:06:52 +00:00
|
|
|
if (Common::File::exists("resmap.pat") && Common::File::exists("ressci.pat")) {
|
2010-01-28 22:26:13 +00:00
|
|
|
// We add this resource with a map which surely won't exist
|
2010-06-01 22:06:52 +00:00
|
|
|
addSource(addExternalMap("resmap.pat", 100), kSourceVolume, "ressci.pat", 100);
|
2010-01-28 11:19:27 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2010-01-27 04:26:28 +00:00
|
|
|
#else
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
addPatchDir(".");
|
2010-06-01 22:06:52 +00:00
|
|
|
if (Common::File::exists("message.map"))
|
|
|
|
addSource(addExternalMap("message.map"), kSourceVolume, "resource.msg", 0);
|
2010-01-27 04:26:28 +00:00
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
int ResourceManager::addAppropriateSources(const Common::FSList &fslist) {
|
|
|
|
ResourceSource *map = 0;
|
2010-06-02 00:20:52 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2010-06-01 22:06:52 +00:00
|
|
|
ResourceSource *sci21PatchMap = 0;
|
|
|
|
const Common::FSNode *sci21PatchRes = 0;
|
2010-06-02 00:20:52 +00:00
|
|
|
#endif
|
2009-08-18 14:10:31 +00:00
|
|
|
|
|
|
|
// First, find resource.map
|
|
|
|
for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
|
|
|
if (file->isDirectory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Common::String filename = file->getName();
|
|
|
|
filename.toLowercase();
|
|
|
|
|
2010-06-01 22:06:52 +00:00
|
|
|
if (filename.contains("resource.map"))
|
2009-08-18 14:10:31 +00:00
|
|
|
map = addExternalMap(file);
|
2010-06-01 22:06:52 +00:00
|
|
|
|
|
|
|
if (filename.contains("resmap.0")) {
|
|
|
|
const char *dot = strrchr(file->getName().c_str(), '.');
|
|
|
|
int number = atoi(dot + 1);
|
|
|
|
map = addExternalMap(file, number);
|
2009-08-18 14:10:31 +00:00
|
|
|
}
|
2010-06-01 22:06:52 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
// SCI2.1 resource patches
|
|
|
|
if (filename.contains("resmap.pat"))
|
|
|
|
sci21PatchMap = addExternalMap(file, 100);
|
|
|
|
|
|
|
|
if (filename.contains("ressci.pat"))
|
|
|
|
sci21PatchRes = file;
|
|
|
|
#endif
|
2009-08-18 14:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!map)
|
|
|
|
return 0;
|
|
|
|
|
2010-06-01 22:06:52 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
if (sci21PatchMap && sci21PatchRes)
|
|
|
|
addSource(sci21PatchMap, kSourceVolume, sci21PatchRes, 100);
|
|
|
|
#endif
|
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
// Now find all the resource.0?? files
|
|
|
|
for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
|
|
|
if (file->isDirectory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Common::String filename = file->getName();
|
|
|
|
filename.toLowercase();
|
|
|
|
|
|
|
|
if (filename.contains("resource.0") || filename.contains("ressci.0")) {
|
|
|
|
const char *dot = strrchr(filename.c_str(), '.');
|
|
|
|
int number = atoi(dot + 1);
|
|
|
|
|
|
|
|
addSource(map, kSourceVolume, file, number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is only called by the advanced detector, and we don't really need
|
|
|
|
// to add a patch directory or message.map here
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
int ResourceManager::addInternalSources() {
|
2009-06-07 19:15:55 +00:00
|
|
|
Common::List<ResourceId> *resources = listResources(kResourceTypeMap);
|
|
|
|
Common::List<ResourceId>::iterator itr = resources->begin();
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
while (itr != resources->end()) {
|
|
|
|
ResourceSource *src = addSource(NULL, kSourceIntMap, "MAP", itr->number);
|
|
|
|
|
|
|
|
if ((itr->number == 65535) && Common::File::exists("RESOURCE.SFX"))
|
2009-06-06 00:07:18 +00:00
|
|
|
addSource(src, kSourceAudioVolume, "RESOURCE.SFX", 0);
|
|
|
|
else if (Common::File::exists("RESOURCE.AUD"))
|
|
|
|
addSource(src, kSourceAudioVolume, "RESOURCE.AUD", 0);
|
2009-06-07 19:15:55 +00:00
|
|
|
|
2010-01-13 23:58:25 +00:00
|
|
|
++itr;
|
2009-06-06 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
void ResourceManager::scanNewSources() {
|
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
|
|
|
ResourceSource *source = *it;
|
|
|
|
|
|
|
|
if (!source->scanned) {
|
|
|
|
source->scanned = true;
|
|
|
|
switch (source->source_type) {
|
|
|
|
case kSourceDirectory:
|
|
|
|
readResourcePatches(source);
|
2010-06-10 15:06:25 +00:00
|
|
|
|
2010-06-08 13:15:05 +00:00
|
|
|
// We can't use getSciVersion() at this point, thus using _volVersion
|
2010-06-08 15:08:45 +00:00
|
|
|
if (_volVersion >= kResVersionSci11) // SCI1.1+
|
2010-06-08 13:15:05 +00:00
|
|
|
readResourcePatchesBase36(source);
|
2010-06-10 15:06:25 +00:00
|
|
|
|
2009-12-27 22:34:12 +00:00
|
|
|
readWaveAudioPatches();
|
2009-06-08 12:20:36 +00:00
|
|
|
break;
|
|
|
|
case kSourceExtMap:
|
2009-08-15 00:28:59 +00:00
|
|
|
if (_mapVersion < kResVersionSci1Late)
|
2009-06-08 12:20:36 +00:00
|
|
|
readResourceMapSCI0(source);
|
|
|
|
else
|
|
|
|
readResourceMapSCI1(source);
|
|
|
|
break;
|
2009-06-09 19:18:48 +00:00
|
|
|
case kSourceExtAudioMap:
|
|
|
|
readAudioMapSCI1(source);
|
|
|
|
break;
|
2009-06-08 12:20:36 +00:00
|
|
|
case kSourceIntMap:
|
2009-06-09 19:18:48 +00:00
|
|
|
readAudioMapSCI11(source);
|
2009-06-08 12:20:36 +00:00
|
|
|
break;
|
2010-05-10 18:29:13 +00:00
|
|
|
case kSourceMacResourceFork:
|
|
|
|
readMacResourceFork(source);
|
|
|
|
break;
|
2009-06-08 12:20:36 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
void ResourceManager::freeResourceSources() {
|
2010-05-18 04:17:58 +00:00
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it)
|
2009-06-08 12:20:36 +00:00
|
|
|
delete *it;
|
|
|
|
|
|
|
|
_sources.clear();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 09:12:41 +00:00
|
|
|
ResourceManager::ResourceManager() {
|
2009-08-18 14:10:31 +00:00
|
|
|
addAppropriateSources();
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceManager::ResourceManager(const Common::FSList &fslist) {
|
|
|
|
addAppropriateSources(fslist);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::init() {
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked = 0;
|
|
|
|
_memoryLRU = 0;
|
2009-03-05 23:27:02 +00:00
|
|
|
_LRU.clear();
|
2009-03-03 23:07:06 +00:00
|
|
|
_resMap.clear();
|
2009-06-09 19:18:48 +00:00
|
|
|
_audioMapSCI1 = NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
// FIXME: put this in an Init() function, so that we can error out if detection fails completely
|
2009-07-11 16:07:14 +00:00
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
_mapVersion = detectMapVersion();
|
|
|
|
_volVersion = detectVolVersion();
|
|
|
|
if ((_volVersion == kResVersionUnknown) && (_mapVersion != kResVersionUnknown)) {
|
|
|
|
warning("Volume version not detected, but map version has been detected. Setting volume version to map version");
|
|
|
|
_volVersion = _mapVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_mapVersion == kResVersionUnknown) && (_volVersion != kResVersionUnknown)) {
|
|
|
|
warning("Map version not detected, but volume version has been detected. Setting map version to volume version");
|
|
|
|
_mapVersion = _volVersion;
|
2009-03-07 01:08:34 +00:00
|
|
|
}
|
2009-08-15 00:28:59 +00:00
|
|
|
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected resource map version %d: %s", _mapVersion, versionDescription(_mapVersion));
|
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected volume version %d: %s", _volVersion, versionDescription(_volVersion));
|
2009-03-07 01:08:34 +00:00
|
|
|
|
2010-01-17 22:32:53 +00:00
|
|
|
if ((_mapVersion == kResVersionUnknown) && (_volVersion == kResVersionUnknown)) {
|
2010-01-17 22:41:29 +00:00
|
|
|
warning("Volume and map version not detected, assuming that this is not a sci game");
|
2010-01-17 22:32:53 +00:00
|
|
|
_viewType = kViewUnknown;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
scanNewSources();
|
2009-06-06 00:07:18 +00:00
|
|
|
addInternalSources();
|
2009-06-08 12:20:36 +00:00
|
|
|
scanNewSources();
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-09-23 10:55:35 +00:00
|
|
|
detectSciVersion();
|
2009-07-11 19:00:56 +00:00
|
|
|
|
2010-02-13 11:58:15 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected %s", getSciVersionDesc(getSciVersion()));
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
switch (_viewType) {
|
|
|
|
case kViewEga:
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected EGA graphic resources");
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-08-16 19:18:19 +00:00
|
|
|
case kViewVga:
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected VGA graphic resources");
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-08-16 19:18:19 +00:00
|
|
|
case kViewVga11:
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected SCI1.1 VGA graphic resources");
|
2009-08-19 21:08:17 +00:00
|
|
|
break;
|
|
|
|
case kViewAmiga:
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected Amiga graphic resources");
|
2009-08-19 21:08:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-09-02 12:02:37 +00:00
|
|
|
warning("resMan: Couldn't determine view type");
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceManager::~ResourceManager() {
|
2009-03-03 23:07:06 +00:00
|
|
|
// freeing resources
|
2009-06-07 02:18:38 +00:00
|
|
|
ResourceMap::iterator itr = _resMap.begin();
|
2009-03-03 23:07:06 +00:00
|
|
|
while (itr != _resMap.end()) {
|
|
|
|
delete itr->_value;
|
2010-01-13 23:58:25 +00:00
|
|
|
++itr;
|
2009-03-03 23:07:06 +00:00
|
|
|
}
|
2009-06-08 12:20:36 +00:00
|
|
|
freeResourceSources();
|
2009-03-11 20:15:42 +00:00
|
|
|
|
|
|
|
Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
|
|
|
while (it != _volumeFiles.end()) {
|
|
|
|
delete *it;
|
2010-01-13 23:58:25 +00:00
|
|
|
++it;
|
2009-03-11 20:15:42 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
void ResourceManager::removeFromLRU(Resource *res) {
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_status != kResStatusEnqueued) {
|
2009-09-02 12:02:37 +00:00
|
|
|
warning("resMan: trying to remove resource that isn't enqueued");
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-03-05 23:27:02 +00:00
|
|
|
_LRU.remove(res);
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLRU -= res->size;
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_status = kResStatusAllocated;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
void ResourceManager::addToLRU(Resource *res) {
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_status != kResStatusAllocated) {
|
|
|
|
warning("resMan: trying to enqueue resource with state %d", res->_status);
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-03-05 23:27:02 +00:00
|
|
|
_LRU.push_front(res);
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLRU += res->size;
|
2009-09-02 12:02:37 +00:00
|
|
|
#if SCI_VERBOSE_resMan
|
2009-03-05 23:27:02 +00:00
|
|
|
debug("Adding %s.%03d (%d bytes) to lru control: %d bytes total",
|
2009-03-11 01:40:08 +00:00
|
|
|
getResourceTypeName(res->type), res->number, res->size,
|
|
|
|
mgr->_memoryLRU);
|
2009-02-15 06:10:59 +00:00
|
|
|
#endif
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_status = kResStatusEnqueued;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
void ResourceManager::printLRU() {
|
2009-02-15 06:10:59 +00:00
|
|
|
int mem = 0;
|
|
|
|
int entries = 0;
|
2009-03-05 23:27:02 +00:00
|
|
|
Common::List<Resource *>::iterator it = _LRU.begin();
|
|
|
|
Resource *res;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-05 23:27:02 +00:00
|
|
|
while (it != _LRU.end()) {
|
|
|
|
res = *it;
|
2010-02-17 23:37:59 +00:00
|
|
|
debug("\t%s: %d bytes", res->_id.toString().c_str(), res->size);
|
2009-02-15 06:10:59 +00:00
|
|
|
mem += res->size;
|
2010-01-13 23:58:25 +00:00
|
|
|
++entries;
|
|
|
|
++it;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 23:27:02 +00:00
|
|
|
debug("Total: %d entries, %d bytes (mgr says %d)", entries, mem, _memoryLRU);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
void ResourceManager::freeOldResources() {
|
2009-08-18 09:12:41 +00:00
|
|
|
while (MAX_MEMORY < _memoryLRU) {
|
2009-06-09 19:18:48 +00:00
|
|
|
assert(!_LRU.empty());
|
2009-03-05 23:27:02 +00:00
|
|
|
Resource *goner = *_LRU.reverse_begin();
|
2009-02-28 20:45:36 +00:00
|
|
|
removeFromLRU(goner);
|
2009-03-03 23:07:06 +00:00
|
|
|
goner->unalloc();
|
2009-09-02 12:02:37 +00:00
|
|
|
#ifdef SCI_VERBOSE_resMan
|
|
|
|
printf("resMan-debug: LRU: Freeing %s.%03d (%d bytes)\n", getResourceTypeName(goner->type), goner->number, goner->size);
|
2009-02-15 06:10:59 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
Common::List<ResourceId> *ResourceManager::listResources(ResourceType type, int mapNumber) {
|
|
|
|
Common::List<ResourceId> *resources = new Common::List<ResourceId>;
|
|
|
|
|
|
|
|
ResourceMap::iterator itr = _resMap.begin();
|
|
|
|
while (itr != _resMap.end()) {
|
2010-02-17 23:37:59 +00:00
|
|
|
if ((itr->_value->_id.type == type) && ((mapNumber == -1) || (itr->_value->_id.number == mapNumber)))
|
|
|
|
resources->push_back(itr->_value->_id);
|
2010-01-13 23:58:25 +00:00
|
|
|
++itr;
|
2009-06-07 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
Resource *ResourceManager::findResource(ResourceId id, bool lock) {
|
2009-07-07 13:39:24 +00:00
|
|
|
Resource *retval = testResource(id);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!retval)
|
|
|
|
return NULL;
|
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if (retval->_status == kResStatusNoMalloc)
|
2009-03-05 23:27:02 +00:00
|
|
|
loadResource(retval);
|
2010-02-17 23:37:59 +00:00
|
|
|
else if (retval->_status == kResStatusEnqueued)
|
2009-02-28 20:45:36 +00:00
|
|
|
removeFromLRU(retval);
|
2009-02-21 12:33:24 +00:00
|
|
|
// Unless an error occured, the resource is now either
|
|
|
|
// locked or allocated, but never queued or freed.
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
freeOldResources();
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
if (lock) {
|
2010-02-17 23:37:59 +00:00
|
|
|
if (retval->_status == kResStatusAllocated) {
|
|
|
|
retval->_status = kResStatusLocked;
|
|
|
|
retval->_lockers = 0;
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked += retval->size;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2010-02-17 23:37:59 +00:00
|
|
|
retval->_lockers++;
|
|
|
|
} else if (retval->_status != kResStatusLocked) { // Don't lock it
|
|
|
|
if (retval->_status == kResStatusAllocated)
|
2009-02-28 20:45:36 +00:00
|
|
|
addToLRU(retval);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retval->data)
|
|
|
|
return retval;
|
|
|
|
else {
|
2010-02-17 23:37:59 +00:00
|
|
|
warning("resMan: Failed to read %s", retval->_id.toString().c_str());
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
void ResourceManager::unlockResource(Resource *res) {
|
|
|
|
assert(res);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_status != kResStatusLocked) {
|
|
|
|
warning("[resMan] Attempt to unlock unlocked resource %s", res->_id.toString().c_str());
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if (!--res->_lockers) { // No more lockers?
|
|
|
|
res->_status = kResStatusAllocated;
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked -= res->size;
|
|
|
|
addToLRU(res);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
freeOldResources();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
const char *ResourceManager::versionDescription(ResVersion version) const {
|
|
|
|
switch (version) {
|
|
|
|
case kResVersionUnknown:
|
|
|
|
return "Unknown";
|
|
|
|
case kResVersionSci0Sci1Early:
|
|
|
|
return "SCI0 / Early SCI1";
|
|
|
|
case kResVersionSci1Middle:
|
|
|
|
return "Middle SCI1";
|
|
|
|
case kResVersionSci1Late:
|
|
|
|
return "Late SCI1";
|
|
|
|
case kResVersionSci11:
|
|
|
|
return "SCI1.1";
|
2010-05-10 18:29:13 +00:00
|
|
|
case kResVersionSci11Mac:
|
|
|
|
return "Mac SCI1.1+";
|
2009-08-15 00:28:59 +00:00
|
|
|
case kResVersionSci32:
|
|
|
|
return "SCI32";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Version not valid";
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceManager::ResVersion ResourceManager::detectMapVersion() {
|
2009-08-18 14:10:31 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2009-03-07 00:59:38 +00:00
|
|
|
byte buff[6];
|
2009-07-01 20:51:34 +00:00
|
|
|
ResourceSource *rsrc= 0;
|
2009-06-08 12:20:36 +00:00
|
|
|
|
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
|
|
|
rsrc = *it;
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
if (rsrc->source_type == kSourceExtMap) {
|
2009-08-18 14:10:31 +00:00
|
|
|
if (rsrc->resourceFile) {
|
|
|
|
fileStream = rsrc->resourceFile->createReadStream();
|
|
|
|
} else {
|
2010-06-02 13:17:36 +00:00
|
|
|
Common::File *file = new Common::File();
|
2009-08-18 14:10:31 +00:00
|
|
|
file->open(rsrc->location_name);
|
|
|
|
if (file->isOpen())
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 00:59:38 +00:00
|
|
|
break;
|
2010-05-10 18:29:13 +00:00
|
|
|
} else if (rsrc->source_type == kSourceMacResourceFork)
|
|
|
|
return kResVersionSci11Mac;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-08-18 09:12:41 +00:00
|
|
|
|
2009-08-21 18:12:13 +00:00
|
|
|
if (!fileStream)
|
2009-07-11 19:00:56 +00:00
|
|
|
error("Failed to open resource map file");
|
2009-08-21 18:12:13 +00:00
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
// detection
|
|
|
|
// SCI0 and SCI01 maps have last 6 bytes set to FF
|
2009-08-18 14:10:31 +00:00
|
|
|
fileStream->seek(-4, SEEK_END);
|
|
|
|
uint32 uEnd = fileStream->readUint32LE();
|
2009-03-07 00:59:38 +00:00
|
|
|
if (uEnd == 0xFFFFFFFF) {
|
|
|
|
// check if 0 or 01 - try to read resources in SCI0 format and see if exists
|
2009-08-18 14:10:31 +00:00
|
|
|
fileStream->seek(0, SEEK_SET);
|
|
|
|
while (fileStream->read(buff, 6) == 6 && !(buff[0] == 0xFF && buff[1] == 0xFF && buff[2] == 0xFF)) {
|
2009-03-07 00:59:38 +00:00
|
|
|
if (getVolume(rsrc, (buff[5] & 0xFC) >> 2) == NULL)
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionSci1Middle;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionSci0Sci1Early;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-07-09 15:54:18 +00:00
|
|
|
|
|
|
|
// SCI1 and SCI1.1 maps consist of a fixed 3-byte header, a directory list (3-bytes each) that has one entry
|
|
|
|
// of id FFh and points to EOF. The actual entries have 6-bytes on SCI1 and 5-bytes on SCI1.1
|
|
|
|
byte directoryType = 0;
|
|
|
|
uint16 directoryOffset = 0;
|
|
|
|
uint16 lastDirectoryOffset = 0;
|
|
|
|
uint16 directorySize = 0;
|
2009-08-15 00:28:59 +00:00
|
|
|
ResVersion mapDetected = kResVersionUnknown;
|
2009-08-18 14:10:31 +00:00
|
|
|
fileStream->seek(0, SEEK_SET);
|
|
|
|
|
|
|
|
while (!fileStream->eos()) {
|
|
|
|
directoryType = fileStream->readByte();
|
|
|
|
directoryOffset = fileStream->readUint16LE();
|
2009-08-21 19:48:48 +00:00
|
|
|
|
2009-08-21 18:12:13 +00:00
|
|
|
// Only SCI32 has directory type < 0x80
|
2009-08-21 19:48:48 +00:00
|
|
|
if (directoryType < 0x80 && (mapDetected == kResVersionUnknown || mapDetected == kResVersionSci32))
|
2009-08-21 18:12:13 +00:00
|
|
|
mapDetected = kResVersionSci32;
|
2009-08-25 23:02:57 +00:00
|
|
|
else if (directoryType < 0x80 || ((directoryType & 0x7f) > 0x20 && directoryType != 0xFF))
|
2009-08-21 18:12:13 +00:00
|
|
|
break;
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2009-07-09 15:54:18 +00:00
|
|
|
// Offset is above file size? -> definitely not SCI1/SCI1.1
|
2009-08-18 14:10:31 +00:00
|
|
|
if (directoryOffset > fileStream->size())
|
2009-07-09 15:54:18 +00:00
|
|
|
break;
|
2009-08-21 18:12:13 +00:00
|
|
|
|
2009-08-25 23:02:57 +00:00
|
|
|
if (lastDirectoryOffset && mapDetected == kResVersionUnknown) {
|
2009-07-09 15:54:18 +00:00
|
|
|
directorySize = directoryOffset - lastDirectoryOffset;
|
|
|
|
if ((directorySize % 5) && (directorySize % 6 == 0))
|
2009-08-15 00:28:59 +00:00
|
|
|
mapDetected = kResVersionSci1Late;
|
2009-07-09 15:54:18 +00:00
|
|
|
if ((directorySize % 5 == 0) && (directorySize % 6))
|
2009-08-15 00:28:59 +00:00
|
|
|
mapDetected = kResVersionSci11;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
if (directoryType == 0xFF) {
|
2009-07-09 15:54:18 +00:00
|
|
|
// FFh entry needs to point to EOF
|
2009-08-18 14:10:31 +00:00
|
|
|
if (directoryOffset != fileStream->size())
|
2009-07-09 15:54:18 +00:00
|
|
|
break;
|
2009-08-18 14:10:31 +00:00
|
|
|
|
|
|
|
delete fileStream;
|
|
|
|
|
|
|
|
if (mapDetected)
|
2009-07-09 15:54:18 +00:00
|
|
|
return mapDetected;
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionSci1Late;
|
2009-07-09 15:54:18 +00:00
|
|
|
}
|
2009-08-21 18:12:13 +00:00
|
|
|
|
2009-07-09 15:54:18 +00:00
|
|
|
lastDirectoryOffset = directoryOffset;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-03-30 07:53:32 +00:00
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
delete fileStream;
|
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionUnknown;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
ResourceManager::ResVersion ResourceManager::detectVolVersion() {
|
2009-08-18 14:10:31 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2009-06-08 12:20:36 +00:00
|
|
|
ResourceSource *rsrc;
|
2009-08-18 14:10:31 +00:00
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
|
|
|
rsrc = *it;
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
if (rsrc->source_type == kSourceVolume) {
|
2009-08-18 14:10:31 +00:00
|
|
|
if (rsrc->resourceFile) {
|
|
|
|
fileStream = rsrc->resourceFile->createReadStream();
|
|
|
|
} else {
|
2010-06-02 13:17:36 +00:00
|
|
|
Common::File *file = new Common::File();
|
2009-08-18 14:10:31 +00:00
|
|
|
file->open(rsrc->location_name);
|
|
|
|
if (file->isOpen())
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 00:59:38 +00:00
|
|
|
break;
|
2010-05-10 18:29:13 +00:00
|
|
|
} else if (rsrc->source_type == kSourceMacResourceFork)
|
|
|
|
return kResVersionSci11Mac;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
if (!fileStream) {
|
2010-01-18 23:01:05 +00:00
|
|
|
error("Failed to open volume file - if you got resource.p01/resource.p02/etc. files, merge them together into resource.000");
|
2010-01-18 23:07:56 +00:00
|
|
|
// resource.p01/resource.p02/etc. may be there when directly copying the files from the original floppies
|
|
|
|
// the sierra installer would merge those together (perhaps we could do this as well?)
|
|
|
|
// possible TODO
|
|
|
|
// example for such game: Laura Bow 2
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionUnknown;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
2009-08-18 14:10:31 +00:00
|
|
|
|
2009-03-07 05:44:36 +00:00
|
|
|
// SCI0 volume format: {wResId wPacked+4 wUnpacked wCompression} = 8 bytes
|
|
|
|
// SCI1 volume format: {bResType wResNumber wPacked+4 wUnpacked wCompression} = 9 bytes
|
2009-03-10 21:44:03 +00:00
|
|
|
// SCI1.1 volume format: {bResType wResNumber wPacked wUnpacked wCompression} = 9 bytes
|
2009-12-27 22:41:33 +00:00
|
|
|
// SCI32 volume format: {bResType wResNumber dwPacked dwUnpacked wCompression} = 13 bytes
|
2009-03-07 00:59:38 +00:00
|
|
|
// Try to parse volume with SCI0 scheme to see if it make sense
|
|
|
|
// Checking 1MB of data should be enough to determine the version
|
2009-03-07 05:44:36 +00:00
|
|
|
uint16 resId, wCompression;
|
|
|
|
uint32 dwPacked, dwUnpacked;
|
2009-08-15 00:28:59 +00:00
|
|
|
ResVersion curVersion = kResVersionSci0Sci1Early;
|
2009-05-14 21:47:38 +00:00
|
|
|
bool failed = false;
|
2009-08-30 19:33:34 +00:00
|
|
|
bool sci11Align = false;
|
2009-03-30 07:53:32 +00:00
|
|
|
|
2009-05-14 21:47:38 +00:00
|
|
|
// Check for SCI0, SCI1, SCI1.1 and SCI32 v2 (Gabriel Knight 1 CD) formats
|
2009-08-18 14:10:31 +00:00
|
|
|
while (!fileStream->eos() && fileStream->pos() < 0x100000) {
|
2009-08-15 00:28:59 +00:00
|
|
|
if (curVersion > kResVersionSci0Sci1Early)
|
2009-08-18 14:10:31 +00:00
|
|
|
fileStream->readByte();
|
|
|
|
resId = fileStream->readUint16LE();
|
|
|
|
dwPacked = (curVersion < kResVersionSci32) ? fileStream->readUint16LE() : fileStream->readUint32LE();
|
|
|
|
dwUnpacked = (curVersion < kResVersionSci32) ? fileStream->readUint16LE() : fileStream->readUint32LE();
|
2009-12-27 22:41:33 +00:00
|
|
|
wCompression = fileStream->readUint16LE();
|
2009-08-18 14:10:31 +00:00
|
|
|
if (fileStream->eos()) {
|
|
|
|
delete fileStream;
|
2009-05-14 21:47:38 +00:00
|
|
|
return curVersion;
|
2009-08-18 14:10:31 +00:00
|
|
|
}
|
2009-05-14 21:47:38 +00:00
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
int chk = (curVersion == kResVersionSci0Sci1Early) ? 4 : 20;
|
|
|
|
int offs = curVersion < kResVersionSci11 ? 4 : 0;
|
|
|
|
if ((curVersion < kResVersionSci32 && wCompression > chk)
|
|
|
|
|| (curVersion == kResVersionSci32 && wCompression != 0 && wCompression != 32)
|
2009-05-14 21:47:38 +00:00
|
|
|
|| (wCompression == 0 && dwPacked != dwUnpacked + offs)
|
|
|
|
|| (dwUnpacked < dwPacked - offs)) {
|
|
|
|
|
|
|
|
// Retry with a newer SCI version
|
2009-08-15 00:28:59 +00:00
|
|
|
if (curVersion == kResVersionSci0Sci1Early) {
|
|
|
|
curVersion = kResVersionSci1Late;
|
|
|
|
} else if (curVersion == kResVersionSci1Late) {
|
|
|
|
curVersion = kResVersionSci11;
|
2009-08-30 19:33:34 +00:00
|
|
|
} else if (curVersion == kResVersionSci11 && !sci11Align) {
|
2009-10-08 08:22:19 +00:00
|
|
|
// Later versions (e.g. QFG1VGA) have resources word-aligned
|
2009-08-30 19:33:34 +00:00
|
|
|
sci11Align = true;
|
2009-08-15 00:28:59 +00:00
|
|
|
} else if (curVersion == kResVersionSci11) {
|
|
|
|
curVersion = kResVersionSci32;
|
2009-05-14 21:47:38 +00:00
|
|
|
} else {
|
|
|
|
// All version checks failed, exit loop
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-21 18:12:13 +00:00
|
|
|
fileStream->seek(0);
|
2009-05-14 21:47:38 +00:00
|
|
|
continue;
|
2009-03-07 05:44:36 +00:00
|
|
|
}
|
2009-05-14 21:47:38 +00:00
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
if (curVersion < kResVersionSci11)
|
2009-08-18 14:10:31 +00:00
|
|
|
fileStream->seek(dwPacked - 4, SEEK_CUR);
|
2009-08-15 00:28:59 +00:00
|
|
|
else if (curVersion == kResVersionSci11)
|
2009-08-30 19:33:34 +00:00
|
|
|
fileStream->seek(sci11Align && ((9 + dwPacked) % 2) ? dwPacked + 1 : dwPacked, SEEK_CUR);
|
2009-08-15 00:28:59 +00:00
|
|
|
else if (curVersion == kResVersionSci32)
|
2009-12-27 22:41:33 +00:00
|
|
|
fileStream->seek(dwPacked, SEEK_CUR);
|
2009-03-07 05:44:36 +00:00
|
|
|
}
|
2009-05-14 21:47:38 +00:00
|
|
|
|
2009-08-18 14:10:31 +00:00
|
|
|
delete fileStream;
|
|
|
|
|
2009-05-14 21:47:38 +00:00
|
|
|
if (!failed)
|
|
|
|
return curVersion;
|
2009-03-07 00:59:38 +00:00
|
|
|
|
2009-03-07 05:44:36 +00:00
|
|
|
// Failed to detect volume version
|
2009-08-15 00:28:59 +00:00
|
|
|
return kResVersionUnknown;
|
2009-03-07 00:59:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
// version-agnostic patch application
|
2010-06-07 21:55:57 +00:00
|
|
|
void ResourceManager::processPatch(ResourceSource *source, ResourceType resourceType, uint16 resourceNr, uint32 tuple) {
|
2010-06-02 13:17:36 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2010-06-10 15:06:25 +00:00
|
|
|
Resource *newrsc = 0;
|
2010-06-07 21:55:57 +00:00
|
|
|
ResourceId resId = ResourceId(resourceType, resourceNr, tuple);
|
2010-06-08 21:15:53 +00:00
|
|
|
ResourceType checkForType = resourceType;
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2010-06-07 17:44:17 +00:00
|
|
|
// base36 encoded patches (i.e. audio36 and sync36) have the same type as their non-base36 encoded counterparts
|
2010-06-08 21:15:53 +00:00
|
|
|
if (checkForType == kResourceTypeAudio36)
|
|
|
|
checkForType = kResourceTypeAudio;
|
2010-06-10 15:06:25 +00:00
|
|
|
else if (checkForType == kResourceTypeSync36)
|
2010-06-08 21:15:53 +00:00
|
|
|
checkForType = kResourceTypeSync;
|
2010-06-07 17:44:17 +00:00
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
if (source->resourceFile) {
|
|
|
|
fileStream = source->resourceFile->createReadStream();
|
|
|
|
} else {
|
|
|
|
Common::File *file = new Common::File();
|
|
|
|
if (!file->open(source->location_name)) {
|
|
|
|
warning("ResourceManager::processPatch(): failed to open %s", source->location_name.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fileStream = file;
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
2010-06-10 15:06:25 +00:00
|
|
|
|
|
|
|
int fsize = fileStream->size();
|
2009-03-07 04:49:34 +00:00
|
|
|
if (fsize < 3) {
|
2009-03-11 22:52:54 +00:00
|
|
|
debug("Patching %s failed - file too small", source->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-03-11 01:40:08 +00:00
|
|
|
|
2010-06-10 15:06:25 +00:00
|
|
|
byte patchType = fileStream->readByte() & 0x7F;
|
|
|
|
byte patchDataOffset = fileStream->readByte();
|
2010-06-08 21:15:53 +00:00
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
delete fileStream;
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2010-06-08 21:15:53 +00:00
|
|
|
if (patchType != checkForType) {
|
2010-01-27 17:31:11 +00:00
|
|
|
debug("Patching %s failed - resource type mismatch", source->location_name.c_str());
|
2010-06-08 21:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-11 06:43:01 +00:00
|
|
|
// Fixes SQ5/German, patch file special case logic taken from SCI View disassembly
|
2010-06-07 21:55:57 +00:00
|
|
|
if (patchDataOffset & 0x80) {
|
|
|
|
switch (patchDataOffset & 0x7F) {
|
2009-07-11 06:43:01 +00:00
|
|
|
case 0:
|
2010-06-07 21:55:57 +00:00
|
|
|
patchDataOffset = 24;
|
2009-07-11 06:43:01 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2010-06-07 21:55:57 +00:00
|
|
|
patchDataOffset = 2;
|
2009-07-11 06:43:01 +00:00
|
|
|
break;
|
2010-06-02 13:17:36 +00:00
|
|
|
case 4:
|
2010-06-07 21:55:57 +00:00
|
|
|
patchDataOffset = 8;
|
2010-06-02 13:17:36 +00:00
|
|
|
break;
|
2009-07-11 06:43:01 +00:00
|
|
|
default:
|
2010-06-07 21:55:57 +00:00
|
|
|
warning("Resource patch unsupported special case %X", patchDataOffset & 0x7F);
|
2010-06-02 13:17:36 +00:00
|
|
|
return;
|
2009-07-11 06:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:55:57 +00:00
|
|
|
if (patchDataOffset + 2 >= fsize) {
|
2009-03-07 04:49:34 +00:00
|
|
|
debug("Patching %s failed - patch starting at offset %d can't be in file of size %d",
|
2010-06-07 21:55:57 +00:00
|
|
|
source->location_name.c_str(), patchDataOffset + 2, fsize);
|
2009-03-07 04:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-06-10 15:06:25 +00:00
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
// Prepare destination, if neccessary
|
|
|
|
if (_resMap.contains(resId) == false) {
|
2009-06-12 23:46:23 +00:00
|
|
|
newrsc = new Resource;
|
2009-03-07 04:49:34 +00:00
|
|
|
_resMap.setVal(resId, newrsc);
|
2009-03-11 01:40:08 +00:00
|
|
|
} else
|
2009-03-07 04:49:34 +00:00
|
|
|
newrsc = _resMap.getVal(resId);
|
2010-06-10 15:06:25 +00:00
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
// Overwrite everything, because we're patching
|
2010-02-17 23:37:59 +00:00
|
|
|
newrsc->_id = resId;
|
|
|
|
newrsc->_status = kResStatusNoMalloc;
|
|
|
|
newrsc->_source = source;
|
2010-06-07 21:55:57 +00:00
|
|
|
newrsc->size = fsize - patchDataOffset - 2;
|
|
|
|
newrsc->_headerSize = patchDataOffset;
|
2010-02-17 23:37:59 +00:00
|
|
|
newrsc->_fileOffset = 0;
|
2009-12-27 02:55:39 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "Patching %s - OK", source->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 09:10:22 +00:00
|
|
|
void ResourceManager::readResourcePatchesBase36(ResourceSource *source) {
|
|
|
|
// The base36 encoded audio36 and sync36 resources use a different naming scheme, because they
|
|
|
|
// cannot be described with a single resource number, but are a result of a
|
|
|
|
// <number, noun, verb, cond, seq> tuple. Please don't be confused with the normal audio patches
|
|
|
|
// (*.aud) and normal sync patches (*.syn). audio36 patches can be seen for example in the AUD
|
|
|
|
// folder of GK1CD, and are like this file: @0CS0M00.0X1. GK1CD is the first game where these
|
|
|
|
// have been observed. The actual audio36 and sync36 resources exist in SCI1.1 as well, but the
|
|
|
|
// first game where external patch files for them have been found is GK1CD. The names of these
|
2010-06-07 11:44:52 +00:00
|
|
|
// files are base36 encoded, and we handle their decoding here. audio36 files start with a '@',
|
|
|
|
// whereas sync36 start with a '#'. Mac versions begin with 'A' (probably meaning AIFF). Torin
|
|
|
|
// has several that begin with 'B'.
|
2010-06-07 09:10:22 +00:00
|
|
|
|
2010-06-07 11:44:52 +00:00
|
|
|
Common::String name, inputName;
|
2010-06-07 09:10:22 +00:00
|
|
|
Common::ArchiveMemberList files;
|
2010-06-07 17:44:17 +00:00
|
|
|
ResourceSource *psrcPatch;
|
2010-06-07 09:10:22 +00:00
|
|
|
|
|
|
|
for (int i = kResourceTypeAudio36; i <= kResourceTypeSync36; ++i) {
|
2010-06-07 17:44:17 +00:00
|
|
|
files.clear();
|
|
|
|
|
2010-06-07 11:44:52 +00:00
|
|
|
// audio36 resources start with a @, A, or B
|
2010-06-07 09:10:22 +00:00
|
|
|
// sync36 resources start with a #
|
2010-06-07 11:44:52 +00:00
|
|
|
if (i == kResourceTypeAudio36) {
|
|
|
|
SearchMan.listMatchingMembers(files, "@???????.???");
|
|
|
|
SearchMan.listMatchingMembers(files, "A???????.???");
|
|
|
|
SearchMan.listMatchingMembers(files, "B???????.???");
|
|
|
|
} else
|
|
|
|
SearchMan.listMatchingMembers(files, "#???????.???");
|
2010-06-07 09:10:22 +00:00
|
|
|
|
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
|
|
|
name = (*x)->getName();
|
2010-06-10 15:06:25 +00:00
|
|
|
|
2010-06-07 09:10:22 +00:00
|
|
|
inputName = (*x)->getName();
|
|
|
|
inputName.toUppercase();
|
|
|
|
inputName.deleteChar(0); // delete the first character (type)
|
|
|
|
inputName.deleteChar(7); // delete the dot
|
|
|
|
|
|
|
|
// The base36 encoded resource contains the following:
|
2010-06-07 21:55:57 +00:00
|
|
|
// uint16 resourceId, byte noun, byte verb, byte cond, byte seq
|
2010-06-10 15:06:25 +00:00
|
|
|
uint16 resourceNr = strtol(Common::String(inputName.c_str(), 3).c_str(), 0, 36); // 3 characters
|
|
|
|
uint16 noun = strtol(Common::String(inputName.c_str() + 3, 2).c_str(), 0, 36); // 2 characters
|
|
|
|
uint16 verb = strtol(Common::String(inputName.c_str() + 5, 2).c_str(), 0, 36); // 2 characters
|
|
|
|
uint16 cond = strtol(Common::String(inputName.c_str() + 7, 2).c_str(), 0, 36); // 2 characters
|
|
|
|
uint16 seq = strtol(Common::String(inputName.c_str() + 9, 1).c_str(), 0, 36); // 1 character
|
|
|
|
|
2010-06-08 15:08:45 +00:00
|
|
|
// Check, if we got valid results
|
|
|
|
if ((noun <= 255) && (verb <= 255) && (cond <= 255) && (seq <= 255)) {
|
|
|
|
ResourceId resource36((ResourceType)i, resourceNr, noun, verb, cond, seq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (i == kResourceTypeAudio36)
|
|
|
|
debug("audio36 patch: %s => %s. tuple:%d, %s\n", name.c_str(), inputName.c_str(), resource36.tuple, resource36.toString().c_str());
|
|
|
|
else
|
|
|
|
debug("sync36 patch: %s => %s. tuple:%d, %s\n", name.c_str(), inputName.c_str(), resource36.tuple, resource36.toString().c_str());
|
|
|
|
*/
|
|
|
|
|
2010-06-10 15:06:25 +00:00
|
|
|
// Make sure that the audio patch is a valid resource
|
|
|
|
if (i == kResourceTypeAudio36) {
|
|
|
|
Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(name);
|
|
|
|
uint32 tag = stream->readUint32BE();
|
|
|
|
|
|
|
|
if (tag == MKID_BE('RIFF') || tag == MKID_BE('FORM')) {
|
|
|
|
delete stream;
|
|
|
|
processWavePatch(resource36, name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for SOL as well
|
|
|
|
tag = (tag << 16) | stream->readUint16BE();
|
|
|
|
|
|
|
|
if (tag != MKID_BE('SOL\0')) {
|
|
|
|
delete stream;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete stream;
|
|
|
|
}
|
|
|
|
|
2010-06-08 15:08:45 +00:00
|
|
|
psrcPatch = new ResourceSource;
|
|
|
|
psrcPatch->source_type = kSourcePatch;
|
|
|
|
psrcPatch->location_name = name;
|
|
|
|
processPatch(psrcPatch, (ResourceType)i, resourceNr, resource36.tuple);
|
|
|
|
}
|
2010-06-07 09:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
void ResourceManager::readResourcePatches(ResourceSource *source) {
|
2010-05-27 07:50:23 +00:00
|
|
|
// Note: since some SCI1 games(KQ5 floppy, SQ4) might use SCI0 naming scheme for patch files
|
|
|
|
// this function tries to read patch file with any supported naming scheme,
|
|
|
|
// regardless of s_sciVersion value
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
Common::String mask, name;
|
|
|
|
Common::ArchiveMemberList files;
|
2010-06-07 21:55:57 +00:00
|
|
|
uint16 resourceNr = 0;
|
2009-03-07 04:49:34 +00:00
|
|
|
const char *szResType;
|
|
|
|
ResourceSource *psrcPatch;
|
|
|
|
|
2010-06-07 09:10:22 +00:00
|
|
|
for (int i = kResourceTypeView; i <= kResourceTypeHeap; ++i) {
|
2009-03-07 04:49:34 +00:00
|
|
|
files.clear();
|
|
|
|
szResType = getResourceTypeName((ResourceType)i);
|
|
|
|
// SCI0 naming - type.nnn
|
|
|
|
mask = szResType;
|
|
|
|
mask += ".???";
|
|
|
|
SearchMan.listMatchingMembers(files, mask);
|
|
|
|
// SCI1 and later naming - nnn.typ
|
|
|
|
mask = "*.";
|
2009-06-04 22:16:31 +00:00
|
|
|
mask += resourceTypeSuffixes[i];
|
2009-03-07 04:49:34 +00:00
|
|
|
SearchMan.listMatchingMembers(files, mask);
|
2010-06-07 09:10:22 +00:00
|
|
|
|
2010-01-13 23:58:25 +00:00
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
2009-12-08 20:44:57 +00:00
|
|
|
bool bAdd = false;
|
2009-03-07 04:49:34 +00:00
|
|
|
name = (*x)->getName();
|
2009-03-11 22:52:54 +00:00
|
|
|
// SCI1 scheme
|
2009-03-07 04:49:34 +00:00
|
|
|
if (isdigit(name[0])) {
|
2010-06-07 21:55:57 +00:00
|
|
|
resourceNr = atoi(name.c_str());
|
2009-03-30 21:41:29 +00:00
|
|
|
bAdd = true;
|
2009-03-07 04:49:34 +00:00
|
|
|
} else {
|
|
|
|
// SCI0 scheme
|
|
|
|
int resname_len = strlen(szResType);
|
2009-05-20 17:53:31 +00:00
|
|
|
if (scumm_strnicmp(name.c_str(), szResType, resname_len) == 0
|
2009-03-11 22:52:54 +00:00
|
|
|
&& !isalpha(name[resname_len + 1])) {
|
2010-06-07 21:55:57 +00:00
|
|
|
resourceNr = atoi(name.c_str() + resname_len + 1);
|
2009-03-11 22:52:54 +00:00
|
|
|
bAdd = true;
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 22:52:54 +00:00
|
|
|
|
|
|
|
if (bAdd) {
|
|
|
|
psrcPatch = new ResourceSource;
|
|
|
|
psrcPatch->source_type = kSourcePatch;
|
|
|
|
psrcPatch->location_name = name;
|
2010-06-07 21:55:57 +00:00
|
|
|
processPatch(psrcPatch, (ResourceType)i, resourceNr);
|
2009-03-11 22:52:54 +00:00
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
int ResourceManager::readResourceMapSCI0(ResourceSource *map) {
|
2010-06-05 14:09:52 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2009-03-07 04:49:34 +00:00
|
|
|
Resource *res;
|
|
|
|
ResourceType type;
|
|
|
|
uint16 number, id;
|
|
|
|
uint32 offset;
|
|
|
|
|
2010-06-05 14:09:52 +00:00
|
|
|
if (map->resourceFile) {
|
|
|
|
fileStream = map->resourceFile->createReadStream();
|
|
|
|
if (!fileStream)
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
Common::File *file = new Common::File();
|
|
|
|
if (!file->open(map->location_name))
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2010-06-05 14:09:52 +00:00
|
|
|
fileStream->seek(0, SEEK_SET);
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2009-08-15 00:28:59 +00:00
|
|
|
byte bMask = (_mapVersion == kResVersionSci1Middle) ? 0xF0 : 0xFC;
|
|
|
|
byte bShift = (_mapVersion == kResVersionSci1Middle) ? 28 : 26;
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
do {
|
2010-06-05 14:09:52 +00:00
|
|
|
id = fileStream->readUint16LE();
|
|
|
|
offset = fileStream->readUint32LE();
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2010-06-05 14:09:52 +00:00
|
|
|
if (fileStream->eos() || fileStream->err()) {
|
|
|
|
delete fileStream;
|
2009-07-28 22:28:40 +00:00
|
|
|
warning("Error while reading %s", map->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
}
|
2009-03-11 01:40:08 +00:00
|
|
|
if (offset == 0xFFFFFFFF)
|
2009-03-07 04:49:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
type = (ResourceType)(id >> 11);
|
|
|
|
number = id & 0x7FF;
|
2009-06-07 02:18:38 +00:00
|
|
|
ResourceId resId = ResourceId(type, number);
|
2009-03-07 04:49:34 +00:00
|
|
|
// adding a new resource
|
|
|
|
if (_resMap.contains(resId) == false) {
|
|
|
|
res = new Resource;
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_source = getVolume(map, offset >> bShift);
|
|
|
|
if (!res->_source) {
|
2009-08-20 19:49:52 +00:00
|
|
|
warning("Could not get volume for resource %d, VolumeID %d", id, offset >> bShift);
|
2009-08-26 09:38:51 +00:00
|
|
|
if (_mapVersion != _volVersion) {
|
|
|
|
warning("Retrying with the detected volume version instead");
|
|
|
|
warning("Map version was: %d, retrying with: %d", _mapVersion, _volVersion);
|
|
|
|
_mapVersion = _volVersion;
|
|
|
|
bMask = (_mapVersion == kResVersionSci1Middle) ? 0xF0 : 0xFC;
|
|
|
|
bShift = (_mapVersion == kResVersionSci1Middle) ? 28 : 26;
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_source = getVolume(map, offset >> bShift);
|
2009-08-26 09:38:51 +00:00
|
|
|
}
|
2009-07-09 15:54:18 +00:00
|
|
|
}
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_fileOffset = offset & (((~bMask) << 24) | 0xFFFFFF);
|
|
|
|
res->_id = resId;
|
2009-03-07 04:49:34 +00:00
|
|
|
_resMap.setVal(resId, res);
|
|
|
|
}
|
2010-06-05 14:09:52 +00:00
|
|
|
} while (!fileStream->eos());
|
|
|
|
|
|
|
|
delete fileStream;
|
2009-03-07 04:49:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-26 13:11:30 +00:00
|
|
|
int ResourceManager::readResourceMapSCI1(ResourceSource *map) {
|
2010-06-05 14:09:52 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2009-03-07 04:49:34 +00:00
|
|
|
Resource *res;
|
2010-06-05 14:09:52 +00:00
|
|
|
|
|
|
|
if (map->resourceFile) {
|
|
|
|
fileStream = map->resourceFile->createReadStream();
|
|
|
|
if (!fileStream)
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
Common::File *file = new Common::File();
|
|
|
|
if (!file->open(map->location_name))
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2009-03-07 14:40:58 +00:00
|
|
|
resource_index_t resMap[32];
|
|
|
|
memset(resMap, 0, sizeof(resource_index_t) * 32);
|
2009-03-07 04:49:34 +00:00
|
|
|
byte type = 0, prevtype = 0;
|
2009-08-15 00:28:59 +00:00
|
|
|
byte nEntrySize = _mapVersion == kResVersionSci11 ? SCI11_RESMAP_ENTRIES_SIZE : SCI1_RESMAP_ENTRIES_SIZE;
|
2009-06-07 02:18:38 +00:00
|
|
|
ResourceId resId;
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
// Read resource type and offsets to resource offsets block from .MAP file
|
|
|
|
// The last entry has type=0xFF (0x1F) and offset equals to map file length
|
|
|
|
do {
|
2010-06-05 14:09:52 +00:00
|
|
|
type = fileStream->readByte() & 0x1F;
|
|
|
|
resMap[type].wOffset = fileStream->readUint16LE();
|
2009-03-07 04:49:34 +00:00
|
|
|
resMap[prevtype].wSize = (resMap[type].wOffset
|
2009-03-11 01:40:08 +00:00
|
|
|
- resMap[prevtype].wOffset) / nEntrySize;
|
2009-03-07 04:49:34 +00:00
|
|
|
prevtype = type;
|
|
|
|
} while (type != 0x1F); // the last entry is FF
|
|
|
|
|
|
|
|
// reading each type's offsets
|
|
|
|
uint32 off = 0;
|
2009-03-07 14:40:58 +00:00
|
|
|
for (type = 0; type < 32; type++) {
|
2009-03-07 04:49:34 +00:00
|
|
|
if (resMap[type].wOffset == 0) // this resource does not exist in map
|
|
|
|
continue;
|
2010-06-05 14:09:52 +00:00
|
|
|
fileStream->seek(resMap[type].wOffset);
|
2009-03-07 04:49:34 +00:00
|
|
|
for (int i = 0; i < resMap[type].wSize; i++) {
|
2010-06-05 14:09:52 +00:00
|
|
|
uint16 number = fileStream->readUint16LE();
|
2009-04-07 13:38:51 +00:00
|
|
|
int volume_nr = 0;
|
2009-08-15 00:28:59 +00:00
|
|
|
if (_mapVersion == kResVersionSci11) {
|
2009-04-07 13:38:51 +00:00
|
|
|
// offset stored in 3 bytes
|
2010-06-05 14:09:52 +00:00
|
|
|
off = fileStream->readUint16LE();
|
|
|
|
off |= fileStream->readByte() << 16;
|
2009-04-07 13:38:51 +00:00
|
|
|
off <<= 1;
|
|
|
|
} else {
|
|
|
|
// offset/volume stored in 4 bytes
|
2010-06-05 14:09:52 +00:00
|
|
|
off = fileStream->readUint32LE();
|
2009-08-15 00:28:59 +00:00
|
|
|
if (_mapVersion < kResVersionSci11) {
|
2009-04-07 13:38:51 +00:00
|
|
|
volume_nr = off >> 28; // most significant 4 bits
|
|
|
|
off &= 0x0FFFFFFF; // least significant 28 bits
|
|
|
|
} else {
|
|
|
|
// in SCI32 it's a plain offset
|
|
|
|
}
|
|
|
|
}
|
2010-06-05 14:09:52 +00:00
|
|
|
if (fileStream->eos() || fileStream->err()) {
|
|
|
|
delete fileStream;
|
2009-07-28 22:28:40 +00:00
|
|
|
warning("Error while reading %s", map->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
}
|
2009-06-07 02:18:38 +00:00
|
|
|
resId = ResourceId((ResourceType)type, number);
|
2009-03-07 04:49:34 +00:00
|
|
|
// adding new resource only if it does not exist
|
|
|
|
if (_resMap.contains(resId) == false) {
|
2009-06-12 23:46:23 +00:00
|
|
|
res = new Resource;
|
2009-03-07 04:49:34 +00:00
|
|
|
_resMap.setVal(resId, res);
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_id = resId;
|
2010-01-27 04:26:28 +00:00
|
|
|
|
|
|
|
// NOTE: We add the map's volume number here to the specified volume number
|
2010-06-01 22:06:52 +00:00
|
|
|
// for SCI2.1 and SCI3 maps that are not resmap.000. The resmap.* files' numbers
|
2010-01-27 04:26:28 +00:00
|
|
|
// need to be used in concurrence with the volume specified in the map to get
|
|
|
|
// the actual resource file.
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_source = getVolume(map, volume_nr + map->volume_number);
|
|
|
|
res->_fileOffset = off;
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-05 14:09:52 +00:00
|
|
|
|
|
|
|
delete fileStream;
|
2009-03-07 04:49:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-18 04:17:58 +00:00
|
|
|
struct {
|
|
|
|
uint32 tag;
|
|
|
|
ResourceType type;
|
|
|
|
} static const macResTagMap[] = {
|
|
|
|
{ MKID_BE('V56 '), kResourceTypeView },
|
|
|
|
{ MKID_BE('P56 '), kResourceTypePic },
|
|
|
|
{ MKID_BE('SCR '), kResourceTypeScript },
|
|
|
|
{ MKID_BE('TEX '), kResourceTypeText },
|
|
|
|
{ MKID_BE('SND '), kResourceTypeSound },
|
|
|
|
{ MKID_BE('VOC '), kResourceTypeVocab },
|
|
|
|
{ MKID_BE('FON '), kResourceTypeFont },
|
|
|
|
{ MKID_BE('CURS'), kResourceTypeCursor },
|
|
|
|
{ MKID_BE('crsr'), kResourceTypeCursor },
|
|
|
|
{ MKID_BE('Pat '), kResourceTypePatch },
|
|
|
|
{ MKID_BE('PAL '), kResourceTypePalette },
|
|
|
|
{ MKID_BE('snd '), kResourceTypeAudio },
|
|
|
|
{ MKID_BE('MSG '), kResourceTypeMessage },
|
2010-05-24 17:21:11 +00:00
|
|
|
{ MKID_BE('HEP '), kResourceTypeHeap },
|
|
|
|
{ MKID_BE('IBIN'), kResourceTypeMacIconBarPictN },
|
|
|
|
{ MKID_BE('IBIS'), kResourceTypeMacIconBarPictS },
|
2010-05-27 03:15:24 +00:00
|
|
|
{ MKID_BE('PICT'), kResourceTypeMacPict },
|
|
|
|
{ MKID_BE('SYN '), kResourceTypeSync }
|
2010-05-10 18:29:13 +00:00
|
|
|
};
|
|
|
|
|
2010-05-13 13:32:12 +00:00
|
|
|
static uint32 resTypeToMacTag(ResourceType type) {
|
2010-05-18 04:17:58 +00:00
|
|
|
for (uint32 i = 0; i < ARRAYSIZE(macResTagMap); i++)
|
|
|
|
if (macResTagMap[i].type == type)
|
|
|
|
return macResTagMap[i].tag;
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2010-05-18 04:17:58 +00:00
|
|
|
return 0;
|
2010-05-10 18:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceManager::readMacResourceFork(ResourceSource *source) {
|
|
|
|
if (!source->macResMan.open(source->location_name.c_str()))
|
|
|
|
error("%s is not a valid Mac resource fork", source->location_name.c_str());
|
|
|
|
|
|
|
|
Common::MacResTagArray tagArray = source->macResMan.getResTagArray();
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < tagArray.size(); i++) {
|
|
|
|
ResourceType type = kResourceTypeInvalid;
|
|
|
|
|
|
|
|
// Map the Mac tags to our ResourceType
|
2010-05-18 04:17:58 +00:00
|
|
|
for (uint32 j = 0; j < ARRAYSIZE(macResTagMap); j++)
|
|
|
|
if (tagArray[i] == macResTagMap[j].tag) {
|
|
|
|
type = macResTagMap[j].type;
|
2010-05-10 18:29:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == kResourceTypeInvalid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Common::MacResIDArray idArray = source->macResMan.getResIDArray(tagArray[i]);
|
|
|
|
|
|
|
|
for (uint32 j = 0; j < idArray.size(); j++) {
|
2010-05-24 17:21:11 +00:00
|
|
|
// Get the size of the file
|
|
|
|
Common::SeekableReadStream *stream = source->macResMan.getResource(tagArray[i], idArray[j]);
|
|
|
|
|
|
|
|
// Some IBIS resources have a size of 0, so we skip them
|
|
|
|
if (!stream)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint32 fileSize = stream->size();
|
|
|
|
delete stream;
|
|
|
|
|
2010-05-10 18:29:13 +00:00
|
|
|
ResourceId resId = ResourceId(type, idArray[j]);
|
|
|
|
|
|
|
|
Resource *newrsc = NULL;
|
|
|
|
|
2010-05-18 04:17:58 +00:00
|
|
|
// Prepare destination, if neccessary. Resource forks may contain patches.
|
2010-05-10 18:29:13 +00:00
|
|
|
if (!_resMap.contains(resId)) {
|
|
|
|
newrsc = new Resource;
|
|
|
|
_resMap.setVal(resId, newrsc);
|
|
|
|
} else
|
|
|
|
newrsc = _resMap.getVal(resId);
|
|
|
|
|
2010-05-18 04:17:58 +00:00
|
|
|
// Overwrite everything
|
2010-05-10 18:29:13 +00:00
|
|
|
newrsc->_id = resId;
|
|
|
|
newrsc->_status = kResStatusNoMalloc;
|
|
|
|
newrsc->_source = source;
|
|
|
|
newrsc->size = fileSize;
|
|
|
|
newrsc->_headerSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
void ResourceManager::addResource(ResourceId resId, ResourceSource *src, uint32 offset, uint32 size) {
|
|
|
|
// Adding new resource only if it does not exist
|
|
|
|
if (_resMap.contains(resId) == false) {
|
2009-06-12 23:46:23 +00:00
|
|
|
Resource *res = new Resource;
|
2009-06-07 19:15:55 +00:00
|
|
|
_resMap.setVal(resId, res);
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_id = resId;
|
|
|
|
res->_source = src;
|
|
|
|
res->_fileOffset = offset;
|
2009-06-07 19:15:55 +00:00
|
|
|
res->size = size;
|
|
|
|
}
|
|
|
|
}
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2010-05-10 18:29:13 +00:00
|
|
|
int ResourceManager::readResourceInfo(Resource *res, Common::SeekableReadStream *file,
|
2009-03-11 01:40:08 +00:00
|
|
|
uint32&szPacked, ResourceCompression &compression) {
|
2009-03-10 21:44:03 +00:00
|
|
|
// SCI0 volume format: {wResId wPacked+4 wUnpacked wCompression} = 8 bytes
|
|
|
|
// SCI1 volume format: {bResType wResNumber wPacked+4 wUnpacked wCompression} = 9 bytes
|
|
|
|
// SCI1.1 volume format: {bResType wResNumber wPacked wUnpacked wCompression} = 9 bytes
|
|
|
|
// SCI32 volume format : {bResType wResNumber dwPacked dwUnpacked wCompression} = 13 bytes
|
2010-01-03 04:46:00 +00:00
|
|
|
uint16 w, number;
|
|
|
|
uint32 wCompression, szUnpacked;
|
2009-03-10 21:44:03 +00:00
|
|
|
ResourceType type;
|
|
|
|
|
|
|
|
switch (_volVersion) {
|
2009-08-15 00:28:59 +00:00
|
|
|
case kResVersionSci0Sci1Early:
|
|
|
|
case kResVersionSci1Middle:
|
2009-03-10 21:44:03 +00:00
|
|
|
w = file->readUint16LE();
|
|
|
|
type = (ResourceType)(w >> 11);
|
|
|
|
number = w & 0x7FF;
|
|
|
|
szPacked = file->readUint16LE() - 4;
|
|
|
|
szUnpacked = file->readUint16LE();
|
|
|
|
wCompression = file->readUint16LE();
|
|
|
|
break;
|
2009-08-15 00:28:59 +00:00
|
|
|
case kResVersionSci1Late:
|
2009-03-11 04:38:54 +00:00
|
|
|
type = (ResourceType)(file->readByte() & 0x7F);
|
2009-03-10 21:44:03 +00:00
|
|
|
number = file->readUint16LE();
|
|
|
|
szPacked = file->readUint16LE() - 4;
|
|
|
|
szUnpacked = file->readUint16LE();
|
|
|
|
wCompression = file->readUint16LE();
|
|
|
|
break;
|
2009-08-15 00:28:59 +00:00
|
|
|
case kResVersionSci11:
|
2009-03-11 04:38:54 +00:00
|
|
|
type = (ResourceType)(file->readByte() & 0x7F);
|
2009-03-10 21:44:03 +00:00
|
|
|
number = file->readUint16LE();
|
|
|
|
szPacked = file->readUint16LE();
|
|
|
|
szUnpacked = file->readUint16LE();
|
|
|
|
wCompression = file->readUint16LE();
|
|
|
|
break;
|
2010-05-10 18:29:13 +00:00
|
|
|
case kResVersionSci11Mac:
|
|
|
|
// Doesn't store this data in the resource. Fortunately,
|
|
|
|
// we already have this data.
|
|
|
|
type = res->_id.type;
|
|
|
|
number = res->_id.number;
|
|
|
|
szPacked = file->size();
|
|
|
|
szUnpacked = file->size();
|
|
|
|
wCompression = 0;
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2009-08-15 00:28:59 +00:00
|
|
|
case kResVersionSci32:
|
2009-08-25 23:02:57 +00:00
|
|
|
type = (ResourceType)(file->readByte() & 0x7F);
|
2009-03-10 21:44:03 +00:00
|
|
|
number = file->readUint16LE();
|
|
|
|
szPacked = file->readUint32LE();
|
|
|
|
szUnpacked = file->readUint32LE();
|
|
|
|
wCompression = file->readUint16LE();
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#endif
|
2009-03-10 21:44:03 +00:00
|
|
|
default:
|
|
|
|
return SCI_ERROR_INVALID_RESMAP_ENTRY;
|
|
|
|
}
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
// check if there were errors while reading
|
2009-12-15 08:18:57 +00:00
|
|
|
if ((file->eos() || file->err()))
|
2009-03-10 21:44:03 +00:00
|
|
|
return SCI_ERROR_IO_ERROR;
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_id = ResourceId(type, number);
|
2009-03-10 21:44:03 +00:00
|
|
|
res->size = szUnpacked;
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
// checking compression method
|
2009-03-14 04:17:03 +00:00
|
|
|
switch (wCompression) {
|
|
|
|
case 0:
|
2009-03-10 21:44:03 +00:00
|
|
|
compression = kCompNone;
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-03-14 04:17:03 +00:00
|
|
|
case 1:
|
2009-09-23 10:55:35 +00:00
|
|
|
compression = (getSciVersion() <= SCI_VERSION_01) ? kCompLZW : kCompHuffman;
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-03-14 04:17:03 +00:00
|
|
|
case 2:
|
2009-09-23 10:55:35 +00:00
|
|
|
compression = (getSciVersion() <= SCI_VERSION_01) ? kCompHuffman : kCompLZW1;
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-03-14 04:17:03 +00:00
|
|
|
case 3:
|
|
|
|
compression = kCompLZW1View;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
compression = kCompLZW1Pic;
|
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
case 19:
|
|
|
|
case 20:
|
|
|
|
compression = kCompDCL;
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2009-03-14 04:17:03 +00:00
|
|
|
case 32:
|
|
|
|
compression = kCompSTACpack;
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#endif
|
2009-03-11 01:40:08 +00:00
|
|
|
default:
|
|
|
|
compression = kCompUnknown;
|
2009-03-10 21:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return compression == kCompUnknown ? SCI_ERROR_UNKNOWN_COMPRESSION : 0;
|
|
|
|
}
|
|
|
|
|
2010-05-10 18:29:13 +00:00
|
|
|
int ResourceManager::decompress(Resource *res, Common::SeekableReadStream *file) {
|
2009-03-10 21:44:03 +00:00
|
|
|
int error;
|
|
|
|
uint32 szPacked = 0;
|
|
|
|
ResourceCompression compression = kCompUnknown;
|
|
|
|
|
|
|
|
// fill resource info
|
|
|
|
error = readResourceInfo(res, file, szPacked, compression);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2010-05-10 18:29:13 +00:00
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
// getting a decompressor
|
|
|
|
Decompressor *dec = NULL;
|
|
|
|
switch (compression) {
|
|
|
|
case kCompNone:
|
|
|
|
dec = new Decompressor;
|
|
|
|
break;
|
|
|
|
case kCompHuffman:
|
|
|
|
dec = new DecompressorHuffman;
|
|
|
|
break;
|
2009-03-14 04:17:03 +00:00
|
|
|
case kCompLZW:
|
|
|
|
case kCompLZW1:
|
|
|
|
case kCompLZW1View:
|
|
|
|
case kCompLZW1Pic:
|
|
|
|
dec = new DecompressorLZW(compression);
|
2009-03-10 21:44:03 +00:00
|
|
|
break;
|
|
|
|
case kCompDCL:
|
|
|
|
dec = new DecompressorDCL;
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2009-03-14 04:17:03 +00:00
|
|
|
case kCompSTACpack:
|
|
|
|
dec = new DecompressorLZS;
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#endif
|
2009-03-10 21:44:03 +00:00
|
|
|
default:
|
2010-02-17 23:37:59 +00:00
|
|
|
warning("Resource %s: Compression method %d not supported", res->_id.toString().c_str(), compression);
|
2009-03-14 04:17:03 +00:00
|
|
|
return SCI_ERROR_UNKNOWN_COMPRESSION;
|
2009-03-10 21:44:03 +00:00
|
|
|
}
|
2009-03-11 01:40:08 +00:00
|
|
|
|
2009-03-14 04:17:03 +00:00
|
|
|
res->data = new byte[res->size];
|
2010-02-17 23:37:59 +00:00
|
|
|
res->_status = kResStatusAllocated;
|
2009-03-14 04:17:03 +00:00
|
|
|
error = res->data ? dec->unpack(file, res->data, szPacked, res->size) : SCI_ERROR_RESOURCE_TOO_BIG;
|
|
|
|
if (error)
|
|
|
|
res->unalloc();
|
2009-03-11 01:40:08 +00:00
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
delete dec;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-08-16 19:18:19 +00:00
|
|
|
ResourceCompression ResourceManager::getViewCompression() {
|
|
|
|
int viewsTested = 0;
|
|
|
|
|
|
|
|
// Test 10 views to see if any are compressed
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2010-06-05 14:09:52 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
2009-08-16 19:18:19 +00:00
|
|
|
Resource *res = testResource(ResourceId(kResourceTypeView, i));
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
continue;
|
|
|
|
|
2010-02-17 23:37:59 +00:00
|
|
|
if (res->_source->source_type != kSourceVolume)
|
2009-08-16 19:18:19 +00:00
|
|
|
continue;
|
|
|
|
|
2010-06-06 13:04:24 +00:00
|
|
|
fileStream = getVolumeFile(res->_source);
|
2010-06-05 14:09:52 +00:00
|
|
|
|
|
|
|
if (!fileStream)
|
2009-08-16 19:18:19 +00:00
|
|
|
continue;
|
2010-06-05 14:09:52 +00:00
|
|
|
fileStream->seek(res->_fileOffset, SEEK_SET);
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
uint32 szPacked;
|
|
|
|
ResourceCompression compression;
|
|
|
|
|
2010-06-05 14:09:52 +00:00
|
|
|
if (readResourceInfo(res, fileStream, szPacked, compression)) {
|
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
2009-08-16 19:18:19 +00:00
|
|
|
continue;
|
2010-06-05 14:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res->_source->resourceFile)
|
|
|
|
delete fileStream;
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
if (compression != kCompNone)
|
|
|
|
return compression;
|
|
|
|
|
|
|
|
if (++viewsTested == 10)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kCompNone;
|
|
|
|
}
|
|
|
|
|
2009-08-19 21:08:17 +00:00
|
|
|
ViewType ResourceManager::detectViewType() {
|
2009-08-16 19:18:19 +00:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypeView, i), 0);
|
2009-08-19 21:08:17 +00:00
|
|
|
|
2009-08-16 19:18:19 +00:00
|
|
|
if (res) {
|
2009-09-30 16:16:53 +00:00
|
|
|
switch (res->data[1]) {
|
2009-08-19 21:08:17 +00:00
|
|
|
case 128:
|
|
|
|
// If the 2nd byte is 128, it's a VGA game
|
2009-08-16 19:18:19 +00:00
|
|
|
return kViewVga;
|
2009-08-19 21:08:17 +00:00
|
|
|
case 0:
|
|
|
|
// EGA or Amiga, try to read as Amiga view
|
|
|
|
|
|
|
|
if (res->size < 10)
|
|
|
|
return kViewUnknown;
|
|
|
|
|
|
|
|
// Read offset of first loop
|
|
|
|
uint16 offset = READ_LE_UINT16(res->data + 8);
|
|
|
|
|
|
|
|
if (offset + 6U >= res->size)
|
|
|
|
return kViewUnknown;
|
|
|
|
|
|
|
|
// Read offset of first cel
|
|
|
|
offset = READ_LE_UINT16(res->data + offset + 4);
|
|
|
|
|
|
|
|
if (offset + 4U >= res->size)
|
|
|
|
return kViewUnknown;
|
|
|
|
|
|
|
|
// Check palette offset, amiga views have no palette
|
|
|
|
if (READ_LE_UINT16(res->data + 6) != 0)
|
|
|
|
return kViewEga;
|
|
|
|
|
|
|
|
uint16 width = READ_LE_UINT16(res->data + offset);
|
|
|
|
offset += 2;
|
|
|
|
uint16 height = READ_LE_UINT16(res->data + offset);
|
|
|
|
offset += 6;
|
|
|
|
|
2010-01-08 13:45:44 +00:00
|
|
|
// To improve the heuristic, we skip very small views
|
|
|
|
if (height < 10)
|
|
|
|
continue;
|
|
|
|
|
2009-08-19 21:08:17 +00:00
|
|
|
// Check that the RLE data stays within bounds
|
|
|
|
int y;
|
|
|
|
for (y = 0; y < height; y++) {
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
while ((x < width) && (offset < res->size)) {
|
|
|
|
byte op = res->data[offset++];
|
|
|
|
x += (op & 0x07) ? op & 0x07 : op >> 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got exactly the right number of pixels for this row
|
|
|
|
if (x != width)
|
|
|
|
return kViewEga;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kViewAmiga;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-02 12:02:37 +00:00
|
|
|
warning("resMan: Couldn't find any views");
|
2009-08-19 21:08:17 +00:00
|
|
|
return kViewUnknown;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 10:55:35 +00:00
|
|
|
void ResourceManager::detectSciVersion() {
|
2009-09-23 12:12:37 +00:00
|
|
|
// We use the view compression to set a preliminary s_sciVersion for the sake of getResourceInfo
|
2009-08-16 19:18:19 +00:00
|
|
|
// Pretend we have a SCI0 game
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_0_EARLY;
|
2009-08-16 19:18:19 +00:00
|
|
|
bool oldDecompressors = true;
|
|
|
|
|
|
|
|
ResourceCompression viewCompression = getViewCompression();
|
|
|
|
if (viewCompression != kCompLZW) {
|
|
|
|
// If it's a different compression type from kCompLZW, the game is probably
|
|
|
|
// SCI_VERSION_1_EGA or later. If the views are uncompressed, it is
|
|
|
|
// likely not an early disk game.
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_EGA;
|
2009-08-16 19:18:19 +00:00
|
|
|
oldDecompressors = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set view type
|
2009-12-27 12:17:43 +00:00
|
|
|
if (viewCompression == kCompDCL
|
2010-01-27 22:55:30 +00:00
|
|
|
|| _volVersion == kResVersionSci11 // pq4demo
|
2010-05-18 04:17:58 +00:00
|
|
|
|| _volVersion == kResVersionSci11Mac
|
2009-08-25 23:15:18 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
|| viewCompression == kCompSTACpack
|
2010-01-26 15:22:04 +00:00
|
|
|
|| _volVersion == kResVersionSci32 // kq7
|
2009-08-25 23:15:18 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2009-08-16 19:18:19 +00:00
|
|
|
// SCI1.1 VGA views
|
|
|
|
_viewType = kViewVga11;
|
|
|
|
} else {
|
|
|
|
// Otherwise we detect it from a view
|
|
|
|
_viewType = detectViewType();
|
|
|
|
}
|
2010-05-10 18:29:13 +00:00
|
|
|
|
|
|
|
if (_volVersion == kResVersionSci11Mac) {
|
|
|
|
// SCI32 doesn't have the resource.cfg file, so we can figure out
|
|
|
|
// which of the games are SCI1.1.
|
|
|
|
// TODO: Decide between SCI2 and SCI2.1
|
|
|
|
if (Common::File::exists("resource.cfg"))
|
|
|
|
s_sciVersion = SCI_VERSION_1_1;
|
2010-05-18 04:17:58 +00:00
|
|
|
else if (Common::File::exists("Patches"))
|
|
|
|
s_sciVersion = SCI_VERSION_2_1;
|
2010-05-10 18:29:13 +00:00
|
|
|
else
|
|
|
|
s_sciVersion = SCI_VERSION_2;
|
|
|
|
return;
|
|
|
|
}
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2009-08-25 23:02:57 +00:00
|
|
|
// Handle SCI32 versions here
|
|
|
|
if (_volVersion == kResVersionSci32) {
|
|
|
|
// SCI2.1/3 and SCI1 Late resource maps are the same, except that
|
|
|
|
// SCI1 Late resource maps have the resource types or'd with
|
|
|
|
// 0x80. We differentiate between SCI2 and SCI2.1/3 based on that.
|
|
|
|
// TODO: Differentiate between SCI2.1 and SCI3
|
2009-09-23 10:55:35 +00:00
|
|
|
if (_mapVersion == kResVersionSci1Late) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_2;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_2_1;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-08-25 23:02:57 +00:00
|
|
|
}
|
2009-08-16 19:18:19 +00:00
|
|
|
|
2009-10-26 21:41:41 +00:00
|
|
|
// Check for transitive SCI1/SCI1.1 games, like PQ1 here
|
|
|
|
// If the game has any heap file (here we check for heap file 0), then
|
|
|
|
// it definitely uses a SCI1.1 kernel
|
|
|
|
if (testResource(ResourceId(kResourceTypeHeap, 0))) {
|
|
|
|
s_sciVersion = SCI_VERSION_1_1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-16 19:18:19 +00:00
|
|
|
switch (_mapVersion) {
|
|
|
|
case kResVersionSci0Sci1Early:
|
|
|
|
if (_viewType == kViewVga) {
|
|
|
|
// VGA
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_EARLY;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EGA
|
2009-09-23 10:55:35 +00:00
|
|
|
if (hasOldScriptHeader()) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_0_EARLY;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
if (oldDecompressors) {
|
|
|
|
// It's either SCI_VERSION_0_LATE or SCI_VERSION_01
|
|
|
|
|
|
|
|
// We first check for SCI1 vocab.999
|
|
|
|
if (testResource(ResourceId(kResourceTypeVocab, 999))) {
|
|
|
|
if (hasSci0Voc999()) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_0_LATE;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
} else {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_01;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If vocab.999 is missing, we try vocab.900
|
|
|
|
if (testResource(ResourceId(kResourceTypeVocab, 900))) {
|
|
|
|
if (hasSci1Voc900()) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_01;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
} else {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_0_LATE;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
warning("Failed to accurately determine SCI version");
|
|
|
|
// No parser, we assume SCI_VERSION_01.
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_01;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New decompressors. It's either SCI_VERSION_1_EGA or SCI_VERSION_1_EARLY.
|
2009-09-23 10:55:35 +00:00
|
|
|
if (hasSci1Voc900()) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_EGA;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
// SCI_VERSION_1_EARLY EGA versions seem to be lacking a valid vocab.900.
|
|
|
|
// If this turns out to be unreliable, we could do some pic resource checks instead.
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_EARLY;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
case kResVersionSci1Middle:
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_MIDDLE;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
case kResVersionSci1Late:
|
2009-09-23 10:55:35 +00:00
|
|
|
if (_volVersion == kResVersionSci11) {
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_1;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_LATE;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
case kResVersionSci11:
|
2009-09-23 12:12:37 +00:00
|
|
|
s_sciVersion = SCI_VERSION_1_1;
|
2009-09-23 10:55:35 +00:00
|
|
|
return;
|
2009-08-16 19:18:19 +00:00
|
|
|
default:
|
2010-01-03 22:50:39 +00:00
|
|
|
s_sciVersion = SCI_VERSION_NONE;
|
2009-09-23 10:55:35 +00:00
|
|
|
error("detectSciVersion(): Unable to detect the game's SCI version");
|
2009-08-16 19:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 21:01:57 +00:00
|
|
|
bool ResourceManager::detectHires() {
|
|
|
|
// SCI 1.1 and prior is never hires
|
|
|
|
if (getSciVersion() <= SCI_VERSION_1_1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
for (int i = 0; i < 32768; i++) {
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypePic, i), 0);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
if (READ_LE_UINT16(res->data) == 0x0e) {
|
|
|
|
// SCI32 picture
|
2010-02-03 21:59:41 +00:00
|
|
|
uint16 width = READ_LE_UINT16(res->data + 10);
|
|
|
|
uint16 height = READ_LE_UINT16(res->data + 12);
|
2010-01-28 21:01:57 +00:00
|
|
|
if ((width == 320) && ((height == 190) || (height == 200)))
|
|
|
|
return false;
|
|
|
|
if ((width >= 600) || (height >= 400))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
warning("resMan: Couldn't detect hires");
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
error("no sci32 support");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-04-17 20:43:09 +00:00
|
|
|
bool ResourceManager::detectFontExtended() {
|
|
|
|
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypeFont, 0), 0);
|
|
|
|
if (res) {
|
|
|
|
if (res->size >= 4) {
|
|
|
|
uint16 numChars = READ_LE_UINT16(res->data + 2);
|
|
|
|
if (numChars > 0x80)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-16 19:18:19 +00:00
|
|
|
// Functions below are based on PD code by Brian Provinciano (SCI Studio)
|
|
|
|
bool ResourceManager::hasOldScriptHeader() {
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypeScript, 0), 0);
|
|
|
|
|
|
|
|
if (!res) {
|
2009-09-02 12:02:37 +00:00
|
|
|
warning("resMan: Failed to find script.000");
|
2009-08-16 19:18:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint offset = 2;
|
|
|
|
const int objTypes = 17;
|
|
|
|
|
|
|
|
while (offset < res->size) {
|
|
|
|
uint16 objType = READ_LE_UINT16(res->data + offset);
|
|
|
|
|
|
|
|
if (!objType) {
|
|
|
|
offset += 2;
|
|
|
|
// We should be at the end of the resource now
|
|
|
|
return offset == res->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objType >= objTypes) {
|
|
|
|
// Invalid objType
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int skip = READ_LE_UINT16(res->data + offset + 2);
|
|
|
|
|
|
|
|
if (skip < 2) {
|
|
|
|
// Invalid size
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += skip;
|
|
|
|
}
|
|
|
|
|
2009-08-16 19:20:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-16 19:18:19 +00:00
|
|
|
|
|
|
|
bool ResourceManager::hasSci0Voc999() {
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypeVocab, 999), 0);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
// No vocab present, possibly a demo version
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res->size < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint16 count = READ_LE_UINT16(res->data);
|
|
|
|
|
|
|
|
// Make sure there's enough room for the pointers
|
|
|
|
if (res->size < (uint)count * 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Iterate over all pointers
|
|
|
|
for (uint i = 0; i < count; i++) {
|
|
|
|
// Offset to string
|
|
|
|
uint16 offset = READ_LE_UINT16(res->data + 2 + count * 2);
|
|
|
|
|
|
|
|
// Look for end of string
|
|
|
|
do {
|
|
|
|
if (offset >= res->size) {
|
|
|
|
// Out of bounds
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (res->data[offset++]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::hasSci1Voc900() {
|
|
|
|
Resource *res = findResource(ResourceId(kResourceTypeVocab, 900), 0);
|
|
|
|
|
|
|
|
if (!res )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (res->size < 0x1fe)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint16 offset = 0x1fe;
|
|
|
|
|
|
|
|
while (offset < res->size) {
|
|
|
|
offset++;
|
|
|
|
do {
|
|
|
|
if (offset >= res->size) {
|
|
|
|
// Out of bounds;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (res->data[offset++]);
|
|
|
|
offset += 3;
|
|
|
|
}
|
|
|
|
|
2009-08-16 19:20:09 +00:00
|
|
|
return offset == res->size;
|
|
|
|
}
|
2009-08-16 19:18:19 +00:00
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
// Same function as Script::findBlock(). Slight code
|
|
|
|
// duplication here, but this has been done to keep the resource
|
|
|
|
// manager independent from the rest of the engine
|
|
|
|
static byte *findSci0ExportsBlock(byte *buffer) {
|
|
|
|
byte *buf = buffer;
|
|
|
|
bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
|
|
|
|
|
|
|
|
if (oldScriptHeader)
|
|
|
|
buf += 2;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int seekerType = READ_LE_UINT16(buf);
|
|
|
|
|
|
|
|
if (seekerType == 0)
|
|
|
|
break;
|
|
|
|
if (seekerType == 7) // exports
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
int seekerSize = READ_LE_UINT16(buf + 2);
|
|
|
|
assert(seekerSize > 0);
|
|
|
|
buf += seekerSize;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-23 10:28:03 +00:00
|
|
|
|
2010-05-29 14:03:08 +00:00
|
|
|
reg_t ResourceManager::findGameObject(bool addSci11ScriptOffset) {
|
2010-05-23 10:28:03 +00:00
|
|
|
Resource *script = findResource(ResourceId(kResourceTypeScript, 0), false);
|
2010-06-02 02:55:55 +00:00
|
|
|
|
|
|
|
if (!script)
|
|
|
|
return NULL_REG;
|
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
byte *offsetPtr = 0;
|
|
|
|
|
|
|
|
if (getSciVersion() < SCI_VERSION_1_1) {
|
|
|
|
byte *buf = (getSciVersion() == SCI_VERSION_0_EARLY) ? script->data + 2 : script->data;
|
|
|
|
|
|
|
|
// Check if the first block is the exports block (in most cases, it is)
|
|
|
|
bool exportsIsFirst = (READ_LE_UINT16(buf + 4) == 7);
|
|
|
|
if (exportsIsFirst) {
|
|
|
|
offsetPtr = buf + 4 + 2;
|
|
|
|
} else {
|
|
|
|
offsetPtr = findSci0ExportsBlock(script->data);
|
|
|
|
if (!offsetPtr)
|
|
|
|
error("Unable to find exports block from script 0");
|
|
|
|
offsetPtr += 4 + 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offsetPtr = script->data + 4 + 2 + 2;
|
|
|
|
}
|
2010-05-29 14:03:08 +00:00
|
|
|
|
2010-06-02 13:17:36 +00:00
|
|
|
int16 offset = !isSci11Mac() ? READ_LE_UINT16(offsetPtr) : READ_BE_UINT16(offsetPtr);
|
2010-05-29 14:03:08 +00:00
|
|
|
|
|
|
|
// In SCI1.1 and newer, the heap is appended at the end of the script,
|
|
|
|
// so adjust the offset accordingly
|
2010-05-30 15:17:29 +00:00
|
|
|
if (getSciVersion() >= SCI_VERSION_1_1 && addSci11ScriptOffset) {
|
2010-05-29 14:03:08 +00:00
|
|
|
offset += script->size;
|
|
|
|
|
2010-05-30 18:45:07 +00:00
|
|
|
// Ensure that the start of the heap is word-aligned - same as in Script::init()
|
2010-05-30 15:17:29 +00:00
|
|
|
if (script->size & 2)
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
2010-05-29 14:03:08 +00:00
|
|
|
return make_reg(1, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String ResourceManager::findSierraGameId() {
|
2010-05-23 10:28:03 +00:00
|
|
|
// In SCI0-SCI1, the heap is embedded in the script. In SCI1.1+, it's separated
|
|
|
|
Resource *heap = 0;
|
2010-05-29 14:03:08 +00:00
|
|
|
int nameSelector = 3;
|
2010-05-23 10:28:03 +00:00
|
|
|
|
|
|
|
if (getSciVersion() < SCI_VERSION_1_1) {
|
2010-05-29 14:03:08 +00:00
|
|
|
heap = findResource(ResourceId(kResourceTypeScript, 0), false);
|
2010-05-23 10:28:03 +00:00
|
|
|
} else {
|
|
|
|
heap = findResource(ResourceId(kResourceTypeHeap, 0), false);
|
2010-05-29 14:03:08 +00:00
|
|
|
nameSelector += 5;
|
2010-05-23 10:28:03 +00:00
|
|
|
}
|
|
|
|
|
2010-06-02 02:55:55 +00:00
|
|
|
if (!heap)
|
|
|
|
return "";
|
|
|
|
|
2010-05-29 15:01:33 +00:00
|
|
|
int16 gameObjectOffset = findGameObject(false).offset;
|
|
|
|
|
2010-06-02 02:55:55 +00:00
|
|
|
if (!gameObjectOffset)
|
|
|
|
return "";
|
|
|
|
|
2010-05-29 14:03:08 +00:00
|
|
|
// Seek to the name selector of the first export
|
2010-05-29 15:01:33 +00:00
|
|
|
byte *seeker = heap->data + READ_UINT16(heap->data + gameObjectOffset + nameSelector * 2);
|
2010-05-29 14:03:08 +00:00
|
|
|
Common::String sierraId;
|
2010-05-26 15:41:50 +00:00
|
|
|
sierraId += (const char *)seeker;
|
2010-05-23 10:28:03 +00:00
|
|
|
|
|
|
|
return sierraId;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|