ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.40
Committed: Mon Jul 16 19:43:33 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.39: +1 -1 lines
Log Message:
make installed maps world-readable

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