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.484 by root, Mon Oct 12 14:00:58 2009 UTC vs.
Revision 1.500 by root, Sat Jan 16 00:08:33 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;
228returns directly I<after> the tick processing (and consequently, can only wake one process 228returns directly I<after> the tick processing (and consequently, can only wake one process
229per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 229per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
230 230
231=item @cf::INVOKE_RESULTS 231=item @cf::INVOKE_RESULTS
232 232
233This array contains the results of the last C<invoke ()> call. When 233This array contains the results of the last C<invoke ()> call. When
234C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 234C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
235that call. 235that call.
236 236
237=item %cf::REFLECT 237=item %cf::REFLECT
238 238
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
2341 : normalise $_ 2341 : normalise $_
2342 } @{ aio_readdir $UNIQUEDIR or [] } 2342 } @{ aio_readdir $UNIQUEDIR or [] }
2343 ] 2343 ]
2344} 2344}
2345 2345
2346=item cf::map::static_maps
2347
2348Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2349file in the shared directory excluding F</styles> and F</editor>). May
2350block.
2351
2352=cut
2353
2354sub static_maps() {
2355 my @dirs = "";
2356 my @maps;
2357
2358 while (@dirs) {
2359 my $dir = shift @dirs;
2360
2361 next if $dir eq "/styles" || $dir eq "/editor";
2362
2363 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2364 or return;
2365
2366 for (@$files) {
2367 s/\.map$// or next;
2368 utf8::decode $_;
2369 push @maps, "$dir/$_";
2370 }
2371
2372 push @dirs, map "$dir/$_", @$dirs;
2373 }
2374
2375 \@maps
2376}
2377
2346=back 2378=back
2347 2379
2348=head3 cf::object 2380=head3 cf::object
2349 2381
2350=cut 2382=cut
2545 ($x, $y) = (-1, -1) 2577 ($x, $y) = (-1, -1)
2546 unless (defined $x) && (defined $y); 2578 unless (defined $x) && (defined $y);
2547 2579
2548 # use -1 or undef as default coordinates, not 0, 0 2580 # use -1 or undef as default coordinates, not 0, 0
2549 ($x, $y) = ($map->enter_x, $map->enter_y) 2581 ($x, $y) = ($map->enter_x, $map->enter_y)
2550 if $x <=0 && $y <= 0; 2582 if $x <= 0 && $y <= 0;
2551 2583
2552 $map->load; 2584 $map->load;
2553 $map->load_neighbours; 2585 $map->load_neighbours;
2554 2586
2555 return unless $self->contr->active; 2587 return unless $self->contr->active;
2754 2786
2755 utf8::encode $text; 2787 utf8::encode $text;
2756 $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);
2757} 2789}
2758 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);
2814}
2815
2759=item $client->send_msg ($channel, $msg, $color, [extra...]) 2816=item $client->send_msg ($channel, $msg, $color, [extra...])
2760 2817
2761Send 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
2762client if neccessary. C<$type> should be a string identifying the type of 2819client if neccessary. C<$type> should be a string identifying the type of
2763the message, with C<log> being the default. If C<$color> is negative, suppress 2820the message, with C<log> being the default. If C<$color> is negative, suppress
2765 2822
2766=cut 2823=cut
2767 2824
2768# non-persistent channels (usually the info channel) 2825# non-persistent channels (usually the info channel)
2769our %CHANNEL = ( 2826our %CHANNEL = (
2827 "c/motd" => {
2828 id => "infobox",
2829 title => "MOTD",
2830 reply => undef,
2831 tooltip => "The message of the day",
2832 },
2770 "c/identify" => { 2833 "c/identify" => {
2771 id => "infobox", 2834 id => "infobox",
2772 title => "Identify", 2835 title => "Identify",
2773 reply => undef, 2836 reply => undef,
2774 tooltip => "Items recently identified", 2837 tooltip => "Items recently identified",
2776 "c/examine" => { 2839 "c/examine" => {
2777 id => "infobox", 2840 id => "infobox",
2778 title => "Examine", 2841 title => "Examine",
2779 reply => undef, 2842 reply => undef,
2780 tooltip => "Signs and other items you examined", 2843 tooltip => "Signs and other items you examined",
2844 },
2845 "c/shopinfo" => {
2846 id => "infobox",
2847 title => "Shop Info",
2848 reply => undef,
2849 tooltip => "What your bargaining skill tells you about the shop",
2781 }, 2850 },
2782 "c/book" => { 2851 "c/book" => {
2783 id => "infobox", 2852 id => "infobox",
2784 title => "Book", 2853 title => "Book",
2785 reply => undef, 2854 reply => undef,
2901 my $pkt = "msg " 2970 my $pkt = "msg "
2902 . $self->{json_coder}->encode ( 2971 . $self->{json_coder}->encode (
2903 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2972 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2904 ); 2973 );
2905 2974
2906 # try lzf for large packets
2907 $pkt = "lzf " . Compress::LZF::compress $pkt
2908 if 1024 <= length $pkt and $self->{can_lzf};
2909
2910 # split very large packets
2911 if (8192 < length $pkt and $self->{can_lzf}) {
2912 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2913 $pkt = "frag";
2914 }
2915
2916 $self->send_packet ($pkt); 2975 $self->send_big_packet ($pkt);
2917} 2976}
2918 2977
2919=item $client->ext_msg ($type, @msg) 2978=item $client->ext_msg ($type, @msg)
2920 2979
2921Sends an ext event to the client. 2980Sends an ext event to the client.
2924 2983
2925sub cf::client::ext_msg($$@) { 2984sub cf::client::ext_msg($$@) {
2926 my ($self, $type, @msg) = @_; 2985 my ($self, $type, @msg) = @_;
2927 2986
2928 if ($self->extcmd == 2) { 2987 if ($self->extcmd == 2) {
2929 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2988 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2930 } elsif ($self->extcmd == 1) { # TODO: remove 2989 } elsif ($self->extcmd == 1) { # TODO: remove
2931 push @msg, msgtype => "event_$type"; 2990 push @msg, msgtype => "event_$type";
2932 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2991 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2933 } 2992 }
2934} 2993}
2935 2994
2936=item $client->ext_reply ($msgid, @msg) 2995=item $client->ext_reply ($msgid, @msg)
2937 2996
2941 3000
2942sub cf::client::ext_reply($$@) { 3001sub cf::client::ext_reply($$@) {
2943 my ($self, $id, @msg) = @_; 3002 my ($self, $id, @msg) = @_;
2944 3003
2945 if ($self->extcmd == 2) { 3004 if ($self->extcmd == 2) {
2946 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3005 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2947 } elsif ($self->extcmd == 1) { 3006 } elsif ($self->extcmd == 1) {
2948 #TODO: version 1, remove 3007 #TODO: version 1, remove
2949 unshift @msg, msgtype => "reply", msgid => $id; 3008 unshift @msg, msgtype => "reply", msgid => $id;
2950 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3009 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2951 } 3010 }
2952} 3011}
2953 3012
2954=item $success = $client->query ($flags, "text", \&cb) 3013=item $success = $client->query ($flags, "text", \&cb)
2955 3014
3082our $safe_hole = new Safe::Hole; 3141our $safe_hole = new Safe::Hole;
3083 3142
3084$SIG{FPE} = 'IGNORE'; 3143$SIG{FPE} = 'IGNORE';
3085 3144
3086$safe->permit_only (Opcode::opset qw( 3145$safe->permit_only (Opcode::opset qw(
3087 :base_core :base_mem :base_orig :base_math 3146 :base_core :base_mem :base_orig :base_math :base_loop
3088 grepstart grepwhile mapstart mapwhile 3147 grepstart grepwhile mapstart mapwhile
3089 sort time 3148 sort time
3090)); 3149));
3091 3150
3092# here we export the classes and methods available to script code 3151# here we export the classes and methods available to script code
3144 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3203 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3145 $qcode =~ s/\n/\\n/g; 3204 $qcode =~ s/\n/\\n/g;
3146 3205
3147 %vars = (_dummy => 0) unless %vars; 3206 %vars = (_dummy => 0) unless %vars;
3148 3207
3208 my @res;
3149 local $_; 3209 local $_;
3150 local @safe::cf::_safe_eval_args = values %vars;
3151 3210
3152 my $eval = 3211 my $eval =
3153 "do {\n" 3212 "do {\n"
3154 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3213 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3155 . "#line 0 \"{$qcode}\"\n" 3214 . "#line 0 \"{$qcode}\"\n"
3156 . $code 3215 . $code
3157 . "\n}" 3216 . "\n}"
3158 ; 3217 ;
3159 3218
3219 if ($CFG{safe_eval}) {
3160 sub_generation_inc; 3220 sub_generation_inc;
3221 local @safe::cf::_safe_eval_args = values %vars;
3161 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3222 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3162 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 }
3163 3228
3164 if ($@) { 3229 if ($@) {
3165 warn "$@"; 3230 warn "$@";
3166 warn "while executing safe code '$code'\n"; 3231 warn "while executing safe code '$code'\n";
3167 warn "with arguments " . (join " ", %vars) . "\n"; 3232 warn "with arguments " . (join " ", %vars) . "\n";
3218 3283
3219 $facedata->{version} == 2 3284 $facedata->{version} == 2
3220 or cf::cleanup "$path: version mismatch, cannot proceed."; 3285 or cf::cleanup "$path: version mismatch, cannot proceed.";
3221 3286
3222 # 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]);
3223 $facedata->{resource}{"res/exp_table"} = { 3289 $facedata->{resource}{"res/exp_table"} = {
3224 type => FT_RSRC, 3290 type => FT_RSRC,
3225 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),
3226 }; 3293 };
3227 cf::cede_to_tick; 3294 cf::cede_to_tick;
3228 3295
3229 { 3296 {
3230 my $faces = $facedata->{faceinfo}; 3297 my $faces = $facedata->{faceinfo};
3232 while (my ($face, $info) = each %$faces) { 3299 while (my ($face, $info) = each %$faces) {
3233 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3300 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3234 3301
3235 cf::face::set_visibility $idx, $info->{visibility}; 3302 cf::face::set_visibility $idx, $info->{visibility};
3236 cf::face::set_magicmap $idx, $info->{magicmap}; 3303 cf::face::set_magicmap $idx, $info->{magicmap};
3237 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3304 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3238 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3305 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3239 3306
3240 cf::cede_to_tick; 3307 cf::cede_to_tick;
3241 } 3308 }
3242 3309
3243 while (my ($face, $info) = each %$faces) { 3310 while (my ($face, $info) = each %$faces) {
3267 3334
3268 cf::anim::invalidate_all; # d'oh 3335 cf::anim::invalidate_all; # d'oh
3269 } 3336 }
3270 3337
3271 { 3338 {
3272 # TODO: for gcfclient pleasure, we should give resources
3273 # that gcfclient doesn't grok a >10000 face index.
3274 my $res = $facedata->{resource}; 3339 my $res = $facedata->{resource};
3275 3340
3276 while (my ($name, $info) = each %$res) { 3341 while (my ($name, $info) = each %$res) {
3277 if (defined $info->{type}) { 3342 if (defined $info->{type}) {
3278 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3343 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3279 my $data;
3280 3344
3281 if ($info->{type} & 1) { 3345 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3282 # prepend meta info
3283
3284 my $meta = $enc->encode ({
3285 name => $name,
3286 %{ $info->{meta} || {} },
3287 });
3288
3289 $data = pack "(w/a*)*", $meta, $info->{data};
3290 } else {
3291 $data = $info->{data};
3292 }
3293
3294 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3295 cf::face::set_type $idx, $info->{type}; 3346 cf::face::set_type $idx, $info->{type};
3296 } else { 3347 } else {
3297 $RESOURCE{$name} = $info; 3348 $RESOURCE{$name} = $info;
3298 } 3349 }
3299 3350
3383 3434
3384 warn "finished reloading resource files\n"; 3435 warn "finished reloading resource files\n";
3385} 3436}
3386 3437
3387sub reload_config { 3438sub reload_config {
3439 warn "reloading config file...\n";
3440
3388 open my $fh, "<:utf8", "$CONFDIR/config" 3441 open my $fh, "<:utf8", "$CONFDIR/config"
3389 or return; 3442 or return;
3390 3443
3391 local $/; 3444 local $/;
3392 *CFG = YAML::XS::Load <$fh>; 3445 *CFG = YAML::XS::Load scalar <$fh>;
3393 3446
3394 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 3447 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
3395 3448
3396 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3449 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3397 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3450 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3401 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3454 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3402 and die "WARNING: m(un)lockall failed: $!\n"; 3455 and die "WARNING: m(un)lockall failed: $!\n";
3403 }; 3456 };
3404 warn $@ if $@; 3457 warn $@ if $@;
3405 } 3458 }
3459
3460 warn "finished reloading resource files\n";
3406} 3461}
3407 3462
3408sub pidfile() { 3463sub pidfile() {
3409 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3464 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3410 or die "$PIDFILE: $!"; 3465 or die "$PIDFILE: $!";
3458 }; 3513 };
3459 3514
3460 evthread_start IO::AIO::poll_fileno; 3515 evthread_start IO::AIO::poll_fileno;
3461 3516
3462 cf::sync_job { 3517 cf::sync_job {
3518 cf::load_settings;
3519 cf::load_materials;
3520
3463 reload_resources; 3521 reload_resources;
3464 reload_config; 3522 reload_config;
3465 db_init; 3523 db_init;
3466 3524
3467 cf::load_settings;
3468 cf::load_materials;
3469 cf::init_uuid; 3525 cf::init_uuid;
3470 cf::init_signals; 3526 cf::init_signals;
3471 cf::init_commands; 3527 cf::init_commands;
3472 cf::init_skills; 3528 cf::init_skills;
3473 3529
3832 3888
3833our @WAIT_FOR_TICK; 3889our @WAIT_FOR_TICK;
3834our @WAIT_FOR_TICK_BEGIN; 3890our @WAIT_FOR_TICK_BEGIN;
3835 3891
3836sub wait_for_tick { 3892sub wait_for_tick {
3837 return if tick_inhibit || $Coro::current == $Coro::main; 3893 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3838 3894
3839 my $signal = new Coro::Signal; 3895 my $signal = new Coro::Signal;
3840 push @WAIT_FOR_TICK, $signal; 3896 push @WAIT_FOR_TICK, $signal;
3841 $signal->wait; 3897 $signal->wait;
3842} 3898}
3843 3899
3844sub wait_for_tick_begin { 3900sub wait_for_tick_begin {
3845 return if tick_inhibit || $Coro::current == $Coro::main; 3901 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3846 3902
3847 my $signal = new Coro::Signal; 3903 my $signal = new Coro::Signal;
3848 push @WAIT_FOR_TICK_BEGIN, $signal; 3904 push @WAIT_FOR_TICK_BEGIN, $signal;
3849 $signal->wait; 3905 $signal->wait;
3850} 3906}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines