ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.489 by root, Fri Oct 23 03:08:35 2009 UTC vs.
Revision 1.501 by root, Sat Jan 16 23:27:21 2010 UTC

32use EV; 32use EV;
33use Opcode; 33use Opcode;
34use Safe; 34use Safe;
35use Safe::Hole; 35use Safe::Hole;
36use Storable (); 36use Storable ();
37use Carp ();
37 38
38use Guard (); 39use Guard ();
39use Coro (); 40use Coro ();
40use Coro::State; 41use Coro::State;
41use Coro::Handle; 42use Coro::Handle;
52use Coro::Util (); 53use Coro::Util ();
53 54
54use JSON::XS 2.01 (); 55use JSON::XS 2.01 ();
55use BDB (); 56use BDB ();
56use Data::Dumper; 57use Data::Dumper;
57use Digest::MD5;
58use Fcntl; 58use Fcntl;
59use YAML::XS (); 59use YAML::XS ();
60use IO::AIO (); 60use IO::AIO ();
61use Time::HiRes; 61use Time::HiRes;
62use Compress::LZF; 62use 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
2014sub pre_load { } 2014sub pre_load { }
2015sub post_load { } 2015#sub post_load { } # XS
2016 2016
2017sub load { 2017sub 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
2348Returns an arrayref if paths of all static maps (all preinstalled F<.map> 2348Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2349file in the shared directory). May block. 2349file in the shared directory excluding F</styles> and F</editor>). May
2350block.
2350 2351
2351=cut 2352=cut
2352 2353
2353sub static_maps() { 2354sub 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;
2781sub cf::client::send_drawinfo { 2784sub 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
2793Like C<send_packet>, but tries to compress large packets, and fragments
2794them as required.
2795
2796=cut
2797
2798our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64;
2799
2800sub 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
2790Send a drawinfo or msg packet to the client, formatting the msg for the 2818Send 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
2962Sends an ext event to the client. 2980Sends an ext event to the client.
2965 2983
2966sub cf::client::ext_msg($$@) { 2984sub 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
2983sub cf::client::ext_reply($$@) { 3001sub 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
3123our $safe_hole = new Safe::Hole; 3141our $safe_hole = new Safe::Hole;
3124 3142
3125$SIG{FPE} = 'IGNORE'; 3143$SIG{FPE} = 'IGNORE';
3126 3144
3127$safe->permit_only (Opcode::opset qw( 3145$safe->permit_only (Opcode::opset qw(
3128 :base_core :base_mem :base_orig :base_math 3146 :base_core :base_mem :base_orig :base_math :base_loop
3129 grepstart grepwhile mapstart mapwhile 3147 grepstart grepwhile mapstart mapwhile
3130 sort time 3148 sort time
3131)); 3149));
3132 3150
3133# here we export the classes and methods available to script code 3151# here we export the classes and methods available to script code
3185 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3203 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3186 $qcode =~ s/\n/\\n/g; 3204 $qcode =~ s/\n/\\n/g;
3187 3205
3188 %vars = (_dummy => 0) unless %vars; 3206 %vars = (_dummy => 0) unless %vars;
3189 3207
3208 my @res;
3190 local $_; 3209 local $_;
3191 local @safe::cf::_safe_eval_args = values %vars;
3192 3210
3193 my $eval = 3211 my $eval =
3194 "do {\n" 3212 "do {\n"
3195 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3213 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3196 . "#line 0 \"{$qcode}\"\n" 3214 . "#line 0 \"{$qcode}\"\n"
3197 . $code 3215 . $code
3198 . "\n}" 3216 . "\n}"
3199 ; 3217 ;
3200 3218
3219 if ($CFG{safe_eval}) {
3201 sub_generation_inc; 3220 sub_generation_inc;
3221 local @safe::cf::_safe_eval_args = values %vars;
3202 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3222 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3203 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 }
3204 3228
3205 if ($@) { 3229 if ($@) {
3206 warn "$@"; 3230 warn "$@";
3207 warn "while executing safe code '$code'\n"; 3231 warn "while executing safe code '$code'\n";
3208 warn "with arguments " . (join " ", %vars) . "\n"; 3232 warn "with arguments " . (join " ", %vars) . "\n";
3227=cut 3251=cut
3228 3252
3229sub register_script_function { 3253sub register_script_function {
3230 my ($fun, $cb) = @_; 3254 my ($fun, $cb) = @_;
3231 3255
3232 no strict 'refs'; 3256 $fun = "safe::$fun" if $CFG{safe_eval};
3233 *{"safe::$fun"} = $safe_hole->wrap ($cb); 3257 *$fun = $safe_hole->wrap ($cb);
3234} 3258}
3235 3259
3236=back 3260=back
3237 3261
3238=cut 3262=cut
3259 3283
3260 $facedata->{version} == 2 3284 $facedata->{version} == 2
3261 or cf::cleanup "$path: version mismatch, cannot proceed."; 3285 or cf::cleanup "$path: version mismatch, cannot proceed.";
3262 3286
3263 # 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]);
3264 $facedata->{resource}{"res/exp_table"} = { 3289 $facedata->{resource}{"res/exp_table"} = {
3265 type => FT_RSRC, 3290 type => FT_RSRC,
3266 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),
3267 }; 3293 };
3268 cf::cede_to_tick; 3294 cf::cede_to_tick;
3269 3295
3270 { 3296 {
3271 my $faces = $facedata->{faceinfo}; 3297 my $faces = $facedata->{faceinfo};
3273 while (my ($face, $info) = each %$faces) { 3299 while (my ($face, $info) = each %$faces) {
3274 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3300 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3275 3301
3276 cf::face::set_visibility $idx, $info->{visibility}; 3302 cf::face::set_visibility $idx, $info->{visibility};
3277 cf::face::set_magicmap $idx, $info->{magicmap}; 3303 cf::face::set_magicmap $idx, $info->{magicmap};
3278 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3304 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3279 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3305 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3280 3306
3281 cf::cede_to_tick; 3307 cf::cede_to_tick;
3282 } 3308 }
3283 3309
3284 while (my ($face, $info) = each %$faces) { 3310 while (my ($face, $info) = each %$faces) {
3308 3334
3309 cf::anim::invalidate_all; # d'oh 3335 cf::anim::invalidate_all; # d'oh
3310 } 3336 }
3311 3337
3312 { 3338 {
3313 # TODO: for gcfclient pleasure, we should give resources
3314 # that gcfclient doesn't grok a >10000 face index.
3315 my $res = $facedata->{resource}; 3339 my $res = $facedata->{resource};
3316 3340
3317 while (my ($name, $info) = each %$res) { 3341 while (my ($name, $info) = each %$res) {
3318 if (defined $info->{type}) { 3342 if (defined $info->{type}) {
3319 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3343 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3320 my $data;
3321 3344
3322 if ($info->{type} & 1) { 3345 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}; 3346 cf::face::set_type $idx, $info->{type};
3337 } else { 3347 } else {
3338 $RESOURCE{$name} = $info; 3348 $RESOURCE{$name} = $info;
3339 } 3349 }
3340 3350
3503 }; 3513 };
3504 3514
3505 evthread_start IO::AIO::poll_fileno; 3515 evthread_start IO::AIO::poll_fileno;
3506 3516
3507 cf::sync_job { 3517 cf::sync_job {
3518 cf::load_settings;
3519 cf::load_materials;
3520
3508 reload_resources; 3521 reload_resources;
3509 reload_config; 3522 reload_config;
3510 db_init; 3523 db_init;
3511 3524
3512 cf::load_settings;
3513 cf::load_materials;
3514 cf::init_uuid; 3525 cf::init_uuid;
3515 cf::init_signals; 3526 cf::init_signals;
3516 cf::init_commands; 3527 cf::init_commands;
3517 cf::init_skills; 3528 cf::init_skills;
3518 3529
3877 3888
3878our @WAIT_FOR_TICK; 3889our @WAIT_FOR_TICK;
3879our @WAIT_FOR_TICK_BEGIN; 3890our @WAIT_FOR_TICK_BEGIN;
3880 3891
3881sub wait_for_tick { 3892sub wait_for_tick {
3882 return if tick_inhibit || $Coro::current == $Coro::main; 3893 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3883 3894
3884 my $signal = new Coro::Signal; 3895 my $signal = new Coro::Signal;
3885 push @WAIT_FOR_TICK, $signal; 3896 push @WAIT_FOR_TICK, $signal;
3886 $signal->wait; 3897 $signal->wait;
3887} 3898}
3888 3899
3889sub wait_for_tick_begin { 3900sub wait_for_tick_begin {
3890 return if tick_inhibit || $Coro::current == $Coro::main; 3901 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3891 3902
3892 my $signal = new Coro::Signal; 3903 my $signal = new Coro::Signal;
3893 push @WAIT_FOR_TICK_BEGIN, $signal; 3904 push @WAIT_FOR_TICK_BEGIN, $signal;
3894 $signal->wait; 3905 $signal->wait;
3895} 3906}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines