| 1 |
root |
1.1 |
#!@PERL@ |
| 2 |
|
|
|
| 3 |
root |
1.2 |
use strict; |
| 4 |
|
|
|
| 5 |
|
|
my $prefix = "@prefix@"; |
| 6 |
|
|
my $exec_prefix = "@exec_prefix@"; |
| 7 |
|
|
my $datarootdir = "@datarootdir@"; |
| 8 |
|
|
my $DATADIR = "@datadir@/@PACKAGE@"; |
| 9 |
|
|
|
| 10 |
|
|
my $CONVERT = "@CONVERT@"; |
| 11 |
root |
1.36 |
#my $IDENTIFY = "@IDENTIFY@"; |
| 12 |
root |
1.3 |
my $OPTIPNG = "@OPTIPNG@"; |
| 13 |
root |
1.2 |
my $RSYNC = "@RSYNC@"; |
| 14 |
root |
1.31 |
my $PNGNQ = "@PNGNQ@"; |
| 15 |
root |
1.2 |
|
| 16 |
|
|
use Getopt::Long; |
| 17 |
root |
1.73 |
use File::Temp; |
| 18 |
|
|
use POSIX (); |
| 19 |
|
|
use Carp; |
| 20 |
|
|
|
| 21 |
root |
1.69 |
use Coro::EV; |
| 22 |
root |
1.3 |
use AnyEvent; |
| 23 |
root |
1.72 |
use YAML::XS (); |
| 24 |
root |
1.63 |
use JSON::XS (); |
| 25 |
root |
1.3 |
use IO::AIO (); |
| 26 |
root |
1.73 |
|
| 27 |
root |
1.70 |
use Coro 5.12; |
| 28 |
root |
1.3 |
use Coro::AIO; |
| 29 |
root |
1.51 |
use Coro::Util; |
| 30 |
root |
1.53 |
use Coro::Channel; |
| 31 |
root |
1.25 |
use Coro::Storable; $Storable::canonical = 1; |
| 32 |
root |
1.2 |
|
| 33 |
root |
1.73 |
use Deliantra; |
| 34 |
|
|
|
| 35 |
root |
1.42 |
$SIG{QUIT} = sub { Carp::cluck "QUIT" }; |
| 36 |
|
|
|
| 37 |
root |
1.2 |
sub usage { |
| 38 |
|
|
warn <<EOF; |
| 39 |
root |
1.3 |
Usage: cfutil [-v] [-q] [--force] [--cache] |
| 40 |
root |
1.2 |
[--install-arch path] |
| 41 |
|
|
[--install-maps maps] |
| 42 |
|
|
[--print-statedir] |
| 43 |
|
|
[--print-confdir] |
| 44 |
|
|
[--print-datadir] |
| 45 |
|
|
[--print-libdir] |
| 46 |
|
|
[--print-bindir] |
| 47 |
|
|
EOF |
| 48 |
|
|
exit 1; |
| 49 |
|
|
} |
| 50 |
|
|
|
| 51 |
|
|
my $VERBOSE = 1; |
| 52 |
root |
1.3 |
my $CACHE = 0; |
| 53 |
root |
1.2 |
my $FORCE; |
| 54 |
root |
1.3 |
my $TMPDIR = "/tmp/cfutil$$~"; |
| 55 |
|
|
my $TMPFILE = "aaaa0"; |
| 56 |
root |
1.2 |
|
| 57 |
root |
1.35 |
our %COLOR = ( |
| 58 |
|
|
black => 0, |
| 59 |
|
|
white => 1, |
| 60 |
|
|
navy => 2, |
| 61 |
|
|
red => 3, |
| 62 |
|
|
orange => 4, |
| 63 |
|
|
blue => 5, |
| 64 |
|
|
darkorange => 6, |
| 65 |
|
|
green => 7, |
| 66 |
|
|
lightgreen => 8, |
| 67 |
|
|
grey => 9, |
| 68 |
|
|
brown => 10, |
| 69 |
|
|
gold => 11, |
| 70 |
|
|
tan => 12, |
| 71 |
|
|
); |
| 72 |
|
|
|
| 73 |
root |
1.2 |
END { system "rm", "-rf", $TMPDIR } |
| 74 |
|
|
|
| 75 |
root |
1.69 |
my $s_INT = EV::signal INT => sub { exit 1 }; |
| 76 |
|
|
my $s_TERM = EV::signal TERM => sub { exit 1 }; |
| 77 |
root |
1.3 |
|
| 78 |
root |
1.2 |
mkdir $TMPDIR, 0700 |
| 79 |
|
|
or die "$TMPDIR: $!"; |
| 80 |
|
|
|
| 81 |
root |
1.3 |
sub fork_sub(&) { |
| 82 |
|
|
my ($cb) = @_; |
| 83 |
|
|
|
| 84 |
|
|
if (my $pid = fork) { |
| 85 |
|
|
my $current = $Coro::current; |
| 86 |
|
|
my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); |
| 87 |
|
|
Coro::schedule; |
| 88 |
|
|
} else { |
| 89 |
|
|
eval { $cb->() }; |
| 90 |
|
|
POSIX::_exit 0 unless $@; |
| 91 |
|
|
warn $@; |
| 92 |
|
|
POSIX::_exit 1; |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
root |
1.2 |
sub inst_maps($) { |
| 97 |
|
|
my (undef, $path) = @_; |
| 98 |
|
|
|
| 99 |
root |
1.29 |
print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
| 100 |
root |
1.2 |
|
| 101 |
|
|
if (!-f "$path/regions") { |
| 102 |
|
|
warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
| 103 |
|
|
exit 1 unless $FORCE; |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
root |
1.62 |
system $RSYNC, "-av", "--chmod=u=rwX,go=rX", |
| 107 |
|
|
"$path/.", "$DATADIR/maps/.", |
| 108 |
|
|
"--exclude", "CVS", "--exclude", "/world-precomposed", |
| 109 |
|
|
"--delete", "--delete-excluded" |
| 110 |
root |
1.16 |
and die "map installation failed.\n"; |
| 111 |
root |
1.13 |
|
| 112 |
|
|
print "maps installed successfully.\n"; |
| 113 |
root |
1.2 |
} |
| 114 |
|
|
|
| 115 |
|
|
{ |
| 116 |
root |
1.24 |
our %ANIMINFO; |
| 117 |
root |
1.6 |
our %FACEINFO; |
| 118 |
root |
1.37 |
our %RESOURCE; |
| 119 |
root |
1.30 |
our @ARC; |
| 120 |
root |
1.27 |
our %ARC; |
| 121 |
root |
1.12 |
our $TRS; |
| 122 |
root |
1.3 |
our $NFILE; |
| 123 |
root |
1.37 |
our $PATH; |
| 124 |
root |
1.3 |
|
| 125 |
root |
1.19 |
our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
| 126 |
|
|
|
| 127 |
root |
1.53 |
our $c_arc = new Coro::Channel; |
| 128 |
|
|
our $c_trs = new Coro::Channel; |
| 129 |
|
|
our $c_res = new Coro::Channel; |
| 130 |
|
|
|
| 131 |
|
|
our @c_png; |
| 132 |
root |
1.2 |
|
| 133 |
root |
1.19 |
sub commit_png($$$) { |
| 134 |
|
|
my ($name, $data, $T) = @_; |
| 135 |
root |
1.5 |
|
| 136 |
root |
1.19 |
$FACEINFO{$name}{"data$T"} = $data; |
| 137 |
root |
1.2 |
} |
| 138 |
|
|
|
| 139 |
root |
1.3 |
sub process_png { |
| 140 |
root |
1.53 |
while (@c_png) { |
| 141 |
|
|
my ($path, $delete) = @{pop @c_png}; |
| 142 |
root |
1.2 |
|
| 143 |
root |
1.3 |
my $png; |
| 144 |
|
|
aio_lstat $path; |
| 145 |
root |
1.2 |
my ($size, $mtime) = (stat _)[7,9]; |
| 146 |
|
|
|
| 147 |
root |
1.3 |
if (0 > aio_load $path, $png) { |
| 148 |
|
|
warn "$path: $!, skipping.\n"; |
| 149 |
root |
1.5 |
next; |
| 150 |
root |
1.3 |
} |
| 151 |
|
|
|
| 152 |
root |
1.19 |
my $stem = $path; |
| 153 |
|
|
my $T; |
| 154 |
|
|
|
| 155 |
|
|
if ($stem =~ s/\.32x32\.png~?$//) { |
| 156 |
|
|
$T = 32; |
| 157 |
|
|
} elsif ($stem =~ s/\.64x64\.png~?$//) { |
| 158 |
|
|
$T = 64; |
| 159 |
|
|
} else { |
| 160 |
|
|
warn "$path: weird filename, skipping.\n"; |
| 161 |
|
|
next; |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
root |
1.6 |
# quickly extract width and height of the (necessarily PNG) image |
| 165 |
root |
1.3 |
unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
| 166 |
root |
1.19 |
warn "$path: not a recognized png file, skipping.\n"; |
| 167 |
root |
1.5 |
next; |
| 168 |
root |
1.3 |
} |
| 169 |
root |
1.2 |
|
| 170 |
root |
1.3 |
my ($w, $h) = unpack "NN", $1; |
| 171 |
root |
1.2 |
|
| 172 |
root |
1.3 |
if ($w < $T || $h < $T) { |
| 173 |
|
|
warn "$path: too small ($w $h), skipping.\n"; |
| 174 |
root |
1.5 |
next; |
| 175 |
root |
1.3 |
} |
| 176 |
|
|
|
| 177 |
|
|
if ($w % $T || $h % $T) { |
| 178 |
|
|
warn "$path: weird png size ($w $h), skipping.\n"; |
| 179 |
root |
1.5 |
next; |
| 180 |
root |
1.3 |
} |
| 181 |
root |
1.2 |
|
| 182 |
root |
1.54 |
(my $base = $stem) =~ s/^.*\///; |
| 183 |
|
|
|
| 184 |
|
|
my $fi = $FACEINFO{$base}; |
| 185 |
|
|
unless ($fi) { |
| 186 |
root |
1.59 |
#warn "$path: <$base> not referenced by any archetype, skipping.\n"; |
| 187 |
|
|
#next; |
| 188 |
root |
1.54 |
} |
| 189 |
|
|
|
| 190 |
root |
1.59 |
my $arc = $fi->{arc} || { }; |
| 191 |
root |
1.54 |
|
| 192 |
root |
1.19 |
unless ($path =~ /~$/) { |
| 193 |
|
|
# possibly enlarge |
| 194 |
|
|
if (0 > aio_stat "$stem.64x64.png") { |
| 195 |
|
|
my $other = "$stem.64x64.png~"; |
| 196 |
|
|
|
| 197 |
|
|
if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
| 198 |
|
|
fork_sub { |
| 199 |
root |
1.52 |
my $CROP; |
| 200 |
|
|
my $SRC = "png:\Q$path\E"; |
| 201 |
|
|
|
| 202 |
root |
1.54 |
my $is_floor = $arc->{is_floor}; |
| 203 |
|
|
my $is_wall = 0; |
| 204 |
|
|
|
| 205 |
|
|
my ($wall_pfx, $wall_dir, $wall_sfx); |
| 206 |
|
|
|
| 207 |
|
|
if ( |
| 208 |
|
|
!$is_floor |
| 209 |
|
|
&& !$arc->{alive} |
| 210 |
root |
1.55 |
&& $arc->{move_block} eq "all" |
| 211 |
root |
1.54 |
&& $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/ |
| 212 |
|
|
) { |
| 213 |
|
|
($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3); |
| 214 |
|
|
|
| 215 |
|
|
unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) { |
| 216 |
|
|
$is_wall = 1; |
| 217 |
|
|
} |
| 218 |
|
|
} |
| 219 |
|
|
|
| 220 |
|
|
if ($is_wall || $is_floor) { |
| 221 |
|
|
# add a 4px border and add other images around it |
| 222 |
|
|
$CROP = "-shave 8x8 +repage"; |
| 223 |
|
|
|
| 224 |
|
|
$w += 8; |
| 225 |
|
|
$h += 8; |
| 226 |
|
|
|
| 227 |
|
|
$SRC = "-size ${w}x${h} xc:transparent"; |
| 228 |
|
|
$SRC .= " png:\Q$path\E -geometry +4+4 -composite"; |
| 229 |
|
|
|
| 230 |
|
|
# 8 surrounding images |
| 231 |
|
|
for ( |
| 232 |
|
|
# x y b r0 r1 |
| 233 |
|
|
[-1, -1, 0, 6], |
| 234 |
|
|
[ 0, -1, 1, 10, 14], |
| 235 |
|
|
[+1, -1, 0, 12], |
| 236 |
|
|
|
| 237 |
|
|
[-1, 0, 8, 5, 7], |
| 238 |
|
|
# |
| 239 |
|
|
[+1, 0, 2, 5, 13], |
| 240 |
|
|
|
| 241 |
|
|
[-1, +1, 0, 3], |
| 242 |
|
|
[ 0, +1, 4, 10, 11], |
| 243 |
|
|
[+1, +1, 0, 9], |
| 244 |
|
|
) { |
| 245 |
|
|
my ($x, $y, $d, $r0, $r1) = @$_; |
| 246 |
|
|
|
| 247 |
|
|
my $tile = $is_floor ? $path |
| 248 |
|
|
: $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx |
| 249 |
|
|
: die; |
| 250 |
|
|
|
| 251 |
|
|
$SRC .= sprintf " png:%s -geometry %+d%+d -composite", |
| 252 |
|
|
"\Q$tile", |
| 253 |
|
|
$x * ($w - 8) + 4, |
| 254 |
|
|
$y * ($h - 8) + 4; |
| 255 |
root |
1.52 |
} |
| 256 |
|
|
} |
| 257 |
|
|
|
| 258 |
|
|
system "convert -depth 8 $SRC rgba:-" |
| 259 |
root |
1.51 |
. "| $exec_prefix/bin/cfhq2xa $w $h 0" |
| 260 |
root |
1.52 |
. "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~" |
| 261 |
|
|
and die "convert/cfhq2xa pipeline error: status $? ($!)"; |
| 262 |
root |
1.31 |
system $OPTIPNG, "-i0", "-q", "$other~"; |
| 263 |
root |
1.19 |
die "$other~ has zero size, aborting." unless -s "$other~"; |
| 264 |
|
|
rename "$other~", $other; |
| 265 |
|
|
}; |
| 266 |
|
|
} |
| 267 |
|
|
|
| 268 |
root |
1.53 |
push @c_png, [$other, !$CACHE]; |
| 269 |
root |
1.19 |
} |
| 270 |
|
|
|
| 271 |
|
|
# possibly scale down |
| 272 |
|
|
if (0 > aio_stat "$stem.32x32.png") { |
| 273 |
|
|
my $other = "$stem.32x32.png~"; |
| 274 |
|
|
|
| 275 |
|
|
if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
| 276 |
|
|
fork_sub { |
| 277 |
|
|
system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
| 278 |
root |
1.31 |
system $OPTIPNG, "-i0", "-q", "$other~"; |
| 279 |
|
|
|
| 280 |
|
|
# reduce smoothfaces >10000 bytes |
| 281 |
root |
1.71 |
# obsolete, no longer required |
| 282 |
|
|
if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) { |
| 283 |
root |
1.31 |
my $ncolor = 256; |
| 284 |
|
|
while () { |
| 285 |
root |
1.33 |
system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; |
| 286 |
root |
1.31 |
system $OPTIPNG, "-i0", "-q", "$other~~"; |
| 287 |
|
|
last if 10000 > -s "$other~~"; |
| 288 |
|
|
$ncolor = int $ncolor * 0.9; |
| 289 |
|
|
$ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
| 290 |
|
|
} |
| 291 |
|
|
|
| 292 |
|
|
printf "reduced %s from %d to %d bytes using %d colours.\n", |
| 293 |
|
|
$other, -s "$other~", -s "$other~~", $ncolor |
| 294 |
root |
1.32 |
if $VERBOSE >= 2; |
| 295 |
root |
1.31 |
rename "$other~~", "$other~"; |
| 296 |
|
|
} |
| 297 |
|
|
|
| 298 |
root |
1.19 |
die "$other~ has zero size, aborting." unless -s "$other~"; |
| 299 |
|
|
rename "$other~", $other; |
| 300 |
|
|
}; |
| 301 |
|
|
} |
| 302 |
|
|
|
| 303 |
root |
1.21 |
#warn "scaled down $path to $other\n";#d# |
| 304 |
root |
1.53 |
push @c_png, [$other, !$CACHE]; |
| 305 |
root |
1.19 |
} |
| 306 |
|
|
} |
| 307 |
|
|
|
| 308 |
|
|
(my $face = $stem) =~ s/^.*\///; |
| 309 |
|
|
|
| 310 |
root |
1.23 |
# split all bigfaces, but avoid smoothfaces (*_S) |
| 311 |
root |
1.3 |
if (($w > $T || $h > $T) && $face !~ /_S\./) { |
| 312 |
|
|
# split |
| 313 |
|
|
my @tile; |
| 314 |
|
|
for my $x (0 .. (int $w / $T) - 1) { |
| 315 |
|
|
for my $y (0 .. (int $h / $T) - 1) { |
| 316 |
|
|
my $file = "$path+$x+$y~"; |
| 317 |
|
|
aio_lstat $file; |
| 318 |
|
|
push @tile, [$x, $y, $file, (stat _)[9]]; |
| 319 |
|
|
} |
| 320 |
root |
1.2 |
} |
| 321 |
|
|
|
| 322 |
root |
1.3 |
my $mtime = (lstat $path)[9]; |
| 323 |
|
|
my @todo = grep { $_->[3] <= $mtime } @tile; |
| 324 |
|
|
if (@todo) { |
| 325 |
|
|
fork_sub { |
| 326 |
|
|
open my $convert, "|-", $CONVERT, |
| 327 |
|
|
"png:-", |
| 328 |
|
|
(map { |
| 329 |
|
|
( |
| 330 |
|
|
"(", |
| 331 |
|
|
"+clone", |
| 332 |
|
|
-crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
| 333 |
root |
1.7 |
"+repage", |
| 334 |
root |
1.19 |
-quality => "00", |
| 335 |
|
|
-write => "png32:$_->[2]~", |
| 336 |
root |
1.3 |
"+delete", |
| 337 |
|
|
")", |
| 338 |
|
|
) |
| 339 |
|
|
} @todo), |
| 340 |
|
|
"null:"; |
| 341 |
|
|
|
| 342 |
|
|
binmode $convert; |
| 343 |
|
|
print $convert $png; |
| 344 |
|
|
close $convert; |
| 345 |
|
|
|
| 346 |
|
|
# pass 2, optimise, and rename |
| 347 |
|
|
for (@todo) { |
| 348 |
|
|
system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
| 349 |
root |
1.19 |
die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~"; |
| 350 |
root |
1.3 |
rename "$_->[2]~", $_->[2]; |
| 351 |
|
|
} |
| 352 |
|
|
}; |
| 353 |
root |
1.2 |
} |
| 354 |
|
|
|
| 355 |
root |
1.3 |
for (@tile) { |
| 356 |
|
|
my ($x, $y, $file) = @$_; |
| 357 |
|
|
my $tile; |
| 358 |
|
|
|
| 359 |
|
|
if (0 > aio_load $file, $tile) { |
| 360 |
|
|
die "$path: unable to read tile +$x+$y, aborting.\n"; |
| 361 |
|
|
} |
| 362 |
|
|
IO::AIO::aio_unlink $file unless $CACHE; |
| 363 |
root |
1.19 |
commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; |
| 364 |
root |
1.2 |
} |
| 365 |
root |
1.3 |
} else { |
| 366 |
|
|
# use as-is (either small, use smooth) |
| 367 |
root |
1.19 |
commit_png $face, $png, $T; |
| 368 |
root |
1.3 |
} |
| 369 |
root |
1.19 |
|
| 370 |
|
|
aio_unlink $path if $delete; |
| 371 |
root |
1.3 |
} |
| 372 |
root |
1.2 |
} |
| 373 |
|
|
|
| 374 |
root |
1.3 |
sub process_arc { |
| 375 |
root |
1.53 |
while (my $job = $c_arc->get) { |
| 376 |
|
|
my ($dir, $file) = @$job; |
| 377 |
root |
1.2 |
|
| 378 |
root |
1.3 |
my $arc; |
| 379 |
|
|
aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
| 380 |
root |
1.4 |
|
| 381 |
root |
1.2 |
my $arc = read_arch "$dir/$file"; |
| 382 |
root |
1.4 |
for my $o (values %$arc) { |
| 383 |
root |
1.30 |
push @ARC, $o; |
| 384 |
|
|
for (my $m = $o; $m; $m = $m->{more}) { |
| 385 |
|
|
$ARC{$m->{_name}} = $m; |
| 386 |
|
|
} |
| 387 |
root |
1.6 |
|
| 388 |
root |
1.76 |
$o->{editor_folder} ||= "\x00$dir"; # horrible kludge |
| 389 |
root |
1.10 |
|
| 390 |
root |
1.6 |
my $visibility = delete $o->{visibility}; |
| 391 |
|
|
my $magicmap = delete $o->{magicmap}; |
| 392 |
|
|
|
| 393 |
|
|
# find upper left corner :/ |
| 394 |
|
|
# omg, this is sooo broken |
| 395 |
root |
1.4 |
my ($dx, $dy); |
| 396 |
|
|
for (my $o = $o; $o; $o = $o->{more}) { |
| 397 |
|
|
$dx = $o->{x} if $o->{x} < $dx; |
| 398 |
|
|
$dy = $o->{y} if $o->{y} < $dy; |
| 399 |
|
|
} |
| 400 |
root |
1.6 |
|
| 401 |
root |
1.4 |
for (my $o = $o; $o; $o = $o->{more}) { |
| 402 |
|
|
my $x = $o->{x} - $dx; |
| 403 |
|
|
my $y = $o->{y} - $dy; |
| 404 |
root |
1.6 |
|
| 405 |
|
|
my $ext = $x|$y ? "+$x+$y" : ""; |
| 406 |
|
|
|
| 407 |
root |
1.26 |
$o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
| 408 |
root |
1.6 |
|
| 409 |
root |
1.35 |
$visibility = delete $o->{visibility} if exists $o->{visibility}; |
| 410 |
|
|
$magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
| 411 |
root |
1.6 |
|
| 412 |
|
|
my $anim = delete $o->{anim}; |
| 413 |
|
|
|
| 414 |
|
|
if ($anim) { |
| 415 |
root |
1.24 |
# possibly add $ext to the animation name to avoid |
| 416 |
|
|
# the need to specify archnames for all parts |
| 417 |
|
|
# of a multipart archetype. |
| 418 |
root |
1.8 |
$o->{animation} = "$o->{_name}"; |
| 419 |
root |
1.24 |
my $facings = 1; |
| 420 |
|
|
my @frames; |
| 421 |
root |
1.6 |
|
| 422 |
|
|
for (@$anim) { |
| 423 |
root |
1.24 |
if (/^facings\s+(\d+)/) { |
| 424 |
|
|
$facings = $1*1; |
| 425 |
|
|
} elsif (/^blank.x11$|^empty.x11$/) { |
| 426 |
|
|
push @frames, $_; |
| 427 |
|
|
} else { |
| 428 |
|
|
push @frames, "$_$ext"; |
| 429 |
|
|
} |
| 430 |
root |
1.6 |
} |
| 431 |
|
|
|
| 432 |
root |
1.24 |
$ANIMINFO{$o->{animation}} = { |
| 433 |
|
|
facings => $facings, |
| 434 |
|
|
frames => \@frames, |
| 435 |
|
|
}; |
| 436 |
root |
1.6 |
} |
| 437 |
|
|
|
| 438 |
root |
1.58 |
for ($o->{face} || (), @{$anim || []}) { |
| 439 |
|
|
next if /^facings\s/; |
| 440 |
|
|
|
| 441 |
|
|
my $face = $_; |
| 442 |
|
|
$face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates |
| 443 |
root |
1.6 |
|
| 444 |
root |
1.56 |
my $info = $FACEINFO{$face} ||= { }; |
| 445 |
|
|
$info->{arc} = $o; |
| 446 |
|
|
|
| 447 |
|
|
next if $face =~ /^blank.x11$|^empty.x11$/; |
| 448 |
root |
1.6 |
|
| 449 |
|
|
$info->{visibility} = $visibility if defined $visibility; |
| 450 |
|
|
$info->{magicmap} = $magicmap if defined $magicmap; |
| 451 |
root |
1.4 |
} |
| 452 |
root |
1.12 |
|
| 453 |
|
|
if (my $smooth = delete $o->{smoothface}) { |
| 454 |
root |
1.54 |
my %kv = split /\s+/, $smooth; |
| 455 |
root |
1.23 |
my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
| 456 |
|
|
while (my ($face, $smooth) = each %kv) { |
| 457 |
root |
1.54 |
$FACEINFO{$smooth}{arc} = $o; |
| 458 |
|
|
|
| 459 |
root |
1.23 |
$FACEINFO{$face}{smooth} = $smooth; |
| 460 |
|
|
$FACEINFO{$face}{smoothlevel} = $level; |
| 461 |
root |
1.14 |
} |
| 462 |
root |
1.12 |
} |
| 463 |
root |
1.4 |
} |
| 464 |
|
|
} |
| 465 |
root |
1.3 |
} |
| 466 |
root |
1.2 |
} |
| 467 |
|
|
|
| 468 |
root |
1.3 |
sub process_trs { |
| 469 |
root |
1.53 |
while (my $job = $c_trs->get) { |
| 470 |
|
|
my ($dir, $file) = @$job; |
| 471 |
root |
1.12 |
my $path = "$dir/$file"; |
| 472 |
|
|
|
| 473 |
|
|
my $trs; |
| 474 |
|
|
if (0 > aio_load $path, $trs) { |
| 475 |
|
|
warn "$path: $!, skipping.\n"; |
| 476 |
|
|
next; |
| 477 |
|
|
} |
| 478 |
|
|
|
| 479 |
|
|
$TRS .= $trs; |
| 480 |
root |
1.3 |
} |
| 481 |
root |
1.2 |
} |
| 482 |
|
|
|
| 483 |
root |
1.45 |
my %FILECACHE; |
| 484 |
|
|
|
| 485 |
|
|
sub load_cached($;$) { |
| 486 |
|
|
unless (exists $FILECACHE{$_[0]}) { |
| 487 |
|
|
my $data; |
| 488 |
|
|
if (0 < aio_load $_[0], $data) { |
| 489 |
root |
1.49 |
if ($_[1]) { |
| 490 |
|
|
$data = eval { $_[1]->($data) }; |
| 491 |
|
|
warn "$_[0]: $@" if $@; |
| 492 |
|
|
} |
| 493 |
root |
1.45 |
} |
| 494 |
|
|
|
| 495 |
|
|
$FILECACHE{$_[0]} = $data; |
| 496 |
|
|
} |
| 497 |
|
|
|
| 498 |
|
|
$FILECACHE{$_[0]} |
| 499 |
|
|
} |
| 500 |
|
|
|
| 501 |
root |
1.37 |
sub process_res { |
| 502 |
root |
1.53 |
while (my $job = $c_res->get) { |
| 503 |
|
|
my ($dir, $file, $type) = @$job; |
| 504 |
root |
1.37 |
|
| 505 |
|
|
my $data; |
| 506 |
|
|
aio_load "$dir/$file", $data; |
| 507 |
|
|
|
| 508 |
root |
1.60 |
my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; |
| 509 |
root |
1.38 |
|
| 510 |
root |
1.61 |
utf8::decode $dir; |
| 511 |
|
|
utf8::decode $file; |
| 512 |
|
|
|
| 513 |
|
|
# a meta file for resources is now mandatory |
| 514 |
|
|
unless (exists $meta->{$file}) { |
| 515 |
|
|
warn "skipping $dir/$file\n" if $VERBOSE >= 3; |
| 516 |
root |
1.64 |
next; |
| 517 |
root |
1.61 |
} |
| 518 |
root |
1.49 |
|
| 519 |
root |
1.48 |
$meta = { |
| 520 |
|
|
%{ $meta->{"" } || {} }, |
| 521 |
|
|
%{ $meta->{$file} || {} }, |
| 522 |
|
|
}; |
| 523 |
|
|
|
| 524 |
root |
1.49 |
if ($meta->{license} =~ s/^#//) { |
| 525 |
|
|
$meta->{license} = ({ |
| 526 |
|
|
"pd" => "Public Domain", |
| 527 |
|
|
"gpl" => "GNU General Public License, version 3.0 or any later", |
| 528 |
|
|
"cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", |
| 529 |
root |
1.53 |
"cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/", |
| 530 |
root |
1.49 |
"cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", |
| 531 |
|
|
})->{$meta->{license}} |
| 532 |
|
|
|| warn "$dir/$file: license tag '$meta->{license}' not found."; |
| 533 |
|
|
} |
| 534 |
|
|
|
| 535 |
root |
1.37 |
$file =~ s/\.res$//; |
| 536 |
root |
1.46 |
$file =~ s/\.(ogg|wav|jpg|png)$//; |
| 537 |
root |
1.37 |
|
| 538 |
|
|
substr $dir, 0, 1 + length $PATH, ""; |
| 539 |
|
|
|
| 540 |
root |
1.63 |
if (my $filter = $meta->{cfutil_filter}) { |
| 541 |
|
|
if ($filter eq "yaml2json") { |
| 542 |
root |
1.72 |
$data = JSON::XS::encode_json YAML::XS::Load $data; |
| 543 |
root |
1.65 |
} elsif ($filter eq "json2json") { |
| 544 |
|
|
$data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); |
| 545 |
root |
1.66 |
} elsif ($filter eq "perl2json") { |
| 546 |
|
|
$data = eval $data; die if $@; |
| 547 |
|
|
$data = JSON::XS::encode_json $data; |
| 548 |
root |
1.63 |
} else { |
| 549 |
|
|
warn "$dir/$file: unknown filter $filter, skipping\n"; |
| 550 |
|
|
} |
| 551 |
|
|
} |
| 552 |
|
|
|
| 553 |
root |
1.37 |
$RESOURCE{"$dir/$file"} = { |
| 554 |
root |
1.63 |
type => (exists $meta->{type} ? delete $meta->{type} : $type), |
| 555 |
root |
1.45 |
data => $data, |
| 556 |
|
|
%$meta ? (meta => $meta) : (), |
| 557 |
root |
1.37 |
}; |
| 558 |
|
|
} |
| 559 |
|
|
} |
| 560 |
|
|
|
| 561 |
root |
1.3 |
sub find_files; |
| 562 |
|
|
sub find_files { |
| 563 |
root |
1.2 |
my ($path) = @_; |
| 564 |
|
|
|
| 565 |
root |
1.3 |
IO::AIO::aioreq_pri 4; |
| 566 |
|
|
IO::AIO::aio_scandir $path, 4, sub { |
| 567 |
root |
1.2 |
my ($dirs, $nondirs) = @_; |
| 568 |
|
|
|
| 569 |
root |
1.3 |
find_files "$path/$_" |
| 570 |
root |
1.2 |
for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
| 571 |
|
|
|
| 572 |
root |
1.43 |
my $dir = $path; |
| 573 |
|
|
substr $dir, 0, 1 + length $PATH, ""; |
| 574 |
|
|
|
| 575 |
root |
1.2 |
for my $file (@$nondirs) { |
| 576 |
root |
1.44 |
if ($dir =~ /^music(?:\/|$)/) { |
| 577 |
root |
1.53 |
$c_res->put ([$path, $file, 3]) # FT_MUSIC |
| 578 |
root |
1.44 |
if $file =~ /\.(ogg)$/; |
| 579 |
|
|
|
| 580 |
root |
1.47 |
} elsif ($dir =~ /^sound(?:\/|$)/) { |
| 581 |
root |
1.53 |
$c_res->put ([$path, $file, 5]) # FT_SOUND |
| 582 |
root |
1.44 |
if $file =~ /\.(wav|ogg)$/; |
| 583 |
|
|
|
| 584 |
|
|
} elsif ($dir =~ /^res(?:\/|$)/) { |
| 585 |
root |
1.63 |
if ($file =~ /\.(jpg|png)$/) { |
| 586 |
|
|
$c_res->put ([$path, $file, 0]) # FT_FACE |
| 587 |
|
|
} elsif ($file =~ /\.(res)$/) { |
| 588 |
root |
1.67 |
$c_res->put ([$path, $file, 6]) # FT_RSRC |
| 589 |
root |
1.63 |
} else { |
| 590 |
|
|
$c_res->put ([$path, $file, undef]); |
| 591 |
|
|
} |
| 592 |
root |
1.44 |
|
| 593 |
root |
1.43 |
} elsif ($file =~ /\.png$/) { |
| 594 |
root |
1.53 |
push @c_png, ["$path/$file", 0]; |
| 595 |
root |
1.44 |
|
| 596 |
root |
1.2 |
} elsif ($file =~ /\.trs$/) { |
| 597 |
root |
1.53 |
$c_trs->put ([$path, $file]); |
| 598 |
root |
1.44 |
|
| 599 |
root |
1.2 |
} elsif ($file =~ /\.arc$/) { |
| 600 |
root |
1.53 |
$c_arc->put ([$path, $file]); |
| 601 |
root |
1.44 |
|
| 602 |
root |
1.2 |
} else { |
| 603 |
root |
1.33 |
warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
| 604 |
root |
1.2 |
} |
| 605 |
|
|
} |
| 606 |
|
|
}; |
| 607 |
|
|
} |
| 608 |
|
|
|
| 609 |
root |
1.73 |
sub generate_plurals { |
| 610 |
|
|
# use Lingua::EN::Inflect (); |
| 611 |
|
|
# Lingua::EN::Inflect::classical; |
| 612 |
root |
1.74 |
# Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy |
| 613 |
root |
1.73 |
# Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack |
| 614 |
|
|
# |
| 615 |
|
|
# for my $a (@ARC) { |
| 616 |
|
|
# my $name = $a->{name} || $a->{_name}; |
| 617 |
|
|
# |
| 618 |
|
|
# next unless $a->{name_pl}; |
| 619 |
root |
1.74 |
# next if $a->{invisible}; |
| 620 |
|
|
# next if $a->{is_floor}; |
| 621 |
|
|
# next if $a->{no_pick}; |
| 622 |
root |
1.73 |
# |
| 623 |
|
|
# my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name; |
| 624 |
|
|
# my $pl = $test =~ /^(?:eq|p:.)$/ |
| 625 |
|
|
# ? $name |
| 626 |
|
|
# : Lingua::EN::Inflect::PL $name; |
| 627 |
|
|
# |
| 628 |
|
|
# if ($pl ne $a->{name_pl}) { |
| 629 |
|
|
# warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n"; |
| 630 |
|
|
# } |
| 631 |
|
|
# } |
| 632 |
|
|
} |
| 633 |
|
|
|
| 634 |
root |
1.2 |
sub inst_arch($) { |
| 635 |
|
|
my (undef, $path) = @_; |
| 636 |
|
|
|
| 637 |
root |
1.37 |
$PATH = $path; |
| 638 |
|
|
|
| 639 |
root |
1.29 |
print "\n", |
| 640 |
|
|
"Installing '$path' to '$DATADIR'\n", |
| 641 |
root |
1.28 |
"\n", |
| 642 |
root |
1.27 |
"This can take a long time if you run this\n", |
| 643 |
|
|
"for the first time or do not use --cache.\n", |
| 644 |
|
|
"\n", |
| 645 |
|
|
"Unless you run verbosely, all following warning\n", |
| 646 |
|
|
"or error messages indicate serious problems.\n", |
| 647 |
|
|
"\n"; |
| 648 |
root |
1.2 |
|
| 649 |
|
|
if (!-d "$path/treasures") { |
| 650 |
|
|
warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
| 651 |
|
|
exit 1 unless $FORCE; |
| 652 |
|
|
} |
| 653 |
|
|
|
| 654 |
root |
1.53 |
print "scanning files...\n" if $VERBOSE; |
| 655 |
|
|
|
| 656 |
root |
1.3 |
find_files $path; |
| 657 |
root |
1.53 |
|
| 658 |
|
|
my @a_arc = map +(async \&process_arc), 1..2; |
| 659 |
|
|
my @a_res = map +(async \&process_res), 1..2; |
| 660 |
|
|
my @a_trs = map +(async \&process_trs), 1..2; |
| 661 |
|
|
|
| 662 |
root |
1.2 |
IO::AIO::flush; |
| 663 |
|
|
|
| 664 |
root |
1.70 |
$c_res->shutdown; |
| 665 |
|
|
$c_arc->shutdown; |
| 666 |
|
|
$c_trs->shutdown; |
| 667 |
root |
1.53 |
|
| 668 |
|
|
print "start file scan, arc, res processing...\n" if $VERBOSE; |
| 669 |
|
|
|
| 670 |
|
|
$_->join for @a_arc; # need to parse all archetypes before png processing |
| 671 |
root |
1.3 |
|
| 672 |
root |
1.53 |
print "end arc, start png processing...\n" if $VERBOSE; |
| 673 |
|
|
|
| 674 |
root |
1.69 |
# eight png crunchers work fine for my 4x smp machine |
| 675 |
|
|
my @a_png = map +(async \&process_png), 1..8; |
| 676 |
root |
1.53 |
|
| 677 |
|
|
$_->join for (@a_trs, @a_res, @a_png); |
| 678 |
|
|
|
| 679 |
|
|
print "scanning done, processing results...\n" if $VERBOSE; |
| 680 |
root |
1.5 |
{ |
| 681 |
root |
1.27 |
# remove path prefix from editor_folder |
| 682 |
root |
1.76 |
$_->{editor_folder} =~ /^\x00/ |
| 683 |
|
|
and substr $_->{editor_folder}, 0, 2 + length $path, "" |
| 684 |
|
|
for values %ARC; |
| 685 |
root |
1.27 |
|
| 686 |
root |
1.53 |
print "resolving inheritance tree...\n" if $VERBOSE; |
| 687 |
root |
1.27 |
# resolve inherit |
| 688 |
|
|
while () { |
| 689 |
|
|
my $progress; |
| 690 |
|
|
my $loop; |
| 691 |
|
|
|
| 692 |
|
|
for my $o (values %ARC) { |
| 693 |
|
|
if (my $other = $o->{inherit}) { |
| 694 |
|
|
if (my $s = $ARC{$other}) { |
| 695 |
|
|
if ($s->{inherit}) { |
| 696 |
|
|
$loop = $s; |
| 697 |
|
|
} else { |
| 698 |
|
|
delete $o->{inherit}; |
| 699 |
root |
1.30 |
my %s = %$s; |
| 700 |
|
|
delete @s{qw(_name more name name_pl)}; |
| 701 |
|
|
%$o = ( %s, %$o ); |
| 702 |
root |
1.27 |
++$progress; |
| 703 |
|
|
} |
| 704 |
|
|
} else { |
| 705 |
|
|
warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
| 706 |
|
|
delete $ARC{$o->{_name}}; |
| 707 |
|
|
} |
| 708 |
|
|
} |
| 709 |
|
|
} |
| 710 |
|
|
|
| 711 |
|
|
unless ($progress) { |
| 712 |
|
|
die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
| 713 |
|
|
if $loop; |
| 714 |
|
|
|
| 715 |
|
|
last; |
| 716 |
|
|
} |
| 717 |
|
|
} |
| 718 |
|
|
|
| 719 |
root |
1.34 |
# remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit |
| 720 |
|
|
@ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; |
| 721 |
|
|
|
| 722 |
root |
1.73 |
print "generating plurals...\n" if $VERBOSE; |
| 723 |
|
|
generate_plurals; |
| 724 |
|
|
|
| 725 |
root |
1.53 |
print "writing archetypes...\n" if $VERBOSE; |
| 726 |
root |
1.5 |
open my $fh, ">:utf8", "$DATADIR/archetypes~" |
| 727 |
|
|
or die "$DATADIR/archetypes~: $!"; |
| 728 |
root |
1.65 |
print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
| 729 |
root |
1.5 |
} |
| 730 |
|
|
|
| 731 |
root |
1.6 |
{ |
| 732 |
root |
1.53 |
print "writing treasures...\n" if $VERBOSE; |
| 733 |
root |
1.12 |
open my $fh, ">:utf8", "$DATADIR/treasures~" |
| 734 |
|
|
or die "$DATADIR/treasures~: $!"; |
| 735 |
|
|
print $fh $TRS; |
| 736 |
|
|
} |
| 737 |
|
|
|
| 738 |
|
|
{ |
| 739 |
root |
1.53 |
print "processing facedata...\n" if $VERBOSE; |
| 740 |
root |
1.6 |
while (my ($k, $v) = each %FACEINFO) { |
| 741 |
root |
1.12 |
length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
| 742 |
root |
1.19 |
length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
| 743 |
root |
1.5 |
|
| 744 |
root |
1.71 |
#length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
| 745 |
root |
1.20 |
#length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
| 746 |
|
|
|
| 747 |
root |
1.35 |
if (my $magicmap = $v->{magicmap}) { |
| 748 |
|
|
$magicmap =~ y/A-Z_\-/a-z/d; |
| 749 |
|
|
$v->{magicmap} = $COLOR{$magicmap}; |
| 750 |
|
|
} |
| 751 |
root |
1.53 |
|
| 752 |
|
|
delete $v->{arc}; |
| 753 |
root |
1.6 |
} |
| 754 |
|
|
|
| 755 |
root |
1.53 |
print "writing facedata...\n" if $VERBOSE; |
| 756 |
root |
1.19 |
open my $fh, ">:perlio", "$DATADIR/facedata~" |
| 757 |
|
|
or die "$DATADIR/facedata~: $!"; |
| 758 |
root |
1.6 |
|
| 759 |
root |
1.25 |
print $fh freeze { |
| 760 |
root |
1.24 |
version => 2, |
| 761 |
|
|
faceinfo => \%FACEINFO, |
| 762 |
|
|
animinfo => \%ANIMINFO, |
| 763 |
root |
1.37 |
resource => \%RESOURCE, |
| 764 |
root |
1.24 |
}; |
| 765 |
root |
1.5 |
} |
| 766 |
|
|
|
| 767 |
root |
1.53 |
print "committing files...\n" if $VERBOSE; |
| 768 |
|
|
|
| 769 |
root |
1.24 |
for (qw(archetypes facedata treasures)) { |
| 770 |
root |
1.6 |
chmod 0644, "$DATADIR/$_~"; |
| 771 |
root |
1.13 |
rename "$DATADIR/$_~", "$DATADIR/$_" |
| 772 |
|
|
or die "$DATADIR/$_: $!"; |
| 773 |
root |
1.6 |
} |
| 774 |
root |
1.13 |
|
| 775 |
|
|
print "archetype data installed successfully.\n"; |
| 776 |
root |
1.2 |
} |
| 777 |
|
|
} |
| 778 |
|
|
|
| 779 |
|
|
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
| 780 |
|
|
GetOptions ( |
| 781 |
|
|
"verbose|v:+" => \$VERBOSE, |
| 782 |
root |
1.3 |
"cache" => \$CACHE, |
| 783 |
root |
1.2 |
"quiet|q" => sub { $VERBOSE = 0 }, |
| 784 |
|
|
"force" => sub { $FORCE = 1 }, |
| 785 |
|
|
"install-arch=s" => \&inst_arch, |
| 786 |
|
|
"install-maps=s" => \&inst_maps, |
| 787 |
|
|
"print-statedir" => sub { print "@pkgstatedir@\n" }, |
| 788 |
|
|
"print-datadir" => sub { print "$DATADIR\n" }, |
| 789 |
|
|
"print-confdir" => sub { print "@pkgconfdir@\n" }, |
| 790 |
|
|
"print-libdir" => sub { print "@libdir@/@PACKAGE@\n" }, |
| 791 |
|
|
"print-bindir" => sub { print "@bindir@/@PACKAGE@\n" }, |
| 792 |
|
|
) or usage; |
| 793 |
|
|
|