… | |
… | |
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] |
… | |
… | |
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; |
89 | our $TRS; |
90 | our $TRS; |
90 | our $NFILE; |
91 | our $NFILE; |
91 | our %ANIM; |
|
|
92 | |
92 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
94 | |
95 | our (@png, @trs, @arc); # files we are interested in |
95 | our (@png, @trs, @arc); # files we are interested in |
96 | |
96 | |
… | |
… | |
255 | my $arc; |
255 | my $arc; |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | |
257 | |
258 | my $arc = read_arch "$dir/$file"; |
258 | my $arc = read_arch "$dir/$file"; |
259 | for my $o (values %$arc) { |
259 | for my $o (values %$arc) { |
260 | push @ARC, $o; |
260 | $ARC{$o->{_name}} = $o; |
261 | |
261 | |
262 | $o->{editor_folder} = $dir; |
262 | $o->{editor_folder} = $dir; |
263 | |
263 | |
264 | my $visibility = delete $o->{visibility}; |
264 | my $visibility = delete $o->{visibility}; |
265 | my $magicmap = delete $o->{magicmap}; |
265 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
276 | my $x = $o->{x} - $dx; |
276 | my $x = $o->{x} - $dx; |
277 | my $y = $o->{y} - $dy; |
277 | my $y = $o->{y} - $dy; |
278 | |
278 | |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
280 | |
280 | |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
282 | |
282 | |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
285 | |
285 | |
286 | my $anim = delete $o->{anim}; |
286 | my $anim = delete $o->{anim}; |
287 | |
287 | |
288 | if ($anim) { |
288 | if ($anim) { |
|
|
289 | # possibly add $ext to the animation name to avoid |
|
|
290 | # the need to specify archnames for all parts |
|
|
291 | # of a multipart archetype. |
289 | $o->{animation} = "$o->{_name}"; |
292 | $o->{animation} = "$o->{_name}"; |
|
|
293 | my $facings = 1; |
|
|
294 | my @frames; |
290 | |
295 | |
291 | for (@$anim) { |
296 | for (@$anim) { |
292 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
297 | if (/^facings\s+(\d+)/) { |
|
|
298 | $facings = $1*1; |
|
|
299 | } elsif (/^blank.x11$|^empty.x11$/) { |
|
|
300 | push @frames, $_; |
|
|
301 | } else { |
|
|
302 | push @frames, "$_$ext"; |
|
|
303 | } |
293 | } |
304 | } |
294 | |
305 | |
295 | $ANIM{"$o->{_name}$ext"} = |
306 | $ANIMINFO{$o->{animation}} = { |
296 | join "", map "$_\n", |
307 | facings => $facings, |
297 | "anim $o->{_name}", |
308 | frames => \@frames, |
298 | @$anim, |
309 | }; |
299 | "mina"; |
|
|
300 | } |
310 | } |
301 | |
311 | |
302 | for my $face ($o->{face} || (), @{$anim || []}) { |
312 | for my $face ($o->{face} || (), @{$anim || []}) { |
303 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
313 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
304 | |
314 | |
… | |
… | |
362 | } |
372 | } |
363 | |
373 | |
364 | sub inst_arch($) { |
374 | sub inst_arch($) { |
365 | my (undef, $path) = @_; |
375 | my (undef, $path) = @_; |
366 | |
376 | |
367 | print "installing '$path' to '$DATADIR'\n", |
377 | print "Installing '$path' to '$DATADIR'\n", |
|
|
378 | "\n", |
368 | "(this can take a long time if you run this\n", |
379 | "This can take a long time if you run this\n", |
369 | "for the first time or do not use --cache).\n"; |
380 | "for the first time or do not use --cache.\n", |
|
|
381 | "\n", |
|
|
382 | "Unless you run verbosely, all following warning\n", |
|
|
383 | "or error messages indicate serious problems.\n", |
|
|
384 | "\n"; |
370 | |
385 | |
371 | if (!-d "$path/treasures") { |
386 | if (!-d "$path/treasures") { |
372 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
387 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
373 | exit 1 unless $FORCE; |
388 | exit 1 unless $FORCE; |
374 | } |
389 | } |
… | |
… | |
382 | (async \&process_trs), (async \&process_trs), |
397 | (async \&process_trs), (async \&process_trs), |
383 | (async \&process_arc), (async \&process_arc), |
398 | (async \&process_arc), (async \&process_arc), |
384 | ); |
399 | ); |
385 | |
400 | |
386 | { |
401 | { |
387 | open my $fh, ">:utf8", "$DATADIR/animations~" |
402 | # remove path prefix from editor_folder |
388 | or die "$DATADIR/animations~: $!"; |
403 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
389 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
404 | for values %ARC; |
|
|
405 | |
|
|
406 | # resolve inherit |
|
|
407 | while () { |
|
|
408 | my $progress; |
|
|
409 | my $loop; |
|
|
410 | |
|
|
411 | for my $o (values %ARC) { |
|
|
412 | if (my $other = $o->{inherit}) { |
|
|
413 | if (my $s = $ARC{$other}) { |
|
|
414 | if ($s->{inherit}) { |
|
|
415 | $loop = $s; |
|
|
416 | } else { |
|
|
417 | delete $o->{inherit}; |
|
|
418 | %$o = ( %$s, %$o ); |
|
|
419 | ++$progress; |
|
|
420 | } |
|
|
421 | } else { |
|
|
422 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
423 | delete $ARC{$o->{_name}}; |
|
|
424 | } |
|
|
425 | } |
|
|
426 | } |
|
|
427 | |
|
|
428 | unless ($progress) { |
|
|
429 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
430 | if $loop; |
|
|
431 | |
|
|
432 | last; |
|
|
433 | } |
390 | } |
434 | } |
391 | |
435 | |
392 | { |
|
|
393 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
436 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
394 | or die "$DATADIR/archetypes~: $!"; |
437 | or die "$DATADIR/archetypes~: $!"; |
395 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
438 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } values %ARC]; |
396 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
397 | } |
439 | } |
398 | |
440 | |
399 | { |
441 | { |
400 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
442 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
401 | or die "$DATADIR/treasures~: $!"; |
443 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
415 | } |
457 | } |
416 | |
458 | |
417 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
459 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
418 | or die "$DATADIR/facedata~: $!"; |
460 | or die "$DATADIR/facedata~: $!"; |
419 | |
461 | |
420 | $FACEINFO{""} = { version => 1}; |
462 | print $fh freeze { |
421 | print $fh Storable::nfreeze \%FACEINFO; |
463 | version => 2, |
|
|
464 | faceinfo => \%FACEINFO, |
|
|
465 | animinfo => \%ANIMINFO, |
422 | } |
466 | }; |
|
|
467 | } |
423 | |
468 | |
424 | for (qw(archetypes facedata animations treasures)) { |
469 | for (qw(archetypes facedata treasures)) { |
425 | chmod 0644, "$DATADIR/$_~"; |
470 | chmod 0644, "$DATADIR/$_~"; |
426 | rename "$DATADIR/$_~", "$DATADIR/$_" |
471 | rename "$DATADIR/$_~", "$DATADIR/$_" |
427 | or die "$DATADIR/$_: $!"; |
472 | or die "$DATADIR/$_: $!"; |
428 | } |
473 | } |
429 | |
474 | |