… | |
… | |
29 | use Fcntl; |
29 | use Fcntl; |
30 | use YAML::Syck (); |
30 | use YAML::Syck (); |
31 | use IO::AIO 2.32 (); |
31 | use IO::AIO 2.32 (); |
32 | use Time::HiRes; |
32 | use Time::HiRes; |
33 | use Compress::LZF; |
33 | use Compress::LZF; |
|
|
34 | use Digest::MD5 (); |
34 | |
35 | |
35 | # configure various modules to our taste |
36 | # configure various modules to our taste |
36 | # |
37 | # |
37 | $Storable::canonical = 1; # reduce rsync transfers |
38 | $Storable::canonical = 1; # reduce rsync transfers |
38 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
39 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
… | |
… | |
409 | Coro::Storable. May, of course, block. Note that the executed sub may |
410 | Coro::Storable. May, of course, block. Note that the executed sub may |
410 | never block itself or use any form of Event handling. |
411 | never block itself or use any form of Event handling. |
411 | |
412 | |
412 | =cut |
413 | =cut |
413 | |
414 | |
|
|
415 | sub _store_scalar { |
|
|
416 | open my $fh, ">", \my $buf |
|
|
417 | or die "fork_call: cannot open fh-to-buf in child : $!"; |
|
|
418 | Storable::store_fd $_[0], $fh; |
|
|
419 | close $fh; |
|
|
420 | |
|
|
421 | $buf |
|
|
422 | } |
|
|
423 | |
414 | sub fork_call(&@) { |
424 | sub fork_call(&@) { |
415 | my ($cb, @args) = @_; |
425 | my ($cb, @args) = @_; |
416 | |
426 | |
417 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
427 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
418 | # or die "socketpair: $!"; |
428 | # or die "socketpair: $!"; |
… | |
… | |
421 | |
431 | |
422 | if (my $pid = fork) { |
432 | if (my $pid = fork) { |
423 | close $fh2; |
433 | close $fh2; |
424 | |
434 | |
425 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
435 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
|
|
436 | warn "pst<$res>" unless $res =~ /^pst/; |
426 | $res = Coro::Storable::thaw $res; |
437 | $res = Coro::Storable::thaw $res; |
427 | |
438 | |
428 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
439 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
429 | |
440 | |
430 | die $$res unless "ARRAY" eq ref $res; |
441 | Carp::confess $$res unless "ARRAY" eq ref $res; |
431 | |
442 | |
432 | return wantarray ? @$res : $res->[-1]; |
443 | return wantarray ? @$res : $res->[-1]; |
433 | } else { |
444 | } else { |
434 | reset_signals; |
445 | reset_signals; |
435 | local $SIG{__WARN__}; |
446 | local $SIG{__WARN__}; |
436 | local $SIG{__DIE__}; |
447 | local $SIG{__DIE__}; |
|
|
448 | # just in case, this hack effectively disables event |
|
|
449 | # in the child. cleaner and slower would be canceling all watchers, |
|
|
450 | # but this works for the time being. |
|
|
451 | local $Coro::idle; |
|
|
452 | $Coro::current->prio (Coro::PRIO_MAX); |
|
|
453 | |
437 | eval { |
454 | eval { |
438 | close $fh1; |
455 | close $fh1; |
439 | |
456 | |
440 | my @res = eval { $cb->(@args) }; |
457 | my @res = eval { $cb->(@args) }; |
|
|
458 | |
441 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
459 | syswrite $fh2, _store_scalar $@ ? \"$@" : \@res; |
|
|
460 | close $fh2; |
442 | }; |
461 | }; |
443 | |
462 | |
444 | warn $@ if $@; |
463 | warn $@ if $@; |
445 | _exit 0; |
464 | _exit 0; |
446 | } |
465 | } |
… | |
… | |
2701 | { |
2720 | { |
2702 | my $faces = $facedata->{faceinfo}; |
2721 | my $faces = $facedata->{faceinfo}; |
2703 | |
2722 | |
2704 | while (my ($face, $info) = each %$faces) { |
2723 | while (my ($face, $info) = each %$faces) { |
2705 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2724 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2706 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
2725 | cf::face::set_visibility $idx, $info->{visibility}; |
|
|
2726 | cf::face::set_magicmap $idx, $info->{magicmap}; |
2707 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2727 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2708 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2728 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2709 | Coro::cede; |
2729 | |
|
|
2730 | cf::cede_to_tick; |
2710 | } |
2731 | } |
2711 | |
2732 | |
2712 | while (my ($face, $info) = each %$faces) { |
2733 | while (my ($face, $info) = each %$faces) { |
2713 | next unless $info->{smooth}; |
2734 | next unless $info->{smooth}; |
2714 | my $idx = cf::face::find $face |
2735 | my $idx = cf::face::find $face |
2715 | or next; |
2736 | or next; |
2716 | if (my $smooth = cf::face::find $info->{smooth}) { |
2737 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2738 | cf::face::set_smooth $idx, $smooth; |
2717 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
2739 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
2718 | } else { |
2740 | } else { |
2719 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2741 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2720 | } |
2742 | } |
2721 | Coro::cede; |
2743 | |
|
|
2744 | cf::cede_to_tick; |
2722 | } |
2745 | } |
2723 | } |
2746 | } |
2724 | |
2747 | |
2725 | { |
2748 | { |
2726 | my $anims = $facedata->{animinfo}; |
2749 | my $anims = $facedata->{animinfo}; |
2727 | |
2750 | |
2728 | while (my ($anim, $info) = each %$anims) { |
2751 | while (my ($anim, $info) = each %$anims) { |
2729 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2752 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2730 | Coro::cede; |
2753 | cf::cede_to_tick; |
2731 | } |
2754 | } |
2732 | |
2755 | |
2733 | cf::anim::invalidate_all; # d'oh |
2756 | cf::anim::invalidate_all; # d'oh |
|
|
2757 | } |
|
|
2758 | |
|
|
2759 | { |
|
|
2760 | # TODO: for gcfclient pleasure, we should give resources |
|
|
2761 | # that gcfclient doesn't grok a >10000 face index. |
|
|
2762 | my $res = $facedata->{resource}; |
|
|
2763 | my $enc = JSON::XS->new->utf8->canonical; |
|
|
2764 | |
|
|
2765 | while (my ($name, $info) = each %$res) { |
|
|
2766 | my $meta = $enc->encode ({ |
|
|
2767 | name => $name, |
|
|
2768 | type => $info->{type}, |
|
|
2769 | copyright => $info->{copyright}, #TODO# |
|
|
2770 | }); |
|
|
2771 | my $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
2772 | my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata |
|
|
2773 | |
|
|
2774 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
|
|
2775 | cf::face::set_type $idx, 1; |
|
|
2776 | cf::face::set_data $idx, 0, $data, $chk; |
|
|
2777 | |
|
|
2778 | cf::cede_to_tick; |
|
|
2779 | } |
2734 | } |
2780 | } |
2735 | |
2781 | |
2736 | 1 |
2782 | 1 |
2737 | } |
2783 | } |
2738 | |
2784 | |