--- deliantra/server/lib/cf.pm 2007/07/03 05:14:15 1.293 +++ deliantra/server/lib/cf.pm 2008/04/11 14:09:57 1.417 @@ -1,3 +1,24 @@ +# +# This file is part of Deliantra, the Roguelike Realtime MMORPG. +# +# Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team +# +# Deliantra is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# The authors can be reached via e-mail to +# + package cf; use utf8; @@ -6,31 +27,34 @@ use Symbol; use List::Util; use Socket; -use Storable; -use Event; +use EV 3.2; use Opcode; use Safe; use Safe::Hole; +use Storable (); -use Coro 3.61 (); +use Coro 4.50 (); use Coro::State; use Coro::Handle; -use Coro::Event; +use Coro::EV; use Coro::Timer; use Coro::Signal; use Coro::Semaphore; use Coro::AIO; +use Coro::BDB; use Coro::Storable; +use Coro::Util (); -use JSON::XS 1.4 (); +use JSON::XS 2.01 (); use BDB (); use Data::Dumper; use Digest::MD5; use Fcntl; -use YAML::Syck (); -use IO::AIO 2.32 (); +use YAML (); +use IO::AIO 2.51 (); use Time::HiRes; use Compress::LZF; +use Digest::MD5 (); # configure various modules to our taste # @@ -38,11 +62,6 @@ Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later -$Event::Eval = 1; # no idea why this is required, but it is - -# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? -$YAML::Syck::ImplicitUnicode = 1; - $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload @@ -70,25 +89,28 @@ our $PLAYERDIR = "$LOCALDIR/" . playerdir; our $RANDOMDIR = "$LOCALDIR/random"; our $BDBDIR = "$LOCALDIR/db"; +our %RESOURCE; our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) -our $TICK_WATCHER; our $AIO_POLL_WATCHER; our $NEXT_RUNTIME_WRITE; # when should the runtime file be written our $NEXT_TICK; -our $NOW; our $USE_FSYNC = 1; # use fsync to write maps - default off our $BDB_POLL_WATCHER; +our $BDB_DEADLOCK_WATCHER; +our $BDB_CHECKPOINT_WATCHER; +our $BDB_TRICKLE_WATCHER; our $DB_ENV; our %CFG; our $UPTIME; $UPTIME ||= time; our $RUNTIME; +our $NOW; -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 ∕ @@ -96,7 +118,8 @@ our $LOAD; # a number between 0 (idle) and 1 (too many objects) our $LOADAVG; # same thing, but with alpha-smoothing -our $tick_start; # for load detecting purposes +our $JITTER; # average jitter +our $TICK_START; # for load detecting purposes binmode STDOUT; binmode STDERR; @@ -159,7 +182,7 @@ =item %cf::CFG -Configuration for the server, loaded from C, or +Configuration for the server, loaded from C, or from wherever your confdir points to. =item cf::wait_for_tick, cf::wait_for_tick_begin @@ -168,6 +191,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 @@ -181,11 +210,25 @@ $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; - utf8::encode $msg; LOG llevError, $msg; }; } +$Coro::State::DIEHOOK = sub { + return unless $^S eq 0; # "eq", not "==" + + if ($Coro::current == $Coro::main) {#d# + warn "DIEHOOK called in main context, Coro bug?\n";#d# + return;#d# + }#d# + + # kill coroutine otherwise + warn Carp::longmess $_[0]; + Coro::terminate +}; + +$SIG{__DIE__} = sub { }; #d#? + @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; @@ -207,7 +250,7 @@ @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } -$Event::DIED = sub { +$EV::DIED = sub { warn "error in event callback: @_"; }; @@ -240,11 +283,11 @@ } || "[unable to dump $_[0]: '$@']"; } -=item $ref = cf::from_json $json +=item $ref = cf::decode_json $json Converts a JSON string into the corresponding perl data structure. -=item $json = cf::to_json $ref +=item $json = cf::encode_json $ref Converts a perl data structure into its JSON representation. @@ -252,8 +295,8 @@ our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max -sub to_json ($) { $json_coder->encode ($_[0]) } -sub from_json ($) { $json_coder->decode ($_[0]) } +sub encode_json($) { $json_coder->encode ($_[0]) } +sub decode_json($) { $json_coder->decode ($_[0]) } =item cf::lock_wait $string @@ -266,6 +309,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). @@ -276,10 +322,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; @@ -294,8 +346,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} }; } @@ -308,13 +362,82 @@ } sub freeze_mainloop { - return unless $TICK_WATCHER->is_active; + tick_inhibit_inc; - my $guard = Coro::guard { - $TICK_WATCHER->start; - }; - $TICK_WATCHER->stop; - $guard + Coro::guard \&tick_inhibit_dec; +} + +=item cf::periodic $interval, $cb + +Like EV::periodic, but randomly selects a starting point so that the actions +get spread over timer. + +=cut + +sub periodic($$) { + my ($interval, $cb) = @_; + + my $start = rand List::Util::min 180, $interval; + + EV::periodic $start, $interval, 0, $cb +} + +=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 { + $Coro::current->desc ("timeslot manager"); + + 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 } @@ -329,8 +452,8 @@ =item cf::sync_job { BLOCK } -The design of Crossfire TRT requires that the main coroutine ($Coro::main) -is always able to handle events or runnable, as Crossfire TRT is only +The design of Deliantra requires that the main coroutine ($Coro::main) +is always able to handle events or runnable, as Deliantra is only partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. @@ -345,34 +468,36 @@ my ($job) = @_; if ($Coro::current == $Coro::main) { - my $time = Event::time; + my $time = EV::time; # this is the main coro, too bad, we have to block # till the operation succeeds, freezing the server :/ - # TODO: use suspend/resume instead - # (but this is cancel-safe) + LOG llevError, Carp::longmess "sync job";#d# + my $freeze_guard = freeze_mainloop; my $busy = 1; my @res; (async { + $Coro::current->desc ("sync job coro"); @res = eval { $job->() }; warn $@ if $@; undef $busy; })->prio (Coro::PRIO_MAX); while ($busy) { - Coro::cede or Event::one_event; + if (Coro::nready) { + Coro::cede_notself; + } else { + EV::loop EV::LOOP_ONESHOT; + } } - $time = Event::time - $time; - - LOG llevError | logBacktrace, Carp::longmess "long sync job" - if $time > $TICK * 0.5 && $TICK_WATCHER->is_active; + my $time = EV::time - $time; - $tick_start += $time; # do not account sync jobs to server load + $TICK_START += $time; # do not account sync jobs to server load wantarray ? @res : $res[0] } else { @@ -407,43 +532,50 @@ Executes the given code block with the given arguments in a seperate process, returning the results. Everything must be serialisable with Coro::Storable. May, of course, block. Note that the executed sub may -never block itself or use any form of Event handling. +never block itself or use any form of event handling. =cut 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; + # we seemingly have to make a local copy of the whole thing, + # otherwise perl prematurely frees the stuff :/ + # TODO: investigate and fix (likely this will be rather laborious) - my $res = (Coro::Handle::unblock $fh1)->readline (undef); - $res = Coro::Storable::thaw $res; + my @res = Coro::Util::fork_eval { + reset_signals; + &$cb + }, @args; - waitpid $pid, 0; # should not block anymore, we expect the child to simply behave + wantarray ? @res : $res[-1] +} - die $$res unless "ARRAY" eq ref $res; +=item $coin = coin_from_name $name - return wantarray ? @$res : $res->[-1]; - } else { - reset_signals; - local $SIG{__WARN__}; - local $SIG{__DIE__}; - eval { - close $fh1; +=cut - my @res = eval { $cb->(@args) }; - syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); - }; +our %coin_alias = ( + "silver" => "silvercoin", + "silvercoin" => "silvercoin", + "silvercoins" => "silvercoin", + "gold" => "goldcoin", + "goldcoin" => "goldcoin", + "goldcoins" => "goldcoin", + "platinum" => "platinacoin", + "platinumcoin" => "platinacoin", + "platinumcoins" => "platinacoin", + "platina" => "platinacoin", + "platinacoin" => "platinacoin", + "platinacoins" => "platinacoin", + "royalty" => "royalty", + "royalties" => "royalty", +); - warn $@ if $@; - _exit 0; - } +sub coin_from_name($) { + $coin_alias{$_[0]} + ? cf::arch::find $coin_alias{$_[0]} + : undef } =item $value = cf::db_get $family => $key @@ -455,25 +587,36 @@ Stores the given C<$value> in the family. It can currently store binary data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). +=item $db = cf::db_table "name" + +Create and/or open a new database table. The string must not be "db" and must be unique +within each server. + =cut -our $DB; +sub db_table($) { + my ($name) = @_; + my $db = BDB::db_create $DB_ENV; -sub db_init { - unless ($DB) { - $DB = BDB::db_create $DB_ENV; + eval { + $db->set_flags (BDB::CHKSUM); - cf::sync_job { - eval { - $DB->set_flags (BDB::CHKSUM); + utf8::encode $name; + BDB::db_open $db, undef, $name, undef, BDB::BTREE, + BDB::CREATE | BDB::AUTO_COMMIT, 0666; + cf::cleanup "db_open(db): $!" if $!; + }; + cf::cleanup "db_open(db): $@" if $@; - BDB::db_open $DB, undef, "db", undef, BDB::BTREE, - BDB::CREATE | BDB::AUTO_COMMIT, 0666; - cf::cleanup "db_open(db): $!" if $!; - }; - cf::cleanup "db_open(db): $@" if $@; - }; - } + $db +} + +our $DB; + +sub db_init { + cf::sync_job { + $DB ||= db_table "db"; + }; } sub db_get($$) { @@ -533,7 +676,7 @@ join "\x00", $processversion, map { - Coro::cede; + cf::cede_to_tick; ($src->[$_], Digest::MD5::md5_hex $data[$_]) } 0.. $#$src; @@ -587,7 +730,7 @@ In the following description, CLASS can be any of C, C C, C or C (i.e. the attachable objects in -Crossfire TRT). +Deliantra). =over 4 @@ -849,18 +992,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; @@ -879,7 +1022,7 @@ 0 } -=item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) +=item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) @@ -887,7 +1030,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 @@ -896,6 +1039,48 @@ ############################################################################# # object support +sub _object_equal($$); +sub _object_equal($$) { + my ($a, $b) = @_; + + return 0 unless (ref $a) eq (ref $b); + + if ("HASH" eq ref $a) { + my @ka = keys %$a; + my @kb = keys %$b; + + return 0 if @ka != @kb; + + for (0 .. $#ka) { + return 0 unless $ka[$_] eq $kb[$_]; + return 0 unless _object_equal $a->{$ka[$_]}, $b->{$kb[$_]}; + } + + } elsif ("ARRAY" eq ref $a) { + + return 0 if @$a != @$b; + + for (0 .. $#$a) { + return 0 unless _object_equal $a->[$_], $b->[$_]; + } + + } elsif ($a ne $b) { + return 0; + } + + 1 +} + +our $SLOW_MERGES;#d# +sub _can_merge { + my ($ob1, $ob2) = @_; + + ++$SLOW_MERGES;#d# + + # we do the slow way here + return _object_equal $ob1, $ob2 +} + sub reattach { # basically do the same as instantiate, without calling instantiate my ($obj) = @_; @@ -925,7 +1110,7 @@ on_instantiate => sub { my ($obj, $data) = @_; - $data = from_json $data; + $data = decode_json $data; for (@$data) { my ($name, $args) = @$_; @@ -951,8 +1136,9 @@ sync_job { if (length $$rdata) { + utf8::decode (my $decname = $filename); warn sprintf "saving %s (%d,%d)\n", - $filename, length $$rdata, scalar @$objs; + $decname, length $$rdata, scalar @$objs; if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { chmod SAVE_MODE, $fh; @@ -963,7 +1149,7 @@ if (@$objs) { if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { chmod SAVE_MODE, $fh; - my $data = Storable::nfreeze { version => 1, objs => $objs }; + my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; aio_write $fh, 0, (length $data), $data, 0; aio_fsync $fh if $cf::USE_FSYNC; close $fh; @@ -981,7 +1167,7 @@ aio_unlink $filename; aio_unlink "$filename.pst"; } - } + }; } sub object_freezer_as_string { @@ -1003,12 +1189,16 @@ unless (aio_stat "$filename.pst") { (aio_load "$filename.pst", $av) >= 0 or return; - $av = eval { (Storable::thaw $av)->{objs} }; + + my $st = eval { Coro::Storable::thaw $av }; + $av = $st->{objs}; } - warn sprintf "loading %s (%d)\n", - $filename, length $data, scalar @{$av || []}; - return ($data, $av); + utf8::decode (my $decname = $filename); + warn sprintf "loading %s (%d,%d)\n", + $decname, length $data, scalar @{$av || []}; + + ($data, $av) } =head2 COMMAND CALLBACKS @@ -1091,11 +1281,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"; } @@ -1158,8 +1357,7 @@ if (exists $v->{meta}{mandatory}) { warn $msg; - warn "mandatory extension failed to load, exiting.\n"; - exit 1; + cf::cleanup "mandatory extension failed to load, exiting."; } warn $msg; @@ -1195,6 +1393,20 @@ =over 4 +=item cf::player::num_playing + +Returns the official number of playing players, as per the Crossfire metaserver rules. + +=cut + +sub num_playing { + scalar grep + $_->ob->map + && !$_->hidden + && !$_->ob->flag (cf::FLAG_WIZ), + cf::player::list +} + =item cf::player::find $login Returns the given player object, loading it if necessary (might block). @@ -1239,8 +1451,13 @@ 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; + + my $pl = cf::player::load_pl $f or return; + local $cf::PLAYER_LOADING{$login} = $pl; + $f->resolve_delayed_derefs; $cf::PLAYER{$login} = $pl } } @@ -1260,7 +1477,7 @@ $pl->{last_save} = $cf::RUNTIME; $pl->save_pl ($path); - Coro::cede; + cf::cede_to_tick; } sub new($) { @@ -1276,6 +1493,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 @@ -1338,9 +1565,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; @@ -1383,73 +1615,115 @@ =cut +use re 'eval'; + +my $group; +my $interior; $interior = qr{ + # match a pod interior sequence sans C<< >> + (?: + \ (.*?)\ (?{ $group = $^N }) + | < (??{$interior}) > + ) +}x; + sub expand_cfpod { - ((my $self), (local $_)) = @_; + my ($self, $pod) = @_; - # escape & and < - s/&/&/g; - s/(?, I<>, U<> etc. - s/B<([^\>]*)>/$1<\/b>/ - || s/I<([^\>]*)>/$1<\/i>/ - || s/U<([^\>]*)>/$1<\/u>/ - # replace G tags - || s{G<([^>|]*)\|([^>]*)>}{ - $self->gender ? $2 : $1 - }ge - # replace H - || s{H<([^\>]*)>} - { - ("[$1 (Use hintmode to suppress hints)]", - "[Hint suppressed, see hintmode]", - "") - [$self->{hintmode}] - }ge; - - # create single paragraphs (very hackish) - s/(?<=\S)\n(?=\w)/ /g; + my $xml; - # compress some whitespace - 1 while s/\s*\n\s*\n\s*/\n/; + while () { + if ($pod =~ /\G( (?: [^BCGHITU]+ | .(?!<) )+ )/xgcs) { + $group = $1; + + $group =~ s/&/&/g; + $group =~ s/]*) (?{ $group = $^N }) + | < $interior > + ) + > + %gcsx + ) { + my ($code, $data) = ($1, $group); + + if ($code eq "B") { + $xml .= "" . expand_cfpod ($self, $data) . ""; + } elsif ($code eq "I") { + $xml .= "" . expand_cfpod ($self, $data) . ""; + } elsif ($code eq "U") { + $xml .= "" . expand_cfpod ($self, $data) . ""; + } elsif ($code eq "C") { + $xml .= "" . expand_cfpod ($self, $data) . ""; + } elsif ($code eq "T") { + $xml .= "" . expand_cfpod ($self, $data) . ""; + } elsif ($code eq "G") { + my ($male, $female) = split /\|/, $data; + $data = $self->gender ? $female : $male; + $xml .= expand_cfpod ($self, $data); + } elsif ($code eq "H") { + $xml .= ("[" . expand_cfpod ($self, $data) . " (Use hintmode to suppress hints)]", + "[Hint suppressed, see hintmode]", + "") + [$self->{hintmode}]; + } else { + $xml .= "error processing '$code($data)' directive"; + } + } else { + if ($pod =~ /\G(.+)/) { + warn "parse error while expanding $pod (at $1)"; + } + last; + } + } + + for ($xml) { + # create single paragraphs (very hackish) + s/(?<=\S)\n(?=\w)/ /g; - s/^\s+//; - s/\s+$//; + # compress some whitespace + s/\s+\n/\n/g; # ws line-ends + s/\n\n+/\n/g; # double lines + s/^\n+//; # beginning lines + s/\n+$//; # ending lines + } - $_ + $xml } +no re 'eval'; + sub hintmode { $_[0]{hintmode} = $_[1] if @_ > 1; $_[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 @@ -1472,7 +1746,7 @@ my ($match, $specificity); for my $region (list) { - if ($region->match && $path =~ $region->match) { + if ($region->{match} && $path =~ $region->{match}) { ($match, $specificity) = ($region, $region->specificity) if $region->specificity > $specificity; } @@ -1602,7 +1876,7 @@ } } - Carp::carp "unable to resolve path '$path' (base '$base')."; + Carp::cluck "unable to resolve path '$path' (base '$base')."; () } @@ -1675,12 +1949,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 @@ -1743,10 +2020,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; @@ -1758,8 +2038,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; } @@ -1778,18 +2059,21 @@ my $path = $self->{path}; { - my $guard = cf::lock_acquire "map_load:$path"; + my $guard = cf::lock_acquire "map_data:$path"; - return if $self->in_memory != cf::MAP_SWAPPED; + return unless $self->valid; + return unless $self->in_memory == cf::MAP_SWAPPED; $self->in_memory (cf::MAP_LOADING); $self->alloc; $self->pre_load; - Coro::cede; + 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) @@ -1797,29 +2081,37 @@ 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; + } } } - Coro::cede; + $f->resolve_delayed_derefs; + + 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); } @@ -1839,10 +2131,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 ($_) @@ -1851,12 +2143,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; } @@ -1871,6 +2165,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 }; } @@ -1878,23 +2175,26 @@ 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 { + $Coro::current->{desc} = "map prefetcher"; + while (%MAP_PREFETCH) { - for my $path (keys %MAP_PREFETCH) { - my $map = find $path - or next; - $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; @@ -1907,7 +2207,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; @@ -1923,6 +2223,7 @@ local $self->{last_access} = $self->last_access;#d# cf::async { + $Coro::current->{desc} = "map player save"; $_->contr->save for $self->players; }; @@ -1940,14 +2241,17 @@ # 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; 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 { @@ -1989,9 +2293,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; @@ -2002,17 +2306,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 } @@ -2071,8 +2379,6 @@ ] } -package cf; - =back =head3 cf::object @@ -2098,6 +2404,34 @@ 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) = @_; + + if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) { + 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 @@ -2129,13 +2463,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; @@ -2144,6 +2473,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" @@ -2230,7 +2569,7 @@ if $x <=0 && $y <= 0; $map->load; - $map->load_diag; + $map->load_neighbours; return unless $self->contr->active; $self->activate_recursive; @@ -2239,19 +2578,20 @@ $self->enter_map ($map, $x, $y); } -=item $player_object->goto ($path, $x, $y[, $check->($map)]) +=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) Moves the player to the given map-path and coordinates by first freezing her, loading and preparing them map, calling the provided $check callback that has to return the map if sucecssful, and then unfreezes the player on -the new (success) or old (failed) map position. +the new (success) or old (failed) map position. In either case, $done will +be called at the end of this process. =cut our $GOTOGEN; sub cf::object::player::goto { - my ($self, $path, $x, $y, $check) = @_; + my ($self, $path, $x, $y, $check, $done) = @_; # do generation counting so two concurrent goto's will be executed in-order my $gen = $self->{_goto_generation} = ++$GOTOGEN; @@ -2259,14 +2599,35 @@ $self->enter_link; (async { + $Coro::current->{desc} = "player::goto $path $x $y"; + + # *tag paths override both path and x|y + if ($path =~ /^\*(.*)$/) { + if (my @obs = grep $_->map, ext::map_tags::find $1) { + my $ob = $obs[rand @obs]; + + # see if we actually can go there + if (@obs = grep !$self->blocked ($_->map, $_->x, $_->y), $ob, $ob->tail) { + $ob = $obs[rand @obs]; + } else { + $self->message ("Wow, it's pretty crowded in there.", cf::NDI_UNIQUE | cf::NDI_RED); + } + # else put us there anyways for now #d# + + ($path, $x, $y) = ($ob->map, $ob->x, $ob->y); + } else { + ($path, $x, $y) = (undef, undef, undef); + } + } + my $map = eval { - my $map = cf::map::find $path; + my $map = defined $path ? cf::map::find $path : undef; if ($map) { $map = $map->customise_for ($self); $map = $check->($map) if $check && $map; } else { - $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); + $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); } $map @@ -2281,6 +2642,8 @@ delete $self->{_goto_generation}; $self->leave_link ($map, $x, $y); } + + $done->() if $done; })->prio (1); } @@ -2327,7 +2690,7 @@ $rmp->{random_seed} ||= $exit->random_seed; - my $data = cf::to_json $rmp; + my $data = cf::encode_json $rmp; my $md5 = Digest::MD5::md5_hex $data; my $meta = "$RANDOMDIR/$md5.meta"; @@ -2359,7 +2722,13 @@ $self->enter_link; + # if exit is damned, update players death & WoR home-position + $self->contr->savebed ($slaying, $hp, $sp) + if $exit->flag (FLAG_DAMNED); + (async { + $Coro::current->{desc} = "enter_exit $slaying $hp $sp"; + $self->deactivate_recursive; # just to be sure unless (eval { $self->goto ($slaying, $hp, $sp); @@ -2395,7 +2764,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 @@ -2404,27 +2773,113 @@ =cut +# non-persistent channels (usually the info channel) +our %CHANNEL = ( + "c/identify" => { + id => "infobox", + title => "Identify", + reply => undef, + tooltip => "Items recently identified", + }, + "c/examine" => { + id => "infobox", + title => "Examine", + reply => undef, + tooltip => "Signs and other items you examined", + }, + "c/book" => { + id => "infobox", + title => "Book", + reply => undef, + tooltip => "The contents of a note or book", + }, + "c/lookat" => { + id => "infobox", + title => "Look", + reply => undef, + tooltip => "What you saw there", + }, + "c/who" => { + id => "infobox", + title => "Players", + reply => undef, + tooltip => "Shows players who are currently online", + }, + "c/body" => { + id => "infobox", + title => "Body Parts", + reply => undef, + tooltip => "Shows which body parts you posess and are available", + }, + "c/uptime" => { + id => "infobox", + title => "Uptime", + reply => undef, + tooltip => "How long the server has been running since last restart", + }, + "c/mapinfo" => { + id => "infobox", + title => "Map Info", + reply => undef, + tooltip => "Information related to the maps", + }, +); + sub cf::client::send_msg { - my ($self, $color, $type, $msg, @extra) = @_; + my ($self, $channel, $msg, $color, @extra) = @_; $msg = $self->pl->expand_cfpod ($msg); - if ($self->can_msg) { - $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $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]/; + $color &= cf::NDI_CLIENT_MASK; # just in case... + + # check predefined channels, for the benefit of C + if ($CHANNEL{$channel}) { + $channel = $CHANNEL{$channel}; + + $self->ext_msg (channel_info => $channel) + if $self->can_msg; + + $channel = $channel->{id}; + + } elsif (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) + if $self->can_msg; } + $channel = $channel->{id}; + } + + return unless @extra || length $msg; + + if ($self->can_msg) { + # 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") @@ -2433,17 +2888,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. + +=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) @@ -2508,12 +2985,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"; } @@ -2570,7 +3055,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 @@ -2578,15 +3067,25 @@ The following functions and methods are available within a safe environment: - cf::object contr pay_amount pay_player map - cf::object::player player - cf::player peaceful - cf::map trigger + cf::object + contr pay_amount pay_player map x y force_find force_add destroy + insert remove name archname title slaying race decrease_ob_nr + + cf::object::player + player + + cf::player + peaceful + + cf::map + trigger =cut for ( - ["cf::object" => qw(contr pay_amount pay_player map)], + ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y + insert remove inv name archname title slaying race + decrease_ob_nr destroy)], ["cf::object::player" => qw(player)], ["cf::player" => qw(peaceful)], ["cf::map" => qw(trigger)], @@ -2672,6 +3171,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; @@ -2683,27 +3188,41 @@ $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}; while (my ($face, $info) = each %$faces) { my $idx = (cf::face::find $face) || cf::face::alloc $face; - cf::face::set $idx, $info->{visibility}, $info->{magicmap}; - cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; - cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; - Coro::cede; + + cf::face::set_visibility $idx, $info->{visibility}; + cf::face::set_magicmap $idx, $info->{magicmap}; + 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; } while (my ($face, $info) = each %$faces) { next unless $info->{smooth}; + my $idx = cf::face::find $face or next; + if (my $smooth = cf::face::find $info->{smooth}) { - cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; + cf::face::set_smooth $idx, $smooth; + cf::face::set_smoothlevel $idx, $info->{smoothlevel}; } else { warn "smooth face '$info->{smooth}' not found for face '$face'"; } - Coro::cede; + + cf::cede_to_tick; } } @@ -2712,18 +3231,90 @@ while (my ($anim, $info) = each %$anims) { cf::anim::set $anim, $info->{frames}, $info->{facings}; - Coro::cede; + cf::cede_to_tick; } cf::anim::invalidate_all; # d'oh } + { + # TODO: for gcfclient pleasure, we should give resources + # that gcfclient doesn't grok a >10000 face index. + my $res = $facedata->{resource}; + + while (my ($name, $info) = each %$res) { + if (defined $info->{type}) { + my $idx = (cf::face::find $name) || cf::face::alloc $name; + my $data; + + if ($info->{type} & 1) { + # prepend meta info + + my $meta = $enc->encode ({ + name => $name, + %{ $info->{meta} || {} }, + }); + + $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}; + } else { + $RESOURCE{$name} = $info; + } + + cf::cede_to_tick; + } + } + + cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); + 1 } +cf::global->attach (on_resource_update => sub { + if (my $soundconf = $RESOURCE{"res/sound.conf"}) { + $soundconf = JSON::XS->new->utf8->relaxed->decode ($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; + } + } +}); + +register_exticmd fx_want => sub { + my ($ns, $want) = @_; + + while (my ($k, $v) = each %$want) { + $ns->fx_want ($k, $v); + } +}; + 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"; + + for (cf::region::list) { + $_->{match} = qr/$_->{match}/ + if exists $_->{match}; + } } sub reload_facedata { @@ -2762,12 +3353,12 @@ reload_resources; } -sub cfg_load { +sub reload_config { open my $fh, "<:utf8", "$CONFDIR/config" or return; local $/; - *CFG = YAML::Syck::Load <$fh>; + *CFG = YAML::Load <$fh>; $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; @@ -2788,16 +3379,18 @@ local $Coro::idle = sub { Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# (async { - Event::one_event; + $Coro::current->{desc} = "IDLE BUG HANDLER"; + EV::loop EV::LOOP_ONESHOT; })->prio (Coro::PRIO_MAX); }; - cfg_load; + reload_config; db_init; load_extensions; - $TICK_WATCHER->start; - Event::loop; + $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority + evthread_start IO::AIO::poll_fileno; + EV::loop; } ############################################################################# @@ -2805,20 +3398,15 @@ # install some emergency cleanup handlers BEGIN { + our %SIGWATCHER = (); for my $signal (qw(INT HUP TERM)) { - Event->signal ( - reentrant => 0, - data => WF_AUTOCANCEL, - signal => $signal, - prio => 0, - cb => sub { - cf::cleanup "SIG$signal"; - }, - ); + $SIGWATCHER{$signal} = EV::signal $signal, sub { + cf::cleanup "SIG$signal"; + }; } } -sub write_runtime { +sub write_runtime_sync { my $runtime = "$LOCALDIR/runtime"; # first touch the runtime file to show we are still running: @@ -2856,6 +3444,49 @@ 1 } +our $uuid_lock; +our $uuid_skip; + +sub write_uuid_sync($) { + $uuid_skip ||= $_[0]; + + return if $uuid_lock; + local $uuid_lock = 1; + + my $uuid = "$LOCALDIR/uuid"; + + my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 + or return; + + my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; + $uuid_skip = 0; + + (aio_write $fh, 0, (length $value), $value, 0) <= 0 + and return; + + # always fsync - this file is important + aio_fsync $fh + and return; + + close $fh + or return; + + aio_rename "$uuid~", $uuid + and return; + + warn "uuid file written ($value).\n"; + + 1 + +} + +sub write_uuid($$) { + my ($skip, $sync) = @_; + + $sync ? write_uuid_sync $skip + : async { write_uuid_sync $skip }; +} + sub emergency_save() { my $freeze_guard = cf::freeze_mainloop; @@ -2869,6 +3500,7 @@ for my $login (keys %cf::PLAYER) { my $pl = $cf::PLAYER{$login} or next; $pl->valid or next; + delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt $pl->save; } warn "end emergency player save\n"; @@ -2884,6 +3516,10 @@ warn "begin emergency database checkpoint\n"; BDB::db_env_txn_checkpoint $DB_ENV; warn "end emergency database checkpoint\n"; + + warn "begin write uuid\n"; + write_uuid_sync 1; + warn "end write uuid\n"; }; warn "leave emergency perl save\n"; @@ -2908,25 +3544,20 @@ warn "entering sync_job"; cf::sync_job { - cf::write_runtime; # external watchdog should not bark + cf::write_runtime_sync; # external watchdog should not bark cf::emergency_save; - cf::write_runtime; # external watchdog should not bark + cf::write_runtime_sync; # external watchdog should not bark warn "syncing database to disk"; BDB::db_env_txn_checkpoint $DB_ENV; # if anything goes wrong in here, we should simply crash as we already saved - warn "cancelling all WF_AUTOCANCEL watchers"; - for (Event::all_watchers) { - $_->cancel if $_->data & WF_AUTOCANCEL; - } - warn "flushing outstanding aio requests"; for (;;) { BDB::flush; IO::AIO::flush; - Coro::cede; + Coro::cede_notself; last unless IO::AIO::nreqs || BDB::nreqs; warn "iterate..."; } @@ -2995,7 +3626,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; @@ -3012,8 +3643,7 @@ 1 } or do { warn $@; - warn "error while reloading, exiting."; - exit 1; + cf::cleanup "error while reloading, exiting."; }; warn "reloaded"; @@ -3025,15 +3655,10 @@ # doing reload synchronously and two reloads happen back-to-back, # coro crashes during coro_state_free->destroy here. - $RELOAD_WATCHER ||= Event->timer ( - reentrant => 0, - after => 0, - data => WF_AUTOCANCEL, - cb => sub { - do_reload_perl; - undef $RELOAD_WATCHER; - }, - ); + $RELOAD_WATCHER ||= EV::timer 0, 0, sub { + do_reload_perl; + undef $RELOAD_WATCHER; + }; } register_command "reload" => sub { @@ -3041,7 +3666,10 @@ if ($who->flag (FLAG_WIZ)) { $who->message ("reloading server."); - async { reload_perl }; + async { + $Coro::current->{desc} = "perl_reload"; + reload_perl; + }; } }; @@ -3053,7 +3681,7 @@ our @WAIT_FOR_TICK_BEGIN; sub wait_for_tick { - return unless $TICK_WATCHER->is_active; + return if tick_inhibit; return if $Coro::current == $Coro::main; my $signal = new Coro::Signal; @@ -3062,7 +3690,7 @@ } sub wait_for_tick_begin { - return unless $TICK_WATCHER->is_active; + return if tick_inhibit; return if $Coro::current == $Coro::main; my $signal = new Coro::Signal; @@ -3070,115 +3698,56 @@ $signal->wait; } - my $min = 1e6;#d# - my $avg = 10; -$TICK_WATCHER = Event->timer ( - reentrant => 0, - parked => 1, - prio => 0, - at => $NEXT_TICK || $TICK, - data => WF_AUTOCANCEL, - cb => sub { - if ($Coro::current != $Coro::main) { - Carp::cluck "major BUG: server tick called outside of main coro, skipping it" - unless ++$bug_warning > 10; - return; - } - - $NOW = $tick_start = Event::time; - - cf::server_tick; # one server iteration - - 0 && sync_job {#d# - for(1..10) { - my $t = Event::time; - my $map = my $map = new_from_path cf::map "/tmp/x.map" - or die; - - $map->width (50); - $map->height (50); - $map->alloc; - $map->_load_objects ("/tmp/x.map", 1); - my $t = Event::time - $t; - - #next unless $t < 0.0013;#d# - if ($t < $min) { - $min = $t; - } - $avg = $avg * 0.99 + $t * 0.01; - } - warn "XXXXXXXXXXXXXXXXXX min $min avg $avg\n";#d# - exit 0; - # 2007-05-22 02:33:04.569 min 0.00112509727478027 avg 0.0012259249572477 - }; - - $RUNTIME += $TICK; - $NEXT_TICK += $TICK; - - if ($NOW >= $NEXT_RUNTIME_WRITE) { - $NEXT_RUNTIME_WRITE = $NOW + 10; - Coro::async_pool { - write_runtime - or warn "ERROR: unable to write runtime file: $!"; - }; - } - -# my $AFTER = Event::time; -# warn $AFTER - $NOW;#d# - - if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { - $sig->send; - } - while (my $sig = shift @WAIT_FOR_TICK) { - $sig->send; - } - - $NOW = Event::time; - - # if we are delayed by four ticks or more, skip them all - $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; +sub tick { + if ($Coro::current != $Coro::main) { + Carp::cluck "major BUG: server tick called outside of main coro, skipping it" + unless ++$bug_warning > 10; + return; + } - $TICK_WATCHER->at ($NEXT_TICK); - $TICK_WATCHER->start; + cf::server_tick; # one server iteration - $LOAD = ($NOW - $tick_start) / $TICK; - $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; + if ($NOW >= $NEXT_RUNTIME_WRITE) { + $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; + Coro::async_pool { + $Coro::current->{desc} = "runtime saver"; + write_runtime_sync + or warn "ERROR: unable to write runtime file: $!"; + }; + } - _post_tick; + if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { + $sig->send; + } + while (my $sig = shift @WAIT_FOR_TICK) { + $sig->send; + } + $LOAD = ($NOW - $TICK_START) / $TICK; + $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; - }, -); + if (0) { + if ($NEXT_TICK) { + my $jitter = $TICK_START - $NEXT_TICK; + $JITTER = $JITTER * 0.75 + $jitter * 0.25; + warn "jitter $JITTER\n";#d# + } + } +} { - BDB::max_poll_time $TICK * 0.1; - $BDB_POLL_WATCHER = Event->io ( - reentrant => 0, - fd => BDB::poll_fileno, - poll => 'r', - prio => 0, - data => WF_AUTOCANCEL, - cb => \&BDB::poll_cb, - ); - BDB::min_parallel 8; + # configure BDB - BDB::set_sync_prepare { - my $status; - my $current = $Coro::current; - ( - sub { - $status = $!; - $current->ready; undef $current; - }, - sub { - Coro::schedule while defined $current; - $! = $status; - }, - ) - }; + BDB::min_parallel 8; + BDB::max_poll_reqs $TICK * 0.1; + $Coro::BDB::WATCHER->priority (1); unless ($DB_ENV) { $DB_ENV = BDB::db_env_create; + $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC + | BDB::LOG_AUTOREMOVE, 1); + $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); + $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); cf::sync_job { eval { @@ -3190,29 +3759,29 @@ 0666; cf::cleanup "db_env_open($BDBDIR): $!" if $!; - - $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); - $DB_ENV->set_lk_detect; }; cf::cleanup "db_env_open(db): $@" if $@; }; } + + $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { + BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; + }; + $BDB_CHECKPOINT_WATCHER = EV::periodic 0, 60, 0, sub { + BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; + }; + $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub { + BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; + }; } { - IO::AIO::min_parallel 8; + # configure IO::AIO - undef $Coro::AIO::WATCHER; + IO::AIO::min_parallel 8; IO::AIO::max_poll_time $TICK * 0.1; - $AIO_POLL_WATCHER = Event->io ( - reentrant => 0, - data => WF_AUTOCANCEL, - fd => IO::AIO::poll_fileno, - poll => 'r', - prio => 6, - cb => \&IO::AIO::poll_cb, - ); + $Coro::AIO::WATCHER->priority (1); } my $_log_backtrace; @@ -3226,6 +3795,8 @@ if ($_log_backtrace < 2) { ++$_log_backtrace; async { + $Coro::current->{desc} = "abt $msg"; + my @bt = fork_call { @addr = map { sprintf "%x", $_ } @addr; my $self = (-f "/proc/$$/exe") ? "/proc/$$/exe" : $^X;