ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.527 by root, Fri Apr 23 04:32:47 2010 UTC vs.
Revision 1.543 by root, Wed May 5 23:23:29 2010 UTC

20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
25use 5.10.0; 25use common::sense;
26use utf8;
27use strict qw(vars subs);
28 26
29use Symbol; 27use Symbol;
30use List::Util; 28use List::Util;
31use Socket; 29use Socket;
32use EV; 30use EV;
78# strictly for debugging 76# strictly for debugging
79$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; 77$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
80 78
81sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 79sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
82 80
81our @ORIG_INC;
82
83our %COMMAND = (); 83our %COMMAND = ();
84our %COMMAND_TIME = (); 84our %COMMAND_TIME = ();
85 85
86our @EXTS = (); # list of extension package names 86our @EXTS = (); # list of extension package names
87our %EXTCMD = (); 87our %EXTCMD = ();
106our $RANDOMDIR = "$LOCALDIR/random"; 106our $RANDOMDIR = "$LOCALDIR/random";
107our $BDBDIR = "$LOCALDIR/db"; 107our $BDBDIR = "$LOCALDIR/db";
108our $PIDFILE = "$LOCALDIR/pid"; 108our $PIDFILE = "$LOCALDIR/pid";
109our $RUNTIMEFILE = "$LOCALDIR/runtime"; 109our $RUNTIMEFILE = "$LOCALDIR/runtime";
110 110
111our %RESOURCE; 111our %RESOURCE; # unused
112 112
113our $OUTPUT_RATE_MIN = 3000; 113our $OUTPUT_RATE_MIN = 3000;
114our $OUTPUT_RATE_MAX = 1000000; 114our $OUTPUT_RATE_MAX = 1000000;
115 115
116our $MAX_LINKS = 32; # how many chained exits to follow 116our $MAX_LINKS = 32; # how many chained exits to follow
117our $VERBOSE_IO = 0;#d# 117our $VERBOSE_IO = 1;
118 118
119our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 119our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
120our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 120our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
121our $NEXT_TICK; 121our $NEXT_TICK;
122our $USE_FSYNC = 1; # use fsync to write maps - default on 122our $USE_FSYNC = 1; # use fsync to write maps - default on
124our $BDB_DEADLOCK_WATCHER; 124our $BDB_DEADLOCK_WATCHER;
125our $BDB_CHECKPOINT_WATCHER; 125our $BDB_CHECKPOINT_WATCHER;
126our $BDB_TRICKLE_WATCHER; 126our $BDB_TRICKLE_WATCHER;
127our $DB_ENV; 127our $DB_ENV;
128 128
129our @EXTRA_MODULES = qw(pod match mapscript); 129our @EXTRA_MODULES = qw(pod match mapscript incloader);
130 130
131our %CFG; 131our %CFG;
132 132
133our $UPTIME; $UPTIME ||= time; 133our $UPTIME; $UPTIME ||= time;
134our $RUNTIME; 134our $RUNTIME;
252 252
253=back 253=back
254 254
255=cut 255=cut
256 256
257sub error(@) { LOG llevError, join "", @_ }
258sub warn (@) { LOG llevWarn , join "", @_ }
259sub info (@) { LOG llevInfo , join "", @_ }
260sub debug(@) { LOG llevDebug, join "", @_ }
261sub trace(@) { LOG llevTrace, join "", @_ }
262
257$Coro::State::WARNHOOK = sub { 263$Coro::State::WARNHOOK = sub {
258 my $msg = join "", @_; 264 my $msg = join "", @_;
259 265
260 $msg .= "\n" 266 $msg .= "\n"
261 unless $msg =~ /\n$/; 267 unless $msg =~ /\n$/;
262 268
263 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 269 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
264 270
265 LOG llevError, $msg; 271 LOG llevWarn, $msg;
266}; 272};
267 273
268$Coro::State::DIEHOOK = sub { 274$Coro::State::DIEHOOK = sub {
269 return unless $^S eq 0; # "eq", not "==" 275 return unless $^S eq 0; # "eq", not "=="
270 276
271 warn Carp::longmess $_[0]; 277 error Carp::longmess $_[0];
272 278
273 if (in_main) {#d# 279 if (in_main) {#d#
274 warn "DIEHOOK called in main context, Coro bug?\n";#d# 280 error "DIEHOOK called in main context, Coro bug?\n";#d#
275 return;#d# 281 return;#d#
276 }#d# 282 }#d#
277 283
278 # kill coroutine otherwise 284 # kill coroutine otherwise
279 Coro::terminate 285 Coro::terminate
512=cut 518=cut
513 519
514sub sync_job(&) { 520sub sync_job(&) {
515 my ($job) = @_; 521 my ($job) = @_;
516 522
517 if ($Coro::current == $Coro::main) { 523 if (in_main) {
518 my $time = AE::time; 524 my $time = AE::time;
519 525
520 # this is the main coro, too bad, we have to block 526 # this is the main coro, too bad, we have to block
521 # till the operation succeeds, freezing the server :/ 527 # till the operation succeeds, freezing the server :/
522 528
523 LOG llevError, Carp::longmess "sync job";#d# 529 #LOG llevError, Carp::longmess "sync job";#d#
524 530
525 my $freeze_guard = freeze_mainloop; 531 my $freeze_guard = freeze_mainloop;
526 532
527 my $busy = 1; 533 my $busy = 1;
528 my @res; 534 my @res;
529 535
530 (async { 536 (async {
531 $Coro::current->desc ("sync job coro"); 537 $Coro::current->desc ("sync job coro");
532 @res = eval { $job->() }; 538 @res = eval { $job->() };
533 warn $@ if $@; 539 error $@ if $@;
534 undef $busy; 540 undef $busy;
535 })->prio (Coro::PRIO_MAX); 541 })->prio (Coro::PRIO_MAX);
536 542
537 while ($busy) { 543 while ($busy) {
538 if (Coro::nready) { 544 if (Coro::nready) {
651within each server. 657within each server.
652 658
653=cut 659=cut
654 660
655sub db_table($) { 661sub db_table($) {
662 cf::error "db_get called from main context"
663 if $Coro::current == $Coro::main;
664
656 my ($name) = @_; 665 my ($name) = @_;
657 my $db = BDB::db_create $DB_ENV; 666 my $db = BDB::db_create $DB_ENV;
658 667
659 eval { 668 eval {
660 $db->set_flags (BDB::CHKSUM); 669 $db->set_flags (BDB::CHKSUM);
670} 679}
671 680
672our $DB; 681our $DB;
673 682
674sub db_init { 683sub db_init {
675 cf::sync_job {
676 $DB ||= db_table "db"; 684 $DB ||= db_table "db";
677 };
678} 685}
679 686
680sub db_get($$) { 687sub db_get($$) {
681 my $key = "$_[0]/$_[1]"; 688 my $key = "$_[0]/$_[1]";
682 689
683 cf::sync_job { 690 cf::error "db_get called from main context"
691 if $Coro::current == $Coro::main;
692
684 BDB::db_get $DB, undef, $key, my $data; 693 BDB::db_get $DB, undef, $key, my $data;
685 694
686 $! ? () 695 $! ? ()
687 : $data 696 : $data
688 }
689} 697}
690 698
691sub db_put($$$) { 699sub db_put($$$) {
692 BDB::dbreq_pri 4; 700 BDB::dbreq_pri 4;
693 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; 701 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
749 757
750 my $t1 = Time::HiRes::time; 758 my $t1 = Time::HiRes::time;
751 my $data = $process->(\@data); 759 my $data = $process->(\@data);
752 my $t2 = Time::HiRes::time; 760 my $t2 = Time::HiRes::time;
753 761
754 warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; 762 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
755 763
756 db_put cache => "$id/data", $data; 764 db_put cache => "$id/data", $data;
757 db_put cache => "$id/md5" , $md5; 765 db_put cache => "$id/md5" , $md5;
758 db_put cache => "$id/meta", $meta; 766 db_put cache => "$id/meta", $meta;
759 767
769 777
770=cut 778=cut
771 779
772sub datalog($@) { 780sub datalog($@) {
773 my ($type, %kv) = @_; 781 my ($type, %kv) = @_;
774 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); 782 info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
775} 783}
776 784
777=back 785=back
778 786
779=cut 787=cut
974 982
975 } elsif (exists $cb_id{$type}) { 983 } elsif (exists $cb_id{$type}) {
976 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; 984 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
977 985
978 } elsif (ref $type) { 986 } elsif (ref $type) {
979 warn "attaching objects not supported, ignoring.\n"; 987 error "attaching objects not supported, ignoring.\n";
980 988
981 } else { 989 } else {
982 shift @arg; 990 shift @arg;
983 warn "attach argument '$type' not supported, ignoring.\n"; 991 error "attach argument '$type' not supported, ignoring.\n";
984 } 992 }
985 } 993 }
986} 994}
987 995
988sub _object_attach { 996sub _object_attach {
998 _attach $registry, $klass, @attach; 1006 _attach $registry, $klass, @attach;
999 } 1007 }
1000 1008
1001 $obj->{$name} = \%arg; 1009 $obj->{$name} = \%arg;
1002 } else { 1010 } else {
1003 warn "object uses attachment '$name' which is not available, postponing.\n"; 1011 info "object uses attachment '$name' which is not available, postponing.\n";
1004 } 1012 }
1005 1013
1006 $obj->{_attachment}{$name} = undef; 1014 $obj->{_attachment}{$name} = undef;
1007} 1015}
1008 1016
1067 1075
1068 for (@$callbacks) { 1076 for (@$callbacks) {
1069 eval { &{$_->[1]} }; 1077 eval { &{$_->[1]} };
1070 1078
1071 if ($@) { 1079 if ($@) {
1072 warn "$@";
1073 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; 1080 error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
1074 override; 1081 override;
1075 } 1082 }
1076 1083
1077 return 1 if $override; 1084 return 1 if $override;
1078 } 1085 }
1157 for (@$attach) { 1164 for (@$attach) {
1158 my ($klass, @attach) = @$_; 1165 my ($klass, @attach) = @$_;
1159 _attach $registry, $klass, @attach; 1166 _attach $registry, $klass, @attach;
1160 } 1167 }
1161 } else { 1168 } else {
1162 warn "object uses attachment '$name' that is not available, postponing.\n"; 1169 info "object uses attachment '$name' that is not available, postponing.\n";
1163 } 1170 }
1164 } 1171 }
1165} 1172}
1166 1173
1167cf::attachable->attach ( 1174cf::attachable->attach (
1194 my ($filename, $rdata, $objs) = @_; 1201 my ($filename, $rdata, $objs) = @_;
1195 1202
1196 sync_job { 1203 sync_job {
1197 if (length $$rdata) { 1204 if (length $$rdata) {
1198 utf8::decode (my $decname = $filename); 1205 utf8::decode (my $decname = $filename);
1199 warn sprintf "saving %s (%d,%d)\n", 1206 trace sprintf "saving %s (%d,%d)\n",
1200 $decname, length $$rdata, scalar @$objs 1207 $decname, length $$rdata, scalar @$objs
1201 if $VERBOSE_IO; 1208 if $VERBOSE_IO;
1202 1209
1203 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1210 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1204 aio_chmod $fh, SAVE_MODE; 1211 aio_chmod $fh, SAVE_MODE;
1205 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1212 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1228 aio_rename "$filename~", $filename; 1235 aio_rename "$filename~", $filename;
1229 1236
1230 $filename =~ s%/[^/]+$%%; 1237 $filename =~ s%/[^/]+$%%;
1231 aio_pathsync $filename if $cf::USE_FSYNC; 1238 aio_pathsync $filename if $cf::USE_FSYNC;
1232 } else { 1239 } else {
1233 warn "unable to save objects: $filename~: $!\n"; 1240 error "unable to save objects: $filename~: $!\n";
1234 } 1241 }
1235 } else { 1242 } else {
1236 aio_unlink $filename; 1243 aio_unlink $filename;
1237 aio_unlink "$filename.pst"; 1244 aio_unlink "$filename.pst";
1238 } 1245 }
1262 my $st = eval { Coro::Storable::thaw $av }; 1269 my $st = eval { Coro::Storable::thaw $av };
1263 $av = $st->{objs}; 1270 $av = $st->{objs};
1264 } 1271 }
1265 1272
1266 utf8::decode (my $decname = $filename); 1273 utf8::decode (my $decname = $filename);
1267 warn sprintf "loading %s (%d,%d)\n", 1274 trace sprintf "loading %s (%d,%d)\n",
1268 $decname, length $data, scalar @{$av || []} 1275 $decname, length $data, scalar @{$av || []}
1269 if $VERBOSE_IO; 1276 if $VERBOSE_IO;
1270 1277
1271 ($data, $av) 1278 ($data, $av)
1272} 1279}
1273 1280
1366 1373
1367 $pl->ext_reply ($reply, @reply) 1374 $pl->ext_reply ($reply, @reply)
1368 if $reply; 1375 if $reply;
1369 1376
1370 } else { 1377 } else {
1371 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1378 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1372 } 1379 }
1373 1380
1374 cf::override; 1381 cf::override;
1375 }, 1382 },
1376); 1383);
1387 1394
1388 $grp 1395 $grp
1389} 1396}
1390 1397
1391sub load_extensions { 1398sub load_extensions {
1399 info "loading extensions...";
1400
1392 cf::sync_job { 1401 cf::sync_job {
1393 my %todo; 1402 my %todo;
1394 1403
1395 for my $path (<$LIBDIR/*.ext>) { 1404 for my $path (<$LIBDIR/*.ext>) {
1396 next unless -r $path; 1405 next unless -r $path;
1414 1423
1415 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } 1424 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 }
1416 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; 1425 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
1417 1426
1418 $ext{source} = 1427 $ext{source} =
1419 "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" 1428 "package $pkg; use common::sense;\n"
1420 . "#line 1 \"$path\"\n{\n" 1429 . "#line 1 \"$path\"\n{\n"
1421 . $source 1430 . $source
1422 . "\n};\n1"; 1431 . "\n};\n1";
1423 1432
1424 $todo{$base} = \%ext; 1433 $todo{$base} = \%ext;
1436 for (split /,\s*/, $v->{meta}{depends}) { 1445 for (split /,\s*/, $v->{meta}{depends}) {
1437 next ext 1446 next ext
1438 unless exists $done{$_}; 1447 unless exists $done{$_};
1439 } 1448 }
1440 1449
1441 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1450 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1442 1451
1443 my $active = eval $v->{source}; 1452 my $active = eval $v->{source};
1444 1453
1445 if (length $@) { 1454 if (length $@) {
1446 warn "$v->{path}: $@\n"; 1455 error "$v->{path}: $@\n";
1447 1456
1448 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1457 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1449 if exists $v->{meta}{mandatory}; 1458 if exists $v->{meta}{mandatory};
1450 1459
1451 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1460 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1453 } else { 1462 } else {
1454 $done{$k} = delete $todo{$k}; 1463 $done{$k} = delete $todo{$k};
1455 push @EXTS, $v->{pkg}; 1464 push @EXTS, $v->{pkg};
1456 $progress = 1; 1465 $progress = 1;
1457 1466
1458 warn "$v->{base}: extension inactive.\n" 1467 info "$v->{base}: extension inactive.\n"
1459 unless $active; 1468 unless $active;
1460 } 1469 }
1461 } 1470 }
1462 1471
1463 unless ($progress) { 1472 unless ($progress) {
1635 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1644 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1636 1645
1637 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1646 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1638 $pl->deactivate; 1647 $pl->deactivate;
1639 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1648 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1640 $pl->ob->check_score;
1641 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1649 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1642 $pl->ns->destroy if $pl->ns; 1650 $pl->ns->destroy if $pl->ns;
1643 1651
1644 my $path = playerdir $pl; 1652 my $path = playerdir $pl;
1645 my $temp = "$path~$cf::RUNTIME~deleting~"; 1653 my $temp = "$path~$cf::RUNTIME~deleting~";
1840 1848
1841sub register { 1849sub register {
1842 my (undef, $regex, $prio) = @_; 1850 my (undef, $regex, $prio) = @_;
1843 my $pkg = caller; 1851 my $pkg = caller;
1844 1852
1845 no strict;
1846 push @{"$pkg\::ISA"}, __PACKAGE__; 1853 push @{"$pkg\::ISA"}, __PACKAGE__;
1847 1854
1848 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1855 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1849} 1856}
1850 1857
1862} 1869}
1863 1870
1864sub normalise { 1871sub normalise {
1865 my ($path, $base) = @_; 1872 my ($path, $base) = @_;
1866 1873
1867 $path = "$path"; # make sure its a string 1874 $path = "$path"; # make sure it's a string
1868 1875
1869 $path =~ s/\.map$//; 1876 $path =~ s/\.map$//;
1870 1877
1871 # map plan: 1878 # map plan:
1872 # 1879 #
1911 $self->init; # pass $1 etc. 1918 $self->init; # pass $1 etc.
1912 return $self; 1919 return $self;
1913 } 1920 }
1914 } 1921 }
1915 1922
1916 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1923 Carp::cluck "unable to resolve path '$path' (base '$base')";
1917 () 1924 ()
1918} 1925}
1919 1926
1920sub init { 1927sub init {
1921 my ($self) = @_; 1928 my ($self) = @_;
2043} 2050}
2044 2051
2045sub find; 2052sub find;
2046sub find { 2053sub find {
2047 my ($path, $origin) = @_; 2054 my ($path, $origin) = @_;
2055
2056 cf::cede_to_tick;
2048 2057
2049 $path = normalise $path, $origin && $origin->path; 2058 $path = normalise $path, $origin && $origin->path;
2050 2059
2051 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2060 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2052 my $guard2 = cf::lock_acquire "map_find:$path"; 2061 my $guard2 = cf::lock_acquire "map_find:$path";
2184} 2193}
2185 2194
2186sub find_sync { 2195sub find_sync {
2187 my ($path, $origin) = @_; 2196 my ($path, $origin) = @_;
2188 2197
2189 cf::sync_job { find $path, $origin } 2198 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2199 if $Coro::current == $Coro::main;
2200
2201 find $path, $origin
2190} 2202}
2191 2203
2192sub do_load_sync { 2204sub do_load_sync {
2193 my ($map) = @_; 2205 my ($map) = @_;
2194 2206
2195 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2207 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2196 if $Coro::current == $Coro::main; 2208 if $Coro::current == $Coro::main;
2197 2209
2198 cf::sync_job { $map->load }; 2210 $map->load;
2199} 2211}
2200 2212
2201our %MAP_PREFETCH; 2213our %MAP_PREFETCH;
2202our $MAP_PREFETCHER = undef; 2214our $MAP_PREFETCHER = undef;
2203 2215
2315 2327
2316 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2328 my $lock = cf::lock_acquire "map_data:$self->{path}";
2317 2329
2318 return if $self->players; 2330 return if $self->players;
2319 2331
2320 warn "resetting map ", $self->path, "\n"; 2332 cf::trace "resetting map ", $self->path, "\n";
2321 2333
2322 $self->in_memory (cf::MAP_SWAPPED); 2334 $self->in_memory (cf::MAP_SWAPPED);
2323 2335
2324 # need to save uniques path 2336 # need to save uniques path
2325 unless ($self->{deny_save}) { 2337 unless ($self->{deny_save}) {
2690 2702
2691sub cf::object::player::goto { 2703sub cf::object::player::goto {
2692 my ($self, $path, $x, $y, $check, $done) = @_; 2704 my ($self, $path, $x, $y, $check, $done) = @_;
2693 2705
2694 if ($self->{_link_recursion} >= $MAX_LINKS) { 2706 if ($self->{_link_recursion} >= $MAX_LINKS) {
2695 warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; 2707 error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting.";
2696 $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); 2708 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2697 ($path, $x, $y) = @$EMERGENCY_POSITION; 2709 ($path, $x, $y) = @$EMERGENCY_POSITION;
2698 } 2710 }
2699 2711
2700 # do generation counting so two concurrent goto's will be executed in-order 2712 # do generation counting so two concurrent goto's will be executed in-order
2848 $self->message ("Something went wrong deep within the deliantra server. " 2860 $self->message ("Something went wrong deep within the deliantra server. "
2849 . "I'll try to bring you back to the map you were before. " 2861 . "I'll try to bring you back to the map you were before. "
2850 . "Please report this to the dungeon master!", 2862 . "Please report this to the dungeon master!",
2851 cf::NDI_UNIQUE | cf::NDI_RED); 2863 cf::NDI_UNIQUE | cf::NDI_RED);
2852 2864
2853 warn "ERROR in enter_exit: $@"; 2865 error "ERROR in enter_exit: $@";
2854 $self->leave_link; 2866 $self->leave_link;
2855 } 2867 }
2856 })->prio (1); 2868 })->prio (1);
2857} 2869}
2858 2870
3171 3183
3172 $ns->ext_reply ($reply, @reply) 3184 $ns->ext_reply ($reply, @reply)
3173 if $reply; 3185 if $reply;
3174 3186
3175 } else { 3187 } else {
3176 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3188 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3177 } 3189 }
3178 3190
3179 cf::override; 3191 cf::override;
3180 }, 3192 },
3181); 3193);
3262 decrease split destroy change_exp value msg lore send_msg)], 3274 decrease split destroy change_exp value msg lore send_msg)],
3263 ["cf::object::player" => qw(player)], 3275 ["cf::object::player" => qw(player)],
3264 ["cf::player" => qw(peaceful send_msg)], 3276 ["cf::player" => qw(peaceful send_msg)],
3265 ["cf::map" => qw(trigger)], 3277 ["cf::map" => qw(trigger)],
3266) { 3278) {
3267 no strict 'refs';
3268 my ($pkg, @funs) = @$_; 3279 my ($pkg, @funs) = @$_;
3269 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3280 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3270 for @funs; 3281 for @funs;
3271} 3282}
3272 3283
3311 local @cf::_safe_eval_args = values %vars; 3322 local @cf::_safe_eval_args = values %vars;
3312 @res = wantarray ? eval eval : scalar eval $eval; 3323 @res = wantarray ? eval eval : scalar eval $eval;
3313 } 3324 }
3314 3325
3315 if ($@) { 3326 if ($@) {
3316 warn "$@"; 3327 warn "$@",
3317 warn "while executing safe code '$code'\n"; 3328 "while executing safe code '$code'\n",
3318 warn "with arguments " . (join " ", %vars) . "\n"; 3329 "with arguments " . (join " ", %vars) . "\n";
3319 } 3330 }
3320 3331
3321 wantarray ? @res : $res[0] 3332 wantarray ? @res : $res[0]
3322} 3333}
3323 3334
3357 # for this (global event?) 3368 # for this (global event?)
3358 %ext::player_env::MUSIC_FACE_CACHE = (); 3369 %ext::player_env::MUSIC_FACE_CACHE = ();
3359 3370
3360 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3371 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3361 3372
3362 warn "loading facedata from $path\n"; 3373 trace "loading facedata from $path\n";
3363 3374
3364 my $facedata; 3375 my $facedata;
3365 0 < aio_load $path, $facedata 3376 0 < aio_load $path, $facedata
3366 or die "$path: $!"; 3377 or die "$path: $!";
3367 3378
3401 3412
3402 if (my $smooth = cf::face::find $info->{smooth}) { 3413 if (my $smooth = cf::face::find $info->{smooth}) {
3403 cf::face::set_smooth $idx, $smooth; 3414 cf::face::set_smooth $idx, $smooth;
3404 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3415 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3405 } else { 3416 } else {
3406 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3417 error "smooth face '$info->{smooth}' not found for face '$face'";
3407 } 3418 }
3408 3419
3409 cf::cede_to_tick; 3420 cf::cede_to_tick;
3410 } 3421 }
3411 } 3422 }
3429 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3440 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3430 3441
3431 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3442 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3432 cf::face::set_type $idx, $info->{type}; 3443 cf::face::set_type $idx, $info->{type};
3433 } else { 3444 } else {
3434 $RESOURCE{$name} = $info; 3445 $RESOURCE{$name} = $info; # unused
3435 } 3446 }
3436 3447
3437 cf::cede_to_tick; 3448 cf::cede_to_tick;
3438 } 3449 }
3439 } 3450 }
3440 3451
3441 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); 3452 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE);
3442 3453
3443 1 3454 1
3444} 3455}
3445
3446cf::global->attach (on_resource_update => sub {
3447 if (my $soundconf = $RESOURCE{"res/sound.conf"}) {
3448 $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data});
3449
3450 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3451 my $sound = $soundconf->{compat}[$_]
3452 or next;
3453
3454 my $face = cf::face::find "sound/$sound->[1]";
3455 cf::sound::set $sound->[0] => $face;
3456 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3457 }
3458
3459 while (my ($k, $v) = each %{$soundconf->{event}}) {
3460 my $face = cf::face::find "sound/$v";
3461 cf::sound::set $k => $face;
3462 }
3463 }
3464});
3465 3456
3466register_exticmd fx_want => sub { 3457register_exticmd fx_want => sub {
3467 my ($ns, $want) = @_; 3458 my ($ns, $want) = @_;
3468 3459
3469 while (my ($k, $v) = each %$want) { 3460 while (my ($k, $v) = each %$want) {
3508sub reload_treasures { 3499sub reload_treasures {
3509 load_resource_file "$DATADIR/treasures" 3500 load_resource_file "$DATADIR/treasures"
3510 or die "unable to load treasurelists\n"; 3501 or die "unable to load treasurelists\n";
3511} 3502}
3512 3503
3504sub reload_sound {
3505 trace "loading sound config from $DATADIR/sound\n";
3506
3507 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3508 or die "$DATADIR/sound $!";
3509
3510 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data);
3511
3512 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3513 my $sound = $soundconf->{compat}[$_]
3514 or next;
3515
3516 my $face = cf::face::find "sound/$sound->[1]";
3517 cf::sound::set $sound->[0] => $face;
3518 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3519 }
3520
3521 while (my ($k, $v) = each %{$soundconf->{event}}) {
3522 my $face = cf::face::find "sound/$v";
3523 cf::sound::set $k => $face;
3524 }
3525}
3526
3513sub reload_resources { 3527sub reload_resources {
3514 warn "reloading resource files...\n"; 3528 trace "reloading resource files...\n";
3515 3529
3516 reload_facedata; 3530 reload_facedata;
3531 reload_sound;
3517 reload_archetypes; 3532 reload_archetypes;
3518 reload_regions; 3533 reload_regions;
3519 reload_treasures; 3534 reload_treasures;
3520 3535
3521 warn "finished reloading resource files\n"; 3536 trace "finished reloading resource files\n";
3522} 3537}
3523 3538
3524sub reload_config { 3539sub reload_config {
3525 warn "reloading config file...\n"; 3540 trace "reloading config file...\n";
3526 3541
3527 open my $fh, "<:utf8", "$CONFDIR/config" 3542 open my $fh, "<:utf8", "$CONFDIR/config"
3528 or return; 3543 or return;
3529 3544
3530 local $/; 3545 local $/;
3541 and die "WARNING: m(un)lockall failed: $!\n"; 3556 and die "WARNING: m(un)lockall failed: $!\n";
3542 }; 3557 };
3543 warn $@ if $@; 3558 warn $@ if $@;
3544 } 3559 }
3545 3560
3546 warn "finished reloading resource files\n"; 3561 trace "finished reloading resource files\n";
3547} 3562}
3548 3563
3549sub pidfile() { 3564sub pidfile() {
3550 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3565 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3551 or die "$PIDFILE: $!"; 3566 or die "$PIDFILE: $!";
3564 seek $fh, 0, 0; 3579 seek $fh, 0, 0;
3565 print $fh $$; 3580 print $fh $$;
3566} 3581}
3567 3582
3568sub main_loop { 3583sub main_loop {
3569 warn "EV::loop starting\n"; 3584 trace "EV::loop starting\n";
3570 if (1) { 3585 if (1) {
3571 EV::loop; 3586 EV::loop;
3572 } 3587 }
3573 warn "EV::loop returned\n"; 3588 trace "EV::loop returned\n";
3574 goto &main_loop unless $REALLY_UNLOOP; 3589 goto &main_loop unless $REALLY_UNLOOP;
3575} 3590}
3576 3591
3577sub main { 3592sub main {
3578 cf::init_globals; # initialise logging 3593 cf::init_globals; # initialise logging
3579 3594
3580 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3595 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3581 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3596 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3582 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3597 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3583 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3598 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3584 3599
3585 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3600 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3586 3601
3594 }; 3609 };
3595 3610
3596 evthread_start IO::AIO::poll_fileno; 3611 evthread_start IO::AIO::poll_fileno;
3597 3612
3598 cf::sync_job { 3613 cf::sync_job {
3614 cf::incloader::init ();
3615
3599 cf::init_experience; 3616 cf::init_experience;
3600 cf::init_anim; 3617 cf::init_anim;
3601 cf::init_attackmess; 3618 cf::init_attackmess;
3602 cf::init_dynamic; 3619 cf::init_dynamic;
3603 3620
3626 3643
3627 (pop @POST_INIT)->(0) while @POST_INIT; 3644 (pop @POST_INIT)->(0) while @POST_INIT;
3628 }; 3645 };
3629 3646
3630 cf::object::thawer::errors_are_fatal 0; 3647 cf::object::thawer::errors_are_fatal 0;
3631 warn "parse errors in files are no longer fatal from this point on.\n"; 3648 info "parse errors in files are no longer fatal from this point on.\n";
3632 3649
3650 my $free_main; $free_main = EV::idle sub {
3651 undef $free_main;
3652 undef &main; # free gobs of memory :)
3653 };
3654
3633 main_loop; 3655 goto &main_loop;
3634} 3656}
3635 3657
3636############################################################################# 3658#############################################################################
3637# initialisation and cleanup 3659# initialisation and cleanup
3638 3660
3677 or return; 3699 or return;
3678 3700
3679 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3701 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3680 and return; 3702 and return;
3681 3703
3682 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3704 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3683 3705
3684 1 3706 1
3685} 3707}
3686 3708
3687our $uuid_lock; 3709our $uuid_lock;
3699 or return; 3721 or return;
3700 3722
3701 my $value = uuid_seq uuid_cur; 3723 my $value = uuid_seq uuid_cur;
3702 3724
3703 unless ($value) { 3725 unless ($value) {
3704 warn "cowardly refusing to write zero uuid value!\n"; 3726 info "cowardly refusing to write zero uuid value!\n";
3705 return; 3727 return;
3706 } 3728 }
3707 3729
3708 my $value = uuid_str $value + $uuid_skip; 3730 my $value = uuid_str $value + $uuid_skip;
3709 $uuid_skip = 0; 3731 $uuid_skip = 0;
3719 or return; 3741 or return;
3720 3742
3721 aio_rename "$uuid~", $uuid 3743 aio_rename "$uuid~", $uuid
3722 and return; 3744 and return;
3723 3745
3724 warn "uuid file written ($value).\n"; 3746 trace "uuid file written ($value).\n";
3725 3747
3726 1 3748 1
3727 3749
3728} 3750}
3729 3751
3735} 3757}
3736 3758
3737sub emergency_save() { 3759sub emergency_save() {
3738 my $freeze_guard = cf::freeze_mainloop; 3760 my $freeze_guard = cf::freeze_mainloop;
3739 3761
3740 warn "emergency_perl_save: enter\n"; 3762 info "emergency_perl_save: enter\n";
3763
3764 # this is a trade-off: we want to be very quick here, so
3765 # save all maps without fsync, and later call a global sync
3766 # (which in turn might be very very slow)
3767 local $USE_FSYNC = 0;
3741 3768
3742 cf::sync_job { 3769 cf::sync_job {
3743 # this is a trade-off: we want to be very quick here, so 3770 cf::write_runtime_sync; # external watchdog should not bark
3744 # save all maps without fsync, and later call a global sync
3745 # (which in turn might be very very slow)
3746 local $USE_FSYNC = 0;
3747 3771
3748 # use a peculiar iteration method to avoid tripping on perl 3772 # use a peculiar iteration method to avoid tripping on perl
3749 # refcount bugs in for. also avoids problems with players 3773 # refcount bugs in for. also avoids problems with players
3750 # and maps saved/destroyed asynchronously. 3774 # and maps saved/destroyed asynchronously.
3751 warn "emergency_perl_save: begin player save\n"; 3775 info "emergency_perl_save: begin player save\n";
3752 for my $login (keys %cf::PLAYER) { 3776 for my $login (keys %cf::PLAYER) {
3753 my $pl = $cf::PLAYER{$login} or next; 3777 my $pl = $cf::PLAYER{$login} or next;
3754 $pl->valid or next; 3778 $pl->valid or next;
3755 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3779 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3756 $pl->save; 3780 $pl->save;
3757 } 3781 }
3758 warn "emergency_perl_save: end player save\n"; 3782 info "emergency_perl_save: end player save\n";
3759 3783
3784 cf::write_runtime_sync; # external watchdog should not bark
3785
3760 warn "emergency_perl_save: begin map save\n"; 3786 info "emergency_perl_save: begin map save\n";
3761 for my $path (keys %cf::MAP) { 3787 for my $path (keys %cf::MAP) {
3762 my $map = $cf::MAP{$path} or next; 3788 my $map = $cf::MAP{$path} or next;
3763 $map->valid or next; 3789 $map->valid or next;
3764 $map->save; 3790 $map->save;
3765 } 3791 }
3766 warn "emergency_perl_save: end map save\n"; 3792 info "emergency_perl_save: end map save\n";
3767 3793
3794 cf::write_runtime_sync; # external watchdog should not bark
3795
3768 warn "emergency_perl_save: begin database checkpoint\n"; 3796 info "emergency_perl_save: begin database checkpoint\n";
3769 BDB::db_env_txn_checkpoint $DB_ENV; 3797 BDB::db_env_txn_checkpoint $DB_ENV;
3770 warn "emergency_perl_save: end database checkpoint\n"; 3798 info "emergency_perl_save: end database checkpoint\n";
3771 3799
3772 warn "emergency_perl_save: begin write uuid\n"; 3800 info "emergency_perl_save: begin write uuid\n";
3773 write_uuid_sync 1; 3801 write_uuid_sync 1;
3774 warn "emergency_perl_save: end write uuid\n"; 3802 info "emergency_perl_save: end write uuid\n";
3803
3804 cf::write_runtime_sync; # external watchdog should not bark
3805
3806 trace "emergency_perl_save: syncing database to disk";
3807 BDB::db_env_txn_checkpoint $DB_ENV;
3808
3809 info "emergency_perl_save: starting sync\n";
3810 IO::AIO::aio_sync sub {
3811 info "emergency_perl_save: finished sync\n";
3812 };
3813
3814 cf::write_runtime_sync; # external watchdog should not bark
3815
3816 trace "emergency_perl_save: flushing outstanding aio requests";
3817 while (IO::AIO::nreqs || BDB::nreqs) {
3818 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3819 }
3820
3821 cf::write_runtime_sync; # external watchdog should not bark
3775 }; 3822 };
3776 3823
3777 warn "emergency_perl_save: starting sync()\n";
3778 IO::AIO::aio_sync sub {
3779 warn "emergency_perl_save: finished sync()\n";
3780 };
3781
3782 warn "emergency_perl_save: leave\n"; 3824 info "emergency_perl_save: leave\n";
3783} 3825}
3784 3826
3785sub post_cleanup { 3827sub post_cleanup {
3786 my ($make_core) = @_; 3828 my ($make_core) = @_;
3787 3829
3830 IO::AIO::flush;
3831
3788 warn Carp::longmess "post_cleanup backtrace" 3832 error Carp::longmess "post_cleanup backtrace"
3789 if $make_core; 3833 if $make_core;
3790 3834
3791 my $fh = pidfile; 3835 my $fh = pidfile;
3792 unlink $PIDFILE if <$fh> == $$; 3836 unlink $PIDFILE if <$fh> == $$;
3793} 3837}
3817 } 3861 }
3818} 3862}
3819 3863
3820sub do_reload_perl() { 3864sub do_reload_perl() {
3821 # can/must only be called in main 3865 # can/must only be called in main
3822 if (in_main) { 3866 unless (in_main) {
3823 warn "can only reload from main coroutine"; 3867 error "can only reload from main coroutine";
3824 return; 3868 return;
3825 } 3869 }
3826 3870
3827 return if $RELOAD++; 3871 return if $RELOAD++;
3828 3872
3829 my $t1 = AE::time; 3873 my $t1 = AE::time;
3830 3874
3831 while ($RELOAD) { 3875 while ($RELOAD) {
3876 cf::get_slot 0.1, -1, "reload_perl";
3832 warn "reloading..."; 3877 info "reloading...";
3833 3878
3834 warn "entering sync_job"; 3879 trace "entering sync_job";
3835 3880
3836 cf::sync_job { 3881 cf::sync_job {
3837 cf::write_runtime_sync; # external watchdog should not bark
3838 cf::emergency_save; 3882 #cf::emergency_save;
3839 cf::write_runtime_sync; # external watchdog should not bark
3840 3883
3841 warn "syncing database to disk";
3842 BDB::db_env_txn_checkpoint $DB_ENV;
3843
3844 # if anything goes wrong in here, we should simply crash as we already saved
3845
3846 warn "flushing outstanding aio requests";
3847 while (IO::AIO::nreqs || BDB::nreqs) {
3848 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3849 }
3850
3851 warn "cancelling all extension coros"; 3884 trace "cancelling all extension coros";
3852 $_->cancel for values %EXT_CORO; 3885 $_->cancel for values %EXT_CORO;
3853 %EXT_CORO = (); 3886 %EXT_CORO = ();
3854 3887
3855 warn "removing commands"; 3888 trace "removing commands";
3856 %COMMAND = (); 3889 %COMMAND = ();
3857 3890
3858 warn "removing ext/exti commands"; 3891 trace "removing ext/exti commands";
3859 %EXTCMD = (); 3892 %EXTCMD = ();
3860 %EXTICMD = (); 3893 %EXTICMD = ();
3861 3894
3862 warn "unloading/nuking all extensions"; 3895 trace "unloading/nuking all extensions";
3863 for my $pkg (@EXTS) { 3896 for my $pkg (@EXTS) {
3864 warn "... unloading $pkg"; 3897 trace "... unloading $pkg";
3865 3898
3866 if (my $cb = $pkg->can ("unload")) { 3899 if (my $cb = $pkg->can ("unload")) {
3867 eval { 3900 eval {
3868 $cb->($pkg); 3901 $cb->($pkg);
3869 1 3902 1
3870 } or warn "$pkg unloaded, but with errors: $@"; 3903 } or error "$pkg unloaded, but with errors: $@";
3871 } 3904 }
3872 3905
3873 warn "... clearing $pkg"; 3906 trace "... clearing $pkg";
3874 clear_package $pkg; 3907 clear_package $pkg;
3875 } 3908 }
3876 3909
3877 warn "unloading all perl modules loaded from $LIBDIR"; 3910 trace "unloading all perl modules loaded from $LIBDIR";
3878 while (my ($k, $v) = each %INC) { 3911 while (my ($k, $v) = each %INC) {
3879 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3912 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3880 3913
3881 warn "... unloading $k"; 3914 trace "... unloading $k";
3882 delete $INC{$k}; 3915 delete $INC{$k};
3883 3916
3884 $k =~ s/\.pm$//; 3917 $k =~ s/\.pm$//;
3885 $k =~ s/\//::/g; 3918 $k =~ s/\//::/g;
3886 3919
3889 } 3922 }
3890 3923
3891 clear_package $k; 3924 clear_package $k;
3892 } 3925 }
3893 3926
3894 warn "getting rid of safe::, as good as possible"; 3927 trace "getting rid of safe::, as good as possible";
3895 clear_package "safe::$_" 3928 clear_package "safe::$_"
3896 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3929 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3897 3930
3898 warn "unloading cf.pm \"a bit\""; 3931 trace "unloading cf.pm \"a bit\"";
3899 delete $INC{"cf.pm"}; 3932 delete $INC{"cf.pm"};
3900 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3933 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3901 3934
3902 # don't, removes xs symbols, too, 3935 # don't, removes xs symbols, too,
3903 # and global variables created in xs 3936 # and global variables created in xs
3904 #clear_package __PACKAGE__; 3937 #clear_package __PACKAGE__;
3905 3938
3906 warn "unload completed, starting to reload now"; 3939 info "unload completed, starting to reload now";
3907 3940
3908 warn "reloading cf.pm"; 3941 trace "reloading cf.pm";
3909 require cf; 3942 require cf;
3910 cf::_connect_to_perl_1; 3943 cf::_connect_to_perl_1;
3911 3944
3912 warn "loading config and database again"; 3945 trace "loading config and database again";
3913 cf::reload_config; 3946 cf::reload_config;
3914 3947
3915 warn "loading extensions"; 3948 trace "loading extensions";
3916 cf::load_extensions; 3949 cf::load_extensions;
3917 3950
3918 if ($REATTACH_ON_RELOAD) { 3951 if ($REATTACH_ON_RELOAD) {
3919 warn "reattaching attachments to objects/players"; 3952 trace "reattaching attachments to objects/players";
3920 _global_reattach; # objects, sockets 3953 _global_reattach; # objects, sockets
3921 warn "reattaching attachments to maps"; 3954 trace "reattaching attachments to maps";
3922 reattach $_ for values %MAP; 3955 reattach $_ for values %MAP;
3923 warn "reattaching attachments to players"; 3956 trace "reattaching attachments to players";
3924 reattach $_ for values %PLAYER; 3957 reattach $_ for values %PLAYER;
3925 } 3958 }
3926 3959
3927 warn "running post_init jobs"; 3960 trace "running post_init jobs";
3928 (pop @POST_INIT)->(1) while @POST_INIT; 3961 (pop @POST_INIT)->(1) while @POST_INIT;
3929 3962
3930 warn "leaving sync_job"; 3963 trace "leaving sync_job";
3931 3964
3932 1 3965 1
3933 } or do { 3966 } or do {
3934 warn $@; 3967 error $@;
3935 cf::cleanup "error while reloading, exiting."; 3968 cf::cleanup "error while reloading, exiting.";
3936 }; 3969 };
3937 3970
3938 warn "reloaded"; 3971 info "reloaded";
3939 --$RELOAD; 3972 --$RELOAD;
3940 } 3973 }
3941 3974
3942 $t1 = AE::time - $t1; 3975 $t1 = AE::time - $t1;
3943 warn "reload completed in ${t1}s\n"; 3976 info "reload completed in ${t1}s\n";
3944}; 3977};
3945 3978
3946our $RELOAD_WATCHER; # used only during reload 3979our $RELOAD_WATCHER; # used only during reload
3947 3980
3948sub reload_perl() { 3981sub reload_perl() {
3969 reload_perl; 4002 reload_perl;
3970 }; 4003 };
3971 } 4004 }
3972}; 4005};
3973 4006
3974unshift @INC, $LIBDIR; 4007#############################################################################
3975 4008
3976my $bug_warning = 0; 4009my $bug_warning = 0;
3977 4010
3978our @WAIT_FOR_TICK; 4011our @WAIT_FOR_TICK;
3979our @WAIT_FOR_TICK_BEGIN; 4012our @WAIT_FOR_TICK_BEGIN;
4008 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4041 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4009 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4042 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4010 Coro::async_pool { 4043 Coro::async_pool {
4011 $Coro::current->{desc} = "runtime saver"; 4044 $Coro::current->{desc} = "runtime saver";
4012 write_runtime_sync 4045 write_runtime_sync
4013 or warn "ERROR: unable to write runtime file: $!"; 4046 or error "ERROR: unable to write runtime file: $!";
4014 }; 4047 };
4015 } 4048 }
4016 4049
4017 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4050 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4018 $sig->send; 4051 $sig->send;
4026 4059
4027 if (0) { 4060 if (0) {
4028 if ($NEXT_TICK) { 4061 if ($NEXT_TICK) {
4029 my $jitter = $TICK_START - $NEXT_TICK; 4062 my $jitter = $TICK_START - $NEXT_TICK;
4030 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4063 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4031 warn "jitter $JITTER\n";#d# 4064 debug "jitter $JITTER\n";#d#
4032 } 4065 }
4033 } 4066 }
4034} 4067}
4035 4068
4036{ 4069{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines