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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines