2006-04-02 14:20:45 +00:00
|
|
|
/* Residual - Virtual machine to run LucasArts' 3D adventure games
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2008-01-26 11:47:23 +00:00
|
|
|
#include "common/sys.h"
|
|
|
|
#include "common/debug.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 <cstring>
|
|
|
|
#include <cctype>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2008-06-12 12:08:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
static void makeLower(std::string& s) {
|
2004-02-24 22:43:32 +00:00
|
|
|
std::transform(s.begin(), s.end(), s.begin(), tolower);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2004-12-31 21:35:04 +00:00
|
|
|
ResourceLoader *g_resourceloader = NULL;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
ResourceLoader::ResourceLoader() {
|
2008-07-31 22:01:31 +00:00
|
|
|
const char *directory = g_registry->get("GrimDataDir", ".");
|
2003-08-24 09:34:19 +00:00
|
|
|
std::string dir_str = (directory != NULL ? directory : ".");
|
|
|
|
dir_str += '/';
|
|
|
|
int lab_counter = 0;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2005-04-08 22:29:06 +00:00
|
|
|
#ifdef _WIN32
|
2003-08-24 17:56:03 +00:00
|
|
|
WIN32_FIND_DATAA find_file_data;
|
2005-01-01 21:07:52 +00:00
|
|
|
std::string dir_strWin32 = dir_str + '*';
|
2003-08-25 19:47:54 +00:00
|
|
|
HANDLE d = FindFirstFile(dir_strWin32.c_str(), &find_file_data);
|
2003-08-24 09:34:19 +00:00
|
|
|
#else
|
|
|
|
DIR *d = opendir(dir_str.c_str());
|
|
|
|
#endif
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!directory)
|
2003-08-24 09:34:19 +00:00
|
|
|
error("Cannot find DataDir registry entry - check configuration file");
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!d)
|
2003-08-24 09:34:19 +00:00
|
|
|
error("Cannot open DataDir (%s)- check configuration file", dir_str.c_str());
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2005-04-08 22:29:06 +00:00
|
|
|
#ifdef _WIN32
|
2003-08-24 09:34:19 +00:00
|
|
|
do {
|
|
|
|
int namelen = strlen(find_file_data.cFileName);
|
2005-08-27 16:08:44 +00:00
|
|
|
if (namelen > 4 && ((stricmp(find_file_data.cFileName + namelen - 4, ".lab") == 0) || (stricmp(find_file_data.cFileName + namelen - 4, ".mus") == 0))) {
|
2003-08-24 09:34:19 +00:00
|
|
|
std::string fullname = dir_str + find_file_data.cFileName;
|
|
|
|
Lab *l = new Lab(fullname.c_str());
|
|
|
|
lab_counter++;
|
2005-08-27 16:08:44 +00:00
|
|
|
if (l->isOpen()) {
|
2004-10-06 18:59:23 +00:00
|
|
|
if (strstr(find_file_data.cFileName, "005"))
|
2004-12-09 23:55:43 +00:00
|
|
|
_labs.push_front(l);
|
2005-08-27 16:08:44 +00:00
|
|
|
else {
|
|
|
|
if (strstr(find_file_data.cFileName, "gfdemo01"))
|
|
|
|
g_flags |= GF_DEMO;
|
2004-12-09 23:55:43 +00:00
|
|
|
_labs.push_back(l);
|
2005-08-27 16:08:44 +00:00
|
|
|
}
|
|
|
|
} else
|
2003-08-24 09:34:19 +00:00
|
|
|
delete l;
|
|
|
|
}
|
2005-04-08 22:29:06 +00:00
|
|
|
} while (FindNextFile(d, &find_file_data));
|
2003-08-24 09:34:19 +00:00
|
|
|
FindClose(d);
|
|
|
|
#else
|
|
|
|
dirent *de;
|
2008-07-30 07:04:32 +00:00
|
|
|
while ((de = readdir(d))) {
|
2003-08-24 09:34:19 +00:00
|
|
|
int namelen = strlen(de->d_name);
|
2005-08-27 16:08:44 +00:00
|
|
|
if (namelen > 4 && ((strcasecmp(de->d_name + namelen - 4, ".lab") == 0) || (strcasecmp(de->d_name + namelen - 4, ".mus") == 0))) {
|
2003-08-24 09:34:19 +00:00
|
|
|
std::string fullname = dir_str + de->d_name;
|
|
|
|
Lab *l = new Lab(fullname.c_str());
|
|
|
|
lab_counter++;
|
|
|
|
if (l->isOpen())
|
2004-09-11 14:09:43 +00:00
|
|
|
// Handle the Grim 1.1 patch's datafile
|
|
|
|
if (strstr(de->d_name, "005"))
|
2004-12-11 13:45:28 +00:00
|
|
|
_labs.push_front(l);
|
2005-08-27 16:08:44 +00:00
|
|
|
else {
|
2005-08-28 11:12:30 +00:00
|
|
|
if (strstr(de->d_name, "gfdemo01"))
|
2005-08-27 16:08:44 +00:00
|
|
|
g_flags |= GF_DEMO;
|
2004-12-11 13:45:28 +00:00
|
|
|
_labs.push_back(l);
|
2005-08-27 16:08:44 +00:00
|
|
|
}
|
2003-08-24 09:34:19 +00:00
|
|
|
else
|
|
|
|
delete l;
|
|
|
|
}
|
|
|
|
}
|
2004-09-11 14:09:43 +00:00
|
|
|
|
2003-08-24 09:34:19 +00:00
|
|
|
closedir(d);
|
|
|
|
#endif
|
2003-08-15 18:53:08 +00:00
|
|
|
|
2003-08-24 09:34:19 +00:00
|
|
|
if (lab_counter == 0)
|
2004-12-09 23:55:43 +00:00
|
|
|
error("Cannot find any resource files in %s - check configuration file", dir_str.c_str());
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2007-02-05 13:49:32 +00:00
|
|
|
ResourceLoader::~ResourceLoader() {
|
|
|
|
for (LabList::const_iterator i = _labs.begin(); i != _labs.end(); i++)
|
2006-02-26 18:38:35 +00:00
|
|
|
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++)
|
2004-02-24 22:43:32 +00:00
|
|
|
if ((*i)->fileExists(filename))
|
|
|
|
return *i;
|
2007-02-05 13:49:32 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
return NULL;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceLoader::fileExists(const char *filename) const {
|
2004-02-24 22:43:32 +00:00
|
|
|
return findFile(filename) != NULL;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block *ResourceLoader::getFileBlock(const char *filename) const {
|
2004-02-24 22:43:32 +00:00
|
|
|
const Lab *l = findFile(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!l)
|
2004-02-24 22:43:32 +00:00
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return l->getFileBlock(filename);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:18:46 +00:00
|
|
|
Common::File *ResourceLoader::openNewStream(const char *filename) const {
|
2004-02-24 22:43:32 +00:00
|
|
|
const Lab *l = findFile(filename);
|
2003-12-13 11:03:42 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!l)
|
2004-02-24 22:43:32 +00:00
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return l->openNewStream(filename);
|
2003-12-13 11:03:42 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
int ResourceLoader::fileLength(const char *filename) const {
|
2004-02-24 22:43:32 +00:00
|
|
|
const Lab *l = findFile(filename);
|
2008-07-26 09:18:46 +00:00
|
|
|
if (l)
|
2004-02-24 22:43:32 +00:00
|
|
|
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) {
|
2004-02-24 22:43:32 +00:00
|
|
|
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()) {
|
2004-02-24 22:43:32 +00:00
|
|
|
return dynamic_cast<Bitmap *>(i->second);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +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)
|
2005-07-10 18:57:27 +00:00
|
|
|
if (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
|
|
|
|
warning("Could not find bitmap %s\n", filename);
|
2005-01-03 20:49:49 +00:00
|
|
|
return NULL;
|
2004-09-11 14:09:43 +00:00
|
|
|
}
|
2003-08-16 11:10:40 +00:00
|
|
|
|
2004-09-11 14:09:43 +00:00
|
|
|
Bitmap *result = new Bitmap(filename, b->data(), b->len());
|
2004-02-24 22:43:32 +00:00
|
|
|
delete b;
|
2004-12-09 23:55:43 +00:00
|
|
|
_cache[fname] = result;
|
2004-02-24 22:43:32 +00:00
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2003-10-05 17:45:46 +00:00
|
|
|
CMap *ResourceLoader::loadColormap(const char *filename) {
|
2004-02-24 22:43:32 +00:00
|
|
|
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()) {
|
2004-02-24 22:43:32 +00:00
|
|
|
return dynamic_cast<CMap *>(i->second);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b)
|
2004-02-24 22:43:32 +00:00
|
|
|
error("Could not find colormap %s\n", filename);
|
|
|
|
CMap *result = new CMap(filename, b->data(), b->len());
|
|
|
|
delete b;
|
2004-12-09 23:55:43 +00:00
|
|
|
_cache[fname] = result;
|
2004-02-24 22:43:32 +00:00
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2003-08-20 14:29:59 +00:00
|
|
|
Costume *ResourceLoader::loadCostume(const char *filename, Costume *prevCost) {
|
2004-02-24 22:43:32 +00:00
|
|
|
std::string fname = filename;
|
|
|
|
makeLower(fname);
|
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b)
|
2004-02-24 22:43:32 +00:00
|
|
|
error("Could not find costume %s\n", filename);
|
|
|
|
Costume *result = new Costume(filename, b->data(), b->len(), prevCost);
|
|
|
|
delete b;
|
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 19:54:40 +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)
|
2005-03-18 19:54:40 +00:00
|
|
|
error("Could not find font file %s\n", 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) {
|
2004-02-24 22:43:32 +00:00
|
|
|
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()) {
|
2004-02-24 22:43:32 +00:00
|
|
|
return dynamic_cast<KeyframeAnim *>(i->second);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b)
|
2004-02-24 22:43:32 +00:00
|
|
|
error("Could not find keyframe file %s\n", filename);
|
|
|
|
KeyframeAnim *result = new KeyframeAnim(filename, b->data(), b->len());
|
|
|
|
delete b;
|
2004-12-09 23:55:43 +00:00
|
|
|
_cache[fname] = result;
|
2004-02-24 22:43:32 +00:00
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2004-09-11 14:09:43 +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()) {
|
2004-09-11 14:09:43 +00:00
|
|
|
return dynamic_cast<LipSynch *>(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b) {
|
2005-07-10 18:57:27 +00:00
|
|
|
if (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
|
|
|
|
warning("Could not find lipsynch file %s\n", filename);
|
2004-09-11 14:09:43 +00:00
|
|
|
result = NULL;
|
|
|
|
} else {
|
|
|
|
result = new LipSynch(filename, b->data(), b->len());
|
2008-07-18 22:44:11 +00:00
|
|
|
|
|
|
|
// Some lipsynch files have no data
|
|
|
|
if (result->isValid()) {
|
|
|
|
delete b;
|
|
|
|
_cache[fname] = result;
|
|
|
|
} else {
|
|
|
|
delete result;
|
|
|
|
result = NULL;
|
|
|
|
}
|
2004-09-11 14:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
Material *ResourceLoader::loadMaterial(const char *filename, const CMap &c) {
|
2006-02-25 23:00:09 +00:00
|
|
|
std::string fname = std::string(filename) + "@" + c.filename();
|
2004-02-24 22:43:32 +00:00
|
|
|
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()) {
|
2006-02-25 23:00:09 +00:00
|
|
|
return dynamic_cast<Material *>(i->second);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b)
|
2004-02-24 22:43:32 +00:00
|
|
|
error("Could not find material %s\n", filename);
|
2006-02-25 23:00:09 +00:00
|
|
|
Material *result = new Material(fname.c_str(), b->data(), b->len(), c);
|
2004-02-24 22:43:32 +00:00
|
|
|
delete b;
|
2004-12-09 23:55:43 +00:00
|
|
|
_cache[fname] = result;
|
2004-02-24 22:43:32 +00:00
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2003-10-05 17:45:46 +00:00
|
|
|
Model *ResourceLoader::loadModel(const char *filename, const CMap &c) {
|
2004-02-24 22:43:32 +00:00
|
|
|
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()) {
|
2004-02-24 22:43:32 +00:00
|
|
|
return dynamic_cast<Model *>(i->second);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
Block *b = getFileBlock(filename);
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!b)
|
2004-02-24 22:43:32 +00:00
|
|
|
error("Could not find model %s\n", filename);
|
|
|
|
Model *result = new Model(filename, b->data(), b->len(), c);
|
|
|
|
delete b;
|
2004-12-09 23:55:43 +00:00
|
|
|
_cache[fname] = result;
|
2004-02-24 22:43:32 +00:00
|
|
|
return result;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2005-07-17 23:40: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)
|
2005-07-17 23:40:22 +00:00
|
|
|
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) {
|
2004-02-24 22:43:32 +00:00
|
|
|
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
|
|
|
}
|