Debian Codegraph @

skycpt/cptcompiler.cpp

raw
1 #include "stdafx.h"
2 #include "cpthelp.h"
3 #include "textfile.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 uint32 crop(char *line);
9 uint16 findCptId(char *name, TextFile *cptFile);
10
11 #define MAX_CPTS 0xA000
12 #define MAX_OBJ_SIZE (0x2000 * 2)
13 #define NUM_DATA_LISTS 9
14 #define ASCII_SIZE (65536 * 2)
15
16 enum CptType {
17 PTR_NULL = 0,
18 COMPACT,
19 TURNTAB,
20 ANIMSEQ,
21 MISCBIN,
22 GETTOTAB,
23 ROUTEBUF,
24 MAINLIST
25 };
26
27 void processMainLists(FILE *inf, CptObj *destArr, uint16 *idList) {
28 char line[1024];
29 dofgets(line, 1024, inf);
30 assert(lineMatchSection(line, "MAINLISTS"));
31 uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
32 uint32 idNum = 0;
33 do {
34 dofgets(line, 1024, inf);
35 if (!isEndOfSection(line)) {
36 char cptName[50];
37 uint16 id = getInfo(line, "MAINLST", cptName);
38 CptObj *dest = destArr + id;
39 assertEmpty(dest);
40 dest->type = MAINLIST;
41 dest->dbgName = (char*)malloc(strlen(cptName) + 1);
42 strcpy(dest->dbgName, cptName);
43 memset(resBuf, 0, MAX_OBJ_SIZE);
44 uint32 resPos = 0;
45 idList[idNum] = id;
46 idNum++;
47
48 do {
49 dofgets(line, 1024, inf);
50 if (!isEndOfObject(line, "MAINLST", id)) {
51 assert((line[0] == '\t') && (line[1] == '\t'));
52 char *stopCh;
53 uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
54 assert(stopCh == (line + 6));
55 assert((stopCh[0] == ':') && (stopCh[1] == ':'));
56 resBuf[resPos] = destId;
57 resPos++;
58 } else
59 break;
60 } while (1);
61 assert(resPos < (MAX_OBJ_SIZE / 2));
62 dest->len = resPos;
63 dest->data = (uint16*)malloc(resPos * 2);
64 memcpy(dest->data, resBuf, resPos * 2);
65 } else
66 break;
67 } while(1);
68
69 free(resBuf);
70 }
71
72 void processCpts(FILE *inf, CptObj *destArr) {
73 char line[1024];
74 dofgets(line, 1024, inf);
75 assert(lineMatchSection(line, "COMPACTS"));
76 uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
77 do {
78 dofgets(line, 1024, inf);
79 if (!isEndOfSection(line)) {
80 char cptName[50];
81 uint16 id = getInfo(line, "COMPACT", cptName);
82 CptObj *dest = destArr + id;
83 assertEmpty(dest);
84 dest->dbgName = (char*)malloc(strlen(cptName) + 1);
85 dest->type = COMPACT;
86 strcpy(dest->dbgName, cptName);
87 memset(resBuf, 0, MAX_OBJ_SIZE);
88 uint32 resPos = 0;
89
90 do {
91 dofgets(line, 1024, inf);
92 if (!isEndOfObject(line, "COMPACT", id)) {
93 assert((line[0] == '\t') && (line[1] == '\t'));
94 char *stopCh;
95 uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
96 assert(stopCh != (line + 2));
97 assert((stopCh[0] == '-') && (stopCh[1] == '>'));
98 if (resPos == 23) { // grafixProg
99 assert(destId == 0);
100 resBuf[resPos] = resBuf[resPos + 1] = 0;
101 resPos += 2;
102 } else if (resPos == 48) { // turnProg. shouldn't it be 49?
103 assert(destId == 0);
104 resBuf[resPos] = resBuf[resPos + 1] = 0;
105 resPos += 2;
106 } else {
107 resBuf[resPos] = destId;
108 resPos++;
109 }
110 } else
111 break;
112 } while (1);
113 assert(resPos < (MAX_OBJ_SIZE / 2));
114 dest->len = resPos;
115 dest->data = (uint16*)malloc(resPos * 2);
116 memcpy(dest->data, resBuf, resPos * 2);
117 } else
118 break;
119 } while(1);
120
121 free(resBuf);
122 }
123
124 void processTurntabs(FILE *inf, CptObj *destArr) {
125 char line[1024];
126 dofgets(line, 1024, inf);
127 assert(lineMatchSection(line, "TURNTABS"));
128 uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
129 do {
130 dofgets(line, 1024, inf);
131 if (!isEndOfSection(line)) {
132 char cptName[50];
133 uint16 id = getInfo(line, "TURNTAB", cptName);
134 CptObj *dest = destArr + id;
135 assertEmpty(dest);
136 dest->dbgName = (char*)malloc(strlen(cptName) + 1);
137 dest->type = TURNTAB;
138 strcpy(dest->dbgName, cptName);
139 memset(resBuf, 0, MAX_OBJ_SIZE);
140 uint32 resPos = 0;
141
142 do {
143 dofgets(line, 1024, inf);
144 if (!isEndOfObject(line, "TURNTAB", id)) {
145 assert((line[0] == '\t') && (line[1] == '\t'));
146 char *stopCh;
147 uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
148 assert(stopCh == (line + 6));
149 assert((stopCh[0] == '-') && (stopCh[1] == '>'));
150 resBuf[resPos] = destId;
151 resPos++;
152 } else
153 break;
154 } while (1);
155 assert(resPos < (MAX_OBJ_SIZE / 2));
156 dest->len = resPos;
157 dest->data = (uint16*)malloc(resPos * 2);
158 memcpy(dest->data, resBuf, resPos * 2);
159 } else
160 break;
161 } while(1);
162
163 free(resBuf);
164 }
165
166 void processBins(FILE *inf, CptObj *destArr, char *typeName, char *objName, uint8 cTypeId) {
167 char line[1024];
168 dofgets(line, 1024, inf);
169 assert(lineMatchSection(line, typeName));
170 uint16 *resBuf = (uint16*)malloc(MAX_OBJ_SIZE);
171 do {
172 dofgets(line, 1024, inf);
173 if (!isEndOfSection(line)) {
174 char cptName[50];
175 uint16 id = getInfo(line, objName, cptName);
176 CptObj *dest = destArr + id;
177 assertEmpty(dest);
178 dest->dbgName = (char*)malloc(strlen(cptName) + 1);
179 dest->type = cTypeId;
180 strcpy(dest->dbgName, cptName);
181 memset(resBuf, 0, MAX_OBJ_SIZE);
182 uint32 resPos = 0;
183
184 do {
185 dofgets(line, 1024, inf);
186 if (!isEndOfObject(line, objName, id)) {
187 assert((line[0] == '\t') && (line[1] == '\t'));
188 char *stopCh;
189 uint16 destId = (uint16)strtoul(line + 2, &stopCh, 16);
190 assert(stopCh == (line + 6));
191 assert(*stopCh == '\0');
192 resBuf[resPos] = destId;
193 resPos++;
194 } else
195 break;
196 } while (1);
197 assert(resPos < (MAX_OBJ_SIZE / 2));
198 dest->len = resPos;
199 dest->data = (uint16*)malloc(resPos * 2);
200 memcpy(dest->data, resBuf, resPos * 2);
201 } else
202 break;
203 } while(1);
204
205 free(resBuf);
206 }
207
208 uint16 dlinkCount = 0;
209 static uint16 dlinks[1024];
210 static char* dlinkNames[512];
211
212 void processSymlinks(FILE *inf, CptObj *destArr, uint16 *baseLists) {
213 char line[1024];
214 dofgets(line, 1024, inf);
215 assert(lineMatchSection(line, "SYMLINKS"));
216 do {
217 dofgets(line, 1024, inf);
218 if (!isEndOfSection(line)) {
219 char cptName[50];
220 uint16 fromId = getInfo(line, "SYMLINK", cptName);
221 CptObj *from = destArr + fromId;
222 assertEmpty(from);
223 dlinkNames[dlinkCount] = (char*)malloc(strlen(cptName) + 1);
224 strcpy(dlinkNames[dlinkCount], cptName);
225
226 dofgets(line, 1024, inf);
227 assert((line[0] == '\t') && (line[1] == '\t') && (line[2] == '-') && (line[3] == '>'));
228 char *stopCh;
229 uint16 destId = (uint16)strtoul(line + 4, &stopCh, 16);
230 assert(stopCh == (line + 8));
231 assert((stopCh[0] == ':') && (stopCh[1] == ':'));
232
233 dlinks[dlinkCount * 2 + 0] = fromId;
234 dlinks[dlinkCount * 2 + 1] = destId;
235
236 dlinkCount++;
237
238 dofgets(line, 1024, inf);
239 assert(isEndOfObject(line, "SYMLINK", fromId));
240 } else
241 break;
242 } while(1);
243 }
244
245 void doCompile(FILE *inf, FILE *debOutf, FILE *resOutf, TextFile *cptDef, FILE *sve) {
246 uint16 maxStrl = 0;
247 uint16 maxCptl = 0;
248
249 printf("Processing...\n");
250 CptObj *resCpts;
251 uint16 baseLists[NUM_DATA_LISTS];
252 memset(baseLists, 0, NUM_DATA_LISTS * 2);
253 resCpts = (CptObj*)malloc(MAX_CPTS * sizeof(CptObj));
254 memset(resCpts, 0, MAX_CPTS * sizeof(CptObj));
255 printf(" MainLists...\n");
256 processMainLists(inf, resCpts, baseLists);
257 printf(" Compacts...\n");
258 processCpts(inf, resCpts);
259 printf(" Turntables...\n");
260 processTurntabs(inf, resCpts);
261 printf(" Animation tables...\n");
262 processBins(inf, resCpts, "ANIMSEQS", "ANIMSEQ", ANIMSEQ);
263 printf(" Unknown binaries...\n");
264 processBins(inf, resCpts, "MISCBINS", "MISCBIN", MISCBIN);
265 printf(" Get To tables...\n");
266 processBins(inf, resCpts, "GETTOTAB", "GET_TOS", GETTOTAB);
267 printf(" Scratch buffers...\n");
268 processBins(inf, resCpts, "SCRATCHR", "SCRATCH", ROUTEBUF);
269 printf(" Symbolic links...\n");
270 processSymlinks(inf, resCpts, baseLists);
271 printf("Converting to binary data...\n");
272 uint32 numCpts = 1;
273 for (uint32 cnt = 1; cnt < MAX_CPTS; cnt++)
274 if (resCpts[cnt].data || resCpts[cnt].dbgName || resCpts[cnt].len)
275 numCpts++;
276
277 uint16 dataListLen[NUM_DATA_LISTS];
278 for (uint32 cnt = 0; cnt < NUM_DATA_LISTS; cnt++)
279 for (uint16 elemCnt = 0; elemCnt < 0x1000; elemCnt++) {
280 uint32 id = (cnt << 12) | elemCnt;
281 if (resCpts[id].data || resCpts[id].dbgName || resCpts[id].len)
282 dataListLen[cnt] = elemCnt + 1;
283 }
284
285 // write the header
286 uint32 rev = 0;
287 fwrite(&rev, 2, 1, debOutf);
288 fwrite(&rev, 2, 1, resOutf);
289 rev = NUM_DATA_LISTS;
290 fwrite(&rev, 2, 1, debOutf);
291 fwrite(&rev, 2, 1, resOutf);
292 for (uint32 cnt = 0; cnt < NUM_DATA_LISTS; cnt++) {
293 fwrite(dataListLen + cnt, 2, 1, debOutf);
294 fwrite(dataListLen + cnt, 2, 1, resOutf);
295 }
296
297 uint32 binSize = 0;
298 uint32 binDest = ftell(debOutf);
299 fwrite(&binSize, 1, 4, debOutf);
300 fwrite(&binSize, 1, 4, resOutf);
301 fwrite(&binSize, 1, 4, debOutf);
302 fwrite(&binSize, 1, 4, resOutf);
303
304 char *asciiBuf = (char*)malloc(ASCII_SIZE);
305 char *asciiPos = asciiBuf;
306
307 // now process all the compacts
308 uint32 cptSize[2];
309 cptSize[0] = ftell(debOutf);
310 cptSize[1] = ftell(resOutf);
311 for (uint32 lcnt = 0; lcnt < NUM_DATA_LISTS; lcnt++) {
312 for (uint32 eCnt = 0; eCnt < dataListLen[lcnt]; eCnt++) {
313 uint32 cId = (lcnt << 12) | eCnt;
314 CptObj *cpt = resCpts + cId;
315 if (resCpts[cId].data || resCpts[cId].dbgName || resCpts[cId].len || resCpts[cId].type) {
316 strcpy(asciiPos, cpt->dbgName);
317 asciiPos += strlen(cpt->dbgName) + 1;
318
319 assert(cpt->len < 0xFFFF);
320 uint16 dlen = (uint16)cpt->len;
321 if (dlen > maxCptl)
322 maxCptl = dlen;
323 binSize += dlen;
324 assert(dlen != 0);
325
326 fwrite(&dlen, 2, 1, debOutf);
327 fwrite(&dlen, 2, 1, resOutf);
328
329 uint16 field = resCpts[cId].type;
330 fwrite(&field, 2, 1, debOutf);
331
332 fwrite(cpt->data, 2, dlen, debOutf);
333 fwrite(cpt->data, 2, dlen, resOutf);
334 } else {
335 uint16 tmp = 0;
336 fwrite(&tmp, 2, 1, debOutf);
337 fwrite(&tmp, 2, 1, resOutf);
338 }
339 }
340 }
341 cptSize[0] = ftell(debOutf) - cptSize[0];
342 cptSize[1] = ftell(resOutf) - cptSize[1];
343 assert(!(cptSize[0] & 1));
344 assert(!(cptSize[1] & 1));
345 cptSize[0] /= 2;
346 cptSize[1] /= 2;
347
348 for (uint32 cnt = 0; cnt < dlinkCount; cnt++) {
349 strcpy(asciiPos, dlinkNames[cnt]);
350 asciiPos += strlen(dlinkNames[cnt]) + 1;
351 }
352
353 uint32 asciiSize = (uint32)(asciiPos - asciiBuf);
354 fwrite(&asciiSize, 1, 4, debOutf);
355 fwrite(asciiBuf, 1, asciiSize, debOutf);
356
357 // the direct links...
358 fwrite(&dlinkCount, 2, 1, debOutf);
359 fwrite(&dlinkCount, 2, 1, resOutf);
360 for (uint32 cnt = 0; cnt < dlinkCount; cnt++) {
361 fwrite(dlinks + cnt * 2 + 0, 2, 1, debOutf);
362 fwrite(dlinks + cnt * 2 + 0, 2, 1, resOutf);
363
364 fwrite(dlinks + cnt * 2 + 1, 2, 1, debOutf);
365 fwrite(dlinks + cnt * 2 + 1, 2, 1, resOutf);
366 }
367 printf("Processing diff data...\n");
368 // 288 diffdata
369 FILE *dif = fopen("288diff.txt", "r");
370 assert(dif);
371 char line[1024];
372 uint16 diff[8192];
373 uint16 diffDest = 0;
374 uint16 diffNo = 0;
375 while (fgets(line, 1024, dif)) {
376 crop(line);
377 if (line[0] != '$') {
378 assert(memcmp(line, "data_", 5) == 0);
379 char *pos = line + 5;
380 char *stopCh;
381 uint16 lId = (uint16)strtoul(pos, &stopCh, 10);
382 assert(*stopCh == '[');
383 uint16 eId = (uint16)strtoul(stopCh + 1, &stopCh, 10);
384 assert((stopCh[0] == ']') && (stopCh[1] == '[') && (eId <= 0xFFF) && (lId <= 7));
385 uint16 id = (lId << 12) | eId;
386 uint16 elemNo = (uint16)strtoul(stopCh + 2, &stopCh, 10);
387 assert(*stopCh == ']');
388 stopCh = strstr(stopCh, "0x") + 2;
389 uint16 val = (uint16)strtoul(stopCh, &stopCh, 16);
390 assert(*stopCh == ';');
391 diff[diffDest++] = id;
392 diff[diffDest++] = elemNo;
393 diff[diffDest++] = 1;
394 diff[diffDest++] = val;
395 diffNo++;
396 } else {
397 char *pos = strchr(line, ' ');
398 *pos = '\0';
399 uint16 id = findCptId(line + 1, cptDef);
400 assert(id);
401 diff[diffDest++] = id;
402 diff[diffDest++] = 0;
403 pos++;
404 uint16 len = (uint16)strtoul(pos, &pos, 10);
405 diff[diffDest++] = len;
406 assert(len);
407 assert(resCpts[id].len == len);
408 for (uint16 cnt = 0; cnt < len; cnt++) {
409 assert(*pos == ' ');
410 pos++;
411 diff[diffDest++] = (uint16)strtoul(pos, &pos, 16);
412 }
413 assert(diff[diffDest - 1] == 0xFFFF);
414 diffNo++;
415 }
416 }
417 assert(diffDest <= 8192);
418 fwrite(&diffNo, 1, 2, debOutf);
419 fwrite(&diffDest, 1, 2, debOutf);
420 fwrite(diff, 2, diffDest, debOutf);
421 fwrite(&diffNo, 1, 2, resOutf);
422 fwrite(&diffDest, 1, 2, resOutf);
423 fwrite(diff, 2, diffDest, resOutf);
424
425 printf("Converting Save data...\n");
426 // the IDs of the compacts to be saved
427 char cptName[1024];
428 uint16 saveIds[2048];
429 uint16 numIds = 0;
430 while (fgets(cptName, 1024, sve)) {
431 crop(cptName);
432 uint16 resId = findCptId(cptName, cptDef);
433 if (!resId)
434 printf("ERROR: Can't find definition of %s\n", cptName);
435 else {
436 saveIds[numIds] = resId;
437 numIds++;
438 }
439 }
440 printf("%d saveIds\n", numIds);
441 fwrite(&numIds, 2, 1, debOutf);
442 fwrite(saveIds, 2, numIds, debOutf);
443 fwrite(&numIds, 2, 1, resOutf);
444 fwrite(saveIds, 2, numIds, resOutf);
445
446 printf("Converting Reset data...\n");
447 // now append the reset data
448 uint16 gameVers[7] = { 303, 331, 348, 365, 368, 372, 288 };
449 // make sure all files exist
450 bool filesExist = true;
451 char inName[32];
452 for (int i = 0; i < 7; i++) {
453 sprintf(inName, "reset.%03d", gameVers[i]);
454 FILE *test = fopen(inName, "rb");
455 if (test)
456 fclose(test);
457 else {
458 filesExist = false;
459 printf("File %s not found\n", inName);
460 }
461 }
462
463 if (filesExist) {
464 FILE *res288 = fopen("RESET.288", "rb");
465 fseek(res288, 0, SEEK_END);
466 assert((ftell(res288) / 2) < 65536);
467 uint16 resSize = (uint16)(ftell(res288) / 2);
468 fseek(res288, 0, SEEK_SET);
469 uint16 *buf288 = (uint16*)malloc(resSize * 2);
470 fread(buf288, 2, resSize, res288);
471 fclose(res288);
472 fwrite(&resSize, 1, 2, debOutf);
473 fwrite(buf288, 2, resSize, debOutf);
474
475 uint16 tmp = 7;
476 fwrite(&tmp, 2, 1, debOutf);
477 tmp = 288;
478 fwrite(&tmp, 2, 1, debOutf);
479 tmp = 0;
480 fwrite(&tmp, 2, 1, debOutf);
481 printf("reset destination: %d\n", ftell(debOutf));
482 for (int cnt = 0; cnt < 6; cnt++) {
483 uint16 diff[8192];
484 uint16 diffPos = 0;
485 sprintf(inName, "reset.%03d", gameVers[cnt]);
486 FILE *resDiff = fopen(inName, "rb");
487 fseek(resDiff, 0, SEEK_END);
488 assert(ftell(resDiff) == (resSize * 2));
489 fseek(resDiff, 0, SEEK_SET);
490 uint16 *bufDif = (uint16*)malloc(resSize *2);
491 fread(bufDif, 2, resSize, resDiff);
492 fclose(resDiff);
493 for (uint16 eCnt = 0; eCnt < resSize; eCnt++)
494 if (buf288[eCnt] != bufDif[eCnt]) {
495 diff[diffPos++] = eCnt;
496 diff[diffPos++] = bufDif[eCnt];
497 }
498 free(bufDif);
499 fwrite(gameVers + cnt, 1, 2, debOutf);
500 assert(!(diffPos & 1));
501 diffPos /= 2;
502 fwrite(&diffPos, 1, 2, debOutf);
503 fwrite(diff, 2, 2 * diffPos, debOutf);
504 printf("diff v0.0%03d: 2 * 2 * %d\n", gameVers[cnt], diffPos);
505 }
506 } else {
507 printf("Creating CPT file with Dummy reset data @ %d\n", ftell(debOutf));
508 uint16 resetFields16 = 4;
509 fwrite(&resetFields16, 2, 1, debOutf);
510 uint32 blah = 8;
511 fwrite(&blah, 4, 1, debOutf); // size field: 8 bytes
512 blah = (uint32)-1;
513 fwrite(&blah, 4, 1, debOutf); // save file revision. -1 is unknown to scummvm, so it'll refuse to load it.
514 resetFields16 = 0;
515 fwrite(&resetFields16, 2, 1, debOutf); // numDiffs: 0, no further reset blocks.
516 }
517
518 // now fill the raw-compact-data-size header field
519 fseek(resOutf, binDest, SEEK_SET);
520 fseek(debOutf, binDest, SEEK_SET);
521 fwrite(&binSize, 1, 4, debOutf);
522 fwrite(&binSize, 1, 4, resOutf);
523 fwrite(cptSize + 0, 1, 4, debOutf);
524 fwrite(cptSize + 1, 1, 4, resOutf);
525
526 printf("%d diffs\n", diffNo);
527 printf("%d Compacts in total\n", numCpts);
528 printf("max strlen = %d\n", maxStrl);
529 printf("raw size = 2 * %d\n", binSize);
530 printf("max cptlen = %d\n", maxCptl);
531 }