aboutsummaryrefslogtreecommitdiff
path: root/tools/skycpt/cptcompiler.cpp
blob: cac009a66208add4bcaeddee513a4487beb1cff6 (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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* ScummVM Tools
 * Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "stdafx.h"
#include "cpthelp.h"
#include "TextFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

uint32 crop(char *line);
uint16 findCptId(char *name, TextFile *cptFile);

#define MAX_CPTS 0xA000
#define MAX_OBJ_SIZE (0x2000 * 2)
#define NUM_DATA_LISTS 9
#define ASCII_SIZE (65536 * 2)

enum CptType {
	PTR_NULL = 0,
	COMPACT,
	TURNTAB,
	ANIMSEQ,
	MISCBIN,
	GETTOTAB,
	ROUTEBUF,
	MAINLIST
};

void processMainLists(FILE *inf, CptObj *destArr, uint16 *idList) {
	char line[1024];
	dofgets(line, 1024, inf);
	assert(lineMatchSection(line, "MAINLISTS"));
	uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
	uint32 idNum = 0;
	do {
		dofgets(line, 1024, inf);
		if (!isEndOfSection(line)) {
			char cptName[50];
			uint16 id = getInfo(line, "MAINLST", cptName);
			CptObj *dest = destArr + id;
			assertEmpty(dest);
			dest->type = MAINLIST;
			dest->dbgName = (char*)malloc(strlen(cptName) + 1);
			strcpy(dest->dbgName, cptName);
			memset(resBuf, 0, MAX_OBJ_SIZE);
			uint32 resPos = 0;
			idList[idNum] = id;
			idNum++;

			do {
				dofgets(line, 1024, inf);
				if (!isEndOfObject(line, "MAINLST", id)) {
					assert((line[0] == '\t') && (line[1] == '\t'));
					char *stopCh;
					uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
					assert(stopCh == (line + 6));
					assert((stopCh[0] == ':') && (stopCh[1] == ':'));
                    resBuf[resPos] = destId;
					resPos++;
				} else
					break;
			} while (1);
			assert(resPos < (MAX_OBJ_SIZE / 2));
			dest->len = resPos;
			dest->data = (uint16*)malloc(resPos * 2);
			memcpy(dest->data, resBuf, resPos * 2);
		} else
			break;
	} while (1);

	free(resBuf);
}

void processCpts(FILE *inf, CptObj *destArr) {
	char line[1024];
	dofgets(line, 1024, inf);
	assert(lineMatchSection(line, "COMPACTS"));
	uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
	do {
		dofgets(line, 1024, inf);
		if (!isEndOfSection(line)) {
			char cptName[50];
			uint16 id = getInfo(line, "COMPACT", cptName);
			CptObj *dest = destArr + id;
			assertEmpty(dest);
			dest->dbgName = (char*)malloc(strlen(cptName) + 1);
			dest->type = COMPACT;
			strcpy(dest->dbgName, cptName);
			memset(resBuf, 0, MAX_OBJ_SIZE);
			uint32 resPos = 0;

			do {
				dofgets(line, 1024, inf);
				if (!isEndOfObject(line, "COMPACT", id)) {
					assert((line[0] == '\t') && (line[1] == '\t'));
					char *stopCh;
					uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
					assert(stopCh != (line + 2));
					assert((stopCh[0] == '-') && (stopCh[1] == '>'));
					if (resPos == 23) { // grafixProg
						assert(destId == 0);
						resBuf[resPos] = resBuf[resPos + 1] = 0;
						resPos += 2;
					} else if (resPos == 48) {	// turnProg. shouldn't it be 49?
						assert(destId == 0);
						resBuf[resPos] = resBuf[resPos + 1] = 0;
						resPos += 2;
					} else {
						resBuf[resPos] = destId;
						resPos++;
					}
				} else
					break;
			} while (1);
			assert(resPos < (MAX_OBJ_SIZE / 2));
			dest->len = resPos;
			dest->data = (uint16*)malloc(resPos * 2);
			memcpy(dest->data, resBuf, resPos * 2);
		} else
			break;
	} while (1);

	free(resBuf);
}

void processTurntabs(FILE *inf, CptObj *destArr) {
	char line[1024];
	dofgets(line, 1024, inf);
	assert(lineMatchSection(line, "TURNTABS"));
	uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
	do {
		dofgets(line, 1024, inf);
		if (!isEndOfSection(line)) {
			char cptName[50];
			uint16 id = getInfo(line, "TURNTAB", cptName);
			CptObj *dest = destArr + id;
			assertEmpty(dest);
			dest->dbgName = (char*)malloc(strlen(cptName) + 1);
			dest->type = TURNTAB;
			strcpy(dest->dbgName, cptName);
			memset(resBuf, 0, MAX_OBJ_SIZE);
			uint32 resPos = 0;

			do {
				dofgets(line, 1024, inf);
				if (!isEndOfObject(line, "TURNTAB", id)) {
					assert((line[0] == '\t') && (line[1] == '\t'));
					char *stopCh;
					uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
					assert(stopCh == (line + 6));
					assert((stopCh[0] == '-') && (stopCh[1] == '>'));
                    resBuf[resPos] = destId;
					resPos++;
				} else
					break;
			} while (1);
			assert(resPos < (MAX_OBJ_SIZE / 2));
			dest->len = resPos;
			dest->data = (uint16*)malloc(resPos * 2);
			memcpy(dest->data, resBuf, resPos * 2);
		} else
			break;
	} while (1);

	free(resBuf);
}

void processBins(FILE *inf, CptObj *destArr, const char *typeName, const char *objName, uint8 cTypeId) {
	char line[1024];
	dofgets(line, 1024, inf);
	assert(lineMatchSection(line, typeName));
	uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
	do {
		dofgets(line, 1024, inf);
		if (!isEndOfSection(line)) {
			char cptName[50];
			uint16 id = getInfo(line, objName, cptName);
			CptObj *dest = destArr + id;
			assertEmpty(dest);
			dest->dbgName = (char*)malloc(strlen(cptName) + 1);
			dest->type = cTypeId;
			strcpy(dest->dbgName, cptName);
			memset(resBuf, 0, MAX_OBJ_SIZE);
			uint32 resPos = 0;

			do {
				dofgets(line, 1024, inf);
				if (!isEndOfObject(line, objName, id)) {
					assert((line[0] == '\t') && (line[1] == '\t'));
					char *stopCh;
					uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
					assert(stopCh == (line + 6));
					assert(*stopCh == '\0');
                    resBuf[resPos] = destId;
					resPos++;
				} else
					break;
			} while (1);
			assert(resPos < (MAX_OBJ_SIZE / 2));
			dest->len = resPos;
			dest->data = (uint16*)malloc(resPos * 2);
			memcpy(dest->data, resBuf, resPos * 2);
		} else
			break;
	} while (1);

	free(resBuf);
}

uint16 dlinkCount = 0;
static uint16 dlinks[1024];
static char* dlinkNames[512];

void processSymlinks(FILE *inf, CptObj *destArr, uint16 *baseLists) {
	char line[1024];
	dofgets(line, 1024, inf);
	assert(lineMatchSection(line, "SYMLINKS"));
	do {
		dofgets(line, 1024, inf);
		if (!isEndOfSection(line)) {
			char cptName[50];
			uint16 fromId = getInfo(line, "SYMLINK", cptName);
			CptObj *from = destArr + fromId;
			assertEmpty(from);
			dlinkNames[dlinkCount] = (char*)malloc(strlen(cptName) + 1);
			strcpy(dlinkNames[dlinkCount], cptName);

			dofgets(line, 1024, inf);
			assert((line[0] == '\t') && (line[1] == '\t') && (line[2] == '-') && (line[3] == '>'));
			char *stopCh;
			uint16 destId = (uint16)strtoul(line + 4, &stopCh, 16);
			assert(stopCh == (line + 8));
			assert((stopCh[0] == ':') && (stopCh[1] == ':'));

			dlinks[dlinkCount * 2 + 0] = fromId;
			dlinks[dlinkCount * 2 + 1] = destId;

			dlinkCount++;

			dofgets(line, 1024, inf);
			assert(isEndOfObject(line, "SYMLINK", fromId));
		} else
			break;
	} while (1);
}

void doCompile(FILE *inf, FILE *debOutf, FILE *resOutf, TextFile *cptDef, FILE *sve) {
	uint16 maxStrl = 0;
	uint16 maxCptl = 0;

	printf("Processing...\n");
	CptObj *resCpts;
	uint16 baseLists[NUM_DATA_LISTS];
	memset(baseLists, 0, NUM_DATA_LISTS * 2);
	resCpts = (CptObj*)malloc(MAX_CPTS * sizeof(CptObj));
	memset(resCpts, 0, MAX_CPTS * sizeof(CptObj));
	printf(" MainLists...\n");
	processMainLists(inf, resCpts, baseLists);
	printf(" Compacts...\n");
	processCpts(inf, resCpts);
	printf(" Turntables...\n");
	processTurntabs(inf, resCpts);
	printf(" Animation tables...\n");
	processBins(inf, resCpts, "ANIMSEQS", "ANIMSEQ", ANIMSEQ);
	printf(" Unknown binaries...\n");
	processBins(inf, resCpts, "MISCBINS", "MISCBIN", MISCBIN);
	printf(" Get To tables...\n");
	processBins(inf, resCpts, "GETTOTAB", "GET_TOS", GETTOTAB);
	printf(" Scratch buffers...\n");
	processBins(inf, resCpts, "SCRATCHR", "SCRATCH", ROUTEBUF);
	printf(" Symbolic links...\n");
	processSymlinks(inf, resCpts, baseLists);
	printf("Converting to binary data...\n");
	uint32 numCpts = 1;
	for (uint32 cnt = 1; cnt < MAX_CPTS; cnt++)
		if (resCpts[cnt].data || resCpts[cnt].dbgName || resCpts[cnt].len)
			numCpts++;

	uint16 dataListLen[NUM_DATA_LISTS];
	for (uint32 cnt = 0; cnt < NUM_DATA_LISTS; cnt++)
		for (uint16 elemCnt = 0; elemCnt < 0x1000; elemCnt++) {
			uint32 id = (cnt << 12) | elemCnt;
			if (resCpts[id].data || resCpts[id].dbgName || resCpts[id].len)
				dataListLen[cnt] = elemCnt + 1;
		}

	// write the header
	uint32 rev = 0;
	fwrite(&rev, 2, 1, debOutf);
	fwrite(&rev, 2, 1, resOutf);
	rev = NUM_DATA_LISTS;
	fwrite(&rev, 2, 1, debOutf);
	fwrite(&rev, 2, 1, resOutf);
	for (uint32 cnt = 0; cnt < NUM_DATA_LISTS; cnt++) {
		fwrite(dataListLen + cnt, 2, 1, debOutf);
		fwrite(dataListLen + cnt, 2, 1, resOutf);
	}

	uint32 binSize = 0;
	uint32 binDest = ftell(debOutf);
	fwrite(&binSize, 1, 4, debOutf);
	fwrite(&binSize, 1, 4, resOutf);
	fwrite(&binSize, 1, 4, debOutf);
	fwrite(&binSize, 1, 4, resOutf);

	char *asciiBuf = (char*)malloc(ASCII_SIZE);
	char *asciiPos = asciiBuf;

	// now process all the compacts
	uint32 cptSize[2];
	cptSize[0] = ftell(debOutf);
	cptSize[1] = ftell(resOutf);
	for (uint32 lcnt = 0; lcnt < NUM_DATA_LISTS; lcnt++) {
		for (uint32 eCnt = 0; eCnt < dataListLen[lcnt]; eCnt++) {
			uint32 cId = (lcnt << 12) | eCnt;
			CptObj *cpt = resCpts + cId;
			if (resCpts[cId].data || resCpts[cId].dbgName || resCpts[cId].len || resCpts[cId].type) {
				strcpy(asciiPos, cpt->dbgName);
				asciiPos += strlen(cpt->dbgName) + 1;

				assert(cpt->len < 0xFFFF);
				uint16 dlen = (uint16)cpt->len;
				if (dlen > maxCptl)
					maxCptl = dlen;
				binSize += dlen;
				assert(dlen != 0);

				fwrite(&dlen, 2, 1, debOutf);
				fwrite(&dlen, 2, 1, resOutf);

				uint16 field = resCpts[cId].type;
				fwrite(&field, 2, 1, debOutf);

				fwrite(cpt->data, 2, dlen, debOutf);
				fwrite(cpt->data, 2, dlen, resOutf);
			} else {
				uint16 tmp = 0;
				fwrite(&tmp, 2, 1, debOutf);
				fwrite(&tmp, 2, 1, resOutf);
			}
		}
	}
	cptSize[0] = ftell(debOutf) - cptSize[0];
	cptSize[1] = ftell(resOutf) - cptSize[1];
	assert(!(cptSize[0] & 1));
	assert(!(cptSize[1] & 1));
	cptSize[0] /= 2;
	cptSize[1] /= 2;

	for (uint32 cnt = 0; cnt < dlinkCount; cnt++) {
		strcpy(asciiPos, dlinkNames[cnt]);
		asciiPos += strlen(dlinkNames[cnt]) + 1;
	}

	uint32 asciiSize = (uint32)(asciiPos - asciiBuf);
	fwrite(&asciiSize, 1, 4, debOutf);
	fwrite(asciiBuf, 1, asciiSize, debOutf);

	// the direct links...
	fwrite(&dlinkCount, 2, 1, debOutf);
	fwrite(&dlinkCount, 2, 1, resOutf);
	for (uint32 cnt = 0; cnt < dlinkCount; cnt++) {
		fwrite(dlinks + cnt * 2 + 0, 2, 1, debOutf);
		fwrite(dlinks + cnt * 2 + 0, 2, 1, resOutf);

		fwrite(dlinks + cnt * 2 + 1, 2, 1, debOutf);
		fwrite(dlinks + cnt * 2 + 1, 2, 1, resOutf);
	}
	printf("Processing diff data...\n");
	// 288 diffdata
	FILE *dif = fopen("288diff.txt", "r");
	assert(dif);
	char line[1024];
	uint16 diff[8192];
	uint16 diffDest = 0;
	uint16 diffNo = 0;
	while (fgets(line, 1024, dif)) {
		crop(line);
		if (line[0] != '$') {
			assert(memcmp(line, "data_", 5) == 0);
			char *pos = line + 5;
			char *stopCh;
			uint16 lId = (uint16)strtoul(pos, &stopCh, 10);
			assert(*stopCh == '[');
			uint16 eId = (uint16)strtoul(stopCh + 1, &stopCh, 10);
			assert((stopCh[0] == ']') && (stopCh[1] == '[') && (eId <= 0xFFF) && (lId <= 7));
			uint16 id = (lId << 12) | eId;
			uint16 elemNo = (uint16)strtoul(stopCh + 2, &stopCh, 10);
			assert(*stopCh == ']');
			stopCh = strstr(stopCh, "0x") + 2;
			uint16 val = (uint16)strtoul(stopCh, &stopCh, 16);
			assert(*stopCh == ';');
			diff[diffDest++] = id;
			diff[diffDest++] = elemNo;
			diff[diffDest++] = 1;
			diff[diffDest++] = val;
			diffNo++;
		} else {
			char *pos = strchr(line, ' ');
			*pos = '\0';
			uint16 id = findCptId(line + 1, cptDef);
			assert(id);
			diff[diffDest++] = id;
			diff[diffDest++] = 0;
            pos++;
			uint16 len = (uint16)strtoul(pos, &pos, 10);
			diff[diffDest++] = len;
			assert(len);
			assert(resCpts[id].len == len);
			for (uint16 cnt = 0; cnt < len; cnt++) {
				assert(*pos == ' ');
				pos++;
				diff[diffDest++] = (uint16)strtoul(pos, &pos, 16);
			}
			assert(diff[diffDest - 1] == 0xFFFF);
			diffNo++;
		}
	}
	assert(diffDest <= 8192);
	fwrite(&diffNo, 1, 2, debOutf);
	fwrite(&diffDest, 1, 2, debOutf);
	fwrite(diff, 2, diffDest, debOutf);
	fwrite(&diffNo, 1, 2, resOutf);
	fwrite(&diffDest, 1, 2, resOutf);
	fwrite(diff, 2, diffDest, resOutf);

	printf("Converting Save data...\n");
	// the IDs of the compacts to be saved
	char cptName[1024];
	uint16 saveIds[2048];
	uint16 numIds = 0;
	while (fgets(cptName, 1024, sve)) {
		crop(cptName);
		uint16 resId = findCptId(cptName, cptDef);
		if (!resId)
			printf("ERROR: Can't find definition of %s\n", cptName);
		else {
			saveIds[numIds] = resId;
			numIds++;
		}
	}
	printf("%d saveIds\n", numIds);
	fwrite(&numIds, 2, 1, debOutf);
	fwrite(saveIds, 2, numIds, debOutf);
	fwrite(&numIds, 2, 1, resOutf);
	fwrite(saveIds, 2, numIds, resOutf);

	printf("Converting Reset data...\n");
	// now append the reset data
	uint16 gameVers[7] = { 303, 331, 348, 365, 368, 372, 288 };
	// make sure all files exist
	bool filesExist = true;
	char inName[32];
	for (int i = 0; i < 7; i++) {
		sprintf(inName, "reset.%03d", gameVers[i]);
		FILE *test = fopen(inName, "rb");
		if (test)
			fclose(test);
		else {
			filesExist = false;
			printf("File %s not found\n", inName);
		}
	}

	if (filesExist) {
		FILE *res288 = fopen("RESET.288", "rb");
		fseek(res288, 0, SEEK_END);
		assert((ftell(res288) / 2) < 65536);
		uint16 resSize = (uint16)(ftell(res288) / 2);
		fseek(res288, 0, SEEK_SET);
		uint16 *buf288 = (uint16*)malloc(resSize * 2);
		fread(buf288, 2, resSize, res288);
		fclose(res288);
		fwrite(&resSize, 1, 2, debOutf);
		fwrite(buf288, 2, resSize, debOutf);

		uint16 tmp = 7;
		fwrite(&tmp, 2, 1, debOutf);
		tmp = 288;
		fwrite(&tmp, 2, 1, debOutf);
		tmp = 0;
		fwrite(&tmp, 2, 1, debOutf);
		printf("reset destination: %ld\n", ftell(debOutf));
		for (int cnt = 0; cnt < 6; cnt++) {
			uint16 diffPos = 0;
			sprintf(inName, "reset.%03d", gameVers[cnt]);
			FILE *resDiff = fopen(inName, "rb");
			fseek(resDiff, 0, SEEK_END);
			assert(ftell(resDiff) == (resSize * 2));
			fseek(resDiff, 0, SEEK_SET);
			uint16 *bufDif = (uint16*)malloc(resSize *2);
			fread(bufDif, 2, resSize, resDiff);
			fclose(resDiff);
			for (uint16 eCnt = 0; eCnt < resSize; eCnt++)
				if (buf288[eCnt] != bufDif[eCnt]) {
					diff[diffPos++] = eCnt;
					diff[diffPos++] = bufDif[eCnt];
				}
			free(bufDif);
			fwrite(gameVers + cnt, 1, 2, debOutf);
			assert(!(diffPos & 1));
			diffPos /= 2;
			fwrite(&diffPos, 1, 2, debOutf);
			fwrite(diff, 2, 2 * diffPos, debOutf);
			printf("diff v0.0%03d: 2 * 2 * %d\n", gameVers[cnt], diffPos);
		}
	} else {
		printf("Creating CPT file with Dummy reset data @ %ld\n", ftell(debOutf));
		uint16 resetFields16 = 4;
		fwrite(&resetFields16, 2, 1, debOutf);
		uint32 blah = 8;
		fwrite(&blah, 4, 1, debOutf); // size field: 8 bytes
		blah = (uint32)-1;
		fwrite(&blah, 4, 1, debOutf); // save file revision. -1 is unknown to scummvm, so it'll refuse to load it.
		resetFields16 = 0;
		fwrite(&resetFields16, 2, 1, debOutf); // numDiffs: 0, no further reset blocks.
	}

	// now fill the raw-compact-data-size header field
	fseek(resOutf, binDest, SEEK_SET);
	fseek(debOutf, binDest, SEEK_SET);
	fwrite(&binSize, 1, 4, debOutf);
	fwrite(&binSize, 1, 4, resOutf);
	fwrite(cptSize + 0, 1, 4, debOutf);
	fwrite(cptSize + 1, 1, 4, resOutf);

	printf("%d diffs\n", diffNo);
	printf("%ld Compacts in total\n", numCpts);
	printf("max strlen = %d\n", maxStrl);
	printf("raw size = 2 * %ld\n", binSize);
	printf("max cptlen = %d\n", maxCptl);
}