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