… | |
… | |
6 | my $exec_prefix = "@exec_prefix@"; |
6 | my $exec_prefix = "@exec_prefix@"; |
7 | my $datarootdir = "@datarootdir@"; |
7 | my $datarootdir = "@datarootdir@"; |
8 | my $DATADIR = "@datadir@/@PACKAGE@"; |
8 | my $DATADIR = "@datadir@/@PACKAGE@"; |
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 | my $PNGNQ = "@PNGNQ@"; |
15 | |
15 | |
16 | use Getopt::Long; |
16 | use Getopt::Long; |
… | |
… | |
19 | use IO::AIO (); |
19 | use IO::AIO (); |
20 | use File::Temp; |
20 | use File::Temp; |
21 | use Crossfire; |
21 | use Crossfire; |
22 | use Coro; |
22 | use Coro; |
23 | use Coro::AIO; |
23 | use Coro::AIO; |
|
|
24 | use Coro::Util; |
24 | use POSIX (); |
25 | use POSIX (); |
25 | use Digest::MD5; |
26 | use Digest::MD5; |
|
|
27 | use Carp; |
26 | use Coro::Storable; $Storable::canonical = 1; |
28 | use Coro::Storable; $Storable::canonical = 1; |
|
|
29 | |
|
|
30 | $SIG{QUIT} = sub { Carp::cluck "QUIT" }; |
27 | |
31 | |
28 | sub usage { |
32 | sub usage { |
29 | warn <<EOF; |
33 | warn <<EOF; |
30 | Usage: cfutil [-v] [-q] [--force] [--cache] |
34 | Usage: cfutil [-v] [-q] [--force] [--cache] |
31 | [--install-arch path] |
35 | [--install-arch path] |
… | |
… | |
92 | if (!-f "$path/regions") { |
96 | if (!-f "$path/regions") { |
93 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
97 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
94 | exit 1 unless $FORCE; |
98 | exit 1 unless $FORCE; |
95 | } |
99 | } |
96 | |
100 | |
97 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
101 | system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
98 | and die "map installation failed.\n"; |
102 | and die "map installation failed.\n"; |
99 | |
103 | |
100 | print "maps installed successfully.\n"; |
104 | print "maps installed successfully.\n"; |
101 | } |
105 | } |
102 | |
106 | |
103 | { |
107 | { |
104 | our %ANIMINFO; |
108 | our %ANIMINFO; |
105 | our %FACEINFO; |
109 | our %FACEINFO; |
|
|
110 | our %RESOURCE; |
106 | our @ARC; |
111 | our @ARC; |
107 | our %ARC; |
112 | our %ARC; |
108 | our $TRS; |
113 | our $TRS; |
109 | our $NFILE; |
114 | our $NFILE; |
|
|
115 | our $PATH; |
110 | |
116 | |
111 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
117 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
112 | |
118 | |
113 | our (@png, @trs, @arc); # files we are interested in |
119 | our (@png, @trs, @arc, @res); # files we are interested in |
114 | |
120 | |
115 | sub commit_png($$$) { |
121 | sub commit_png($$$) { |
116 | my ($name, $data, $T) = @_; |
122 | my ($name, $data, $T) = @_; |
117 | |
123 | |
118 | $FACEINFO{$name}{"data$T"} = $data; |
124 | $FACEINFO{$name}{"data$T"} = $data; |
… | |
… | |
165 | # possibly enlarge |
171 | # possibly enlarge |
166 | if (0 > aio_stat "$stem.64x64.png") { |
172 | if (0 > aio_stat "$stem.64x64.png") { |
167 | my $other = "$stem.64x64.png~"; |
173 | my $other = "$stem.64x64.png~"; |
168 | |
174 | |
169 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
175 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
170 | my $wrap = 0; # for the time being |
|
|
171 | fork_sub { |
176 | fork_sub { |
172 | system "convert png:\Q$path\E -depth 8 rgba:-" |
177 | system "convert -depth 8 png:\Q$path\E rgba:-" |
173 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
178 | . "| $exec_prefix/bin/cfhq2xa $w $h 0" |
174 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
179 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
175 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
180 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
176 | system $OPTIPNG, "-i0", "-q", "$other~"; |
181 | system $OPTIPNG, "-i0", "-q", "$other~"; |
177 | die "$other~ has zero size, aborting." unless -s "$other~"; |
182 | die "$other~ has zero size, aborting." unless -s "$other~"; |
178 | rename "$other~", $other; |
183 | rename "$other~", $other; |
… | |
… | |
383 | |
388 | |
384 | $TRS .= $trs; |
389 | $TRS .= $trs; |
385 | } |
390 | } |
386 | } |
391 | } |
387 | |
392 | |
|
|
393 | my %FILECACHE; |
|
|
394 | |
|
|
395 | sub load_cached($;$) { |
|
|
396 | unless (exists $FILECACHE{$_[0]}) { |
|
|
397 | my $data; |
|
|
398 | if (0 < aio_load $_[0], $data) { |
|
|
399 | if ($_[1]) { |
|
|
400 | $data = eval { $_[1]->($data) }; |
|
|
401 | warn "$_[0]: $@" if $@; |
|
|
402 | } |
|
|
403 | } |
|
|
404 | |
|
|
405 | $FILECACHE{$_[0]} = $data; |
|
|
406 | } |
|
|
407 | |
|
|
408 | $FILECACHE{$_[0]} |
|
|
409 | } |
|
|
410 | |
|
|
411 | sub process_res { |
|
|
412 | while (@res) { |
|
|
413 | my ($dir, $file, $type) = @{pop @res}; |
|
|
414 | |
|
|
415 | my $data; |
|
|
416 | aio_load "$dir/$file", $data; |
|
|
417 | |
|
|
418 | my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift }; |
|
|
419 | |
|
|
420 | next if $meta && !exists $meta->{$file}; |
|
|
421 | |
|
|
422 | $meta = { |
|
|
423 | %{ $meta->{"" } || {} }, |
|
|
424 | %{ $meta->{$file} || {} }, |
|
|
425 | }; |
|
|
426 | |
|
|
427 | if ($meta->{license} =~ s/^#//) { |
|
|
428 | $meta->{license} = ({ |
|
|
429 | "pd" => "Public Domain", |
|
|
430 | "gpl" => "GNU General Public License, version 3.0 or any later", |
|
|
431 | "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", |
|
|
432 | "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.5/", |
|
|
433 | "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", |
|
|
434 | })->{$meta->{license}} |
|
|
435 | || warn "$dir/$file: license tag '$meta->{license}' not found."; |
|
|
436 | } |
|
|
437 | |
|
|
438 | $file =~ s/\.res$//; |
|
|
439 | $file =~ s/\.(ogg|wav|jpg|png)$//; |
|
|
440 | |
|
|
441 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
442 | |
|
|
443 | $RESOURCE{"$dir/$file"} = { |
|
|
444 | type => (delete $meta->{type}) || $type, |
|
|
445 | data => $data, |
|
|
446 | chksum => (Digest::MD5::md5 $data), |
|
|
447 | %$meta ? (meta => $meta) : (), |
|
|
448 | }; |
|
|
449 | } |
|
|
450 | } |
|
|
451 | |
388 | sub find_files; |
452 | sub find_files; |
389 | sub find_files { |
453 | sub find_files { |
390 | my ($path) = @_; |
454 | my ($path) = @_; |
391 | |
455 | |
392 | IO::AIO::aioreq_pri 4; |
456 | IO::AIO::aioreq_pri 4; |
… | |
… | |
394 | my ($dirs, $nondirs) = @_; |
458 | my ($dirs, $nondirs) = @_; |
395 | |
459 | |
396 | find_files "$path/$_" |
460 | find_files "$path/$_" |
397 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
461 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
398 | |
462 | |
|
|
463 | my $dir = $path; |
|
|
464 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
465 | |
399 | for my $file (@$nondirs) { |
466 | for my $file (@$nondirs) { |
|
|
467 | if ($dir =~ /^music(?:\/|$)/) { |
|
|
468 | push @res, [$path, $file, 3] # FT_MUSIC |
|
|
469 | if $file =~ /\.(ogg)$/; |
|
|
470 | |
|
|
471 | } elsif ($dir =~ /^sound(?:\/|$)/) { |
|
|
472 | push @res, [$path, $file, 5] # FT_SOUND |
|
|
473 | if $file =~ /\.(wav|ogg)$/; |
|
|
474 | |
|
|
475 | } elsif ($dir =~ /^res(?:\/|$)/) { |
|
|
476 | push @res, [$path, $file, 0] # FT_FACE |
|
|
477 | if $file =~ /\.(jpg|png)$/; |
|
|
478 | push @res, [$path, $file, 7] # FT_RSRC |
|
|
479 | if $file =~ /\.(res)$/; |
|
|
480 | |
400 | if ($file =~ /\.png$/) { |
481 | } elsif ($file =~ /\.png$/) { |
401 | push @png, ["$path/$file", 0]; |
482 | push @png, ["$path/$file", 0]; |
|
|
483 | |
402 | } elsif ($file =~ /\.trs$/) { |
484 | } elsif ($file =~ /\.trs$/) { |
403 | push @trs, [$path, $file]; |
485 | push @trs, [$path, $file]; |
|
|
486 | |
404 | } elsif ($file =~ /\.arc$/) { |
487 | } elsif ($file =~ /\.arc$/) { |
405 | push @arc, [$path, $file]; |
488 | push @arc, [$path, $file]; |
|
|
489 | |
406 | } else { |
490 | } else { |
407 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
491 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
408 | } |
492 | } |
409 | } |
493 | } |
410 | }; |
494 | }; |
411 | } |
495 | } |
412 | |
496 | |
413 | sub inst_arch($) { |
497 | sub inst_arch($) { |
414 | my (undef, $path) = @_; |
498 | my (undef, $path) = @_; |
|
|
499 | |
|
|
500 | $PATH = $path; |
415 | |
501 | |
416 | print "\n", |
502 | print "\n", |
417 | "Installing '$path' to '$DATADIR'\n", |
503 | "Installing '$path' to '$DATADIR'\n", |
418 | "\n", |
504 | "\n", |
419 | "This can take a long time if you run this\n", |
505 | "This can take a long time if you run this\n", |
… | |
… | |
434 | $_->join for ( |
520 | $_->join for ( |
435 | # four png crunchers work fine for my 2x smp machine |
521 | # four png crunchers work fine for my 2x smp machine |
436 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
522 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
437 | (async \&process_trs), (async \&process_trs), |
523 | (async \&process_trs), (async \&process_trs), |
438 | (async \&process_arc), (async \&process_arc), |
524 | (async \&process_arc), (async \&process_arc), |
|
|
525 | (async \&process_res), (async \&process_res), |
439 | ); |
526 | ); |
440 | |
527 | |
441 | { |
528 | { |
442 | # remove path prefix from editor_folder |
529 | # remove path prefix from editor_folder |
443 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
530 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
… | |
… | |
511 | |
598 | |
512 | print $fh freeze { |
599 | print $fh freeze { |
513 | version => 2, |
600 | version => 2, |
514 | faceinfo => \%FACEINFO, |
601 | faceinfo => \%FACEINFO, |
515 | animinfo => \%ANIMINFO, |
602 | animinfo => \%ANIMINFO, |
|
|
603 | resource => \%RESOURCE, |
516 | }; |
604 | }; |
517 | } |
605 | } |
518 | |
606 | |
519 | for (qw(archetypes facedata treasures)) { |
607 | for (qw(archetypes facedata treasures)) { |
520 | chmod 0644, "$DATADIR/$_~"; |
608 | chmod 0644, "$DATADIR/$_~"; |