--- deliantra/server/utils/cfutil.in 2007/04/18 09:26:46 1.28 +++ deliantra/server/utils/cfutil.in 2007/07/26 21:44:43 1.47 @@ -8,9 +8,10 @@ my $DATADIR = "@datadir@/@PACKAGE@"; my $CONVERT = "@CONVERT@"; -my $IDENTIFY = "@IDENTIFY@"; +#my $IDENTIFY = "@IDENTIFY@"; my $OPTIPNG = "@OPTIPNG@"; my $RSYNC = "@RSYNC@"; +my $PNGNQ = "@PNGNQ@"; use Getopt::Long; use Coro::Event; @@ -22,8 +23,11 @@ use Coro::AIO; use POSIX (); use Digest::MD5; +use Carp; use Coro::Storable; $Storable::canonical = 1; +$SIG{QUIT} = sub { Carp::cluck "QUIT" }; + sub usage { warn < 0, + white => 1, + navy => 2, + red => 3, + orange => 4, + blue => 5, + darkorange => 6, + green => 7, + lightgreen => 8, + grey => 9, + brown => 10, + gold => 11, + tan => 12, +); + END { system "rm", "-rf", $TMPDIR } Event->signal (signal => "INT", cb => sub { exit 1 }); @@ -70,14 +90,14 @@ sub inst_maps($) { my (undef, $path) = @_; - print "installing '$path' to '$DATADIR/maps'\n"; + print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; if (!-f "$path/regions") { warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; exit 1 unless $FORCE; } - system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" + system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" and die "map installation failed.\n"; print "maps installed successfully.\n"; @@ -86,13 +106,16 @@ { our %ANIMINFO; our %FACEINFO; + our %RESOURCE; + our @ARC; our %ARC; our $TRS; our $NFILE; + our $PATH; our $QUANTIZE = "+dither -colorspace RGB -colors 256"; - our (@png, @trs, @arc); # files we are interested in + our (@png, @trs, @arc, @res); # files we are interested in sub commit_png($$$) { my ($name, $data, $T) = @_; @@ -155,7 +178,7 @@ . "| $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 $? ($!)"; - system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; + system $OPTIPNG, "-i0", "-q", "$other~"; die "$other~ has zero size, aborting." unless -s "$other~"; rename "$other~", $other; }; @@ -171,7 +194,25 @@ if (0 > aio_lstat $other or (-M _) > (-M $path)) { fork_sub { system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; - system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; + system $OPTIPNG, "-i0", "-q", "$other~"; + + # reduce smoothfaces >10000 bytes + if ($stem =~ /_S\./ && (-s "$other~") > 10000) { + my $ncolor = 256; + while () { + system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; + system $OPTIPNG, "-i0", "-q", "$other~~"; + last if 10000 > -s "$other~~"; + $ncolor = int $ncolor * 0.9; + $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; + } + + printf "reduced %s from %d to %d bytes using %d colours.\n", + $other, -s "$other~", -s "$other~~", $ncolor + if $VERBOSE >= 2; + rename "$other~~", "$other~"; + } + die "$other~ has zero size, aborting." unless -s "$other~"; rename "$other~", $other; }; @@ -257,7 +298,10 @@ my $arc = read_arch "$dir/$file"; for my $o (values %$arc) { - $ARC{$o->{_name}} = $o; + push @ARC, $o; + for (my $m = $o; $m; $m = $m->{more}) { + $ARC{$m->{_name}} = $m; + } $o->{editor_folder} = $dir; @@ -280,8 +324,8 @@ $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; - my $visibility = delete $o->{visibility} if exists $o->{visibility}; - my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; + $visibility = delete $o->{visibility} if exists $o->{visibility}; + $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; my $anim = delete $o->{anim}; @@ -346,6 +390,50 @@ } } + my %FILECACHE; + + sub load_cached($;$) { + unless (exists $FILECACHE{$_[0]}) { + my $data; + if (0 < aio_load $_[0], $data) { + $data = $_[1]->($data) + if $_[1]; + } + + $FILECACHE{$_[0]} = $data; + } + + $FILECACHE{$_[0]} + } + + sub process_res { + while (@res) { + my ($dir, $file, $type) = @{pop @res}; + + my $data; + aio_load "$dir/$file", $data; + + my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift }; + + $file =~ s/\.res$//; + $file =~ s/\.(ogg|wav|jpg|png)$//; + + substr $dir, 0, 1 + length $PATH, ""; + + $meta = { + %{ $meta->{"" } || {} }, + %{ $meta->{$file} || {} }, + }; + + $RESOURCE{"$dir/$file"} = { + type => (delete $meta->{type}) || $type, + data => $data, + chksum => (Digest::MD5::md5 $data), + %$meta ? (meta => $meta) : (), + }; + } + } + sub find_files; sub find_files { my ($path) = @_; @@ -357,15 +445,35 @@ 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]; + } else { - warn "ignoring $path/$file\n" if $VERBOSE >= 2; + warn "ignoring $path/$file\n" if $VERBOSE >= 3; } } }; @@ -374,7 +482,10 @@ sub inst_arch($) { my (undef, $path) = @_; - print "Installing '$path' to '$DATADIR'\n", + $PATH = $path; + + print "\n", + "Installing '$path' to '$DATADIR'\n", "\n", "This can take a long time if you run this\n", "for the first time or do not use --cache.\n", @@ -396,6 +507,7 @@ (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_trs), (async \&process_trs), (async \&process_arc), (async \&process_arc), + (async \&process_res), (async \&process_res), ); { @@ -415,7 +527,9 @@ $loop = $s; } else { delete $o->{inherit}; - %$o = ( %$s, %$o ); + my %s = %$s; + delete @s{qw(_name more name name_pl)}; + %$o = ( %s, %$o ); ++$progress; } } else { @@ -433,9 +547,12 @@ } } + # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit + @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; + open my $fh, ">:utf8", "$DATADIR/archetypes~" or die "$DATADIR/archetypes~: $!"; - print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } values %ARC]; + print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; } { @@ -454,6 +571,11 @@ $v->{chksum32} = Digest::MD5::md5 $v->{data32}; $v->{chksum64} = Digest::MD5::md5 $v->{data64}; + + if (my $magicmap = $v->{magicmap}) { + $magicmap =~ y/A-Z_\-/a-z/d; + $v->{magicmap} = $COLOR{$magicmap}; + } } open my $fh, ">:perlio", "$DATADIR/facedata~" @@ -463,6 +585,7 @@ version => 2, faceinfo => \%FACEINFO, animinfo => \%ANIMINFO, + resource => \%RESOURCE, }; }