aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/scicore/resource_map.cpp
blob: 984efa83ac716a59039b0f340eaffaadea0252ec (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/***************************************************************************
 resource_map.c Copyright (C) 2001 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

#include "sci/include/sci_memory.h"
#include "sci/include/sciresource.h"
#include "sci/include/resource.h"
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#define RESOURCE_MAP_FILENAME "resource.map"

#define SCI0_RESMAP_ENTRIES_SIZE 6
#define SCI1_RESMAP_ENTRIES_SIZE 6
#define SCI11_RESMAP_ENTRIES_SIZE 5

static int
detect_odd_sci01(int fh) {
	byte buf[6];
	int files_ok = 1;
	int fsize, resources_nr, tempfh, read_ok;
	char filename[14];

	fsize = sci_fd_size(fh);
	if (fsize < 0) {
		perror("Error occured while trying to get filesize of resource.map");
		return SCI_ERROR_RESMAP_NOT_FOUND;
	}

	resources_nr = fsize / SCI0_RESMAP_ENTRIES_SIZE;

	while (resources_nr-- > 1) {
		read_ok = read(fh, &buf, SCI0_RESMAP_ENTRIES_SIZE);

		if (read_ok) {
			sprintf(filename, "resource.%03i", SCI0_RESFILE_GET_FILE(buf + 2));
			tempfh = sci_open(filename, O_RDONLY | O_BINARY);

			if (tempfh == SCI_INVALID_FD) {
				files_ok = 0;
				break;
			}

			close(tempfh);
		}
	}

	lseek(fh, 0, SEEK_SET);

	return files_ok;
}

static int
sci_res_read_entry(resource_mgr_t *mgr, resource_source_t *map,
                   byte *buf, resource_t *res, int sci_version) {
	res->id = buf[0] | (buf[1] << 8);
	res->type = SCI0_RESID_GET_TYPE(buf);
	res->number = SCI0_RESID_GET_NUMBER(buf);
	res->status = SCI_STATUS_NOMALLOC;

	if (sci_version == SCI_VERSION_01_VGA_ODD) {
		res->source = scir_get_volume(mgr, map, SCI01V_RESFILE_GET_FILE(buf + 2));
		res->file_offset = SCI01V_RESFILE_GET_OFFSET(buf + 2);

#if 0
		if (res->type < 0 || res->type > sci1_last_resource)
			return 1;
#endif
	} else {
		res->source = scir_get_volume(mgr, map, SCI0_RESFILE_GET_FILE(buf + 2));
		res->file_offset = SCI0_RESFILE_GET_OFFSET(buf + 2);

#if 0
		if (res->type < 0 || res->type > sci0_last_resource)
			return 1;
#endif
	}

#if 0
	fprintf(stderr, "Read [%04x] %6d.%s\tresource.%03d, %08x\n",
	        res->id, res->number,
	        sci_resource_type_suffixes[res->type],
	        res->file, res->file_offset);
#endif

	if (res->source == NULL) return 1;
	return 0;
}

inline int sci1_res_type(int ofs, int *types, int lastrt) {
	int i, last = -1;

	for (i = 0;i <= sci1_last_resource;i++)
		if (types[i]) {
			if (types[i] > ofs)
				return last;
			last = i;
		}

	return lastrt;
}

int sci1_parse_header(int fd, int *types, int *lastrt) {
	unsigned char rtype;
	unsigned char offset[2];
	int read_ok;
	int size = 0;

	do {
		read_ok = read(fd, &rtype, 1);
		if (!read_ok) break;
		read_ok = read(fd, &offset, 2);
		if (read_ok < 2)
			read_ok = 0;
		if (rtype != 0xff) {
			types[rtype&0x7f] = (offset[1] << 8) | (offset[0]);
			*lastrt = rtype & 0x7f;
		}
		size += 3;
	} while (read_ok && (rtype != 0xFF));

	if (!read_ok) return 0;

	return size;
}



int
sci0_read_resource_map(resource_mgr_t *mgr, resource_source_t *map, resource_t **resource_p, int *resource_nr_p, int *sci_version) {
	int fsize;
	int fd;
	resource_t *resources;
	int resources_nr;
	int resource_index = 0;
	int resources_total_read = 0;
	int next_entry;
	int max_resfile_nr = 0;

	byte buf[SCI0_RESMAP_ENTRIES_SIZE];
	fd = sci_open(map->location.file.name, O_RDONLY | O_BINARY);

	if (!IS_VALID_FD(fd))
		return SCI_ERROR_RESMAP_NOT_FOUND;

	read(fd, &buf, 4);

	/* Theory: An SCI1 map file begins with an index that allows us to seek quickly
	   to a particular resource type. The entries are three bytes long; one byte
	   resource type, two bytes start position and so on.
	   The below code therefore tests for three things:

	   Is the first resource type 'view'?
	   Do those entries start at an offset that is an exact multiple of the
	   index entry size?
	   Is the second resource type 'pic'?

	   This requires that a given game has both views and pics,
	   a safe assumption usually, except in message.map and room-specific
	   (audio) map files, neither of which SCI0 has.

	   */

	if ((buf[0] == 0x80) &&
	        (buf[1] % 3 == 0) &&
	        (buf[3] == 0x81)) {
		close(fd);
		return SCI_ERROR_INVALID_RESMAP_ENTRY;
	}

	lseek(fd, 0, SEEK_SET);

	switch (detect_odd_sci01(fd)) {
	case 0 : /* Odd SCI01 */
		if (*sci_version == SCI_VERSION_AUTODETECT)
			*sci_version = SCI_VERSION_01_VGA_ODD;
		break;
	case 1 : /* SCI0 or normal SCI01 */
		if (*sci_version == SCI_VERSION_AUTODETECT)
			*sci_version = SCI_VERSION_0;
		break;
	default : /* Neither, or error occurred */
		return SCI_ERROR_RESMAP_NOT_FOUND;
	}

	if ((fsize = sci_fd_size(fd)) < 0) {
		perror("Error occured while trying to get filesize of resource.map");
		return SCI_ERROR_RESMAP_NOT_FOUND;
	}

	resources_nr = fsize / SCI0_RESMAP_ENTRIES_SIZE;

	resources = (resource_t*)sci_calloc(resources_nr, sizeof(resource_t));
	/* Sets valid default values for most entries */

	do {
		int read_ok = read(fd, &buf, SCI0_RESMAP_ENTRIES_SIZE);
		next_entry = 1;

		if (read_ok < 0) {
			sciprintf("Error while reading %s: ", map->location.file.name);
			perror("");
			next_entry = 0;
		} else if (read_ok != SCI0_RESMAP_ENTRIES_SIZE) {
			next_entry = 0;
		} else if (buf[5] == 0xff) /* Most significant offset byte */
			next_entry = 0;

		if (next_entry) {
			int fresh = 1;
			int addto = resource_index;
			int i;

			if (sci_res_read_entry(mgr, map, buf, resources + resource_index, *sci_version)) {
				sci_free(resources);
				close(fd);
				return SCI_ERROR_RESMAP_NOT_FOUND;
			}

			for (i = 0; i < resource_index; i++)
				if (resources[resource_index].id ==
				        resources[i].id) {
					addto = i;
					fresh = 0;
				}

			_scir_add_altsource(resources + addto,
			                    resources[resource_index].source,
			                    resources[resource_index].file_offset);

			if (fresh)
				++resource_index;

			if (++resources_total_read >= resources_nr) {
				sciprintf("Warning: After %d entries, resource.map"
				          " is not terminated!\n", resource_index);
				next_entry = 0;
			}

		}

	} while (next_entry);

	close(fd);

	if (!resource_index) {
		sciprintf("resource.map was empty!\n");
		_scir_free_resources(resources, resources_nr);
		return SCI_ERROR_RESMAP_NOT_FOUND;
	}

	if (max_resfile_nr > 999) {
		_scir_free_resources(resources, resources_nr);
		return SCI_ERROR_INVALID_RESMAP_ENTRY;
	} else {
#if 0
		/* Check disabled, Mac SQ3 thinks it has resource.004 but doesn't need it -- CR */
		/* Check whether the highest resfile used exists */
		char filename_buf[14];
		sprintf(filename_buf, "resource.%03d", max_resfile_nr);
		fd = sci_open(filename_buf, O_RDONLY);

		if (!IS_VALID_FD(fd)) {
			_scir_free_resources(resources, resources_nr);
			sciprintf("'%s' requested by resource.map, but not found\n", filename_buf);
			return SCI_ERROR_INVALID_RESMAP_ENTRY;
		} else
			close(fd);
#endif
	}

	if (resource_index < resources_nr)
		resources = (resource_t*)sci_realloc(resources, sizeof(resource_t) * resource_index);

	*resource_p = resources;
	*resource_nr_p = resource_index;

	return 0;
}

#define TEST fprintf(stderr, "OK in line %d\n", __LINE__);

static int sci10_or_11(int *types) {
	int this_restype = 0;
	int next_restype = 1;

	while (next_restype <= sci_heap) {
		int could_be_10 = 0;
		int could_be_11 = 0;

		while (types[this_restype] == 0) {
			this_restype++;
			next_restype++;
		}

		while (types[next_restype] == 0)
			next_restype++;

		could_be_10 = ((types[next_restype] - types[this_restype])
		               % SCI1_RESMAP_ENTRIES_SIZE) == 0;
		could_be_11 = ((types[next_restype] - types[this_restype])
		               % SCI11_RESMAP_ENTRIES_SIZE) == 0;

		if (could_be_10 && !could_be_11) return SCI_VERSION_1;
		if (could_be_11 && !could_be_10) return SCI_VERSION_1_1;

		this_restype++;
		next_restype++;
	}

	return SCI_VERSION_AUTODETECT;
}

int
sci1_read_resource_map(resource_mgr_t *mgr, resource_source_t *map, resource_source_t *vol,
                       resource_t **resource_p, int *resource_nr_p, int *sci_version) {
	int fsize;
	int fd;
	resource_t *resources, *resource_start;
	int resources_nr;
	int resource_index = 0;
	int ofs, header_size;
	int *types = (int*)sci_malloc(sizeof(int) * (sci1_last_resource + 1));
	int i;
	byte buf[SCI1_RESMAP_ENTRIES_SIZE];
	int lastrt;
	int entrysize;
	int entry_size_selector;

	fd = sci_open(map->location.file.name, O_RDONLY | O_BINARY);

	if (!IS_VALID_FD(fd))
		return SCI_ERROR_RESMAP_NOT_FOUND;

	memset(types, 0, sizeof(int) * (sci1_last_resource + 1));

	if (!(sci1_parse_header(fd, types, &lastrt))) {
		close(fd);
		return SCI_ERROR_INVALID_RESMAP_ENTRY;
	}

	entry_size_selector = sci10_or_11(types);
	if (*sci_version == SCI_VERSION_AUTODETECT)
		*sci_version = entry_size_selector;

	if (*sci_version == SCI_VERSION_AUTODETECT) { /* That didn't help */
		sciprintf("Unable to detect resource map version\n");
		close(fd);
		return SCI_ERROR_NO_RESOURCE_FILES_FOUND;
	}

	entrysize = entry_size_selector == SCI_VERSION_1_1
	            ? SCI11_RESMAP_ENTRIES_SIZE
	            : SCI1_RESMAP_ENTRIES_SIZE;

	if ((fsize = sci_fd_size(fd)) < 0) {
		perror("Error occured while trying to get filesize of resource.map");
		close(fd);
		return SCI_ERROR_RESMAP_NOT_FOUND;
	}

	resources_nr = (fsize - types[0]) / entrysize;
	resource_start = resources = (resource_t*)sci_realloc(mgr->resources, (mgr->resources_nr + resources_nr) * sizeof(resource_t));
	resources += mgr->resources_nr;

	i = 0;
	while (types[i] == 0) i++;
	header_size = ofs = types[i];

	lseek(fd, ofs, SEEK_SET);

	for (i = 0; i < resources_nr; i++) {
		int read_ok = read(fd, &buf, entrysize);
		int j;
		resource_t *res;
		int addto = resource_index;
		int fresh = 1;

		if (read_ok < entrysize) {
#if 0
			if (!eof(fd)) {
				sciprintf("Error while reading %s: ", map->location.file.name);
				perror("");
			} else read_ok = 1;
			break;
#endif
		}

		res = &(resources[resource_index]);
		res->type = sci1_res_type(ofs, types, lastrt);
		res->number = SCI1_RESFILE_GET_NUMBER(buf);
		res->status = SCI_STATUS_NOMALLOC;

		if (entry_size_selector < SCI_VERSION_1_1) {
			res->source = scir_get_volume(mgr, map, SCI1_RESFILE_GET_FILE(buf));
			res->file_offset = SCI1_RESFILE_GET_OFFSET(buf);
		} else {
			res->source = vol;
			res->file_offset = SCI11_RESFILE_GET_OFFSET(buf);
		};

		res->id = res->number | (res->type << 16);

		for (j = 0; i < resource_index; i++)
			if (resources[resource_index].id ==
			        resources[i].id) {
				addto = i;
				fresh = 0;
			}

#if 0
		fprintf(stderr, "Read [%04x] %6d.%s\tresource.%03d, %08x ==> %d\n",
		        res->id, res->number,
		        sci_resource_type_suffixes[res->type],
		        res->file, res->file_offset, addto);
#endif

		_scir_add_altsource(resources + addto,
		                    resources[resource_index].source,
		                    resources[resource_index].file_offset);

		if (fresh)
			++resource_index;

		ofs += entrysize;
	}

	close(fd);
	free(types);

	*resource_p = resource_start;
	*resource_nr_p += resource_index;
	return 0;

}

#ifdef TEST_RESOURCE_MAP
int
main(int argc, char **argv) {
	int resources_nr;
	resource_t *resources;
	int notok = sci0_read_resource_map(".", &resources, &resources_nr);

	if (notok) {
		fprintf(stderr, "Failed: Error code %d\n", notok);
		return 1;
	}

	if (resources) {
		int i;

		printf("Found %d resources:\n", resources_nr);

		for (i = 0; i < resources_nr; i++) {
			resource_t *res = resources + i;

			printf("#%04d:\tRESOURCE.%03d:%8d\t%s.%03d\n",
			       i, res->file, res->file_offset,
			       sci_resource_types[res->type],
			       res->number);
		}
	} else
		fprintf(stderr, "Found no resources.\n");

	return 0;
}
#endif