--- cf.schmorp.de/server/utils/cfutil.in 2007/07/12 08:40:14 1.38 +++ cf.schmorp.de/server/utils/cfutil.in 2007/08/18 22:25:35 1.52 @@ -21,10 +21,14 @@ use Crossfire; use Coro; use Coro::AIO; +use Coro::Util; use POSIX (); use Digest::MD5; +use Carp; use Coro::Storable; $Storable::canonical = 1; +$SIG{QUIT} = sub { Carp::cluck "QUIT" }; + sub usage { warn < aio_lstat $other or (-M _) > (-M $path)) { - my $wrap = 0; # for the time being fork_sub { - system "convert png:\Q$path\E -depth 8 rgba:-" - . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" - . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" - and die "convert/hq2xa pipeline error: status $? ($!)"; + my $CROP; + my $SRC = "png:\Q$path\E"; + + # check if this is a wall. ultra-ugly. ultra-ultra-ugly. + if ($path =~ /^(.*\/wall\/.*_)([0-9A-F])(\.x11.*\.png)$/) { + my ($pfx, $dir, $sfx) = ($1, hex $2, $3); + #check for 0..F images to be sure(?) this is a wall + unless (grep { !-e sprintf "%s%X%s", $pfx, $_, $sfx } 0..15) { + # add a 4px border and add other images around it + $CROP = "-shave 8x8 +repage"; + + $w += 8; + $h += 8; + + $SRC = "-size ${w}x${h} xc:transparent"; + $SRC .= " png:\Q$path\E -geometry +4+4 -composite"; + + # 8 surrounding images + for ( + # x y b r0 r1 + [-1, -1, 0, 6], + [ 0, -1, 1, 10, 14], + [+1, -1, 0, 12], + + [-1, 0, 8, 5, 7], + # + [+1, 0, 2, 5, 13], + + [-1, +1, 0, 3], + [ 0, +1, 4, 10, 11], + [+1, +1, 0, 9], + ) { + my ($x, $y, $d, $r0, $r1) = @$_; + $SRC .= sprintf " png:%s%X%s -geometry %+d%+d -composite", + "\Q$pfx", + ($dir & $d) ? $r1 : $r0, + "\Q$sfx", + $x * ($w - 8) + 4, + $y * ($h - 8) + 4; + } + } + } + + system "convert -depth 8 $SRC rgba:-" + . "| $exec_prefix/bin/cfhq2xa $w $h 0" + . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~" + and die "convert/cfhq2xa pipeline error: status $? ($!)"; system $OPTIPNG, "-i0", "-q", "$other~"; die "$other~ has zero size, aborting." unless -s "$other~"; rename "$other~", $other; @@ -387,29 +433,61 @@ } } + my %FILECACHE; + + sub load_cached($;$) { + unless (exists $FILECACHE{$_[0]}) { + my $data; + if (0 < aio_load $_[0], $data) { + if ($_[1]) { + $data = eval { $_[1]->($data) }; + warn "$_[0]: $@" if $@; + } + } + + $FILECACHE{$_[0]} = $data; + } + + $FILECACHE{$_[0]} + } + sub process_res { while (@res) { - my ($dir, $file) = @{pop @res}; + my ($dir, $file, $type) = @{pop @res}; my $data; aio_load "$dir/$file", $data; - my $copyright; - aio_load "$dir/copyright", $copyright; + my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift }; - $file =~ s/\.res$//; - $file =~ /\.([^.]+)$/ - or next; + next if $meta && !exists $meta->{$file}; + + $meta = { + %{ $meta->{"" } || {} }, + %{ $meta->{$file} || {} }, + }; - my $type = $1; + if ($meta->{license} =~ s/^#//) { + $meta->{license} = ({ + "pd" => "Public Domain", + "gpl" => "GNU General Public License, version 3.0 or any later", + "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", + "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.5/", + "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", + })->{$meta->{license}} + || warn "$dir/$file: license tag '$meta->{license}' not found."; + } + + $file =~ s/\.res$//; + $file =~ s/\.(ogg|wav|jpg|png)$//; substr $dir, 0, 1 + length $PATH, ""; $RESOURCE{"$dir/$file"} = { - type => $1, - copyright => $copyright, - data => $data, - chksum => Digest::MD5::md5 $data, + type => (delete $meta->{type}) || $type, + data => $data, + chksum => (Digest::MD5::md5 $data), + %$meta ? (meta => $meta) : (), }; } } @@ -425,15 +503,33 @@ find_files "$path/$_" for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; + my $dir = $path; + substr $dir, 0, 1 + length $PATH, ""; + for my $file (@$nondirs) { - if ($file =~ /\.png$/) { + if ($dir =~ /^music(?:\/|$)/) { + push @res, [$path, $file, 3] # FT_MUSIC + if $file =~ /\.(ogg)$/; + + } elsif ($dir =~ /^sound(?:\/|$)/) { + push @res, [$path, $file, 5] # FT_SOUND + if $file =~ /\.(wav|ogg)$/; + + } elsif ($dir =~ /^res(?:\/|$)/) { + push @res, [$path, $file, 0] # FT_FACE + if $file =~ /\.(jpg|png)$/; + push @res, [$path, $file, 7] # FT_RSRC + if $file =~ /\.(res)$/; + + } elsif ($file =~ /\.png$/) { push @png, ["$path/$file", 0]; + } elsif ($file =~ /\.trs$/) { push @trs, [$path, $file]; + } elsif ($file =~ /\.arc$/) { push @arc, [$path, $file]; - } elsif ($file =~ /\.(ogg|res)$/) { - push @res, [$path, $file]; + } else { warn "ignoring $path/$file\n" if $VERBOSE >= 3; }