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-02-21 19:54:15 +00:00
|
|
|
#include "sci/tools.h"
|
2009-02-24 05:39:10 +00:00
|
|
|
#include "sci/sci_memory.h"
|
2009-02-27 02:23:00 +00:00
|
|
|
#include "sci/scicore/resource.h"
|
|
|
|
#include "sci/scicore/vocabulary.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-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-02-21 12:33:24 +00:00
|
|
|
// These are the 18 resource types supported by SCI1
|
2009-02-28 23:46:50 +00:00
|
|
|
const char *resourceTypeNames[] = {
|
|
|
|
"view", "pic", "script", "text", "sound",
|
|
|
|
"memory", "vocab", "font", "cursor",
|
|
|
|
"patch", "bitmap", "palette", "cdaudio",
|
|
|
|
"audio", "sync", "message", "map", "heap"
|
|
|
|
};
|
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",
|
|
|
|
"msg", "map", "hep"
|
|
|
|
};
|
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
|
|
|
|
|
|
|
static decomp_funct *decompressors[] = {
|
|
|
|
NULL,
|
|
|
|
&decompress0,
|
|
|
|
&decompress01,
|
|
|
|
&decompress01,
|
|
|
|
&decompress01,
|
|
|
|
&decompress1,
|
|
|
|
&decompress1,
|
|
|
|
&decompress11,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static patch_sprintf_funct *patch_sprintfers[] = {
|
|
|
|
NULL,
|
|
|
|
&sci0_sprintf_patch_file_name,
|
|
|
|
&sci0_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name,
|
|
|
|
&sci1_sprintf_patch_file_name
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
status = SCI_STATUS_NOMALLOC;
|
|
|
|
lockers = 0;
|
|
|
|
next = prev = NULL;
|
|
|
|
source = NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
Resource::~Resource() {
|
|
|
|
delete[] data;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
void Resource::unalloc() {
|
|
|
|
delete[] data;
|
|
|
|
data = NULL;
|
|
|
|
status = SCI_STATUS_NOMALLOC;
|
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;
|
|
|
|
char filename[MAXPATHLEN];
|
|
|
|
if (!patch_sprintfers[_sciVersion]) {
|
|
|
|
error("Resource manager's SCI version (%d) has no patch file name printers", _sciVersion);
|
|
|
|
}
|
|
|
|
// TODO: use only dir specified by res->source->location_dir_name
|
|
|
|
patch_sprintfers[_sciVersion](filename, res);
|
|
|
|
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
|
|
|
}
|
|
|
|
res->status = SCI_STATUS_ALLOCATED;
|
2009-03-03 23:07:06 +00:00
|
|
|
return true;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
void ResourceManager::loadResource(Resource *res, bool protect) {
|
2009-03-03 23:07:06 +00:00
|
|
|
// TODO: check if protect is needed at all
|
2009-02-15 09:35:14 +00:00
|
|
|
char filename[MAXPATHLEN];
|
2009-02-20 16:03:50 +00:00
|
|
|
Common::File file;
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource backup;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
memcpy(&backup, res, sizeof(Resource));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (res->source->source_type == kSourceDirectory && loadFromPatchFile(res))
|
|
|
|
return;
|
|
|
|
// Either loading from volume or patch loading failed
|
|
|
|
strcpy(filename, res->source->location_name.c_str());
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
if (!file.open(filename)) {
|
|
|
|
warning("Failed to open %s", filename);
|
2009-03-03 23:07:06 +00:00
|
|
|
res->unalloc();
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-02-20 16:03:50 +00:00
|
|
|
file.seek(res->file_offset, SEEK_SET);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
// Check whether we support this at all
|
|
|
|
if (decompressors[_sciVersion] == NULL)
|
2009-02-28 22:19:22 +00:00
|
|
|
error("Resource manager's SCI version (%d) is invalid", _sciVersion);
|
2009-03-03 23:07:06 +00:00
|
|
|
// Decompress from regular resource file
|
|
|
|
int error = decompressors[_sciVersion](res, file, _sciVersion);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (error) {
|
|
|
|
warning("Error %d occured while reading %s.%03d from resource file: %s\n",
|
|
|
|
error, getResourceTypeName(res->type), res->number, sci_error_types[error]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (protect)
|
|
|
|
memcpy(res, &backup, sizeof(Resource));
|
|
|
|
res->unalloc();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 23:46:50 +00:00
|
|
|
Resource *ResourceManager::testResource(ResourceType type, int number) {
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource binseeker;
|
2009-02-15 06:10:59 +00:00
|
|
|
binseeker.type = type;
|
|
|
|
binseeker.number = 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-02-20 16:03:50 +00:00
|
|
|
int sci0_get_compression_method(Common::ReadStream &stream);
|
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-28 22:19:22 +00:00
|
|
|
mgr->_sciVersion = SCI_VERSION_AUTODETECT;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
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-02-28 22:19:22 +00:00
|
|
|
mgr->_sciVersion = SCI_VERSION_01_VGA;
|
|
|
|
return mgr->_sciVersion;
|
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-02-28 22:19:22 +00:00
|
|
|
mgr->_sciVersion = SCI_VERSION_01_VGA;
|
|
|
|
return mgr->_sciVersion;
|
2009-02-22 04:22:53 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
return mgr->_sciVersion;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
int ResourceManager::addAppropriateSources() {
|
2009-02-15 14:26:33 +00:00
|
|
|
//char path_separator;
|
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-02-28 20:45:36 +00:00
|
|
|
addPatchDir(""); // FIXME: used to pass the 'current' instead of ""
|
2009-02-15 06:10:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
int ResourceManager::scanNewSources(int *detected_version, ResourceSource *source) {
|
2009-03-03 23:07:06 +00:00
|
|
|
if (!source)
|
|
|
|
return SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
int preset_version = _sciVersion;
|
2009-02-15 06:10:59 +00:00
|
|
|
int resource_error = 0;
|
2009-02-28 22:19:22 +00:00
|
|
|
int dummy = _sciVersion;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (detected_version == NULL)
|
|
|
|
detected_version = &dummy;
|
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
*detected_version = _sciVersion;
|
2009-02-15 06:10:59 +00:00
|
|
|
if (source->next)
|
2009-02-28 20:45:36 +00:00
|
|
|
scanNewSources(detected_version, 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-02-21 12:33:24 +00:00
|
|
|
if (preset_version <= SCI_VERSION_01_VGA_ODD /* || preset_version == SCI_VERSION_AUTODETECT -- subsumed by the above line */) {
|
2009-02-28 20:45:36 +00:00
|
|
|
resource_error = readResourceMapSCI0(source, detected_version);
|
2009-02-15 06:10:59 +00:00
|
|
|
#if 0
|
|
|
|
if (resource_error >= SCI_ERROR_CRITICAL) {
|
2009-02-21 12:33:24 +00:00
|
|
|
sciprintf("Resmgr: Error while loading resource map: %s\n", sci_error_types[resource_error]);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (resource_error == SCI_ERROR_RESMAP_NOT_FOUND)
|
|
|
|
sciprintf("Running SCI games without a resource map is not supported ATM\n");
|
|
|
|
sci_free(mgr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (resource_error == SCI_ERROR_RESMAP_NOT_FOUND) {
|
2009-02-21 22:40:58 +00:00
|
|
|
// FIXME: Try reading w/o resource.map
|
2009-02-15 06:10:59 +00:00
|
|
|
resource_error = SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) {
|
2009-02-21 12:33:24 +00:00
|
|
|
// Initialize empty resource manager
|
2009-02-20 16:03:50 +00:00
|
|
|
mgr->_resourcesNr = 0;
|
2009-02-28 21:59:49 +00:00
|
|
|
mgr->_resources = 0; // FIXME: Was = (Resource*)sci_malloc(1);
|
2009-02-15 06:10:59 +00:00
|
|
|
resource_error = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
if ((preset_version == SCI_VERSION_1_EARLY) || (preset_version == SCI_VERSION_1_LATE) || (preset_version == SCI_VERSION_1_1) ||
|
2009-02-15 22:28:50 +00:00
|
|
|
((*detected_version == SCI_VERSION_AUTODETECT) && (preset_version == SCI_VERSION_AUTODETECT))) {
|
2009-02-28 20:45:36 +00:00
|
|
|
resource_error = readResourceMapSCI1(source, getVolume(source, 0), detected_version);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (resource_error == SCI_ERROR_RESMAP_NOT_FOUND) {
|
2009-02-21 22:40:58 +00:00
|
|
|
// FIXME: Try reading w/o resource.map
|
2009-02-15 06:10:59 +00:00
|
|
|
resource_error = SCI_ERROR_NO_RESOURCE_FILES_FOUND;
|
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) {
|
2009-02-21 12:33:24 +00:00
|
|
|
// Initialize empty resource manager
|
2009-03-03 23:07:06 +00:00
|
|
|
_resMap.clear();
|
2009-02-15 06:10:59 +00:00
|
|
|
resource_error = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = *detected_version;
|
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) {
|
2009-02-15 06:10:59 +00:00
|
|
|
int resmap_version = version;
|
|
|
|
|
2009-02-20 16:03:50 +00:00
|
|
|
_maxMemory = maxMemory;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked = 0;
|
|
|
|
_memoryLRU = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
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
|
|
|
lru_first = NULL;
|
|
|
|
lru_last = NULL;
|
2009-02-20 16:03:50 +00:00
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
addAppropriateSources();
|
|
|
|
scanNewSources(&resmap_version, _sources);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (version == SCI_VERSION_AUTODETECT)
|
|
|
|
switch (resmap_version) {
|
|
|
|
case SCI_VERSION_0:
|
2009-02-28 23:46:50 +00:00
|
|
|
if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) {
|
2009-02-28 20:45:36 +00:00
|
|
|
version = sci_test_view_type(this);
|
2009-02-15 22:28:50 +00:00
|
|
|
if (version == SCI_VERSION_01_VGA) {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected KQ5 or similar\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
} else {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected SCI0\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
version = 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-02-15 22:28:50 +00:00
|
|
|
if (version == SCI_VERSION_01_VGA) {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected KQ5 or similar\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
} else {
|
2009-02-28 23:46:50 +00:00
|
|
|
if (testResource(kResourceTypeVocab, 912)) {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Running KQ1 or similar, using SCI0 resource encoding\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
version = SCI_VERSION_0;
|
|
|
|
} else {
|
|
|
|
version = SCI_VERSION_01;
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected SCI01\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-02-28 20:45:36 +00:00
|
|
|
version = sci_test_view_type(this);
|
2009-02-15 22:28:50 +00:00
|
|
|
if (version == SCI_VERSION_01_VGA) {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected KQ5 or similar\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
} else {
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Warning: Could not find vocabulary; assuming SCI0 w/o parser\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
version = 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:
|
|
|
|
version = resmap_version;
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected Jones/CD or similar\n");
|
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-02-28 20:45:36 +00:00
|
|
|
loadResource(res, true);
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2009-03-03 23:07:06 +00:00
|
|
|
if (res->status == SCI_STATUS_NOMALLOC) {
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = version = SCI_VERSION_1_LATE;
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected SCI1 Late");
|
|
|
|
} else
|
|
|
|
debug("Resmgr: Detected SCI1 Early");
|
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;
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Detected SCI1.1");
|
2009-02-19 04:16:20 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
default:
|
2009-03-03 23:07:06 +00:00
|
|
|
debug("Resmgr: Warning: While autodetecting: Couldn't determine SCI version");
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 22:19:22 +00:00
|
|
|
_sciVersion = version;
|
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-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
void ResourceManager::removeFromLRU(Resource *res) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (res->status != SCI_STATUS_ENQUEUED) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res->next)
|
|
|
|
res->next->prev = res->prev;
|
|
|
|
if (res->prev)
|
|
|
|
res->prev->next = res->next;
|
2009-02-28 20:45:36 +00:00
|
|
|
if (lru_first == res)
|
|
|
|
lru_first = res->next;
|
|
|
|
if (lru_last == res)
|
|
|
|
lru_last = res->prev;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLRU -= res->size;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
res->status = SCI_STATUS_ALLOCATED;
|
|
|
|
}
|
|
|
|
|
2009-02-28 21:59:49 +00:00
|
|
|
void ResourceManager::addToLRU(Resource *res) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (res->status != SCI_STATUS_ALLOCATED) {
|
2009-02-21 12:33:24 +00:00
|
|
|
sciprintf("Resmgr: Oops: trying to enqueue resource with state %d\n", res->status);
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->prev = NULL;
|
2009-02-28 20:45:36 +00:00
|
|
|
res->next = lru_first;
|
|
|
|
lru_first = res;
|
|
|
|
if (!lru_last)
|
|
|
|
lru_last = res;
|
2009-02-15 06:10:59 +00:00
|
|
|
if (res->next)
|
|
|
|
res->next->prev = 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-02-21 14:11:41 +00:00
|
|
|
fprintf(stderr, "Adding %s.%03d (%d bytes) to lru control: %d bytes total\n",
|
2009-02-28 23:46:50 +00:00
|
|
|
getResourceTypeName(res->type), res->number, res->size,
|
2009-02-28 20:45:36 +00:00
|
|
|
mgr->_memoryLRU);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
res->status = SCI_STATUS_ENQUEUED;
|
|
|
|
}
|
|
|
|
|
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-02-28 21:59:49 +00:00
|
|
|
Resource *res = lru_first;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
while (res) {
|
2009-02-21 14:11:41 +00:00
|
|
|
fprintf(stderr, "\t%s.%03d: %d bytes\n",
|
2009-02-28 23:46:50 +00:00
|
|
|
getResourceTypeName(res->type), res->number,
|
2009-02-21 14:11:41 +00:00
|
|
|
res->size);
|
2009-02-15 06:10:59 +00:00
|
|
|
mem += res->size;
|
|
|
|
++entries;
|
|
|
|
res = res->next;
|
|
|
|
}
|
|
|
|
|
2009-02-21 14:11:41 +00:00
|
|
|
fprintf(stderr, "Total: %d entries, %d bytes (mgr says %d)\n",
|
2009-02-28 20:45:36 +00:00
|
|
|
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) {
|
|
|
|
while (_maxMemory < _memoryLRU && (!last_invulnerable || lru_first != lru_last)) {
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource *goner = lru_last;
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!goner) {
|
2009-02-21 14:11:41 +00:00
|
|
|
fprintf(stderr, "Internal error: mgr->lru_last is NULL!\n");
|
2009-02-28 20:45:36 +00:00
|
|
|
fprintf(stderr, "LRU-mem= %d\n", _memoryLRU);
|
|
|
|
fprintf(stderr, "lru_first = %p\n", (void *)lru_first);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!retval->status)
|
2009-02-28 20:45:36 +00:00
|
|
|
loadResource(retval, false);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
else if (retval->status == SCI_STATUS_ENQUEUED)
|
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) {
|
|
|
|
if (retval->status == SCI_STATUS_ALLOCATED) {
|
|
|
|
retval->status = SCI_STATUS_LOCKED;
|
|
|
|
retval->lockers = 0;
|
2009-02-28 20:45:36 +00:00
|
|
|
_memoryLocked += retval->size;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++retval->lockers;
|
|
|
|
|
2009-02-21 12:33:24 +00:00
|
|
|
} else if (retval->status != SCI_STATUS_LOCKED) { // Don't lock it
|
2009-02-15 06:10:59 +00:00
|
|
|
if (retval->status == SCI_STATUS_ALLOCATED)
|
2009-02-28 20:45:36 +00:00
|
|
|
addToLRU(retval);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:45:36 +00:00
|
|
|
freeOldResources(retval->status == SCI_STATUS_ALLOCATED);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res->status != SCI_STATUS_LOCKED) {
|
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-02-15 06:10:59 +00:00
|
|
|
res->status = SCI_STATUS_ALLOCATED;
|
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-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|