ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/server/utils/cfutil.in
(Generate patch)

Comparing cf.schmorp.de/server/utils/cfutil.in (file contents):
Revision 1.54 by root, Sun Aug 19 10:28:52 2007 UTC vs.
Revision 1.112 by root, Fri Apr 22 02:15:45 2011 UTC

1#!@PERL@ 1#!@PERL@
2 2
3use strict; 3#
4# This file is part of Deliantra, the Roguelike Realtime MMORPG.
5#
6# Copyright (©) 2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
7#
8# Deliantra is free software: you can redistribute it and/or modify it under
9# the terms of the Affero GNU General Public License as published by the
10# Free Software Foundation, either version 3 of the License, or (at your
11# option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the Affero GNU General Public License
19# and the GNU General Public License along with this program. If not, see
20# <http://www.gnu.org/licenses/>.
21#
22# The authors can be reached via e-mail to <support@deliantra.net>
23#
24
25use common::sense;
4 26
5my $prefix = "@prefix@"; 27my $prefix = "@prefix@";
6my $exec_prefix = "@exec_prefix@"; 28my $exec_prefix = "@exec_prefix@";
7my $datarootdir = "@datarootdir@"; 29my $datarootdir = "@datarootdir@";
8my $DATADIR = "@datadir@/@PACKAGE@"; 30my $DATADIR = "@datadir@/@PACKAGE@";
9 31
10my $CONVERT = "@CONVERT@"; 32my $CONVERT = "@CONVERT@";
11#my $IDENTIFY = "@IDENTIFY@"; 33my $IDENTIFY = "@IDENTIFY@";
12my $OPTIPNG = "@OPTIPNG@"; 34my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@"; 35my $RSYNC = "@RSYNC@";
14my $PNGNQ = "@PNGNQ@"; 36my $PNGNQ = "@PNGNQ@";
15 37
16use Getopt::Long; 38use Getopt::Long;
17use Coro::Event; 39use File::Temp;
40use POSIX ();
41use Carp;
42
43use YAML::XS ();
44use Digest::MD5 ();
45use Storable ();
46use JSON::XS ();
47use IO::AIO ();
48use Compress::LZF ();
49
18use AnyEvent; 50use AnyEvent;
19use IO::AIO (); 51use Coro 5.12;
20use File::Temp;
21use Crossfire;
22use Coro; 52use Coro::EV;
23use Coro::AIO; 53use Coro::AIO;
24use Coro::Util; 54use Coro::Util;
25use POSIX ();
26use Digest::MD5;
27use Carp;
28use Coro::Channel; 55use Coro::Channel;
56use Coro::AnyEvent;
29use Coro::Storable; $Storable::canonical = 1; 57use Coro::Storable; $Storable::canonical = 1;
58
59use Deliantra;
30 60
31$SIG{QUIT} = sub { Carp::cluck "QUIT" }; 61$SIG{QUIT} = sub { Carp::cluck "QUIT" };
32 62
33sub usage { 63sub usage {
34 warn <<EOF; 64 warn <<EOF;
47my $VERBOSE = 1; 77my $VERBOSE = 1;
48my $CACHE = 0; 78my $CACHE = 0;
49my $FORCE; 79my $FORCE;
50my $TMPDIR = "/tmp/cfutil$$~"; 80my $TMPDIR = "/tmp/cfutil$$~";
51my $TMPFILE = "aaaa0"; 81my $TMPFILE = "aaaa0";
82my @COMMIT;
52 83
53our %COLOR = ( 84our %COLOR = (
85 # original cf colours
54 black => 0, 86 black => 0,
55 white => 1, 87 white => 1,
56 navy => 2, 88 navy => 2, # "dark blue"
57 red => 3, 89 red => 3,
58 orange => 4, 90 orange => 4,
59 blue => 5, 91 blue => 5, # "light blue"
60 darkorange => 6, 92 darkorange => 6,
61 green => 7, 93 green => 7,
62 lightgreen => 8, 94 lightgreen => 8,
63 grey => 9, 95 grey => 9,
96 brown => 10, # "yellow"
97 gold => 11, # "light yellow"
98 tan => 12, # yellowish gray
99
100 # new for deliantra
64 brown => 10, 101 none => 13,
102
103 # compatibility to existing archetypes
104 lightblue => 5,
65 gold => 11, 105 gray => 9,
106 yellow => 11,
66 tan => 12, 107 khaki => 12,
67); 108);
68 109
69END { system "rm", "-rf", $TMPDIR } 110END { system "rm", "-rf", $TMPDIR }
70 111
71Event->signal (signal => "INT", cb => sub { exit 1 }); 112my $s_INT = EV::signal INT => sub { exit 1 };
72Event->signal (signal => "TERM", cb => sub { exit 1 }); 113my $s_TERM = EV::signal TERM => sub { exit 1 };
114
115our %hash;
116
117# here we could try to avoid collisions and reduce chksum size further
118sub make_hash($\$\$;$) {
119 my ($id, $dataref, $hashref, $clen) = @_;
120
121 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4;
122
123 if (exists $hash{$hash}) {
124 # hash collision, but some files are simply identical
125 if (${$hash{$hash}[1]} ne $$dataref) {
126 warn "hash collision $hash{$hash}[0] vs. $id\n";
127 exit 1;
128 } else {
129 print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3;
130 }
131 }
132 $hash{$hash} = [$id, $dataref, $hashref];
133
134 $$hashref = $hash;
135}
73 136
74mkdir $TMPDIR, 0700 137mkdir $TMPDIR, 0700
75 or die "$TMPDIR: $!"; 138 or die "$TMPDIR: $!";
76 139
77sub fork_sub(&) { 140sub fork_exec(&) {
78 my ($cb) = @_; 141 my ($cb) = @_;
79 142
80 if (my $pid = fork) { 143 if (my $pid = fork) {
81 my $current = $Coro::current; 144 my $current = $Coro::current;
82 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); 145 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
87 warn $@; 150 warn $@;
88 POSIX::_exit 1; 151 POSIX::_exit 1;
89 } 152 }
90} 153}
91 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
92sub inst_maps($) { 184sub inst_maps($) {
93 my (undef, $path) = @_; 185 my (undef, $path) = @_;
94 186
95 print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; 187 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
96 188
97 if (!-f "$path/regions") { 189 if (!-f "$path/regions") {
98 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 190 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
99 exit 1 unless $FORCE; 191 exit 1 unless $FORCE;
100 } 192 }
101 193
102 system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" 194 system $RSYNC, "-av", "--chmod=u=rwX,go=rX",
195 "$path/.", "$DATADIR/maps/.",
196 "--exclude", "CVS", "--exclude", "/world-precomposed",
197 "--delete", "--delete-excluded"
103 and die "map installation failed.\n"; 198 and die "map installation failed.\n";
104 199
105 print "maps installed successfully.\n"; 200 print "maps installed successfully.\n";
106} 201}
107 202
116 our $PATH; 211 our $PATH;
117 212
118 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 213 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
119 214
120 our $c_arc = new Coro::Channel; 215 our $c_arc = new Coro::Channel;
121 our $c_trs = new Coro::Channel; 216 our $c_any = new Coro::Channel;
122 our $c_res = new Coro::Channel;
123 217
124 our @c_png; 218 our @c_png;
125 219
126 sub commit_png($$$) { 220 sub commit_png($$$$) {
127 my ($name, $data, $T) = @_; 221 my ($stem, $name, $data, $T) = @_;
128 222
223 $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH;
129 $FACEINFO{$name}{"data$T"} = $data; 224 $FACEINFO{$name}{"data$T"} = $data;
130 } 225 }
131 226
132 sub process_png { 227 sub process_png {
133 while (@c_png) { 228 while (@c_png) {
174 269
175 (my $base = $stem) =~ s/^.*\///; 270 (my $base = $stem) =~ s/^.*\///;
176 271
177 my $fi = $FACEINFO{$base}; 272 my $fi = $FACEINFO{$base};
178 unless ($fi) { 273 unless ($fi) {
179 warn "$path: <$base> not referenced by any archetype, skipping.\n"; 274 #warn "$path: <$base> not referenced by any archetype, skipping.\n";
180 next; 275 #next;
181 } 276 }
182 277
183 my $arc = $FACEINFO{$base}{arc} 278 my $arc = $fi->{arc} || { };
184 or die "FATAL: internal error, cannot continue";
185 279
186 unless ($path =~ /~$/) { 280 unless ($path =~ /~$/) {
187 # possibly enlarge 281 # possibly enlarge
188 if (0 > aio_stat "$stem.64x64.png") { 282 if (0 > aio_stat "$stem.64x64.png") {
189 my $other = "$stem.64x64.png~"; 283 my $other = "$stem.64x64.png~";
190 284
191 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 285 make_file $path, $other, sub {
192 fork_sub { 286 fork_exec {
193 my $CROP; 287 my $CROP;
194 my $SRC = "png:\Q$path\E"; 288 my $SRC = "png:\Q$path\E";
195 289
196 my $is_floor = $arc->{is_floor}; 290 my $is_floor = $arc->{is_floor};
197 my $is_wall = 0; 291 my $is_wall = 0;
199 my ($wall_pfx, $wall_dir, $wall_sfx); 293 my ($wall_pfx, $wall_dir, $wall_sfx);
200 294
201 if ( 295 if (
202 !$is_floor 296 !$is_floor
203 && !$arc->{alive} 297 && !$arc->{alive}
204 && $arc->{no_pass} 298 && $arc->{move_block} eq "all"
205 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/ 299 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/
206 ) { 300 ) {
207 ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3); 301 ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3);
208 302
209 unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) { 303 unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) {
255 and die "convert/cfhq2xa pipeline error: status $? ($!)"; 349 and die "convert/cfhq2xa pipeline error: status $? ($!)";
256 system $OPTIPNG, "-i0", "-q", "$other~"; 350 system $OPTIPNG, "-i0", "-q", "$other~";
257 die "$other~ has zero size, aborting." unless -s "$other~"; 351 die "$other~ has zero size, aborting." unless -s "$other~";
258 rename "$other~", $other; 352 rename "$other~", $other;
259 }; 353 };
260 } 354 };
261 355
262 push @c_png, [$other, !$CACHE]; 356 push @c_png, [$other, !$CACHE];
263 } 357 }
264 358
265 # possibly scale down 359 # possibly scale down
266 if (0 > aio_stat "$stem.32x32.png") { 360 if (0 > aio_stat "$stem.32x32.png") {
267 my $other = "$stem.32x32.png~"; 361 my $other = "$stem.32x32.png~";
268 362
269 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 363 make_file $path, $other, sub {
270 fork_sub { 364 fork_exec {
271 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~";
272 system $OPTIPNG, "-i0", "-q", "$other~"; 366 system $OPTIPNG, "-i0", "-q", "$other~";
273 367
274 # reduce smoothfaces >10000 bytes 368 # reduce smoothfaces >10000 bytes
369 # obsolete, no longer required
275 if ($stem =~ /_S\./ && (-s "$other~") > 10000) { 370 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
276 my $ncolor = 256; 371 my $ncolor = 256;
277 while () { 372 while () {
278 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; 373 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
279 system $OPTIPNG, "-i0", "-q", "$other~~"; 374 system $OPTIPNG, "-i0", "-q", "$other~~";
280 last if 10000 > -s "$other~~"; 375 last if 10000 > -s "$other~~";
289 } 384 }
290 385
291 die "$other~ has zero size, aborting." unless -s "$other~"; 386 die "$other~ has zero size, aborting." unless -s "$other~";
292 rename "$other~", $other; 387 rename "$other~", $other;
293 }; 388 };
294 } 389 };
295 390
296 #warn "scaled down $path to $other\n";#d# 391 #warn "scaled down $path to $other\n";#d#
297 push @c_png, [$other, !$CACHE]; 392 push @c_png, [$other, !$CACHE];
298 } 393 }
299 } 394 }
313 } 408 }
314 409
315 my $mtime = (lstat $path)[9]; 410 my $mtime = (lstat $path)[9];
316 my @todo = grep { $_->[3] <= $mtime } @tile; 411 my @todo = grep { $_->[3] <= $mtime } @tile;
317 if (@todo) { 412 if (@todo) {
318 fork_sub { 413 fork_exec {
319 open my $convert, "|-", $CONVERT, 414 open my $convert, "|-", $CONVERT,
320 "png:-", 415 "png:-",
321 (map { 416 (map {
322 ( 417 (
323 "(", 418 "(",
351 446
352 if (0 > aio_load $file, $tile) { 447 if (0 > aio_load $file, $tile) {
353 die "$path: unable to read tile +$x+$y, aborting.\n"; 448 die "$path: unable to read tile +$x+$y, aborting.\n";
354 } 449 }
355 IO::AIO::aio_unlink $file unless $CACHE; 450 IO::AIO::aio_unlink $file unless $CACHE;
356 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; 451 commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T;
357 } 452 }
358 } else { 453 } else {
359 # use as-is (either small, use smooth) 454 # use as-is (either small, use smooth)
360 commit_png $face, $png, $T; 455 commit_png $stem, $face, $png, $T;
361 } 456 }
362 457
363 aio_unlink $path if $delete; 458 aio_unlink $path if $delete;
364 } 459 }
365 } 460 }
366 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
367 sub process_arc { 503 sub process_arc {
368 while (my $job = $c_arc->get) { 504 while (my ($dir, $file) = @{ $c_arc->get }) {
369 my ($dir, $file) = @$job;
370
371 my $arc; 505 my $arc;
372 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 :/
373 507
374 my $arc = read_arch "$dir/$file"; 508 my $arc = read_arch "$dir/$file";
375 for my $o (values %$arc) { 509 for my $o (values %$arc) {
376 push @ARC, $o; 510 push @ARC, $o;
377 for (my $m = $o; $m; $m = $m->{more}) { 511 for (my $m = $o; $m; $m = $m->{more}) {
378 $ARC{$m->{_name}} = $m; 512 $ARC{$m->{_name}} = $m;
379 } 513 }
380 514
381 $o->{editor_folder} = $dir; 515 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
382
383 my $visibility = delete $o->{visibility};
384 my $magicmap = delete $o->{magicmap};
385 516
386 # find upper left corner :/ 517 # find upper left corner :/
387 # omg, this is sooo broken 518 # omg, this is sooo broken
388 my ($dx, $dy); 519 my ($dx, $dy);
389 for (my $o = $o; $o; $o = $o->{more}) { 520 for (my $o = $o; $o; $o = $o->{more}) {
396 my $y = $o->{y} - $dy; 527 my $y = $o->{y} - $dy;
397 528
398 my $ext = $x|$y ? "+$x+$y" : ""; 529 my $ext = $x|$y ? "+$x+$y" : "";
399 530
400 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; 531 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
401
402 $visibility = delete $o->{visibility} if exists $o->{visibility};
403 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
404 532
405 my $anim = delete $o->{anim}; 533 my $anim = delete $o->{anim};
406 534
407 if ($anim) { 535 if ($anim) {
408 # possibly add $ext to the animation name to avoid 536 # possibly add $ext to the animation name to avoid
426 facings => $facings, 554 facings => $facings,
427 frames => \@frames, 555 frames => \@frames,
428 }; 556 };
429 } 557 }
430 558
431 for my $face ($o->{face} || (), @{$anim || []}) { 559 for ($o->{face} || (), @{$anim || []}) {
432 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; 560 next if /^facings\s/;
433 561
562 my $face = $_;
563 $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates
564
434 my $info = $FACEINFO{$face} ||= {}; 565 my $info = $FACEINFO{$face} ||= { };
435
436 $info->{arc} = $o; 566 $info->{arc} = $o;
437 $info->{visibility} = $visibility if defined $visibility; 567
438 $info->{magicmap} = $magicmap if defined $magicmap; 568 next if $face =~ /^blank.x11$|^empty.x11$/;
439 } 569 }
440 570
441 if (my $smooth = delete $o->{smoothface}) { 571 if (my $smooth = delete $o->{smoothface}) {
442 my %kv = split /\s+/, $smooth; 572 my %kv = split /\s+/, $smooth;
443 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 573 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
452 } 582 }
453 } 583 }
454 } 584 }
455 585
456 sub process_trs { 586 sub process_trs {
457 while (my $job = $c_trs->get) {
458 my ($dir, $file) = @$job; 587 my ($dir, $file) = @_;
459 my $path = "$dir/$file"; 588 my $path = "$dir/$file";
460 589
461 my $trs; 590 my $trs;
462 if (0 > aio_load $path, $trs) { 591 if (0 > aio_load $path, $trs) {
463 warn "$path: $!, skipping.\n"; 592 warn "$path: $!, skipping.\n";
464 next; 593 return;
465 } 594 }
466 595
467 $TRS .= $trs; 596 $TRS .= $trs;
468 }
469 } 597 }
470 598
471 my %FILECACHE; 599 my %FILECACHE;
472 600
473 sub load_cached($;$) { 601 sub load_cached($;$) {
484 } 612 }
485 613
486 $FILECACHE{$_[0]} 614 $FILECACHE{$_[0]}
487 } 615 }
488 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
489 sub process_res { 685 sub process_res {
490 while (my $job = $c_res->get) {
491 my ($dir, $file, $type) = @$job; 686 my ($dir, $file, $type) = @_;
492 687
493 my $data;
494 aio_load "$dir/$file", $data; 688 0 <= aio_load "$dir/$file", my $data
689 or die "$dir/$file: $!";
495 690
496 my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift }; 691 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
497 692
498 next if $meta && !exists $meta->{$file}; 693 utf8::decode $dir;
694 utf8::decode $file;
499 695
696 # a meta file for resources is now mandatory
697 unless (exists $meta->{$file}) {
698 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
699 return;
700 }
701
500 $meta = { 702 $meta = {
501 %{ $meta->{"" } || {} }, 703 %{ $meta->{"" } || {} },
502 %{ $meta->{$file} || {} }, 704 %{ $meta->{$file} || {} },
503 }; 705 };
504 706
505 if ($meta->{license} =~ s/^#//) { 707 if ($meta->{license} =~ s/^#//) {
506 $meta->{license} = ({ 708 $meta->{license} = ({
507 "pd" => "Public Domain", 709 "pd" => "Public Domain",
508 "gpl" => "GNU General Public License, version 3.0 or any later", 710 "gpl" => "GNU General Public License, version 3.0 or any later",
509 "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/",
712 "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/",
510 "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/",
511 "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/",
512 })->{$meta->{license}} 715 })->{$meta->{license}}
513 || warn "$dir/$file: license tag '$meta->{license}' not found."; 716 || warn "$dir/$file: license tag '$meta->{license}' not found.";
514 } 717 }
515 718
719 if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) {
720 ($meta->{author} = $1) =~ s/_/ /g;
721 }
722
516 $file =~ s/\.res$//; 723 $file =~ s/\.res$//;
517 $file =~ s/\.(ogg|wav|jpg|png)$//; 724 $file =~ s/\.(ogg|wav|jpg|png)$//;
518 725
726 if ($file =~ s/\.plt$//) {
727 $data = process_plt "$dir/$file", $data;
728 } elsif (my $filter = $meta->{cfutil_filter}) {
729 if ($filter eq "yaml2json") {
730 $data = JSON::XS::encode_json YAML::XS::Load $data;
731 } elsif ($filter eq "json2json") {
732 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
733 } elsif ($filter eq "perl2json") {
734 $data = eval $data; die if $@;
735 $data = JSON::XS::encode_json $data;
736 } else {
737 warn "$dir/$file: unknown filter $filter, skipping\n";
738 }
739 }
740
519 substr $dir, 0, 1 + length $PATH, ""; 741 substr $dir, 0, 1 + length $PATH, "";
520 742
521 $RESOURCE{"$dir/$file"} = { 743 $RESOURCE{"$dir/$file"} = {
522 type => (delete $meta->{type}) || $type, 744 type => (exists $meta->{type} ? delete $meta->{type} : $type),
523 data => $data, 745 data => $data,
524 chksum => (Digest::MD5::md5 $data),
525 %$meta ? (meta => $meta) : (), 746 %$meta ? (meta => $meta) : (),
526 }; 747 };
748 }
749
750 sub process_any {
751 while (my ($func, @args) = @{ $c_any->get }) {
752 $func->(@args);
527 } 753 }
528 } 754 }
529 755
530 sub find_files; 756 sub find_files;
531 sub find_files { 757 sub find_files {
532 my ($path) = @_; 758 my ($path) = @_;
533 759
760 my $grp = IO::AIO::aio_group;
761
762 my $scandir; $scandir = sub {
763 my ($path) = @_;
764
534 IO::AIO::aioreq_pri 4; 765 IO::AIO::aioreq_pri 4;
535 IO::AIO::aio_scandir $path, 4, sub { 766 add $grp IO::AIO::aio_scandir $path, 4, sub {
536 my ($dirs, $nondirs) = @_; 767 my ($dirs, $nondirs) = @_;
537 768
538 find_files "$path/$_" 769 $scandir->("$path/$_")
539 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 770 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
540 771
541 my $dir = $path; 772 my $dir = $path;
542 substr $dir, 0, 1 + length $PATH, ""; 773 substr $dir, 0, 1 + length $PATH, "";
543 774
544 for my $file (@$nondirs) { 775 for my $file (@$nondirs) {
545 if ($dir =~ /^music(?:\/|$)/) { 776 if ($dir =~ /^music(?:\/|$)/) {
546 $c_res->put ([$path, $file, 3]) # FT_MUSIC 777 $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC
547 if $file =~ /\.(ogg)$/; 778 if $file =~ /\.(ogg)$/;
548 779
549 } elsif ($dir =~ /^sound(?:\/|$)/) { 780 } elsif ($dir =~ /^sound(?:\/|$)/) {
550 $c_res->put ([$path, $file, 5]) # FT_SOUND 781 $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND
551 if $file =~ /\.(wav|ogg)$/; 782 if $file =~ /\.(wav|ogg)$/;
552 783
553 } elsif ($dir =~ /^res(?:\/|$)/) { 784 } elsif ($dir =~ /^res(?:\/|$)/) {
554 $c_res->put ([$path, $file, 0]) # FT_FACE
555 if $file =~ /\.(jpg|png)$/; 785 if ($file =~ /\.(jpg|png)$/) {
556 $c_res->put ([$path, $file, 7]) # FT_RSRC 786 $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE
557 if $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 }
558 792
559 } elsif ($file =~ /\.png$/) { 793 } elsif ($file =~ /\.png$/) {
560 push @c_png, ["$path/$file", 0]; 794 push @c_png, ["$path/$file", 0];
561 795
796 } elsif ($file =~ /\.faceinfo$/) {
797 $c_any->put ([\&process_faceinfo, $path, $file]);
798
562 } elsif ($file =~ /\.trs$/) { 799 } elsif ($file =~ /\.trs$/) {
563 $c_trs->put ([$path, $file]); 800 $c_any->put ([\&process_trs, $path, $file]);
564 801
565 } elsif ($file =~ /\.arc$/) { 802 } elsif ($file =~ /\.arc$/) {
566 $c_arc->put ([$path, $file]); 803 $c_arc->put ([$path, $file]);
567 804
568 } else { 805 } else {
569 warn "ignoring $path/$file\n" if $VERBOSE >= 3; 806 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
570 } 807 }
808 }
571 } 809 };
572 }; 810 };
811
812 $scandir->($path);
813 aio_wait $grp;
814 }
815
816 sub generate_plurals {
817# use Lingua::EN::Inflect ();
818# Lingua::EN::Inflect::classical;
819# Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy
820# Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack
821#
822# for my $a (@ARC) {
823# my $name = $a->{name} || $a->{_name};
824#
825# next unless $a->{name_pl};
826# next if $a->{invisible};
827# next if $a->{is_floor};
828# next if $a->{no_pick};
829#
830# my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name;
831# my $pl = $test =~ /^(?:eq|p:.)$/
832# ? $name
833# : Lingua::EN::Inflect::PL $name;
834#
835# if ($pl ne $a->{name_pl}) {
836# warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n";
837# }
838# }
573 } 839 }
574 840
575 sub inst_arch($) { 841 sub inst_arch($) {
576 my (undef, $path) = @_; 842 my (undef, $path) = @_;
577 843
590 if (!-d "$path/treasures") { 856 if (!-d "$path/treasures") {
591 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";
592 exit 1 unless $FORCE; 858 exit 1 unless $FORCE;
593 } 859 }
594 860
595 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;
596 865
597 find_files $path; 866 find_files $path;
598 867
599 my @a_arc = map +(async \&process_arc), 1..2; 868 $c_arc->shutdown;
600 my @a_res = map +(async \&process_res), 1..2; 869 $c_any->shutdown;
601 my @a_trs = map +(async \&process_trs), 1..2;
602
603 IO::AIO::flush;
604
605 $c_res->put (undef) for @a_res;
606 $c_arc->put (undef) for @a_arc;
607 $c_trs->put (undef) for @a_trs;
608
609 print "start file scan, arc, res processing...\n" if $VERBOSE;
610 870
611 $_->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
612 872
613 print "end arc, start png processing...\n" if $VERBOSE; 873 print "end arc, start png processing...\n" if $VERBOSE;
614 874
615 # four png crunchers work fine for my 2x smp machine 875 # eight png crunchers work fine for my 4x smp machine
616 my @a_png = map +(async \&process_png), 1..4; 876 my @a_png = map +(async \&process_png), 1..8;
617 877
618 $_->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;
619 883
620 print "scanning done, processing results...\n" if $VERBOSE; 884 print "scanning done, processing results...\n" if $VERBOSE;
621 { 885 {
622 # remove path prefix from editor_folder 886 # remove path prefix from editor_folder
887 $_->{editor_folder} =~ /^\x00/
623 substr $_->{editor_folder}, 0, 1 + length $path, "" 888 and substr $_->{editor_folder}, 0, 2 + length $path, ""
624 for values %ARC; 889 for values %ARC;
625 890
626 print "resolving inheritance tree...\n" if $VERBOSE; 891 print "resolving inheritance tree...\n" if $VERBOSE;
627 # resolve inherit 892 # resolve inherit
628 while () { 893 while () {
629 my $progress; 894 my $progress;
630 my $loop; 895 my $loop;
631 896
632 for my $o (values %ARC) { 897 for my $o (values %ARC) {
633 if (my $other = $o->{inherit}) { 898 for my $other (split /,/, $o->{inherit}) {
634 if (my $s = $ARC{$other}) { 899 if (my $s = $ARC{$other}) {
635 if ($s->{inherit}) { 900 if ($s->{inherit}) {
636 $loop = $s; 901 $loop = $s;
637 } else { 902 } else {
638 delete $o->{inherit}; 903 delete $o->{inherit};
640 delete @s{qw(_name more name name_pl)}; 905 delete @s{qw(_name more name name_pl)};
641 %$o = ( %s, %$o ); 906 %$o = ( %s, %$o );
642 ++$progress; 907 ++$progress;
643 } 908 }
644 } else { 909 } else {
645 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";
646 delete $ARC{$o->{_name}}; 911 delete $ARC{$o->{_name}};
647 } 912 }
648 } 913 }
649 } 914 }
650 915
657 } 922 }
658 923
659 # 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
660 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 925 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
661 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
938 #print "generating plurals...\n" if $VERBOSE;
939 #generate_plurals;
940
662 print "writing archetypes...\n" if $VERBOSE; 941 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
663 open my $fh, ">:utf8", "$DATADIR/archetypes~" 942 open my $fh, ">:utf8", "$DATADIR/archetypes~"
664 or die "$DATADIR/archetypes~: $!"; 943 or die "$DATADIR/archetypes~: $!";
665 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 944 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
666 } 945 }
667 946
668 { 947 {
669 print "writing treasures...\n" if $VERBOSE; 948 printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE;
670 open my $fh, ">:utf8", "$DATADIR/treasures~" 949 open my $fh, ">:utf8", "$DATADIR/treasures~"
671 or die "$DATADIR/treasures~: $!"; 950 or die "$DATADIR/treasures~: $!";
672 print $fh $TRS; 951 print $fh $TRS;
673 } 952 }
674 953
675 { 954 {
676 print "processing facedata...\n" if $VERBOSE; 955 print "processing facedata...\n" if $VERBOSE;
677 while (my ($k, $v) = each %FACEINFO) { 956 while (my ($k, $v) = each %FACEINFO) {
678 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";
679 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";
680 959
960 make_hash $k, $v->{data32}, $v->{hash32};
961 make_hash $k, $v->{data64}, $v->{hash64};
962
681 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";
682 #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";
683 965
684 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 966 $v->{glyph} // warn "WARNING: face '$k' has glyph.";
685 $v->{chksum64} = Digest::MD5::md5 $v->{data64}; 967 $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n";
968 $v->{magicmap} // warn "WARNING: face '$k' has foreground colour.";
686 969
687 if (my $magicmap = $v->{magicmap}) { 970 delete @$v{qw(arc stem)}; # not used by the server
688 $magicmap =~ y/A-Z_\-/a-z/d; 971 }
689 $v->{magicmap} = $COLOR{$magicmap}; 972
973 print "processing resources...\n" if $VERBOSE;
974 my $enc = JSON::XS->new->utf8->canonical->relaxed;
975 while (my ($k, $v) = each %RESOURCE) {
976
977 if ($v->{meta} && $v->{meta}{datadir}) {
978 delete $RESOURCE{$k};
979
980 $k =~ s/^res\/// or die "$k: datadir files must be in res/";
981
982 printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE;
983 open my $fh, ">:raw", "$DATADIR/$k~"
984 or die "$DATADIR/$k~: $!";
985 syswrite $fh, $v->{data};
986 push @COMMIT, $k;
987
988 } else {
989 if ($v->{type} & 1) {
990 # prepend meta info
991
992 my $meta = $enc->encode ({
993 name => $k,
994 %{ $v->{meta} || {} },
995 });
996
997 $v->{data} = pack "(w/a*)*", $meta, $v->{data};
690 } 998 }
691 999
692 delete $v->{arc}; 1000 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
693 } 1001 }
1002 }
694 1003
695 print "writing facedata...\n" if $VERBOSE; 1004 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
1005 scalar keys %FACEINFO,
1006 scalar keys %ANIMINFO,
1007 scalar keys %RESOURCE
1008 if $VERBOSE;
1009
696 open my $fh, ">:perlio", "$DATADIR/facedata~" 1010 open my $fh, ">:perlio", "$DATADIR/facedata~"
697 or die "$DATADIR/facedata~: $!"; 1011 or die "$DATADIR/facedata~: $!";
698 1012
699 print $fh freeze { 1013 print $fh nfreeze {
700 version => 2, 1014 version => 2,
701 faceinfo => \%FACEINFO, 1015 faceinfo => \%FACEINFO,
702 animinfo => \%ANIMINFO, 1016 animinfo => \%ANIMINFO,
703 resource => \%RESOURCE, 1017 resource => \%RESOURCE,
704 }; 1018 };
705 } 1019 }
706 1020
707 print "committing files...\n" if $VERBOSE; 1021 print "committing files...\n" if $VERBOSE;
708 1022
709 for (qw(archetypes facedata treasures)) { 1023 for (qw(archetypes facedata treasures), @COMMIT) {
710 chmod 0644, "$DATADIR/$_~"; 1024 chmod 0644, "$DATADIR/$_~";
711 rename "$DATADIR/$_~", "$DATADIR/$_" 1025 rename "$DATADIR/$_~", "$DATADIR/$_"
712 or die "$DATADIR/$_: $!"; 1026 or die "$DATADIR/$_: $!";
713 } 1027 }
714 1028

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines