-
Notifications
You must be signed in to change notification settings - Fork 14
/
DataFile.c
762 lines (702 loc) · 22.5 KB
/
DataFile.c
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "DataFile.h"
#include "Script.h"
#ifdef _WIN32
#define PSEP '\\'
#else
#define PSEP '/'
#endif
void ADF_close(ADF* a) {
AF_close(a->f);
}
ASI *ADF_get_global_script(ADF* a) {
return &a->globalscript;
}
ASI *ADF_get_dialog_script(ADF* a) {
return &a->dialogscript;
}
ASI *ADF_get_script(ADF* a, size_t index) {
if(index >= a->scriptcount) return 0;
return &a->scripts[index];
}
size_t ADF_get_scriptcount(ADF* a) {
return a->scriptcount;
}
typedef enum interaction_type {
it_char = 0,
it_inventory,
it_max
} interaction_type;
static int ADF_read_interaction(ADF *a, interaction_type t) {
/* deserialize_interaction_scripts */
static const size_t iter_start[it_max] = {
[it_char] = 0,
[it_inventory] = 1,
};
size_t *countmap[it_max] = {
[it_char] = &a->game.charactercount,
[it_inventory] = &a->game.inventorycount,
}, l = *countmap[t], i = iter_start[t];
char buf[200];
for(; i < l; i++) {
size_t j = 0, evcnt = AF_read_uint(a->f);
for(; j < evcnt; j++) {
if(!AF_read_string(a->f, buf, 200)) return 0; /* function names of interaction scripts */
}
}
return 1;
}
static int deserialize_command_list(ADF *a) {
size_t l = AF_read_uint(a->f);
AF_read_uint(a->f); /*timesrun*/
size_t i;
char childs[1024];
assert(l < sizeof(childs));
for(i = 0; i < l; i++) {
AF_read_uint(a->f); // unused
AF_read_uint(a->f); // type
size_t j = 0;
for(; j < 5; j++) {
char buf[4];
if(1 != AF_read(a->f, buf, 1)) // valType
return 0;
if(3 != AF_read(a->f, buf, 3)) // padding
return 0;
AF_read_uint(a->f); // val
AF_read_uint(a->f); // extra
}
childs[i] = !!AF_read_uint(a->f); // children
AF_read_uint(a->f); // parent
}
for(i = 0; i < l; i++) {
if(childs[i]) deserialize_command_list(a);
}
return 1;
}
static int ADF_read_interaction2x(ADF *a, interaction_type t) {
/* deserialize_new_interaction */
size_t *countmap[it_max] = {
[it_char] = &a->game.charactercount,
[it_inventory] = &a->game.inventorycount,
}, l = *countmap[t], i = 0;
for(; i < l; i++) {
int response[32];
if(AF_read_uint(a->f) != 1) continue;
size_t evcnt = AF_read_uint(a->f);
if(evcnt > 30) return 0;
AF_read_junk(a->f, evcnt * sizeof(int)); /*event types */
size_t j = 0;
for(; j < evcnt; j++)
response[j] = AF_read_int(a->f);
for(j = 0; j < evcnt; j++)
if(response[j]) {
if(!deserialize_command_list(a))
return 0;
}
}
return 1;
}
static int ADF_read_gamebase(ADF *a) {
/* acroom.h: 2881. void ReadFile() */
size_t l;
unsigned x;
char game_name[52];
l = 50 /* game name */ + 2 /*padding*/;
if(!AF_read(a->f, game_name, l)) return 0;
/* 100 options */
if(!AF_read_junk(a->f, 100*4)) return 0;
l = 256; /* 256 "paluses", unsigned char*/
if(!AF_read_junk(a->f, l)) return 0;
l = 256 * 4; /*sizeof color, read into defpal*/
if(!AF_read_junk(a->f, l)) return 0;
a->game.viewcount = AF_read_uint(a->f);
a->game.charactercount = AF_read_uint(a->f);
AF_read_uint(a->f); /* character of player*/
AF_read_uint(a->f); /* totalscore*/
a->game.inventorycount = AF_read_ushort(a->f);
AF_read_ushort(a->f); /* padding */
a->game.dialogcount = AF_read_uint(a->f);
AF_read_uint(a->f); /* numdlgmessage*/
a->game.fontcount = AF_read_uint(a->f);
a->game.color_depth = AF_read_uint(a->f); /* color depth - offset 0x710 into game28.dta */
AF_read_uint(a->f); /* target_win */
AF_read_uint(a->f);/* dialog_bullet */
AF_read_ushort(a->f);/* hotdot */
AF_read_ushort(a->f);/* hotdotouter */
AF_read_uint(a->f);/* uniqueid */
AF_read_uint(a->f);/* numgui */
a->game.cursorcount = AF_read_uint(a->f);
x = AF_read_uint(a->f);/* default_resolution */
if(a->version >= 43 /* 3.3.0 */ && x == 8 /* custom resolution */) {
/* 2 ints with the custom width and height */
if(!AF_read_junk(a->f, 8)) return 0;
}
AF_read_uint(a->f);/* default_lipsync_frame */
AF_read_uint(a->f);/* invhotdotsprite */
l = 4 * 17; /* reserved */
if(!AF_read_junk(a->f, l)) return 0;
a->game.globalmessagecount = 0;
{
int buf[500], n; /* 500 global message numbers */;
if(!AF_read(a->f, buf, sizeof(buf))) return 0;
for(n = 0; n < 500; ++n)
if(buf[n]) a->game.globalmessagecount++;
}
a->game.hasdict = !!AF_read_int(a->f);/* dict */
AF_read_uint(a->f);/* globalscript */
AF_read_uint(a->f);/* chars */
AF_read_uint(a->f);/* compiled_script */
return 1;
}
static int ADF_read_dictionary(ADF *a) {
/* acroom.h:1547 */
size_t i,l = AF_read_uint(a->f);
for(i = 0; i < l; i++) {
/* length of encrypted string */
size_t e = AF_read_uint(a->f);
if(!AF_read_junk(a->f, e)) return 0;
AF_read_short(a->f); /* wordnum */
}
return 1;
}
static int ADF_read_view(ADF *a) {
size_t i, numloops = AF_read_ushort(a->f);
for(i=0; i<numloops; ++i) {
size_t numframes = AF_read_ushort(a->f);
size_t l = 4 /* flags */ + numframes * (4/*pic*/+2/*xoffs*/+2/*yoffs*/+2/*speed*/+2/*padding*/+4/*flags*/+4/*sound*/+8/*reserved*/);
if(!AF_read_junk(a->f, l)) return 0;
}
return 1;
}
static int ADF_read_view2x(ADF *a) {
size_t l = 2 /* numloops */ + (16*2) /* numframes */
+ 2 /* padding? */ + (16*4) /* loopflags */
+ (16*20*(4/*pic*/+2/*xoffs*/+2/*yoffs*/+2/*speed*/+2/*padding*/+4/*flags*/+4/*sound*/+8/*reserved*/)) /* viewframes */;
return AF_read_junk(a->f, l);
}
static int ADF_read_characters(ADF *a) {
#define MAX_INV 301
#define MAX_SCRIPT_NAME_LEN 20
struct CharacterInfo {
int defview;
int talkview;
int view;
int room, prevroom;
int x, y, wait;
int flags;
short following;
short followinfo;
int idleview;
short idletime, idleleft;
short transparency;
short baseline;
int activeinv;
int talkcolor;
int thinkview;
short blinkview, blinkinterval;
short blinktimer, blinkframe;
short walkspeed_y, pic_yoffs;
int z;
int walkwait;
short speech_anim_speed, reserved1;
short blocking_width, blocking_height;
int index_id;
short pic_xoffs, walkwaitcounter;
short loop, frame;
short walking, animating;
short walkspeed, animspeed;
short inv[MAX_INV];
short actx, acty;
char name[40];
char scrname[MAX_SCRIPT_NAME_LEN];
char on;
} character;
size_t i;
a->characternames = malloc(a->game.charactercount*sizeof(char*));
a->characterscriptnames = malloc(a->game.charactercount*sizeof(char*));
for(i=0; i<a->game.charactercount; ++i) {
if(sizeof(character) != AF_read(a->f, &character, sizeof(character))) return 0;
if(strlen(character.name) >= sizeof(character.name)) return 0;
if(strlen(character.scrname) >= sizeof(character.scrname)) return 0;
a->characternames[i] = strdup(character.name);
a->characterscriptnames[i] = strdup(character.scrname);
}
return 1;
}
int ADF_find_datafile(const char *dir, char *fnbuf, size_t flen)
{
size_t l = strlen(dir);
if(l >= flen - 20) return 0;
memcpy(fnbuf, dir, l);
char* p = fnbuf + l;
*p = PSEP; p++;
memcpy(p, "game28.dta", sizeof("game28.dta"));
AF f;
if(!AF_open(&f, fnbuf)) {
memcpy(p, "ac2game.dta", sizeof("ac2game.dta"));
if(!AF_open(&f, fnbuf)) return 0;
}
AF_close(&f);
return 1;
}
int ADF_read_cursors(ADF* a) {
a->cursornames = malloc(a->game.cursorcount * sizeof(char*));
if(!a->cursornames) return 0;
unsigned i;
for(i=0; i<a->game.cursorcount; ++i) {
char buf[24];
if(24 != AF_read(a->f, buf, 24)) return 0;
if(buf[19] != 0) return 0;
a->cursornames[i] = strdup(buf+10);
}
return 1;
}
int ADF_read_dialogtopics(ADF *a) {
/* Common/acroom.h:2722 */
a->dialog_codesize = malloc(a->game.dialogcount*sizeof(short));
#define MAXTOPICOPTIONS 30
size_t i, l; // = (150*MAXTOPICOPTIONS)+(4*MAXTOPICOPTIONS)+4+(2*MAXTOPICOPTIONS)+2+2+4+4;
for(i=0; i<a->game.dialogcount; ++i) {
/* optionnames + optionflags + optionscripts + entrypoints*/
l = (150*MAXTOPICOPTIONS)+(4*MAXTOPICOPTIONS)+4+(2*MAXTOPICOPTIONS);
if(!AF_read_junk(a->f, l)) return 0;
unsigned short startupentrypoint = AF_read_ushort(a->f);
a->dialog_codesize[i] = AF_read_ushort(a->f);
if(!AF_read_junk(a->f, 8 /* numoptions+topicFlags*/)) return 0;
}
return 1;
}
static void dialog_decrypt_text(char *s, int len) {
unsigned i = 0;
while (i < len) {
*s -= "Avis Durgan"[i % 11];
if (!*s) break;
++i; ++s;
}
}
static int is_zeroterminated(char *s, size_t maxsize) {
char *p = s, *e = s + maxsize;
while(p < e) if(!*(p++)) return 1;
return 0;
}
#define MAX_GUIOBJ_SCRIPTNAME_LEN 25
#define MAX_GUIOBJ_EVENTHANDLER_LEN 30
static int ADF_read_gui_object(ADF *a, unsigned guiver) {
if(!AF_read_junk(a->f, guiver >= 119 ? 6*4 : 7*4)) return 0;
char buf[512]; /* arbitrary limit.
ags commit ed06eb64 made away with any length restrictions;
that was done when kGuiVersion_340 = 118 was current. */
size_t i;
if(guiver >= 106) {
if(!AF_read_string(a->f, buf, guiver >= 118 ? sizeof buf : MAX_GUIOBJ_SCRIPTNAME_LEN)) return 0;
}
if(guiver >= 108) {
unsigned numev = AF_read_uint(a->f);
for(i=0; i<numev; ++i) {
if(!AF_read_string(a->f, buf, guiver >= 118 ? sizeof buf : MAX_GUIOBJ_EVENTHANDLER_LEN+1)) return 0;
}
}
return 1;
}
static int AF_read_string_with_length(AF *f, char *buf, size_t maxlen) {
unsigned len = AF_read_uint(f);
if(len >= maxlen) return 0;
if(len && !AF_read(f, buf, len)) return 0;
buf[len] = 0;
return 1;
}
int ADF_read_guis(ADF *a) {
#define MAX_OBJS_ON_GUI 30
#define FAIL() goto fail
#if 0
volatile unsigned fail_line = 0;
#undef FAIL
#define FAIL() do { fail_line = __LINE__; goto fail; } while(0)
#endif
/* Engine/acgui.cpp:1471 */
unsigned x = AF_read_uint(a->f);
if(x != 0xCAFEBEEF) return 0;
int guiver, n = AF_read_uint(a->f);
if(n >= 100) {
guiver = n;
n = AF_read_uint(a->f);
} else {
guiver = 0;
}
if(n < 0 || n > 1000) return 0;
a->guicount = n;
a->guinames = malloc(sizeof(char*)*a->guicount);
size_t i;
for(i = 0; i < a->guicount; ++i) {
char buf[512];
if(guiver < 119 /* 3.5.0 */) {
if(!AF_read_junk(a->f, 4 /*vtext*/)) FAIL();
}
if(guiver >= 118) /* 3.4.0 */ {
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) FAIL();
} else {
if(16 != AF_read(a->f, buf, 16)) FAIL();
if(!is_zeroterminated(buf, 16)) FAIL();
}
if(!buf[0]) snprintf(buf, sizeof buf, "GUI%zu", i);
a->guinames[i] = strdup(buf);
if(guiver >= 118) {
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) FAIL();
} else {
if(!AF_read_junk(a->f, 20 /* clickEventHandler */)) FAIL();
}
if(!AF_read_junk(a->f, guiver<119 ? 5*4 : 4*4 )) FAIL();
unsigned n_ctrls = AF_read_uint(a->f);
if(guiver < 118)
n_ctrls = MAX_OBJS_ON_GUI;
// have read 6 of 27 legacy ints at this point
size_t l;
if(guiver >= 119)
l = (10+n_ctrls) * 4;
else if(guiver >= 118)
l = (21+n_ctrls) * 4;
else
l = 21*4 /* some ints */
+MAX_OBJS_ON_GUI*4+MAX_OBJS_ON_GUI*4;
if(!AF_read_junk(a->f, l)) FAIL();
}
unsigned n_buttons = AF_read_uint(a->f);
for(i = 0; i < n_buttons; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
char buf[512];
if(!AF_read_junk(a->f, guiver>=119? 9*4 : 12*4/*pic*/)) FAIL();
if(guiver < 119) {
if(!AF_read(a->f, buf, 50 /*text*/)) FAIL(); // for debugging
} else {
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) FAIL();
}
if(guiver >= 119) {
if(!AF_read_junk(a->f, 4 /*alignment*/)) FAIL();
} else if(guiver >= 111) {
if(!AF_read_junk(a->f, 4+4 /*alignment,reserved*/)) FAIL();
}
}
unsigned n_labels = AF_read_uint(a->f);
for(i = 0; i < n_labels; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
unsigned textlen = guiver < 113 ? 200 : AF_read_uint(a->f);
if(!AF_read_junk(a->f, textlen + 4*3/*font*/)) FAIL();
}
unsigned n_invs = AF_read_uint(a->f);
for(i = 0; i < n_invs; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
if(guiver >= 109) {
if(!AF_read_junk(a->f, guiver >= 119 ? 3*4 : 4*4 /*charid,itemw,itemh,topindex*/)) FAIL();
}
}
if(guiver >= 100) {
unsigned n_sliders = AF_read_uint(a->f);
for(i = 0; i < n_sliders; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
unsigned n;
if(guiver >= 119) n = 6;
else if(guiver >= 104) n = 7;
else n = 4;
if(!AF_read_junk(a->f, 4*n)) FAIL();
}
}
if(guiver >= 101) {
unsigned n_texts = AF_read_uint(a->f);
for(i = 0; i < n_texts; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
if(guiver >= 119) {
char buf[2048];
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) FAIL();
if(!AF_read_junk(a->f, 3*4/*font*/)) FAIL();
} else {
if(!AF_read_junk(a->f, 200/*text*/+3*4/*font*/)) FAIL();
}
}
}
if(guiver >= 102) {
unsigned n_lists = AF_read_uint(a->f);
size_t l_add1 = guiver >= 119 ? 4 : (guiver >= 112 ? 8 : 0);
size_t l_add2 = guiver >= 107 ? 4 : 0;
for(i = 0; i < n_lists; ++i) {
if(!ADF_read_gui_object(a, guiver)) FAIL();
unsigned j, n_items = AF_read_uint(a->f);
if(!AF_read_junk(a->f, guiver>=119 ? 3*4 : 9*4)) FAIL();
unsigned flags = AF_read_uint(a->f);
if((l_add1 + l_add2) && !AF_read_junk(a->f, l_add1 + l_add2)) FAIL();
for(j = 0; j < n_items; ++j) {
char buf[1024];
if(!AF_read_string(a->f, buf, sizeof buf)) FAIL();
}
if(guiver >= 114 && guiver < 119 && (flags & 4/* GLF_SGINDEXVALID */)) {
if(!AF_read_junk(a->f, n_items*2)) FAIL();
}
}
}
return 1;
fail:
free(a->guinames);
a->guinames = 0;
a->guicount = 0;
return 0;
#undef FAIL
}
int ADF_read_custom_property(ADF *a) {
unsigned i, x, propver = AF_read_uint(a->f);
if(propver > 2) return 0;
x = AF_read_uint(a->f); /* numprops */
for(i=0;i<x;++i) {
if(propver == 1) {
char buf[500]; /* MAX_CUSTOM_PROPERTY_VALUE_LENGTH */
if(!AF_read_string(a->f, buf, 200)) return 0; // propname
if(!AF_read_string(a->f, buf, 500)) return 0; // propval
} else {
char buf[2048];
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) return 0;
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) return 0;
}
}
return 1;
}
const char *AOE2str(enum ADF_open_error e) {
static const char err[][12] = {
[AOE_success] = "success",
[AOE_open] = "open",
[AOE_read] = "read",
[AOE_sig] = "signature",
[AOE_header] = "header",
[AOE_gamebase] = "gamebase",
[AOE_cursors] = "cursors",
[AOE_interaction] = "interaction",
[AOE_dictionary] = "dictionary",
[AOE_script] = "script",
[AOE_view] = "view",
[AOE_character] = "character",
[AOE_lipsync] = "lipsync",
[AOE_msg] = "message",
[AOE_dialogtopic] = "dialogtopic",
[AOE_dialog] = "dialog",
[AOE_guis] = "guis",
[AOE_props] = "props",
[AOE_views] = "views",
[AOE_inventories] = "inventories",
};
return err[e];
}
enum ADF_open_error ADF_open(ADF* a, const char *filename) {
#define FAIL(E) do { aoe = E; goto err_close; } while(0)
#define ERR(E) do { return E; } while(0)
enum ADF_open_error aoe = AOE_success;
char fnbuf[512];
size_t l, i;
memset(a, 0, sizeof(*a));
a->f = &a->f_b;
if(!AF_open(a->f, filename)) return AOE_open;
if(30 != AF_read(a->f, fnbuf, 30)) {
aoe = AOE_read;
err_close:
AF_close(a->f);
return aoe;
}
if(memcmp("Adventure Creator Game File v2", fnbuf, 30)) FAIL(AOE_sig);
/* the version read here is called kGameVersion_xxx in recent AGS
engine codebase, e.g. kGameVersion_350 == 50 */
a->version = AF_read_int(a->f);
/* here comes some version string with minor, major - we dont need it */
/* FIXME? newer ags does this only with version >= 12 */
/* 4 bytes containing the length of the string, followed by the string */
l = AF_read_uint(a->f);
if(l > 20) FAIL(AOE_header);
if(l != (size_t) AF_read(a->f, fnbuf, l)) goto err_close;
/* main_game_file.cpp:OpenMainGameFileBase */
if(a->version >= 48 /* kGameVersion_341 */) {
/* latest ags now has placed an int here: number of required capabilities */
l = AF_read_uint(a->f);
/* followed by l int/string pairs */
for(i=0; i<l; i++) {
size_t len = AF_read_uint(a->f);
if(!AF_read_junk(a->f, len)) FAIL(AOE_header);
}
}
if(!ADF_read_gamebase(a)) FAIL(AOE_gamebase);
if(a->version > 32) {
/* we're at line 11798 in Engine/ac.cpp, git revision:
205c56d693d903516b7b21beb454251e9489aabf - which is highly
recommended as the old C-style spaghetti code is more
readable than the current refactored OOP version. */
l = 40 /* guid */ + 20 /*savegame extension*/ + 50 /*savegame dir*/;
if(l != (size_t) AF_read(a->f, fnbuf, l)) FAIL(AOE_gamebase);
}
if(a->version < 50 /* 3.5.0 */) {
l = a->game.fontcount * 2; /* fontflags and fontoutline arrays [each 1b/font] */
if(!AF_read_junk(a->f, l)) FAIL(AOE_gamebase);
if(a->version >= 48) {
/* version == 3.4.1 have YOffset and versions >= 3.4.1.2 < 3.5.0 have YOffeset and LineSpacing ints */
l = a->game.fontcount * 4;
if(a->version == 49) l*=2;
if(!AF_read_junk(a->f, l)) FAIL(AOE_gamebase);
}
} else {
/* 3.5.0 has 5 ints per font, see gamesetupstruct.cpp */
l = a->game.fontcount * 4 * 5;
if(!AF_read_junk(a->f, l)) FAIL(AOE_gamebase);
}
// number of sprites
l = a->numsprites = (a->version < 24) ? 6000 : AF_read_uint(a->f);
a->spriteflagsstart = AF_get_pos(a->f);
// array of spriteflags (1 char each, max: MAX_SPRITES==30000)
if(!AF_read_junk(a->f, l)) FAIL(AOE_gamebase);
l = 68 * a->game.inventorycount; /* sizeof(InventoryItemInfo) */
if(!AF_read_junk(a->f, l)) FAIL(AOE_gamebase);
if(!ADF_read_cursors(a)) FAIL(AOE_cursors);
if(a->version > 32) {
if(!ADF_read_interaction(a, it_char)) FAIL(AOE_interaction);
if(!ADF_read_interaction(a, it_inventory)) FAIL(AOE_interaction);
} else {
if(!ADF_read_interaction2x(a, it_char)) FAIL(AOE_interaction);
if(!ADF_read_interaction2x(a, it_inventory)) FAIL(AOE_interaction);
a->globalvarcount = AF_read_uint(a->f);
l = 28 /* sizeof(InteractionVariable)*/ * a->globalvarcount;
if(!AF_read_junk(a->f, l)) FAIL(AOE_interaction);
}
if(a->game.hasdict)
if(!ADF_read_dictionary(a)) FAIL(AOE_dictionary);
a->scriptstart = AF_get_pos(a->f);
if(!ASI_read_script(a->f, &a->globalscript)) FAIL(AOE_script);
if(a->version > 37)
if(!ASI_read_script(a->f, &a->dialogscript)) FAIL(AOE_script);
if(a->version >= 31) {
a->scriptcount = AF_read_uint(a->f);
for(l = 0; l < a->scriptcount; l++)
if(!ASI_read_script(a->f, &a->scripts[l])) FAIL(AOE_script);
}
a->scriptend = AF_get_pos(a->f);
if(a->version > 32) {
for(l = 0; l < a->game.viewcount; l++)
if(!ADF_read_view(a)) FAIL(AOE_view);
} else {
for(l = 0; l < a->game.viewcount; l++)
if(!ADF_read_view2x(a)) FAIL(AOE_view);
}
if(a->version <= 19) {
/* skip version <= 2.51 unknown data */
l = AF_read_uint(a->f) * 0x204;
if(!AF_read_junk(a->f, l)) FAIL(AOE_view);
}
/* ... we are around line 11977 in ac.cpp at this point*/
if(!ADF_read_characters(a)) ERR(AOE_character);
if(a->version > 19 /* 2.51*/) // current AGS code says lipsync was added in 2.54==21
if(!AF_read_junk(a->f, 50*20/*MAXLIPSYNCFRAMES*/)) ERR(AOE_lipsync);
if(a->version < 26) {
for(l = 0; l < a->game.globalmessagecount; ++l) {
char buf[512];
if(!AF_read_string(a->f, buf, sizeof buf)) ERR(AOE_msg);
}
} else {
for(l = 0; l < a->game.globalmessagecount; ++l) {
/* length of encrypted string */
size_t e = AF_read_uint(a->f);
if(!AF_read_junk(a->f, e)) ERR(AOE_msg);
}
}
if(!ADF_read_dialogtopics(a)) ERR(AOE_dialogtopic);
/* Engine/ac.cpp:12045 */
a->old_dialogscripts = 0;
if(a->version <= 37) {
a->old_dialogscripts = malloc(a->game.dialogcount*sizeof(char*));
for(i = 0; i<a->game.dialogcount; ++i) {
AF_read_junk(a->f, a->dialog_codesize[i]);
l = AF_read_int(a->f);
char* buf = malloc(l);
AF_read(a->f, buf, l);
dialog_decrypt_text(buf, l);
a->old_dialogscripts[i] = buf;
//ASI scr;
//ASI_read_script(a, &scr);
}
if(a->version <= 25) {
// unencrypted dialog lines
// we just seek till end marker
// FIXME handle EOF
int c;
while((c = AF_read_uchar(a->f)) != 0xef);
AF_set_pos(a->f, AF_get_pos(a->f) -1);
} else {
// encrypted dialog lines
while(1) {
l = AF_read_uint(a->f);
if(l == 0xCAFEBEEF) break;
if(!AF_read_junk(a->f, l)) ERR(AOE_dialog);
}
AF_set_pos(a->f, AF_get_pos(a->f) -4);
}
}
if(!ADF_read_guis(a)) ERR(AOE_guis);
if(a->version >= 25) {
unsigned prop_version, x = AF_read_uint(a->f);
if(x != 1) ERR(AOE_props);
x = AF_read_uint(a->f); /* numplugins */
for(i = 0; i < x; ++i) {
char buf[80];
if(!AF_read_string(a->f, buf, sizeof buf)) ERR(AOE_props);
unsigned psize = AF_read_uint(a->f);
AF_read_junk(a->f, psize); /* plugin content */
}
/* CustomPropertySchema::UnSerialize */
prop_version = AF_read_uint(a->f);
if(prop_version > 2) ERR(AOE_props);
x = AF_read_uint(a->f); /* numprops */
for(i=0; i<x; ++i) {
if(prop_version == 1) {
char buf[500]; /* MAX_CUSTOM_PROPERTY_VALUE_LENGTH */
/* note: that 20 might actually be 200,
because the AGS code reserves 200 for propname,
but then reads only 20 */
/* new AGS has a comment about it:
NOTE: for some reason the property name stored in schema object was limited to only 20 characters, while the custom properties map could hold up to 200. Whether this was an error or design choice is unknown. */
if(!AF_read_string(a->f, buf, 20)) ERR(AOE_props); // propname
if(!AF_read_string(a->f, buf, 100)) ERR(AOE_props); //propdesc
if(!AF_read_string(a->f, buf, 500)) ERR(AOE_props); //defvalue
int foo = AF_read_uint(a->f); /* proptype */
} else {
char buf[2048];
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) ERR(AOE_props);
AF_read_uint(a->f);
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) ERR(AOE_props);
if(!AF_read_string_with_length(a->f, buf, sizeof buf)) ERR(AOE_props);
}
}
for(i=0; i<a->game.charactercount; ++i)
if(!ADF_read_custom_property(a)) ERR(AOE_props);
for(i=0; i<a->game.inventorycount; ++i)
if(!ADF_read_custom_property(a)) ERR(AOE_props);
a->viewnames = malloc(a->game.viewcount * sizeof(char*));
for(i=0; i<a->game.viewcount; ++i) {
char buf[255+1]; /* was originally MAXVIEWNAMELENGTH aka 15 bytes, however "meaningless name restrictions for Views and InvItems" was removed in 8bb1bfa30610f3c3291029b2c05e6e5b37415269 */
if(!AF_read_string(a->f, buf, sizeof buf)) ERR(AOE_views);
if(!is_zeroterminated(buf, sizeof buf)) ERR(AOE_views);
a->viewnames[i] = strdup(buf);
}
if(a->version >= 31) { // 2.7.0
a->inventorynames = malloc(a->game.inventorycount*sizeof(char*));
for(i=0; i<a->game.inventorycount; ++i) {
char buf[20]; /* MAX_SCRIPT_NAME_LEN */
if(!AF_read_string(a->f, buf, sizeof buf)) ERR(AOE_inventories);
if(!is_zeroterminated(buf, sizeof buf)) ERR(AOE_inventories);
a->inventorynames[i] = strdup(buf);
}
if(a->version >= 32) { // 2.7.2
/* here follow dialog script names */
}
}
}
/* prevent usage of uninitialized data */
if(!a->inventorynames) a->game.inventorycount = 0;
if(!a->viewnames) a->game.viewcount = 0;
/* at this point we have everything we need */
return AOE_success;
#undef FAIL
#undef ERR
}