-sub template {
- my ($self, $format) = @_;
- # total (flattened) unpack template from nested format definitions
- return join '', map {
- my $value = $format->[-($_ << 1) - 1];
- if (ref $value eq 'ARRAY') {
- my $count = $value->[0];
- $value = $self->template($value);
- $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
- : $count."X[$count]$count/($value)";
- }
- else {
- $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e; # length prefix
- }
- $value;
- } reverse 0 .. ($#$format - 1) >> 1;
-}
-
-sub convert {
- my ($self, $format, $data) = @_;
- # map flat results into a named and nested hash
- my %res;
- while (my ($field, $template) = splice @$format, 0, 2) {
- if (ref $template eq 'ARRAY') {
- my ($count, @subformat) = @$template;
- my $max = $count =~ s/^(\d+)// ? $1 : 0;
- $count = !$count ? $max
- : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
- $res{$field}->[$_] = $self->convert([@subformat], $data) for 0 .. ($max || $count)-1;
- splice @{ $res{$field} }, $count if $max > $count;
- $res{$field} = $res{$field}->[0] if $max == 1;
- next;
- }
- elsif ($template =~ /^Ca/) {
- $data->[0] = CORE::unpack 'C/a', $data->[0];
+ if ($variant eq 'single') {
+ $offset++;
+ $data->{finish}->{code} =
+ my $code = substr $data->{leveldata}, $offset, -5*$slots;
+
+ my %FINISHCODE = (
+ 0 => chr 0xC9, # ret
+ 1 => join('',
+ chr 0x21, # ld hl, MESSAGE
+ pack('v', $offsetbase + $offset + 9),
+ (map {chr}
+ 0xCD, 0x37, 0x4A, # call _puts
+ 0xC3, 0xAA, 0x55, # jp _getkey
+ ),
+ ),
+ 2 => join('',
+ (map {chr}
+ 0x21, 0, 0x1C, # ld hl, $POS
+ 0x22, 0x7C, 0xC3, # ld (_penCol), hl
+ 0x21, # ld hl, MESSAGE
+ ),
+ pack('v', $offsetbase + $offset + 15),
+ (map {chr}
+ 0xCD, 0xA5, 0x4A, # call _vputs
+ 0xC3, 0xAA, 0x55, # jp _getkey
+ ),
+ ),
+ );
+ while (my ($finish, $match) = each %FINISHCODE) {
+ $match eq substr $code, 0, length $match or next;
+ $data->{finish}->{type} = $finish and
+ $data->{finish}->{message} = unpack 'Z*', substr($code, length $match);
+ last;
+ }