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.528 by root, Fri Apr 23 04:42:12 2010 UTC vs.
Revision 1.536 by root, Thu Apr 29 08:26:38 2010 UTC

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
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;
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~";
2184} 2192}
2185 2193
2186sub find_sync { 2194sub find_sync {
2187 my ($path, $origin) = @_; 2195 my ($path, $origin) = @_;
2188 2196
2189 cf::sync_job { find $path, $origin } 2197 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2198 if $Coro::current == $Coro::main;
2199
2200 find $path, $origin
2190} 2201}
2191 2202
2192sub do_load_sync { 2203sub do_load_sync {
2193 my ($map) = @_; 2204 my ($map) = @_;
2194 2205
2195 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2206 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2196 if $Coro::current == $Coro::main; 2207 if $Coro::current == $Coro::main;
2197 2208
2198 cf::sync_job { $map->load }; 2209 $map->load;
2199} 2210}
2200 2211
2201our %MAP_PREFETCH; 2212our %MAP_PREFETCH;
2202our $MAP_PREFETCHER = undef; 2213our $MAP_PREFETCHER = undef;
2203 2214
2315 2326
2316 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2327 my $lock = cf::lock_acquire "map_data:$self->{path}";
2317 2328
2318 return if $self->players; 2329 return if $self->players;
2319 2330
2320 warn "resetting map ", $self->path, "\n"; 2331 cf::trace "resetting map ", $self->path, "\n";
2321 2332
2322 $self->in_memory (cf::MAP_SWAPPED); 2333 $self->in_memory (cf::MAP_SWAPPED);
2323 2334
2324 # need to save uniques path 2335 # need to save uniques path
2325 unless ($self->{deny_save}) { 2336 unless ($self->{deny_save}) {
2690 2701
2691sub cf::object::player::goto { 2702sub cf::object::player::goto {
2692 my ($self, $path, $x, $y, $check, $done) = @_; 2703 my ($self, $path, $x, $y, $check, $done) = @_;
2693 2704
2694 if ($self->{_link_recursion} >= $MAX_LINKS) { 2705 if ($self->{_link_recursion} >= $MAX_LINKS) {
2695 warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; 2706 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!"); 2707 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2697 ($path, $x, $y) = @$EMERGENCY_POSITION; 2708 ($path, $x, $y) = @$EMERGENCY_POSITION;
2698 } 2709 }
2699 2710
2700 # do generation counting so two concurrent goto's will be executed in-order 2711 # do generation counting so two concurrent goto's will be executed in-order
2848 $self->message ("Something went wrong deep within the deliantra server. " 2859 $self->message ("Something went wrong deep within the deliantra server. "
2849 . "I'll try to bring you back to the map you were before. " 2860 . "I'll try to bring you back to the map you were before. "
2850 . "Please report this to the dungeon master!", 2861 . "Please report this to the dungeon master!",
2851 cf::NDI_UNIQUE | cf::NDI_RED); 2862 cf::NDI_UNIQUE | cf::NDI_RED);
2852 2863
2853 warn "ERROR in enter_exit: $@"; 2864 error "ERROR in enter_exit: $@";
2854 $self->leave_link; 2865 $self->leave_link;
2855 } 2866 }
2856 })->prio (1); 2867 })->prio (1);
2857} 2868}
2858 2869
3171 3182
3172 $ns->ext_reply ($reply, @reply) 3183 $ns->ext_reply ($reply, @reply)
3173 if $reply; 3184 if $reply;
3174 3185
3175 } else { 3186 } else {
3176 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3187 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3177 } 3188 }
3178 3189
3179 cf::override; 3190 cf::override;
3180 }, 3191 },
3181); 3192);
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
3626 3641
3627 (pop @POST_INIT)->(0) while @POST_INIT; 3642 (pop @POST_INIT)->(0) while @POST_INIT;
3628 }; 3643 };
3629 3644
3630 cf::object::thawer::errors_are_fatal 0; 3645 cf::object::thawer::errors_are_fatal 0;
3631 warn "parse errors in files are no longer fatal from this point on.\n"; 3646 info "parse errors in files are no longer fatal from this point on.\n";
3632 3647
3633 main_loop; 3648 main_loop;
3634} 3649}
3635 3650
3636############################################################################# 3651#############################################################################
3677 or return; 3692 or return;
3678 3693
3679 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3694 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3680 and return; 3695 and return;
3681 3696
3682 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3697 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3683 3698
3684 1 3699 1
3685} 3700}
3686 3701
3687our $uuid_lock; 3702our $uuid_lock;
3699 or return; 3714 or return;
3700 3715
3701 my $value = uuid_seq uuid_cur; 3716 my $value = uuid_seq uuid_cur;
3702 3717
3703 unless ($value) { 3718 unless ($value) {
3704 warn "cowardly refusing to write zero uuid value!\n"; 3719 info "cowardly refusing to write zero uuid value!\n";
3705 return; 3720 return;
3706 } 3721 }
3707 3722
3708 my $value = uuid_str $value + $uuid_skip; 3723 my $value = uuid_str $value + $uuid_skip;
3709 $uuid_skip = 0; 3724 $uuid_skip = 0;
3719 or return; 3734 or return;
3720 3735
3721 aio_rename "$uuid~", $uuid 3736 aio_rename "$uuid~", $uuid
3722 and return; 3737 and return;
3723 3738
3724 warn "uuid file written ($value).\n"; 3739 trace "uuid file written ($value).\n";
3725 3740
3726 1 3741 1
3727 3742
3728} 3743}
3729 3744
3735} 3750}
3736 3751
3737sub emergency_save() { 3752sub emergency_save() {
3738 my $freeze_guard = cf::freeze_mainloop; 3753 my $freeze_guard = cf::freeze_mainloop;
3739 3754
3740 warn "emergency_perl_save: enter\n"; 3755 info "emergency_perl_save: enter\n";
3756
3757 # this is a trade-off: we want to be very quick here, so
3758 # save all maps without fsync, and later call a global sync
3759 # (which in turn might be very very slow)
3760 local $USE_FSYNC = 0;
3741 3761
3742 cf::sync_job { 3762 cf::sync_job {
3743 # this is a trade-off: we want to be very quick here, so 3763 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 3764
3748 # use a peculiar iteration method to avoid tripping on perl 3765 # use a peculiar iteration method to avoid tripping on perl
3749 # refcount bugs in for. also avoids problems with players 3766 # refcount bugs in for. also avoids problems with players
3750 # and maps saved/destroyed asynchronously. 3767 # and maps saved/destroyed asynchronously.
3751 warn "emergency_perl_save: begin player save\n"; 3768 info "emergency_perl_save: begin player save\n";
3752 for my $login (keys %cf::PLAYER) { 3769 for my $login (keys %cf::PLAYER) {
3753 my $pl = $cf::PLAYER{$login} or next; 3770 my $pl = $cf::PLAYER{$login} or next;
3754 $pl->valid or next; 3771 $pl->valid or next;
3755 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3772 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3756 $pl->save; 3773 $pl->save;
3757 } 3774 }
3758 warn "emergency_perl_save: end player save\n"; 3775 info "emergency_perl_save: end player save\n";
3759 3776
3777 cf::write_runtime_sync; # external watchdog should not bark
3778
3760 warn "emergency_perl_save: begin map save\n"; 3779 info "emergency_perl_save: begin map save\n";
3761 for my $path (keys %cf::MAP) { 3780 for my $path (keys %cf::MAP) {
3762 my $map = $cf::MAP{$path} or next; 3781 my $map = $cf::MAP{$path} or next;
3763 $map->valid or next; 3782 $map->valid or next;
3764 $map->save; 3783 $map->save;
3765 } 3784 }
3766 warn "emergency_perl_save: end map save\n"; 3785 info "emergency_perl_save: end map save\n";
3767 3786
3787 cf::write_runtime_sync; # external watchdog should not bark
3788
3768 warn "emergency_perl_save: begin database checkpoint\n"; 3789 info "emergency_perl_save: begin database checkpoint\n";
3769 BDB::db_env_txn_checkpoint $DB_ENV; 3790 BDB::db_env_txn_checkpoint $DB_ENV;
3770 warn "emergency_perl_save: end database checkpoint\n"; 3791 info "emergency_perl_save: end database checkpoint\n";
3771 3792
3772 warn "emergency_perl_save: begin write uuid\n"; 3793 info "emergency_perl_save: begin write uuid\n";
3773 write_uuid_sync 1; 3794 write_uuid_sync 1;
3774 warn "emergency_perl_save: end write uuid\n"; 3795 info "emergency_perl_save: end write uuid\n";
3796
3797 cf::write_runtime_sync; # external watchdog should not bark
3798
3799 trace "emergency_perl_save: syncing database to disk";
3800 BDB::db_env_txn_checkpoint $DB_ENV;
3801
3802 info "emergency_perl_save: starting sync\n";
3803 IO::AIO::aio_sync sub {
3804 info "emergency_perl_save: finished sync\n";
3805 };
3806
3807 cf::write_runtime_sync; # external watchdog should not bark
3808
3809 trace "emergency_perl_save: flushing outstanding aio requests";
3810 while (IO::AIO::nreqs || BDB::nreqs) {
3811 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3812 }
3813
3814 cf::write_runtime_sync; # external watchdog should not bark
3775 }; 3815 };
3776 3816
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"; 3817 info "emergency_perl_save: leave\n";
3783} 3818}
3784 3819
3785sub post_cleanup { 3820sub post_cleanup {
3786 my ($make_core) = @_; 3821 my ($make_core) = @_;
3787 3822
3823 IO::AIO::flush;
3824
3825 IO::AIO::flush;
3826
3788 warn Carp::longmess "post_cleanup backtrace" 3827 error Carp::longmess "post_cleanup backtrace"
3789 if $make_core; 3828 if $make_core;
3790 3829
3791 my $fh = pidfile; 3830 my $fh = pidfile;
3792 unlink $PIDFILE if <$fh> == $$; 3831 unlink $PIDFILE if <$fh> == $$;
3793} 3832}
3818} 3857}
3819 3858
3820sub do_reload_perl() { 3859sub do_reload_perl() {
3821 # can/must only be called in main 3860 # can/must only be called in main
3822 if (in_main) { 3861 if (in_main) {
3823 warn "can only reload from main coroutine"; 3862 error "can only reload from main coroutine";
3824 return; 3863 return;
3825 } 3864 }
3826 3865
3827 return if $RELOAD++; 3866 return if $RELOAD++;
3828 3867
3829 my $t1 = AE::time; 3868 my $t1 = AE::time;
3830 3869
3831 while ($RELOAD) { 3870 while ($RELOAD) {
3832 warn "reloading..."; 3871 info "reloading...";
3833 3872
3834 warn "entering sync_job"; 3873 trace "entering sync_job";
3835 3874
3836 cf::sync_job { 3875 cf::sync_job {
3837 cf::write_runtime_sync; # external watchdog should not bark
3838 cf::emergency_save; 3876 cf::emergency_save;
3839 cf::write_runtime_sync; # external watchdog should not bark
3840 3877
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"; 3878 trace "cancelling all extension coros";
3852 $_->cancel for values %EXT_CORO; 3879 $_->cancel for values %EXT_CORO;
3853 %EXT_CORO = (); 3880 %EXT_CORO = ();
3854 3881
3855 warn "removing commands"; 3882 trace "removing commands";
3856 %COMMAND = (); 3883 %COMMAND = ();
3857 3884
3858 warn "removing ext/exti commands"; 3885 trace "removing ext/exti commands";
3859 %EXTCMD = (); 3886 %EXTCMD = ();
3860 %EXTICMD = (); 3887 %EXTICMD = ();
3861 3888
3862 warn "unloading/nuking all extensions"; 3889 trace "unloading/nuking all extensions";
3863 for my $pkg (@EXTS) { 3890 for my $pkg (@EXTS) {
3864 warn "... unloading $pkg"; 3891 trace "... unloading $pkg";
3865 3892
3866 if (my $cb = $pkg->can ("unload")) { 3893 if (my $cb = $pkg->can ("unload")) {
3867 eval { 3894 eval {
3868 $cb->($pkg); 3895 $cb->($pkg);
3869 1 3896 1
3870 } or warn "$pkg unloaded, but with errors: $@"; 3897 } or error "$pkg unloaded, but with errors: $@";
3871 } 3898 }
3872 3899
3873 warn "... clearing $pkg"; 3900 trace "... clearing $pkg";
3874 clear_package $pkg; 3901 clear_package $pkg;
3875 } 3902 }
3876 3903
3877 warn "unloading all perl modules loaded from $LIBDIR"; 3904 trace "unloading all perl modules loaded from $LIBDIR";
3878 while (my ($k, $v) = each %INC) { 3905 while (my ($k, $v) = each %INC) {
3879 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3906 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3880 3907
3881 warn "... unloading $k"; 3908 trace "... unloading $k";
3882 delete $INC{$k}; 3909 delete $INC{$k};
3883 3910
3884 $k =~ s/\.pm$//; 3911 $k =~ s/\.pm$//;
3885 $k =~ s/\//::/g; 3912 $k =~ s/\//::/g;
3886 3913
3889 } 3916 }
3890 3917
3891 clear_package $k; 3918 clear_package $k;
3892 } 3919 }
3893 3920
3894 warn "getting rid of safe::, as good as possible"; 3921 trace "getting rid of safe::, as good as possible";
3895 clear_package "safe::$_" 3922 clear_package "safe::$_"
3896 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3923 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3897 3924
3898 warn "unloading cf.pm \"a bit\""; 3925 trace "unloading cf.pm \"a bit\"";
3899 delete $INC{"cf.pm"}; 3926 delete $INC{"cf.pm"};
3900 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3927 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3901 3928
3902 # don't, removes xs symbols, too, 3929 # don't, removes xs symbols, too,
3903 # and global variables created in xs 3930 # and global variables created in xs
3904 #clear_package __PACKAGE__; 3931 #clear_package __PACKAGE__;
3905 3932
3906 warn "unload completed, starting to reload now"; 3933 info "unload completed, starting to reload now";
3907 3934
3908 warn "reloading cf.pm"; 3935 trace "reloading cf.pm";
3909 require cf; 3936 require cf;
3910 cf::_connect_to_perl_1; 3937 cf::_connect_to_perl_1;
3911 3938
3912 warn "loading config and database again"; 3939 trace "loading config and database again";
3913 cf::reload_config; 3940 cf::reload_config;
3914 3941
3915 warn "loading extensions"; 3942 trace "loading extensions";
3916 cf::load_extensions; 3943 cf::load_extensions;
3917 3944
3918 if ($REATTACH_ON_RELOAD) { 3945 if ($REATTACH_ON_RELOAD) {
3919 warn "reattaching attachments to objects/players"; 3946 trace "reattaching attachments to objects/players";
3920 _global_reattach; # objects, sockets 3947 _global_reattach; # objects, sockets
3921 warn "reattaching attachments to maps"; 3948 trace "reattaching attachments to maps";
3922 reattach $_ for values %MAP; 3949 reattach $_ for values %MAP;
3923 warn "reattaching attachments to players"; 3950 trace "reattaching attachments to players";
3924 reattach $_ for values %PLAYER; 3951 reattach $_ for values %PLAYER;
3925 } 3952 }
3926 3953
3927 warn "running post_init jobs"; 3954 trace "running post_init jobs";
3928 (pop @POST_INIT)->(1) while @POST_INIT; 3955 (pop @POST_INIT)->(1) while @POST_INIT;
3929 3956
3930 warn "leaving sync_job"; 3957 trace "leaving sync_job";
3931 3958
3932 1 3959 1
3933 } or do { 3960 } or do {
3934 warn $@; 3961 error $@;
3935 cf::cleanup "error while reloading, exiting."; 3962 cf::cleanup "error while reloading, exiting.";
3936 }; 3963 };
3937 3964
3938 warn "reloaded"; 3965 info "reloaded";
3939 --$RELOAD; 3966 --$RELOAD;
3940 } 3967 }
3941 3968
3942 $t1 = AE::time - $t1; 3969 $t1 = AE::time - $t1;
3943 warn "reload completed in ${t1}s\n"; 3970 info "reload completed in ${t1}s\n";
3944}; 3971};
3945 3972
3946our $RELOAD_WATCHER; # used only during reload 3973our $RELOAD_WATCHER; # used only during reload
3947 3974
3948sub reload_perl() { 3975sub reload_perl() {
4008 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4035 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4009 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4036 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4010 Coro::async_pool { 4037 Coro::async_pool {
4011 $Coro::current->{desc} = "runtime saver"; 4038 $Coro::current->{desc} = "runtime saver";
4012 write_runtime_sync 4039 write_runtime_sync
4013 or warn "ERROR: unable to write runtime file: $!"; 4040 or error "ERROR: unable to write runtime file: $!";
4014 }; 4041 };
4015 } 4042 }
4016 4043
4017 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4044 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4018 $sig->send; 4045 $sig->send;
4026 4053
4027 if (0) { 4054 if (0) {
4028 if ($NEXT_TICK) { 4055 if ($NEXT_TICK) {
4029 my $jitter = $TICK_START - $NEXT_TICK; 4056 my $jitter = $TICK_START - $NEXT_TICK;
4030 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4057 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4031 warn "jitter $JITTER\n";#d# 4058 debug "jitter $JITTER\n";#d#
4032 } 4059 }
4033 } 4060 }
4034} 4061}
4035 4062
4036{ 4063{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines