diff options
author | Greg Frieger | 2009-02-28 20:45:36 +0000 |
---|---|---|
committer | Greg Frieger | 2009-02-28 20:45:36 +0000 |
commit | 4dd1b7dad55333fdbfefb6c1ea7acb55a5127863 (patch) | |
tree | 07c7b74de7e3f50273dc53cc8927e90075f0ce4c /engines/sci/scicore/resource.cpp | |
parent | 460131f5d35fffba4126cdf4ea24f2f32cfebc25 (diff) | |
download | scummvm-rg350-4dd1b7dad55333fdbfefb6c1ea7acb55a5127863.tar.gz scummvm-rg350-4dd1b7dad55333fdbfefb6c1ea7acb55a5127863.tar.bz2 scummvm-rg350-4dd1b7dad55333fdbfefb6c1ea7acb55a5127863.zip |
Turned ResourceManager into a class, along with all related functions
svn-id: r38978
Diffstat (limited to 'engines/sci/scicore/resource.cpp')
-rw-r--r-- | engines/sci/scicore/resource.cpp | 229 |
1 files changed, 109 insertions, 120 deletions
diff --git a/engines/sci/scicore/resource.cpp b/engines/sci/scicore/resource.cpp index f68b50ef17..88efb1a4ce 100644 --- a/engines/sci/scicore/resource.cpp +++ b/engines/sci/scicore/resource.cpp @@ -25,8 +25,6 @@ // Resource library -#include "common/archive.h" -#include "common/file.h" #include "common/util.h" #include "sci/tools.h" @@ -126,7 +124,7 @@ int resourcecmp(const void *first, const void *second) { //-- Resmgr helper functions -- -void _scir_add_altsource(resource_t *res, ResourceSource *source, unsigned int file_offset) { +void ResourceManager::addAltSource(resource_t *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; @@ -135,7 +133,7 @@ void _scir_add_altsource(resource_t *res, ResourceSource *source, unsigned int f res->alt_sources = rsrc; } -resource_t *_scir_find_resource_unsorted(resource_t *res, int res_nr, int type, int number) { +resource_t *ResourceManager::findResourceUnsorted(resource_t *res, int res_nr, int type, int number) { int i; for (i = 0; i < res_nr; i++) if (res[i].number == number && res[i].type == type) @@ -145,12 +143,12 @@ resource_t *_scir_find_resource_unsorted(resource_t *res, int res_nr, int type, // Resource source list management -ResourceSource *scir_add_external_map(ResourceManager *mgr, const char *file_name) { +ResourceSource *ResourceManager::addExternalMap(const char *file_name) { ResourceSource *newsrc = new ResourceSource(); // Add the new source to the SLL of sources - newsrc->next = mgr->_sources; - mgr->_sources = newsrc; + newsrc->next = _sources; + _sources = newsrc; newsrc->source_type = RESSOURCE_TYPE_EXTERNAL_MAP; newsrc->location_name = file_name; @@ -160,12 +158,12 @@ ResourceSource *scir_add_external_map(ResourceManager *mgr, const char *file_nam return newsrc; } -ResourceSource *scir_add_volume(ResourceManager *mgr, ResourceSource *map, const char *filename, int number, int extended_addressing) { +ResourceSource *ResourceManager::addVolume(ResourceSource *map, const char *filename, int number, int extended_addressing) { ResourceSource *newsrc = new ResourceSource(); // Add the new source to the SLL of sources - newsrc->next = mgr->_sources; - mgr->_sources = newsrc; + newsrc->next = _sources; + _sources = newsrc; newsrc->source_type = RESSOURCE_TYPE_VOLUME; newsrc->scanned = false; @@ -176,12 +174,12 @@ ResourceSource *scir_add_volume(ResourceManager *mgr, ResourceSource *map, const return 0; } -ResourceSource *scir_add_patch_dir(ResourceManager *mgr, const char *dirname) { +ResourceSource *ResourceManager::addPatchDir(const char *dirname) { ResourceSource *newsrc = new ResourceSource(); // Add the new source to the SLL of sources - newsrc->next = mgr->_sources; - mgr->_sources = newsrc; + newsrc->next = _sources; + _sources = newsrc; newsrc->source_type = RESSOURCE_TYPE_DIRECTORY; newsrc->scanned = false; @@ -190,8 +188,8 @@ ResourceSource *scir_add_patch_dir(ResourceManager *mgr, const char *dirname) { return 0; } -ResourceSource *scir_get_volume(ResourceManager *mgr, ResourceSource *map, int volume_nr) { - ResourceSource *seeker = mgr->_sources; +ResourceSource *ResourceManager::getVolume(ResourceSource *map, int volume_nr) { + ResourceSource *seeker = _sources; while (seeker) { if (seeker->source_type == RESSOURCE_TYPE_VOLUME && seeker->associated_map == map && @@ -205,7 +203,7 @@ ResourceSource *scir_get_volume(ResourceManager *mgr, ResourceSource *map, int v // Resource manager constructors and operations -static void _scir_load_from_patch_file(Common::File &file, resource_t *res, char *filename) { +void ResourceManager::loadFromPatchFile(Common::File &file, resource_t *res, char *filename) { unsigned int really_read; res->data = (unsigned char *)sci_malloc(res->size); @@ -218,7 +216,7 @@ static void _scir_load_from_patch_file(Common::File &file, resource_t *res, char res->status = SCI_STATUS_ALLOCATED; } -static void _scir_load_resource(ResourceManager *mgr, resource_t *res, bool protect) { +void ResourceManager::loadResource(resource_t *res, bool protect) { char filename[MAXPATHLEN]; Common::File file; resource_t backup; @@ -227,12 +225,12 @@ static void _scir_load_resource(ResourceManager *mgr, resource_t *res, bool prot // First try lower-case name if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) { - if (!patch_sprintfers[mgr->sci_version]) { - error("Resource manager's SCI version (%d) has no patch file name printers", mgr->sci_version); + if (!patch_sprintfers[sci_version]) { + error("Resource manager's SCI version (%d) has no patch file name printers", sci_version); } // Get patch file name - patch_sprintfers[mgr->sci_version](filename, res); + patch_sprintfers[sci_version](filename, res); // FIXME: Instead of using SearchMan, maybe we should only search // a single dir specified by this RESSOURCE_TYPE_DIRECTORY ResourceSource? @@ -250,13 +248,13 @@ static void _scir_load_resource(ResourceManager *mgr, resource_t *res, bool prot file.seek(res->file_offset, SEEK_SET); if (res->source->source_type == RESSOURCE_TYPE_DIRECTORY) - _scir_load_from_patch_file(file, res, filename); - else if (!decompressors[mgr->sci_version]) { + loadFromPatchFile(file, res, filename); + else if (!decompressors[sci_version]) { // Check whether we support this at all - error("Resource manager's SCI version (%d) is invalid", mgr->sci_version); + error("Resource manager's SCI version (%d) is invalid", sci_version); } else { int error = // Decompress from regular resource file - decompressors[mgr->sci_version](res, file, mgr->sci_version); + decompressors[sci_version](res, file, sci_version); if (error) { sciprintf("Error %d occured while reading %s.%03d from resource file: %s\n", @@ -273,11 +271,11 @@ static void _scir_load_resource(ResourceManager *mgr, resource_t *res, bool prot } -resource_t *scir_test_resource(ResourceManager *mgr, int type, int number) { +resource_t *ResourceManager::testResource(int type, int number) { resource_t binseeker; binseeker.type = type; binseeker.number = number; - return (resource_t *)bsearch(&binseeker, mgr->_resources, mgr->_resourcesNr, sizeof(resource_t), resourcecmp); + return (resource_t *)bsearch(&binseeker, _resources, _resourcesNr, sizeof(resource_t), resourcecmp); } int sci0_get_compression_method(Common::ReadStream &stream); @@ -292,7 +290,7 @@ int sci_test_view_type(ResourceManager *mgr) { mgr->sci_version = SCI_VERSION_AUTODETECT; for (i = 0; i < 1000; i++) { - res = scir_test_resource(mgr, sci_view, i); + res = mgr->testResource(sci_view, i); if (!res) continue; @@ -317,7 +315,7 @@ int sci_test_view_type(ResourceManager *mgr) { // Try the same thing with pics for (i = 0; i < 1000; i++) { - res = scir_test_resource(mgr, sci_pic, i); + res = mgr->testResource(sci_pic, i); if (!res) continue; @@ -343,13 +341,13 @@ int sci_test_view_type(ResourceManager *mgr) { return mgr->sci_version; } -int scir_add_appropriate_sources(ResourceManager *mgr) { +int ResourceManager::addAppropriateSources() { //char path_separator; ResourceSource *map; if (!Common::File::exists("RESOURCE.MAP")) return 0; - map = scir_add_external_map(mgr, "RESOURCE.MAP"); + map = addExternalMap("RESOURCE.MAP"); Common::ArchiveMemberList files; SearchMan.listMatchingMembers(files, "RESOURCE.0??"); @@ -359,38 +357,37 @@ int scir_add_appropriate_sources(ResourceManager *mgr) { const char *dot = strrchr(name.c_str(), '.'); int number = atoi(dot + 1); - scir_add_volume(mgr, map, name.c_str(), number, 0); + addVolume(map, name.c_str(), number, 0); } - scir_add_patch_dir(mgr, ""); // FIXME: used to pass the 'current' instead of "" - + addPatchDir(""); // FIXME: used to pass the 'current' instead of "" return 1; } -static int _scir_scan_new_sources(ResourceManager *mgr, int *detected_version, ResourceSource *source) { - int preset_version = mgr->sci_version; +int ResourceManager::scanNewSources(int *detected_version, ResourceSource *source) { + int preset_version = sci_version; int resource_error = 0; - int dummy = mgr->sci_version; + int dummy = sci_version; //resource_t **concat_ptr = &(mgr->_resources[mgr->_resourcesNr - 1].next); if (detected_version == NULL) detected_version = &dummy; - *detected_version = mgr->sci_version; + *detected_version = sci_version; if (source->next) - _scir_scan_new_sources(mgr, detected_version, source->next); + scanNewSources(detected_version, source->next); if (!source->scanned) { source->scanned = true; switch (source->source_type) { case RESSOURCE_TYPE_DIRECTORY: - if (mgr->sci_version <= SCI_VERSION_01) - sci0_read_resource_patches(source, &mgr->_resources, &mgr->_resourcesNr); + if (sci_version <= SCI_VERSION_01) + readResourcePatchesSCI0(source); else - sci1_read_resource_patches(source, &mgr->_resources, &mgr->_resourcesNr); + readResourcePatchesSCI1(source); break; case RESSOURCE_TYPE_EXTERNAL_MAP: if (preset_version <= SCI_VERSION_01_VGA_ODD /* || preset_version == SCI_VERSION_AUTODETECT -- subsumed by the above line */) { - resource_error = sci0_read_resource_map(mgr, source, &mgr->_resources, &mgr->_resourcesNr, detected_version); + resource_error = readResourceMapSCI0(source, detected_version); #if 0 if (resource_error >= SCI_ERROR_CRITICAL) { sciprintf("Resmgr: Error while loading resource map: %s\n", sci_error_types[resource_error]); @@ -415,8 +412,7 @@ static int _scir_scan_new_sources(ResourceManager *mgr, int *detected_version, R if ((preset_version == SCI_VERSION_1_EARLY) || (preset_version == SCI_VERSION_1_LATE) || (preset_version == SCI_VERSION_1_1) || ((*detected_version == SCI_VERSION_AUTODETECT) && (preset_version == SCI_VERSION_AUTODETECT))) { - resource_error = sci1_read_resource_map(mgr, source, scir_get_volume(mgr, source, 0), - &mgr->_resources, &mgr->_resourcesNr, detected_version); + resource_error = readResourceMapSCI1(source, getVolume(source, 0), detected_version); if (resource_error == SCI_ERROR_RESMAP_NOT_FOUND) { // FIXME: Try reading w/o resource.map resource_error = SCI_ERROR_NO_RESOURCE_FILES_FOUND; @@ -424,55 +420,48 @@ static int _scir_scan_new_sources(ResourceManager *mgr, int *detected_version, R if (resource_error == SCI_ERROR_NO_RESOURCE_FILES_FOUND) { // Initialize empty resource manager - mgr->_resourcesNr = 0; - mgr->_resources = 0; // FIXME: Was = (resource_t*)sci_malloc(1); + _resourcesNr = 0; + _resources = 0; // FIXME: Was = (resource_t*)sci_malloc(1); resource_error = 0; } } - mgr->sci_version = *detected_version; + sci_version = *detected_version; break; default: break; } - qsort(mgr->_resources, mgr->_resourcesNr, sizeof(resource_t), resourcecmp); // Sort resources + qsort(_resources, _resourcesNr, sizeof(resource_t), resourcecmp); // Sort resources } return resource_error; } -int scir_scan_new_sources(ResourceManager *mgr, int *detected_version) { - _scir_scan_new_sources(mgr, detected_version, mgr->_sources); - - return 0; -} - -static void _scir_free_resource_sources(ResourceSource *rss) { +void ResourceManager::freeResourceSources(ResourceSource *rss) { if (rss) { - _scir_free_resource_sources(rss->next); + freeResourceSources(rss->next); delete rss; } } ResourceManager::ResourceManager(int version, int maxMemory) { int resource_error = 0; - ResourceManager *mgr = this; int resmap_version = version; _maxMemory = maxMemory; - mgr->memory_locked = 0; - mgr->memory_lru = 0; + _memoryLocked = 0; + _memoryLRU = 0; _resources = NULL; _resourcesNr = 0; _sources = NULL; - mgr->sci_version = version; + sci_version = version; - mgr->lru_first = NULL; - mgr->lru_last = NULL; + lru_first = NULL; + lru_last = NULL; - scir_add_appropriate_sources(mgr); - scir_scan_new_sources(mgr, &resmap_version); + addAppropriateSources(); + scanNewSources(&resmap_version, _sources); if (!_resources || !_resourcesNr) { if (_resources) { @@ -480,7 +469,7 @@ ResourceManager::ResourceManager(int version, int maxMemory) { _resources = NULL; } sciprintf("Resmgr: Could not retrieve a resource list!\n"); - _scir_free_resource_sources(mgr->_sources); + freeResourceSources(_sources); error("FIXME: Move this code to an init() method so that we can perform error handling"); // return NULL; } @@ -490,20 +479,20 @@ ResourceManager::ResourceManager(int version, int maxMemory) { if (version == SCI_VERSION_AUTODETECT) switch (resmap_version) { case SCI_VERSION_0: - if (scir_test_resource(mgr, sci_vocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) { - version = sci_test_view_type(mgr); + if (testResource(sci_vocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB)) { + version = sci_test_view_type(this); if (version == SCI_VERSION_01_VGA) { sciprintf("Resmgr: Detected KQ5 or similar\n"); } else { sciprintf("Resmgr: Detected SCI0\n"); version = SCI_VERSION_0; } - } else if (scir_test_resource(mgr, sci_vocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB)) { - version = sci_test_view_type(mgr); + } else if (testResource(sci_vocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB)) { + version = sci_test_view_type(this); if (version == SCI_VERSION_01_VGA) { sciprintf("Resmgr: Detected KQ5 or similar\n"); } else { - if (scir_test_resource(mgr, sci_vocab, 912)) { + if (testResource(sci_vocab, 912)) { sciprintf("Resmgr: Running KQ1 or similar, using SCI0 resource encoding\n"); version = SCI_VERSION_0; } else { @@ -512,7 +501,7 @@ ResourceManager::ResourceManager(int version, int maxMemory) { } } } else { - version = sci_test_view_type(mgr); + version = sci_test_view_type(this); if (version == SCI_VERSION_01_VGA) { sciprintf("Resmgr: Detected KQ5 or similar\n"); } else { @@ -526,13 +515,13 @@ ResourceManager::ResourceManager(int version, int maxMemory) { sciprintf("Resmgr: Detected Jones/CD or similar\n"); break; case SCI_VERSION_1: { - resource_t *res = scir_test_resource(mgr, sci_script, 0); + resource_t *res = testResource(sci_script, 0); - mgr->sci_version = version = SCI_VERSION_1_EARLY; - _scir_load_resource(mgr, res, true); + sci_version = version = SCI_VERSION_1_EARLY; + loadResource(res, true); if (res->status == SCI_STATUS_NOMALLOC) - mgr->sci_version = version = SCI_VERSION_1_LATE; + sci_version = version = SCI_VERSION_1_LATE; break; } case SCI_VERSION_1_1: @@ -547,24 +536,24 @@ ResourceManager::ResourceManager(int version, int maxMemory) { qsort(_resources, _resourcesNr, sizeof(resource_t), resourcecmp); // Sort resources } - mgr->sci_version = version; + sci_version = version; } -static void _scir_free_altsources(resource_altsource_t *dynressrc) { +void ResourceManager::freeAltSources(resource_altsource_t *dynressrc) { if (dynressrc) { - _scir_free_altsources(dynressrc->next); + freeAltSources(dynressrc->next); free(dynressrc); } } -void _scir_free_resources(resource_t *resources, int _resourcesNr) { +void ResourceManager::freeResources(resource_t *resources, int _resourcesNr) { int i; for (i = 0; i < _resourcesNr; i++) { resource_t *res = resources + i; // FIXME: alt_sources->next may point to an invalid memory location - _scir_free_altsources(res->alt_sources); + freeAltSources(res->alt_sources); if (res->status != SCI_STATUS_NOMALLOC) free(res->data); @@ -574,18 +563,18 @@ void _scir_free_resources(resource_t *resources, int _resourcesNr) { } ResourceManager::~ResourceManager() { - _scir_free_resources(_resources, _resourcesNr); - _scir_free_resource_sources(_sources); + freeResources(_resources, _resourcesNr); + freeResourceSources(_sources); _resources = NULL; } -static void _scir_unalloc(resource_t *res) { +void ResourceManager::unalloc(resource_t *res) { free(res->data); res->data = NULL; res->status = SCI_STATUS_NOMALLOC; } -static void _scir_remove_from_lru(ResourceManager *mgr, resource_t *res) { +void ResourceManager::removeFromLRU(resource_t *res) { if (res->status != SCI_STATUS_ENQUEUED) { sciprintf("Resmgr: Oops: trying to remove resource that isn't enqueued\n"); return; @@ -595,45 +584,45 @@ static void _scir_remove_from_lru(ResourceManager *mgr, resource_t *res) { res->next->prev = res->prev; if (res->prev) res->prev->next = res->next; - if (mgr->lru_first == res) - mgr->lru_first = res->next; - if (mgr->lru_last == res) - mgr->lru_last = res->prev; + if (lru_first == res) + lru_first = res->next; + if (lru_last == res) + lru_last = res->prev; - mgr->memory_lru -= res->size; + _memoryLRU -= res->size; res->status = SCI_STATUS_ALLOCATED; } -static void _scir_add_to_lru(ResourceManager *mgr, resource_t *res) { +void ResourceManager::addToLRU(resource_t *res) { if (res->status != SCI_STATUS_ALLOCATED) { sciprintf("Resmgr: Oops: trying to enqueue resource with state %d\n", res->status); return; } res->prev = NULL; - res->next = mgr->lru_first; - mgr->lru_first = res; - if (!mgr->lru_last) - mgr->lru_last = res; + res->next = lru_first; + lru_first = res; + if (!lru_last) + lru_last = res; if (res->next) res->next->prev = res; - mgr->memory_lru += res->size; + _memoryLRU += res->size; #if (SCI_VERBOSE_RESMGR > 1) fprintf(stderr, "Adding %s.%03d (%d bytes) to lru control: %d bytes total\n", sci_resource_types[res->type], res->number, res->size, - mgr->memory_lru); + mgr->_memoryLRU); #endif res->status = SCI_STATUS_ENQUEUED; } -static void _scir_print_lru_list(ResourceManager *mgr) { +void ResourceManager::printLRU() { int mem = 0; int entries = 0; - resource_t *res = mgr->lru_first; + resource_t *res = lru_first; while (res) { fprintf(stderr, "\t%s.%03d: %d bytes\n", @@ -645,47 +634,47 @@ static void _scir_print_lru_list(ResourceManager *mgr) { } fprintf(stderr, "Total: %d entries, %d bytes (mgr says %d)\n", - entries, mem, mgr->memory_lru); + entries, mem, _memoryLRU); } -static void _scir_free_old_resources(ResourceManager *mgr, int last_invulnerable) { - while (mgr->_maxMemory < mgr->memory_lru && (!last_invulnerable || mgr->lru_first != mgr->lru_last)) { - resource_t *goner = mgr->lru_last; +void ResourceManager::freeOldResources(int last_invulnerable) { + while (_maxMemory < _memoryLRU && (!last_invulnerable || lru_first != lru_last)) { + resource_t *goner = lru_last; if (!goner) { fprintf(stderr, "Internal error: mgr->lru_last is NULL!\n"); - fprintf(stderr, "LRU-mem= %d\n", mgr->memory_lru); - fprintf(stderr, "lru_first = %p\n", (void *)mgr->lru_first); - _scir_print_lru_list(mgr); + fprintf(stderr, "LRU-mem= %d\n", _memoryLRU); + fprintf(stderr, "lru_first = %p\n", (void *)lru_first); + printLRU(); } - _scir_remove_from_lru(mgr, goner); - _scir_unalloc(goner); + removeFromLRU(goner); + unalloc(goner); #ifdef SCI_VERBOSE_RESMGR sciprintf("Resmgr-debug: LRU: Freeing %s.%03d (%d bytes)\n", sci_resource_types[goner->type], goner->number, goner->size); #endif } } -resource_t * scir_find_resource(ResourceManager *mgr, int type, int number, int lock) { +resource_t *ResourceManager::findResource(int type, int number, int lock) { resource_t *retval; - if (number >= sci_max_resource_nr[mgr->sci_version]) { - int modded_number = number % sci_max_resource_nr[mgr->sci_version]; + if (number >= sci_max_resource_nr[sci_version]) { + int modded_number = number % sci_max_resource_nr[sci_version]; sciprintf("[resmgr] Requested invalid resource %s.%d, mapped to %s.%d\n", sci_resource_types[type], number, sci_resource_types[type], modded_number); number = modded_number; } - retval = scir_test_resource(mgr, type, number); + retval = testResource(type, number); if (!retval) return NULL; if (!retval->status) - _scir_load_resource(mgr, retval, false); + loadResource(retval, false); else if (retval->status == SCI_STATUS_ENQUEUED) - _scir_remove_from_lru(mgr, retval); + removeFromLRU(retval); // Unless an error occured, the resource is now either // locked or allocated, but never queued or freed. @@ -693,17 +682,17 @@ resource_t * scir_find_resource(ResourceManager *mgr, int type, int number, int if (retval->status == SCI_STATUS_ALLOCATED) { retval->status = SCI_STATUS_LOCKED; retval->lockers = 0; - mgr->memory_locked += retval->size; + _memoryLocked += retval->size; } ++retval->lockers; } else if (retval->status != SCI_STATUS_LOCKED) { // Don't lock it if (retval->status == SCI_STATUS_ALLOCATED) - _scir_add_to_lru(mgr, retval); + addToLRU(retval); } - _scir_free_old_resources(mgr, retval->status == SCI_STATUS_ALLOCATED); + freeOldResources(retval->status == SCI_STATUS_ALLOCATED); if (retval->data) return retval; @@ -713,7 +702,7 @@ resource_t * scir_find_resource(ResourceManager *mgr, int type, int number, int } } -void scir_unlock_resource(ResourceManager *mgr, resource_t *res, int resnum, int restype) { +void ResourceManager::unlockResource(resource_t *res, int resnum, int restype) { if (!res) { if (restype >= ARRAYSIZE(sci_resource_types)) sciprintf("Resmgr: Warning: Attempt to unlock non-existant resource %03d.%03d!\n", restype, resnum); @@ -730,11 +719,11 @@ void scir_unlock_resource(ResourceManager *mgr, resource_t *res, int resnum, int if (!--res->lockers) { // No more lockers? res->status = SCI_STATUS_ALLOCATED; - mgr->memory_locked -= res->size; - _scir_add_to_lru(mgr, res); + _memoryLocked -= res->size; + addToLRU(res); } - _scir_free_old_resources(mgr, 0); + freeOldResources(0); } } // End of namespace Sci |