--- deliantra/server/lib/cf.pm 2007/07/27 10:10:51 1.322 +++ deliantra/server/lib/cf.pm 2007/09/02 00:57:38 1.351 @@ -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; @@ -273,6 +274,9 @@ for example when the coroutine gets canceled), the lock is automatically returned. +Locks are *not* recursive, locking from the same coro twice results in a +deadlocked coro. + Lock names should begin with a unique identifier (for example, cf::map::find uses map_find and cf::map::load uses map_load). @@ -474,57 +478,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: $!"; - - 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; + # 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) - 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); - - eval { - close $fh1; - - my @res = eval { $cb->(@args) }; - - syswrite $fh2, _store_scalar $@ ? \"$@" : \@res; - close $fh2; - }; + &$cb + }, @args; - warn $@ if $@; - _exit 0; - } + wantarray ? @res : $res[-1] } =item $value = cf::db_get $family => $key @@ -614,7 +580,7 @@ join "\x00", $processversion, map { - Coro::cede; + cf::cede_to_tick; ($src->[$_], Digest::MD5::md5_hex $data[$_]) } 0.. $#$src; @@ -1188,12 +1154,15 @@ ? @$msg : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove - if (my $cb = $EXTCMD{$type}) { - my @reply = $cb->($pl, @payload); + my @reply; - $pl->ext_reply ($reply, @reply) - if $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"; } @@ -1358,7 +1327,7 @@ $pl->{last_save} = $cf::RUNTIME; $pl->save_pl ($path); - Coro::cede; + cf::cede_to_tick; } sub new($) { @@ -1374,6 +1343,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 @@ -1534,13 +1513,7 @@ sub ext_reply($$@) { my ($self, $id, @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})); - } + $self->ns->ext_reply ($id, @msg) } =item $player->ext_msg ($type, @msg) @@ -1881,7 +1854,8 @@ my $path = $self->{path}; { - my $guard = cf::lock_acquire "map_load:$path"; + my $guard1 = cf::lock_acquire "map_data:$path"; + my $guard2 = cf::lock_acquire "map_load:$path"; return if $self->in_memory != cf::MAP_SWAPPED; @@ -1890,7 +1864,7 @@ $self->alloc; $self->pre_load; - Coro::cede; + cf::cede_to_tick; $self->_load_objects ($self->{load_path}, 1) or return; @@ -1906,23 +1880,26 @@ } } - Coro::cede; + cf::cede_to_tick; # now do the right thing for maps $self->link_multipart_objects; $self->difficulty ($self->estimate_difficulty) unless $self->difficulty; - Coro::cede; + cf::cede_to_tick; unless ($self->{deny_activate}) { $self->decay_objects; $self->fix_auto_apply; $self->update_buttons; - Coro::cede; + cf::cede_to_tick; $self->set_darkness_map; - Coro::cede; + cf::cede_to_tick; $self->activate; } + $self->{last_save} = $cf::RUNTIME; + $self->last_access ($cf::RUNTIME); + $self->in_memory (cf::MAP_IN_MEMORY); } @@ -1942,10 +1919,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 ($_) @@ -1954,12 +1931,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; } @@ -1974,6 +1953,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 }; } @@ -1981,23 +1963,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; @@ -2010,7 +1993,7 @@ sub save { my ($self) = @_; - my $lock = cf::lock_acquire "map_data:" . $self->path; + my $lock = cf::lock_acquire "map_data:$self->{path}"; $self->{last_save} = $cf::RUNTIME; @@ -2043,7 +2026,7 @@ # save first because save cedes $self->save; - my $lock = cf::lock_acquire "map_data:" . $self->path; + my $lock = cf::lock_acquire "map_data:$self->{path}"; return if $self->players; return if $self->in_memory != cf::MAP_IN_MEMORY; @@ -2105,17 +2088,21 @@ sub nuke { my ($self) = @_; - delete $cf::MAP{$self->path}; + { + my $lock = cf::lock_acquire "map_data:$self->{path}"; - $self->unlink_save; + delete $cf::MAP{$self->path}; - bless $self, "cf::map"; - delete $self->{deny_reset}; - $self->{deny_save} = 1; - $self->reset_timeout (1); - $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); + $self->unlink_save; - $cf::MAP{$self->path} = $self; + bless $self, "cf::map"; + delete $self->{deny_reset}; + $self->{deny_save} = 1; + $self->reset_timeout (1); + $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); + + $cf::MAP{$self->path} = $self; + } $self->reset; # polite request, might not happen } @@ -2242,6 +2229,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" @@ -2328,7 +2325,7 @@ if $x <=0 && $y <= 0; $map->load; - $map->load_diag; + $map->load_neighbours; return unless $self->contr->active; $self->activate_recursive; @@ -2509,12 +2506,24 @@ =cut +our %CHANNEL = ( + "c/identify" => { + id => "identify", + title => "Identify", + reply => undef, + tooltip => "Items recently identified", + }, +); + sub cf::client::send_msg { my ($self, $channel, $msg, $color, @extra) = @_; $msg = $self->pl->expand_cfpod ($msg); - $color &= ~cf::NDI_UNIQUE; # just in case... + $color &= cf::NDI_CLIENT_MASK; # just in case... + + # check predefined channels, for the benefit of C + $channel = $CHANNEL{$channel} if $CHANNEL{$channel}; if (ref $channel) { # send meta info to client, if not yet sent @@ -2529,21 +2538,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") @@ -2561,16 +2580,32 @@ sub cf::client::ext_msg($$@) { my ($self, $type, @msg) = @_; - my $extcmd = $self->extcmd; - - if ($extcmd == 2) { + if ($self->extcmd == 2) { $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); - } elsif ($extcmd == 1) { # TODO: remove + } 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. + +=cut + +sub cf::client::ext_reply($$@) { + my ($self, $id, @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) Queues a query to the client, calling the given callback with @@ -2638,12 +2673,15 @@ ? @$msg : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove - if (my $cb = $EXTICMD{$type}) { - my @reply = $cb->($ns, @payload); + my @reply; - $ns->ext_reply ($reply, @reply) - if $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"; } @@ -2700,7 +2738,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 @@ -2710,7 +2752,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 @@ -2725,7 +2767,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)], @@ -2811,6 +2854,12 @@ sub load_facedata($) { my ($path) = @_; + # HACK to clear player env face cache, we need some signal framework + # for this (global event?) + %ext::player_env::MUSIC_FACE_CACHE = (); + + my $enc = JSON::XS->new->utf8->canonical->relaxed; + warn "loading facedata from $path\n"; my $facedata; @@ -2822,6 +2871,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}; @@ -2829,8 +2885,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; } @@ -2865,29 +2921,27 @@ # 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; my $soundconf = delete $res->{"res/sound.conf"}; while (my ($name, $info) = each %$res) { - my $meta = $enc->encode ({ - name => $name, - %{ $info->{meta} || {} }, - }); - my $idx = (cf::face::find $name) || cf::face::alloc $name; + my $data; 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 + my $meta = $enc->encode ({ + name => $name, + %{ $info->{meta} || {} }, + }); - cf::face::set_data $idx, 0, $data, $chk; + $data = pack "(w/a*)*", $meta, $info->{data}; } else { - cf::face::set_data $idx, 0, $info->{data}, $info->{chksum}; + $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; @@ -2901,12 +2955,14 @@ or next; my $face = cf::face::find "sound/$sound->[1]"; - cf::sound::set $sound->[0] => $face; cf::sound::old_sound_index $_, $face; # gcfclient-compat } - #TODO + while (my ($k, $v) = each %{$soundconf->{event}}) { + my $face = cf::face::find "sound/$v"; + cf::sound::set $k => $face; + } } } @@ -2922,6 +2978,10 @@ }; sub reload_regions { + # HACK to clear player env face cache, we need some signal framework + # for this (global event?) + %ext::player_env::MUSIC_FACE_CACHE = (); + load_resource_file "$MAPDIR/regions" or die "unable to load regions file\n"; @@ -2967,7 +3027,7 @@ reload_resources; } -sub cfg_load { +sub reload_config { open my $fh, "<:utf8", "$CONFDIR/config" or return; @@ -2997,7 +3057,7 @@ })->prio (Coro::PRIO_MAX); }; - cfg_load; + reload_config; db_init; load_extensions; @@ -3200,7 +3260,7 @@ cf::_connect_to_perl; # nominally unnecessary, but cannot hurt warn "loading config and database again"; - cf::cfg_load; + cf::reload_config; warn "loading extensions"; cf::load_extensions;