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.91 by root, Tue Oct 5 23:52:52 2010 UTC vs.
Revision 1.113 by root, Sat Apr 23 04:56:58 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.
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 };
106 my ($id, $dataref, $hashref, $clen) = @_; 119 my ($id, $dataref, $hashref, $clen) = @_;
107 120
108 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4; 121 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4;
109 122
110 if (exists $hash{$hash}) { 123 if (exists $hash{$hash}) {
111 # hahs collinion, but some files are simply identical 124 # hash collision, but some files are simply identical
112 if (${$hash{$hash}[1]} ne $$dataref) { 125 if (${$hash{$hash}[1]} ne $$dataref) {
113 warn "hash collision $hash{$hash}[0] vs. $id\n"; 126 warn "hash collision $hash{$hash}[0] vs. $id\n";
114 exit 1; 127 exit 1;
115 } else { 128 } else {
116 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;
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 = (
162 "0" => "⬤",
163 "1_3" => "╸",
164 "1_4" => "╺",
165 "2_1_2" => "━",
166
167 "1_2" => "╹",
168 "2_2_4" => "┛",
169 "2_2_1" => "┗",
170 "3_1" => "┻",
171
172 "1_1" => "╻",
173 "2_2_3" => "┓",
174 "2_2_2" => "┏",
175 "3_3" => "┳",
176
177 "2_1_1" => "┃",
178 "3_4" => "┫",
179 "3_2" => "┣",
180 "4" => "╋",
181);
182
183sub autoglyph {
184 my ($stem, $face) = @_;
185
186 if ($stem =~ /^wall\/|Nimwall/) {
187 while (my ($k, $v) = each %WALL_SUFFIX) {
188 return $v if $stem =~ /_$k.x11/;
189 }
190
191 "+"
192
193 } elsif ($stem =~ /^floor\/|^ground\/|Nimfloor/) {
194 "."
195
196 } elsif ($stem =~ /^player\//) {
197 "\@"
198
199 } else {
200 substr $stem, 0, 1
201 }
202} 201}
203 202
204{ 203{
205 our %ANIMINFO; 204 our %ANIMINFO;
206 our %FACEINFO; 205 our %FACEINFO;
212 our $PATH; 211 our $PATH;
213 212
214 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 213 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
215 214
216 our $c_arc = new Coro::Channel; 215 our $c_arc = new Coro::Channel;
217 our $c_trs = new Coro::Channel; 216 our $c_any = new Coro::Channel;
218 our $c_res = new Coro::Channel;
219 217
220 our @c_png; 218 our @c_png;
221 219
222 sub commit_png($$$$) { 220 sub commit_png($$$$) {
223 my ($stem, $name, $data, $T) = @_; 221 my ($stem, $name, $data, $T) = @_;
282 unless ($path =~ /~$/) { 280 unless ($path =~ /~$/) {
283 # possibly enlarge 281 # possibly enlarge
284 if (0 > aio_stat "$stem.64x64.png") { 282 if (0 > aio_stat "$stem.64x64.png") {
285 my $other = "$stem.64x64.png~"; 283 my $other = "$stem.64x64.png~";
286 284
287 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 285 make_file $path, $other, sub {
288 fork_sub { 286 fork_exec {
289 my $CROP; 287 my $CROP;
290 my $SRC = "png:\Q$path\E"; 288 my $SRC = "png:\Q$path\E";
291 289
292 my $is_floor = $arc->{is_floor}; 290 my $is_floor = $arc->{is_floor};
293 my $is_wall = 0; 291 my $is_wall = 0;
351 and die "convert/cfhq2xa pipeline error: status $? ($!)"; 349 and die "convert/cfhq2xa pipeline error: status $? ($!)";
352 system $OPTIPNG, "-i0", "-q", "$other~"; 350 system $OPTIPNG, "-i0", "-q", "$other~";
353 die "$other~ has zero size, aborting." unless -s "$other~"; 351 die "$other~ has zero size, aborting." unless -s "$other~";
354 rename "$other~", $other; 352 rename "$other~", $other;
355 }; 353 };
356 } 354 };
357 355
358 push @c_png, [$other, !$CACHE]; 356 push @c_png, [$other, !$CACHE];
359 } 357 }
360 358
361 # possibly scale down 359 # possibly scale down
362 if (0 > aio_stat "$stem.32x32.png") { 360 if (0 > aio_stat "$stem.32x32.png") {
363 my $other = "$stem.32x32.png~"; 361 my $other = "$stem.32x32.png~";
364 362
365 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 363 make_file $path, $other, sub {
366 fork_sub { 364 fork_exec {
367 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~";
368 system $OPTIPNG, "-i0", "-q", "$other~"; 366 system $OPTIPNG, "-i0", "-q", "$other~";
369 367
370 # reduce smoothfaces >10000 bytes 368 # reduce smoothfaces >10000 bytes
371 # obsolete, no longer required 369 # obsolete, no longer required
386 } 384 }
387 385
388 die "$other~ has zero size, aborting." unless -s "$other~"; 386 die "$other~ has zero size, aborting." unless -s "$other~";
389 rename "$other~", $other; 387 rename "$other~", $other;
390 }; 388 };
391 } 389 };
392 390
393 #warn "scaled down $path to $other\n";#d# 391 #warn "scaled down $path to $other\n";#d#
394 push @c_png, [$other, !$CACHE]; 392 push @c_png, [$other, !$CACHE];
395 } 393 }
396 } 394 }
410 } 408 }
411 409
412 my $mtime = (lstat $path)[9]; 410 my $mtime = (lstat $path)[9];
413 my @todo = grep { $_->[3] <= $mtime } @tile; 411 my @todo = grep { $_->[3] <= $mtime } @tile;
414 if (@todo) { 412 if (@todo) {
415 fork_sub { 413 fork_exec {
416 open my $convert, "|-", $CONVERT, 414 open my $convert, "|-", $CONVERT,
417 "png:-", 415 "png:-",
418 (map { 416 (map {
419 ( 417 (
420 "(", 418 "(",
459 457
460 aio_unlink $path if $delete; 458 aio_unlink $path if $delete;
461 } 459 }
462 } 460 }
463 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
464 sub process_arc { 503 sub process_arc {
465 while (my $job = $c_arc->get) { 504 while (my ($dir, $file) = @{ $c_arc->get }) {
466 my ($dir, $file) = @$job;
467
468 my $arc; 505 my $arc;
469 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 :/
470 507
471 my $arc = read_arch "$dir/$file"; 508 my $arc = read_arch "$dir/$file";
472 for my $o (values %$arc) { 509 for my $o (values %$arc) {
474 for (my $m = $o; $m; $m = $m->{more}) { 511 for (my $m = $o; $m; $m = $m->{more}) {
475 $ARC{$m->{_name}} = $m; 512 $ARC{$m->{_name}} = $m;
476 } 513 }
477 514
478 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge 515 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
479
480 my $visibility = delete $o->{visibility};
481 my $magicmap = delete $o->{magicmap};
482 my $glyph = delete $o->{glyph};
483 516
484 # find upper left corner :/ 517 # find upper left corner :/
485 # omg, this is sooo broken 518 # omg, this is sooo broken
486 my ($dx, $dy); 519 my ($dx, $dy);
487 for (my $o = $o; $o; $o = $o->{more}) { 520 for (my $o = $o; $o; $o = $o->{more}) {
494 my $y = $o->{y} - $dy; 527 my $y = $o->{y} - $dy;
495 528
496 my $ext = $x|$y ? "+$x+$y" : ""; 529 my $ext = $x|$y ? "+$x+$y" : "";
497 530
498 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; 531 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
499
500 $visibility = delete $o->{visibility} if exists $o->{visibility};
501 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
502 $glyph = delete $o->{glyph} if exists $o->{glyph};
503 532
504 my $anim = delete $o->{anim}; 533 my $anim = delete $o->{anim};
505 534
506 if ($anim) { 535 if ($anim) {
507 # possibly add $ext to the animation name to avoid 536 # possibly add $ext to the animation name to avoid
535 564
536 my $info = $FACEINFO{$face} ||= { }; 565 my $info = $FACEINFO{$face} ||= { };
537 $info->{arc} = $o; 566 $info->{arc} = $o;
538 567
539 next if $face =~ /^blank.x11$|^empty.x11$/; 568 next if $face =~ /^blank.x11$|^empty.x11$/;
540
541 $info->{visibility} = $visibility if defined $visibility;
542 $info->{magicmap} = $magicmap if defined $magicmap;
543 $info->{glyph} = $glyph if defined $glyph;
544 } 569 }
545 570
546 if (my $smooth = delete $o->{smoothface}) { 571 if (my $smooth = delete $o->{smoothface}) {
547 my %kv = split /\s+/, $smooth; 572 my %kv = split /\s+/, $smooth;
548 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 573 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
557 } 582 }
558 } 583 }
559 } 584 }
560 585
561 sub process_trs { 586 sub process_trs {
562 while (my $job = $c_trs->get) {
563 my ($dir, $file) = @$job; 587 my ($dir, $file) = @_;
564 my $path = "$dir/$file"; 588 my $path = "$dir/$file";
565 589
566 my $trs; 590 my $trs;
567 if (0 > aio_load $path, $trs) { 591 if (0 > aio_load $path, $trs) {
568 warn "$path: $!, skipping.\n"; 592 warn "$path: $!, skipping.\n";
569 next; 593 return;
570 } 594 }
571 595
572 $TRS .= $trs; 596 $TRS .= $trs;
573 }
574 } 597 }
575 598
576 my %FILECACHE; 599 my %FILECACHE;
577 600
578 sub load_cached($;$) { 601 sub load_cached($;$) {
589 } 612 }
590 613
591 $FILECACHE{$_[0]} 614 $FILECACHE{$_[0]}
592 } 615 }
593 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 next if /^\s*#/;
632
633 /^([0-9a-fA-F]{3,6})\s*(.*?)\s*$/
634 or die "unparseable palette entry for $base.plt: $_";
635
636 my ($rgb, $name) = ($1, $2);
637
638 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
639
640 $map{pack "H*", $rgb} = chr @plt;
641 push @plt, $name;
642 }
643
644 make_file ["$base.plt", "$base.png"], "$base.tbl~", sub {
645 warn "building $base\n" if $VERBOSE >= 3;
646
647 fork_exec {
648 open my $png, "-|", $CONVERT, qw(-depth 8 --), "$base.png", "rgb:"
649 or die "$base.png: $!";
650
651 local $/;
652 $png = <$png>;
653
654 $w * $h * 3 == length $png
655 or die "$base.png: failed to read enough data from file\n";
656
657 $png =~ s/(...)/$map{$1}/ge;
658
659 $w * $h == length $png
660 or die "$base.png: failed to map all data - wrong palette?\n";
661
662 {
663 open my $fh, ">:raw", "$base.tbl~~"
664 or die "$base.tbl~~: $!";
665 syswrite $fh, $png;
666 }
667
668 rename "$base.tbl~~", "$base.tbl~";
669 };
670 };
671
672 0 <= aio_load "$base.tbl~", my $tbl
673 or die "$base.tbl~: $!";
674
675 IO::AIO::aio_unlink "$base.tbl~" unless $CACHE;
676
677 Compress::LZF::compress nfreeze {
678 w => $w,
679 h => $h,
680 plt => \@plt,
681 tbl => $tbl,
682 }
683 }
684
594 sub process_res { 685 sub process_res {
595 while (my $job = $c_res->get) {
596 my ($dir, $file, $type) = @$job; 686 my ($dir, $file, $type) = @_;
597 687
598 my $data;
599 aio_load "$dir/$file", $data; 688 0 <= aio_load "$dir/$file", my $data
689 or die "$dir/$file: $!";
600 690
601 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; 691 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
602 692
603 utf8::decode $dir; 693 utf8::decode $dir;
604 utf8::decode $file; 694 utf8::decode $file;
605 695
606 # a meta file for resources is now mandatory 696 # a meta file for resources is now mandatory
607 unless (exists $meta->{$file}) { 697 unless (exists $meta->{$file}) {
608 warn "skipping $dir/$file\n" if $VERBOSE >= 3; 698 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
609 next; 699 return;
610 } 700 }
611 701
612 $meta = { 702 $meta = {
613 %{ $meta->{"" } || {} }, 703 %{ $meta->{"" } || {} },
614 %{ $meta->{$file} || {} }, 704 %{ $meta->{$file} || {} },
615 }; 705 };
616 706
617 if ($meta->{license} =~ s/^#//) { 707 if ($meta->{license} =~ s/^#//) {
618 $meta->{license} = ({ 708 $meta->{license} = ({
619 "pd" => "Public Domain", 709 "pd" => "Public Domain",
620 "gpl" => "GNU General Public License, version 3.0 or any later", 710 "gpl" => "GNU General Public License, version 3.0 or any later",
621 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", 711 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
622 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/", 712 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/",
623 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/", 713 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
624 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", 714 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
625 })->{$meta->{license}} 715 })->{$meta->{license}}
626 || warn "$dir/$file: license tag '$meta->{license}' not found."; 716 || warn "$dir/$file: license tag '$meta->{license}' not found.";
627 } 717 }
628 718
629 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) { 719 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
630 ($meta->{author} = $1) =~ s/_/ /g; 720 ($meta->{author} = $1) =~ s/_/ /g;
631 } 721 }
632 722
633 $file =~ s/\.res$//; 723 $file =~ s/\.res$//;
634 $file =~ s/\.(ogg|wav|jpg|png)$//; 724 $file =~ s/\.(ogg|wav|jpg|png)$//;
635 725
636 substr $dir, 0, 1 + length $PATH, ""; 726 if ($file =~ s/\.plt$//) {
637 727 $data = process_plt "$dir/$file", $data;
638 if (my $filter = $meta->{cfutil_filter}) { 728 } elsif (my $filter = $meta->{cfutil_filter}) {
639 if ($filter eq "yaml2json") { 729 if ($filter eq "yaml2json") {
640 $data = JSON::XS::encode_json YAML::XS::Load $data; 730 $data = JSON::XS::encode_json YAML::XS::Load $data;
641 } elsif ($filter eq "json2json") { 731 } elsif ($filter eq "json2json") {
642 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); 732 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
643 } elsif ($filter eq "perl2json") { 733 } elsif ($filter eq "perl2json") {
644 $data = eval $data; die if $@; 734 $data = eval $data; die if $@;
645 $data = JSON::XS::encode_json $data; 735 $data = JSON::XS::encode_json $data;
646 } else { 736 } else {
647 warn "$dir/$file: unknown filter $filter, skipping\n"; 737 warn "$dir/$file: unknown filter $filter, skipping\n";
648 } 738 }
649 } 739 }
650 740
741 substr $dir, 0, 1 + length $PATH, "";
742
651 $RESOURCE{"$dir/$file"} = { 743 $RESOURCE{"$dir/$file"} = {
652 type => (exists $meta->{type} ? delete $meta->{type} : $type), 744 type => (exists $meta->{type} ? delete $meta->{type} : $type),
653 data => $data, 745 data => $data,
654 %$meta ? (meta => $meta) : (), 746 %$meta ? (meta => $meta) : (),
655 }; 747 };
748 }
749
750 sub process_any {
751 while (my ($func, @args) = @{ $c_any->get }) {
752 $func->(@args);
656 } 753 }
657 } 754 }
658 755
659 sub find_files; 756 sub find_files;
660 sub find_files { 757 sub find_files {
661 my ($path) = @_; 758 my ($path) = @_;
662 759
760 my $grp = IO::AIO::aio_group;
761
762 my $scandir; $scandir = sub {
763 my ($path) = @_;
764
663 IO::AIO::aioreq_pri 4; 765 IO::AIO::aioreq_pri 4;
664 IO::AIO::aio_scandir $path, 4, sub { 766 add $grp IO::AIO::aio_scandir $path, 4, sub {
665 my ($dirs, $nondirs) = @_; 767 my ($dirs, $nondirs) = @_;
666 768
667 find_files "$path/$_" 769 $scandir->("$path/$_")
668 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs; 770 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
669 771
670 my $dir = $path; 772 my $dir = $path;
671 substr $dir, 0, 1 + length $PATH, ""; 773 substr $dir, 0, 1 + length $PATH, "";
672 774
673 for my $file (@$nondirs) { 775 for my $file (@$nondirs) {
674 if ($dir =~ /^music(?:\/|$)/) { 776 if ($dir =~ /^music(?:\/|$)/) {
675 $c_res->put ([$path, $file, 3]) # FT_MUSIC 777 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
676 if $file =~ /\.(ogg)$/; 778 if $file =~ /\.(ogg)$/;
677 779
678 } elsif ($dir =~ /^sound(?:\/|$)/) { 780 } elsif ($dir =~ /^sound(?:\/|$)/) {
679 $c_res->put ([$path, $file, 5]) # FT_SOUND 781 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
680 if $file =~ /\.(wav|ogg)$/; 782 if $file =~ /\.(wav|ogg)$/;
681 783
682 } elsif ($dir =~ /^res(?:\/|$)/) { 784 } elsif ($dir =~ /^res(?:\/|$)/) {
683 if ($file =~ /\.(jpg|png)$/) { 785 if ($file =~ /\.(jpg|png)$/) {
684 $c_res->put ([$path, $file, 0]) # FT_FACE 786 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
685 } elsif ($file =~ /\.(res)$/) { 787 } elsif ($file =~ /\.(res)$/) {
788 $c_any->put ([\&process_res, $path, $file, 6]) # FT_RSRC
789 } else {
790 $c_any->put ([\&process_res, $path, $file, undef]);
791 }
792
793 } elsif ($file =~ /\.png$/) {
794 push @c_png, ["$path/$file", 0];
795
796 } elsif ($file =~ /\.faceinfo$/) {
797 $c_any->put ([\&process_faceinfo, $path, $file]);
798
799 } elsif ($file =~ /\.trs$/) {
800 $c_any->put ([\&process_trs, $path, $file]);
801
802 } elsif ($file =~ /\.arc$/) {
686 $c_res->put ([$path, $file, 6]) # FT_RSRC 803 $c_arc->put ([$path, $file]);
804
687 } else { 805 } else {
688 $c_res->put ([$path, $file, undef]); 806 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
689 } 807 }
690
691 } elsif ($file =~ /\.png$/) {
692 push @c_png, ["$path/$file", 0];
693
694 } elsif ($file =~ /\.trs$/) {
695 $c_trs->put ([$path, $file]);
696
697 } elsif ($file =~ /\.arc$/) {
698 $c_arc->put ([$path, $file]);
699
700 } else { 808 }
701 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
702 }
703 } 809 };
704 }; 810 };
811
812 $scandir->($path);
813 aio_wait $grp;
705 } 814 }
706 815
707 sub generate_plurals { 816 sub generate_plurals {
708# use Lingua::EN::Inflect (); 817# use Lingua::EN::Inflect ();
709# Lingua::EN::Inflect::classical; 818# Lingua::EN::Inflect::classical;
747 if (!-d "$path/treasures") { 856 if (!-d "$path/treasures") {
748 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 857 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
749 exit 1 unless $FORCE; 858 exit 1 unless $FORCE;
750 } 859 }
751 860
752 print "scanning files...\n" if $VERBOSE; 861 print "start file scan, arc, any processing...\n" if $VERBOSE;
862
863 my @a_arc = map +(async \&process_arc), 1..2;
864 my @a_any = map +(async \&process_any), 1..4;
753 865
754 find_files $path; 866 find_files $path;
755 867
756 my @a_arc = map +(async \&process_arc), 1..2;
757 my @a_res = map +(async \&process_res), 1..2;
758 my @a_trs = map +(async \&process_trs), 1..2;
759
760 IO::AIO::flush;
761
762 $c_res->shutdown;
763 $c_arc->shutdown; 868 $c_arc->shutdown;
764 $c_trs->shutdown; 869 $c_any->shutdown;
765
766 print "start file scan, arc, res processing...\n" if $VERBOSE;
767 870
768 $_->join for @a_arc; # need to parse all archetypes before png processing 871 $_->join for @a_arc; # need to parse all archetypes before png processing
769 872
770 print "end arc, start png processing...\n" if $VERBOSE; 873 print "end arc, start png processing...\n" if $VERBOSE;
771 874
772 # eight png crunchers work fine for my 4x smp machine 875 # eight png crunchers work fine for my 4x smp machine
773 my @a_png = map +(async \&process_png), 1..8; 876 my @a_png = map +(async \&process_png), 1..8;
774 877
775 $_->join for (@a_trs, @a_res, @a_png); 878 print "end any processing...\n" if $VERBOSE;
879 $_->join for @a_any;
880
881 print "end png processing...\n" if $VERBOSE;
882 $_->join for @a_png;
776 883
777 print "scanning done, processing results...\n" if $VERBOSE; 884 print "scanning done, processing results...\n" if $VERBOSE;
778 { 885 {
779 # remove path prefix from editor_folder 886 # remove path prefix from editor_folder
780 $_->{editor_folder} =~ /^\x00/ 887 $_->{editor_folder} =~ /^\x00/
798 delete @s{qw(_name more name name_pl)}; 905 delete @s{qw(_name more name name_pl)};
799 %$o = ( %s, %$o ); 906 %$o = ( %s, %$o );
800 ++$progress; 907 ++$progress;
801 } 908 }
802 } else { 909 } else {
803 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; 910 warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
804 delete $ARC{$o->{_name}}; 911 delete $ARC{$o->{_name}};
805 } 912 }
806 } 913 }
807 } 914 }
808 915
815 } 922 }
816 923
817 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit 924 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
818 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 925 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
819 926
927 # fix up archetypes without names, where the archanme doesn't work at all
928 for (@ARC) {
929 if (!exists $_->{name} and $_->{_name} =~ /_/) {
930 for ($_->{name} = $_->{_name}) {
931 s/(?:_\d+)+$//;
932 s/_[nesw]+$//;
933 y/_/ /;
934 }
935 }
936 }
937
820 print "generating plurals...\n" if $VERBOSE; 938 #print "generating plurals...\n" if $VERBOSE;
821 generate_plurals; 939 #generate_plurals;
822 940
823 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; 941 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
824 open my $fh, ">:utf8", "$DATADIR/archetypes~" 942 open my $fh, ">:utf8", "$DATADIR/archetypes~"
825 or die "$DATADIR/archetypes~: $!"; 943 or die "$DATADIR/archetypes~: $!";
826 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 944 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
834 } 952 }
835 953
836 { 954 {
837 print "processing facedata...\n" if $VERBOSE; 955 print "processing facedata...\n" if $VERBOSE;
838 while (my ($k, $v) = each %FACEINFO) { 956 while (my ($k, $v) = each %FACEINFO) {
839 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; 957 length $v->{data32} or warn "WARNING: face '$k' has no png32. this will not work (shoddy gcfclient will crash of course).\n";
840 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; 958 length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n";
841 959
842 make_hash $k, $v->{data32}, $v->{hash32}; 960 make_hash $k, $v->{data32}, $v->{hash32};
843 make_hash $k, $v->{data64}, $v->{hash64}; 961 make_hash $k, $v->{data64}, $v->{hash64};
844 962
845 #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; 963 #length $v->{data32} <= 10000 or warn "WARNING: face '$k' has face32 larger than 10000 bytes, will not work with crossfire client.\n";
846 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; 964 #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n";
847 965
848 if (my $magicmap = $v->{magicmap}) { 966 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
849 $magicmap =~ y/A-Z_\-/a-z/d; 967 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
850 $v->{magicmap} = $COLOR{$magicmap}; 968 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
851 }
852 969
853 my $stem = delete $v->{stem}; 970 delete @$v{qw(arc stem)}; # not used by the server
854 $v->{glyph} //= autoglyph $stem, $v;
855 $v->{glyph} = (chr $v->{magicmap}) . $v->{glyph};
856
857 delete $v->{arc};
858 } 971 }
859 972
860 print "processing resources...\n" if $VERBOSE; 973 print "processing resources...\n" if $VERBOSE;
861 my $enc = JSON::XS->new->utf8->canonical->relaxed; 974 my $enc = JSON::XS->new->utf8->canonical->relaxed;
862 while (my ($k, $v) = each %RESOURCE) { 975 while (my ($k, $v) = each %RESOURCE) {
901 version => 2, 1014 version => 2,
902 faceinfo => \%FACEINFO, 1015 faceinfo => \%FACEINFO,
903 animinfo => \%ANIMINFO, 1016 animinfo => \%ANIMINFO,
904 resource => \%RESOURCE, 1017 resource => \%RESOURCE,
905 }; 1018 };
906
907 } 1019 }
908 1020
909 print "committing files...\n" if $VERBOSE; 1021 print "committing files...\n" if $VERBOSE;
910 1022
911 for (qw(archetypes facedata treasures), @COMMIT) { 1023 for (qw(archetypes facedata treasures), @COMMIT) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines