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.42 by root, Thu Jul 19 13:46:39 2007 UTC vs.
Revision 1.83 by root, Wed Apr 28 20:51:58 2010 UTC

1#!@PERL@ 1#!@PERL@
2
3#
4# This file is part of Deliantra, the Roguelike Realtime MMORPG.
5#
6# Copyright (©) 2007,2008,2009 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#
2 24
3use strict; 25use strict;
4 26
5my $prefix = "@prefix@"; 27my $prefix = "@prefix@";
6my $exec_prefix = "@exec_prefix@"; 28my $exec_prefix = "@exec_prefix@";
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;
39use File::Temp;
40use POSIX ();
41use Carp;
42
17use Coro::Event; 43use Coro::EV;
18use AnyEvent; 44use AnyEvent;
45use YAML::XS ();
46use JSON::XS ();
19use IO::AIO (); 47use IO::AIO ();
20use File::Temp; 48use Digest::MD5 ();
21use Crossfire; 49
22use Coro; 50use Coro 5.12;
23use Coro::AIO; 51use Coro::AIO;
24use POSIX (); 52use Coro::Util;
25use Digest::MD5; 53use Coro::Channel;
26use Carp;
27use Coro::Storable; $Storable::canonical = 1; 54use Coro::Storable; $Storable::canonical = 1;
55
56use Deliantra;
28 57
29$SIG{QUIT} = sub { Carp::cluck "QUIT" }; 58$SIG{QUIT} = sub { Carp::cluck "QUIT" };
30 59
31sub usage { 60sub usage {
32 warn <<EOF; 61 warn <<EOF;
45my $VERBOSE = 1; 74my $VERBOSE = 1;
46my $CACHE = 0; 75my $CACHE = 0;
47my $FORCE; 76my $FORCE;
48my $TMPDIR = "/tmp/cfutil$$~"; 77my $TMPDIR = "/tmp/cfutil$$~";
49my $TMPFILE = "aaaa0"; 78my $TMPFILE = "aaaa0";
79my @COMMIT;
50 80
51our %COLOR = ( 81our %COLOR = (
52 black => 0, 82 black => 0,
53 white => 1, 83 white => 1,
54 navy => 2, 84 navy => 2,
64 tan => 12, 94 tan => 12,
65); 95);
66 96
67END { system "rm", "-rf", $TMPDIR } 97END { system "rm", "-rf", $TMPDIR }
68 98
69Event->signal (signal => "INT", cb => sub { exit 1 }); 99my $s_INT = EV::signal INT => sub { exit 1 };
70Event->signal (signal => "TERM", cb => sub { exit 1 }); 100my $s_TERM = EV::signal TERM => sub { exit 1 };
101
102our %hash;
103
104# here we could try to avoid collisions and reduce chksum size further
105sub make_hash($\$\$;$) {
106 my ($id, $dataref, $hashref, $clen) = @_;
107
108 my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4;
109
110 if (exists $hash{$hash}) {
111 # hahs collinion, but some files are simply identical
112 if (${$hash{$hash}[1]} ne $$dataref) {
113 warn "hash collision $hash{$hash}[0] vs. $id\n";
114 exit 1;
115 } else {
116 print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3;
117 }
118 }
119 $hash{$hash} = [$id, $dataref, $hashref];
120
121 $$hashref = $hash;
122}
71 123
72mkdir $TMPDIR, 0700 124mkdir $TMPDIR, 0700
73 or die "$TMPDIR: $!"; 125 or die "$TMPDIR: $!";
74 126
75sub fork_sub(&) { 127sub fork_sub(&) {
95 if (!-f "$path/regions") { 147 if (!-f "$path/regions") {
96 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 148 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
97 exit 1 unless $FORCE; 149 exit 1 unless $FORCE;
98 } 150 }
99 151
100 system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" 152 system $RSYNC, "-av", "--chmod=u=rwX,go=rX",
153 "$path/.", "$DATADIR/maps/.",
154 "--exclude", "CVS", "--exclude", "/world-precomposed",
155 "--delete", "--delete-excluded"
101 and die "map installation failed.\n"; 156 and die "map installation failed.\n";
102 157
103 print "maps installed successfully.\n"; 158 print "maps installed successfully.\n";
104} 159}
105 160
113 our $NFILE; 168 our $NFILE;
114 our $PATH; 169 our $PATH;
115 170
116 our $QUANTIZE = "+dither -colorspace RGB -colors 256"; 171 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
117 172
118 our (@png, @trs, @arc, @res); # files we are interested in 173 our $c_arc = new Coro::Channel;
174 our $c_trs = new Coro::Channel;
175 our $c_res = new Coro::Channel;
176
177 our @c_png;
119 178
120 sub commit_png($$$) { 179 sub commit_png($$$) {
121 my ($name, $data, $T) = @_; 180 my ($name, $data, $T) = @_;
122 181
123 $FACEINFO{$name}{"data$T"} = $data; 182 $FACEINFO{$name}{"data$T"} = $data;
124 } 183 }
125 184
126 sub process_png { 185 sub process_png {
127 while (@png) { 186 while (@c_png) {
128 my ($path, $delete) = @{pop @png}; 187 my ($path, $delete) = @{pop @c_png};
129 188
130 my $png; 189 my $png;
131 aio_lstat $path; 190 aio_lstat $path;
132 my ($size, $mtime) = (stat _)[7,9]; 191 my ($size, $mtime) = (stat _)[7,9];
133 192
164 if ($w % $T || $h % $T) { 223 if ($w % $T || $h % $T) {
165 warn "$path: weird png size ($w $h), skipping.\n"; 224 warn "$path: weird png size ($w $h), skipping.\n";
166 next; 225 next;
167 } 226 }
168 227
228 (my $base = $stem) =~ s/^.*\///;
229
230 my $fi = $FACEINFO{$base};
231 unless ($fi) {
232 #warn "$path: <$base> not referenced by any archetype, skipping.\n";
233 #next;
234 }
235
236 my $arc = $fi->{arc} || { };
237
169 unless ($path =~ /~$/) { 238 unless ($path =~ /~$/) {
170 # possibly enlarge 239 # possibly enlarge
171 if (0 > aio_stat "$stem.64x64.png") { 240 if (0 > aio_stat "$stem.64x64.png") {
172 my $other = "$stem.64x64.png~"; 241 my $other = "$stem.64x64.png~";
173 242
174 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 243 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
175 my $wrap = 0; # for the time being
176 fork_sub { 244 fork_sub {
245 my $CROP;
246 my $SRC = "png:\Q$path\E";
247
248 my $is_floor = $arc->{is_floor};
249 my $is_wall = 0;
250
251 my ($wall_pfx, $wall_dir, $wall_sfx);
252
253 if (
254 !$is_floor
255 && !$arc->{alive}
256 && $arc->{move_block} eq "all"
257 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/
258 ) {
259 ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3);
260
261 unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) {
262 $is_wall = 1;
263 }
264 }
265
266 if ($is_wall || $is_floor) {
267 # add a 4px border and add other images around it
268 $CROP = "-shave 8x8 +repage";
269
270 $w += 8;
271 $h += 8;
272
273 $SRC = "-size ${w}x${h} xc:transparent";
274 $SRC .= " png:\Q$path\E -geometry +4+4 -composite";
275
276 # 8 surrounding images
277 for (
278 # x y b r0 r1
279 [-1, -1, 0, 6],
280 [ 0, -1, 1, 10, 14],
281 [+1, -1, 0, 12],
282
283 [-1, 0, 8, 5, 7],
284 #
285 [+1, 0, 2, 5, 13],
286
287 [-1, +1, 0, 3],
288 [ 0, +1, 4, 10, 11],
289 [+1, +1, 0, 9],
290 ) {
291 my ($x, $y, $d, $r0, $r1) = @$_;
292
293 my $tile = $is_floor ? $path
294 : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx
295 : die;
296
297 $SRC .= sprintf " png:%s -geometry %+d%+d -composite",
298 "\Q$tile",
299 $x * ($w - 8) + 4,
300 $y * ($h - 8) + 4;
301 }
302 }
303
177 system "convert png:\Q$path\E -depth 8 rgba:-" 304 system "convert -depth 8 $SRC rgba:-"
178 . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" 305 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
179 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" 306 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~"
180 and die "convert/hq2xa pipeline error: status $? ($!)"; 307 and die "convert/cfhq2xa pipeline error: status $? ($!)";
181 system $OPTIPNG, "-i0", "-q", "$other~"; 308 system $OPTIPNG, "-i0", "-q", "$other~";
182 die "$other~ has zero size, aborting." unless -s "$other~"; 309 die "$other~ has zero size, aborting." unless -s "$other~";
183 rename "$other~", $other; 310 rename "$other~", $other;
184 }; 311 };
185 } 312 }
186 313
187 push @png, [$other, !$CACHE]; 314 push @c_png, [$other, !$CACHE];
188 } 315 }
189 316
190 # possibly scale down 317 # possibly scale down
191 if (0 > aio_stat "$stem.32x32.png") { 318 if (0 > aio_stat "$stem.32x32.png") {
192 my $other = "$stem.32x32.png~"; 319 my $other = "$stem.32x32.png~";
195 fork_sub { 322 fork_sub {
196 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; 323 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
197 system $OPTIPNG, "-i0", "-q", "$other~"; 324 system $OPTIPNG, "-i0", "-q", "$other~";
198 325
199 # reduce smoothfaces >10000 bytes 326 # reduce smoothfaces >10000 bytes
327 # obsolete, no longer required
200 if ($stem =~ /_S\./ && (-s "$other~") > 10000) { 328 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
201 my $ncolor = 256; 329 my $ncolor = 256;
202 while () { 330 while () {
203 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; 331 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
204 system $OPTIPNG, "-i0", "-q", "$other~~"; 332 system $OPTIPNG, "-i0", "-q", "$other~~";
205 last if 10000 > -s "$other~~"; 333 last if 10000 > -s "$other~~";
217 rename "$other~", $other; 345 rename "$other~", $other;
218 }; 346 };
219 } 347 }
220 348
221 #warn "scaled down $path to $other\n";#d# 349 #warn "scaled down $path to $other\n";#d#
222 push @png, [$other, !$CACHE]; 350 push @c_png, [$other, !$CACHE];
223 } 351 }
224 } 352 }
225 353
226 (my $face = $stem) =~ s/^.*\///; 354 (my $face = $stem) =~ s/^.*\///;
227 355
288 aio_unlink $path if $delete; 416 aio_unlink $path if $delete;
289 } 417 }
290 } 418 }
291 419
292 sub process_arc { 420 sub process_arc {
293 while (@arc) { 421 while (my $job = $c_arc->get) {
294 my ($dir, $file) = @{pop @arc}; 422 my ($dir, $file) = @$job;
295 423
296 my $arc; 424 my $arc;
297 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ 425 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
298 426
299 my $arc = read_arch "$dir/$file"; 427 my $arc = read_arch "$dir/$file";
301 push @ARC, $o; 429 push @ARC, $o;
302 for (my $m = $o; $m; $m = $m->{more}) { 430 for (my $m = $o; $m; $m = $m->{more}) {
303 $ARC{$m->{_name}} = $m; 431 $ARC{$m->{_name}} = $m;
304 } 432 }
305 433
306 $o->{editor_folder} = $dir; 434 $o->{editor_folder} ||= "\x00$dir"; # horrible kludge
307 435
308 my $visibility = delete $o->{visibility}; 436 my $visibility = delete $o->{visibility};
309 my $magicmap = delete $o->{magicmap}; 437 my $magicmap = delete $o->{magicmap};
310 438
311 # find upper left corner :/ 439 # find upper left corner :/
351 facings => $facings, 479 facings => $facings,
352 frames => \@frames, 480 frames => \@frames,
353 }; 481 };
354 } 482 }
355 483
356 for my $face ($o->{face} || (), @{$anim || []}) { 484 for ($o->{face} || (), @{$anim || []}) {
357 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; 485 next if /^facings\s/;
358 486
487 my $face = $_;
488 $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates
489
359 my $info = $FACEINFO{$face} ||= {}; 490 my $info = $FACEINFO{$face} ||= { };
491 $info->{arc} = $o;
492
493 next if $face =~ /^blank.x11$|^empty.x11$/;
360 494
361 $info->{visibility} = $visibility if defined $visibility; 495 $info->{visibility} = $visibility if defined $visibility;
362 $info->{magicmap} = $magicmap if defined $magicmap; 496 $info->{magicmap} = $magicmap if defined $magicmap;
363 } 497 }
364 498
365 if (my $smooth = delete $o->{smoothface}) { 499 if (my $smooth = delete $o->{smoothface}) {
366 my %kv =split /\s+/, $smooth; 500 my %kv = split /\s+/, $smooth;
367 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 501 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
368 while (my ($face, $smooth) = each %kv) { 502 while (my ($face, $smooth) = each %kv) {
503 $FACEINFO{$smooth}{arc} = $o;
504
369 $FACEINFO{$face}{smooth} = $smooth; 505 $FACEINFO{$face}{smooth} = $smooth;
370 $FACEINFO{$face}{smoothlevel} = $level; 506 $FACEINFO{$face}{smoothlevel} = $level;
371 } 507 }
372 } 508 }
373 } 509 }
374 } 510 }
375 } 511 }
376 } 512 }
377 513
378 sub process_trs { 514 sub process_trs {
379 while (@trs) { 515 while (my $job = $c_trs->get) {
380 my ($dir, $file) = @{pop @trs}; 516 my ($dir, $file) = @$job;
381 my $path = "$dir/$file"; 517 my $path = "$dir/$file";
382 518
383 my $trs; 519 my $trs;
384 if (0 > aio_load $path, $trs) { 520 if (0 > aio_load $path, $trs) {
385 warn "$path: $!, skipping.\n"; 521 warn "$path: $!, skipping.\n";
388 524
389 $TRS .= $trs; 525 $TRS .= $trs;
390 } 526 }
391 } 527 }
392 528
529 my %FILECACHE;
530
531 sub load_cached($;$) {
532 unless (exists $FILECACHE{$_[0]}) {
533 my $data;
534 if (0 < aio_load $_[0], $data) {
535 if ($_[1]) {
536 $data = eval { $_[1]->($data) };
537 warn "$_[0]: $@" if $@;
538 }
539 }
540
541 $FILECACHE{$_[0]} = $data;
542 }
543
544 $FILECACHE{$_[0]}
545 }
546
393 sub process_res { 547 sub process_res {
394 while (@res) { 548 while (my $job = $c_res->get) {
395 my ($dir, $file) = @{pop @res}; 549 my ($dir, $file, $type) = @$job;
396 550
397 my $data; 551 my $data;
398 aio_load "$dir/$file", $data; 552 aio_load "$dir/$file", $data;
399 553
400 my $copyright; 554 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
401 aio_load "$dir/copyright", $copyright; 555
556 utf8::decode $dir;
557 utf8::decode $file;
558
559 # a meta file for resources is now mandatory
560 unless (exists $meta->{$file}) {
561 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
562 next;
563 }
564
565 $meta = {
566 %{ $meta->{"" } || {} },
567 %{ $meta->{$file} || {} },
568 };
569
570 if ($meta->{license} =~ s/^#//) {
571 $meta->{license} = ({
572 "pd" => "Public Domain",
573 "gpl" => "GNU General Public License, version 3.0 or any later",
574 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
575 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
576 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
577 })->{$meta->{license}}
578 || warn "$dir/$file: license tag '$meta->{license}' not found.";
579 }
402 580
403 $file =~ s/\.res$//; 581 $file =~ s/\.res$//;
404 $file =~ /\.([^.]+)$/ 582 $file =~ s/\.(ogg|wav|jpg|png)$//;
405 or next;
406
407 my $type = $1;
408 583
409 substr $dir, 0, 1 + length $PATH, ""; 584 substr $dir, 0, 1 + length $PATH, "";
410 585
586 if (my $filter = $meta->{cfutil_filter}) {
587 if ($filter eq "yaml2json") {
588 $data = JSON::XS::encode_json YAML::XS::Load $data;
589 } elsif ($filter eq "json2json") {
590 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
591 } elsif ($filter eq "perl2json") {
592 $data = eval $data; die if $@;
593 $data = JSON::XS::encode_json $data;
594 } else {
595 warn "$dir/$file: unknown filter $filter, skipping\n";
596 }
597 }
598
411 $RESOURCE{"$dir/$file"} = { 599 $RESOURCE{"$dir/$file"} = {
412 type => $1, 600 type => (exists $meta->{type} ? delete $meta->{type} : $type),
413 copyright => $copyright,
414 data => $data, 601 data => $data,
415 chksum => Digest::MD5::md5 $data, 602 %$meta ? (meta => $meta) : (),
416 }; 603 };
417 } 604 }
418 } 605 }
419 606
420 sub find_files; 607 sub find_files;
424 IO::AIO::aioreq_pri 4; 611 IO::AIO::aioreq_pri 4;
425 IO::AIO::aio_scandir $path, 4, sub { 612 IO::AIO::aio_scandir $path, 4, sub {
426 my ($dirs, $nondirs) = @_; 613 my ($dirs, $nondirs) = @_;
427 614
428 find_files "$path/$_" 615 find_files "$path/$_"
429 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 616 for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs;
617
618 my $dir = $path;
619 substr $dir, 0, 1 + length $PATH, "";
430 620
431 for my $file (@$nondirs) { 621 for my $file (@$nondirs) {
622 if ($dir =~ /^music(?:\/|$)/) {
623 $c_res->put ([$path, $file, 3]) # FT_MUSIC
624 if $file =~ /\.(ogg)$/;
625
626 } elsif ($dir =~ /^sound(?:\/|$)/) {
627 $c_res->put ([$path, $file, 5]) # FT_SOUND
628 if $file =~ /\.(wav|ogg)$/;
629
630 } elsif ($dir =~ /^res(?:\/|$)/) {
631 if ($file =~ /\.(jpg|png)$/) {
632 $c_res->put ([$path, $file, 0]) # FT_FACE
633 } elsif ($file =~ /\.(res)$/) {
634 $c_res->put ([$path, $file, 6]) # FT_RSRC
635 } else {
636 $c_res->put ([$path, $file, undef]);
637 }
638
432 if ($file =~ /\.png$/) { 639 } elsif ($file =~ /\.png$/) {
433 push @png, ["$path/$file", 0]; 640 push @c_png, ["$path/$file", 0];
641
434 } elsif ($file =~ /\.trs$/) { 642 } elsif ($file =~ /\.trs$/) {
435 push @trs, [$path, $file]; 643 $c_trs->put ([$path, $file]);
644
436 } elsif ($file =~ /\.arc$/) { 645 } elsif ($file =~ /\.arc$/) {
437 push @arc, [$path, $file]; 646 $c_arc->put ([$path, $file]);
438 } elsif ($file =~ /\.(ogg|jpg|res)$/) { 647
439 push @res, [$path, $file];
440 } else { 648 } else {
441 warn "ignoring $path/$file\n" if $VERBOSE >= 3; 649 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
442 } 650 }
443 } 651 }
444 }; 652 };
653 }
654
655 sub generate_plurals {
656# use Lingua::EN::Inflect ();
657# Lingua::EN::Inflect::classical;
658# Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy
659# Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack
660#
661# for my $a (@ARC) {
662# my $name = $a->{name} || $a->{_name};
663#
664# next unless $a->{name_pl};
665# next if $a->{invisible};
666# next if $a->{is_floor};
667# next if $a->{no_pick};
668#
669# my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name;
670# my $pl = $test =~ /^(?:eq|p:.)$/
671# ? $name
672# : Lingua::EN::Inflect::PL $name;
673#
674# if ($pl ne $a->{name_pl}) {
675# warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n";
676# }
677# }
445 } 678 }
446 679
447 sub inst_arch($) { 680 sub inst_arch($) {
448 my (undef, $path) = @_; 681 my (undef, $path) = @_;
449 682
462 if (!-d "$path/treasures") { 695 if (!-d "$path/treasures") {
463 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 696 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
464 exit 1 unless $FORCE; 697 exit 1 unless $FORCE;
465 } 698 }
466 699
700 print "scanning files...\n" if $VERBOSE;
701
467 find_files $path; 702 find_files $path;
703
704 my @a_arc = map +(async \&process_arc), 1..2;
705 my @a_res = map +(async \&process_res), 1..2;
706 my @a_trs = map +(async \&process_trs), 1..2;
707
468 IO::AIO::flush; 708 IO::AIO::flush;
469 709
470 $_->join for ( 710 $c_res->shutdown;
711 $c_arc->shutdown;
712 $c_trs->shutdown;
713
714 print "start file scan, arc, res processing...\n" if $VERBOSE;
715
716 $_->join for @a_arc; # need to parse all archetypes before png processing
717
718 print "end arc, start png processing...\n" if $VERBOSE;
719
471 # four png crunchers work fine for my 2x smp machine 720 # eight png crunchers work fine for my 4x smp machine
472 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), 721 my @a_png = map +(async \&process_png), 1..8;
473 (async \&process_trs), (async \&process_trs),
474 (async \&process_arc), (async \&process_arc),
475 (async \&process_res), (async \&process_res),
476 );
477 722
723 $_->join for (@a_trs, @a_res, @a_png);
724
725 print "scanning done, processing results...\n" if $VERBOSE;
478 { 726 {
479 # remove path prefix from editor_folder 727 # remove path prefix from editor_folder
728 $_->{editor_folder} =~ /^\x00/
480 substr $_->{editor_folder}, 0, 1 + length $path, "" 729 and substr $_->{editor_folder}, 0, 2 + length $path, ""
481 for values %ARC; 730 for values %ARC;
482 731
732 print "resolving inheritance tree...\n" if $VERBOSE;
483 # resolve inherit 733 # resolve inherit
484 while () { 734 while () {
485 my $progress; 735 my $progress;
486 my $loop; 736 my $loop;
487 737
488 for my $o (values %ARC) { 738 for my $o (values %ARC) {
489 if (my $other = $o->{inherit}) { 739 for my $other (split /,/, $o->{inherit}) {
490 if (my $s = $ARC{$other}) { 740 if (my $s = $ARC{$other}) {
491 if ($s->{inherit}) { 741 if ($s->{inherit}) {
492 $loop = $s; 742 $loop = $s;
493 } else { 743 } else {
494 delete $o->{inherit}; 744 delete $o->{inherit};
513 } 763 }
514 764
515 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit 765 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
516 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 766 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
517 767
768 print "generating plurals...\n" if $VERBOSE;
769 generate_plurals;
770
771 printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE;
518 open my $fh, ">:utf8", "$DATADIR/archetypes~" 772 open my $fh, ">:utf8", "$DATADIR/archetypes~"
519 or die "$DATADIR/archetypes~: $!"; 773 or die "$DATADIR/archetypes~: $!";
520 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 774 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
521 } 775 }
522 776
523 { 777 {
778 printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE;
524 open my $fh, ">:utf8", "$DATADIR/treasures~" 779 open my $fh, ">:utf8", "$DATADIR/treasures~"
525 or die "$DATADIR/treasures~: $!"; 780 or die "$DATADIR/treasures~: $!";
526 print $fh $TRS; 781 print $fh $TRS;
527 } 782 }
528 783
529 { 784 {
785 print "processing facedata...\n" if $VERBOSE;
530 while (my ($k, $v) = each %FACEINFO) { 786 while (my ($k, $v) = each %FACEINFO) {
531 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; 787 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
532 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; 788 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
533 789
790 make_hash $k, $v->{data32}, $v->{hash32};
791 make_hash $k, $v->{data64}, $v->{hash64};
792
534 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; 793 #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
535 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; 794 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
536
537 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
538 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
539 795
540 if (my $magicmap = $v->{magicmap}) { 796 if (my $magicmap = $v->{magicmap}) {
541 $magicmap =~ y/A-Z_\-/a-z/d; 797 $magicmap =~ y/A-Z_\-/a-z/d;
542 $v->{magicmap} = $COLOR{$magicmap}; 798 $v->{magicmap} = $COLOR{$magicmap};
543 } 799 }
800
801 delete $v->{arc};
802 }
803
804 print "processing resources...\n" if $VERBOSE;
805 my $enc = JSON::XS->new->utf8->canonical->relaxed;
806 while (my ($k, $v) = each %RESOURCE) {
807
808 if ($v->{meta} && $v->{meta}{datadir}) {
809 delete $RESOURCE{$k};
810
811 $k =~ s/^res\/// or die "$k: datadir files must be in res/";
812
813 printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE;
814 open my $fh, ">:raw", "$DATADIR/$k~"
815 or die "$DATADIR/$k~: $!";
816 syswrite $fh, $v->{data};
817 push @COMMIT, $k;
818
819 } else {
820 if ($v->{type} & 1) {
821 # prepend meta info
822
823 my $meta = $enc->encode ({
824 name => $k,
825 %{ $v->{meta} || {} },
826 });
827
828 $v->{data} = pack "(w/a*)*", $meta, $v->{data};
829 }
830
831 make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients
544 } 832 }
833 }
834
835 printf "writing facedata (%d faces, %d anims, %d resources)...\n",
836 scalar keys %FACEINFO,
837 scalar keys %ANIMINFO,
838 scalar keys %RESOURCE
839 if $VERBOSE;
545 840
546 open my $fh, ">:perlio", "$DATADIR/facedata~" 841 open my $fh, ">:perlio", "$DATADIR/facedata~"
547 or die "$DATADIR/facedata~: $!"; 842 or die "$DATADIR/facedata~: $!";
548 843
549 print $fh freeze { 844 print $fh nfreeze {
550 version => 2, 845 version => 2,
551 faceinfo => \%FACEINFO, 846 faceinfo => \%FACEINFO,
552 animinfo => \%ANIMINFO, 847 animinfo => \%ANIMINFO,
553 resource => \%RESOURCE, 848 resource => \%RESOURCE,
554 }; 849 };
555 }
556 850
851 }
852
853 print "committing files...\n" if $VERBOSE;
854
557 for (qw(archetypes facedata treasures)) { 855 for (qw(archetypes facedata treasures), @COMMIT) {
558 chmod 0644, "$DATADIR/$_~"; 856 chmod 0644, "$DATADIR/$_~";
559 rename "$DATADIR/$_~", "$DATADIR/$_" 857 rename "$DATADIR/$_~", "$DATADIR/$_"
560 or die "$DATADIR/$_: $!"; 858 or die "$DATADIR/$_: $!";
561 } 859 }
562 860

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines