ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.30
Committed: Sat Apr 21 11:55:36 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.29: +9 -3 lines
Log Message:
some hacks/fixes to make multipart treasures work on worldmaps only

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