… | |
… | |
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; |
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 | |
… | |
… | |
147 | # possibly enlarge |
147 | # possibly enlarge |
148 | if (0 > aio_stat "$stem.64x64.png") { |
148 | if (0 > aio_stat "$stem.64x64.png") { |
149 | my $other = "$stem.64x64.png~"; |
149 | my $other = "$stem.64x64.png~"; |
150 | |
150 | |
151 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
151 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
152 | warn "rescale $other\n";#d# |
|
|
153 | my $wrap = 0; # for the time being |
152 | my $wrap = 0; # for the time being |
154 | fork_sub { |
153 | fork_sub { |
155 | system "convert png:\Q$path\E -depth 8 rgba:-" |
154 | system "convert png:\Q$path\E -depth 8 rgba:-" |
156 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
155 | . "| $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~" |
156 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
… | |
… | |
176 | die "$other~ has zero size, aborting." unless -s "$other~"; |
175 | die "$other~ has zero size, aborting." unless -s "$other~"; |
177 | rename "$other~", $other; |
176 | rename "$other~", $other; |
178 | }; |
177 | }; |
179 | } |
178 | } |
180 | |
179 | |
181 | warn "scaled down $path to $other\n";#d# |
180 | #warn "scaled down $path to $other\n";#d# |
182 | |
|
|
183 | push @png, [$other, !$CACHE]; |
181 | push @png, [$other, !$CACHE]; |
184 | } |
182 | } |
185 | } |
183 | } |
186 | |
184 | |
187 | (my $face = $stem) =~ s/^.*\///; |
185 | (my $face = $stem) =~ s/^.*\///; |
188 | |
186 | |
|
|
187 | # split all bigfaces, but avoid smoothfaces (*_S) |
189 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
188 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
190 | # split |
189 | # split |
191 | my @tile; |
190 | my @tile; |
192 | for my $x (0 .. (int $w / $T) - 1) { |
191 | for my $x (0 .. (int $w / $T) - 1) { |
193 | for my $y (0 .. (int $h / $T) - 1) { |
192 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
256 | my $arc; |
255 | my $arc; |
257 | 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 :/ |
258 | |
257 | |
259 | my $arc = read_arch "$dir/$file"; |
258 | my $arc = read_arch "$dir/$file"; |
260 | for my $o (values %$arc) { |
259 | for my $o (values %$arc) { |
261 | push @ARC, $o; |
260 | $ARC{$o->{_name}} = $o; |
262 | |
261 | |
263 | $o->{editor_folder} = $dir; |
262 | $o->{editor_folder} = $dir; |
264 | |
263 | |
265 | my $visibility = delete $o->{visibility}; |
264 | my $visibility = delete $o->{visibility}; |
266 | my $magicmap = delete $o->{magicmap}; |
265 | my $magicmap = delete $o->{magicmap}; |
… | |
… | |
277 | my $x = $o->{x} - $dx; |
276 | my $x = $o->{x} - $dx; |
278 | my $y = $o->{y} - $dy; |
277 | my $y = $o->{y} - $dy; |
279 | |
278 | |
280 | my $ext = $x|$y ? "+$x+$y" : ""; |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
281 | |
280 | |
282 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
283 | |
282 | |
284 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
285 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
286 | |
285 | |
287 | my $anim = delete $o->{anim}; |
286 | my $anim = delete $o->{anim}; |
288 | |
287 | |
289 | 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. |
290 | $o->{animation} = "$o->{_name}"; |
292 | $o->{animation} = "$o->{_name}"; |
|
|
293 | my $facings = 1; |
|
|
294 | my @frames; |
291 | |
295 | |
292 | for (@$anim) { |
296 | for (@$anim) { |
293 | $_ .= $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 | } |
294 | } |
304 | } |
295 | |
305 | |
296 | $ANIM{"$o->{_name}$ext"} = |
306 | $ANIMINFO{$o->{animation}} = { |
297 | join "", map "$_\n", |
307 | facings => $facings, |
298 | "anim $o->{_name}", |
308 | frames => \@frames, |
299 | @$anim, |
309 | }; |
300 | "mina"; |
|
|
301 | } |
310 | } |
302 | |
311 | |
303 | for my $face ($o->{face} || (), @{$anim || []}) { |
312 | for my $face ($o->{face} || (), @{$anim || []}) { |
304 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
313 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
305 | |
314 | |
… | |
… | |
308 | $info->{visibility} = $visibility if defined $visibility; |
317 | $info->{visibility} = $visibility if defined $visibility; |
309 | $info->{magicmap} = $magicmap if defined $magicmap; |
318 | $info->{magicmap} = $magicmap if defined $magicmap; |
310 | } |
319 | } |
311 | |
320 | |
312 | if (my $smooth = delete $o->{smoothface}) { |
321 | if (my $smooth = delete $o->{smoothface}) { |
313 | my ($face, $smooth) = split /\s+/, $smooth; |
322 | my %kv =split /\s+/, $smooth; |
314 | # skip empty_S.x11, it seems to server no purpose whatsoever |
323 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
315 | # but increases bandwidth demands and worse. |
324 | while (my ($face, $smooth) = each %kv) { |
316 | unless ($smooth eq "empty_S.x11") { |
|
|
317 | $FACEINFO{$face}{smooth} = $smooth; |
325 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
326 | $FACEINFO{$face}{smoothlevel} = $level; |
318 | } |
327 | } |
319 | } |
328 | } |
320 | } |
329 | } |
321 | } |
330 | } |
322 | } |
331 | } |
… | |
… | |
363 | } |
372 | } |
364 | |
373 | |
365 | sub inst_arch($) { |
374 | sub inst_arch($) { |
366 | my (undef, $path) = @_; |
375 | my (undef, $path) = @_; |
367 | |
376 | |
|
|
377 | print "\n", |
368 | print "installing '$path' to '$DATADIR'\n", |
378 | "Installing '$path' to '$DATADIR'\n", |
|
|
379 | "\n", |
369 | "(this can take a long time if you run this\n", |
380 | "This can take a long time if you run this\n", |
370 | "for the first time or do not use --cache).\n"; |
381 | "for the first time or do not use --cache.\n", |
|
|
382 | "\n", |
|
|
383 | "Unless you run verbosely, all following warning\n", |
|
|
384 | "or error messages indicate serious problems.\n", |
|
|
385 | "\n"; |
371 | |
386 | |
372 | if (!-d "$path/treasures") { |
387 | if (!-d "$path/treasures") { |
373 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
388 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
374 | exit 1 unless $FORCE; |
389 | exit 1 unless $FORCE; |
375 | } |
390 | } |
… | |
… | |
383 | (async \&process_trs), (async \&process_trs), |
398 | (async \&process_trs), (async \&process_trs), |
384 | (async \&process_arc), (async \&process_arc), |
399 | (async \&process_arc), (async \&process_arc), |
385 | ); |
400 | ); |
386 | |
401 | |
387 | { |
402 | { |
388 | open my $fh, ">:utf8", "$DATADIR/animations~" |
403 | # remove path prefix from editor_folder |
389 | or die "$DATADIR/animations~: $!"; |
404 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
390 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
405 | for values %ARC; |
|
|
406 | |
|
|
407 | # resolve inherit |
|
|
408 | while () { |
|
|
409 | my $progress; |
|
|
410 | my $loop; |
|
|
411 | |
|
|
412 | for my $o (values %ARC) { |
|
|
413 | if (my $other = $o->{inherit}) { |
|
|
414 | if (my $s = $ARC{$other}) { |
|
|
415 | if ($s->{inherit}) { |
|
|
416 | $loop = $s; |
|
|
417 | } else { |
|
|
418 | delete $o->{inherit}; |
|
|
419 | %$o = ( %$s, %$o ); |
|
|
420 | ++$progress; |
|
|
421 | } |
|
|
422 | } else { |
|
|
423 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
424 | delete $ARC{$o->{_name}}; |
|
|
425 | } |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | unless ($progress) { |
|
|
430 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
431 | if $loop; |
|
|
432 | |
|
|
433 | last; |
|
|
434 | } |
391 | } |
435 | } |
392 | |
436 | |
393 | { |
|
|
394 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
437 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
395 | or die "$DATADIR/archetypes~: $!"; |
438 | or die "$DATADIR/archetypes~: $!"; |
396 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
439 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } values %ARC]; |
397 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
398 | } |
440 | } |
399 | |
441 | |
400 | { |
442 | { |
401 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
443 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
402 | or die "$DATADIR/treasures~: $!"; |
444 | or die "$DATADIR/treasures~: $!"; |
… | |
… | |
406 | { |
448 | { |
407 | while (my ($k, $v) = each %FACEINFO) { |
449 | while (my ($k, $v) = each %FACEINFO) { |
408 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
450 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
409 | length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
451 | length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
410 | |
452 | |
|
|
453 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
|
|
454 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
|
|
455 | |
411 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
456 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
412 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
457 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
413 | } |
458 | } |
414 | |
459 | |
415 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
460 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
416 | or die "$DATADIR/facedata~: $!"; |
461 | or die "$DATADIR/facedata~: $!"; |
417 | |
462 | |
418 | $FACEINFO{""} = { version => 1}; |
463 | print $fh freeze { |
419 | print $fh Storable::nfreeze \%FACEINFO; |
464 | version => 2, |
|
|
465 | faceinfo => \%FACEINFO, |
|
|
466 | animinfo => \%ANIMINFO, |
420 | } |
467 | }; |
|
|
468 | } |
421 | |
469 | |
422 | for (qw(archetypes facedata animations treasures)) { |
470 | for (qw(archetypes facedata treasures)) { |
423 | chmod 0644, "$DATADIR/$_~"; |
471 | chmod 0644, "$DATADIR/$_~"; |
424 | rename "$DATADIR/$_~", "$DATADIR/$_" |
472 | rename "$DATADIR/$_~", "$DATADIR/$_" |
425 | or die "$DATADIR/$_: $!"; |
473 | or die "$DATADIR/$_: $!"; |
426 | } |
474 | } |
427 | |
475 | |