ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.106
Committed: Wed Oct 20 06:21:48 2010 UTC (13 years, 7 months ago) by root
Branch: MAIN
Changes since 1.105: +15 -96 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!@PERL@
2    
3 root 1.77 #
4     # This file is part of Deliantra, the Roguelike Realtime MMORPG.
5     #
6     # Copyright (©) 2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
7     #
8     # Deliantra is free software: you can redistribute it and/or modify it under
9     # the terms of the Affero GNU General Public License as published by the
10     # Free Software Foundation, either version 3 of the License, or (at your
11     # option) any later version.
12     #
13     # This program is distributed in the hope that it will be useful,
14     # but WITHOUT ANY WARRANTY; without even the implied warranty of
15     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     # GNU General Public License for more details.
17     #
18     # You should have received a copy of the Affero GNU General Public License
19     # and the GNU General Public License along with this program. If not, see
20     # <http://www.gnu.org/licenses/>.
21     #
22     # The authors can be reached via e-mail to <support@deliantra.net>
23     #
24    
25 root 1.84 use common::sense;
26 root 1.2
27     my $prefix = "@prefix@";
28     my $exec_prefix = "@exec_prefix@";
29     my $datarootdir = "@datarootdir@";
30     my $DATADIR = "@datadir@/@PACKAGE@";
31    
32     my $CONVERT = "@CONVERT@";
33 root 1.36 #my $IDENTIFY = "@IDENTIFY@";
34 root 1.3 my $OPTIPNG = "@OPTIPNG@";
35 root 1.2 my $RSYNC = "@RSYNC@";
36 root 1.31 my $PNGNQ = "@PNGNQ@";
37 root 1.2
38     use Getopt::Long;
39 root 1.73 use File::Temp;
40     use POSIX ();
41     use Carp;
42    
43 root 1.69 use Coro::EV;
44 root 1.3 use AnyEvent;
45 root 1.72 use YAML::XS ();
46 root 1.63 use JSON::XS ();
47 root 1.3 use IO::AIO ();
48 root 1.79 use Digest::MD5 ();
49 root 1.73
50 root 1.70 use Coro 5.12;
51 root 1.3 use Coro::AIO;
52 root 1.51 use Coro::Util;
53 root 1.53 use Coro::Channel;
54 root 1.25 use Coro::Storable; $Storable::canonical = 1;
55 root 1.2
56 root 1.73 use Deliantra;
57    
58 root 1.42 $SIG{QUIT} = sub { Carp::cluck "QUIT" };
59    
60 root 1.2 sub usage {
61     warn <<EOF;
62 root 1.3 Usage: cfutil [-v] [-q] [--force] [--cache]
63 root 1.2 [--install-arch path]
64     [--install-maps maps]
65     [--print-statedir]
66     [--print-confdir]
67     [--print-datadir]
68     [--print-libdir]
69     [--print-bindir]
70     EOF
71     exit 1;
72     }
73    
74     my $VERBOSE = 1;
75 root 1.3 my $CACHE = 0;
76 root 1.2 my $FORCE;
77 root 1.3 my $TMPDIR = "/tmp/cfutil$$~";
78     my $TMPFILE = "aaaa0";
79 root 1.83 my @COMMIT;
80 root 1.2
81 root 1.35 our %COLOR = (
82 root 1.103 # original cf colours
83 root 1.35 black => 0,
84     white => 1,
85 root 1.103 navy => 2, # "dark blue"
86 root 1.35 red => 3,
87     orange => 4,
88 root 1.103 blue => 5, # "light blue"
89 root 1.35 darkorange => 6,
90     green => 7,
91     lightgreen => 8,
92     grey => 9,
93 root 1.103 brown => 10, # "yellow"
94     gold => 11, # "light yellow"
95     tan => 12, # yellowish gray
96    
97     # new for deliantra
98     none => 13,
99    
100     # compatibility to existing archetypes
101     lightblue => 5,
102     gray => 9,
103     yellow => 11,
104     khaki => 12,
105 root 1.35 );
106    
107 root 1.2 END { system "rm", "-rf", $TMPDIR }
108    
109 root 1.69 my $s_INT = EV::signal INT => sub { exit 1 };
110     my $s_TERM = EV::signal TERM => sub { exit 1 };
111 root 1.3
112 root 1.79 our %hash;
113    
114     # here we could try to avoid collisions and reduce chksum size further
115     sub make_hash($\$\$;$) {
116     my ($id, $dataref, $hashref, $clen) = @_;
117    
118     my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4;
119    
120     if (exists $hash{$hash}) {
121 root 1.92 # hash collision, but some files are simply identical
122 root 1.79 if (${$hash{$hash}[1]} ne $$dataref) {
123     warn "hash collision $hash{$hash}[0] vs. $id\n";
124     exit 1;
125     } else {
126     print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3;
127     }
128     }
129     $hash{$hash} = [$id, $dataref, $hashref];
130    
131     $$hashref = $hash;
132     }
133    
134 root 1.2 mkdir $TMPDIR, 0700
135     or die "$TMPDIR: $!";
136    
137 root 1.3 sub fork_sub(&) {
138     my ($cb) = @_;
139    
140     if (my $pid = fork) {
141     my $current = $Coro::current;
142     my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
143     Coro::schedule;
144     } else {
145     eval { $cb->() };
146     POSIX::_exit 0 unless $@;
147     warn $@;
148     POSIX::_exit 1;
149     }
150     }
151    
152 root 1.2 sub inst_maps($) {
153     my (undef, $path) = @_;
154    
155 root 1.29 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
156 root 1.2
157     if (!-f "$path/regions") {
158     warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
159     exit 1 unless $FORCE;
160     }
161    
162 root 1.62 system $RSYNC, "-av", "--chmod=u=rwX,go=rX",
163     "$path/.", "$DATADIR/maps/.",
164     "--exclude", "CVS", "--exclude", "/world-precomposed",
165     "--delete", "--delete-excluded"
166 root 1.16 and die "map installation failed.\n";
167 root 1.13
168     print "maps installed successfully.\n";
169 root 1.2 }
170    
171 root 1.103 {
172 root 1.24 our %ANIMINFO;
173 root 1.6 our %FACEINFO;
174 root 1.37 our %RESOURCE;
175 root 1.30 our @ARC;
176 root 1.27 our %ARC;
177 root 1.12 our $TRS;
178 root 1.3 our $NFILE;
179 root 1.37 our $PATH;
180 root 1.3
181 root 1.19 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
182    
183 root 1.53 our $c_arc = new Coro::Channel;
184 root 1.103 our $c_any = new Coro::Channel;
185 root 1.53
186     our @c_png;
187 root 1.2
188 root 1.86 sub commit_png($$$$) {
189     my ($stem, $name, $data, $T) = @_;
190 root 1.5
191 root 1.86 $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH;
192 root 1.19 $FACEINFO{$name}{"data$T"} = $data;
193 root 1.2 }
194    
195 root 1.3 sub process_png {
196 root 1.53 while (@c_png) {
197     my ($path, $delete) = @{pop @c_png};
198 root 1.2
199 root 1.3 my $png;
200     aio_lstat $path;
201 root 1.2 my ($size, $mtime) = (stat _)[7,9];
202    
203 root 1.3 if (0 > aio_load $path, $png) {
204     warn "$path: $!, skipping.\n";
205 root 1.5 next;
206 root 1.3 }
207    
208 root 1.19 my $stem = $path;
209     my $T;
210    
211     if ($stem =~ s/\.32x32\.png~?$//) {
212     $T = 32;
213     } elsif ($stem =~ s/\.64x64\.png~?$//) {
214     $T = 64;
215     } else {
216     warn "$path: weird filename, skipping.\n";
217     next;
218     }
219    
220 root 1.6 # quickly extract width and height of the (necessarily PNG) image
221 root 1.3 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
222 root 1.19 warn "$path: not a recognized png file, skipping.\n";
223 root 1.5 next;
224 root 1.3 }
225 root 1.2
226 root 1.3 my ($w, $h) = unpack "NN", $1;
227 root 1.2
228 root 1.3 if ($w < $T || $h < $T) {
229     warn "$path: too small ($w $h), skipping.\n";
230 root 1.5 next;
231 root 1.3 }
232    
233     if ($w % $T || $h % $T) {
234     warn "$path: weird png size ($w $h), skipping.\n";
235 root 1.5 next;
236 root 1.3 }
237 root 1.2
238 root 1.54 (my $base = $stem) =~ s/^.*\///;
239    
240     my $fi = $FACEINFO{$base};
241     unless ($fi) {
242 root 1.59 #warn "$path: <$base> not referenced by any archetype, skipping.\n";
243     #next;
244 root 1.54 }
245    
246 root 1.59 my $arc = $fi->{arc} || { };
247 root 1.54
248 root 1.19 unless ($path =~ /~$/) {
249     # possibly enlarge
250     if (0 > aio_stat "$stem.64x64.png") {
251     my $other = "$stem.64x64.png~";
252    
253     if (0 > aio_lstat $other or (-M _) > (-M $path)) {
254     fork_sub {
255 root 1.52 my $CROP;
256     my $SRC = "png:\Q$path\E";
257    
258 root 1.54 my $is_floor = $arc->{is_floor};
259     my $is_wall = 0;
260    
261     my ($wall_pfx, $wall_dir, $wall_sfx);
262    
263     if (
264     !$is_floor
265     && !$arc->{alive}
266 root 1.55 && $arc->{move_block} eq "all"
267 root 1.54 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/
268     ) {
269     ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3);
270    
271     unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) {
272     $is_wall = 1;
273     }
274     }
275    
276     if ($is_wall || $is_floor) {
277     # add a 4px border and add other images around it
278     $CROP = "-shave 8x8 +repage";
279    
280     $w += 8;
281     $h += 8;
282    
283     $SRC = "-size ${w}x${h} xc:transparent";
284     $SRC .= " png:\Q$path\E -geometry +4+4 -composite";
285    
286     # 8 surrounding images
287     for (
288     # x y b r0 r1
289     [-1, -1, 0, 6],
290     [ 0, -1, 1, 10, 14],
291     [+1, -1, 0, 12],
292    
293     [-1, 0, 8, 5, 7],
294     #
295     [+1, 0, 2, 5, 13],
296    
297     [-1, +1, 0, 3],
298     [ 0, +1, 4, 10, 11],
299     [+1, +1, 0, 9],
300     ) {
301     my ($x, $y, $d, $r0, $r1) = @$_;
302    
303     my $tile = $is_floor ? $path
304     : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx
305     : die;
306    
307     $SRC .= sprintf " png:%s -geometry %+d%+d -composite",
308     "\Q$tile",
309     $x * ($w - 8) + 4,
310     $y * ($h - 8) + 4;
311 root 1.52 }
312     }
313    
314     system "convert -depth 8 $SRC rgba:-"
315 root 1.51 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
316 root 1.52 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~"
317     and die "convert/cfhq2xa pipeline error: status $? ($!)";
318 root 1.31 system $OPTIPNG, "-i0", "-q", "$other~";
319 root 1.19 die "$other~ has zero size, aborting." unless -s "$other~";
320     rename "$other~", $other;
321     };
322     }
323    
324 root 1.53 push @c_png, [$other, !$CACHE];
325 root 1.19 }
326    
327     # possibly scale down
328     if (0 > aio_stat "$stem.32x32.png") {
329     my $other = "$stem.32x32.png~";
330    
331     if (0 > aio_lstat $other or (-M _) > (-M $path)) {
332     fork_sub {
333     system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
334 root 1.31 system $OPTIPNG, "-i0", "-q", "$other~";
335    
336     # reduce smoothfaces >10000 bytes
337 root 1.71 # obsolete, no longer required
338     if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
339 root 1.31 my $ncolor = 256;
340     while () {
341 root 1.33 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
342 root 1.31 system $OPTIPNG, "-i0", "-q", "$other~~";
343     last if 10000 > -s "$other~~";
344     $ncolor = int $ncolor * 0.9;
345     $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
346     }
347    
348     printf "reduced %s from %d to %d bytes using %d colours.\n",
349     $other, -s "$other~", -s "$other~~", $ncolor
350 root 1.32 if $VERBOSE >= 2;
351 root 1.31 rename "$other~~", "$other~";
352     }
353    
354 root 1.19 die "$other~ has zero size, aborting." unless -s "$other~";
355     rename "$other~", $other;
356     };
357     }
358    
359 root 1.21 #warn "scaled down $path to $other\n";#d#
360 root 1.53 push @c_png, [$other, !$CACHE];
361 root 1.19 }
362     }
363    
364     (my $face = $stem) =~ s/^.*\///;
365    
366 root 1.23 # split all bigfaces, but avoid smoothfaces (*_S)
367 root 1.3 if (($w > $T || $h > $T) && $face !~ /_S\./) {
368     # split
369     my @tile;
370     for my $x (0 .. (int $w / $T) - 1) {
371     for my $y (0 .. (int $h / $T) - 1) {
372     my $file = "$path+$x+$y~";
373     aio_lstat $file;
374     push @tile, [$x, $y, $file, (stat _)[9]];
375     }
376 root 1.2 }
377    
378 root 1.3 my $mtime = (lstat $path)[9];
379     my @todo = grep { $_->[3] <= $mtime } @tile;
380     if (@todo) {
381     fork_sub {
382     open my $convert, "|-", $CONVERT,
383     "png:-",
384     (map {
385     (
386     "(",
387     "+clone",
388     -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
389 root 1.7 "+repage",
390 root 1.19 -quality => "00",
391     -write => "png32:$_->[2]~",
392 root 1.3 "+delete",
393     ")",
394     )
395     } @todo),
396     "null:";
397    
398     binmode $convert;
399     print $convert $png;
400     close $convert;
401    
402     # pass 2, optimise, and rename
403     for (@todo) {
404     system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
405 root 1.19 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
406 root 1.3 rename "$_->[2]~", $_->[2];
407     }
408     };
409 root 1.2 }
410    
411 root 1.3 for (@tile) {
412     my ($x, $y, $file) = @$_;
413     my $tile;
414    
415     if (0 > aio_load $file, $tile) {
416     die "$path: unable to read tile +$x+$y, aborting.\n";
417     }
418     IO::AIO::aio_unlink $file unless $CACHE;
419 root 1.86 commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T;
420 root 1.2 }
421 root 1.3 } else {
422     # use as-is (either small, use smooth)
423 root 1.86 commit_png $stem, $face, $png, $T;
424 root 1.3 }
425 root 1.19
426     aio_unlink $path if $delete;
427 root 1.3 }
428 root 1.2 }
429    
430 root 1.103 sub process_faceinfo {
431     my ($dir, $file) = @_;
432     my $path = "$dir/$file";
433    
434     my $data;
435     if (0 > aio_load $path, $data) {
436     warn "$path: $!, skipping.\n";
437     return;
438     }
439    
440     for (split /\n/, $data) {
441 root 1.106 chomp;
442 root 1.103 my ($face, $visibility, $fg, $bg, $glyph) = split /\s+/;
443 root 1.106 # bg not used except for text clients
444 root 1.103
445 root 1.106 utf8::decode $glyph;
446 root 1.104 $glyph =~ s/^\?(?=.)//; # remove "autoglyph" flag
447 root 1.103
448 root 1.105 $fg = "white" if $fg eq "none"; # lots of faces have no fg colour yet
449    
450 root 1.103 (my $fgi = $COLOR{$fg})
451     // warn "WARNING: $path: $face specifies unknown foreground colour '$fg'.\n";
452     (my $bgi = $COLOR{$bg})
453     // warn "WARNING: $path: $face specifies unknown background colour '$bg'.\n";
454    
455     my $fi = $FACEINFO{$face} ||= { };
456     $fi->{visibility} = $visibility * 1;
457     $fi->{magicmap} = $fgi; # foreground colour becomes magicmap
458 root 1.106
459     $glyph .= " " if 2 > length $glyph; # TODO kanji
460    
461     $fi->{glyph} = "";
462     for (split //, $glyph, -1) {
463     utf8::encode $_;
464     $fi->{glyph} .= (chr $fgi) . (chr $bgi) . $_;
465     }
466 root 1.103 }
467     }
468    
469 root 1.3 sub process_arc {
470 root 1.103 while (my ($dir, $file) = @{ $c_arc->get }) {
471 root 1.3 my $arc;
472     aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
473 root 1.4
474 root 1.2 my $arc = read_arch "$dir/$file";
475 root 1.4 for my $o (values %$arc) {
476 root 1.30 push @ARC, $o;
477     for (my $m = $o; $m; $m = $m->{more}) {
478     $ARC{$m->{_name}} = $m;
479     }
480 root 1.6
481 root 1.76 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
482 root 1.10
483 root 1.6 # find upper left corner :/
484     # omg, this is sooo broken
485 root 1.4 my ($dx, $dy);
486     for (my $o = $o; $o; $o = $o->{more}) {
487     $dx = $o->{x} if $o->{x} < $dx;
488     $dy = $o->{y} if $o->{y} < $dy;
489     }
490 root 1.6
491 root 1.4 for (my $o = $o; $o; $o = $o->{more}) {
492     my $x = $o->{x} - $dx;
493     my $y = $o->{y} - $dy;
494 root 1.6
495     my $ext = $x|$y ? "+$x+$y" : "";
496    
497 root 1.26 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
498 root 1.6
499     my $anim = delete $o->{anim};
500    
501     if ($anim) {
502 root 1.24 # possibly add $ext to the animation name to avoid
503     # the need to specify archnames for all parts
504     # of a multipart archetype.
505 root 1.8 $o->{animation} = "$o->{_name}";
506 root 1.24 my $facings = 1;
507     my @frames;
508 root 1.6
509     for (@$anim) {
510 root 1.24 if (/^facings\s+(\d+)/) {
511     $facings = $1*1;
512     } elsif (/^blank.x11$|^empty.x11$/) {
513     push @frames, $_;
514     } else {
515     push @frames, "$_$ext";
516     }
517 root 1.6 }
518    
519 root 1.24 $ANIMINFO{$o->{animation}} = {
520     facings => $facings,
521     frames => \@frames,
522     };
523 root 1.6 }
524    
525 root 1.58 for ($o->{face} || (), @{$anim || []}) {
526     next if /^facings\s/;
527    
528     my $face = $_;
529     $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates
530 root 1.6
531 root 1.56 my $info = $FACEINFO{$face} ||= { };
532     $info->{arc} = $o;
533    
534     next if $face =~ /^blank.x11$|^empty.x11$/;
535 root 1.4 }
536 root 1.12
537     if (my $smooth = delete $o->{smoothface}) {
538 root 1.54 my %kv = split /\s+/, $smooth;
539 root 1.23 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
540     while (my ($face, $smooth) = each %kv) {
541 root 1.54 $FACEINFO{$smooth}{arc} = $o;
542    
543 root 1.23 $FACEINFO{$face}{smooth} = $smooth;
544     $FACEINFO{$face}{smoothlevel} = $level;
545 root 1.14 }
546 root 1.12 }
547 root 1.4 }
548     }
549 root 1.3 }
550 root 1.2 }
551    
552 root 1.3 sub process_trs {
553 root 1.103 my ($dir, $file) = @_;
554     my $path = "$dir/$file";
555 root 1.12
556 root 1.103 my $trs;
557     if (0 > aio_load $path, $trs) {
558     warn "$path: $!, skipping.\n";
559     return;
560     }
561 root 1.12
562 root 1.103 $TRS .= $trs;
563 root 1.2 }
564    
565 root 1.45 my %FILECACHE;
566    
567     sub load_cached($;$) {
568     unless (exists $FILECACHE{$_[0]}) {
569     my $data;
570     if (0 < aio_load $_[0], $data) {
571 root 1.49 if ($_[1]) {
572     $data = eval { $_[1]->($data) };
573     warn "$_[0]: $@" if $@;
574     }
575 root 1.45 }
576    
577     $FILECACHE{$_[0]} = $data;
578     }
579    
580     $FILECACHE{$_[0]}
581     }
582    
583 root 1.37 sub process_res {
584 root 1.103 my ($dir, $file, $type) = @_;
585 root 1.37
586 root 1.103 my $data;
587     aio_load "$dir/$file", $data;
588 root 1.37
589 root 1.103 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
590 root 1.38
591 root 1.103 utf8::decode $dir;
592     utf8::decode $file;
593 root 1.61
594 root 1.103 # a meta file for resources is now mandatory
595     unless (exists $meta->{$file}) {
596     warn "skipping $dir/$file\n" if $VERBOSE >= 3;
597     return;
598     }
599 root 1.49
600 root 1.103 $meta = {
601     %{ $meta->{"" } || {} },
602     %{ $meta->{$file} || {} },
603     };
604 root 1.48
605 root 1.103 if ($meta->{license} =~ s/^#//) {
606     $meta->{license} = ({
607     "pd" => "Public Domain",
608     "gpl" => "GNU General Public License, version 3.0 or any later",
609     "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
610     "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/",
611     "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
612     "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
613     })->{$meta->{license}}
614     || warn "$dir/$file: license tag '$meta->{license}' not found.";
615     }
616    
617     if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
618     ($meta->{author} = $1) =~ s/_/ /g;
619     }
620    
621     $file =~ s/\.res$//;
622     $file =~ s/\.(ogg|wav|jpg|png)$//;
623    
624     substr $dir, 0, 1 + length $PATH, "";
625    
626     if (my $filter = $meta->{cfutil_filter}) {
627     if ($filter eq "yaml2json") {
628     $data = JSON::XS::encode_json YAML::XS::Load $data;
629     } elsif ($filter eq "json2json") {
630     $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
631     } elsif ($filter eq "perl2json") {
632     $data = eval $data; die if $@;
633     $data = JSON::XS::encode_json $data;
634     } else {
635     warn "$dir/$file: unknown filter $filter, skipping\n";
636 root 1.63 }
637 root 1.103 }
638    
639     $RESOURCE{"$dir/$file"} = {
640     type => (exists $meta->{type} ? delete $meta->{type} : $type),
641     data => $data,
642     %$meta ? (meta => $meta) : (),
643     };
644     }
645 root 1.63
646 root 1.103 sub process_any {
647     while (my ($func, @args) = @{ $c_any->get }) {
648     $func->(@args);
649 root 1.37 }
650     }
651    
652 root 1.3 sub find_files;
653     sub find_files {
654 root 1.2 my ($path) = @_;
655    
656 root 1.103 my $grp = IO::AIO::aio_group;
657    
658     my $scandir; $scandir = sub {
659     my ($path) = @_;
660    
661     IO::AIO::aioreq_pri 4;
662     add $grp IO::AIO::aio_scandir $path, 4, sub {
663     my ($dirs, $nondirs) = @_;
664    
665     $scandir->("$path/$_")
666     for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
667    
668     my $dir = $path;
669     substr $dir, 0, 1 + length $PATH, "";
670    
671     for my $file (@$nondirs) {
672     if ($dir =~ /^music(?:\/|$)/) {
673     $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
674     if $file =~ /\.(ogg)$/;
675    
676     } elsif ($dir =~ /^sound(?:\/|$)/) {
677     $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
678     if $file =~ /\.(wav|ogg)$/;
679    
680     } elsif ($dir =~ /^res(?:\/|$)/) {
681     if ($file =~ /\.(jpg|png)$/) {
682     $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
683     } elsif ($file =~ /\.(res)$/) {
684     $c_any->put ([\&process_res, $path, $file, 6]) # FT_RSRC
685     } else {
686     $c_any->put ([\&process_res, $path, $file, undef]);
687     }
688    
689     } elsif ($file =~ /\.png$/) {
690     push @c_png, ["$path/$file", 0];
691 root 1.44
692 root 1.103 } elsif ($file =~ /\.faceinfo$/) {
693     $c_any->put ([\&process_faceinfo, $path, $file]);
694 root 1.44
695 root 1.103 } elsif ($file =~ /\.trs$/) {
696     $c_any->put ([\&process_trs, $path, $file]);
697 root 1.44
698 root 1.103 } elsif ($file =~ /\.arc$/) {
699     $c_arc->put ([$path, $file]);
700 root 1.44
701 root 1.103 } else {
702     warn "ignoring $path/$file\n" if $VERBOSE >= 3;
703     }
704 root 1.2 }
705 root 1.103 };
706 root 1.2 };
707 root 1.103
708     $scandir->($path);
709     aio_wait $grp;
710 root 1.2 }
711    
712 root 1.73 sub generate_plurals {
713     # use Lingua::EN::Inflect ();
714     # Lingua::EN::Inflect::classical;
715 root 1.74 # Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy
716 root 1.73 # Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack
717     #
718     # for my $a (@ARC) {
719     # my $name = $a->{name} || $a->{_name};
720     #
721     # next unless $a->{name_pl};
722 root 1.74 # next if $a->{invisible};
723     # next if $a->{is_floor};
724     # next if $a->{no_pick};
725 root 1.73 #
726     # my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name;
727     # my $pl = $test =~ /^(?:eq|p:.)$/
728     # ? $name
729     # : Lingua::EN::Inflect::PL $name;
730     #
731     # if ($pl ne $a->{name_pl}) {
732     # warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n";
733     # }
734     # }
735     }
736    
737 root 1.2 sub inst_arch($) {
738     my (undef, $path) = @_;
739    
740 root 1.37 $PATH = $path;
741    
742 root 1.29 print "\n",
743     "Installing '$path' to '$DATADIR'\n",
744 root 1.28 "\n",
745 root 1.27 "This can take a long time if you run this\n",
746     "for the first time or do not use --cache.\n",
747     "\n",
748     "Unless you run verbosely, all following warning\n",
749     "or error messages indicate serious problems.\n",
750     "\n";
751 root 1.2
752     if (!-d "$path/treasures") {
753     warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
754     exit 1 unless $FORCE;
755     }
756    
757 root 1.103 print "start file scan, arc, any processing...\n" if $VERBOSE;
758 root 1.53
759     my @a_arc = map +(async \&process_arc), 1..2;
760 root 1.103 my @a_any = map +(async \&process_any), 1..4;
761 root 1.53
762 root 1.103 find_files $path;
763 root 1.2
764 root 1.70 $c_arc->shutdown;
765 root 1.103 $c_any->shutdown;
766 root 1.53
767     $_->join for @a_arc; # need to parse all archetypes before png processing
768 root 1.3
769 root 1.53 print "end arc, start png processing...\n" if $VERBOSE;
770    
771 root 1.69 # eight png crunchers work fine for my 4x smp machine
772     my @a_png = map +(async \&process_png), 1..8;
773 root 1.53
774 root 1.103 print "end any processing...\n" if $VERBOSE;
775     $_->join for @a_any;
776    
777     print "end png processing...\n" if $VERBOSE;
778     $_->join for @a_png;
779 root 1.53
780     print "scanning done, processing results...\n" if $VERBOSE;
781 root 1.5 {
782 root 1.27 # remove path prefix from editor_folder
783 root 1.76 $_->{editor_folder} =~ /^\x00/
784     and substr $_->{editor_folder}, 0, 2 + length $path, ""
785     for values %ARC;
786 root 1.27
787 root 1.53 print "resolving inheritance tree...\n" if $VERBOSE;
788 root 1.27 # resolve inherit
789     while () {
790     my $progress;
791     my $loop;
792    
793     for my $o (values %ARC) {
794 root 1.81 for my $other (split /,/, $o->{inherit}) {
795 root 1.27 if (my $s = $ARC{$other}) {
796     if ($s->{inherit}) {
797     $loop = $s;
798     } else {
799     delete $o->{inherit};
800 root 1.30 my %s = %$s;
801     delete @s{qw(_name more name name_pl)};
802     %$o = ( %s, %$o );
803 root 1.27 ++$progress;
804     }
805     } else {
806 root 1.103 warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
807 root 1.27 delete $ARC{$o->{_name}};
808     }
809     }
810     }
811    
812     unless ($progress) {
813     die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
814     if $loop;
815    
816     last;
817     }
818     }
819    
820 root 1.34 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
821     @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
822    
823 root 1.104 # fix up archetypes without names, where the archanme doesn't work at all
824     for (@ARC) {
825     if (!exists $_->{name} and $_->{_name} =~ /_/) {
826     for ($_->{name} = $_->{_name}) {
827     s/(?:_\d+)+$//;
828     s/_[nesw]+$//;
829     y/_/ /;
830     }
831     }
832     }
833    
834     #print "generating plurals...\n" if $VERBOSE;
835     #generate_plurals;
836 root 1.73
837 root 1.78 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
838 root 1.5 open my $fh, ">:utf8", "$DATADIR/archetypes~"
839     or die "$DATADIR/archetypes~: $!";
840 root 1.65 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
841 root 1.5 }
842    
843 root 1.6 {
844 root 1.78 printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE;
845 root 1.12 open my $fh, ">:utf8", "$DATADIR/treasures~"
846     or die "$DATADIR/treasures~: $!";
847     print $fh $TRS;
848     }
849    
850     {
851 root 1.53 print "processing facedata...\n" if $VERBOSE;
852 root 1.6 while (my ($k, $v) = each %FACEINFO) {
853 root 1.103 length $v->{data32} or warn "WARNING: face '$k' has no png32. this will not work (shoddy gcfclient will crash of course).\n";
854     length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n";
855 root 1.5
856 root 1.79 make_hash $k, $v->{data32}, $v->{hash32};
857     make_hash $k, $v->{data64}, $v->{hash64};
858    
859 root 1.103 #length $v->{data32} <= 10000 or warn "WARNING: face '$k' has face32 larger than 10000 bytes, will not work with crossfire client.\n";
860     #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n";
861 root 1.20
862 root 1.106 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
863     $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
864     $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
865 root 1.91
866 root 1.106 delete @$v{qw(arc stem)}; # not used by the server
867 root 1.6 }
868    
869 root 1.79 print "processing resources...\n" if $VERBOSE;
870     my $enc = JSON::XS->new->utf8->canonical->relaxed;
871     while (my ($k, $v) = each %RESOURCE) {
872    
873 root 1.83 if ($v->{meta} && $v->{meta}{datadir}) {
874     delete $RESOURCE{$k};
875 root 1.79
876 root 1.83 $k =~ s/^res\/// or die "$k: datadir files must be in res/";
877 root 1.79
878 root 1.83 printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE;
879     open my $fh, ">:raw", "$DATADIR/$k~"
880     or die "$DATADIR/$k~: $!";
881     syswrite $fh, $v->{data};
882     push @COMMIT, $k;
883    
884     } else {
885     if ($v->{type} & 1) {
886     # prepend meta info
887    
888     my $meta = $enc->encode ({
889     name => $k,
890     %{ $v->{meta} || {} },
891     });
892    
893     $v->{data} = pack "(w/a*)*", $meta, $v->{data};
894     }
895    
896     make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
897 root 1.79 }
898     }
899    
900 root 1.78 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
901     scalar keys %FACEINFO,
902     scalar keys %ANIMINFO,
903     scalar keys %RESOURCE
904     if $VERBOSE;
905    
906 root 1.19 open my $fh, ">:perlio", "$DATADIR/facedata~"
907     or die "$DATADIR/facedata~: $!";
908 root 1.6
909 root 1.82 print $fh nfreeze {
910 root 1.24 version => 2,
911     faceinfo => \%FACEINFO,
912     animinfo => \%ANIMINFO,
913 root 1.37 resource => \%RESOURCE,
914 root 1.24 };
915 root 1.78
916 root 1.5 }
917    
918 root 1.53 print "committing files...\n" if $VERBOSE;
919    
920 root 1.83 for (qw(archetypes facedata treasures), @COMMIT) {
921 root 1.6 chmod 0644, "$DATADIR/$_~";
922 root 1.13 rename "$DATADIR/$_~", "$DATADIR/$_"
923     or die "$DATADIR/$_: $!";
924 root 1.6 }
925 root 1.13
926     print "archetype data installed successfully.\n";
927 root 1.2 }
928     }
929    
930     Getopt::Long::Configure ("bundling", "no_ignore_case");
931     GetOptions (
932     "verbose|v:+" => \$VERBOSE,
933 root 1.3 "cache" => \$CACHE,
934 root 1.2 "quiet|q" => sub { $VERBOSE = 0 },
935     "force" => sub { $FORCE = 1 },
936     "install-arch=s" => \&inst_arch,
937     "install-maps=s" => \&inst_maps,
938     "print-statedir" => sub { print "@pkgstatedir@\n" },
939     "print-datadir" => sub { print "$DATADIR\n" },
940     "print-confdir" => sub { print "@pkgconfdir@\n" },
941     "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
942     "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
943     ) or usage;
944