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.53 by root, Sun Aug 19 09:27:08 2007 UTC vs.
Revision 1.71 by root, Sat Feb 7 21:40:54 2009 UTC

12my $OPTIPNG = "@OPTIPNG@"; 12my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@"; 13my $RSYNC = "@RSYNC@";
14my $PNGNQ = "@PNGNQ@"; 14my $PNGNQ = "@PNGNQ@";
15 15
16use Getopt::Long; 16use Getopt::Long;
17use Coro::Event; 17use Coro::EV;
18use AnyEvent; 18use AnyEvent;
19use YAML ();
20use JSON::XS ();
19use IO::AIO (); 21use IO::AIO ();
20use File::Temp; 22use File::Temp;
21use Crossfire; 23use Deliantra;
22use Coro; 24use Coro 5.12;
23use Coro::AIO; 25use Coro::AIO;
24use Coro::Util; 26use Coro::Util;
25use POSIX (); 27use POSIX ();
26use Digest::MD5;
27use Carp; 28use Carp;
28use Coro::Channel; 29use Coro::Channel;
29use Coro::Storable; $Storable::canonical = 1; 30use Coro::Storable; $Storable::canonical = 1;
30 31
31$SIG{QUIT} = sub { Carp::cluck "QUIT" }; 32$SIG{QUIT} = sub { Carp::cluck "QUIT" };
66 tan => 12, 67 tan => 12,
67); 68);
68 69
69END { system "rm", "-rf", $TMPDIR } 70END { system "rm", "-rf", $TMPDIR }
70 71
71Event->signal (signal => "INT", cb => sub { exit 1 }); 72my $s_INT = EV::signal INT => sub { exit 1 };
72Event->signal (signal => "TERM", cb => sub { exit 1 }); 73my $s_TERM = EV::signal TERM => sub { exit 1 };
73 74
74mkdir $TMPDIR, 0700 75mkdir $TMPDIR, 0700
75 or die "$TMPDIR: $!"; 76 or die "$TMPDIR: $!";
76 77
77sub fork_sub(&) { 78sub fork_sub(&) {
97 if (!-f "$path/regions") { 98 if (!-f "$path/regions") {
98 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 99 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
99 exit 1 unless $FORCE; 100 exit 1 unless $FORCE;
100 } 101 }
101 102
102 system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" 103 system $RSYNC, "-av", "--chmod=u=rwX,go=rX",
104 "$path/.", "$DATADIR/maps/.",
105 "--exclude", "CVS", "--exclude", "/world-precomposed",
106 "--delete", "--delete-excluded"
103 and die "map installation failed.\n"; 107 and die "map installation failed.\n";
104 108
105 print "maps installed successfully.\n"; 109 print "maps installed successfully.\n";
106} 110}
107 111
170 if ($w % $T || $h % $T) { 174 if ($w % $T || $h % $T) {
171 warn "$path: weird png size ($w $h), skipping.\n"; 175 warn "$path: weird png size ($w $h), skipping.\n";
172 next; 176 next;
173 } 177 }
174 178
179 (my $base = $stem) =~ s/^.*\///;
180
181 my $fi = $FACEINFO{$base};
182 unless ($fi) {
183 #warn "$path: <$base> not referenced by any archetype, skipping.\n";
184 #next;
185 }
186
187 my $arc = $fi->{arc} || { };
188
175 unless ($path =~ /~$/) { 189 unless ($path =~ /~$/) {
176 # possibly enlarge 190 # possibly enlarge
177 if (0 > aio_stat "$stem.64x64.png") { 191 if (0 > aio_stat "$stem.64x64.png") {
178 my $other = "$stem.64x64.png~"; 192 my $other = "$stem.64x64.png~";
179 193
180 if (0 > aio_lstat $other or (-M _) > (-M $path)) { 194 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
181 fork_sub { 195 fork_sub {
182 my $CROP; 196 my $CROP;
183 my $SRC = "png:\Q$path\E"; 197 my $SRC = "png:\Q$path\E";
184 198
185 # check if this is a wall. ultra-ugly. ultra-ultra-ugly. 199 my $is_floor = $arc->{is_floor};
200 my $is_wall = 0;
201
202 my ($wall_pfx, $wall_dir, $wall_sfx);
203
204 if (
205 !$is_floor
206 && !$arc->{alive}
207 && $arc->{move_block} eq "all"
186 if ($path =~ /^(.*\/wall\/.*_)([0-9A-F])(\.x11.*\.png)$/) { 208 && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/
209 ) {
187 my ($pfx, $dir, $sfx) = ($1, hex $2, $3); 210 ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3);
188 #check for 0..F images to be sure(?) this is a wall 211
189 unless (grep { !-e sprintf "%s%X%s", $pfx, $_, $sfx } 0..15) { 212 unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) {
213 $is_wall = 1;
214 }
215 }
216
217 if ($is_wall || $is_floor) {
190 # add a 4px border and add other images around it 218 # add a 4px border and add other images around it
191 $CROP = "-shave 8x8 +repage"; 219 $CROP = "-shave 8x8 +repage";
192 220
193 $w += 8; 221 $w += 8;
194 $h += 8; 222 $h += 8;
195 223
196 $SRC = "-size ${w}x${h} xc:transparent"; 224 $SRC = "-size ${w}x${h} xc:transparent";
197 $SRC .= " png:\Q$path\E -geometry +4+4 -composite"; 225 $SRC .= " png:\Q$path\E -geometry +4+4 -composite";
198 226
199 # 8 surrounding images 227 # 8 surrounding images
200 for ( 228 for (
201 # x y b r0 r1 229 # x y b r0 r1
202 [-1, -1, 0, 6], 230 [-1, -1, 0, 6],
203 [ 0, -1, 1, 10, 14], 231 [ 0, -1, 1, 10, 14],
204 [+1, -1, 0, 12], 232 [+1, -1, 0, 12],
205 233
206 [-1, 0, 8, 5, 7], 234 [-1, 0, 8, 5, 7],
207 # 235 #
208 [+1, 0, 2, 5, 13], 236 [+1, 0, 2, 5, 13],
209 237
210 [-1, +1, 0, 3], 238 [-1, +1, 0, 3],
211 [ 0, +1, 4, 10, 11], 239 [ 0, +1, 4, 10, 11],
212 [+1, +1, 0, 9], 240 [+1, +1, 0, 9],
213 ) { 241 ) {
214 my ($x, $y, $d, $r0, $r1) = @$_; 242 my ($x, $y, $d, $r0, $r1) = @$_;
243
244 my $tile = $is_floor ? $path
245 : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx
246 : die;
247
215 $SRC .= sprintf " png:%s%X%s -geometry %+d%+d -composite", 248 $SRC .= sprintf " png:%s -geometry %+d%+d -composite",
216 "\Q$pfx", 249 "\Q$tile",
217 ($dir & $d) ? $r1 : $r0,
218 "\Q$sfx",
219 $x * ($w - 8) + 4, 250 $x * ($w - 8) + 4,
220 $y * ($h - 8) + 4; 251 $y * ($h - 8) + 4;
221 }
222 } 252 }
223 } 253 }
224 254
225 system "convert -depth 8 $SRC rgba:-" 255 system "convert -depth 8 $SRC rgba:-"
226 . "| $exec_prefix/bin/cfhq2xa $w $h 0" 256 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
243 fork_sub { 273 fork_sub {
244 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; 274 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
245 system $OPTIPNG, "-i0", "-q", "$other~"; 275 system $OPTIPNG, "-i0", "-q", "$other~";
246 276
247 # reduce smoothfaces >10000 bytes 277 # reduce smoothfaces >10000 bytes
278 # obsolete, no longer required
248 if ($stem =~ /_S\./ && (-s "$other~") > 10000) { 279 if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) {
249 my $ncolor = 256; 280 my $ncolor = 256;
250 while () { 281 while () {
251 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; 282 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
252 system $OPTIPNG, "-i0", "-q", "$other~~"; 283 system $OPTIPNG, "-i0", "-q", "$other~~";
253 last if 10000 > -s "$other~~"; 284 last if 10000 > -s "$other~~";
399 facings => $facings, 430 facings => $facings,
400 frames => \@frames, 431 frames => \@frames,
401 }; 432 };
402 } 433 }
403 434
404 for my $face ($o->{face} || (), @{$anim || []}) { 435 for ($o->{face} || (), @{$anim || []}) {
405 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; 436 next if /^facings\s/;
406 437
438 my $face = $_;
439 $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates
440
407 my $info = $FACEINFO{$face} ||= {}; 441 my $info = $FACEINFO{$face} ||= { };
408
409 $info->{arc} = $o; 442 $info->{arc} = $o;
443
444 next if $face =~ /^blank.x11$|^empty.x11$/;
445
410 $info->{visibility} = $visibility if defined $visibility; 446 $info->{visibility} = $visibility if defined $visibility;
411 $info->{magicmap} = $magicmap if defined $magicmap; 447 $info->{magicmap} = $magicmap if defined $magicmap;
412 } 448 }
413 449
414 if (my $smooth = delete $o->{smoothface}) { 450 if (my $smooth = delete $o->{smoothface}) {
415 my %kv =split /\s+/, $smooth; 451 my %kv = split /\s+/, $smooth;
416 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support 452 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
417 while (my ($face, $smooth) = each %kv) { 453 while (my ($face, $smooth) = each %kv) {
454 $FACEINFO{$smooth}{arc} = $o;
455
418 $FACEINFO{$face}{smooth} = $smooth; 456 $FACEINFO{$face}{smooth} = $smooth;
419 $FACEINFO{$face}{smoothlevel} = $level; 457 $FACEINFO{$face}{smoothlevel} = $level;
420 } 458 }
421 } 459 }
422 } 460 }
462 my ($dir, $file, $type) = @$job; 500 my ($dir, $file, $type) = @$job;
463 501
464 my $data; 502 my $data;
465 aio_load "$dir/$file", $data; 503 aio_load "$dir/$file", $data;
466 504
467 my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift }; 505 my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) };
468 506
469 next if $meta && !exists $meta->{$file}; 507 utf8::decode $dir;
508 utf8::decode $file;
509
510 # a meta file for resources is now mandatory
511 unless (exists $meta->{$file}) {
512 warn "skipping $dir/$file\n" if $VERBOSE >= 3;
513 next;
514 }
470 515
471 $meta = { 516 $meta = {
472 %{ $meta->{"" } || {} }, 517 %{ $meta->{"" } || {} },
473 %{ $meta->{$file} || {} }, 518 %{ $meta->{$file} || {} },
474 }; 519 };
487 $file =~ s/\.res$//; 532 $file =~ s/\.res$//;
488 $file =~ s/\.(ogg|wav|jpg|png)$//; 533 $file =~ s/\.(ogg|wav|jpg|png)$//;
489 534
490 substr $dir, 0, 1 + length $PATH, ""; 535 substr $dir, 0, 1 + length $PATH, "";
491 536
537 if (my $filter = $meta->{cfutil_filter}) {
538 if ($filter eq "yaml2json") {
539 $data = JSON::XS::encode_json YAML::Load $data;
540 } elsif ($filter eq "json2json") {
541 $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data);
542 } elsif ($filter eq "perl2json") {
543 $data = eval $data; die if $@;
544 $data = JSON::XS::encode_json $data;
545 } else {
546 warn "$dir/$file: unknown filter $filter, skipping\n";
547 }
548 }
549
492 $RESOURCE{"$dir/$file"} = { 550 $RESOURCE{"$dir/$file"} = {
493 type => (delete $meta->{type}) || $type, 551 type => (exists $meta->{type} ? delete $meta->{type} : $type),
494 data => $data, 552 data => $data,
495 chksum => (Digest::MD5::md5 $data),
496 %$meta ? (meta => $meta) : (), 553 %$meta ? (meta => $meta) : (),
497 }; 554 };
498 } 555 }
499 } 556 }
500 557
520 } elsif ($dir =~ /^sound(?:\/|$)/) { 577 } elsif ($dir =~ /^sound(?:\/|$)/) {
521 $c_res->put ([$path, $file, 5]) # FT_SOUND 578 $c_res->put ([$path, $file, 5]) # FT_SOUND
522 if $file =~ /\.(wav|ogg)$/; 579 if $file =~ /\.(wav|ogg)$/;
523 580
524 } elsif ($dir =~ /^res(?:\/|$)/) { 581 } elsif ($dir =~ /^res(?:\/|$)/) {
582 if ($file =~ /\.(jpg|png)$/) {
525 $c_res->put ([$path, $file, 0]) # FT_FACE 583 $c_res->put ([$path, $file, 0]) # FT_FACE
526 if $file =~ /\.(jpg|png)$/; 584 } elsif ($file =~ /\.(res)$/) {
527 $c_res->put ([$path, $file, 7]) # FT_RSRC 585 $c_res->put ([$path, $file, 6]) # FT_RSRC
528 if $file =~ /\.(res)$/; 586 } else {
587 $c_res->put ([$path, $file, undef]);
588 }
529 589
530 } elsif ($file =~ /\.png$/) { 590 } elsif ($file =~ /\.png$/) {
531 push @c_png, ["$path/$file", 0]; 591 push @c_png, ["$path/$file", 0];
532 592
533 } elsif ($file =~ /\.trs$/) { 593 } elsif ($file =~ /\.trs$/) {
571 my @a_res = map +(async \&process_res), 1..2; 631 my @a_res = map +(async \&process_res), 1..2;
572 my @a_trs = map +(async \&process_trs), 1..2; 632 my @a_trs = map +(async \&process_trs), 1..2;
573 633
574 IO::AIO::flush; 634 IO::AIO::flush;
575 635
576 $c_res->put (undef) for @a_res; 636 $c_res->shutdown;
577 $c_arc->put (undef) for @a_arc; 637 $c_arc->shutdown;
578 $c_trs->put (undef) for @a_trs; 638 $c_trs->shutdown;
579 639
580 print "start file scan, arc, res processing...\n" if $VERBOSE; 640 print "start file scan, arc, res processing...\n" if $VERBOSE;
581 641
582 $_->join for @a_arc; # need to parse all archetypes before png processing 642 $_->join for @a_arc; # need to parse all archetypes before png processing
583 643
584 print "end arc, start png processing...\n" if $VERBOSE; 644 print "end arc, start png processing...\n" if $VERBOSE;
585 645
586 # four png crunchers work fine for my 2x smp machine 646 # eight png crunchers work fine for my 4x smp machine
587 my @a_png = map +(async \&process_png), 1..4; 647 my @a_png = map +(async \&process_png), 1..8;
588 648
589 $_->join for (@a_trs, @a_res, @a_png); 649 $_->join for (@a_trs, @a_res, @a_png);
590 650
591 print "scanning done, processing results...\n" if $VERBOSE; 651 print "scanning done, processing results...\n" if $VERBOSE;
592 { 652 {
631 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; 691 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
632 692
633 print "writing archetypes...\n" if $VERBOSE; 693 print "writing archetypes...\n" if $VERBOSE;
634 open my $fh, ">:utf8", "$DATADIR/archetypes~" 694 open my $fh, ">:utf8", "$DATADIR/archetypes~"
635 or die "$DATADIR/archetypes~: $!"; 695 or die "$DATADIR/archetypes~: $!";
636 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; 696 print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
637 } 697 }
638 698
639 { 699 {
640 print "writing treasures...\n" if $VERBOSE; 700 print "writing treasures...\n" if $VERBOSE;
641 open my $fh, ">:utf8", "$DATADIR/treasures~" 701 open my $fh, ">:utf8", "$DATADIR/treasures~"
647 print "processing facedata...\n" if $VERBOSE; 707 print "processing facedata...\n" if $VERBOSE;
648 while (my ($k, $v) = each %FACEINFO) { 708 while (my ($k, $v) = each %FACEINFO) {
649 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; 709 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
650 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; 710 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
651 711
652 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; 712 #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
653 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; 713 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
654
655 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
656 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
657 714
658 if (my $magicmap = $v->{magicmap}) { 715 if (my $magicmap = $v->{magicmap}) {
659 $magicmap =~ y/A-Z_\-/a-z/d; 716 $magicmap =~ y/A-Z_\-/a-z/d;
660 $v->{magicmap} = $COLOR{$magicmap}; 717 $v->{magicmap} = $COLOR{$magicmap};
661 } 718 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines