aboutsummaryrefslogtreecommitdiff
path: root/gob/dataio.cpp
blob: a3ffabbf6cd8270f33a5444d4a1473a4b6153999 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 Ivan Dubrov
 * Copyright (C) 2004-2005 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */
#include "gob/gob.h"
#include "gob/global.h"
#include "gob/dataio.h"
#include "gob/pack.h"

namespace Gob {

int16 file_write(int16 handle, char *buf, int16 size) {
	return filesHandles[handle].write(buf, size);
}

int16 file_open(const char *path, Common::File::AccessMode mode) {
	int16 i;

	for (i = 0; i < MAX_FILES; i++) {
		if (!filesHandles[i].isOpen())
			break;
	}
	if (i == MAX_FILES)
		return -1;

	filesHandles[i].open(path, mode);

	if (filesHandles[i].isOpen())
		return i;
	
	return -1;
}

Common::File *file_getHandle(int16 handle) {
	return &filesHandles[handle];
}

int16 data_getChunk(const char *chunkName) {
	int16 file;
	int16 slot;
	int16 chunk;
	struct ChunkDesc *dataDesc;

	for (file = 0; file < MAX_DATA_FILES; file++) {
		if (dataFiles[file] == 0)
			return -1;

		for (slot = 0; slot < MAX_SLOT_COUNT; slot++)
			if (chunkPos[file * MAX_SLOT_COUNT + slot] == -1)
				break;

		if (slot == MAX_SLOT_COUNT)
			return -1;

		dataDesc = dataFiles[file];
		for (chunk = 0; chunk < numDataChunks[file];
		    chunk++, dataDesc++) {
			if (scumm_stricmp(chunkName, dataDesc->chunkName) != 0)
				continue;

			isCurrentSlot[file * MAX_SLOT_COUNT + slot] = 0;
			chunkSize[file * MAX_SLOT_COUNT + slot] =
			    dataDesc->size;
			chunkOffset[file * MAX_SLOT_COUNT + slot] =
			    dataDesc->offset;
			chunkPos[file * MAX_SLOT_COUNT + slot] = 0;
			return file * 10 + slot + 50;
		}
	}
	return -1;
}

char data_freeChunk(int16 handle) {
	if (handle >= 50 && handle < 100) {
		handle -= 50;
		chunkPos[(handle / 10) * MAX_SLOT_COUNT + (handle % 10)] = -1;
		return 0;
	}
	return 1;
}

int32 data_readChunk(int16 handle, char *buf, int16 size) {
	int16 file;
	int16 slot;
	int16 i;
	int32 offset;

	if (handle < 50 || handle >= 100)
		return -2;

	file = (handle - 50) / 10;
	slot = (handle - 50) % 10;
	if (isCurrentSlot[file * MAX_SLOT_COUNT + slot] == 0) {
		for (i = 0; i < MAX_SLOT_COUNT; i++)
			isCurrentSlot[file * MAX_SLOT_COUNT + i] = 0;

		offset =
		    chunkOffset[file * MAX_SLOT_COUNT + slot] +
		    chunkPos[file * MAX_SLOT_COUNT + slot];
		debug(7, "seek: %ld, %ld", chunkOffset[file * MAX_SLOT_COUNT + slot], chunkPos[file * MAX_SLOT_COUNT + slot]);
		file_getHandle(dataFileHandles[file])->seek(offset, SEEK_SET);
	}

	isCurrentSlot[file * MAX_SLOT_COUNT + slot] = 1;
	if (chunkPos[file * MAX_SLOT_COUNT + slot] + size >
	    chunkSize[file * MAX_SLOT_COUNT + slot])
		size =
		    chunkSize[file * MAX_SLOT_COUNT + slot] -
		    chunkPos[file * MAX_SLOT_COUNT + slot];

	file_getHandle(dataFileHandles[file])->read(buf, size);
	chunkPos[file * MAX_SLOT_COUNT + slot] += size;
	return size;
}

int16 data_seekChunk(int16 handle, int32 pos, int16 from) {
	int16 file;
	int16 slot;

	if (handle < 50 || handle >= 100)
		return -1;

	file = (handle - 50) / 10;
	slot = (handle - 50) % 10;
	isCurrentSlot[file * MAX_SLOT_COUNT + slot] = 0;
	if (from == SEEK_SET)
		chunkPos[file * MAX_SLOT_COUNT + slot] = pos;
	else
		chunkPos[file * MAX_SLOT_COUNT + slot] += pos;

	return chunkPos[file * MAX_SLOT_COUNT + slot];
}

int32 data_getChunkSize(const char *chunkName) {
	int16 file;
	int16 chunk;
	struct ChunkDesc *dataDesc;
	int16 slot;
	int32 realSize;

	for (file = 0; file < MAX_DATA_FILES; file++) {
		if (dataFiles[file] == 0)
			return -1;

		dataDesc = dataFiles[file];
		for (chunk = 0; chunk < numDataChunks[file];
		    chunk++, dataDesc++) {
			if (scumm_stricmp(chunkName, dataDesc->chunkName) != 0)
				continue;

			if (dataDesc->packed == 0) {
				packedSize = -1;
				return dataDesc->size;
			}

			for (slot = 0; slot < MAX_SLOT_COUNT; slot++)
				isCurrentSlot[slot] = 0;

			file_getHandle(dataFileHandles[file])->seek(dataDesc->offset, SEEK_SET);
			realSize = file_getHandle(dataFileHandles[file])->readUint32LE();
			packedSize = dataDesc->size;
			return realSize;
		}
	}
	return -1;
}

void data_openDataFile(const char *src) {
	char path[128];
	int16 i;
	int16 file;
	struct ChunkDesc *dataDesc;

	strcpy(path, src);
	for (i = 0; path[i] != '.' && path[i] != 0; i++);
	if (path[i] == 0)
		strcat(path, ".stk");

	for (file = 0; file < MAX_DATA_FILES; file++)
		if (dataFiles[file] == 0)
			break;

	if (file == MAX_DATA_FILES)
		error("data_dataFileOpen: Data file slots are full\n");
	dataFileHandles[file] = file_open(path);

	if (dataFileHandles[file] == -1)
		error("data_dataFileOpen: Can't open %s data file\n", path);

	numDataChunks[file] = file_getHandle(dataFileHandles[file])->readUint16LE();

	debug(7, "DataChunks: %d [for %s]", numDataChunks[file], path);

	dataFiles[file] = dataDesc = 
	    (struct ChunkDesc *)malloc(sizeof(struct ChunkDesc) *
	    numDataChunks[file]);

	for (i = 0; i < numDataChunks[file]; i++) {
		file_getHandle(dataFileHandles[file])->read(dataDesc[i].chunkName, 13);
		dataDesc[i].size = file_getHandle(dataFileHandles[file])->readUint32LE();
		dataDesc[i].offset = file_getHandle(dataFileHandles[file])->readUint32LE();
		dataDesc[i].packed = file_getHandle(dataFileHandles[file])->readByte();
	}

	for (i = 0; i < numDataChunks[file]; i++)
		debug(7, "%d: %s %d", i, dataDesc[i].chunkName, dataDesc[i].size);

	for (i = 0; i < MAX_SLOT_COUNT; i++)
		chunkPos[file * MAX_SLOT_COUNT + i] = -1;

}

void data_closeDataFile() {
	int16 file;
	for (file = MAX_DATA_FILES - 1; file >= 0; file--) {
		if (dataFiles[file] != 0) {
			free(dataFiles[file]);
			dataFiles[file] = 0;
			file_getHandle(dataFileHandles[file])->close();
			return;
		}
	}
}

char *data_getUnpackedData(const char *name) {
	int32 realSize;
	int16 chunk;
	char *unpackBuf;
	char *packBuf;
	char *ptr;
	int32 sizeLeft;

	realSize = data_getChunkSize(name);
	if (packedSize == -1 || realSize == -1)
		return 0;

	chunk = data_getChunk(name);
	if (chunk == -1)
		return 0;

	unpackBuf = (char *)malloc(realSize);
	if (unpackBuf == 0)
		return 0;

	packBuf = (char *)malloc(packedSize);
	if (packBuf == 0) {
		free(unpackBuf);
		return 0;
	}

	sizeLeft = packedSize;
	ptr = packBuf;
	while (sizeLeft > 0x4000) {
		data_readChunk(chunk, ptr, 0x4000);
		sizeLeft -= 0x4000;
		ptr += 0x4000;
	}
	data_readChunk(chunk, ptr, sizeLeft);
	data_freeChunk(chunk);
	unpackData(packBuf, unpackBuf);
	free(packBuf);
	return unpackBuf;
}

void data_closeData(int16 handle) {
	if (data_freeChunk(handle) != 0)
		file_getHandle(handle)->close();
}

int16 data_openData(const char *path, Common::File::AccessMode mode) {
	int16 handle;

	if (mode != Common::File::kFileReadMode)
		return file_open(path, mode);

	handle = data_getChunk(path);
	if (handle >= 0)
		return handle;

	return file_open(path, mode);
}

int32 data_readData(int16 handle, char *buf, int16 size) {
	int32 res;

	res = data_readChunk(handle, buf, size);
	if (res >= 0)
		return res;

	return file_getHandle(handle)->read(buf, size);
}

void data_seekData(int16 handle, int32 pos, int16 from) {
	int32 resPos;

	resPos = data_seekChunk(handle, pos, from);
	if (resPos != -1)
		return;

	file_getHandle(handle)->seek(pos, from);
}

int32 data_getDataSize(const char *name) {
	char buf[128];
	int32 chunkSz;
	Common::File file;

	strcpy(buf, name);
	chunkSz = data_getChunkSize(buf);
	if (chunkSz >= 0)
		return chunkSz;

	if (!file.open(buf))
		error("data_getDataSize: Can't find data(%s)", name);

	chunkSz = file.size();
	file.close();

	return chunkSz;
}

char *data_getData(const char *path) {
	char *data;
	char *ptr;
	int32 size;
	int16 handle;

	data = data_getUnpackedData(path);
	if (data != 0)
		return data;

	size = data_getDataSize(path);
	data = (char *)malloc(size);
	if (data == 0)
		return 0;

	handle = data_openData(path);

	ptr = data;
	while (size > 0x4000) {
		data_readData(handle, ptr, 0x4000);
		size -= 0x4000;
		ptr += 0x4000;
	}
	data_readData(handle, ptr, size);
	data_closeData(handle);
	return data;
}

char *data_getSmallData(const char *path) {
	return data_getData(path);
}

}				// End of namespace Gob