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.585 by root, Tue Oct 30 20:18:00 2012 UTC vs.
Revision 1.593 by root, Fri Nov 9 01:59:33 2012 UTC

32use Safe; 32use Safe;
33use Safe::Hole; 33use Safe::Hole;
34use Storable (); 34use Storable ();
35use Carp (); 35use Carp ();
36 36
37use Guard (); 37use AnyEvent ();
38use AnyEvent::IO ();
39use AnyEvent::DNS ();
40
38use Coro (); 41use Coro ();
39use Coro::State; 42use Coro::State;
40use Coro::Handle; 43use Coro::Handle;
41use Coro::EV; 44use Coro::EV;
42use Coro::AnyEvent; 45use Coro::AnyEvent;
48use Coro::AIO; 51use Coro::AIO;
49use Coro::BDB 1.6; 52use Coro::BDB 1.6;
50use Coro::Storable; 53use Coro::Storable;
51use Coro::Util (); 54use Coro::Util ();
52 55
56use Guard ();
53use JSON::XS 2.01 (); 57use JSON::XS 2.01 ();
54use BDB (); 58use BDB ();
55use Data::Dumper; 59use Data::Dumper;
56use Fcntl; 60use Fcntl;
57use YAML::XS (); 61use YAML::XS ();
220=item $cf::RUNTIME 224=item $cf::RUNTIME
221 225
222The time this server has run, starts at 0 and is increased by $cf::TICK on 226The time this server has run, starts at 0 and is increased by $cf::TICK on
223every server tick. 227every server tick.
224 228
225=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR 229=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR
226$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR 230$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR
227$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR 231$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR
228 232
229Various directories - "/etc", read-only install directory, perl-library 233Various directories - "/etc", read-only install directory, perl-library
230directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 234directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
231unique-items directory, player file directory, random maps directory and 235unique-items directory, player file directory, random maps directory and
232database environment. 236database environment.
336)) { 340)) {
337 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 341 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
338} 342}
339 343
340$EV::DIED = sub { 344$EV::DIED = sub {
341 Carp::cluck "error in event callback: @_"; 345 warn "error in event callback: $@";
342}; 346};
343 347
344############################################################################# 348#############################################################################
345 349
346sub fork_call(&@); 350sub fork_call(&@);
1427=cut 1431=cut
1428 1432
1429############################################################################# 1433#############################################################################
1430# command handling &c 1434# command handling &c
1431 1435
1432=item cf::register_command $name => \&callback($ob,$args); 1436=item cf::register_command $name => \&callback($ob,$args)
1433 1437
1434Register a callback for execution when the client sends the user command 1438Register a callback for execution when the client sends the user command
1435$name. 1439$name.
1436 1440
1437=cut 1441=cut
1443 #warn "registering command '$name/$time' to '$caller'"; 1447 #warn "registering command '$name/$time' to '$caller'";
1444 1448
1445 push @{ $COMMAND{$name} }, [$caller, $cb]; 1449 push @{ $COMMAND{$name} }, [$caller, $cb];
1446} 1450}
1447 1451
1448=item cf::register_extcmd $name => \&callback($pl,$packet); 1452=item cf::register_extcmd $name => \&callback($pl,$packet)
1449 1453
1450Register a callback for execution when the client sends an (synchronous) 1454Register a callback for execution when the client sends an (synchronous)
1451extcmd packet. Ext commands will be processed in the order they are 1455extcmd packet. Ext commands will be processed in the order they are
1452received by the server, like other user commands. The first argument is 1456received by the server, like other user commands. The first argument is
1453the logged-in player. Ext commands can only be processed after a player 1457the logged-in player. Ext commands can only be processed after a player
1454has logged in successfully. 1458has logged in successfully.
1455 1459
1456If the callback returns something, it is sent back as if reply was being 1460If the callback returns something, it is sent back as if reply was being
1457called. 1461called.
1458 1462
1459=item cf::register_exticmd $name => \&callback($ns,$packet); 1463=item cf::register_exticmd $name => \&callback($ns,$packet)
1460 1464
1461Register a callback for execution when the client sends an (asynchronous) 1465Register a callback for execution when the client sends an (asynchronous)
1462exticmd packet. Exti commands are processed by the server as soon as they 1466exticmd packet. Exti commands are processed by the server as soon as they
1463are received, i.e. out of order w.r.t. other commands. The first argument 1467are received, i.e. out of order w.r.t. other commands. The first argument
1464is a client socket. Exti commands can be received anytime, even before 1468is a client socket. Exti commands can be received anytime, even before
1508 1512
1509 if (my $cb = $EXTCMD{$type}) { 1513 if (my $cb = $EXTCMD{$type}) {
1510 @reply = $cb->($pl, @payload); 1514 @reply = $cb->($pl, @payload);
1511 } 1515 }
1512 1516
1513 $pl->ext_reply ($reply, @reply) 1517 $pl->ext_msg ("reply-$reply", @reply)
1514 if $reply; 1518 if $reply;
1515 1519
1516 } else { 1520 } else {
1517 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1521 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1518 } 1522 }
1905} 1909}
1906 1910
1907=item $protocol_xml = $player->expand_cfpod ($cfpod) 1911=item $protocol_xml = $player->expand_cfpod ($cfpod)
1908 1912
1909Expand deliantra pod fragments into protocol xml. 1913Expand deliantra pod fragments into protocol xml.
1910
1911=item $player->ext_reply ($msgid, @msg)
1912
1913Sends an ext reply to the player.
1914
1915=cut
1916
1917sub ext_reply($$@) {
1918 my ($self, $id, @msg) = @_;
1919
1920 $self->ns->ext_reply ($id, @msg)
1921}
1922 1914
1923=item $player->ext_msg ($type, @msg) 1915=item $player->ext_msg ($type, @msg)
1924 1916
1925Sends an ext event to the client. 1917Sends an ext event to the client.
1926 1918
3231=cut 3223=cut
3232 3224
3233sub cf::client::ext_msg($$@) { 3225sub cf::client::ext_msg($$@) {
3234 my ($self, $type, @msg) = @_; 3226 my ($self, $type, @msg) = @_;
3235 3227
3236 if ($self->extcmd == 2) {
3237 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 3228 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
3238 } elsif ($self->extcmd == 1) { # TODO: remove
3239 push @msg, msgtype => "event_$type";
3240 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
3241 }
3242}
3243
3244=item $client->ext_reply ($msgid, @msg)
3245
3246Sends an ext reply to the client.
3247
3248=cut
3249
3250sub cf::client::ext_reply($$@) {
3251 my ($self, $id, @msg) = @_;
3252
3253 return unless $self->extcmd == 2;
3254
3255 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
3256} 3229}
3257 3230
3258=item $success = $client->query ($flags, "text", \&cb) 3231=item $success = $client->query ($flags, "text", \&cb)
3259 3232
3260Queues a query to the client, calling the given callback with 3233Queues a query to the client, calling the given callback with
3323 3296
3324 if (my $cb = $EXTICMD{$type}) { 3297 if (my $cb = $EXTICMD{$type}) {
3325 @reply = $cb->($ns, @payload); 3298 @reply = $cb->($ns, @payload);
3326 } 3299 }
3327 3300
3328 $ns->ext_reply ($reply, @reply) 3301 $ns->ext_msg ("reply-$reply", @reply)
3329 if $reply; 3302 if $reply;
3330 3303
3331 } else { 3304 } else {
3332 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3305 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3333 } 3306 }
3505############################################################################# 3478#############################################################################
3506# the server's init and main functions 3479# the server's init and main functions
3507 3480
3508our %FACEHASH; # hash => idx, #d# HACK for http server 3481our %FACEHASH; # hash => idx, #d# HACK for http server
3509 3482
3483# internal api, not fianlised
3484sub add_face {
3485 my ($name, $type, $data) = @_;
3486
3487 my $idx = cf::face::find $name;
3488
3489 if ($idx) {
3490 delete $FACEHASH{cf::face::get_chksum $idx};
3491 } else {
3492 $idx = cf::face::alloc $name;
3493 }
3494
3495 my $hash = cf::face::mangle_chksum Digest::MD5::md5 $data;
3496
3497 cf::face::set_type $idx, $type;
3498 cf::face::set_data $idx, 0, $data, $hash;
3499 cf::face::set_meta $idx, $type & 1 ? undef : undef;
3500 $FACEHASH{$hash} = $idx;#d#
3501
3502 $idx
3503}
3504
3510sub load_facedata($) { 3505sub load_facedata($) {
3511 my ($path) = @_; 3506 my ($path) = @_;
3512 3507
3513 # HACK to clear player env face cache, we need some signal framework 3508 # HACK to clear player env face cache, we need some signal framework
3514 # for this (global event?) 3509 # for this (global event?)
3521 my $facedata = decode_storable load_file $path; 3516 my $facedata = decode_storable load_file $path;
3522 3517
3523 $facedata->{version} == 2 3518 $facedata->{version} == 2
3524 or cf::cleanup "$path: version mismatch, cannot proceed."; 3519 or cf::cleanup "$path: version mismatch, cannot proceed.";
3525 3520
3526 # patch in the exptable
3527 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3528 $facedata->{resource}{"res/exp_table"} = {
3529 type => FT_RSRC,
3530 data => $exp_table,
3531 hash => (Digest::MD5::md5 $exp_table),
3532 };
3533 cf::cede_to_tick; 3521 cf::cede_to_tick;
3534 3522
3535 { 3523 {
3536 my $faces = $facedata->{faceinfo}; 3524 my $faces = $facedata->{faceinfo};
3537 3525
3583 while (my ($name, $info) = each %$res) { 3571 while (my ($name, $info) = each %$res) {
3584 if (defined (my $type = $info->{type})) { 3572 if (defined (my $type = $info->{type})) {
3585 # TODO: different hash - must free and use new index, or cache ixface data queue 3573 # TODO: different hash - must free and use new index, or cache ixface data queue
3586 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3574 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3587 3575
3576 cf::face::set_type $idx, $type;
3588 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3577 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3589 cf::face::set_type $idx, $type;
3590 cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already 3578 cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already
3591 $FACEHASH{$info->{hash}} = $idx;#d# 3579 $FACEHASH{$info->{hash}} = $idx;#d#
3592 } else { 3580 } else {
3593# $RESOURCE{$name} = $info; # unused 3581# $RESOURCE{$name} = $info; # unused
3594 } 3582 }
3618 cf::arch::commit_load; 3606 cf::arch::commit_load;
3619 3607
3620 $status 3608 $status
3621} 3609}
3622 3610
3611sub reload_exp_table {
3612 _reload_exp_table;
3613
3614 add_face "res/exp_table" => FT_RSRC,
3615 JSON::XS->new->utf8->canonical->encode (
3616 [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]
3617 );
3618}
3619
3620sub reload_materials {
3621 _reload_materials;
3622}
3623
3623sub reload_regions { 3624sub reload_regions {
3624 # HACK to clear player env face cache, we need some signal framework 3625 # HACK to clear player env face cache, we need some signal framework
3625 # for this (global event?) 3626 # for this (global event?)
3626 %ext::player_env::MUSIC_FACE_CACHE = (); 3627 %ext::player_env::MUSIC_FACE_CACHE = ();
3627 3628
3640} 3641}
3641 3642
3642sub reload_archetypes { 3643sub reload_archetypes {
3643 load_resource_file "$DATADIR/archetypes" 3644 load_resource_file "$DATADIR/archetypes"
3644 or die "unable to load archetypes\n"; 3645 or die "unable to load archetypes\n";
3646
3647 add_face "res/skill_info" => FT_RSRC,
3648 JSON::XS->new->utf8->canonical->encode (
3649 [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1]
3650 );
3651 add_face "res/spell_paths" => FT_RSRC,
3652 JSON::XS->new->utf8->canonical->encode (
3653 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1]
3654 );
3645} 3655}
3646 3656
3647sub reload_treasures { 3657sub reload_treasures {
3648 load_resource_file "$DATADIR/treasures" 3658 load_resource_file "$DATADIR/treasures"
3649 or die "unable to load treasurelists\n"; 3659 or die "unable to load treasurelists\n";
3667 my $face = cf::face::find "sound/$v"; 3677 my $face = cf::face::find "sound/$v";
3668 cf::sound::set $k => $face; 3678 cf::sound::set $k => $face;
3669 } 3679 }
3670} 3680}
3671 3681
3682#d# move docstuff to help or so
3683our %DOCSTRING;
3684
3685sub reload_pod {
3686 trace "loading pods $PODDIR\n";
3687
3688 %DOCSTRING = ();
3689 my @command_list;
3690
3691 for (
3692 [0, "command_help"],
3693 [1, "emote_help"],
3694 [2, "dmcommand_help"],
3695 ) {
3696 my ($type, $path) = @$_;
3697
3698 my $paragraphs = &cf::pod::load_pod ("$PODDIR/$path.pod")
3699 or die "unable to load $path";
3700
3701 my $level = 1e9;
3702 my $rpar;
3703
3704 for my $par (@$paragraphs) {
3705 if ($par->{type} eq "head2") {
3706 # this code taken almost verbatim from DC/Protocol.pm
3707
3708 if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) {
3709 my $cmd = $1;
3710 my @args = split /\|/, $2;
3711 @args = (".*") unless @args;
3712
3713 $_ = $_ eq ".*" ? "" : " $_"
3714 for @args;
3715
3716 my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args;
3717
3718 $rpar = \($DOCSTRING{"command/$cmd"} = &cf::pod::as_cfpod ([$par]));
3719
3720 push @command_list, [$type, \@variants];
3721 $level = $par->{level};
3722 } else {
3723 error "$par->{markup}: unparsable command heading";
3724 }
3725 } elsif ($par->{level} > $level) {
3726 $$rpar .= &cf::pod::as_cfpod ([$par]);
3727 }
3728
3729 cf::cede_to_tick;
3730 }
3731 }
3732
3733 @command_list = sort {
3734 $a->[0] <=> $b->[0]
3735 or $a->[1] cmp $b->[1]
3736 } @command_list;
3737
3738 cf::cede_to_tick;
3739
3740 add_face "res/command_list" => FT_RSRC,
3741 JSON::XS->new->utf8->encode (\@command_list);
3742}
3743
3672sub reload_resources { 3744sub reload_resources {
3673 trace "reloading resource files...\n"; 3745 trace "reloading resource files...\n";
3674 3746
3675 reload_exp_table;
3676 reload_materials; 3747 reload_materials;
3677 reload_facedata; 3748 reload_facedata;
3749 reload_exp_table;
3678 reload_sound; 3750 reload_sound;
3679 reload_archetypes; 3751 reload_archetypes;
3680 reload_regions; 3752 reload_regions;
3681 reload_treasures; 3753 reload_treasures;
3754 reload_pod;
3682 3755
3683 trace "finished reloading resource files\n"; 3756 trace "finished reloading resource files\n";
3684} 3757}
3685 3758
3686sub reload_config { 3759sub reload_config {
3741 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3814 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3742 3815
3743 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3816 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3744 3817
3745 # we must not ever block the main coroutine 3818 # we must not ever block the main coroutine
3746 local $Coro::idle = sub { 3819 $Coro::idle = sub {
3747 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3820 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3748 (async { 3821 (async {
3749 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3822 $Coro::current->{desc} = "IDLE BUG HANDLER";
3750 EV::loop EV::LOOP_ONESHOT; 3823 EV::loop EV::LOOP_ONESHOT;
3751 })->prio (Coro::PRIO_MAX); 3824 })->prio (Coro::PRIO_MAX);
3754 evthread_start IO::AIO::poll_fileno; 3827 evthread_start IO::AIO::poll_fileno;
3755 3828
3756 cf::sync_job { 3829 cf::sync_job {
3757 cf::incloader::init (); 3830 cf::incloader::init ();
3758 3831
3832 db_init;
3833
3759 cf::init_anim; 3834 cf::init_anim;
3760 cf::init_attackmess; 3835 cf::init_attackmess;
3761 cf::init_dynamic; 3836 cf::init_dynamic;
3762 3837
3763 cf::load_settings; 3838 cf::load_settings;
3764 3839
3765 reload_resources; 3840 reload_resources;
3766 reload_config; 3841 reload_config;
3767 db_init;
3768 3842
3769 cf::init_uuid; 3843 cf::init_uuid;
3770 cf::init_signals; 3844 cf::init_signals;
3771 cf::init_skills; 3845 cf::init_skills;
3772 3846
4197 } 4271 }
4198} 4272}
4199 4273
4200{ 4274{
4201 # configure BDB 4275 # configure BDB
4276 info "initialising database";
4202 4277
4203 BDB::min_parallel 16; 4278 BDB::min_parallel 16;
4204 BDB::max_poll_reqs $TICK * 0.1; 4279 BDB::max_poll_reqs $TICK * 0.1;
4205 #$AnyEvent::BDB::WATCHER->priority (1); 4280 #$AnyEvent::BDB::WATCHER->priority (1);
4206 4281
4235 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 4310 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
4236 }; 4311 };
4237 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub { 4312 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub {
4238 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; 4313 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
4239 }; 4314 };
4315
4316 info "database initialised";
4240} 4317}
4241 4318
4242{ 4319{
4243 # configure IO::AIO 4320 # configure IO::AIO
4244 4321
4322 info "initialising aio";
4245 IO::AIO::min_parallel 8; 4323 IO::AIO::min_parallel 8;
4246 IO::AIO::max_poll_time $TICK * 0.1; 4324 IO::AIO::max_poll_time $TICK * 0.1;
4247 undef $AnyEvent::AIO::WATCHER; 4325 undef $AnyEvent::AIO::WATCHER;
4326 info "aio initialised";
4248} 4327}
4249 4328
4250our $_log_backtrace; 4329our $_log_backtrace;
4251our $_log_backtrace_last; 4330our $_log_backtrace_last;
4252 4331

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines