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