Parse::Binary::Nested: byte-terminated groups
[wormy.git] / parse-wormedit
index ed64118e23ef31f7f7f8fc8cce90c9b6427c7b9a..77580676baee866cb3e67ad1a7fa4c6f41146143 100755 (executable)
@@ -222,6 +222,10 @@ sub read {
                        line => 'B8',
                ],
                leveldata => 'a*',
+               #levels
+               #finish code
+               #levels-multi
+               #hinames
        );
        my @LEVELFORM = (
                peas       => 'C',
@@ -247,17 +251,13 @@ sub read {
                        y => 'C',
                        x => 'C',
                ],
-               #levels
-               #finish code
-               #levels-multi
-               #hinames
-       );
-       my @OBJECTFORM = (
+               objects    => ['?0',
                        type => 'C',
                        x1   => 'C',
                        y1   => 'C',
                        x2   => 'C',
                        y2   => 'C',
+               ],
        );
        my $offsetbase = 0xF080;
 
@@ -283,6 +283,7 @@ sub read {
                                $_->[13] = $_->[15];  # ctf
                                $_->[15] = 'domination';
                        } for @{ $FORMAT[9] }; # no multifood
+                       splice @LEVELFORM, -2;
                        push @LEVELFORM, "objects$_" => ['C',
                                type => "=$_",
                                map {$_ => 'C'} qw(x1 y1 x2 y2)
@@ -329,8 +330,8 @@ sub read {
                my @varform = @LEVELFORM;
                $varform[13]->[0] = $variant eq 'single' ? 1 : 4;
                unshift @varform, name => 'Z*' unless $variant eq 'single' or $version <= 91;
-               $varform[-1]->[0] = 1 if $variant eq 'race' and $version > 91;
-               $varform[-1]->[0] = 2 if $variant eq 'ctf';
+               $varform[-3]->[0] = 1 if $variant eq 'race' and $version > 91;
+               $varform[-3]->[0] = 2 if $variant eq 'ctf';
                push @varform, size => '=.';
                my $parselevel = Parse::Binary::Nested->new(\@varform);
 
@@ -344,13 +345,6 @@ sub read {
                        }
 
                        my $level = $parselevel->unpackf(substr $data->{leveldata}, $offset);
-                       my $size = 8  # unpack length (ugh, ugly recalculation)
-                               + (defined $level->{name} ? 1 + length $level->{name} : 0)
-                               + 3 * (ref $level->{worms} eq 'ARRAY' ? scalar @{$level->{worms}} : 1)
-                               + 2 * ($level->{flags} ? ref $level->{flags} eq 'ARRAY' ? scalar @{$level->{flags}} : 1 : 0)
-                               + ($level->{sprite} ? scalar @{$level->{sprite}} : 0)
-                               + ($level->{balls} ? 3 * scalar @{$level->{balls}} : 0);
-                       $level->{sizecalc} = $size;
                        $level->{offset} = $offset + $offsetbase;
 
                        # add objects until terminator
@@ -359,14 +353,6 @@ sub read {
                        ref $_ eq 'ARRAY' and push @{ $level->{objects} }, @$_
                                for map { delete $level->{"objects$_"} } 2, 3;
                }
-               else {
-                       while (my $object = ord substr($data->{leveldata}, $offset+$size, 1)) {
-                               push @{ $level->{objects} }, Parse::Binary::Nested->new([@OBJECTFORM])->unpackf(
-                                       substr $data->{leveldata}, $offset+$size, 5
-                               );
-                               $size += 5;
-                       }
-               }
 
                        # add parsed level and advance
                        push @{ $data->{levels} }, $level;