7 use Getopt::Long 2.33 qw(HelpMessage :config bundling);
12 'raw|r', # full output
13 'version=i', # force version
14 ) or HelpMessage(-exitval => 2);
17 "WormEdit053\000LVL" => 53,
18 "WormEdit\34195\000LVL" => 95,
19 "WormEdit\34194\000LVL" => 94,
20 "WormEdit\34193\000LVL" => 93,
27 description => 'Ca64x256',
36 map { (start => $_, end => $_) } [1,
53 enddata => 'Ca255x256',
55 levels => ['*', # levelcount->total actually
92 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
93 my @ENDTYPE = ('none', 'message', 'small message');
97 my @objtypes = map { $_->{type} } @$objects;
99 $count{$_}++ for @objtypes;
100 return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
101 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
106 # read and parse all input data
108 my $rawdata = readline;
109 my ($id, $subid) = (substr($rawdata, 0, 15), ord substr($rawdata, 15, 1));
110 my $version = $opt{version} // $MAGICID{$id}
111 or die "File does not match any known WormEdit level header\n";
113 or warn "Unsupported version $subid (expecting $version)\n";
118 when ($_ <= 95 and $_ > 90) {
119 ref $_ and pop @$_ for @{ $FORMAT[11] }; # only 8 moderefs
120 $FORMAT[-1]->[-1]->[0] = '32C'; # less objects
124 $FORMAT[7] = 'Ca64'; # no reserved space after description
125 #ref $_ and $_->[-1] = 'C' for @{ $FORMAT[11] }; # only 9 moderefs
126 $FORMAT[19] = 'Ca255'; # enddata
127 splice @FORMAT, 6, 2 if $subid < 95; # early (sub)version without description
129 when ($_ <= 94 and $_ > 90) {
130 splice @FORMAT, 6, 2; # no description
131 splice @{ $FORMAT[7] }, 4, 2; # no race
132 splice @FORMAT, 16, 2; # no enddata
133 splice @{ $FORMAT[-1] }, 1, 2; # no name
137 splice @FORMAT, 16, 2; # no hiname
138 $FORMAT[-1]->[0] = 64; # constant amount of levels
141 die "Cannot parse data for Wormedit $version\n";
144 my @rawdata = unpack Shiar_Parse::Nested->template(\@FORMAT).'a*', $rawdata;
146 # convert to an easily accessible hash
147 my $data = Shiar_Parse::Nested->convert(\@FORMAT, \@rawdata);
148 warn "Trailing data left unparsed\n" if grep {length} @rawdata;
150 # output with user-preferred formatting
153 my $output = JSON::XS->new->ascii->canonical->pretty->allow_nonref;
154 print $output->encode($data), "\n";
158 print " ($data->{description})" if defined $data->{description};
160 printf "File version: %s\n", "WormEdit v$data->{version}";
161 printf "Defaults: %s\n", join('; ',
162 'sprite ' . scalar @{ $data->{sprite} },
163 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
167 for my $variant (qw/single multi race ctf/) {
168 my $count = $data->{levelcount}->{$variant};
170 printf '%s (%s)', ucfirst $variant, $count // 'invalid';
173 printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
174 $_->{id} || $_->{name},
175 @$_{qw/size bsize growth/},
176 $variant eq 'single' && "x$_->{peas}",
177 @$_{qw/width height/},
178 join(';', map {" $_"} grep {$_}
179 @{$_->{objects}} && sprintf('%2d object%s (%s)',
180 scalar @{$_->{objects}}, @{$_->{objects}} != 1 && 's',
181 objsummary($_->{objects}),
183 @{$_->{sprite}} && sprintf('sprite %d',
184 scalar @{$_->{sprite}},
187 ) for map { $data->{levels}->[$_ + $startnr] }
193 printf("-- %-21s%4s: %s (%s)\n",
195 defined $data->{enddata} ? length $data->{enddata} : '?',
196 $ENDTYPE[$data->{endtype}] || 'unknown', $data->{endstr},
197 ) if $variant eq 'single';
201 package Shiar_Parse::Nested;
204 my ($self, $format) = @_;
205 # total (flattened) unpack template from nested format definitions
206 return join '', map {
207 my $value = $format->[-($_ << 1) - 1];
208 if (ref $value eq 'ARRAY') {
209 my $count = $value->[0];
210 $value = $self->template($value);
211 $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
212 : $count."X[$count]$count/($value)";
215 $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e; # length prefix
218 } reverse 0 .. ($#$format - 1) >> 1;
222 my ($self, $format, $data) = @_;
223 # map flat results into a named and nested hash
225 while (my ($field, $template) = splice @$format, 0, 2) {
226 if (ref $template eq 'ARRAY') {
227 my ($count, @subformat) = @$template;
228 my $max = $count =~ s/^(\d+)// ? $1 : 0;
229 $count = !$count ? $max
230 : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
232 $res{$field}->[$_] = $self->convert([@subformat], $data) for 0 .. $max-1;
233 splice @{ $res{$field} }, $count if $max > $count;
234 $res{$field} = $res{$field}->[0] if $max == 1;
237 elsif ($template =~ /^Ca/) {
238 $data->[0] = unpack 'C/a', $data->[0];
240 $res{$field} = shift @$data;
249 parse-wormedit - WormEdit level data parser
253 parse-wormedit [--raw] <input.lvl>
257 Reads WormEdit v0.53 levels from STDIN or given file,
258 and outputs contents, summarised or in full.
262 Mischa POSLAWSKY <wormy@shiar.org>