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