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"
|
2008-07-26 09:18:46 +00:00
|
|
|
|
2008-01-26 11:47:23 +00:00
|
|
|
#include "engine/lab.h"
|
2009-05-09 17:44:05 +00:00
|
|
|
#include "engine/lua/lua.h"
|
2005-01-01 12:27:57 +00:00
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
#include <algorithm>
|
2009-05-09 17:44:05 +00:00
|
|
|
#include <cstring>
|
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];
|
2008-08-01 18:27:11 +00:00
|
|
|
if (_f->read(header, sizeof(header)) < sizeof(header)) {
|
2004-02-24 21:09:53 +00:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (std::memcmp(header, "LABN", 4) != 0) {
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_entries = READ_LE_UINT32(header + 8);
|
|
|
|
int string_table_size = READ_LE_UINT32(header + 12);
|
|
|
|
|
|
|
|
char *string_table = new char[string_table_size];
|
2008-08-01 18:27:11 +00:00
|
|
|
_f->seek(16 * (num_entries + 1), SEEK_SET);
|
|
|
|
_f->read(string_table, string_table_size);
|
2004-02-24 21:09:53 +00:00
|
|
|
|
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];
|
|
|
|
for (int i = 0; i < num_entries; 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);
|
|
|
|
|
|
|
|
std::string fname = string_table + fname_offset;
|
|
|
|
std::transform(fname.begin(), fname.end(), fname.begin(), tolower);
|
|
|
|
|
2004-12-10 07:26:03 +00:00
|
|
|
_fileMap.insert(std::make_pair(fname, LabEntry(start, size)));
|
|
|
|
_fileMap.size();
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2008-08-11 19:16:21 +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 {
|
2004-12-10 07:26:03 +00:00
|
|
|
return findFilename(filename) != _fileMap.end();
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block *Lab::getFileBlock(const char *filename) const {
|
2004-12-10 07:26:03 +00:00
|
|
|
FileMapType::const_iterator i = findFilename(filename);
|
|
|
|
if (i == _fileMap.end())
|
2004-02-24 21:09:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
2008-08-01 18:27:11 +00:00
|
|
|
_f->seek(i->second.offset, SEEK_SET);
|
2008-08-12 19:02:21 +00:00
|
|
|
char *data = new char[i->second.len];
|
|
|
|
_f->read(data, i->second.len);
|
2004-02-24 21:09:53 +00:00
|
|
|
return new Block(data, i->second.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 {
|
|
|
|
FileMapType::const_iterator i = findFilename(filename);
|
|
|
|
if (i == _fileMap.end())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Common::File *file = new Common::File();
|
|
|
|
file->open(_labFileName.c_str());
|
|
|
|
file->seek(i->second.offset, SEEK_SET);
|
|
|
|
|
|
|
|
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;
|
2004-12-10 07:26:03 +00:00
|
|
|
FileMapType::const_iterator i = findFilename(filename);
|
|
|
|
if (i == _fileMap.end())
|
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());
|
|
|
|
file->seek(i->second.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 {
|
2004-12-10 07:26:03 +00:00
|
|
|
FileMapType::const_iterator i = findFilename(filename);
|
|
|
|
if (i == _fileMap.end())
|
2004-02-24 21:09:53 +00:00
|
|
|
return -1;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
return i->second.len;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 07:26:03 +00:00
|
|
|
Lab::FileMapType::const_iterator Lab::findFilename(const char *filename) const {
|
2004-02-24 21:09:53 +00:00
|
|
|
std::string s = filename;
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), tolower);
|
2004-12-10 07:26:03 +00:00
|
|
|
return _fileMap.find(s);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lab::close() {
|
2008-07-30 07:04:32 +00:00
|
|
|
if (_f)
|
2008-08-01 18:27:11 +00:00
|
|
|
delete _f;
|
2004-12-09 23:55:43 +00:00
|
|
|
_f = NULL;
|
2004-12-10 07:26:03 +00:00
|
|
|
_fileMap.clear();
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|