… | |
… | |
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 |
… | |
… | |
1451 | |
1456 | |
1452 | my $active = eval $v->{source}; |
1457 | my $active = eval $v->{source}; |
1453 | |
1458 | |
1454 | if (length $@) { |
1459 | if (length $@) { |
1455 | error "$v->{path}: $@\n"; |
1460 | error "$v->{path}: $@\n"; |
1456 | undef $@; # work around perl 5.10.0 utf-8 caching bug |
|
|
1457 | |
1461 | |
1458 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1462 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1459 | if exists $v->{meta}{mandatory}; |
1463 | if exists $v->{meta}{mandatory}; |
1460 | |
1464 | |
1461 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
1465 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
… | |
… | |
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"; |
… | |
… | |
3358 | =cut |
3364 | =cut |
3359 | |
3365 | |
3360 | ############################################################################# |
3366 | ############################################################################# |
3361 | # the server's init and main functions |
3367 | # the server's init and main functions |
3362 | |
3368 | |
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 | warn "$dir/$mod\n";#d# |
|
|
3376 | ref $dir and next; |
|
|
3377 | 0 <= Coro::AIO::aio_load "$dir/$mod", my $data |
|
|
3378 | or next; |
|
|
3379 | |
|
|
3380 | $data = "#line 1 $dir/$mod\n$data"; |
|
|
3381 | |
|
|
3382 | open my $fh, "<", \$data or die; |
|
|
3383 | |
|
|
3384 | return $fh; |
|
|
3385 | } |
|
|
3386 | |
|
|
3387 | () |
|
|
3388 | } |
|
|
3389 | |
|
|
3390 | sub init_inc { |
|
|
3391 | # save original @INC |
|
|
3392 | @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC; |
|
|
3393 | |
|
|
3394 | # make sure we can do scalar-opens |
|
|
3395 | open my $dummy, "<", \my $dummy2; |
|
|
3396 | |
|
|
3397 | # execute some stuff so perl load's some of the core modules |
|
|
3398 | /Ü/ =~ /ü/i; |
|
|
3399 | eval { &Storable::nstore_fd }; |
|
|
3400 | |
|
|
3401 | @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc. |
|
|
3402 | |
|
|
3403 | debug "module loading will be asynchronous from this point on."; |
|
|
3404 | } |
|
|
3405 | |
|
|
3406 | sub load_facedata($) { |
3369 | sub load_facedata($) { |
3407 | my ($path) = @_; |
3370 | my ($path) = @_; |
3408 | |
3371 | |
3409 | # HACK to clear player env face cache, we need some signal framework |
3372 | # HACK to clear player env face cache, we need some signal framework |
3410 | # for this (global event?) |
3373 | # for this (global event?) |
… | |
… | |
3412 | |
3375 | |
3413 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3376 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3414 | |
3377 | |
3415 | trace "loading facedata from $path\n"; |
3378 | trace "loading facedata from $path\n"; |
3416 | |
3379 | |
3417 | my $facedata; |
|
|
3418 | 0 < aio_load $path, $facedata |
3380 | 0 < aio_load $path, my $facedata |
3419 | or die "$path: $!"; |
3381 | or die "$path: $!"; |
3420 | |
3382 | |
3421 | $facedata = Coro::Storable::thaw $facedata; |
3383 | $facedata = Coro::Storable::thaw $facedata; |
3422 | |
3384 | |
3423 | $facedata->{version} == 2 |
3385 | $facedata->{version} == 2 |
… | |
… | |
3567 | } |
3529 | } |
3568 | |
3530 | |
3569 | sub reload_resources { |
3531 | sub reload_resources { |
3570 | trace "reloading resource files...\n"; |
3532 | trace "reloading resource files...\n"; |
3571 | |
3533 | |
|
|
3534 | reload_exp_table; |
|
|
3535 | reload_materials; |
3572 | reload_facedata; |
3536 | reload_facedata; |
3573 | reload_sound; |
3537 | reload_sound; |
3574 | reload_archetypes; |
3538 | reload_archetypes; |
3575 | reload_regions; |
3539 | reload_regions; |
3576 | reload_treasures; |
3540 | reload_treasures; |
… | |
… | |
3579 | } |
3543 | } |
3580 | |
3544 | |
3581 | sub reload_config { |
3545 | sub reload_config { |
3582 | trace "reloading config file...\n"; |
3546 | trace "reloading config file...\n"; |
3583 | |
3547 | |
3584 | open my $fh, "<:utf8", "$CONFDIR/config" |
3548 | 0 < aio_load "$CONFDIR/config", my $config |
3585 | or return; |
3549 | or die "$CONFDIR/config: $!"; |
3586 | |
3550 | |
3587 | local $/; |
3551 | utf8::decode $config; |
|
|
3552 | |
|
|
3553 | cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow... |
3588 | *CFG = YAML::XS::Load scalar <$fh>; |
3554 | *CFG = YAML::XS::Load $config; |
3589 | |
3555 | |
3590 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3556 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3591 | |
3557 | |
3592 | $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}; |
3593 | $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}; |
… | |
… | |
3597 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3563 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3598 | and die "WARNING: m(un)lockall failed: $!\n"; |
3564 | and die "WARNING: m(un)lockall failed: $!\n"; |
3599 | }; |
3565 | }; |
3600 | warn $@ if $@; |
3566 | warn $@ if $@; |
3601 | } |
3567 | } |
3602 | |
|
|
3603 | trace "finished reloading resource files\n"; |
|
|
3604 | } |
3568 | } |
3605 | |
3569 | |
3606 | sub pidfile() { |
3570 | sub pidfile() { |
3607 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3571 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3608 | or die "$PIDFILE: $!"; |
3572 | or die "$PIDFILE: $!"; |
… | |
… | |
3651 | }; |
3615 | }; |
3652 | |
3616 | |
3653 | evthread_start IO::AIO::poll_fileno; |
3617 | evthread_start IO::AIO::poll_fileno; |
3654 | |
3618 | |
3655 | cf::sync_job { |
3619 | cf::sync_job { |
3656 | init_inc; |
3620 | cf::incloader::init (); |
3657 | |
3621 | |
3658 | cf::init_experience; |
|
|
3659 | cf::init_anim; |
3622 | cf::init_anim; |
3660 | cf::init_attackmess; |
3623 | cf::init_attackmess; |
3661 | cf::init_dynamic; |
3624 | cf::init_dynamic; |
3662 | |
3625 | |
3663 | cf::load_settings; |
3626 | cf::load_settings; |
3664 | cf::load_materials; |
|
|
3665 | |
3627 | |
3666 | reload_resources; |
3628 | reload_resources; |
3667 | reload_config; |
3629 | reload_config; |
3668 | db_init; |
3630 | db_init; |
3669 | |
3631 | |
… | |
… | |
3903 | } |
3865 | } |
3904 | } |
3866 | } |
3905 | |
3867 | |
3906 | sub do_reload_perl() { |
3868 | sub do_reload_perl() { |
3907 | # can/must only be called in main |
3869 | # can/must only be called in main |
3908 | if (in_main) { |
3870 | unless (in_main) { |
3909 | error "can only reload from main coroutine"; |
3871 | error "can only reload from main coroutine"; |
3910 | return; |
3872 | return; |
3911 | } |
3873 | } |
3912 | |
3874 | |
3913 | return if $RELOAD++; |
3875 | return if $RELOAD++; |
3914 | |
3876 | |
3915 | my $t1 = AE::time; |
3877 | my $t1 = AE::time; |
3916 | |
3878 | |
3917 | while ($RELOAD) { |
3879 | while ($RELOAD) { |
|
|
3880 | cf::get_slot 0.1, -1, "reload_perl"; |
3918 | info "reloading..."; |
3881 | info "reloading..."; |
3919 | |
3882 | |
3920 | trace "entering sync_job"; |
3883 | trace "entering sync_job"; |
3921 | |
3884 | |
3922 | cf::sync_job { |
3885 | cf::sync_job { |
3923 | cf::emergency_save; |
3886 | #cf::emergency_save; |
3924 | |
3887 | |
3925 | trace "cancelling all extension coros"; |
3888 | trace "cancelling all extension coros"; |
3926 | $_->cancel for values %EXT_CORO; |
3889 | $_->cancel for values %EXT_CORO; |
3927 | %EXT_CORO = (); |
3890 | %EXT_CORO = (); |
3928 | |
3891 | |
… | |
… | |
4050 | my $bug_warning = 0; |
4013 | my $bug_warning = 0; |
4051 | |
4014 | |
4052 | our @WAIT_FOR_TICK; |
4015 | our @WAIT_FOR_TICK; |
4053 | our @WAIT_FOR_TICK_BEGIN; |
4016 | our @WAIT_FOR_TICK_BEGIN; |
4054 | |
4017 | |
4055 | sub wait_for_tick { |
4018 | sub wait_for_tick() { |
4056 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4019 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4057 | |
4020 | |
4058 | my $signal = new Coro::Signal; |
4021 | my $signal = new Coro::Signal; |
4059 | push @WAIT_FOR_TICK, $signal; |
4022 | push @WAIT_FOR_TICK, $signal; |
4060 | $signal->wait; |
4023 | $signal->wait; |
4061 | } |
4024 | } |
4062 | |
4025 | |
4063 | sub wait_for_tick_begin { |
4026 | sub wait_for_tick_begin() { |
4064 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4027 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4065 | |
4028 | |
4066 | my $signal = new Coro::Signal; |
4029 | my $signal = new Coro::Signal; |
4067 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4030 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4068 | $signal->wait; |
4031 | $signal->wait; |