… | |
… | |
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 | |
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] |
… | |
… | |
42 | my $CACHE = 0; |
43 | my $CACHE = 0; |
43 | my $FORCE; |
44 | my $FORCE; |
44 | my $TMPDIR = "/tmp/cfutil$$~"; |
45 | my $TMPDIR = "/tmp/cfutil$$~"; |
45 | my $TMPFILE = "aaaa0"; |
46 | my $TMPFILE = "aaaa0"; |
46 | |
47 | |
|
|
48 | our %COLOR = ( |
|
|
49 | black => 0, |
|
|
50 | white => 1, |
|
|
51 | navy => 2, |
|
|
52 | red => 3, |
|
|
53 | orange => 4, |
|
|
54 | blue => 5, |
|
|
55 | darkorange => 6, |
|
|
56 | green => 7, |
|
|
57 | lightgreen => 8, |
|
|
58 | grey => 9, |
|
|
59 | brown => 10, |
|
|
60 | gold => 11, |
|
|
61 | tan => 12, |
|
|
62 | ); |
|
|
63 | |
47 | END { system "rm", "-rf", $TMPDIR } |
64 | END { system "rm", "-rf", $TMPDIR } |
48 | |
65 | |
49 | Event->signal (signal => "INT", cb => sub { exit 1 }); |
66 | Event->signal (signal => "INT", cb => sub { exit 1 }); |
50 | Event->signal (signal => "TERM", cb => sub { exit 1 }); |
67 | Event->signal (signal => "TERM", cb => sub { exit 1 }); |
51 | |
68 | |
… | |
… | |
68 | } |
85 | } |
69 | |
86 | |
70 | sub inst_maps($) { |
87 | sub inst_maps($) { |
71 | my (undef, $path) = @_; |
88 | my (undef, $path) = @_; |
72 | |
89 | |
73 | print "installing '$path' to '$DATADIR/maps'\n"; |
90 | print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
74 | |
91 | |
75 | if (!-f "$path/regions") { |
92 | if (!-f "$path/regions") { |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
93 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
77 | exit 1 unless $FORCE; |
94 | exit 1 unless $FORCE; |
78 | } |
95 | } |
… | |
… | |
84 | } |
101 | } |
85 | |
102 | |
86 | { |
103 | { |
87 | our %ANIMINFO; |
104 | our %ANIMINFO; |
88 | our %FACEINFO; |
105 | our %FACEINFO; |
|
|
106 | our %RESOURCE; |
89 | our @ARC; |
107 | our @ARC; |
|
|
108 | our %ARC; |
90 | our $TRS; |
109 | our $TRS; |
91 | our $NFILE; |
110 | our $NFILE; |
|
|
111 | our $PATH; |
92 | |
112 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
113 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
114 | |
95 | our (@png, @trs, @arc); # files we are interested in |
115 | our (@png, @trs, @arc, @res); # files we are interested in |
96 | |
116 | |
97 | sub commit_png($$$) { |
117 | sub commit_png($$$) { |
98 | my ($name, $data, $T) = @_; |
118 | my ($name, $data, $T) = @_; |
99 | |
119 | |
100 | $FACEINFO{$name}{"data$T"} = $data; |
120 | $FACEINFO{$name}{"data$T"} = $data; |
… | |
… | |
153 | fork_sub { |
173 | fork_sub { |
154 | system "convert png:\Q$path\E -depth 8 rgba:-" |
174 | system "convert png:\Q$path\E -depth 8 rgba:-" |
155 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
175 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
156 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
176 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
157 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
177 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
158 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
178 | system $OPTIPNG, "-i0", "-q", "$other~"; |
159 | die "$other~ has zero size, aborting." unless -s "$other~"; |
179 | die "$other~ has zero size, aborting." unless -s "$other~"; |
160 | rename "$other~", $other; |
180 | rename "$other~", $other; |
161 | }; |
181 | }; |
162 | } |
182 | } |
163 | |
183 | |
… | |
… | |
169 | my $other = "$stem.32x32.png~"; |
189 | my $other = "$stem.32x32.png~"; |
170 | |
190 | |
171 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
191 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
172 | fork_sub { |
192 | fork_sub { |
173 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
193 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
174 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
194 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
195 | |
|
|
196 | # reduce smoothfaces >10000 bytes |
|
|
197 | if ($stem =~ /_S\./ && (-s "$other~") > 10000) { |
|
|
198 | my $ncolor = 256; |
|
|
199 | while () { |
|
|
200 | system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; |
|
|
201 | system $OPTIPNG, "-i0", "-q", "$other~~"; |
|
|
202 | last if 10000 > -s "$other~~"; |
|
|
203 | $ncolor = int $ncolor * 0.9; |
|
|
204 | $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
|
|
205 | } |
|
|
206 | |
|
|
207 | printf "reduced %s from %d to %d bytes using %d colours.\n", |
|
|
208 | $other, -s "$other~", -s "$other~~", $ncolor |
|
|
209 | if $VERBOSE >= 2; |
|
|
210 | rename "$other~~", "$other~"; |
|
|
211 | } |
|
|
212 | |
175 | die "$other~ has zero size, aborting." unless -s "$other~"; |
213 | die "$other~ has zero size, aborting." unless -s "$other~"; |
176 | rename "$other~", $other; |
214 | rename "$other~", $other; |
177 | }; |
215 | }; |
178 | } |
216 | } |
179 | |
217 | |
… | |
… | |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
294 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | |
295 | |
258 | my $arc = read_arch "$dir/$file"; |
296 | my $arc = read_arch "$dir/$file"; |
259 | for my $o (values %$arc) { |
297 | for my $o (values %$arc) { |
260 | push @ARC, $o; |
298 | push @ARC, $o; |
|
|
299 | for (my $m = $o; $m; $m = $m->{more}) { |
|
|
300 | $ARC{$m->{_name}} = $m; |
|
|
301 | } |
261 | |
302 | |
262 | $o->{editor_folder} = $dir; |
303 | $o->{editor_folder} = $dir; |
263 | |
304 | |
264 | my $visibility = delete $o->{visibility}; |
305 | my $visibility = delete $o->{visibility}; |
265 | my $magicmap = delete $o->{magicmap}; |
306 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
276 | my $x = $o->{x} - $dx; |
317 | my $x = $o->{x} - $dx; |
277 | my $y = $o->{y} - $dy; |
318 | my $y = $o->{y} - $dy; |
278 | |
319 | |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
320 | my $ext = $x|$y ? "+$x+$y" : ""; |
280 | |
321 | |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
322 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
282 | |
323 | |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
324 | $visibility = delete $o->{visibility} if exists $o->{visibility}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
325 | $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
285 | |
326 | |
286 | my $anim = delete $o->{anim}; |
327 | my $anim = delete $o->{anim}; |
287 | |
328 | |
288 | if ($anim) { |
329 | if ($anim) { |
289 | # possibly add $ext to the animation name to avoid |
330 | # possibly add $ext to the animation name to avoid |
… | |
… | |
344 | |
385 | |
345 | $TRS .= $trs; |
386 | $TRS .= $trs; |
346 | } |
387 | } |
347 | } |
388 | } |
348 | |
389 | |
|
|
390 | sub process_res { |
|
|
391 | while (@res) { |
|
|
392 | my ($dir, $file) = @{pop @res}; |
|
|
393 | |
|
|
394 | my $data; |
|
|
395 | aio_load "$dir/$file", $data; |
|
|
396 | |
|
|
397 | $file =~ s/\.res$//; |
|
|
398 | $file =~ /\.([^.]+)$/ |
|
|
399 | or next; |
|
|
400 | |
|
|
401 | my $type = $1; |
|
|
402 | |
|
|
403 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
404 | |
|
|
405 | $RESOURCE{"$dir/$file"} = { |
|
|
406 | type => $1, |
|
|
407 | copyright => "", # TODO |
|
|
408 | data => $data, |
|
|
409 | chksum => Digest::MD5::md5 $data, |
|
|
410 | }; |
|
|
411 | } |
|
|
412 | } |
|
|
413 | |
349 | sub find_files; |
414 | sub find_files; |
350 | sub find_files { |
415 | sub find_files { |
351 | my ($path) = @_; |
416 | my ($path) = @_; |
352 | |
417 | |
353 | IO::AIO::aioreq_pri 4; |
418 | IO::AIO::aioreq_pri 4; |
… | |
… | |
362 | push @png, ["$path/$file", 0]; |
427 | push @png, ["$path/$file", 0]; |
363 | } elsif ($file =~ /\.trs$/) { |
428 | } elsif ($file =~ /\.trs$/) { |
364 | push @trs, [$path, $file]; |
429 | push @trs, [$path, $file]; |
365 | } elsif ($file =~ /\.arc$/) { |
430 | } elsif ($file =~ /\.arc$/) { |
366 | push @arc, [$path, $file]; |
431 | push @arc, [$path, $file]; |
|
|
432 | } elsif ($file =~ /\.(ogg|res)$/) { |
|
|
433 | push @res, [$path, $file]; |
367 | } else { |
434 | } else { |
368 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
435 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
369 | } |
436 | } |
370 | } |
437 | } |
371 | }; |
438 | }; |
372 | } |
439 | } |
373 | |
440 | |
374 | sub inst_arch($) { |
441 | sub inst_arch($) { |
375 | my (undef, $path) = @_; |
442 | my (undef, $path) = @_; |
376 | |
443 | |
|
|
444 | $PATH = $path; |
|
|
445 | |
|
|
446 | print "\n", |
377 | print "installing '$path' to '$DATADIR'\n", |
447 | "Installing '$path' to '$DATADIR'\n", |
|
|
448 | "\n", |
378 | "(this can take a long time if you run this\n", |
449 | "This can take a long time if you run this\n", |
379 | "for the first time or do not use --cache).\n"; |
450 | "for the first time or do not use --cache.\n", |
|
|
451 | "\n", |
|
|
452 | "Unless you run verbosely, all following warning\n", |
|
|
453 | "or error messages indicate serious problems.\n", |
|
|
454 | "\n"; |
380 | |
455 | |
381 | if (!-d "$path/treasures") { |
456 | if (!-d "$path/treasures") { |
382 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
457 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
383 | exit 1 unless $FORCE; |
458 | exit 1 unless $FORCE; |
384 | } |
459 | } |
… | |
… | |
389 | $_->join for ( |
464 | $_->join for ( |
390 | # four png crunchers work fine for my 2x smp machine |
465 | # four png crunchers work fine for my 2x smp machine |
391 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
466 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
392 | (async \&process_trs), (async \&process_trs), |
467 | (async \&process_trs), (async \&process_trs), |
393 | (async \&process_arc), (async \&process_arc), |
468 | (async \&process_arc), (async \&process_arc), |
|
|
469 | (async \&process_res), (async \&process_res), |
394 | ); |
470 | ); |
395 | |
471 | |
396 | { |
472 | { |
|
|
473 | # remove path prefix from editor_folder |
|
|
474 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
|
|
475 | for values %ARC; |
|
|
476 | |
|
|
477 | # resolve inherit |
|
|
478 | while () { |
|
|
479 | my $progress; |
|
|
480 | my $loop; |
|
|
481 | |
|
|
482 | for my $o (values %ARC) { |
|
|
483 | if (my $other = $o->{inherit}) { |
|
|
484 | if (my $s = $ARC{$other}) { |
|
|
485 | if ($s->{inherit}) { |
|
|
486 | $loop = $s; |
|
|
487 | } else { |
|
|
488 | delete $o->{inherit}; |
|
|
489 | my %s = %$s; |
|
|
490 | delete @s{qw(_name more name name_pl)}; |
|
|
491 | %$o = ( %s, %$o ); |
|
|
492 | ++$progress; |
|
|
493 | } |
|
|
494 | } else { |
|
|
495 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
496 | delete $ARC{$o->{_name}}; |
|
|
497 | } |
|
|
498 | } |
|
|
499 | } |
|
|
500 | |
|
|
501 | unless ($progress) { |
|
|
502 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
503 | if $loop; |
|
|
504 | |
|
|
505 | last; |
|
|
506 | } |
|
|
507 | } |
|
|
508 | |
|
|
509 | # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit |
|
|
510 | @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; |
|
|
511 | |
397 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
512 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
398 | or die "$DATADIR/archetypes~: $!"; |
513 | or die "$DATADIR/archetypes~: $!"; |
399 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
514 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
400 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
401 | } |
515 | } |
402 | |
516 | |
403 | { |
517 | { |
404 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
518 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
405 | or die "$DATADIR/treasures~: $!"; |
519 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
414 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
528 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
415 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
529 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
416 | |
530 | |
417 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
531 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
418 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
532 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
|
|
533 | |
|
|
534 | if (my $magicmap = $v->{magicmap}) { |
|
|
535 | $magicmap =~ y/A-Z_\-/a-z/d; |
|
|
536 | $v->{magicmap} = $COLOR{$magicmap}; |
|
|
537 | } |
419 | } |
538 | } |
420 | |
539 | |
421 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
540 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
422 | or die "$DATADIR/facedata~: $!"; |
541 | or die "$DATADIR/facedata~: $!"; |
423 | |
542 | |
424 | print $fh Storable::nfreeze { |
543 | print $fh freeze { |
425 | version => 2, |
544 | version => 2, |
426 | faceinfo => \%FACEINFO, |
545 | faceinfo => \%FACEINFO, |
427 | animinfo => \%ANIMINFO, |
546 | animinfo => \%ANIMINFO, |
|
|
547 | resource => \%RESOURCE, |
428 | }; |
548 | }; |
429 | } |
549 | } |
430 | |
550 | |
431 | for (qw(archetypes facedata treasures)) { |
551 | for (qw(archetypes facedata treasures)) { |
432 | chmod 0644, "$DATADIR/$_~"; |
552 | chmod 0644, "$DATADIR/$_~"; |