2009-08-04 13:27:04 +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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asylum/respack.h"
|
|
|
|
|
|
|
|
namespace Asylum {
|
|
|
|
|
2010-11-08 21:09:02 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// ResourceManager
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-11-09 00:58:40 +00:00
|
|
|
|
|
|
|
ResourceManager::ResourceManager() : _musicPackId(kResourcePackInvalid) {
|
|
|
|
}
|
|
|
|
|
2010-11-08 21:09:02 +00:00
|
|
|
ResourceEntry *ResourceManager::get(ResourceId id) {
|
|
|
|
ResourcePackId packId = (ResourcePackId)((id >> 16) & 0x7FFF);
|
|
|
|
uint16 index = (uint16)id;
|
|
|
|
|
|
|
|
// Check if we need to load a music pack
|
2010-11-09 00:58:40 +00:00
|
|
|
bool isMusicPack = (packId == kResourcePackMusic);
|
|
|
|
|
|
|
|
// Check that a music pack has been set
|
|
|
|
if (isMusicPack && _musicPackId == kResourcePackInvalid)
|
|
|
|
error("[ResourceManager::get] Current music pack Id has not been set!");
|
|
|
|
|
|
|
|
ResourceCache *cache = isMusicPack ? &_music : &_resources;
|
2010-11-08 21:09:02 +00:00
|
|
|
|
|
|
|
// Try getting the resource pack
|
|
|
|
if (!cache->contains(packId)) {
|
2010-11-09 00:58:40 +00:00
|
|
|
ResourcePack *pack = new ResourcePack(Common::String::format(isMusicPack ? "mus.%03d" : "res.%03d", isMusicPack ? _musicPackId : packId));
|
2010-11-08 21:09:02 +00:00
|
|
|
|
|
|
|
cache->setVal(packId, pack);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache->getVal(packId)->get(index);
|
2009-08-04 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-11-09 00:58:34 +00:00
|
|
|
void ResourceManager::unload(ResourcePackId id) {
|
2010-11-09 03:10:09 +00:00
|
|
|
if (_resources.contains(id))
|
|
|
|
_resources.erase(id);
|
2010-11-09 00:58:34 +00:00
|
|
|
|
2010-11-09 03:10:09 +00:00
|
|
|
if (_music.contains(id))
|
|
|
|
_music.erase(id);
|
2010-11-09 00:58:34 +00:00
|
|
|
}
|
2010-11-08 21:09:02 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// ResourcePack
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
ResourcePack::ResourcePack(Common::String filename) {
|
2009-08-04 13:27:04 +00:00
|
|
|
init(filename);
|
|
|
|
}
|
|
|
|
|
2010-11-08 21:09:02 +00:00
|
|
|
//ResourcePack::ResourcePack(ResourcePackId id) {
|
|
|
|
// // We don't use the file number part of resource IDs
|
|
|
|
// //uint32 fileNum = (resourceID >> 16) & 0x7FFF;
|
|
|
|
// char filename[20];
|
|
|
|
// sprintf(filename, "res.%03d", id);
|
|
|
|
// init(filename);
|
|
|
|
//}
|
|
|
|
|
2009-08-04 13:27:04 +00:00
|
|
|
ResourcePack::~ResourcePack() {
|
2009-09-14 10:48:56 +00:00
|
|
|
for (uint32 i = 0; i < _resources.size(); i++)
|
2009-09-14 10:31:54 +00:00
|
|
|
delete [] _resources[i].data;
|
2009-08-04 13:27:04 +00:00
|
|
|
|
|
|
|
_resources.clear();
|
|
|
|
_packFile.close();
|
|
|
|
}
|
|
|
|
|
2010-11-08 21:09:02 +00:00
|
|
|
void ResourcePack::init(Common::String filename) {
|
|
|
|
_packFile.open(filename);
|
2009-08-04 13:27:04 +00:00
|
|
|
|
|
|
|
uint32 entryCount = _packFile.readUint32LE();
|
|
|
|
_resources.resize(entryCount);
|
|
|
|
|
|
|
|
uint32 prevOffset = _packFile.readUint32LE();
|
|
|
|
uint32 nextOffset = 0;
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < entryCount; i++) {
|
|
|
|
ResourceEntry entry;
|
|
|
|
entry.offset = prevOffset;
|
|
|
|
|
|
|
|
// Read the offset of the next entry to determine the size of this one
|
|
|
|
nextOffset = (i < entryCount - 1) ? _packFile.readUint32LE() : _packFile.size();
|
|
|
|
entry.size = (nextOffset > 0) ? nextOffset - prevOffset : _packFile.size() - prevOffset;
|
|
|
|
entry.data = 0;
|
|
|
|
|
|
|
|
_resources[i] = entry;
|
|
|
|
|
|
|
|
prevOffset = nextOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 21:09:02 +00:00
|
|
|
ResourceEntry *ResourcePack::get(uint16 index) {
|
2009-08-04 13:27:04 +00:00
|
|
|
if (!_resources[index].data) {
|
|
|
|
// Load the requested resource if it's not loaded already
|
|
|
|
_packFile.seek(_resources[index].offset, SEEK_SET);
|
|
|
|
_resources[index].data = new byte[_resources[index].size];
|
|
|
|
_packFile.read(_resources[index].data, _resources[index].size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &_resources[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace Asylum
|