… | |
… | |
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 = (); |
… | |
… | |
124 | our $BDB_DEADLOCK_WATCHER; |
124 | our $BDB_DEADLOCK_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
127 | our $DB_ENV; |
127 | our $DB_ENV; |
128 | |
128 | |
129 | our @EXTRA_MODULES = qw(pod match mapscript); |
129 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
130 | |
130 | |
131 | our %CFG; |
131 | our %CFG; |
132 | |
132 | |
133 | our $UPTIME; $UPTIME ||= time; |
133 | our $UPTIME; $UPTIME ||= time; |
134 | our $RUNTIME; |
134 | our $RUNTIME; |
… | |
… | |
233 | from wherever your confdir points to. |
233 | from wherever your confdir points to. |
234 | |
234 | |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
236 | |
236 | |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
238 | returns directly I<after> the tick processing (and consequently, can only wake one process |
238 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
240 | |
|
|
241 | =cut |
|
|
242 | |
|
|
243 | sub wait_for_tick(); |
|
|
244 | sub wait_for_tick_begin(); |
240 | |
245 | |
241 | =item @cf::INVOKE_RESULTS |
246 | =item @cf::INVOKE_RESULTS |
242 | |
247 | |
243 | This array contains the results of the last C<invoke ()> call. When |
248 | This array contains the results of the last C<invoke ()> call. When |
244 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
249 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
… | |
… | |
352 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
353 | |
358 | |
354 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
355 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
356 | |
361 | |
|
|
362 | =item $ref = cf::yaml_load $scalar |
|
|
363 | |
|
|
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
365 | |
|
|
366 | =cut |
|
|
367 | |
|
|
368 | sub fork_call(&@); |
|
|
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
372 | } |
|
|
373 | |
357 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
358 | |
375 | |
359 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
360 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
361 | |
378 | |
362 | The cdoeblock will have a single boolean argument to indicate whether this |
379 | The codeblock will have a single boolean argument to indicate whether this |
363 | is a reload or not. |
380 | is a reload or not. |
364 | |
381 | |
365 | =cut |
382 | =cut |
366 | |
383 | |
367 | sub post_init(&) { |
384 | sub post_init(&) { |
368 | push @POST_INIT, shift; |
385 | push @POST_INIT, shift; |
|
|
386 | } |
|
|
387 | |
|
|
388 | sub _post_init { |
|
|
389 | trace "running post_init jobs"; |
|
|
390 | |
|
|
391 | # run them in parallel... |
|
|
392 | |
|
|
393 | my @join; |
|
|
394 | |
|
|
395 | while () { |
|
|
396 | push @join, map &Coro::async ($_, 0), @POST_INIT; |
|
|
397 | @POST_INIT = (); |
|
|
398 | |
|
|
399 | @join or last; |
|
|
400 | |
|
|
401 | (pop @join)->join; |
|
|
402 | } |
369 | } |
403 | } |
370 | |
404 | |
371 | =item cf::lock_wait $string |
405 | =item cf::lock_wait $string |
372 | |
406 | |
373 | Wait until the given lock is available. See cf::lock_acquire. |
407 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
578 | $EXT_CORO{$coro+0} = $coro; |
612 | $EXT_CORO{$coro+0} = $coro; |
579 | |
613 | |
580 | $coro |
614 | $coro |
581 | } |
615 | } |
582 | |
616 | |
583 | =item fork_call { }, $args |
617 | =item fork_call { }, @args |
584 | |
618 | |
585 | Executes the given code block with the given arguments in a seperate |
619 | Executes the given code block with the given arguments in a seperate |
586 | process, returning the results. Everything must be serialisable with |
620 | process, returning the results. Everything must be serialisable with |
587 | Coro::Storable. May, of course, block. Note that the executed sub may |
621 | Coro::Storable. May, of course, block. Note that the executed sub may |
588 | never block itself or use any form of event handling. |
622 | never block itself or use any form of event handling. |
589 | |
623 | |
590 | =cut |
624 | =cut |
591 | |
625 | |
|
|
626 | sub post_fork { |
|
|
627 | reset_signals; |
|
|
628 | } |
|
|
629 | |
592 | sub fork_call(&@) { |
630 | sub fork_call(&@) { |
593 | my ($cb, @args) = @_; |
631 | my ($cb, @args) = @_; |
594 | |
632 | |
595 | # we seemingly have to make a local copy of the whole thing, |
633 | # we seemingly have to make a local copy of the whole thing, |
596 | # otherwise perl prematurely frees the stuff :/ |
634 | # otherwise perl prematurely frees the stuff :/ |
597 | # TODO: investigate and fix (likely this will be rather laborious) |
635 | # TODO: investigate and fix (likely this will be rather laborious) |
598 | |
636 | |
599 | my @res = Coro::Util::fork_eval { |
637 | my @res = Coro::Util::fork_eval { |
600 | reset_signals; |
638 | cf::post_fork; |
601 | &$cb |
639 | &$cb |
602 | }, @args; |
640 | } @args; |
603 | |
641 | |
604 | wantarray ? @res : $res[-1] |
642 | wantarray ? @res : $res[-1] |
605 | } |
643 | } |
606 | |
644 | |
607 | sub objinfo { |
645 | sub objinfo { |
… | |
… | |
1423 | |
1461 | |
1424 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1462 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1425 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1463 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1426 | |
1464 | |
1427 | $ext{source} = |
1465 | $ext{source} = |
1428 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1466 | "package $pkg; use common::sense;\n" |
1429 | . "#line 1 \"$path\"\n{\n" |
1467 | . "#line 1 \"$path\"\n{\n" |
1430 | . $source |
1468 | . $source |
1431 | . "\n};\n1"; |
1469 | . "\n};\n1"; |
1432 | |
1470 | |
1433 | $todo{$base} = \%ext; |
1471 | $todo{$base} = \%ext; |
… | |
… | |
1641 | my $name = $pl->ob->name; |
1679 | my $name = $pl->ob->name; |
1642 | |
1680 | |
1643 | $pl->{deny_save} = 1; |
1681 | $pl->{deny_save} = 1; |
1644 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1682 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1645 | |
1683 | |
1646 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1684 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1647 | $pl->deactivate; |
1685 | $pl->deactivate; |
|
|
1686 | |
1648 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1687 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1649 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1688 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1689 | ext::highscore::check ($pl->ob); |
|
|
1690 | |
1650 | $pl->ns->destroy if $pl->ns; |
1691 | $pl->ns->destroy if $pl->ns; |
1651 | |
1692 | |
1652 | my $path = playerdir $pl; |
1693 | my $path = playerdir $pl; |
1653 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1694 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1654 | aio_rename $path, $temp; |
1695 | aio_rename $path, $temp; |
… | |
… | |
1848 | |
1889 | |
1849 | sub register { |
1890 | sub register { |
1850 | my (undef, $regex, $prio) = @_; |
1891 | my (undef, $regex, $prio) = @_; |
1851 | my $pkg = caller; |
1892 | my $pkg = caller; |
1852 | |
1893 | |
1853 | no strict; |
|
|
1854 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1894 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1855 | |
1895 | |
1856 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1896 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1857 | } |
1897 | } |
1858 | |
1898 | |
… | |
… | |
1870 | } |
1910 | } |
1871 | |
1911 | |
1872 | sub normalise { |
1912 | sub normalise { |
1873 | my ($path, $base) = @_; |
1913 | my ($path, $base) = @_; |
1874 | |
1914 | |
1875 | $path = "$path"; # make sure its a string |
1915 | $path = "$path"; # make sure it's a string |
1876 | |
1916 | |
1877 | $path =~ s/\.map$//; |
1917 | $path =~ s/\.map$//; |
1878 | |
1918 | |
1879 | # map plan: |
1919 | # map plan: |
1880 | # |
1920 | # |
… | |
… | |
1895 | $base =~ s{[^/]+/?$}{}; |
1935 | $base =~ s{[^/]+/?$}{}; |
1896 | $path = "$base/$path"; |
1936 | $path = "$base/$path"; |
1897 | } |
1937 | } |
1898 | |
1938 | |
1899 | for ($path) { |
1939 | for ($path) { |
1900 | redo if s{//}{/}; |
|
|
1901 | redo if s{/\.?/}{/}; |
1940 | redo if s{/\.?/}{/}; |
1902 | redo if s{/[^/]+/\.\./}{/}; |
1941 | redo if s{/[^/]+/\.\./}{/}; |
1903 | } |
1942 | } |
1904 | |
1943 | |
1905 | $path |
1944 | $path |
… | |
… | |
1919 | $self->init; # pass $1 etc. |
1958 | $self->init; # pass $1 etc. |
1920 | return $self; |
1959 | return $self; |
1921 | } |
1960 | } |
1922 | } |
1961 | } |
1923 | |
1962 | |
1924 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1963 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1925 | () |
1964 | () |
1926 | } |
1965 | } |
1927 | |
1966 | |
1928 | sub init { |
1967 | sub init { |
1929 | my ($self) = @_; |
1968 | my ($self) = @_; |
… | |
… | |
2051 | } |
2090 | } |
2052 | |
2091 | |
2053 | sub find; |
2092 | sub find; |
2054 | sub find { |
2093 | sub find { |
2055 | my ($path, $origin) = @_; |
2094 | my ($path, $origin) = @_; |
|
|
2095 | |
|
|
2096 | cf::cede_to_tick; |
2056 | |
2097 | |
2057 | $path = normalise $path, $origin && $origin->path; |
2098 | $path = normalise $path, $origin && $origin->path; |
2058 | |
2099 | |
2059 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2100 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2060 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2101 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
3273 | decrease split destroy change_exp value msg lore send_msg)], |
3314 | decrease split destroy change_exp value msg lore send_msg)], |
3274 | ["cf::object::player" => qw(player)], |
3315 | ["cf::object::player" => qw(player)], |
3275 | ["cf::player" => qw(peaceful send_msg)], |
3316 | ["cf::player" => qw(peaceful send_msg)], |
3276 | ["cf::map" => qw(trigger)], |
3317 | ["cf::map" => qw(trigger)], |
3277 | ) { |
3318 | ) { |
3278 | no strict 'refs'; |
|
|
3279 | my ($pkg, @funs) = @$_; |
3319 | my ($pkg, @funs) = @$_; |
3280 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3320 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3281 | for @funs; |
3321 | for @funs; |
3282 | } |
3322 | } |
3283 | |
3323 | |
… | |
… | |
3370 | |
3410 | |
3371 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3411 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3372 | |
3412 | |
3373 | trace "loading facedata from $path\n"; |
3413 | trace "loading facedata from $path\n"; |
3374 | |
3414 | |
3375 | my $facedata; |
|
|
3376 | 0 < aio_load $path, $facedata |
3415 | 0 < aio_load $path, my $facedata |
3377 | or die "$path: $!"; |
3416 | or die "$path: $!"; |
3378 | |
3417 | |
3379 | $facedata = Coro::Storable::thaw $facedata; |
3418 | $facedata = Coro::Storable::thaw $facedata; |
3380 | |
3419 | |
3381 | $facedata->{version} == 2 |
3420 | $facedata->{version} == 2 |
… | |
… | |
3525 | } |
3564 | } |
3526 | |
3565 | |
3527 | sub reload_resources { |
3566 | sub reload_resources { |
3528 | trace "reloading resource files...\n"; |
3567 | trace "reloading resource files...\n"; |
3529 | |
3568 | |
|
|
3569 | reload_exp_table; |
|
|
3570 | reload_materials; |
3530 | reload_facedata; |
3571 | reload_facedata; |
3531 | reload_sound; |
3572 | reload_sound; |
3532 | reload_archetypes; |
3573 | reload_archetypes; |
3533 | reload_regions; |
3574 | reload_regions; |
3534 | reload_treasures; |
3575 | reload_treasures; |
… | |
… | |
3537 | } |
3578 | } |
3538 | |
3579 | |
3539 | sub reload_config { |
3580 | sub reload_config { |
3540 | trace "reloading config file...\n"; |
3581 | trace "reloading config file...\n"; |
3541 | |
3582 | |
3542 | open my $fh, "<:utf8", "$CONFDIR/config" |
3583 | 0 < aio_load "$CONFDIR/config", my $config |
3543 | or return; |
3584 | or die "$CONFDIR/config: $!"; |
3544 | |
3585 | |
3545 | local $/; |
3586 | utf8::decode $config; |
3546 | *CFG = YAML::XS::Load scalar <$fh>; |
3587 | *CFG = yaml_load $config; |
3547 | |
3588 | |
3548 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3589 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3549 | |
3590 | |
3550 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3591 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3551 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3592 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3555 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3596 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3556 | and die "WARNING: m(un)lockall failed: $!\n"; |
3597 | and die "WARNING: m(un)lockall failed: $!\n"; |
3557 | }; |
3598 | }; |
3558 | warn $@ if $@; |
3599 | warn $@ if $@; |
3559 | } |
3600 | } |
3560 | |
|
|
3561 | trace "finished reloading resource files\n"; |
|
|
3562 | } |
3601 | } |
3563 | |
3602 | |
3564 | sub pidfile() { |
3603 | sub pidfile() { |
3565 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3604 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3566 | or die "$PIDFILE: $!"; |
3605 | or die "$PIDFILE: $!"; |
… | |
… | |
3591 | |
3630 | |
3592 | sub main { |
3631 | sub main { |
3593 | cf::init_globals; # initialise logging |
3632 | cf::init_globals; # initialise logging |
3594 | |
3633 | |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3634 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3596 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3635 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3636 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3637 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3599 | |
3638 | |
3600 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3639 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3601 | |
3640 | |
… | |
… | |
3609 | }; |
3648 | }; |
3610 | |
3649 | |
3611 | evthread_start IO::AIO::poll_fileno; |
3650 | evthread_start IO::AIO::poll_fileno; |
3612 | |
3651 | |
3613 | cf::sync_job { |
3652 | cf::sync_job { |
3614 | cf::init_experience; |
3653 | cf::incloader::init (); |
|
|
3654 | |
3615 | cf::init_anim; |
3655 | cf::init_anim; |
3616 | cf::init_attackmess; |
3656 | cf::init_attackmess; |
3617 | cf::init_dynamic; |
3657 | cf::init_dynamic; |
3618 | |
3658 | |
3619 | cf::load_settings; |
3659 | cf::load_settings; |
3620 | cf::load_materials; |
|
|
3621 | |
3660 | |
3622 | reload_resources; |
3661 | reload_resources; |
3623 | reload_config; |
3662 | reload_config; |
3624 | db_init; |
3663 | db_init; |
3625 | |
3664 | |
… | |
… | |
3637 | |
3676 | |
3638 | # no (long-running) fork's whatsoever before this point(!) |
3677 | # no (long-running) fork's whatsoever before this point(!) |
3639 | use POSIX (); |
3678 | use POSIX (); |
3640 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3679 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3641 | |
3680 | |
3642 | (pop @POST_INIT)->(0) while @POST_INIT; |
3681 | cf::_post_init 0; |
3643 | }; |
3682 | }; |
3644 | |
3683 | |
3645 | cf::object::thawer::errors_are_fatal 0; |
3684 | cf::object::thawer::errors_are_fatal 0; |
3646 | info "parse errors in files are no longer fatal from this point on.\n"; |
3685 | info "parse errors in files are no longer fatal from this point on.\n"; |
3647 | |
3686 | |
|
|
3687 | my $free_main; $free_main = EV::idle sub { |
|
|
3688 | undef $free_main; |
|
|
3689 | undef &main; # free gobs of memory :) |
|
|
3690 | }; |
|
|
3691 | |
3648 | main_loop; |
3692 | goto &main_loop; |
3649 | } |
3693 | } |
3650 | |
3694 | |
3651 | ############################################################################# |
3695 | ############################################################################# |
3652 | # initialisation and cleanup |
3696 | # initialisation and cleanup |
3653 | |
3697 | |
… | |
… | |
3854 | } |
3898 | } |
3855 | } |
3899 | } |
3856 | |
3900 | |
3857 | sub do_reload_perl() { |
3901 | sub do_reload_perl() { |
3858 | # can/must only be called in main |
3902 | # can/must only be called in main |
3859 | if (in_main) { |
3903 | unless (in_main) { |
3860 | error "can only reload from main coroutine"; |
3904 | error "can only reload from main coroutine"; |
3861 | return; |
3905 | return; |
3862 | } |
3906 | } |
3863 | |
3907 | |
3864 | return if $RELOAD++; |
3908 | return if $RELOAD++; |
3865 | |
3909 | |
3866 | my $t1 = AE::time; |
3910 | my $t1 = AE::time; |
3867 | |
3911 | |
3868 | while ($RELOAD) { |
3912 | while ($RELOAD) { |
|
|
3913 | cf::get_slot 0.1, -1, "reload_perl"; |
3869 | info "reloading..."; |
3914 | info "perl_reload: reloading..."; |
3870 | |
3915 | |
3871 | trace "entering sync_job"; |
3916 | trace "perl_reload: entering sync_job"; |
3872 | |
3917 | |
3873 | cf::sync_job { |
3918 | cf::sync_job { |
3874 | cf::emergency_save; |
3919 | #cf::emergency_save; |
3875 | |
3920 | |
3876 | trace "cancelling all extension coros"; |
3921 | trace "perl_reload: cancelling all extension coros"; |
3877 | $_->cancel for values %EXT_CORO; |
3922 | $_->cancel for values %EXT_CORO; |
3878 | %EXT_CORO = (); |
3923 | %EXT_CORO = (); |
3879 | |
3924 | |
3880 | trace "removing commands"; |
3925 | trace "perl_reload: removing commands"; |
3881 | %COMMAND = (); |
3926 | %COMMAND = (); |
3882 | |
3927 | |
3883 | trace "removing ext/exti commands"; |
3928 | trace "perl_reload: removing ext/exti commands"; |
3884 | %EXTCMD = (); |
3929 | %EXTCMD = (); |
3885 | %EXTICMD = (); |
3930 | %EXTICMD = (); |
3886 | |
3931 | |
3887 | trace "unloading/nuking all extensions"; |
3932 | trace "perl_reload: unloading/nuking all extensions"; |
3888 | for my $pkg (@EXTS) { |
3933 | for my $pkg (@EXTS) { |
3889 | trace "... unloading $pkg"; |
3934 | trace "... unloading $pkg"; |
3890 | |
3935 | |
3891 | if (my $cb = $pkg->can ("unload")) { |
3936 | if (my $cb = $pkg->can ("unload")) { |
3892 | eval { |
3937 | eval { |
… | |
… | |
3897 | |
3942 | |
3898 | trace "... clearing $pkg"; |
3943 | trace "... clearing $pkg"; |
3899 | clear_package $pkg; |
3944 | clear_package $pkg; |
3900 | } |
3945 | } |
3901 | |
3946 | |
3902 | trace "unloading all perl modules loaded from $LIBDIR"; |
3947 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3903 | while (my ($k, $v) = each %INC) { |
3948 | while (my ($k, $v) = each %INC) { |
3904 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3949 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3905 | |
3950 | |
3906 | trace "... unloading $k"; |
3951 | trace "... unloading $k"; |
3907 | delete $INC{$k}; |
3952 | delete $INC{$k}; |
… | |
… | |
3914 | } |
3959 | } |
3915 | |
3960 | |
3916 | clear_package $k; |
3961 | clear_package $k; |
3917 | } |
3962 | } |
3918 | |
3963 | |
3919 | trace "getting rid of safe::, as good as possible"; |
3964 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3920 | clear_package "safe::$_" |
3965 | clear_package "safe::$_" |
3921 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3966 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3922 | |
3967 | |
3923 | trace "unloading cf.pm \"a bit\""; |
3968 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3924 | delete $INC{"cf.pm"}; |
3969 | delete $INC{"cf.pm"}; |
3925 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3970 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3926 | |
3971 | |
3927 | # don't, removes xs symbols, too, |
3972 | # don't, removes xs symbols, too, |
3928 | # and global variables created in xs |
3973 | # and global variables created in xs |
3929 | #clear_package __PACKAGE__; |
3974 | #clear_package __PACKAGE__; |
3930 | |
3975 | |
3931 | info "unload completed, starting to reload now"; |
3976 | info "perl_reload: unload completed, starting to reload now"; |
3932 | |
3977 | |
3933 | trace "reloading cf.pm"; |
3978 | trace "perl_reload: reloading cf.pm"; |
3934 | require cf; |
3979 | require cf; |
3935 | cf::_connect_to_perl_1; |
3980 | cf::_connect_to_perl_1; |
3936 | |
3981 | |
3937 | trace "loading config and database again"; |
3982 | trace "perl_reload: loading config and database again"; |
3938 | cf::reload_config; |
3983 | cf::reload_config; |
3939 | |
3984 | |
3940 | trace "loading extensions"; |
3985 | trace "perl_reload: loading extensions"; |
3941 | cf::load_extensions; |
3986 | cf::load_extensions; |
3942 | |
3987 | |
3943 | if ($REATTACH_ON_RELOAD) { |
3988 | if ($REATTACH_ON_RELOAD) { |
3944 | trace "reattaching attachments to objects/players"; |
3989 | trace "perl_reload: reattaching attachments to objects/players"; |
3945 | _global_reattach; # objects, sockets |
3990 | _global_reattach; # objects, sockets |
3946 | trace "reattaching attachments to maps"; |
3991 | trace "perl_reload: reattaching attachments to maps"; |
3947 | reattach $_ for values %MAP; |
3992 | reattach $_ for values %MAP; |
3948 | trace "reattaching attachments to players"; |
3993 | trace "perl_reload: reattaching attachments to players"; |
3949 | reattach $_ for values %PLAYER; |
3994 | reattach $_ for values %PLAYER; |
3950 | } |
3995 | } |
3951 | |
3996 | |
3952 | trace "running post_init jobs"; |
3997 | cf::_post_init 1; |
3953 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3954 | |
3998 | |
3955 | trace "leaving sync_job"; |
3999 | trace "perl_reload: leaving sync_job"; |
3956 | |
4000 | |
3957 | 1 |
4001 | 1 |
3958 | } or do { |
4002 | } or do { |
3959 | error $@; |
4003 | error $@; |
3960 | cf::cleanup "error while reloading, exiting."; |
4004 | cf::cleanup "perl_reload: error, exiting."; |
3961 | }; |
4005 | }; |
3962 | |
4006 | |
3963 | info "reloaded"; |
|
|
3964 | --$RELOAD; |
4007 | --$RELOAD; |
3965 | } |
4008 | } |
3966 | |
4009 | |
3967 | $t1 = AE::time - $t1; |
4010 | $t1 = AE::time - $t1; |
3968 | info "reload completed in ${t1}s\n"; |
4011 | info "perl_reload: completed in ${t1}s\n"; |
3969 | }; |
4012 | }; |
3970 | |
4013 | |
3971 | our $RELOAD_WATCHER; # used only during reload |
4014 | our $RELOAD_WATCHER; # used only during reload |
3972 | |
4015 | |
3973 | sub reload_perl() { |
4016 | sub reload_perl() { |
… | |
… | |
3994 | reload_perl; |
4037 | reload_perl; |
3995 | }; |
4038 | }; |
3996 | } |
4039 | } |
3997 | }; |
4040 | }; |
3998 | |
4041 | |
3999 | unshift @INC, $LIBDIR; |
4042 | ############################################################################# |
4000 | |
4043 | |
4001 | my $bug_warning = 0; |
4044 | my $bug_warning = 0; |
4002 | |
4045 | |
4003 | our @WAIT_FOR_TICK; |
4046 | our @WAIT_FOR_TICK; |
4004 | our @WAIT_FOR_TICK_BEGIN; |
4047 | our @WAIT_FOR_TICK_BEGIN; |
4005 | |
4048 | |
4006 | sub wait_for_tick { |
4049 | sub wait_for_tick() { |
4007 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4050 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4008 | |
4051 | |
4009 | my $signal = new Coro::Signal; |
4052 | my $signal = new Coro::Signal; |
4010 | push @WAIT_FOR_TICK, $signal; |
4053 | push @WAIT_FOR_TICK, $signal; |
4011 | $signal->wait; |
4054 | $signal->wait; |
4012 | } |
4055 | } |
4013 | |
4056 | |
4014 | sub wait_for_tick_begin { |
4057 | sub wait_for_tick_begin() { |
4015 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4058 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4016 | |
4059 | |
4017 | my $signal = new Coro::Signal; |
4060 | my $signal = new Coro::Signal; |
4018 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4061 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4019 | $signal->wait; |
4062 | $signal->wait; |