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
|
|
|
|
2009-02-17 21:18:11 +00:00
|
|
|
#include "common/util.h"
|
2009-03-03 23:07:06 +00:00
|
|
|
#include "common/debug.h"
|
2009-02-17 21:18:11 +00:00
|
|
|
|
2009-05-13 16:52:41 +00:00
|
|
|
#include "sci/sci.h"
|
2009-04-25 23:31:03 +00:00
|
|
|
#include "sci/engine/state.h"
|
|
|
|
#include "sci/engine/kernel.h"
|
2009-05-15 14:07:45 +00:00
|
|
|
#include "sci/resource.h"
|
2010-01-23 17:55:54 +00:00
|
|
|
#include "sci/parser/vocabulary.h"
|
2009-05-15 14:07:45 +00:00
|
|
|
#include "sci/decompressor.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2010-01-03 22:50:39 +00:00
|
|
|
static SciVersion s_sciVersion = SCI_VERSION_NONE;
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
file_offset = 0;
|
2009-03-10 21:44:03 +00:00
|
|
|
status = kResStatusNoMalloc;
|
2009-03-03 23:07:06 +00:00
|
|
|
lockers = 0;
|
|
|
|
source = NULL;
|
2009-06-06 00:07:18 +00:00
|
|
|
header = NULL;
|
|
|
|
headerSize = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
Resource::~Resource() {
|
|
|
|
delete[] data;
|
2009-03-11 01:40:08 +00:00
|
|
|
if (source && source->source_type == kSourcePatch)
|
2009-03-05 23:27:02 +00:00
|
|
|
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;
|
2009-03-10 21:44:03 +00:00
|
|
|
status = kResStatusNoMalloc;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2009-08-18 14:10:31 +00:00
|
|
|
newsrc->resourceFile = 0;
|
|
|
|
newsrc->scanned = false;
|
|
|
|
newsrc->associated_map = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceSource *ResourceManager::addExternalMap(const Common::FSNode *mapFile) {
|
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
|
|
|
|
|
|
|
newsrc->source_type = kSourceExtMap;
|
|
|
|
newsrc->location_name = mapFile->getName();
|
|
|
|
newsrc->resourceFile = mapFile;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->scanned = false;
|
2009-02-15 06:10:59 +00:00
|
|
|
newsrc->associated_map = NULL;
|
2010-01-27 04:26:28 +00:00
|
|
|
newsrc->volume_number = 0;
|
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->scanned = false;
|
|
|
|
newsrc->location_name = filename;
|
2009-08-18 14:10:31 +00:00
|
|
|
newsrc->resourceFile = 0;
|
|
|
|
newsrc->volume_number = number;
|
|
|
|
newsrc->associated_map = map;
|
|
|
|
|
|
|
|
_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->scanned = false;
|
|
|
|
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;
|
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->scanned = false;
|
|
|
|
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
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
bool ResourceManager::loadPatch(Resource *res, Common::File &file) {
|
|
|
|
// We assume that the resource type matches res->type
|
2009-12-27 23:35:22 +00:00
|
|
|
file.seek(res->file_offset + 2, SEEK_SET);
|
2009-06-06 00:07:18 +00:00
|
|
|
|
|
|
|
res->data = new byte[res->size];
|
|
|
|
|
|
|
|
if (res->headerSize > 0)
|
2009-12-27 12:17:43 +00:00
|
|
|
res->header = new byte[res->headerSize];
|
2009-06-06 00:07:18 +00:00
|
|
|
|
|
|
|
if ((res->data == NULL) || ((res->headerSize > 0) && (res->header == NULL))) {
|
2009-06-07 02:18:38 +00:00
|
|
|
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;
|
|
|
|
if (res->headerSize > 0) {
|
|
|
|
really_read = file.read(res->header, res->headerSize);
|
|
|
|
if (really_read != res->headerSize)
|
2009-06-07 02:18:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
really_read = file.read(res->data, res->size);
|
|
|
|
if (really_read != res->size)
|
2009-06-07 02:18:38 +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
|
|
|
|
|
|
|
res->status = kResStatusAllocated;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
bool ResourceManager::loadFromPatchFile(Resource *res) {
|
|
|
|
Common::File file;
|
2009-03-11 01:40:08 +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;
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-06-06 00:07:18 +00:00
|
|
|
return loadPatch(res, file);
|
|
|
|
}
|
|
|
|
|
2010-01-03 03:37:50 +00:00
|
|
|
bool ResourceManager::loadFromWaveFile(Resource *res, Common::File &file) {
|
|
|
|
res->data = new byte[res->size];
|
|
|
|
|
|
|
|
uint32 really_read = file.read(res->data, res->size);
|
|
|
|
if (really_read != res->size)
|
|
|
|
error("Read %d bytes from %s but expected %d", really_read, res->id.toString().c_str(), res->size);
|
|
|
|
|
|
|
|
res->status = kResStatusAllocated;
|
2010-01-25 01:39:44 +00:00
|
|
|
return true;
|
2010-01-03 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
bool ResourceManager::loadFromAudioVolumeSCI11(Resource *res, Common::File &file) {
|
2010-01-03 03:37:50 +00:00
|
|
|
// Check for WAVE files here
|
|
|
|
uint32 riffTag = file.readUint32BE();
|
|
|
|
if (riffTag == MKID_BE('RIFF')) {
|
|
|
|
res->headerSize = 0;
|
|
|
|
res->size = file.readUint32LE();
|
|
|
|
file.seek(-8, SEEK_CUR);
|
|
|
|
return loadFromWaveFile(res, file);
|
|
|
|
}
|
|
|
|
file.seek(-4, SEEK_CUR);
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
ResourceType type = (ResourceType)(file.readByte() & 0x7f);
|
|
|
|
if (((res->id.type == kResourceTypeAudio || res->id.type == kResourceTypeAudio36) && (type != kResourceTypeAudio))
|
|
|
|
|| ((res->id.type == kResourceTypeSync || res->id.type == kResourceTypeSync36) && (type != kResourceTypeSync))) {
|
2009-06-08 12:20:36 +00:00
|
|
|
warning("Resource type mismatch loading %s from %s", res->id.toString().c_str(), file.getName());
|
2009-06-06 00:07:18 +00:00
|
|
|
res->unalloc();
|
|
|
|
return false;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-06-06 00:07:18 +00:00
|
|
|
|
|
|
|
res->headerSize = file.readByte();
|
|
|
|
|
|
|
|
if (type == kResourceTypeAudio) {
|
|
|
|
if (res->headerSize != 11 && res->headerSize != 12) {
|
|
|
|
warning("Unsupported audio header");
|
|
|
|
res->unalloc();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load sample size
|
|
|
|
file.seek(7, SEEK_CUR);
|
|
|
|
res->size = file.readUint32LE();
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadPatch(res, file);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
bool ResourceManager::loadFromAudioVolumeSCI1(Resource *res, Common::File &file) {
|
|
|
|
res->data = new byte[res->size];
|
|
|
|
|
|
|
|
if (res->data == NULL) {
|
|
|
|
error("Can't allocate %d bytes needed for loading %s", res->size, res->id.toString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int really_read = file.read(res->data, res->size);
|
|
|
|
if (really_read != res->size)
|
|
|
|
warning("Read %d bytes from %s but expected %d", really_read, res->id.toString().c_str(), res->size);
|
|
|
|
|
|
|
|
res->status = kResStatusAllocated;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-11 20:15:42 +00:00
|
|
|
Common::File *ResourceManager::getVolumeFile(const char *filename) {
|
|
|
|
Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
|
|
|
Common::File *file;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-03-05 23:27:02 +00:00
|
|
|
void ResourceManager::loadResource(Resource *res) {
|
2009-03-11 20:15:42 +00:00
|
|
|
Common::File *file;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-05 23:27:02 +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
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
// Either loading from volume or patch loading failed
|
2009-03-11 20:15:42 +00:00
|
|
|
file = getVolumeFile(res->source->location_name.c_str());
|
|
|
|
if (!file) {
|
|
|
|
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;
|
|
|
|
}
|
2009-03-11 20:15:42 +00:00
|
|
|
file->seek(res->file_offset, SEEK_SET);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-01-03 03:37:50 +00:00
|
|
|
if (res->source->source_type == kSourceWave && loadFromWaveFile(res, *file))
|
|
|
|
return;
|
2009-03-10 21:44:03 +00:00
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
if (res->source->source_type == kSourceAudioVolume) {
|
2009-09-23 10:55:35 +00:00
|
|
|
if (getSciVersion() < SCI_VERSION_1_1)
|
2009-06-09 19:18:48 +00:00
|
|
|
loadFromAudioVolumeSCI1(res, *file);
|
|
|
|
else
|
|
|
|
loadFromAudioVolumeSCI11(res, *file);
|
2009-06-08 12:20:36 +00:00
|
|
|
} else {
|
|
|
|
int error = decompress(res, file);
|
|
|
|
if (error) {
|
|
|
|
warning("Error %d occured while reading %s from resource file: %s",
|
|
|
|
error, res->id.toString().c_str(), sci_error_types[error]);
|
|
|
|
res->unalloc();
|
|
|
|
}
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
Resource *ResourceManager::testResource(ResourceId id) {
|
|
|
|
if (_resMap.contains(id))
|
|
|
|
return _resMap.getVal(id);
|
2009-03-03 23:07:06 +00:00
|
|
|
return NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
int sci0_get_compression_method(Common::ReadStream &stream) {
|
|
|
|
uint16 compressionMethod;
|
|
|
|
|
|
|
|
stream.readUint16LE();
|
|
|
|
stream.readUint16LE();
|
|
|
|
stream.readUint16LE();
|
|
|
|
compressionMethod = stream.readUint16LE();
|
|
|
|
if (stream.err())
|
|
|
|
return SCI_ERROR_IO_ERROR;
|
|
|
|
|
|
|
|
return compressionMethod;
|
|
|
|
}
|
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-01-27 04:26:28 +00:00
|
|
|
if (Common::File::exists("RESOURCE.MAP")) {
|
|
|
|
// SCI0-SCI2 file naming scheme
|
|
|
|
ResourceSource *map = addExternalMap("RESOURCE.MAP");
|
2009-12-27 12:17:43 +00:00
|
|
|
|
2010-01-27 04:26:28 +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-27 04:26:28 +00:00
|
|
|
} else {
|
|
|
|
// SCI2.1-SCI3 file naming scheme
|
|
|
|
Common::ArchiveMemberList mapFiles;
|
|
|
|
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;
|
|
|
|
|
|
|
|
Common::ArchiveMemberList::const_iterator fileIterator = files.begin();
|
|
|
|
Common::ArchiveMemberList::const_iterator mapIterator = mapFiles.begin();
|
|
|
|
|
|
|
|
while (fileIterator != files.end()) {
|
|
|
|
Common::String mapName = (*mapIterator)->getName();
|
|
|
|
Common::String resName = (*fileIterator)->getName();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-01-27 04:26:28 +00:00
|
|
|
const char *dot = strrchr(mapName.c_str(), '.');
|
|
|
|
int number = atoi(dot + 1);
|
|
|
|
|
|
|
|
addSource(addExternalMap(mapName.c_str(), number), kSourceVolume, resName.c_str(), number);
|
|
|
|
++fileIterator;
|
|
|
|
++mapIterator;
|
|
|
|
}
|
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(".");
|
2009-03-11 22:52:54 +00:00
|
|
|
if (Common::File::exists("MESSAGE.MAP"))
|
2009-06-06 00:07:18 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// 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-01-27 04:26:28 +00:00
|
|
|
// TODO: Load the SCI2.1+ maps (resmap.*) in concurrence with the volumes to
|
|
|
|
// get the proper volume numbers from the maps.
|
2009-08-18 14:10:31 +00:00
|
|
|
if (filename.contains("resource.map") || filename.contains("resmap.000")) {
|
|
|
|
map = addExternalMap(file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-01-16 00:22:22 +00:00
|
|
|
void ResourceManager::addNewGMPatch(Common::String gameId) {
|
|
|
|
Common::String gmPatchFile;
|
|
|
|
|
|
|
|
if (gameId == "ecoquest")
|
|
|
|
gmPatchFile = "ECO1GM.PAT";
|
|
|
|
else if (gameId == "hoyle3")
|
|
|
|
gmPatchFile = "HOY3GM.PAT";
|
|
|
|
else if (gameId == "hoyle3")
|
|
|
|
gmPatchFile = "HOY3GM.PAT";
|
|
|
|
else if (gameId == "lsl1sci")
|
|
|
|
gmPatchFile = "LL1_GM.PAT";
|
|
|
|
else if (gameId == "lsl5")
|
|
|
|
gmPatchFile = "LL5_GM.PAT";
|
|
|
|
else if (gameId == "longbow")
|
|
|
|
gmPatchFile = "ROBNGM.PAT";
|
|
|
|
else if (gameId == "sq1sci")
|
|
|
|
gmPatchFile = "SQ1_GM.PAT";
|
|
|
|
else if (gameId == "sq4")
|
|
|
|
gmPatchFile = "SQ4_GM.PAT";
|
|
|
|
else if (gameId == "fairytales")
|
|
|
|
gmPatchFile = "TALEGM.PAT";
|
|
|
|
|
|
|
|
if (!gmPatchFile.empty() && Common::File::exists(gmPatchFile)) {
|
|
|
|
ResourceSource *psrcPatch = new ResourceSource;
|
|
|
|
psrcPatch->source_type = kSourcePatch;
|
|
|
|
psrcPatch->location_name = gmPatchFile;
|
|
|
|
processPatch(psrcPatch, kResourceTypePatch, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-08 12:20:36 +00:00
|
|
|
void ResourceManager::freeResourceSources() {
|
|
|
|
for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it)
|
|
|
|
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
|
|
|
|
2009-10-10 00:07:19 +00:00
|
|
|
debugC(1, kDebugLevelResMan, "resMan: Detected %s", getSciVersionDesc(getSciVersion()).c_str());
|
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) {
|
2009-03-10 21:44:03 +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;
|
2009-03-10 21:44:03 +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) {
|
2009-03-10 21:44:03 +00:00
|
|
|
if (res->status != kResStatusAllocated) {
|
2009-09-02 12:02:37 +00:00
|
|
|
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
|
2009-03-10 21:44:03 +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;
|
2009-06-07 02:18:38 +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()) {
|
|
|
|
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;
|
|
|
|
|
2009-03-11 20:15:42 +00:00
|
|
|
if (retval->status == kResStatusNoMalloc)
|
2009-03-05 23:27:02 +00:00
|
|
|
loadResource(retval);
|
2009-03-10 21:44:03 +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) {
|
2009-03-10 21:44:03 +00:00
|
|
|
if (retval->status == kResStatusAllocated) {
|
|
|
|
retval->status = kResStatusLocked;
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->lockers = 0;
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked += retval->size;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-03-11 20:15:42 +00:00
|
|
|
retval->lockers++;
|
2009-03-10 21:44:03 +00:00
|
|
|
} 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 {
|
2009-09-02 12:02:37 +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
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
if (res->status != kResStatusLocked) {
|
2009-09-02 12:02:37 +00:00
|
|
|
warning("[resMan] Attempt to unlock unlocked resource %s", res->id.toString().c_str());
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
if (!--res->lockers) { // No more lockers?
|
2009-03-10 21:44:03 +00:00
|
|
|
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";
|
|
|
|
case kResVersionSci32:
|
|
|
|
return "SCI32";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Version not valid";
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceManager::ResVersion ResourceManager::detectMapVersion() {
|
2009-08-18 14:10:31 +00:00
|
|
|
Common::SeekableReadStream *fileStream = 0;
|
|
|
|
Common::File *file = 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 {
|
|
|
|
file = new Common::File();
|
|
|
|
file->open(rsrc->location_name);
|
|
|
|
if (file->isOpen())
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 00:59:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
Common::File *file = 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 {
|
|
|
|
file = new Common::File();
|
|
|
|
file->open(rsrc->location_name);
|
|
|
|
if (file->isOpen())
|
|
|
|
fileStream = file;
|
|
|
|
}
|
2009-03-07 00:59:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
2009-03-11 22:52:54 +00:00
|
|
|
void ResourceManager::processPatch(ResourceSource *source, ResourceType restype, int resnumber) {
|
2009-03-07 04:49:34 +00:00
|
|
|
Common::File file;
|
|
|
|
Resource *newrsc;
|
2009-06-07 02:18:38 +00:00
|
|
|
ResourceId resId = ResourceId(restype, resnumber);
|
2009-03-07 04:49:34 +00:00
|
|
|
byte patchtype, patch_data_offset;
|
|
|
|
int fsize;
|
|
|
|
|
|
|
|
if (resnumber == -1)
|
|
|
|
return;
|
2009-03-11 22:52:54 +00:00
|
|
|
if (!file.open(source->location_name)) {
|
2009-07-28 22:28:40 +00:00
|
|
|
warning("ResourceManager::processPatch(): failed to open %s", source->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fsize = file.size();
|
|
|
|
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
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
patchtype = file.readByte() & 0x7F;
|
|
|
|
patch_data_offset = file.readByte();
|
|
|
|
|
|
|
|
if (patchtype != restype) {
|
2009-03-11 22:52:54 +00:00
|
|
|
debug("Patching %s failed - resource type mismatch", source->location_name.c_str());
|
2009-03-07 04:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-07-11 06:43:01 +00:00
|
|
|
|
|
|
|
// Fixes SQ5/German, patch file special case logic taken from SCI View disassembly
|
|
|
|
if (patch_data_offset & 0x80) {
|
|
|
|
switch (patch_data_offset & 0x7F) {
|
|
|
|
case 0:
|
|
|
|
patch_data_offset = 24;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
patch_data_offset = 2;
|
|
|
|
break;
|
|
|
|
default:
|
2009-08-20 19:49:52 +00:00
|
|
|
warning("Resource patch unsupported special case %X", patch_data_offset);
|
2009-07-11 06:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 04:49:34 +00:00
|
|
|
if (patch_data_offset + 2 >= fsize) {
|
|
|
|
debug("Patching %s failed - patch starting at offset %d can't be in file of size %d",
|
2009-03-11 22:52:54 +00:00
|
|
|
source->location_name.c_str(), patch_data_offset + 2, fsize);
|
2009-03-07 04:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
// Overwrite everything, because we're patching
|
|
|
|
newrsc->id = resId;
|
2009-03-10 21:44:03 +00:00
|
|
|
newrsc->status = kResStatusNoMalloc;
|
2009-03-07 04:49:34 +00:00
|
|
|
newrsc->source = source;
|
|
|
|
newrsc->size = fsize - patch_data_offset - 2;
|
2009-06-06 00:07:18 +00:00
|
|
|
newrsc->headerSize = patch_data_offset;
|
2009-12-27 23:35:22 +00:00
|
|
|
newrsc->file_offset = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ResourceManager::readResourcePatches(ResourceSource *source) {
|
|
|
|
// 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,
|
2009-09-23 12:12:37 +00:00
|
|
|
// regardless of s_sciVersion value
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
Common::String mask, name;
|
|
|
|
Common::ArchiveMemberList files;
|
2009-12-08 20:44:57 +00:00
|
|
|
int number = -1;
|
2009-03-07 04:49:34 +00:00
|
|
|
const char *szResType;
|
|
|
|
ResourceSource *psrcPatch;
|
|
|
|
|
2010-01-13 23:58:25 +00:00
|
|
|
for (int i = kResourceTypeView; i < kResourceTypeInvalid; ++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-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])) {
|
|
|
|
number = 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])) {
|
2009-03-07 04:49:34 +00:00
|
|
|
number = 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;
|
|
|
|
processPatch(psrcPatch, (ResourceType)i, number);
|
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-27 22:34:12 +00:00
|
|
|
void ResourceManager::readWaveAudioPatches() {
|
|
|
|
// Here we do check for SCI1.1+ so we can patch wav files in as audio resources
|
|
|
|
Common::ArchiveMemberList files;
|
|
|
|
SearchMan.listMatchingMembers(files, "*.wav");
|
|
|
|
|
2010-01-13 23:58:25 +00:00
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
2009-12-27 22:34:12 +00:00
|
|
|
Common::String name = (*x)->getName();
|
|
|
|
|
|
|
|
if (isdigit(name[0])) {
|
|
|
|
int number = atoi(name.c_str());
|
|
|
|
ResourceSource *psrcPatch = new ResourceSource;
|
2010-01-03 03:37:50 +00:00
|
|
|
psrcPatch->source_type = kSourceWave;
|
2009-12-27 22:34:12 +00:00
|
|
|
psrcPatch->location_name = name;
|
|
|
|
|
|
|
|
ResourceId resId = ResourceId(kResourceTypeAudio, number);
|
|
|
|
|
|
|
|
Resource *newrsc = NULL;
|
|
|
|
|
|
|
|
// Prepare destination, if neccessary
|
|
|
|
if (_resMap.contains(resId) == false) {
|
|
|
|
newrsc = new Resource;
|
|
|
|
_resMap.setVal(resId, newrsc);
|
|
|
|
} else
|
|
|
|
newrsc = _resMap.getVal(resId);
|
|
|
|
|
|
|
|
// Get the size of the file
|
|
|
|
Common::SeekableReadStream *stream = (*x)->createReadStream();
|
|
|
|
uint32 fileSize = stream->size();
|
|
|
|
delete stream;
|
|
|
|
|
|
|
|
// Overwrite everything, because we're patching
|
|
|
|
newrsc->id = resId;
|
|
|
|
newrsc->status = kResStatusNoMalloc;
|
|
|
|
newrsc->source = psrcPatch;
|
|
|
|
newrsc->size = fileSize;
|
|
|
|
newrsc->headerSize = 0;
|
|
|
|
debugC(1, kDebugLevelResMan, "Patching %s - OK", psrcPatch->location_name.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
int ResourceManager::readResourceMapSCI0(ResourceSource *map) {
|
|
|
|
Common::File file;
|
|
|
|
Resource *res;
|
|
|
|
ResourceType type;
|
|
|
|
uint16 number, id;
|
|
|
|
uint32 offset;
|
|
|
|
|
|
|
|
if (!file.open(map->location_name))
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
|
|
|
|
file.seek(0, SEEK_SET);
|
|
|
|
|
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 {
|
|
|
|
id = file.readUint16LE();
|
|
|
|
offset = file.readUint32LE();
|
|
|
|
|
2009-12-15 08:18:57 +00:00
|
|
|
if (file.eos() || file.err()) {
|
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;
|
|
|
|
res->source = getVolume(map, offset >> bShift);
|
2009-07-09 15:54:18 +00:00
|
|
|
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;
|
|
|
|
res->source = getVolume(map, offset >> bShift);
|
|
|
|
}
|
2009-07-09 15:54:18 +00:00
|
|
|
}
|
2009-08-26 09:38:51 +00:00
|
|
|
res->file_offset = offset & (((~bMask) << 24) | 0xFFFFFF);
|
|
|
|
res->id = resId;
|
2009-03-07 04:49:34 +00:00
|
|
|
_resMap.setVal(resId, res);
|
|
|
|
}
|
|
|
|
} while (!file.eos());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-26 13:11:30 +00:00
|
|
|
int ResourceManager::readResourceMapSCI1(ResourceSource *map) {
|
2009-03-07 04:49:34 +00:00
|
|
|
Common::File file;
|
|
|
|
Resource *res;
|
|
|
|
if (!file.open(map->location_name))
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
|
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 {
|
|
|
|
type = file.readByte() & 0x1F;
|
|
|
|
resMap[type].wOffset = file.readUint16LE();
|
|
|
|
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;
|
|
|
|
file.seek(resMap[type].wOffset);
|
|
|
|
for (int i = 0; i < resMap[type].wSize; i++) {
|
|
|
|
uint16 number = file.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
|
|
|
|
off = file.readUint16LE();
|
|
|
|
off |= file.readByte() << 16;
|
|
|
|
off <<= 1;
|
|
|
|
} else {
|
|
|
|
// offset/volume stored in 4 bytes
|
|
|
|
off = file.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
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 08:18:57 +00:00
|
|
|
if (file.eos() || file.err()) {
|
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);
|
2009-06-07 02:18:38 +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
|
|
|
|
// for SCI2.1 and SCI3 maps that are not RESMAP.000. The RESMAP.* files' numbers
|
|
|
|
// need to be used in concurrence with the volume specified in the map to get
|
|
|
|
// the actual resource file.
|
|
|
|
res->source = getVolume(map, volume_nr + map->volume_number);
|
2009-04-07 13:38:51 +00:00
|
|
|
res->file_offset = off;
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
res->id = resId;
|
|
|
|
res->source = src;
|
|
|
|
res->file_offset = offset;
|
|
|
|
res->size = size;
|
|
|
|
}
|
|
|
|
}
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
void ResourceManager::removeAudioResource(ResourceId resId) {
|
|
|
|
// Remove resource, unless it was loaded from a patch
|
|
|
|
if (_resMap.contains(resId)) {
|
|
|
|
Resource *res = _resMap.getVal(resId);
|
|
|
|
|
|
|
|
if (res->source->source_type == kSourceAudioVolume) {
|
|
|
|
if (res->lockers == 0) {
|
|
|
|
_resMap.erase(resId);
|
|
|
|
delete res;
|
|
|
|
} else {
|
|
|
|
warning("Failed to remove resource %s (still in use)", resId.toString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
// Early SCI1.1 65535.MAP structure (uses RESOURCE.AUD):
|
|
|
|
// =========
|
|
|
|
// 6-byte entries:
|
|
|
|
// w nEntry
|
|
|
|
// dw offset
|
|
|
|
|
|
|
|
// Late SCI1.1 65535.MAP structure (uses RESOURCE.SFX):
|
|
|
|
// =========
|
|
|
|
// 5-byte entries:
|
|
|
|
// w nEntry
|
|
|
|
// tb offset (cumulative)
|
|
|
|
|
|
|
|
// Early SCI1.1 MAP structure:
|
|
|
|
// ===============
|
|
|
|
// 10-byte entries:
|
|
|
|
// b noun
|
|
|
|
// b verb
|
|
|
|
// b cond
|
|
|
|
// b seq
|
|
|
|
// dw offset
|
|
|
|
// w syncSize + syncAscSize
|
|
|
|
|
|
|
|
// Late SCI1.1 MAP structure:
|
|
|
|
// ===============
|
|
|
|
// Header:
|
|
|
|
// dw baseOffset
|
|
|
|
// Followed by 7 or 11-byte entries:
|
|
|
|
// b noun
|
|
|
|
// b verb
|
|
|
|
// b cond
|
|
|
|
// b seq
|
|
|
|
// tb cOffset (cumulative offset)
|
|
|
|
// w syncSize (iff seq has bit 7 set)
|
|
|
|
// w syncAscSize (iff seq has bit 6 set)
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
int ResourceManager::readAudioMapSCI11(ResourceSource *map) {
|
2009-06-07 19:15:55 +00:00
|
|
|
bool isEarly = true;
|
|
|
|
uint32 offset = 0;
|
|
|
|
Resource *mapRes = findResource(ResourceId(kResourceTypeMap, map->volume_number), false);
|
2009-06-06 00:07:18 +00:00
|
|
|
|
|
|
|
if (!mapRes) {
|
2009-06-07 19:15:55 +00:00
|
|
|
warning("Failed to open %i.MAP", map->volume_number);
|
2009-06-06 00:07:18 +00:00
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceSource *src = getVolume(map, 0);
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
if (!src)
|
2009-06-06 00:07:18 +00:00
|
|
|
return SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
|
|
|
|
|
|
|
byte *ptr = mapRes->data;
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
if (map->volume_number == 65535) {
|
|
|
|
// Heuristic to detect late SCI1.1 map format
|
|
|
|
if ((mapRes->size >= 6) && (ptr[mapRes->size - 6] != 0xff))
|
|
|
|
isEarly = false;
|
|
|
|
|
|
|
|
while (ptr < mapRes->data + mapRes->size) {
|
|
|
|
uint16 n = READ_LE_UINT16(ptr);
|
|
|
|
ptr += 2;
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
if (n == 0xffff)
|
|
|
|
break;
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
if (isEarly) {
|
|
|
|
offset = READ_LE_UINT32(ptr);
|
|
|
|
ptr += 4;
|
|
|
|
} else {
|
|
|
|
offset += READ_LE_UINT24(ptr);
|
|
|
|
ptr += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
addResource(ResourceId(kResourceTypeAudio, n), src, offset);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Heuristic to detect late SCI1.1 map format
|
|
|
|
if ((mapRes->size >= 11) && (ptr[mapRes->size - 11] == 0xff))
|
|
|
|
isEarly = false;
|
2009-06-06 00:07:18 +00:00
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
if (!isEarly) {
|
2009-06-06 00:07:18 +00:00
|
|
|
offset = READ_LE_UINT32(ptr);
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
|
2009-06-07 19:15:55 +00:00
|
|
|
while (ptr < mapRes->data + mapRes->size) {
|
|
|
|
uint32 n = READ_BE_UINT32(ptr);
|
|
|
|
int syncSize = 0;
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
if (n == 0xffffffff)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (isEarly) {
|
|
|
|
offset = READ_LE_UINT32(ptr);
|
|
|
|
ptr += 4;
|
|
|
|
} else {
|
|
|
|
offset += READ_LE_UINT24(ptr);
|
|
|
|
ptr += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEarly || (n & 0x80)) {
|
|
|
|
syncSize = READ_LE_UINT16(ptr);
|
|
|
|
ptr += 2;
|
|
|
|
|
|
|
|
if (syncSize > 0)
|
|
|
|
addResource(ResourceId(kResourceTypeSync36, map->volume_number, n & 0xffffff3f), src, offset, syncSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n & 0x40) {
|
|
|
|
syncSize += READ_LE_UINT16(ptr);
|
|
|
|
ptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
addResource(ResourceId(kResourceTypeAudio36, map->volume_number, n & 0xffffff3f), src, offset + syncSize);
|
2009-06-06 00:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-09 19:18:48 +00:00
|
|
|
// AUDIOnnn.MAP contains 10-byte entries:
|
|
|
|
// w nEntry
|
|
|
|
// dw offset+volume (as in resource.map)
|
|
|
|
// dw size
|
|
|
|
// ending with 10 0xFFs
|
|
|
|
|
|
|
|
int ResourceManager::readAudioMapSCI1(ResourceSource *map, bool unload) {
|
|
|
|
Common::File file;
|
|
|
|
|
|
|
|
if (!file.open(map->location_name))
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint16 n = file.readUint16LE();
|
|
|
|
uint32 offset = file.readUint32LE();
|
|
|
|
uint32 size = file.readUint32LE();
|
|
|
|
|
2009-12-15 08:18:57 +00:00
|
|
|
if (file.eos() || file.err()) {
|
2009-06-09 19:18:48 +00:00
|
|
|
warning("Error while reading %s", map->location_name.c_str());
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0xffff)
|
|
|
|
break;
|
|
|
|
|
|
|
|
byte volume_nr = offset >> 28; // most significant 4 bits
|
|
|
|
offset &= 0x0fffffff; // least significant 28 bits
|
|
|
|
|
|
|
|
ResourceSource *src = getVolume(map, volume_nr);
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
if (unload)
|
|
|
|
removeAudioResource(ResourceId(kResourceTypeAudio, n));
|
|
|
|
else
|
|
|
|
addResource(ResourceId(kResourceTypeAudio, n), src, offset, size);
|
|
|
|
} else {
|
|
|
|
warning("Failed to find audio volume %i", volume_nr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::setAudioLanguage(int language) {
|
|
|
|
if (_audioMapSCI1) {
|
|
|
|
if (_audioMapSCI1->volume_number == language) {
|
|
|
|
// This language is already loaded
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already have a map loaded, so we unload it first
|
|
|
|
readAudioMapSCI1(_audioMapSCI1, true);
|
|
|
|
|
|
|
|
// Remove all volumes that use this map from the source list
|
|
|
|
Common::List<ResourceSource *>::iterator it = _sources.begin();
|
|
|
|
while (it != _sources.end()) {
|
|
|
|
ResourceSource *src = *it;
|
|
|
|
if (src->associated_map == _audioMapSCI1) {
|
|
|
|
it = _sources.erase(it);
|
|
|
|
delete src;
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the map itself from the source list
|
|
|
|
_sources.remove(_audioMapSCI1);
|
|
|
|
delete _audioMapSCI1;
|
|
|
|
|
|
|
|
_audioMapSCI1 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char filename[9];
|
|
|
|
snprintf(filename, 9, "AUDIO%03d", language);
|
|
|
|
|
|
|
|
Common::String fullname = Common::String(filename) + ".MAP";
|
|
|
|
if (!Common::File::exists(fullname)) {
|
|
|
|
warning("No audio map found for language %i", language);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_audioMapSCI1 = addSource(NULL, kSourceExtAudioMap, fullname.c_str(), language);
|
|
|
|
|
|
|
|
// Search for audio volumes for this language and add them to the source list
|
|
|
|
Common::ArchiveMemberList files;
|
|
|
|
SearchMan.listMatchingMembers(files, Common::String(filename) + ".0??");
|
|
|
|
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);
|
|
|
|
|
|
|
|
addSource(_audioMapSCI1, kSourceAudioVolume, name.c_str(), number);
|
|
|
|
}
|
|
|
|
|
|
|
|
scanNewSources();
|
|
|
|
}
|
|
|
|
|
2009-03-11 01:40:08 +00:00
|
|
|
int ResourceManager::readResourceInfo(Resource *res, Common::File *file,
|
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
// 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;
|
2009-06-07 02:18:38 +00:00
|
|
|
res->id = ResourceId(type, number);
|
2009-03-10 21:44:03 +00:00
|
|
|
res->size = szUnpacked;
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceManager::decompress(Resource *res, Common::File *file) {
|
|
|
|
int error;
|
|
|
|
uint32 szPacked = 0;
|
|
|
|
ResourceCompression compression = kCompUnknown;
|
|
|
|
|
|
|
|
// fill resource info
|
|
|
|
error = readResourceInfo(res, file, szPacked, compression);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
// 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:
|
2009-06-07 02:18:38 +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];
|
|
|
|
res->status = kResStatusAllocated;
|
|
|
|
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++) {
|
|
|
|
Common::File *file;
|
|
|
|
Resource *res = testResource(ResourceId(kResourceTypeView, i));
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (res->source->source_type != kSourceVolume)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file = getVolumeFile(res->source->location_name.c_str());
|
|
|
|
if (!file)
|
|
|
|
continue;
|
|
|
|
file->seek(res->file_offset, SEEK_SET);
|
|
|
|
|
|
|
|
uint32 szPacked;
|
|
|
|
ResourceCompression compression;
|
|
|
|
|
|
|
|
if (readResourceInfo(res, file, szPacked, compression))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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
|
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();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2009-12-23 16:17:42 +00:00
|
|
|
SoundResource::SoundResource(uint32 resNumber, ResourceManager *resMan, SciVersion soundVersion) : _resMan(resMan), _soundVersion(soundVersion) {
|
|
|
|
Resource *resource = _resMan->findResource(ResourceId(kResourceTypeSound, resNumber), true);
|
|
|
|
int trackNr, channelNr;
|
|
|
|
if (!resource)
|
2009-12-19 16:19:53 +00:00
|
|
|
return;
|
|
|
|
|
2009-12-23 16:17:42 +00:00
|
|
|
_innerResource = resource;
|
|
|
|
|
2009-12-27 12:17:43 +00:00
|
|
|
byte *data, *data2;
|
|
|
|
byte *dataEnd;
|
|
|
|
Channel *channel, *sampleChannel;
|
2009-12-23 16:17:42 +00:00
|
|
|
|
|
|
|
switch (_soundVersion) {
|
|
|
|
case SCI_VERSION_0_EARLY:
|
2009-12-25 18:15:16 +00:00
|
|
|
case SCI_VERSION_0_LATE:
|
2009-12-27 11:56:51 +00:00
|
|
|
// SCI0 only has a header of 0x11/0x21 byte length and the actual midi track follows afterwards
|
2009-12-23 16:51:41 +00:00
|
|
|
_trackCount = 1;
|
|
|
|
_tracks = new Track[_trackCount];
|
2009-12-27 12:17:43 +00:00
|
|
|
_tracks->digitalChannelNr = -1;
|
2010-01-09 02:14:12 +00:00
|
|
|
_tracks->type = 0; // Not used for SCI0
|
2010-01-04 15:17:46 +00:00
|
|
|
_tracks->channelCount = 1;
|
2009-12-27 12:17:43 +00:00
|
|
|
// Digital sample data included? -> Add an additional channel
|
|
|
|
if (resource->data[0] == 2)
|
|
|
|
_tracks->channelCount++;
|
2009-12-23 16:51:41 +00:00
|
|
|
_tracks->channels = new Channel[_tracks->channelCount];
|
2009-12-27 12:17:43 +00:00
|
|
|
memset(_tracks->channels, 0, sizeof(Channel) * _tracks->channelCount);
|
|
|
|
channel = &_tracks->channels[0];
|
2009-12-25 21:54:31 +00:00
|
|
|
if (_soundVersion == SCI_VERSION_0_EARLY) {
|
2009-12-25 18:25:15 +00:00
|
|
|
channel->data = resource->data + 0x11;
|
|
|
|
channel->size = resource->size - 0x11;
|
2009-12-25 21:54:31 +00:00
|
|
|
} else {
|
2009-12-25 18:25:15 +00:00
|
|
|
channel->data = resource->data + 0x21;
|
|
|
|
channel->size = resource->size - 0x21;
|
|
|
|
}
|
2010-01-04 15:17:46 +00:00
|
|
|
if (_tracks->channelCount == 2) {
|
2009-12-27 12:17:43 +00:00
|
|
|
// Digital sample data included
|
2010-01-04 15:17:46 +00:00
|
|
|
_tracks->digitalChannelNr = 1;
|
|
|
|
sampleChannel = &_tracks->channels[1];
|
2009-12-27 12:17:43 +00:00
|
|
|
// we need to find 0xFC (channel terminator) within the data
|
|
|
|
data = channel->data;
|
|
|
|
dataEnd = channel->data + channel->size;
|
|
|
|
while ((data < dataEnd) && (*data != 0xfc))
|
|
|
|
data++;
|
|
|
|
// Skip any following 0xFCs as well
|
|
|
|
while ((data < dataEnd) && (*data == 0xfc))
|
|
|
|
data++;
|
|
|
|
// Now adjust channels accordingly
|
|
|
|
sampleChannel->data = data;
|
|
|
|
sampleChannel->size = channel->size - (data - channel->data);
|
|
|
|
channel->size = data - channel->data;
|
2009-12-27 12:33:47 +00:00
|
|
|
// Read sample header information
|
|
|
|
//Offset 14 in the header contains the frequency as a short integer. Offset 32 contains the sample length, also as a short integer.
|
|
|
|
_tracks->digitalSampleRate = READ_LE_UINT16(sampleChannel->data + 14);
|
|
|
|
_tracks->digitalSampleSize = READ_LE_UINT16(sampleChannel->data + 32);
|
|
|
|
sampleChannel->data += 44; // Skip over header
|
|
|
|
sampleChannel->size -= 44;
|
2009-12-27 12:17:43 +00:00
|
|
|
}
|
2009-12-23 16:17:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCI_VERSION_1_EARLY:
|
|
|
|
case SCI_VERSION_1_LATE:
|
2009-12-27 12:17:43 +00:00
|
|
|
data = resource->data;
|
2009-12-27 11:56:51 +00:00
|
|
|
// Count # of tracks
|
2009-12-23 16:17:42 +00:00
|
|
|
_trackCount = 0;
|
|
|
|
while ((*data++) != 0xFF) {
|
|
|
|
_trackCount++;
|
|
|
|
while (*data != 0xFF)
|
|
|
|
data += 6;
|
|
|
|
data++;
|
2009-12-19 16:19:53 +00:00
|
|
|
}
|
2009-12-23 16:17:42 +00:00
|
|
|
_tracks = new Track[_trackCount];
|
|
|
|
data = resource->data;
|
|
|
|
for (trackNr = 0; trackNr < _trackCount; trackNr++) {
|
2009-12-27 11:56:51 +00:00
|
|
|
// Track info starts with track type:BYTE
|
|
|
|
// Then the channel information gets appended Unknown:WORD, ChannelOffset:WORD, ChannelSize:WORD
|
|
|
|
// 0xFF:BYTE as terminator to end that track and begin with another track type
|
|
|
|
// Track type 0xFF is the marker signifying the end of the tracks
|
2009-12-23 16:17:42 +00:00
|
|
|
|
2010-01-09 02:14:12 +00:00
|
|
|
_tracks[trackNr].type = *data++;
|
2009-12-27 11:56:51 +00:00
|
|
|
// Counting # of channels used
|
2009-12-23 16:17:42 +00:00
|
|
|
data2 = data;
|
|
|
|
_tracks[trackNr].channelCount = 0;
|
|
|
|
while (*data2 != 0xFF) {
|
|
|
|
data2 += 6;
|
|
|
|
_tracks[trackNr].channelCount++;
|
2009-12-19 16:19:53 +00:00
|
|
|
}
|
2009-12-23 16:17:42 +00:00
|
|
|
_tracks[trackNr].channels = new Channel[_tracks[trackNr].channelCount];
|
2010-01-10 11:01:36 +00:00
|
|
|
_tracks[trackNr].digitalChannelNr = -1; // No digital sound associated
|
|
|
|
_tracks[trackNr].digitalSampleRate = 0;
|
|
|
|
_tracks[trackNr].digitalSampleSize = 0;
|
2009-12-27 11:56:51 +00:00
|
|
|
if (_tracks[trackNr].type != 0xF0) { // Digital track marker - not supported currently
|
2009-12-23 16:17:42 +00:00
|
|
|
for (channelNr = 0; channelNr < _tracks[trackNr].channelCount; channelNr++) {
|
|
|
|
channel = &_tracks[trackNr].channels[channelNr];
|
2010-01-02 01:40:10 +00:00
|
|
|
channel->prio = READ_LE_UINT16(data);
|
2009-12-23 16:17:42 +00:00
|
|
|
channel->data = resource->data + READ_LE_UINT16(data + 2) + 2;
|
2009-12-27 11:56:51 +00:00
|
|
|
channel->size = READ_LE_UINT16(data + 4) - 2; // Not counting channel header
|
2009-12-23 16:17:42 +00:00
|
|
|
channel->number = *(channel->data - 2);
|
|
|
|
channel->poly = *(channel->data - 1);
|
|
|
|
channel->time = channel->prev = 0;
|
2009-12-27 12:33:47 +00:00
|
|
|
if (channel->number == 0xFE) { // Digital channel
|
2009-12-27 12:17:43 +00:00
|
|
|
_tracks[trackNr].digitalChannelNr = channelNr;
|
2009-12-27 12:33:47 +00:00
|
|
|
_tracks[trackNr].digitalSampleRate = READ_LE_UINT16(channel->data);
|
|
|
|
_tracks[trackNr].digitalSampleSize = READ_LE_UINT16(channel->data + 2);
|
2010-01-27 15:06:47 +00:00
|
|
|
uint16 unk2 = READ_LE_UINT16(channel->data + 4);
|
|
|
|
uint16 unkSize = READ_LE_UINT16(channel->data + 6);
|
|
|
|
if (unk2 != 0)
|
|
|
|
warning("Unknown sound field isn't 0 (it's %d), "
|
|
|
|
"sound might be compressed. Sound size: %d, "
|
|
|
|
"unknown size: %d", unk2, _tracks[trackNr].digitalSampleSize, unkSize);
|
|
|
|
|
|
|
|
//assert(READ_LE_UINT16(channel->data + 4) == 0); // Possibly a compression flag
|
|
|
|
//assert(READ_LE_UINT16(channelData + 6) == size - 1);
|
2009-12-27 12:33:47 +00:00
|
|
|
channel->data += 8; // Skip over header
|
|
|
|
channel->size -= 8;
|
|
|
|
}
|
2009-12-23 16:17:42 +00:00
|
|
|
data += 6;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Skip over digital track
|
|
|
|
data += 6;
|
|
|
|
}
|
2009-12-27 11:56:51 +00:00
|
|
|
data++; // Skipping 0xFF that closes channels list
|
2009-12-19 16:19:53 +00:00
|
|
|
}
|
2009-12-23 16:17:42 +00:00
|
|
|
/*
|
2009-12-27 11:56:51 +00:00
|
|
|
Digital track ->ptr points to header:
|
2009-12-23 16:17:42 +00:00
|
|
|
[w] sample rate
|
|
|
|
[w] size
|
|
|
|
[w] ? 00 00 maybe compression flag
|
|
|
|
[w] ? size again - decompressed size maybe
|
|
|
|
*/
|
|
|
|
break;
|
2009-12-23 17:07:01 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
error("SoundResource: SCI version %d is unsupported", _soundVersion);
|
2009-12-23 16:17:42 +00:00
|
|
|
}
|
2009-12-19 16:19:53 +00:00
|
|
|
}
|
2010-01-02 01:40:10 +00:00
|
|
|
|
2009-12-19 16:19:53 +00:00
|
|
|
SoundResource::~SoundResource() {
|
2009-12-23 16:17:42 +00:00
|
|
|
for (int trackNr = 0; trackNr < _trackCount; trackNr++)
|
|
|
|
delete[] _tracks[trackNr].channels;
|
|
|
|
delete[] _tracks;
|
2009-12-19 16:19:53 +00:00
|
|
|
|
|
|
|
_resMan->unlockResource(_innerResource);
|
|
|
|
}
|
2010-01-02 01:40:10 +00:00
|
|
|
|
|
|
|
#if 0
|
2009-12-23 16:17:42 +00:00
|
|
|
SoundResource::Track* SoundResource::getTrackByNumber(uint16 number) {
|
2009-12-25 18:15:16 +00:00
|
|
|
if (_soundVersion <= SCI_VERSION_0_LATE)
|
2009-12-23 16:17:42 +00:00
|
|
|
return &_tracks[0];
|
|
|
|
|
|
|
|
if (/*number >= 0 &&*/number < _trackCount)
|
|
|
|
return &_tracks[number];
|
2009-12-19 16:19:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-02 01:40:10 +00:00
|
|
|
#endif
|
2009-12-19 16:19:53 +00:00
|
|
|
|
2010-01-09 02:14:12 +00:00
|
|
|
SoundResource::Track *SoundResource::getTrackByType(byte type) {
|
2009-12-25 18:15:16 +00:00
|
|
|
if (_soundVersion <= SCI_VERSION_0_LATE)
|
2009-12-23 16:17:42 +00:00
|
|
|
return &_tracks[0];
|
|
|
|
|
|
|
|
for (int trackNr = 0; trackNr < _trackCount; trackNr++) {
|
|
|
|
if (_tracks[trackNr].type == type)
|
|
|
|
return &_tracks[trackNr];
|
2010-01-09 02:14:12 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundResource::Track *SoundResource::getDigitalTrack() {
|
|
|
|
for (int trackNr = 0; trackNr < _trackCount; trackNr++) {
|
|
|
|
if (_tracks[trackNr].digitalChannelNr != -1)
|
|
|
|
return &_tracks[trackNr];
|
2009-12-23 12:39:24 +00:00
|
|
|
}
|
2009-12-19 16:19:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-27 11:56:51 +00:00
|
|
|
// Gets the filter mask for SCI0 sound resources
|
2010-01-16 19:11:37 +00:00
|
|
|
int SoundResource::getChannelFilterMask(int hardwareMask, bool wantsRhythm) {
|
2009-12-24 11:42:37 +00:00
|
|
|
byte *data = _innerResource->data;
|
|
|
|
int channelMask = 0;
|
|
|
|
|
2010-01-17 02:13:40 +00:00
|
|
|
if (_soundVersion > SCI_VERSION_0_LATE)
|
|
|
|
return 0;
|
2009-12-25 18:25:15 +00:00
|
|
|
|
2010-01-17 02:13:40 +00:00
|
|
|
data++; // Skip over digital sample flag
|
|
|
|
|
|
|
|
for (int channelNr = 0; channelNr < 16; channelNr++) {
|
|
|
|
channelMask = channelMask >> 1;
|
|
|
|
|
|
|
|
byte flags;
|
|
|
|
|
|
|
|
if (_soundVersion == SCI_VERSION_0_EARLY) {
|
|
|
|
// Each channel is specified by a single byte
|
|
|
|
// Upper 4 bits of the byte is a voices count
|
|
|
|
// Lower 4 bits -> bit 0 set: use for AdLib
|
|
|
|
// bit 1 set: use for PCjr
|
|
|
|
// bit 2 set: use for PC speaker
|
|
|
|
// bit 3 set and bit 0 clear: control channel (15)
|
|
|
|
// bit 3 set and bit 0 set: rhythm channel (9)
|
2010-01-17 04:57:21 +00:00
|
|
|
// Note: control channel is dynamically assigned inside the drivers,
|
|
|
|
// but seems to be fixed at 15 in the song data.
|
2010-01-17 02:13:40 +00:00
|
|
|
flags = *data++;
|
|
|
|
|
|
|
|
// Get device bits
|
|
|
|
flags &= 0x7;
|
|
|
|
} else {
|
|
|
|
// Each channel is specified by 2 bytes
|
|
|
|
// 1st byte is voices count
|
|
|
|
// 2nd byte is play mask, which specifies if the channel is supposed to be played
|
|
|
|
// by the corresponding hardware
|
|
|
|
|
|
|
|
// Skip voice count
|
2009-12-24 11:42:37 +00:00
|
|
|
data++;
|
2010-01-17 02:13:40 +00:00
|
|
|
|
|
|
|
flags = *data++;
|
2009-12-24 11:42:37 +00:00
|
|
|
}
|
2010-01-16 19:11:37 +00:00
|
|
|
|
2010-01-17 02:13:40 +00:00
|
|
|
bool play;
|
|
|
|
switch (channelNr) {
|
|
|
|
case 15:
|
|
|
|
// Always play control channel
|
|
|
|
play = true;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
// Play rhythm channel when requested
|
|
|
|
play = wantsRhythm;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Otherwise check for flag
|
|
|
|
play = flags & hardwareMask;
|
|
|
|
}
|
2010-01-16 19:11:37 +00:00
|
|
|
|
2010-01-17 02:13:40 +00:00
|
|
|
if (play) {
|
|
|
|
// This Channel is supposed to be played by the hardware
|
|
|
|
channelMask |= 0x8000;
|
|
|
|
}
|
2009-12-24 11:42:37 +00:00
|
|
|
}
|
2009-12-25 21:54:31 +00:00
|
|
|
|
2009-12-24 11:42:37 +00:00
|
|
|
return channelMask;
|
|
|
|
}
|
|
|
|
|
2010-01-26 19:25:33 +00:00
|
|
|
byte SoundResource::getInitialVoiceCount(byte channel) {
|
|
|
|
byte *data = _innerResource->data;
|
|
|
|
|
|
|
|
if (_soundVersion > SCI_VERSION_0_LATE)
|
|
|
|
return 0; // TODO
|
|
|
|
|
|
|
|
data++; // Skip over digital sample flag
|
|
|
|
|
|
|
|
if (_soundVersion == SCI_VERSION_0_EARLY)
|
|
|
|
return data[channel] >> 4;
|
|
|
|
else
|
|
|
|
return data[channel * 2];
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|