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-06-12 12:08:15 +00:00
|
|
|
#include "common/endian.h"
|
2009-05-10 07:31:02 +00:00
|
|
|
#include "common/file.h"
|
2008-07-26 09:18:46 +00:00
|
|
|
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/lab.h"
|
|
|
|
#include "engines/grim/lua/lua.h"
|
2005-01-01 12:27:57 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
static int sortCallback(const void *entry1, const void *entry2) {
|
|
|
|
return strcasecmp(((Lab::LabEntry *)entry1)->filename, ((Lab::LabEntry *)entry2)->filename);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
bool Lab::open(const char *filename) {
|
2005-01-01 09:57:33 +00:00
|
|
|
_labFileName = filename;
|
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
close();
|
2008-08-01 18:27:11 +00:00
|
|
|
_f = new Common::File();
|
|
|
|
_f->open(filename);
|
|
|
|
if (!_f->isOpen())
|
2004-02-24 21:09:53 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
char header[16];
|
2009-05-10 07:31:02 +00:00
|
|
|
_f->read(header, sizeof(header));
|
|
|
|
if (READ_BE_UINT32(header) != MKID_BE('LABN')) {
|
2004-02-24 21:09:53 +00:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
_numEntries = READ_LE_UINT32(header + 8);
|
2004-02-24 21:09:53 +00:00
|
|
|
int string_table_size = READ_LE_UINT32(header + 12);
|
|
|
|
|
|
|
|
char *string_table = new char[string_table_size];
|
2009-05-10 07:31:02 +00:00
|
|
|
_f->seek(16 * (_numEntries + 1), SEEK_SET);
|
2008-08-01 18:27:11 +00:00
|
|
|
_f->read(string_table, string_table_size);
|
2004-02-24 21:09:53 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
_entries = new LabEntry[_numEntries];
|
2008-08-01 18:27:11 +00:00
|
|
|
_f->seek(16, SEEK_SET);
|
2004-02-24 21:09:53 +00:00
|
|
|
char binary_entry[16];
|
2009-05-10 07:31:02 +00:00
|
|
|
for (int i = 0; i < _numEntries; i++) {
|
2008-08-01 18:27:11 +00:00
|
|
|
_f->read(binary_entry, 16);
|
2004-02-24 21:09:53 +00:00
|
|
|
int fname_offset = READ_LE_UINT32(binary_entry);
|
|
|
|
int start = READ_LE_UINT32(binary_entry + 4);
|
|
|
|
int size = READ_LE_UINT32(binary_entry + 8);
|
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
Common::String fname = string_table + fname_offset;
|
|
|
|
fname.toLowercase();
|
2004-02-24 21:09:53 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
_entries[i].offset = start;
|
|
|
|
_entries[i].len = size;
|
|
|
|
_entries[i].filename = new char[fname.size() + 1];
|
|
|
|
strcpy(_entries[i].filename, fname.c_str());
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
qsort(_entries, _numEntries, sizeof(LabEntry), sortCallback);
|
|
|
|
|
2009-05-10 09:23:11 +00:00
|
|
|
delete[] string_table;
|
2004-02-24 21:09:53 +00:00
|
|
|
return true;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Lab::fileExists(const char *filename) const {
|
2009-05-10 07:31:02 +00:00
|
|
|
return findFilename(filename) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Lab::isOpen() const {
|
|
|
|
return _f->isOpen();
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block *Lab::getFileBlock(const char *filename) const {
|
2009-05-10 07:31:02 +00:00
|
|
|
LabEntry *i = findFilename(filename);
|
|
|
|
if (!i)
|
2004-02-24 21:09:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
_f->seek(i->offset, SEEK_SET);
|
|
|
|
char *data = new char[i->len];
|
|
|
|
_f->read(data, i->len);
|
|
|
|
return new Block(data, i->len);
|
2004-10-31 15:34:14 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-04-05 14:48:54 +00:00
|
|
|
LuaFile *Lab::openNewStreamLua(const char *filename) const {
|
2009-05-10 07:31:02 +00:00
|
|
|
LabEntry *i = findFilename(filename);
|
|
|
|
if (!i)
|
2009-04-05 14:48:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Common::File *file = new Common::File();
|
2009-05-10 07:31:02 +00:00
|
|
|
file->open(_labFileName);
|
|
|
|
file->seek(i->offset, SEEK_SET);
|
2009-04-05 14:48:54 +00:00
|
|
|
|
|
|
|
LuaFile *filehandle = new LuaFile();
|
2009-05-07 22:15:25 +00:00
|
|
|
filehandle->_in = file;
|
2009-04-05 14:48:54 +00:00
|
|
|
|
|
|
|
return filehandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::File *Lab::openNewStreamFile(const char *filename) const {
|
2008-07-26 09:18:46 +00:00
|
|
|
Common::File *file;
|
2009-05-10 07:31:02 +00:00
|
|
|
LabEntry *i = findFilename(filename);
|
|
|
|
if (!i)
|
2004-02-24 21:09:53 +00:00
|
|
|
return NULL;
|
2003-12-13 11:03:42 +00:00
|
|
|
|
2008-07-26 09:18:46 +00:00
|
|
|
file = new Common::File();
|
|
|
|
file->open(_labFileName.c_str());
|
2009-05-10 07:31:02 +00:00
|
|
|
file->seek(i->offset, SEEK_SET);
|
2003-12-13 11:03:42 +00:00
|
|
|
|
2005-01-01 09:57:33 +00:00
|
|
|
return file;
|
2003-12-13 11:03:42 +00:00
|
|
|
}
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
int Lab::fileLength(const char *filename) const {
|
2009-05-10 07:31:02 +00:00
|
|
|
LabEntry *i = findFilename(filename);
|
|
|
|
if (!i)
|
2004-02-24 21:09:53 +00:00
|
|
|
return -1;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
return i->len;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
Lab::LabEntry *Lab::findFilename(const char *filename) const {
|
|
|
|
LabEntry key;
|
|
|
|
|
|
|
|
Common::String s = filename;
|
|
|
|
s.toLowercase();
|
|
|
|
key.filename = (char *)s.c_str();
|
|
|
|
|
|
|
|
return (Lab::LabEntry *)bsearch(&key, _entries, _numEntries, sizeof(LabEntry), sortCallback);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lab::close() {
|
2009-05-10 07:31:02 +00:00
|
|
|
delete _f;
|
2004-12-09 23:55:43 +00:00
|
|
|
_f = NULL;
|
2009-05-10 18:15:26 +00:00
|
|
|
if (_entries)
|
|
|
|
for (int i = 0; i < _numEntries; i++)
|
|
|
|
delete[] _entries[i].filename;
|
2009-05-10 07:31:02 +00:00
|
|
|
|
2009-05-10 09:28:32 +00:00
|
|
|
delete[] _entries;
|
2009-05-10 18:16:46 +00:00
|
|
|
_entries = NULL;
|
|
|
|
_numEntries = 0;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|