scummvm/engines/grim/resource.cpp

463 lines
11 KiB
C++
Raw Normal View History

2009-05-26 14:13:08 +00:00
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
2003-08-15 18:00:22 +00:00
#include "engines/grim/resource.h"
#include "engines/grim/colormap.h"
#include "engines/grim/costume.h"
#include "engines/grim/keyframe.h"
#include "engines/grim/material.h"
#include "engines/grim/grim.h"
#include "engines/grim/lipsync.h"
#include "engines/grim/savegame.h"
#include "engines/grim/actor.h"
2005-01-01 12:27:57 +00:00
2009-05-25 06:49:57 +00:00
namespace Grim {
ResourceLoader *g_resourceloader = NULL;
2003-08-15 18:00:22 +00:00
ResourceLoader::ResourceLoader() {
int lab_counter = 0;
2009-05-12 14:31:48 +00:00
_cacheDirty = false;
2009-06-27 06:08:44 +00:00
_cacheMemorySize = 0;
2009-05-12 14:31:48 +00:00
Lab *l;
Common::ArchiveMemberList files;
2008-08-01 18:27:11 +00:00
SearchMan.listMatchingMembers(files, "*.lab");
2011-03-25 06:15:54 +08:00
SearchMan.listMatchingMembers(files, "*.m4b");
2008-08-01 18:27:11 +00:00
if (files.empty())
error("Cannot find game data - check configuration file");
2008-08-01 18:27:11 +00:00
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
const Common::String filename = (*x)->getName();
l = new Lab();
if (l->open(filename)) {
if (filename.equalsIgnoreCase("data005.lab"))
2008-08-01 18:27:11 +00:00
_labs.push_front(l);
else
2008-08-01 18:27:11 +00:00
_labs.push_back(l);
lab_counter++;
2008-08-01 18:27:11 +00:00
} else {
delete l;
}
2008-08-01 18:27:11 +00:00
}
files.clear();
if (g_grim->getGameFlags() & GF_DEMO) {
SearchMan.listMatchingMembers(files, "*.mus");
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
const Common::String filename = (*x)->getName();
l = new Lab();
if (l->open(filename)) {
_labs.push_back(l);
lab_counter++;
} else {
delete l;
}
}
}
2003-08-15 18:00:22 +00:00
}
ResourceLoader::~ResourceLoader() {
for (LabList::const_iterator i = _labs.begin(); i != _labs.end(); ++i)
2009-06-26 16:13:11 +00:00
delete *i;
}
2009-05-12 14:31:48 +00:00
const Lab *ResourceLoader::getLab(const char *filename) const {
for (LabList::const_iterator i = _labs.begin(); i != _labs.end(); ++i)
if ((*i)->fileExists(filename))
return *i;
return NULL;
2003-08-15 18:00:22 +00:00
}
2009-05-12 14:31:48 +00:00
static int sortCallback(const void *entry1, const void *entry2) {
return strcasecmp(((ResourceLoader::ResourceCache *)entry1)->fname, ((ResourceLoader::ResourceCache *)entry2)->fname);
}
2009-06-27 06:47:18 +00:00
Block *ResourceLoader::getFileFromCache(const char *filename) {
2009-05-12 14:31:48 +00:00
ResourceLoader::ResourceCache *entry = getEntryFromCache(filename);
if (entry)
return entry->resPtr;
else
return NULL;
}
ResourceLoader::ResourceCache *ResourceLoader::getEntryFromCache(const char *filename) {
if (_cache.empty())
return NULL;
2009-05-12 14:31:48 +00:00
if (_cacheDirty) {
qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
_cacheDirty = false;
}
ResourceCache key;
2011-03-25 07:29:35 +08:00
key.fname = const_cast<char *>(filename);
2009-05-12 14:31:48 +00:00
return (ResourceLoader::ResourceCache *)bsearch(&key, _cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
}
2003-08-15 18:00:22 +00:00
bool ResourceLoader::fileExists(const char *filename) const {
2009-05-12 14:31:48 +00:00
return getLab(filename) != NULL;
2003-08-15 18:00:22 +00:00
}
Block *ResourceLoader::getFileBlock(const char *filename) const {
2009-05-12 14:31:48 +00:00
const Lab *l = getLab(filename);
2008-07-30 07:04:32 +00:00
if (!l)
return NULL;
else
return l->getFileBlock(filename);
2003-08-15 18:00:22 +00:00
}
Block *ResourceLoader::getBlock(const char *filename) {
Common::String fname = filename;
fname.toLowercase();
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (b) {
putIntoCache(fname, b);
}
}
return b;
}
2009-06-26 16:13:11 +00:00
LuaFile *ResourceLoader::openNewStreamLuaFile(const char *filename) const {
2009-05-12 14:31:48 +00:00
const Lab *l = getLab(filename);
if (!l)
return NULL;
else
return l->openNewStreamLua(filename);
}
Common::File *ResourceLoader::openNewStreamFile(const char *filename) const {
2009-05-12 14:31:48 +00:00
const Lab *l = getLab(filename);
2008-07-30 07:04:32 +00:00
if (!l)
return NULL;
else
return l->openNewStreamFile(filename);
}
2003-08-15 18:00:22 +00:00
int ResourceLoader::fileLength(const char *filename) const {
2009-05-12 14:31:48 +00:00
const Lab *l = getLab(filename);
2008-07-26 09:18:46 +00:00
if (l)
return l->fileLength(filename);
2008-07-26 09:18:46 +00:00
else
return 0;
2003-08-15 18:00:22 +00:00
}
2009-06-27 06:47:18 +00:00
void ResourceLoader::putIntoCache(Common::String fname, Block *res) {
2009-05-12 18:45:14 +00:00
ResourceCache entry;
entry.resPtr = res;
entry.fname = new char[fname.size() + 1];
strcpy(entry.fname, fname.c_str());
2009-06-27 06:47:18 +00:00
_cacheMemorySize += res->len();
2009-05-12 18:45:14 +00:00
_cache.push_back(entry);
_cacheDirty = true;
}
2003-08-15 18:00:22 +00:00
Bitmap *ResourceLoader::loadBitmap(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b) { // Grim sometimes asks for non-existant bitmaps (eg, ha_overhead)
warning("Could not find bitmap %s", filename);
return NULL;
}
putIntoCache(fname, b);
}
2009-06-26 16:13:11 +00:00
Bitmap *result = g_grim->registerBitmap(filename, b->data(), b->len());
_bitmaps.push_back(result);
2009-05-12 14:31:48 +00:00
return result;
2003-08-15 18:00:22 +00:00
}
CMap *ResourceLoader::loadColormap(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b) {
2009-06-27 06:47:18 +00:00
error("Could not find colormap %s", filename);
}
2009-06-27 06:47:18 +00:00
putIntoCache(fname, b);
}
2003-08-15 18:00:22 +00:00
CMap *result = new CMap(filename, b->data(), b->len());
_colormaps.push_back(result);
2009-05-12 14:31:48 +00:00
return result;
2003-08-15 18:00:22 +00:00
}
Costume *ResourceLoader::loadCostume(const char *filename, Costume *prevCost) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b)
error("Could not find costume \"%s\"", filename);
2009-06-27 06:47:18 +00:00
putIntoCache(fname, b);
}
Costume *result = new Costume(filename, b->data(), b->len(), prevCost);
2009-05-12 14:31:48 +00:00
return result;
2003-08-15 18:00:22 +00:00
}
Font *ResourceLoader::loadFont(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b)
error("Could not find font file %s", filename);
putIntoCache(fname, b);
}
Font *result = new Font(filename, b->data(), b->len());
_fonts.push_back(result);
2009-05-12 14:31:48 +00:00
return result;
}
2003-08-15 18:00:22 +00:00
KeyframeAnim *ResourceLoader::loadKeyframe(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b)
error("Could not find keyframe file %s", filename);
putIntoCache(fname, b);
}
2003-08-15 18:00:22 +00:00
KeyframeAnim *result = new KeyframeAnim(filename, b->data(), b->len());
_keyframeAnims.push_back(result);
2009-05-12 14:31:48 +00:00
return result;
2003-08-15 18:00:22 +00:00
}
2009-04-21 18:04:24 +00:00
LipSync *ResourceLoader::loadLipSync(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-04-21 18:04:24 +00:00
LipSync *result;
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
2008-07-30 07:04:32 +00:00
if (!b) {
2009-06-27 06:47:18 +00:00
b = getFileBlock(fname.c_str());
if (!b)
return NULL;
}
2009-05-09 17:47:28 +00:00
2009-06-27 06:47:18 +00:00
result = new LipSync(filename, b->data(), b->len());
// Some lipsync files have no data
if (result->isValid()) {
putIntoCache(fname, b);
_lipsyncs.push_back(result);
2009-06-27 06:47:18 +00:00
} else {
delete result;
2009-06-27 06:08:44 +00:00
delete b;
2009-06-27 06:47:18 +00:00
result = NULL;
2009-05-09 17:47:28 +00:00
}
return result;
}
Material *ResourceLoader::loadMaterial(const char *filename, CMap *c) {
2009-06-27 06:47:18 +00:00
Common::String fname = Common::String(filename);
2009-05-10 16:43:41 +00:00
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b)
error("Could not find material %s", filename);
putIntoCache(fname, b);
}
2003-08-15 18:00:22 +00:00
Material *result = new Material(fname.c_str(), b->data(), b->len(), c);
_materials.push_back(result);
2009-05-12 14:31:48 +00:00
return result;
2003-08-15 18:00:22 +00:00
}
Model *ResourceLoader::loadModel(const char *filename, CMap *c) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-06-27 06:47:18 +00:00
Block *b = getFileFromCache(fname.c_str());
if (!b) {
b = getFileBlock(fname.c_str());
if (!b)
error("Could not find model %s", filename);
putIntoCache(fname, b);
}
2003-08-15 18:00:22 +00:00
Model *result = new Model(filename, b->data(), b->len(), c);
_models.push_back(result);
2003-08-15 18:00:22 +00:00
2009-05-12 14:31:48 +00:00
return result;
}
2003-08-15 18:00:22 +00:00
void ResourceLoader::uncache(const char *filename) {
2009-05-10 16:43:41 +00:00
Common::String fname = filename;
fname.toLowercase();
2009-05-12 14:31:48 +00:00
2009-05-12 18:47:34 +00:00
if (_cacheDirty) {
2009-05-12 14:31:48 +00:00
qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
2009-05-12 18:47:34 +00:00
_cacheDirty = false;
}
2009-05-12 14:31:48 +00:00
for (unsigned int i = 0; i < _cache.size(); i++) {
if (fname.compareTo(_cache[i].fname) == 0) {
delete[] _cache[i].fname;
2009-06-27 06:47:18 +00:00
_cacheMemorySize -= _cache[i].resPtr->len();
delete _cache[i].resPtr;
2009-05-12 14:31:48 +00:00
_cache.remove_at(i);
_cacheDirty = true;
}
}
2003-08-15 18:00:22 +00:00
}
2009-05-25 06:49:57 +00:00
void ResourceLoader::uncacheMaterial(Material *mat) {
_materials.remove(mat);
}
void ResourceLoader::uncacheBitmap(Bitmap *bitmap) {
_bitmaps.remove(bitmap);
}
void ResourceLoader::uncacheModel(Model *m) {
_models.remove(m);
}
void ResourceLoader::uncacheColormap(CMap *c) {
_colormaps.remove(c);
}
void ResourceLoader::uncacheKeyframe(KeyframeAnim *k) {
_keyframeAnims.remove(k);
}
void ResourceLoader::uncacheFont(Font *f) {
_fonts.remove(f);
}
void ResourceLoader::uncacheLipSync(LipSync *s) {
_lipsyncs.remove(s);
}
MaterialPtr ResourceLoader::getMaterial(const char *fname, CMap *c) {
for (Common::List<Material *>::const_iterator i = _materials.begin(); i != _materials.end(); ++i) {
Material *m = *i;
if (strcmp(fname, m->_fname.c_str()) == 0 && *m->_cmap == *c) {
return m;
}
}
return loadMaterial(fname, c);
}
BitmapPtr ResourceLoader::getBitmap(const char *fname) {
for (Common::List<Bitmap *>::const_iterator i = _bitmaps.begin(); i != _bitmaps.end(); ++i) {
Bitmap *b = *i;
if (strcmp(fname, b->filename()) == 0) {
return b;
}
}
return loadBitmap(fname);
}
ModelPtr ResourceLoader::getModel(const char *fname, CMap *c) {
for (Common::List<Model *>::const_iterator i = _models.begin(); i != _models.end(); ++i) {
Model *m = *i;
if (strcmp(fname, m->_fname.c_str()) == 0 && *m->_cmap == *c) {
return m;
}
}
return loadModel(fname, c);
}
CMapPtr ResourceLoader::getColormap(const char *fname) {
for (Common::List<CMap *>::const_iterator i = _colormaps.begin(); i != _colormaps.end(); ++i) {
CMap *c = *i;
if (strcmp(fname, c->_fname.c_str()) == 0) {
return c;
}
}
return loadColormap(fname);
}
KeyframeAnimPtr ResourceLoader::getKeyframe(const char *fname) {
for (Common::List<KeyframeAnim *>::const_iterator i = _keyframeAnims.begin(); i != _keyframeAnims.end(); ++i) {
KeyframeAnim *k = *i;
if (strcmp(fname, k->filename()) == 0) {
return k;
}
}
return loadKeyframe(fname);
}
FontPtr ResourceLoader::getFont(const char *fname) {
for (Common::List<Font *>::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
Font *f = *i;
if (strcmp(fname, f->getFilename().c_str()) == 0) {
return f;
}
}
return loadFont(fname);
}
LipSyncPtr ResourceLoader::getLipSync(const char *fname) {
for (Common::List<LipSync *>::const_iterator i = _lipsyncs.begin(); i != _lipsyncs.end(); ++i) {
LipSync *l = *i;
2011-03-25 07:29:35 +08:00
if (strcmp(fname, l->getFilename()) == 0) {
return l;
}
}
return loadLipSync(fname);
}
2009-05-25 06:49:57 +00:00
} // end of namespace Grim