2006-04-02 14:20:45 +00:00
|
|
|
/* Residual - Virtual machine to run LucasArts' 3D adventure games
|
|
|
|
* Copyright (C) 2003-2006 The ScummVM-Residual Team (www.scummvm.org)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2003-08-24 17:56:03 +00:00
|
|
|
#include "stdafx.h"
|
2003-08-15 19:41:26 +00:00
|
|
|
#include "bits.h"
|
2005-01-01 12:27:57 +00:00
|
|
|
#include "lab.h"
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
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();
|
2004-12-09 23:55:43 +00:00
|
|
|
_f = std::fopen(filename, "rb");
|
2004-02-24 21:09:53 +00:00
|
|
|
if (!isOpen())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char header[16];
|
2004-12-09 23:55:43 +00:00
|
|
|
if (std::fread(header, 1, sizeof(header), _f) < 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];
|
2004-12-09 23:55:43 +00:00
|
|
|
std::fseek(_f, 16 * (num_entries + 1), SEEK_SET);
|
|
|
|
std::fread(string_table, 1, string_table_size, _f);
|
2004-02-24 21:09:53 +00:00
|
|
|
|
2004-12-09 23:55:43 +00:00
|
|
|
std::fseek(_f, 16, SEEK_SET);
|
2004-02-24 21:09:53 +00:00
|
|
|
char binary_entry[16];
|
|
|
|
for (int i = 0; i < num_entries; i++) {
|
2004-12-09 23:55:43 +00:00
|
|
|
std::fread(binary_entry, 1, 16, _f);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
delete [] string_table;
|
|
|
|
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;
|
|
|
|
|
2004-12-09 23:55:43 +00:00
|
|
|
std::fseek(_f, i->second.offset, SEEK_SET);
|
2004-02-24 21:09:53 +00:00
|
|
|
|
|
|
|
// The sound decoder reads up to two bytes past the end of data
|
|
|
|
// (but shouldn't actually use those bytes). So allocate two extra bytes
|
|
|
|
// to be safe against crashes.
|
|
|
|
char *data = new char[i->second.len + 2];
|
2004-12-09 23:55:43 +00:00
|
|
|
std::fread(data, 1, i->second.len, _f);
|
2004-02-24 21:09:53 +00:00
|
|
|
data[i->second.len] = '\0'; // For valgrind cleanness
|
|
|
|
data[i->second.len + 1] = '\0';
|
|
|
|
return new Block(data, i->second.len);
|
2004-10-31 15:34:14 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2003-12-13 11:03:42 +00:00
|
|
|
std::FILE *Lab::openNewStream(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;
|
2003-12-13 11:03:42 +00:00
|
|
|
|
2005-01-01 09:57:33 +00:00
|
|
|
FILE *file = std::fopen(_labFileName.c_str(), "rb");
|
|
|
|
assert(file);
|
|
|
|
std::fseek(file, 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() {
|
2004-12-09 23:55:43 +00:00
|
|
|
if (_f != NULL)
|
|
|
|
std::fclose(_f);
|
|
|
|
_f = NULL;
|
2004-12-10 07:26:03 +00:00
|
|
|
_fileMap.clear();
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|