… | |
… | |
9 | |
9 | |
10 | my $CONVERT = "@CONVERT@"; |
10 | my $CONVERT = "@CONVERT@"; |
11 | my $IDENTIFY = "@IDENTIFY@"; |
11 | my $IDENTIFY = "@IDENTIFY@"; |
12 | my $OPTIPNG = "@OPTIPNG@"; |
12 | my $OPTIPNG = "@OPTIPNG@"; |
13 | my $RSYNC = "@RSYNC@"; |
13 | my $RSYNC = "@RSYNC@"; |
|
|
14 | my $PNGNQ = "@PNGNQ@"; |
14 | |
15 | |
15 | use Getopt::Long; |
16 | use Getopt::Long; |
16 | use Coro::Event; |
17 | use Coro::Event; |
17 | use AnyEvent; |
18 | use AnyEvent; |
18 | use IO::AIO (); |
19 | use IO::AIO (); |
… | |
… | |
20 | use Crossfire; |
21 | use Crossfire; |
21 | use Coro; |
22 | use Coro; |
22 | use Coro::AIO; |
23 | use Coro::AIO; |
23 | use POSIX (); |
24 | use POSIX (); |
24 | use Digest::MD5; |
25 | use Digest::MD5; |
25 | use Storable; $Storable::canonical = 1; |
26 | use Coro::Storable; $Storable::canonical = 1; |
26 | |
27 | |
27 | sub usage { |
28 | sub usage { |
28 | warn <<EOF; |
29 | warn <<EOF; |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
30 | Usage: cfutil [-v] [-q] [--force] [--cache] |
30 | [--install-arch path] |
31 | [--install-arch path] |
… | |
… | |
68 | } |
69 | } |
69 | |
70 | |
70 | sub inst_maps($) { |
71 | sub inst_maps($) { |
71 | my (undef, $path) = @_; |
72 | my (undef, $path) = @_; |
72 | |
73 | |
73 | print "installing '$path' to '$DATADIR/maps'\n"; |
74 | print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
74 | |
75 | |
75 | if (!-f "$path/regions") { |
76 | if (!-f "$path/regions") { |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
77 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
77 | exit 1 unless $FORCE; |
78 | exit 1 unless $FORCE; |
78 | } |
79 | } |
… | |
… | |
82 | |
83 | |
83 | print "maps installed successfully.\n"; |
84 | print "maps installed successfully.\n"; |
84 | } |
85 | } |
85 | |
86 | |
86 | { |
87 | { |
|
|
88 | our %ANIMINFO; |
87 | our %FACEINFO; |
89 | our %FACEINFO; |
88 | our @ARC; |
90 | our @ARC; |
|
|
91 | our %ARC; |
89 | our $TRS; |
92 | our $TRS; |
90 | our $NFILE; |
93 | our $NFILE; |
91 | our %ANIM; |
|
|
92 | |
94 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
95 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
96 | |
95 | our (@png, @trs, @arc); # files we are interested in |
97 | our (@png, @trs, @arc); # files we are interested in |
96 | |
98 | |
… | |
… | |
147 | # possibly enlarge |
149 | # possibly enlarge |
148 | if (0 > aio_stat "$stem.64x64.png") { |
150 | if (0 > aio_stat "$stem.64x64.png") { |
149 | my $other = "$stem.64x64.png~"; |
151 | my $other = "$stem.64x64.png~"; |
150 | |
152 | |
151 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
153 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
152 | warn "rescale $other\n";#d# |
|
|
153 | my $wrap = 0; # for the time being |
154 | my $wrap = 0; # for the time being |
154 | fork_sub { |
155 | fork_sub { |
155 | system "convert png:\Q$path\E -depth 8 rgba:-" |
156 | system "convert png:\Q$path\E -depth 8 rgba:-" |
156 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
157 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
157 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
158 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
158 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
159 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
159 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
160 | system $OPTIPNG, "-i0", "-q", "$other~"; |
160 | die "$other~ has zero size, aborting." unless -s "$other~"; |
161 | die "$other~ has zero size, aborting." unless -s "$other~"; |
161 | rename "$other~", $other; |
162 | rename "$other~", $other; |
162 | }; |
163 | }; |
163 | } |
164 | } |
164 | |
165 | |
… | |
… | |
170 | my $other = "$stem.32x32.png~"; |
171 | my $other = "$stem.32x32.png~"; |
171 | |
172 | |
172 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
173 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
173 | fork_sub { |
174 | fork_sub { |
174 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
175 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
175 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
176 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
177 | |
|
|
178 | # reduce smoothfaces >10000 bytes |
|
|
179 | if ($stem =~ /_S\./ && (-s "$other~") > 10000) { |
|
|
180 | my $ncolor = 256; |
|
|
181 | while () { |
|
|
182 | system "<\Q$other~\E $PNGNQ -n$ncolor >\Q$other~~\E"; |
|
|
183 | system $OPTIPNG, "-i0", "-q", "$other~~"; |
|
|
184 | last if 10000 > -s "$other~~"; |
|
|
185 | $ncolor = int $ncolor * 0.9; |
|
|
186 | $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
|
|
187 | } |
|
|
188 | |
|
|
189 | printf "reduced %s from %d to %d bytes using %d colours.\n", |
|
|
190 | $other, -s "$other~", -s "$other~~", $ncolor |
|
|
191 | if $VERBOSE > 0; |
|
|
192 | rename "$other~~", "$other~"; |
|
|
193 | } |
|
|
194 | |
176 | die "$other~ has zero size, aborting." unless -s "$other~"; |
195 | die "$other~ has zero size, aborting." unless -s "$other~"; |
177 | rename "$other~", $other; |
196 | rename "$other~", $other; |
178 | }; |
197 | }; |
179 | } |
198 | } |
180 | |
199 | |
… | |
… | |
183 | } |
202 | } |
184 | } |
203 | } |
185 | |
204 | |
186 | (my $face = $stem) =~ s/^.*\///; |
205 | (my $face = $stem) =~ s/^.*\///; |
187 | |
206 | |
|
|
207 | # split all bigfaces, but avoid smoothfaces (*_S) |
188 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
208 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
189 | # split |
209 | # split |
190 | my @tile; |
210 | my @tile; |
191 | for my $x (0 .. (int $w / $T) - 1) { |
211 | for my $x (0 .. (int $w / $T) - 1) { |
192 | for my $y (0 .. (int $h / $T) - 1) { |
212 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
276 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | |
277 | |
258 | my $arc = read_arch "$dir/$file"; |
278 | my $arc = read_arch "$dir/$file"; |
259 | for my $o (values %$arc) { |
279 | for my $o (values %$arc) { |
260 | push @ARC, $o; |
280 | push @ARC, $o; |
|
|
281 | for (my $m = $o; $m; $m = $m->{more}) { |
|
|
282 | $ARC{$m->{_name}} = $m; |
|
|
283 | } |
261 | |
284 | |
262 | $o->{editor_folder} = $dir; |
285 | $o->{editor_folder} = $dir; |
263 | |
286 | |
264 | my $visibility = delete $o->{visibility}; |
287 | my $visibility = delete $o->{visibility}; |
265 | my $magicmap = delete $o->{magicmap}; |
288 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
276 | my $x = $o->{x} - $dx; |
299 | my $x = $o->{x} - $dx; |
277 | my $y = $o->{y} - $dy; |
300 | my $y = $o->{y} - $dy; |
278 | |
301 | |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
302 | my $ext = $x|$y ? "+$x+$y" : ""; |
280 | |
303 | |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
304 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
282 | |
305 | |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
306 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
307 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
285 | |
308 | |
286 | my $anim = delete $o->{anim}; |
309 | my $anim = delete $o->{anim}; |
287 | |
310 | |
288 | if ($anim) { |
311 | if ($anim) { |
|
|
312 | # possibly add $ext to the animation name to avoid |
|
|
313 | # the need to specify archnames for all parts |
|
|
314 | # of a multipart archetype. |
289 | $o->{animation} = "$o->{_name}"; |
315 | $o->{animation} = "$o->{_name}"; |
|
|
316 | my $facings = 1; |
|
|
317 | my @frames; |
290 | |
318 | |
291 | for (@$anim) { |
319 | for (@$anim) { |
292 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
320 | if (/^facings\s+(\d+)/) { |
|
|
321 | $facings = $1*1; |
|
|
322 | } elsif (/^blank.x11$|^empty.x11$/) { |
|
|
323 | push @frames, $_; |
|
|
324 | } else { |
|
|
325 | push @frames, "$_$ext"; |
|
|
326 | } |
293 | } |
327 | } |
294 | |
328 | |
295 | $ANIM{"$o->{_name}$ext"} = |
329 | $ANIMINFO{$o->{animation}} = { |
296 | join "", map "$_\n", |
330 | facings => $facings, |
297 | "anim $o->{_name}", |
331 | frames => \@frames, |
298 | @$anim, |
332 | }; |
299 | "mina"; |
|
|
300 | } |
333 | } |
301 | |
334 | |
302 | for my $face ($o->{face} || (), @{$anim || []}) { |
335 | for my $face ($o->{face} || (), @{$anim || []}) { |
303 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
336 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
304 | |
337 | |
… | |
… | |
307 | $info->{visibility} = $visibility if defined $visibility; |
340 | $info->{visibility} = $visibility if defined $visibility; |
308 | $info->{magicmap} = $magicmap if defined $magicmap; |
341 | $info->{magicmap} = $magicmap if defined $magicmap; |
309 | } |
342 | } |
310 | |
343 | |
311 | if (my $smooth = delete $o->{smoothface}) { |
344 | if (my $smooth = delete $o->{smoothface}) { |
312 | my ($face, $smooth) = split /\s+/, $smooth; |
345 | my %kv =split /\s+/, $smooth; |
313 | # skip empty_S.x11, it seems to server no purpose whatsoever |
346 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
314 | # but increases bandwidth demands and worse. |
347 | while (my ($face, $smooth) = each %kv) { |
315 | unless ($smooth eq "empty_S.x11") { |
|
|
316 | $FACEINFO{$face}{smooth} = $smooth; |
348 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
349 | $FACEINFO{$face}{smoothlevel} = $level; |
317 | } |
350 | } |
318 | } |
351 | } |
319 | } |
352 | } |
320 | } |
353 | } |
321 | } |
354 | } |
… | |
… | |
362 | } |
395 | } |
363 | |
396 | |
364 | sub inst_arch($) { |
397 | sub inst_arch($) { |
365 | my (undef, $path) = @_; |
398 | my (undef, $path) = @_; |
366 | |
399 | |
|
|
400 | print "\n", |
367 | print "installing '$path' to '$DATADIR'\n", |
401 | "Installing '$path' to '$DATADIR'\n", |
|
|
402 | "\n", |
368 | "(this can take a long time if you run this\n", |
403 | "This can take a long time if you run this\n", |
369 | "for the first time or do not use --cache).\n"; |
404 | "for the first time or do not use --cache.\n", |
|
|
405 | "\n", |
|
|
406 | "Unless you run verbosely, all following warning\n", |
|
|
407 | "or error messages indicate serious problems.\n", |
|
|
408 | "\n"; |
370 | |
409 | |
371 | if (!-d "$path/treasures") { |
410 | if (!-d "$path/treasures") { |
372 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
411 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
373 | exit 1 unless $FORCE; |
412 | exit 1 unless $FORCE; |
374 | } |
413 | } |
… | |
… | |
382 | (async \&process_trs), (async \&process_trs), |
421 | (async \&process_trs), (async \&process_trs), |
383 | (async \&process_arc), (async \&process_arc), |
422 | (async \&process_arc), (async \&process_arc), |
384 | ); |
423 | ); |
385 | |
424 | |
386 | { |
425 | { |
387 | open my $fh, ">:utf8", "$DATADIR/animations~" |
426 | # remove path prefix from editor_folder |
388 | or die "$DATADIR/animations~: $!"; |
427 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
389 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
428 | for values %ARC; |
|
|
429 | |
|
|
430 | # resolve inherit |
|
|
431 | while () { |
|
|
432 | my $progress; |
|
|
433 | my $loop; |
|
|
434 | |
|
|
435 | for my $o (values %ARC) { |
|
|
436 | if (my $other = $o->{inherit}) { |
|
|
437 | if (my $s = $ARC{$other}) { |
|
|
438 | if ($s->{inherit}) { |
|
|
439 | $loop = $s; |
|
|
440 | } else { |
|
|
441 | delete $o->{inherit}; |
|
|
442 | my %s = %$s; |
|
|
443 | delete @s{qw(_name more name name_pl)}; |
|
|
444 | %$o = ( %s, %$o ); |
|
|
445 | ++$progress; |
|
|
446 | } |
|
|
447 | } else { |
|
|
448 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
449 | delete $ARC{$o->{_name}}; |
|
|
450 | } |
|
|
451 | } |
|
|
452 | } |
|
|
453 | |
|
|
454 | unless ($progress) { |
|
|
455 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
456 | if $loop; |
|
|
457 | |
|
|
458 | last; |
|
|
459 | } |
390 | } |
460 | } |
391 | |
461 | |
392 | { |
|
|
393 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
462 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
394 | or die "$DATADIR/archetypes~: $!"; |
463 | or die "$DATADIR/archetypes~: $!"; |
395 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
464 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
396 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
397 | } |
465 | } |
398 | |
466 | |
399 | { |
467 | { |
400 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
468 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
401 | or die "$DATADIR/treasures~: $!"; |
469 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
415 | } |
483 | } |
416 | |
484 | |
417 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
485 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
418 | or die "$DATADIR/facedata~: $!"; |
486 | or die "$DATADIR/facedata~: $!"; |
419 | |
487 | |
420 | $FACEINFO{""} = { version => 1}; |
488 | print $fh freeze { |
421 | print $fh Storable::nfreeze \%FACEINFO; |
489 | version => 2, |
|
|
490 | faceinfo => \%FACEINFO, |
|
|
491 | animinfo => \%ANIMINFO, |
422 | } |
492 | }; |
|
|
493 | } |
423 | |
494 | |
424 | for (qw(archetypes facedata animations treasures)) { |
495 | for (qw(archetypes facedata treasures)) { |
425 | chmod 0644, "$DATADIR/$_~"; |
496 | chmod 0644, "$DATADIR/$_~"; |
426 | rename "$DATADIR/$_~", "$DATADIR/$_" |
497 | rename "$DATADIR/$_~", "$DATADIR/$_" |
427 | or die "$DATADIR/$_: $!"; |
498 | or die "$DATADIR/$_: $!"; |
428 | } |
499 | } |
429 | |
500 | |