--- deliantra/server/lib/cf.pm 2007/08/28 19:38:40 1.346 +++ deliantra/server/lib/cf.pm 2007/09/08 18:15:55 1.359 @@ -89,8 +89,8 @@ our $UPTIME; $UPTIME ||= time; our $RUNTIME; -our %PLAYER; # all users -our %MAP; # all maps +our (%PLAYER, %PLAYER_LOADING); # all users +our (%MAP, %MAP_LOADING ); # all maps our $LINK_MAP; # the special {link} map, which is always available # used to convert map paths into valid unix filenames by replacing / by ∕ @@ -274,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). @@ -284,10 +287,16 @@ =cut our %LOCK; +our %LOCKER;#d# sub lock_wait($) { my ($key) = @_; + if ($LOCKER{$key} == $Coro::current) {#d# + Carp::cluck "lock_wait($key) for already-acquired lock";#d# + return;#d# + }#d# + # wait for lock, if any while ($LOCK{$key}) { push @{ $LOCK{$key} }, $Coro::current; @@ -302,8 +311,10 @@ lock_wait $key; $LOCK{$key} = []; + $LOCKER{$key} = $Coro::current;#d# Coro::guard { + delete $LOCKER{$key};#d# # wake up all waiters, to be on the safe side $_->ready for @{ delete $LOCK{$key} }; } @@ -480,7 +491,7 @@ # 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) + # TODO: investigate and fix (likely this will be rather laborious) my @res = Coro::Util::fork_eval { reset_signals; @@ -1004,6 +1015,8 @@ sub object_freezer_save { my ($filename, $rdata, $objs) = @_; + my $guard = cf::lock_acquire "io"; + sync_job { if (length $$rdata) { warn sprintf "saving %s (%d,%d)\n", @@ -1036,7 +1049,9 @@ aio_unlink $filename; aio_unlink "$filename.pst"; } - } + }; + + undef $guard; } sub object_freezer_as_string { @@ -1052,18 +1067,23 @@ my ($data, $av); + my $guard = cf::lock_acquire "io"; + (aio_load $filename, $data) >= 0 or return; unless (aio_stat "$filename.pst") { (aio_load "$filename.pst", $av) >= 0 or return; + + undef $guard; $av = eval { (Storable::thaw $av)->{objs} }; } warn sprintf "loading %s (%d)\n", $filename, length $data, scalar @{$av || []}; - return ($data, $av); + + ($data, $av) } =head2 COMMAND CALLBACKS @@ -1303,8 +1323,14 @@ aio_unlink +(playerdir $login) . "/$login.pl.pst"; aio_unlink +(playerdir $login) . "/$login.pl"; - my $pl = load_pl path $login + my $f = new_from_file cf::object::thawer path $login + or return; + + $f->next; + my $pl = cf::player::load_pl $f or return; + local $cf::PLAYER_LOADING{$login} = $pl; + $f->resolve_delayed_derefs; $cf::PLAYER{$login} = $pl } } @@ -1412,9 +1438,14 @@ my @logins; for my $login (@$dirs) { - my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; - aio_read $fh, 0, 512, my $buf, 0 or next; - $buf !~ /^password -------------$/m or next; # official not-valid tag + my $path = path $login; + + # a .pst is a dead give-away for a valid player + unless (-e "$path.pst") { + my $fh = aio_open $path, Fcntl::O_RDONLY, 0 or next; + aio_read $fh, 0, 512, my $buf, 0 or next; + $buf !~ /^password -------------$/m or next; # official not-valid tag + } utf8::decode $login; push @logins, $login; @@ -1462,7 +1493,7 @@ # escape & and < s/&/&/g; - s/(?]*)>/$1<\/b>/ || s/I<([^\>]*)>/$1<\/i>/ || s/U<([^\>]*)>/$1<\/u>/ + || s/T<([^\>]*)>/$1<\/b><\/big>/ # replace G tags || s{G<([^>|]*)\|([^>]*)>}{ $self->gender ? $2 : $1 @@ -1748,12 +1780,15 @@ my ($self, $path) = @_; utf8::encode $path; - #aio_open $path, O_RDONLY, 0 - # or return; + my $f = new_from_file cf::object::thawer $path + or return; - $self->_load_header ($path) + $self->_load_header ($f) or return; + local $MAP_LOADING{$self->{path}} = $self; + $f->resolve_delayed_derefs; + $self->{load_path} = $path; 1 @@ -1816,10 +1851,13 @@ $path = normalise $path, $origin && $origin->path; + cf::lock_wait "map_data:$path";#d#remove cf::lock_wait "map_find:$path"; $cf::MAP{$path} || do { - my $guard = cf::lock_acquire "map_find:$path"; + my $guard1 = cf::lock_acquire "map_find:$path"; + my $guard2 = cf::lock_acquire "map_data:$path"; # just for the fun of it + my $map = new_from_path cf::map $path or return; @@ -1831,8 +1869,9 @@ if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) # doing this can freeze the server in a sync job, obviously #$cf::WAIT_FOR_TICK->wait; + undef $guard1; + undef $guard2; $map->reset; - undef $guard; return find $path; } @@ -1851,9 +1890,9 @@ my $path = $self->{path}; { - my $guard1 = cf::lock_acquire "map_data:$path"; - my $guard2 = cf::lock_acquire "map_load:$path"; + my $guard = cf::lock_acquire "map_data:$path"; + return unless $self->valid; return if $self->in_memory != cf::MAP_SWAPPED; $self->in_memory (cf::MAP_LOADING); @@ -1863,7 +1902,9 @@ $self->pre_load; cf::cede_to_tick; - $self->_load_objects ($self->{load_path}, 1) + my $f = new_from_file cf::object::thawer $self->{load_path}; + $f->skip_block; + $self->_load_objects ($f) or return; $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) @@ -1871,12 +1912,17 @@ if (my $uniq = $self->uniq_path) { utf8::encode $uniq; - if (aio_open $uniq, O_RDONLY, 0) { - $self->clear_unique_items; - $self->_load_objects ($uniq, 0); + unless (aio_stat $uniq) { + if (my $f = new_from_file cf::object::thawer $uniq) { + $self->clear_unique_items; + $self->_load_objects ($f); + $f->resolve_delayed_derefs; + } } } + $f->resolve_delayed_derefs; + cf::cede_to_tick; # now do the right thing for maps $self->link_multipart_objects; @@ -2029,8 +2075,11 @@ return if $self->in_memory != cf::MAP_IN_MEMORY; return if $self->{deny_save}; - $self->clear; $self->in_memory (cf::MAP_SWAPPED); + + $self->deactivate; + $_->clear_links_to ($self) for values %cf::MAP; + $self->clear; } sub reset_at { @@ -2072,9 +2121,9 @@ delete $cf::MAP{$self->path}; - $self->clear; - + $self->deactivate; $_->clear_links_to ($self) for values %cf::MAP; + $self->clear; $self->unlink_save; $self->destroy; @@ -2085,17 +2134,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 } @@ -2181,6 +2234,37 @@ inv_recursive_ inv $_[0] } +=item $ref = $ob->ref + +creates and returns a persistent reference to an objetc that can be stored as a string. + +=item $ob = cf::object::deref ($refstring) + +returns the objetc referenced by refstring. may return undef when it cnanot find the object, +even if the object actually exists. May block. + +=cut + +sub deref { + my ($ref) = @_; + + # temporary compatibility#TODO#remove + $ref =~ s{^<}{player/<}; + + if ($ref =~ m{^player\/(<1\.\d+>)/(.*)$}) { + my ($uuid, $name) = ($1, $2); + my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name + or return; + $pl->ob->uuid eq $uuid + or return; + + $pl->ob + } else { + warn "$ref: cannot resolve object reference\n"; + undef + } +} + package cf; =back @@ -2499,6 +2583,21 @@ =cut +our %CHANNEL = ( + "c/identify" => { + id => "identify", + title => "Identify", + reply => undef, + tooltip => "Items recently identified", + }, + "c/examine" => { + id => "examine", + title => "Examine", + reply => undef, + tooltip => "Signs and other items you examined", + }, +); + sub cf::client::send_msg { my ($self, $channel, $msg, $color, @extra) = @_; @@ -2506,11 +2605,15 @@ $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 unless (exists $self->{channel}{$channel->{id}}) { $self->{channel}{$channel->{id}} = $channel; - $self->ext_msg (channel_info => $channel); + $self->ext_msg (channel_info => $channel) + if $self->can_msg; } $channel = $channel->{id}; @@ -2835,6 +2938,10 @@ 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"; @@ -2955,6 +3062,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"; @@ -3336,7 +3447,7 @@ $map->width (50); $map->height (50); $map->alloc; - $map->_load_objects ("/tmp/x.map", 1); + $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work my $t = Event::time - $t; #next unless $t < 0.0013;#d#