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.82 by root, Sat Apr 3 22:30:21 2010 UTC vs.
Revision 1.111 by root, Fri Apr 22 02:03:12 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 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 };
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 || 4;
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\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;
121} 135}
122 136
123mkdir $TMPDIR, 0700 137mkdir $TMPDIR, 0700
124 or die "$TMPDIR: $!"; 138 or die "$TMPDIR: $!";
125 139
126sub fork_sub(&) { 140sub fork_exec(&) {
127 my ($cb) = @_; 141 my ($cb) = @_;
128 142
129 if (my $pid = fork) { 143 if (my $pid = fork) {
130 my $current = $Coro::current; 144 my $current = $Coro::current;
131 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); 145 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
136 warn $@; 150 warn $@;
137 POSIX::_exit 1; 151 POSIX::_exit 1;
138 } 152 }
139} 153}
140 154
155sub imgsize($) {
156 open my $fh, "-|", $IDENTIFY, qw(-ping -format %w,%h --), $_[0]
157 or die "$IDENTIFY: $!";
158
159 Coro::AnyEvent::readable $fh;
160
161 my ($w, $h) = split /,/, <$fh>;
162
163 ($w+0, $h+0)
164}
165
166# make $dst from $src, if not uptodate, by calling $how
167sub make_file($$$) {
168 my ($src, $dst, $how) = @_;
169
170 my $t = (aio_stat $dst) ? -1 : (stat _)[9];
171
172 for (ref $src ? @$src : $src) {
173 aio_stat $_
174 and die "$_: $!";
175
176 if ((stat _)[9] > $t) {
177 # outdated, redo
178 $how->();
179 last;
180 }
181 }
182}
183
141sub inst_maps($) { 184sub inst_maps($) {
142 my (undef, $path) = @_; 185 my (undef, $path) = @_;
143 186
144 print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; 187 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
145 188
168 our $PATH; 211 our $PATH;
169 212
170 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 213 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
171 214
172 our $c_arc = new Coro::Channel; 215 our $c_arc = new Coro::Channel;
173 our $c_trs = new Coro::Channel; 216 our $c_any = new Coro::Channel;
174 our $c_res = new Coro::Channel;
175 217
176 our @c_png; 218 our @c_png;
177 219
178 sub commit_png($$$) { 220 sub commit_png($$$$) {
179 my ($name, $data, $T) = @_; 221 my ($stem, $name, $data, $T) = @_;
180 222
223 $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH;
181 $FACEINFO{$name}{"data$T"} = $data; 224 $FACEINFO{$name}{"data$T"} = $data;
182 } 225 }
183 226
184 sub process_png { 227 sub process_png {
185 while (@c_png) { 228 while (@c_png) {
237 unless ($path =~ /~$/) { 280 unless ($path =~ /~$/) {
238 # possibly enlarge 281 # possibly enlarge
239 if (0 > aio_stat "$stem.64x64.png") { 282 if (0 > aio_stat "$stem.64x64.png") {
240 my $other = "$stem.64x64.png~"; 283 my $other = "$stem.64x64.png~";
241 284
242 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 285 make_file $path, $other, sub {
243 fork_sub { 286 fork_exec {
244 my $CROP; 287 my $CROP;
245 my $SRC = "png:\Q$path\E"; 288 my $SRC = "png:\Q$path\E";
246 289
247 my $is_floor = $arc->{is_floor}; 290 my $is_floor = $arc->{is_floor};
248 my $is_wall = 0; 291 my $is_wall = 0;
306 and die "convert/cfhq2xa pipeline error: status $? ($!)"; 349 and die "convert/cfhq2xa pipeline error: status $? ($!)";
307 system $OPTIPNG, "-i0", "-q", "$other~"; 350 system $OPTIPNG, "-i0", "-q", "$other~";
308 die "$other~ has zero size, aborting." unless -s "$other~"; 351 die "$other~ has zero size, aborting." unless -s "$other~";
309 rename "$other~", $other; 352 rename "$other~", $other;
310 }; 353 };
311 } 354 };
312 355
313 push @c_png, [$other, !$CACHE]; 356 push @c_png, [$other, !$CACHE];
314 } 357 }
315 358
316 # possibly scale down 359 # possibly scale down
317 if (0 > aio_stat "$stem.32x32.png") { 360 if (0 > aio_stat "$stem.32x32.png") {
318 my $other = "$stem.32x32.png~"; 361 my $other = "$stem.32x32.png~";
319 362
320 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 363 make_file $path, $other, sub {
321 fork_sub { 364 fork_exec {
322 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; 365 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
323 system $OPTIPNG, "-i0", "-q", "$other~"; 366 system $OPTIPNG, "-i0", "-q", "$other~";
324 367
325 # reduce smoothfaces >10000 bytes 368 # reduce smoothfaces >10000 bytes
326 # obsolete, no longer required 369 # obsolete, no longer required
341 } 384 }
342 385
343 die "$other~ has zero size, aborting." unless -s "$other~"; 386 die "$other~ has zero size, aborting." unless -s "$other~";
344 rename "$other~", $other; 387 rename "$other~", $other;
345 }; 388 };
346 } 389 };
347 390
348 #warn "scaled down $path to $other\n";#d# 391 #warn "scaled down $path to $other\n";#d#
349 push @c_png, [$other, !$CACHE]; 392 push @c_png, [$other, !$CACHE];
350 } 393 }
351 } 394 }
365 } 408 }
366 409
367 my $mtime = (lstat $path)[9]; 410 my $mtime = (lstat $path)[9];
368 my @todo = grep { $_->[3] <= $mtime } @tile; 411 my @todo = grep { $_->[3] <= $mtime } @tile;
369 if (@todo) { 412 if (@todo) {
370 fork_sub { 413 fork_exec {
371 open my $convert, "|-", $CONVERT, 414 open my $convert, "|-", $CONVERT,
372 "png:-", 415 "png:-",
373 (map { 416 (map {
374 ( 417 (
375 "(", 418 "(",
403 446
404 if (0 > aio_load $file, $tile) { 447 if (0 > aio_load $file, $tile) {
405 die "$path: unable to read tile +$x+$y, aborting.\n"; 448 die "$path: unable to read tile +$x+$y, aborting.\n";
406 } 449 }
407 IO::AIO::aio_unlink $file unless $CACHE; 450 IO::AIO::aio_unlink $file unless $CACHE;
408 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; 451 commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T;
409 } 452 }
410 } else { 453 } else {
411 # use as-is (either small, use smooth) 454 # use as-is (either small, use smooth)
412 commit_png $face, $png, $T; 455 commit_png $stem, $face, $png, $T;
413 } 456 }
414 457
415 aio_unlink $path if $delete; 458 aio_unlink $path if $delete;
416 } 459 }
417 } 460 }
418 461
462 sub process_faceinfo {
463 my ($dir, $file) = @_;
464 my $path = "$dir/$file";
465
466 my $data;
467 if (0 > aio_load $path, $data) {
468 warn "$path: $!, skipping.\n";
469 return;
470 }
471
472 for (split /\n/, $data) {
473 chomp;
474 my ($face, $visibility, $fg, $bg, $glyph) = split /\s+/, $_, 5;
475 # bg not used except for text clients
476
477 utf8::decode $glyph;
478 $glyph =~ s/^\?(?=.)//; # remove "autoglyph" flag
479 $glyph =~ s/^"(.+)"$/$1/; # allow for ""-style quoting
480
481 $fg = "white" if $fg eq "none"; # lots of faces have no fg colour yet
482
483 (my $fgi = $COLOR{$fg})
484 // warn "WARNING: $path: $face specifies unknown foreground colour '$fg'.\n";
485 (my $bgi = $COLOR{$bg})
486 // warn "WARNING: $path: $face specifies unknown background colour '$bg'.\n";
487
488 my $fi = $FACEINFO{$face} ||= { };
489 $fi->{visibility} = $visibility * 1;
490 $fi->{magicmap} = $fgi; # foreground colour becomes magicmap
491
492 $glyph .= " " if 2 > length $glyph; # TODO kanji
493 die "glyph $face too long" if 2 < length $glyph;
494
495 $fi->{glyph} = "";
496 for (split //, $glyph) {
497 utf8::encode $_;
498 $fi->{glyph} .= (chr $fgi) . (chr $bgi) . $_;
499 }
500 }
501 }
502
419 sub process_arc { 503 sub process_arc {
420 while (my $job = $c_arc->get) { 504 while (my ($dir, $file) = @{ $c_arc->get }) {
421 my ($dir, $file) = @$job;
422
423 my $arc; 505 my $arc;
424 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ 506 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
425 507
426 my $arc = read_arch "$dir/$file"; 508 my $arc = read_arch "$dir/$file";
427 for my $o (values %$arc) { 509 for my $o (values %$arc) {
429 for (my $m = $o; $m; $m = $m->{more}) { 511 for (my $m = $o; $m; $m = $m->{more}) {
430 $ARC{$m->{_name}} = $m; 512 $ARC{$m->{_name}} = $m;
431 } 513 }
432 514
433 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge 515 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
434
435 my $visibility = delete $o->{visibility};
436 my $magicmap = delete $o->{magicmap};
437 516
438 # find upper left corner :/ 517 # find upper left corner :/
439 # omg, this is sooo broken 518 # omg, this is sooo broken
440 my ($dx, $dy); 519 my ($dx, $dy);
441 for (my $o = $o; $o; $o = $o->{more}) { 520 for (my $o = $o; $o; $o = $o->{more}) {
448 my $y = $o->{y} - $dy; 527 my $y = $o->{y} - $dy;
449 528
450 my $ext = $x|$y ? "+$x+$y" : ""; 529 my $ext = $x|$y ? "+$x+$y" : "";
451 530
452 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; 531 $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 532
457 my $anim = delete $o->{anim}; 533 my $anim = delete $o->{anim};
458 534
459 if ($anim) { 535 if ($anim) {
460 # possibly add $ext to the animation name to avoid 536 # possibly add $ext to the animation name to avoid
488 564
489 my $info = $FACEINFO{$face} ||= { }; 565 my $info = $FACEINFO{$face} ||= { };
490 $info->{arc} = $o; 566 $info->{arc} = $o;
491 567
492 next if $face =~ /^blank.x11$|^empty.x11$/; 568 next if $face =~ /^blank.x11$|^empty.x11$/;
493
494 $info->{visibility} = $visibility if defined $visibility;
495 $info->{magicmap} = $magicmap if defined $magicmap;
496 } 569 }
497 570
498 if (my $smooth = delete $o->{smoothface}) { 571 if (my $smooth = delete $o->{smoothface}) {
499 my %kv = split /\s+/, $smooth; 572 my %kv = split /\s+/, $smooth;
500 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 573 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
509 } 582 }
510 } 583 }
511 } 584 }
512 585
513 sub process_trs { 586 sub process_trs {
514 while (my $job = $c_trs->get) {
515 my ($dir, $file) = @$job; 587 my ($dir, $file) = @_;
516 my $path = "$dir/$file"; 588 my $path = "$dir/$file";
517 589
518 my $trs; 590 my $trs;
519 if (0 > aio_load $path, $trs) { 591 if (0 > aio_load $path, $trs) {
520 warn "$path: $!, skipping.\n"; 592 warn "$path: $!, skipping.\n";
521 next; 593 return;
522 } 594 }
523 595
524 $TRS .= $trs; 596 $TRS .= $trs;
525 }
526 } 597 }
527 598
528 my %FILECACHE; 599 my %FILECACHE;
529 600
530 sub load_cached($;$) { 601 sub load_cached($;$) {
541 } 612 }
542 613
543 $FILECACHE{$_[0]} 614 $FILECACHE{$_[0]}
544 } 615 }
545 616
617 # convert an image and a palette to some indexed 2d-matrix structure
618 sub process_plt {
619 my ($base, $plt) = @_;
620
621 my ($w, $h) = imgsize "$base.png";
622
623 $w * $h
624 or die "$base.png: unable to identify correct size\n";
625
626 my @plt;
627 my %map;
628
629 for (split /\n/, $plt) {
630 next unless /\S/;
631
632 /^([0-9a-fA-F]{3,6})\s*(.*?)\s*$/
633 or die "unparseable palette entry for $base.plt: $_";
634
635 my ($rgb, $name) = ($1, $2);
636
637 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
638
639 $map{pack "H*", $rgb} = chr @plt;
640 push @plt, $name;
641 }
642
643 make_file ["$base.plt", "$base.png"], "$base.tbl~", sub {
644 warn "building $base\n" if $VERBOSE >= 3;
645
646 fork_exec {
647 open my $png, "-|", $CONVERT, qw(-depth 8 --), "$base.png", "rgb:"
648 or die "$base.png: $!";
649
650 local $/;
651 $png = <$png>;
652
653 $w * $h * 3 == length $png
654 or die "$base.png: failed to read enough data from file\n";
655
656 $png =~ s/(...)/$map{$1}/ge;
657
658 $w * $h == length $png
659 or die "$base.png: failed to map all data - wrong palette?\n";
660
661 {
662 open my $fh, ">:raw", "$base.tbl~~"
663 or die "$base.tbl~~: $!";
664 syswrite $fh, $png;
665 }
666
667 rename "$base.tbl~~", "$base.tbl~";
668 };
669 };
670
671 0 <= aio_load "$base.tbl~", my $tbl
672 or die "$base.tbl~: $!";
673
674 IO::AIO::aio_unlink "$base.tbl~" unless $CACHE;
675
676 Compress::LZF::compress nfreeze {
677 w => $w,
678 h => $h,
679 plt => \@plt,
680 tbl => $tbl,
681 }
682 }
683
546 sub process_res { 684 sub process_res {
547 while (my $job = $c_res->get) {
548 my ($dir, $file, $type) = @$job; 685 my ($dir, $file, $type) = @_;
549 686
550 my $data;
551 aio_load "$dir/$file", $data; 687 0 <= aio_load "$dir/$file", my $data
688 or die "$dir/$file: $!";
552 689
553 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; 690 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
554 691
555 utf8::decode $dir; 692 utf8::decode $dir;
556 utf8::decode $file; 693 utf8::decode $file;
557 694
558 # a meta file for resources is now mandatory 695 # a meta file for resources is now mandatory
559 unless (exists $meta->{$file}) { 696 unless (exists $meta->{$file}) {
560 warn "skipping $dir/$file\n" if $VERBOSE >= 3; 697 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
561 next; 698 return;
562 } 699 }
563 700
564 $meta = { 701 $meta = {
565 %{ $meta->{"" } || {} }, 702 %{ $meta->{"" } || {} },
566 %{ $meta->{$file} || {} }, 703 %{ $meta->{$file} || {} },
567 }; 704 };
568 705
569 if ($meta->{license} =~ s/^#//) { 706 if ($meta->{license} =~ s/^#//) {
570 $meta->{license} = ({ 707 $meta->{license} = ({
571 "pd" => "Public Domain", 708 "pd" => "Public Domain",
572 "gpl" => "GNU General Public License, version 3.0 or any later", 709 "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/", 710 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
711 "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/", 712 "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/", 713 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
576 })->{$meta->{license}} 714 })->{$meta->{license}}
577 || warn "$dir/$file: license tag '$meta->{license}' not found."; 715 || warn "$dir/$file: license tag '$meta->{license}' not found.";
578 } 716 }
579 717
718 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
719 ($meta->{author} = $1) =~ s/_/ /g;
720 }
721
580 $file =~ s/\.res$//; 722 $file =~ s/\.res$//;
581 $file =~ s/\.(ogg|wav|jpg|png)$//; 723 $file =~ s/\.(ogg|wav|jpg|png)$//;
582 724
583 substr $dir, 0, 1 + length $PATH, ""; 725 if ($file =~ s/\.plt$//) {
584 726 $data = process_plt "$dir/$file", $data;
585 if (my $filter = $meta->{cfutil_filter}) { 727 } elsif (my $filter = $meta->{cfutil_filter}) {
586 if ($filter eq "yaml2json") { 728 if ($filter eq "yaml2json") {
587 $data = JSON::XS::encode_json YAML::XS::Load $data; 729 $data = JSON::XS::encode_json YAML::XS::Load $data;
588 } elsif ($filter eq "json2json") { 730 } elsif ($filter eq "json2json") {
589 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); 731 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
590 } elsif ($filter eq "perl2json") { 732 } elsif ($filter eq "perl2json") {
591 $data = eval $data; die if $@; 733 $data = eval $data; die if $@;
592 $data = JSON::XS::encode_json $data; 734 $data = JSON::XS::encode_json $data;
593 } else { 735 } else {
594 warn "$dir/$file: unknown filter $filter, skipping\n"; 736 warn "$dir/$file: unknown filter $filter, skipping\n";
595 } 737 }
596 } 738 }
597 739
740 substr $dir, 0, 1 + length $PATH, "";
741
598 $RESOURCE{"$dir/$file"} = { 742 $RESOURCE{"$dir/$file"} = {
599 type => (exists $meta->{type} ? delete $meta->{type} : $type), 743 type => (exists $meta->{type} ? delete $meta->{type} : $type),
600 data => $data, 744 data => $data,
601 %$meta ? (meta => $meta) : (), 745 %$meta ? (meta => $meta) : (),
602 }; 746 };
747 }
748
749 sub process_any {
750 while (my ($func, @args) = @{ $c_any->get }) {
751 $func->(@args);
603 } 752 }
604 } 753 }
605 754
606 sub find_files; 755 sub find_files;
607 sub find_files { 756 sub find_files {
608 my ($path) = @_; 757 my ($path) = @_;
609 758
759 my $grp = IO::AIO::aio_group;
760
761 my $scandir; $scandir = sub {
762 my ($path) = @_;
763
610 IO::AIO::aioreq_pri 4; 764 IO::AIO::aioreq_pri 4;
611 IO::AIO::aio_scandir $path, 4, sub { 765 add $grp IO::AIO::aio_scandir $path, 4, sub {
612 my ($dirs, $nondirs) = @_; 766 my ($dirs, $nondirs) = @_;
613 767
614 find_files "$path/$_" 768 $scandir->("$path/$_")
615 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs; 769 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
616 770
617 my $dir = $path; 771 my $dir = $path;
618 substr $dir, 0, 1 + length $PATH, ""; 772 substr $dir, 0, 1 + length $PATH, "";
619 773
620 for my $file (@$nondirs) { 774 for my $file (@$nondirs) {
621 if ($dir =~ /^music(?:\/|$)/) { 775 if ($dir =~ /^music(?:\/|$)/) {
622 $c_res->put ([$path, $file, 3]) # FT_MUSIC 776 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
623 if $file =~ /\.(ogg)$/; 777 if $file =~ /\.(ogg)$/;
624 778
625 } elsif ($dir =~ /^sound(?:\/|$)/) { 779 } elsif ($dir =~ /^sound(?:\/|$)/) {
626 $c_res->put ([$path, $file, 5]) # FT_SOUND 780 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
627 if $file =~ /\.(wav|ogg)$/; 781 if $file =~ /\.(wav|ogg)$/;
628 782
629 } elsif ($dir =~ /^res(?:\/|$)/) { 783 } elsif ($dir =~ /^res(?:\/|$)/) {
630 if ($file =~ /\.(jpg|png)$/) { 784 if ($file =~ /\.(jpg|png)$/) {
631 $c_res->put ([$path, $file, 0]) # FT_FACE 785 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
632 } elsif ($file =~ /\.(res)$/) { 786 } elsif ($file =~ /\.(res)$/) {
787 $c_any->put ([\&process_res, $path, $file, 6]) # FT_RSRC
788 } else {
789 $c_any->put ([\&process_res, $path, $file, undef]);
790 }
791
792 } elsif ($file =~ /\.png$/) {
793 push @c_png, ["$path/$file", 0];
794
795 } elsif ($file =~ /\.faceinfo$/) {
796 $c_any->put ([\&process_faceinfo, $path, $file]);
797
798 } elsif ($file =~ /\.trs$/) {
799 $c_any->put ([\&process_trs, $path, $file]);
800
801 } elsif ($file =~ /\.arc$/) {
633 $c_res->put ([$path, $file, 6]) # FT_RSRC 802 $c_arc->put ([$path, $file]);
803
634 } else { 804 } else {
635 $c_res->put ([$path, $file, undef]); 805 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
636 } 806 }
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 { 807 }
648 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
649 }
650 } 808 };
651 }; 809 };
810
811 $scandir->($path);
812 aio_wait $grp;
652 } 813 }
653 814
654 sub generate_plurals { 815 sub generate_plurals {
655# use Lingua::EN::Inflect (); 816# use Lingua::EN::Inflect ();
656# Lingua::EN::Inflect::classical; 817# Lingua::EN::Inflect::classical;
694 if (!-d "$path/treasures") { 855 if (!-d "$path/treasures") {
695 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 856 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
696 exit 1 unless $FORCE; 857 exit 1 unless $FORCE;
697 } 858 }
698 859
699 print "scanning files...\n" if $VERBOSE; 860 print "start file scan, arc, any processing...\n" if $VERBOSE;
861
862 my @a_arc = map +(async \&process_arc), 1..2;
863 my @a_any = map +(async \&process_any), 1..4;
700 864
701 find_files $path; 865 find_files $path;
702 866
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; 867 $c_arc->shutdown;
711 $c_trs->shutdown; 868 $c_any->shutdown;
712
713 print "start file scan, arc, res processing...\n" if $VERBOSE;
714 869
715 $_->join for @a_arc; # need to parse all archetypes before png processing 870 $_->join for @a_arc; # need to parse all archetypes before png processing
716 871
717 print "end arc, start png processing...\n" if $VERBOSE; 872 print "end arc, start png processing...\n" if $VERBOSE;
718 873
719 # eight png crunchers work fine for my 4x smp machine 874 # eight png crunchers work fine for my 4x smp machine
720 my @a_png = map +(async \&process_png), 1..8; 875 my @a_png = map +(async \&process_png), 1..8;
721 876
722 $_->join for (@a_trs, @a_res, @a_png); 877 print "end any processing...\n" if $VERBOSE;
878 $_->join for @a_any;
879
880 print "end png processing...\n" if $VERBOSE;
881 $_->join for @a_png;
723 882
724 print "scanning done, processing results...\n" if $VERBOSE; 883 print "scanning done, processing results...\n" if $VERBOSE;
725 { 884 {
726 # remove path prefix from editor_folder 885 # remove path prefix from editor_folder
727 $_->{editor_folder} =~ /^\x00/ 886 $_->{editor_folder} =~ /^\x00/
745 delete @s{qw(_name more name name_pl)}; 904 delete @s{qw(_name more name name_pl)};
746 %$o = ( %s, %$o ); 905 %$o = ( %s, %$o );
747 ++$progress; 906 ++$progress;
748 } 907 }
749 } else { 908 } else {
750 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; 909 warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
751 delete $ARC{$o->{_name}}; 910 delete $ARC{$o->{_name}};
752 } 911 }
753 } 912 }
754 } 913 }
755 914
762 } 921 }
763 922
764 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit 923 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
765 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 924 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
766 925
926 # fix up archetypes without names, where the archanme doesn't work at all
927 for (@ARC) {
928 if (!exists $_->{name} and $_->{_name} =~ /_/) {
929 for ($_->{name} = $_->{_name}) {
930 s/(?:_\d+)+$//;
931 s/_[nesw]+$//;
932 y/_/ /;
933 }
934 }
935 }
936
767 print "generating plurals...\n" if $VERBOSE; 937 #print "generating plurals...\n" if $VERBOSE;
768 generate_plurals; 938 #generate_plurals;
769 939
770 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; 940 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
771 open my $fh, ">:utf8", "$DATADIR/archetypes~" 941 open my $fh, ">:utf8", "$DATADIR/archetypes~"
772 or die "$DATADIR/archetypes~: $!"; 942 or die "$DATADIR/archetypes~: $!";
773 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 943 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
781 } 951 }
782 952
783 { 953 {
784 print "processing facedata...\n" if $VERBOSE; 954 print "processing facedata...\n" if $VERBOSE;
785 while (my ($k, $v) = each %FACEINFO) { 955 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"; 956 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"; 957 length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n";
788 958
789 make_hash $k, $v->{data32}, $v->{hash32}; 959 make_hash $k, $v->{data32}, $v->{hash32};
790 make_hash $k, $v->{data64}, $v->{hash64}; 960 make_hash $k, $v->{data64}, $v->{hash64};
791 961
792 #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; 962 #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"; 963 #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n";
794 964
795 if (my $magicmap = $v->{magicmap}) { 965 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
796 $magicmap =~ y/A-Z_\-/a-z/d; 966 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
797 $v->{magicmap} = $COLOR{$magicmap}; 967 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
798 }
799 968
800 delete $v->{arc}; 969 delete @$v{qw(arc stem)}; # not used by the server
801 } 970 }
802 971
803 print "processing resources...\n" if $VERBOSE; 972 print "processing resources...\n" if $VERBOSE;
804 my $enc = JSON::XS->new->utf8->canonical->relaxed; 973 my $enc = JSON::XS->new->utf8->canonical->relaxed;
805 while (my ($k, $v) = each %RESOURCE) { 974 while (my ($k, $v) = each %RESOURCE) {
806 975
976 if ($v->{meta} && $v->{meta}{datadir}) {
977 delete $RESOURCE{$k};
978
979 $k =~ s/^res\/// or die "$k: datadir files must be in res/";
980
981 printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE;
982 open my $fh, ">:raw", "$DATADIR/$k~"
983 or die "$DATADIR/$k~: $!";
984 syswrite $fh, $v->{data};
985 push @COMMIT, $k;
986
987 } else {
807 if ($v->{type} & 1) { 988 if ($v->{type} & 1) {
808 # prepend meta info 989 # prepend meta info
809 990
810 my $meta = $enc->encode ({ 991 my $meta = $enc->encode ({
811 name => $k, 992 name => $k,
812 %{ $v->{meta} || {} }, 993 %{ $v->{meta} || {} },
813 }); 994 });
814 995
815 $v->{data} = pack "(w/a*)*", $meta, $v->{data}; 996 $v->{data} = pack "(w/a*)*", $meta, $v->{data};
816 } 997 }
817 998
818 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients 999 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
1000 }
819 } 1001 }
820 1002
821 printf "writing facedata (%d faces, %d anims, %d resources)...\n", 1003 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
822 scalar keys %FACEINFO, 1004 scalar keys %FACEINFO,
823 scalar keys %ANIMINFO, 1005 scalar keys %ANIMINFO,
831 version => 2, 1013 version => 2,
832 faceinfo => \%FACEINFO, 1014 faceinfo => \%FACEINFO,
833 animinfo => \%ANIMINFO, 1015 animinfo => \%ANIMINFO,
834 resource => \%RESOURCE, 1016 resource => \%RESOURCE,
835 }; 1017 };
836
837 } 1018 }
838 1019
839 print "committing files...\n" if $VERBOSE; 1020 print "committing files...\n" if $VERBOSE;
840 1021
841 for (qw(archetypes facedata treasures)) { 1022 for (qw(archetypes facedata treasures), @COMMIT) {
842 chmod 0644, "$DATADIR/$_~"; 1023 chmod 0644, "$DATADIR/$_~";
843 rename "$DATADIR/$_~", "$DATADIR/$_" 1024 rename "$DATADIR/$_~", "$DATADIR/$_"
844 or die "$DATADIR/$_: $!"; 1025 or die "$DATADIR/$_: $!";
845 } 1026 }
846 1027

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines