- ResourceManager uses HashMap to store and access resource info.

- Changes and clean-ups in resource loading code.

svn-id: r39098
This commit is contained in:
Greg Frieger 2009-03-03 23:07:06 +00:00
parent 3f2af1e8a1
commit bc3702dcdb
4 changed files with 186 additions and 386 deletions

View file

@ -26,6 +26,7 @@
// Resource library // Resource library
#include "common/util.h" #include "common/util.h"
#include "common/debug.h"
#include "sci/tools.h" #include "sci/tools.h"
#include "sci/sci_memory.h" #include "sci/sci_memory.h"
@ -88,8 +89,6 @@ const char *getResourceTypeSuffix(ResourceType restype) {
return resourceTypeSuffixes[restype]; return resourceTypeSuffixes[restype];
} }
int resourcecmp(const void *first, const void *second);
typedef int decomp_funct(Resource *result, Common::ReadStream &stream, int sci_version); typedef int decomp_funct(Resource *result, Common::ReadStream &stream, int sci_version);
typedef void patch_sprintf_funct(char *string, Resource *res); typedef void patch_sprintf_funct(char *string, Resource *res);
@ -117,39 +116,32 @@ static patch_sprintf_funct *patch_sprintfers[] = {
&sci1_sprintf_patch_file_name &sci1_sprintf_patch_file_name
}; };
//-- Resource main functions --
int resourcecmp(const void *first, const void *second) { Resource::Resource() {
if (((Resource *)first)->type == data = NULL;
((Resource *)second)->type) number = 0;
return (((Resource *)first)->number < type = kResourceTypeInvalid;
((Resource *)second)->number) ? -1 : id = 0;
!(((Resource *)first)->number == size = 0;
((Resource *)second)->number); file_offset = 0;
else status = SCI_STATUS_NOMALLOC;
return (((Resource *)first)->type < lockers = 0;
((Resource *)second)->type) ? -1 : 1; next = prev = NULL;
source = NULL;
} }
Resource::~Resource() {
delete[] data;
}
void Resource::unalloc() {
delete[] data;
data = NULL;
status = SCI_STATUS_NOMALLOC;
}
//-- Resmgr helper functions -- //-- Resmgr helper functions --
void ResourceManager::addAltSource(Resource *res, ResourceSource *source, unsigned int file_offset) {
resource_altsource_t *rsrc = (resource_altsource_t *)sci_malloc(sizeof(resource_altsource_t));
rsrc->next = res->alt_sources;
rsrc->source = source;
rsrc->file_offset = file_offset;
res->alt_sources = rsrc;
}
Resource *ResourceManager::findResourceUnsorted(Resource *res, int res_nr, ResourceType type, int number) {
int i;
for (i = 0; i < res_nr; i++)
if (res[i].number == number && res[i].type == type)
return res + i;
return NULL;
}
// Resource source list management // Resource source list management
ResourceSource *ResourceManager::addExternalMap(const char *file_name) { ResourceSource *ResourceManager::addExternalMap(const char *file_name) {
@ -159,7 +151,7 @@ ResourceSource *ResourceManager::addExternalMap(const char *file_name) {
newsrc->next = _sources; newsrc->next = _sources;
_sources = newsrc; _sources = newsrc;
newsrc->source_type = RESSOURCE_TYPE_EXTERNAL_MAP; newsrc->source_type = kSourceExtMap;
newsrc->location_name = file_name; newsrc->location_name = file_name;
newsrc->scanned = false; newsrc->scanned = false;
newsrc->associated_map = NULL; newsrc->associated_map = NULL;
@ -174,7 +166,7 @@ ResourceSource *ResourceManager::addVolume(ResourceSource *map, const char *file
newsrc->next = _sources; newsrc->next = _sources;
_sources = newsrc; _sources = newsrc;
newsrc->source_type = RESSOURCE_TYPE_VOLUME; newsrc->source_type = kSourceVolume;
newsrc->scanned = false; newsrc->scanned = false;
newsrc->location_name = filename; newsrc->location_name = filename;
newsrc->volume_number = number; newsrc->volume_number = number;
@ -190,7 +182,7 @@ ResourceSource *ResourceManager::addPatchDir(const char *dirname) {
newsrc->next = _sources; newsrc->next = _sources;
_sources = newsrc; _sources = newsrc;
newsrc->source_type = RESSOURCE_TYPE_DIRECTORY; newsrc->source_type = kSourceDirectory;
newsrc->scanned = false; newsrc->scanned = false;
newsrc->location_name = dirname; newsrc->location_name = dirname;
@ -201,7 +193,7 @@ ResourceSource *ResourceManager::getVolume(ResourceSource *map, int volume_nr) {
ResourceSource *seeker = _sources; ResourceSource *seeker = _sources;
while (seeker) { while (seeker) {
if (seeker->source_type == RESSOURCE_TYPE_VOLUME && seeker->associated_map == map && if (seeker->source_type == kSourceVolume && seeker->associated_map == map &&
seeker->volume_number == volume_nr) seeker->volume_number == volume_nr)
return seeker; return seeker;
seeker = seeker->next; seeker = seeker->next;
@ -212,79 +204,77 @@ ResourceSource *ResourceManager::getVolume(ResourceSource *map, int volume_nr) {
// Resource manager constructors and operations // Resource manager constructors and operations
void ResourceManager::loadFromPatchFile(Common::File &file, Resource *res, char *filename) { bool ResourceManager::loadFromPatchFile(Resource *res) {
unsigned int really_read; 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];
res->data = (unsigned char *)sci_malloc(res->size); if (res->data == NULL) {
really_read = file.read(res->data, res->size); error("Can't allocate %d bytes needed for loading %s!", res->size, filename);
if (really_read < res->size) {
error("Read %d bytes from %s but expected %d!", really_read, filename, res->size);
} }
file.seek(res->file_offset, SEEK_SET);
unsigned int really_read = file.read(res->data, res->size);
if (really_read != res->size) {
error("Read %d bytes from %s but expected %d!", really_read, filename, res->size);
}
res->status = SCI_STATUS_ALLOCATED; res->status = SCI_STATUS_ALLOCATED;
return true;
} }
void ResourceManager::loadResource(Resource *res, bool protect) { void ResourceManager::loadResource(Resource *res, bool protect) {
// TODO: check if protect is needed at all
char filename[MAXPATHLEN]; char filename[MAXPATHLEN];
Common::File file; Common::File file;
Resource backup; Resource backup;
memcpy(&backup, res, sizeof(Resource)); memcpy(&backup, res, sizeof(Resource));
// First try lower-case name if (res->source->source_type == kSourceDirectory && loadFromPatchFile(res))
if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) { return;
if (!patch_sprintfers[_sciVersion]) { // Either loading from volume or patch loading failed
error("Resource manager's SCI version (%d) has no patch file name printers", _sciVersion); strcpy(filename, res->source->location_name.c_str());
}
// Get patch file name
patch_sprintfers[_sciVersion](filename, res);
// FIXME: Instead of using SearchMan, maybe we should only search
// a single dir specified by this RESSOURCE_TYPE_DIRECTORY ResourceSource?
} else
strcpy(filename, res->source->location_name.c_str());
if (!file.open(filename)) { if (!file.open(filename)) {
warning("Failed to open %s", filename); warning("Failed to open %s", filename);
res->data = NULL; res->unalloc();
res->status = SCI_STATUS_NOMALLOC;
res->size = 0;
return; return;
} }
file.seek(res->file_offset, SEEK_SET); file.seek(res->file_offset, SEEK_SET);
if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) // Check whether we support this at all
loadFromPatchFile(file, res, filename); if (decompressors[_sciVersion] == NULL)
else if (!decompressors[_sciVersion]) {
// Check whether we support this at all
error("Resource manager's SCI version (%d) is invalid", _sciVersion); error("Resource manager's SCI version (%d) is invalid", _sciVersion);
} else { // Decompress from regular resource file
int error = // Decompress from regular resource file int error = decompressors[_sciVersion](res, file, _sciVersion);
decompressors[_sciVersion](res, file, _sciVersion);
if (error) { if (error) {
sciprintf("Error %d occured while reading %s.%03d from resource file: %s\n", warning("Error %d occured while reading %s.%03d from resource file: %s\n",
error, getResourceTypeName(res->type), res->number, sci_error_types[error]); error, getResourceTypeName(res->type), res->number, sci_error_types[error]);
if (protect) if (protect)
memcpy(res, &backup, sizeof(Resource)); memcpy(res, &backup, sizeof(Resource));
res->unalloc();
res->data = NULL;
res->status = SCI_STATUS_NOMALLOC;
res->size = 0;
}
} }
} }
Resource *ResourceManager::testResource(ResourceType type, int number) { Resource *ResourceManager::testResource(ResourceType type, int number) {
Resource binseeker; Resource binseeker;
binseeker.type = type; binseeker.type = type;
binseeker.number = number; binseeker.number = number;
return (Resource *)bsearch(&binseeker, _resources, _resourcesNr, sizeof(Resource), resourcecmp); if (_resMap.contains(RESOURCE_HASH(type, number)))
return _resMap.getVal(RESOURCE_HASH(type, number));
return NULL;
} }
int sci0_get_compression_method(Common::ReadStream &stream); int sci0_get_compression_method(Common::ReadStream &stream);
@ -304,7 +294,7 @@ int sci_test_view_type(ResourceManager *mgr) {
if (!res) if (!res)
continue; continue;
if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) if (res->source->source_type == kSourceDirectory)
continue; continue;
strcpy(filename, res->source->location_name.c_str()); strcpy(filename, res->source->location_name.c_str());
@ -329,7 +319,7 @@ int sci_test_view_type(ResourceManager *mgr) {
if (!res) if (!res)
continue; continue;
if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) if (res->source->source_type == kSourceDirectory)
continue; continue;
strcpy(filename, res->source->location_name.c_str()); strcpy(filename, res->source->location_name.c_str());
@ -373,10 +363,12 @@ int ResourceManager::addAppropriateSources() {
} }
int ResourceManager::scanNewSources(int *detected_version, ResourceSource *source) { int ResourceManager::scanNewSources(int *detected_version, ResourceSource *source) {
if (!source)
return SCI_ERROR_NO_RESOURCE_FILES_FOUND;
int preset_version = _sciVersion; int preset_version = _sciVersion;
int resource_error = 0; int resource_error = 0;
int dummy = _sciVersion; int dummy = _sciVersion;
//Resource **concat_ptr = &(mgr->_resources[mgr->_resourcesNr - 1].next);
if (detected_version == NULL) if (detected_version == NULL)
detected_version = &dummy; detected_version = &dummy;
@ -388,13 +380,13 @@ int ResourceManager::scanNewSources(int *detected_version, ResourceSource *sourc
if (!source->scanned) { if (!source->scanned) {
source->scanned = true; source->scanned = true;
switch (source->source_type) { switch (source->source_type) {
case RESSOURCE_TYPE_DIRECTORY: case kSourceDirectory:
if (_sciVersion <= SCI_VERSION_01) if (_sciVersion <= SCI_VERSION_01)
readResourcePatchesSCI0(source); readResourcePatchesSCI0(source);
else else
readResourcePatchesSCI1(source); readResourcePatchesSCI1(source);
break; break;
case RESSOURCE_TYPE_EXTERNAL_MAP: case kSourceExtMap:
if (preset_version <= SCI_VERSION_01_VGA_ODD /* || preset_version == SCI_VERSION_AUTODETECT -- subsumed by the above line */) { if (preset_version <= SCI_VERSION_01_VGA_ODD /* || preset_version == SCI_VERSION_AUTODETECT -- subsumed by the above line */) {
resource_error = readResourceMapSCI0(source, detected_version); resource_error = readResourceMapSCI0(source, detected_version);
#if 0 #if 0
@ -429,8 +421,7 @@ int ResourceManager::scanNewSources(int *detected_version, ResourceSource *sourc
if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) { if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) {
// Initialize empty resource manager // Initialize empty resource manager
_resourcesNr = 0; _resMap.clear();
_resources = 0; // FIXME: Was = (Resource*)sci_malloc(1);
resource_error = 0; resource_error = 0;
} }
} }
@ -440,7 +431,6 @@ int ResourceManager::scanNewSources(int *detected_version, ResourceSource *sourc
default: default:
break; break;
} }
qsort(_resources, _resourcesNr, sizeof(Resource), resourcecmp); // Sort resources
} }
return resource_error; return resource_error;
} }
@ -461,8 +451,7 @@ ResourceManager::ResourceManager(int version, int maxMemory) {
_memoryLocked = 0; _memoryLocked = 0;
_memoryLRU = 0; _memoryLRU = 0;
_resources = NULL; _resMap.clear();
_resourcesNr = 0;
_sources = NULL; _sources = NULL;
_sciVersion = version; _sciVersion = version;
@ -472,56 +461,43 @@ ResourceManager::ResourceManager(int version, int maxMemory) {
addAppropriateSources(); addAppropriateSources();
scanNewSources(&resmap_version, _sources); scanNewSources(&resmap_version, _sources);
if (!_resources || !_resourcesNr) {
if (_resources) {
free(_resources);
_resources = NULL;
}
sciprintf("Resmgr: Could not retrieve a resource list!\n");
freeResourceSources(_sources);
error("FIXME: Move this code to an init() method so that we can perform error handling");
// return NULL;
}
qsort(_resources, _resourcesNr, sizeof(Resource), resourcecmp); // Sort resources
if (version == SCI_VERSION_AUTODETECT) if (version == SCI_VERSION_AUTODETECT)
switch (resmap_version) { switch (resmap_version) {
case SCI_VERSION_0: case SCI_VERSION_0:
if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) { if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) {
version = sci_test_view_type(this); version = sci_test_view_type(this);
if (version == SCI_VERSION_01_VGA) { if (version == SCI_VERSION_01_VGA) {
sciprintf("Resmgr: Detected KQ5 or similar\n"); debug("Resmgr: Detected KQ5 or similar\n");
} else { } else {
sciprintf("Resmgr: Detected SCI0\n"); debug("Resmgr: Detected SCI0\n");
version = SCI_VERSION_0; version = SCI_VERSION_0;
} }
} else if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB)) { } else if (testResource(kResourceTypeVocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB)) {
version = sci_test_view_type(this); version = sci_test_view_type(this);
if (version == SCI_VERSION_01_VGA) { if (version == SCI_VERSION_01_VGA) {
sciprintf("Resmgr: Detected KQ5 or similar\n"); debug("Resmgr: Detected KQ5 or similar\n");
} else { } else {
if (testResource(kResourceTypeVocab, 912)) { if (testResource(kResourceTypeVocab, 912)) {
sciprintf("Resmgr: Running KQ1 or similar, using SCI0 resource encoding\n"); debug("Resmgr: Running KQ1 or similar, using SCI0 resource encoding\n");
version = SCI_VERSION_0; version = SCI_VERSION_0;
} else { } else {
version = SCI_VERSION_01; version = SCI_VERSION_01;
sciprintf("Resmgr: Detected SCI01\n"); debug("Resmgr: Detected SCI01\n");
} }
} }
} else { } else {
version = sci_test_view_type(this); version = sci_test_view_type(this);
if (version == SCI_VERSION_01_VGA) { if (version == SCI_VERSION_01_VGA) {
sciprintf("Resmgr: Detected KQ5 or similar\n"); debug("Resmgr: Detected KQ5 or similar\n");
} else { } else {
sciprintf("Resmgr: Warning: Could not find vocabulary; assuming SCI0 w/o parser\n"); debug("Resmgr: Warning: Could not find vocabulary; assuming SCI0 w/o parser\n");
version = SCI_VERSION_0; version = SCI_VERSION_0;
} }
} }
break; break;
case SCI_VERSION_01_VGA_ODD: case SCI_VERSION_01_VGA_ODD:
version = resmap_version; version = resmap_version;
sciprintf("Resmgr: Detected Jones/CD or similar\n"); debug("Resmgr: Detected Jones/CD or similar\n");
break; break;
case SCI_VERSION_1: { case SCI_VERSION_1: {
Resource *res = testResource(kResourceTypeScript, 0); Resource *res = testResource(kResourceTypeScript, 0);
@ -529,58 +505,34 @@ ResourceManager::ResourceManager(int version, int maxMemory) {
_sciVersion = version = SCI_VERSION_1_EARLY; _sciVersion = version = SCI_VERSION_1_EARLY;
loadResource(res, true); loadResource(res, true);
if (res->status == SCI_STATUS_NOMALLOC) if (res->status == SCI_STATUS_NOMALLOC) {
_sciVersion = version = SCI_VERSION_1_LATE; _sciVersion = version = SCI_VERSION_1_LATE;
debug("Resmgr: Detected SCI1 Late");
} else
debug("Resmgr: Detected SCI1 Early");
break; break;
} }
case SCI_VERSION_1_1: case SCI_VERSION_1_1:
// No need to handle SCI 1.1 here - it was done in resource_map.cpp // No need to handle SCI 1.1 here - it was done in resource_map.cpp
version = SCI_VERSION_1_1; version = SCI_VERSION_1_1;
debug("Resmgr: Detected SCI1.1");
break; break;
default: default:
sciprintf("Resmgr: Warning: While autodetecting: Couldn't determine SCI version"); debug("Resmgr: Warning: While autodetecting: Couldn't determine SCI version");
} }
if (!resource_error) {
qsort(_resources, _resourcesNr, sizeof(Resource), resourcecmp); // Sort resources
}
_sciVersion = version; _sciVersion = version;
} }
void ResourceManager::freeAltSources(resource_altsource_t *dynressrc) {
if (dynressrc) {
freeAltSources(dynressrc->next);
free(dynressrc);
}
}
void ResourceManager::freeResources(Resource *resources, int resourcesNr) {
int i;
for (i = 0; i < resourcesNr; i++) {
Resource *res = resources + i;
// FIXME: alt_sources->next may point to an invalid memory location
freeAltSources(res->alt_sources);
if (res->status != SCI_STATUS_NOMALLOC)
free(res->data);
}
free(resources);
}
ResourceManager::~ResourceManager() { ResourceManager::~ResourceManager() {
freeResources(_resources, _resourcesNr); // freeing resources
Common::HashMap<uint32, Resource *>::iterator itr = _resMap.begin();
while (itr != _resMap.end()) {
delete itr->_value;
itr ++;
}
freeResourceSources(_sources); freeResourceSources(_sources);
_resources = NULL; _resMap.empty();
}
void ResourceManager::unalloc(Resource *res) {
free(res->data);
res->data = NULL;
res->status = SCI_STATUS_NOMALLOC;
} }
void ResourceManager::removeFromLRU(Resource *res) { void ResourceManager::removeFromLRU(Resource *res) {
@ -657,7 +609,7 @@ void ResourceManager::freeOldResources(int last_invulnerable) {
} }
removeFromLRU(goner); removeFromLRU(goner);
unalloc(goner); goner->unalloc();
#ifdef SCI_VERBOSE_RESMGR #ifdef SCI_VERBOSE_RESMGR
sciprintf("Resmgr-debug: LRU: Freeing %s.%03d (%d bytes)\n", getResourceTypeName(goner->type), goner->number, goner->size); sciprintf("Resmgr-debug: LRU: Freeing %s.%03d (%d bytes)\n", getResourceTypeName(goner->type), goner->number, goner->size);
#endif #endif

View file

@ -84,17 +84,19 @@ namespace Sci {
#define SCI_VERSION_1 SCI_VERSION_1_EARLY #define SCI_VERSION_1 SCI_VERSION_1_EARLY
enum ResSourceType { enum ResSourceType {
RESSOURCE_TYPE_DIRECTORY = 0, kSourceDirectory = 0,
RESSOURCE_TYPE_VOLUME = 2, kSourceVolume = 2,
RESSOURCE_TYPE_EXTERNAL_MAP = 3, kSourceExtMap = 3,
RESSOURCE_TYPE_INTERNAL_MAP = 4, kSourceIntMap = 4,
RESSOURCE_TYPE_MASK = 127 kSourceMask = 127
}; };
#define RESSOURCE_ADDRESSING_BASIC 0 #define RESSOURCE_ADDRESSING_BASIC 0
#define RESSOURCE_ADDRESSING_EXTENDED 128 #define RESSOURCE_ADDRESSING_EXTENDED 128
#define RESSOURCE_ADDRESSING_MASK 128 #define RESSOURCE_ADDRESSING_MASK 128
#define RESOURCE_HASH(type, number) (uint32)((type<<16) | number)
extern const char *sci_error_types[]; extern const char *sci_error_types[];
extern const char *sci_version_types[]; extern const char *sci_version_types[];
extern const int sci_max_resource_nr[]; /* Highest possible resource numbers */ extern const int sci_max_resource_nr[]; /* Highest possible resource numbers */
@ -150,31 +152,28 @@ struct ResourceSource {
ResourceSource *next; ResourceSource *next;
}; };
struct resource_altsource_t { /** Class for storing resources in memory */
ResourceSource *source;
unsigned int file_offset;
resource_altsource_t *next;
};
/** Struct for storing resources in memory */
class Resource { class Resource {
public: public:
Resource::Resource();
Resource::~Resource();
void unalloc();
// NOTE : Currently all member data has the same name and public visibility // NOTE : Currently all member data has the same name and public visibility
// to let the rest of the engine compile without changes // to let the rest of the engine compile without changes
unsigned char *data; public:
unsigned short number; byte *data;
uint16 number;
ResourceType type; ResourceType type;
uint16 id; /* contains number and type */ uint16 id; /* contains number and type */
unsigned int size; unsigned int size;
unsigned int file_offset; /* Offset in file */ unsigned int file_offset; /* Offset in file */
unsigned char status; byte status;
unsigned short lockers; /* Number of places where this resource was locked */ unsigned short lockers; /* Number of places where this resource was locked */
Resource *next; /* Position marker for the LRU queue */ Resource *next; /* Position marker for the LRU queue */
Resource *prev; Resource *prev;
ResourceSource *source; ResourceSource *source;
resource_altsource_t *alt_sources; /* SLL of alternative resource data sources */
}; };
@ -257,50 +256,21 @@ public:
protected: protected:
int _maxMemory; /* Config option: Maximum total byte number allocated */ int _maxMemory; /* Config option: Maximum total byte number allocated */
int _resourcesNr; //int _resourcesNr;
ResourceSource *_sources; ResourceSource *_sources;
Resource *_resources; //Resource *_resources;
int _memoryLocked; // Amount of resource bytes in locked memory int _memoryLocked; // Amount of resource bytes in locked memory
int _memoryLRU; // Amount of resource bytes under LRU control int _memoryLRU; // Amount of resource bytes under LRU control
Resource *lru_first, *lru_last; // Pointers to the first and last LRU queue entries Resource *lru_first, *lru_last; // Pointers to the first and last LRU queue entries
// LRU queue: lru_first points to the most recent entry // LRU queue: lru_first points to the most recent entry
Common::HashMap<uint32, Resource *> _resMap;
/* Frees a block of resources and associated data
** Parameters: (Resource *) resources: The resources to free
** (int) _resourcesNr: Number of resources in the block
** Returns : (void)
*/
void freeResources(Resource *resources, int _resourcesNr);
/* Finds a resource matching type.number in an unsorted Resource block
** To be used during initial resource loading, when the resource list
** may not have been sorted yet.
** Parameters: (Resource *) res: Pointer to the block to search in
** (int) res_nr: Number of Resource structs allocated and defined
** in the block pointed to by res
** (ResourceType) type: Type of the resource to look for
** (int) number: Number of the resource to look for
** Returns : (Resource) The matching resource entry, or NULL if not found
*/
Resource *findResourceUnsorted(Resource *res, int res_nr, ResourceType type, int number);
/* Adds an alternative source to a resource
** Parameters: (Resource *) res: The resource to add to
** (ResourceSource *) source: The source of the resource
** (unsigned int) file_offset: Offset in the file the resource
** is stored at
** Returns : (void)
*/
void addAltSource(Resource *res, ResourceSource *source, unsigned int file_offset);
int addAppropriateSources(); int addAppropriateSources();
void freeResourceSources(ResourceSource *rss); void freeResourceSources(ResourceSource *rss);
void freeAltSources(resource_altsource_t *dynressrc);
void loadResource(Resource *res, bool protect); void loadResource(Resource *res, bool protect);
void loadFromPatchFile(Common::File &file, Resource *res, char *filename); bool loadFromPatchFile(Resource *res);
void freeOldResources(int last_invulnerable); void freeOldResources(int last_invulnerable);
void unalloc(Resource *res);
/**--- Resource map decoding functions ---*/ /**--- Resource map decoding functions ---*/
@ -326,28 +296,16 @@ protected:
/**--- Patch management functions ---*/ /**--- Patch management functions ---*/
/* Reads SCI0 patch files from a local directory //! Reads SCI0 patch files from a local directory
** Parameters: (char *) path: (unused) /** @paramParameters: ResourceSource *source
** (Resource **) resources: Pointer to a pointer * @return : (int) 0 on success, an SCI_ERROR_* code otherwise
** that will be set to the */
** location of the resources
** (in one large chunk)
** (int *) resource_nr_p: Pointer to an int the number of resources
** read is stored in
** Returns : (int) 0 on success, an SCI_ERROR_* code otherwise
*/
int readResourcePatchesSCI0(ResourceSource *source); int readResourcePatchesSCI0(ResourceSource *source);
/* Reads SCI1 patch files from a local directory //! Reads SCI1 patch files from a local directory
** Parameters: (char *) path: (unused) /** @paramParameters: ResourceSource *source
** (Resource **) resources: Pointer to a pointer * @return : (int) 0 on success, an SCI_ERROR_* code otherwise
** that will be set to the */
** location of the resources
** (in one large chunk)
** (int *) resource_nr_p: Pointer to an int the number of resources
** read is stored in
** Returns : (int) 0 on success, an SCI_ERROR_* code otherwise
*/
int readResourcePatchesSCI1(ResourceSource *source); int readResourcePatchesSCI1(ResourceSource *source);
void process_patch(ResourceSource *source, Common::ArchiveMember &member, void process_patch(ResourceSource *source, Common::ArchiveMember &member,

View file

@ -200,13 +200,9 @@ int ResourceManager::parseHeaderSCI1(Common::ReadStream &stream, int *types, Res
int ResourceManager::readResourceMapSCI0(ResourceSource *map, int *sci_version) { int ResourceManager::readResourceMapSCI0(ResourceSource *map, int *sci_version) {
int fsize; int fsize;
Common::File file; Common::File file;
Resource *resources; Resource *res, res1;
int resource_nr;
int resource_index = 0;
int resources_total_read = 0; int resources_total_read = 0;
int next_entry; bool bAdded = false;
int max_resfile_nr = 0;
byte buf[SCI0_RESMAP_ENTRIES_SIZE]; byte buf[SCI0_RESMAP_ENTRIES_SIZE];
if (!file.open(map->location_name)) if (!file.open(map->location_name))
@ -255,86 +251,43 @@ int ResourceManager::readResourceMapSCI0(ResourceSource *map, int *sci_version)
return SCI_ERROR_RESMAP_NOT_FOUND; return SCI_ERROR_RESMAP_NOT_FOUND;
} }
resource_nr = fsize / SCI0_RESMAP_ENTRIES_SIZE; int resource_nr = fsize / SCI0_RESMAP_ENTRIES_SIZE;
resources = (Resource *)sci_calloc(resource_nr, sizeof(Resource));
// Sets valid default values for most entries
do { do {
int read_ok = file.read(&buf, SCI0_RESMAP_ENTRIES_SIZE); int read_ok = file.read(&buf, SCI0_RESMAP_ENTRIES_SIZE);
next_entry = 1;
if (read_ok < 0) { if (read_ok != SCI0_RESMAP_ENTRIES_SIZE) {
sciprintf("Error while reading %s: ", map->location_name.c_str()); sciprintf("Error while reading %s: ", map->location_name.c_str());
perror(""); perror("");
next_entry = 0; break;
} else if (read_ok != SCI0_RESMAP_ENTRIES_SIZE) { }
next_entry = 0; if(buf[5] == 0xff)
} else if (buf[5] == 0xff) // Most significant offset byte break;
next_entry = 0;
if (next_entry) {
int fresh = 1;
int addto = resource_index;
int i;
if (resReadEntry(map, buf, resources + resource_index, *sci_version)) {
free(resources);
return SCI_ERROR_RESMAP_NOT_FOUND;
}
for (i = 0; i < resource_index; i++)
if (resources[resource_index].id == resources[i].id) {
addto = i;
fresh = 0;
}
addAltSource(resources + addto, resources[resource_index].source, resources[resource_index].file_offset);
if (fresh)
++resource_index;
if (++resources_total_read >= resource_nr) {
warning("After %d entries, resource.map is not terminated", resource_index);
next_entry = 0;
}
if (resReadEntry(map, buf, &res1, *sci_version))
return SCI_ERROR_RESMAP_NOT_FOUND;
uint32 resId = RESOURCE_HASH(res1.type, res1.number);
// adding a new resource
if (_resMap.contains(resId) == false) {
res = new Resource;
res->id = res1.id;
res->file_offset = res1.file_offset;
res->number = res1.number;
res->type = res1.type;
res->source = res1.source;
_resMap.setVal(resId, res);
bAdded = true;
} }
} while (next_entry); if (++resources_total_read >= resource_nr) {
warning("After %d entries, resource.map is not terminated", resources_total_read);
break;
}
} while (!file.eos());
if (!bAdded)
return SCI_ERROR_RESMAP_NOT_FOUND;
file.close(); file.close();
if (!resource_index) {
sciprintf("resource.map was empty!\n");
freeResources(resources, resource_nr);
return SCI_ERROR_RESMAP_NOT_FOUND;
}
if (max_resfile_nr > 999) {
freeResources(resources, resource_nr);
return SCI_ERROR_INVALID_RESMAP_ENTRY;
} else {
#if 0
// Check disabled, Mac SQ3 thinks it has resource.004 but doesn't need it -- CR
// Check whether the highest resfile used exists
char filename_buf[14];
sprintf(filename_buf, "resource.%03d", max_resfile_nr);
if (!file.open(filename_buf) {
_scir_free_resources(resources, resource_nr);
sciprintf("'%s' requested by resource.map, but not found\n", filename_buf);
return SCI_ERROR_INVALID_RESMAP_ENTRY;
}
#endif
}
if (resource_index < resource_nr)
resources = (Resource *)sci_realloc(resources, sizeof(Resource) * resource_index);
_resources = resources;
_resourcesNr = resource_index;
return 0; return 0;
} }
@ -374,7 +327,6 @@ int ResourceManager::isSCI10or11(int *types) {
int ResourceManager::readResourceMapSCI1(ResourceSource *map, ResourceSource *vol, int *sci_version) { int ResourceManager::readResourceMapSCI1(ResourceSource *map, ResourceSource *vol, int *sci_version) {
int fsize; int fsize;
Common::File file; Common::File file;
Resource *resources, *resource_start;
int resource_nr; int resource_nr;
int resource_index = 0; int resource_index = 0;
int ofs, header_size; int ofs, header_size;
@ -412,10 +364,6 @@ int ResourceManager::readResourceMapSCI1(ResourceSource *map, ResourceSource *vo
} }
resource_nr = (fsize - types[0]) / entrysize; resource_nr = (fsize - types[0]) / entrysize;
resource_start = resources = (Resource*)sci_realloc(_resources, (_resourcesNr + resource_nr) * sizeof(Resource));
memset(resource_start + sizeof(Resource) * _resourcesNr, 0, resource_nr * sizeof(Resource));
resources += _resourcesNr;
i = 0; i = 0;
while (types[i] == 0) while (types[i] == 0)
i++; i++;
@ -424,94 +372,38 @@ int ResourceManager::readResourceMapSCI1(ResourceSource *map, ResourceSource *vo
file.seek(ofs, SEEK_SET); file.seek(ofs, SEEK_SET);
for (i = 0; i < resource_nr; i++) { for (i = 0; i < resource_nr; i++) {
int read_ok = file.read(&buf, entrysize);
int j;
Resource *res; Resource *res;
int addto = resource_index; int number;
int fresh = 1; ResourceType type;
uint32 resId;
if (read_ok < entrysize) { file.read(&buf, entrysize);
#if 0 type = resTypeSCI1(ofs, types, lastrt);
if (!file.eof()) { number = SCI1_RESFILE_GET_NUMBER(buf);
sciprintf("Error while reading %s: ", map->location_name.c_str()); resId = RESOURCE_HASH(type, number);
perror(""); // adding new resource only if it does not exist
} else if (_resMap.contains(resId) == false) {
read_ok = 1; res = new Resource;
break; _resMap.setVal(resId, res);
#endif res->type = type;
res->number = number;
res->id = res->number | (res->type << 16);
// only 1st source would be used when loading resource
if (entry_size_selector < SCI_VERSION_1_1) {
res->source = getVolume(map, SCI1_RESFILE_GET_FILE(buf));
res->file_offset = SCI1_RESFILE_GET_OFFSET(buf);
} else {
res->source = vol;
res->file_offset = SCI11_RESFILE_GET_OFFSET(buf);
};
} }
res = &(resources[resource_index]);
res->type = resTypeSCI1(ofs, types, lastrt);
res->number = SCI1_RESFILE_GET_NUMBER(buf);
res->status = SCI_STATUS_NOMALLOC;
if (entry_size_selector < SCI_VERSION_1_1) {
res->source = getVolume(map, SCI1_RESFILE_GET_FILE(buf));
res->file_offset = SCI1_RESFILE_GET_OFFSET(buf);
} else {
res->source = vol;
res->file_offset = SCI11_RESFILE_GET_OFFSET(buf);
};
res->id = res->number | (res->type << 16);
for (j = 0; i < resource_index; i++)
if (resources[resource_index].id == resources[i].id) {
addto = i;
fresh = 0;
}
#if 0
fprintf(stderr, "Read [%04x] %6d.%s\tresource.%03d, %08x ==> %d\n",
res->id, res->number,
sci_resource_type_suffixes[res->type],
res->file, res->file_offset, addto);
#endif
addAltSource(resources + addto, resources[resource_index].source, resources[resource_index].file_offset);
if (fresh)
++resource_index;
ofs += entrysize; ofs += entrysize;
} }
free(types); free(types);
_resources = resource_start;
_resourcesNr += resource_index;
return 0;
}
#ifdef TEST_RESOURCE_MAP
int main(int argc, char **argv) {
int resource_nr;
Resource *resources;
int notok = sci0_read_resource_map(".", &resources, &resource_nr);
if (notok) {
fprintf(stderr, "Failed: Error code %d\n", notok);
return 1;
}
if (resources) {
int i;
printf("Found %d resources:\n", resource_nr);
for (i = 0; i < resource_nr; i++) {
Resource *res = resources + i;
printf("#%04d:\tRESOURCE.%03d:%8d\t%s.%03d\n", i, res->file, res->file_offset,
sci_resource_types[res->type], res->number);
}
} else
fprintf(stderr, "Found no resources.\n");
return 0; return 0;
} }
#endif
} // End of namespace Sci } // End of namespace Sci

View file

@ -51,14 +51,15 @@ void ResourceManager::process_patch(ResourceSource *source,
if (!file.open(member.createReadStream(), member.getName())) if (!file.open(member.createReadStream(), member.getName()))
perror("""__FILE__"": (""__LINE__""): failed to open"); perror("""__FILE__"": (""__LINE__""): failed to open");
else { else {
uint8 filehdr[2]; byte filehdr[2];
Resource *newrsc = findResourceUnsorted(_resources, _resourcesNr, restype, resnumber);
int fsize = file.size(); int fsize = file.size();
if (fsize < 3) { if (fsize < 3) {
printf("File too small\n"); printf("File too small\n");
return; return;
} }
uint32 resId = RESOURCE_HASH(restype, resnumber);
Resource *newrsc;
int patch_data_offset; int patch_data_offset;
file.read(filehdr, 2); file.read(filehdr, 2);
@ -74,23 +75,20 @@ void ResourceManager::process_patch(ResourceSource *source,
fsize -= patch_data_offset; fsize -= patch_data_offset;
// Prepare destination, if neccessary // Prepare destination, if neccessary
if (!newrsc) { if (_resMap.contains(resId) == false) {
// Completely new resource! newrsc = new Resource;
_resourcesNr++; _resMap.setVal(resId, newrsc);
_resources = (Resource *)sci_realloc(_resources, _resourcesNr * sizeof(Resource)); } else
newrsc = (_resources - 1) + _resourcesNr; newrsc = _resMap.getVal(resId);
newrsc->alt_sources = NULL;
}
// Overwrite everything, because we're patching // Overwrite everything, because we're patching
newrsc->size = fsize - 2; newrsc->size = fsize - 2;
newrsc->id = restype << 11 | resnumber; newrsc->id = resId;
newrsc->number = resnumber; newrsc->number = resnumber;
newrsc->status = SCI_STATUS_NOMALLOC; newrsc->status = SCI_STATUS_NOMALLOC;
newrsc->type = restype; newrsc->type = restype;
newrsc->source = source; newrsc->source = source;
newrsc->file_offset = 2 + patch_data_offset; newrsc->file_offset = 2 + patch_data_offset;
addAltSource(newrsc, source, 2);
printf("OK\n"); printf("OK\n");
} }
} }