… | |
… | |
76 | # strictly for debugging |
76 | # strictly for debugging |
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
78 | |
78 | |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
80 | |
80 | |
|
|
81 | our @ORIG_INC; |
|
|
82 | |
81 | our %COMMAND = (); |
83 | our %COMMAND = (); |
82 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
83 | |
85 | |
84 | our @EXTS = (); # list of extension package names |
86 | our @EXTS = (); # list of extension package names |
85 | our %EXTCMD = (); |
87 | our %EXTCMD = (); |
… | |
… | |
122 | our $BDB_DEADLOCK_WATCHER; |
124 | our $BDB_DEADLOCK_WATCHER; |
123 | our $BDB_CHECKPOINT_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
124 | our $BDB_TRICKLE_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
125 | our $DB_ENV; |
127 | our $DB_ENV; |
126 | |
128 | |
127 | our @EXTRA_MODULES = qw(pod match mapscript); |
129 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
128 | |
130 | |
129 | our %CFG; |
131 | our %CFG; |
130 | |
132 | |
131 | our $UPTIME; $UPTIME ||= time; |
133 | our $UPTIME; $UPTIME ||= time; |
132 | our $RUNTIME; |
134 | our $RUNTIME; |
… | |
… | |
231 | from wherever your confdir points to. |
233 | from wherever your confdir points to. |
232 | |
234 | |
233 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
234 | |
236 | |
235 | 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 |
236 | 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 |
237 | 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(); |
238 | |
245 | |
239 | =item @cf::INVOKE_RESULTS |
246 | =item @cf::INVOKE_RESULTS |
240 | |
247 | |
241 | 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 |
242 | 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 |
… | |
… | |
350 | 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 |
351 | |
358 | |
352 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
353 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
354 | |
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 | |
355 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
356 | |
375 | |
357 | 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, |
358 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
359 | |
378 | |
360 | 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 |
361 | is a reload or not. |
380 | is a reload or not. |
362 | |
381 | |
363 | =cut |
382 | =cut |
364 | |
383 | |
365 | sub post_init(&) { |
384 | sub post_init(&) { |
366 | 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 | } |
367 | } |
403 | } |
368 | |
404 | |
369 | =item cf::lock_wait $string |
405 | =item cf::lock_wait $string |
370 | |
406 | |
371 | Wait until the given lock is available. See cf::lock_acquire. |
407 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
576 | $EXT_CORO{$coro+0} = $coro; |
612 | $EXT_CORO{$coro+0} = $coro; |
577 | |
613 | |
578 | $coro |
614 | $coro |
579 | } |
615 | } |
580 | |
616 | |
581 | =item fork_call { }, $args |
617 | =item fork_call { }, @args |
582 | |
618 | |
583 | 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 |
584 | process, returning the results. Everything must be serialisable with |
620 | process, returning the results. Everything must be serialisable with |
585 | 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 |
586 | never block itself or use any form of event handling. |
622 | never block itself or use any form of event handling. |
587 | |
623 | |
588 | =cut |
624 | =cut |
589 | |
625 | |
|
|
626 | sub post_fork { |
|
|
627 | reset_signals; |
|
|
628 | } |
|
|
629 | |
590 | sub fork_call(&@) { |
630 | sub fork_call(&@) { |
591 | my ($cb, @args) = @_; |
631 | my ($cb, @args) = @_; |
592 | |
632 | |
593 | # 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, |
594 | # otherwise perl prematurely frees the stuff :/ |
634 | # otherwise perl prematurely frees the stuff :/ |
595 | # TODO: investigate and fix (likely this will be rather laborious) |
635 | # TODO: investigate and fix (likely this will be rather laborious) |
596 | |
636 | |
597 | my @res = Coro::Util::fork_eval { |
637 | my @res = Coro::Util::fork_eval { |
598 | reset_signals; |
638 | cf::post_fork; |
599 | &$cb |
639 | &$cb |
600 | }, @args; |
640 | } @args; |
601 | |
641 | |
602 | wantarray ? @res : $res[-1] |
642 | wantarray ? @res : $res[-1] |
603 | } |
643 | } |
604 | |
644 | |
605 | sub objinfo { |
645 | sub objinfo { |
… | |
… | |
1449 | |
1489 | |
1450 | my $active = eval $v->{source}; |
1490 | my $active = eval $v->{source}; |
1451 | |
1491 | |
1452 | if (length $@) { |
1492 | if (length $@) { |
1453 | error "$v->{path}: $@\n"; |
1493 | error "$v->{path}: $@\n"; |
1454 | undef $@; # work around perl 5.10.0 utf-8 caching bug |
|
|
1455 | |
1494 | |
1456 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1495 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1457 | if exists $v->{meta}{mandatory}; |
1496 | if exists $v->{meta}{mandatory}; |
1458 | |
1497 | |
1459 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
1498 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
… | |
… | |
1640 | my $name = $pl->ob->name; |
1679 | my $name = $pl->ob->name; |
1641 | |
1680 | |
1642 | $pl->{deny_save} = 1; |
1681 | $pl->{deny_save} = 1; |
1643 | $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 |
1644 | |
1683 | |
1645 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1684 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1646 | $pl->deactivate; |
1685 | $pl->deactivate; |
|
|
1686 | |
1647 | 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; |
1648 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1688 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1689 | ext::highscore::check ($pl->ob); |
|
|
1690 | |
1649 | $pl->ns->destroy if $pl->ns; |
1691 | $pl->ns->destroy if $pl->ns; |
1650 | |
1692 | |
1651 | my $path = playerdir $pl; |
1693 | my $path = playerdir $pl; |
1652 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1694 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1653 | aio_rename $path, $temp; |
1695 | aio_rename $path, $temp; |
… | |
… | |
1868 | } |
1910 | } |
1869 | |
1911 | |
1870 | sub normalise { |
1912 | sub normalise { |
1871 | my ($path, $base) = @_; |
1913 | my ($path, $base) = @_; |
1872 | |
1914 | |
1873 | $path = "$path"; # make sure its a string |
1915 | $path = "$path"; # make sure it's a string |
1874 | |
1916 | |
1875 | $path =~ s/\.map$//; |
1917 | $path =~ s/\.map$//; |
1876 | |
1918 | |
1877 | # map plan: |
1919 | # map plan: |
1878 | # |
1920 | # |
… | |
… | |
1893 | $base =~ s{[^/]+/?$}{}; |
1935 | $base =~ s{[^/]+/?$}{}; |
1894 | $path = "$base/$path"; |
1936 | $path = "$base/$path"; |
1895 | } |
1937 | } |
1896 | |
1938 | |
1897 | for ($path) { |
1939 | for ($path) { |
1898 | redo if s{//}{/}; |
|
|
1899 | redo if s{/\.?/}{/}; |
1940 | redo if s{/\.?/}{/}; |
1900 | redo if s{/[^/]+/\.\./}{/}; |
1941 | redo if s{/[^/]+/\.\./}{/}; |
1901 | } |
1942 | } |
1902 | |
1943 | |
1903 | $path |
1944 | $path |
… | |
… | |
1917 | $self->init; # pass $1 etc. |
1958 | $self->init; # pass $1 etc. |
1918 | return $self; |
1959 | return $self; |
1919 | } |
1960 | } |
1920 | } |
1961 | } |
1921 | |
1962 | |
1922 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1963 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1923 | () |
1964 | () |
1924 | } |
1965 | } |
1925 | |
1966 | |
1926 | sub init { |
1967 | sub init { |
1927 | my ($self) = @_; |
1968 | my ($self) = @_; |
… | |
… | |
2049 | } |
2090 | } |
2050 | |
2091 | |
2051 | sub find; |
2092 | sub find; |
2052 | sub find { |
2093 | sub find { |
2053 | my ($path, $origin) = @_; |
2094 | my ($path, $origin) = @_; |
|
|
2095 | |
|
|
2096 | cf::cede_to_tick; |
2054 | |
2097 | |
2055 | $path = normalise $path, $origin && $origin->path; |
2098 | $path = normalise $path, $origin && $origin->path; |
2056 | |
2099 | |
2057 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2100 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2058 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2101 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
2143 | |
2186 | |
2144 | $self->in_memory (cf::MAP_ACTIVE); |
2187 | $self->in_memory (cf::MAP_ACTIVE); |
2145 | } |
2188 | } |
2146 | |
2189 | |
2147 | $self->post_load; |
2190 | $self->post_load; |
|
|
2191 | |
|
|
2192 | 1 |
2148 | } |
2193 | } |
2149 | |
2194 | |
2150 | # customize the map for a given player, i.e. |
2195 | # customize the map for a given player, i.e. |
2151 | # return the _real_ map. used by e.g. per-player |
2196 | # return the _real_ map. used by e.g. per-player |
2152 | # maps to change the path to ~playername/mappath |
2197 | # maps to change the path to ~playername/mappath |
… | |
… | |
3367 | |
3412 | |
3368 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3413 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3369 | |
3414 | |
3370 | trace "loading facedata from $path\n"; |
3415 | trace "loading facedata from $path\n"; |
3371 | |
3416 | |
3372 | my $facedata; |
|
|
3373 | 0 < aio_load $path, $facedata |
3417 | 0 < aio_load $path, my $facedata |
3374 | or die "$path: $!"; |
3418 | or die "$path: $!"; |
3375 | |
3419 | |
3376 | $facedata = Coro::Storable::thaw $facedata; |
3420 | $facedata = Coro::Storable::thaw $facedata; |
3377 | |
3421 | |
3378 | $facedata->{version} == 2 |
3422 | $facedata->{version} == 2 |
… | |
… | |
3522 | } |
3566 | } |
3523 | |
3567 | |
3524 | sub reload_resources { |
3568 | sub reload_resources { |
3525 | trace "reloading resource files...\n"; |
3569 | trace "reloading resource files...\n"; |
3526 | |
3570 | |
|
|
3571 | reload_exp_table; |
|
|
3572 | reload_materials; |
3527 | reload_facedata; |
3573 | reload_facedata; |
3528 | reload_sound; |
3574 | reload_sound; |
3529 | reload_archetypes; |
3575 | reload_archetypes; |
3530 | reload_regions; |
3576 | reload_regions; |
3531 | reload_treasures; |
3577 | reload_treasures; |
… | |
… | |
3534 | } |
3580 | } |
3535 | |
3581 | |
3536 | sub reload_config { |
3582 | sub reload_config { |
3537 | trace "reloading config file...\n"; |
3583 | trace "reloading config file...\n"; |
3538 | |
3584 | |
3539 | open my $fh, "<:utf8", "$CONFDIR/config" |
3585 | 0 < aio_load "$CONFDIR/config", my $config |
3540 | or return; |
3586 | or die "$CONFDIR/config: $!"; |
3541 | |
3587 | |
3542 | local $/; |
3588 | utf8::decode $config; |
3543 | *CFG = YAML::XS::Load scalar <$fh>; |
3589 | *CFG = yaml_load $config; |
3544 | |
3590 | |
3545 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3591 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3546 | |
3592 | |
3547 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3593 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3548 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3594 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3552 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3598 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3553 | and die "WARNING: m(un)lockall failed: $!\n"; |
3599 | and die "WARNING: m(un)lockall failed: $!\n"; |
3554 | }; |
3600 | }; |
3555 | warn $@ if $@; |
3601 | warn $@ if $@; |
3556 | } |
3602 | } |
3557 | |
|
|
3558 | trace "finished reloading resource files\n"; |
|
|
3559 | } |
3603 | } |
3560 | |
3604 | |
3561 | sub pidfile() { |
3605 | sub pidfile() { |
3562 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3606 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3563 | or die "$PIDFILE: $!"; |
3607 | or die "$PIDFILE: $!"; |
… | |
… | |
3588 | |
3632 | |
3589 | sub main { |
3633 | sub main { |
3590 | cf::init_globals; # initialise logging |
3634 | cf::init_globals; # initialise logging |
3591 | |
3635 | |
3592 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3636 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3593 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3637 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3594 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3638 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3595 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3639 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3596 | |
3640 | |
3597 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3641 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3598 | |
3642 | |
… | |
… | |
3606 | }; |
3650 | }; |
3607 | |
3651 | |
3608 | evthread_start IO::AIO::poll_fileno; |
3652 | evthread_start IO::AIO::poll_fileno; |
3609 | |
3653 | |
3610 | cf::sync_job { |
3654 | cf::sync_job { |
3611 | cf::init_experience; |
3655 | cf::incloader::init (); |
|
|
3656 | |
3612 | cf::init_anim; |
3657 | cf::init_anim; |
3613 | cf::init_attackmess; |
3658 | cf::init_attackmess; |
3614 | cf::init_dynamic; |
3659 | cf::init_dynamic; |
3615 | |
3660 | |
3616 | cf::load_settings; |
3661 | cf::load_settings; |
3617 | cf::load_materials; |
|
|
3618 | |
3662 | |
3619 | reload_resources; |
3663 | reload_resources; |
3620 | reload_config; |
3664 | reload_config; |
3621 | db_init; |
3665 | db_init; |
3622 | |
3666 | |
… | |
… | |
3634 | |
3678 | |
3635 | # no (long-running) fork's whatsoever before this point(!) |
3679 | # no (long-running) fork's whatsoever before this point(!) |
3636 | use POSIX (); |
3680 | use POSIX (); |
3637 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3681 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3638 | |
3682 | |
3639 | (pop @POST_INIT)->(0) while @POST_INIT; |
3683 | cf::_post_init 0; |
3640 | }; |
3684 | }; |
3641 | |
3685 | |
3642 | cf::object::thawer::errors_are_fatal 0; |
3686 | cf::object::thawer::errors_are_fatal 0; |
3643 | info "parse errors in files are no longer fatal from this point on.\n"; |
3687 | info "parse errors in files are no longer fatal from this point on.\n"; |
3644 | |
3688 | |
|
|
3689 | my $free_main; $free_main = EV::idle sub { |
|
|
3690 | undef $free_main; |
|
|
3691 | undef &main; # free gobs of memory :) |
|
|
3692 | }; |
|
|
3693 | |
3645 | main_loop; |
3694 | goto &main_loop; |
3646 | } |
3695 | } |
3647 | |
3696 | |
3648 | ############################################################################# |
3697 | ############################################################################# |
3649 | # initialisation and cleanup |
3698 | # initialisation and cleanup |
3650 | |
3699 | |
… | |
… | |
3851 | } |
3900 | } |
3852 | } |
3901 | } |
3853 | |
3902 | |
3854 | sub do_reload_perl() { |
3903 | sub do_reload_perl() { |
3855 | # can/must only be called in main |
3904 | # can/must only be called in main |
3856 | if (in_main) { |
3905 | unless (in_main) { |
3857 | error "can only reload from main coroutine"; |
3906 | error "can only reload from main coroutine"; |
3858 | return; |
3907 | return; |
3859 | } |
3908 | } |
3860 | |
3909 | |
3861 | return if $RELOAD++; |
3910 | return if $RELOAD++; |
3862 | |
3911 | |
3863 | my $t1 = AE::time; |
3912 | my $t1 = AE::time; |
3864 | |
3913 | |
3865 | while ($RELOAD) { |
3914 | while ($RELOAD) { |
|
|
3915 | cf::get_slot 0.1, -1, "reload_perl"; |
3866 | info "reloading..."; |
3916 | info "perl_reload: reloading..."; |
3867 | |
3917 | |
3868 | trace "entering sync_job"; |
3918 | trace "perl_reload: entering sync_job"; |
3869 | |
3919 | |
3870 | cf::sync_job { |
3920 | cf::sync_job { |
3871 | cf::emergency_save; |
3921 | #cf::emergency_save; |
3872 | |
3922 | |
3873 | trace "cancelling all extension coros"; |
3923 | trace "perl_reload: cancelling all extension coros"; |
3874 | $_->cancel for values %EXT_CORO; |
3924 | $_->cancel for values %EXT_CORO; |
3875 | %EXT_CORO = (); |
3925 | %EXT_CORO = (); |
3876 | |
3926 | |
3877 | trace "removing commands"; |
3927 | trace "perl_reload: removing commands"; |
3878 | %COMMAND = (); |
3928 | %COMMAND = (); |
3879 | |
3929 | |
3880 | trace "removing ext/exti commands"; |
3930 | trace "perl_reload: removing ext/exti commands"; |
3881 | %EXTCMD = (); |
3931 | %EXTCMD = (); |
3882 | %EXTICMD = (); |
3932 | %EXTICMD = (); |
3883 | |
3933 | |
3884 | trace "unloading/nuking all extensions"; |
3934 | trace "perl_reload: unloading/nuking all extensions"; |
3885 | for my $pkg (@EXTS) { |
3935 | for my $pkg (@EXTS) { |
3886 | trace "... unloading $pkg"; |
3936 | trace "... unloading $pkg"; |
3887 | |
3937 | |
3888 | if (my $cb = $pkg->can ("unload")) { |
3938 | if (my $cb = $pkg->can ("unload")) { |
3889 | eval { |
3939 | eval { |
… | |
… | |
3894 | |
3944 | |
3895 | trace "... clearing $pkg"; |
3945 | trace "... clearing $pkg"; |
3896 | clear_package $pkg; |
3946 | clear_package $pkg; |
3897 | } |
3947 | } |
3898 | |
3948 | |
3899 | trace "unloading all perl modules loaded from $LIBDIR"; |
3949 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3900 | while (my ($k, $v) = each %INC) { |
3950 | while (my ($k, $v) = each %INC) { |
3901 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3951 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3902 | |
3952 | |
3903 | trace "... unloading $k"; |
3953 | trace "... unloading $k"; |
3904 | delete $INC{$k}; |
3954 | delete $INC{$k}; |
… | |
… | |
3911 | } |
3961 | } |
3912 | |
3962 | |
3913 | clear_package $k; |
3963 | clear_package $k; |
3914 | } |
3964 | } |
3915 | |
3965 | |
3916 | trace "getting rid of safe::, as good as possible"; |
3966 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3917 | clear_package "safe::$_" |
3967 | clear_package "safe::$_" |
3918 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3968 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3919 | |
3969 | |
3920 | trace "unloading cf.pm \"a bit\""; |
3970 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3921 | delete $INC{"cf.pm"}; |
3971 | delete $INC{"cf.pm"}; |
3922 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3972 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3923 | |
3973 | |
3924 | # don't, removes xs symbols, too, |
3974 | # don't, removes xs symbols, too, |
3925 | # and global variables created in xs |
3975 | # and global variables created in xs |
3926 | #clear_package __PACKAGE__; |
3976 | #clear_package __PACKAGE__; |
3927 | |
3977 | |
3928 | info "unload completed, starting to reload now"; |
3978 | info "perl_reload: unload completed, starting to reload now"; |
3929 | |
3979 | |
3930 | trace "reloading cf.pm"; |
3980 | trace "perl_reload: reloading cf.pm"; |
3931 | require cf; |
3981 | require cf; |
3932 | cf::_connect_to_perl_1; |
3982 | cf::_connect_to_perl_1; |
3933 | |
3983 | |
3934 | trace "loading config and database again"; |
3984 | trace "perl_reload: loading config and database again"; |
3935 | cf::reload_config; |
3985 | cf::reload_config; |
3936 | |
3986 | |
3937 | trace "loading extensions"; |
3987 | trace "perl_reload: loading extensions"; |
3938 | cf::load_extensions; |
3988 | cf::load_extensions; |
3939 | |
3989 | |
3940 | if ($REATTACH_ON_RELOAD) { |
3990 | if ($REATTACH_ON_RELOAD) { |
3941 | trace "reattaching attachments to objects/players"; |
3991 | trace "perl_reload: reattaching attachments to objects/players"; |
3942 | _global_reattach; # objects, sockets |
3992 | _global_reattach; # objects, sockets |
3943 | trace "reattaching attachments to maps"; |
3993 | trace "perl_reload: reattaching attachments to maps"; |
3944 | reattach $_ for values %MAP; |
3994 | reattach $_ for values %MAP; |
3945 | trace "reattaching attachments to players"; |
3995 | trace "perl_reload: reattaching attachments to players"; |
3946 | reattach $_ for values %PLAYER; |
3996 | reattach $_ for values %PLAYER; |
3947 | } |
3997 | } |
3948 | |
3998 | |
3949 | trace "running post_init jobs"; |
3999 | cf::_post_init 1; |
3950 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3951 | |
4000 | |
3952 | trace "leaving sync_job"; |
4001 | trace "perl_reload: leaving sync_job"; |
3953 | |
4002 | |
3954 | 1 |
4003 | 1 |
3955 | } or do { |
4004 | } or do { |
3956 | error $@; |
4005 | error $@; |
3957 | cf::cleanup "error while reloading, exiting."; |
4006 | cf::cleanup "perl_reload: error, exiting."; |
3958 | }; |
4007 | }; |
3959 | |
4008 | |
3960 | info "reloaded"; |
|
|
3961 | --$RELOAD; |
4009 | --$RELOAD; |
3962 | } |
4010 | } |
3963 | |
4011 | |
3964 | $t1 = AE::time - $t1; |
4012 | $t1 = AE::time - $t1; |
3965 | info "reload completed in ${t1}s\n"; |
4013 | info "perl_reload: completed in ${t1}s\n"; |
3966 | }; |
4014 | }; |
3967 | |
4015 | |
3968 | our $RELOAD_WATCHER; # used only during reload |
4016 | our $RELOAD_WATCHER; # used only during reload |
3969 | |
4017 | |
3970 | sub reload_perl() { |
4018 | sub reload_perl() { |
… | |
… | |
3991 | reload_perl; |
4039 | reload_perl; |
3992 | }; |
4040 | }; |
3993 | } |
4041 | } |
3994 | }; |
4042 | }; |
3995 | |
4043 | |
3996 | unshift @INC, $LIBDIR; |
4044 | ############################################################################# |
3997 | |
4045 | |
3998 | my $bug_warning = 0; |
4046 | my $bug_warning = 0; |
3999 | |
4047 | |
4000 | our @WAIT_FOR_TICK; |
4048 | our @WAIT_FOR_TICK; |
4001 | our @WAIT_FOR_TICK_BEGIN; |
4049 | our @WAIT_FOR_TICK_BEGIN; |
4002 | |
4050 | |
4003 | sub wait_for_tick { |
4051 | sub wait_for_tick() { |
4004 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4052 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4005 | |
4053 | |
4006 | my $signal = new Coro::Signal; |
4054 | my $signal = new Coro::Signal; |
4007 | push @WAIT_FOR_TICK, $signal; |
4055 | push @WAIT_FOR_TICK, $signal; |
4008 | $signal->wait; |
4056 | $signal->wait; |
4009 | } |
4057 | } |
4010 | |
4058 | |
4011 | sub wait_for_tick_begin { |
4059 | sub wait_for_tick_begin() { |
4012 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4060 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4013 | |
4061 | |
4014 | my $signal = new Coro::Signal; |
4062 | my $signal = new Coro::Signal; |
4015 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4063 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4016 | $signal->wait; |
4064 | $signal->wait; |
… | |
… | |
4103 | IO::AIO::min_parallel 8; |
4151 | IO::AIO::min_parallel 8; |
4104 | IO::AIO::max_poll_time $TICK * 0.1; |
4152 | IO::AIO::max_poll_time $TICK * 0.1; |
4105 | undef $AnyEvent::AIO::WATCHER; |
4153 | undef $AnyEvent::AIO::WATCHER; |
4106 | } |
4154 | } |
4107 | |
4155 | |
4108 | my $_log_backtrace; |
4156 | our $_log_backtrace; |
|
|
4157 | our $_log_backtrace_last; |
4109 | |
4158 | |
4110 | sub _log_backtrace { |
4159 | sub _log_backtrace { |
4111 | my ($msg, @addr) = @_; |
4160 | my ($msg, @addr) = @_; |
4112 | |
4161 | |
4113 | $msg =~ s/\n//; |
4162 | $msg =~ s/\n$//; |
4114 | |
4163 | |
|
|
4164 | if ($_log_backtrace_last eq $msg) { |
|
|
4165 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
4166 | LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; |
4115 | # limit the # of concurrent backtraces |
4167 | # limit the # of concurrent backtraces |
4116 | if ($_log_backtrace < 2) { |
4168 | } elsif ($_log_backtrace < 2) { |
|
|
4169 | $_log_backtrace_last = $msg; |
4117 | ++$_log_backtrace; |
4170 | ++$_log_backtrace; |
4118 | my $perl_bt = Carp::longmess $msg; |
4171 | my $perl_bt = Carp::longmess $msg; |
4119 | async { |
4172 | async { |
4120 | $Coro::current->{desc} = "abt $msg"; |
4173 | $Coro::current->{desc} = "abt $msg"; |
4121 | |
4174 | |
… | |
… | |
4141 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4194 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4142 | --$_log_backtrace; |
4195 | --$_log_backtrace; |
4143 | }; |
4196 | }; |
4144 | } else { |
4197 | } else { |
4145 | LOG llevInfo, "[ABT] $msg\n"; |
4198 | LOG llevInfo, "[ABT] $msg\n"; |
4146 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4199 | LOG llevInfo, "[ABT] [overload, suppressed]\n"; |
4147 | } |
4200 | } |
4148 | } |
4201 | } |
4149 | |
4202 | |
4150 | # load additional modules |
4203 | # load additional modules |
4151 | require "cf/$_.pm" for @EXTRA_MODULES; |
4204 | require "cf/$_.pm" for @EXTRA_MODULES; |