aboutsummaryrefslogtreecommitdiff
path: root/engines/access/files.cpp
blob: 48276ee477b0fce44d12b2a280bc2d17bc9738c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/substream.h"
#include "access/files.h"
#include "access/amazon/amazon_resources.h"
#include "access/martian/martian_resources.h"
#include "access/access.h"

namespace Access {

FileIdent::FileIdent() {
	_fileNum = -1;
	_subfile = 0;
}

void FileIdent::load(Common::SeekableReadStream &s) {
	_fileNum = s.readSint16LE();
	_subfile = s.readUint16LE();
}

/*------------------------------------------------------------------------*/

CellIdent::	CellIdent() {
	_cell = 0;
}

CellIdent::CellIdent(int cell, int fileNum, int subfile) {
	_cell = cell;
	_fileNum = fileNum;
	_subfile = subfile;
}

/*------------------------------------------------------------------------*/

Resource::Resource() {
	_stream = nullptr;
	_size = 0;
	_data = nullptr;
}

Resource::~Resource() {
	delete[] _data;
	delete _stream;
}

Resource::Resource(byte *p, int size) {
	_data = p;
	_size = size;
	_stream = new Common::MemoryReadStream(p, size);
}

byte *Resource::data() {
	if (_data == nullptr) {
		_data = new byte[_size];
		int pos = _stream->pos();
		_stream->seek(0);
		_stream->read(_data, _size);
		_stream->seek(pos);
	}

	return _data;
}

/*------------------------------------------------------------------------*/

FileManager::FileManager(AccessEngine *vm) : _vm(vm) {
	_fileNumber = -1;
	_setPaletteFlag = true;
}

FileManager::~FileManager() {
}

Resource *FileManager::loadFile(int fileNum, int subfile) {
	Resource *res = new Resource();
	setAppended(res, fileNum);
	gotoAppended(res, subfile);

	handleFile(res);
	return res;
}

Resource *FileManager::loadFile(const FileIdent &fileIdent) {
	return loadFile(fileIdent._fileNum, fileIdent._subfile);
}

Resource *FileManager::loadFile(const Common::String &filename) {
	Resource *res = new Resource();

	// Open the file
	openFile(res, filename);

	// Set up stream for the entire file
	res->_size = res->_file.size();
	res->_stream = res->_file.readStream(res->_size);

	handleFile(res);
	return res;
}

bool FileManager::existFile(const Common::String &filename) {
	Common::File f;
	return f.exists(filename);
}

void FileManager::openFile(Resource *res, const Common::String &filename) {
	// Open up the file
	_fileNumber = -1;
	if (!res->_file.open(filename))
		error("Could not open file - %s", filename.c_str());
}

void FileManager::loadScreen(Graphics::ManagedSurface *dest, int fileNum, int subfile) {
	Resource *res = loadFile(fileNum, subfile);
	handleScreen(dest, res);
	delete res;
}

void FileManager::handleScreen(Graphics::ManagedSurface *dest, Resource *res) {
	_vm->_screen->loadRawPalette(res->_stream);
	if (_setPaletteFlag)
		_vm->_screen->setPalette();
	_setPaletteFlag = true;

	// The remainder of the file after the palette may be separately compressed,
	// so call handleFile to handle it if it is
	res->_size -= res->_stream->pos();
	handleFile(res);

	Graphics::Surface destSurface = dest->getSubArea(Common::Rect(0, 0,
		_vm->_screen->w, _vm->_screen->h));

	if (destSurface.w == destSurface.pitch) {
		res->_stream->read((byte *)destSurface.getPixels(), destSurface.w * destSurface.h);
	} else {
		for (int y = 0; y < destSurface.h; ++y) {
			byte *pDest = (byte *)destSurface.getBasePtr(0, y);
			res->_stream->read(pDest, destSurface.w);
		}
	}
}

void FileManager::loadScreen(int fileNum, int subfile) {
	loadScreen(_vm->_screen, fileNum, subfile);
}

void FileManager::loadScreen(const Common::String &filename) {
	Resource *res = loadFile(filename);
	handleScreen(_vm->_screen, res);
	delete res;
}

void FileManager::handleFile(Resource *res) {
	char header[3];
	res->_stream->read(&header[0], 3);
	res->_stream->seek(-3, SEEK_CUR);

	bool isCompressed = !strncmp(header, "DBE", 3);

	// If the data is compressed, uncompress it and replace the stream
	// in the resource with the decompressed one
	if (isCompressed) {
		// Read in the entire compressed data
		byte *src = new byte[res->_size];
		res->_stream->read(src, res->_size);

		// Decompress the data
		res->_size = decompressDBE(src, &res->_data);

		// Replace the default resource stream with a stream for the decompressed data
		delete res->_stream;
		res->_file.close();
		res->_stream = new Common::MemoryReadStream(res->_data, res->_size);

		delete[] src;
	}
}

void FileManager::setAppended(Resource *res, int fileNum) {
	// Open the file for access
	if (!res->_file.open(_vm->_res->FILENAMES[fileNum]))
		error("Could not open file %s", _vm->_res->FILENAMES[fileNum].c_str());

	// If a different file has been opened then previously, load its index
	if (_fileNumber != fileNum) {
		_fileNumber = fileNum;

		// Read in the file index
		int count = res->_file.readUint16LE();
		assert(count <= 100);
		_fileIndex.resize(count);
		for (int i = 0; i < count; ++i)
			_fileIndex[i] = res->_file.readUint32LE();
	}
}

void FileManager::gotoAppended(Resource *res, int subfile) {
	uint32 offset = _fileIndex[subfile];
	uint32 size = (subfile == (int)_fileIndex.size() - 1) ? res->_file.size() - offset :
		_fileIndex[subfile + 1] - offset;

	res->_size = size;
	res->_stream = new Common::SeekableSubReadStream(&res->_file, offset, offset + size);
}

} // End of namespace Access