… | |
… | |
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 | |
… | |
… | |
3591 | |
3591 | |
3592 | sub main { |
3592 | sub main { |
3593 | cf::init_globals; # initialise logging |
3593 | cf::init_globals; # initialise logging |
3594 | |
3594 | |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3596 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3596 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3599 | |
3599 | |
3600 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3600 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3601 | |
3601 | |
… | |
… | |
3609 | }; |
3609 | }; |
3610 | |
3610 | |
3611 | evthread_start IO::AIO::poll_fileno; |
3611 | evthread_start IO::AIO::poll_fileno; |
3612 | |
3612 | |
3613 | cf::sync_job { |
3613 | cf::sync_job { |
|
|
3614 | cf::incloader::init (); |
|
|
3615 | |
3614 | cf::init_experience; |
3616 | cf::init_experience; |
3615 | cf::init_anim; |
3617 | cf::init_anim; |
3616 | cf::init_attackmess; |
3618 | cf::init_attackmess; |
3617 | cf::init_dynamic; |
3619 | cf::init_dynamic; |
3618 | |
3620 | |
… | |
… | |
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 | |
… | |
… | |
3797 | cf::write_runtime_sync; # external watchdog should not bark |
3804 | cf::write_runtime_sync; # external watchdog should not bark |
3798 | |
3805 | |
3799 | trace "emergency_perl_save: syncing database to disk"; |
3806 | trace "emergency_perl_save: syncing database to disk"; |
3800 | BDB::db_env_txn_checkpoint $DB_ENV; |
3807 | BDB::db_env_txn_checkpoint $DB_ENV; |
3801 | |
3808 | |
3802 | info "emergency_perl_save: starting sync()\n"; |
3809 | info "emergency_perl_save: starting sync\n"; |
3803 | IO::AIO::aio_sync sub { |
3810 | IO::AIO::aio_sync sub { |
3804 | info "emergency_perl_save: finished sync()\n"; |
3811 | info "emergency_perl_save: finished sync\n"; |
3805 | }; |
3812 | }; |
3806 | |
3813 | |
3807 | cf::write_runtime_sync; # external watchdog should not bark |
3814 | cf::write_runtime_sync; # external watchdog should not bark |
3808 | |
3815 | |
3809 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3816 | trace "emergency_perl_save: flushing outstanding aio requests"; |
… | |
… | |
3817 | info "emergency_perl_save: leave\n"; |
3824 | info "emergency_perl_save: leave\n"; |
3818 | } |
3825 | } |
3819 | |
3826 | |
3820 | sub post_cleanup { |
3827 | sub post_cleanup { |
3821 | my ($make_core) = @_; |
3828 | my ($make_core) = @_; |
3822 | |
|
|
3823 | IO::AIO::flush; |
|
|
3824 | |
3829 | |
3825 | IO::AIO::flush; |
3830 | IO::AIO::flush; |
3826 | |
3831 | |
3827 | error Carp::longmess "post_cleanup backtrace" |
3832 | error Carp::longmess "post_cleanup backtrace" |
3828 | if $make_core; |
3833 | if $make_core; |
… | |
… | |
3856 | } |
3861 | } |
3857 | } |
3862 | } |
3858 | |
3863 | |
3859 | sub do_reload_perl() { |
3864 | sub do_reload_perl() { |
3860 | # can/must only be called in main |
3865 | # can/must only be called in main |
3861 | if (in_main) { |
3866 | unless (in_main) { |
3862 | error "can only reload from main coroutine"; |
3867 | error "can only reload from main coroutine"; |
3863 | return; |
3868 | return; |
3864 | } |
3869 | } |
3865 | |
3870 | |
3866 | return if $RELOAD++; |
3871 | return if $RELOAD++; |
3867 | |
3872 | |
3868 | my $t1 = AE::time; |
3873 | my $t1 = AE::time; |
3869 | |
3874 | |
3870 | while ($RELOAD) { |
3875 | while ($RELOAD) { |
|
|
3876 | cf::get_slot 0.1, -1, "reload_perl"; |
3871 | info "reloading..."; |
3877 | info "reloading..."; |
3872 | |
3878 | |
3873 | trace "entering sync_job"; |
3879 | trace "entering sync_job"; |
3874 | |
3880 | |
3875 | cf::sync_job { |
3881 | cf::sync_job { |
3876 | cf::emergency_save; |
3882 | #cf::emergency_save; |
3877 | |
3883 | |
3878 | trace "cancelling all extension coros"; |
3884 | trace "cancelling all extension coros"; |
3879 | $_->cancel for values %EXT_CORO; |
3885 | $_->cancel for values %EXT_CORO; |
3880 | %EXT_CORO = (); |
3886 | %EXT_CORO = (); |
3881 | |
3887 | |
… | |
… | |
3996 | reload_perl; |
4002 | reload_perl; |
3997 | }; |
4003 | }; |
3998 | } |
4004 | } |
3999 | }; |
4005 | }; |
4000 | |
4006 | |
4001 | unshift @INC, $LIBDIR; |
4007 | ############################################################################# |
4002 | |
4008 | |
4003 | my $bug_warning = 0; |
4009 | my $bug_warning = 0; |
4004 | |
4010 | |
4005 | our @WAIT_FOR_TICK; |
4011 | our @WAIT_FOR_TICK; |
4006 | our @WAIT_FOR_TICK_BEGIN; |
4012 | our @WAIT_FOR_TICK_BEGIN; |