--- deliantra/server/lib/cf.pm 2007/07/22 14:17:58 1.313 +++ deliantra/server/lib/cf.pm 2007/08/27 02:43:38 1.344 @@ -12,7 +12,7 @@ use Safe; use Safe::Hole; -use Coro 3.61 (); +use Coro 3.64 (); use Coro::State; use Coro::Handle; use Coro::Event; @@ -21,8 +21,9 @@ use Coro::Semaphore; use Coro::AIO; use Coro::Storable; +use Coro::Util (); -use JSON::XS 1.4 (); +use JSON::XS (); use BDB (); use Data::Dumper; use Digest::MD5; @@ -324,6 +325,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 @@ -418,57 +475,19 @@ =cut -sub _store_scalar { - open my $fh, ">", \my $buf - or die "fork_call: cannot open fh-to-buf in child : $!"; - Storable::store_fd $_[0], $fh; - close $fh; - - $buf -} - sub fork_call(&@) { my ($cb, @args) = @_; -# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC -# or die "socketpair: $!"; - pipe my $fh1, my $fh2 - or die "pipe: $!"; + # we seemingly have to make a local copy of the whole thing, + # otherwise perl prematurely frees the stuff :/ + # TODO: investigate and fix (liekly this will be rather laborious) - if (my $pid = fork) { - close $fh2; - - my $res = (Coro::Handle::unblock $fh1)->readline (undef); - warn "pst<$res>" unless $res =~ /^pst/; - $res = Coro::Storable::thaw $res; - - waitpid $pid, 0; # should not block anymore, we expect the child to simply behave - - Carp::confess $$res unless "ARRAY" eq ref $res; - - return wantarray ? @$res : $res->[-1]; - } else { + my @res = Coro::Util::fork_eval { reset_signals; - local $SIG{__WARN__}; - local $SIG{__DIE__}; - # just in case, this hack effectively disables event - # in the child. cleaner and slower would be canceling all watchers, - # but this works for the time being. - local $Coro::idle; - $Coro::current->prio (Coro::PRIO_MAX); + &$cb + }, @args; - eval { - close $fh1; - - my @res = eval { $cb->(@args) }; - - syswrite $fh2, _store_scalar $@ ? \"$@" : \@res; - close $fh2; - }; - - warn $@ if $@; - _exit 0; - } + wantarray ? @res : $res[-1] } =item $value = cf::db_get $family => $key @@ -1127,11 +1146,20 @@ 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 + + my @reply; + + if (my $cb = $EXTCMD{$type}) { + @reply = $cb->($pl, @payload); } + + $pl->ext_reply ($reply, @reply) + if $reply; + } else { warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; } @@ -1312,6 +1340,16 @@ $self } +=item $player->send_msg ($channel, $msg, $color, [extra...]) + +=cut + +sub send_msg { + my $ns = shift->ns + or return; + $ns->send_msg (@_); +} + =item $pl->quit_character Nukes the player without looking back. If logged in, the connection will @@ -1463,29 +1501,28 @@ $_[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)); + $self->ns->ext_reply ($id, @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 @@ -1856,6 +1893,9 @@ $self->activate; } + $self->{last_save} = $cf::RUNTIME; + $self->last_access ($cf::RUNTIME); + $self->in_memory (cf::MAP_IN_MEMORY); } @@ -1875,10 +1915,10 @@ } # find and load all maps in the 3x3 area around a map -sub load_diag { +sub load_neighbours { my ($map) = @_; - my @diag; # diagonal neighbours + my @neigh; # diagonal neighbours for (0 .. 3) { my $neigh = $map->tile_path ($_) @@ -1887,12 +1927,14 @@ or next; $neigh->load; - push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], - [$neigh->tile_path (($_ + 1) % 4), $neigh]; + push @neigh, + [$neigh->tile_path (($_ + 3) % 4), $neigh], + [$neigh->tile_path (($_ + 1) % 4), $neigh]; } - for (@diag) { - my $neigh = find @$_ + for (grep defined $_->[0], @neigh) { + my ($path, $origin) = @$_; + my $neigh = find $path, $origin or next; $neigh->load; } @@ -1907,6 +1949,9 @@ sub do_load_sync { my ($map) = @_; + cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" + if $Coro::current == $Coro::main; + cf::sync_job { $map->load }; } @@ -1914,23 +1959,24 @@ our $MAP_PREFETCHER = undef; sub find_async { - my ($path, $origin) = @_; + my ($path, $origin, $load) = @_; $path = normalise $path, $origin && $origin->{path}; if (my $map = $cf::MAP{$path}) { - return $map if $map->in_memory == cf::MAP_IN_MEMORY; + return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY; } - undef $MAP_PREFETCH{$path}; + $MAP_PREFETCH{$path} |= $load; + $MAP_PREFETCHER ||= cf::async { while (%MAP_PREFETCH) { - for my $path (keys %MAP_PREFETCH) { - if (my $map = find $path) { - $map->load; + while (my ($k, $v) = each %MAP_PREFETCH) { + if (my $map = find $k) { + $map->load if $v; } - delete $MAP_PREFETCH{$path}; + delete $MAP_PREFETCH{$k}; } } undef $MAP_PREFETCHER; @@ -2165,13 +2211,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; @@ -2180,6 +2221,16 @@ } } +=item $object->send_msg ($channel, $msg, $color, [extra...]) + +=cut + +sub cf::object::send_msg { + my $pl = shift->contr + or return; + $pl->send_msg (@_); +} + =item $player_object->may ("access") Returns wether the given player is authorized to access resource "access" @@ -2266,7 +2317,7 @@ if $x <=0 && $y <= 0; $map->load; - $map->load_diag; + $map->load_neighbours; return unless $self->contr->active; $self->activate_recursive; @@ -2452,13 +2503,13 @@ $msg = $self->pl->expand_cfpod ($msg); - $color &= ~cf::NDI_UNIQUE; # just in case... + $color &= cf::NDI_CLIENT_MASK; # 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_event (channel_info => %$channel); + $self->ext_msg (channel_info => $channel); } $channel = $channel->{id}; @@ -2467,21 +2518,31 @@ return unless @extra || length $msg; if ($self->can_msg) { - $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); - } else { - # replace some tags by gcfclient-compatible ones - for ($msg) { - 1 while - s/([^<]*)<\/b>/[b]${1}[\/b]/ - || s/([^<]*)<\/i>/[i]${1}[\/i]/ - || s/([^<]*)<\/u>/[ul]${1}[\/ul]/ - || s/([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ - || s/([^<]*)<\/fg>/[color=$1]${2}[\/color]/; - } + # default colour, mask it out + $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) + if $color & cf::NDI_DEF; + $self->send_packet ("msg " . $self->{json_coder}->encode ( + [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra])); + } else { if ($color >= 0) { + # replace some tags by gcfclient-compatible ones + for ($msg) { + 1 while + s/([^<]*)<\/b>/[b]${1}[\/b]/ + || s/([^<]*)<\/i>/[i]${1}[\/i]/ + || s/([^<]*)<\/u>/[ul]${1}[\/ul]/ + || s/([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ + || s/([^<]*)<\/fg>/[color=$1]${2}[\/color]/; + } + + $color &= cf::NDI_COLOR_MASK; + + utf8::encode $msg; + if (0 && $msg =~ /\[/) { - $self->send_packet ("drawextinfo $color 4 0 $msg") + # COMMAND/INFO + $self->send_packet ("drawextinfo $color 10 8 $msg") } else { $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; $self->send_packet ("drawinfo $color $msg") @@ -2490,19 +2551,39 @@ } } -=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) = @_; + + if ($self->extcmd == 2) { + $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); + } elsif ($self->extcmd == 1) { # TODO: remove + push @msg, msgtype => "event_$type"; + $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); + } +} + +=item $client->ext_reply ($msgid, @msg) + +Sends an ext reply to the client. - return unless $self->extcmd; +=cut + +sub cf::client::ext_reply($$@) { + my ($self, $id, @msg) = @_; - $msg{msgtype} = "event_$type"; - $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); + if ($self->extcmd == 2) { + $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); + } elsif ($self->extcmd == 1) { + #TODO: version 1, remove + unshift @msg, msgtype => "reply", msgid => $id; + $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); + } } =item $success = $client->query ($flags, "text", \&cb) @@ -2567,12 +2648,20 @@ 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 + + my @reply; + + if (my $cb = $EXTICMD{$type}) { + @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"; } @@ -2629,7 +2718,11 @@ $SIG{FPE} = 'IGNORE'; -$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); +$safe->permit_only (Opcode::opset qw( + :base_core :base_mem :base_orig :base_math + grepstart grepwhile mapstart mapwhile + sort time +)); # here we export the classes and methods available to script code @@ -2639,7 +2732,7 @@ cf::object contr pay_amount pay_player map x y force_find force_add - insert remove + insert remove name archname title slaying race decrease_ob_nr cf::object::player player @@ -2654,7 +2747,8 @@ for ( ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y - insert remove)], + insert remove inv name archname title slaying race + decrease_ob_nr)], ["cf::object::player" => qw(player)], ["cf::player" => qw(peaceful)], ["cf::map" => qw(trigger)], @@ -2740,6 +2834,8 @@ sub load_facedata($) { my ($path) = @_; + my $enc = JSON::XS->new->utf8->canonical->relaxed; + warn "loading facedata from $path\n"; my $facedata; @@ -2751,6 +2847,13 @@ $facedata->{version} == 2 or cf::cleanup "$path: version mismatch, cannot proceed."; + # patch in the exptable + $facedata->{resource}{"res/exp_table"} = { + type => FT_RSRC, + data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), + }; + cf::cede_to_tick; + { my $faces = $facedata->{faceinfo}; @@ -2758,8 +2861,8 @@ my $idx = (cf::face::find $face) || cf::face::alloc $face; cf::face::set_visibility $idx, $info->{visibility}; cf::face::set_magicmap $idx, $info->{magicmap}; - cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; - cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; + cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; + cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; cf::cede_to_tick; } @@ -2794,35 +2897,62 @@ # TODO: for gcfclient pleasure, we should give resources # that gcfclient doesn't grok a >10000 face index. my $res = $facedata->{resource}; - my $enc = JSON::XS->new->utf8->canonical; - while (my ($name, $info) = each %$res) { - my $meta = $enc->encode ({ - name => $name, - type => $info->{type}, - copyright => $info->{copyright}, #TODO# - }); + my $soundconf = delete $res->{"res/sound.conf"}; + while (my ($name, $info) = each %$res) { my $idx = (cf::face::find $name) || cf::face::alloc $name; + my $data; - if ($name =~ /\.jpg$/) { - cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack - cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack - } else { - my $data = pack "(w/a*)*", $meta, $info->{data}; - my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata + if ($info->{type} & 1) { + # prepend meta info + + my $meta = $enc->encode ({ + name => $name, + %{ $info->{meta} || {} }, + }); - cf::face::set_type $idx, 1; - cf::face::set_data $idx, 0, $data, $chk; + $data = pack "(w/a*)*", $meta, $info->{data}; + } else { + $data = $info->{data}; } + cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; + cf::face::set_type $idx, $info->{type}; + cf::cede_to_tick; } + + if ($soundconf) { + $soundconf = $enc->decode (delete $soundconf->{data}); + + for (0 .. SOUND_CAST_SPELL_0 - 1) { + my $sound = $soundconf->{compat}[$_] + or next; + + my $face = cf::face::find "sound/$sound->[1]"; + cf::sound::set $sound->[0] => $face; + cf::sound::old_sound_index $_, $face; # gcfclient-compat + } + + while (my ($k, $v) = each %{$soundconf->{event}}) { + my $face = cf::face::find "sound/$v"; + cf::sound::set $k => $face; + } + } } 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";