From 70d20096c16b983d254366b82e725ce14e5b195c Mon Sep 17 00:00:00 2001 From: johndoe123 Date: Fri, 24 Jun 2011 15:36:35 +0000 Subject: NEVERHOOD: Start with the resource manager --- engines/neverhood/blbarchive.cpp | 10 ++- engines/neverhood/blbarchive.h | 3 + engines/neverhood/module.mk | 3 +- engines/neverhood/neverhood.cpp | 19 ++++- engines/neverhood/resourceman.cpp | 167 ++++++++++++++++++++++++++++++++++++++ engines/neverhood/resourceman.h | 78 ++++++++++++++++++ 6 files changed, 274 insertions(+), 6 deletions(-) create mode 100644 engines/neverhood/resourceman.cpp create mode 100644 engines/neverhood/resourceman.h (limited to 'engines') diff --git a/engines/neverhood/blbarchive.cpp b/engines/neverhood/blbarchive.cpp index 48452efa8b..4259be33f7 100644 --- a/engines/neverhood/blbarchive.cpp +++ b/engines/neverhood/blbarchive.cpp @@ -49,7 +49,7 @@ void BlbArchive::open(const Common::String &filename) { if (header.id1 != 0x2004940 || header.id2 != 7 || header.fileSize != _fd.size()) error("BlbArchive::open() %s seems to be corrupt", filename.c_str()); - debug("fileCount = %d", header.fileCount); + debug(2, "fileCount = %d", header.fileCount); _entries.reserve(header.fileCount); @@ -70,7 +70,7 @@ void BlbArchive::open(const Common::String &filename) { entry.offset = _fd.readUint32LE(); entry.diskSize = _fd.readUint32LE(); entry.size = _fd.readUint32LE(); - debug("%08X: %03d, %02X, %04X, %08X, %08X, %08X, %08X", + debug(2, "%08X: %03d, %02X, %04X, %08X, %08X, %08X, %08X", entry.fileHash, entry.type, entry.comprType, entry.extDataOfs, entry.timeStamp, entry.offset, entry.diskSize, entry.size); } @@ -84,7 +84,6 @@ void BlbArchive::open(const Common::String &filename) { } void BlbArchive::load(uint index, byte *buffer, uint32 size) { - BlbArchiveEntry &entry = _entries[index]; _fd.seek(entry.offset); @@ -104,4 +103,9 @@ void BlbArchive::load(uint index, byte *buffer, uint32 size) { } +byte *BlbArchive::getEntryExtData(uint index) { + BlbArchiveEntry &entry = _entries[index]; + return _extData && entry.extDataOfs != 0 ? &_extData[entry.extDataOfs - 1] : NULL; +} + } // End of namespace Neverhood diff --git a/engines/neverhood/blbarchive.h b/engines/neverhood/blbarchive.h index 7fc54d90a7..3c373ccc96 100644 --- a/engines/neverhood/blbarchive.h +++ b/engines/neverhood/blbarchive.h @@ -54,7 +54,10 @@ public: ~BlbArchive(); void open(const Common::String &filename); void load(uint index, byte *buffer, uint32 size); + byte *getEntryExtData(uint index); uint32 getSize(uint index) { return _entries[index].size; } + BlbArchiveEntry *getEntry(uint index) { return &_entries[index]; } + uint getCount() { return _entries.size(); } private: Common::File _fd; Common::Array _entries; diff --git a/engines/neverhood/module.mk b/engines/neverhood/module.mk index 0601f54d82..bdb6d8684e 100644 --- a/engines/neverhood/module.mk +++ b/engines/neverhood/module.mk @@ -3,7 +3,8 @@ MODULE := engines/neverhood MODULE_OBJS = \ blbarchive.o \ detection.o \ - neverhood.o + neverhood.o \ + resourceman.o # This module can be built as a plugin ifdef BUILD_PLUGINS diff --git a/engines/neverhood/neverhood.cpp b/engines/neverhood/neverhood.cpp index 873d7ff8ed..9254f886c0 100644 --- a/engines/neverhood/neverhood.cpp +++ b/engines/neverhood/neverhood.cpp @@ -38,6 +38,7 @@ #include "neverhood/neverhood.h" #include "neverhood/blbarchive.h" +#include "neverhood/resourceman.h" namespace Neverhood { @@ -67,11 +68,25 @@ Common::Error NeverhoodEngine::run() { _isSaveAllowed = false; - +#if 0 BlbArchive *blb = new BlbArchive(); blb->open("m.blb"); - delete blb; +#endif + +#if 1 + ResourceMan *res = new ResourceMan(); + res->addArchive("a.blb"); + res->addArchive("c.blb"); + res->addArchive("hd.blb"); + res->addArchive("i.blb"); + res->addArchive("m.blb"); + res->addArchive("s.blb"); + res->addArchive("t.blb"); + + ResourceFileEntry *r = res->findEntry(0x50A80517); + +#endif return Common::kNoError; } diff --git a/engines/neverhood/resourceman.cpp b/engines/neverhood/resourceman.cpp new file mode 100644 index 0000000000..59e3fad14a --- /dev/null +++ b/engines/neverhood/resourceman.cpp @@ -0,0 +1,167 @@ +/* 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. + * + */ + +#include "neverhood/resourceman.h" + +namespace Neverhood { + +ResourceMan::ResourceMan() { +} + +ResourceMan::~ResourceMan() { +} + +void ResourceMan::addArchive(const Common::String &filename) { + BlbArchive *archive = new BlbArchive(); + uint archiveIndex = _archives.size(); + archive->open(filename); + _archives.push_back(archive); + debug("ResourceMan::addArchive(%s) %d files", filename.c_str(), archive->getCount()); + _entries.reserve(_entries.size() + archive->getCount()); + for (uint archiveEntryIndex = 0; archiveEntryIndex < archive->getCount(); archiveEntryIndex++) { + BlbArchiveEntry *archiveEntry = archive->getEntry(archiveEntryIndex); + ResourceFileEntry *entry = findEntrySimple(archiveEntry->fileHash); + if (entry) { + if (archiveEntry->timeStamp > _archives[entry->archiveIndex]->getEntry(entry->entryIndex)->timeStamp) { + entry->archiveIndex = archiveIndex; + entry->entryIndex = archiveEntryIndex; + } + } else { + ResourceFileEntry newEntry; + newEntry.fileHash = archiveEntry->fileHash; + newEntry.resourceHandle = -1; + newEntry.archiveIndex = archiveIndex; + newEntry.entryIndex = archiveEntryIndex; + _entries.push_back(newEntry); + } + } +} + +ResourceFileEntry *ResourceMan::findEntrySimple(uint32 fileHash) { + for (uint i = 0; i < _entries.size(); i++) { + if (_entries[i].fileHash == fileHash) + return &_entries[i]; + } + return NULL; +} + +ResourceFileEntry *ResourceMan::findEntry(uint32 fileHash) { + ResourceFileEntry *entry = findEntrySimple(fileHash); + for (; entry && getArchiveEntry(entry)->comprType == 0x65; fileHash = getArchiveEntry(entry)->diskSize) + entry = findEntrySimple(fileHash); + return entry; +} + +BlbArchiveEntry *ResourceMan::getArchiveEntry(ResourceFileEntry *entry) const { + return _archives[entry->archiveIndex]->getEntry(entry->entryIndex); +} + +int ResourceMan::useResource(uint32 fileHash) { + ResourceFileEntry *entry = findEntry(fileHash); + if (entry->resourceHandle != -1) { + _resources[entry->resourceHandle]->useRefCount++; + } else { + Resource *resource = new Resource(); + resource->fileHash = entry->fileHash; + resource->archiveIndex = entry->archiveIndex; + resource->entryIndex = entry->entryIndex; + resource->data = NULL; + resource->dataRefCount = 0; + resource->useRefCount = 1; + entry->resourceHandle = (int)_resources.size(); + _resources.push_back(resource); + } + return entry->resourceHandle; +} + +void ResourceMan::unuseResource(int resourceHandle) { + Resource *resource = _resources[resourceHandle]; + if (resource->useRefCount > 0) + resource->useRefCount--; +} + +void ResourceMan::unuseResourceByHash(uint32 fileHash) { + ResourceFileEntry *entry = findEntry(fileHash); + if (entry->resourceHandle != -1) + unuseResource(entry->resourceHandle); +} + +int ResourceMan::getResourceHandleByHash(uint32 fileHash) { + ResourceFileEntry *entry = findEntry(fileHash); + return entry->resourceHandle; +} + +bool ResourceMan::isResourceDataValid(int resourceHandle) const { + return _resources[resourceHandle]->data != NULL; +} + +uint32 ResourceMan::getResourceSize(int resourceHandle) const { + Resource *resource = _resources[resourceHandle]; + return _archives[resource->archiveIndex]->getEntry(resource->entryIndex)->size; +} + +byte ResourceMan::getResourceType(int resourceHandle) { + Resource *resource = _resources[resourceHandle]; + return _archives[resource->archiveIndex]->getEntry(resource->entryIndex)->type; +} + +byte ResourceMan::getResourceTypeByHash(uint32 fileHash) { + ResourceFileEntry *entry = findEntry(fileHash); + return getArchiveEntry(entry)->type; +} + +byte *ResourceMan::getResourceExtData(int resourceHandle) { + Resource *resource = _resources[resourceHandle]; + return _archives[resource->archiveIndex]->getEntryExtData(resource->entryIndex); +} + +byte *ResourceMan::getResourceExtDataByHash(uint32 fileHash) { + ResourceFileEntry *entry = findEntry(fileHash); + return _archives[entry->archiveIndex]->getEntryExtData(entry->entryIndex); +} + +byte *ResourceMan::loadResource(int resourceHandle, bool moveToFront) { + Resource *resource = _resources[resourceHandle]; + if (resource->data != NULL) { + resource->dataRefCount++; + } else { + BlbArchive *archive = _archives[resource->archiveIndex]; + BlbArchiveEntry *archiveEntry = archive->getEntry(resource->entryIndex); + resource->data = new byte[archiveEntry->size]; + archive->load(resource->entryIndex, resource->data, 0); + resource->dataRefCount = 1; + } + return resource->data; +} + +void ResourceMan::unloadResource(int resourceHandle) { + Resource *resource = _resources[resourceHandle]; + if (resource->dataRefCount > 0) + resource->dataRefCount--; +} + +void ResourceMan::freeResource(Resource *resource) { + delete[] resource->data; + resource->data = NULL; +} + +} // End of namespace Neverhood diff --git a/engines/neverhood/resourceman.h b/engines/neverhood/resourceman.h new file mode 100644 index 0000000000..0dd70e13a2 --- /dev/null +++ b/engines/neverhood/resourceman.h @@ -0,0 +1,78 @@ +/* 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. + * + */ + +#ifndef NEVERHOOD_RESOURCEMAN_H +#define NEVERHOOD_RESOURCEMAN_H + +#include "common/array.h" +#include "common/file.h" +#include "neverhood/neverhood.h" +#include "neverhood/blbarchive.h" + +namespace Neverhood { + +struct ResourceFileEntry { + uint32 fileHash; + int resourceHandle; + uint archiveIndex; + uint entryIndex; +}; + +struct Resource { + uint32 fileHash; + uint archiveIndex; + uint entryIndex; + byte *data; + int dataRefCount; + int useRefCount; +}; + +class ResourceMan { +public: + ResourceMan(); + ~ResourceMan(); + void addArchive(const Common::String &filename); + ResourceFileEntry *findEntrySimple(uint32 fileHash); + ResourceFileEntry *findEntry(uint32 fileHash); + BlbArchiveEntry *getArchiveEntry(ResourceFileEntry *entry) const; + int useResource(uint32 fileHash); + void unuseResource(int resourceHandle); + void unuseResourceByHash(uint32 fileHash); + int getResourceHandleByHash(uint32 fileHash); + bool isResourceDataValid(int resourceHandle) const; + uint32 getResourceSize(int resourceHandle) const; + byte getResourceType(int resourceHandle); + byte getResourceTypeByHash(uint32 fileHash); + byte *getResourceExtData(int resourceHandle); + byte *getResourceExtDataByHash(uint32 fileHash); + byte *loadResource(int resourceHandle, bool moveToFront = false); + void unloadResource(int resourceHandle); + void freeResource(Resource *resource); +private: + Common::Array _archives; + Common::Array _entries; + Common::Array _resources; +}; + +} // End of namespace Neverhood + +#endif /* NEVERHOOD_RESOURCEMAN_H */ -- cgit v1.2.3