forked from entropia/tip-toi-reveng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameanalyse.c
969 lines (866 loc) · 22.1 KB
/
gameanalyse.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define DUMP_BLOCKS 0
char *gme;
uint16_t start_oid;
uint16_t end_oid;
uint16_t count_games;
uint16_t count_media;
uint32_t file_size;
uint32_t end_game;
uint32_t unknown_table;
uint32_t media_table;
void prepare_tests(FILE * file)
{
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
uint32_t p_script_table;
fread(&p_script_table, sizeof(p_script_table), 1, file);
fseek(file, p_script_table, SEEK_SET);
fread(&end_oid, sizeof(end_oid), 1, file);
fseek(file, 2, SEEK_CUR);
fread(&start_oid, sizeof(start_oid), 1, file);
fseek(file, 4, SEEK_SET);
uint32_t p_media_table;
fread(&p_media_table, sizeof(p_media_table), 1, file);
media_table = p_media_table;
fseek(file, 0x18, SEEK_SET);
uint32_t p_unknown_table;
fread(&p_unknown_table, sizeof(p_unknown_table), 1, file);
unknown_table = p_unknown_table;
end_game = p_unknown_table;
uint32_t p_media_start, v_media_length, p_media_next;
do {
fseek(file,
p_media_table + count_media++ * 2 * sizeof(p_media_start),
SEEK_SET);
fread(&p_media_start, sizeof(p_media_start), 1, file);
fread(&v_media_length, sizeof(v_media_length), 1, file);
fread(&p_media_next, sizeof(p_media_next), 1, file);
}
while (p_media_start + v_media_length == p_media_next);
}
int
test_list_16(FILE * file, uint32_t start, uint16_t v_begin, uint16_t v_end)
{
int result = 0;
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
uint16_t i;
for (i = 0; i < count; i++) {
uint16_t value;
fseek(file, start + 2 + i * sizeof(value), SEEK_SET);
fread(&value, sizeof(value), 1, file);
if ((value < v_begin) || (value > v_end)) {
result++;
}
}
if (count == 0)
result = -1;
return result;
}
int
test_list_32(FILE * file, uint32_t start, uint32_t v_begin, uint32_t v_end)
{
int result = 0;
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
uint16_t i;
for (i = 0; i < count; i++) {
uint32_t value;
fseek(file, start + 2 + i * sizeof(value), SEEK_SET);
fread(&value, sizeof(value), 1, file);
if ((value < v_begin) || (value > v_end)) {
result++;
}
}
if (count == 0)
result = -1;
return result;
}
int test_list_oid(FILE * file, uint32_t start)
{
return test_list_16(file, start, start_oid, end_oid);
}
int test_list_gid(FILE * file, uint32_t start)
{
return test_list_16(file, start, 0, count_games);
}
int test_list_media(FILE * file, uint32_t start)
{
return test_list_16(file, start, 0, count_media);
}
void save_block(char *name, uint32_t start, uint32_t size)
{
char out_name[2024];
snprintf(out_name, 2020, "%s_%s", gme, name);
FILE *out = fopen(out_name, "wb");
FILE *in = fopen(gme, "rb");
fseek(in, start, SEEK_SET);
unsigned char buffer = 0;
int i;
for (i = 0; i < size; i++) {
fread(&buffer, sizeof(buffer), 1, in);
fwrite(&buffer, sizeof(buffer), 1, out);
}
fclose(in);
fclose(out);
}
void parse_list_16(FILE * file, uint32_t start)
{
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("List (0x%X, %d) = [ ", start, count);
int i = 0;
for (i = 0; i < count; i++) {
fflush(stdout);
uint16_t id;
fseek(file, start + 2 + i * 2, SEEK_SET);
fread(&id, sizeof(id), 1, file);
printf("%3d, ", id);
}
printf(" ]\n");
}
void parse_playlist(FILE * file, uint32_t start)
{
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("\t\t\t\tPlaylist (0x%X, %d) = [ ", start, count);
int i = 0;
for (i = 0; i < count; i++) {
fflush(stdout);
uint16_t id;
fseek(file, start + 2 + i * 2, SEEK_SET);
fread(&id, sizeof(id), 1, file);
printf("%3d, ", id);
}
printf(" ]\n");
}
void parse_oidlist(FILE * file, uint32_t start)
{
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("\t\t\t\tOIDlist (0x%X, %d) = [ ", start, count);
int i = 0;
for (i = 0; i < count; i++) {
fflush(stdout);
uint16_t id;
fseek(file, start + 2 + i * 2, SEEK_SET);
fread(&id, sizeof(id), 1, file);
printf("%3d, ", id);
}
printf(" ]\n");
}
void parse_gameidlist(FILE * file, uint32_t start)
{
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("\t\t\t\tGamelist (0x%X, %d) = [ ", start, count);
int i = 0;
for (i = 0; i < count; i++) {
fflush(stdout);
uint16_t id;
fseek(file, start + 2 + i * 2, SEEK_SET);
fread(&id, sizeof(id), 1, file);
printf("%3d, ", id);
}
printf(" ]\n");
}
void
parse_playlistpointer(FILE * file, uint32_t start, uint32_t pbegin,
uint32_t pend)
{
uint16_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("\t\t\tPlaylistpointer (0x%X, %d)\n", start, count);
int i = 0;
int playerror = 0;
for (i = 0; i < count; i++) {
uint32_t pointer;
fseek(file, start + 2 + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
if ((pointer < pbegin) || (pointer > pend)) {
printf("\t\t\t\tPlaylist (0x%X) = PlayError!\n ", pointer);
playerror = 1;
} else {
parse_playlist(file, pointer);
}
}
if (playerror == 1) {
parse_playlist(file, start);
}
}
void
print_block(FILE * file, uint32_t start, uint32_t end, uint32_t bstart,
uint32_t bend)
{
uint32_t blocks[50001];
blocks[0] = start;
int bi = 1;
int i = 0;
do {
i++;
fseek(file, start + i, SEEK_SET);
uint32_t testp;
fread(&testp, sizeof(testp), 1, file);
if ((testp >= start) && (testp < end)) {
blocks[bi++] = testp;
}
}
while ((bi < 50000) && (i < (end - start - 3)));
if (bi < 50000) {
blocks[bi++] = end;
// sortieren
int z = 0;
for (i = 0; i < bi; i++) {
int j;
for (j = i + 1; j < bi; j++) {
if (blocks[i] > blocks[j]) {
uint32_t tmp = blocks[i];
blocks[i] = blocks[j];
blocks[j] = tmp;
}
if (blocks[i] == blocks[j] && (blocks[i] != 0xffffffff)) {
blocks[j] = 0xffffffff;
z++;
}
}
}
bi -= z;
// ausgeben
for (i = 0; i < (bi - 1); i++) {
printf("0x%X B%02d %06d\n\t", blocks[i], i,
blocks[i + 1] - blocks[i]);
uint32_t j;
int p = 0;
for (j = blocks[i]; j < blocks[i + 1]; j++) {
fseek(file, j, SEEK_SET);
uint8_t val;
fread(&val, sizeof(val), 1, file);
printf("%02X ", val);
if (p == 7) {
printf(" ");
}
if (p++ == 15) {
p = 0;
printf("\n\t");
}
}
printf("\n\t\t");
int coun = 0;
uint32_t last = 0;
int k = 0;
for (j = blocks[i]; j < blocks[i + 1]; j++) {
fseek(file, j, SEEK_SET);
uint32_t point;
fread(&point, sizeof(point), 1, file);
if ((point >= bstart) && (point < bend)) {
j += 3;
if (last < point) {
coun++;
last = point;
} else {
printf("LB%02d I%02d L%02d \n\t\t", i, k++, coun);
coun = 1;
last = 0;
}
} else {
if (coun > 0) {
printf("LB%02d I%02d L%02d \n\t\t", i, k++, coun);
coun = 0;
last = 0;
}
}
}
if (coun > 0) {
printf("LB%02d I%02d L%02d \n\t\t", i, k++, coun);
coun = 0;
last = 0;
}
printf("\n");
}
}
printf("EndSub\n");
}
void
parse_subgame(FILE * file, uint32_t start, uint32_t end, uint16_t type)
{
uint16_t index = 20;
fseek(file, start + index, SEEK_SET);
int i;
for (i = 0; i < 3; i++) {
uint16_t count;
fread(&(count), sizeof(count), 1, file);
printf("\t\tOID List (%d, %d) = [ ", i, count);
int j;
for (j = 0; j < count; j++) {
uint16_t oid;
fread(&oid, sizeof(oid), 1, file);
printf("%d, ", oid);
}
printf(" ]\n");
}
int pindex = ftell(file) - start;
for (i = 0; i < 9; i++) {
uint32_t pointer;
fseek(file, start + pindex + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
if (test_list_32(file, pointer, 0, file_size) > 0) {
printf("No Playlist pointer (%08X)\n", pointer);
} else {
parse_playlistpointer(file, pointer, 0, file_size);
}
}
uint16_t tmp;
fseek(file, start + index, SEEK_SET);
uint16_t count[3];
fread(&(tmp), sizeof(tmp), 1, file);
count[0] = tmp;
index += 2 + count[0] * 2;
fseek(file, start + index, SEEK_SET);
fread(&(tmp), sizeof(tmp), 1, file);
count[1] = tmp;
index += 2 + count[1] * 2;
fseek(file, start + index, SEEK_SET);
fread(&(tmp), sizeof(tmp), 1, file);
count[2] = tmp;
index += 2 + count[2] * 2;
int testlen =
20 + 2 + 2 * count[0] + 2 + 2 * count[1] + 2 + 2 * count[2] +
4 * 9;
if (testlen != (end - start)) {
printf(" 20 + 2 + %d + 2 + %d + 2 + %d + %d = %d != %d\n",
count[0] * 2, count[1] * 2, count[2] * 2, 9 * 4, testlen,
end - start);
printf("SubType%02d Error2!\n", type);
}
if (DUMP_BLOCKS) {
if (start < end) {
print_block(file, start, end, start, end);
} else {
print_block(file, start, start + testlen, start,
start + testlen);
}
// hinten immer 9 * 32 bit
}
}
void parse_game(FILE * file, int id, uint32_t start, uint32_t end)
{
fseek(file, start, SEEK_SET);
uint32_t index = 0;
uint16_t type = 0;
fread(&type, sizeof(type), 1, file);
printf("\tType:\t%d\n", type);
if (type == 253) {
print_block(file, start, end, start, end);
uint16_t count;
fseek(file, start + 2, SEEK_SET);
fread(&count, sizeof(count), 1, file);
index = 4;
int i;
for (i = 0; i < count; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_list_16(file, pointer);
}
printf("Typ%03d (%d)PLY ", type, count);
for (i = 0; i < count; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_media(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
index = 4;
printf("Typ%03d (%d)OID ", type, count);
for (i = 0; i < count; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_oid(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
index = 4;
printf("Typ%03d (%d)GID ", type, count);
for (i = 0; i < count; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_gid(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
return;
}
uint16_t aa;
fread(&aa, sizeof(aa), 1, file);
uint16_t ignore1;
fread(&ignore1, sizeof(ignore1), 1, file);
uint16_t bb;
fread(&bb, sizeof(bb), 1, file);
int playscripts = 5;
int ps2 = 10;
if ((type == 7) || (type == 10) || (type == 6)) {
ps2 = 11;
}
if ((type == 8) || (type == 16)) {
ps2 = 14;
}
if (type == 9) {
ps2 = 85;
}
printf("HT%02d C%03d I%02d C%03d\n", type, aa, ignore1, bb);
if (type == 6) {
index = 26;
fseek(file, start + 26, SEEK_SET);
playscripts = 7;
} else {
index = 18;
fseek(file, start + 18 + playscripts * 4, SEEK_SET);
uint32_t pointer;
fread(&pointer, sizeof(pointer), 1, file);
if (bb != 0) {
printf("Error4\n");
}
}
int i = 0;
for (i = 0; i < playscripts; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
if (test_list_32(file, pointer, start, end) > 0) {
printf("No Playlist pointer (%08X)\n", pointer);
} else {
parse_playlistpointer(file, pointer, start, end);
}
}
index += 4 * playscripts;
// if (type != 6)
// {
int subgamestart = index;
if (aa != 0) {
printf("\n\tSubgames:\t[");
for (i = 0; i < aa; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
printf("0x%X,", pointer);
}
printf("]\n\n");
}
index += aa * 4;
if (type == 6) {
if (bb != 0) {
printf("\n\tSubgames2:\t[");
for (i = 0; i < bb; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
printf("0x%X,", pointer);
}
printf("]\n\n");
}
index += bb * 4;
}
index += 20;
for (i = 0; i < 10; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
if (test_list_32(file, pointer, start, end) > 0) {
printf("No Playlist pointer (%08X)\n", pointer);
} else {
parse_playlistpointer(file, pointer, start, end);
}
}
index += 10 * 4;
if (type == 6) {
uint32_t pointer;
fseek(file, start + index, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_playlist(file, pointer);
index += 4;
} else if (type == 7 || type == 10) {
uint32_t pointer;
fseek(file, start + index, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_playlistpointer(file, pointer, start, end);
index += 4;
} else if (type == 8) {
uint32_t pointer;
fseek(file, start + index, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_oidlist(file, pointer);
index += 4;
fseek(file, start + index, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_gameidlist(file, pointer);
index += 4;
int i;
for (i = 0; i < 2; i++) {
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_playlistpointer(file, pointer, start, end);
}
index += 2 * 4;
} else if (type == 9) {
int i;
for (i = 0; i < 75; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_playlistpointer(file, pointer, start, end);
}
index += 75 * 4;
} else if (type == 16) {
uint32_t pointer;
fseek(file, start + index, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_oidlist(file, pointer);
index += 4;
int i;
for (i = 0; i < 3; i++) {
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
parse_playlistpointer(file, pointer, start, end);
}
index += 3 * 4;
}
if (0) {
printf("Typ%02d PLP ", type);
for (i = 0; i < ps2; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_32(file, pointer, start, end);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
if (test_list_32(file, pointer, start, end)) {
// printf("No Playlist pointer (%08X)\n", pointer);
// parse_playlistpointer(file, pointer, start, end);
} else {
// parse_playlistpointer(file, pointer, start, end);
}
}
printf("\n", type);
printf("Typ%02d PLY ", type);
for (i = 0; i < ps2; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_media(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
printf("Typ%02d OID ", type);
for (i = 0; i < ps2; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_oid(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
printf("Typ%02d GID ", type);
for (i = 0; i < ps2; i++) {
uint32_t pointer;
fseek(file, start + index + i * 4, SEEK_SET);
fread(&pointer, sizeof(pointer), 1, file);
int tmp = test_list_gid(file, pointer);
if (tmp == 0) {
printf("+");
}
if (tmp > 0) {
printf("-");
}
if (tmp < 0) {
printf("0");
}
}
printf("\n", type);
}
uint32_t firstsub;
if (type == 6) {
fseek(file, start + subgamestart + 4, SEEK_SET);
} else {
if (aa != 0) {
fseek(file, start + subgamestart, SEEK_SET);
} else {
fseek(file, start + 18, SEEK_SET);
}
}
fread(&firstsub, sizeof(firstsub), 1, file);
if (start + index != firstsub) {
printf
("start(0x%X) + index(%d) + ps2(%d) * 4 = 0x%X != firstsub(0x%X)\n",
start, index, ps2, start + index + ps2 * 4, firstsub);
printf("Error7!\n");
}
for (i = 0; i < aa - 1; i++) {
uint32_t pointer1, pointer2;
fseek(file, start + subgamestart + i * 4, SEEK_SET);
fread(&pointer1, sizeof(pointer1), 1, file);
fread(&pointer2, sizeof(pointer2), 1, file);
printf("SubGame(%2d, 0x%X)\n--------------------------\n", i,
pointer1);
parse_subgame(file, pointer1, pointer2, type);
// print_block(file, pointer1, pointer2, start, end);
}
if (type == 6) {
int sbs2 = subgamestart + 4 * aa;
for (i = 0; i < bb - 1; i++) {
uint32_t pointer1, pointer2;
fseek(file, start + sbs2 + i * 4, SEEK_SET);
fread(&pointer1, sizeof(pointer1), 1, file);
fread(&pointer2, sizeof(pointer2), 1, file);
printf("SubGame2(%2d, 0x%X)\n--------------------------\n", i,
pointer1);
parse_subgame(file, pointer1, pointer2, type);
// print_block(file, pointer1, pointer2, start, end);
}
}
// }
// printf("\tType? = %d\n\n", type);
// fread(&bb, sizeof(bb),1,file);
if (DUMP_BLOCKS) {
// if (type == 6)
// {
uint32_t test = 0;
i = 0;
int b = 0;
do {
i++;
fseek(file, start + i, SEEK_SET);
b = 0;
int j = 0;
for (j = 0; j < aa + 5; j++) {
fread(&test, sizeof(test), 1, file);
if ((test < start) || (test > end)) {
b = 1;
}
}
// printf("\t%X = %X\n", i, test);
}
while ((b == 1) && (i < (end - start - ((aa + 5) * 4))));
printf("\tType? = %d\t%d\n\n", type, i);
// fseek(file, start + 18 , SEEK_SET);
// fread(&test, sizeof(test),1,file);
/* if ((test < start) || (test > end )) {
//
printf("%s_game%02d", gme, id);
printf("\tType? = %d\t%d\n\n", type,i);
}
*/
/* if ((aa == 0) || (bb == 0 )) {
//
printf("%s_game%02d", gme, id);
printf("\taa = %d\tbb = %d\n\n", aa,bb);
printf("\tType? = %d\t%d\n\n", type,i);
}
*/
uint32_t blocks[5001];
blocks[0] = start;
int bi = 1;
i = 16;
do {
i++;
fseek(file, start + i, SEEK_SET);
uint32_t testp;
fread(&testp, sizeof(testp), 1, file);
if ((testp >= start) && (testp < end)) {
blocks[bi++] = testp;
}
}
while ((bi < 5000) && (i < (end - start - 3)));
if (bi < 5000) {
blocks[bi++] = end;
// sortieren
int z = 0;
for (i = 0; i < bi; i++) {
int j;
for (j = i + 1; j < bi; j++) {
if (blocks[i] > blocks[j]) {
uint32_t tmp = blocks[i];
blocks[i] = blocks[j];
blocks[j] = tmp;
}
if (blocks[i] == blocks[j]
&& (blocks[i] != 0xffffffff)) {
blocks[j] = 0xffffffff;
z++;
}
}
}
bi -= z;
// ausgeben
for (i = 0; i < (bi - 1); i++) {
printf("0x%X B%02d %06d\n\t", blocks[i], i,
blocks[i + 1] - blocks[i]);
uint32_t j;
int p = 0;
for (j = blocks[i]; j < blocks[i + 1]; j++) {
fseek(file, j, SEEK_SET);
uint8_t val;
fread(&val, sizeof(val), 1, file);
printf("%02X ", val);
if (p == 7) {
printf(" ");
}
if (p++ == 15) {
p = 0;
printf("\n\t");
}
}
printf("\n\t\t");
int coun = 0;
uint32_t last = 0;
int k = 0;
for (j = blocks[i]; j < blocks[i + 1]; j++) {
fseek(file, j, SEEK_SET);
uint32_t point;
fread(&point, sizeof(point), 1, file);
if ((point >= start) && (point < end)) {
j += 3;
if (last < point) {
coun++;
last = point;
} else {
printf("B%02d TP%02d I%02d L%02d \n\t\t", i,
type, k++, coun);
if ((i == 0) && (k == 2) && (coun != aa)
&& (aa != 0))
printf("Error!\n");
coun = 1;
last = 0;
}
} else {
if (coun > 0) {
if ((coun > 1) || (k > 0)) {
printf("B%02d TP%02d I%02d L%02d \n\t\t",
i, type, k++, coun);
if ((i == 0) && (k == 2) && (coun != aa)
&& (aa != 0))
printf("Error!\n");
}
coun = 0;
last = 0;
}
}
}
if (coun > 0) {
printf("B%02d TP%02d I%02d L%02d \n\t\t", i, type, k++,
coun);
if ((i == 0) && (k == 2) && (coun != aa) && (aa != 0))
printf("Error!\n");
coun = 0;
last = 0;
}
printf("\n");
}
}
}
}
void parse_games(FILE * file, uint32_t start)
{
uint32_t count = 0;
fseek(file, start, SEEK_SET);
fread(&count, sizeof(count), 1, file);
printf("\tCount = %d\n\n", count);
count_games = count;
int i;
for (i = 0; i < (count); i++) {
uint32_t start_script;
uint32_t next_script;
fseek(file, (start + 4) + 4 * i, SEEK_SET);
fread(&start_script, sizeof(start_script), 1, file);
if (i < (count - 1)) {
fread(&next_script, sizeof(next_script), 1, file);
} else {
next_script = end_game;
}
char name[2024];
snprintf(name, 2020, "game%02d", i);
save_block(name, start_script, next_script - start_script);
printf("Game Header (%2d, 0x%X)\n--------------------------\n", i,
start_script);
parse_game(file, i, start_script, next_script);
}
}
int main(int argc, char *argv[])
{
gme = argv[1];
FILE *file = fopen(gme, "rb");
prepare_tests(file);
uint32_t start_game = 0;
fseek(file, 0x10, SEEK_SET);
fread(&start_game, sizeof(start_game), 1, file);
// parse_list_16(file, p_unknown_table);
// printf ("Scripts:\n");
// print_block (file, 0x200, start_game, 0, file_size);
// parse_list_16(file, p_unknown_table);
// printf("Unknown (0x%X - 0x%X) = (%d)\n", unknown_table, media_table , media_table - unknown_table);
// print_block (file, unknown_table, media_table, 0, file_size);
printf("Game (%s)\n", gme);
printf("Games (0x%X)\n--------------------------\n", start_game);
parse_games(file, start_game);
fclose(file);
}