X-Git-Url: http://git.shiar.nl/wormy.git/blobdiff_plain/effab7820acc8ce86c99cab89a516dbaabc1b86c..5b16db3a9d109027d02c3281c8e2b61ec32c13ca:/parse-wormedit diff --git a/parse-wormedit b/parse-wormedit index 99daaa1..aa22051 100755 --- a/parse-wormedit +++ b/parse-wormedit @@ -54,12 +54,12 @@ else { if ($opt{output}) {{ # derive format from file extension - if ($opt{output} =~ /\.yaml$/) { - $opt{format} //= 'yaml'; + if ($opt{output} =~ /\.(yaml|json|txt)$/) { + $opt{format} //= $1 } - elsif ($opt{output} !~ /\.txt$/) { - $opt{format} //= 'pnm'; - last; # images are written directly to file + else { + # images are written directly to file + last; } # redirect standard output to given file @@ -70,26 +70,9 @@ if ($opt{output}) {{ # output with user-preferred formatting given ($opt{format}) { -when ('pnm') { - require Games::Wormy::Render; - - my @request; - if ($opt{levels}) { - # find all numeric values in argument - @request = $opt{levels} =~ /(\d+)/g; - } - else { - # default to all singleplayer levels - @request = 0 .. $data->{levelcount}->{single} - 1; - } - @request or die "no levels found or specified\n"; - - my $img = Games::Wormy::Render->composite( - map { $data->{levels}->[$_] } @request - ) or die "empty result for levels\n"; - $img->write( - $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm') - ) or die $img->errstr; +when ('json') { + require JSON; + say JSON->new->encode($data); } when ('yaml') { # full data in yaml (human-readable) formatting @@ -113,7 +96,7 @@ when ('yaml') { print $yml; } -default { +when ('txt') { print $data->{name}; print " ($data->{description})" if defined $data->{description}; print "\n"; @@ -166,6 +149,32 @@ default { } print "\n"; } +default { + require Games::Wormy::Render; + + my @request; + if ($opt{levels}) { + # find all numeric values in argument + @request = $opt{levels} =~ /(\d+)/g; + } + else { + # default to all singleplayer levels + @request = 0 .. $data->{levelcount}->{single} - 1; + } + @request or die "no levels found or specified\n"; + + my $img = Games::Wormy::Render->composite( + map { $data->{levels}->[$_] } @request + ) or die "empty result for levels\n"; + if ($opt{format} ~~ 'pbm') { + $img = $img->to_paletted({make_colors => 'mono'}); + $opt{format} = 'pnm'; + } + $img->write( + $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT), + type => $opt{format} // 'pnm', + ) or die $img->errstr; +} } __END__ @@ -196,6 +205,10 @@ Plain text summary of levelpack contents. All parsed data in YAML syntax. +=item json + +Parsed data in JSON syntax. + =item pnm, png, bmp, ... Image drawing of rendered levels.