ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.31
Committed: Thu Apr 26 00:39:18 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.30: +21 -2 lines
Log Message:
require pngnq and automatically requantise too large 32x32 smooth faces so gcfclient doesn't nuke

File Contents

# User Rev Content
1 root 1.1 #!@PERL@
2    
3 root 1.2 use strict;
4    
5     my $prefix = "@prefix@";
6     my $exec_prefix = "@exec_prefix@";
7     my $datarootdir = "@datarootdir@";
8     my $DATADIR = "@datadir@/@PACKAGE@";
9    
10     my $CONVERT = "@CONVERT@";
11     my $IDENTIFY = "@IDENTIFY@";
12 root 1.3 my $OPTIPNG = "@OPTIPNG@";
13 root 1.2 my $RSYNC = "@RSYNC@";
14 root 1.31 my $PNGNQ = "@PNGNQ@";
15 root 1.2
16     use Getopt::Long;
17 root 1.3 use Coro::Event;
18     use AnyEvent;
19     use IO::AIO ();
20 root 1.2 use File::Temp;
21     use Crossfire;
22 root 1.3 use Coro;
23     use Coro::AIO;
24     use POSIX ();
25 root 1.6 use Digest::MD5;
26 root 1.25 use Coro::Storable; $Storable::canonical = 1;
27 root 1.2
28     sub usage {
29     warn <<EOF;
30 root 1.3 Usage: cfutil [-v] [-q] [--force] [--cache]
31 root 1.2 [--install-arch path]
32     [--install-maps maps]
33     [--print-statedir]
34     [--print-confdir]
35     [--print-datadir]
36     [--print-libdir]
37     [--print-bindir]
38     EOF
39     exit 1;
40     }
41    
42     my $VERBOSE = 1;
43 root 1.3 my $CACHE = 0;
44 root 1.2 my $FORCE;
45 root 1.3 my $TMPDIR = "/tmp/cfutil$$~";
46     my $TMPFILE = "aaaa0";
47 root 1.2
48     END { system "rm", "-rf", $TMPDIR }
49    
50 root 1.3 Event->signal (signal => "INT", cb => sub { exit 1 });
51     Event->signal (signal => "TERM", cb => sub { exit 1 });
52    
53 root 1.2 mkdir $TMPDIR, 0700
54     or die "$TMPDIR: $!";
55    
56 root 1.3 sub fork_sub(&) {
57     my ($cb) = @_;
58    
59     if (my $pid = fork) {
60     my $current = $Coro::current;
61     my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
62     Coro::schedule;
63     } else {
64     eval { $cb->() };
65     POSIX::_exit 0 unless $@;
66     warn $@;
67     POSIX::_exit 1;
68     }
69     }
70    
71 root 1.2 sub inst_maps($) {
72     my (undef, $path) = @_;
73    
74 root 1.29 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
75 root 1.2
76     if (!-f "$path/regions") {
77     warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
78     exit 1 unless $FORCE;
79     }
80    
81 root 1.13 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"
82 root 1.16 and die "map installation failed.\n";
83 root 1.13
84     print "maps installed successfully.\n";
85 root 1.2 }
86    
87     {
88 root 1.24 our %ANIMINFO;
89 root 1.6 our %FACEINFO;
90 root 1.30 our @ARC;
91 root 1.27 our %ARC;
92 root 1.12 our $TRS;
93 root 1.3 our $NFILE;
94    
95 root 1.19 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
96    
97 root 1.3 our (@png, @trs, @arc); # files we are interested in
98 root 1.2
99 root 1.19 sub commit_png($$$) {
100     my ($name, $data, $T) = @_;
101 root 1.5
102 root 1.19 $FACEINFO{$name}{"data$T"} = $data;
103 root 1.2 }
104    
105 root 1.3 sub process_png {
106     while (@png) {
107 root 1.19 my ($path, $delete) = @{pop @png};
108 root 1.2
109 root 1.3 my $png;
110     aio_lstat $path;
111 root 1.2 my ($size, $mtime) = (stat _)[7,9];
112    
113 root 1.3 if (0 > aio_load $path, $png) {
114     warn "$path: $!, skipping.\n";
115 root 1.5 next;
116 root 1.3 }
117    
118 root 1.19 my $stem = $path;
119     my $T;
120    
121     if ($stem =~ s/\.32x32\.png~?$//) {
122     $T = 32;
123     } elsif ($stem =~ s/\.64x64\.png~?$//) {
124     $T = 64;
125     } else {
126     warn "$path: weird filename, skipping.\n";
127     next;
128     }
129    
130 root 1.6 # quickly extract width and height of the (necessarily PNG) image
131 root 1.3 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
132 root 1.19 warn "$path: not a recognized png file, skipping.\n";
133 root 1.5 next;
134 root 1.3 }
135 root 1.2
136 root 1.3 my ($w, $h) = unpack "NN", $1;
137 root 1.2
138 root 1.3 if ($w < $T || $h < $T) {
139     warn "$path: too small ($w $h), skipping.\n";
140 root 1.5 next;
141 root 1.3 }
142    
143     if ($w % $T || $h % $T) {
144     warn "$path: weird png size ($w $h), skipping.\n";
145 root 1.5 next;
146 root 1.3 }
147 root 1.2
148 root 1.19 unless ($path =~ /~$/) {
149     # possibly enlarge
150     if (0 > aio_stat "$stem.64x64.png") {
151     my $other = "$stem.64x64.png~";
152    
153     if (0 > aio_lstat $other or (-M _) > (-M $path)) {
154     my $wrap = 0; # for the time being
155     fork_sub {
156     system "convert png:\Q$path\E -depth 8 rgba:-"
157     . "| $exec_prefix/bin/cfhq2xa $w $h $wrap"
158     . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~"
159     and die "convert/hq2xa pipeline error: status $? ($!)";
160 root 1.31 system $OPTIPNG, "-i0", "-q", "$other~";
161 root 1.19 die "$other~ has zero size, aborting." unless -s "$other~";
162     rename "$other~", $other;
163     };
164     }
165    
166     push @png, [$other, !$CACHE];
167     }
168    
169     # possibly scale down
170     if (0 > aio_stat "$stem.32x32.png") {
171     my $other = "$stem.32x32.png~";
172    
173     if (0 > aio_lstat $other or (-M _) > (-M $path)) {
174     fork_sub {
175     system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
176 root 1.31 system $OPTIPNG, "-i0", "-q", "$other~";
177    
178     # reduce smoothfaces >10000 bytes
179     if ($stem =~ /_S\./ && (-s "$other~") > 10000) {
180     my $ncolor = 256;
181     while () {
182     system "<\Q$other~\E $PNGNQ -n$ncolor >\Q$other~~\E";
183     system $OPTIPNG, "-i0", "-q", "$other~~";
184     last if 10000 > -s "$other~~";
185     $ncolor = int $ncolor * 0.9;
186     $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
187     }
188    
189     printf "reduced %s from %d to %d bytes using %d colours.\n",
190     $other, -s "$other~", -s "$other~~", $ncolor
191     if $VERBOSE > 0;
192     rename "$other~~", "$other~";
193     }
194    
195 root 1.19 die "$other~ has zero size, aborting." unless -s "$other~";
196     rename "$other~", $other;
197     };
198     }
199    
200 root 1.21 #warn "scaled down $path to $other\n";#d#
201 root 1.19 push @png, [$other, !$CACHE];
202     }
203     }
204    
205     (my $face = $stem) =~ s/^.*\///;
206    
207 root 1.23 # split all bigfaces, but avoid smoothfaces (*_S)
208 root 1.3 if (($w > $T || $h > $T) && $face !~ /_S\./) {
209     # split
210     my @tile;
211     for my $x (0 .. (int $w / $T) - 1) {
212     for my $y (0 .. (int $h / $T) - 1) {
213     my $file = "$path+$x+$y~";
214     aio_lstat $file;
215     push @tile, [$x, $y, $file, (stat _)[9]];
216     }
217 root 1.2 }
218    
219 root 1.3 my $mtime = (lstat $path)[9];
220     my @todo = grep { $_->[3] <= $mtime } @tile;
221     if (@todo) {
222     fork_sub {
223     open my $convert, "|-", $CONVERT,
224     "png:-",
225     (map {
226     (
227     "(",
228     "+clone",
229     -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
230 root 1.7 "+repage",
231 root 1.19 -quality => "00",
232     -write => "png32:$_->[2]~",
233 root 1.3 "+delete",
234     ")",
235     )
236     } @todo),
237     "null:";
238    
239     binmode $convert;
240     print $convert $png;
241     close $convert;
242    
243     # pass 2, optimise, and rename
244     for (@todo) {
245     system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
246 root 1.19 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
247 root 1.3 rename "$_->[2]~", $_->[2];
248     }
249     };
250 root 1.2 }
251    
252 root 1.3 for (@tile) {
253     my ($x, $y, $file) = @$_;
254     my $tile;
255    
256     if (0 > aio_load $file, $tile) {
257     die "$path: unable to read tile +$x+$y, aborting.\n";
258     }
259     IO::AIO::aio_unlink $file unless $CACHE;
260 root 1.19 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
261 root 1.2 }
262 root 1.3 } else {
263     # use as-is (either small, use smooth)
264 root 1.19 commit_png $face, $png, $T;
265 root 1.3 }
266 root 1.19
267     aio_unlink $path if $delete;
268 root 1.3 }
269 root 1.2 }
270    
271 root 1.3 sub process_arc {
272     while (@arc) {
273     my ($dir, $file) = @{pop @arc};
274 root 1.2
275 root 1.3 my $arc;
276     aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
277 root 1.4
278 root 1.2 my $arc = read_arch "$dir/$file";
279 root 1.4 for my $o (values %$arc) {
280 root 1.30 push @ARC, $o;
281     for (my $m = $o; $m; $m = $m->{more}) {
282     $ARC{$m->{_name}} = $m;
283     }
284 root 1.6
285 root 1.10 $o->{editor_folder} = $dir;
286    
287 root 1.6 my $visibility = delete $o->{visibility};
288     my $magicmap = delete $o->{magicmap};
289    
290     # find upper left corner :/
291     # omg, this is sooo broken
292 root 1.4 my ($dx, $dy);
293     for (my $o = $o; $o; $o = $o->{more}) {
294     $dx = $o->{x} if $o->{x} < $dx;
295     $dy = $o->{y} if $o->{y} < $dy;
296     }
297 root 1.6
298 root 1.4 for (my $o = $o; $o; $o = $o->{more}) {
299     my $x = $o->{x} - $dx;
300     my $y = $o->{y} - $dy;
301 root 1.6
302     my $ext = $x|$y ? "+$x+$y" : "";
303    
304 root 1.26 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
305 root 1.6
306     my $visibility = delete $o->{visibility} if exists $o->{visibility};
307     my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
308    
309     my $anim = delete $o->{anim};
310    
311     if ($anim) {
312 root 1.24 # possibly add $ext to the animation name to avoid
313     # the need to specify archnames for all parts
314     # of a multipart archetype.
315 root 1.8 $o->{animation} = "$o->{_name}";
316 root 1.24 my $facings = 1;
317     my @frames;
318 root 1.6
319     for (@$anim) {
320 root 1.24 if (/^facings\s+(\d+)/) {
321     $facings = $1*1;
322     } elsif (/^blank.x11$|^empty.x11$/) {
323     push @frames, $_;
324     } else {
325     push @frames, "$_$ext";
326     }
327 root 1.6 }
328    
329 root 1.24 $ANIMINFO{$o->{animation}} = {
330     facings => $facings,
331     frames => \@frames,
332     };
333 root 1.6 }
334    
335     for my $face ($o->{face} || (), @{$anim || []}) {
336 root 1.9 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
337 root 1.6
338     my $info = $FACEINFO{$face} ||= {};
339    
340     $info->{visibility} = $visibility if defined $visibility;
341     $info->{magicmap} = $magicmap if defined $magicmap;
342 root 1.4 }
343 root 1.12
344     if (my $smooth = delete $o->{smoothface}) {
345 root 1.23 my %kv =split /\s+/, $smooth;
346     my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
347     while (my ($face, $smooth) = each %kv) {
348     $FACEINFO{$face}{smooth} = $smooth;
349     $FACEINFO{$face}{smoothlevel} = $level;
350 root 1.14 }
351 root 1.12 }
352 root 1.4 }
353     }
354 root 1.3 }
355 root 1.2 }
356    
357 root 1.3 sub process_trs {
358     while (@trs) {
359     my ($dir, $file) = @{pop @trs};
360 root 1.12 my $path = "$dir/$file";
361    
362     my $trs;
363     if (0 > aio_load $path, $trs) {
364     warn "$path: $!, skipping.\n";
365     next;
366     }
367    
368     $TRS .= $trs;
369 root 1.3 }
370 root 1.2 }
371    
372 root 1.3 sub find_files;
373     sub find_files {
374 root 1.2 my ($path) = @_;
375    
376 root 1.3 IO::AIO::aioreq_pri 4;
377     IO::AIO::aio_scandir $path, 4, sub {
378 root 1.2 my ($dirs, $nondirs) = @_;
379    
380 root 1.3 find_files "$path/$_"
381 root 1.2 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
382    
383     for my $file (@$nondirs) {
384     if ($file =~ /\.png$/) {
385 root 1.19 push @png, ["$path/$file", 0];
386 root 1.2 } elsif ($file =~ /\.trs$/) {
387 root 1.3 push @trs, [$path, $file];
388 root 1.2 } elsif ($file =~ /\.arc$/) {
389 root 1.3 push @arc, [$path, $file];
390 root 1.2 } else {
391     warn "ignoring $path/$file\n" if $VERBOSE >= 2;
392     }
393     }
394     };
395     }
396    
397     sub inst_arch($) {
398     my (undef, $path) = @_;
399    
400 root 1.29 print "\n",
401     "Installing '$path' to '$DATADIR'\n",
402 root 1.28 "\n",
403 root 1.27 "This can take a long time if you run this\n",
404     "for the first time or do not use --cache.\n",
405     "\n",
406     "Unless you run verbosely, all following warning\n",
407     "or error messages indicate serious problems.\n",
408     "\n";
409 root 1.2
410     if (!-d "$path/treasures") {
411     warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
412     exit 1 unless $FORCE;
413     }
414    
415 root 1.3 find_files $path;
416 root 1.2 IO::AIO::flush;
417    
418 root 1.3 $_->join for (
419 root 1.19 # four png crunchers work fine for my 2x smp machine
420     (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
421 root 1.3 (async \&process_trs), (async \&process_trs),
422     (async \&process_arc), (async \&process_arc),
423     );
424    
425 root 1.5 {
426 root 1.27 # remove path prefix from editor_folder
427     substr $_->{editor_folder}, 0, 1 + length $path, ""
428     for values %ARC;
429    
430     # resolve inherit
431     while () {
432     my $progress;
433     my $loop;
434    
435     for my $o (values %ARC) {
436     if (my $other = $o->{inherit}) {
437     if (my $s = $ARC{$other}) {
438     if ($s->{inherit}) {
439     $loop = $s;
440     } else {
441     delete $o->{inherit};
442 root 1.30 my %s = %$s;
443     delete @s{qw(_name more name name_pl)};
444     %$o = ( %s, %$o );
445 root 1.27 ++$progress;
446     }
447     } else {
448     warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
449     delete $ARC{$o->{_name}};
450     }
451     }
452     }
453    
454     unless ($progress) {
455     die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
456     if $loop;
457    
458     last;
459     }
460     }
461    
462 root 1.5 open my $fh, ">:utf8", "$DATADIR/archetypes~"
463     or die "$DATADIR/archetypes~: $!";
464 root 1.30 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
465 root 1.5 }
466    
467 root 1.6 {
468 root 1.12 open my $fh, ">:utf8", "$DATADIR/treasures~"
469     or die "$DATADIR/treasures~: $!";
470     print $fh $TRS;
471     }
472    
473     {
474 root 1.6 while (my ($k, $v) = each %FACEINFO) {
475 root 1.12 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
476 root 1.19 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
477 root 1.5
478 root 1.20 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
479     #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
480    
481 root 1.6 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
482 root 1.19 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
483 root 1.6 }
484    
485 root 1.19 open my $fh, ">:perlio", "$DATADIR/facedata~"
486     or die "$DATADIR/facedata~: $!";
487 root 1.6
488 root 1.25 print $fh freeze {
489 root 1.24 version => 2,
490     faceinfo => \%FACEINFO,
491     animinfo => \%ANIMINFO,
492     };
493 root 1.5 }
494    
495 root 1.24 for (qw(archetypes facedata treasures)) {
496 root 1.6 chmod 0644, "$DATADIR/$_~";
497 root 1.13 rename "$DATADIR/$_~", "$DATADIR/$_"
498     or die "$DATADIR/$_: $!";
499 root 1.6 }
500 root 1.13
501     print "archetype data installed successfully.\n";
502 root 1.2 }
503     }
504    
505     Getopt::Long::Configure ("bundling", "no_ignore_case");
506     GetOptions (
507     "verbose|v:+" => \$VERBOSE,
508 root 1.3 "cache" => \$CACHE,
509 root 1.2 "quiet|q" => sub { $VERBOSE = 0 },
510     "force" => sub { $FORCE = 1 },
511     "install-arch=s" => \&inst_arch,
512     "install-maps=s" => \&inst_maps,
513     "print-statedir" => sub { print "@pkgstatedir@\n" },
514     "print-datadir" => sub { print "$DATADIR\n" },
515     "print-confdir" => sub { print "@pkgconfdir@\n" },
516     "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
517     "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
518     ) or usage;
519