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