ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.23
Committed: Tue Apr 10 09:35:24 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.22: +6 -5 lines
Log Message:
- implement smoothing in the fxix (or better: fxixsx) protocol.
- this *enforces* that smoothing is *per face* and not *per archetype*.
- no extra bandwidth is needed for map information (unlike in the
  crossfire 1.x protocol, where smoothlevels are per-mapspace
  and take up a lot of bandwidth).
- no extra storage is required on the client-side (although smoothing
  for cached map data is then only available for tiles that have been seen).
- temporarily supply empty_S.x11 smoothfaces again, as this hack is required
  because there is no way to specify a smoothlevel but no smoothface
  otherwise. this should be optimised away at a future point, but this
  will break gcfclient (but disabling smoothing for that might be a good
  compromise).

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