-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadrift.pl
More file actions
3826 lines (3814 loc) · 144 KB
/
Copy pathadrift.pl
File metadata and controls
3826 lines (3814 loc) · 144 KB
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use strict; # 'Safest' operation level
use warnings; # Give warnings
use File::Basename; # Interpreting resource filenames
use File::Path 'make_path'; # Creating directories for resources
use File::Slurp; # Read entire file at once
use Compress::Zlib; # ZLib compression
use Data::Dumper; # Dumping data structures
use Carp; # For stack tracing at errors
my $Time_Start = time(); # Epoch time for start of processing
##Version History
my $Decompiler_Version = '0.14';
#v0.1: Initial structure for flow and storage
#v0.2: Signature parsing, inflation/decryption of source
#v0.3: Raw dump
#v0.4: Parse header
#v0.5: Parse rooms with basic XML output
#v0.6: Parse objects with basic XML output
#v0.7: Parse tasks
#v0.8: Parse events, persons and roomgroups
#v0.9: Parse synonyms, variables and ALRs
#v0.10: Improved output to XML and Inform
#v0.11: Improved parsing of actions and restrictions
#v0.12: Code restructuring
#v0.13: Symbol generation
#v0.14: Improved references
#v0.15: Expanded XML output
##Global Variables
#Story Settings
my $Container_Version; # The version used to contain the storyfile
#Story Data
my %Story;
my @Rooms = ( undef ); # Contains the room objects for the story, starting from ID 1
my @Objects = ( undef ); # Contains the 'object' objects for the story, starting from ID 1
my @Tasks = ( undef ); # Contains the task objects for the story, starting from ID 1
my @Events = ( undef ); # Contains the event objects for the story, starting from ID 1
my @Persons = ( undef ); # Contains the person objects for the story, starting from ID 1
my @Groups = ( undef ); # Contains the room group objects for the story, starting from ID 1
my @Synonyms = ( undef ); # Contains the synonym objects for the story, starting from ID 1
my @Variables = ( undef ); # Contains the variable objects for the story, starting from ID 1
my @ALRs = ( undef ); # Contains the ALR objects for the story, starting from ID 1
#Object mappings
my @ObjectStatic = ( 0 ); # Mapping from static object ID to actual object ID
my @ObjectPortable = ( 0 ); # Mapping from non-static object ID to actual object ID
my @ObjectOpenable = ( 0 ); # Mapping from openable object ID to actual object ID
my @ObjectStateful = ( 0 ); # Mapping from stateful object ID to actual object ID
my @ObjectContainer = ( 0 ); # Mapping from container object ID to actual object ID
my @ObjectSurface = ( 0 ); # Mapping from surface object ID to actual object ID
my @ObjectHolder = ( 0 ); # Mapping from holder/parent object ID to actual object ID
my @ObjectLieable = ( 0 ); # Mapping from lieable object ID to actual object ID
my @ObjectSitStandable = ( 0 ); # Mapping from sit/standable object ID to actual object ID
#Symbol Translation Names
my @Symbol_Room; # Translation names for rooms
my @Symbol_Object; # Translation names for 'objects'
my @Symbol_Task; # Translation names for tasks
my @Symbol_Event; # Translation names for event
my @Symbol_Person; # Translation names for persons
my @Symbol_Group; # Translation names for room groups
my @Symbol_Variable; # Translation names for variables
#Static Symbol Names
my @Symbol_Compass_Direction; # Symbolic names for compass directions
my @Symbol_Compass_Reversed; # Symbolic names for reversed compass directions
my @Symbol_Gender; # Symbolic names for genders
#Constants
my @Versions = (); # Printable names for each compiler version, ascending order
my @Signatures = (); # Header signatures for each compiler version, corresponsing to @Versions
my %PRNG = ();
$PRNG{Constant1} = 0x43fd43fd;
$PRNG{Constant2} = 0x00c39ec3;
$PRNG{Constant3} = 0x00ffffff;
$PRNG{Initial} = 0x00a09e86;
$PRNG{State} = $PRNG{Initial};
#File handling
my $FileName_Compiled; # Filename for the compiled gamefile to decompile
my $FileName_Path = './'; # Path to place output files in
my $FileName_Decompiled; # Filename for the decompiled sourcecode
my $FileName_Log; # Filename for the log
my $FileName_Symbol_In; # Filename for the symbol mapping translation input file
my $FileName_Symbol_Out; # Filename for the symbol mapping translation output file
my $FileName_I7; # Filename for the Inform output
my $FileName_XML; # Filename for the XML output
my $File_Compiled; # File handle for input compiled gamefile
my $File_Log; # File handle for logging output
my $File_Decompiled; # File handle for output decompiled sourcecode
my $File_Symbol_In; # File handle for symbol mapping translation input
my $File_Symbol_Out; # File handle for symbol mapping translation output
my $File_I7; # File handle for Inform output
my $File_XML; # File handle for XML output
my $File_XML_Indent = -1; # Storing the indentation level of the XML file
my $Contents_Compiled; # File contents
#Decompiling Options; see parseArguments()
my $Options = "Decompiling Options:\n";
my $Option_Minimal; # Skip output directory and embedded resources
$Options .= "-m\t\tMinimalist mode, skipping output directory and resources\n";
my $Option_Verbose; # Extra information dumped to story file
$Options .= "-v\t\tVerbose loging output\n";
my $Option_Naming; # Be extra aggressive on trying to determine names
# TODO: This will create duplicate names, remake to avoid that
$Options .= "-a\t\tAggressive search for symbol names\n";
my $Option_Rawdump; # Dump raw decompiled source
$Options .= "+r\t\tRaw dump of decompiled source\n";
my $Option_Generate; # Generate a new symbol file
$Options .= "+s\t\tGenerate symbol translation file in output directory\n";
$Options .= "-s [file]:\tSymbol translation file\n";
#Utility functions to access ADRIFTs line-oriented structure
my @Lines; # Stores the lines which form the basis of ADRIFT files
my $Lines_Next; # Index of next entry in @Lines
my $Terminator; # String indicating end of multi-line value
#Next single-line value
sub nextLine(){
die "Tried to access line $Lines_Next, $#Lines defined" unless defined $Lines[$Lines_Next];
return $Lines[$Lines_Next++];
}
#Next multi-line value
sub nextMulti(){
my $multi;
do {
$multi .= "\n" if defined $multi;
$multi .= nextLine();
} until $Terminator eq substr ($multi, - length($Terminator));
return $multi;
}
#Generate the next value of the PRNG
sub nextRandom(){
$PRNG{State} = ($PRNG{State} * $PRNG{Constant1} + $PRNG{Constant2}) & $PRNG{Constant3};
return (255 * $PRNG{State}) / ($PRNG{Constant3} + 1);
}
##Initialization
sub initialize(){
#Parse arguments;
parseArguments();
#There should be only one argument left, giving the name of the file to parse.
die "Use: adrift [options] story.taf\n$Options" unless ($#ARGV eq 0);
$FileName_Compiled = $ARGV[0];
if ($FileName_Compiled =~ m/([-_\w\s]*)\.(taf)/i) {
#When input file has a recognized extension, use the name of the file
$FileName_Path = $1 . '/' unless defined $Option_Minimal;
$FileName_Log = $1 . '.log';
$FileName_Decompiled = $1 . '.src';
$FileName_XML = $1 . '.xml';
$FileName_I7 = $1 . '.ni';
$FileName_Symbol_Out = $1 . '.sym' if defined $Option_Generate;
}
else{
#Otherwise decide on input agnostic file names
$FileName_Path = 'decompiled/' unless defined $Option_Minimal;
$FileName_Log = 'story.log';
$FileName_Decompiled = 'story.src';
$FileName_XML = 'story.xml';
$FileName_I7 = 'story.ni';
$FileName_Symbol_Out = 'story.sym' if defined $Option_Generate;
}
openFiles();
}
sub parseArguments(){ # Interpret command line arguments
while (defined $ARGV[0]) {
if ($ARGV[0] eq '-m') { # Minimalist mode
$Option_Minimal = 1;
splice(@ARGV, 0, 1);
}
elsif($ARGV[0] eq '-v') { #Verbose logging
$Option_Verbose = 1;
splice(@ARGV, 0, 1);
}
elsif($ARGV[0] eq '-a') { #Aggressive naming search
$Option_Naming = 1;
splice(@ARGV, 0, 1);
}
elsif($ARGV[0] eq '-s') { #Read symbol mapping file
$FileName_Symbol_In = $ARGV[1];
splice(@ARGV, 0, 2);
}
elsif($ARGV[0] eq '+s') { #Generate symbol mapping file template
$Option_Generate = 1;
splice(@ARGV, 0, 1);
}
elsif($#ARGV >= 0 && $ARGV[0] eq '-a') {
$Option_Naming = 1;
splice(@ARGV, 0, 1);
}
elsif($#ARGV >= 0 && $ARGV[0] eq '+r') { # Rawdump mode
$Option_Rawdump = 1;
splice(@ARGV, 0, 1);
}
else{ last }
}
}
sub openFiles(){ # Open file handles
#Determine filenames to use
#Create path as needed
mkdir $FileName_Path unless -e $FileName_Path;
die "$FileName_Path is not a valid path" unless -d $FileName_Path;
#Open log; use :unix to flush as we write to it
open($File_Log, "> :raw :bytes :unix", $FileName_Path . $FileName_Log)
or die "$0: can't open $FileName_Path$FileName_Log for writing: $!";
#Open compiled file with some sanity checking
die "$FileName_Compiled is not a valid file" unless -f $FileName_Compiled;
open($File_Compiled, "< :raw :bytes", $FileName_Compiled)
or die("Couldn't open $FileName_Compiled for reading: $!");
#Open symbol mapping input file if defined
open($File_Symbol_In, "< :raw :bytes", $FileName_Symbol_In)
or die("Couldn't open $FileName_Symbol_In for reading.")
if defined $FileName_Symbol_In;
#Open symbol mapping output file if defined
open($File_Symbol_Out, "> :raw :bytes", $FileName_Path . $FileName_Symbol_Out)
or die "$0: can't open $FileName_Path$FileName_Symbol_Out for writing: $!"
if defined $FileName_Symbol_Out;
#Open generated output files
open($File_Decompiled, "> :raw :bytes", $FileName_Path . $FileName_Decompiled)
or die "$0: can't open $FileName_Path$FileName_Decompiled for writing: $!";
open($File_I7, "> :raw :bytes", $FileName_Path . $FileName_I7)
or die "$0: can't open $FileName_Path$FileName_I7 for writing: $!";
open($File_XML, "> :raw :bytes", $FileName_Path . $FileName_XML)
or die "$0: can't open $FileName_Path$FileName_XML for writing: $!";
#Open mapping file with some sanity checking
die "Overwriting existing symbol file with autogenerated is not supported in minimal mode"
if defined $FileName_Symbol_In
&& $Option_Minimal
&& -e $FileName_Symbol_In;
}
sub determineVersion(){ # Determine container version by reading the story header
#Load signature constants
# $Signature{Size} = 14;
# $Signature{Extra} = 8;
my $base = chr(0x3c).chr(0x42).chr(0x3f).chr(0xc9).chr(0x6a).chr(0x87).chr(0xc2).chr(0xcf);
#Load known signatures
{ # v3.80
my $v380 = 0;
$Versions[$v380] = '3.80';
$Signatures[$v380] = $base.chr(0x94).chr(0x45).chr(0x36).chr(0x61).chr(0x39).chr(0xfa);
}
{ # v3.90
my $v390 = 1;
$Versions[$v390] = '3.90';
$Signatures[$v390] = $base.chr(0x94).chr(0x45).chr(0x37).chr(0x61).chr(0x39).chr(0xfa);
}
{ # v4.00
my $v400 = 2;
$Versions[$v400] = '4.00';
$Signatures[$v400] = $base.chr(0x93).chr(0x45).chr(0x3e).chr(0x61).chr(0x39).chr(0xfa);
}
{ # v5.00
my $v500 = 3;
$Versions[$v500] = '5.00';
$Signatures[$v500] = $base.chr(0x92).chr(0x45).chr(0x3e).chr(0x61).chr(0x30).chr(0x32);
}
#Read signature from file
my $signature_file;
my $bytes_read = read ($File_Compiled, $signature_file, 14);
die 'Unable to read file signature' unless $bytes_read eq 14;
#Check file signature against known signatures
for my $v (0..$#Signatures) { $Container_Version = $Versions[$v] if ($signature_file eq $Signatures[$v]) }
unless (defined $Container_Version){
my $mask = '';
for (1 .. length($signature_file)) { $mask .= chr(nextRandom()) }
#Decrypt; don't bother reseting PRNG
my $decrypted = $signature_file ^ $mask;
#Print and die
print $File_Log "Unknown or unhandled version: $decrypted:\n";
debugDump($signature_file);
die 'Unable to determine version';
}
#Set some version dependant constants
$Terminator = chr(189).chr(208) if $Container_Version eq '4.00' or $Container_Version eq '5.00';
$Terminator = chr( 42).chr( 42) if $Container_Version eq '3.90' or $Container_Version eq '3.80';
}
sub loadConstants(){ # Initialize constants that might depend on version
{ #Null-values
$Symbol_Room[0] = 'nowhere';
$Symbol_Object[0] = 'nothing';
$Symbol_Person[0] = 'player';
}
{ #Compass directions; dependant on the ExpandedCompass global
$Symbol_Compass_Direction[0] = 'North';
$Symbol_Compass_Direction[1] = 'East';
$Symbol_Compass_Direction[2] = 'South';
$Symbol_Compass_Direction[3] = 'West';
$Symbol_Compass_Direction[4] = 'Up';
$Symbol_Compass_Direction[5] = 'Down';
$Symbol_Compass_Direction[6] = 'Inside';
$Symbol_Compass_Direction[7] = 'Outside';
$Symbol_Compass_Direction[8] = 'Northeast' if $Story{ExpandedCompass};
$Symbol_Compass_Direction[9] = 'Southeast' if $Story{ExpandedCompass};
$Symbol_Compass_Direction[10] = 'Southwest' if $Story{ExpandedCompass};
$Symbol_Compass_Direction[11] = 'Northwest' if $Story{ExpandedCompass};
}
{ #Reversed Compass directions; dependant on the ExpandedCompass global
$Symbol_Compass_Reversed[0] = 'south of';
$Symbol_Compass_Reversed[1] = 'west of';
$Symbol_Compass_Reversed[2] = 'north of';
$Symbol_Compass_Reversed[3] = 'east of';
$Symbol_Compass_Reversed[4] = 'down from';
$Symbol_Compass_Reversed[5] = 'up from';
$Symbol_Compass_Reversed[6] = 'outside from';
$Symbol_Compass_Reversed[7] = 'inside from';
$Symbol_Compass_Reversed[8] = 'southwest of' if $Story{ExpandedCompass};
$Symbol_Compass_Reversed[9] = 'northwest of' if $Story{ExpandedCompass};
$Symbol_Compass_Reversed[10] = 'northeast of' if $Story{ExpandedCompass};
$Symbol_Compass_Reversed[11] = 'southeast of' if $Story{ExpandedCompass};
}
{ #Gender names
$Symbol_Gender[0] = 'male';
$Symbol_Gender[1] = 'female';
$Symbol_Gender[2] = 'neuter';
}
}
sub loadSymbols(){ # Read symbol mapping translation from given file
return unless defined $FileName_Symbol_In;
while (my $line = <$File_Symbol_In>) {
#Pre-process the line
chomp $line;
$line = (split('#', $line))[0]; # Remove comments
#Skip ahead if the line doesn't contain anything
# next unless length $line;
next if($line =~ m/^\s*$/i );
my $parsed;
#TODO: This could be improved
if ($line =~ m/(Room)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Room[$2] = $parsed;
}
if ($line =~ m/(Object|Obj)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Object[$2] = $parsed;
}
if ($line =~ m/(Task)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Task[$2] = $parsed;
}
if ($line =~ m/(Event)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Event[$2] = $parsed;
}
if ($line =~ m/(Person|NPC)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Person[$2] = $parsed;
}
if ($line =~ m/(Group|RoomGroup)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Group[$2] = $parsed;
}
if ($line =~ m/(Variable|Var)s?\[?(\d*)\]?\s*=\s*['"](.*)['"]/i ) {
$parsed = $3;
$Symbol_Variable[$2] = $parsed;
}
print "Unable to parse $line (".length($line).")\n" unless defined $parsed;
}
}
sub loadFile(){
#Read in the raw file
my $buffer = read_file ( $FileName_Compiled, binmode => ':raw' );
#V3 is stored as encrypted text
if ($Container_Version eq '3.90' or $Container_Version eq '3.80'){
#Generate decryption mask
my $size = length($buffer);
my $mask = '';
for (1 .. $size) { $mask .= chr(nextRandom()) }
#Decrypt
$Contents_Compiled = $buffer ^ $mask;
}
#V4 is stored as a deflated zlib after an extended header
if ($Container_Version eq '4.00' or $Container_Version eq '5.00'){
#Skip file signature and header
$buffer = substr($buffer, 14+8);
#Initiate inflation stream
my $stream = inflateInit() or die 'Cannot create inflation stream';
#Inflate and store in Contents_Compiled
my $status;
($Contents_Compiled, $status) = $stream->inflate($buffer);
#TODO: Check status
}
#V5 is stored as a deflated zlib
if ($Container_Version eq '5.00'){
#Skip file signature and header
my $skip = index($buffer, '</ifindex>')+10 if $Container_Version eq '5.00';
print $skip;
debugDump(substr($buffer, $skip, 30));
$buffer = substr($buffer, $skip);
die "v5.00 containeris not yet supported";
#Initiate inflation stream
my $stream = inflateInit() or die 'Cannot create inflation stream';
#Inflate and store in Contents_Compiled
my $status;
($Contents_Compiled, $status) = $stream->inflate($buffer);
#TODO: Check status
}
#Sanity check
die "V$Container_Version not supported, unable to load file" unless defined $Contents_Compiled;
#Dump raw file if called for
print $File_Decompiled $Contents_Compiled if defined $Option_Rawdump;
#Split and store lines
$Lines_Next = 0;
@Lines = split(chr(13).chr(10), $Contents_Compiled);
print $File_Log "Decompiler v$Decompiler_Version on $FileName_Compiled";
}
##Parsing file into memory structure
sub parse(){
#Parse header, printing the most important settings
parseHeader();
print $File_Log " $Story{Title} by $Story{Author} (ADRIFT v$Container_Version)\n";
print $File_Log "\tBattles\n" if $Story{EnableBattle};
print $File_Log "\t8-point compass\n" if $Story{ExpandedCompass};
print $File_Log "\tGraphics\n" if $Story{EnableGraphics};
print $File_Log "\tSound\n" if $Story{EnableSound};
#Load constants based on header
loadConstants();
$Story{Directions} = 8;
$Story{Directions} = 12 if $Story{ExpandedCompass};
#Parse rooms
my $rooms = nextLine();
print $File_Log "$rooms rooms\n";
for (1 .. $rooms) { push @Rooms, parseRoom() }
#Parse objects
my $objects = nextLine();
print $File_Log "$objects objects\n";
for (1 .. $objects) { push @Objects, parseObject() }
#Parse tasks
my $tasks = nextLine();
print $File_Log "$tasks tasks\n";
for (1 .. $tasks) { push @Tasks, parseTask() }
#Parse timed events
my $events = nextLine();
print $File_Log "$events events\n";
for (1 .. $events) { push @Events, parseEvent() }
#Parse persons
my $persons = nextLine();
print $File_Log "$persons persons\n";
for (1 .. $persons) { push @Persons, parsePerson(); }
#Parse room-groups
my $groups = nextLine();
print $File_Log "$groups groups\n";
for (1 .. $groups) { push @Groups, parseGroup(); }
#Parse parser-synonyms
my $synonyms = nextLine();
print $File_Log "$synonyms synonyms\n";
for (1 .. $synonyms) { push @Synonyms, parseSynonym(); }
#Parse variables
my $variables = 0;
$variables = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
print $File_Log "$variables variables\n";
for (1 .. $variables) { push @Variables, parseVariable(); }
#Parse alternate-language resources
my $alrs = 0;
$alrs = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
print $File_Log "$alrs ALRs\n";
for (1 .. $alrs) { push @ALRs, parseALR(); }
#Parse footer
parseFooter();
print $File_Log "Parsed $Lines_Next of ". ($#Lines + 1) ." lines\n";
}
sub parseHeader(){
#Intro Text
$Story{Intro} = nextMulti();
$Story{Start} = nextLine() + 1;
$Story{Ending} = nextMulti();
#text GameName
$Story{Title} = nextLine();
#text GameAuthor
$Story{Author} = nextLine();
#number MaxCarried TODO postprocessing into MaxSize and MaxWeight
my $max_carried = nextLine() if $Container_Version eq '3.80';
#text DontUnderstand
$Story{Error} = nextLine();
#number Perspective
$Story{Perspective} = nextLine();
#truth ShowExits
$Story{ShowExits} = nextLine();
#number WaitTurns
$Story{WaitTurns} = nextLine();
#truth DispFirstRoom
$Story{InitialLook} = 0 if $Container_Version eq '3.80';
$Story{InitialLook} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth BattleSystem
$Story{EnableBattle} = 0 if $Container_Version eq '3.80';
$Story{EnableBattle} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number MaxScore
$Story{MaxScore} = 0 if $Container_Version eq '3.80';
$Story{MaxScore} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#text PlayerName
$Story{PlayerName} = '' if $Container_Version eq '3.80';
$Story{PlayerName} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth PromptName
$Story{PromptName} = 0 if $Container_Version eq '3.80';
$Story{PromptName} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#text PlayerDesc
$Story{PlayerDesc} = '' if $Container_Version eq '3.80';
$Story{PlayerDesc} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number Task
$Story{AltDescTask} = 0 if $Container_Version eq '3.80';
$Story{AltDescTask} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#text AltDesc
$Story{AltDesc} = nextLine() if $Story{AltDescTask};
#number Position
$Story{Position} = 0 if $Container_Version eq '3.80';
$Story{Position} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number ParentObject
$Story{ParentObject} = 0 if $Container_Version eq '3.80';
$Story{ParentObject} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number PlayerGender
$Story{PlayerGender} = 0 if $Container_Version eq '3.80';
$Story{PlayerGender} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number MaxSize
$Story{MaxSize} = 0 if $Container_Version eq '3.80'; #TODO Process $max_carried into this
$Story{MaxSize} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number MaxWeight
$Story{MaxWeight} = 0 if $Container_Version eq '3.80'; #TODO Process $max_carried into this
$Story{MaxWeight} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#battle PlayerStats
$Story{PlayerStats} = parseBattle() if $Story{EnableBattle};
#truth EightPointCompass
$Story{ExpandedCompass} = 0 if $Container_Version eq '3.80';
$Story{ExpandedCompass} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth NoDebug SKIP
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth NoScoreNotify
$Story{NoScoreNotify} = 1 if $Container_Version eq '3.80';
$Story{NoScoreNotify} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth DisableMap
$Story{DisableMap} = 0 if $Container_Version eq '3.80';
$Story{DisableMap} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth NoAutoComplete SKIP
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth NoControlPanel SKIP
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth NoMouse SKIP
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth Sound
$Story{EnableSound} = 0 if $Container_Version eq '3.80';
$Story{EnableSound} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth Graphics
$Story{EnableGraphics} = 0 if $Container_Version eq '3.80';
$Story{EnableGraphics} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#resource IntroRes
$Story{IntroRes} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#resource WinRes
$Story{WinRes} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth StatusBox
$Story{EnableStatusBox} = 0 if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$Story{EnableStatusBox} = nextLine() if $Container_Version eq '4.00';
#text StatusBoxText
$Story{StatusBoxText} = '' if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$Story{StatusBoxText} = nextLine() if $Container_Version eq '4.00';
#2x Unknown SKIP
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth Embedded
$Story{Embedded} = 0 if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$Story{Embedded} = nextLine() if $Container_Version eq '4.00';
}
sub parseFooter(){
#truth CustomFont
$Story{CustomFont} = 0 if $Container_Version eq '3.80';
$Story{CustomFont} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#text FontNameSize
$Story{FontNameSize} = nextLine() if $Story{CustomFont};
#text CompileDate
$Story{CompileDate} = nextLine();
#text Password SKIP
nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
}
#Routines for parsing major objects
sub parseRoom(){
my $id = @Rooms;
my %room = ();
#References
$room{RoomReferences} = [];
$room{ObjectReferences} = [];
$room{TaskReferences} = [];
$room{EventReferences} = [];
$room{PersonReferences} = [];
$room{GroupReferences} = [];
$room{SynonymReferences} = [];
$room{VariableReferences} = [];
$room{ALRReferences} = [];
#text Short
$room{Short} = nextLine();
print $File_Log "\t\t$id: $room{Short}\n" if defined $Option_Verbose;
#text Long
$room{Description} = nextLine();
#text LastDesc
$room{LastDesc} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#exit RoomExits
my $exit_count = 0;
$room{Exits} = [];
for my $dir (1 .. $Story{Directions}) {
push @{ $room{Exits} }, parseExit();
$exit_count++ unless $room{Exits}[$dir - 1]{Destination} eq 0;
}
print $File_Log "\t\t\t$exit_count exit(s)\n" if defined $Option_Verbose;
#text AddDesc1
$room{AltDesc1} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#number AddDesc1Task
$room{AltDesc1Task} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#text AddDesc2
$room{AltDesc2} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#number AddDesc2Task
$room{AltDesc2Task} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#number Obj
$room{AltDesc3Obj} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#text AltDesc
$room{AltDesc3} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#number TypeHideObjects
$room{TypeHideObjects} = nextLine() if $Container_Version eq '3.80' or $Container_Version eq '3.90';
#resource Res
$room{Resource} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#resource LastRes
$room{LastDescResource} = parseResource() if $Container_Version eq '3.90';
#resource Task1Res
$room{AltDesc1Resource} = parseResource() if $Container_Version eq '3.90';
#resource Task2Res
$room{AltDesc2Resource} = parseResource() if $Container_Version eq '3.90';
#resource AltRes
$room{AltDesc3Resource} = parseResource() if $Container_Version eq '3.90';
#RoomAlt Alternates
my $alternate_count = 0;
my @alternates = ();
$alternate_count = nextLine() if $Container_Version eq '4.00';
for (1 .. $alternate_count){ push @alternates, parseRoomAlt() }
$room{Alternates} = \@alternates;
print $File_Log "\t\t\t$alternate_count alternate(s)\n" if defined $Option_Verbose && $alternate_count;
#truth HideOnMap
$room{Hidden} = nextLine() if ($Container_Version eq '3.90' or $Container_Version eq '4.00') && not $Story{DisableMap};
return \%room;
}
sub parseObject(){
my $id = @Objects;
my %object = ();
#References
$object{RoomReferences} = [];
$object{ObjectReferences} = [];
$object{TaskReferences} = [];
$object{EventReferences} = [];
$object{PersonReferences} = [];
$object{GroupReferences} = [];
$object{SynonymReferences} = [];
$object{VariableReferences} = [];
$object{ALRReferences} = [];
#text Prefix
$object{Prefix} = nextLine();
#text Short
$object{Short} = nextLine();
print $File_Log "\t\t$id: ($object{Prefix}) $object{Short}\n" if defined $Option_Verbose;
#text Alias
my $alias = 1;
$alias = nextLine() if $Container_Version eq '4.00';
$object{Alias} = ();
for (1 .. $alias) { push @{ $object{Alias} }, nextLine(); }
#truth Static
$object{Static} = nextLine();
#text Description
$object{Description} = nextLine();
#number InitialPosition
$object{InitialPosition}= nextLine();
#number Task
$object{AltDescTask} = nextLine();
#truth TaskNotDone
$object{AltDescType} = nextLine();
#text AltDesc
$object{AltDesc} = nextLine();
#RoomList Where
$object{WhereType} = 9;
$object{WhereType} = nextLine() if $object{Static};
# 0: NO_ROOMS
# 1: ONE_ROOM
# 2: SOME_ROOMS
# 3: ALL_ROOMS
# 4: NPC_PART
# 9: NULL/Off-stage
$object{Where} = [];
if ($object{WhereType} eq 1) {
my $roomID = nextLine() + 1;
push @{ $object{Where} }, $roomID;
}
if($object{WhereType} eq 2){
for my $room (0 .. $#Rooms){ push @{ $object{Where} }, $room if nextLine(); }
}
my $surfaceContainer = 0;
$surfaceContainer = nextLine() if $Container_Version eq '3.80';
#truth Container
$object{Container} = 0 unless $surfaceContainer eq 1;
$object{Container} = 1 if $surfaceContainer eq 1;
$object{Container} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#truth Surface
$object{Surface} = 0 unless $surfaceContainer eq 2;
$object{Surface} = 1 if $surfaceContainer eq 2;
$object{Surface} = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#number Capacity
$object{Capacity} = nextLine();
$object{Capacity} = $object{Capacity} * 10 + 2 if $Container_Version eq '3.80';
#truth Wearable
$object{Wearable} = 0;
$object{Wearable} = nextLine() unless $object{Static};
#number SizeWeight
$object{SizeWeight} = 0;
$object{SizeWeight} = nextLine() unless $object{Static};
#number Parent
$object{Parent} = 0;
$object{Parent} = nextLine() unless $object{Static};
$object{Parent} = nextLine() if $object{Static} && $object{WhereType} eq 4;
#number Openable
my $openable = nextLine();
# 0: UNOPENABLE
# 5: OPEN
# 6: CLOSED
# 7: LOCKED
$object{Openable} = $openable;
#Code 5 and 6 are reversed in v3.XX
if($Container_Version eq '3.80' or $Container_Version eq '3.90'){
$object{Openable} = 6 if $openable eq 5;
$object{Openable} = 5 if $openable eq 6;
}
#number Key
$object{Key} = nextLine() if $Container_Version eq '4.00' && $object{Openable};
#number SitLie
my $enterableType = nextLine();
$object{EnterableType} = $enterableType;
$object{SitStandable} = 1 if $enterableType eq 1 || $enterableType eq 3;
$object{Lieable} = 1 if $enterableType eq 2 || $enterableType eq 3;
#truth Edible
$object{Edible} = nextLine() unless $object{Static};
#truth Readable
$object{Readable} = nextLine();
#truth ReadText
$object{ReadText} = nextLine() if $object{Readable};
#truth Weapon
$object{Weapon} = nextLine() unless $object{Static};
#number CurrentState
$object{CurrentState} = 0 if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$object{CurrentState} = nextLine() if $Container_Version eq '4.00';
#number States
$object{States} = 0;
$object{States} = nextLine() if $object{CurrentState};
#truth StateListed
$object{StateListed} = 0;
$object{StateListed} = nextLine() if $object{CurrentState};
#truth ListFlag
$object{ListFlag} = 0 if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$object{ListFlag} = nextLine() if $Container_Version eq '4.00';
#resource Res1
$object{Resource1} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#resource Res2
$object{Resource2} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
#battle Battle
$object{BattleStats} = parseBattle() if $Story{EnableBattle};
#text InRoomDesc
$object{InRoomDesc} = '' if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$object{InRoomDesc} = nextLine() if $Container_Version eq '4.00';
#number OnlyWhenNotMoved
$object{InRoomDescType} = 0 if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$object{InRoomDescType} = nextLine() if $Container_Version eq '4.00';
#Update the Object mapping references:
push @ObjectPortable, $id unless $object{Static};
push @ObjectStatic, $id if $object{Static};
push @ObjectOpenable, $id if $object{Openable};
push @ObjectStateful, $id if $object{Openable} or $object{CurrentState};
push @ObjectContainer, $id if $object{Container};
push @ObjectSurface, $id if $object{Surface};
push @ObjectHolder, $id if $object{Container} or $object{Surface};
push @ObjectLieable, $id if $object{Lieable};
push @ObjectSitStandable, $id if $object{SitStandable};
return \%object;
}
sub parseTask(){
my $id = @Tasks;
my %task = ();
#References
$task{RoomReferences} = [];
$task{ObjectReferences} = [];
$task{TaskReferences} = [];
$task{EventReferences} = [];
$task{PersonReferences} = [];
$task{GroupReferences} = [];
$task{SynonymReferences} = [];
$task{VariableReferences} = [];
$task{ALRReferences} = [];
#text Command
my $commands = nextLine();
$commands++ if $Container_Version eq '3.80' or $Container_Version eq '3.90';
$task{Commands} = ();
for (1 .. $commands) { push @{ $task{Commands} }, nextLine(); }
print $File_Log "\t\t$id: $task{Commands}[0] ($commands)\n" if defined $Option_Verbose;
#text CompleteText
$task{CompleteText} = nextLine();
#text ReverseMessage
$task{ReverseText} = nextLine();
#text RepeatText
$task{RepeatText} = nextLine();
#text AdditionalMessage
$task{AdditionalText} = nextLine();
#number ShowRoomDesc
$task{ShowRoomDesc} = nextLine();
#Some 3.80 variables
if ($Container_Version eq '3.80'){
#number Score
$task{Score} = nextLine();
#number SingleScore
$task{BSingleScore} = nextLine();
#TaskMove Movements
$task{Movements} = ();
for (1 .. 6) {
my %movement = ();
$movement{Var1} = nextLine();
$movement{Var2} = nextLine();
$movement{Var3} = nextLine();
push @{ $task{Movements} }, \%movement;
}
}
#truth Repeatable
$task{Repeatable} = nextLine();
#truth Reversible
$task{Reversible} = nextLine();
#text ReverseCommand
my $commands_reverse = nextLine();
$commands_reverse++ if $Container_Version eq '3.80' or $Container_Version eq '3.90';
print $File_Log "\t\t\t$commands_reverse reversion(s)\n" if defined $Option_Verbose;
$task{CommandsReverse} = [];
for (1 .. $commands_reverse) { push @{ $task{CommandsReverse} }, nextLine(); }
#Some 3.80 variables
if ($Container_Version eq '3.80'){
#number WearObj1
$task{WearObj1} = nextLine();
#number WearObj2
$task{WearObj2} = nextLine();
#number HoldObj1
$task{HoldObj1} = nextLine();
#number HoldObj2
$task{HoldObj2} = nextLine();
#number HoldObj3
$task{HoldObj3} = nextLine();
#number Obj1
$task{Obj1} = nextLine();
#number Task
$task{Task} = nextLine();
#truth TaskNotDone
$task{TaskNotDone} = nextLine();
#text TaskMsg
$task{TaskMsg} = nextLine();
#text HoldMsg
$task{HoldMsg} = nextLine();
#text WearMsg
$task{WearMsg} = nextLine();
#text CompanyMsg
$task{CompanyMsg} = nextLine();
#truth NotInSameRoom
$task{NotInSameRoom} = nextLine();
#number NPC
$task{NPC} = nextLine();
#text Obj1Msg
$task{Obj1Msg} = nextLine();
#number Obj1Room
$task{Obj1Room} = nextLine();
}
#RoomList Where
$task{WhereType} = 9;
$task{WhereType} = nextLine();
# 0: NO_ROOMS
# 1: ONE_ROOM
# 2: SOME_ROOMS
# 3: ALL_ROOMS
# 4: NPC_PART
# 9: NULL/Off-stage
$task{Where} = [];
if ($task{WhereType} eq 1) {
my $roomID = nextLine() + 1;
push @{ $task{Where} }, $roomID;
}
if($task{WhereType} eq 2){
for my $room (1 .. $#Rooms){ push @{ $task{Where} }, $room if nextLine(); }
}
#Some 3.80 variables
if ($Container_Version eq '3.80'){
#truth KillsPlayer
$task{KillsPlayer} = nextLine();
#truth HoldingSameRoom
$task{HoldingSameRoom} = nextLine();
}
# $Question ?$Question:$Hint1,$Hint2
#text Question
$task{Question} = nextLine();
#text Hint1
$task{Hint1} = nextLine() if length $task{Question};
#text Hint2
$task{Hint2} = nextLine() if length $task{Question};
#Some 3.80 variables
if ($Container_Version eq '3.80'){
#number Obj2
$task{Obj2} = nextLine();
#number Obj2Var1
$task{Obj2Var1} = nextLine() if $task{Obj2};
#number Obj2Var2
$task{Obj2Var2} = nextLine() if $task{Obj2};
#text Obj2Msg
$task{Obj2Msg} = nextLine() if $task{Obj2};
#truth WinGame
$task{WinGame} = nextLine();
}
#Restrictions Restrictions
$task{Restrictions} = [];
my $restrictions = 0;
$restrictions = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
print $File_Log "\t\t\t$restrictions restriction(s)\n" if defined $Option_Verbose;
for (1 .. $restrictions) { push @{ $task{Restrictions} }, parseRestriction(); }
#Actions Actions
$task{Actions} = [];
my $actions = 0;
$actions = nextLine() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
print $File_Log "\t\t\t$actions action(s)\n" if defined $Option_Verbose;
for (1 .. $actions) { push @{ $task{Actions} }, parseAction(); }
#text RestrMask
$task{RestrMask} = nextLine() if $Container_Version eq '4.00';
#resource Res
$task{Resource} = parseResource() if $Container_Version eq '3.90' or $Container_Version eq '4.00';
return \%task;
}
sub parseEvent(){
my $id = @Events;
my %event = ();
#References
$event{RoomReferences} = [];
$event{ObjectReferences} = [];
$event{TaskReferences} = [];
$event{EventReferences} = [];
$event{PersonReferences} = [];
$event{GroupReferences} = [];
$event{SynonymReferences} = [];
$event{VariableReferences} = [];
$event{ALRReferences} = [];
#text Short
$event{Short} = nextLine();
print $File_Log "\t\t$id: $event{Short}\n" if defined $Option_Verbose;
#number StarterType
$event{StarterType} = nextLine();
#number StartTime
$event{StartTime} = nextLine() if $event{StarterType} eq 2;
#number EndTime
$event{EndTime} = nextLine() if $event{StarterType} eq 2;
#number TaskNum
$event{TaskNum} = nextLine() if $event{StarterType} eq 3;
#number RestartType
$event{RestartType} = nextLine();
#truth TaskFinished
$event{TaskFinished} = nextLine();
#number Time1
$event{Time1} = nextLine();
#number Time2
$event{Time2} = nextLine();
#text StartText
$event{StartText} = nextLine();
#text LookText
$event{LookText} = nextLine();
#text FinishText
$event{FinishText} = nextLine();
#RoomList Where
$event{WhereType} = 9;
$event{WhereType} = nextLine();
# 0: NO_ROOMS
# 1: ONE_ROOM
# 2: SOME_ROOMS
# 3: ALL_ROOMS
# 4: NPC_PART
# 9: NULL/Off-stage
$event{Where} = ();
if ($event{WhereType} eq 1) {
my $roomID = nextLine() + 1;
push @{ $event{Where} }, $roomID;
}
if($event{WhereType} eq 2){
for my $room (1 .. $#Rooms){ push @{ $event{Where} }, $room if nextLine(); }
}
#number PauseTask
$event{PauseTask} = nextLine();
#truth PauserCompleted
$event{PauserCompleted} = nextLine();
#number PrefTime1
$event{PrefTime1} = nextLine();
#text PrefText1
$event{PrefText1} = nextLine();
#number ResumeTask
$event{ResumeTask} = nextLine();
#truth ResumerCompleted
$event{ResumerCompleted}= nextLine();
#number PrefTime2
$event{PrefTime2} = nextLine();
#text PrefText2
$event{PrefText2} = nextLine();
#number Obj2
$event{Obj2} = nextLine();
#number Obj2Dest
$event{Obj2Dest} = nextLine();
#number Obj3