… | |
… | |
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 |
… | |
… | |
1423 | |
1428 | |
1424 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1429 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1425 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1430 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1426 | |
1431 | |
1427 | $ext{source} = |
1432 | $ext{source} = |
1428 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1433 | "package $pkg; use common::sense;\n" |
1429 | . "#line 1 \"$path\"\n{\n" |
1434 | . "#line 1 \"$path\"\n{\n" |
1430 | . $source |
1435 | . $source |
1431 | . "\n};\n1"; |
1436 | . "\n};\n1"; |
1432 | |
1437 | |
1433 | $todo{$base} = \%ext; |
1438 | $todo{$base} = \%ext; |
… | |
… | |
1848 | |
1853 | |
1849 | sub register { |
1854 | sub register { |
1850 | my (undef, $regex, $prio) = @_; |
1855 | my (undef, $regex, $prio) = @_; |
1851 | my $pkg = caller; |
1856 | my $pkg = caller; |
1852 | |
1857 | |
1853 | no strict; |
|
|
1854 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1858 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1855 | |
1859 | |
1856 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1860 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1857 | } |
1861 | } |
1858 | |
1862 | |
… | |
… | |
1870 | } |
1874 | } |
1871 | |
1875 | |
1872 | sub normalise { |
1876 | sub normalise { |
1873 | my ($path, $base) = @_; |
1877 | my ($path, $base) = @_; |
1874 | |
1878 | |
1875 | $path = "$path"; # make sure its a string |
1879 | $path = "$path"; # make sure it's a string |
1876 | |
1880 | |
1877 | $path =~ s/\.map$//; |
1881 | $path =~ s/\.map$//; |
1878 | |
1882 | |
1879 | # map plan: |
1883 | # map plan: |
1880 | # |
1884 | # |
… | |
… | |
1919 | $self->init; # pass $1 etc. |
1923 | $self->init; # pass $1 etc. |
1920 | return $self; |
1924 | return $self; |
1921 | } |
1925 | } |
1922 | } |
1926 | } |
1923 | |
1927 | |
1924 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1928 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1925 | () |
1929 | () |
1926 | } |
1930 | } |
1927 | |
1931 | |
1928 | sub init { |
1932 | sub init { |
1929 | my ($self) = @_; |
1933 | my ($self) = @_; |
… | |
… | |
2051 | } |
2055 | } |
2052 | |
2056 | |
2053 | sub find; |
2057 | sub find; |
2054 | sub find { |
2058 | sub find { |
2055 | my ($path, $origin) = @_; |
2059 | my ($path, $origin) = @_; |
|
|
2060 | |
|
|
2061 | cf::cede_to_tick; |
2056 | |
2062 | |
2057 | $path = normalise $path, $origin && $origin->path; |
2063 | $path = normalise $path, $origin && $origin->path; |
2058 | |
2064 | |
2059 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2065 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2060 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2066 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
3273 | decrease split destroy change_exp value msg lore send_msg)], |
3279 | decrease split destroy change_exp value msg lore send_msg)], |
3274 | ["cf::object::player" => qw(player)], |
3280 | ["cf::object::player" => qw(player)], |
3275 | ["cf::player" => qw(peaceful send_msg)], |
3281 | ["cf::player" => qw(peaceful send_msg)], |
3276 | ["cf::map" => qw(trigger)], |
3282 | ["cf::map" => qw(trigger)], |
3277 | ) { |
3283 | ) { |
3278 | no strict 'refs'; |
|
|
3279 | my ($pkg, @funs) = @$_; |
3284 | my ($pkg, @funs) = @$_; |
3280 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3285 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3281 | for @funs; |
3286 | for @funs; |
3282 | } |
3287 | } |
3283 | |
3288 | |
… | |
… | |
3370 | |
3375 | |
3371 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3376 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3372 | |
3377 | |
3373 | trace "loading facedata from $path\n"; |
3378 | trace "loading facedata from $path\n"; |
3374 | |
3379 | |
3375 | my $facedata; |
|
|
3376 | 0 < aio_load $path, $facedata |
3380 | 0 < aio_load $path, my $facedata |
3377 | or die "$path: $!"; |
3381 | or die "$path: $!"; |
3378 | |
3382 | |
3379 | $facedata = Coro::Storable::thaw $facedata; |
3383 | $facedata = Coro::Storable::thaw $facedata; |
3380 | |
3384 | |
3381 | $facedata->{version} == 2 |
3385 | $facedata->{version} == 2 |
… | |
… | |
3525 | } |
3529 | } |
3526 | |
3530 | |
3527 | sub reload_resources { |
3531 | sub reload_resources { |
3528 | trace "reloading resource files...\n"; |
3532 | trace "reloading resource files...\n"; |
3529 | |
3533 | |
|
|
3534 | reload_exp_table; |
|
|
3535 | reload_materials; |
3530 | reload_facedata; |
3536 | reload_facedata; |
3531 | reload_sound; |
3537 | reload_sound; |
3532 | reload_archetypes; |
3538 | reload_archetypes; |
3533 | reload_regions; |
3539 | reload_regions; |
3534 | reload_treasures; |
3540 | reload_treasures; |
… | |
… | |
3537 | } |
3543 | } |
3538 | |
3544 | |
3539 | sub reload_config { |
3545 | sub reload_config { |
3540 | trace "reloading config file...\n"; |
3546 | trace "reloading config file...\n"; |
3541 | |
3547 | |
3542 | open my $fh, "<:utf8", "$CONFDIR/config" |
3548 | 0 < aio_load "$CONFDIR/config", my $config |
3543 | or return; |
3549 | or die "$CONFDIR/config: $!"; |
3544 | |
3550 | |
3545 | local $/; |
3551 | utf8::decode $config; |
|
|
3552 | |
|
|
3553 | cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow... |
3546 | *CFG = YAML::XS::Load scalar <$fh>; |
3554 | *CFG = YAML::XS::Load $config; |
3547 | |
3555 | |
3548 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3556 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3549 | |
3557 | |
3550 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3558 | $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}; |
3559 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3555 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3563 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3556 | and die "WARNING: m(un)lockall failed: $!\n"; |
3564 | and die "WARNING: m(un)lockall failed: $!\n"; |
3557 | }; |
3565 | }; |
3558 | warn $@ if $@; |
3566 | warn $@ if $@; |
3559 | } |
3567 | } |
3560 | |
|
|
3561 | trace "finished reloading resource files\n"; |
|
|
3562 | } |
3568 | } |
3563 | |
3569 | |
3564 | sub pidfile() { |
3570 | sub pidfile() { |
3565 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3571 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3566 | or die "$PIDFILE: $!"; |
3572 | or die "$PIDFILE: $!"; |
… | |
… | |
3591 | |
3597 | |
3592 | sub main { |
3598 | sub main { |
3593 | cf::init_globals; # initialise logging |
3599 | cf::init_globals; # initialise logging |
3594 | |
3600 | |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3601 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3596 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3602 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3603 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3604 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3599 | |
3605 | |
3600 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3606 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3601 | |
3607 | |
… | |
… | |
3609 | }; |
3615 | }; |
3610 | |
3616 | |
3611 | evthread_start IO::AIO::poll_fileno; |
3617 | evthread_start IO::AIO::poll_fileno; |
3612 | |
3618 | |
3613 | cf::sync_job { |
3619 | cf::sync_job { |
3614 | cf::init_experience; |
3620 | cf::incloader::init (); |
|
|
3621 | |
3615 | cf::init_anim; |
3622 | cf::init_anim; |
3616 | cf::init_attackmess; |
3623 | cf::init_attackmess; |
3617 | cf::init_dynamic; |
3624 | cf::init_dynamic; |
3618 | |
3625 | |
3619 | cf::load_settings; |
3626 | cf::load_settings; |
3620 | cf::load_materials; |
|
|
3621 | |
3627 | |
3622 | reload_resources; |
3628 | reload_resources; |
3623 | reload_config; |
3629 | reload_config; |
3624 | db_init; |
3630 | db_init; |
3625 | |
3631 | |
… | |
… | |
3643 | }; |
3649 | }; |
3644 | |
3650 | |
3645 | cf::object::thawer::errors_are_fatal 0; |
3651 | cf::object::thawer::errors_are_fatal 0; |
3646 | info "parse errors in files are no longer fatal from this point on.\n"; |
3652 | info "parse errors in files are no longer fatal from this point on.\n"; |
3647 | |
3653 | |
|
|
3654 | my $free_main; $free_main = EV::idle sub { |
|
|
3655 | undef $free_main; |
|
|
3656 | undef &main; # free gobs of memory :) |
|
|
3657 | }; |
|
|
3658 | |
3648 | main_loop; |
3659 | goto &main_loop; |
3649 | } |
3660 | } |
3650 | |
3661 | |
3651 | ############################################################################# |
3662 | ############################################################################# |
3652 | # initialisation and cleanup |
3663 | # initialisation and cleanup |
3653 | |
3664 | |
… | |
… | |
3797 | cf::write_runtime_sync; # external watchdog should not bark |
3808 | cf::write_runtime_sync; # external watchdog should not bark |
3798 | |
3809 | |
3799 | trace "emergency_perl_save: syncing database to disk"; |
3810 | trace "emergency_perl_save: syncing database to disk"; |
3800 | BDB::db_env_txn_checkpoint $DB_ENV; |
3811 | BDB::db_env_txn_checkpoint $DB_ENV; |
3801 | |
3812 | |
3802 | info "emergency_perl_save: starting sync()\n"; |
3813 | info "emergency_perl_save: starting sync\n"; |
3803 | IO::AIO::aio_sync sub { |
3814 | IO::AIO::aio_sync sub { |
3804 | info "emergency_perl_save: finished sync()\n"; |
3815 | info "emergency_perl_save: finished sync\n"; |
3805 | }; |
3816 | }; |
3806 | |
3817 | |
3807 | cf::write_runtime_sync; # external watchdog should not bark |
3818 | cf::write_runtime_sync; # external watchdog should not bark |
3808 | |
3819 | |
3809 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3820 | trace "emergency_perl_save: flushing outstanding aio requests"; |
… | |
… | |
3817 | info "emergency_perl_save: leave\n"; |
3828 | info "emergency_perl_save: leave\n"; |
3818 | } |
3829 | } |
3819 | |
3830 | |
3820 | sub post_cleanup { |
3831 | sub post_cleanup { |
3821 | my ($make_core) = @_; |
3832 | my ($make_core) = @_; |
3822 | |
|
|
3823 | IO::AIO::flush; |
|
|
3824 | |
3833 | |
3825 | IO::AIO::flush; |
3834 | IO::AIO::flush; |
3826 | |
3835 | |
3827 | error Carp::longmess "post_cleanup backtrace" |
3836 | error Carp::longmess "post_cleanup backtrace" |
3828 | if $make_core; |
3837 | if $make_core; |
… | |
… | |
3856 | } |
3865 | } |
3857 | } |
3866 | } |
3858 | |
3867 | |
3859 | sub do_reload_perl() { |
3868 | sub do_reload_perl() { |
3860 | # can/must only be called in main |
3869 | # can/must only be called in main |
3861 | if (in_main) { |
3870 | unless (in_main) { |
3862 | error "can only reload from main coroutine"; |
3871 | error "can only reload from main coroutine"; |
3863 | return; |
3872 | return; |
3864 | } |
3873 | } |
3865 | |
3874 | |
3866 | return if $RELOAD++; |
3875 | return if $RELOAD++; |
3867 | |
3876 | |
3868 | my $t1 = AE::time; |
3877 | my $t1 = AE::time; |
3869 | |
3878 | |
3870 | while ($RELOAD) { |
3879 | while ($RELOAD) { |
|
|
3880 | cf::get_slot 0.1, -1, "reload_perl"; |
3871 | info "reloading..."; |
3881 | info "reloading..."; |
3872 | |
3882 | |
3873 | trace "entering sync_job"; |
3883 | trace "entering sync_job"; |
3874 | |
3884 | |
3875 | cf::sync_job { |
3885 | cf::sync_job { |
3876 | cf::emergency_save; |
3886 | #cf::emergency_save; |
3877 | |
3887 | |
3878 | trace "cancelling all extension coros"; |
3888 | trace "cancelling all extension coros"; |
3879 | $_->cancel for values %EXT_CORO; |
3889 | $_->cancel for values %EXT_CORO; |
3880 | %EXT_CORO = (); |
3890 | %EXT_CORO = (); |
3881 | |
3891 | |
… | |
… | |
3996 | reload_perl; |
4006 | reload_perl; |
3997 | }; |
4007 | }; |
3998 | } |
4008 | } |
3999 | }; |
4009 | }; |
4000 | |
4010 | |
4001 | unshift @INC, $LIBDIR; |
4011 | ############################################################################# |
4002 | |
4012 | |
4003 | my $bug_warning = 0; |
4013 | my $bug_warning = 0; |
4004 | |
4014 | |
4005 | our @WAIT_FOR_TICK; |
4015 | our @WAIT_FOR_TICK; |
4006 | our @WAIT_FOR_TICK_BEGIN; |
4016 | our @WAIT_FOR_TICK_BEGIN; |
4007 | |
4017 | |
4008 | sub wait_for_tick { |
4018 | sub wait_for_tick() { |
4009 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4019 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4010 | |
4020 | |
4011 | my $signal = new Coro::Signal; |
4021 | my $signal = new Coro::Signal; |
4012 | push @WAIT_FOR_TICK, $signal; |
4022 | push @WAIT_FOR_TICK, $signal; |
4013 | $signal->wait; |
4023 | $signal->wait; |
4014 | } |
4024 | } |
4015 | |
4025 | |
4016 | sub wait_for_tick_begin { |
4026 | sub wait_for_tick_begin() { |
4017 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4027 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4018 | |
4028 | |
4019 | my $signal = new Coro::Signal; |
4029 | my $signal = new Coro::Signal; |
4020 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4030 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4021 | $signal->wait; |
4031 | $signal->wait; |