scummvm/engine/resource.cpp

312 lines
7.8 KiB
C++
Raw Normal View History

2006-04-02 14:20:45 +00:00
/* Residual - Virtual machine to run LucasArts' 3D adventure games
*
* 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 "common/sys.h"
#include "common/debug.h"
2008-08-01 18:27:11 +00:00
#include "common/fs.h"
#include "engine/resource.h"
#include "engine/registry.h"
#include "engine/bitmap.h"
#include "engine/colormap.h"
#include "engine/costume.h"
#include "engine/font.h"
#include "engine/keyframe.h"
#include "engine/material.h"
#include "engine/model.h"
#include "engine/engine.h"
#include "engine/lipsynch.h"
2005-01-01 12:27:57 +00:00
2003-08-15 18:00:22 +00:00
#include <algorithm>
static void makeLower(std::string& s) {
std::transform(s.begin(), s.end(), s.begin(), tolower);
2003-08-15 18:00:22 +00:00
}
ResourceLoader *g_resourceloader = NULL;
2003-08-15 18:00:22 +00:00
ResourceLoader::ResourceLoader() {
int lab_counter = 0;
Lab *l;
Common::ArchiveMemberList files;
2008-08-01 18:27:11 +00:00
SearchMan.listMatchingMembers(files, "*.lab");
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(filename.c_str());
2008-08-01 18:27:11 +00:00
if (l->isOpen()) {
if (filename == "005.lab")
_labs.push_front(l);
else {
if (filename == "gfdemo01.lab")
g_flags |= GF_DEMO;
_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();
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(filename.c_str());
2008-08-01 18:27:11 +00:00
if (l->isOpen()) {
_labs.push_back(l);
lab_counter++;
2008-08-01 18:27:11 +00:00
} else {
delete l;
}
}
2003-08-15 18:00:22 +00:00
}
ResourceLoader::~ResourceLoader() {
for (LabList::const_iterator i = _labs.begin(); i != _labs.end(); i++)
delete (*i);
}
2003-08-15 18:00:22 +00:00
const Lab *ResourceLoader::findFile(const char *filename) const {
2004-12-09 23:55:43 +00:00
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
}
bool ResourceLoader::fileExists(const char *filename) const {
return findFile(filename) != NULL;
2003-08-15 18:00:22 +00:00
}
Block *ResourceLoader::getFileBlock(const char *filename) const {
const Lab *l = findFile(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
}
LuaFile *ResourceLoader::openNewStreamLua(const char *filename) const {
const Lab *l = findFile(filename);
if (!l)
return NULL;
else
return l->openNewStreamLua(filename);
}
Common::File *ResourceLoader::openNewStreamFile(const char *filename) const {
const Lab *l = findFile(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 {
const Lab *l = findFile(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
}
Bitmap *ResourceLoader::loadBitmap(const char *filename) {
std::string fname = filename;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<Bitmap *>(i->second);
}
2003-08-15 18:00:22 +00:00
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b) { // Grim sometimes asks for non-existant bitmaps (eg, ha_overhead)
if (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
warning("Could not find bitmap %s", filename);
return NULL;
}
Bitmap *result = new Bitmap(filename, b->data(), b->len());
delete b;
2004-12-09 23:55:43 +00:00
_cache[fname] = result;
return result;
2003-08-15 18:00:22 +00:00
}
CMap *ResourceLoader::loadColormap(const char *filename) {
std::string fname = filename;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<CMap *>(i->second);
}
2003-08-15 18:00:22 +00:00
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find colormap %s", filename);
CMap *result = new CMap(filename, b->data(), b->len());
delete b;
2004-12-09 23:55:43 +00:00
_cache[fname] = result;
return result;
2003-08-15 18:00:22 +00:00
}
Costume *ResourceLoader::loadCostume(const char *filename, Costume *prevCost) {
std::string fname = filename;
makeLower(fname);
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find costume %s", filename);
Costume *result = new Costume(filename, b->data(), b->len(), prevCost);
delete b;
return result;
2003-08-15 18:00:22 +00:00
}
Font *ResourceLoader::loadFont(const char *filename) {
std::string fname = filename;
makeLower(fname);
CacheType::iterator i = _cache.find(fname);
if (i != _cache.end()) {
return dynamic_cast<Font *>(i->second);
}
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find font file %s", filename);
Font *result = new Font(filename, b->data(), b->len());
delete b;
_cache[fname] = result;
return result;
}
2003-08-15 18:00:22 +00:00
KeyframeAnim *ResourceLoader::loadKeyframe(const char *filename) {
std::string fname = filename;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<KeyframeAnim *>(i->second);
}
2003-08-15 18:00:22 +00:00
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find keyframe file %s", filename);
KeyframeAnim *result = new KeyframeAnim(filename, b->data(), b->len());
delete b;
2004-12-09 23:55:43 +00:00
_cache[fname] = result;
return result;
2003-08-15 18:00:22 +00:00
}
LipSynch *ResourceLoader::loadLipSynch(const char *filename) {
std::string fname = filename;
LipSynch *result;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<LipSynch *>(i->second);
}
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b) {
if (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
warning("Could not find lipsynch file %s", filename);
result = NULL;
} else {
result = new LipSynch(filename, b->data(), b->len());
// Some lipsynch files have no data
if (result->isValid()) {
delete b;
_cache[fname] = result;
} else {
delete result;
result = NULL;
}
}
return result;
}
Material *ResourceLoader::loadMaterial(const char *filename, const CMap &c) {
std::string fname = std::string(filename) + "@" + c.filename();
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<Material *>(i->second);
}
2003-08-15 18:00:22 +00:00
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find material %s", filename);
Material *result = new Material(fname.c_str(), b->data(), b->len(), c);
delete b;
2004-12-09 23:55:43 +00:00
_cache[fname] = result;
return result;
2003-08-15 18:00:22 +00:00
}
Model *ResourceLoader::loadModel(const char *filename, const CMap &c) {
std::string fname = filename;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end()) {
return dynamic_cast<Model *>(i->second);
}
2003-08-15 18:00:22 +00:00
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
error("Could not find model %s", filename);
Model *result = new Model(filename, b->data(), b->len(), c);
delete b;
2004-12-09 23:55:43 +00:00
_cache[fname] = result;
return result;
2003-08-15 18:00:22 +00:00
}
bool ResourceLoader::exportResource(const char *filename) {
FILE *myFile = fopen(filename, "w");
Block *b = getFileBlock(filename);
2008-07-30 07:04:32 +00:00
if (!b)
return false;
fwrite(b->data(), b->len(), 1, myFile);
fclose(myFile);
delete b;
return true;
}
2003-08-15 18:00:22 +00:00
void ResourceLoader::uncache(const char *filename) {
std::string fname = filename;
makeLower(fname);
2004-12-10 07:26:03 +00:00
CacheType::iterator i = _cache.find(fname);
2004-12-09 23:55:43 +00:00
if (i != _cache.end())
_cache.erase(i);
2003-08-15 18:00:22 +00:00
}