… | |
… | |
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 |
… | |
… | |
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). 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; |
|
|
2360 | |
|
|
2361 | next if $dir eq "/styles" || $dir eq "/editor"; |
2359 | |
2362 | |
2360 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
2363 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
2361 | or return; |
2364 | or return; |
2362 | |
2365 | |
2363 | for (@$files) { |
2366 | for (@$files) { |
… | |
… | |
2574 | ($x, $y) = (-1, -1) |
2577 | ($x, $y) = (-1, -1) |
2575 | unless (defined $x) && (defined $y); |
2578 | unless (defined $x) && (defined $y); |
2576 | |
2579 | |
2577 | # use -1 or undef as default coordinates, not 0, 0 |
2580 | # use -1 or undef as default coordinates, not 0, 0 |
2578 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2581 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2579 | if $x <=0 && $y <= 0; |
2582 | if $x <= 0 && $y <= 0; |
2580 | |
2583 | |
2581 | $map->load; |
2584 | $map->load; |
2582 | $map->load_neighbours; |
2585 | $map->load_neighbours; |
2583 | |
2586 | |
2584 | return unless $self->contr->active; |
2587 | return unless $self->contr->active; |
… | |
… | |
2781 | sub cf::client::send_drawinfo { |
2784 | sub cf::client::send_drawinfo { |
2782 | my ($self, $text, $flags) = @_; |
2785 | my ($self, $text, $flags) = @_; |
2783 | |
2786 | |
2784 | utf8::encode $text; |
2787 | utf8::encode $text; |
2785 | $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); |
2786 | } |
2814 | } |
2787 | |
2815 | |
2788 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2816 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2789 | |
2817 | |
2790 | 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 |
… | |
… | |
2942 | my $pkt = "msg " |
2970 | my $pkt = "msg " |
2943 | . $self->{json_coder}->encode ( |
2971 | . $self->{json_coder}->encode ( |
2944 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2972 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2945 | ); |
2973 | ); |
2946 | |
2974 | |
2947 | # try lzf for large packets |
|
|
2948 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2949 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2950 | |
|
|
2951 | # split very large packets |
|
|
2952 | if (8192 < length $pkt and $self->{can_lzf}) { |
|
|
2953 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
|
|
2954 | $pkt = "frag"; |
|
|
2955 | } |
|
|
2956 | |
|
|
2957 | $self->send_packet ($pkt); |
2975 | $self->send_big_packet ($pkt); |
2958 | } |
2976 | } |
2959 | |
2977 | |
2960 | =item $client->ext_msg ($type, @msg) |
2978 | =item $client->ext_msg ($type, @msg) |
2961 | |
2979 | |
2962 | Sends an ext event to the client. |
2980 | Sends an ext event to the client. |
… | |
… | |
2965 | |
2983 | |
2966 | sub cf::client::ext_msg($$@) { |
2984 | sub cf::client::ext_msg($$@) { |
2967 | my ($self, $type, @msg) = @_; |
2985 | my ($self, $type, @msg) = @_; |
2968 | |
2986 | |
2969 | if ($self->extcmd == 2) { |
2987 | if ($self->extcmd == 2) { |
2970 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2988 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2971 | } elsif ($self->extcmd == 1) { # TODO: remove |
2989 | } elsif ($self->extcmd == 1) { # TODO: remove |
2972 | push @msg, msgtype => "event_$type"; |
2990 | push @msg, msgtype => "event_$type"; |
2973 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2991 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2974 | } |
2992 | } |
2975 | } |
2993 | } |
2976 | |
2994 | |
2977 | =item $client->ext_reply ($msgid, @msg) |
2995 | =item $client->ext_reply ($msgid, @msg) |
2978 | |
2996 | |
… | |
… | |
2982 | |
3000 | |
2983 | sub cf::client::ext_reply($$@) { |
3001 | sub cf::client::ext_reply($$@) { |
2984 | my ($self, $id, @msg) = @_; |
3002 | my ($self, $id, @msg) = @_; |
2985 | |
3003 | |
2986 | if ($self->extcmd == 2) { |
3004 | if ($self->extcmd == 2) { |
2987 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3005 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2988 | } elsif ($self->extcmd == 1) { |
3006 | } elsif ($self->extcmd == 1) { |
2989 | #TODO: version 1, remove |
3007 | #TODO: version 1, remove |
2990 | unshift @msg, msgtype => "reply", msgid => $id; |
3008 | unshift @msg, msgtype => "reply", msgid => $id; |
2991 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3009 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2992 | } |
3010 | } |
2993 | } |
3011 | } |
2994 | |
3012 | |
2995 | =item $success = $client->query ($flags, "text", \&cb) |
3013 | =item $success = $client->query ($flags, "text", \&cb) |
2996 | |
3014 | |
… | |
… | |
3273 | while (my ($face, $info) = each %$faces) { |
3291 | while (my ($face, $info) = each %$faces) { |
3274 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3292 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3275 | |
3293 | |
3276 | cf::face::set_visibility $idx, $info->{visibility}; |
3294 | cf::face::set_visibility $idx, $info->{visibility}; |
3277 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3295 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3278 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3296 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3279 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3297 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3280 | |
3298 | |
3281 | cf::cede_to_tick; |
3299 | cf::cede_to_tick; |
3282 | } |
3300 | } |
3283 | |
3301 | |
3284 | while (my ($face, $info) = each %$faces) { |
3302 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3308 | |
3326 | |
3309 | cf::anim::invalidate_all; # d'oh |
3327 | cf::anim::invalidate_all; # d'oh |
3310 | } |
3328 | } |
3311 | |
3329 | |
3312 | { |
3330 | { |
3313 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3314 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3315 | my $res = $facedata->{resource}; |
3331 | my $res = $facedata->{resource}; |
3316 | |
3332 | |
3317 | while (my ($name, $info) = each %$res) { |
3333 | while (my ($name, $info) = each %$res) { |
3318 | if (defined $info->{type}) { |
3334 | if (defined $info->{type}) { |
3319 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3335 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3320 | my $data; |
|
|
3321 | |
3336 | |
3322 | if ($info->{type} & 1) { |
3337 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3323 | # prepend meta info |
|
|
3324 | |
|
|
3325 | my $meta = $enc->encode ({ |
|
|
3326 | name => $name, |
|
|
3327 | %{ $info->{meta} || {} }, |
|
|
3328 | }); |
|
|
3329 | |
|
|
3330 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3331 | } else { |
|
|
3332 | $data = $info->{data}; |
|
|
3333 | } |
|
|
3334 | |
|
|
3335 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3336 | cf::face::set_type $idx, $info->{type}; |
3338 | cf::face::set_type $idx, $info->{type}; |
3337 | } else { |
3339 | } else { |
3338 | $RESOURCE{$name} = $info; |
3340 | $RESOURCE{$name} = $info; |
3339 | } |
3341 | } |
3340 | |
3342 | |
… | |
… | |
3503 | }; |
3505 | }; |
3504 | |
3506 | |
3505 | evthread_start IO::AIO::poll_fileno; |
3507 | evthread_start IO::AIO::poll_fileno; |
3506 | |
3508 | |
3507 | cf::sync_job { |
3509 | cf::sync_job { |
|
|
3510 | cf::load_settings; |
|
|
3511 | cf::load_materials; |
|
|
3512 | |
3508 | reload_resources; |
3513 | reload_resources; |
3509 | reload_config; |
3514 | reload_config; |
3510 | db_init; |
3515 | db_init; |
3511 | |
3516 | |
3512 | cf::load_settings; |
|
|
3513 | cf::load_materials; |
|
|
3514 | cf::init_uuid; |
3517 | cf::init_uuid; |
3515 | cf::init_signals; |
3518 | cf::init_signals; |
3516 | cf::init_commands; |
3519 | cf::init_commands; |
3517 | cf::init_skills; |
3520 | cf::init_skills; |
3518 | |
3521 | |