… | |
… | |
573 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
573 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
574 | (default: 0): This call blocks and returns only when you have at least |
574 | (default: 0): This call blocks and returns only when you have at least |
575 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
575 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
576 | till the next cede. |
576 | till the next cede. |
577 | |
577 | |
578 | Background jobs should use a priority les than zero, interactive jobs |
578 | Background jobs should use a priority less than zero, interactive jobs |
579 | should use 100 or more. |
579 | should use 100 or more. |
580 | |
580 | |
581 | The optional C<$name> can be used to identify the job to run. It might be |
581 | The optional C<$name> can be used to identify the job to run. It might be |
582 | used for statistical purposes and should identify the same time-class. |
582 | used for statistical purposes and should identify the same time-class. |
583 | |
583 | |
… | |
… | |
738 | reset_signals; |
738 | reset_signals; |
739 | } |
739 | } |
740 | |
740 | |
741 | sub fork_call(&@) { |
741 | sub fork_call(&@) { |
742 | my ($cb, @args) = @_; |
742 | my ($cb, @args) = @_; |
743 | |
|
|
744 | # we seemingly have to make a local copy of the whole thing, |
|
|
745 | # otherwise perl prematurely frees the stuff :/ |
|
|
746 | # TODO: investigate and fix (likely this will be rather laborious) |
|
|
747 | |
743 | |
748 | my @res = Coro::Util::fork_eval { |
744 | my @res = Coro::Util::fork_eval { |
749 | cf::post_fork; |
745 | cf::post_fork; |
750 | &$cb |
746 | &$cb |
751 | } @args; |
747 | } @args; |
… | |
… | |
2654 | |
2650 | |
2655 | Creates and returns a persistent reference to an object that can be stored as a string. |
2651 | Creates and returns a persistent reference to an object that can be stored as a string. |
2656 | |
2652 | |
2657 | =item $ob = cf::object::deref ($refstring) |
2653 | =item $ob = cf::object::deref ($refstring) |
2658 | |
2654 | |
2659 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2655 | returns the objetc referenced by refstring. may return undef when it cannot find the object, |
2660 | even if the object actually exists. May block. |
2656 | even if the object actually exists. May block. |
2661 | |
2657 | |
2662 | =cut |
2658 | =cut |
2663 | |
2659 | |
2664 | sub deref { |
2660 | sub deref { |
… | |
… | |
3057 | |
3053 | |
3058 | sub cf::client::send_drawinfo { |
3054 | sub cf::client::send_drawinfo { |
3059 | my ($self, $text, $flags) = @_; |
3055 | my ($self, $text, $flags) = @_; |
3060 | |
3056 | |
3061 | utf8::encode $text; |
3057 | utf8::encode $text; |
3062 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
3058 | $self->send_packet (sprintf "msg %d 0 %s", $flags || cf::NDI_BLACK, $text); |
3063 | } |
3059 | } |
3064 | |
3060 | |
3065 | =item $client->send_big_packet ($pkt) |
3061 | =item $client->send_big_packet ($pkt) |
3066 | |
3062 | |
3067 | Like C<send_packet>, but tries to compress large packets, and fragments |
3063 | Like C<send_packet>, but tries to compress large packets, and fragments |
… | |
… | |
3087 | $self->send_packet ($pkt); |
3083 | $self->send_packet ($pkt); |
3088 | } |
3084 | } |
3089 | |
3085 | |
3090 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
3086 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
3091 | |
3087 | |
3092 | Send a drawinfo or msg packet to the client, formatting the msg for the |
3088 | Send a msg packet to the client, formatting the msg for the client if |
3093 | client if neccessary. C<$type> should be a string identifying the type of |
3089 | necessary. C<$type> should be a string identifying the type of the |
3094 | the message, with C<log> being the default. If C<$color> is negative, suppress |
3090 | message, with C<log> being the default. If C<$color> is negative, suppress |
3095 | the message unless the client supports the msg packet. |
3091 | the message unless the client supports the msg packet. |
3096 | |
3092 | |
3097 | =cut |
3093 | =cut |
3098 | |
3094 | |
3099 | # non-persistent channels (usually the info channel) |
3095 | # non-persistent channels (usually the info channel) |
… | |
… | |
3204 | id => "death", |
3200 | id => "death", |
3205 | title => "Death", |
3201 | title => "Death", |
3206 | reply => undef, |
3202 | reply => undef, |
3207 | tooltip => "Reason for and more info about your most recent death", |
3203 | tooltip => "Reason for and more info about your most recent death", |
3208 | }, |
3204 | }, |
|
|
3205 | "c/fatal" => { |
|
|
3206 | id => "fatal", |
|
|
3207 | title => "Fatal Error", |
|
|
3208 | reply => undef, |
|
|
3209 | tooltip => "Reason for the server disconnect", |
|
|
3210 | }, |
3209 | "c/say" => $SAY_CHANNEL, |
3211 | "c/say" => $SAY_CHANNEL, |
3210 | "c/chat" => $CHAT_CHANNEL, |
3212 | "c/chat" => $CHAT_CHANNEL, |
3211 | ); |
3213 | ); |
3212 | |
3214 | |
3213 | sub cf::client::send_msg { |
3215 | sub cf::client::send_msg { |
… | |
… | |
3519 | =cut |
3521 | =cut |
3520 | |
3522 | |
3521 | ############################################################################# |
3523 | ############################################################################# |
3522 | # the server's init and main functions |
3524 | # the server's init and main functions |
3523 | |
3525 | |
3524 | our %FACEHASH; # hash => idx, #d# HACK for http server |
3526 | { |
3525 | our @FACEDATA; # dynamically-created facedata |
3527 | package cf::face; |
3526 | |
3528 | |
|
|
3529 | our %HASH; # hash => idx |
|
|
3530 | our @DATA; # dynamically-created facedata, only faceste 0 used |
|
|
3531 | our @FOFS; # file offset, if > 0 |
|
|
3532 | our @SIZE; # size of face, in octets |
|
|
3533 | our @META; # meta hash of face, if any |
|
|
3534 | our $DATAFH; # facedata filehandle |
|
|
3535 | |
3527 | # internal api, not fianlised |
3536 | # internal api, not finalised |
3528 | sub set_face { |
3537 | sub set { |
3529 | my ($name, $type, $data) = @_; |
3538 | my ($name, $type, $data) = @_; |
3530 | |
3539 | |
3531 | my $idx = cf::face::find $name; |
3540 | my $idx = cf::face::find $name; |
3532 | |
3541 | |
3533 | if ($idx) { |
3542 | if ($idx) { |
3534 | delete $FACEHASH{cf::face::get_chksum $idx}; |
3543 | delete $HASH{cf::face::get_csum $idx}; |
3535 | } else { |
3544 | } else { |
3536 | $idx = cf::face::alloc $name; |
3545 | $idx = cf::face::alloc $name; |
3537 | } |
3546 | } |
3538 | |
3547 | |
3539 | my $hash = cf::face::mangle_chksum Digest::MD5::md5 $data; |
3548 | my $hash = cf::face::mangle_csum Digest::MD5::md5 $data; |
3540 | |
3549 | |
3541 | cf::face::set_type $idx, $type; |
3550 | cf::face::set_type $idx, $type; |
3542 | cf::face::set_data $idx, 0, (length $data), 0, $hash; |
3551 | cf::face::set_csum $idx, 0, $hash; |
3543 | cf::face::set_meta $idx, $type & 1 ? undef : undef; |
|
|
3544 | |
3552 | |
3545 | # we need to destroy the SV itself, not just overwrite, |
3553 | # we need to destroy the SV itself, not just modify it, as a running ix |
3546 | # as a running ix might hold a reference to it. |
3554 | # might hold a reference to it: "delete" achieves that. |
3547 | # delete achieves that. |
3555 | delete $FOFS[0][$idx]; |
|
|
3556 | delete $DATA[0][$idx]; |
|
|
3557 | $DATA[0][$idx] = $data; |
|
|
3558 | $SIZE[0][$idx] = length $data; |
3548 | delete $FACEDATA[$idx]; |
3559 | delete $META[$idx]; |
3549 | $FACEDATA[$idx] = $data; |
|
|
3550 | $FACEHASH{$hash} = $idx;#d# |
3560 | $HASH{$hash} = $idx;#d# |
3551 | |
3561 | |
3552 | $idx |
3562 | $idx |
3553 | } |
3563 | } |
3554 | |
3564 | |
3555 | our $FACEDATA; # facedata filehandle |
|
|
3556 | |
|
|
3557 | sub _face_get_data($$$$) { |
3565 | sub _get_data($$$) { |
3558 | my ($idx, $size, $fofs, $cb) = @_; |
3566 | my ($idx, $set, $cb) = @_; |
3559 | |
3567 | |
3560 | if (defined $FACEDATA[$idx]) { |
3568 | if (defined $DATA[$set][$idx]) { |
3561 | $cb->($FACEDATA[$idx]); |
3569 | $cb->($DATA[$set][$idx]); |
3562 | } elsif ($fofs) { |
3570 | } elsif (my $fofs = $FOFS[$set][$idx]) { |
|
|
3571 | my $size = $SIZE[$set][$idx]; |
3563 | my $buf; |
3572 | my $buf; |
3564 | IO::AIO::aio_read $FACEDATA, $fofs, $size, $buf, 0, sub { |
3573 | IO::AIO::aio_read $DATAFH, $fofs, $size, $buf, 0, sub { |
3565 | if ($_[0] == $size) { |
3574 | if ($_[0] == $size) { |
3566 | #cf::debug "read face $idx, $size from $fofs as ", length $buf;#d# |
3575 | #cf::debug "read face $idx, $size from $fofs as ", length $buf;#d# |
3567 | $cb->($buf); |
3576 | $cb->($buf); |
3568 | } else { |
3577 | } else { |
3569 | cf::error "INTERNAL ERROR: unable to read facedata for face $idx ($size, $fofs), ignoring request."; |
3578 | cf::error "INTERNAL ERROR: unable to read facedata for face $idx#$set ($size, $fofs), ignoring request."; |
|
|
3579 | } |
3570 | } |
3580 | }; |
|
|
3581 | } else { |
|
|
3582 | cf::error "requested facedata for unknown face $idx#$set, ignoring."; |
|
|
3583 | } |
|
|
3584 | } |
|
|
3585 | |
|
|
3586 | # rather ineffient |
|
|
3587 | sub cf::face::get_data($;$) { |
|
|
3588 | my ($idx, $set) = @_; |
|
|
3589 | |
|
|
3590 | _get_data $idx, $set, Coro::rouse_cb; |
|
|
3591 | Coro::rouse_wait |
|
|
3592 | } |
|
|
3593 | |
|
|
3594 | sub cf::face::ix { |
|
|
3595 | my ($ns, $set, $idx, $pri) = @_; |
|
|
3596 | |
|
|
3597 | _get_data $idx, $set, sub { |
|
|
3598 | $ns->ix_send ($idx, $pri, $_[0]); |
3571 | }; |
3599 | }; |
3572 | } else { |
|
|
3573 | cf::error "requested facedata for unknown face $idx ($size, $fofs), ignoring."; |
|
|
3574 | } |
|
|
3575 | } |
|
|
3576 | |
|
|
3577 | # rather ineffient |
|
|
3578 | sub cf::face::get_data($;$) { |
|
|
3579 | my ($idx, $faceset) = @_; |
|
|
3580 | |
|
|
3581 | my $size = cf::face::get_size $idx, $faceset; |
|
|
3582 | my $fofs = cf::face::get_fofs $idx, $faceset; |
|
|
3583 | |
|
|
3584 | _face_get_data $idx, $size, $fofs, Coro::rouse_cb; |
|
|
3585 | Coro::rouse_wait |
|
|
3586 | } |
|
|
3587 | |
|
|
3588 | sub cf::face::ix { |
|
|
3589 | my ($ns, $idx, $pri, $size, $fofs) = @_; |
|
|
3590 | |
|
|
3591 | _face_get_data $idx, $size, $fofs, sub { |
|
|
3592 | $ns->ix_send ($idx, $pri, $_[0]); |
|
|
3593 | }; |
3600 | } |
3594 | } |
3601 | } |
3595 | |
3602 | |
3596 | sub load_facedata($) { |
3603 | sub load_facedata($) { |
3597 | my ($path) = @_; |
3604 | my ($path) = @_; |
3598 | |
3605 | |
… | |
… | |
3604 | |
3611 | |
3605 | $facedata->{version} == 2 |
3612 | $facedata->{version} == 2 |
3606 | or cf::cleanup "$path/faceinfo: version mismatch, cannot proceed."; |
3613 | or cf::cleanup "$path/faceinfo: version mismatch, cannot proceed."; |
3607 | |
3614 | |
3608 | my $fh = aio_open "$DATADIR/facedata", IO::AIO::O_RDONLY, 0 |
3615 | my $fh = aio_open "$DATADIR/facedata", IO::AIO::O_RDONLY, 0 |
3609 | or cf::cleanup "$path/facedata: $!, cnanot proceed."; |
3616 | or cf::cleanup "$path/facedata: $!, cannot proceed."; |
3610 | |
3617 | |
3611 | get_slot 1, -100, "load_facedata"; # make sure we get a very big slot |
3618 | get_slot 1, -100, "load_facedata"; # make sure we get a very big slot |
3612 | |
3619 | |
3613 | # BEGIN ATOMIC |
3620 | # BEGIN ATOMIC |
3614 | # from here on, everything must be atomic - no thread switch allowed |
3621 | # from here on, everything must be atomic - no thread switch allowed |
… | |
… | |
3621 | my $info = $faces->{$face}; |
3628 | my $info = $faces->{$face}; |
3622 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3629 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3623 | |
3630 | |
3624 | cf::face::set_visibility $idx, $info->{visibility}; |
3631 | cf::face::set_visibility $idx, $info->{visibility}; |
3625 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3632 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3626 | cf::face::set_data $idx, 0, $info->{size32}, $info->{fofs32}, $info->{hash32}; |
3633 | cf::face::set_csum $idx, 0, $info->{hash64}; $cf::face::SIZE[0][$idx] = $info->{size64}; $cf::face::FOFS[0][$idx] = $info->{fofs64}; |
3627 | cf::face::set_data $idx, 1, $info->{size64}, $info->{fofs64}, $info->{hash64}; |
3634 | cf::face::set_csum $idx, 1, $info->{hash32}; $cf::face::SIZE[1][$idx] = $info->{size32}; $cf::face::FOFS[1][$idx] = $info->{fofs32}; |
3628 | #cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; |
3635 | cf::face::set_csum $idx, 2, $info->{glyph}; $cf::face::DATA[2][$idx] = $info->{glyph}; |
3629 | $FACEHASH{$info->{hash64}} = $idx;#d# |
3636 | $cf::face::HASH{$info->{hash64}} = $idx; |
|
|
3637 | delete $cf::face::META[$idx]; |
3630 | } |
3638 | } |
3631 | |
3639 | |
3632 | while (my ($face, $info) = each %$faces) { |
3640 | while (my ($face, $info) = each %$faces) { |
3633 | next unless $info->{smooth}; |
3641 | next unless $info->{smooth}; |
3634 | |
3642 | |
… | |
… | |
3661 | if (defined (my $type = $info->{type})) { |
3669 | if (defined (my $type = $info->{type})) { |
3662 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3670 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3663 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3671 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3664 | |
3672 | |
3665 | cf::face::set_type $idx, $type; |
3673 | cf::face::set_type $idx, $type; |
3666 | cf::face::set_data $idx, 0, $info->{size}, $info->{fofs}, $info->{hash}; |
3674 | cf::face::set_csum $idx, 0, $info->{hash}; |
|
|
3675 | $cf::face::SIZE[0][$idx] = $info->{size}; |
|
|
3676 | $cf::face::FOFS[0][$idx] = $info->{fofs}; |
3667 | cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3677 | $cf::face::META[$idx] = $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3668 | $FACEHASH{$info->{hash}} = $idx;#d# |
3678 | $cf::face::HASH{$info->{hash}} = $idx; |
3669 | } else { |
3679 | } else { |
3670 | # $RESOURCE{$name} = $info; # unused |
3680 | # $RESOURCE{$name} = $info; # unused |
3671 | } |
3681 | } |
3672 | } |
3682 | } |
3673 | } |
3683 | } |
3674 | |
3684 | |
3675 | ($fh, $FACEDATA) = ($FACEDATA, $fh); |
3685 | ($fh, $cf::face::DATAFH) = ($cf::face::DATAFH, $fh); |
3676 | |
3686 | |
3677 | # HACK to clear player env face cache, we need some signal framework |
3687 | # HACK to clear player env face cache, we need some signal framework |
3678 | # for this (global event?) |
3688 | # for this (global event?) |
3679 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3689 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3680 | |
3690 | |
… | |
… | |
3708 | } |
3718 | } |
3709 | |
3719 | |
3710 | sub reload_exp_table { |
3720 | sub reload_exp_table { |
3711 | _reload_exp_table; |
3721 | _reload_exp_table; |
3712 | |
3722 | |
|
|
3723 | cf::face::set |
3713 | set_face "res/exp_table" => FT_RSRC, |
3724 | "res/exp_table" => FT_RSRC, |
3714 | JSON::XS->new->utf8->canonical->encode ( |
3725 | JSON::XS->new->utf8->canonical->encode ( |
3715 | [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level] |
3726 | [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level] |
3716 | ); |
3727 | ); |
3717 | } |
3728 | } |
3718 | |
3729 | |
3719 | sub reload_materials { |
3730 | sub reload_materials { |
3720 | _reload_materials; |
3731 | _reload_materials; |
3721 | } |
3732 | } |
… | |
… | |
3741 | |
3752 | |
3742 | sub reload_archetypes { |
3753 | sub reload_archetypes { |
3743 | load_resource_file "$DATADIR/archetypes" |
3754 | load_resource_file "$DATADIR/archetypes" |
3744 | or die "unable to load archetypes\n"; |
3755 | or die "unable to load archetypes\n"; |
3745 | |
3756 | |
|
|
3757 | cf::face::set |
3746 | set_face "res/skill_info" => FT_RSRC, |
3758 | "res/skill_info" => FT_RSRC, |
3747 | JSON::XS->new->utf8->canonical->encode ( |
3759 | JSON::XS->new->utf8->canonical->encode ( |
3748 | [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1] |
3760 | [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1] |
3749 | ); |
3761 | ); |
|
|
3762 | |
|
|
3763 | cf::face::set |
3750 | set_face "res/spell_paths" => FT_RSRC, |
3764 | "res/spell_paths" => FT_RSRC, |
3751 | JSON::XS->new->utf8->canonical->encode ( |
3765 | JSON::XS->new->utf8->canonical->encode ( |
3752 | [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] |
3766 | [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] |
3753 | ); |
3767 | ); |
3754 | } |
3768 | } |
3755 | |
3769 | |
3756 | sub reload_treasures { |
3770 | sub reload_treasures { |
3757 | load_resource_file "$DATADIR/treasures" |
3771 | load_resource_file "$DATADIR/treasures" |
3758 | or die "unable to load treasurelists\n"; |
3772 | or die "unable to load treasurelists\n"; |