… | |
… | |
20 | use Crossfire; |
20 | use Crossfire; |
21 | use Coro; |
21 | use Coro; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
23 | use POSIX (); |
23 | use POSIX (); |
24 | use Digest::MD5; |
24 | use Digest::MD5; |
25 | use Storable; $Storable::canonical = 1; |
25 | use Coro::Storable; $Storable::canonical = 1; |
26 | |
26 | |
27 | sub usage { |
27 | sub usage { |
28 | warn <<EOF; |
28 | warn <<EOF; |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
30 | [--install-arch path] |
30 | [--install-arch path] |
… | |
… | |
68 | } |
68 | } |
69 | |
69 | |
70 | sub inst_maps($) { |
70 | sub inst_maps($) { |
71 | my (undef, $path) = @_; |
71 | my (undef, $path) = @_; |
72 | |
72 | |
73 | print "installing '$path' to '$DATADIR/maps'\n"; |
73 | print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
74 | |
74 | |
75 | if (!-f "$path/regions") { |
75 | if (!-f "$path/regions") { |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
77 | exit 1 unless $FORCE; |
77 | exit 1 unless $FORCE; |
78 | } |
78 | } |
… | |
… | |
82 | |
82 | |
83 | print "maps installed successfully.\n"; |
83 | print "maps installed successfully.\n"; |
84 | } |
84 | } |
85 | |
85 | |
86 | { |
86 | { |
|
|
87 | our %ANIMINFO; |
87 | our %FACEINFO; |
88 | our %FACEINFO; |
88 | our @ARC; |
89 | our @ARC; |
|
|
90 | our %ARC; |
89 | our $TRS; |
91 | our $TRS; |
90 | our $NFILE; |
92 | our $NFILE; |
91 | our %ANIM; |
|
|
92 | |
93 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
95 | |
95 | our (@png, @trs, @arc); # files we are interested in |
96 | our (@png, @trs, @arc); # files we are interested in |
96 | |
97 | |
… | |
… | |
147 | # possibly enlarge |
148 | # possibly enlarge |
148 | if (0 > aio_stat "$stem.64x64.png") { |
149 | if (0 > aio_stat "$stem.64x64.png") { |
149 | my $other = "$stem.64x64.png~"; |
150 | my $other = "$stem.64x64.png~"; |
150 | |
151 | |
151 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
152 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
152 | warn "rescale $other\n";#d# |
|
|
153 | my $wrap = 0; # for the time being |
153 | my $wrap = 0; # for the time being |
154 | fork_sub { |
154 | fork_sub { |
155 | system "convert png:\Q$path\E -depth 8 rgba:-" |
155 | system "convert png:\Q$path\E -depth 8 rgba:-" |
156 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
156 | . "| $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~" |
157 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
… | |
… | |
183 | } |
183 | } |
184 | } |
184 | } |
185 | |
185 | |
186 | (my $face = $stem) =~ s/^.*\///; |
186 | (my $face = $stem) =~ s/^.*\///; |
187 | |
187 | |
|
|
188 | # split all bigfaces, but avoid smoothfaces (*_S) |
188 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
189 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
189 | # split |
190 | # split |
190 | my @tile; |
191 | my @tile; |
191 | for my $x (0 .. (int $w / $T) - 1) { |
192 | for my $x (0 .. (int $w / $T) - 1) { |
192 | for my $y (0 .. (int $h / $T) - 1) { |
193 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | |
258 | |
258 | my $arc = read_arch "$dir/$file"; |
259 | my $arc = read_arch "$dir/$file"; |
259 | for my $o (values %$arc) { |
260 | for my $o (values %$arc) { |
260 | push @ARC, $o; |
261 | push @ARC, $o; |
|
|
262 | for (my $m = $o; $m; $m = $m->{more}) { |
|
|
263 | $ARC{$m->{_name}} = $m; |
|
|
264 | } |
261 | |
265 | |
262 | $o->{editor_folder} = $dir; |
266 | $o->{editor_folder} = $dir; |
263 | |
267 | |
264 | my $visibility = delete $o->{visibility}; |
268 | my $visibility = delete $o->{visibility}; |
265 | my $magicmap = delete $o->{magicmap}; |
269 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
276 | my $x = $o->{x} - $dx; |
280 | my $x = $o->{x} - $dx; |
277 | my $y = $o->{y} - $dy; |
281 | my $y = $o->{y} - $dy; |
278 | |
282 | |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
283 | my $ext = $x|$y ? "+$x+$y" : ""; |
280 | |
284 | |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
285 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
282 | |
286 | |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
287 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
288 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
285 | |
289 | |
286 | my $anim = delete $o->{anim}; |
290 | my $anim = delete $o->{anim}; |
287 | |
291 | |
288 | if ($anim) { |
292 | if ($anim) { |
|
|
293 | # possibly add $ext to the animation name to avoid |
|
|
294 | # the need to specify archnames for all parts |
|
|
295 | # of a multipart archetype. |
289 | $o->{animation} = "$o->{_name}"; |
296 | $o->{animation} = "$o->{_name}"; |
|
|
297 | my $facings = 1; |
|
|
298 | my @frames; |
290 | |
299 | |
291 | for (@$anim) { |
300 | for (@$anim) { |
292 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
301 | if (/^facings\s+(\d+)/) { |
|
|
302 | $facings = $1*1; |
|
|
303 | } elsif (/^blank.x11$|^empty.x11$/) { |
|
|
304 | push @frames, $_; |
|
|
305 | } else { |
|
|
306 | push @frames, "$_$ext"; |
|
|
307 | } |
293 | } |
308 | } |
294 | |
309 | |
295 | $ANIM{"$o->{_name}$ext"} = |
310 | $ANIMINFO{$o->{animation}} = { |
296 | join "", map "$_\n", |
311 | facings => $facings, |
297 | "anim $o->{_name}", |
312 | frames => \@frames, |
298 | @$anim, |
313 | }; |
299 | "mina"; |
|
|
300 | } |
314 | } |
301 | |
315 | |
302 | for my $face ($o->{face} || (), @{$anim || []}) { |
316 | for my $face ($o->{face} || (), @{$anim || []}) { |
303 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
317 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
304 | |
318 | |
… | |
… | |
307 | $info->{visibility} = $visibility if defined $visibility; |
321 | $info->{visibility} = $visibility if defined $visibility; |
308 | $info->{magicmap} = $magicmap if defined $magicmap; |
322 | $info->{magicmap} = $magicmap if defined $magicmap; |
309 | } |
323 | } |
310 | |
324 | |
311 | if (my $smooth = delete $o->{smoothface}) { |
325 | if (my $smooth = delete $o->{smoothface}) { |
312 | my ($face, $smooth) = split /\s+/, $smooth; |
326 | my %kv =split /\s+/, $smooth; |
313 | # skip empty_S.x11, it seems to server no purpose whatsoever |
327 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
314 | # but increases bandwidth demands and worse. |
328 | while (my ($face, $smooth) = each %kv) { |
315 | unless ($smooth eq "empty_S.x11") { |
|
|
316 | $FACEINFO{$face}{smooth} = $smooth; |
329 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
330 | $FACEINFO{$face}{smoothlevel} = $level; |
317 | } |
331 | } |
318 | } |
332 | } |
319 | } |
333 | } |
320 | } |
334 | } |
321 | } |
335 | } |
… | |
… | |
362 | } |
376 | } |
363 | |
377 | |
364 | sub inst_arch($) { |
378 | sub inst_arch($) { |
365 | my (undef, $path) = @_; |
379 | my (undef, $path) = @_; |
366 | |
380 | |
|
|
381 | print "\n", |
367 | print "installing '$path' to '$DATADIR'\n", |
382 | "Installing '$path' to '$DATADIR'\n", |
|
|
383 | "\n", |
368 | "(this can take a long time if you run this\n", |
384 | "This can take a long time if you run this\n", |
369 | "for the first time or do not use --cache).\n"; |
385 | "for the first time or do not use --cache.\n", |
|
|
386 | "\n", |
|
|
387 | "Unless you run verbosely, all following warning\n", |
|
|
388 | "or error messages indicate serious problems.\n", |
|
|
389 | "\n"; |
370 | |
390 | |
371 | if (!-d "$path/treasures") { |
391 | if (!-d "$path/treasures") { |
372 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
392 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
373 | exit 1 unless $FORCE; |
393 | exit 1 unless $FORCE; |
374 | } |
394 | } |
… | |
… | |
382 | (async \&process_trs), (async \&process_trs), |
402 | (async \&process_trs), (async \&process_trs), |
383 | (async \&process_arc), (async \&process_arc), |
403 | (async \&process_arc), (async \&process_arc), |
384 | ); |
404 | ); |
385 | |
405 | |
386 | { |
406 | { |
387 | open my $fh, ">:utf8", "$DATADIR/animations~" |
407 | # remove path prefix from editor_folder |
388 | or die "$DATADIR/animations~: $!"; |
408 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
389 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
409 | for values %ARC; |
|
|
410 | |
|
|
411 | # resolve inherit |
|
|
412 | while () { |
|
|
413 | my $progress; |
|
|
414 | my $loop; |
|
|
415 | |
|
|
416 | for my $o (values %ARC) { |
|
|
417 | if (my $other = $o->{inherit}) { |
|
|
418 | if (my $s = $ARC{$other}) { |
|
|
419 | if ($s->{inherit}) { |
|
|
420 | $loop = $s; |
|
|
421 | } else { |
|
|
422 | delete $o->{inherit}; |
|
|
423 | my %s = %$s; |
|
|
424 | delete @s{qw(_name more name name_pl)}; |
|
|
425 | %$o = ( %s, %$o ); |
|
|
426 | ++$progress; |
|
|
427 | } |
|
|
428 | } else { |
|
|
429 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
430 | delete $ARC{$o->{_name}}; |
|
|
431 | } |
|
|
432 | } |
|
|
433 | } |
|
|
434 | |
|
|
435 | unless ($progress) { |
|
|
436 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
437 | if $loop; |
|
|
438 | |
|
|
439 | last; |
|
|
440 | } |
390 | } |
441 | } |
391 | |
442 | |
392 | { |
|
|
393 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
443 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
394 | or die "$DATADIR/archetypes~: $!"; |
444 | or die "$DATADIR/archetypes~: $!"; |
395 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
445 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
396 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
397 | } |
446 | } |
398 | |
447 | |
399 | { |
448 | { |
400 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
449 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
401 | or die "$DATADIR/treasures~: $!"; |
450 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
415 | } |
464 | } |
416 | |
465 | |
417 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
466 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
418 | or die "$DATADIR/facedata~: $!"; |
467 | or die "$DATADIR/facedata~: $!"; |
419 | |
468 | |
420 | $FACEINFO{""} = { version => 1}; |
469 | print $fh freeze { |
421 | print $fh Storable::nfreeze \%FACEINFO; |
470 | version => 2, |
|
|
471 | faceinfo => \%FACEINFO, |
|
|
472 | animinfo => \%ANIMINFO, |
422 | } |
473 | }; |
|
|
474 | } |
423 | |
475 | |
424 | for (qw(archetypes facedata animations treasures)) { |
476 | for (qw(archetypes facedata treasures)) { |
425 | chmod 0644, "$DATADIR/$_~"; |
477 | chmod 0644, "$DATADIR/$_~"; |
426 | rename "$DATADIR/$_~", "$DATADIR/$_" |
478 | rename "$DATADIR/$_~", "$DATADIR/$_" |
427 | or die "$DATADIR/$_: $!"; |
479 | or die "$DATADIR/$_: $!"; |
428 | } |
480 | } |
429 | |
481 | |