ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.36
Committed: Sun Jun 10 02:51:46 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.35: +1 -1 lines
Log Message:
use configure to check for tcp_info

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