… | |
… | |
357 | 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 |
358 | |
358 | |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
361 | |
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 | |
362 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
363 | |
375 | |
364 | 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, |
365 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
366 | |
378 | |
367 | 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 |
368 | is a reload or not. |
380 | is a reload or not. |
369 | |
381 | |
370 | =cut |
382 | =cut |
371 | |
383 | |
372 | sub post_init(&) { |
384 | sub post_init(&) { |
373 | 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 | } |
374 | } |
403 | } |
375 | |
404 | |
376 | =item cf::lock_wait $string |
405 | =item cf::lock_wait $string |
377 | |
406 | |
378 | Wait until the given lock is available. See cf::lock_acquire. |
407 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
583 | $EXT_CORO{$coro+0} = $coro; |
612 | $EXT_CORO{$coro+0} = $coro; |
584 | |
613 | |
585 | $coro |
614 | $coro |
586 | } |
615 | } |
587 | |
616 | |
588 | =item fork_call { }, $args |
617 | =item fork_call { }, @args |
589 | |
618 | |
590 | 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 |
591 | process, returning the results. Everything must be serialisable with |
620 | process, returning the results. Everything must be serialisable with |
592 | 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 |
593 | never block itself or use any form of event handling. |
622 | never block itself or use any form of event handling. |
594 | |
623 | |
595 | =cut |
624 | =cut |
596 | |
625 | |
|
|
626 | sub post_fork { |
|
|
627 | reset_signals; |
|
|
628 | } |
|
|
629 | |
597 | sub fork_call(&@) { |
630 | sub fork_call(&@) { |
598 | my ($cb, @args) = @_; |
631 | my ($cb, @args) = @_; |
599 | |
632 | |
600 | # 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, |
601 | # otherwise perl prematurely frees the stuff :/ |
634 | # otherwise perl prematurely frees the stuff :/ |
602 | # TODO: investigate and fix (likely this will be rather laborious) |
635 | # TODO: investigate and fix (likely this will be rather laborious) |
603 | |
636 | |
604 | my @res = Coro::Util::fork_eval { |
637 | my @res = Coro::Util::fork_eval { |
605 | reset_signals; |
638 | cf::post_fork; |
606 | &$cb |
639 | &$cb |
607 | }, @args; |
640 | } @args; |
608 | |
641 | |
609 | wantarray ? @res : $res[-1] |
642 | wantarray ? @res : $res[-1] |
610 | } |
643 | } |
611 | |
644 | |
612 | sub objinfo { |
645 | sub objinfo { |
… | |
… | |
1646 | my $name = $pl->ob->name; |
1679 | my $name = $pl->ob->name; |
1647 | |
1680 | |
1648 | $pl->{deny_save} = 1; |
1681 | $pl->{deny_save} = 1; |
1649 | $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 |
1650 | |
1683 | |
1651 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1684 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1652 | $pl->deactivate; |
1685 | $pl->deactivate; |
|
|
1686 | |
1653 | 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; |
1654 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1688 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1689 | ext::highscore::check ($pl->ob); |
|
|
1690 | |
1655 | $pl->ns->destroy if $pl->ns; |
1691 | $pl->ns->destroy if $pl->ns; |
1656 | |
1692 | |
1657 | my $path = playerdir $pl; |
1693 | my $path = playerdir $pl; |
1658 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1694 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1659 | aio_rename $path, $temp; |
1695 | aio_rename $path, $temp; |
… | |
… | |
1825 | sub generate_random_map { |
1861 | sub generate_random_map { |
1826 | my ($self, $rmp) = @_; |
1862 | my ($self, $rmp) = @_; |
1827 | |
1863 | |
1828 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1864 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1829 | |
1865 | |
1830 | # mit "rum" bekleckern, nicht |
|
|
1831 | $self->_create_random_map ( |
1866 | $self->_create_random_map ($rmp); |
1832 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
|
|
1833 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
|
|
1834 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
|
|
1835 | $rmp->{exit_on_final_map}, |
|
|
1836 | $rmp->{xsize}, $rmp->{ysize}, |
|
|
1837 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
|
|
1838 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
|
|
1839 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
|
|
1840 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
|
|
1841 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
|
|
1842 | (cf::region::find $rmp->{region}), $rmp->{custom} |
|
|
1843 | ) |
|
|
1844 | } |
1867 | } |
1845 | |
1868 | |
1846 | =item cf::map->register ($regex, $prio) |
1869 | =item cf::map->register ($regex, $prio) |
1847 | |
1870 | |
1848 | Register a handler for the map path matching the given regex at the |
1871 | Register a handler for the map path matching the given regex at the |
… | |
… | |
1899 | $base =~ s{[^/]+/?$}{}; |
1922 | $base =~ s{[^/]+/?$}{}; |
1900 | $path = "$base/$path"; |
1923 | $path = "$base/$path"; |
1901 | } |
1924 | } |
1902 | |
1925 | |
1903 | for ($path) { |
1926 | for ($path) { |
1904 | redo if s{//}{/}; |
|
|
1905 | redo if s{/\.?/}{/}; |
1927 | redo if s{/\.?/}{/}; |
1906 | redo if s{/[^/]+/\.\./}{/}; |
1928 | redo if s{/[^/]+/\.\./}{/}; |
1907 | } |
1929 | } |
1908 | |
1930 | |
1909 | $path |
1931 | $path |
… | |
… | |
2151 | |
2173 | |
2152 | $self->in_memory (cf::MAP_ACTIVE); |
2174 | $self->in_memory (cf::MAP_ACTIVE); |
2153 | } |
2175 | } |
2154 | |
2176 | |
2155 | $self->post_load; |
2177 | $self->post_load; |
|
|
2178 | |
|
|
2179 | 1 |
2156 | } |
2180 | } |
2157 | |
2181 | |
2158 | # customize the map for a given player, i.e. |
2182 | # customize the map for a given player, i.e. |
2159 | # return the _real_ map. used by e.g. per-player |
2183 | # return the _real_ map. used by e.g. per-player |
2160 | # maps to change the path to ~playername/mappath |
2184 | # maps to change the path to ~playername/mappath |
… | |
… | |
3547 | |
3571 | |
3548 | 0 < aio_load "$CONFDIR/config", my $config |
3572 | 0 < aio_load "$CONFDIR/config", my $config |
3549 | or die "$CONFDIR/config: $!"; |
3573 | or die "$CONFDIR/config: $!"; |
3550 | |
3574 | |
3551 | utf8::decode $config; |
3575 | utf8::decode $config; |
3552 | |
3576 | *CFG = yaml_load $config; |
3553 | cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow... |
|
|
3554 | *CFG = YAML::XS::Load $config; |
|
|
3555 | |
3577 | |
3556 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3578 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3557 | |
3579 | |
3558 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3580 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3559 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3581 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3643 | |
3665 | |
3644 | # no (long-running) fork's whatsoever before this point(!) |
3666 | # no (long-running) fork's whatsoever before this point(!) |
3645 | use POSIX (); |
3667 | use POSIX (); |
3646 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3668 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3647 | |
3669 | |
3648 | (pop @POST_INIT)->(0) while @POST_INIT; |
3670 | cf::_post_init 0; |
3649 | }; |
3671 | }; |
3650 | |
3672 | |
3651 | cf::object::thawer::errors_are_fatal 0; |
3673 | cf::object::thawer::errors_are_fatal 0; |
3652 | info "parse errors in files are no longer fatal from this point on.\n"; |
3674 | info "parse errors in files are no longer fatal from this point on.\n"; |
3653 | |
3675 | |
… | |
… | |
3876 | |
3898 | |
3877 | my $t1 = AE::time; |
3899 | my $t1 = AE::time; |
3878 | |
3900 | |
3879 | while ($RELOAD) { |
3901 | while ($RELOAD) { |
3880 | cf::get_slot 0.1, -1, "reload_perl"; |
3902 | cf::get_slot 0.1, -1, "reload_perl"; |
3881 | info "reloading..."; |
3903 | info "perl_reload: reloading..."; |
3882 | |
3904 | |
3883 | trace "entering sync_job"; |
3905 | trace "perl_reload: entering sync_job"; |
3884 | |
3906 | |
3885 | cf::sync_job { |
3907 | cf::sync_job { |
3886 | #cf::emergency_save; |
3908 | #cf::emergency_save; |
3887 | |
3909 | |
3888 | trace "cancelling all extension coros"; |
3910 | trace "perl_reload: cancelling all extension coros"; |
3889 | $_->cancel for values %EXT_CORO; |
3911 | $_->cancel for values %EXT_CORO; |
3890 | %EXT_CORO = (); |
3912 | %EXT_CORO = (); |
3891 | |
3913 | |
3892 | trace "removing commands"; |
3914 | trace "perl_reload: removing commands"; |
3893 | %COMMAND = (); |
3915 | %COMMAND = (); |
3894 | |
3916 | |
3895 | trace "removing ext/exti commands"; |
3917 | trace "perl_reload: removing ext/exti commands"; |
3896 | %EXTCMD = (); |
3918 | %EXTCMD = (); |
3897 | %EXTICMD = (); |
3919 | %EXTICMD = (); |
3898 | |
3920 | |
3899 | trace "unloading/nuking all extensions"; |
3921 | trace "perl_reload: unloading/nuking all extensions"; |
3900 | for my $pkg (@EXTS) { |
3922 | for my $pkg (@EXTS) { |
3901 | trace "... unloading $pkg"; |
3923 | trace "... unloading $pkg"; |
3902 | |
3924 | |
3903 | if (my $cb = $pkg->can ("unload")) { |
3925 | if (my $cb = $pkg->can ("unload")) { |
3904 | eval { |
3926 | eval { |
… | |
… | |
3909 | |
3931 | |
3910 | trace "... clearing $pkg"; |
3932 | trace "... clearing $pkg"; |
3911 | clear_package $pkg; |
3933 | clear_package $pkg; |
3912 | } |
3934 | } |
3913 | |
3935 | |
3914 | trace "unloading all perl modules loaded from $LIBDIR"; |
3936 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3915 | while (my ($k, $v) = each %INC) { |
3937 | while (my ($k, $v) = each %INC) { |
3916 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3938 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3917 | |
3939 | |
3918 | trace "... unloading $k"; |
3940 | trace "... unloading $k"; |
3919 | delete $INC{$k}; |
3941 | delete $INC{$k}; |
… | |
… | |
3926 | } |
3948 | } |
3927 | |
3949 | |
3928 | clear_package $k; |
3950 | clear_package $k; |
3929 | } |
3951 | } |
3930 | |
3952 | |
3931 | trace "getting rid of safe::, as good as possible"; |
3953 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3932 | clear_package "safe::$_" |
3954 | clear_package "safe::$_" |
3933 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3955 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3934 | |
3956 | |
3935 | trace "unloading cf.pm \"a bit\""; |
3957 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3936 | delete $INC{"cf.pm"}; |
3958 | delete $INC{"cf.pm"}; |
3937 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3959 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3938 | |
3960 | |
3939 | # don't, removes xs symbols, too, |
3961 | # don't, removes xs symbols, too, |
3940 | # and global variables created in xs |
3962 | # and global variables created in xs |
3941 | #clear_package __PACKAGE__; |
3963 | #clear_package __PACKAGE__; |
3942 | |
3964 | |
3943 | info "unload completed, starting to reload now"; |
3965 | info "perl_reload: unload completed, starting to reload now"; |
3944 | |
3966 | |
3945 | trace "reloading cf.pm"; |
3967 | trace "perl_reload: reloading cf.pm"; |
3946 | require cf; |
3968 | require cf; |
3947 | cf::_connect_to_perl_1; |
3969 | cf::_connect_to_perl_1; |
3948 | |
3970 | |
3949 | trace "loading config and database again"; |
3971 | trace "perl_reload: loading config and database again"; |
3950 | cf::reload_config; |
3972 | cf::reload_config; |
3951 | |
3973 | |
3952 | trace "loading extensions"; |
3974 | trace "perl_reload: loading extensions"; |
3953 | cf::load_extensions; |
3975 | cf::load_extensions; |
3954 | |
3976 | |
3955 | if ($REATTACH_ON_RELOAD) { |
3977 | if ($REATTACH_ON_RELOAD) { |
3956 | trace "reattaching attachments to objects/players"; |
3978 | trace "perl_reload: reattaching attachments to objects/players"; |
3957 | _global_reattach; # objects, sockets |
3979 | _global_reattach; # objects, sockets |
3958 | trace "reattaching attachments to maps"; |
3980 | trace "perl_reload: reattaching attachments to maps"; |
3959 | reattach $_ for values %MAP; |
3981 | reattach $_ for values %MAP; |
3960 | trace "reattaching attachments to players"; |
3982 | trace "perl_reload: reattaching attachments to players"; |
3961 | reattach $_ for values %PLAYER; |
3983 | reattach $_ for values %PLAYER; |
3962 | } |
3984 | } |
3963 | |
3985 | |
3964 | trace "running post_init jobs"; |
3986 | cf::_post_init 1; |
3965 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3966 | |
3987 | |
3967 | trace "leaving sync_job"; |
3988 | trace "perl_reload: leaving sync_job"; |
3968 | |
3989 | |
3969 | 1 |
3990 | 1 |
3970 | } or do { |
3991 | } or do { |
3971 | error $@; |
3992 | error $@; |
3972 | cf::cleanup "error while reloading, exiting."; |
3993 | cf::cleanup "perl_reload: error, exiting."; |
3973 | }; |
3994 | }; |
3974 | |
3995 | |
3975 | info "reloaded"; |
|
|
3976 | --$RELOAD; |
3996 | --$RELOAD; |
3977 | } |
3997 | } |
3978 | |
3998 | |
3979 | $t1 = AE::time - $t1; |
3999 | $t1 = AE::time - $t1; |
3980 | info "reload completed in ${t1}s\n"; |
4000 | info "perl_reload: completed in ${t1}s\n"; |
3981 | }; |
4001 | }; |
3982 | |
4002 | |
3983 | our $RELOAD_WATCHER; # used only during reload |
4003 | our $RELOAD_WATCHER; # used only during reload |
3984 | |
4004 | |
3985 | sub reload_perl() { |
4005 | sub reload_perl() { |
… | |
… | |
4118 | IO::AIO::min_parallel 8; |
4138 | IO::AIO::min_parallel 8; |
4119 | IO::AIO::max_poll_time $TICK * 0.1; |
4139 | IO::AIO::max_poll_time $TICK * 0.1; |
4120 | undef $AnyEvent::AIO::WATCHER; |
4140 | undef $AnyEvent::AIO::WATCHER; |
4121 | } |
4141 | } |
4122 | |
4142 | |
4123 | my $_log_backtrace; |
4143 | our $_log_backtrace; |
|
|
4144 | our $_log_backtrace_last; |
4124 | |
4145 | |
4125 | sub _log_backtrace { |
4146 | sub _log_backtrace { |
4126 | my ($msg, @addr) = @_; |
4147 | my ($msg, @addr) = @_; |
4127 | |
4148 | |
4128 | $msg =~ s/\n//; |
4149 | $msg =~ s/\n$//; |
4129 | |
4150 | |
|
|
4151 | if ($_log_backtrace_last eq $msg) { |
|
|
4152 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
4153 | LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; |
4130 | # limit the # of concurrent backtraces |
4154 | # limit the # of concurrent backtraces |
4131 | if ($_log_backtrace < 2) { |
4155 | } elsif ($_log_backtrace < 2) { |
|
|
4156 | $_log_backtrace_last = $msg; |
4132 | ++$_log_backtrace; |
4157 | ++$_log_backtrace; |
4133 | my $perl_bt = Carp::longmess $msg; |
4158 | my $perl_bt = Carp::longmess $msg; |
4134 | async { |
4159 | async { |
4135 | $Coro::current->{desc} = "abt $msg"; |
4160 | $Coro::current->{desc} = "abt $msg"; |
4136 | |
4161 | |
… | |
… | |
4156 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4181 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4157 | --$_log_backtrace; |
4182 | --$_log_backtrace; |
4158 | }; |
4183 | }; |
4159 | } else { |
4184 | } else { |
4160 | LOG llevInfo, "[ABT] $msg\n"; |
4185 | LOG llevInfo, "[ABT] $msg\n"; |
4161 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4186 | LOG llevInfo, "[ABT] [overload, suppressed]\n"; |
4162 | } |
4187 | } |
4163 | } |
4188 | } |
4164 | |
4189 | |
4165 | # load additional modules |
4190 | # load additional modules |
4166 | require "cf/$_.pm" for @EXTRA_MODULES; |
4191 | require "cf/$_.pm" for @EXTRA_MODULES; |