--- deliantra/server/lib/cf.pm 2007/07/14 14:33:30 1.306 +++ deliantra/server/lib/cf.pm 2007/07/24 22:49:40 1.320 @@ -169,6 +169,12 @@ returns directly I the tick processing (and consequently, can only wake one process per tick), while cf::wait_for_tick wakes up all waiters after tick processing. +=item @cf::INVOKE_RESULTS + +This array contains the results of the last C call. When +C is called C<@cf::INVOKE_RESULTS> is set to the parameters of +that call. + =back =cut @@ -318,6 +324,62 @@ $guard } +=item cf::get_slot $time[, $priority[, $name]] + +Allocate $time seconds of blocking CPU time at priority C<$priority>: +This call blocks and returns only when you have at least C<$time> seconds +of cpu time till the next tick. The slot is only valid till the next cede. + +The optional C<$name> can be used to identify the job to run. It might be +used for statistical purposes and should identify the same time-class. + +Useful for short background jobs. + +=cut + +our @SLOT_QUEUE; +our $SLOT_QUEUE; + +$SLOT_QUEUE->cancel if $SLOT_QUEUE; +$SLOT_QUEUE = Coro::async { + my $signal = new Coro::Signal; + + while () { + next_job: + my $avail = cf::till_tick; + if ($avail > 0.01) { + for (0 .. $#SLOT_QUEUE) { + if ($SLOT_QUEUE[$_][0] < $avail) { + my $job = splice @SLOT_QUEUE, $_, 1, (); + $job->[2]->send; + Coro::cede; + goto next_job; + } + } + } + + if (@SLOT_QUEUE) { + # we do not use wait_For_tick() as it returns immediately when tick is inactive + push @cf::WAIT_FOR_TICK, $signal; + $signal->wait; + } else { + Coro::schedule; + } + } +}; + +sub get_slot($;$$) { + my ($time, $pri, $name) = @_; + + $time = $TICK * .6 if $time > $TICK * .6; + my $sig = new Coro::Signal; + + push @SLOT_QUEUE, [$time, $pri, $sig, $name]; + @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; + $SLOT_QUEUE->ready; + $sig->wait; +} + =item cf::async { BLOCK } Currently the same as Coro::async_pool, meaning you cannot use @@ -868,18 +930,18 @@ } our $override; -our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? +our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals? sub override { $override = 1; - @invoke_results = (); + @INVOKE_RESULTS = (@_); } sub do_invoke { my $event = shift; my $callbacks = shift; - @invoke_results = (); + @INVOKE_RESULTS = (); local $override; @@ -906,7 +968,7 @@ This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be removed in future versions), and there is no public API to access override -results (if you must, access C<@cf::invoke_results> directly). +results (if you must, access C<@cf::INVOKE_RESULTS> directly). =back @@ -914,6 +976,17 @@ ############################################################################# # object support +# + +sub _can_merge { + my ($ob1, $ob2) = @_; + + local $Storable::canonical = 1; + my $fob1 = Storable::freeze $ob1; + my $fob2 = Storable::freeze $ob2; + + $fob1 eq $fob2 +} sub reattach { # basically do the same as instantiate, without calling instantiate @@ -1110,10 +1183,16 @@ my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; if (ref $msg) { - if (my $cb = $EXTCMD{$msg->{msgtype}}) { - if (my %reply = $cb->($pl, $msg)) { - $pl->ext_reply ($msg->{msgid}, %reply); - } + my ($type, $reply, @payload) = + "ARRAY" eq ref $msg + ? @$msg + : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove + + if (my $cb = $EXTCMD{$type}) { + my @reply = $cb->($pl, @payload); + + $pl->ext_reply ($reply, @reply) + if $reply; } } else { warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; @@ -1446,29 +1525,34 @@ $_[0]{hintmode} } -=item $player->ext_reply ($msgid, %msg) +=item $player->ext_reply ($msgid, @msg) Sends an ext reply to the player. =cut -sub ext_reply($$%) { - my ($self, $id, %msg) = @_; +sub ext_reply($$@) { + my ($self, $id, @msg) = @_; - $msg{msgid} = $id; - $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg)); + if ($self->ns->extcmd == 2) { + $self->send ("ext " . $self->ns->{json_coder}->encode (["reply-$id", @msg])); + } elsif ($self->ns->extcmd == 1) { + #TODO: version 1, remove + unshift @msg, msgtype => "reply", msgid => $id; + $self->send ("ext " . $self->ns->{json_coder}->encode ({@msg})); + } } -=item $player->ext_event ($type, %msg) +=item $player->ext_msg ($type, @msg) Sends an ext event to the client. =cut -sub ext_event($$%) { - my ($self, $type, %msg) = @_; +sub ext_msg($$@) { + my ($self, $type, @msg) = @_; - $self->ns->ext_event ($type, %msg); + $self->ns->ext_msg ($type, @msg); } =head3 cf::region @@ -1621,7 +1705,7 @@ } } - Carp::carp "unable to resolve path '$path' (base '$base')."; + Carp::cluck "unable to resolve path '$path' (base '$base')."; () } @@ -1909,9 +1993,9 @@ $MAP_PREFETCHER ||= cf::async { while (%MAP_PREFETCH) { for my $path (keys %MAP_PREFETCH) { - my $map = find $path - or next; - $map->load; + if (my $map = find $path) { + $map->load; + } delete $MAP_PREFETCH{$path}; } @@ -2148,13 +2232,8 @@ my $pl = $self->contr; if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { - my $diag = $pl->{npc_dialog}; - $diag->{pl}->ext_reply ( - $diag->{id}, - msgtype => "reply", - msg => $diag->{pl}->expand_cfpod ($msg), - add_topics => [] - ); + my $dialog = $pl->{npc_dialog}; + $dialog->{pl}->ext_msg ($dialog->{id}, update => msg => $dialog->{pl}->expand_cfpod ($msg)); } else { $msg = $npc->name . " says: $msg" if $npc; @@ -2421,7 +2500,7 @@ $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); } -=item $client->send_msg ($color, $type, $msg, [extra...]) +=item $client->send_msg ($channel, $msg, $color, [extra...]) Send a drawinfo or msg packet to the client, formatting the msg for the client if neccessary. C<$type> should be a string identifying the type of @@ -2431,14 +2510,26 @@ =cut sub cf::client::send_msg { - my ($self, $color, $type, $msg, @extra) = @_; + my ($self, $channel, $msg, $color, @extra) = @_; $msg = $self->pl->expand_cfpod ($msg); + $color &= ~cf::NDI_UNIQUE; # just in case... + + if (ref $channel) { + # send meta info to client, if not yet sent + unless (exists $self->{channel}{$channel->{id}}) { + $self->{channel}{$channel->{id}} = $channel; + $self->ext_msg (channel_info => $channel); + } + + $channel = $channel->{id}; + } + return unless @extra || length $msg; if ($self->can_msg) { - $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); + $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); } else { # replace some tags by gcfclient-compatible ones for ($msg) { @@ -2461,19 +2552,23 @@ } } -=item $client->ext_event ($type, %msg) +=item $client->ext_msg ($type, @msg) Sends an ext event to the client. =cut -sub cf::client::ext_event($$%) { - my ($self, $type, %msg) = @_; +sub cf::client::ext_msg($$@) { + my ($self, $type, @msg) = @_; - return unless $self->extcmd; + my $extcmd = $self->extcmd; - $msg{msgtype} = "event_$type"; - $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); + if ($extcmd == 2) { + $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); + } elsif ($extcmd == 1) { # TODO: remove + push @msg, msgtype => "event_$type"; + $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); + } } =item $success = $client->query ($flags, "text", \&cb) @@ -2538,11 +2633,16 @@ my $msg = eval { $ns->{json_coder}->decode ($buf) }; if (ref $msg) { - if (my $cb = $EXTICMD{$msg->{msgtype}}) { - if (my %reply = $cb->($ns, $msg)) { - $reply{msgid} = $msg->{msgid}; - $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); - } + my ($type, $reply, @payload) = + "ARRAY" eq ref $msg + ? @$msg + : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove + + if (my $cb = $EXTICMD{$type}) { + my @reply = $cb->($ns, @payload); + + $ns->ext_reply ($reply, @reply) + if $reply; } } else { warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; @@ -2767,18 +2867,31 @@ my $res = $facedata->{resource}; my $enc = JSON::XS->new->utf8->canonical; + if (my $soundconf = delete $res->{"res/sound.conf"}) { + $soundconf = $enc->decode (delete $soundconf->{data}); + # todo + } + while (my ($name, $info) = each %$res) { my $meta = $enc->encode ({ - name => $name, - type => $info->{type}, - copyright => $info->{copyright}, #TODO# + name => $name, + %{ $info->{meta} || {} }, }); - my $data = pack "(w/a*)*", $meta, $info->{data}; - my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata my $idx = (cf::face::find $name) || cf::face::alloc $name; - cf::face::set_type $idx, 1; - cf::face::set_data $idx, 0, $data, $chk; + + if ($info->{type} & 1) { + # prepend meta info + + my $data = pack "(w/a*)*", $meta, $info->{data}; + my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata + + cf::face::set_data $idx, 0, $data, $chk; + } else { + cf::face::set_data $idx, 0, $info->{data}, $info->{chksum}; + } + + cf::face::set_type $idx, $info->{type}; cf::cede_to_tick; } @@ -2787,6 +2900,14 @@ 1 } +register_exticmd fx_want => sub { + my ($ns, $want) = @_; + + while (my ($k, $v) = each %$want) { + $ns->fx_want ($k, $v); + } +}; + sub reload_regions { load_resource_file "$MAPDIR/regions" or die "unable to load regions file\n";