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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines