parse-wormedit: declare hardcoded bytes in unpackf template
authorMischa Poslawsky <wormy@shiar.org>
Tue, 3 Mar 2009 14:00:08 +0000 (15:00 +0100)
committerMischa Poslawsky <wormy@shiar.org>
Wed, 4 Mar 2009 14:28:21 +0000 (15:28 +0100)
Recognise a new template value '=$N' (where $N is any number) in
Parse::Binary::Nested to insert a hardcoded value instead of reading it.
This allows object structures of v90 files without type declarations to
be returned in the same format as newer versions.  Still requires some
custom merging of the multiple arrays afterwards, but this may be solved
later on.

Parse/Binary/Nested.pm
parse-wormedit

index 9bac394264d2a35709bc5fc4caa255c18ac944f0..872ccaac1e772d0b97384c1b93b2307cd8f0f715 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 use Carp;
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 
 sub new {
        my ($class, $format) = @_;
@@ -26,6 +26,7 @@ sub template {
                                : $count."X[$count]$count/($value)";
                }
                else {
+                       $value =~ s/=\d*//g;  # hardcoded values
                        $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e;  # length prefix
                }
                $value;
@@ -53,6 +54,10 @@ sub convert {
                elsif ($template =~ /^(?:[xX]\d*)*$/) {
                        next;  # no values
                }
+               elsif ($template =~ /=(\d+)?/) {
+                       $res{$field} = $1;
+                       next;
+               }
                $res{$field} = shift @$data;
        }
        return \%res;
@@ -117,6 +122,21 @@ to its length.
 This behaviour is very similar to C<(C/a@x$length)>, except that it never reads
 more than the given number of bytes.
 
+=item *
+
+Hardcoded values can be inserted using C<=$number> values.
+This can for example be useful to retain forwards-compatibility:
+
+       rows => ['C',
+               type => '=1', # nothing read
+               data => 'S',
+       ]
+       
+       rows => ['C',
+               type => 'C',
+               data => 'S',
+       ]
+
 =back
 
 =head1 AUTHOR
index 81c073418c9d1c11f86e956867ec1bba20e9ed9e..5761a30107a585d2af0f172d9e607c9a05b1fb26 100755 (executable)
@@ -283,9 +283,10 @@ sub read {
                                $_->[13] = $_->[15];  # ctf
                                $_->[15] = 'domination';
                        } for @{ $FORMAT[9] }; # no multifood
-                       push @LEVELFORM, "object$_" => ['C',
+                       push @LEVELFORM, "objects$_" => ['C',
+                               type => "=$_",
                                map {$_ => 'C'} qw(x1 y1 x2 y2)
-                       ] for qw(lines boxes);
+                       ] for 2, 3;
                }
                default {
                        die "Unsupported level version $version\n";
@@ -354,8 +355,8 @@ sub read {
                        # add objects until terminator
                        $level->{objects} = [];
                if ($version <= 91) {
-                       push @{ $level->{objects} }, { %$_, type => 2 } for map { $level->{$_} ? @{ $level->{$_} } : () } qw(objectlines);
-                       push @{ $level->{objects} }, { %$_, type => 3 } for map { $level->{$_} ? @{ $level->{$_} } : () } qw(objectboxes);
+                       ref $_ eq 'ARRAY' and push @{ $level->{objects} }, @$_
+                               for map { delete $level->{"objects$_"} } 2, 3;
                        $size += 1 + 4 * scalar @{ $level->{objects} };
                }
                else {