… | |
… | |
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; |
… | |
… | |
77 | |
75 | |
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 |
|
|
80 | |
|
|
81 | our @ORIG_INC; |
82 | |
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 |
… | |
… | |
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; |
… | |
… | |
1451 | |
1451 | |
1452 | my $active = eval $v->{source}; |
1452 | my $active = eval $v->{source}; |
1453 | |
1453 | |
1454 | if (length $@) { |
1454 | if (length $@) { |
1455 | error "$v->{path}: $@\n"; |
1455 | error "$v->{path}: $@\n"; |
|
|
1456 | undef $@; # work around perl 5.10.0 utf-8 caching bug |
1456 | |
1457 | |
1457 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1458 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1458 | if exists $v->{meta}{mandatory}; |
1459 | if exists $v->{meta}{mandatory}; |
1459 | |
1460 | |
1460 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
1461 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
… | |
… | |
1848 | |
1849 | |
1849 | sub register { |
1850 | sub register { |
1850 | my (undef, $regex, $prio) = @_; |
1851 | my (undef, $regex, $prio) = @_; |
1851 | my $pkg = caller; |
1852 | my $pkg = caller; |
1852 | |
1853 | |
1853 | no strict; |
|
|
1854 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1854 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1855 | |
1855 | |
1856 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1856 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1857 | } |
1857 | } |
1858 | |
1858 | |
… | |
… | |
3273 | decrease split destroy change_exp value msg lore send_msg)], |
3273 | decrease split destroy change_exp value msg lore send_msg)], |
3274 | ["cf::object::player" => qw(player)], |
3274 | ["cf::object::player" => qw(player)], |
3275 | ["cf::player" => qw(peaceful send_msg)], |
3275 | ["cf::player" => qw(peaceful send_msg)], |
3276 | ["cf::map" => qw(trigger)], |
3276 | ["cf::map" => qw(trigger)], |
3277 | ) { |
3277 | ) { |
3278 | no strict 'refs'; |
|
|
3279 | my ($pkg, @funs) = @$_; |
3278 | my ($pkg, @funs) = @$_; |
3280 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3279 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3281 | for @funs; |
3280 | for @funs; |
3282 | } |
3281 | } |
3283 | |
3282 | |
… | |
… | |
3359 | =cut |
3358 | =cut |
3360 | |
3359 | |
3361 | ############################################################################# |
3360 | ############################################################################# |
3362 | # the server's init and main functions |
3361 | # the server's init and main functions |
3363 | |
3362 | |
|
|
3363 | # async inc loader. yay. |
|
|
3364 | sub inc_loader { |
|
|
3365 | my $mod = $_[1]; |
|
|
3366 | |
|
|
3367 | if (in_main && !tick_inhibit) { |
|
|
3368 | Carp::cluck "ERROR: attempted synchronous perl module load ($mod)"; |
|
|
3369 | } else { |
|
|
3370 | debug "loading perl module $mod\n"; |
|
|
3371 | } |
|
|
3372 | |
|
|
3373 | # 1. find real file |
|
|
3374 | for my $dir (@ORIG_INC) { |
|
|
3375 | ref $dir and next; |
|
|
3376 | 0 <= Coro::AIO::aio_load "$dir/$mod", my $data |
|
|
3377 | or next; |
|
|
3378 | |
|
|
3379 | $data = "#line 1 $dir/$mod\n$data"; |
|
|
3380 | |
|
|
3381 | open my $fh, "<", \$data or die; |
|
|
3382 | |
|
|
3383 | return $fh; |
|
|
3384 | } |
|
|
3385 | |
|
|
3386 | () |
|
|
3387 | } |
|
|
3388 | |
|
|
3389 | sub init_inc { |
|
|
3390 | # save original @INC |
|
|
3391 | @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC; |
|
|
3392 | |
|
|
3393 | # make sure we can do scalar-opens |
|
|
3394 | open my $dummy, "<", \my $dummy2; |
|
|
3395 | |
|
|
3396 | # execute some stuff so perl load's some of the core modules |
|
|
3397 | /Ü/ =~ /ü/i; |
|
|
3398 | eval { &Storable::nstore_fd }; |
|
|
3399 | |
|
|
3400 | @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc. |
|
|
3401 | |
|
|
3402 | debug "module loading will be asynchronous from this point on."; |
|
|
3403 | } |
|
|
3404 | |
3364 | sub load_facedata($) { |
3405 | sub load_facedata($) { |
3365 | my ($path) = @_; |
3406 | my ($path) = @_; |
3366 | |
3407 | |
3367 | # HACK to clear player env face cache, we need some signal framework |
3408 | # HACK to clear player env face cache, we need some signal framework |
3368 | # for this (global event?) |
3409 | # for this (global event?) |
… | |
… | |
3591 | |
3632 | |
3592 | sub main { |
3633 | sub main { |
3593 | cf::init_globals; # initialise logging |
3634 | cf::init_globals; # initialise logging |
3594 | |
3635 | |
3595 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3636 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3596 | 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."; |
3597 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3638 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3598 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3639 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3599 | |
3640 | |
3600 | $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 |
3601 | |
3642 | |
… | |
… | |
3609 | }; |
3650 | }; |
3610 | |
3651 | |
3611 | evthread_start IO::AIO::poll_fileno; |
3652 | evthread_start IO::AIO::poll_fileno; |
3612 | |
3653 | |
3613 | cf::sync_job { |
3654 | cf::sync_job { |
|
|
3655 | init_inc; |
|
|
3656 | |
3614 | cf::init_experience; |
3657 | cf::init_experience; |
3615 | cf::init_anim; |
3658 | cf::init_anim; |
3616 | cf::init_attackmess; |
3659 | cf::init_attackmess; |
3617 | cf::init_dynamic; |
3660 | cf::init_dynamic; |
3618 | |
3661 | |
… | |
… | |
3643 | }; |
3686 | }; |
3644 | |
3687 | |
3645 | cf::object::thawer::errors_are_fatal 0; |
3688 | cf::object::thawer::errors_are_fatal 0; |
3646 | info "parse errors in files are no longer fatal from this point on.\n"; |
3689 | info "parse errors in files are no longer fatal from this point on.\n"; |
3647 | |
3690 | |
|
|
3691 | my $free_main; $free_main = EV::idle sub { |
|
|
3692 | undef $free_main; |
|
|
3693 | undef &main; # free gobs of memory :) |
|
|
3694 | }; |
|
|
3695 | |
3648 | main_loop; |
3696 | goto &main_loop; |
3649 | } |
3697 | } |
3650 | |
3698 | |
3651 | ############################################################################# |
3699 | ############################################################################# |
3652 | # initialisation and cleanup |
3700 | # initialisation and cleanup |
3653 | |
3701 | |
… | |
… | |
3797 | cf::write_runtime_sync; # external watchdog should not bark |
3845 | cf::write_runtime_sync; # external watchdog should not bark |
3798 | |
3846 | |
3799 | trace "emergency_perl_save: syncing database to disk"; |
3847 | trace "emergency_perl_save: syncing database to disk"; |
3800 | BDB::db_env_txn_checkpoint $DB_ENV; |
3848 | BDB::db_env_txn_checkpoint $DB_ENV; |
3801 | |
3849 | |
3802 | info "emergency_perl_save: starting sync()\n"; |
3850 | info "emergency_perl_save: starting sync\n"; |
3803 | IO::AIO::aio_sync sub { |
3851 | IO::AIO::aio_sync sub { |
3804 | info "emergency_perl_save: finished sync()\n"; |
3852 | info "emergency_perl_save: finished sync\n"; |
3805 | }; |
3853 | }; |
3806 | |
3854 | |
3807 | cf::write_runtime_sync; # external watchdog should not bark |
3855 | cf::write_runtime_sync; # external watchdog should not bark |
3808 | |
3856 | |
3809 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3857 | trace "emergency_perl_save: flushing outstanding aio requests"; |
… | |
… | |
3817 | info "emergency_perl_save: leave\n"; |
3865 | info "emergency_perl_save: leave\n"; |
3818 | } |
3866 | } |
3819 | |
3867 | |
3820 | sub post_cleanup { |
3868 | sub post_cleanup { |
3821 | my ($make_core) = @_; |
3869 | my ($make_core) = @_; |
3822 | |
|
|
3823 | IO::AIO::flush; |
|
|
3824 | |
3870 | |
3825 | IO::AIO::flush; |
3871 | IO::AIO::flush; |
3826 | |
3872 | |
3827 | error Carp::longmess "post_cleanup backtrace" |
3873 | error Carp::longmess "post_cleanup backtrace" |
3828 | if $make_core; |
3874 | if $make_core; |
… | |
… | |
3996 | reload_perl; |
4042 | reload_perl; |
3997 | }; |
4043 | }; |
3998 | } |
4044 | } |
3999 | }; |
4045 | }; |
4000 | |
4046 | |
4001 | unshift @INC, $LIBDIR; |
4047 | ############################################################################# |
4002 | |
4048 | |
4003 | my $bug_warning = 0; |
4049 | my $bug_warning = 0; |
4004 | |
4050 | |
4005 | our @WAIT_FOR_TICK; |
4051 | our @WAIT_FOR_TICK; |
4006 | our @WAIT_FOR_TICK_BEGIN; |
4052 | our @WAIT_FOR_TICK_BEGIN; |