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.79 by root, Sun Nov 15 18:04:00 2009 UTC vs.
Revision 1.115 by root, Sat May 14 17:41:57 2011 UTC

1#!@PERL@ 1#!@PERL@
2 2
3# 3#
4# This file is part of Deliantra, the Roguelike Realtime MMORPG. 4# This file is part of Deliantra, the Roguelike Realtime MMORPG.
5# 5#
6# Copyright (©) 2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 6# Copyright (©) 2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
7# 7#
8# Deliantra is free software: you can redistribute it and/or modify it under 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 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 10# Free Software Foundation, either version 3 of the License, or (at your
11# option) any later version. 11# option) any later version.
20# <http://www.gnu.org/licenses/>. 20# <http://www.gnu.org/licenses/>.
21# 21#
22# The authors can be reached via e-mail to <support@deliantra.net> 22# The authors can be reached via e-mail to <support@deliantra.net>
23# 23#
24 24
25use strict; 25use common::sense;
26 26
27my $prefix = "@prefix@"; 27my $prefix = "@prefix@";
28my $exec_prefix = "@exec_prefix@"; 28my $exec_prefix = "@exec_prefix@";
29my $datarootdir = "@datarootdir@"; 29my $datarootdir = "@datarootdir@";
30my $DATADIR = "@datadir@/@PACKAGE@"; 30my $DATADIR = "@datadir@/@PACKAGE@";
31 31
32my $CONVERT = "@CONVERT@"; 32my $CONVERT = "@CONVERT@";
33#my $IDENTIFY = "@IDENTIFY@"; 33my $IDENTIFY = "@IDENTIFY@";
34my $OPTIPNG = "@OPTIPNG@"; 34my $OPTIPNG = "@OPTIPNG@";
35my $RSYNC = "@RSYNC@"; 35my $RSYNC = "@RSYNC@";
36my $PNGNQ = "@PNGNQ@"; 36my $PNGNQ = "@PNGNQ@";
37 37
38use Getopt::Long; 38use Getopt::Long;
39use File::Temp; 39use File::Temp;
40use POSIX (); 40use POSIX ();
41use Carp; 41use Carp;
42 42
43use Coro::EV;
44use AnyEvent;
45use YAML::XS (); 43use YAML::XS ();
44use Digest::MD5 ();
45use Storable ();
46use JSON::XS (); 46use JSON::XS ();
47use IO::AIO (); 47use IO::AIO ();
48use Digest::MD5 (); 48use Compress::LZF ();
49 49
50use AnyEvent;
50use Coro 5.12; 51use Coro 5.12;
52use Coro::EV;
51use Coro::AIO; 53use Coro::AIO;
52use Coro::Util; 54use Coro::Util;
53use Coro::Channel; 55use Coro::Channel;
56use Coro::AnyEvent;
54use Coro::Storable; $Storable::canonical = 1; 57use Coro::Storable; $Storable::canonical = 1;
55 58
56use Deliantra; 59use Deliantra;
57 60
58$SIG{QUIT} = sub { Carp::cluck "QUIT" }; 61$SIG{QUIT} = sub { Carp::cluck "QUIT" };
74my $VERBOSE = 1; 77my $VERBOSE = 1;
75my $CACHE = 0; 78my $CACHE = 0;
76my $FORCE; 79my $FORCE;
77my $TMPDIR = "/tmp/cfutil$$~"; 80my $TMPDIR = "/tmp/cfutil$$~";
78my $TMPFILE = "aaaa0"; 81my $TMPFILE = "aaaa0";
82my @COMMIT;
79 83
80our %COLOR = ( 84our %COLOR = (
85 # original cf colours
81 black => 0, 86 black => 0,
82 white => 1, 87 white => 1,
83 navy => 2, 88 navy => 2, # "dark blue"
84 red => 3, 89 red => 3,
85 orange => 4, 90 orange => 4,
86 blue => 5, 91 blue => 5, # "light blue"
87 darkorange => 6, 92 darkorange => 6,
88 green => 7, 93 green => 7,
89 lightgreen => 8, 94 lightgreen => 8,
90 grey => 9, 95 grey => 9,
96 brown => 10, # "yellow"
97 gold => 11, # "light yellow"
98 tan => 12, # yellowish gray
99
100 # new for deliantra
91 brown => 10, 101 none => 13,
102
103 # compatibility to existing archetypes
104 lightblue => 5,
92 gold => 11, 105 gray => 9,
106 yellow => 11,
93 tan => 12, 107 khaki => 12,
94); 108);
95 109
96END { system "rm", "-rf", $TMPDIR } 110END { system "rm", "-rf", $TMPDIR }
97 111
98my $s_INT = EV::signal INT => sub { exit 1 }; 112my $s_INT = EV::signal INT => sub { exit 1 };
102 116
103# here we could try to avoid collisions and reduce chksum size further 117# here we could try to avoid collisions and reduce chksum size further
104sub make_hash($\$\$;$) { 118sub make_hash($\$\$;$) {
105 my ($id, $dataref, $hashref, $clen) = @_; 119 my ($id, $dataref, $hashref, $clen) = @_;
106 120
107 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4; 121 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 5;
108 122
109 if (exists $hash{$hash}) { 123 if (exists $hash{$hash}) {
110 # hahs collinion, but some files are simply identical 124 # hash collision, but some files are simply identical
111 if (${$hash{$hash}[1]} ne $$dataref) { 125 if (${$hash{$hash}[1]} ne $$dataref) {
112 warn "hash collision $hash{$hash}[0] vs. $id\n"; 126 warn "hash collision $hash{$hash}[0] vs. $id (increase the default \$clen in make_hash?)\n";
113 exit 1; 127 exit 1;
114 } else { 128 } else {
115 print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3; 129 print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3;
116 } 130 }
117 } 131 }
118 $hash{$hash} = [$id, $dataref, $hashref]; 132 $hash{$hash} = [$id, $dataref, $hashref];
119 133
120 $$hashref = $hash; 134 $$hashref = $hash;
121} 135}
122 136
137sub optipng($) {
138 system $OPTIPNG, "-o5", "-i0", "-q", $_[0];
139 die "$_[0] has zero size, aborting." unless -s $_[0];
140}
141
123mkdir $TMPDIR, 0700 142mkdir $TMPDIR, 0700
124 or die "$TMPDIR: $!"; 143 or die "$TMPDIR: $!";
125 144
126sub fork_sub(&) { 145sub fork_exec(&) {
127 my ($cb) = @_; 146 my ($cb) = @_;
128 147
129 if (my $pid = fork) { 148 if (my $pid = fork) {
130 my $current = $Coro::current; 149 my $current = $Coro::current;
131 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); 150 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
136 warn $@; 155 warn $@;
137 POSIX::_exit 1; 156 POSIX::_exit 1;
138 } 157 }
139} 158}
140 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
141sub inst_maps($) { 189sub inst_maps($) {
142 my (undef, $path) = @_; 190 my (undef, $path) = @_;
143 191
144 print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; 192 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
145 193
168 our $PATH; 216 our $PATH;
169 217
170 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 218 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
171 219
172 our $c_arc = new Coro::Channel; 220 our $c_arc = new Coro::Channel;
173 our $c_trs = new Coro::Channel; 221 our $c_any = new Coro::Channel;
174 our $c_res = new Coro::Channel;
175 222
176 our @c_png; 223 our @c_png;
177 224
178 sub commit_png($$$) { 225 sub commit_png($$$$) {
179 my ($name, $data, $T) = @_; 226 my ($stem, $name, $data, $T) = @_;
180 227
228 $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH;
181 $FACEINFO{$name}{"data$T"} = $data; 229 $FACEINFO{$name}{"data$T"} = $data;
182 } 230 }
183 231
184 sub process_png { 232 sub process_png {
185 while (@c_png) { 233 while (@c_png) {
237 unless ($path =~ /~$/) { 285 unless ($path =~ /~$/) {
238 # possibly enlarge 286 # possibly enlarge
239 if (0 > aio_stat "$stem.64x64.png") { 287 if (0 > aio_stat "$stem.64x64.png") {
240 my $other = "$stem.64x64.png~"; 288 my $other = "$stem.64x64.png~";
241 289
242 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 290 make_file $path, $other, sub {
243 fork_sub { 291 fork_exec {
244 my $CROP; 292 my $CROP;
245 my $SRC = "png:\Q$path\E"; 293 my $SRC = "png:\Q$path\E";
246 294
247 my $is_floor = $arc->{is_floor}; 295 my $is_floor = $arc->{is_floor};
248 my $is_wall = 0; 296 my $is_wall = 0;
302 350
303 system "convert -depth 8 $SRC rgba:-" 351 system "convert -depth 8 $SRC rgba:-"
304 . "| $exec_prefix/bin/cfhq2xa $w $h 0" 352 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
305 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~" 353 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~"
306 and die "convert/cfhq2xa pipeline error: status $? ($!)"; 354 and die "convert/cfhq2xa pipeline error: status $? ($!)";
307 system $OPTIPNG, "-i0", "-q", "$other~"; 355 optipng "$other~";
308 die "$other~ has zero size, aborting." unless -s "$other~";
309 rename "$other~", $other; 356 rename "$other~", $other;
310 }; 357 };
311 } 358 };
312 359
313 push @c_png, [$other, !$CACHE]; 360 push @c_png, [$other, !$CACHE];
314 } 361 }
315 362
316 # possibly scale down 363 # possibly scale down
317 if (0 > aio_stat "$stem.32x32.png") { 364 if (0 > aio_stat "$stem.32x32.png") {
318 my $other = "$stem.32x32.png~"; 365 my $other = "$stem.32x32.png~";
319 366
320 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 367 make_file $path, $other, sub {
321 fork_sub { 368 fork_exec {
322 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; 369 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
323 system $OPTIPNG, "-i0", "-q", "$other~"; 370 optipng "$other~";
324 371
325 # reduce smoothfaces >10000 bytes 372 # reduce smoothfaces >10000 bytes
326 # obsolete, no longer required 373 # obsolete, no longer required
327 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) { 374 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
328 my $ncolor = 256; 375 my $ncolor = 256;
329 while () { 376 while () {
330 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; 377 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
331 system $OPTIPNG, "-i0", "-q", "$other~~"; 378 optipng "$other~~";
332 last if 10000 > -s "$other~~"; 379 last if 10000 > -s "$other~~";
333 $ncolor = int $ncolor * 0.9; 380 $ncolor = int $ncolor * 0.9;
334 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; 381 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
335 } 382 }
336 383
341 } 388 }
342 389
343 die "$other~ has zero size, aborting." unless -s "$other~"; 390 die "$other~ has zero size, aborting." unless -s "$other~";
344 rename "$other~", $other; 391 rename "$other~", $other;
345 }; 392 };
346 } 393 };
347 394
348 #warn "scaled down $path to $other\n";#d# 395 #warn "scaled down $path to $other\n";#d#
349 push @c_png, [$other, !$CACHE]; 396 push @c_png, [$other, !$CACHE];
350 } 397 }
351 } 398 }
365 } 412 }
366 413
367 my $mtime = (lstat $path)[9]; 414 my $mtime = (lstat $path)[9];
368 my @todo = grep { $_->[3] <= $mtime } @tile; 415 my @todo = grep { $_->[3] <= $mtime } @tile;
369 if (@todo) { 416 if (@todo) {
370 fork_sub { 417 fork_exec {
371 open my $convert, "|-", $CONVERT, 418 open my $convert, "|-", $CONVERT,
372 "png:-", 419 "png:-",
373 (map { 420 (map {
374 ( 421 (
375 "(", 422 "(",
388 print $convert $png; 435 print $convert $png;
389 close $convert; 436 close $convert;
390 437
391 # pass 2, optimise, and rename 438 # pass 2, optimise, and rename
392 for (@todo) { 439 for (@todo) {
393 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 440 optipng "$_->[2]~";
394 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
395 rename "$_->[2]~", $_->[2]; 441 rename "$_->[2]~", $_->[2];
396 } 442 }
397 }; 443 };
398 } 444 }
399 445
403 449
404 if (0 > aio_load $file, $tile) { 450 if (0 > aio_load $file, $tile) {
405 die "$path: unable to read tile +$x+$y, aborting.\n"; 451 die "$path: unable to read tile +$x+$y, aborting.\n";
406 } 452 }
407 IO::AIO::aio_unlink $file unless $CACHE; 453 IO::AIO::aio_unlink $file unless $CACHE;
408 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; 454 commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T;
409 } 455 }
410 } else { 456 } else {
411 # use as-is (either small, use smooth) 457 # use as-is (either small, use smooth)
412 commit_png $face, $png, $T; 458 commit_png $stem, $face, $png, $T;
413 } 459 }
414 460
415 aio_unlink $path if $delete; 461 aio_unlink $path if $delete;
416 } 462 }
417 } 463 }
418 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) . $_;
502 }
503 }
504 }
505
419 sub process_arc { 506 sub process_arc {
420 while (my $job = $c_arc->get) { 507 while (my ($dir, $file) = @{ $c_arc->get }) {
421 my ($dir, $file) = @$job;
422
423 my $arc; 508 my $arc;
424 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 :/
425 510
426 my $arc = read_arch "$dir/$file"; 511 my $arc = read_arch "$dir/$file";
427 for my $o (values %$arc) { 512 for my $o (values %$arc) {
429 for (my $m = $o; $m; $m = $m->{more}) { 514 for (my $m = $o; $m; $m = $m->{more}) {
430 $ARC{$m->{_name}} = $m; 515 $ARC{$m->{_name}} = $m;
431 } 516 }
432 517
433 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge 518 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
434
435 my $visibility = delete $o->{visibility};
436 my $magicmap = delete $o->{magicmap};
437 519
438 # find upper left corner :/ 520 # find upper left corner :/
439 # omg, this is sooo broken 521 # omg, this is sooo broken
440 my ($dx, $dy); 522 my ($dx, $dy);
441 for (my $o = $o; $o; $o = $o->{more}) { 523 for (my $o = $o; $o; $o = $o->{more}) {
448 my $y = $o->{y} - $dy; 530 my $y = $o->{y} - $dy;
449 531
450 my $ext = $x|$y ? "+$x+$y" : ""; 532 my $ext = $x|$y ? "+$x+$y" : "";
451 533
452 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; 534 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
453
454 $visibility = delete $o->{visibility} if exists $o->{visibility};
455 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
456 535
457 my $anim = delete $o->{anim}; 536 my $anim = delete $o->{anim};
458 537
459 if ($anim) { 538 if ($anim) {
460 # possibly add $ext to the animation name to avoid 539 # possibly add $ext to the animation name to avoid
488 567
489 my $info = $FACEINFO{$face} ||= { }; 568 my $info = $FACEINFO{$face} ||= { };
490 $info->{arc} = $o; 569 $info->{arc} = $o;
491 570
492 next if $face =~ /^blank.x11$|^empty.x11$/; 571 next if $face =~ /^blank.x11$|^empty.x11$/;
493
494 $info->{visibility} = $visibility if defined $visibility;
495 $info->{magicmap} = $magicmap if defined $magicmap;
496 } 572 }
497 573
498 if (my $smooth = delete $o->{smoothface}) { 574 if (my $smooth = delete $o->{smoothface}) {
499 my %kv = split /\s+/, $smooth; 575 my %kv = split /\s+/, $smooth;
500 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 576 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
509 } 585 }
510 } 586 }
511 } 587 }
512 588
513 sub process_trs { 589 sub process_trs {
514 while (my $job = $c_trs->get) {
515 my ($dir, $file) = @$job; 590 my ($dir, $file) = @_;
516 my $path = "$dir/$file"; 591 my $path = "$dir/$file";
517 592
518 my $trs; 593 my $trs;
519 if (0 > aio_load $path, $trs) { 594 if (0 > aio_load $path, $trs) {
520 warn "$path: $!, skipping.\n"; 595 warn "$path: $!, skipping.\n";
521 next; 596 return;
522 } 597 }
523 598
524 $TRS .= $trs; 599 $TRS .= $trs;
525 }
526 } 600 }
527 601
528 my %FILECACHE; 602 my %FILECACHE;
529 603
530 sub load_cached($;$) { 604 sub load_cached($;$) {
541 } 615 }
542 616
543 $FILECACHE{$_[0]} 617 $FILECACHE{$_[0]}
544 } 618 }
545 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~";
672 };
673 };
674
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
546 sub process_res { 688 sub process_res {
547 while (my $job = $c_res->get) {
548 my ($dir, $file, $type) = @$job; 689 my ($dir, $file, $type) = @_;
549 690
550 my $data;
551 aio_load "$dir/$file", $data; 691 0 <= aio_load "$dir/$file", my $data
692 or die "$dir/$file: $!";
552 693
553 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; 694 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
554 695
555 utf8::decode $dir; 696 utf8::decode $dir;
556 utf8::decode $file; 697 utf8::decode $file;
557 698
558 # a meta file for resources is now mandatory 699 # a meta file for resources is now mandatory
559 unless (exists $meta->{$file}) { 700 unless (exists $meta->{$file}) {
560 warn "skipping $dir/$file\n" if $VERBOSE >= 3; 701 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
561 next; 702 return;
562 } 703 }
563 704
564 $meta = { 705 $meta = {
565 %{ $meta->{"" } || {} }, 706 %{ $meta->{"" } || {} },
566 %{ $meta->{$file} || {} }, 707 %{ $meta->{$file} || {} },
567 }; 708 };
568 709
569 if ($meta->{license} =~ s/^#//) { 710 if ($meta->{license} =~ s/^#//) {
570 $meta->{license} = ({ 711 $meta->{license} = ({
571 "pd" => "Public Domain", 712 "pd" => "Public Domain",
572 "gpl" => "GNU General Public License, version 3.0 or any later", 713 "gpl" => "GNU General Public License, version 3.0 or any later",
573 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", 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/",
574 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/", 716 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
575 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", 717 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
576 })->{$meta->{license}} 718 })->{$meta->{license}}
577 || warn "$dir/$file: license tag '$meta->{license}' not found."; 719 || warn "$dir/$file: license tag '$meta->{license}' not found.";
578 } 720 }
579 721
722 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
723 ($meta->{author} = $1) =~ s/_/ /g;
724 }
725
580 $file =~ s/\.res$//; 726 $file =~ s/\.res$//;
581 $file =~ s/\.(ogg|wav|jpg|png)$//; 727 $file =~ s/\.(ogg|wav|jpg|png)$//;
582 728
583 substr $dir, 0, 1 + length $PATH, ""; 729 if ($file =~ s/\.plt$//) {
584 730 $data = process_plt "$dir/$file", $data;
585 if (my $filter = $meta->{cfutil_filter}) { 731 } elsif (my $filter = $meta->{cfutil_filter}) {
586 if ($filter eq "yaml2json") { 732 if ($filter eq "yaml2json") {
587 $data = JSON::XS::encode_json YAML::XS::Load $data; 733 $data = JSON::XS::encode_json YAML::XS::Load $data;
588 } elsif ($filter eq "json2json") { 734 } elsif ($filter eq "json2json") {
589 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); 735 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
590 } elsif ($filter eq "perl2json") { 736 } elsif ($filter eq "perl2json") {
591 $data = eval $data; die if $@; 737 $data = eval $data; die if $@;
592 $data = JSON::XS::encode_json $data; 738 $data = JSON::XS::encode_json $data;
593 } else { 739 } else {
594 warn "$dir/$file: unknown filter $filter, skipping\n"; 740 warn "$dir/$file: unknown filter $filter, skipping\n";
595 } 741 }
596 } 742 }
597 743
744 substr $dir, 0, 1 + length $PATH, "";
745
598 $RESOURCE{"$dir/$file"} = { 746 $RESOURCE{"$dir/$file"} = {
599 type => (exists $meta->{type} ? delete $meta->{type} : $type), 747 type => (exists $meta->{type} ? delete $meta->{type} : $type),
600 data => $data, 748 data => $data,
601 %$meta ? (meta => $meta) : (), 749 %$meta ? (meta => $meta) : (),
602 }; 750 };
751 }
752
753 sub process_any {
754 while (my ($func, @args) = @{ $c_any->get }) {
755 $func->(@args);
603 } 756 }
604 } 757 }
605 758
606 sub find_files; 759 sub find_files;
607 sub find_files { 760 sub find_files {
608 my ($path) = @_; 761 my ($path) = @_;
609 762
763 my $grp = IO::AIO::aio_group;
764
765 my $scandir; $scandir = sub {
766 my ($path) = @_;
767
610 IO::AIO::aioreq_pri 4; 768 IO::AIO::aioreq_pri 4;
611 IO::AIO::aio_scandir $path, 4, sub { 769 add $grp IO::AIO::aio_scandir $path, 4, sub {
612 my ($dirs, $nondirs) = @_; 770 my ($dirs, $nondirs) = @_;
613 771
614 find_files "$path/$_" 772 $scandir->("$path/$_")
615 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 773 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
616 774
617 my $dir = $path; 775 my $dir = $path;
618 substr $dir, 0, 1 + length $PATH, ""; 776 substr $dir, 0, 1 + length $PATH, "";
619 777
620 for my $file (@$nondirs) { 778 for my $file (@$nondirs) {
621 if ($dir =~ /^music(?:\/|$)/) { 779 if ($dir =~ /^music(?:\/|$)/) {
622 $c_res->put ([$path, $file, 3]) # FT_MUSIC 780 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
623 if $file =~ /\.(ogg)$/; 781 if $file =~ /\.(ogg)$/;
624 782
625 } elsif ($dir =~ /^sound(?:\/|$)/) { 783 } elsif ($dir =~ /^sound(?:\/|$)/) {
626 $c_res->put ([$path, $file, 5]) # FT_SOUND 784 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
627 if $file =~ /\.(wav|ogg)$/; 785 if $file =~ /\.(wav|ogg)$/;
628 786
629 } elsif ($dir =~ /^res(?:\/|$)/) { 787 } elsif ($dir =~ /^res(?:\/|$)/) {
630 if ($file =~ /\.(jpg|png)$/) { 788 if ($file =~ /\.(jpg|png)$/) {
631 $c_res->put ([$path, $file, 0]) # FT_FACE 789 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
632 } elsif ($file =~ /\.(res)$/) { 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
796 } elsif ($file =~ /\.png$/) {
797 push @c_png, ["$path/$file", 0];
798
799 } elsif ($file =~ /\.faceinfo$/) {
800 $c_any->put ([\&process_faceinfo, $path, $file]);
801
802 } elsif ($file =~ /\.trs$/) {
803 $c_any->put ([\&process_trs, $path, $file]);
804
805 } elsif ($file =~ /\.arc$/) {
633 $c_res->put ([$path, $file, 6]) # FT_RSRC 806 $c_arc->put ([$path, $file]);
807
634 } else { 808 } else {
635 $c_res->put ([$path, $file, undef]); 809 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
636 } 810 }
637
638 } elsif ($file =~ /\.png$/) {
639 push @c_png, ["$path/$file", 0];
640
641 } elsif ($file =~ /\.trs$/) {
642 $c_trs->put ([$path, $file]);
643
644 } elsif ($file =~ /\.arc$/) {
645 $c_arc->put ([$path, $file]);
646
647 } else { 811 }
648 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
649 }
650 } 812 };
651 }; 813 };
814
815 $scandir->($path);
816 aio_wait $grp;
652 } 817 }
653 818
654 sub generate_plurals { 819 sub generate_plurals {
655# use Lingua::EN::Inflect (); 820# use Lingua::EN::Inflect ();
656# Lingua::EN::Inflect::classical; 821# Lingua::EN::Inflect::classical;
694 if (!-d "$path/treasures") { 859 if (!-d "$path/treasures") {
695 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";
696 exit 1 unless $FORCE; 861 exit 1 unless $FORCE;
697 } 862 }
698 863
699 print "scanning files...\n" if $VERBOSE; 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;
700 868
701 find_files $path; 869 find_files $path;
702 870
703 my @a_arc = map +(async \&process_arc), 1..2;
704 my @a_res = map +(async \&process_res), 1..2;
705 my @a_trs = map +(async \&process_trs), 1..2;
706
707 IO::AIO::flush;
708
709 $c_res->shutdown;
710 $c_arc->shutdown; 871 $c_arc->shutdown;
711 $c_trs->shutdown; 872 $c_any->shutdown;
712
713 print "start file scan, arc, res processing...\n" if $VERBOSE;
714 873
715 $_->join for @a_arc; # need to parse all archetypes before png processing 874 $_->join for @a_arc; # need to parse all archetypes before png processing
875 print "ended arc processing...\n" if $VERBOSE;
716 876
717 print "end arc, start png processing...\n" if $VERBOSE; 877 print "starting png processing...\n" if $VERBOSE;
718 878
719 # eight png crunchers work fine for my 4x smp machine 879 # eight png crunchers work fine for my 4x smp machine
720 my @a_png = map +(async \&process_png), 1..8; 880 my @a_png = map +(async \&process_png), 1..8;
721 881
722 $_->join for (@a_trs, @a_res, @a_png); 882 $_->join for @a_any;
883 print "ended any processing...\n" if $VERBOSE;
884
885 $_->join for @a_png;
886 print "ended png processing...\n" if $VERBOSE;
723 887
724 print "scanning done, processing results...\n" if $VERBOSE; 888 print "scanning done, processing results...\n" if $VERBOSE;
725 { 889 {
726 # remove path prefix from editor_folder 890 # remove path prefix from editor_folder
727 $_->{editor_folder} =~ /^\x00/ 891 $_->{editor_folder} =~ /^\x00/
733 while () { 897 while () {
734 my $progress; 898 my $progress;
735 my $loop; 899 my $loop;
736 900
737 for my $o (values %ARC) { 901 for my $o (values %ARC) {
738 if (my $other = $o->{inherit}) { 902 for my $other (split /,/, $o->{inherit}) {
739 if (my $s = $ARC{$other}) { 903 if (my $s = $ARC{$other}) {
740 if ($s->{inherit}) { 904 if ($s->{inherit}) {
741 $loop = $s; 905 $loop = $s;
742 } else { 906 } else {
743 delete $o->{inherit}; 907 delete $o->{inherit};
745 delete @s{qw(_name more name name_pl)}; 909 delete @s{qw(_name more name name_pl)};
746 %$o = ( %s, %$o ); 910 %$o = ( %s, %$o );
747 ++$progress; 911 ++$progress;
748 } 912 }
749 } else { 913 } else {
750 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; 914 warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
751 delete $ARC{$o->{_name}}; 915 delete $ARC{$o->{_name}};
752 } 916 }
753 } 917 }
754 } 918 }
755 919
762 } 926 }
763 927
764 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit 928 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
765 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 929 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
766 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
767 print "generating plurals...\n" if $VERBOSE; 942 #print "generating plurals...\n" if $VERBOSE;
768 generate_plurals; 943 #generate_plurals;
769 944
770 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; 945 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
771 open my $fh, ">:utf8", "$DATADIR/archetypes~" 946 open my $fh, ">:utf8", "$DATADIR/archetypes~"
772 or die "$DATADIR/archetypes~: $!"; 947 or die "$DATADIR/archetypes~: $!";
773 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 948 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
781 } 956 }
782 957
783 { 958 {
784 print "processing facedata...\n" if $VERBOSE; 959 print "processing facedata...\n" if $VERBOSE;
785 while (my ($k, $v) = each %FACEINFO) { 960 while (my ($k, $v) = each %FACEINFO) {
786 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";
787 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; 962 length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n";
788 963
789 make_hash $k, $v->{data32}, $v->{hash32}; 964 make_hash $k, $v->{data32}, $v->{hash32};
790 make_hash $k, $v->{data64}, $v->{hash64}; 965 make_hash $k, $v->{data64}, $v->{hash64};
791 966
792 #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; 967 #length $v->{data32} <= 10000 or warn "WARNING: face '$k' has face32 larger than 10000 bytes, will not work with crossfire client.\n";
793 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; 968 #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n";
794 969
795 if (my $magicmap = $v->{magicmap}) { 970 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
796 $magicmap =~ y/A-Z_\-/a-z/d; 971 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
797 $v->{magicmap} = $COLOR{$magicmap}; 972 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
798 }
799 973
800 delete $v->{arc}; 974 delete @$v{qw(arc stem)}; # not used by the server
801 } 975 }
802 976
803 print "processing resources...\n" if $VERBOSE; 977 print "processing resources...\n" if $VERBOSE;
804 my $enc = JSON::XS->new->utf8->canonical->relaxed; 978 my $enc = JSON::XS->new->utf8->canonical->relaxed;
805 while (my ($k, $v) = each %RESOURCE) { 979 while (my ($k, $v) = each %RESOURCE) {
806 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 {
807 if ($v->{type} & 1) { 993 if ($v->{type} & 1) {
808 # prepend meta info 994 # prepend meta info
809 995
810 my $meta = $enc->encode ({ 996 my $meta = $enc->encode ({
811 name => $k, 997 name => $k,
812 %{ $v->{meta} || {} }, 998 %{ $v->{meta} || {} },
813 }); 999 });
814 1000
815 $v->{data} = pack "(w/a*)*", $meta, $v->{data}; 1001 $v->{data} = pack "(w/a*)*", $meta, $v->{data};
816 } 1002 }
817 1003
818 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients 1004 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
1005 }
819 } 1006 }
820 1007
821 printf "writing facedata (%d faces, %d anims, %d resources)...\n", 1008 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
822 scalar keys %FACEINFO, 1009 scalar keys %FACEINFO,
823 scalar keys %ANIMINFO, 1010 scalar keys %ANIMINFO,
825 if $VERBOSE; 1012 if $VERBOSE;
826 1013
827 open my $fh, ">:perlio", "$DATADIR/facedata~" 1014 open my $fh, ">:perlio", "$DATADIR/facedata~"
828 or die "$DATADIR/facedata~: $!"; 1015 or die "$DATADIR/facedata~: $!";
829 1016
830 print $fh freeze { 1017 print $fh nfreeze {
831 version => 2, 1018 version => 2,
832 faceinfo => \%FACEINFO, 1019 faceinfo => \%FACEINFO,
833 animinfo => \%ANIMINFO, 1020 animinfo => \%ANIMINFO,
834 resource => \%RESOURCE, 1021 resource => \%RESOURCE,
835 }; 1022 };
836
837 } 1023 }
838 1024
839 print "committing files...\n" if $VERBOSE; 1025 print "committing files...\n" if $VERBOSE;
840 1026
841 for (qw(archetypes facedata treasures)) { 1027 for (qw(archetypes facedata treasures), @COMMIT) {
842 chmod 0644, "$DATADIR/$_~"; 1028 chmod 0644, "$DATADIR/$_~";
843 rename "$DATADIR/$_~", "$DATADIR/$_" 1029 rename "$DATADIR/$_~", "$DATADIR/$_"
844 or die "$DATADIR/$_: $!"; 1030 or die "$DATADIR/$_: $!";
845 } 1031 }
846 1032

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines