… | |
… | |
32 | use EV; |
32 | use EV; |
33 | use Opcode; |
33 | use Opcode; |
34 | use Safe; |
34 | use Safe; |
35 | use Safe::Hole; |
35 | use Safe::Hole; |
36 | use Storable (); |
36 | use Storable (); |
|
|
37 | use Carp (); |
37 | |
38 | |
38 | use Guard (); |
39 | use Guard (); |
39 | use Coro (); |
40 | use Coro (); |
40 | use Coro::State; |
41 | use Coro::State; |
41 | use Coro::Handle; |
42 | use Coro::Handle; |
… | |
… | |
52 | use Coro::Util (); |
53 | use Coro::Util (); |
53 | |
54 | |
54 | use JSON::XS 2.01 (); |
55 | use JSON::XS 2.01 (); |
55 | use BDB (); |
56 | use BDB (); |
56 | use Data::Dumper; |
57 | use Data::Dumper; |
57 | use Digest::MD5; |
|
|
58 | use Fcntl; |
58 | use Fcntl; |
59 | use YAML::XS (); |
59 | use YAML::XS (); |
60 | use IO::AIO (); |
60 | use IO::AIO (); |
61 | use Time::HiRes; |
61 | use Time::HiRes; |
62 | use Compress::LZF; |
62 | use Compress::LZF; |
… | |
… | |
289 | )) { |
289 | )) { |
290 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
290 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
291 | } |
291 | } |
292 | |
292 | |
293 | $EV::DIED = sub { |
293 | $EV::DIED = sub { |
294 | warn "error in event callback: @_"; |
294 | Carp::cluck "error in event callback: @_"; |
295 | }; |
295 | }; |
296 | |
296 | |
297 | ############################################################################# |
297 | ############################################################################# |
298 | |
298 | |
299 | =head2 UTILITY FUNCTIONS |
299 | =head2 UTILITY FUNCTIONS |
… | |
… | |
2009 | |
2009 | |
2010 | $cf::MAP{$path} = $map |
2010 | $cf::MAP{$path} = $map |
2011 | } |
2011 | } |
2012 | } |
2012 | } |
2013 | |
2013 | |
2014 | sub pre_load { } |
2014 | sub pre_load { } |
2015 | sub post_load { } |
2015 | #sub post_load { } # XS |
2016 | |
2016 | |
2017 | sub load { |
2017 | sub load { |
2018 | my ($self) = @_; |
2018 | my ($self) = @_; |
2019 | |
2019 | |
2020 | local $self->{deny_reset} = 1; # loading can take a long time |
2020 | local $self->{deny_reset} = 1; # loading can take a long time |
… | |
… | |
2344 | } |
2344 | } |
2345 | |
2345 | |
2346 | =item cf::map::static_maps |
2346 | =item cf::map::static_maps |
2347 | |
2347 | |
2348 | Returns an arrayref if paths of all static maps (all preinstalled F<.map> |
2348 | Returns an arrayref if paths of all static maps (all preinstalled F<.map> |
2349 | file in the shared directory excluding F</styles>). May block. |
2349 | file in the shared directory excluding F</styles> and F</editor>). May |
|
|
2350 | block. |
2350 | |
2351 | |
2351 | =cut |
2352 | =cut |
2352 | |
2353 | |
2353 | sub static_maps() { |
2354 | sub static_maps() { |
2354 | my @dirs = ""; |
2355 | my @dirs = ""; |
2355 | my @maps; |
2356 | my @maps; |
2356 | |
2357 | |
2357 | while (@dirs) { |
2358 | while (@dirs) { |
2358 | my $dir = shift @dirs; |
2359 | my $dir = shift @dirs; |
2359 | |
2360 | |
2360 | next if $dir eq "/styles"; |
2361 | next if $dir eq "/styles" || $dir eq "/editor"; |
2361 | |
2362 | |
2362 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
2363 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
2363 | or return; |
2364 | or return; |
2364 | |
2365 | |
2365 | for (@$files) { |
2366 | for (@$files) { |
… | |
… | |
2576 | ($x, $y) = (-1, -1) |
2577 | ($x, $y) = (-1, -1) |
2577 | unless (defined $x) && (defined $y); |
2578 | unless (defined $x) && (defined $y); |
2578 | |
2579 | |
2579 | # use -1 or undef as default coordinates, not 0, 0 |
2580 | # use -1 or undef as default coordinates, not 0, 0 |
2580 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2581 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2581 | if $x <=0 && $y <= 0; |
2582 | if $x <= 0 && $y <= 0; |
2582 | |
2583 | |
2583 | $map->load; |
2584 | $map->load; |
2584 | $map->load_neighbours; |
2585 | $map->load_neighbours; |
2585 | |
2586 | |
2586 | return unless $self->contr->active; |
2587 | return unless $self->contr->active; |
… | |
… | |
2783 | sub cf::client::send_drawinfo { |
2784 | sub cf::client::send_drawinfo { |
2784 | my ($self, $text, $flags) = @_; |
2785 | my ($self, $text, $flags) = @_; |
2785 | |
2786 | |
2786 | utf8::encode $text; |
2787 | utf8::encode $text; |
2787 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2788 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
|
|
2789 | } |
|
|
2790 | |
|
|
2791 | =item $client->send_big_packet ($pkt) |
|
|
2792 | |
|
|
2793 | Like C<send_packet>, but tries to compress large packets, and fragments |
|
|
2794 | them as required. |
|
|
2795 | |
|
|
2796 | =cut |
|
|
2797 | |
|
|
2798 | our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64; |
|
|
2799 | |
|
|
2800 | sub cf::client::send_big_packet { |
|
|
2801 | my ($self, $pkt) = @_; |
|
|
2802 | |
|
|
2803 | # try lzf for large packets |
|
|
2804 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2805 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2806 | |
|
|
2807 | # split very large packets |
|
|
2808 | if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) { |
|
|
2809 | $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt; |
|
|
2810 | $pkt = "frag"; |
|
|
2811 | } |
|
|
2812 | |
|
|
2813 | $self->send_packet ($pkt); |
2788 | } |
2814 | } |
2789 | |
2815 | |
2790 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2816 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2791 | |
2817 | |
2792 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2818 | Send a drawinfo or msg packet to the client, formatting the msg for the |
… | |
… | |
2944 | my $pkt = "msg " |
2970 | my $pkt = "msg " |
2945 | . $self->{json_coder}->encode ( |
2971 | . $self->{json_coder}->encode ( |
2946 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2972 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2947 | ); |
2973 | ); |
2948 | |
2974 | |
2949 | # try lzf for large packets |
|
|
2950 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2951 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2952 | |
|
|
2953 | # split very large packets |
|
|
2954 | if (8192 < length $pkt and $self->{can_lzf}) { |
|
|
2955 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
|
|
2956 | $pkt = "frag"; |
|
|
2957 | } |
|
|
2958 | |
|
|
2959 | $self->send_packet ($pkt); |
2975 | $self->send_big_packet ($pkt); |
2960 | } |
2976 | } |
2961 | |
2977 | |
2962 | =item $client->ext_msg ($type, @msg) |
2978 | =item $client->ext_msg ($type, @msg) |
2963 | |
2979 | |
2964 | Sends an ext event to the client. |
2980 | Sends an ext event to the client. |
… | |
… | |
2967 | |
2983 | |
2968 | sub cf::client::ext_msg($$@) { |
2984 | sub cf::client::ext_msg($$@) { |
2969 | my ($self, $type, @msg) = @_; |
2985 | my ($self, $type, @msg) = @_; |
2970 | |
2986 | |
2971 | if ($self->extcmd == 2) { |
2987 | if ($self->extcmd == 2) { |
2972 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2988 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2973 | } elsif ($self->extcmd == 1) { # TODO: remove |
2989 | } elsif ($self->extcmd == 1) { # TODO: remove |
2974 | push @msg, msgtype => "event_$type"; |
2990 | push @msg, msgtype => "event_$type"; |
2975 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2991 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2976 | } |
2992 | } |
2977 | } |
2993 | } |
2978 | |
2994 | |
2979 | =item $client->ext_reply ($msgid, @msg) |
2995 | =item $client->ext_reply ($msgid, @msg) |
2980 | |
2996 | |
… | |
… | |
2984 | |
3000 | |
2985 | sub cf::client::ext_reply($$@) { |
3001 | sub cf::client::ext_reply($$@) { |
2986 | my ($self, $id, @msg) = @_; |
3002 | my ($self, $id, @msg) = @_; |
2987 | |
3003 | |
2988 | if ($self->extcmd == 2) { |
3004 | if ($self->extcmd == 2) { |
2989 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3005 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2990 | } elsif ($self->extcmd == 1) { |
3006 | } elsif ($self->extcmd == 1) { |
2991 | #TODO: version 1, remove |
3007 | #TODO: version 1, remove |
2992 | unshift @msg, msgtype => "reply", msgid => $id; |
3008 | unshift @msg, msgtype => "reply", msgid => $id; |
2993 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3009 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2994 | } |
3010 | } |
2995 | } |
3011 | } |
2996 | |
3012 | |
2997 | =item $success = $client->query ($flags, "text", \&cb) |
3013 | =item $success = $client->query ($flags, "text", \&cb) |
2998 | |
3014 | |
… | |
… | |
3125 | our $safe_hole = new Safe::Hole; |
3141 | our $safe_hole = new Safe::Hole; |
3126 | |
3142 | |
3127 | $SIG{FPE} = 'IGNORE'; |
3143 | $SIG{FPE} = 'IGNORE'; |
3128 | |
3144 | |
3129 | $safe->permit_only (Opcode::opset qw( |
3145 | $safe->permit_only (Opcode::opset qw( |
3130 | :base_core :base_mem :base_orig :base_math |
3146 | :base_core :base_mem :base_orig :base_math :base_loop |
3131 | grepstart grepwhile mapstart mapwhile |
3147 | grepstart grepwhile mapstart mapwhile |
3132 | sort time |
3148 | sort time |
3133 | )); |
3149 | )); |
3134 | |
3150 | |
3135 | # here we export the classes and methods available to script code |
3151 | # here we export the classes and methods available to script code |
… | |
… | |
3187 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3203 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3188 | $qcode =~ s/\n/\\n/g; |
3204 | $qcode =~ s/\n/\\n/g; |
3189 | |
3205 | |
3190 | %vars = (_dummy => 0) unless %vars; |
3206 | %vars = (_dummy => 0) unless %vars; |
3191 | |
3207 | |
|
|
3208 | my @res; |
3192 | local $_; |
3209 | local $_; |
3193 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
3194 | |
3210 | |
3195 | my $eval = |
3211 | my $eval = |
3196 | "do {\n" |
3212 | "do {\n" |
3197 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3213 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3198 | . "#line 0 \"{$qcode}\"\n" |
3214 | . "#line 0 \"{$qcode}\"\n" |
3199 | . $code |
3215 | . $code |
3200 | . "\n}" |
3216 | . "\n}" |
3201 | ; |
3217 | ; |
3202 | |
3218 | |
|
|
3219 | if ($CFG{safe_eval}) { |
3203 | sub_generation_inc; |
3220 | sub_generation_inc; |
|
|
3221 | local @safe::cf::_safe_eval_args = values %vars; |
3204 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3222 | @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3205 | sub_generation_inc; |
3223 | sub_generation_inc; |
|
|
3224 | } else { |
|
|
3225 | local @cf::_safe_eval_args = values %vars; |
|
|
3226 | @res = wantarray ? eval eval : scalar eval $eval; |
|
|
3227 | } |
3206 | |
3228 | |
3207 | if ($@) { |
3229 | if ($@) { |
3208 | warn "$@"; |
3230 | warn "$@"; |
3209 | warn "while executing safe code '$code'\n"; |
3231 | warn "while executing safe code '$code'\n"; |
3210 | warn "with arguments " . (join " ", %vars) . "\n"; |
3232 | warn "with arguments " . (join " ", %vars) . "\n"; |
… | |
… | |
3229 | =cut |
3251 | =cut |
3230 | |
3252 | |
3231 | sub register_script_function { |
3253 | sub register_script_function { |
3232 | my ($fun, $cb) = @_; |
3254 | my ($fun, $cb) = @_; |
3233 | |
3255 | |
3234 | no strict 'refs'; |
3256 | $fun = "safe::$fun" if $CFG{safe_eval}; |
3235 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
3257 | *$fun = $safe_hole->wrap ($cb); |
3236 | } |
3258 | } |
3237 | |
3259 | |
3238 | =back |
3260 | =back |
3239 | |
3261 | |
3240 | =cut |
3262 | =cut |
… | |
… | |
3261 | |
3283 | |
3262 | $facedata->{version} == 2 |
3284 | $facedata->{version} == 2 |
3263 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3285 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3264 | |
3286 | |
3265 | # patch in the exptable |
3287 | # patch in the exptable |
|
|
3288 | my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]); |
3266 | $facedata->{resource}{"res/exp_table"} = { |
3289 | $facedata->{resource}{"res/exp_table"} = { |
3267 | type => FT_RSRC, |
3290 | type => FT_RSRC, |
3268 | data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), |
3291 | data => $exp_table, |
|
|
3292 | hash => (Digest::MD5::md5 $exp_table), |
3269 | }; |
3293 | }; |
3270 | cf::cede_to_tick; |
3294 | cf::cede_to_tick; |
3271 | |
3295 | |
3272 | { |
3296 | { |
3273 | my $faces = $facedata->{faceinfo}; |
3297 | my $faces = $facedata->{faceinfo}; |
… | |
… | |
3275 | while (my ($face, $info) = each %$faces) { |
3299 | while (my ($face, $info) = each %$faces) { |
3276 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3300 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3277 | |
3301 | |
3278 | cf::face::set_visibility $idx, $info->{visibility}; |
3302 | cf::face::set_visibility $idx, $info->{visibility}; |
3279 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3303 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3280 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3304 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3281 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3305 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3282 | |
3306 | |
3283 | cf::cede_to_tick; |
3307 | cf::cede_to_tick; |
3284 | } |
3308 | } |
3285 | |
3309 | |
3286 | while (my ($face, $info) = each %$faces) { |
3310 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3310 | |
3334 | |
3311 | cf::anim::invalidate_all; # d'oh |
3335 | cf::anim::invalidate_all; # d'oh |
3312 | } |
3336 | } |
3313 | |
3337 | |
3314 | { |
3338 | { |
3315 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3316 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3317 | my $res = $facedata->{resource}; |
3339 | my $res = $facedata->{resource}; |
3318 | |
3340 | |
3319 | while (my ($name, $info) = each %$res) { |
3341 | while (my ($name, $info) = each %$res) { |
3320 | if (defined $info->{type}) { |
3342 | if (defined $info->{type}) { |
3321 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3343 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3322 | my $data; |
|
|
3323 | |
3344 | |
3324 | if ($info->{type} & 1) { |
3345 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3325 | # prepend meta info |
|
|
3326 | |
|
|
3327 | my $meta = $enc->encode ({ |
|
|
3328 | name => $name, |
|
|
3329 | %{ $info->{meta} || {} }, |
|
|
3330 | }); |
|
|
3331 | |
|
|
3332 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3333 | } else { |
|
|
3334 | $data = $info->{data}; |
|
|
3335 | } |
|
|
3336 | |
|
|
3337 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3338 | cf::face::set_type $idx, $info->{type}; |
3346 | cf::face::set_type $idx, $info->{type}; |
3339 | } else { |
3347 | } else { |
3340 | $RESOURCE{$name} = $info; |
3348 | $RESOURCE{$name} = $info; |
3341 | } |
3349 | } |
3342 | |
3350 | |
… | |
… | |
3505 | }; |
3513 | }; |
3506 | |
3514 | |
3507 | evthread_start IO::AIO::poll_fileno; |
3515 | evthread_start IO::AIO::poll_fileno; |
3508 | |
3516 | |
3509 | cf::sync_job { |
3517 | cf::sync_job { |
|
|
3518 | cf::load_settings; |
|
|
3519 | cf::load_materials; |
|
|
3520 | |
3510 | reload_resources; |
3521 | reload_resources; |
3511 | reload_config; |
3522 | reload_config; |
3512 | db_init; |
3523 | db_init; |
3513 | |
3524 | |
3514 | cf::load_settings; |
|
|
3515 | cf::load_materials; |
|
|
3516 | cf::init_uuid; |
3525 | cf::init_uuid; |
3517 | cf::init_signals; |
3526 | cf::init_signals; |
3518 | cf::init_commands; |
3527 | cf::init_commands; |
3519 | cf::init_skills; |
3528 | cf::init_skills; |
3520 | |
3529 | |
… | |
… | |
3879 | |
3888 | |
3880 | our @WAIT_FOR_TICK; |
3889 | our @WAIT_FOR_TICK; |
3881 | our @WAIT_FOR_TICK_BEGIN; |
3890 | our @WAIT_FOR_TICK_BEGIN; |
3882 | |
3891 | |
3883 | sub wait_for_tick { |
3892 | sub wait_for_tick { |
3884 | return if tick_inhibit || $Coro::current == $Coro::main; |
3893 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3885 | |
3894 | |
3886 | my $signal = new Coro::Signal; |
3895 | my $signal = new Coro::Signal; |
3887 | push @WAIT_FOR_TICK, $signal; |
3896 | push @WAIT_FOR_TICK, $signal; |
3888 | $signal->wait; |
3897 | $signal->wait; |
3889 | } |
3898 | } |
3890 | |
3899 | |
3891 | sub wait_for_tick_begin { |
3900 | sub wait_for_tick_begin { |
3892 | return if tick_inhibit || $Coro::current == $Coro::main; |
3901 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3893 | |
3902 | |
3894 | my $signal = new Coro::Signal; |
3903 | my $signal = new Coro::Signal; |
3895 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3904 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3896 | $signal->wait; |
3905 | $signal->wait; |
3897 | } |
3906 | } |