--- deliantra/server/lib/cf.pm 2008/04/24 04:40:31 1.426
+++ deliantra/server/lib/cf.pm 2008/08/31 09:03:31 1.442
@@ -21,27 +21,30 @@
package cf;
+use 5.10.0;
use utf8;
-use strict;
+use strict "vars", "subs";
use Symbol;
use List::Util;
use Socket;
-use EV 3.2;
+use EV;
use Opcode;
use Safe;
use Safe::Hole;
use Storable ();
-use Coro 4.50 ();
+use Coro ();
use Coro::State;
use Coro::Handle;
use Coro::EV;
+use Coro::AnyEvent;
use Coro::Timer;
use Coro::Signal;
use Coro::Semaphore;
+use Coro::AnyEvent;
use Coro::AIO;
-use Coro::BDB;
+use Coro::BDB 1.6;
use Coro::Storable;
use Coro::Util ();
@@ -51,11 +54,13 @@
use Digest::MD5;
use Fcntl;
use YAML ();
-use IO::AIO 2.51 ();
+use IO::AIO ();
use Time::HiRes;
use Compress::LZF;
use Digest::MD5 ();
+AnyEvent::detect;
+
# configure various modules to our taste
#
$Storable::canonical = 1; # reduce rsync transfers
@@ -92,12 +97,10 @@
our %RESOURCE;
our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
-our $AIO_POLL_WATCHER;
our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
our $NEXT_TICK;
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;
@@ -244,9 +247,9 @@
cf::object cf::object::player
cf::client cf::player
cf::arch cf::living
- cf::map cf::party cf::region
+ cf::map cf::mapspace
+ cf::party cf::region
)) {
- no strict 'refs';
@{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
}
@@ -1087,7 +1090,8 @@
# basically do the same as instantiate, without calling instantiate
my ($obj) = @_;
- bless $obj, ref $obj; # re-bless in case extensions have been reloaded
+ # no longer needed after getting rid of delete_package?
+ #bless $obj, ref $obj; # re-bless in case extensions have been reloaded
my $registry = $obj->registry;
@@ -1143,18 +1147,18 @@
$decname, length $$rdata, scalar @$objs;
if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
- chmod SAVE_MODE, $fh;
+ aio_chmod $fh, SAVE_MODE;
aio_write $fh, 0, (length $$rdata), $$rdata, 0;
aio_fsync $fh if $cf::USE_FSYNC;
- close $fh;
+ aio_close $fh;
if (@$objs) {
if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
- chmod SAVE_MODE, $fh;
+ aio_chmod $fh, SAVE_MODE;
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;
+ aio_close $fh;
aio_rename "$filename.pst~", "$filename.pst";
}
} else {
@@ -1333,7 +1337,7 @@
if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
$ext{source} =
- "package $pkg; use strict; use utf8;\n"
+ "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n"
. "#line 1 \"$path\"\n{\n"
. $source
. "\n};\n1";
@@ -1458,6 +1462,7 @@
my $pl = cf::player::load_pl $f
or return;
+
local $cf::PLAYER_LOADING{$login} = $pl;
$f->resolve_delayed_derefs;
$cf::PLAYER{$login} = $pl
@@ -1524,6 +1529,8 @@
$pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
$pl->deactivate;
+ my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
+ $pl->ob->check_score;
$pl->invoke (cf::EVENT_PLAYER_QUIT);
$pl->ns->destroy if $pl->ns;
@@ -1572,7 +1579,8 @@
my $path = path $login;
# a .pst is a dead give-away for a valid player
- unless (-e "$path.pst") {
+ # if no pst file found, open and chekc for blocked users
+ if (aio_stat "$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
@@ -1619,88 +1627,90 @@
=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, $pod) = @_;
+ my @nest = [qr<\G$>, undef, ""];
my $xml;
- while () {
- if ($pod =~ /\G( (?: [^BCGHITU]+ | .(?!<) )+ )/xgcs) {
- $group = $1;
+ for ($pod) {
+ while () {
+ if (/\G( (?: [^BCEGHITUZ&>\n\ ]+ | [BCEGHITUZ](?!<) | \ (?!>) )+ )/xgcs) {
+ $xml .= $1;
+ } elsif (/\G\n(?=\S)/xgcs) {
+ $xml .= " ";
+ } elsif (/\G\n/xgcs) {
+ $xml .= "\n";
+ } elsif (/\G ([BCEGHITUZ]) (< (?: <+\ | (?!<) ) )/xgcs) {
+ my ($code, $delim) = ($1, scalar reverse $2);
+ $delim =~ y/>/; # delim now contains the stop sequence
+ $delim = qr{\G\Q$delim};
+
+ my $cb;
+
+ if ($code eq "B") {
+ $cb = sub { "$_[0]" };
+ } elsif ($code eq "C") {
+ $cb = sub { "$_[0]" };
+ } elsif ($code eq "E") {
+ $cb = sub { warn "E<$_[0]>\n";"&$_[0];" };
+ } elsif ($code eq "G") {
+ $cb = sub {
+ my ($male, $female) = split /\|/, $_[0];
+ $self->gender ? $female : $male
+ };
+ } elsif ($code eq "H") {
+ $cb = sub {
+ (
+ "[$_[0] (Use hintmode to suppress hints)]",
+ "[Hint suppressed, see hintmode]",
+ "",
+ )[$self->{hintmode}];
+ };
+ } elsif ($code eq "I") {
+ $cb = sub { "$_[0]" };
+ } elsif ($code eq "T") {
+ $cb = sub { "$_[0]" };
+ } elsif ($code eq "U") {
+ $cb = sub { "$_[0]" };
+ } elsif ($code eq "Z") {
+ $cb = sub { };
+ } else {
+ die "FATAL error in expand_cfpod";
+ }
- $group =~ s/&/&/g;
- $group =~ s/</g;
+ push @nest, [$delim, $cb, $xml];
+ undef $xml;
- $xml .= $group;
- } elsif ($pod =~ m%\G
- ([BCGHITU])
- <
- (?:
- ([^<>]*) (?{ $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}];
+ } elsif ($_ =~ /$nest[-1][0]/gcs) {
+ my $nest = pop @nest;
+
+ if ($nest->[1]) {
+ $xml = $nest->[2] . $nest->[1]->($xml);
+ } else {
+ last;
+ }
+ } elsif (/\G/xgcs) {
+ $xml .= ">";
} else {
- $xml .= "error processing '$code($data)' directive";
- }
- } else {
- if ($pod =~ /\G(.+)/) {
- warn "parse error while expanding $pod (at $1)";
+ if ($pod =~ /\G(.+)/xgcs) {
+ warn "parse error (at $1)($nest[-1][0]) while expanding cfpod:\n$pod";
+ last;
+ } else {
+ warn "parse error (unclosed interior sequence at end of cfpod) while expanding cfpod:\n$pod";
+ return "Sorry, the server encountered an internal error when formatting this message, please report this.";
+ }
}
- last;
}
}
- for ($xml) {
- # create single paragraphs (very hackish)
- s/(?<=\S)\n(?=\w)/ /g;
-
- # 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}
@@ -2031,8 +2041,8 @@
cf::lock_wait "map_find:$path";
$cf::MAP{$path} || do {
- my $guard1 = cf::lock_acquire "map_find:$path";
- my $guard2 = cf::lock_acquire "map_data:$path"; # just for the fun of it
+ my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it
+ my $guard2 = cf::lock_acquire "map_find:$path";
my $map = new_from_path cf::map $path
or return;
@@ -2045,8 +2055,8 @@
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;
+ undef $guard1;
$map->reset;
return find $path;
}
@@ -2083,7 +2093,7 @@
$self->_load_objects ($f)
or return;
- $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
+ $self->post_load_original
if delete $self->{load_original};
if (my $uniq = $self->uniq_path) {
@@ -2459,6 +2469,20 @@
=cut
+our $SAY_CHANNEL = {
+ id => "say",
+ title => "Map",
+ reply => "say ",
+ tooltip => "Things said to and replied from npcs near you and other players on the same map only.",
+};
+
+our $CHAT_CHANNEL = {
+ id => "chat",
+ title => "Chat",
+ reply => "chat ",
+ tooltip => "Player chat and shouts, global to the server.",
+};
+
# rough implementation of a future "reply" method that works
# with dialog boxes.
#TODO: the first argument must go, split into a $npc->reply_to ( method
@@ -2479,7 +2503,7 @@
} else {
$msg = $npc->name . " says: $msg" if $npc;
- $self->message ($msg, $flags);
+ $self->send_msg ($SAY_CHANNEL => $msg, $flags);
}
}
}
@@ -2597,6 +2621,9 @@
the new (success) or old (failed) map position. In either case, $done will
be called at the end of this process.
+Note that $check will be called with a potentially non-loaded map, so if
+it needs a loaded map it has to call C<< ->load >>.
+
=cut
our $GOTOGEN;
@@ -2684,8 +2711,6 @@
sub prepare_random_map {
my ($exit) = @_;
- my $guard = cf::lock_acquire "exit_prepare:$exit";
-
# all this does is basically replace the /! path by
# a new random map path (?random/...) with a seed
# that depends on the exit object
@@ -2697,6 +2722,8 @@
$rmp->{origin_map} = $exit->map->path;
$rmp->{origin_x} = $exit->x;
$rmp->{origin_y} = $exit->y;
+
+ $exit->map->touch;
}
$rmp->{random_seed} ||= $exit->random_seed;
@@ -2710,8 +2737,12 @@
undef $fh;
aio_rename "$meta~", $meta;
- $exit->slaying ("?random/$md5");
- $exit->msg (undef);
+ my $slaying = "?random/$md5";
+
+ if ($exit->valid) {
+ $exit->slaying ("?random/$md5");
+ $exit->msg (undef);
+ }
}
}
@@ -2720,31 +2751,33 @@
return unless $self->type == cf::PLAYER;
- if ($exit->slaying eq "/!") {
- #TODO: this should de-fi-ni-te-ly not be a sync-job
- # the problem is that $exit might not survive long enough
- # so it needs to be done right now, right here
- cf::sync_job { prepare_random_map $exit };
- }
-
- my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path;
- my $hp = $exit->stats->hp;
- my $sp = $exit->stats->sp;
-
$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";
+ $Coro::current->{desc} = "enter_exit";
- $self->deactivate_recursive; # just to be sure
unless (eval {
- $self->goto ($slaying, $hp, $sp);
+ $self->deactivate_recursive; # just to be sure
+
+ # random map handling
+ {
+ my $guard = cf::lock_acquire "exit_prepare:$exit";
+
+ prepare_random_map $exit
+ if $exit->slaying eq "/!";
+ }
- 1;
+ my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path;
+ my $x = $exit->stats->hp;
+ my $y = $exit->stats->sp;
+
+ $self->goto ($map, $x, $y);
+
+ # if exit is damned, update players death & WoR home-position
+ $self->contr->savebed ($map, $x, $y)
+ if $exit->flag (cf::FLAG_DAMNED);
+
+ 1
}) {
$self->message ("Something went wrong deep within the crossfire server. "
. "I'll try to bring you back to the map you were before. "
@@ -3101,8 +3134,8 @@
for (
["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
- insert remove inv name archname title slaying race
- decrease split destroy)],
+ insert remove inv nrof name archname title slaying race
+ decrease split destroy change_exp)],
["cf::object::player" => qw(player)],
["cf::player" => qw(peaceful)],
["cf::map" => qw(trigger)],
@@ -3373,6 +3406,8 @@
sub init {
my $guard = freeze_mainloop;
+ evthread_start IO::AIO::poll_fileno;
+
reload_resources;
}
@@ -3414,7 +3449,6 @@
load_extensions;
$Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
- evthread_start IO::AIO::poll_fileno;
}
EV::loop;
@@ -3559,6 +3593,34 @@
if $make_core;
}
+# a safer delete_package, copied from Symbol
+sub clear_package($) {
+ my $pkg = shift;
+
+ # expand to full symbol table name if needed
+ unless ($pkg =~ /^main::.*::$/) {
+ $pkg = "main$pkg" if $pkg =~ /^::/;
+ $pkg = "main::$pkg" unless $pkg =~ /^main::/;
+ $pkg .= '::' unless $pkg =~ /::$/;
+ }
+
+ my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
+ my $stem_symtab = *{$stem}{HASH};
+
+ defined $stem_symtab and exists $stem_symtab->{$leaf}
+ or return;
+
+ # clear all symbols
+ my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
+ for my $name (keys %$leaf_symtab) {
+ _gv_clear *{"$pkg$name"};
+# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
+ }
+ warn "cleared package #$pkg\n";#d#
+}
+
+our $RELOAD; # how many times to reload
+
sub do_reload_perl() {
# can/must only be called in main
if ($Coro::current != $Coro::main) {
@@ -3566,114 +3628,113 @@
return;
}
- warn "reloading...";
+ return if $RELOAD++;
- warn "entering sync_job";
+ while ($RELOAD) {
+ warn "reloading...";
- cf::sync_job {
- cf::write_runtime_sync; # external watchdog should not bark
- cf::emergency_save;
- cf::write_runtime_sync; # external watchdog should not bark
+ warn "entering sync_job";
- warn "syncing database to disk";
- BDB::db_env_txn_checkpoint $DB_ENV;
+ cf::sync_job {
+ cf::write_runtime_sync; # external watchdog should not bark
+ cf::emergency_save;
+ cf::write_runtime_sync; # external watchdog should not bark
- # if anything goes wrong in here, we should simply crash as we already saved
+ warn "syncing database to disk";
+ BDB::db_env_txn_checkpoint $DB_ENV;
- warn "flushing outstanding aio requests";
- for (;;) {
- BDB::flush;
- IO::AIO::flush;
- Coro::cede_notself;
- last unless IO::AIO::nreqs || BDB::nreqs;
- warn "iterate...";
- }
+ # if anything goes wrong in here, we should simply crash as we already saved
+
+ warn "flushing outstanding aio requests";
+ while (IO::AIO::nreqs || BDB::nreqs) {
+ Coro::EV::timer_once 0.01; # let the sync_job do it's thing
+ }
- ++$RELOAD;
+ warn "cancelling all extension coros";
+ $_->cancel for values %EXT_CORO;
+ %EXT_CORO = ();
- warn "cancelling all extension coros";
- $_->cancel for values %EXT_CORO;
- %EXT_CORO = ();
+ warn "removing commands";
+ %COMMAND = ();
- warn "removing commands";
- %COMMAND = ();
+ warn "removing ext/exti commands";
+ %EXTCMD = ();
+ %EXTICMD = ();
- warn "removing ext/exti commands";
- %EXTCMD = ();
- %EXTICMD = ();
+ warn "unloading/nuking all extensions";
+ for my $pkg (@EXTS) {
+ warn "... unloading $pkg";
- warn "unloading/nuking all extensions";
- for my $pkg (@EXTS) {
- warn "... unloading $pkg";
+ if (my $cb = $pkg->can ("unload")) {
+ eval {
+ $cb->($pkg);
+ 1
+ } or warn "$pkg unloaded, but with errors: $@";
+ }
- if (my $cb = $pkg->can ("unload")) {
- eval {
- $cb->($pkg);
- 1
- } or warn "$pkg unloaded, but with errors: $@";
+ warn "... clearing $pkg";
+ clear_package $pkg;
}
- warn "... nuking $pkg";
- Symbol::delete_package $pkg;
- }
+ warn "unloading all perl modules loaded from $LIBDIR";
+ while (my ($k, $v) = each %INC) {
+ next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
- warn "unloading all perl modules loaded from $LIBDIR";
- while (my ($k, $v) = each %INC) {
- next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
+ warn "... unloading $k";
+ delete $INC{$k};
- warn "... unloading $k";
- delete $INC{$k};
+ $k =~ s/\.pm$//;
+ $k =~ s/\//::/g;
- $k =~ s/\.pm$//;
- $k =~ s/\//::/g;
+ if (my $cb = $k->can ("unload_module")) {
+ $cb->();
+ }
- if (my $cb = $k->can ("unload_module")) {
- $cb->();
+ clear_package $k;
}
- Symbol::delete_package $k;
- }
-
- warn "getting rid of safe::, as good as possible";
- Symbol::delete_package "safe::$_"
- for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
+ warn "getting rid of safe::, as good as possible";
+ clear_package "safe::$_"
+ for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
- warn "unloading cf.pm \"a bit\"";
- delete $INC{"cf.pm"};
- delete $INC{"cf/pod.pm"};
+ warn "unloading cf.pm \"a bit\"";
+ delete $INC{"cf.pm"};
+ delete $INC{"cf/pod.pm"};
- # don't, removes xs symbols, too,
- # and global variables created in xs
- #Symbol::delete_package __PACKAGE__;
+ # don't, removes xs symbols, too,
+ # and global variables created in xs
+ #clear_package __PACKAGE__;
- warn "unload completed, starting to reload now";
+ warn "unload completed, starting to reload now";
- warn "reloading cf.pm";
- require cf;
- cf::_connect_to_perl; # nominally unnecessary, but cannot hurt
+ warn "reloading cf.pm";
+ require cf;
+ cf::_connect_to_perl; # nominally unnecessary, but cannot hurt
- warn "loading config and database again";
- cf::reload_config;
+ warn "loading config and database again";
+ cf::reload_config;
- warn "loading extensions";
- cf::load_extensions;
+ warn "loading extensions";
+ cf::load_extensions;
- warn "reattaching attachments to objects/players";
- _global_reattach; # objects, sockets
- warn "reattaching attachments to maps";
- reattach $_ for values %MAP;
- warn "reattaching attachments to players";
- reattach $_ for values %PLAYER;
+ warn "reattaching attachments to objects/players";
+ _global_reattach; # objects, sockets
+ warn "reattaching attachments to maps";
+ reattach $_ for values %MAP;
+ warn "reattaching attachments to players";
+ reattach $_ for values %PLAYER;
- warn "leaving sync_job";
+ warn "leaving sync_job";
- 1
- } or do {
- warn $@;
- cf::cleanup "error while reloading, exiting.";
- };
+ 1
+ } or do {
+ warn $@;
+ cf::cleanup "error while reloading, exiting.";
+ };
- warn "reloaded";
+ warn "reloaded";
+ --$RELOAD;
+ }
};
our $RELOAD_WATCHER; # used only during reload
@@ -3765,12 +3826,13 @@
BDB::min_parallel 8;
BDB::max_poll_reqs $TICK * 0.1;
- $Coro::BDB::WATCHER->priority (1);
+ $AnyEvent::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_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
+ $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
+ $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
$DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
$DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
@@ -3806,7 +3868,7 @@
IO::AIO::min_parallel 8;
IO::AIO::max_poll_time $TICK * 0.1;
- $Coro::AIO::WATCHER->priority (1);
+ undef $AnyEvent::AIO::WATCHER;
}
my $_log_backtrace;