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.591 by root, Tue Nov 6 23:33:15 2012 UTC vs.
Revision 1.598 by root, Sun Nov 11 02:38:10 2012 UTC

58use BDB (); 58use BDB ();
59use Data::Dumper; 59use Data::Dumper;
60use Fcntl; 60use Fcntl;
61use YAML::XS (); 61use YAML::XS ();
62use IO::AIO (); 62use IO::AIO ();
63use Time::HiRes;
64use Compress::LZF; 63use Compress::LZF;
65use Digest::MD5 (); 64use Digest::MD5 ();
66 65
67AnyEvent::detect; 66AnyEvent::detect;
68 67
87our %COMMAND = (); 86our %COMMAND = ();
88our %COMMAND_TIME = (); 87our %COMMAND_TIME = ();
89 88
90our @EXTS = (); # list of extension package names 89our @EXTS = (); # list of extension package names
91our %EXTCMD = (); 90our %EXTCMD = ();
91our %EXTACMD = ();
92our %EXTICMD = (); 92our %EXTICMD = ();
93our %EXTIACMD = ();
93our %EXT_CORO = (); # coroutines bound to extensions 94our %EXT_CORO = (); # coroutines bound to extensions
94our %EXT_MAP = (); # pluggable maps 95our %EXT_MAP = (); # pluggable maps
95 96
96our $RELOAD; # number of reloads so far, non-zero while in reload 97our $RELOAD; # number of reloads so far, non-zero while in reload
97our @EVENT; 98our @EVENT;
899 900
900 return db_get cache => "$id/data"; 901 return db_get cache => "$id/data";
901 } 902 }
902 } 903 }
903 904
904 my $t1 = Time::HiRes::time; 905 my $t1 = EV::time;
905 my $data = $process->(\@data); 906 my $data = $process->(\@data);
906 my $t2 = Time::HiRes::time; 907 my $t2 = EV::time;
907 908
908 info "cache: '$id' processed in ", $t2 - $t1, "s\n"; 909 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
909 910
910 db_put cache => "$id/data", $data; 911 db_put cache => "$id/data", $data;
911 db_put cache => "$id/md5" , $md5; 912 db_put cache => "$id/md5" , $md5;
1431=cut 1432=cut
1432 1433
1433############################################################################# 1434#############################################################################
1434# command handling &c 1435# command handling &c
1435 1436
1436=item cf::register_command $name => \&callback($ob,$args); 1437=item cf::register_command $name => \&callback($ob,$args)
1437 1438
1438Register a callback for execution when the client sends the user command 1439Register a callback for execution when the client sends the user command
1439$name. 1440$name.
1440 1441
1441=cut 1442=cut
1447 #warn "registering command '$name/$time' to '$caller'"; 1448 #warn "registering command '$name/$time' to '$caller'";
1448 1449
1449 push @{ $COMMAND{$name} }, [$caller, $cb]; 1450 push @{ $COMMAND{$name} }, [$caller, $cb];
1450} 1451}
1451 1452
1452=item cf::register_extcmd $name => \&callback($pl,$packet); 1453=item cf::register_extcmd $name => \&callback($pl,@args)
1453 1454
1454Register a callback for execution when the client sends an (synchronous) 1455Register a callback for execution when the client sends an (synchronous)
1455extcmd packet. Ext commands will be processed in the order they are 1456extcmd packet. Ext commands will be processed in the order they are
1456received by the server, like other user commands. The first argument is 1457received by the server, like other user commands. The first argument is
1457the logged-in player. Ext commands can only be processed after a player 1458the logged-in player. Ext commands can only be processed after a player
1458has logged in successfully. 1459has logged in successfully.
1459 1460
1460If the callback returns something, it is sent back as if reply was being 1461The values will be sent back to the client.
1461called.
1462 1462
1463=item cf::register_async_extcmd $name => \&callback($pl,$reply->(...),@args)
1464
1465Same as C<cf::register_extcmd>, but instead of returning values, the
1466callback needs to clal the C<$reply> function.
1467
1463=item cf::register_exticmd $name => \&callback($ns,$packet); 1468=item cf::register_exticmd $name => \&callback($ns,@args)
1464 1469
1465Register a callback for execution when the client sends an (asynchronous) 1470Register a callback for execution when the client sends an (asynchronous)
1466exticmd packet. Exti commands are processed by the server as soon as they 1471exticmd packet. Exti commands are processed by the server as soon as they
1467are received, i.e. out of order w.r.t. other commands. The first argument 1472are received, i.e. out of order w.r.t. other commands. The first argument
1468is a client socket. Exti commands can be received anytime, even before 1473is a client socket. Exti commands can be received anytime, even before
1469log-in. 1474log-in.
1470 1475
1471If the callback returns something, it is sent back as if reply was being 1476The values will be sent back to the client.
1472called.
1473 1477
1474=cut 1478=item cf::register_async_exticmd $name => \&callback($ns,$reply->(...),@args)
1475 1479
1480Same as C<cf::register_extcmd>, but instead of returning values, the
1481callback needs to clal the C<$reply> function.
1482
1483=cut
1484
1476sub register_extcmd { 1485sub register_extcmd($$) {
1477 my ($name, $cb) = @_; 1486 my ($name, $cb) = @_;
1478 1487
1479 $EXTCMD{$name} = $cb; 1488 $EXTCMD{$name} = $cb;
1480} 1489}
1481 1490
1482sub register_exticmd { 1491sub register_async_extcmd($$) {
1483 my ($name, $cb) = @_; 1492 my ($name, $cb) = @_;
1484 1493
1494 $EXTACMD{$name} = $cb;
1495}
1496
1497sub register_exticmd($$) {
1498 my ($name, $cb) = @_;
1499
1485 $EXTICMD{$name} = $cb; 1500 $EXTICMD{$name} = $cb;
1501}
1502
1503sub register_async_exticmd($$) {
1504 my ($name, $cb) = @_;
1505
1506 $EXTIACMD{$name} = $cb;
1486} 1507}
1487 1508
1488use File::Glob (); 1509use File::Glob ();
1489 1510
1490cf::player->attach ( 1511cf::player->attach (
1506 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; 1527 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1507 1528
1508 if (ref $msg) { 1529 if (ref $msg) {
1509 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash 1530 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
1510 1531
1511 my @reply;
1512
1513 if (my $cb = $EXTCMD{$type}) { 1532 if (my $cb = $EXTACMD{$type}) {
1533 $cb->(
1534 $pl,
1535 sub {
1536 $pl->ext_msg ("reply-$reply", @_)
1537 if $reply;
1538 },
1539 @payload
1540 );
1541 } else {
1542 my @reply;
1543
1544 if (my $cb = $EXTCMD{$type}) {
1514 @reply = $cb->($pl, @payload); 1545 @reply = $cb->($pl, @payload);
1546 }
1547
1548 $pl->ext_msg ("reply-$reply", @reply)
1549 if $reply;
1515 } 1550 }
1516
1517 $pl->ext_reply ($reply, @reply)
1518 if $reply;
1519 1551
1520 } else { 1552 } else {
1521 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1553 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1522 } 1554 }
1523 1555
1909} 1941}
1910 1942
1911=item $protocol_xml = $player->expand_cfpod ($cfpod) 1943=item $protocol_xml = $player->expand_cfpod ($cfpod)
1912 1944
1913Expand deliantra pod fragments into protocol xml. 1945Expand deliantra pod fragments into protocol xml.
1914
1915=item $player->ext_reply ($msgid, @msg)
1916
1917Sends an ext reply to the player.
1918
1919=cut
1920
1921sub ext_reply($$@) {
1922 my ($self, $id, @msg) = @_;
1923
1924 $self->ns->ext_reply ($id, @msg)
1925}
1926 1946
1927=item $player->ext_msg ($type, @msg) 1947=item $player->ext_msg ($type, @msg)
1928 1948
1929Sends an ext event to the client. 1949Sends an ext event to the client.
1930 1950
3238 my ($self, $type, @msg) = @_; 3258 my ($self, $type, @msg) = @_;
3239 3259
3240 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 3260 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
3241} 3261}
3242 3262
3243=item $client->ext_reply ($msgid, @msg)
3244
3245Sends an ext reply to the client.
3246
3247=cut
3248
3249sub cf::client::ext_reply($$@) {
3250 my ($self, $id, @msg) = @_;
3251
3252 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
3253}
3254
3255=item $success = $client->query ($flags, "text", \&cb) 3263=item $success = $client->query ($flags, "text", \&cb)
3256 3264
3257Queues a query to the client, calling the given callback with 3265Queues a query to the client, calling the given callback with
3258the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, 3266the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
3259C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. 3267C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
3314 my $msg = eval { $ns->{json_coder}->decode ($buf) }; 3322 my $msg = eval { $ns->{json_coder}->decode ($buf) };
3315 3323
3316 if (ref $msg) { 3324 if (ref $msg) {
3317 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash 3325 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
3318 3326
3319 my @reply;
3320
3321 if (my $cb = $EXTICMD{$type}) { 3327 if (my $cb = $EXTIACMD{$type}) {
3328 $cb->(
3329 $ns,
3330 sub {
3331 $ns->ext_msg ("reply-$reply", @_)
3332 if $reply;
3333 },
3334 @payload
3335 );
3336 } else {
3337 my @reply;
3338
3339 if (my $cb = $EXTICMD{$type}) {
3322 @reply = $cb->($ns, @payload); 3340 @reply = $cb->($ns, @payload);
3341 }
3342
3343 $ns->ext_msg ("reply-$reply", @reply)
3344 if $reply;
3323 } 3345 }
3324
3325 $ns->ext_reply ($reply, @reply)
3326 if $reply;
3327
3328 } else { 3346 } else {
3329 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3347 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3330 } 3348 }
3331 3349
3332 cf::override; 3350 cf::override;
3503# the server's init and main functions 3521# the server's init and main functions
3504 3522
3505our %FACEHASH; # hash => idx, #d# HACK for http server 3523our %FACEHASH; # hash => idx, #d# HACK for http server
3506 3524
3507# internal api, not fianlised 3525# internal api, not fianlised
3508sub add_face { 3526sub set_face {
3509 my ($name, $type, $data) = @_; 3527 my ($name, $type, $data) = @_;
3510 3528
3511 my $idx = cf::face::find $name; 3529 my $idx = cf::face::find $name;
3512 3530
3513 if ($idx) { 3531 if ($idx) {
3543 or cf::cleanup "$path: version mismatch, cannot proceed."; 3561 or cf::cleanup "$path: version mismatch, cannot proceed.";
3544 3562
3545 cf::cede_to_tick; 3563 cf::cede_to_tick;
3546 3564
3547 { 3565 {
3548 my $faces = $facedata->{faceinfo}; 3566 my $faces = delete $facedata->{faceinfo};
3549 3567
3550 for my $face (sort keys %$faces) { 3568 for my $face (sort keys %$faces) {
3551 my $info = $faces->{$face}; 3569 my $info = $faces->{$face};
3552 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3570 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3553 3571
3555 cf::face::set_magicmap $idx, $info->{magicmap}; 3573 cf::face::set_magicmap $idx, $info->{magicmap};
3556 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3574 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3557 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3575 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3558 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; 3576 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3559 $FACEHASH{$info->{hash64}} = $idx;#d# 3577 $FACEHASH{$info->{hash64}} = $idx;#d#
3560 3578
3561 cf::cede_to_tick; 3579 cf::cede_to_tick;
3562 } 3580 }
3563 3581
3564 while (my ($face, $info) = each %$faces) { 3582 while (my ($face, $info) = each %$faces) {
3565 next unless $info->{smooth}; 3583 next unless $info->{smooth};
3577 cf::cede_to_tick; 3595 cf::cede_to_tick;
3578 } 3596 }
3579 } 3597 }
3580 3598
3581 { 3599 {
3582 my $anims = $facedata->{animinfo}; 3600 my $anims = delete $facedata->{animinfo};
3583 3601
3584 while (my ($anim, $info) = each %$anims) { 3602 while (my ($anim, $info) = each %$anims) {
3585 cf::anim::set $anim, $info->{frames}, $info->{facings}; 3603 cf::anim::set $anim, $info->{frames}, $info->{facings};
3586 cf::cede_to_tick; 3604 cf::cede_to_tick;
3587 } 3605 }
3588 3606
3589 cf::anim::invalidate_all; # d'oh 3607 cf::anim::invalidate_all; # d'oh
3590 } 3608 }
3591 3609
3592 { 3610 {
3593 my $res = $facedata->{resource}; 3611 my $res = delete $facedata->{resource};
3594 3612
3595 while (my ($name, $info) = each %$res) { 3613 while (my ($name, $info) = each %$res) {
3596 if (defined (my $type = $info->{type})) { 3614 if (defined (my $type = $info->{type})) {
3597 # TODO: different hash - must free and use new index, or cache ixface data queue 3615 # TODO: different hash - must free and use new index, or cache ixface data queue
3598 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3616 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3633} 3651}
3634 3652
3635sub reload_exp_table { 3653sub reload_exp_table {
3636 _reload_exp_table; 3654 _reload_exp_table;
3637 3655
3638 add_face "res/exp_table" => FT_RSRC, 3656 set_face "res/exp_table" => FT_RSRC,
3639 JSON::XS->new->utf8->canonical->encode ( 3657 JSON::XS->new->utf8->canonical->encode (
3640 [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level] 3658 [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]
3641 ); 3659 );
3642} 3660}
3643 3661
3666 3684
3667sub reload_archetypes { 3685sub reload_archetypes {
3668 load_resource_file "$DATADIR/archetypes" 3686 load_resource_file "$DATADIR/archetypes"
3669 or die "unable to load archetypes\n"; 3687 or die "unable to load archetypes\n";
3670 3688
3671 add_face "res/skill_info" => FT_RSRC, 3689 set_face "res/skill_info" => FT_RSRC,
3672 JSON::XS->new->utf8->canonical->encode ( 3690 JSON::XS->new->utf8->canonical->encode (
3673 [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1] 3691 [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1]
3674 ); 3692 );
3675 add_face "res/spell_paths" => FT_RSRC, 3693 set_face "res/spell_paths" => FT_RSRC,
3676 JSON::XS->new->utf8->canonical->encode ( 3694 JSON::XS->new->utf8->canonical->encode (
3677 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] 3695 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1]
3678 ); 3696 );
3679} 3697}
3680 3698
3699 3717
3700 while (my ($k, $v) = each %{$soundconf->{event}}) { 3718 while (my ($k, $v) = each %{$soundconf->{event}}) {
3701 my $face = cf::face::find "sound/$v"; 3719 my $face = cf::face::find "sound/$v";
3702 cf::sound::set $k => $face; 3720 cf::sound::set $k => $face;
3703 } 3721 }
3704}
3705
3706sub reload_pod {
3707 trace "loading pods $PODDIR\n";
3708
3709 my @command_help;
3710
3711 for (
3712 [0, "command_help"],
3713 [1, "emote_help"],
3714 [2, "dmcommand_help"],
3715 ) {
3716 my ($type, $path) = @$_;
3717
3718 my $paragraphs = &cf::pod::load_pod ("$PODDIR/$path.pod")
3719 or die "unable to load $path";
3720
3721 my $level = 1e9;
3722
3723 for my $par (@$paragraphs) {
3724 if ($par->{type} eq "head2") {
3725 # this code taken almost verbatim from DC/Protocol.pm
3726
3727 if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) {
3728 my $cmd = $1;
3729 my @args = split /\|/, $2;
3730 @args = (".*") unless @args;
3731
3732 $_ = $_ eq ".*" ? "" : " $_"
3733 for @args;
3734
3735 my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args;
3736
3737 push @command_help, [$type, \@variants, &cf::pod::as_cfpod ([$par])];
3738 $level = $par->{level};
3739 } else {
3740 error "$par->{markup}: unparsable command heading";
3741 }
3742 } elsif ($par->{level} > $level) {
3743 $command_help[-1][2] .= &cf::pod::as_cfpod ([$par]);
3744 }
3745
3746 cf::cede_to_tick;
3747 }
3748 }
3749
3750 @command_help = sort {
3751 $a->[0] <=> $b->[0]
3752 or $a->[1] cmp $b->[1]
3753 } @command_help;
3754
3755 cf::cede_to_tick;
3756
3757 add_face "res/command_help" => FT_RSRC,
3758 JSON::XS->new->utf8->encode (\@command_help);
3759} 3722}
3760 3723
3761sub reload_resources { 3724sub reload_resources {
3762 trace "reloading resource files...\n"; 3725 trace "reloading resource files...\n";
3763 3726
3766 reload_exp_table; 3729 reload_exp_table;
3767 reload_sound; 3730 reload_sound;
3768 reload_archetypes; 3731 reload_archetypes;
3769 reload_regions; 3732 reload_regions;
3770 reload_treasures; 3733 reload_treasures;
3771 reload_pod;
3772 3734
3773 trace "finished reloading resource files\n"; 3735 trace "finished reloading resource files\n";
3774} 3736}
3775 3737
3776sub reload_config { 3738sub reload_config {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines