… | |
… | |
21 | use Coro::Semaphore; |
21 | use Coro::Semaphore; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
23 | use Coro::Storable; |
23 | use Coro::Storable; |
24 | use Coro::Util (); |
24 | use Coro::Util (); |
25 | |
25 | |
26 | use JSON::XS 1.4 (); |
26 | use JSON::XS (); |
27 | use BDB (); |
27 | use BDB (); |
28 | use Data::Dumper; |
28 | use Data::Dumper; |
29 | use Digest::MD5; |
29 | use Digest::MD5; |
30 | use Fcntl; |
30 | use Fcntl; |
31 | use YAML::Syck (); |
31 | use YAML::Syck (); |
… | |
… | |
1849 | local $self->{deny_reset} = 1; # loading can take a long time |
1849 | local $self->{deny_reset} = 1; # loading can take a long time |
1850 | |
1850 | |
1851 | my $path = $self->{path}; |
1851 | my $path = $self->{path}; |
1852 | |
1852 | |
1853 | { |
1853 | { |
|
|
1854 | my $guard1 = cf::lock_acquire "map_data:$path"; |
1854 | my $guard = cf::lock_acquire "map_load:$path"; |
1855 | my $guard2 = cf::lock_acquire "map_load:$path"; |
1855 | |
1856 | |
1856 | return if $self->in_memory != cf::MAP_SWAPPED; |
1857 | return if $self->in_memory != cf::MAP_SWAPPED; |
1857 | |
1858 | |
1858 | $self->in_memory (cf::MAP_LOADING); |
1859 | $self->in_memory (cf::MAP_LOADING); |
1859 | |
1860 | |
… | |
… | |
1947 | } |
1948 | } |
1948 | |
1949 | |
1949 | sub do_load_sync { |
1950 | sub do_load_sync { |
1950 | my ($map) = @_; |
1951 | my ($map) = @_; |
1951 | |
1952 | |
|
|
1953 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
|
|
1954 | if $Coro::current == $Coro::main; |
|
|
1955 | |
1952 | cf::sync_job { $map->load }; |
1956 | cf::sync_job { $map->load }; |
1953 | } |
1957 | } |
1954 | |
1958 | |
1955 | our %MAP_PREFETCH; |
1959 | our %MAP_PREFETCH; |
1956 | our $MAP_PREFETCHER = undef; |
1960 | our $MAP_PREFETCHER = undef; |
1957 | |
1961 | |
1958 | sub find_async { |
1962 | sub find_async { |
1959 | my ($path, $origin) = @_; |
1963 | my ($path, $origin, $load) = @_; |
1960 | |
1964 | |
1961 | $path = normalise $path, $origin && $origin->{path}; |
1965 | $path = normalise $path, $origin && $origin->{path}; |
1962 | |
1966 | |
1963 | if (my $map = $cf::MAP{$path}) { |
1967 | if (my $map = $cf::MAP{$path}) { |
1964 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1968 | return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY; |
1965 | } |
1969 | } |
1966 | |
1970 | |
1967 | undef $MAP_PREFETCH{$path}; |
1971 | $MAP_PREFETCH{$path} |= $load; |
|
|
1972 | |
1968 | $MAP_PREFETCHER ||= cf::async { |
1973 | $MAP_PREFETCHER ||= cf::async { |
1969 | while (%MAP_PREFETCH) { |
1974 | while (%MAP_PREFETCH) { |
1970 | for my $path (keys %MAP_PREFETCH) { |
1975 | while (my ($k, $v) = each %MAP_PREFETCH) { |
1971 | if (my $map = find $path) { |
1976 | if (my $map = find $k) { |
1972 | $map->load; |
1977 | $map->load if $v; |
1973 | } |
1978 | } |
1974 | |
1979 | |
1975 | delete $MAP_PREFETCH{$path}; |
1980 | delete $MAP_PREFETCH{$k}; |
1976 | } |
1981 | } |
1977 | } |
1982 | } |
1978 | undef $MAP_PREFETCHER; |
1983 | undef $MAP_PREFETCHER; |
1979 | }; |
1984 | }; |
1980 | $MAP_PREFETCHER->prio (6); |
1985 | $MAP_PREFETCHER->prio (6); |
… | |
… | |
1983 | } |
1988 | } |
1984 | |
1989 | |
1985 | sub save { |
1990 | sub save { |
1986 | my ($self) = @_; |
1991 | my ($self) = @_; |
1987 | |
1992 | |
1988 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1993 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1989 | |
1994 | |
1990 | $self->{last_save} = $cf::RUNTIME; |
1995 | $self->{last_save} = $cf::RUNTIME; |
1991 | |
1996 | |
1992 | return unless $self->dirty; |
1997 | return unless $self->dirty; |
1993 | |
1998 | |
… | |
… | |
2016 | my ($self) = @_; |
2021 | my ($self) = @_; |
2017 | |
2022 | |
2018 | # save first because save cedes |
2023 | # save first because save cedes |
2019 | $self->save; |
2024 | $self->save; |
2020 | |
2025 | |
2021 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
2026 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2022 | |
2027 | |
2023 | return if $self->players; |
2028 | return if $self->players; |
2024 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2029 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2025 | return if $self->{deny_save}; |
2030 | return if $self->{deny_save}; |
2026 | |
2031 | |
… | |
… | |
2554 | =cut |
2559 | =cut |
2555 | |
2560 | |
2556 | sub cf::client::ext_msg($$@) { |
2561 | sub cf::client::ext_msg($$@) { |
2557 | my ($self, $type, @msg) = @_; |
2562 | my ($self, $type, @msg) = @_; |
2558 | |
2563 | |
2559 | my $extcmd = $self->extcmd; |
|
|
2560 | |
|
|
2561 | if ($extcmd == 2) { |
2564 | if ($self->extcmd == 2) { |
2562 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2565 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2563 | } elsif ($extcmd == 1) { # TODO: remove |
2566 | } elsif ($self->extcmd == 1) { # TODO: remove |
2564 | push @msg, msgtype => "event_$type"; |
2567 | push @msg, msgtype => "event_$type"; |
2565 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2568 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2566 | } |
2569 | } |
2567 | } |
2570 | } |
2568 | |
2571 | |
… | |
… | |
2575 | sub cf::client::ext_reply($$@) { |
2578 | sub cf::client::ext_reply($$@) { |
2576 | my ($self, $id, @msg) = @_; |
2579 | my ($self, $id, @msg) = @_; |
2577 | |
2580 | |
2578 | if ($self->extcmd == 2) { |
2581 | if ($self->extcmd == 2) { |
2579 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2582 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2580 | } elsif ($self->ns->extcmd == 1) { |
2583 | } elsif ($self->extcmd == 1) { |
2581 | #TODO: version 1, remove |
2584 | #TODO: version 1, remove |
2582 | unshift @msg, msgtype => "reply", msgid => $id; |
2585 | unshift @msg, msgtype => "reply", msgid => $id; |
2583 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2586 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2584 | } |
2587 | } |
2585 | } |
2588 | } |
… | |
… | |
2728 | |
2731 | |
2729 | The following functions and methods are available within a safe environment: |
2732 | The following functions and methods are available within a safe environment: |
2730 | |
2733 | |
2731 | cf::object |
2734 | cf::object |
2732 | contr pay_amount pay_player map x y force_find force_add |
2735 | contr pay_amount pay_player map x y force_find force_add |
2733 | insert remove name archname title slaying race |
2736 | insert remove name archname title slaying race decrease_ob_nr |
2734 | |
2737 | |
2735 | cf::object::player |
2738 | cf::object::player |
2736 | player |
2739 | player |
2737 | |
2740 | |
2738 | cf::player |
2741 | cf::player |
… | |
… | |
2743 | |
2746 | |
2744 | =cut |
2747 | =cut |
2745 | |
2748 | |
2746 | for ( |
2749 | for ( |
2747 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2750 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2748 | insert remove inv name archname title slaying race)], |
2751 | insert remove inv name archname title slaying race |
|
|
2752 | decrease_ob_nr)], |
2749 | ["cf::object::player" => qw(player)], |
2753 | ["cf::object::player" => qw(player)], |
2750 | ["cf::player" => qw(peaceful)], |
2754 | ["cf::player" => qw(peaceful)], |
2751 | ["cf::map" => qw(trigger)], |
2755 | ["cf::map" => qw(trigger)], |
2752 | ) { |
2756 | ) { |
2753 | no strict 'refs'; |
2757 | no strict 'refs'; |
… | |
… | |
2829 | # the server's init and main functions |
2833 | # the server's init and main functions |
2830 | |
2834 | |
2831 | sub load_facedata($) { |
2835 | sub load_facedata($) { |
2832 | my ($path) = @_; |
2836 | my ($path) = @_; |
2833 | |
2837 | |
2834 | my $enc = JSON::XS->new->utf8->canonical; |
2838 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
2835 | |
2839 | |
2836 | warn "loading facedata from $path\n"; |
2840 | warn "loading facedata from $path\n"; |
2837 | |
2841 | |
2838 | my $facedata; |
2842 | my $facedata; |
2839 | 0 < aio_load $path, $facedata |
2843 | 0 < aio_load $path, $facedata |
… | |
… | |
2994 | |
2998 | |
2995 | sub init { |
2999 | sub init { |
2996 | reload_resources; |
3000 | reload_resources; |
2997 | } |
3001 | } |
2998 | |
3002 | |
2999 | sub cfg_load { |
3003 | sub reload_config { |
3000 | open my $fh, "<:utf8", "$CONFDIR/config" |
3004 | open my $fh, "<:utf8", "$CONFDIR/config" |
3001 | or return; |
3005 | or return; |
3002 | |
3006 | |
3003 | local $/; |
3007 | local $/; |
3004 | *CFG = YAML::Syck::Load <$fh>; |
3008 | *CFG = YAML::Syck::Load <$fh>; |
… | |
… | |
3024 | (async { |
3028 | (async { |
3025 | Event::one_event; |
3029 | Event::one_event; |
3026 | })->prio (Coro::PRIO_MAX); |
3030 | })->prio (Coro::PRIO_MAX); |
3027 | }; |
3031 | }; |
3028 | |
3032 | |
3029 | cfg_load; |
3033 | reload_config; |
3030 | db_init; |
3034 | db_init; |
3031 | load_extensions; |
3035 | load_extensions; |
3032 | |
3036 | |
3033 | $TICK_WATCHER->start; |
3037 | $TICK_WATCHER->start; |
3034 | Event::loop; |
3038 | Event::loop; |
… | |
… | |
3227 | warn "reloading cf.pm"; |
3231 | warn "reloading cf.pm"; |
3228 | require cf; |
3232 | require cf; |
3229 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3233 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3230 | |
3234 | |
3231 | warn "loading config and database again"; |
3235 | warn "loading config and database again"; |
3232 | cf::cfg_load; |
3236 | cf::reload_config; |
3233 | |
3237 | |
3234 | warn "loading extensions"; |
3238 | warn "loading extensions"; |
3235 | cf::load_extensions; |
3239 | cf::load_extensions; |
3236 | |
3240 | |
3237 | warn "reattaching attachments to objects/players"; |
3241 | warn "reattaching attachments to objects/players"; |