… | |
… | |
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 | |
23 | package cf; |
23 | package cf; |
24 | |
24 | |
25 | use 5.10.0; |
25 | use common::sense; |
26 | use utf8; |
|
|
27 | use strict qw(vars subs); |
|
|
28 | |
26 | |
29 | use Symbol; |
27 | use Symbol; |
30 | use List::Util; |
28 | use List::Util; |
31 | use Socket; |
29 | use Socket; |
32 | use EV; |
30 | use EV; |
… | |
… | |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
104 | our $RANDOMDIR = "$LOCALDIR/random"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
105 | our $BDBDIR = "$LOCALDIR/db"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
106 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
107 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
108 | |
111 | our %RESOURCE; |
109 | our %RESOURCE; # unused |
|
|
110 | |
|
|
111 | our $OUTPUT_RATE_MIN = 3000; |
|
|
112 | our $OUTPUT_RATE_MAX = 1000000; |
|
|
113 | |
|
|
114 | our $MAX_LINKS = 32; # how many chained exits to follow |
|
|
115 | our $VERBOSE_IO = 1; |
112 | |
116 | |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
117 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
118 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
115 | our $NEXT_TICK; |
119 | our $NEXT_TICK; |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
120 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
… | |
… | |
162 | |
166 | |
163 | our $EMERGENCY_POSITION; |
167 | our $EMERGENCY_POSITION; |
164 | |
168 | |
165 | sub cf::map::normalise; |
169 | sub cf::map::normalise; |
166 | |
170 | |
|
|
171 | sub in_main() { |
|
|
172 | $Coro::current == $Coro::main |
|
|
173 | } |
|
|
174 | |
167 | ############################################################################# |
175 | ############################################################################# |
168 | |
176 | |
169 | %REFLECT = (); |
177 | %REFLECT = (); |
170 | for (@REFLECT) { |
178 | for (@REFLECT) { |
171 | my $reflect = JSON::XS::decode_json $_; |
179 | my $reflect = JSON::XS::decode_json $_; |
… | |
… | |
242 | |
250 | |
243 | =back |
251 | =back |
244 | |
252 | |
245 | =cut |
253 | =cut |
246 | |
254 | |
|
|
255 | sub error(@) { LOG llevError, join "", @_ } |
|
|
256 | sub warn (@) { LOG llevWarn , join "", @_ } |
|
|
257 | sub info (@) { LOG llevInfo , join "", @_ } |
|
|
258 | sub debug(@) { LOG llevDebug, join "", @_ } |
|
|
259 | sub trace(@) { LOG llevTrace, join "", @_ } |
|
|
260 | |
247 | $Coro::State::WARNHOOK = sub { |
261 | $Coro::State::WARNHOOK = sub { |
248 | my $msg = join "", @_; |
262 | my $msg = join "", @_; |
249 | |
263 | |
250 | $msg .= "\n" |
264 | $msg .= "\n" |
251 | unless $msg =~ /\n$/; |
265 | unless $msg =~ /\n$/; |
252 | |
266 | |
253 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
267 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
254 | |
268 | |
255 | LOG llevError, $msg; |
269 | LOG llevWarn, $msg; |
256 | }; |
270 | }; |
257 | |
271 | |
258 | $Coro::State::DIEHOOK = sub { |
272 | $Coro::State::DIEHOOK = sub { |
259 | return unless $^S eq 0; # "eq", not "==" |
273 | return unless $^S eq 0; # "eq", not "==" |
260 | |
274 | |
261 | warn Carp::longmess $_[0]; |
275 | error Carp::longmess $_[0]; |
262 | |
276 | |
263 | if ($Coro::current == $Coro::main) {#d# |
277 | if (in_main) {#d# |
264 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
278 | error "DIEHOOK called in main context, Coro bug?\n";#d# |
265 | return;#d# |
279 | return;#d# |
266 | }#d# |
280 | }#d# |
267 | |
281 | |
268 | # kill coroutine otherwise |
282 | # kill coroutine otherwise |
269 | Coro::terminate |
283 | Coro::terminate |
… | |
… | |
396 | } |
410 | } |
397 | |
411 | |
398 | =item cf::periodic $interval, $cb |
412 | =item cf::periodic $interval, $cb |
399 | |
413 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
414 | Like EV::periodic, but randomly selects a starting point so that the actions |
401 | get spread over timer. |
415 | get spread over time. |
402 | |
416 | |
403 | =cut |
417 | =cut |
404 | |
418 | |
405 | sub periodic($$) { |
419 | sub periodic($$) { |
406 | my ($interval, $cb) = @_; |
420 | my ($interval, $cb) = @_; |
… | |
… | |
502 | =cut |
516 | =cut |
503 | |
517 | |
504 | sub sync_job(&) { |
518 | sub sync_job(&) { |
505 | my ($job) = @_; |
519 | my ($job) = @_; |
506 | |
520 | |
507 | if ($Coro::current == $Coro::main) { |
521 | if (in_main) { |
508 | my $time = EV::time; |
522 | my $time = AE::time; |
509 | |
523 | |
510 | # this is the main coro, too bad, we have to block |
524 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
525 | # till the operation succeeds, freezing the server :/ |
512 | |
526 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
527 | #LOG llevError, Carp::longmess "sync job";#d# |
514 | |
528 | |
515 | my $freeze_guard = freeze_mainloop; |
529 | my $freeze_guard = freeze_mainloop; |
516 | |
530 | |
517 | my $busy = 1; |
531 | my $busy = 1; |
518 | my @res; |
532 | my @res; |
519 | |
533 | |
520 | (async { |
534 | (async { |
521 | $Coro::current->desc ("sync job coro"); |
535 | $Coro::current->desc ("sync job coro"); |
522 | @res = eval { $job->() }; |
536 | @res = eval { $job->() }; |
523 | warn $@ if $@; |
537 | error $@ if $@; |
524 | undef $busy; |
538 | undef $busy; |
525 | })->prio (Coro::PRIO_MAX); |
539 | })->prio (Coro::PRIO_MAX); |
526 | |
540 | |
527 | while ($busy) { |
541 | while ($busy) { |
528 | if (Coro::nready) { |
542 | if (Coro::nready) { |
… | |
… | |
530 | } else { |
544 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
545 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
546 | } |
533 | } |
547 | } |
534 | |
548 | |
535 | my $time = EV::time - $time; |
549 | my $time = AE::time - $time; |
536 | |
550 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
551 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
552 | |
539 | wantarray ? @res : $res[0] |
553 | wantarray ? @res : $res[0] |
540 | } else { |
554 | } else { |
… | |
… | |
584 | reset_signals; |
598 | reset_signals; |
585 | &$cb |
599 | &$cb |
586 | }, @args; |
600 | }, @args; |
587 | |
601 | |
588 | wantarray ? @res : $res[-1] |
602 | wantarray ? @res : $res[-1] |
|
|
603 | } |
|
|
604 | |
|
|
605 | sub objinfo { |
|
|
606 | ( |
|
|
607 | "counter value" => cf::object::object_count, |
|
|
608 | "objects created" => cf::object::create_count, |
|
|
609 | "objects destroyed" => cf::object::destroy_count, |
|
|
610 | "freelist size" => cf::object::free_count, |
|
|
611 | "allocated objects" => cf::object::objects_size, |
|
|
612 | "active objects" => cf::object::actives_size, |
|
|
613 | ) |
589 | } |
614 | } |
590 | |
615 | |
591 | =item $coin = coin_from_name $name |
616 | =item $coin = coin_from_name $name |
592 | |
617 | |
593 | =cut |
618 | =cut |
… | |
… | |
630 | within each server. |
655 | within each server. |
631 | |
656 | |
632 | =cut |
657 | =cut |
633 | |
658 | |
634 | sub db_table($) { |
659 | sub db_table($) { |
|
|
660 | cf::error "db_get called from main context" |
|
|
661 | if $Coro::current == $Coro::main; |
|
|
662 | |
635 | my ($name) = @_; |
663 | my ($name) = @_; |
636 | my $db = BDB::db_create $DB_ENV; |
664 | my $db = BDB::db_create $DB_ENV; |
637 | |
665 | |
638 | eval { |
666 | eval { |
639 | $db->set_flags (BDB::CHKSUM); |
667 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
649 | } |
677 | } |
650 | |
678 | |
651 | our $DB; |
679 | our $DB; |
652 | |
680 | |
653 | sub db_init { |
681 | sub db_init { |
654 | cf::sync_job { |
|
|
655 | $DB ||= db_table "db"; |
682 | $DB ||= db_table "db"; |
656 | }; |
|
|
657 | } |
683 | } |
658 | |
684 | |
659 | sub db_get($$) { |
685 | sub db_get($$) { |
660 | my $key = "$_[0]/$_[1]"; |
686 | my $key = "$_[0]/$_[1]"; |
661 | |
687 | |
662 | cf::sync_job { |
688 | cf::error "db_get called from main context" |
|
|
689 | if $Coro::current == $Coro::main; |
|
|
690 | |
663 | BDB::db_get $DB, undef, $key, my $data; |
691 | BDB::db_get $DB, undef, $key, my $data; |
664 | |
692 | |
665 | $! ? () |
693 | $! ? () |
666 | : $data |
694 | : $data |
667 | } |
|
|
668 | } |
695 | } |
669 | |
696 | |
670 | sub db_put($$$) { |
697 | sub db_put($$$) { |
671 | BDB::dbreq_pri 4; |
698 | BDB::dbreq_pri 4; |
672 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
699 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
728 | |
755 | |
729 | my $t1 = Time::HiRes::time; |
756 | my $t1 = Time::HiRes::time; |
730 | my $data = $process->(\@data); |
757 | my $data = $process->(\@data); |
731 | my $t2 = Time::HiRes::time; |
758 | my $t2 = Time::HiRes::time; |
732 | |
759 | |
733 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
760 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
734 | |
761 | |
735 | db_put cache => "$id/data", $data; |
762 | db_put cache => "$id/data", $data; |
736 | db_put cache => "$id/md5" , $md5; |
763 | db_put cache => "$id/md5" , $md5; |
737 | db_put cache => "$id/meta", $meta; |
764 | db_put cache => "$id/meta", $meta; |
738 | |
765 | |
… | |
… | |
748 | |
775 | |
749 | =cut |
776 | =cut |
750 | |
777 | |
751 | sub datalog($@) { |
778 | sub datalog($@) { |
752 | my ($type, %kv) = @_; |
779 | my ($type, %kv) = @_; |
753 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
780 | info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
754 | } |
781 | } |
755 | |
782 | |
756 | =back |
783 | =back |
757 | |
784 | |
758 | =cut |
785 | =cut |
… | |
… | |
953 | |
980 | |
954 | } elsif (exists $cb_id{$type}) { |
981 | } elsif (exists $cb_id{$type}) { |
955 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
982 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
956 | |
983 | |
957 | } elsif (ref $type) { |
984 | } elsif (ref $type) { |
958 | warn "attaching objects not supported, ignoring.\n"; |
985 | error "attaching objects not supported, ignoring.\n"; |
959 | |
986 | |
960 | } else { |
987 | } else { |
961 | shift @arg; |
988 | shift @arg; |
962 | warn "attach argument '$type' not supported, ignoring.\n"; |
989 | error "attach argument '$type' not supported, ignoring.\n"; |
963 | } |
990 | } |
964 | } |
991 | } |
965 | } |
992 | } |
966 | |
993 | |
967 | sub _object_attach { |
994 | sub _object_attach { |
… | |
… | |
977 | _attach $registry, $klass, @attach; |
1004 | _attach $registry, $klass, @attach; |
978 | } |
1005 | } |
979 | |
1006 | |
980 | $obj->{$name} = \%arg; |
1007 | $obj->{$name} = \%arg; |
981 | } else { |
1008 | } else { |
982 | warn "object uses attachment '$name' which is not available, postponing.\n"; |
1009 | info "object uses attachment '$name' which is not available, postponing.\n"; |
983 | } |
1010 | } |
984 | |
1011 | |
985 | $obj->{_attachment}{$name} = undef; |
1012 | $obj->{_attachment}{$name} = undef; |
986 | } |
1013 | } |
987 | |
1014 | |
… | |
… | |
1046 | |
1073 | |
1047 | for (@$callbacks) { |
1074 | for (@$callbacks) { |
1048 | eval { &{$_->[1]} }; |
1075 | eval { &{$_->[1]} }; |
1049 | |
1076 | |
1050 | if ($@) { |
1077 | if ($@) { |
1051 | warn "$@"; |
|
|
1052 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1078 | error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1053 | override; |
1079 | override; |
1054 | } |
1080 | } |
1055 | |
1081 | |
1056 | return 1 if $override; |
1082 | return 1 if $override; |
1057 | } |
1083 | } |
… | |
… | |
1136 | for (@$attach) { |
1162 | for (@$attach) { |
1137 | my ($klass, @attach) = @$_; |
1163 | my ($klass, @attach) = @$_; |
1138 | _attach $registry, $klass, @attach; |
1164 | _attach $registry, $klass, @attach; |
1139 | } |
1165 | } |
1140 | } else { |
1166 | } else { |
1141 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
1167 | info "object uses attachment '$name' that is not available, postponing.\n"; |
1142 | } |
1168 | } |
1143 | } |
1169 | } |
1144 | } |
1170 | } |
1145 | |
1171 | |
1146 | cf::attachable->attach ( |
1172 | cf::attachable->attach ( |
… | |
… | |
1173 | my ($filename, $rdata, $objs) = @_; |
1199 | my ($filename, $rdata, $objs) = @_; |
1174 | |
1200 | |
1175 | sync_job { |
1201 | sync_job { |
1176 | if (length $$rdata) { |
1202 | if (length $$rdata) { |
1177 | utf8::decode (my $decname = $filename); |
1203 | utf8::decode (my $decname = $filename); |
1178 | warn sprintf "saving %s (%d,%d)\n", |
1204 | trace sprintf "saving %s (%d,%d)\n", |
1179 | $decname, length $$rdata, scalar @$objs; |
1205 | $decname, length $$rdata, scalar @$objs |
|
|
1206 | if $VERBOSE_IO; |
1180 | |
1207 | |
1181 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1208 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1182 | aio_chmod $fh, SAVE_MODE; |
1209 | aio_chmod $fh, SAVE_MODE; |
1183 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1210 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1184 | if ($cf::USE_FSYNC) { |
1211 | if ($cf::USE_FSYNC) { |
… | |
… | |
1206 | aio_rename "$filename~", $filename; |
1233 | aio_rename "$filename~", $filename; |
1207 | |
1234 | |
1208 | $filename =~ s%/[^/]+$%%; |
1235 | $filename =~ s%/[^/]+$%%; |
1209 | aio_pathsync $filename if $cf::USE_FSYNC; |
1236 | aio_pathsync $filename if $cf::USE_FSYNC; |
1210 | } else { |
1237 | } else { |
1211 | warn "unable to save objects: $filename~: $!\n"; |
1238 | error "unable to save objects: $filename~: $!\n"; |
1212 | } |
1239 | } |
1213 | } else { |
1240 | } else { |
1214 | aio_unlink $filename; |
1241 | aio_unlink $filename; |
1215 | aio_unlink "$filename.pst"; |
1242 | aio_unlink "$filename.pst"; |
1216 | } |
1243 | } |
… | |
… | |
1240 | my $st = eval { Coro::Storable::thaw $av }; |
1267 | my $st = eval { Coro::Storable::thaw $av }; |
1241 | $av = $st->{objs}; |
1268 | $av = $st->{objs}; |
1242 | } |
1269 | } |
1243 | |
1270 | |
1244 | utf8::decode (my $decname = $filename); |
1271 | utf8::decode (my $decname = $filename); |
1245 | warn sprintf "loading %s (%d,%d)\n", |
1272 | trace sprintf "loading %s (%d,%d)\n", |
1246 | $decname, length $data, scalar @{$av || []}; |
1273 | $decname, length $data, scalar @{$av || []} |
|
|
1274 | if $VERBOSE_IO; |
1247 | |
1275 | |
1248 | ($data, $av) |
1276 | ($data, $av) |
1249 | } |
1277 | } |
1250 | |
1278 | |
1251 | =head2 COMMAND CALLBACKS |
1279 | =head2 COMMAND CALLBACKS |
… | |
… | |
1310 | } |
1338 | } |
1311 | |
1339 | |
1312 | use File::Glob (); |
1340 | use File::Glob (); |
1313 | |
1341 | |
1314 | cf::player->attach ( |
1342 | cf::player->attach ( |
1315 | on_command => sub { |
1343 | on_unknown_command => sub { |
1316 | my ($pl, $name, $params) = @_; |
1344 | my ($pl, $name, $params) = @_; |
1317 | |
1345 | |
1318 | my $cb = $COMMAND{$name} |
1346 | my $cb = $COMMAND{$name} |
1319 | or return; |
1347 | or return; |
1320 | |
1348 | |
… | |
… | |
1343 | |
1371 | |
1344 | $pl->ext_reply ($reply, @reply) |
1372 | $pl->ext_reply ($reply, @reply) |
1345 | if $reply; |
1373 | if $reply; |
1346 | |
1374 | |
1347 | } else { |
1375 | } else { |
1348 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1376 | error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1349 | } |
1377 | } |
1350 | |
1378 | |
1351 | cf::override; |
1379 | cf::override; |
1352 | }, |
1380 | }, |
1353 | ); |
1381 | ); |
… | |
… | |
1364 | |
1392 | |
1365 | $grp |
1393 | $grp |
1366 | } |
1394 | } |
1367 | |
1395 | |
1368 | sub load_extensions { |
1396 | sub load_extensions { |
|
|
1397 | info "loading extensions..."; |
|
|
1398 | |
1369 | cf::sync_job { |
1399 | cf::sync_job { |
1370 | my %todo; |
1400 | my %todo; |
1371 | |
1401 | |
1372 | for my $path (<$LIBDIR/*.ext>) { |
1402 | for my $path (<$LIBDIR/*.ext>) { |
1373 | next unless -r $path; |
1403 | next unless -r $path; |
… | |
… | |
1391 | |
1421 | |
1392 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1422 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1393 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1423 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1394 | |
1424 | |
1395 | $ext{source} = |
1425 | $ext{source} = |
1396 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1426 | "package $pkg; use common::sense;\n" |
1397 | . "#line 1 \"$path\"\n{\n" |
1427 | . "#line 1 \"$path\"\n{\n" |
1398 | . $source |
1428 | . $source |
1399 | . "\n};\n1"; |
1429 | . "\n};\n1"; |
1400 | |
1430 | |
1401 | $todo{$base} = \%ext; |
1431 | $todo{$base} = \%ext; |
1402 | } |
1432 | } |
1403 | |
1433 | |
|
|
1434 | my $pass = 0; |
1404 | my %done; |
1435 | my %done; |
1405 | while (%todo) { |
1436 | while (%todo) { |
1406 | my $progress; |
1437 | my $progress; |
1407 | |
1438 | |
|
|
1439 | ++$pass; |
|
|
1440 | |
|
|
1441 | ext: |
1408 | while (my ($k, $v) = each %todo) { |
1442 | while (my ($k, $v) = each %todo) { |
1409 | for (split /,\s*/, $v->{meta}{depends}) { |
1443 | for (split /,\s*/, $v->{meta}{depends}) { |
1410 | goto skip |
1444 | next ext |
1411 | unless exists $done{$_}; |
1445 | unless exists $done{$_}; |
1412 | } |
1446 | } |
1413 | |
1447 | |
1414 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1448 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1415 | |
1449 | |
1416 | unless (eval $v->{source}) { |
1450 | my $active = eval $v->{source}; |
|
|
1451 | |
|
|
1452 | if (length $@) { |
1417 | my $msg = $@ ? "$v->{path}: $@\n" |
1453 | error "$v->{path}: $@\n"; |
1418 | : "$v->{base}: extension inactive.\n"; |
1454 | undef $@; # work around perl 5.10.0 utf-8 caching bug |
1419 | |
1455 | |
1420 | if (exists $v->{meta}{mandatory}) { |
|
|
1421 | warn $msg; |
|
|
1422 | cf::cleanup "mandatory extension failed to load, exiting."; |
1456 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1423 | } |
1457 | if exists $v->{meta}{mandatory}; |
1424 | |
1458 | |
1425 | warn $msg; |
1459 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
|
|
1460 | delete $todo{$k}; |
|
|
1461 | } else { |
|
|
1462 | $done{$k} = delete $todo{$k}; |
|
|
1463 | push @EXTS, $v->{pkg}; |
|
|
1464 | $progress = 1; |
|
|
1465 | |
|
|
1466 | info "$v->{base}: extension inactive.\n" |
|
|
1467 | unless $active; |
1426 | } |
1468 | } |
1427 | |
|
|
1428 | $done{$k} = delete $todo{$k}; |
|
|
1429 | push @EXTS, $v->{pkg}; |
|
|
1430 | $progress = 1; |
|
|
1431 | } |
1469 | } |
1432 | |
1470 | |
1433 | skip: |
1471 | unless ($progress) { |
1434 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1472 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1435 | unless $progress; |
1473 | |
|
|
1474 | while (my ($k, $v) = each %todo) { |
|
|
1475 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1476 | if exists $v->{meta}{mandatory}; |
|
|
1477 | } |
|
|
1478 | } |
1436 | } |
1479 | } |
1437 | }; |
1480 | }; |
1438 | } |
1481 | } |
1439 | |
1482 | |
1440 | ############################################################################# |
1483 | ############################################################################# |
… | |
… | |
1524 | $cf::PLAYER{$login} = $pl |
1567 | $cf::PLAYER{$login} = $pl |
1525 | } |
1568 | } |
1526 | } |
1569 | } |
1527 | } |
1570 | } |
1528 | |
1571 | |
|
|
1572 | cf::player->attach ( |
|
|
1573 | on_load => sub { |
|
|
1574 | my ($pl, $path) = @_; |
|
|
1575 | |
|
|
1576 | # restore slots saved in save, below |
|
|
1577 | my $slots = delete $pl->{_slots}; |
|
|
1578 | |
|
|
1579 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1580 | $pl->combat_ob ($slots->[1]); |
|
|
1581 | $pl->ranged_ob ($slots->[2]); |
|
|
1582 | }, |
|
|
1583 | ); |
|
|
1584 | |
1529 | sub save($) { |
1585 | sub save($) { |
1530 | my ($pl) = @_; |
1586 | my ($pl) = @_; |
1531 | |
1587 | |
1532 | return if $pl->{deny_save}; |
1588 | return if $pl->{deny_save}; |
1533 | |
1589 | |
… | |
… | |
1538 | |
1594 | |
1539 | aio_mkdir playerdir $pl, 0770; |
1595 | aio_mkdir playerdir $pl, 0770; |
1540 | $pl->{last_save} = $cf::RUNTIME; |
1596 | $pl->{last_save} = $cf::RUNTIME; |
1541 | |
1597 | |
1542 | cf::get_slot 0.01; |
1598 | cf::get_slot 0.01; |
|
|
1599 | |
|
|
1600 | # save slots, to be restored later |
|
|
1601 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1543 | |
1602 | |
1544 | $pl->save_pl ($path); |
1603 | $pl->save_pl ($path); |
1545 | cf::cede_to_tick; |
1604 | cf::cede_to_tick; |
1546 | } |
1605 | } |
1547 | |
1606 | |
… | |
… | |
1584 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1643 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1585 | |
1644 | |
1586 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1645 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1587 | $pl->deactivate; |
1646 | $pl->deactivate; |
1588 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1647 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1589 | $pl->ob->check_score; |
|
|
1590 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1648 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1591 | $pl->ns->destroy if $pl->ns; |
1649 | $pl->ns->destroy if $pl->ns; |
1592 | |
1650 | |
1593 | my $path = playerdir $pl; |
1651 | my $path = playerdir $pl; |
1594 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1652 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
… | |
… | |
1649 | \@logins |
1707 | \@logins |
1650 | } |
1708 | } |
1651 | |
1709 | |
1652 | =item $player->maps |
1710 | =item $player->maps |
1653 | |
1711 | |
|
|
1712 | =item cf::player::maps $login |
|
|
1713 | |
1654 | Returns an arrayref of map paths that are private for this |
1714 | Returns an arrayref of map paths that are private for this |
1655 | player. May block. |
1715 | player. May block. |
1656 | |
1716 | |
1657 | =cut |
1717 | =cut |
1658 | |
1718 | |
… | |
… | |
1720 | =cut |
1780 | =cut |
1721 | |
1781 | |
1722 | sub find_by_path($) { |
1782 | sub find_by_path($) { |
1723 | my ($path) = @_; |
1783 | my ($path) = @_; |
1724 | |
1784 | |
|
|
1785 | $path =~ s/^~[^\/]*//; # skip ~login |
|
|
1786 | |
1725 | my ($match, $specificity); |
1787 | my ($match, $specificity); |
1726 | |
1788 | |
1727 | for my $region (list) { |
1789 | for my $region (list) { |
1728 | if ($region->{match} && $path =~ $region->{match}) { |
1790 | if ($region->{match} && $path =~ $region->{match}) { |
1729 | ($match, $specificity) = ($region, $region->specificity) |
1791 | ($match, $specificity) = ($region, $region->specificity) |
… | |
… | |
1760 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1822 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1761 | |
1823 | |
1762 | # mit "rum" bekleckern, nicht |
1824 | # mit "rum" bekleckern, nicht |
1763 | $self->_create_random_map ( |
1825 | $self->_create_random_map ( |
1764 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1826 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1765 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1827 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1766 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1828 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1767 | $rmp->{exit_on_final_map}, |
1829 | $rmp->{exit_on_final_map}, |
1768 | $rmp->{xsize}, $rmp->{ysize}, |
1830 | $rmp->{xsize}, $rmp->{ysize}, |
1769 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1831 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1770 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1832 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
1785 | |
1847 | |
1786 | sub register { |
1848 | sub register { |
1787 | my (undef, $regex, $prio) = @_; |
1849 | my (undef, $regex, $prio) = @_; |
1788 | my $pkg = caller; |
1850 | my $pkg = caller; |
1789 | |
1851 | |
1790 | no strict; |
|
|
1791 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1852 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1792 | |
1853 | |
1793 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1854 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1794 | } |
1855 | } |
1795 | |
1856 | |
1796 | # also paths starting with '/' |
1857 | # also paths starting with '/' |
1797 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1858 | $EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}]; |
1798 | |
1859 | |
1799 | sub thawer_merge { |
1860 | sub thawer_merge { |
1800 | my ($self, $merge) = @_; |
1861 | my ($self, $merge) = @_; |
1801 | |
1862 | |
1802 | # we have to keep some variables in memory intact |
1863 | # we have to keep some variables in memory intact |
… | |
… | |
2084 | } |
2145 | } |
2085 | |
2146 | |
2086 | $self->post_load; |
2147 | $self->post_load; |
2087 | } |
2148 | } |
2088 | |
2149 | |
|
|
2150 | # customize the map for a given player, i.e. |
|
|
2151 | # return the _real_ map. used by e.g. per-player |
|
|
2152 | # maps to change the path to ~playername/mappath |
2089 | sub customise_for { |
2153 | sub customise_for { |
2090 | my ($self, $ob) = @_; |
2154 | my ($self, $ob) = @_; |
2091 | |
2155 | |
2092 | return find "~" . $ob->name . "/" . $self->{path} |
2156 | return find "~" . $ob->name . "/" . $self->{path} |
2093 | if $self->per_player; |
2157 | if $self->per_player; |
… | |
… | |
2109 | or next; |
2173 | or next; |
2110 | $neigh = find $neigh, $map |
2174 | $neigh = find $neigh, $map |
2111 | or next; |
2175 | or next; |
2112 | $neigh->load; |
2176 | $neigh->load; |
2113 | |
2177 | |
|
|
2178 | # now find the diagonal neighbours |
2114 | push @neigh, |
2179 | push @neigh, |
2115 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2180 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2116 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2181 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2117 | } |
2182 | } |
2118 | |
2183 | |
… | |
… | |
2125 | } |
2190 | } |
2126 | |
2191 | |
2127 | sub find_sync { |
2192 | sub find_sync { |
2128 | my ($path, $origin) = @_; |
2193 | my ($path, $origin) = @_; |
2129 | |
2194 | |
2130 | cf::sync_job { find $path, $origin } |
2195 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2196 | if $Coro::current == $Coro::main; |
|
|
2197 | |
|
|
2198 | find $path, $origin |
2131 | } |
2199 | } |
2132 | |
2200 | |
2133 | sub do_load_sync { |
2201 | sub do_load_sync { |
2134 | my ($map) = @_; |
2202 | my ($map) = @_; |
2135 | |
2203 | |
2136 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2204 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2137 | if $Coro::current == $Coro::main; |
2205 | if $Coro::current == $Coro::main; |
2138 | |
2206 | |
2139 | cf::sync_job { $map->load }; |
2207 | $map->load; |
2140 | } |
2208 | } |
2141 | |
2209 | |
2142 | our %MAP_PREFETCH; |
2210 | our %MAP_PREFETCH; |
2143 | our $MAP_PREFETCHER = undef; |
2211 | our $MAP_PREFETCHER = undef; |
2144 | |
2212 | |
… | |
… | |
2170 | $MAP_PREFETCHER->prio (6); |
2238 | $MAP_PREFETCHER->prio (6); |
2171 | |
2239 | |
2172 | () |
2240 | () |
2173 | } |
2241 | } |
2174 | |
2242 | |
|
|
2243 | # common code, used by both ->save and ->swapout |
2175 | sub save { |
2244 | sub _save { |
2176 | my ($self) = @_; |
2245 | my ($self) = @_; |
2177 | |
|
|
2178 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2179 | |
2246 | |
2180 | $self->{last_save} = $cf::RUNTIME; |
2247 | $self->{last_save} = $cf::RUNTIME; |
2181 | |
2248 | |
2182 | return unless $self->dirty; |
2249 | return unless $self->dirty; |
2183 | |
2250 | |
… | |
… | |
2203 | } else { |
2270 | } else { |
2204 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2271 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2205 | } |
2272 | } |
2206 | } |
2273 | } |
2207 | |
2274 | |
|
|
2275 | sub save { |
|
|
2276 | my ($self) = @_; |
|
|
2277 | |
|
|
2278 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2279 | |
|
|
2280 | $self->_save; |
|
|
2281 | } |
|
|
2282 | |
2208 | sub swap_out { |
2283 | sub swap_out { |
2209 | my ($self) = @_; |
2284 | my ($self) = @_; |
2210 | |
2285 | |
2211 | # save first because save cedes |
|
|
2212 | $self->save; |
|
|
2213 | |
|
|
2214 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2286 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2215 | |
2287 | |
2216 | return if $self->players; |
|
|
2217 | return if $self->in_memory != cf::MAP_ACTIVE; |
2288 | return if $self->in_memory != cf::MAP_ACTIVE; |
2218 | return if $self->{deny_save}; |
2289 | return if $self->{deny_save}; |
|
|
2290 | return if $self->players; |
2219 | |
2291 | |
|
|
2292 | # first deactivate the map and "unlink" it from the core |
|
|
2293 | $self->deactivate; |
|
|
2294 | $_->clear_links_to ($self) for values %cf::MAP; |
2220 | $self->in_memory (cf::MAP_SWAPPED); |
2295 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2296 | |
|
|
2297 | # then atomically save |
|
|
2298 | $self->_save; |
|
|
2299 | |
|
|
2300 | # then free the map |
|
|
2301 | $self->clear; |
|
|
2302 | } |
|
|
2303 | |
|
|
2304 | sub reset_at { |
|
|
2305 | my ($self) = @_; |
|
|
2306 | |
|
|
2307 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2308 | return 1e99 if $self->{deny_reset}; |
|
|
2309 | |
|
|
2310 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2311 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2312 | |
|
|
2313 | $time + $to |
|
|
2314 | } |
|
|
2315 | |
|
|
2316 | sub should_reset { |
|
|
2317 | my ($self) = @_; |
|
|
2318 | |
|
|
2319 | $self->reset_at <= $cf::RUNTIME |
|
|
2320 | } |
|
|
2321 | |
|
|
2322 | sub reset { |
|
|
2323 | my ($self) = @_; |
|
|
2324 | |
|
|
2325 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2326 | |
|
|
2327 | return if $self->players; |
|
|
2328 | |
|
|
2329 | cf::trace "resetting map ", $self->path, "\n"; |
|
|
2330 | |
|
|
2331 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2332 | |
|
|
2333 | # need to save uniques path |
|
|
2334 | unless ($self->{deny_save}) { |
|
|
2335 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2336 | |
|
|
2337 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2338 | if $uniq; |
|
|
2339 | } |
|
|
2340 | |
|
|
2341 | delete $cf::MAP{$self->path}; |
2221 | |
2342 | |
2222 | $self->deactivate; |
2343 | $self->deactivate; |
2223 | $_->clear_links_to ($self) for values %cf::MAP; |
2344 | $_->clear_links_to ($self) for values %cf::MAP; |
2224 | $self->clear; |
2345 | $self->clear; |
2225 | } |
|
|
2226 | |
|
|
2227 | sub reset_at { |
|
|
2228 | my ($self) = @_; |
|
|
2229 | |
|
|
2230 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2231 | return 1e99 if $self->{deny_reset}; |
|
|
2232 | |
|
|
2233 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2234 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2235 | |
|
|
2236 | $time + $to |
|
|
2237 | } |
|
|
2238 | |
|
|
2239 | sub should_reset { |
|
|
2240 | my ($self) = @_; |
|
|
2241 | |
|
|
2242 | $self->reset_at <= $cf::RUNTIME |
|
|
2243 | } |
|
|
2244 | |
|
|
2245 | sub reset { |
|
|
2246 | my ($self) = @_; |
|
|
2247 | |
|
|
2248 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2249 | |
|
|
2250 | return if $self->players; |
|
|
2251 | |
|
|
2252 | warn "resetting map ", $self->path, "\n"; |
|
|
2253 | |
|
|
2254 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2255 | |
|
|
2256 | # need to save uniques path |
|
|
2257 | unless ($self->{deny_save}) { |
|
|
2258 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2259 | |
|
|
2260 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2261 | if $uniq; |
|
|
2262 | } |
|
|
2263 | |
|
|
2264 | delete $cf::MAP{$self->path}; |
|
|
2265 | |
|
|
2266 | $self->deactivate; |
|
|
2267 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2268 | $self->clear; |
|
|
2269 | |
2346 | |
2270 | $self->unlink_save; |
2347 | $self->unlink_save; |
2271 | $self->destroy; |
2348 | $self->destroy; |
2272 | } |
2349 | } |
2273 | |
2350 | |
… | |
… | |
2281 | |
2358 | |
2282 | delete $cf::MAP{$self->path}; |
2359 | delete $cf::MAP{$self->path}; |
2283 | |
2360 | |
2284 | $self->unlink_save; |
2361 | $self->unlink_save; |
2285 | |
2362 | |
2286 | bless $self, "cf::map"; |
2363 | bless $self, "cf::map::wrap"; |
2287 | delete $self->{deny_reset}; |
2364 | delete $self->{deny_reset}; |
2288 | $self->{deny_save} = 1; |
2365 | $self->{deny_save} = 1; |
2289 | $self->reset_timeout (1); |
2366 | $self->reset_timeout (1); |
2290 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2367 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2291 | |
2368 | |
… | |
… | |
2521 | |
2598 | |
2522 | Freezes the player and moves him/her to a special map (C<{link}>). |
2599 | Freezes the player and moves him/her to a special map (C<{link}>). |
2523 | |
2600 | |
2524 | The player should be reasonably safe there for short amounts of time (e.g. |
2601 | The player should be reasonably safe there for short amounts of time (e.g. |
2525 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2602 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2526 | though, as the palyer cannot control the character while it is on the link |
2603 | though, as the player cannot control the character while it is on the link |
2527 | map. |
2604 | map. |
2528 | |
2605 | |
2529 | Will never block. |
2606 | Will never block. |
2530 | |
2607 | |
2531 | =item $player_object->leave_link ($map, $x, $y) |
2608 | =item $player_object->leave_link ($map, $x, $y) |
… | |
… | |
2552 | sub cf::object::player::enter_link { |
2629 | sub cf::object::player::enter_link { |
2553 | my ($self) = @_; |
2630 | my ($self) = @_; |
2554 | |
2631 | |
2555 | $self->deactivate_recursive; |
2632 | $self->deactivate_recursive; |
2556 | |
2633 | |
|
|
2634 | ++$self->{_link_recursion}; |
|
|
2635 | |
2557 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2636 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2558 | |
2637 | |
2559 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2638 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2560 | if $self->map && $self->map->{path} ne "{link}"; |
2639 | if $self->map && $self->map->{path} ne "{link}"; |
2561 | |
2640 | |
2562 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
2641 | $self->enter_map ($LINK_MAP || link_map, 3, 3); |
2563 | } |
2642 | } |
2564 | |
2643 | |
2565 | sub cf::object::player::leave_link { |
2644 | sub cf::object::player::leave_link { |
2566 | my ($self, $map, $x, $y) = @_; |
2645 | my ($self, $map, $x, $y) = @_; |
2567 | |
2646 | |
… | |
… | |
2592 | $map->load_neighbours; |
2671 | $map->load_neighbours; |
2593 | |
2672 | |
2594 | return unless $self->contr->active; |
2673 | return unless $self->contr->active; |
2595 | |
2674 | |
2596 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2675 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2597 | $self->enter_map ($map, $x, $y); |
2676 | if ($self->enter_map ($map, $x, $y)) { |
2598 | |
2677 | # entering was successful |
|
|
2678 | delete $self->{_link_recursion}; |
2599 | # only activate afterwards, to support waiting in hooks |
2679 | # only activate afterwards, to support waiting in hooks |
2600 | $self->activate_recursive; |
2680 | $self->activate_recursive; |
2601 | } |
2681 | } |
2602 | |
2682 | |
|
|
2683 | } |
|
|
2684 | |
2603 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2685 | =item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]]) |
2604 | |
2686 | |
2605 | Moves the player to the given map-path and coordinates by first freezing |
2687 | Moves the player to the given map-path and coordinates by first freezing |
2606 | her, loading and preparing them map, calling the provided $check callback |
2688 | her, loading and preparing them map, calling the provided $check callback |
2607 | that has to return the map if sucecssful, and then unfreezes the player on |
2689 | that has to return the map if sucecssful, and then unfreezes the player on |
2608 | the new (success) or old (failed) map position. In either case, $done will |
2690 | the new (success) or old (failed) map position. In either case, $done will |
… | |
… | |
2615 | |
2697 | |
2616 | our $GOTOGEN; |
2698 | our $GOTOGEN; |
2617 | |
2699 | |
2618 | sub cf::object::player::goto { |
2700 | sub cf::object::player::goto { |
2619 | my ($self, $path, $x, $y, $check, $done) = @_; |
2701 | my ($self, $path, $x, $y, $check, $done) = @_; |
|
|
2702 | |
|
|
2703 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
|
|
2704 | error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
|
|
2705 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
|
|
2706 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
|
|
2707 | } |
2620 | |
2708 | |
2621 | # do generation counting so two concurrent goto's will be executed in-order |
2709 | # do generation counting so two concurrent goto's will be executed in-order |
2622 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2710 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2623 | |
2711 | |
2624 | $self->enter_link; |
2712 | $self->enter_link; |
… | |
… | |
2648 | my $map = eval { |
2736 | my $map = eval { |
2649 | my $map = defined $path ? cf::map::find $path : undef; |
2737 | my $map = defined $path ? cf::map::find $path : undef; |
2650 | |
2738 | |
2651 | if ($map) { |
2739 | if ($map) { |
2652 | $map = $map->customise_for ($self); |
2740 | $map = $map->customise_for ($self); |
2653 | $map = $check->($map) if $check && $map; |
2741 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2654 | } else { |
2742 | } else { |
2655 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2743 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2656 | } |
2744 | } |
2657 | |
2745 | |
2658 | $map |
2746 | $map |
… | |
… | |
2666 | if ($gen == $self->{_goto_generation}) { |
2754 | if ($gen == $self->{_goto_generation}) { |
2667 | delete $self->{_goto_generation}; |
2755 | delete $self->{_goto_generation}; |
2668 | $self->leave_link ($map, $x, $y); |
2756 | $self->leave_link ($map, $x, $y); |
2669 | } |
2757 | } |
2670 | |
2758 | |
2671 | $done->() if $done; |
2759 | $done->($self) if $done; |
2672 | })->prio (1); |
2760 | })->prio (1); |
2673 | } |
2761 | } |
2674 | |
2762 | |
2675 | =item $player_object->enter_exit ($exit_object) |
2763 | =item $player_object->enter_exit ($exit_object) |
2676 | |
2764 | |
… | |
… | |
2769 | $self->message ("Something went wrong deep within the deliantra server. " |
2857 | $self->message ("Something went wrong deep within the deliantra server. " |
2770 | . "I'll try to bring you back to the map you were before. " |
2858 | . "I'll try to bring you back to the map you were before. " |
2771 | . "Please report this to the dungeon master!", |
2859 | . "Please report this to the dungeon master!", |
2772 | cf::NDI_UNIQUE | cf::NDI_RED); |
2860 | cf::NDI_UNIQUE | cf::NDI_RED); |
2773 | |
2861 | |
2774 | warn "ERROR in enter_exit: $@"; |
2862 | error "ERROR in enter_exit: $@"; |
2775 | $self->leave_link; |
2863 | $self->leave_link; |
2776 | } |
2864 | } |
2777 | })->prio (1); |
2865 | })->prio (1); |
2778 | } |
2866 | } |
2779 | |
2867 | |
… | |
… | |
3092 | |
3180 | |
3093 | $ns->ext_reply ($reply, @reply) |
3181 | $ns->ext_reply ($reply, @reply) |
3094 | if $reply; |
3182 | if $reply; |
3095 | |
3183 | |
3096 | } else { |
3184 | } else { |
3097 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3185 | error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3098 | } |
3186 | } |
3099 | |
3187 | |
3100 | cf::override; |
3188 | cf::override; |
3101 | }, |
3189 | }, |
3102 | ); |
3190 | ); |
… | |
… | |
3122 | |
3210 | |
3123 | $coro |
3211 | $coro |
3124 | } |
3212 | } |
3125 | |
3213 | |
3126 | cf::client->attach ( |
3214 | cf::client->attach ( |
3127 | on_destroy => sub { |
3215 | on_client_destroy => sub { |
3128 | my ($ns) = @_; |
3216 | my ($ns) = @_; |
3129 | |
3217 | |
3130 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3218 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3131 | }, |
3219 | }, |
3132 | ); |
3220 | ); |
… | |
… | |
3183 | decrease split destroy change_exp value msg lore send_msg)], |
3271 | decrease split destroy change_exp value msg lore send_msg)], |
3184 | ["cf::object::player" => qw(player)], |
3272 | ["cf::object::player" => qw(player)], |
3185 | ["cf::player" => qw(peaceful send_msg)], |
3273 | ["cf::player" => qw(peaceful send_msg)], |
3186 | ["cf::map" => qw(trigger)], |
3274 | ["cf::map" => qw(trigger)], |
3187 | ) { |
3275 | ) { |
3188 | no strict 'refs'; |
|
|
3189 | my ($pkg, @funs) = @$_; |
3276 | my ($pkg, @funs) = @$_; |
3190 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3277 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3191 | for @funs; |
3278 | for @funs; |
3192 | } |
3279 | } |
3193 | |
3280 | |
… | |
… | |
3232 | local @cf::_safe_eval_args = values %vars; |
3319 | local @cf::_safe_eval_args = values %vars; |
3233 | @res = wantarray ? eval eval : scalar eval $eval; |
3320 | @res = wantarray ? eval eval : scalar eval $eval; |
3234 | } |
3321 | } |
3235 | |
3322 | |
3236 | if ($@) { |
3323 | if ($@) { |
3237 | warn "$@"; |
3324 | warn "$@", |
3238 | warn "while executing safe code '$code'\n"; |
3325 | "while executing safe code '$code'\n", |
3239 | warn "with arguments " . (join " ", %vars) . "\n"; |
3326 | "with arguments " . (join " ", %vars) . "\n"; |
3240 | } |
3327 | } |
3241 | |
3328 | |
3242 | wantarray ? @res : $res[0] |
3329 | wantarray ? @res : $res[0] |
3243 | } |
3330 | } |
3244 | |
3331 | |
… | |
… | |
3278 | # for this (global event?) |
3365 | # for this (global event?) |
3279 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3366 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3280 | |
3367 | |
3281 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3368 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3282 | |
3369 | |
3283 | warn "loading facedata from $path\n"; |
3370 | trace "loading facedata from $path\n"; |
3284 | |
3371 | |
3285 | my $facedata; |
3372 | my $facedata; |
3286 | 0 < aio_load $path, $facedata |
3373 | 0 < aio_load $path, $facedata |
3287 | or die "$path: $!"; |
3374 | or die "$path: $!"; |
3288 | |
3375 | |
… | |
… | |
3322 | |
3409 | |
3323 | if (my $smooth = cf::face::find $info->{smooth}) { |
3410 | if (my $smooth = cf::face::find $info->{smooth}) { |
3324 | cf::face::set_smooth $idx, $smooth; |
3411 | cf::face::set_smooth $idx, $smooth; |
3325 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3412 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3326 | } else { |
3413 | } else { |
3327 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3414 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3328 | } |
3415 | } |
3329 | |
3416 | |
3330 | cf::cede_to_tick; |
3417 | cf::cede_to_tick; |
3331 | } |
3418 | } |
3332 | } |
3419 | } |
… | |
… | |
3350 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3437 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3351 | |
3438 | |
3352 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3439 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3353 | cf::face::set_type $idx, $info->{type}; |
3440 | cf::face::set_type $idx, $info->{type}; |
3354 | } else { |
3441 | } else { |
3355 | $RESOURCE{$name} = $info; |
3442 | $RESOURCE{$name} = $info; # unused |
3356 | } |
3443 | } |
3357 | |
3444 | |
3358 | cf::cede_to_tick; |
3445 | cf::cede_to_tick; |
3359 | } |
3446 | } |
3360 | } |
3447 | } |
3361 | |
3448 | |
3362 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3449 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3363 | |
3450 | |
3364 | 1 |
3451 | 1 |
3365 | } |
3452 | } |
3366 | |
|
|
3367 | cf::global->attach (on_resource_update => sub { |
|
|
3368 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3369 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3370 | |
|
|
3371 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3372 | my $sound = $soundconf->{compat}[$_] |
|
|
3373 | or next; |
|
|
3374 | |
|
|
3375 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3376 | cf::sound::set $sound->[0] => $face; |
|
|
3377 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3378 | } |
|
|
3379 | |
|
|
3380 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3381 | my $face = cf::face::find "sound/$v"; |
|
|
3382 | cf::sound::set $k => $face; |
|
|
3383 | } |
|
|
3384 | } |
|
|
3385 | }); |
|
|
3386 | |
3453 | |
3387 | register_exticmd fx_want => sub { |
3454 | register_exticmd fx_want => sub { |
3388 | my ($ns, $want) = @_; |
3455 | my ($ns, $want) = @_; |
3389 | |
3456 | |
3390 | while (my ($k, $v) = each %$want) { |
3457 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3429 | sub reload_treasures { |
3496 | sub reload_treasures { |
3430 | load_resource_file "$DATADIR/treasures" |
3497 | load_resource_file "$DATADIR/treasures" |
3431 | or die "unable to load treasurelists\n"; |
3498 | or die "unable to load treasurelists\n"; |
3432 | } |
3499 | } |
3433 | |
3500 | |
|
|
3501 | sub reload_sound { |
|
|
3502 | trace "loading sound config from $DATADIR/sound\n"; |
|
|
3503 | |
|
|
3504 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3505 | or die "$DATADIR/sound $!"; |
|
|
3506 | |
|
|
3507 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
|
|
3508 | |
|
|
3509 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3510 | my $sound = $soundconf->{compat}[$_] |
|
|
3511 | or next; |
|
|
3512 | |
|
|
3513 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3514 | cf::sound::set $sound->[0] => $face; |
|
|
3515 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3516 | } |
|
|
3517 | |
|
|
3518 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3519 | my $face = cf::face::find "sound/$v"; |
|
|
3520 | cf::sound::set $k => $face; |
|
|
3521 | } |
|
|
3522 | } |
|
|
3523 | |
3434 | sub reload_resources { |
3524 | sub reload_resources { |
3435 | warn "reloading resource files...\n"; |
3525 | trace "reloading resource files...\n"; |
3436 | |
3526 | |
3437 | reload_facedata; |
3527 | reload_facedata; |
|
|
3528 | reload_sound; |
3438 | reload_archetypes; |
3529 | reload_archetypes; |
3439 | reload_regions; |
3530 | reload_regions; |
3440 | reload_treasures; |
3531 | reload_treasures; |
3441 | |
3532 | |
3442 | warn "finished reloading resource files\n"; |
3533 | trace "finished reloading resource files\n"; |
3443 | } |
3534 | } |
3444 | |
3535 | |
3445 | sub reload_config { |
3536 | sub reload_config { |
3446 | warn "reloading config file...\n"; |
3537 | trace "reloading config file...\n"; |
3447 | |
3538 | |
3448 | open my $fh, "<:utf8", "$CONFDIR/config" |
3539 | open my $fh, "<:utf8", "$CONFDIR/config" |
3449 | or return; |
3540 | or return; |
3450 | |
3541 | |
3451 | local $/; |
3542 | local $/; |
3452 | *CFG = YAML::XS::Load scalar <$fh>; |
3543 | *CFG = YAML::XS::Load scalar <$fh>; |
3453 | |
3544 | |
3454 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3545 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3455 | |
3546 | |
3456 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3547 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3457 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3548 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3458 | |
3549 | |
3459 | if (exists $CFG{mlockall}) { |
3550 | if (exists $CFG{mlockall}) { |
… | |
… | |
3462 | and die "WARNING: m(un)lockall failed: $!\n"; |
3553 | and die "WARNING: m(un)lockall failed: $!\n"; |
3463 | }; |
3554 | }; |
3464 | warn $@ if $@; |
3555 | warn $@ if $@; |
3465 | } |
3556 | } |
3466 | |
3557 | |
3467 | warn "finished reloading resource files\n"; |
3558 | trace "finished reloading resource files\n"; |
3468 | } |
3559 | } |
3469 | |
3560 | |
3470 | sub pidfile() { |
3561 | sub pidfile() { |
3471 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3562 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3472 | or die "$PIDFILE: $!"; |
3563 | or die "$PIDFILE: $!"; |
… | |
… | |
3485 | seek $fh, 0, 0; |
3576 | seek $fh, 0, 0; |
3486 | print $fh $$; |
3577 | print $fh $$; |
3487 | } |
3578 | } |
3488 | |
3579 | |
3489 | sub main_loop { |
3580 | sub main_loop { |
3490 | warn "EV::loop starting\n"; |
3581 | trace "EV::loop starting\n"; |
3491 | if (1) { |
3582 | if (1) { |
3492 | EV::loop; |
3583 | EV::loop; |
3493 | } |
3584 | } |
3494 | warn "EV::loop returned\n"; |
3585 | trace "EV::loop returned\n"; |
3495 | goto &main_loop unless $REALLY_UNLOOP; |
3586 | goto &main_loop unless $REALLY_UNLOOP; |
3496 | } |
3587 | } |
3497 | |
3588 | |
3498 | sub main { |
3589 | sub main { |
3499 | cf::init_globals; # initialise logging |
3590 | cf::init_globals; # initialise logging |
3500 | |
3591 | |
3501 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3592 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3502 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3593 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3503 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3594 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3504 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3595 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3505 | |
|
|
3506 | cf::init_experience; |
|
|
3507 | cf::init_anim; |
|
|
3508 | cf::init_attackmess; |
|
|
3509 | cf::init_dynamic; |
|
|
3510 | |
3596 | |
3511 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3597 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3512 | |
3598 | |
3513 | # we must not ever block the main coroutine |
3599 | # we must not ever block the main coroutine |
3514 | local $Coro::idle = sub { |
3600 | local $Coro::idle = sub { |
… | |
… | |
3520 | }; |
3606 | }; |
3521 | |
3607 | |
3522 | evthread_start IO::AIO::poll_fileno; |
3608 | evthread_start IO::AIO::poll_fileno; |
3523 | |
3609 | |
3524 | cf::sync_job { |
3610 | cf::sync_job { |
|
|
3611 | cf::init_experience; |
|
|
3612 | cf::init_anim; |
|
|
3613 | cf::init_attackmess; |
|
|
3614 | cf::init_dynamic; |
|
|
3615 | |
3525 | cf::load_settings; |
3616 | cf::load_settings; |
3526 | cf::load_materials; |
3617 | cf::load_materials; |
3527 | |
3618 | |
3528 | reload_resources; |
3619 | reload_resources; |
3529 | reload_config; |
3620 | reload_config; |
3530 | db_init; |
3621 | db_init; |
3531 | |
3622 | |
3532 | cf::init_uuid; |
3623 | cf::init_uuid; |
3533 | cf::init_signals; |
3624 | cf::init_signals; |
3534 | cf::init_commands; |
|
|
3535 | cf::init_skills; |
3625 | cf::init_skills; |
3536 | |
3626 | |
3537 | cf::init_beforeplay; |
3627 | cf::init_beforeplay; |
3538 | |
3628 | |
3539 | atomic; |
3629 | atomic; |
… | |
… | |
3546 | use POSIX (); |
3636 | use POSIX (); |
3547 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3637 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3548 | |
3638 | |
3549 | (pop @POST_INIT)->(0) while @POST_INIT; |
3639 | (pop @POST_INIT)->(0) while @POST_INIT; |
3550 | }; |
3640 | }; |
|
|
3641 | |
|
|
3642 | cf::object::thawer::errors_are_fatal 0; |
|
|
3643 | info "parse errors in files are no longer fatal from this point on.\n"; |
3551 | |
3644 | |
3552 | main_loop; |
3645 | main_loop; |
3553 | } |
3646 | } |
3554 | |
3647 | |
3555 | ############################################################################# |
3648 | ############################################################################# |
… | |
… | |
3557 | |
3650 | |
3558 | # install some emergency cleanup handlers |
3651 | # install some emergency cleanup handlers |
3559 | BEGIN { |
3652 | BEGIN { |
3560 | our %SIGWATCHER = (); |
3653 | our %SIGWATCHER = (); |
3561 | for my $signal (qw(INT HUP TERM)) { |
3654 | for my $signal (qw(INT HUP TERM)) { |
3562 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3655 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3563 | cf::cleanup "SIG$signal"; |
3656 | cf::cleanup "SIG$signal"; |
3564 | }; |
3657 | }; |
3565 | } |
3658 | } |
3566 | } |
3659 | } |
3567 | |
3660 | |
3568 | sub write_runtime_sync { |
3661 | sub write_runtime_sync { |
|
|
3662 | my $t0 = AE::time; |
|
|
3663 | |
3569 | # first touch the runtime file to show we are still running: |
3664 | # first touch the runtime file to show we are still running: |
3570 | # the fsync below can take a very very long time. |
3665 | # the fsync below can take a very very long time. |
3571 | |
3666 | |
3572 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3667 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3573 | |
3668 | |
3574 | my $guard = cf::lock_acquire "write_runtime"; |
3669 | my $guard = cf::lock_acquire "write_runtime"; |
3575 | |
3670 | |
3576 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3671 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3577 | or return; |
3672 | or return; |
3578 | |
3673 | |
3579 | my $value = $cf::RUNTIME + 90 + 10; |
3674 | my $value = $cf::RUNTIME + 90 + 10; |
3580 | # 10 is the runtime save interval, for a monotonic clock |
3675 | # 10 is the runtime save interval, for a monotonic clock |
3581 | # 60 allows for the watchdog to kill the server. |
3676 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3594 | or return; |
3689 | or return; |
3595 | |
3690 | |
3596 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3691 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3597 | and return; |
3692 | and return; |
3598 | |
3693 | |
3599 | warn "runtime file written.\n"; |
3694 | trace sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3600 | |
3695 | |
3601 | 1 |
3696 | 1 |
3602 | } |
3697 | } |
3603 | |
3698 | |
3604 | our $uuid_lock; |
3699 | our $uuid_lock; |
… | |
… | |
3616 | or return; |
3711 | or return; |
3617 | |
3712 | |
3618 | my $value = uuid_seq uuid_cur; |
3713 | my $value = uuid_seq uuid_cur; |
3619 | |
3714 | |
3620 | unless ($value) { |
3715 | unless ($value) { |
3621 | warn "cowardly refusing to write zero uuid value!\n"; |
3716 | info "cowardly refusing to write zero uuid value!\n"; |
3622 | return; |
3717 | return; |
3623 | } |
3718 | } |
3624 | |
3719 | |
3625 | my $value = uuid_str $value + $uuid_skip; |
3720 | my $value = uuid_str $value + $uuid_skip; |
3626 | $uuid_skip = 0; |
3721 | $uuid_skip = 0; |
… | |
… | |
3636 | or return; |
3731 | or return; |
3637 | |
3732 | |
3638 | aio_rename "$uuid~", $uuid |
3733 | aio_rename "$uuid~", $uuid |
3639 | and return; |
3734 | and return; |
3640 | |
3735 | |
3641 | warn "uuid file written ($value).\n"; |
3736 | trace "uuid file written ($value).\n"; |
3642 | |
3737 | |
3643 | 1 |
3738 | 1 |
3644 | |
3739 | |
3645 | } |
3740 | } |
3646 | |
3741 | |
… | |
… | |
3652 | } |
3747 | } |
3653 | |
3748 | |
3654 | sub emergency_save() { |
3749 | sub emergency_save() { |
3655 | my $freeze_guard = cf::freeze_mainloop; |
3750 | my $freeze_guard = cf::freeze_mainloop; |
3656 | |
3751 | |
3657 | warn "emergency_perl_save: enter\n"; |
3752 | info "emergency_perl_save: enter\n"; |
|
|
3753 | |
|
|
3754 | # this is a trade-off: we want to be very quick here, so |
|
|
3755 | # save all maps without fsync, and later call a global sync |
|
|
3756 | # (which in turn might be very very slow) |
|
|
3757 | local $USE_FSYNC = 0; |
3658 | |
3758 | |
3659 | cf::sync_job { |
3759 | cf::sync_job { |
3660 | # this is a trade-off: we want to be very quick here, so |
3760 | cf::write_runtime_sync; # external watchdog should not bark |
3661 | # save all maps without fsync, and later call a global sync |
|
|
3662 | # (which in turn might be very very slow) |
|
|
3663 | local $USE_FSYNC = 0; |
|
|
3664 | |
3761 | |
3665 | # use a peculiar iteration method to avoid tripping on perl |
3762 | # use a peculiar iteration method to avoid tripping on perl |
3666 | # refcount bugs in for. also avoids problems with players |
3763 | # refcount bugs in for. also avoids problems with players |
3667 | # and maps saved/destroyed asynchronously. |
3764 | # and maps saved/destroyed asynchronously. |
3668 | warn "emergency_perl_save: begin player save\n"; |
3765 | info "emergency_perl_save: begin player save\n"; |
3669 | for my $login (keys %cf::PLAYER) { |
3766 | for my $login (keys %cf::PLAYER) { |
3670 | my $pl = $cf::PLAYER{$login} or next; |
3767 | my $pl = $cf::PLAYER{$login} or next; |
3671 | $pl->valid or next; |
3768 | $pl->valid or next; |
3672 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3769 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3673 | $pl->save; |
3770 | $pl->save; |
3674 | } |
3771 | } |
3675 | warn "emergency_perl_save: end player save\n"; |
3772 | info "emergency_perl_save: end player save\n"; |
3676 | |
3773 | |
|
|
3774 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3775 | |
3677 | warn "emergency_perl_save: begin map save\n"; |
3776 | info "emergency_perl_save: begin map save\n"; |
3678 | for my $path (keys %cf::MAP) { |
3777 | for my $path (keys %cf::MAP) { |
3679 | my $map = $cf::MAP{$path} or next; |
3778 | my $map = $cf::MAP{$path} or next; |
3680 | $map->valid or next; |
3779 | $map->valid or next; |
3681 | $map->save; |
3780 | $map->save; |
3682 | } |
3781 | } |
3683 | warn "emergency_perl_save: end map save\n"; |
3782 | info "emergency_perl_save: end map save\n"; |
3684 | |
3783 | |
|
|
3784 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3785 | |
3685 | warn "emergency_perl_save: begin database checkpoint\n"; |
3786 | info "emergency_perl_save: begin database checkpoint\n"; |
3686 | BDB::db_env_txn_checkpoint $DB_ENV; |
3787 | BDB::db_env_txn_checkpoint $DB_ENV; |
3687 | warn "emergency_perl_save: end database checkpoint\n"; |
3788 | info "emergency_perl_save: end database checkpoint\n"; |
3688 | |
3789 | |
3689 | warn "emergency_perl_save: begin write uuid\n"; |
3790 | info "emergency_perl_save: begin write uuid\n"; |
3690 | write_uuid_sync 1; |
3791 | write_uuid_sync 1; |
3691 | warn "emergency_perl_save: end write uuid\n"; |
3792 | info "emergency_perl_save: end write uuid\n"; |
|
|
3793 | |
|
|
3794 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3795 | |
|
|
3796 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3797 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3798 | |
|
|
3799 | info "emergency_perl_save: starting sync\n"; |
|
|
3800 | IO::AIO::aio_sync sub { |
|
|
3801 | info "emergency_perl_save: finished sync\n"; |
|
|
3802 | }; |
|
|
3803 | |
|
|
3804 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3805 | |
|
|
3806 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3807 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3808 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3809 | } |
|
|
3810 | |
|
|
3811 | cf::write_runtime_sync; # external watchdog should not bark |
3692 | }; |
3812 | }; |
3693 | |
3813 | |
3694 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3695 | IO::AIO::aio_sync sub { |
|
|
3696 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3697 | }; |
|
|
3698 | |
|
|
3699 | warn "emergency_perl_save: leave\n"; |
3814 | info "emergency_perl_save: leave\n"; |
3700 | } |
3815 | } |
3701 | |
3816 | |
3702 | sub post_cleanup { |
3817 | sub post_cleanup { |
3703 | my ($make_core) = @_; |
3818 | my ($make_core) = @_; |
3704 | |
3819 | |
|
|
3820 | IO::AIO::flush; |
|
|
3821 | |
3705 | warn Carp::longmess "post_cleanup backtrace" |
3822 | error Carp::longmess "post_cleanup backtrace" |
3706 | if $make_core; |
3823 | if $make_core; |
3707 | |
3824 | |
3708 | my $fh = pidfile; |
3825 | my $fh = pidfile; |
3709 | unlink $PIDFILE if <$fh> == $$; |
3826 | unlink $PIDFILE if <$fh> == $$; |
3710 | } |
3827 | } |
… | |
… | |
3730 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3847 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3731 | for my $name (keys %$leaf_symtab) { |
3848 | for my $name (keys %$leaf_symtab) { |
3732 | _gv_clear *{"$pkg$name"}; |
3849 | _gv_clear *{"$pkg$name"}; |
3733 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3850 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3734 | } |
3851 | } |
3735 | warn "cleared package $pkg\n";#d# |
|
|
3736 | } |
3852 | } |
3737 | |
3853 | |
3738 | sub do_reload_perl() { |
3854 | sub do_reload_perl() { |
3739 | # can/must only be called in main |
3855 | # can/must only be called in main |
3740 | if ($Coro::current != $Coro::main) { |
3856 | if (in_main) { |
3741 | warn "can only reload from main coroutine"; |
3857 | error "can only reload from main coroutine"; |
3742 | return; |
3858 | return; |
3743 | } |
3859 | } |
3744 | |
3860 | |
3745 | return if $RELOAD++; |
3861 | return if $RELOAD++; |
3746 | |
3862 | |
3747 | my $t1 = EV::time; |
3863 | my $t1 = AE::time; |
3748 | |
3864 | |
3749 | while ($RELOAD) { |
3865 | while ($RELOAD) { |
3750 | warn "reloading..."; |
3866 | info "reloading..."; |
3751 | |
3867 | |
3752 | warn "entering sync_job"; |
3868 | trace "entering sync_job"; |
3753 | |
3869 | |
3754 | cf::sync_job { |
3870 | cf::sync_job { |
3755 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3756 | cf::emergency_save; |
3871 | cf::emergency_save; |
3757 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3758 | |
3872 | |
3759 | warn "syncing database to disk"; |
|
|
3760 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3761 | |
|
|
3762 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
3763 | |
|
|
3764 | warn "flushing outstanding aio requests"; |
|
|
3765 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3766 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3767 | } |
|
|
3768 | |
|
|
3769 | warn "cancelling all extension coros"; |
3873 | trace "cancelling all extension coros"; |
3770 | $_->cancel for values %EXT_CORO; |
3874 | $_->cancel for values %EXT_CORO; |
3771 | %EXT_CORO = (); |
3875 | %EXT_CORO = (); |
3772 | |
3876 | |
3773 | warn "removing commands"; |
3877 | trace "removing commands"; |
3774 | %COMMAND = (); |
3878 | %COMMAND = (); |
3775 | |
3879 | |
3776 | warn "removing ext/exti commands"; |
3880 | trace "removing ext/exti commands"; |
3777 | %EXTCMD = (); |
3881 | %EXTCMD = (); |
3778 | %EXTICMD = (); |
3882 | %EXTICMD = (); |
3779 | |
3883 | |
3780 | warn "unloading/nuking all extensions"; |
3884 | trace "unloading/nuking all extensions"; |
3781 | for my $pkg (@EXTS) { |
3885 | for my $pkg (@EXTS) { |
3782 | warn "... unloading $pkg"; |
3886 | trace "... unloading $pkg"; |
3783 | |
3887 | |
3784 | if (my $cb = $pkg->can ("unload")) { |
3888 | if (my $cb = $pkg->can ("unload")) { |
3785 | eval { |
3889 | eval { |
3786 | $cb->($pkg); |
3890 | $cb->($pkg); |
3787 | 1 |
3891 | 1 |
3788 | } or warn "$pkg unloaded, but with errors: $@"; |
3892 | } or error "$pkg unloaded, but with errors: $@"; |
3789 | } |
3893 | } |
3790 | |
3894 | |
3791 | warn "... clearing $pkg"; |
3895 | trace "... clearing $pkg"; |
3792 | clear_package $pkg; |
3896 | clear_package $pkg; |
3793 | } |
3897 | } |
3794 | |
3898 | |
3795 | warn "unloading all perl modules loaded from $LIBDIR"; |
3899 | trace "unloading all perl modules loaded from $LIBDIR"; |
3796 | while (my ($k, $v) = each %INC) { |
3900 | while (my ($k, $v) = each %INC) { |
3797 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3901 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3798 | |
3902 | |
3799 | warn "... unloading $k"; |
3903 | trace "... unloading $k"; |
3800 | delete $INC{$k}; |
3904 | delete $INC{$k}; |
3801 | |
3905 | |
3802 | $k =~ s/\.pm$//; |
3906 | $k =~ s/\.pm$//; |
3803 | $k =~ s/\//::/g; |
3907 | $k =~ s/\//::/g; |
3804 | |
3908 | |
… | |
… | |
3807 | } |
3911 | } |
3808 | |
3912 | |
3809 | clear_package $k; |
3913 | clear_package $k; |
3810 | } |
3914 | } |
3811 | |
3915 | |
3812 | warn "getting rid of safe::, as good as possible"; |
3916 | trace "getting rid of safe::, as good as possible"; |
3813 | clear_package "safe::$_" |
3917 | clear_package "safe::$_" |
3814 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3918 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3815 | |
3919 | |
3816 | warn "unloading cf.pm \"a bit\""; |
3920 | trace "unloading cf.pm \"a bit\""; |
3817 | delete $INC{"cf.pm"}; |
3921 | delete $INC{"cf.pm"}; |
3818 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3922 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3819 | |
3923 | |
3820 | # don't, removes xs symbols, too, |
3924 | # don't, removes xs symbols, too, |
3821 | # and global variables created in xs |
3925 | # and global variables created in xs |
3822 | #clear_package __PACKAGE__; |
3926 | #clear_package __PACKAGE__; |
3823 | |
3927 | |
3824 | warn "unload completed, starting to reload now"; |
3928 | info "unload completed, starting to reload now"; |
3825 | |
3929 | |
3826 | warn "reloading cf.pm"; |
3930 | trace "reloading cf.pm"; |
3827 | require cf; |
3931 | require cf; |
3828 | cf::_connect_to_perl_1; |
3932 | cf::_connect_to_perl_1; |
3829 | |
3933 | |
3830 | warn "loading config and database again"; |
3934 | trace "loading config and database again"; |
3831 | cf::reload_config; |
3935 | cf::reload_config; |
3832 | |
3936 | |
3833 | warn "loading extensions"; |
3937 | trace "loading extensions"; |
3834 | cf::load_extensions; |
3938 | cf::load_extensions; |
3835 | |
3939 | |
3836 | if ($REATTACH_ON_RELOAD) { |
3940 | if ($REATTACH_ON_RELOAD) { |
3837 | warn "reattaching attachments to objects/players"; |
3941 | trace "reattaching attachments to objects/players"; |
3838 | _global_reattach; # objects, sockets |
3942 | _global_reattach; # objects, sockets |
3839 | warn "reattaching attachments to maps"; |
3943 | trace "reattaching attachments to maps"; |
3840 | reattach $_ for values %MAP; |
3944 | reattach $_ for values %MAP; |
3841 | warn "reattaching attachments to players"; |
3945 | trace "reattaching attachments to players"; |
3842 | reattach $_ for values %PLAYER; |
3946 | reattach $_ for values %PLAYER; |
3843 | } |
3947 | } |
3844 | |
3948 | |
3845 | warn "running post_init jobs"; |
3949 | trace "running post_init jobs"; |
3846 | (pop @POST_INIT)->(1) while @POST_INIT; |
3950 | (pop @POST_INIT)->(1) while @POST_INIT; |
3847 | |
3951 | |
3848 | warn "leaving sync_job"; |
3952 | trace "leaving sync_job"; |
3849 | |
3953 | |
3850 | 1 |
3954 | 1 |
3851 | } or do { |
3955 | } or do { |
3852 | warn $@; |
3956 | error $@; |
3853 | cf::cleanup "error while reloading, exiting."; |
3957 | cf::cleanup "error while reloading, exiting."; |
3854 | }; |
3958 | }; |
3855 | |
3959 | |
3856 | warn "reloaded"; |
3960 | info "reloaded"; |
3857 | --$RELOAD; |
3961 | --$RELOAD; |
3858 | } |
3962 | } |
3859 | |
3963 | |
3860 | $t1 = EV::time - $t1; |
3964 | $t1 = AE::time - $t1; |
3861 | warn "reload completed in ${t1}s\n"; |
3965 | info "reload completed in ${t1}s\n"; |
3862 | }; |
3966 | }; |
3863 | |
3967 | |
3864 | our $RELOAD_WATCHER; # used only during reload |
3968 | our $RELOAD_WATCHER; # used only during reload |
3865 | |
3969 | |
3866 | sub reload_perl() { |
3970 | sub reload_perl() { |
… | |
… | |
3868 | # coro crashes during coro_state_free->destroy here. |
3972 | # coro crashes during coro_state_free->destroy here. |
3869 | |
3973 | |
3870 | $RELOAD_WATCHER ||= cf::async { |
3974 | $RELOAD_WATCHER ||= cf::async { |
3871 | Coro::AIO::aio_wait cache_extensions; |
3975 | Coro::AIO::aio_wait cache_extensions; |
3872 | |
3976 | |
3873 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3977 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3874 | do_reload_perl; |
3978 | do_reload_perl; |
3875 | undef $RELOAD_WATCHER; |
3979 | undef $RELOAD_WATCHER; |
3876 | }; |
3980 | }; |
3877 | }; |
3981 | }; |
3878 | } |
3982 | } |
… | |
… | |
3919 | return; |
4023 | return; |
3920 | } |
4024 | } |
3921 | |
4025 | |
3922 | cf::server_tick; # one server iteration |
4026 | cf::server_tick; # one server iteration |
3923 | |
4027 | |
3924 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
4028 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3925 | |
4029 | |
3926 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4030 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3927 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4031 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3928 | Coro::async_pool { |
4032 | Coro::async_pool { |
3929 | $Coro::current->{desc} = "runtime saver"; |
4033 | $Coro::current->{desc} = "runtime saver"; |
3930 | write_runtime_sync |
4034 | write_runtime_sync |
3931 | or warn "ERROR: unable to write runtime file: $!"; |
4035 | or error "ERROR: unable to write runtime file: $!"; |
3932 | }; |
4036 | }; |
3933 | } |
4037 | } |
3934 | |
4038 | |
3935 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4039 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3936 | $sig->send; |
4040 | $sig->send; |
… | |
… | |
3944 | |
4048 | |
3945 | if (0) { |
4049 | if (0) { |
3946 | if ($NEXT_TICK) { |
4050 | if ($NEXT_TICK) { |
3947 | my $jitter = $TICK_START - $NEXT_TICK; |
4051 | my $jitter = $TICK_START - $NEXT_TICK; |
3948 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4052 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
3949 | warn "jitter $JITTER\n";#d# |
4053 | debug "jitter $JITTER\n";#d# |
3950 | } |
4054 | } |
3951 | } |
4055 | } |
3952 | } |
4056 | } |
3953 | |
4057 | |
3954 | { |
4058 | { |