X-Git-Url: http://git.shiar.nl/wormy.git/blobdiff_plain/6d64bb2aa29809c9c24fb5ee036766440b62bf4a..HEAD:/parse-wormedit diff --git a/parse-wormedit b/parse-wormedit index 4c4358f..06a261d 100755 --- a/parse-wormedit +++ b/parse-wormedit @@ -1,7 +1,8 @@ #!/usr/bin/env perl +use 5.010; use strict; use warnings; -use 5.010; +use experimental 'switch'; use lib 'lib'; # make runnable for simple cases use Data::Dumper; @@ -9,12 +10,17 @@ use Getopt::Long 2.33 qw(HelpMessage :config bundling); use Games::Wormy::TICalcLevels; use Games::Wormy::WormEdit; -our $VERSION = '1.05'; +our $VERSION = '1.07'; GetOptions(\my %opt, - 'raw|r', # full output + 'format|f=s', # output type + 'raw|r!', # compatibility for yaml format 'version=i', # force version + 'levels|render:i', # image of level(s) + 'output|o=s', # output file ) or HelpMessage(-exitval => 2); +$opt{format} //= 'yaml' if $opt{raw}; +$opt{format} //= 'pnm' if defined $opt{levels}; my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle'); my @ENDTYPE = ('none', 'message', 'small message'); @@ -46,8 +52,32 @@ else { die "Unrecognised file type\n"; } +my $format = $opt{format}; # override distinct from image fallback +if ($opt{output}) {{ + # derive format from file extension + $format //= $1 if $opt{output} =~ /\.([^.]+)$/; + + if ($format ~~ [qw{ yaml json txt }]) { + # redirect standard output to given file + open my $output, '>', $opt{output} + or die "Cannot output to '$opt{output}': $!"; + select $output; + } + else { + # images are written directly to file + } +}} +else { + $format //= 'txt'; +} + # output with user-preferred formatting -if ($opt{raw}) { +given ($format) { +when ('json') { + require JSON; + say JSON->new->encode($data); +} +when ('yaml') { # full data in yaml (human-readable) formatting require YAML; local $YAML::CompressSeries; @@ -69,7 +99,7 @@ if ($opt{raw}) { print $yml; } -else { +when ('txt') { print $data->{name}; print " ($data->{description})" if defined $data->{description}; print "\n"; @@ -122,6 +152,33 @@ else { } 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 levels + @request = 0 .. $data->{levelcount}->{total} - 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 ($format ~~ 'pbm') { + $img = $img->to_paletted({make_colors => 'mono'}); + $opt{format} = 'pnm'; + } + $img->write( + $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT), + type => $opt{format}, + ) or die $img->errstr; +} +} __END__ @@ -131,12 +188,36 @@ parse-wormedit - Wormy level data parser =head1 SYNOPSIS - parse-wormedit [--raw] + parse-wormedit [--format=] [--level=] [--output=] =head1 DESCRIPTION Reads Wormy levels (either original WormEdit source or compiled TI-86 string) -from STDIN or given file, and outputs contents, summarised or in full. +from STDIN or given file, and prints parsed data to STDOUT. + +If an I file name is given, its extension determines the format, +otherwise explicitly given by the I option: + +=over 6 + +=item txt + +Plain text summary of levelpack contents. + +=item yaml + +All parsed data in YAML syntax. + +=item json + +Parsed data in JSON syntax. + +=item pnm, png, bmp, ... + +Image drawing of rendered levels. +Unrecognised values are interpreted as file type and converted by Imager. + +=back =head1 AUTHOR