ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
(Generate patch)

Comparing deliantra/server/utils/cfutil.in (file contents):
Revision 1.12 by root, Mon Mar 12 17:26:41 2007 UTC vs.
Revision 1.115 by root, Sat May 14 17:41:57 2011 UTC

1#!@PERL@ 1#!@PERL@
2 2
3use strict; 3#
4# This file is part of Deliantra, the Roguelike Realtime MMORPG.
5#
6# Copyright (©) 2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
7#
8# Deliantra is free software: you can redistribute it and/or modify it under
9# the terms of the Affero GNU General Public License as published by the
10# Free Software Foundation, either version 3 of the License, or (at your
11# option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the Affero GNU General Public License
19# and the GNU General Public License along with this program. If not, see
20# <http://www.gnu.org/licenses/>.
21#
22# The authors can be reached via e-mail to <support@deliantra.net>
23#
24
25use common::sense;
4 26
5my $prefix = "@prefix@"; 27my $prefix = "@prefix@";
6my $exec_prefix = "@exec_prefix@"; 28my $exec_prefix = "@exec_prefix@";
7my $datarootdir = "@datarootdir@"; 29my $datarootdir = "@datarootdir@";
8my $DATADIR = "@datadir@/@PACKAGE@"; 30my $DATADIR = "@datadir@/@PACKAGE@";
9 31
10my $CONVERT = "@CONVERT@"; 32my $CONVERT = "@CONVERT@";
11my $IDENTIFY = "@IDENTIFY@"; 33my $IDENTIFY = "@IDENTIFY@";
12my $OPTIPNG = "@OPTIPNG@"; 34my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@"; 35my $RSYNC = "@RSYNC@";
36my $PNGNQ = "@PNGNQ@";
14 37
15use Getopt::Long; 38use Getopt::Long;
16use Coro::Event; 39use File::Temp;
40use POSIX ();
41use Carp;
42
43use YAML::XS ();
44use Digest::MD5 ();
45use Storable ();
46use JSON::XS ();
47use IO::AIO ();
48use Compress::LZF ();
49
17use AnyEvent; 50use AnyEvent;
18use IO::AIO (); 51use Coro 5.12;
19use File::Temp;
20use Crossfire;
21use Coro; 52use Coro::EV;
22use Coro::AIO; 53use Coro::AIO;
23use POSIX (); 54use Coro::Util;
24use Digest::MD5; 55use Coro::Channel;
56use Coro::AnyEvent;
57use Coro::Storable; $Storable::canonical = 1;
58
59use Deliantra;
60
61$SIG{QUIT} = sub { Carp::cluck "QUIT" };
25 62
26sub usage { 63sub usage {
27 warn <<EOF; 64 warn <<EOF;
28Usage: cfutil [-v] [-q] [--force] [--cache] 65Usage: cfutil [-v] [-q] [--force] [--cache]
29 [--install-arch path] 66 [--install-arch path]
40my $VERBOSE = 1; 77my $VERBOSE = 1;
41my $CACHE = 0; 78my $CACHE = 0;
42my $FORCE; 79my $FORCE;
43my $TMPDIR = "/tmp/cfutil$$~"; 80my $TMPDIR = "/tmp/cfutil$$~";
44my $TMPFILE = "aaaa0"; 81my $TMPFILE = "aaaa0";
82my @COMMIT;
83
84our %COLOR = (
85 # original cf colours
86 black => 0,
87 white => 1,
88 navy => 2, # "dark blue"
89 red => 3,
90 orange => 4,
91 blue => 5, # "light blue"
92 darkorange => 6,
93 green => 7,
94 lightgreen => 8,
95 grey => 9,
96 brown => 10, # "yellow"
97 gold => 11, # "light yellow"
98 tan => 12, # yellowish gray
99
100 # new for deliantra
101 none => 13,
102
103 # compatibility to existing archetypes
104 lightblue => 5,
105 gray => 9,
106 yellow => 11,
107 khaki => 12,
108);
45 109
46END { system "rm", "-rf", $TMPDIR } 110END { system "rm", "-rf", $TMPDIR }
47 111
48Event->signal (signal => "INT", cb => sub { exit 1 }); 112my $s_INT = EV::signal INT => sub { exit 1 };
49Event->signal (signal => "TERM", cb => sub { exit 1 }); 113my $s_TERM = EV::signal TERM => sub { exit 1 };
114
115our %hash;
116
117# here we could try to avoid collisions and reduce chksum size further
118sub make_hash($\$\$;$) {
119 my ($id, $dataref, $hashref, $clen) = @_;
120
121 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 5;
122
123 if (exists $hash{$hash}) {
124 # hash collision, but some files are simply identical
125 if (${$hash{$hash}[1]} ne $$dataref) {
126 warn "hash collision $hash{$hash}[0] vs. $id (increase the default \$clen in make_hash?)\n";
127 exit 1;
128 } else {
129 print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3;
130 }
131 }
132 $hash{$hash} = [$id, $dataref, $hashref];
133
134 $$hashref = $hash;
135}
136
137sub optipng($) {
138 system $OPTIPNG, "-o5", "-i0", "-q", $_[0];
139 die "$_[0] has zero size, aborting." unless -s $_[0];
140}
50 141
51mkdir $TMPDIR, 0700 142mkdir $TMPDIR, 0700
52 or die "$TMPDIR: $!"; 143 or die "$TMPDIR: $!";
53 144
54sub fork_sub(&) { 145sub fork_exec(&) {
55 my ($cb) = @_; 146 my ($cb) = @_;
56 147
57 if (my $pid = fork) { 148 if (my $pid = fork) {
58 my $current = $Coro::current; 149 my $current = $Coro::current;
59 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); 150 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
64 warn $@; 155 warn $@;
65 POSIX::_exit 1; 156 POSIX::_exit 1;
66 } 157 }
67} 158}
68 159
160sub imgsize($) {
161 open my $fh, "-|", $IDENTIFY, qw(-ping -format %w,%h --), $_[0]
162 or die "$IDENTIFY: $!";
163
164 Coro::AnyEvent::readable $fh;
165
166 my ($w, $h) = split /,/, <$fh>;
167
168 ($w+0, $h+0)
169}
170
171# make $dst from $src, if not uptodate, by calling $how
172sub make_file($$$) {
173 my ($src, $dst, $how) = @_;
174
175 my $t = (aio_stat $dst) ? -1 : (stat _)[9];
176
177 for (ref $src ? @$src : $src) {
178 aio_stat $_
179 and die "$_: $!";
180
181 if ((stat _)[9] > $t) {
182 # outdated, redo
183 $how->();
184 last;
185 }
186 }
187}
188
69sub inst_maps($) { 189sub inst_maps($) {
70 my (undef, $path) = @_; 190 my (undef, $path) = @_;
71 191
72 print "installing '$path' to '$DATADIR/maps'\n\n"; 192 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
73 193
74 if (!-f "$path/regions") { 194 if (!-f "$path/regions") {
75 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 195 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
76 exit 1 unless $FORCE; 196 exit 1 unless $FORCE;
77 } 197 }
78 198
79 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; 199 system $RSYNC, "-av", "--chmod=u=rwX,go=rX",
200 "$path/.", "$DATADIR/maps/.",
201 "--exclude", "CVS", "--exclude", "/world-precomposed",
202 "--delete", "--delete-excluded"
203 and die "map installation failed.\n";
204
205 print "maps installed successfully.\n";
80} 206}
81 207
82{ 208{
83 our %PNG32; 209 our %ANIMINFO;
84 our %FACEINFO; 210 our %FACEINFO;
211 our %RESOURCE;
85 our @ARC; 212 our @ARC;
213 our %ARC;
86 our $TRS; 214 our $TRS;
87 our $NFILE; 215 our $NFILE;
88 our %ANIM;
89 our $SMOOTH; 216 our $PATH;
90 217
91 our (@png, @trs, @arc); # files we are interested in 218 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
92 219
220 our $c_arc = new Coro::Channel;
221 our $c_any = new Coro::Channel;
222
223 our @c_png;
224
93 sub commit_png { 225 sub commit_png($$$$) {
94 my ($name, $data) = @_; 226 my ($stem, $name, $data, $T) = @_;
95 227
96 $PNG32{$name} = $data; 228 $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH;
97 $FACEINFO{$name} ||= {}; 229 $FACEINFO{$name}{"data$T"} = $data;
98 } 230 }
99 231
100 sub process_png { 232 sub process_png {
101 while (@png) { 233 while (@c_png) {
102 my $path = pop @png; 234 my ($path, $delete) = @{pop @c_png};
103 235
104 my $png; 236 my $png;
105 aio_lstat $path; 237 aio_lstat $path;
106 my ($size, $mtime) = (stat _)[7,9]; 238 my ($size, $mtime) = (stat _)[7,9];
107 239
108 if (0 > aio_load $path, $png) { 240 if (0 > aio_load $path, $png) {
109 warn "$path: $!, skipping.\n"; 241 warn "$path: $!, skipping.\n";
110 next; 242 next;
111 } 243 }
112 244
245 my $stem = $path;
246 my $T;
247
248 if ($stem =~ s/\.32x32\.png~?$//) {
249 $T = 32;
250 } elsif ($stem =~ s/\.64x64\.png~?$//) {
251 $T = 64;
252 } else {
253 warn "$path: weird filename, skipping.\n";
254 next;
255 }
256
113 # quickly extract width and height of the (necessarily PNG) image 257 # quickly extract width and height of the (necessarily PNG) image
114 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { 258 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
115 warn "$path: not a recongized png file, skipping.\n"; 259 warn "$path: not a recognized png file, skipping.\n";
116 next; 260 next;
117 } 261 }
118 262
119 my ($w, $h) = unpack "NN", $1; 263 my ($w, $h) = unpack "NN", $1;
120
121 (my $face = $path) =~ s/^.*\///;
122 my $T = 32;
123
124 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
125 warn "$path: weird filename, skipping.\n";
126 next;
127 }
128 264
129 if ($w < $T || $h < $T) { 265 if ($w < $T || $h < $T) {
130 warn "$path: too small ($w $h), skipping.\n"; 266 warn "$path: too small ($w $h), skipping.\n";
131 next; 267 next;
132 } 268 }
134 if ($w % $T || $h % $T) { 270 if ($w % $T || $h % $T) {
135 warn "$path: weird png size ($w $h), skipping.\n"; 271 warn "$path: weird png size ($w $h), skipping.\n";
136 next; 272 next;
137 } 273 }
138 274
275 (my $base = $stem) =~ s/^.*\///;
276
277 my $fi = $FACEINFO{$base};
278 unless ($fi) {
279 #warn "$path: <$base> not referenced by any archetype, skipping.\n";
280 #next;
281 }
282
283 my $arc = $fi->{arc} || { };
284
285 unless ($path =~ /~$/) {
286 # possibly enlarge
287 if (0 > aio_stat "$stem.64x64.png") {
288 my $other = "$stem.64x64.png~";
289
290 make_file $path, $other, sub {
291 fork_exec {
292 my $CROP;
293 my $SRC = "png:\Q$path\E";
294
295 my $is_floor = $arc->{is_floor};
296 my $is_wall = 0;
297
298 my ($wall_pfx, $wall_dir, $wall_sfx);
299
300 if (
301 !$is_floor
302 && !$arc->{alive}
303 && $arc->{move_block} eq "all"
304 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/
305 ) {
306 ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3);
307
308 unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) {
309 $is_wall = 1;
310 }
311 }
312
313 if ($is_wall || $is_floor) {
314 # add a 4px border and add other images around it
315 $CROP = "-shave 8x8 +repage";
316
317 $w += 8;
318 $h += 8;
319
320 $SRC = "-size ${w}x${h} xc:transparent";
321 $SRC .= " png:\Q$path\E -geometry +4+4 -composite";
322
323 # 8 surrounding images
324 for (
325 # x y b r0 r1
326 [-1, -1, 0, 6],
327 [ 0, -1, 1, 10, 14],
328 [+1, -1, 0, 12],
329
330 [-1, 0, 8, 5, 7],
331 #
332 [+1, 0, 2, 5, 13],
333
334 [-1, +1, 0, 3],
335 [ 0, +1, 4, 10, 11],
336 [+1, +1, 0, 9],
337 ) {
338 my ($x, $y, $d, $r0, $r1) = @$_;
339
340 my $tile = $is_floor ? $path
341 : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx
342 : die;
343
344 $SRC .= sprintf " png:%s -geometry %+d%+d -composite",
345 "\Q$tile",
346 $x * ($w - 8) + 4,
347 $y * ($h - 8) + 4;
348 }
349 }
350
351 system "convert -depth 8 $SRC rgba:-"
352 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
353 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~"
354 and die "convert/cfhq2xa pipeline error: status $? ($!)";
355 optipng "$other~";
356 rename "$other~", $other;
357 };
358 };
359
360 push @c_png, [$other, !$CACHE];
361 }
362
363 # possibly scale down
364 if (0 > aio_stat "$stem.32x32.png") {
365 my $other = "$stem.32x32.png~";
366
367 make_file $path, $other, sub {
368 fork_exec {
369 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
370 optipng "$other~";
371
372 # reduce smoothfaces >10000 bytes
373 # obsolete, no longer required
374 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
375 my $ncolor = 256;
376 while () {
377 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
378 optipng "$other~~";
379 last if 10000 > -s "$other~~";
380 $ncolor = int $ncolor * 0.9;
381 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
382 }
383
384 printf "reduced %s from %d to %d bytes using %d colours.\n",
385 $other, -s "$other~", -s "$other~~", $ncolor
386 if $VERBOSE >= 2;
387 rename "$other~~", "$other~";
388 }
389
390 die "$other~ has zero size, aborting." unless -s "$other~";
391 rename "$other~", $other;
392 };
393 };
394
395 #warn "scaled down $path to $other\n";#d#
396 push @c_png, [$other, !$CACHE];
397 }
398 }
399
400 (my $face = $stem) =~ s/^.*\///;
401
402 # split all bigfaces, but avoid smoothfaces (*_S)
139 if (($w > $T || $h > $T) && $face !~ /_S\./) { 403 if (($w > $T || $h > $T) && $face !~ /_S\./) {
140 # split 404 # split
141 my @tile; 405 my @tile;
142 for my $x (0 .. (int $w / $T) - 1) { 406 for my $x (0 .. (int $w / $T) - 1) {
143 for my $y (0 .. (int $h / $T) - 1) { 407 for my $y (0 .. (int $h / $T) - 1) {
148 } 412 }
149 413
150 my $mtime = (lstat $path)[9]; 414 my $mtime = (lstat $path)[9];
151 my @todo = grep { $_->[3] <= $mtime } @tile; 415 my @todo = grep { $_->[3] <= $mtime } @tile;
152 if (@todo) { 416 if (@todo) {
153 fork_sub { 417 fork_exec {
154 open my $convert, "|-", $CONVERT, 418 open my $convert, "|-", $CONVERT,
155 "png:-", 419 "png:-",
156 (map { 420 (map {
157 ( 421 (
158 "(", 422 "(",
159 "+clone", 423 "+clone",
160 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), 424 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
161 "+repage", 425 "+repage",
426 -quality => "00",
162 -write => "png:$_->[2]~", 427 -write => "png32:$_->[2]~",
163 "+delete", 428 "+delete",
164 ")", 429 ")",
165 ) 430 )
166 } @todo), 431 } @todo),
167 "null:"; 432 "null:";
170 print $convert $png; 435 print $convert $png;
171 close $convert; 436 close $convert;
172 437
173 # pass 2, optimise, and rename 438 # pass 2, optimise, and rename
174 for (@todo) { 439 for (@todo) {
175 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 440 optipng "$_->[2]~";
176 rename "$_->[2]~", $_->[2]; 441 rename "$_->[2]~", $_->[2];
177 } 442 }
178 }; 443 };
179 } 444 }
180 445
184 449
185 if (0 > aio_load $file, $tile) { 450 if (0 > aio_load $file, $tile) {
186 die "$path: unable to read tile +$x+$y, aborting.\n"; 451 die "$path: unable to read tile +$x+$y, aborting.\n";
187 } 452 }
188 IO::AIO::aio_unlink $file unless $CACHE; 453 IO::AIO::aio_unlink $file unless $CACHE;
189 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 454 commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T;
190 } 455 }
191 } else { 456 } else {
192 # use as-is (either small, use smooth) 457 # use as-is (either small, use smooth)
193 commit_png $face, $png; 458 commit_png $stem, $face, $png, $T;
459 }
460
461 aio_unlink $path if $delete;
462 }
463 }
464
465 sub process_faceinfo {
466 my ($dir, $file) = @_;
467 my $path = "$dir/$file";
468
469 my $data;
470 if (0 > aio_load $path, $data) {
471 warn "$path: $!, skipping.\n";
472 return;
473 }
474
475 for (split /\n/, $data) {
476 chomp;
477 my ($face, $visibility, $fg, $bg, $glyph) = split /\s+/, $_, 5;
478 # bg not used except for text clients
479
480 utf8::decode $glyph;
481 $glyph =~ s/^\?(?=.)//; # remove "autoglyph" flag
482 $glyph =~ s/^"(.+)"$/$1/; # allow for ""-style quoting
483
484 $fg = "white" if $fg eq "none"; # lots of faces have no fg colour yet
485
486 (my $fgi = $COLOR{$fg})
487 // warn "WARNING: $path: $face specifies unknown foreground colour '$fg'.\n";
488 (my $bgi = $COLOR{$bg})
489 // warn "WARNING: $path: $face specifies unknown background colour '$bg'.\n";
490
491 my $fi = $FACEINFO{$face} ||= { };
492 $fi->{visibility} = $visibility * 1;
493 $fi->{magicmap} = $fgi; # foreground colour becomes magicmap
494
495 $glyph .= " " if 2 > length $glyph; # TODO kanji
496 die "glyph $face too long" if 2 < length $glyph;
497
498 $fi->{glyph} = "";
499 for (split //, $glyph) {
500 utf8::encode $_;
501 $fi->{glyph} .= (chr $fgi) . (chr $bgi) . $_;
194 } 502 }
195 } 503 }
196 } 504 }
197 505
198 sub process_arc { 506 sub process_arc {
199 while (@arc) { 507 while (my ($dir, $file) = @{ $c_arc->get }) {
200 my ($dir, $file) = @{pop @arc};
201
202 my $arc; 508 my $arc;
203 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ 509 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
204 510
205 my $arc = read_arch "$dir/$file"; 511 my $arc = read_arch "$dir/$file";
206 for my $o (values %$arc) { 512 for my $o (values %$arc) {
207 push @ARC, $o; 513 push @ARC, $o;
514 for (my $m = $o; $m; $m = $m->{more}) {
515 $ARC{$m->{_name}} = $m;
516 }
208 517
209 $o->{editor_folder} = $dir; 518 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
210
211 my $visibility = delete $o->{visibility};
212 my $magicmap = delete $o->{magicmap};
213 519
214 # find upper left corner :/ 520 # find upper left corner :/
215 # omg, this is sooo broken 521 # omg, this is sooo broken
216 my ($dx, $dy); 522 my ($dx, $dy);
217 for (my $o = $o; $o; $o = $o->{more}) { 523 for (my $o = $o; $o; $o = $o->{more}) {
223 my $x = $o->{x} - $dx; 529 my $x = $o->{x} - $dx;
224 my $y = $o->{y} - $dy; 530 my $y = $o->{y} - $dy;
225 531
226 my $ext = $x|$y ? "+$x+$y" : ""; 532 my $ext = $x|$y ? "+$x+$y" : "";
227 533
228 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; 534 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
229
230 my $visibility = delete $o->{visibility} if exists $o->{visibility};
231 my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
232 535
233 my $anim = delete $o->{anim}; 536 my $anim = delete $o->{anim};
234 537
235 if ($anim) { 538 if ($anim) {
539 # possibly add $ext to the animation name to avoid
540 # the need to specify archnames for all parts
541 # of a multipart archetype.
236 $o->{animation} = "$o->{_name}"; 542 $o->{animation} = "$o->{_name}";
543 my $facings = 1;
544 my @frames;
237 545
238 for (@$anim) { 546 for (@$anim) {
239 $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; 547 if (/^facings\s+(\d+)/) {
548 $facings = $1*1;
549 } elsif (/^blank.x11$|^empty.x11$/) {
550 push @frames, $_;
551 } else {
552 push @frames, "$_$ext";
553 }
240 } 554 }
241 555
242 $ANIM{"$o->{_name}$ext"} = 556 $ANIMINFO{$o->{animation}} = {
243 join "", map "$_\n", 557 facings => $facings,
244 "anim $o->{_name}", 558 frames => \@frames,
245 @$anim, 559 };
246 "mina";
247 } 560 }
248 561
249 for my $face ($o->{face} || (), @{$anim || []}) { 562 for ($o->{face} || (), @{$anim || []}) {
250 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; 563 next if /^facings\s/;
251 564
565 my $face = $_;
566 $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates
567
252 my $info = $FACEINFO{$face} ||= {}; 568 my $info = $FACEINFO{$face} ||= { };
569 $info->{arc} = $o;
253 570
254 $info->{visibility} = $visibility if defined $visibility; 571 next if $face =~ /^blank.x11$|^empty.x11$/;
255 $info->{magicmap} = $magicmap if defined $magicmap;
256 } 572 }
257 573
258 if (my $smooth = delete $o->{smoothface}) { 574 if (my $smooth = delete $o->{smoothface}) {
259 $SMOOTH .= "$smooth\n"; 575 my %kv = split /\s+/, $smooth;
576 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
577 while (my ($face, $smooth) = each %kv) {
578 $FACEINFO{$smooth}{arc} = $o;
579
580 $FACEINFO{$face}{smooth} = $smooth;
581 $FACEINFO{$face}{smoothlevel} = $level;
582 }
260 } 583 }
261 } 584 }
262 } 585 }
263 } 586 }
264 } 587 }
265 588
266 sub process_trs { 589 sub process_trs {
267 while (@trs) {
268 my ($dir, $file) = @{pop @trs}; 590 my ($dir, $file) = @_;
269 my $path = "$dir/$file"; 591 my $path = "$dir/$file";
270 592
271 my $trs; 593 my $trs;
272 if (0 > aio_load $path, $trs) { 594 if (0 > aio_load $path, $trs) {
273 warn "$path: $!, skipping.\n"; 595 warn "$path: $!, skipping.\n";
596 return;
597 }
598
599 $TRS .= $trs;
600 }
601
602 my %FILECACHE;
603
604 sub load_cached($;$) {
605 unless (exists $FILECACHE{$_[0]}) {
606 my $data;
607 if (0 < aio_load $_[0], $data) {
608 if ($_[1]) {
609 $data = eval { $_[1]->($data) };
610 warn "$_[0]: $@" if $@;
274 next; 611 }
612 }
613
614 $FILECACHE{$_[0]} = $data;
615 }
616
617 $FILECACHE{$_[0]}
618 }
619
620 # convert an image and a palette to some indexed 2d-matrix structure
621 sub process_plt {
622 my ($base, $plt) = @_;
623
624 my ($w, $h) = imgsize "$base.png";
625
626 $w * $h
627 or die "$base.png: unable to identify correct size\n";
628
629 my @plt;
630 my %map;
631
632 for (split /\n/, $plt) {
633 next unless /\S/;
634 next if /^\s*#/;
635
636 /^([0-9a-fA-F]{3,6})\s*(.*?)\s*$/
637 or die "unparseable palette entry for $base.plt: $_";
638
639 my ($rgb, $name) = ($1, $2);
640
641 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
642
643 $map{pack "H*", $rgb} = chr @plt;
644 push @plt, $name;
645 }
646
647 make_file ["$base.plt", "$base.png"], "$base.tbl~", sub {
648 warn "building $base\n" if $VERBOSE >= 3;
649
650 fork_exec {
651 open my $png, "-|", $CONVERT, qw(-depth 8 --), "$base.png", "rgb:"
652 or die "$base.png: $!";
653
654 local $/;
655 $png = <$png>;
656
657 $w * $h * 3 == length $png
658 or die "$base.png: failed to read enough data from file\n";
659
660 $png =~ s/(...)/$map{$1}/ge;
661
662 $w * $h == length $png
663 or die "$base.png: failed to map all data - wrong palette?\n";
664
665 {
666 open my $fh, ">:raw", "$base.tbl~~"
667 or die "$base.tbl~~: $!";
668 syswrite $fh, $png;
669 }
670
671 rename "$base.tbl~~", "$base.tbl~";
275 } 672 };
673 };
276 674
277 $TRS .= $trs; 675 0 <= aio_load "$base.tbl~", my $tbl
676 or die "$base.tbl~: $!";
677
678 IO::AIO::aio_unlink "$base.tbl~" unless $CACHE;
679
680 Compress::LZF::compress nfreeze {
681 w => $w,
682 h => $h,
683 plt => \@plt,
684 tbl => $tbl,
685 }
686 }
687
688 sub process_res {
689 my ($dir, $file, $type) = @_;
690
691 0 <= aio_load "$dir/$file", my $data
692 or die "$dir/$file: $!";
693
694 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
695
696 utf8::decode $dir;
697 utf8::decode $file;
698
699 # a meta file for resources is now mandatory
700 unless (exists $meta->{$file}) {
701 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
702 return;
703 }
704
705 $meta = {
706 %{ $meta->{"" } || {} },
707 %{ $meta->{$file} || {} },
708 };
709
710 if ($meta->{license} =~ s/^#//) {
711 $meta->{license} = ({
712 "pd" => "Public Domain",
713 "gpl" => "GNU General Public License, version 3.0 or any later",
714 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
715 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/",
716 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
717 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
718 })->{$meta->{license}}
719 || warn "$dir/$file: license tag '$meta->{license}' not found.";
720 }
721
722 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
723 ($meta->{author} = $1) =~ s/_/ /g;
724 }
725
726 $file =~ s/\.res$//;
727 $file =~ s/\.(ogg|wav|jpg|png)$//;
728
729 if ($file =~ s/\.plt$//) {
730 $data = process_plt "$dir/$file", $data;
731 } elsif (my $filter = $meta->{cfutil_filter}) {
732 if ($filter eq "yaml2json") {
733 $data = JSON::XS::encode_json YAML::XS::Load $data;
734 } elsif ($filter eq "json2json") {
735 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
736 } elsif ($filter eq "perl2json") {
737 $data = eval $data; die if $@;
738 $data = JSON::XS::encode_json $data;
739 } else {
740 warn "$dir/$file: unknown filter $filter, skipping\n";
741 }
742 }
743
744 substr $dir, 0, 1 + length $PATH, "";
745
746 $RESOURCE{"$dir/$file"} = {
747 type => (exists $meta->{type} ? delete $meta->{type} : $type),
748 data => $data,
749 %$meta ? (meta => $meta) : (),
750 };
751 }
752
753 sub process_any {
754 while (my ($func, @args) = @{ $c_any->get }) {
755 $func->(@args);
278 } 756 }
279 } 757 }
280 758
281 sub find_files; 759 sub find_files;
282 sub find_files { 760 sub find_files {
283 my ($path) = @_; 761 my ($path) = @_;
284 762
763 my $grp = IO::AIO::aio_group;
764
765 my $scandir; $scandir = sub {
766 my ($path) = @_;
767
285 IO::AIO::aioreq_pri 4; 768 IO::AIO::aioreq_pri 4;
286 IO::AIO::aio_scandir $path, 4, sub { 769 add $grp IO::AIO::aio_scandir $path, 4, sub {
287 my ($dirs, $nondirs) = @_; 770 my ($dirs, $nondirs) = @_;
288 771
289 find_files "$path/$_" 772 $scandir->("$path/$_")
290 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 773 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
291 774
775 my $dir = $path;
776 substr $dir, 0, 1 + length $PATH, "";
777
292 for my $file (@$nondirs) { 778 for my $file (@$nondirs) {
779 if ($dir =~ /^music(?:\/|$)/) {
780 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
781 if $file =~ /\.(ogg)$/;
782
783 } elsif ($dir =~ /^sound(?:\/|$)/) {
784 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
785 if $file =~ /\.(wav|ogg)$/;
786
787 } elsif ($dir =~ /^res(?:\/|$)/) {
788 if ($file =~ /\.(jpg|png)$/) {
789 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
790 } elsif ($file =~ /\.(res)$/) {
791 $c_any->put ([\&process_res, $path, $file, 6]) # FT_RSRC
792 } else {
793 $c_any->put ([\&process_res, $path, $file, undef]);
794 }
795
293 if ($file =~ /\.png$/) { 796 } elsif ($file =~ /\.png$/) {
294 push @png, "$path/$file"; 797 push @c_png, ["$path/$file", 0];
798
799 } elsif ($file =~ /\.faceinfo$/) {
800 $c_any->put ([\&process_faceinfo, $path, $file]);
801
295 } elsif ($file =~ /\.trs$/) { 802 } elsif ($file =~ /\.trs$/) {
296 push @trs, [$path, $file]; 803 $c_any->put ([\&process_trs, $path, $file]);
804
297 } elsif ($file =~ /\.arc$/) { 805 } elsif ($file =~ /\.arc$/) {
298 push @arc, [$path, $file]; 806 $c_arc->put ([$path, $file]);
807
299 } else { 808 } else {
300 warn "ignoring $path/$file\n" if $VERBOSE >= 2; 809 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
301 } 810 }
811 }
302 } 812 };
303 }; 813 };
814
815 $scandir->($path);
816 aio_wait $grp;
817 }
818
819 sub generate_plurals {
820# use Lingua::EN::Inflect ();
821# Lingua::EN::Inflect::classical;
822# Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy
823# Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack
824#
825# for my $a (@ARC) {
826# my $name = $a->{name} || $a->{_name};
827#
828# next unless $a->{name_pl};
829# next if $a->{invisible};
830# next if $a->{is_floor};
831# next if $a->{no_pick};
832#
833# my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name;
834# my $pl = $test =~ /^(?:eq|p:.)$/
835# ? $name
836# : Lingua::EN::Inflect::PL $name;
837#
838# if ($pl ne $a->{name_pl}) {
839# warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n";
840# }
841# }
304 } 842 }
305 843
306 sub inst_arch($) { 844 sub inst_arch($) {
307 my (undef, $path) = @_; 845 my (undef, $path) = @_;
308 846
847 $PATH = $path;
848
849 print "\n",
309 print "installing '$path' to '$DATADIR'\n\n"; 850 "Installing '$path' to '$DATADIR'\n",
851 "\n",
852 "This can take a long time if you run this\n",
853 "for the first time or do not use --cache.\n",
854 "\n",
855 "Unless you run verbosely, all following warning\n",
856 "or error messages indicate serious problems.\n",
857 "\n";
310 858
311 if (!-d "$path/treasures") { 859 if (!-d "$path/treasures") {
312 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 860 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
313 exit 1 unless $FORCE; 861 exit 1 unless $FORCE;
314 } 862 }
315 863
864 print "starting file scan, arc, any processing...\n" if $VERBOSE;
865
866 my @a_arc = map +(async \&process_arc), 1..2;
867 my @a_any = map +(async \&process_any), 1..4;
868
316 find_files $path; 869 find_files $path;
317 IO::AIO::flush;
318 870
871 $c_arc->shutdown;
872 $c_any->shutdown;
873
874 $_->join for @a_arc; # need to parse all archetypes before png processing
875 print "ended arc processing...\n" if $VERBOSE;
876
877 print "starting png processing...\n" if $VERBOSE;
878
879 # eight png crunchers work fine for my 4x smp machine
880 my @a_png = map +(async \&process_png), 1..8;
881
319 $_->join for ( 882 $_->join for @a_any;
320 (async \&process_png), (async \&process_png), 883 print "ended any processing...\n" if $VERBOSE;
321 (async \&process_trs), (async \&process_trs),
322 (async \&process_arc), (async \&process_arc),
323 );
324 884
885 $_->join for @a_png;
886 print "ended png processing...\n" if $VERBOSE;
887
888 print "scanning done, processing results...\n" if $VERBOSE;
325 { 889 {
326 open my $fh, ">:utf8", "$DATADIR/animations~" 890 # remove path prefix from editor_folder
327 or die "$DATADIR/animations~: $!"; 891 $_->{editor_folder} =~ /^\x00/
328 print $fh join "", map $ANIM{$_}, sort keys %ANIM 892 and substr $_->{editor_folder}, 0, 2 + length $path, ""
893 for values %ARC;
894
895 print "resolving inheritance tree...\n" if $VERBOSE;
896 # resolve inherit
897 while () {
898 my $progress;
899 my $loop;
900
901 for my $o (values %ARC) {
902 for my $other (split /,/, $o->{inherit}) {
903 if (my $s = $ARC{$other}) {
904 if ($s->{inherit}) {
905 $loop = $s;
906 } else {
907 delete $o->{inherit};
908 my %s = %$s;
909 delete @s{qw(_name more name name_pl)};
910 %$o = ( %s, %$o );
911 ++$progress;
912 }
913 } else {
914 warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
915 delete $ARC{$o->{_name}};
916 }
917 }
918 }
919
920 unless ($progress) {
921 die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
922 if $loop;
923
924 last;
925 }
329 } 926 }
330 927
331 { 928 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
929 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
930
931 # fix up archetypes without names, where the archanme doesn't work at all
932 for (@ARC) {
933 if (!exists $_->{name} and $_->{_name} =~ /_/) {
934 for ($_->{name} = $_->{_name}) {
935 s/(?:_\d+)+$//;
936 s/_[nesw]+$//;
937 y/_/ /;
938 }
939 }
940 }
941
942 #print "generating plurals...\n" if $VERBOSE;
943 #generate_plurals;
944
945 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
332 open my $fh, ">:utf8", "$DATADIR/archetypes~" 946 open my $fh, ">:utf8", "$DATADIR/archetypes~"
333 or die "$DATADIR/archetypes~: $!"; 947 or die "$DATADIR/archetypes~: $!";
334 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; 948 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
335 print $fh Crossfire::archlist_to_string \@ARC;
336 } 949 }
337 950
338 { 951 {
339 open my $fh, ">:utf8", "$DATADIR/smooth~" 952 printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE;
340 or die "$DATADIR/smooth~: $!";
341 print $fh $SMOOTH;
342 }
343
344 {
345 open my $fh, ">:utf8", "$DATADIR/treasures~" 953 open my $fh, ">:utf8", "$DATADIR/treasures~"
346 or die "$DATADIR/treasures~: $!"; 954 or die "$DATADIR/treasures~: $!";
347 print $fh $TRS; 955 print $fh $TRS;
348 } 956 }
349 957
350 { 958 {
959 print "processing facedata...\n" if $VERBOSE;
351 while (my ($k, $v) = each %FACEINFO) { 960 while (my ($k, $v) = each %FACEINFO) {
352 $v->{data32} ||= delete $PNG32{$k};
353 }
354
355 while (my ($k, $v) = each %FACEINFO) {
356 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; 961 length $v->{data32} or warn "WARNING: face '$k' has no png32. this will not work (shoddy gcfclient will crash of course).\n";
962 length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n";
357 963
358 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 964 make_hash $k, $v->{data32}, $v->{hash32};
965 make_hash $k, $v->{data64}, $v->{hash64};
966
967 #length $v->{data32} <= 10000 or warn "WARNING: face '$k' has face32 larger than 10000 bytes, will not work with crossfire client.\n";
968 #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n";
969
970 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
971 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
972 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
973
974 delete @$v{qw(arc stem)}; # not used by the server
975 }
976
977 print "processing resources...\n" if $VERBOSE;
978 my $enc = JSON::XS->new->utf8->canonical->relaxed;
979 while (my ($k, $v) = each %RESOURCE) {
980
981 if ($v->{meta} && $v->{meta}{datadir}) {
982 delete $RESOURCE{$k};
983
984 $k =~ s/^res\/// or die "$k: datadir files must be in res/";
985
986 printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE;
987 open my $fh, ">:raw", "$DATADIR/$k~"
988 or die "$DATADIR/$k~: $!";
989 syswrite $fh, $v->{data};
990 push @COMMIT, $k;
991
992 } else {
993 if ($v->{type} & 1) {
994 # prepend meta info
995
996 my $meta = $enc->encode ({
997 name => $k,
998 %{ $v->{meta} || {} },
999 });
1000
1001 $v->{data} = pack "(w/a*)*", $meta, $v->{data};
1002 }
1003
1004 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
359 } 1005 }
1006 }
360 1007
1008 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
1009 scalar keys %FACEINFO,
1010 scalar keys %ANIMINFO,
1011 scalar keys %RESOURCE
1012 if $VERBOSE;
1013
361 open my $fh, ">:perlio", "$DATADIR/faces~" 1014 open my $fh, ">:perlio", "$DATADIR/facedata~"
362 or die "$DATADIR/faces~: $!"; 1015 or die "$DATADIR/facedata~: $!";
363 1016
364 print $fh Storable::nfreeze \%FACEINFO; 1017 print $fh nfreeze {
1018 version => 2,
1019 faceinfo => \%FACEINFO,
1020 animinfo => \%ANIMINFO,
1021 resource => \%RESOURCE,
365 } 1022 };
1023 }
366 1024
1025 print "committing files...\n" if $VERBOSE;
1026
367 for (qw(archetypes faces animations treasures smooth)) { 1027 for (qw(archetypes facedata treasures), @COMMIT) {
368 chmod 0644, "$DATADIR/$_~"; 1028 chmod 0644, "$DATADIR/$_~";
369 rename "$DATADIR/$_~", "$DATADIR/$_"; 1029 rename "$DATADIR/$_~", "$DATADIR/$_"
1030 or die "$DATADIR/$_: $!";
370 } 1031 }
1032
1033 print "archetype data installed successfully.\n";
371 } 1034 }
372} 1035}
373 1036
374Getopt::Long::Configure ("bundling", "no_ignore_case"); 1037Getopt::Long::Configure ("bundling", "no_ignore_case");
375GetOptions ( 1038GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines