aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/resource.cpp
blob: ffcabbd34881c90788556d3726aae4490e4d26e9 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* 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 "toon/resource.h"
#include "common/debug.h"
#include "common/file.h"
#include "common/memstream.h"
#include "common/substream.h"
#include "toon/toon.h"

namespace Toon {

Resources::Resources(ToonEngine *vm) : _vm(vm), _cacheSize(0) {
	_resourceCache.clear();
}

Resources::~Resources() {

	while (!_resourceCache.empty()) {
		CacheEntry *temp = _resourceCache.back();
		_resourceCache.pop_back();
		delete temp;
	}

	while (!_pakFiles.empty()) {
		PakFile *temp = _pakFiles.back();
		_pakFiles.pop_back();
		delete temp;
	}

	purgeFileData();
}

void Resources::removePackageFromCache(const Common::String &packName) {
	// I'm not sure what's a good strategy here. It seems unnecessary to
	// actually remove the cached resources, because the player may be
	// wandering back and forth between rooms. So for now, do nothing.
}

bool Resources::getFromCache(const Common::String &fileName, uint32 *fileSize, uint8 **fileData) {
	for (Common::Array<CacheEntry *>::iterator entry = _resourceCache.begin(); entry != _resourceCache.end(); ++entry) {
		if ((*entry)->_data && (*entry)->_fileName.compareToIgnoreCase(fileName) == 0) {
			debugC(5, kDebugResource, "getFromCache(%s) - Got %d bytes from %s", fileName.c_str(), (*entry)->_size, (*entry)->_packName.c_str());
			(*entry)->_age = 0;
			*fileSize = (*entry)->_size;
			*fileData = (*entry)->_data;
			return true;
			}
		}
	return false;
}

void Resources::addToCache(const Common::String &packName, const Common::String &fileName, uint32 fileSize, uint8 *fileData) {
	debugC(5, kDebugResource, "addToCache(%s, %s, %d) - Total Size: %d", packName.c_str(), fileName.c_str(), fileSize, _cacheSize + fileSize);
	for (Common::Array<CacheEntry *>::iterator entry = _resourceCache.begin(); entry != _resourceCache.end(); ++entry) {
		if ((*entry)->_data) {
			(*entry)->_age++;
		}
	}
	_cacheSize += fileSize;

	while (_cacheSize > MAX_CACHE_SIZE) {
		CacheEntry *bestEntry = 0;
		for (Common::Array<CacheEntry *>::iterator entry = _resourceCache.begin(); entry != _resourceCache.end(); ++entry) {
			if ((*entry)->_data) {
				if (!bestEntry || ((*entry)->_age >= bestEntry->_age && (*entry)->_size >= bestEntry->_size)) {
					bestEntry = *entry;
				}
			}
		}
		if (!bestEntry)
			break;

		free(bestEntry->_data);
		bestEntry->_data = 0;
		_cacheSize -= bestEntry->_size;
		debugC(5, kDebugResource, "Freed %s (%s) to reclaim %d bytes", bestEntry->_fileName.c_str(), bestEntry->_packName.c_str(), bestEntry->_size);
	}

	for (Common::Array<CacheEntry *>::iterator entry = _resourceCache.begin(); entry != _resourceCache.end(); ++entry) {
		if (!(*entry)->_data) {
			(*entry)->_packName = packName;
			(*entry)->_fileName = fileName;
			(*entry)->_age = 0;
			(*entry)->_size = fileSize;
			(*entry)->_data = fileData;
			return;
		}
	}

	CacheEntry *entry = new CacheEntry();
	entry->_packName = packName;
	entry->_fileName = fileName;
	entry->_size = fileSize;
	entry->_data = fileData;
	_resourceCache.push_back(entry);
}

void Resources::openPackage(const Common::String &fileName) {
	debugC(1, kDebugResource, "openPackage(%s)", fileName.c_str());

	Common::File file;
	bool opened = file.open(fileName);

	if (!opened)
		return;

	PakFile *pakFile = new PakFile();
	pakFile->open(&file, fileName);

	file.close();

	_pakFiles.push_back(pakFile);
}

void Resources::closePackage(const Common::String &fileName) {

	removePackageFromCache(fileName);
	for (uint32 i = 0; i < _pakFiles.size(); i++) {
		if (_pakFiles[i]->getPackName() == fileName) {
			delete _pakFiles[i];
			_pakFiles.remove_at(i);
			return;
		}
	}
}

uint8 *Resources::getFileData(const Common::String &fileName, uint32 *fileSize) {
	debugC(4, kDebugResource, "getFileData(%s, fileSize)", fileName.c_str());

	// first try to find files outside of .pak
	// some patched files have not been included in package.
	if (Common::File::exists(fileName)) {
		Common::File file;
		bool opened = file.open(fileName);
		if (!opened)
			return 0;

		*fileSize = file.size();
		uint8 *memory = (uint8 *)new uint8[*fileSize];
		file.read(memory, *fileSize);
		file.close();
		_allocatedFileData.push_back(memory);
		return memory;
	} else {

		uint32 locFileSize = 0;
		uint8 *locFileData = 0;

		if (getFromCache(fileName, &locFileSize, &locFileData)) {
			*fileSize = locFileSize;
			return locFileData;
		}

		for (uint32 i = 0; i < _pakFiles.size(); i++) {

			locFileData = _pakFiles[i]->getFileData(fileName, &locFileSize);
			if (locFileData) {
				*fileSize = locFileSize;
				addToCache(_pakFiles[i]->getPackName(), fileName, locFileSize, locFileData);
				return locFileData;
			}
		}
		return 0;
	}
}

Common::SeekableReadStream *Resources::openFile(const Common::String &fileName) {
	debugC(1, kDebugResource, "openFile(%s)", fileName.c_str());

	// first try to find files outside of .pak
	// some patched files have not been included in package.
	if (Common::File::exists(fileName)) {
		Common::File *file = new Common::File();
		bool opened = file->open(fileName);
		if (!opened) {
			delete file;
			return 0;
		}
		return file;
	} else {
		for (uint32 i = 0; i < _pakFiles.size(); i++) {
			Common::SeekableReadStream *stream = 0;
			stream = _pakFiles[i]->createReadStream(fileName);
			if (stream)
				return stream;
		}

		return 0;
	}
}

void Resources::purgeFileData() {
	for (uint32 i = 0; i < _allocatedFileData.size(); i++) {
		delete[] _allocatedFileData[i];
	}
	_allocatedFileData.clear();
}

Common::SeekableReadStream *PakFile::createReadStream(const Common::String &fileName) {
	debugC(1, kDebugResource, "createReadStream(%s)", fileName.c_str());

	uint32 fileSize = 0;
	uint8 *buffer = getFileData(fileName, &fileSize);
	if (buffer)
		return new Common::MemoryReadStream(buffer, fileSize, DisposeAfterUse::YES);
	else
		return 0;
}

uint8 *PakFile::getFileData(const Common::String &fileName, uint32 *fileSize) {
	debugC(4, kDebugResource, "getFileData(%s, fileSize)", fileName.c_str());

	for (uint32 i = 0; i < _numFiles; i++) {
		if (fileName.compareToIgnoreCase(_files[i]._name) == 0) {
			Common::File file;
			if (file.open(_packName)) {
					*fileSize = _files[i]._size;
					file.seek(_files[i]._offset);

					// Use malloc() because that's what MemoryReadStream
					// uses to dispose of the memory when it's done.
					uint8 *buffer = (uint8 *)malloc(*fileSize);
					file.read(buffer, *fileSize);
					file.close();
					return buffer;
			}
		}
	}

	return 0;
}

void PakFile::open(Common::SeekableReadStream *rs, const Common::String &packName) {
	debugC(1, kDebugResource, "open(rs)");

	char buffer[64];
	int32 currentPos = 0;
	_numFiles = 0;
	_packName = packName;

	while (1) {
		rs->seek(currentPos);
		rs->read(buffer, 64);

		int32 offset = READ_LE_UINT32(buffer);
		char *name = buffer + 4;

		if (!*name)
			break;

		int32 nameSize = strlen(name) + 1;
		int32 nextOffset = READ_LE_UINT32(buffer + 4 + nameSize);
		currentPos += 4 + nameSize;

		PakFile::File newFile;
		strcpy(newFile._name, name);
		newFile._offset = offset;
		newFile._size = nextOffset - offset;
		_numFiles++;
		_files.push_back(newFile);
	}
}

void PakFile::close() {
}

PakFile::PakFile() {
	_numFiles = 0;
}

PakFile::~PakFile() {
	close();
}

} // End of namespace Toon