… | |
… | |
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; |
… | |
… | |
1423 | |
1423 | |
1424 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1424 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1425 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1425 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1426 | |
1426 | |
1427 | $ext{source} = |
1427 | $ext{source} = |
1428 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1428 | "package $pkg; use common::sense;\n" |
1429 | . "#line 1 \"$path\"\n{\n" |
1429 | . "#line 1 \"$path\"\n{\n" |
1430 | . $source |
1430 | . $source |
1431 | . "\n};\n1"; |
1431 | . "\n};\n1"; |
1432 | |
1432 | |
1433 | $todo{$base} = \%ext; |
1433 | $todo{$base} = \%ext; |
… | |
… | |
1848 | |
1848 | |
1849 | sub register { |
1849 | sub register { |
1850 | my (undef, $regex, $prio) = @_; |
1850 | my (undef, $regex, $prio) = @_; |
1851 | my $pkg = caller; |
1851 | my $pkg = caller; |
1852 | |
1852 | |
1853 | no strict; |
|
|
1854 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1853 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1855 | |
1854 | |
1856 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1855 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1857 | } |
1856 | } |
1858 | |
1857 | |
… | |
… | |
1870 | } |
1869 | } |
1871 | |
1870 | |
1872 | sub normalise { |
1871 | sub normalise { |
1873 | my ($path, $base) = @_; |
1872 | my ($path, $base) = @_; |
1874 | |
1873 | |
1875 | $path = "$path"; # make sure its a string |
1874 | $path = "$path"; # make sure it's a string |
1876 | |
1875 | |
1877 | $path =~ s/\.map$//; |
1876 | $path =~ s/\.map$//; |
1878 | |
1877 | |
1879 | # map plan: |
1878 | # map plan: |
1880 | # |
1879 | # |
… | |
… | |
1919 | $self->init; # pass $1 etc. |
1918 | $self->init; # pass $1 etc. |
1920 | return $self; |
1919 | return $self; |
1921 | } |
1920 | } |
1922 | } |
1921 | } |
1923 | |
1922 | |
1924 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1923 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1925 | () |
1924 | () |
1926 | } |
1925 | } |
1927 | |
1926 | |
1928 | sub init { |
1927 | sub init { |
1929 | my ($self) = @_; |
1928 | my ($self) = @_; |
… | |
… | |
2051 | } |
2050 | } |
2052 | |
2051 | |
2053 | sub find; |
2052 | sub find; |
2054 | sub find { |
2053 | sub find { |
2055 | my ($path, $origin) = @_; |
2054 | my ($path, $origin) = @_; |
|
|
2055 | |
|
|
2056 | cf::cede_to_tick; |
2056 | |
2057 | |
2057 | $path = normalise $path, $origin && $origin->path; |
2058 | $path = normalise $path, $origin && $origin->path; |
2058 | |
2059 | |
2059 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2060 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2060 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2061 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
3273 | decrease split destroy change_exp value msg lore send_msg)], |
3274 | decrease split destroy change_exp value msg lore send_msg)], |
3274 | ["cf::object::player" => qw(player)], |
3275 | ["cf::object::player" => qw(player)], |
3275 | ["cf::player" => qw(peaceful send_msg)], |
3276 | ["cf::player" => qw(peaceful send_msg)], |
3276 | ["cf::map" => qw(trigger)], |
3277 | ["cf::map" => qw(trigger)], |
3277 | ) { |
3278 | ) { |
3278 | no strict 'refs'; |
|
|
3279 | my ($pkg, @funs) = @$_; |
3279 | my ($pkg, @funs) = @$_; |
3280 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3280 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3281 | for @funs; |
3281 | for @funs; |
3282 | } |
3282 | } |
3283 | |
3283 | |
… | |
… | |
3525 | } |
3525 | } |
3526 | |
3526 | |
3527 | sub reload_resources { |
3527 | sub reload_resources { |
3528 | trace "reloading resource files...\n"; |
3528 | trace "reloading resource files...\n"; |
3529 | |
3529 | |
|
|
3530 | reload_exp_table; |
|
|
3531 | reload_materials; |
3530 | reload_facedata; |
3532 | reload_facedata; |
3531 | reload_sound; |
3533 | reload_sound; |
3532 | reload_archetypes; |
3534 | reload_archetypes; |
3533 | reload_regions; |
3535 | reload_regions; |
3534 | reload_treasures; |
3536 | reload_treasures; |
… | |
… | |
3591 | |
3593 | |
3592 | sub main { |
3594 | sub main { |
3593 | cf::init_globals; # initialise logging |
3595 | cf::init_globals; # initialise logging |
3594 | |
3596 | |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3597 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3596 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3598 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3599 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3600 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3599 | |
3601 | |
3600 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3602 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3601 | |
3603 | |
… | |
… | |
3609 | }; |
3611 | }; |
3610 | |
3612 | |
3611 | evthread_start IO::AIO::poll_fileno; |
3613 | evthread_start IO::AIO::poll_fileno; |
3612 | |
3614 | |
3613 | cf::sync_job { |
3615 | cf::sync_job { |
3614 | cf::init_experience; |
3616 | cf::incloader::init (); |
|
|
3617 | |
3615 | cf::init_anim; |
3618 | cf::init_anim; |
3616 | cf::init_attackmess; |
3619 | cf::init_attackmess; |
3617 | cf::init_dynamic; |
3620 | cf::init_dynamic; |
3618 | |
3621 | |
3619 | cf::load_settings; |
3622 | cf::load_settings; |
3620 | cf::load_materials; |
|
|
3621 | |
3623 | |
3622 | reload_resources; |
3624 | reload_resources; |
3623 | reload_config; |
3625 | reload_config; |
3624 | db_init; |
3626 | db_init; |
3625 | |
3627 | |
… | |
… | |
3643 | }; |
3645 | }; |
3644 | |
3646 | |
3645 | cf::object::thawer::errors_are_fatal 0; |
3647 | cf::object::thawer::errors_are_fatal 0; |
3646 | info "parse errors in files are no longer fatal from this point on.\n"; |
3648 | info "parse errors in files are no longer fatal from this point on.\n"; |
3647 | |
3649 | |
|
|
3650 | my $free_main; $free_main = EV::idle sub { |
|
|
3651 | undef $free_main; |
|
|
3652 | undef &main; # free gobs of memory :) |
|
|
3653 | }; |
|
|
3654 | |
3648 | main_loop; |
3655 | goto &main_loop; |
3649 | } |
3656 | } |
3650 | |
3657 | |
3651 | ############################################################################# |
3658 | ############################################################################# |
3652 | # initialisation and cleanup |
3659 | # initialisation and cleanup |
3653 | |
3660 | |
… | |
… | |
3758 | # save all maps without fsync, and later call a global sync |
3765 | # save all maps without fsync, and later call a global sync |
3759 | # (which in turn might be very very slow) |
3766 | # (which in turn might be very very slow) |
3760 | local $USE_FSYNC = 0; |
3767 | local $USE_FSYNC = 0; |
3761 | |
3768 | |
3762 | cf::sync_job { |
3769 | cf::sync_job { |
|
|
3770 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3771 | |
3763 | # use a peculiar iteration method to avoid tripping on perl |
3772 | # use a peculiar iteration method to avoid tripping on perl |
3764 | # refcount bugs in for. also avoids problems with players |
3773 | # refcount bugs in for. also avoids problems with players |
3765 | # and maps saved/destroyed asynchronously. |
3774 | # and maps saved/destroyed asynchronously. |
3766 | info "emergency_perl_save: begin player save\n"; |
3775 | info "emergency_perl_save: begin player save\n"; |
3767 | for my $login (keys %cf::PLAYER) { |
3776 | for my $login (keys %cf::PLAYER) { |
… | |
… | |
3770 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3779 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3771 | $pl->save; |
3780 | $pl->save; |
3772 | } |
3781 | } |
3773 | info "emergency_perl_save: end player save\n"; |
3782 | info "emergency_perl_save: end player save\n"; |
3774 | |
3783 | |
|
|
3784 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3785 | |
3775 | info "emergency_perl_save: begin map save\n"; |
3786 | info "emergency_perl_save: begin map save\n"; |
3776 | for my $path (keys %cf::MAP) { |
3787 | for my $path (keys %cf::MAP) { |
3777 | my $map = $cf::MAP{$path} or next; |
3788 | my $map = $cf::MAP{$path} or next; |
3778 | $map->valid or next; |
3789 | $map->valid or next; |
3779 | $map->save; |
3790 | $map->save; |
3780 | } |
3791 | } |
3781 | info "emergency_perl_save: end map save\n"; |
3792 | info "emergency_perl_save: end map save\n"; |
3782 | |
3793 | |
|
|
3794 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3795 | |
3783 | info "emergency_perl_save: begin database checkpoint\n"; |
3796 | info "emergency_perl_save: begin database checkpoint\n"; |
3784 | BDB::db_env_txn_checkpoint $DB_ENV; |
3797 | BDB::db_env_txn_checkpoint $DB_ENV; |
3785 | info "emergency_perl_save: end database checkpoint\n"; |
3798 | info "emergency_perl_save: end database checkpoint\n"; |
3786 | |
3799 | |
3787 | info "emergency_perl_save: begin write uuid\n"; |
3800 | info "emergency_perl_save: begin write uuid\n"; |
3788 | write_uuid_sync 1; |
3801 | write_uuid_sync 1; |
3789 | info "emergency_perl_save: end write uuid\n"; |
3802 | info "emergency_perl_save: end write uuid\n"; |
3790 | }; |
|
|
3791 | |
3803 | |
|
|
3804 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3805 | |
|
|
3806 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3807 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3808 | |
3792 | info "emergency_perl_save: starting sync()\n"; |
3809 | info "emergency_perl_save: starting sync\n"; |
3793 | IO::AIO::aio_sync sub { |
3810 | IO::AIO::aio_sync sub { |
3794 | info "emergency_perl_save: finished sync()\n"; |
3811 | info "emergency_perl_save: finished sync\n"; |
|
|
3812 | }; |
|
|
3813 | |
|
|
3814 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3815 | |
|
|
3816 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3817 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3818 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3819 | } |
|
|
3820 | |
|
|
3821 | cf::write_runtime_sync; # external watchdog should not bark |
3795 | }; |
3822 | }; |
3796 | |
3823 | |
3797 | info "emergency_perl_save: leave\n"; |
3824 | info "emergency_perl_save: leave\n"; |
3798 | } |
3825 | } |
3799 | |
3826 | |
3800 | sub post_cleanup { |
3827 | sub post_cleanup { |
3801 | my ($make_core) = @_; |
3828 | my ($make_core) = @_; |
|
|
3829 | |
|
|
3830 | IO::AIO::flush; |
3802 | |
3831 | |
3803 | error Carp::longmess "post_cleanup backtrace" |
3832 | error Carp::longmess "post_cleanup backtrace" |
3804 | if $make_core; |
3833 | if $make_core; |
3805 | |
3834 | |
3806 | my $fh = pidfile; |
3835 | my $fh = pidfile; |
… | |
… | |
3832 | } |
3861 | } |
3833 | } |
3862 | } |
3834 | |
3863 | |
3835 | sub do_reload_perl() { |
3864 | sub do_reload_perl() { |
3836 | # can/must only be called in main |
3865 | # can/must only be called in main |
3837 | if (in_main) { |
3866 | unless (in_main) { |
3838 | error "can only reload from main coroutine"; |
3867 | error "can only reload from main coroutine"; |
3839 | return; |
3868 | return; |
3840 | } |
3869 | } |
3841 | |
3870 | |
3842 | return if $RELOAD++; |
3871 | return if $RELOAD++; |
3843 | |
3872 | |
3844 | my $t1 = AE::time; |
3873 | my $t1 = AE::time; |
3845 | |
3874 | |
3846 | while ($RELOAD) { |
3875 | while ($RELOAD) { |
|
|
3876 | cf::get_slot 0.1, -1, "reload_perl"; |
3847 | info "reloading..."; |
3877 | info "reloading..."; |
3848 | |
3878 | |
3849 | trace "entering sync_job"; |
3879 | trace "entering sync_job"; |
3850 | |
3880 | |
3851 | cf::sync_job { |
3881 | cf::sync_job { |
3852 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3853 | cf::emergency_save; |
3882 | #cf::emergency_save; |
3854 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3855 | |
|
|
3856 | trace "syncing database to disk"; |
|
|
3857 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3858 | |
|
|
3859 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
3860 | |
|
|
3861 | trace "flushing outstanding aio requests"; |
|
|
3862 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3863 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3864 | } |
|
|
3865 | |
3883 | |
3866 | trace "cancelling all extension coros"; |
3884 | trace "cancelling all extension coros"; |
3867 | $_->cancel for values %EXT_CORO; |
3885 | $_->cancel for values %EXT_CORO; |
3868 | %EXT_CORO = (); |
3886 | %EXT_CORO = (); |
3869 | |
3887 | |
… | |
… | |
3984 | reload_perl; |
4002 | reload_perl; |
3985 | }; |
4003 | }; |
3986 | } |
4004 | } |
3987 | }; |
4005 | }; |
3988 | |
4006 | |
3989 | unshift @INC, $LIBDIR; |
4007 | ############################################################################# |
3990 | |
4008 | |
3991 | my $bug_warning = 0; |
4009 | my $bug_warning = 0; |
3992 | |
4010 | |
3993 | our @WAIT_FOR_TICK; |
4011 | our @WAIT_FOR_TICK; |
3994 | our @WAIT_FOR_TICK_BEGIN; |
4012 | our @WAIT_FOR_TICK_BEGIN; |