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-02-21 19:54:15 +00:00
|
|
|
#include "sci/tools.h"
|
2009-05-15 14:07:45 +00:00
|
|
|
#include "sci/resource.h"
|
|
|
|
#include "sci/vocabulary.h"
|
|
|
|
#include "sci/decompressor.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
#undef SCI_REQUIRE_RESOURCE_FILES
|
2009-03-07 04:49:34 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
//#define SCI_VERBOSE_RESMGR 1
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-27 12:59:02 +00:00
|
|
|
const char *sci_version_types[] = {
|
2009-02-15 06:10:59 +00:00
|
|
|
"SCI version undetermined (Autodetect failed / not run)",
|
|
|
|
"SCI version 0.xxx",
|
|
|
|
"SCI version 0.xxx w/ 1.000 compression",
|
|
|
|
"SCI version 1.000 w/ 0.xxx resource.map",
|
|
|
|
"SCI version 1.000 w/ special resource.map",
|
|
|
|
"SCI version 1.000 (early)",
|
|
|
|
"SCI version 1.000 (late)",
|
|
|
|
"SCI version 1.001",
|
|
|
|
"SCI WIN/32"
|
|
|
|
};
|
|
|
|
|
|
|
|
const int sci_max_resource_nr[] = {65536, 1000, 2048, 2048, 2048, 8192, 8192, 65536};
|
|
|
|
|
2009-02-27 12:59:02 +00:00
|
|
|
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-02-28 23:46:50 +00:00
|
|
|
const char *resourceTypeNames[] = {
|
|
|
|
"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",
|
|
|
|
"audio36", "sync36"
|
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 *resourceTypeSuffixes[] = {
|
|
|
|
"v56", "p56", "scr", "tex", "snd",
|
|
|
|
" ", "voc", "fon", "cur", "pat",
|
|
|
|
"bit", "pal", "cda", "aud", "syn",
|
2009-05-19 02:10:58 +00:00
|
|
|
"msg", "map", "hep", "aud", "syn"
|
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) {
|
|
|
|
return resourceTypeNames[restype];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getResourceTypeSuffix(ResourceType restype) {
|
|
|
|
return resourceTypeSuffixes[restype];
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
typedef int decomp_funct(Resource *result, Common::ReadStream &stream, int sci_version);
|
|
|
|
typedef void patch_sprintf_funct(char *string, Resource *res);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
//-- Resource main functions --
|
|
|
|
Resource::Resource() {
|
|
|
|
data = NULL;
|
|
|
|
number = 0;
|
|
|
|
type = kResourceTypeInvalid;
|
|
|
|
id = 0;
|
|
|
|
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-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-03-03 23:07:06 +00:00
|
|
|
//-- Resmgr helper functions --
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Resource source list management
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
ResourceSource *ResourceManager::addExternalMap(const char *file_name) {
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Add the new source to the SLL of sources
|
2009-02-28 20:45:36 +00:00
|
|
|
newsrc->next = _sources;
|
|
|
|
_sources = newsrc;
|
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;
|
|
|
|
newsrc->scanned = false;
|
2009-02-15 06:10:59 +00:00
|
|
|
newsrc->associated_map = NULL;
|
|
|
|
|
|
|
|
return newsrc;
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
ResourceSource *ResourceManager::addVolume(ResourceSource *map, const char *filename, int number, int extended_addressing) {
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceSource *newsrc = new ResourceSource();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Add the new source to the SLL of sources
|
2009-02-28 20:45:36 +00:00
|
|
|
newsrc->next = _sources;
|
|
|
|
_sources = newsrc;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
newsrc->source_type = kSourceVolume;
|
2009-02-20 16:03:50 +00:00
|
|
|
newsrc->scanned = false;
|
|
|
|
newsrc->location_name = filename;
|
|
|
|
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-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::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-02-21 12:33:24 +00:00
|
|
|
// Add the new source to the SLL of sources
|
2009-02-28 20:45:36 +00:00
|
|
|
newsrc->next = _sources;
|
|
|
|
_sources = newsrc;
|
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-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) {
|
|
|
|
ResourceSource *seeker = _sources;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 22:28:50 +00:00
|
|
|
while (seeker) {
|
2009-03-03 23:07:06 +00:00
|
|
|
if (seeker->source_type == kSourceVolume && seeker->associated_map == map &&
|
2009-02-20 16:03:50 +00:00
|
|
|
seeker->volume_number == volume_nr)
|
2009-02-15 06:10:59 +00:00
|
|
|
return seeker;
|
|
|
|
seeker = seeker->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Resource manager constructors and operations
|
2009-02-15 06:10:59 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
res->data = new byte[res->size];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (res->data == NULL) {
|
|
|
|
error("Can't allocate %d bytes needed for loading %s!", res->size, filename);
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
file.seek(res->file_offset, SEEK_SET);
|
|
|
|
unsigned int really_read = file.read(res->data, res->size);
|
|
|
|
if (really_read != res->size) {
|
2009-02-21 12:33:24 +00:00
|
|
|
error("Read %d bytes from %s but expected %d!", really_read, filename, res->size);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-03-10 21:44:03 +00:00
|
|
|
res->status = kResStatusAllocated;
|
2009-03-03 23:07:06 +00:00
|
|
|
return true;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
it ++;
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
// 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);
|
|
|
|
int error = decompress(res, file);
|
2009-03-03 23:07:06 +00:00
|
|
|
if (error) {
|
|
|
|
warning("Error %d occured while reading %s.%03d from resource file: %s\n",
|
2009-03-11 01:40:08 +00:00
|
|
|
error, getResourceTypeName(res->type), res->number, sci_error_types[error]);
|
2009-03-03 23:07:06 +00:00
|
|
|
res->unalloc();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-03-10 21:44:03 +00:00
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 23:46:50 +00:00
|
|
|
Resource *ResourceManager::testResource(ResourceType type, int number) {
|
2009-03-03 23:07:06 +00:00
|
|
|
if (_resMap.contains(RESOURCE_HASH(type, number)))
|
|
|
|
return _resMap.getVal(RESOURCE_HASH(type, number));
|
|
|
|
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-20 16:03:50 +00:00
|
|
|
int sci_test_view_type(ResourceManager *mgr) {
|
|
|
|
Common::File file;
|
2009-02-15 09:35:14 +00:00
|
|
|
char filename[MAXPATHLEN];
|
2009-02-15 06:10:59 +00:00
|
|
|
int compression;
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource *res;
|
2009-02-15 06:10:59 +00:00
|
|
|
int i;
|
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
for (i = 0; i < 1000; i++) {
|
2009-02-28 23:46:50 +00:00
|
|
|
res = mgr->testResource(kResourceTypeView, i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!res)
|
2009-02-15 06:10:59 +00:00
|
|
|
continue;
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (res->source->source_type == kSourceDirectory)
|
2009-02-20 16:03:50 +00:00
|
|
|
continue;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
strcpy(filename, res->source->location_name.c_str());
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!file.open(filename))
|
|
|
|
continue;
|
|
|
|
file.seek(res->file_offset, SEEK_SET);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
compression = sci0_get_compression_method(file);
|
|
|
|
file.close();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-22 04:22:53 +00:00
|
|
|
if (compression == 3) {
|
2009-03-07 00:59:38 +00:00
|
|
|
return SCI_VERSION_01_VGA;
|
2009-02-22 04:22:53 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
// Try the same thing with pics
|
2009-02-20 16:03:50 +00:00
|
|
|
for (i = 0; i < 1000; i++) {
|
2009-02-28 23:46:50 +00:00
|
|
|
res = mgr->testResource(kResourceTypePic, i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!res)
|
2009-02-15 06:10:59 +00:00
|
|
|
continue;
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (res->source->source_type == kSourceDirectory)
|
2009-02-20 16:03:50 +00:00
|
|
|
continue;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
strcpy(filename, res->source->location_name.c_str());
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!file.open(filename))
|
|
|
|
continue;
|
|
|
|
file.seek(res->file_offset, SEEK_SET);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
compression = sci0_get_compression_method(file);
|
|
|
|
file.close();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-22 04:22:53 +00:00
|
|
|
if (compression == 3) {
|
2009-03-07 00:59:38 +00:00
|
|
|
return SCI_VERSION_01_VGA;
|
2009-02-22 04:22:53 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
return SCI_VERSION_AUTODETECT;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
int ResourceManager::addAppropriateSources() {
|
2009-02-20 15:41:58 +00:00
|
|
|
ResourceSource *map;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!Common::File::exists("RESOURCE.MAP"))
|
|
|
|
return 0;
|
2009-02-28 20:45:36 +00:00
|
|
|
map = addExternalMap("RESOURCE.MAP");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
Common::ArchiveMemberList files;
|
|
|
|
SearchMan.listMatchingMembers(files, "RESOURCE.0??");
|
|
|
|
|
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
|
|
|
const Common::String name = (*x)->getName();
|
2009-02-20 16:19:29 +00:00
|
|
|
const char *dot = strrchr(name.c_str(), '.');
|
2009-02-15 06:10:59 +00:00
|
|
|
int number = atoi(dot + 1);
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
addVolume(map, name.c_str(), number, 0);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-03-11 20:15:42 +00:00
|
|
|
addPatchDir("");
|
2009-03-11 22:52:54 +00:00
|
|
|
// TODO: add RESOURCE.AUD and RESOURCE.SFX for SCI1.1 games
|
|
|
|
if (Common::File::exists("MESSAGE.MAP"))
|
|
|
|
addVolume(addExternalMap("MESSAGE.MAP"), "RESOURCE.MSG",0 ,0);
|
2009-02-15 06:10:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
int ResourceManager::scanNewSources(ResourceSource *source) {
|
2009-03-03 23:07:06 +00:00
|
|
|
if (!source)
|
|
|
|
return SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
int resource_error = 0;
|
|
|
|
if (source->next)
|
2009-03-07 00:59:38 +00:00
|
|
|
scanNewSources(source->next);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 22:28:50 +00:00
|
|
|
if (!source->scanned) {
|
2009-02-20 16:03:50 +00:00
|
|
|
source->scanned = true;
|
2009-02-15 22:28:50 +00:00
|
|
|
switch (source->source_type) {
|
2009-03-03 23:07:06 +00:00
|
|
|
case kSourceDirectory:
|
2009-03-05 22:19:29 +00:00
|
|
|
readResourcePatches(source);
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
2009-03-03 23:07:06 +00:00
|
|
|
case kSourceExtMap:
|
2009-03-07 00:59:38 +00:00
|
|
|
if (_mapVersion < SCI_VERSION_1)
|
|
|
|
resource_error = readResourceMapSCI0(source);
|
2009-03-11 01:40:08 +00:00
|
|
|
else
|
2009-03-26 13:11:30 +00:00
|
|
|
resource_error = readResourceMapSCI1(source);
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
if (resource_error == SCI_ERROR_RESMAP_NOT_FOUND) {
|
|
|
|
// FIXME: Try reading w/o resource.map
|
|
|
|
resource_error = SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) {
|
|
|
|
// Initialize empty resource manager
|
|
|
|
_resMap.clear();
|
|
|
|
resource_error = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-02-22 04:22:53 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return resource_error;
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
void ResourceManager::freeResourceSources(ResourceSource *rss) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (rss) {
|
2009-02-28 20:45:36 +00:00
|
|
|
freeResourceSources(rss->next);
|
2009-02-20 16:03:50 +00:00
|
|
|
delete rss;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
ResourceManager::ResourceManager(int version, int maxMemory) {
|
|
|
|
_maxMemory = maxMemory;
|
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-02-20 16:03:50 +00:00
|
|
|
_sources = NULL;
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = version;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
addAppropriateSources();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-07 01:08:34 +00:00
|
|
|
if (version != SCI_VERSION_AUTODETECT) {
|
|
|
|
_mapVersion = version;
|
|
|
|
_volVersion = version;
|
|
|
|
} else {
|
|
|
|
_mapVersion = detectMapVersion();
|
|
|
|
_volVersion = detectVolVersion();
|
|
|
|
}
|
|
|
|
debug("Using resource map version %d %s", _mapVersion, sci_version_types[_mapVersion]);
|
|
|
|
debug("Using volume version %d %s", _volVersion, sci_version_types[_volVersion]);
|
|
|
|
|
|
|
|
scanNewSources(_sources);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (version == SCI_VERSION_AUTODETECT)
|
2009-03-07 00:59:38 +00:00
|
|
|
switch (_mapVersion) {
|
2009-02-15 06:10:59 +00:00
|
|
|
case SCI_VERSION_0:
|
2009-02-28 23:46:50 +00:00
|
|
|
if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) {
|
2009-03-07 00:59:38 +00:00
|
|
|
version = sci_test_view_type(this) ? SCI_VERSION_01_VGA : SCI_VERSION_0;
|
2009-02-28 23:46:50 +00:00
|
|
|
} else if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB)) {
|
2009-02-28 20:45:36 +00:00
|
|
|
version = sci_test_view_type(this);
|
2009-03-07 00:59:38 +00:00
|
|
|
if (version != SCI_VERSION_01_VGA) {
|
|
|
|
version = testResource(kResourceTypeVocab, 912) ? SCI_VERSION_0 : SCI_VERSION_01;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-07 00:59:38 +00:00
|
|
|
version = sci_test_view_type(this) ? SCI_VERSION_01_VGA : SCI_VERSION_0;
|
2009-02-15 22:28:50 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case SCI_VERSION_01_VGA_ODD:
|
2009-03-11 01:40:08 +00:00
|
|
|
version = _mapVersion;
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
2009-02-15 22:28:50 +00:00
|
|
|
case SCI_VERSION_1: {
|
2009-02-28 23:46:50 +00:00
|
|
|
Resource *res = testResource(kResourceTypeScript, 0);
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = version = SCI_VERSION_1_EARLY;
|
2009-03-05 23:27:02 +00:00
|
|
|
loadResource(res);
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
if (res->status == kResStatusNoMalloc)
|
2009-03-07 00:59:38 +00:00
|
|
|
version = SCI_VERSION_1_LATE;
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-02-19 04:16:20 +00:00
|
|
|
case SCI_VERSION_1_1:
|
2009-02-21 12:33:24 +00:00
|
|
|
// No need to handle SCI 1.1 here - it was done in resource_map.cpp
|
2009-02-19 04:16:20 +00:00
|
|
|
version = SCI_VERSION_1_1;
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
default:
|
2009-03-07 00:59:38 +00:00
|
|
|
version = SCI_VERSION_AUTODETECT;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = version;
|
2009-03-07 00:59:38 +00:00
|
|
|
// temporary version printout - should be reworked later
|
|
|
|
switch (_sciVersion) {
|
|
|
|
case SCI_VERSION_0:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI0");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_01:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI01");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_01_VGA:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI01VGA - KQ5 or similar");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_01_VGA_ODD:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI01VGA - Jones/CD or similar");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_1_EARLY:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI1 Early");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_1_LATE:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI1 Late");
|
|
|
|
break;
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_1_1:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Detected SCI1.1");
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
2009-03-07 00:59:38 +00:00
|
|
|
case SCI_VERSION_32:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Couldn't determine SCI version");
|
|
|
|
break;
|
2009-03-30 07:53:32 +00:00
|
|
|
#endif
|
2009-03-07 00:59:38 +00:00
|
|
|
default:
|
2009-03-11 01:40:08 +00:00
|
|
|
debug("Resmgr: Couldn't determine SCI version");
|
|
|
|
break;
|
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
|
|
|
|
Common::HashMap<uint32, Resource *>::iterator itr = _resMap.begin();
|
|
|
|
while (itr != _resMap.end()) {
|
|
|
|
delete itr->_value;
|
|
|
|
itr ++;
|
|
|
|
}
|
2009-02-28 20:45:36 +00:00
|
|
|
freeResourceSources(_sources);
|
2009-03-03 23:07:06 +00:00
|
|
|
_resMap.empty();
|
2009-03-11 20:15:42 +00:00
|
|
|
|
|
|
|
Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
|
|
|
while (it != _volumeFiles.end()) {
|
|
|
|
delete *it;
|
|
|
|
it ++;
|
|
|
|
}
|
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-02-21 12:33:24 +00:00
|
|
|
sciprintf("Resmgr: Oops: trying to remove resource that isn't enqueued\n");
|
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-03-05 23:27:02 +00:00
|
|
|
warning("Resmgr: Oops: 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-02-15 06:10:59 +00:00
|
|
|
#if (SCI_VERBOSE_RESMGR > 1)
|
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;
|
|
|
|
debug("\t%s.%03d: %d bytes", getResourceTypeName(res->type),
|
2009-03-11 01:40:08 +00:00
|
|
|
res->number, res->size);
|
2009-02-15 06:10:59 +00:00
|
|
|
mem += res->size;
|
2009-03-05 23:27:02 +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-02-28 20:45:36 +00:00
|
|
|
void ResourceManager::freeOldResources(int last_invulnerable) {
|
2009-03-05 23:27:02 +00:00
|
|
|
while (_maxMemory < _memoryLRU && (!last_invulnerable || !_LRU.empty())) {
|
|
|
|
Resource *goner = *_LRU.reverse_begin();
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!goner) {
|
2009-03-05 23:27:02 +00:00
|
|
|
debug("Internal error: mgr->lru_last is NULL!");
|
|
|
|
debug("LRU-mem= %d", _memoryLRU);
|
|
|
|
debug("lru_first = %p", (void *)*_LRU.begin());
|
2009-02-28 20:45:36 +00:00
|
|
|
printLRU();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-28 20:45:36 +00:00
|
|
|
removeFromLRU(goner);
|
2009-03-03 23:07:06 +00:00
|
|
|
goner->unalloc();
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef SCI_VERBOSE_RESMGR
|
2009-02-28 23:46:50 +00:00
|
|
|
sciprintf("Resmgr-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-02-28 23:46:50 +00:00
|
|
|
Resource *ResourceManager::findResource(ResourceType type, int number, int lock) {
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource *retval;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
if (number >= sci_max_resource_nr[_sciVersion]) {
|
|
|
|
int modded_number = number % sci_max_resource_nr[_sciVersion];
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("[resmgr] Requested invalid resource %s.%d, mapped to %s.%d\n",
|
2009-02-28 23:46:50 +00:00
|
|
|
getResourceTypeName(type), number, getResourceTypeName(type), modded_number);
|
2009-02-15 06:10:59 +00:00
|
|
|
number = modded_number;
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
retval = testResource(type, number);
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
freeOldResources(retval->status == kResStatusAllocated);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (retval->data)
|
|
|
|
return retval;
|
|
|
|
else {
|
2009-02-28 23:46:50 +00:00
|
|
|
sciprintf("Resmgr: Failed to read %s.%03d\n", getResourceTypeName(retval->type), retval->number);
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 23:46:50 +00:00
|
|
|
void ResourceManager::unlockResource(Resource *res, int resnum, ResourceType restype) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!res) {
|
2009-02-28 23:46:50 +00:00
|
|
|
if (restype == kResourceTypeInvalid)
|
2009-02-21 14:11:41 +00:00
|
|
|
sciprintf("Resmgr: Warning: Attempt to unlock non-existant resource %03d.%03d!\n", restype, resnum);
|
2009-02-17 21:18:11 +00:00
|
|
|
else
|
2009-02-28 23:46:50 +00:00
|
|
|
sciprintf("Resmgr: Warning: Attempt to unlock non-existant resource %s.%03d!\n", getResourceTypeName(restype), resnum);
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-10 21:44:03 +00:00
|
|
|
if (res->status != kResStatusLocked) {
|
2009-02-21 12:33:24 +00:00
|
|
|
sciprintf("Resmgr: Warning: Attempt to unlock unlocked resource %s.%03d\n",
|
2009-02-28 23:46:50 +00:00
|
|
|
getResourceTypeName(res->type), res->number);
|
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-02-28 20:45:36 +00:00
|
|
|
freeOldResources(0);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 00:59:38 +00:00
|
|
|
int ResourceManager::detectMapVersion() {
|
|
|
|
Common::File file;
|
|
|
|
byte buff[6];
|
|
|
|
ResourceSource *rsrc = _sources;
|
|
|
|
// looking for extarnal map among sources
|
|
|
|
while (rsrc) {
|
|
|
|
if (rsrc->source_type == kSourceExtMap) {
|
|
|
|
file.open(rsrc->location_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rsrc = rsrc->next;
|
|
|
|
}
|
|
|
|
if (file.isOpen() == false) {
|
2009-03-11 01:40:08 +00:00
|
|
|
warning("Failed to open resource map file");
|
2009-03-07 00:59:38 +00:00
|
|
|
return SCI_VERSION_AUTODETECT;
|
|
|
|
}
|
|
|
|
// detection
|
|
|
|
// SCI0 and SCI01 maps have last 6 bytes set to FF
|
|
|
|
file.seek(-4, SEEK_END);
|
|
|
|
uint32 uEnd = file.readUint32LE();
|
|
|
|
if (uEnd == 0xFFFFFFFF) {
|
|
|
|
// check if 0 or 01 - try to read resources in SCI0 format and see if exists
|
|
|
|
file.seek(0, SEEK_SET);
|
|
|
|
while (file.read(buff, 6) == 6 && !(buff[0] == 0xFF && buff[1] == 0xFF && buff[2] == 0xFF)) {
|
|
|
|
if (getVolume(rsrc, (buff[5] & 0xFC) >> 2) == NULL)
|
|
|
|
return SCI_VERSION_01_VGA_ODD;
|
|
|
|
}
|
|
|
|
return SCI_VERSION_0;
|
|
|
|
}
|
2009-03-11 01:40:08 +00:00
|
|
|
// SCI1E/L and some SCI1.1 maps have last directory entry set to 0xFF
|
2009-03-07 00:59:38 +00:00
|
|
|
// and offset set to filesize
|
|
|
|
// SCI1 have 6-bytes entries, while SCI1.1 have 5-byte entries
|
|
|
|
file.seek(1, SEEK_SET);
|
|
|
|
uint16 off1, off = file.readUint16LE();
|
|
|
|
uint16 nEntries = off / 3;
|
|
|
|
file.seek(1, SEEK_CUR);
|
|
|
|
file.seek(off - 3, SEEK_SET);
|
|
|
|
if (file.readByte() == 0xFF && file.readUint16LE() == file.size()) {
|
|
|
|
file.seek(3, SEEK_SET);
|
|
|
|
for (int i = 0; i < nEntries; i++) {
|
|
|
|
file.seek(1, SEEK_CUR);
|
|
|
|
off1 = file.readUint16LE();
|
|
|
|
if ((off1 - off) % 5 && (off1 - off) % 6 == 0)
|
|
|
|
return SCI_VERSION_1;
|
|
|
|
if ((off1 - off) % 5 == 0 && (off1 - off) % 6)
|
|
|
|
return SCI_VERSION_1_1;
|
|
|
|
off = off1;
|
|
|
|
}
|
|
|
|
return SCI_VERSION_1;
|
|
|
|
}
|
2009-03-30 07:53:32 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_SCI32
|
2009-03-07 00:59:38 +00:00
|
|
|
// late SCI1.1 and SCI32 maps have last directory entry set to 0xFF
|
|
|
|
// offset set to filesize and 4 more bytes
|
|
|
|
file.seek(off - 7, SEEK_SET);
|
|
|
|
if (file.readByte() == 0xFF && file.readUint16LE() == file.size())
|
2009-03-14 08:41:11 +00:00
|
|
|
return SCI_VERSION_32; // TODO : check if there is a difference between these maps
|
2009-03-30 07:53:32 +00:00
|
|
|
#endif
|
2009-03-07 00:59:38 +00:00
|
|
|
|
|
|
|
return SCI_VERSION_AUTODETECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceManager::detectVolVersion() {
|
|
|
|
Common::File file;
|
|
|
|
ResourceSource *rsrc = _sources;
|
|
|
|
// looking for a volume among sources
|
|
|
|
while (rsrc) {
|
|
|
|
if (rsrc->source_type == kSourceVolume) {
|
|
|
|
file.open(rsrc->location_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rsrc = rsrc->next;
|
|
|
|
}
|
|
|
|
if (file.isOpen() == false) {
|
2009-03-11 01:40:08 +00:00
|
|
|
warning("Failed to open volume file");
|
2009-03-07 00:59:38 +00:00
|
|
|
return SCI_VERSION_AUTODETECT;
|
|
|
|
}
|
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
|
|
|
|
// 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-05-14 21:47:38 +00:00
|
|
|
int curVersion = SCI_VERSION_0;
|
|
|
|
bool failed = 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
|
|
|
|
while (!file.eos() && file.pos() < 0x100000) {
|
|
|
|
if (curVersion > SCI_VERSION_0)
|
|
|
|
file.readByte();
|
2009-03-07 05:44:36 +00:00
|
|
|
resId = file.readUint16LE();
|
2009-05-14 21:47:38 +00:00
|
|
|
dwPacked = (curVersion < SCI_VERSION_32) ? file.readUint16LE() : file.readUint32LE();
|
|
|
|
dwUnpacked = (curVersion < SCI_VERSION_32) ? file.readUint16LE() : file.readUint32LE();
|
|
|
|
wCompression = (curVersion < SCI_VERSION_32) ? file.readUint16LE() : file.readUint32LE();
|
2009-03-11 01:40:08 +00:00
|
|
|
if (file.eos())
|
2009-05-14 21:47:38 +00:00
|
|
|
return curVersion;
|
|
|
|
|
|
|
|
int chk = (curVersion == SCI_VERSION_0) ? 4 : 20;
|
|
|
|
int offs = curVersion < SCI_VERSION_1_1 ? 4 : 0;
|
|
|
|
if ((curVersion < SCI_VERSION_32 && wCompression > chk)
|
|
|
|
|| (curVersion == SCI_VERSION_32 && wCompression != 0 && wCompression != 32)
|
|
|
|
|| (wCompression == 0 && dwPacked != dwUnpacked + offs)
|
|
|
|
|| (dwUnpacked < dwPacked - offs)) {
|
|
|
|
|
|
|
|
// Retry with a newer SCI version
|
|
|
|
if (curVersion == SCI_VERSION_0) {
|
|
|
|
curVersion = SCI_VERSION_1;
|
|
|
|
} else if (curVersion == SCI_VERSION_1) {
|
|
|
|
curVersion = SCI_VERSION_1_1;
|
|
|
|
} else if (curVersion == SCI_VERSION_1_1) {
|
|
|
|
curVersion = SCI_VERSION_32;
|
|
|
|
} else {
|
|
|
|
// All version checks failed, exit loop
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.seek(0, SEEK_SET);
|
|
|
|
continue;
|
2009-03-07 05:44:36 +00:00
|
|
|
}
|
2009-05-14 21:47:38 +00:00
|
|
|
|
|
|
|
if (curVersion < SCI_VERSION_1_1)
|
|
|
|
file.seek(dwPacked - 4, SEEK_CUR);
|
|
|
|
else if (curVersion == SCI_VERSION_1_1)
|
|
|
|
file.seek((9 + dwPacked) % 2 ? dwPacked + 1 : dwPacked, SEEK_CUR);
|
|
|
|
else if (curVersion == SCI_VERSION_32)
|
|
|
|
file.seek(dwPacked, SEEK_CUR);//(9 + wPacked) % 2 ? wPacked + 1 : wPacked, SEEK_CUR);
|
2009-03-07 05:44:36 +00:00
|
|
|
}
|
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
|
|
|
|
return SCI_VERSION_AUTODETECT;
|
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;
|
|
|
|
uint32 resId = RESOURCE_HASH(restype, resnumber);
|
|
|
|
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-03-07 04:49:34 +00:00
|
|
|
perror("""__FILE__"": (""__LINE__""): failed to open");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
newrsc = new Resource;
|
|
|
|
_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;
|
|
|
|
newrsc->number = resnumber;
|
2009-03-10 21:44:03 +00:00
|
|
|
newrsc->status = kResStatusNoMalloc;
|
2009-03-07 04:49:34 +00:00
|
|
|
newrsc->type = restype;
|
|
|
|
newrsc->source = source;
|
|
|
|
newrsc->size = fsize - patch_data_offset - 2;
|
|
|
|
newrsc->file_offset = 2 + patch_data_offset;
|
2009-03-11 22:52:54 +00:00
|
|
|
debug("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,
|
|
|
|
// regardless of _sciVersion value
|
|
|
|
|
|
|
|
Common::String mask, name;
|
|
|
|
Common::ArchiveMemberList files;
|
|
|
|
int number;
|
|
|
|
const char *szResType;
|
|
|
|
ResourceSource *psrcPatch;
|
2009-03-11 22:52:54 +00:00
|
|
|
bool bAdd;
|
2009-03-07 04:49:34 +00:00
|
|
|
|
|
|
|
for (int i = kResourceTypeView; i < kResourceTypeInvalid; i ++) {
|
|
|
|
files.clear();
|
|
|
|
szResType = getResourceTypeName((ResourceType)i);
|
|
|
|
// SCI0 naming - type.nnn
|
|
|
|
mask = szResType;
|
|
|
|
mask += ".???";
|
|
|
|
SearchMan.listMatchingMembers(files, mask);
|
|
|
|
// SCI1 and later naming - nnn.typ
|
|
|
|
mask = "*.";
|
|
|
|
mask += getResourceTypeSuffix((ResourceType)i);
|
|
|
|
SearchMan.listMatchingMembers(files, mask);
|
|
|
|
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); x++) {
|
2009-03-11 22:52:54 +00:00
|
|
|
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-03-11 22:52:54 +00:00
|
|
|
if (scumm_strnicmp(name.c_str(), szResType, resname_len) == 0
|
|
|
|
&& !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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-14 17:50:09 +00:00
|
|
|
byte bMask = 0xFC;
|
|
|
|
// FIXME: The code above seems to give correct results for Jones
|
|
|
|
//byte bMask = _mapVersion == SCI_VERSION_01_VGA_ODD ? 0xF0 : 0xFC;
|
2009-03-07 04:49:34 +00:00
|
|
|
byte bShift = _mapVersion == SCI_VERSION_01_VGA_ODD ? 28 : 26;
|
|
|
|
|
|
|
|
do {
|
|
|
|
id = file.readUint16LE();
|
|
|
|
offset = file.readUint32LE();
|
|
|
|
|
|
|
|
if (file.ioFailed()) {
|
|
|
|
warning("Error while reading %s: ", map->location_name.c_str());
|
|
|
|
perror("");
|
|
|
|
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;
|
|
|
|
uint32 resId = RESOURCE_HASH(type, number);
|
|
|
|
// adding a new resource
|
|
|
|
if (_resMap.contains(resId) == false) {
|
|
|
|
res = new Resource;
|
2009-03-10 21:44:03 +00:00
|
|
|
res->id = resId;//id;
|
2009-03-07 04:49:34 +00:00
|
|
|
res->file_offset = offset & (((~bMask) << 24) | 0xFFFFFF);
|
|
|
|
res->number = number;
|
|
|
|
res->type = type;
|
|
|
|
res->source = getVolume(map, offset >> bShift);
|
|
|
|
_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;
|
|
|
|
byte nEntrySize = _mapVersion == SCI_VERSION_1_1 ? SCI11_RESMAP_ENTRIES_SIZE : SCI1_RESMAP_ENTRIES_SIZE;
|
|
|
|
uint32 resId;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if (_mapVersion == SCI_VERSION_1_1) {
|
|
|
|
// offset stored in 3 bytes
|
|
|
|
off = file.readUint16LE();
|
|
|
|
off |= file.readByte() << 16;
|
|
|
|
off <<= 1;
|
|
|
|
} else {
|
|
|
|
// offset/volume stored in 4 bytes
|
|
|
|
off = file.readUint32LE();
|
|
|
|
if (_mapVersion < SCI_VERSION_1_1) {
|
|
|
|
volume_nr = off >> 28; // most significant 4 bits
|
|
|
|
off &= 0x0FFFFFFF; // least significant 28 bits
|
|
|
|
} else {
|
|
|
|
// in SCI32 it's a plain offset
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 04:49:34 +00:00
|
|
|
if (file.ioFailed()) {
|
|
|
|
warning("Error while reading %s: ", map->location_name.c_str());
|
|
|
|
perror("");
|
|
|
|
return SCI_ERROR_RESMAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
resId = RESOURCE_HASH(type, number);
|
|
|
|
// adding new resource only if it does not exist
|
|
|
|
if (_resMap.contains(resId) == false) {
|
|
|
|
res = new Resource;
|
|
|
|
_resMap.setVal(resId, res);
|
|
|
|
res->type = (ResourceType)type;
|
|
|
|
res->number = number;
|
2009-03-10 21:44:03 +00:00
|
|
|
res->id = resId;//res->number | (res->type << 16);
|
2009-04-07 13:38:51 +00:00
|
|
|
res->source = getVolume(map, volume_nr);
|
|
|
|
res->file_offset = off;
|
2009-03-07 04:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
uint16 w, number, szUnpacked;
|
|
|
|
uint32 wCompression;
|
|
|
|
ResourceType type;
|
|
|
|
|
|
|
|
switch (_volVersion) {
|
|
|
|
case SCI_VERSION_0:
|
|
|
|
w = file->readUint16LE();
|
|
|
|
type = (ResourceType)(w >> 11);
|
|
|
|
number = w & 0x7FF;
|
|
|
|
szPacked = file->readUint16LE() - 4;
|
|
|
|
szUnpacked = file->readUint16LE();
|
|
|
|
wCompression = file->readUint16LE();
|
|
|
|
break;
|
|
|
|
case SCI_VERSION_1:
|
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;
|
|
|
|
case SCI_VERSION_1_1:
|
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-03-10 21:44:03 +00:00
|
|
|
case SCI_VERSION_32:
|
2009-03-14 04:17:03 +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
|
|
|
|
if (file->ioFailed())
|
|
|
|
return SCI_ERROR_IO_ERROR;
|
|
|
|
res->id = RESOURCE_HASH(type, number);
|
|
|
|
res->type = type;
|
|
|
|
res->number = number;
|
|
|
|
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:
|
|
|
|
compression = (_sciVersion == SCI_VERSION_0) ? kCompLZW : kCompHuffman;
|
2009-03-11 01:40:08 +00:00
|
|
|
break;
|
2009-03-14 04:17:03 +00:00
|
|
|
case 2:
|
|
|
|
compression = (_sciVersion == SCI_VERSION_0) ? 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:
|
|
|
|
warning("Resource %s #%d: Compression method %d not supported",
|
|
|
|
getResourceTypeName(res->type), res->number, 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-04-25 23:31:03 +00:00
|
|
|
void ResourceSync::startSync(EngineState *s, reg_t obj) {
|
|
|
|
_syncTime = _syncCue = -1;
|
|
|
|
PUT_SEL32V(obj, syncCue, 0);
|
2009-04-25 08:50:42 +00:00
|
|
|
_ptr = (uint16 *)data;
|
|
|
|
//syncStarted = true; // not used
|
|
|
|
}
|
|
|
|
|
2009-04-25 23:31:03 +00:00
|
|
|
void ResourceSync::nextSync(EngineState *s, reg_t obj) {
|
2009-04-25 08:50:42 +00:00
|
|
|
if (_ptr) {
|
2009-04-25 23:31:03 +00:00
|
|
|
_syncTime = (int16)READ_LE_UINT16(_ptr);
|
|
|
|
if (_syncTime == -1) {
|
2009-04-25 08:50:42 +00:00
|
|
|
stopSync();
|
|
|
|
} else {
|
2009-04-25 23:31:03 +00:00
|
|
|
_syncCue = (int16)READ_LE_UINT16(_ptr + 1);
|
2009-04-25 08:50:42 +00:00
|
|
|
_ptr += 2;
|
|
|
|
}
|
2009-04-25 23:31:03 +00:00
|
|
|
PUT_SEL32V(obj, syncTime, _syncTime);
|
|
|
|
PUT_SEL32V(obj, syncCue, _syncCue);
|
2009-04-25 08:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------------
|
|
|
|
void ResourceSync::stopSync() {
|
|
|
|
_ptr = 0;
|
2009-04-25 23:31:03 +00:00
|
|
|
_syncCue = -1;
|
2009-04-25 08:50:42 +00:00
|
|
|
//syncStarted = false; // not used
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|