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.100 by root, Wed Oct 6 03:44:55 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.
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" };
77my $TMPDIR = "/tmp/cfutil$$~"; 80my $TMPDIR = "/tmp/cfutil$$~";
78my $TMPFILE = "aaaa0"; 81my $TMPFILE = "aaaa0";
79my @COMMIT; 82my @COMMIT;
80 83
81our %COLOR = ( 84our %COLOR = (
85 # original cf colours
82 black => 0, 86 black => 0,
83 white => 1, 87 white => 1,
84 navy => 2, 88 navy => 2, # "dark blue"
85 red => 3, 89 red => 3,
86 orange => 4, 90 orange => 4,
87 blue => 5, 91 blue => 5, # "light blue"
88 darkorange => 6, 92 darkorange => 6,
89 green => 7, 93 green => 7,
90 lightgreen => 8, 94 lightgreen => 8,
91 grey => 9, 95 grey => 9,
96 brown => 10, # "yellow"
97 gold => 11, # "light yellow"
98 tan => 12, # yellowish gray
99
100 # new for deliantra
92 brown => 10, 101 none => 13,
102
103 # compatibility to existing archetypes
104 lightblue => 5,
93 gold => 11, 105 gray => 9,
106 yellow => 11,
94 tan => 12, 107 khaki => 12,
95); 108);
96 109
97END { system "rm", "-rf", $TMPDIR } 110END { system "rm", "-rf", $TMPDIR }
98 111
99my $s_INT = EV::signal INT => sub { exit 1 }; 112my $s_INT = EV::signal INT => sub { exit 1 };
122} 135}
123 136
124mkdir $TMPDIR, 0700 137mkdir $TMPDIR, 0700
125 or die "$TMPDIR: $!"; 138 or die "$TMPDIR: $!";
126 139
127sub fork_sub(&) { 140sub fork_exec(&) {
128 my ($cb) = @_; 141 my ($cb) = @_;
129 142
130 if (my $pid = fork) { 143 if (my $pid = fork) {
131 my $current = $Coro::current; 144 my $current = $Coro::current;
132 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); 145 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
137 warn $@; 150 warn $@;
138 POSIX::_exit 1; 151 POSIX::_exit 1;
139 } 152 }
140} 153}
141 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
142sub inst_maps($) { 184sub inst_maps($) {
143 my (undef, $path) = @_; 185 my (undef, $path) = @_;
144 186
145 print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; 187 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
146 188
154 "--exclude", "CVS", "--exclude", "/world-precomposed", 196 "--exclude", "CVS", "--exclude", "/world-precomposed",
155 "--delete", "--delete-excluded" 197 "--delete", "--delete-excluded"
156 and die "map installation failed.\n"; 198 and die "map installation failed.\n";
157 199
158 print "maps installed successfully.\n"; 200 print "maps installed successfully.\n";
159}
160
161our @WALL_SUFFIX = qw(⬤ ╹ ╺ ┗ ╻ ┃ ┏ ┣ ╸ ┛ ━ ┻ ┓ ┫ ┳ ╋);
162
163# used to create crude text glyphs for text-based clients
164sub autoglyph {
165 my ($stem, $face) = @_;
166
167 if ($stem =~ /^wall\/|Nimwall/) {
168 return $WALL_SUFFIX[hex $1]
169 if $stem =~ /(_[0-9A-F]).x11/;
170
171 "█"
172
173 } elsif ($stem =~ /^traps\//) {
174 "☠"
175
176 } elsif ($stem =~ /^armour\/shield/) {
177 "Ø"
178
179 } elsif ($stem =~ /^armour\//) {
180 "A"
181
182 } elsif ($stem =~ /^weapon\//) {
183 "†"
184
185 } elsif ($stem =~ /^readable\//) {
186 "✉"
187
188 } elsif ($stem =~ /^river\//) {
189 "~"
190
191 } elsif ($stem =~ /^floor\/|^ground\/|Nimfloor/) {
192 "·"
193
194 } elsif ($stem =~ /^spells\//) {
195 "!"
196
197 } elsif ($stem =~ /^player\//) {
198 "\@"
199
200 } elsif ($stem =~ /^(?:monster|misc|class|connect|gods|indoor|inorganic|mining|music|skills).*\/(.)/) {
201 $1
202
203 } else {
204 substr $stem, 0, 1
205 }
206} 201}
207 202
208{ 203{
209 our %ANIMINFO; 204 our %ANIMINFO;
210 our %FACEINFO; 205 our %FACEINFO;
216 our $PATH; 211 our $PATH;
217 212
218 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 213 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
219 214
220 our $c_arc = new Coro::Channel; 215 our $c_arc = new Coro::Channel;
221 our $c_trs = new Coro::Channel; 216 our $c_any = new Coro::Channel;
222 our $c_res = new Coro::Channel;
223 217
224 our @c_png; 218 our @c_png;
225 219
226 sub commit_png($$$$) { 220 sub commit_png($$$$) {
227 my ($stem, $name, $data, $T) = @_; 221 my ($stem, $name, $data, $T) = @_;
286 unless ($path =~ /~$/) { 280 unless ($path =~ /~$/) {
287 # possibly enlarge 281 # possibly enlarge
288 if (0 > aio_stat "$stem.64x64.png") { 282 if (0 > aio_stat "$stem.64x64.png") {
289 my $other = "$stem.64x64.png~"; 283 my $other = "$stem.64x64.png~";
290 284
291 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 285 make_file $path, $other, sub {
292 fork_sub { 286 fork_exec {
293 my $CROP; 287 my $CROP;
294 my $SRC = "png:\Q$path\E"; 288 my $SRC = "png:\Q$path\E";
295 289
296 my $is_floor = $arc->{is_floor}; 290 my $is_floor = $arc->{is_floor};
297 my $is_wall = 0; 291 my $is_wall = 0;
355 and die "convert/cfhq2xa pipeline error: status $? ($!)"; 349 and die "convert/cfhq2xa pipeline error: status $? ($!)";
356 system $OPTIPNG, "-i0", "-q", "$other~"; 350 system $OPTIPNG, "-i0", "-q", "$other~";
357 die "$other~ has zero size, aborting." unless -s "$other~"; 351 die "$other~ has zero size, aborting." unless -s "$other~";
358 rename "$other~", $other; 352 rename "$other~", $other;
359 }; 353 };
360 } 354 };
361 355
362 push @c_png, [$other, !$CACHE]; 356 push @c_png, [$other, !$CACHE];
363 } 357 }
364 358
365 # possibly scale down 359 # possibly scale down
366 if (0 > aio_stat "$stem.32x32.png") { 360 if (0 > aio_stat "$stem.32x32.png") {
367 my $other = "$stem.32x32.png~"; 361 my $other = "$stem.32x32.png~";
368 362
369 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 363 make_file $path, $other, sub {
370 fork_sub { 364 fork_exec {
371 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~";
372 system $OPTIPNG, "-i0", "-q", "$other~"; 366 system $OPTIPNG, "-i0", "-q", "$other~";
373 367
374 # reduce smoothfaces >10000 bytes 368 # reduce smoothfaces >10000 bytes
375 # obsolete, no longer required 369 # obsolete, no longer required
390 } 384 }
391 385
392 die "$other~ has zero size, aborting." unless -s "$other~"; 386 die "$other~ has zero size, aborting." unless -s "$other~";
393 rename "$other~", $other; 387 rename "$other~", $other;
394 }; 388 };
395 } 389 };
396 390
397 #warn "scaled down $path to $other\n";#d# 391 #warn "scaled down $path to $other\n";#d#
398 push @c_png, [$other, !$CACHE]; 392 push @c_png, [$other, !$CACHE];
399 } 393 }
400 } 394 }
414 } 408 }
415 409
416 my $mtime = (lstat $path)[9]; 410 my $mtime = (lstat $path)[9];
417 my @todo = grep { $_->[3] <= $mtime } @tile; 411 my @todo = grep { $_->[3] <= $mtime } @tile;
418 if (@todo) { 412 if (@todo) {
419 fork_sub { 413 fork_exec {
420 open my $convert, "|-", $CONVERT, 414 open my $convert, "|-", $CONVERT,
421 "png:-", 415 "png:-",
422 (map { 416 (map {
423 ( 417 (
424 "(", 418 "(",
463 457
464 aio_unlink $path if $delete; 458 aio_unlink $path if $delete;
465 } 459 }
466 } 460 }
467 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
468 sub process_arc { 503 sub process_arc {
469 while (my $job = $c_arc->get) { 504 while (my ($dir, $file) = @{ $c_arc->get }) {
470 my ($dir, $file) = @$job;
471
472 my $arc; 505 my $arc;
473 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 :/
474 507
475 my $arc = read_arch "$dir/$file"; 508 my $arc = read_arch "$dir/$file";
476 for my $o (values %$arc) { 509 for my $o (values %$arc) {
478 for (my $m = $o; $m; $m = $m->{more}) { 511 for (my $m = $o; $m; $m = $m->{more}) {
479 $ARC{$m->{_name}} = $m; 512 $ARC{$m->{_name}} = $m;
480 } 513 }
481 514
482 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge 515 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
483
484 my $visibility = delete $o->{visibility};
485 my $magicmap = delete $o->{magicmap};
486 my $glyph = delete $o->{glyph};
487 516
488 # find upper left corner :/ 517 # find upper left corner :/
489 # omg, this is sooo broken 518 # omg, this is sooo broken
490 my ($dx, $dy); 519 my ($dx, $dy);
491 for (my $o = $o; $o; $o = $o->{more}) { 520 for (my $o = $o; $o; $o = $o->{more}) {
498 my $y = $o->{y} - $dy; 527 my $y = $o->{y} - $dy;
499 528
500 my $ext = $x|$y ? "+$x+$y" : ""; 529 my $ext = $x|$y ? "+$x+$y" : "";
501 530
502 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; 531 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
503
504 $visibility = delete $o->{visibility} if exists $o->{visibility};
505 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
506 $glyph = delete $o->{glyph} if exists $o->{glyph};
507 532
508 my $anim = delete $o->{anim}; 533 my $anim = delete $o->{anim};
509 534
510 if ($anim) { 535 if ($anim) {
511 # possibly add $ext to the animation name to avoid 536 # possibly add $ext to the animation name to avoid
539 564
540 my $info = $FACEINFO{$face} ||= { }; 565 my $info = $FACEINFO{$face} ||= { };
541 $info->{arc} = $o; 566 $info->{arc} = $o;
542 567
543 next if $face =~ /^blank.x11$|^empty.x11$/; 568 next if $face =~ /^blank.x11$|^empty.x11$/;
544
545 $info->{visibility} = $visibility if defined $visibility;
546 $info->{magicmap} = $magicmap if defined $magicmap;
547 $info->{glyph} = $glyph if defined $glyph;
548 } 569 }
549 570
550 if (my $smooth = delete $o->{smoothface}) { 571 if (my $smooth = delete $o->{smoothface}) {
551 my %kv = split /\s+/, $smooth; 572 my %kv = split /\s+/, $smooth;
552 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 573 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
561 } 582 }
562 } 583 }
563 } 584 }
564 585
565 sub process_trs { 586 sub process_trs {
566 while (my $job = $c_trs->get) {
567 my ($dir, $file) = @$job; 587 my ($dir, $file) = @_;
568 my $path = "$dir/$file"; 588 my $path = "$dir/$file";
569 589
570 my $trs; 590 my $trs;
571 if (0 > aio_load $path, $trs) { 591 if (0 > aio_load $path, $trs) {
572 warn "$path: $!, skipping.\n"; 592 warn "$path: $!, skipping.\n";
573 next; 593 return;
574 } 594 }
575 595
576 $TRS .= $trs; 596 $TRS .= $trs;
577 }
578 } 597 }
579 598
580 my %FILECACHE; 599 my %FILECACHE;
581 600
582 sub load_cached($;$) { 601 sub load_cached($;$) {
593 } 612 }
594 613
595 $FILECACHE{$_[0]} 614 $FILECACHE{$_[0]}
596 } 615 }
597 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
598 sub process_res { 684 sub process_res {
599 while (my $job = $c_res->get) {
600 my ($dir, $file, $type) = @$job; 685 my ($dir, $file, $type) = @_;
601 686
602 my $data;
603 aio_load "$dir/$file", $data; 687 0 <= aio_load "$dir/$file", my $data
688 or die "$dir/$file: $!";
604 689
605 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) };
606 691
607 utf8::decode $dir; 692 utf8::decode $dir;
608 utf8::decode $file; 693 utf8::decode $file;
609 694
610 # a meta file for resources is now mandatory 695 # a meta file for resources is now mandatory
611 unless (exists $meta->{$file}) { 696 unless (exists $meta->{$file}) {
612 warn "skipping $dir/$file\n" if $VERBOSE >= 3; 697 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
613 next; 698 return;
614 } 699 }
615 700
616 $meta = { 701 $meta = {
617 %{ $meta->{"" } || {} }, 702 %{ $meta->{"" } || {} },
618 %{ $meta->{$file} || {} }, 703 %{ $meta->{$file} || {} },
619 }; 704 };
620 705
621 if ($meta->{license} =~ s/^#//) { 706 if ($meta->{license} =~ s/^#//) {
622 $meta->{license} = ({ 707 $meta->{license} = ({
623 "pd" => "Public Domain", 708 "pd" => "Public Domain",
624 "gpl" => "GNU General Public License, version 3.0 or any later", 709 "gpl" => "GNU General Public License, version 3.0 or any later",
625 "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/",
626 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/", 711 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/",
627 "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/",
628 "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/",
629 })->{$meta->{license}} 714 })->{$meta->{license}}
630 || warn "$dir/$file: license tag '$meta->{license}' not found."; 715 || warn "$dir/$file: license tag '$meta->{license}' not found.";
631 } 716 }
632 717
633 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) { 718 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
634 ($meta->{author} = $1) =~ s/_/ /g; 719 ($meta->{author} = $1) =~ s/_/ /g;
635 } 720 }
636 721
637 $file =~ s/\.res$//; 722 $file =~ s/\.res$//;
638 $file =~ s/\.(ogg|wav|jpg|png)$//; 723 $file =~ s/\.(ogg|wav|jpg|png)$//;
639 724
640 substr $dir, 0, 1 + length $PATH, ""; 725 if ($file =~ s/\.plt$//) {
641 726 $data = process_plt "$dir/$file", $data;
642 if (my $filter = $meta->{cfutil_filter}) { 727 } elsif (my $filter = $meta->{cfutil_filter}) {
643 if ($filter eq "yaml2json") { 728 if ($filter eq "yaml2json") {
644 $data = JSON::XS::encode_json YAML::XS::Load $data; 729 $data = JSON::XS::encode_json YAML::XS::Load $data;
645 } elsif ($filter eq "json2json") { 730 } elsif ($filter eq "json2json") {
646 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); 731 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
647 } elsif ($filter eq "perl2json") { 732 } elsif ($filter eq "perl2json") {
648 $data = eval $data; die if $@; 733 $data = eval $data; die if $@;
649 $data = JSON::XS::encode_json $data; 734 $data = JSON::XS::encode_json $data;
650 } else { 735 } else {
651 warn "$dir/$file: unknown filter $filter, skipping\n"; 736 warn "$dir/$file: unknown filter $filter, skipping\n";
652 } 737 }
653 } 738 }
654 739
740 substr $dir, 0, 1 + length $PATH, "";
741
655 $RESOURCE{"$dir/$file"} = { 742 $RESOURCE{"$dir/$file"} = {
656 type => (exists $meta->{type} ? delete $meta->{type} : $type), 743 type => (exists $meta->{type} ? delete $meta->{type} : $type),
657 data => $data, 744 data => $data,
658 %$meta ? (meta => $meta) : (), 745 %$meta ? (meta => $meta) : (),
659 }; 746 };
747 }
748
749 sub process_any {
750 while (my ($func, @args) = @{ $c_any->get }) {
751 $func->(@args);
660 } 752 }
661 } 753 }
662 754
663 sub find_files; 755 sub find_files;
664 sub find_files { 756 sub find_files {
665 my ($path) = @_; 757 my ($path) = @_;
666 758
759 my $grp = IO::AIO::aio_group;
760
761 my $scandir; $scandir = sub {
762 my ($path) = @_;
763
667 IO::AIO::aioreq_pri 4; 764 IO::AIO::aioreq_pri 4;
668 IO::AIO::aio_scandir $path, 4, sub { 765 add $grp IO::AIO::aio_scandir $path, 4, sub {
669 my ($dirs, $nondirs) = @_; 766 my ($dirs, $nondirs) = @_;
670 767
671 find_files "$path/$_" 768 $scandir->("$path/$_")
672 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs; 769 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
673 770
674 my $dir = $path; 771 my $dir = $path;
675 substr $dir, 0, 1 + length $PATH, ""; 772 substr $dir, 0, 1 + length $PATH, "";
676 773
677 for my $file (@$nondirs) { 774 for my $file (@$nondirs) {
678 if ($dir =~ /^music(?:\/|$)/) { 775 if ($dir =~ /^music(?:\/|$)/) {
679 $c_res->put ([$path, $file, 3]) # FT_MUSIC 776 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
680 if $file =~ /\.(ogg)$/; 777 if $file =~ /\.(ogg)$/;
681 778
682 } elsif ($dir =~ /^sound(?:\/|$)/) { 779 } elsif ($dir =~ /^sound(?:\/|$)/) {
683 $c_res->put ([$path, $file, 5]) # FT_SOUND 780 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
684 if $file =~ /\.(wav|ogg)$/; 781 if $file =~ /\.(wav|ogg)$/;
685 782
686 } elsif ($dir =~ /^res(?:\/|$)/) { 783 } elsif ($dir =~ /^res(?:\/|$)/) {
687 if ($file =~ /\.(jpg|png)$/) { 784 if ($file =~ /\.(jpg|png)$/) {
688 $c_res->put ([$path, $file, 0]) # FT_FACE 785 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
689 } 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$/) {
690 $c_res->put ([$path, $file, 6]) # FT_RSRC 802 $c_arc->put ([$path, $file]);
803
691 } else { 804 } else {
692 $c_res->put ([$path, $file, undef]); 805 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
693 } 806 }
694
695 } elsif ($file =~ /\.png$/) {
696 push @c_png, ["$path/$file", 0];
697
698 } elsif ($file =~ /\.trs$/) {
699 $c_trs->put ([$path, $file]);
700
701 } elsif ($file =~ /\.arc$/) {
702 $c_arc->put ([$path, $file]);
703
704 } else { 807 }
705 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
706 }
707 } 808 };
708 }; 809 };
810
811 $scandir->($path);
812 aio_wait $grp;
709 } 813 }
710 814
711 sub generate_plurals { 815 sub generate_plurals {
712# use Lingua::EN::Inflect (); 816# use Lingua::EN::Inflect ();
713# Lingua::EN::Inflect::classical; 817# Lingua::EN::Inflect::classical;
751 if (!-d "$path/treasures") { 855 if (!-d "$path/treasures") {
752 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";
753 exit 1 unless $FORCE; 857 exit 1 unless $FORCE;
754 } 858 }
755 859
756 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;
757 864
758 find_files $path; 865 find_files $path;
759 866
760 my @a_arc = map +(async \&process_arc), 1..2;
761 my @a_res = map +(async \&process_res), 1..2;
762 my @a_trs = map +(async \&process_trs), 1..2;
763
764 IO::AIO::flush;
765
766 $c_res->shutdown;
767 $c_arc->shutdown; 867 $c_arc->shutdown;
768 $c_trs->shutdown; 868 $c_any->shutdown;
769
770 print "start file scan, arc, res processing...\n" if $VERBOSE;
771 869
772 $_->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
773 871
774 print "end arc, start png processing...\n" if $VERBOSE; 872 print "end arc, start png processing...\n" if $VERBOSE;
775 873
776 # eight png crunchers work fine for my 4x smp machine 874 # eight png crunchers work fine for my 4x smp machine
777 my @a_png = map +(async \&process_png), 1..8; 875 my @a_png = map +(async \&process_png), 1..8;
778 876
779 $_->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;
780 882
781 print "scanning done, processing results...\n" if $VERBOSE; 883 print "scanning done, processing results...\n" if $VERBOSE;
782 { 884 {
783 # remove path prefix from editor_folder 885 # remove path prefix from editor_folder
784 $_->{editor_folder} =~ /^\x00/ 886 $_->{editor_folder} =~ /^\x00/
802 delete @s{qw(_name more name name_pl)}; 904 delete @s{qw(_name more name name_pl)};
803 %$o = ( %s, %$o ); 905 %$o = ( %s, %$o );
804 ++$progress; 906 ++$progress;
805 } 907 }
806 } else { 908 } else {
807 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";
808 delete $ARC{$o->{_name}}; 910 delete $ARC{$o->{_name}};
809 } 911 }
810 } 912 }
811 } 913 }
812 914
819 } 921 }
820 922
821 # 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
822 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 924 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
823 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
824 print "generating plurals...\n" if $VERBOSE; 937 #print "generating plurals...\n" if $VERBOSE;
825 generate_plurals; 938 #generate_plurals;
826 939
827 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; 940 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
828 open my $fh, ">:utf8", "$DATADIR/archetypes~" 941 open my $fh, ">:utf8", "$DATADIR/archetypes~"
829 or die "$DATADIR/archetypes~: $!"; 942 or die "$DATADIR/archetypes~: $!";
830 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];
838 } 951 }
839 952
840 { 953 {
841 print "processing facedata...\n" if $VERBOSE; 954 print "processing facedata...\n" if $VERBOSE;
842 while (my ($k, $v) = each %FACEINFO) { 955 while (my ($k, $v) = each %FACEINFO) {
843 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";
844 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";
845 958
846 make_hash $k, $v->{data32}, $v->{hash32}; 959 make_hash $k, $v->{data32}, $v->{hash32};
847 make_hash $k, $v->{data64}, $v->{hash64}; 960 make_hash $k, $v->{data64}, $v->{hash64};
848 961
849 #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";
850 #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";
851 964
852 if (my $magicmap = $v->{magicmap}) { 965 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
853 $magicmap =~ y/A-Z_\-/a-z/d; 966 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
854 $v->{magicmap} = $COLOR{$magicmap}; 967 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
855 }
856 968
857 my $stem = delete $v->{stem}; 969 delete @$v{qw(arc stem)}; # not used by the server
858 $v->{glyph} //= autoglyph $stem, $v;
859 utf8::encode $v->{glyph};
860 $v->{glyph} = (chr $v->{magicmap}) . $v->{glyph};
861
862 delete $v->{arc};
863 } 970 }
864 971
865 print "processing resources...\n" if $VERBOSE; 972 print "processing resources...\n" if $VERBOSE;
866 my $enc = JSON::XS->new->utf8->canonical->relaxed; 973 my $enc = JSON::XS->new->utf8->canonical->relaxed;
867 while (my ($k, $v) = each %RESOURCE) { 974 while (my ($k, $v) = each %RESOURCE) {
906 version => 2, 1013 version => 2,
907 faceinfo => \%FACEINFO, 1014 faceinfo => \%FACEINFO,
908 animinfo => \%ANIMINFO, 1015 animinfo => \%ANIMINFO,
909 resource => \%RESOURCE, 1016 resource => \%RESOURCE,
910 }; 1017 };
911
912 } 1018 }
913 1019
914 print "committing files...\n" if $VERBOSE; 1020 print "committing files...\n" if $VERBOSE;
915 1021
916 for (qw(archetypes facedata treasures), @COMMIT) { 1022 for (qw(archetypes facedata treasures), @COMMIT) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines