… | |
… | |
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; |
… | |
… | |
78 | # strictly for debugging |
76 | # strictly for debugging |
79 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
80 | |
78 | |
81 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
82 | |
80 | |
|
|
81 | our @ORIG_INC; |
|
|
82 | |
83 | our %COMMAND = (); |
83 | our %COMMAND = (); |
84 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
85 | |
85 | |
86 | our @EXTS = (); # list of extension package names |
86 | our @EXTS = (); # list of extension package names |
87 | our %EXTCMD = (); |
87 | our %EXTCMD = (); |
… | |
… | |
93 | our @EVENT; |
93 | our @EVENT; |
94 | our @REFLECT; # set by XS |
94 | our @REFLECT; # set by XS |
95 | our %REFLECT; # set by us |
95 | our %REFLECT; # set by us |
96 | |
96 | |
97 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
|
|
98 | |
98 | our $DATADIR = datadir; |
99 | our $DATADIR = datadir; |
99 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $PODDIR = "$DATADIR/pod"; |
101 | our $PODDIR = "$DATADIR/pod"; |
101 | our $MAPDIR = "$DATADIR/" . mapdir; |
102 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
103 | |
102 | our $LOCALDIR = localdir; |
104 | our $LOCALDIR = localdir; |
103 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
105 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
104 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
106 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
105 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
107 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
109 | our $BDBDIR = "$LOCALDIR/db"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
110 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
111 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
112 | |
111 | our %RESOURCE; |
113 | our %RESOURCE; # unused |
|
|
114 | |
|
|
115 | our $OUTPUT_RATE_MIN = 3000; |
|
|
116 | our $OUTPUT_RATE_MAX = 1000000; |
|
|
117 | |
|
|
118 | our $MAX_LINKS = 32; # how many chained exits to follow |
|
|
119 | our $VERBOSE_IO = 1; |
112 | |
120 | |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
121 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
122 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
115 | our $NEXT_TICK; |
123 | our $NEXT_TICK; |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
124 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
… | |
… | |
118 | our $BDB_DEADLOCK_WATCHER; |
126 | our $BDB_DEADLOCK_WATCHER; |
119 | our $BDB_CHECKPOINT_WATCHER; |
127 | our $BDB_CHECKPOINT_WATCHER; |
120 | our $BDB_TRICKLE_WATCHER; |
128 | our $BDB_TRICKLE_WATCHER; |
121 | our $DB_ENV; |
129 | our $DB_ENV; |
122 | |
130 | |
123 | our @EXTRA_MODULES = qw(pod match mapscript); |
131 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
124 | |
132 | |
125 | our %CFG; |
133 | our %CFG; |
126 | |
134 | |
127 | our $UPTIME; $UPTIME ||= time; |
135 | our $UPTIME; $UPTIME ||= time; |
128 | our $RUNTIME; |
136 | our $RUNTIME; |
… | |
… | |
162 | |
170 | |
163 | our $EMERGENCY_POSITION; |
171 | our $EMERGENCY_POSITION; |
164 | |
172 | |
165 | sub cf::map::normalise; |
173 | sub cf::map::normalise; |
166 | |
174 | |
|
|
175 | sub in_main() { |
|
|
176 | $Coro::current == $Coro::main |
|
|
177 | } |
|
|
178 | |
167 | ############################################################################# |
179 | ############################################################################# |
168 | |
180 | |
169 | %REFLECT = (); |
181 | %REFLECT = (); |
170 | for (@REFLECT) { |
182 | for (@REFLECT) { |
171 | my $reflect = JSON::XS::decode_json $_; |
183 | my $reflect = JSON::XS::decode_json $_; |
… | |
… | |
223 | from wherever your confdir points to. |
235 | from wherever your confdir points to. |
224 | |
236 | |
225 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
237 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
226 | |
238 | |
227 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
239 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
228 | returns directly I<after> the tick processing (and consequently, can only wake one process |
240 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
229 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
241 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
242 | |
|
|
243 | =cut |
|
|
244 | |
|
|
245 | sub wait_for_tick(); |
|
|
246 | sub wait_for_tick_begin(); |
230 | |
247 | |
231 | =item @cf::INVOKE_RESULTS |
248 | =item @cf::INVOKE_RESULTS |
232 | |
249 | |
233 | This array contains the results of the last C<invoke ()> call. When |
250 | This array contains the results of the last C<invoke ()> call. When |
234 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
251 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
… | |
… | |
242 | |
259 | |
243 | =back |
260 | =back |
244 | |
261 | |
245 | =cut |
262 | =cut |
246 | |
263 | |
|
|
264 | sub error(@) { LOG llevError, join "", @_ } |
|
|
265 | sub warn (@) { LOG llevWarn , join "", @_ } |
|
|
266 | sub info (@) { LOG llevInfo , join "", @_ } |
|
|
267 | sub debug(@) { LOG llevDebug, join "", @_ } |
|
|
268 | sub trace(@) { LOG llevTrace, join "", @_ } |
|
|
269 | |
247 | $Coro::State::WARNHOOK = sub { |
270 | $Coro::State::WARNHOOK = sub { |
248 | my $msg = join "", @_; |
271 | my $msg = join "", @_; |
249 | |
272 | |
250 | $msg .= "\n" |
273 | $msg .= "\n" |
251 | unless $msg =~ /\n$/; |
274 | unless $msg =~ /\n$/; |
252 | |
275 | |
253 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
276 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
254 | |
277 | |
255 | LOG llevError, $msg; |
278 | LOG llevWarn, $msg; |
256 | }; |
279 | }; |
257 | |
280 | |
258 | $Coro::State::DIEHOOK = sub { |
281 | $Coro::State::DIEHOOK = sub { |
259 | return unless $^S eq 0; # "eq", not "==" |
282 | return unless $^S eq 0; # "eq", not "==" |
260 | |
283 | |
261 | warn Carp::longmess $_[0]; |
284 | error Carp::longmess $_[0]; |
262 | |
285 | |
263 | if ($Coro::current == $Coro::main) {#d# |
286 | if (in_main) {#d# |
264 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
287 | error "DIEHOOK called in main context, Coro bug?\n";#d# |
265 | return;#d# |
288 | return;#d# |
266 | }#d# |
289 | }#d# |
267 | |
290 | |
268 | # kill coroutine otherwise |
291 | # kill coroutine otherwise |
269 | Coro::terminate |
292 | Coro::terminate |
… | |
… | |
336 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
359 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
337 | |
360 | |
338 | sub encode_json($) { $json_coder->encode ($_[0]) } |
361 | sub encode_json($) { $json_coder->encode ($_[0]) } |
339 | sub decode_json($) { $json_coder->decode ($_[0]) } |
362 | sub decode_json($) { $json_coder->decode ($_[0]) } |
340 | |
363 | |
|
|
364 | =item $ref = cf::yaml_load $scalar |
|
|
365 | |
|
|
366 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
367 | |
|
|
368 | =cut |
|
|
369 | |
|
|
370 | sub fork_call(&@); |
|
|
371 | |
|
|
372 | sub yaml_load($) { |
|
|
373 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
374 | } |
|
|
375 | |
341 | =item cf::post_init { BLOCK } |
376 | =item cf::post_init { BLOCK } |
342 | |
377 | |
343 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
378 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
344 | but I<before> the server starts ticking again. |
379 | but I<before> the server starts ticking again. |
345 | |
380 | |
346 | The cdoeblock will have a single boolean argument to indicate whether this |
381 | The codeblock will have a single boolean argument to indicate whether this |
347 | is a reload or not. |
382 | is a reload or not. |
348 | |
383 | |
349 | =cut |
384 | =cut |
350 | |
385 | |
351 | sub post_init(&) { |
386 | sub post_init(&) { |
352 | push @POST_INIT, shift; |
387 | push @POST_INIT, shift; |
|
|
388 | } |
|
|
389 | |
|
|
390 | sub _post_init { |
|
|
391 | trace "running post_init jobs"; |
|
|
392 | |
|
|
393 | # run them in parallel... |
|
|
394 | |
|
|
395 | my @join; |
|
|
396 | |
|
|
397 | while () { |
|
|
398 | push @join, map &Coro::async ($_, 0), @POST_INIT; |
|
|
399 | @POST_INIT = (); |
|
|
400 | |
|
|
401 | @join or last; |
|
|
402 | |
|
|
403 | (pop @join)->join; |
|
|
404 | } |
353 | } |
405 | } |
354 | |
406 | |
355 | =item cf::lock_wait $string |
407 | =item cf::lock_wait $string |
356 | |
408 | |
357 | Wait until the given lock is available. See cf::lock_acquire. |
409 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
396 | } |
448 | } |
397 | |
449 | |
398 | =item cf::periodic $interval, $cb |
450 | =item cf::periodic $interval, $cb |
399 | |
451 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
452 | Like EV::periodic, but randomly selects a starting point so that the actions |
401 | get spread over timer. |
453 | get spread over time. |
402 | |
454 | |
403 | =cut |
455 | =cut |
404 | |
456 | |
405 | sub periodic($$) { |
457 | sub periodic($$) { |
406 | my ($interval, $cb) = @_; |
458 | my ($interval, $cb) = @_; |
… | |
… | |
502 | =cut |
554 | =cut |
503 | |
555 | |
504 | sub sync_job(&) { |
556 | sub sync_job(&) { |
505 | my ($job) = @_; |
557 | my ($job) = @_; |
506 | |
558 | |
507 | if ($Coro::current == $Coro::main) { |
559 | if (in_main) { |
508 | my $time = EV::time; |
560 | my $time = AE::time; |
509 | |
561 | |
510 | # this is the main coro, too bad, we have to block |
562 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
563 | # till the operation succeeds, freezing the server :/ |
512 | |
564 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
565 | #LOG llevError, Carp::longmess "sync job";#d# |
514 | |
566 | |
515 | my $freeze_guard = freeze_mainloop; |
567 | my $freeze_guard = freeze_mainloop; |
516 | |
568 | |
517 | my $busy = 1; |
569 | my $busy = 1; |
518 | my @res; |
570 | my @res; |
519 | |
571 | |
520 | (async { |
572 | (async { |
521 | $Coro::current->desc ("sync job coro"); |
573 | $Coro::current->desc ("sync job coro"); |
522 | @res = eval { $job->() }; |
574 | @res = eval { $job->() }; |
523 | warn $@ if $@; |
575 | error $@ if $@; |
524 | undef $busy; |
576 | undef $busy; |
525 | })->prio (Coro::PRIO_MAX); |
577 | })->prio (Coro::PRIO_MAX); |
526 | |
578 | |
527 | while ($busy) { |
579 | while ($busy) { |
528 | if (Coro::nready) { |
580 | if (Coro::nready) { |
… | |
… | |
530 | } else { |
582 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
583 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
584 | } |
533 | } |
585 | } |
534 | |
586 | |
535 | my $time = EV::time - $time; |
587 | my $time = AE::time - $time; |
536 | |
588 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
589 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
590 | |
539 | wantarray ? @res : $res[0] |
591 | wantarray ? @res : $res[0] |
540 | } else { |
592 | } else { |
… | |
… | |
562 | $EXT_CORO{$coro+0} = $coro; |
614 | $EXT_CORO{$coro+0} = $coro; |
563 | |
615 | |
564 | $coro |
616 | $coro |
565 | } |
617 | } |
566 | |
618 | |
567 | =item fork_call { }, $args |
619 | =item fork_call { }, @args |
568 | |
620 | |
569 | Executes the given code block with the given arguments in a seperate |
621 | Executes the given code block with the given arguments in a seperate |
570 | process, returning the results. Everything must be serialisable with |
622 | process, returning the results. Everything must be serialisable with |
571 | Coro::Storable. May, of course, block. Note that the executed sub may |
623 | Coro::Storable. May, of course, block. Note that the executed sub may |
572 | never block itself or use any form of event handling. |
624 | never block itself or use any form of event handling. |
573 | |
625 | |
574 | =cut |
626 | =cut |
575 | |
627 | |
|
|
628 | sub post_fork { |
|
|
629 | reset_signals; |
|
|
630 | } |
|
|
631 | |
576 | sub fork_call(&@) { |
632 | sub fork_call(&@) { |
577 | my ($cb, @args) = @_; |
633 | my ($cb, @args) = @_; |
578 | |
634 | |
579 | # we seemingly have to make a local copy of the whole thing, |
635 | # we seemingly have to make a local copy of the whole thing, |
580 | # otherwise perl prematurely frees the stuff :/ |
636 | # otherwise perl prematurely frees the stuff :/ |
581 | # TODO: investigate and fix (likely this will be rather laborious) |
637 | # TODO: investigate and fix (likely this will be rather laborious) |
582 | |
638 | |
583 | my @res = Coro::Util::fork_eval { |
639 | my @res = Coro::Util::fork_eval { |
584 | reset_signals; |
640 | cf::post_fork; |
585 | &$cb |
641 | &$cb |
586 | }, @args; |
642 | } @args; |
587 | |
643 | |
588 | wantarray ? @res : $res[-1] |
644 | wantarray ? @res : $res[-1] |
|
|
645 | } |
|
|
646 | |
|
|
647 | sub objinfo { |
|
|
648 | ( |
|
|
649 | "counter value" => cf::object::object_count, |
|
|
650 | "objects created" => cf::object::create_count, |
|
|
651 | "objects destroyed" => cf::object::destroy_count, |
|
|
652 | "freelist size" => cf::object::free_count, |
|
|
653 | "allocated objects" => cf::object::objects_size, |
|
|
654 | "active objects" => cf::object::actives_size, |
|
|
655 | ) |
589 | } |
656 | } |
590 | |
657 | |
591 | =item $coin = coin_from_name $name |
658 | =item $coin = coin_from_name $name |
592 | |
659 | |
593 | =cut |
660 | =cut |
… | |
… | |
630 | within each server. |
697 | within each server. |
631 | |
698 | |
632 | =cut |
699 | =cut |
633 | |
700 | |
634 | sub db_table($) { |
701 | sub db_table($) { |
|
|
702 | cf::error "db_get called from main context" |
|
|
703 | if $Coro::current == $Coro::main; |
|
|
704 | |
635 | my ($name) = @_; |
705 | my ($name) = @_; |
636 | my $db = BDB::db_create $DB_ENV; |
706 | my $db = BDB::db_create $DB_ENV; |
637 | |
707 | |
638 | eval { |
708 | eval { |
639 | $db->set_flags (BDB::CHKSUM); |
709 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
649 | } |
719 | } |
650 | |
720 | |
651 | our $DB; |
721 | our $DB; |
652 | |
722 | |
653 | sub db_init { |
723 | sub db_init { |
654 | cf::sync_job { |
|
|
655 | $DB ||= db_table "db"; |
724 | $DB ||= db_table "db"; |
656 | }; |
|
|
657 | } |
725 | } |
658 | |
726 | |
659 | sub db_get($$) { |
727 | sub db_get($$) { |
660 | my $key = "$_[0]/$_[1]"; |
728 | my $key = "$_[0]/$_[1]"; |
661 | |
729 | |
662 | cf::sync_job { |
730 | cf::error "db_get called from main context" |
|
|
731 | if $Coro::current == $Coro::main; |
|
|
732 | |
663 | BDB::db_get $DB, undef, $key, my $data; |
733 | BDB::db_get $DB, undef, $key, my $data; |
664 | |
734 | |
665 | $! ? () |
735 | $! ? () |
666 | : $data |
736 | : $data |
667 | } |
|
|
668 | } |
737 | } |
669 | |
738 | |
670 | sub db_put($$$) { |
739 | sub db_put($$$) { |
671 | BDB::dbreq_pri 4; |
740 | BDB::dbreq_pri 4; |
672 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
741 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
728 | |
797 | |
729 | my $t1 = Time::HiRes::time; |
798 | my $t1 = Time::HiRes::time; |
730 | my $data = $process->(\@data); |
799 | my $data = $process->(\@data); |
731 | my $t2 = Time::HiRes::time; |
800 | my $t2 = Time::HiRes::time; |
732 | |
801 | |
733 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
802 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
734 | |
803 | |
735 | db_put cache => "$id/data", $data; |
804 | db_put cache => "$id/data", $data; |
736 | db_put cache => "$id/md5" , $md5; |
805 | db_put cache => "$id/md5" , $md5; |
737 | db_put cache => "$id/meta", $meta; |
806 | db_put cache => "$id/meta", $meta; |
738 | |
807 | |
… | |
… | |
748 | |
817 | |
749 | =cut |
818 | =cut |
750 | |
819 | |
751 | sub datalog($@) { |
820 | sub datalog($@) { |
752 | my ($type, %kv) = @_; |
821 | my ($type, %kv) = @_; |
753 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
822 | info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
754 | } |
823 | } |
755 | |
824 | |
756 | =back |
825 | =back |
757 | |
826 | |
758 | =cut |
827 | =cut |
… | |
… | |
953 | |
1022 | |
954 | } elsif (exists $cb_id{$type}) { |
1023 | } elsif (exists $cb_id{$type}) { |
955 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
1024 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
956 | |
1025 | |
957 | } elsif (ref $type) { |
1026 | } elsif (ref $type) { |
958 | warn "attaching objects not supported, ignoring.\n"; |
1027 | error "attaching objects not supported, ignoring.\n"; |
959 | |
1028 | |
960 | } else { |
1029 | } else { |
961 | shift @arg; |
1030 | shift @arg; |
962 | warn "attach argument '$type' not supported, ignoring.\n"; |
1031 | error "attach argument '$type' not supported, ignoring.\n"; |
963 | } |
1032 | } |
964 | } |
1033 | } |
965 | } |
1034 | } |
966 | |
1035 | |
967 | sub _object_attach { |
1036 | sub _object_attach { |
… | |
… | |
977 | _attach $registry, $klass, @attach; |
1046 | _attach $registry, $klass, @attach; |
978 | } |
1047 | } |
979 | |
1048 | |
980 | $obj->{$name} = \%arg; |
1049 | $obj->{$name} = \%arg; |
981 | } else { |
1050 | } else { |
982 | warn "object uses attachment '$name' which is not available, postponing.\n"; |
1051 | info "object uses attachment '$name' which is not available, postponing.\n"; |
983 | } |
1052 | } |
984 | |
1053 | |
985 | $obj->{_attachment}{$name} = undef; |
1054 | $obj->{_attachment}{$name} = undef; |
986 | } |
1055 | } |
987 | |
1056 | |
… | |
… | |
1046 | |
1115 | |
1047 | for (@$callbacks) { |
1116 | for (@$callbacks) { |
1048 | eval { &{$_->[1]} }; |
1117 | eval { &{$_->[1]} }; |
1049 | |
1118 | |
1050 | if ($@) { |
1119 | if ($@) { |
1051 | warn "$@"; |
|
|
1052 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1120 | error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1053 | override; |
1121 | override; |
1054 | } |
1122 | } |
1055 | |
1123 | |
1056 | return 1 if $override; |
1124 | return 1 if $override; |
1057 | } |
1125 | } |
… | |
… | |
1136 | for (@$attach) { |
1204 | for (@$attach) { |
1137 | my ($klass, @attach) = @$_; |
1205 | my ($klass, @attach) = @$_; |
1138 | _attach $registry, $klass, @attach; |
1206 | _attach $registry, $klass, @attach; |
1139 | } |
1207 | } |
1140 | } else { |
1208 | } else { |
1141 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
1209 | info "object uses attachment '$name' that is not available, postponing.\n"; |
1142 | } |
1210 | } |
1143 | } |
1211 | } |
1144 | } |
1212 | } |
1145 | |
1213 | |
1146 | cf::attachable->attach ( |
1214 | cf::attachable->attach ( |
… | |
… | |
1173 | my ($filename, $rdata, $objs) = @_; |
1241 | my ($filename, $rdata, $objs) = @_; |
1174 | |
1242 | |
1175 | sync_job { |
1243 | sync_job { |
1176 | if (length $$rdata) { |
1244 | if (length $$rdata) { |
1177 | utf8::decode (my $decname = $filename); |
1245 | utf8::decode (my $decname = $filename); |
1178 | warn sprintf "saving %s (%d,%d)\n", |
1246 | trace sprintf "saving %s (%d,%d)\n", |
1179 | $decname, length $$rdata, scalar @$objs; |
1247 | $decname, length $$rdata, scalar @$objs |
|
|
1248 | if $VERBOSE_IO; |
1180 | |
1249 | |
1181 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1250 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1182 | aio_chmod $fh, SAVE_MODE; |
1251 | aio_chmod $fh, SAVE_MODE; |
1183 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1252 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1184 | if ($cf::USE_FSYNC) { |
1253 | if ($cf::USE_FSYNC) { |
… | |
… | |
1206 | aio_rename "$filename~", $filename; |
1275 | aio_rename "$filename~", $filename; |
1207 | |
1276 | |
1208 | $filename =~ s%/[^/]+$%%; |
1277 | $filename =~ s%/[^/]+$%%; |
1209 | aio_pathsync $filename if $cf::USE_FSYNC; |
1278 | aio_pathsync $filename if $cf::USE_FSYNC; |
1210 | } else { |
1279 | } else { |
1211 | warn "unable to save objects: $filename~: $!\n"; |
1280 | error "unable to save objects: $filename~: $!\n"; |
1212 | } |
1281 | } |
1213 | } else { |
1282 | } else { |
1214 | aio_unlink $filename; |
1283 | aio_unlink $filename; |
1215 | aio_unlink "$filename.pst"; |
1284 | aio_unlink "$filename.pst"; |
1216 | } |
1285 | } |
… | |
… | |
1240 | my $st = eval { Coro::Storable::thaw $av }; |
1309 | my $st = eval { Coro::Storable::thaw $av }; |
1241 | $av = $st->{objs}; |
1310 | $av = $st->{objs}; |
1242 | } |
1311 | } |
1243 | |
1312 | |
1244 | utf8::decode (my $decname = $filename); |
1313 | utf8::decode (my $decname = $filename); |
1245 | warn sprintf "loading %s (%d,%d)\n", |
1314 | trace sprintf "loading %s (%d,%d)\n", |
1246 | $decname, length $data, scalar @{$av || []}; |
1315 | $decname, length $data, scalar @{$av || []} |
|
|
1316 | if $VERBOSE_IO; |
1247 | |
1317 | |
1248 | ($data, $av) |
1318 | ($data, $av) |
1249 | } |
1319 | } |
1250 | |
1320 | |
1251 | =head2 COMMAND CALLBACKS |
1321 | =head2 COMMAND CALLBACKS |
… | |
… | |
1310 | } |
1380 | } |
1311 | |
1381 | |
1312 | use File::Glob (); |
1382 | use File::Glob (); |
1313 | |
1383 | |
1314 | cf::player->attach ( |
1384 | cf::player->attach ( |
1315 | on_command => sub { |
1385 | on_unknown_command => sub { |
1316 | my ($pl, $name, $params) = @_; |
1386 | my ($pl, $name, $params) = @_; |
1317 | |
1387 | |
1318 | my $cb = $COMMAND{$name} |
1388 | my $cb = $COMMAND{$name} |
1319 | or return; |
1389 | or return; |
1320 | |
1390 | |
… | |
… | |
1343 | |
1413 | |
1344 | $pl->ext_reply ($reply, @reply) |
1414 | $pl->ext_reply ($reply, @reply) |
1345 | if $reply; |
1415 | if $reply; |
1346 | |
1416 | |
1347 | } else { |
1417 | } else { |
1348 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1418 | error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1349 | } |
1419 | } |
1350 | |
1420 | |
1351 | cf::override; |
1421 | cf::override; |
1352 | }, |
1422 | }, |
1353 | ); |
1423 | ); |
… | |
… | |
1364 | |
1434 | |
1365 | $grp |
1435 | $grp |
1366 | } |
1436 | } |
1367 | |
1437 | |
1368 | sub load_extensions { |
1438 | sub load_extensions { |
|
|
1439 | info "loading extensions..."; |
|
|
1440 | |
1369 | cf::sync_job { |
1441 | cf::sync_job { |
1370 | my %todo; |
1442 | my %todo; |
1371 | |
1443 | |
1372 | for my $path (<$LIBDIR/*.ext>) { |
1444 | for my $path (<$LIBDIR/*.ext>) { |
1373 | next unless -r $path; |
1445 | next unless -r $path; |
… | |
… | |
1391 | |
1463 | |
1392 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1464 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1393 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1465 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1394 | |
1466 | |
1395 | $ext{source} = |
1467 | $ext{source} = |
1396 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1468 | "package $pkg; use common::sense;\n" |
1397 | . "#line 1 \"$path\"\n{\n" |
1469 | . "#line 1 \"$path\"\n{\n" |
1398 | . $source |
1470 | . $source |
1399 | . "\n};\n1"; |
1471 | . "\n};\n1"; |
1400 | |
1472 | |
1401 | $todo{$base} = \%ext; |
1473 | $todo{$base} = \%ext; |
1402 | } |
1474 | } |
1403 | |
1475 | |
|
|
1476 | my $pass = 0; |
1404 | my %done; |
1477 | my %done; |
1405 | while (%todo) { |
1478 | while (%todo) { |
1406 | my $progress; |
1479 | my $progress; |
1407 | |
1480 | |
|
|
1481 | ++$pass; |
|
|
1482 | |
|
|
1483 | ext: |
1408 | while (my ($k, $v) = each %todo) { |
1484 | while (my ($k, $v) = each %todo) { |
1409 | for (split /,\s*/, $v->{meta}{depends}) { |
1485 | for (split /,\s*/, $v->{meta}{depends}) { |
1410 | goto skip |
1486 | next ext |
1411 | unless exists $done{$_}; |
1487 | unless exists $done{$_}; |
1412 | } |
1488 | } |
1413 | |
1489 | |
1414 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1490 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1415 | |
1491 | |
1416 | unless (eval $v->{source}) { |
1492 | my $active = eval $v->{source}; |
|
|
1493 | |
|
|
1494 | if (length $@) { |
1417 | my $msg = $@ ? "$v->{path}: $@\n" |
1495 | error "$v->{path}: $@\n"; |
1418 | : "$v->{base}: extension inactive.\n"; |
|
|
1419 | |
1496 | |
1420 | if (exists $v->{meta}{mandatory}) { |
|
|
1421 | warn $msg; |
|
|
1422 | cf::cleanup "mandatory extension failed to load, exiting."; |
1497 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1423 | } |
1498 | if exists $v->{meta}{mandatory}; |
1424 | |
1499 | |
1425 | warn $msg; |
1500 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
|
|
1501 | delete $todo{$k}; |
|
|
1502 | } else { |
|
|
1503 | $done{$k} = delete $todo{$k}; |
|
|
1504 | push @EXTS, $v->{pkg}; |
|
|
1505 | $progress = 1; |
|
|
1506 | |
|
|
1507 | info "$v->{base}: extension inactive.\n" |
|
|
1508 | unless $active; |
1426 | } |
1509 | } |
1427 | |
|
|
1428 | $done{$k} = delete $todo{$k}; |
|
|
1429 | push @EXTS, $v->{pkg}; |
|
|
1430 | $progress = 1; |
|
|
1431 | } |
1510 | } |
1432 | |
1511 | |
1433 | skip: |
1512 | unless ($progress) { |
1434 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1513 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1435 | unless $progress; |
1514 | |
|
|
1515 | while (my ($k, $v) = each %todo) { |
|
|
1516 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1517 | if exists $v->{meta}{mandatory}; |
|
|
1518 | } |
|
|
1519 | } |
1436 | } |
1520 | } |
1437 | }; |
1521 | }; |
1438 | } |
1522 | } |
1439 | |
1523 | |
1440 | ############################################################################# |
1524 | ############################################################################# |
… | |
… | |
1524 | $cf::PLAYER{$login} = $pl |
1608 | $cf::PLAYER{$login} = $pl |
1525 | } |
1609 | } |
1526 | } |
1610 | } |
1527 | } |
1611 | } |
1528 | |
1612 | |
|
|
1613 | cf::player->attach ( |
|
|
1614 | on_load => sub { |
|
|
1615 | my ($pl, $path) = @_; |
|
|
1616 | |
|
|
1617 | # restore slots saved in save, below |
|
|
1618 | my $slots = delete $pl->{_slots}; |
|
|
1619 | |
|
|
1620 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1621 | $pl->combat_ob ($slots->[1]); |
|
|
1622 | $pl->ranged_ob ($slots->[2]); |
|
|
1623 | }, |
|
|
1624 | ); |
|
|
1625 | |
1529 | sub save($) { |
1626 | sub save($) { |
1530 | my ($pl) = @_; |
1627 | my ($pl) = @_; |
1531 | |
1628 | |
1532 | return if $pl->{deny_save}; |
1629 | return if $pl->{deny_save}; |
1533 | |
1630 | |
… | |
… | |
1538 | |
1635 | |
1539 | aio_mkdir playerdir $pl, 0770; |
1636 | aio_mkdir playerdir $pl, 0770; |
1540 | $pl->{last_save} = $cf::RUNTIME; |
1637 | $pl->{last_save} = $cf::RUNTIME; |
1541 | |
1638 | |
1542 | cf::get_slot 0.01; |
1639 | cf::get_slot 0.01; |
|
|
1640 | |
|
|
1641 | # save slots, to be restored later |
|
|
1642 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1543 | |
1643 | |
1544 | $pl->save_pl ($path); |
1644 | $pl->save_pl ($path); |
1545 | cf::cede_to_tick; |
1645 | cf::cede_to_tick; |
1546 | } |
1646 | } |
1547 | |
1647 | |
… | |
… | |
1581 | my $name = $pl->ob->name; |
1681 | my $name = $pl->ob->name; |
1582 | |
1682 | |
1583 | $pl->{deny_save} = 1; |
1683 | $pl->{deny_save} = 1; |
1584 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1684 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1585 | |
1685 | |
1586 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1686 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1587 | $pl->deactivate; |
1687 | $pl->deactivate; |
|
|
1688 | |
1588 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1689 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1589 | $pl->ob->check_score; |
|
|
1590 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1690 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1691 | ext::highscore::check ($pl->ob); |
|
|
1692 | |
1591 | $pl->ns->destroy if $pl->ns; |
1693 | $pl->ns->destroy if $pl->ns; |
1592 | |
1694 | |
1593 | my $path = playerdir $pl; |
1695 | my $path = playerdir $pl; |
1594 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1696 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1595 | aio_rename $path, $temp; |
1697 | aio_rename $path, $temp; |
… | |
… | |
1649 | \@logins |
1751 | \@logins |
1650 | } |
1752 | } |
1651 | |
1753 | |
1652 | =item $player->maps |
1754 | =item $player->maps |
1653 | |
1755 | |
|
|
1756 | =item cf::player::maps $login |
|
|
1757 | |
1654 | Returns an arrayref of map paths that are private for this |
1758 | Returns an arrayref of map paths that are private for this |
1655 | player. May block. |
1759 | player. May block. |
1656 | |
1760 | |
1657 | =cut |
1761 | =cut |
1658 | |
1762 | |
… | |
… | |
1720 | =cut |
1824 | =cut |
1721 | |
1825 | |
1722 | sub find_by_path($) { |
1826 | sub find_by_path($) { |
1723 | my ($path) = @_; |
1827 | my ($path) = @_; |
1724 | |
1828 | |
|
|
1829 | $path =~ s/^~[^\/]*//; # skip ~login |
|
|
1830 | |
1725 | my ($match, $specificity); |
1831 | my ($match, $specificity); |
1726 | |
1832 | |
1727 | for my $region (list) { |
1833 | for my $region (list) { |
1728 | if ($region->{match} && $path =~ $region->{match}) { |
1834 | if ($region->{match} && $path =~ $region->{match}) { |
1729 | ($match, $specificity) = ($region, $region->specificity) |
1835 | ($match, $specificity) = ($region, $region->specificity) |
… | |
… | |
1757 | sub generate_random_map { |
1863 | sub generate_random_map { |
1758 | my ($self, $rmp) = @_; |
1864 | my ($self, $rmp) = @_; |
1759 | |
1865 | |
1760 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1866 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1761 | |
1867 | |
1762 | # mit "rum" bekleckern, nicht |
|
|
1763 | $self->_create_random_map ( |
1868 | $self->_create_random_map ($rmp); |
1764 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
|
|
1765 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
|
|
1766 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
|
|
1767 | $rmp->{exit_on_final_map}, |
|
|
1768 | $rmp->{xsize}, $rmp->{ysize}, |
|
|
1769 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
|
|
1770 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
|
|
1771 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
|
|
1772 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
|
|
1773 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
|
|
1774 | (cf::region::find $rmp->{region}), $rmp->{custom} |
|
|
1775 | ) |
|
|
1776 | } |
1869 | } |
1777 | |
1870 | |
1778 | =item cf::map->register ($regex, $prio) |
1871 | =item cf::map->register ($regex, $prio) |
1779 | |
1872 | |
1780 | Register a handler for the map path matching the given regex at the |
1873 | Register a handler for the map path matching the given regex at the |
… | |
… | |
1785 | |
1878 | |
1786 | sub register { |
1879 | sub register { |
1787 | my (undef, $regex, $prio) = @_; |
1880 | my (undef, $regex, $prio) = @_; |
1788 | my $pkg = caller; |
1881 | my $pkg = caller; |
1789 | |
1882 | |
1790 | no strict; |
|
|
1791 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1883 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1792 | |
1884 | |
1793 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1885 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1794 | } |
1886 | } |
1795 | |
1887 | |
1796 | # also paths starting with '/' |
1888 | # also paths starting with '/' |
1797 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1889 | $EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}]; |
1798 | |
1890 | |
1799 | sub thawer_merge { |
1891 | sub thawer_merge { |
1800 | my ($self, $merge) = @_; |
1892 | my ($self, $merge) = @_; |
1801 | |
1893 | |
1802 | # we have to keep some variables in memory intact |
1894 | # we have to keep some variables in memory intact |
… | |
… | |
1807 | } |
1899 | } |
1808 | |
1900 | |
1809 | sub normalise { |
1901 | sub normalise { |
1810 | my ($path, $base) = @_; |
1902 | my ($path, $base) = @_; |
1811 | |
1903 | |
1812 | $path = "$path"; # make sure its a string |
1904 | $path = "$path"; # make sure it's a string |
1813 | |
1905 | |
1814 | $path =~ s/\.map$//; |
1906 | $path =~ s/\.map$//; |
1815 | |
1907 | |
1816 | # map plan: |
1908 | # map plan: |
1817 | # |
1909 | # |
… | |
… | |
1832 | $base =~ s{[^/]+/?$}{}; |
1924 | $base =~ s{[^/]+/?$}{}; |
1833 | $path = "$base/$path"; |
1925 | $path = "$base/$path"; |
1834 | } |
1926 | } |
1835 | |
1927 | |
1836 | for ($path) { |
1928 | for ($path) { |
1837 | redo if s{//}{/}; |
|
|
1838 | redo if s{/\.?/}{/}; |
1929 | redo if s{/\.?/}{/}; |
1839 | redo if s{/[^/]+/\.\./}{/}; |
1930 | redo if s{/[^/]+/\.\./}{/}; |
1840 | } |
1931 | } |
1841 | |
1932 | |
1842 | $path |
1933 | $path |
… | |
… | |
1856 | $self->init; # pass $1 etc. |
1947 | $self->init; # pass $1 etc. |
1857 | return $self; |
1948 | return $self; |
1858 | } |
1949 | } |
1859 | } |
1950 | } |
1860 | |
1951 | |
1861 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1952 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1862 | () |
1953 | () |
1863 | } |
1954 | } |
1864 | |
1955 | |
1865 | sub init { |
1956 | sub init { |
1866 | my ($self) = @_; |
1957 | my ($self) = @_; |
… | |
… | |
1988 | } |
2079 | } |
1989 | |
2080 | |
1990 | sub find; |
2081 | sub find; |
1991 | sub find { |
2082 | sub find { |
1992 | my ($path, $origin) = @_; |
2083 | my ($path, $origin) = @_; |
|
|
2084 | |
|
|
2085 | cf::cede_to_tick; |
1993 | |
2086 | |
1994 | $path = normalise $path, $origin && $origin->path; |
2087 | $path = normalise $path, $origin && $origin->path; |
1995 | |
2088 | |
1996 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2089 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1997 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2090 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
2082 | |
2175 | |
2083 | $self->in_memory (cf::MAP_ACTIVE); |
2176 | $self->in_memory (cf::MAP_ACTIVE); |
2084 | } |
2177 | } |
2085 | |
2178 | |
2086 | $self->post_load; |
2179 | $self->post_load; |
2087 | } |
|
|
2088 | |
2180 | |
|
|
2181 | 1 |
|
|
2182 | } |
|
|
2183 | |
|
|
2184 | # customize the map for a given player, i.e. |
|
|
2185 | # return the _real_ map. used by e.g. per-player |
|
|
2186 | # maps to change the path to ~playername/mappath |
2089 | sub customise_for { |
2187 | sub customise_for { |
2090 | my ($self, $ob) = @_; |
2188 | my ($self, $ob) = @_; |
2091 | |
2189 | |
2092 | return find "~" . $ob->name . "/" . $self->{path} |
2190 | return find "~" . $ob->name . "/" . $self->{path} |
2093 | if $self->per_player; |
2191 | if $self->per_player; |
… | |
… | |
2109 | or next; |
2207 | or next; |
2110 | $neigh = find $neigh, $map |
2208 | $neigh = find $neigh, $map |
2111 | or next; |
2209 | or next; |
2112 | $neigh->load; |
2210 | $neigh->load; |
2113 | |
2211 | |
|
|
2212 | # now find the diagonal neighbours |
2114 | push @neigh, |
2213 | push @neigh, |
2115 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2214 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2116 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2215 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2117 | } |
2216 | } |
2118 | |
2217 | |
… | |
… | |
2125 | } |
2224 | } |
2126 | |
2225 | |
2127 | sub find_sync { |
2226 | sub find_sync { |
2128 | my ($path, $origin) = @_; |
2227 | my ($path, $origin) = @_; |
2129 | |
2228 | |
2130 | cf::sync_job { find $path, $origin } |
2229 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2230 | if $Coro::current == $Coro::main; |
|
|
2231 | |
|
|
2232 | find $path, $origin |
2131 | } |
2233 | } |
2132 | |
2234 | |
2133 | sub do_load_sync { |
2235 | sub do_load_sync { |
2134 | my ($map) = @_; |
2236 | my ($map) = @_; |
2135 | |
2237 | |
2136 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2238 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2137 | if $Coro::current == $Coro::main; |
2239 | if $Coro::current == $Coro::main; |
2138 | |
2240 | |
2139 | cf::sync_job { $map->load }; |
2241 | $map->load; |
2140 | } |
2242 | } |
2141 | |
2243 | |
2142 | our %MAP_PREFETCH; |
2244 | our %MAP_PREFETCH; |
2143 | our $MAP_PREFETCHER = undef; |
2245 | our $MAP_PREFETCHER = undef; |
2144 | |
2246 | |
… | |
… | |
2170 | $MAP_PREFETCHER->prio (6); |
2272 | $MAP_PREFETCHER->prio (6); |
2171 | |
2273 | |
2172 | () |
2274 | () |
2173 | } |
2275 | } |
2174 | |
2276 | |
|
|
2277 | # common code, used by both ->save and ->swapout |
2175 | sub save { |
2278 | sub _save { |
2176 | my ($self) = @_; |
2279 | my ($self) = @_; |
2177 | |
|
|
2178 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2179 | |
2280 | |
2180 | $self->{last_save} = $cf::RUNTIME; |
2281 | $self->{last_save} = $cf::RUNTIME; |
2181 | |
2282 | |
2182 | return unless $self->dirty; |
2283 | return unless $self->dirty; |
2183 | |
2284 | |
… | |
… | |
2203 | } else { |
2304 | } else { |
2204 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2305 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2205 | } |
2306 | } |
2206 | } |
2307 | } |
2207 | |
2308 | |
|
|
2309 | sub save { |
|
|
2310 | my ($self) = @_; |
|
|
2311 | |
|
|
2312 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2313 | |
|
|
2314 | $self->_save; |
|
|
2315 | } |
|
|
2316 | |
2208 | sub swap_out { |
2317 | sub swap_out { |
2209 | my ($self) = @_; |
2318 | my ($self) = @_; |
2210 | |
2319 | |
2211 | # save first because save cedes |
|
|
2212 | $self->save; |
|
|
2213 | |
|
|
2214 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2320 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2215 | |
2321 | |
2216 | return if $self->players; |
|
|
2217 | return if $self->in_memory != cf::MAP_ACTIVE; |
2322 | return if $self->in_memory != cf::MAP_ACTIVE; |
2218 | return if $self->{deny_save}; |
2323 | return if $self->{deny_save}; |
|
|
2324 | return if $self->players; |
2219 | |
2325 | |
|
|
2326 | # first deactivate the map and "unlink" it from the core |
|
|
2327 | $self->deactivate; |
|
|
2328 | $_->clear_links_to ($self) for values %cf::MAP; |
2220 | $self->in_memory (cf::MAP_SWAPPED); |
2329 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2330 | |
|
|
2331 | # then atomically save |
|
|
2332 | $self->_save; |
|
|
2333 | |
|
|
2334 | # then free the map |
|
|
2335 | $self->clear; |
|
|
2336 | } |
|
|
2337 | |
|
|
2338 | sub reset_at { |
|
|
2339 | my ($self) = @_; |
|
|
2340 | |
|
|
2341 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2342 | return 1e99 if $self->{deny_reset}; |
|
|
2343 | |
|
|
2344 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2345 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2346 | |
|
|
2347 | $time + $to |
|
|
2348 | } |
|
|
2349 | |
|
|
2350 | sub should_reset { |
|
|
2351 | my ($self) = @_; |
|
|
2352 | |
|
|
2353 | $self->reset_at <= $cf::RUNTIME |
|
|
2354 | } |
|
|
2355 | |
|
|
2356 | sub reset { |
|
|
2357 | my ($self) = @_; |
|
|
2358 | |
|
|
2359 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2360 | |
|
|
2361 | return if $self->players; |
|
|
2362 | |
|
|
2363 | cf::trace "resetting map ", $self->path, "\n"; |
|
|
2364 | |
|
|
2365 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2366 | |
|
|
2367 | # need to save uniques path |
|
|
2368 | unless ($self->{deny_save}) { |
|
|
2369 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2370 | |
|
|
2371 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2372 | if $uniq; |
|
|
2373 | } |
|
|
2374 | |
|
|
2375 | delete $cf::MAP{$self->path}; |
2221 | |
2376 | |
2222 | $self->deactivate; |
2377 | $self->deactivate; |
2223 | $_->clear_links_to ($self) for values %cf::MAP; |
2378 | $_->clear_links_to ($self) for values %cf::MAP; |
2224 | $self->clear; |
2379 | $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 | |
2380 | |
2270 | $self->unlink_save; |
2381 | $self->unlink_save; |
2271 | $self->destroy; |
2382 | $self->destroy; |
2272 | } |
2383 | } |
2273 | |
2384 | |
… | |
… | |
2281 | |
2392 | |
2282 | delete $cf::MAP{$self->path}; |
2393 | delete $cf::MAP{$self->path}; |
2283 | |
2394 | |
2284 | $self->unlink_save; |
2395 | $self->unlink_save; |
2285 | |
2396 | |
2286 | bless $self, "cf::map"; |
2397 | bless $self, "cf::map::wrap"; |
2287 | delete $self->{deny_reset}; |
2398 | delete $self->{deny_reset}; |
2288 | $self->{deny_save} = 1; |
2399 | $self->{deny_save} = 1; |
2289 | $self->reset_timeout (1); |
2400 | $self->reset_timeout (1); |
2290 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2401 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2291 | |
2402 | |
… | |
… | |
2521 | |
2632 | |
2522 | Freezes the player and moves him/her to a special map (C<{link}>). |
2633 | Freezes the player and moves him/her to a special map (C<{link}>). |
2523 | |
2634 | |
2524 | The player should be reasonably safe there for short amounts of time (e.g. |
2635 | 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, |
2636 | 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 |
2637 | though, as the player cannot control the character while it is on the link |
2527 | map. |
2638 | map. |
2528 | |
2639 | |
2529 | Will never block. |
2640 | Will never block. |
2530 | |
2641 | |
2531 | =item $player_object->leave_link ($map, $x, $y) |
2642 | =item $player_object->leave_link ($map, $x, $y) |
… | |
… | |
2552 | sub cf::object::player::enter_link { |
2663 | sub cf::object::player::enter_link { |
2553 | my ($self) = @_; |
2664 | my ($self) = @_; |
2554 | |
2665 | |
2555 | $self->deactivate_recursive; |
2666 | $self->deactivate_recursive; |
2556 | |
2667 | |
|
|
2668 | ++$self->{_link_recursion}; |
|
|
2669 | |
2557 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2670 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2558 | |
2671 | |
2559 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2672 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2560 | if $self->map && $self->map->{path} ne "{link}"; |
2673 | if $self->map && $self->map->{path} ne "{link}"; |
2561 | |
2674 | |
2562 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
2675 | $self->enter_map ($LINK_MAP || link_map, 3, 3); |
2563 | } |
2676 | } |
2564 | |
2677 | |
2565 | sub cf::object::player::leave_link { |
2678 | sub cf::object::player::leave_link { |
2566 | my ($self, $map, $x, $y) = @_; |
2679 | my ($self, $map, $x, $y) = @_; |
2567 | |
2680 | |
… | |
… | |
2592 | $map->load_neighbours; |
2705 | $map->load_neighbours; |
2593 | |
2706 | |
2594 | return unless $self->contr->active; |
2707 | return unless $self->contr->active; |
2595 | |
2708 | |
2596 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2709 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2597 | $self->enter_map ($map, $x, $y); |
2710 | if ($self->enter_map ($map, $x, $y)) { |
2598 | |
2711 | # entering was successful |
|
|
2712 | delete $self->{_link_recursion}; |
2599 | # only activate afterwards, to support waiting in hooks |
2713 | # only activate afterwards, to support waiting in hooks |
2600 | $self->activate_recursive; |
2714 | $self->activate_recursive; |
2601 | } |
2715 | } |
2602 | |
2716 | |
|
|
2717 | } |
|
|
2718 | |
2603 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2719 | =item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]]) |
2604 | |
2720 | |
2605 | Moves the player to the given map-path and coordinates by first freezing |
2721 | 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 |
2722 | 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 |
2723 | 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 |
2724 | the new (success) or old (failed) map position. In either case, $done will |
… | |
… | |
2615 | |
2731 | |
2616 | our $GOTOGEN; |
2732 | our $GOTOGEN; |
2617 | |
2733 | |
2618 | sub cf::object::player::goto { |
2734 | sub cf::object::player::goto { |
2619 | my ($self, $path, $x, $y, $check, $done) = @_; |
2735 | my ($self, $path, $x, $y, $check, $done) = @_; |
|
|
2736 | |
|
|
2737 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
|
|
2738 | error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
|
|
2739 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
|
|
2740 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
|
|
2741 | } |
2620 | |
2742 | |
2621 | # do generation counting so two concurrent goto's will be executed in-order |
2743 | # do generation counting so two concurrent goto's will be executed in-order |
2622 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2744 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2623 | |
2745 | |
2624 | $self->enter_link; |
2746 | $self->enter_link; |
… | |
… | |
2648 | my $map = eval { |
2770 | my $map = eval { |
2649 | my $map = defined $path ? cf::map::find $path : undef; |
2771 | my $map = defined $path ? cf::map::find $path : undef; |
2650 | |
2772 | |
2651 | if ($map) { |
2773 | if ($map) { |
2652 | $map = $map->customise_for ($self); |
2774 | $map = $map->customise_for ($self); |
2653 | $map = $check->($map) if $check && $map; |
2775 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2654 | } else { |
2776 | } else { |
2655 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2777 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2656 | } |
2778 | } |
2657 | |
2779 | |
2658 | $map |
2780 | $map |
… | |
… | |
2666 | if ($gen == $self->{_goto_generation}) { |
2788 | if ($gen == $self->{_goto_generation}) { |
2667 | delete $self->{_goto_generation}; |
2789 | delete $self->{_goto_generation}; |
2668 | $self->leave_link ($map, $x, $y); |
2790 | $self->leave_link ($map, $x, $y); |
2669 | } |
2791 | } |
2670 | |
2792 | |
2671 | $done->() if $done; |
2793 | $done->($self) if $done; |
2672 | })->prio (1); |
2794 | })->prio (1); |
2673 | } |
2795 | } |
2674 | |
2796 | |
2675 | =item $player_object->enter_exit ($exit_object) |
2797 | =item $player_object->enter_exit ($exit_object) |
2676 | |
2798 | |
… | |
… | |
2769 | $self->message ("Something went wrong deep within the deliantra server. " |
2891 | $self->message ("Something went wrong deep within the deliantra server. " |
2770 | . "I'll try to bring you back to the map you were before. " |
2892 | . "I'll try to bring you back to the map you were before. " |
2771 | . "Please report this to the dungeon master!", |
2893 | . "Please report this to the dungeon master!", |
2772 | cf::NDI_UNIQUE | cf::NDI_RED); |
2894 | cf::NDI_UNIQUE | cf::NDI_RED); |
2773 | |
2895 | |
2774 | warn "ERROR in enter_exit: $@"; |
2896 | error "ERROR in enter_exit: $@"; |
2775 | $self->leave_link; |
2897 | $self->leave_link; |
2776 | } |
2898 | } |
2777 | })->prio (1); |
2899 | })->prio (1); |
2778 | } |
2900 | } |
2779 | |
2901 | |
… | |
… | |
3092 | |
3214 | |
3093 | $ns->ext_reply ($reply, @reply) |
3215 | $ns->ext_reply ($reply, @reply) |
3094 | if $reply; |
3216 | if $reply; |
3095 | |
3217 | |
3096 | } else { |
3218 | } else { |
3097 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3219 | error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3098 | } |
3220 | } |
3099 | |
3221 | |
3100 | cf::override; |
3222 | cf::override; |
3101 | }, |
3223 | }, |
3102 | ); |
3224 | ); |
… | |
… | |
3122 | |
3244 | |
3123 | $coro |
3245 | $coro |
3124 | } |
3246 | } |
3125 | |
3247 | |
3126 | cf::client->attach ( |
3248 | cf::client->attach ( |
3127 | on_destroy => sub { |
3249 | on_client_destroy => sub { |
3128 | my ($ns) = @_; |
3250 | my ($ns) = @_; |
3129 | |
3251 | |
3130 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3252 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3131 | }, |
3253 | }, |
3132 | ); |
3254 | ); |
… | |
… | |
3183 | decrease split destroy change_exp value msg lore send_msg)], |
3305 | decrease split destroy change_exp value msg lore send_msg)], |
3184 | ["cf::object::player" => qw(player)], |
3306 | ["cf::object::player" => qw(player)], |
3185 | ["cf::player" => qw(peaceful send_msg)], |
3307 | ["cf::player" => qw(peaceful send_msg)], |
3186 | ["cf::map" => qw(trigger)], |
3308 | ["cf::map" => qw(trigger)], |
3187 | ) { |
3309 | ) { |
3188 | no strict 'refs'; |
|
|
3189 | my ($pkg, @funs) = @$_; |
3310 | my ($pkg, @funs) = @$_; |
3190 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3311 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3191 | for @funs; |
3312 | for @funs; |
3192 | } |
3313 | } |
3193 | |
3314 | |
… | |
… | |
3232 | local @cf::_safe_eval_args = values %vars; |
3353 | local @cf::_safe_eval_args = values %vars; |
3233 | @res = wantarray ? eval eval : scalar eval $eval; |
3354 | @res = wantarray ? eval eval : scalar eval $eval; |
3234 | } |
3355 | } |
3235 | |
3356 | |
3236 | if ($@) { |
3357 | if ($@) { |
3237 | warn "$@"; |
3358 | warn "$@", |
3238 | warn "while executing safe code '$code'\n"; |
3359 | "while executing safe code '$code'\n", |
3239 | warn "with arguments " . (join " ", %vars) . "\n"; |
3360 | "with arguments " . (join " ", %vars) . "\n"; |
3240 | } |
3361 | } |
3241 | |
3362 | |
3242 | wantarray ? @res : $res[0] |
3363 | wantarray ? @res : $res[0] |
3243 | } |
3364 | } |
3244 | |
3365 | |
… | |
… | |
3278 | # for this (global event?) |
3399 | # for this (global event?) |
3279 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3400 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3280 | |
3401 | |
3281 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3402 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3282 | |
3403 | |
3283 | warn "loading facedata from $path\n"; |
3404 | trace "loading facedata from $path\n"; |
3284 | |
3405 | |
3285 | my $facedata; |
|
|
3286 | 0 < aio_load $path, $facedata |
3406 | 0 < aio_load $path, my $facedata |
3287 | or die "$path: $!"; |
3407 | or die "$path: $!"; |
3288 | |
3408 | |
3289 | $facedata = Coro::Storable::thaw $facedata; |
3409 | $facedata = Coro::Storable::thaw $facedata; |
3290 | |
3410 | |
3291 | $facedata->{version} == 2 |
3411 | $facedata->{version} == 2 |
… | |
… | |
3308 | |
3428 | |
3309 | cf::face::set_visibility $idx, $info->{visibility}; |
3429 | cf::face::set_visibility $idx, $info->{visibility}; |
3310 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3430 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3311 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3431 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3312 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3432 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
|
|
3433 | cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; |
3313 | |
3434 | |
3314 | cf::cede_to_tick; |
3435 | cf::cede_to_tick; |
3315 | } |
3436 | } |
3316 | |
3437 | |
3317 | while (my ($face, $info) = each %$faces) { |
3438 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3322 | |
3443 | |
3323 | if (my $smooth = cf::face::find $info->{smooth}) { |
3444 | if (my $smooth = cf::face::find $info->{smooth}) { |
3324 | cf::face::set_smooth $idx, $smooth; |
3445 | cf::face::set_smooth $idx, $smooth; |
3325 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3446 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3326 | } else { |
3447 | } else { |
3327 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3448 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3328 | } |
3449 | } |
3329 | |
3450 | |
3330 | cf::cede_to_tick; |
3451 | cf::cede_to_tick; |
3331 | } |
3452 | } |
3332 | } |
3453 | } |
… | |
… | |
3350 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3471 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3351 | |
3472 | |
3352 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3473 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3353 | cf::face::set_type $idx, $info->{type}; |
3474 | cf::face::set_type $idx, $info->{type}; |
3354 | } else { |
3475 | } else { |
3355 | $RESOURCE{$name} = $info; |
3476 | $RESOURCE{$name} = $info; # unused |
3356 | } |
3477 | } |
3357 | |
3478 | |
3358 | cf::cede_to_tick; |
3479 | cf::cede_to_tick; |
3359 | } |
3480 | } |
3360 | } |
3481 | } |
3361 | |
3482 | |
3362 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3483 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3363 | |
3484 | |
3364 | 1 |
3485 | 1 |
3365 | } |
3486 | } |
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 | |
3487 | |
3387 | register_exticmd fx_want => sub { |
3488 | register_exticmd fx_want => sub { |
3388 | my ($ns, $want) = @_; |
3489 | my ($ns, $want) = @_; |
3389 | |
3490 | |
3390 | while (my ($k, $v) = each %$want) { |
3491 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3429 | sub reload_treasures { |
3530 | sub reload_treasures { |
3430 | load_resource_file "$DATADIR/treasures" |
3531 | load_resource_file "$DATADIR/treasures" |
3431 | or die "unable to load treasurelists\n"; |
3532 | or die "unable to load treasurelists\n"; |
3432 | } |
3533 | } |
3433 | |
3534 | |
|
|
3535 | sub reload_sound { |
|
|
3536 | trace "loading sound config from $DATADIR/sound\n"; |
|
|
3537 | |
|
|
3538 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3539 | or die "$DATADIR/sound $!"; |
|
|
3540 | |
|
|
3541 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
|
|
3542 | |
|
|
3543 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3544 | my $sound = $soundconf->{compat}[$_] |
|
|
3545 | or next; |
|
|
3546 | |
|
|
3547 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3548 | cf::sound::set $sound->[0] => $face; |
|
|
3549 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3550 | } |
|
|
3551 | |
|
|
3552 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3553 | my $face = cf::face::find "sound/$v"; |
|
|
3554 | cf::sound::set $k => $face; |
|
|
3555 | } |
|
|
3556 | } |
|
|
3557 | |
3434 | sub reload_resources { |
3558 | sub reload_resources { |
3435 | warn "reloading resource files...\n"; |
3559 | trace "reloading resource files...\n"; |
3436 | |
3560 | |
|
|
3561 | reload_exp_table; |
|
|
3562 | reload_materials; |
3437 | reload_facedata; |
3563 | reload_facedata; |
|
|
3564 | reload_sound; |
3438 | reload_archetypes; |
3565 | reload_archetypes; |
3439 | reload_regions; |
3566 | reload_regions; |
3440 | reload_treasures; |
3567 | reload_treasures; |
3441 | |
3568 | |
3442 | warn "finished reloading resource files\n"; |
3569 | trace "finished reloading resource files\n"; |
3443 | } |
3570 | } |
3444 | |
3571 | |
3445 | sub reload_config { |
3572 | sub reload_config { |
3446 | warn "reloading config file...\n"; |
3573 | trace "reloading config file...\n"; |
3447 | |
3574 | |
3448 | open my $fh, "<:utf8", "$CONFDIR/config" |
3575 | 0 < aio_load "$CONFDIR/config", my $config |
3449 | or return; |
3576 | or die "$CONFDIR/config: $!"; |
3450 | |
3577 | |
3451 | local $/; |
3578 | utf8::decode $config; |
3452 | *CFG = YAML::XS::Load scalar <$fh>; |
3579 | *CFG = yaml_load $config; |
3453 | |
3580 | |
3454 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3581 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3455 | |
3582 | |
3456 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3583 | $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}; |
3584 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3458 | |
3585 | |
3459 | if (exists $CFG{mlockall}) { |
3586 | if (exists $CFG{mlockall}) { |
… | |
… | |
3461 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3588 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3462 | and die "WARNING: m(un)lockall failed: $!\n"; |
3589 | and die "WARNING: m(un)lockall failed: $!\n"; |
3463 | }; |
3590 | }; |
3464 | warn $@ if $@; |
3591 | warn $@ if $@; |
3465 | } |
3592 | } |
3466 | |
|
|
3467 | warn "finished reloading resource files\n"; |
|
|
3468 | } |
3593 | } |
3469 | |
3594 | |
3470 | sub pidfile() { |
3595 | sub pidfile() { |
3471 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3596 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3472 | or die "$PIDFILE: $!"; |
3597 | or die "$PIDFILE: $!"; |
… | |
… | |
3485 | seek $fh, 0, 0; |
3610 | seek $fh, 0, 0; |
3486 | print $fh $$; |
3611 | print $fh $$; |
3487 | } |
3612 | } |
3488 | |
3613 | |
3489 | sub main_loop { |
3614 | sub main_loop { |
3490 | warn "EV::loop starting\n"; |
3615 | trace "EV::loop starting\n"; |
3491 | if (1) { |
3616 | if (1) { |
3492 | EV::loop; |
3617 | EV::loop; |
3493 | } |
3618 | } |
3494 | warn "EV::loop returned\n"; |
3619 | trace "EV::loop returned\n"; |
3495 | goto &main_loop unless $REALLY_UNLOOP; |
3620 | goto &main_loop unless $REALLY_UNLOOP; |
3496 | } |
3621 | } |
3497 | |
3622 | |
3498 | sub main { |
3623 | sub main { |
3499 | cf::init_globals; # initialise logging |
3624 | cf::init_globals; # initialise logging |
3500 | |
3625 | |
3501 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3626 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3502 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3627 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3503 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3628 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3504 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3629 | 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 | |
3630 | |
3511 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3631 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3512 | |
3632 | |
3513 | # we must not ever block the main coroutine |
3633 | # we must not ever block the main coroutine |
3514 | local $Coro::idle = sub { |
3634 | local $Coro::idle = sub { |
… | |
… | |
3520 | }; |
3640 | }; |
3521 | |
3641 | |
3522 | evthread_start IO::AIO::poll_fileno; |
3642 | evthread_start IO::AIO::poll_fileno; |
3523 | |
3643 | |
3524 | cf::sync_job { |
3644 | cf::sync_job { |
|
|
3645 | cf::incloader::init (); |
|
|
3646 | |
|
|
3647 | cf::init_anim; |
|
|
3648 | cf::init_attackmess; |
|
|
3649 | cf::init_dynamic; |
|
|
3650 | |
3525 | cf::load_settings; |
3651 | cf::load_settings; |
3526 | cf::load_materials; |
|
|
3527 | |
3652 | |
3528 | reload_resources; |
3653 | reload_resources; |
3529 | reload_config; |
3654 | reload_config; |
3530 | db_init; |
3655 | db_init; |
3531 | |
3656 | |
3532 | cf::init_uuid; |
3657 | cf::init_uuid; |
3533 | cf::init_signals; |
3658 | cf::init_signals; |
3534 | cf::init_commands; |
|
|
3535 | cf::init_skills; |
3659 | cf::init_skills; |
3536 | |
3660 | |
3537 | cf::init_beforeplay; |
3661 | cf::init_beforeplay; |
3538 | |
3662 | |
3539 | atomic; |
3663 | atomic; |
… | |
… | |
3544 | |
3668 | |
3545 | # no (long-running) fork's whatsoever before this point(!) |
3669 | # no (long-running) fork's whatsoever before this point(!) |
3546 | use POSIX (); |
3670 | use POSIX (); |
3547 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3671 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3548 | |
3672 | |
3549 | (pop @POST_INIT)->(0) while @POST_INIT; |
3673 | cf::_post_init 0; |
3550 | }; |
3674 | }; |
3551 | |
3675 | |
|
|
3676 | cf::object::thawer::errors_are_fatal 0; |
|
|
3677 | info "parse errors in files are no longer fatal from this point on.\n"; |
|
|
3678 | |
|
|
3679 | my $free_main; $free_main = EV::idle sub { |
|
|
3680 | undef $free_main; |
|
|
3681 | undef &main; # free gobs of memory :) |
|
|
3682 | }; |
|
|
3683 | |
3552 | main_loop; |
3684 | goto &main_loop; |
3553 | } |
3685 | } |
3554 | |
3686 | |
3555 | ############################################################################# |
3687 | ############################################################################# |
3556 | # initialisation and cleanup |
3688 | # initialisation and cleanup |
3557 | |
3689 | |
3558 | # install some emergency cleanup handlers |
3690 | # install some emergency cleanup handlers |
3559 | BEGIN { |
3691 | BEGIN { |
3560 | our %SIGWATCHER = (); |
3692 | our %SIGWATCHER = (); |
3561 | for my $signal (qw(INT HUP TERM)) { |
3693 | for my $signal (qw(INT HUP TERM)) { |
3562 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3694 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3563 | cf::cleanup "SIG$signal"; |
3695 | cf::cleanup "SIG$signal"; |
3564 | }; |
3696 | }; |
3565 | } |
3697 | } |
3566 | } |
3698 | } |
3567 | |
3699 | |
3568 | sub write_runtime_sync { |
3700 | sub write_runtime_sync { |
|
|
3701 | my $t0 = AE::time; |
|
|
3702 | |
3569 | # first touch the runtime file to show we are still running: |
3703 | # first touch the runtime file to show we are still running: |
3570 | # the fsync below can take a very very long time. |
3704 | # the fsync below can take a very very long time. |
3571 | |
3705 | |
3572 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3706 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3573 | |
3707 | |
3574 | my $guard = cf::lock_acquire "write_runtime"; |
3708 | my $guard = cf::lock_acquire "write_runtime"; |
3575 | |
3709 | |
3576 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3710 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3577 | or return; |
3711 | or return; |
3578 | |
3712 | |
3579 | my $value = $cf::RUNTIME + 90 + 10; |
3713 | my $value = $cf::RUNTIME + 90 + 10; |
3580 | # 10 is the runtime save interval, for a monotonic clock |
3714 | # 10 is the runtime save interval, for a monotonic clock |
3581 | # 60 allows for the watchdog to kill the server. |
3715 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3594 | or return; |
3728 | or return; |
3595 | |
3729 | |
3596 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3730 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3597 | and return; |
3731 | and return; |
3598 | |
3732 | |
3599 | warn "runtime file written.\n"; |
3733 | trace sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3600 | |
3734 | |
3601 | 1 |
3735 | 1 |
3602 | } |
3736 | } |
3603 | |
3737 | |
3604 | our $uuid_lock; |
3738 | our $uuid_lock; |
… | |
… | |
3616 | or return; |
3750 | or return; |
3617 | |
3751 | |
3618 | my $value = uuid_seq uuid_cur; |
3752 | my $value = uuid_seq uuid_cur; |
3619 | |
3753 | |
3620 | unless ($value) { |
3754 | unless ($value) { |
3621 | warn "cowardly refusing to write zero uuid value!\n"; |
3755 | info "cowardly refusing to write zero uuid value!\n"; |
3622 | return; |
3756 | return; |
3623 | } |
3757 | } |
3624 | |
3758 | |
3625 | my $value = uuid_str $value + $uuid_skip; |
3759 | my $value = uuid_str $value + $uuid_skip; |
3626 | $uuid_skip = 0; |
3760 | $uuid_skip = 0; |
… | |
… | |
3636 | or return; |
3770 | or return; |
3637 | |
3771 | |
3638 | aio_rename "$uuid~", $uuid |
3772 | aio_rename "$uuid~", $uuid |
3639 | and return; |
3773 | and return; |
3640 | |
3774 | |
3641 | warn "uuid file written ($value).\n"; |
3775 | trace "uuid file written ($value).\n"; |
3642 | |
3776 | |
3643 | 1 |
3777 | 1 |
3644 | |
3778 | |
3645 | } |
3779 | } |
3646 | |
3780 | |
… | |
… | |
3652 | } |
3786 | } |
3653 | |
3787 | |
3654 | sub emergency_save() { |
3788 | sub emergency_save() { |
3655 | my $freeze_guard = cf::freeze_mainloop; |
3789 | my $freeze_guard = cf::freeze_mainloop; |
3656 | |
3790 | |
3657 | warn "emergency_perl_save: enter\n"; |
3791 | info "emergency_perl_save: enter\n"; |
|
|
3792 | |
|
|
3793 | # this is a trade-off: we want to be very quick here, so |
|
|
3794 | # save all maps without fsync, and later call a global sync |
|
|
3795 | # (which in turn might be very very slow) |
|
|
3796 | local $USE_FSYNC = 0; |
3658 | |
3797 | |
3659 | cf::sync_job { |
3798 | cf::sync_job { |
3660 | # this is a trade-off: we want to be very quick here, so |
3799 | 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 | |
3800 | |
3665 | # use a peculiar iteration method to avoid tripping on perl |
3801 | # use a peculiar iteration method to avoid tripping on perl |
3666 | # refcount bugs in for. also avoids problems with players |
3802 | # refcount bugs in for. also avoids problems with players |
3667 | # and maps saved/destroyed asynchronously. |
3803 | # and maps saved/destroyed asynchronously. |
3668 | warn "emergency_perl_save: begin player save\n"; |
3804 | info "emergency_perl_save: begin player save\n"; |
3669 | for my $login (keys %cf::PLAYER) { |
3805 | for my $login (keys %cf::PLAYER) { |
3670 | my $pl = $cf::PLAYER{$login} or next; |
3806 | my $pl = $cf::PLAYER{$login} or next; |
3671 | $pl->valid or next; |
3807 | $pl->valid or next; |
3672 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3808 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3673 | $pl->save; |
3809 | $pl->save; |
3674 | } |
3810 | } |
3675 | warn "emergency_perl_save: end player save\n"; |
3811 | info "emergency_perl_save: end player save\n"; |
3676 | |
3812 | |
|
|
3813 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3814 | |
3677 | warn "emergency_perl_save: begin map save\n"; |
3815 | info "emergency_perl_save: begin map save\n"; |
3678 | for my $path (keys %cf::MAP) { |
3816 | for my $path (keys %cf::MAP) { |
3679 | my $map = $cf::MAP{$path} or next; |
3817 | my $map = $cf::MAP{$path} or next; |
3680 | $map->valid or next; |
3818 | $map->valid or next; |
3681 | $map->save; |
3819 | $map->save; |
3682 | } |
3820 | } |
3683 | warn "emergency_perl_save: end map save\n"; |
3821 | info "emergency_perl_save: end map save\n"; |
3684 | |
3822 | |
|
|
3823 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3824 | |
3685 | warn "emergency_perl_save: begin database checkpoint\n"; |
3825 | info "emergency_perl_save: begin database checkpoint\n"; |
3686 | BDB::db_env_txn_checkpoint $DB_ENV; |
3826 | BDB::db_env_txn_checkpoint $DB_ENV; |
3687 | warn "emergency_perl_save: end database checkpoint\n"; |
3827 | info "emergency_perl_save: end database checkpoint\n"; |
3688 | |
3828 | |
3689 | warn "emergency_perl_save: begin write uuid\n"; |
3829 | info "emergency_perl_save: begin write uuid\n"; |
3690 | write_uuid_sync 1; |
3830 | write_uuid_sync 1; |
3691 | warn "emergency_perl_save: end write uuid\n"; |
3831 | info "emergency_perl_save: end write uuid\n"; |
|
|
3832 | |
|
|
3833 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3834 | |
|
|
3835 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3836 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3837 | |
|
|
3838 | info "emergency_perl_save: starting sync\n"; |
|
|
3839 | IO::AIO::aio_sync sub { |
|
|
3840 | info "emergency_perl_save: finished sync\n"; |
|
|
3841 | }; |
|
|
3842 | |
|
|
3843 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3844 | |
|
|
3845 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3846 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3847 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3848 | } |
|
|
3849 | |
|
|
3850 | cf::write_runtime_sync; # external watchdog should not bark |
3692 | }; |
3851 | }; |
3693 | |
3852 | |
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"; |
3853 | info "emergency_perl_save: leave\n"; |
3700 | } |
3854 | } |
3701 | |
3855 | |
3702 | sub post_cleanup { |
3856 | sub post_cleanup { |
3703 | my ($make_core) = @_; |
3857 | my ($make_core) = @_; |
3704 | |
3858 | |
|
|
3859 | IO::AIO::flush; |
|
|
3860 | |
3705 | warn Carp::longmess "post_cleanup backtrace" |
3861 | error Carp::longmess "post_cleanup backtrace" |
3706 | if $make_core; |
3862 | if $make_core; |
3707 | |
3863 | |
3708 | my $fh = pidfile; |
3864 | my $fh = pidfile; |
3709 | unlink $PIDFILE if <$fh> == $$; |
3865 | unlink $PIDFILE if <$fh> == $$; |
3710 | } |
3866 | } |
… | |
… | |
3730 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3886 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3731 | for my $name (keys %$leaf_symtab) { |
3887 | for my $name (keys %$leaf_symtab) { |
3732 | _gv_clear *{"$pkg$name"}; |
3888 | _gv_clear *{"$pkg$name"}; |
3733 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3889 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3734 | } |
3890 | } |
3735 | warn "cleared package $pkg\n";#d# |
|
|
3736 | } |
3891 | } |
3737 | |
3892 | |
3738 | sub do_reload_perl() { |
3893 | sub do_reload_perl() { |
3739 | # can/must only be called in main |
3894 | # can/must only be called in main |
3740 | if ($Coro::current != $Coro::main) { |
3895 | unless (in_main) { |
3741 | warn "can only reload from main coroutine"; |
3896 | error "can only reload from main coroutine"; |
3742 | return; |
3897 | return; |
3743 | } |
3898 | } |
3744 | |
3899 | |
3745 | return if $RELOAD++; |
3900 | return if $RELOAD++; |
3746 | |
3901 | |
3747 | my $t1 = EV::time; |
3902 | my $t1 = AE::time; |
3748 | |
3903 | |
3749 | while ($RELOAD) { |
3904 | while ($RELOAD) { |
3750 | warn "reloading..."; |
3905 | cf::get_slot 0.1, -1, "reload_perl"; |
|
|
3906 | info "perl_reload: reloading..."; |
3751 | |
3907 | |
3752 | warn "entering sync_job"; |
3908 | trace "perl_reload: entering sync_job"; |
3753 | |
3909 | |
3754 | cf::sync_job { |
3910 | cf::sync_job { |
3755 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3756 | cf::emergency_save; |
3911 | #cf::emergency_save; |
3757 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3758 | |
3912 | |
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"; |
3913 | trace "perl_reload: cancelling all extension coros"; |
3770 | $_->cancel for values %EXT_CORO; |
3914 | $_->cancel for values %EXT_CORO; |
3771 | %EXT_CORO = (); |
3915 | %EXT_CORO = (); |
3772 | |
3916 | |
3773 | warn "removing commands"; |
3917 | trace "perl_reload: removing commands"; |
3774 | %COMMAND = (); |
3918 | %COMMAND = (); |
3775 | |
3919 | |
3776 | warn "removing ext/exti commands"; |
3920 | trace "perl_reload: removing ext/exti commands"; |
3777 | %EXTCMD = (); |
3921 | %EXTCMD = (); |
3778 | %EXTICMD = (); |
3922 | %EXTICMD = (); |
3779 | |
3923 | |
3780 | warn "unloading/nuking all extensions"; |
3924 | trace "perl_reload: unloading/nuking all extensions"; |
3781 | for my $pkg (@EXTS) { |
3925 | for my $pkg (@EXTS) { |
3782 | warn "... unloading $pkg"; |
3926 | trace "... unloading $pkg"; |
3783 | |
3927 | |
3784 | if (my $cb = $pkg->can ("unload")) { |
3928 | if (my $cb = $pkg->can ("unload")) { |
3785 | eval { |
3929 | eval { |
3786 | $cb->($pkg); |
3930 | $cb->($pkg); |
3787 | 1 |
3931 | 1 |
3788 | } or warn "$pkg unloaded, but with errors: $@"; |
3932 | } or error "$pkg unloaded, but with errors: $@"; |
3789 | } |
3933 | } |
3790 | |
3934 | |
3791 | warn "... clearing $pkg"; |
3935 | trace "... clearing $pkg"; |
3792 | clear_package $pkg; |
3936 | clear_package $pkg; |
3793 | } |
3937 | } |
3794 | |
3938 | |
3795 | warn "unloading all perl modules loaded from $LIBDIR"; |
3939 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3796 | while (my ($k, $v) = each %INC) { |
3940 | while (my ($k, $v) = each %INC) { |
3797 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3941 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3798 | |
3942 | |
3799 | warn "... unloading $k"; |
3943 | trace "... unloading $k"; |
3800 | delete $INC{$k}; |
3944 | delete $INC{$k}; |
3801 | |
3945 | |
3802 | $k =~ s/\.pm$//; |
3946 | $k =~ s/\.pm$//; |
3803 | $k =~ s/\//::/g; |
3947 | $k =~ s/\//::/g; |
3804 | |
3948 | |
… | |
… | |
3807 | } |
3951 | } |
3808 | |
3952 | |
3809 | clear_package $k; |
3953 | clear_package $k; |
3810 | } |
3954 | } |
3811 | |
3955 | |
3812 | warn "getting rid of safe::, as good as possible"; |
3956 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3813 | clear_package "safe::$_" |
3957 | clear_package "safe::$_" |
3814 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3958 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3815 | |
3959 | |
3816 | warn "unloading cf.pm \"a bit\""; |
3960 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3817 | delete $INC{"cf.pm"}; |
3961 | delete $INC{"cf.pm"}; |
3818 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3962 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3819 | |
3963 | |
3820 | # don't, removes xs symbols, too, |
3964 | # don't, removes xs symbols, too, |
3821 | # and global variables created in xs |
3965 | # and global variables created in xs |
3822 | #clear_package __PACKAGE__; |
3966 | #clear_package __PACKAGE__; |
3823 | |
3967 | |
3824 | warn "unload completed, starting to reload now"; |
3968 | info "perl_reload: unload completed, starting to reload now"; |
3825 | |
3969 | |
3826 | warn "reloading cf.pm"; |
3970 | trace "perl_reload: reloading cf.pm"; |
3827 | require cf; |
3971 | require cf; |
3828 | cf::_connect_to_perl_1; |
3972 | cf::_connect_to_perl_1; |
3829 | |
3973 | |
3830 | warn "loading config and database again"; |
3974 | trace "perl_reload: loading config and database again"; |
3831 | cf::reload_config; |
3975 | cf::reload_config; |
3832 | |
3976 | |
3833 | warn "loading extensions"; |
3977 | trace "perl_reload: loading extensions"; |
3834 | cf::load_extensions; |
3978 | cf::load_extensions; |
3835 | |
3979 | |
3836 | if ($REATTACH_ON_RELOAD) { |
3980 | if ($REATTACH_ON_RELOAD) { |
3837 | warn "reattaching attachments to objects/players"; |
3981 | trace "perl_reload: reattaching attachments to objects/players"; |
3838 | _global_reattach; # objects, sockets |
3982 | _global_reattach; # objects, sockets |
3839 | warn "reattaching attachments to maps"; |
3983 | trace "perl_reload: reattaching attachments to maps"; |
3840 | reattach $_ for values %MAP; |
3984 | reattach $_ for values %MAP; |
3841 | warn "reattaching attachments to players"; |
3985 | trace "perl_reload: reattaching attachments to players"; |
3842 | reattach $_ for values %PLAYER; |
3986 | reattach $_ for values %PLAYER; |
3843 | } |
3987 | } |
3844 | |
3988 | |
3845 | warn "running post_init jobs"; |
3989 | cf::_post_init 1; |
3846 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3847 | |
3990 | |
3848 | warn "leaving sync_job"; |
3991 | trace "perl_reload: leaving sync_job"; |
3849 | |
3992 | |
3850 | 1 |
3993 | 1 |
3851 | } or do { |
3994 | } or do { |
3852 | warn $@; |
3995 | error $@; |
3853 | cf::cleanup "error while reloading, exiting."; |
3996 | cf::cleanup "perl_reload: error, exiting."; |
3854 | }; |
3997 | }; |
3855 | |
3998 | |
3856 | warn "reloaded"; |
|
|
3857 | --$RELOAD; |
3999 | --$RELOAD; |
3858 | } |
4000 | } |
3859 | |
4001 | |
3860 | $t1 = EV::time - $t1; |
4002 | $t1 = AE::time - $t1; |
3861 | warn "reload completed in ${t1}s\n"; |
4003 | info "perl_reload: completed in ${t1}s\n"; |
3862 | }; |
4004 | }; |
3863 | |
4005 | |
3864 | our $RELOAD_WATCHER; # used only during reload |
4006 | our $RELOAD_WATCHER; # used only during reload |
3865 | |
4007 | |
3866 | sub reload_perl() { |
4008 | sub reload_perl() { |
… | |
… | |
3868 | # coro crashes during coro_state_free->destroy here. |
4010 | # coro crashes during coro_state_free->destroy here. |
3869 | |
4011 | |
3870 | $RELOAD_WATCHER ||= cf::async { |
4012 | $RELOAD_WATCHER ||= cf::async { |
3871 | Coro::AIO::aio_wait cache_extensions; |
4013 | Coro::AIO::aio_wait cache_extensions; |
3872 | |
4014 | |
3873 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
4015 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3874 | do_reload_perl; |
4016 | do_reload_perl; |
3875 | undef $RELOAD_WATCHER; |
4017 | undef $RELOAD_WATCHER; |
3876 | }; |
4018 | }; |
3877 | }; |
4019 | }; |
3878 | } |
4020 | } |
… | |
… | |
3887 | reload_perl; |
4029 | reload_perl; |
3888 | }; |
4030 | }; |
3889 | } |
4031 | } |
3890 | }; |
4032 | }; |
3891 | |
4033 | |
3892 | unshift @INC, $LIBDIR; |
4034 | ############################################################################# |
3893 | |
4035 | |
3894 | my $bug_warning = 0; |
4036 | my $bug_warning = 0; |
3895 | |
4037 | |
3896 | our @WAIT_FOR_TICK; |
4038 | our @WAIT_FOR_TICK; |
3897 | our @WAIT_FOR_TICK_BEGIN; |
4039 | our @WAIT_FOR_TICK_BEGIN; |
3898 | |
4040 | |
3899 | sub wait_for_tick { |
4041 | sub wait_for_tick() { |
3900 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4042 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3901 | |
4043 | |
3902 | my $signal = new Coro::Signal; |
4044 | my $signal = new Coro::Signal; |
3903 | push @WAIT_FOR_TICK, $signal; |
4045 | push @WAIT_FOR_TICK, $signal; |
3904 | $signal->wait; |
4046 | $signal->wait; |
3905 | } |
4047 | } |
3906 | |
4048 | |
3907 | sub wait_for_tick_begin { |
4049 | sub wait_for_tick_begin() { |
3908 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4050 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3909 | |
4051 | |
3910 | my $signal = new Coro::Signal; |
4052 | my $signal = new Coro::Signal; |
3911 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4053 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3912 | $signal->wait; |
4054 | $signal->wait; |
… | |
… | |
3919 | return; |
4061 | return; |
3920 | } |
4062 | } |
3921 | |
4063 | |
3922 | cf::server_tick; # one server iteration |
4064 | cf::server_tick; # one server iteration |
3923 | |
4065 | |
3924 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
4066 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3925 | |
4067 | |
3926 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4068 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3927 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4069 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3928 | Coro::async_pool { |
4070 | Coro::async_pool { |
3929 | $Coro::current->{desc} = "runtime saver"; |
4071 | $Coro::current->{desc} = "runtime saver"; |
3930 | write_runtime_sync |
4072 | write_runtime_sync |
3931 | or warn "ERROR: unable to write runtime file: $!"; |
4073 | or error "ERROR: unable to write runtime file: $!"; |
3932 | }; |
4074 | }; |
3933 | } |
4075 | } |
3934 | |
4076 | |
3935 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4077 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3936 | $sig->send; |
4078 | $sig->send; |
… | |
… | |
3944 | |
4086 | |
3945 | if (0) { |
4087 | if (0) { |
3946 | if ($NEXT_TICK) { |
4088 | if ($NEXT_TICK) { |
3947 | my $jitter = $TICK_START - $NEXT_TICK; |
4089 | my $jitter = $TICK_START - $NEXT_TICK; |
3948 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4090 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
3949 | warn "jitter $JITTER\n";#d# |
4091 | debug "jitter $JITTER\n";#d# |
3950 | } |
4092 | } |
3951 | } |
4093 | } |
3952 | } |
4094 | } |
3953 | |
4095 | |
3954 | { |
4096 | { |
… | |
… | |
3999 | IO::AIO::min_parallel 8; |
4141 | IO::AIO::min_parallel 8; |
4000 | IO::AIO::max_poll_time $TICK * 0.1; |
4142 | IO::AIO::max_poll_time $TICK * 0.1; |
4001 | undef $AnyEvent::AIO::WATCHER; |
4143 | undef $AnyEvent::AIO::WATCHER; |
4002 | } |
4144 | } |
4003 | |
4145 | |
4004 | my $_log_backtrace; |
4146 | our $_log_backtrace; |
|
|
4147 | our $_log_backtrace_last; |
4005 | |
4148 | |
4006 | sub _log_backtrace { |
4149 | sub _log_backtrace { |
4007 | my ($msg, @addr) = @_; |
4150 | my ($msg, @addr) = @_; |
4008 | |
4151 | |
4009 | $msg =~ s/\n//; |
4152 | $msg =~ s/\n$//; |
4010 | |
4153 | |
|
|
4154 | if ($_log_backtrace_last eq $msg) { |
|
|
4155 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
4156 | LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; |
4011 | # limit the # of concurrent backtraces |
4157 | # limit the # of concurrent backtraces |
4012 | if ($_log_backtrace < 2) { |
4158 | } elsif ($_log_backtrace < 2) { |
|
|
4159 | $_log_backtrace_last = $msg; |
4013 | ++$_log_backtrace; |
4160 | ++$_log_backtrace; |
4014 | my $perl_bt = Carp::longmess $msg; |
4161 | my $perl_bt = Carp::longmess $msg; |
4015 | async { |
4162 | async { |
4016 | $Coro::current->{desc} = "abt $msg"; |
4163 | $Coro::current->{desc} = "abt $msg"; |
4017 | |
4164 | |
… | |
… | |
4037 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4184 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4038 | --$_log_backtrace; |
4185 | --$_log_backtrace; |
4039 | }; |
4186 | }; |
4040 | } else { |
4187 | } else { |
4041 | LOG llevInfo, "[ABT] $msg\n"; |
4188 | LOG llevInfo, "[ABT] $msg\n"; |
4042 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4189 | LOG llevInfo, "[ABT] [overload, suppressed]\n"; |
4043 | } |
4190 | } |
4044 | } |
4191 | } |
4045 | |
4192 | |
4046 | # load additional modules |
4193 | # load additional modules |
4047 | require "cf/$_.pm" for @EXTRA_MODULES; |
4194 | require "cf/$_.pm" for @EXTRA_MODULES; |