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 package Shiar_Parse::WormEdit;
23 "WormEdit053\000LVL" => 53,
24 "WormEdit\34195\000LVL" => 95,
25 "WormEdit\34194\000LVL" => 94,
26 "WormEdit\34193\000LVL" => 93,
33 description => 'Ca64x256',
42 map { (start => $_, end => $_) } [1,
59 enddata => 'Ca255x256',
61 levels => ['*', # levelcount->total actually
99 my ($self, $input) = @_;
100 my ($id, $subid) = (substr($input, 0, 15), ord substr($input, 15, 1));
101 my $version = $opt{version} // $MAGICID{$id}
102 or die "File does not match any known WormEdit level header\n";
104 or warn "Unsupported version $subid (expecting $version)\n";
109 when ($_ <= 95 and $_ > 90) {
110 ref $_ and pop @$_ for @{ $FORMAT[11] }; # only 8 moderefs
111 $FORMAT[-1]->[-1]->[0] = '32C'; # less objects
115 $FORMAT[7] = 'Ca64'; # no reserved space after description
116 #ref $_ and $_->[-1] = 'C' for @{ $FORMAT[11] }; # only 9 moderefs
117 $FORMAT[19] = 'Ca255'; # enddata
118 splice @FORMAT, 6, 2 if $subid < 95; # early (sub)version without description
120 when ($_ <= 94 and $_ > 90) {
121 splice @FORMAT, 6, 2; # no description
122 splice @{ $FORMAT[7] }, 4, 2; # no race
123 splice @FORMAT, 16, 2; # no enddata
124 splice @{ $FORMAT[-1] }, 1, 2; # no name
128 splice @FORMAT, 16, 2; # no hiname
129 $FORMAT[-1]->[0] = 64; # constant amount of levels
132 die "Cannot parse data for Wormedit $version\n";
136 # convert to an easily accessible hash
137 my @values = unpack Shiar_Parse::Nested->template(\@FORMAT).'a*', $input;
138 my $data = Shiar_Parse::Nested->convert(\@FORMAT, \@values);
139 warn "Trailing data left unparsed\n" if grep {length} @values;
140 $data->{format} = 'WormEdit';
145 package Shiar_Parse::WormyLevel;
148 my ($self, $input) = @_;
149 my ($psize, $ptype, $size, $type, $vsize, $dsize, $id, $subid) = unpack q{
150 x11 x42 # file signature and comment
151 S a2 S a2 # file size, type; data size, type
153 S S # var size; content size
157 or die "Not a calculator string, thus cannot be a Wormy level file\n";
159 or warn "File size ($size) does not correspond with data size ($psize)\n";
161 or die "Not a calculator string, thus cannot be a Wormy level file\n";
162 $size == $vsize and $vsize == $dsize+2
163 or warn "Mismatch in string data size declarations\n";
164 # substr($input, -2) eq $CHECKSUM
166 $input = substr $input, 73, -2;
168 or die "Wormy level identifier not found\n";
179 start => [1, map {$_ => 'S'} @$_],
180 end => [1, map {$_ => 'C'} @$_],
182 [qw/single peaworm tron deathmatch foodmatch multifood timematch race ctf/]
184 theanswer => 'C', # 42
227 die "Unsupported level version $subid\n";
231 my $data = Shiar_Parse::Nested->unpack(\@FORMAT, $input);
232 while (length $data->{leveldata}) {
233 my $level = Shiar_Parse::Nested->unpack([@LEVELFORM], $data->{leveldata});
235 + 3 * (ref $level->{worms} eq 'ARRAY' ? scalar @{$level->{worms}} : 1)
236 + ($level->{sprite} ? scalar @{$level->{sprite}} : 0)
237 + ($level->{balls} ? 3 * scalar @{$level->{balls}} : 0);
238 $level->{objects} = [];
239 while (my $object = ord substr($data->{leveldata}, $offset, 1)) {
240 push @{ $level->{objects} }, Shiar_Parse::Nested->unpack(
241 [@OBJECTFORM], substr($data->{leveldata}, $offset, 5)
245 $level->{size} = $offset;
247 push @{ $data->{levels} }, $level;
248 substr($data->{leveldata}, 0, $offset) = '';
249 last if substr($data->{leveldata}, 0, 1) eq chr(255);
252 $data->{hinames} = [ unpack '(a3)*', substr($data->{leveldata}, -3 * $slots) ];
253 $data->{enddata} = substr delete($data->{leveldata}), 0, -3 * $slots;
254 $data->{format} = '86s';
255 $data->{levelcount}->{single} = scalar @{ $data->{levels} };
260 package Shiar_Parse::Nested;
263 my ($self, $format) = @_;
264 # total (flattened) unpack template from nested format definitions
265 return join '', map {
266 my $value = $format->[-($_ << 1) - 1];
267 if (ref $value eq 'ARRAY') {
268 my $count = $value->[0];
269 $value = $self->template($value);
270 $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
271 : $count."X[$count]$count/($value)";
274 $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e; # length prefix
277 } reverse 0 .. ($#$format - 1) >> 1;
281 my ($self, $format, $data) = @_;
282 # map flat results into a named and nested hash
284 while (my ($field, $template) = splice @$format, 0, 2) {
285 if (ref $template eq 'ARRAY') {
286 my ($count, @subformat) = @$template;
287 my $max = $count =~ s/^(\d+)// ? $1 : 0;
288 $count = !$count ? $max
289 : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
290 $res{$field}->[$_] = $self->convert([@subformat], $data) for 0 .. ($max || $count)-1;
291 splice @{ $res{$field} }, $count if $max > $count;
292 $res{$field} = $res{$field}->[0] if $max == 1;
295 elsif ($template =~ /^Ca/) {
296 $data->[0] = CORE::unpack 'C/a', $data->[0];
298 $res{$field} = shift @$data;
304 my ($self, $format, $input) = @_;
305 my @data = CORE::unpack $self->template($format), $input;
306 return $self->convert($format, \@data);
312 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
313 my @ENDTYPE = ('none', 'message', 'small message');
317 my @objtypes = map { $_->{type} } @$objects;
319 $count{$_}++ for @objtypes;
320 return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
321 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
326 # read and parse all input data
329 my $rawdata = readline;
330 if (substr($rawdata, 0, 11) eq "**TI86**\032\012\000") {
331 # compiled calculator file
332 $data = Shiar_Parse::WormyLevel->read($rawdata);
334 elsif (substr($rawdata, 0, 8) eq 'WormEdit') {
335 # original wormedit source
336 $data = Shiar_Parse::WormEdit->read($rawdata);
339 die "Unrecognised file type\n";
342 # output with user-preferred formatting
345 my $output = JSON::XS->new->ascii->canonical->pretty->allow_nonref;
346 print $output->encode($data), "\n";
350 print " ($data->{description})" if defined $data->{description};
352 printf "File version: %s\n", "$data->{format} v$data->{version}";
353 printf "Defaults: %s\n", join('; ',
354 'sprite ' . scalar @{ $data->{sprite} },
355 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
359 for my $variant (qw/single multi race ctf/) {
360 my $count = $data->{levelcount}->{$variant};
362 printf '%s (%s)', ucfirst $variant, $count // 'invalid';
365 for (0 .. $count - 1) {
366 my $level = $data->{levels}->[$_ + $startnr];
367 printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
368 $level->{id} || $level->{name} || '#'.($_+1),
369 @$level{qw/size bsize growth/},
370 $variant eq 'single' && "x$level->{peas}",
371 @$level{qw/width height/},
372 join(';', map {" $_"} grep {$_}
373 @{$level->{objects}} && sprintf('%2d object%s (%s)',
374 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
375 objsummary($level->{objects}),
377 $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
378 scalar @{$level->{sprite}},
380 $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
381 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
390 printf("-- %-21s%4s: %s (%s)\n",
392 defined $data->{enddata} ? length $data->{enddata} : '?',
393 defined $data->{endtype} ? $ENDTYPE[$data->{endtype}] || 'unknown' : 'code',
394 $data->{endstr} // '?',
395 ) if $variant eq 'single';
403 parse-wormedit - WormEdit level data parser
407 parse-wormedit [--raw] <input.lvl>
411 Reads WormEdit v0.53 levels from STDIN or given file,
412 and outputs contents, summarised or in full.
416 Mischa POSLAWSKY <wormy@shiar.org>