… | |
… | |
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 | } |
… | |
… | |
82 | |
99 | |
83 | print "maps installed successfully.\n"; |
100 | print "maps installed successfully.\n"; |
84 | } |
101 | } |
85 | |
102 | |
86 | { |
103 | { |
|
|
104 | our %ANIMINFO; |
87 | our %FACEINFO; |
105 | our %FACEINFO; |
88 | our @ARC; |
106 | our @ARC; |
|
|
107 | our %ARC; |
89 | our $TRS; |
108 | our $TRS; |
90 | our $NFILE; |
109 | our $NFILE; |
91 | our %ANIM; |
|
|
92 | |
110 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
111 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
112 | |
95 | our (@png, @trs, @arc); # files we are interested in |
113 | our (@png, @trs, @arc); # files we are interested in |
96 | |
114 | |
… | |
… | |
153 | fork_sub { |
171 | fork_sub { |
154 | system "convert png:\Q$path\E -depth 8 rgba:-" |
172 | system "convert png:\Q$path\E -depth 8 rgba:-" |
155 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
173 | . "| $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~" |
174 | . "| 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 $? ($!)"; |
175 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
158 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
176 | system $OPTIPNG, "-i0", "-q", "$other~"; |
159 | die "$other~ has zero size, aborting." unless -s "$other~"; |
177 | die "$other~ has zero size, aborting." unless -s "$other~"; |
160 | rename "$other~", $other; |
178 | rename "$other~", $other; |
161 | }; |
179 | }; |
162 | } |
180 | } |
163 | |
181 | |
… | |
… | |
169 | my $other = "$stem.32x32.png~"; |
187 | my $other = "$stem.32x32.png~"; |
170 | |
188 | |
171 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
189 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
172 | fork_sub { |
190 | fork_sub { |
173 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
191 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
174 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
192 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
193 | |
|
|
194 | # reduce smoothfaces >10000 bytes |
|
|
195 | if ($stem =~ /_S\./ && (-s "$other~") > 10000) { |
|
|
196 | my $ncolor = 256; |
|
|
197 | while () { |
|
|
198 | system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; |
|
|
199 | system $OPTIPNG, "-i0", "-q", "$other~~"; |
|
|
200 | last if 10000 > -s "$other~~"; |
|
|
201 | $ncolor = int $ncolor * 0.9; |
|
|
202 | $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
|
|
203 | } |
|
|
204 | |
|
|
205 | printf "reduced %s from %d to %d bytes using %d colours.\n", |
|
|
206 | $other, -s "$other~", -s "$other~~", $ncolor |
|
|
207 | if $VERBOSE >= 2; |
|
|
208 | rename "$other~~", "$other~"; |
|
|
209 | } |
|
|
210 | |
175 | die "$other~ has zero size, aborting." unless -s "$other~"; |
211 | die "$other~ has zero size, aborting." unless -s "$other~"; |
176 | rename "$other~", $other; |
212 | rename "$other~", $other; |
177 | }; |
213 | }; |
178 | } |
214 | } |
179 | |
215 | |
… | |
… | |
182 | } |
218 | } |
183 | } |
219 | } |
184 | |
220 | |
185 | (my $face = $stem) =~ s/^.*\///; |
221 | (my $face = $stem) =~ s/^.*\///; |
186 | |
222 | |
|
|
223 | # split all bigfaces, but avoid smoothfaces (*_S) |
187 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
224 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
188 | # split |
225 | # split |
189 | my @tile; |
226 | my @tile; |
190 | for my $x (0 .. (int $w / $T) - 1) { |
227 | for my $x (0 .. (int $w / $T) - 1) { |
191 | for my $y (0 .. (int $h / $T) - 1) { |
228 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
255 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
292 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
256 | |
293 | |
257 | my $arc = read_arch "$dir/$file"; |
294 | my $arc = read_arch "$dir/$file"; |
258 | for my $o (values %$arc) { |
295 | for my $o (values %$arc) { |
259 | push @ARC, $o; |
296 | push @ARC, $o; |
|
|
297 | for (my $m = $o; $m; $m = $m->{more}) { |
|
|
298 | $ARC{$m->{_name}} = $m; |
|
|
299 | } |
260 | |
300 | |
261 | $o->{editor_folder} = $dir; |
301 | $o->{editor_folder} = $dir; |
262 | |
302 | |
263 | my $visibility = delete $o->{visibility}; |
303 | my $visibility = delete $o->{visibility}; |
264 | my $magicmap = delete $o->{magicmap}; |
304 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
275 | my $x = $o->{x} - $dx; |
315 | my $x = $o->{x} - $dx; |
276 | my $y = $o->{y} - $dy; |
316 | my $y = $o->{y} - $dy; |
277 | |
317 | |
278 | my $ext = $x|$y ? "+$x+$y" : ""; |
318 | my $ext = $x|$y ? "+$x+$y" : ""; |
279 | |
319 | |
280 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
320 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
281 | |
321 | |
282 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
322 | $visibility = delete $o->{visibility} if exists $o->{visibility}; |
283 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
323 | $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
284 | |
324 | |
285 | my $anim = delete $o->{anim}; |
325 | my $anim = delete $o->{anim}; |
286 | |
326 | |
287 | if ($anim) { |
327 | if ($anim) { |
|
|
328 | # possibly add $ext to the animation name to avoid |
|
|
329 | # the need to specify archnames for all parts |
|
|
330 | # of a multipart archetype. |
288 | $o->{animation} = "$o->{_name}"; |
331 | $o->{animation} = "$o->{_name}"; |
|
|
332 | my $facings = 1; |
|
|
333 | my @frames; |
289 | |
334 | |
290 | for (@$anim) { |
335 | for (@$anim) { |
291 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
336 | if (/^facings\s+(\d+)/) { |
|
|
337 | $facings = $1*1; |
|
|
338 | } elsif (/^blank.x11$|^empty.x11$/) { |
|
|
339 | push @frames, $_; |
|
|
340 | } else { |
|
|
341 | push @frames, "$_$ext"; |
|
|
342 | } |
292 | } |
343 | } |
293 | |
344 | |
294 | $ANIM{"$o->{_name}$ext"} = |
345 | $ANIMINFO{$o->{animation}} = { |
295 | join "", map "$_\n", |
346 | facings => $facings, |
296 | "anim $o->{_name}", |
347 | frames => \@frames, |
297 | @$anim, |
348 | }; |
298 | "mina"; |
|
|
299 | } |
349 | } |
300 | |
350 | |
301 | for my $face ($o->{face} || (), @{$anim || []}) { |
351 | for my $face ($o->{face} || (), @{$anim || []}) { |
302 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
352 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
303 | |
353 | |
… | |
… | |
306 | $info->{visibility} = $visibility if defined $visibility; |
356 | $info->{visibility} = $visibility if defined $visibility; |
307 | $info->{magicmap} = $magicmap if defined $magicmap; |
357 | $info->{magicmap} = $magicmap if defined $magicmap; |
308 | } |
358 | } |
309 | |
359 | |
310 | if (my $smooth = delete $o->{smoothface}) { |
360 | if (my $smooth = delete $o->{smoothface}) { |
311 | my ($face, $smooth) = split /\s+/, $smooth; |
361 | my %kv =split /\s+/, $smooth; |
312 | # skip empty_S.x11, it seems to server no purpose whatsoever |
362 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
313 | # but increases bandwidth demands and worse. |
363 | while (my ($face, $smooth) = each %kv) { |
314 | unless ($smooth eq "empty_S.x11") { |
|
|
315 | $FACEINFO{$face}{smooth} = $smooth; |
364 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
365 | $FACEINFO{$face}{smoothlevel} = $level; |
316 | } |
366 | } |
317 | } |
367 | } |
318 | } |
368 | } |
319 | } |
369 | } |
320 | } |
370 | } |
… | |
… | |
352 | } elsif ($file =~ /\.trs$/) { |
402 | } elsif ($file =~ /\.trs$/) { |
353 | push @trs, [$path, $file]; |
403 | push @trs, [$path, $file]; |
354 | } elsif ($file =~ /\.arc$/) { |
404 | } elsif ($file =~ /\.arc$/) { |
355 | push @arc, [$path, $file]; |
405 | push @arc, [$path, $file]; |
356 | } else { |
406 | } else { |
357 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
407 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
358 | } |
408 | } |
359 | } |
409 | } |
360 | }; |
410 | }; |
361 | } |
411 | } |
362 | |
412 | |
363 | sub inst_arch($) { |
413 | sub inst_arch($) { |
364 | my (undef, $path) = @_; |
414 | my (undef, $path) = @_; |
365 | |
415 | |
|
|
416 | print "\n", |
366 | print "installing '$path' to '$DATADIR'\n", |
417 | "Installing '$path' to '$DATADIR'\n", |
|
|
418 | "\n", |
367 | "(this can take a long time if you run this\n", |
419 | "This can take a long time if you run this\n", |
368 | "for the first time or do not use --cache).\n"; |
420 | "for the first time or do not use --cache.\n", |
|
|
421 | "\n", |
|
|
422 | "Unless you run verbosely, all following warning\n", |
|
|
423 | "or error messages indicate serious problems.\n", |
|
|
424 | "\n"; |
369 | |
425 | |
370 | if (!-d "$path/treasures") { |
426 | if (!-d "$path/treasures") { |
371 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
427 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
372 | exit 1 unless $FORCE; |
428 | exit 1 unless $FORCE; |
373 | } |
429 | } |
… | |
… | |
381 | (async \&process_trs), (async \&process_trs), |
437 | (async \&process_trs), (async \&process_trs), |
382 | (async \&process_arc), (async \&process_arc), |
438 | (async \&process_arc), (async \&process_arc), |
383 | ); |
439 | ); |
384 | |
440 | |
385 | { |
441 | { |
386 | open my $fh, ">:utf8", "$DATADIR/animations~" |
442 | # remove path prefix from editor_folder |
387 | or die "$DATADIR/animations~: $!"; |
443 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
388 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
444 | for values %ARC; |
|
|
445 | |
|
|
446 | # resolve inherit |
|
|
447 | while () { |
|
|
448 | my $progress; |
|
|
449 | my $loop; |
|
|
450 | |
|
|
451 | for my $o (values %ARC) { |
|
|
452 | if (my $other = $o->{inherit}) { |
|
|
453 | if (my $s = $ARC{$other}) { |
|
|
454 | if ($s->{inherit}) { |
|
|
455 | $loop = $s; |
|
|
456 | } else { |
|
|
457 | delete $o->{inherit}; |
|
|
458 | my %s = %$s; |
|
|
459 | delete @s{qw(_name more name name_pl)}; |
|
|
460 | %$o = ( %s, %$o ); |
|
|
461 | ++$progress; |
|
|
462 | } |
|
|
463 | } else { |
|
|
464 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
465 | delete $ARC{$o->{_name}}; |
|
|
466 | } |
|
|
467 | } |
|
|
468 | } |
|
|
469 | |
|
|
470 | unless ($progress) { |
|
|
471 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
472 | if $loop; |
|
|
473 | |
|
|
474 | last; |
|
|
475 | } |
389 | } |
476 | } |
390 | |
477 | |
391 | { |
478 | # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit |
|
|
479 | @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; |
|
|
480 | |
392 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
481 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
393 | or die "$DATADIR/archetypes~: $!"; |
482 | or die "$DATADIR/archetypes~: $!"; |
394 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
483 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
395 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
396 | } |
484 | } |
397 | |
485 | |
398 | { |
486 | { |
399 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
487 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
400 | or die "$DATADIR/treasures~: $!"; |
488 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
409 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
497 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
410 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
498 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
411 | |
499 | |
412 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
500 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
413 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
501 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
|
|
502 | |
|
|
503 | if (my $magicmap = $v->{magicmap}) { |
|
|
504 | $magicmap =~ y/A-Z_\-/a-z/d; |
|
|
505 | $v->{magicmap} = $COLOR{$magicmap}; |
|
|
506 | } |
414 | } |
507 | } |
415 | |
508 | |
416 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
509 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
417 | or die "$DATADIR/facedata~: $!"; |
510 | or die "$DATADIR/facedata~: $!"; |
418 | |
511 | |
419 | $FACEINFO{""} = { version => 1}; |
512 | print $fh freeze { |
420 | print $fh Storable::nfreeze \%FACEINFO; |
513 | version => 2, |
|
|
514 | faceinfo => \%FACEINFO, |
|
|
515 | animinfo => \%ANIMINFO, |
421 | } |
516 | }; |
|
|
517 | } |
422 | |
518 | |
423 | for (qw(archetypes facedata animations treasures)) { |
519 | for (qw(archetypes facedata treasures)) { |
424 | chmod 0644, "$DATADIR/$_~"; |
520 | chmod 0644, "$DATADIR/$_~"; |
425 | rename "$DATADIR/$_~", "$DATADIR/$_" |
521 | rename "$DATADIR/$_~", "$DATADIR/$_" |
426 | or die "$DATADIR/$_: $!"; |
522 | or die "$DATADIR/$_: $!"; |
427 | } |
523 | } |
428 | |
524 | |