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.583 by root, Mon Oct 29 23:12:37 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 }
3503=cut 3476=cut
3504 3477
3505############################################################################# 3478#############################################################################
3506# the server's init and main functions 3479# the server's init and main functions
3507 3480
3481our %FACEHASH; # hash => idx, #d# HACK for http server
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
3508sub load_facedata($) { 3505sub load_facedata($) {
3509 my ($path) = @_; 3506 my ($path) = @_;
3510 3507
3511 # 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
3512 # for this (global event?) 3509 # for this (global event?)
3519 my $facedata = decode_storable load_file $path; 3516 my $facedata = decode_storable load_file $path;
3520 3517
3521 $facedata->{version} == 2 3518 $facedata->{version} == 2
3522 or cf::cleanup "$path: version mismatch, cannot proceed."; 3519 or cf::cleanup "$path: version mismatch, cannot proceed.";
3523 3520
3524 # patch in the exptable
3525 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3526 $facedata->{resource}{"res/exp_table"} = {
3527 type => FT_RSRC,
3528 data => $exp_table,
3529 hash => (Digest::MD5::md5 $exp_table),
3530 };
3531 cf::cede_to_tick; 3521 cf::cede_to_tick;
3532 3522
3533 { 3523 {
3534 my $faces = $facedata->{faceinfo}; 3524 my $faces = $facedata->{faceinfo};
3535 3525
3536 while (my ($face, $info) = each %$faces) { 3526 for my $face (sort keys %$faces) {
3527 my $info = $faces->{$face};
3537 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3528 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3538 3529
3539 cf::face::set_visibility $idx, $info->{visibility}; 3530 cf::face::set_visibility $idx, $info->{visibility};
3540 cf::face::set_magicmap $idx, $info->{magicmap}; 3531 cf::face::set_magicmap $idx, $info->{magicmap};
3541 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3532 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3542 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3533 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3543 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; 3534 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3535 $FACEHASH{$info->{hash64}} = $idx;#d#
3544 3536
3545 cf::cede_to_tick; 3537 cf::cede_to_tick;
3546 } 3538 }
3547 3539
3548 while (my ($face, $info) = each %$faces) { 3540 while (my ($face, $info) = each %$faces) {
3579 while (my ($name, $info) = each %$res) { 3571 while (my ($name, $info) = each %$res) {
3580 if (defined (my $type = $info->{type})) { 3572 if (defined (my $type = $info->{type})) {
3581 # 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
3582 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3574 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3583 3575
3576 cf::face::set_type $idx, $type;
3584 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3577 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3585 cf::face::set_type $idx, $type;
3586 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
3579 $FACEHASH{$info->{hash}} = $idx;#d#
3587 } else { 3580 } else {
3588# $RESOURCE{$name} = $info; # unused 3581# $RESOURCE{$name} = $info; # unused
3589 } 3582 }
3590 3583
3591 cf::cede_to_tick; 3584 cf::cede_to_tick;
3611 my $status = load_resource_file_ $_[0]; 3604 my $status = load_resource_file_ $_[0];
3612 get_slot 0.1, 100; 3605 get_slot 0.1, 100;
3613 cf::arch::commit_load; 3606 cf::arch::commit_load;
3614 3607
3615 $status 3608 $status
3609}
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;
3616} 3622}
3617 3623
3618sub reload_regions { 3624sub reload_regions {
3619 # 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
3620 # for this (global event?) 3626 # for this (global event?)
3635} 3641}
3636 3642
3637sub reload_archetypes { 3643sub reload_archetypes {
3638 load_resource_file "$DATADIR/archetypes" 3644 load_resource_file "$DATADIR/archetypes"
3639 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 );
3640} 3655}
3641 3656
3642sub reload_treasures { 3657sub reload_treasures {
3643 load_resource_file "$DATADIR/treasures" 3658 load_resource_file "$DATADIR/treasures"
3644 or die "unable to load treasurelists\n"; 3659 or die "unable to load treasurelists\n";
3662 my $face = cf::face::find "sound/$v"; 3677 my $face = cf::face::find "sound/$v";
3663 cf::sound::set $k => $face; 3678 cf::sound::set $k => $face;
3664 } 3679 }
3665} 3680}
3666 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
3667sub reload_resources { 3744sub reload_resources {
3668 trace "reloading resource files...\n"; 3745 trace "reloading resource files...\n";
3669 3746
3670 reload_exp_table;
3671 reload_materials; 3747 reload_materials;
3672 reload_facedata; 3748 reload_facedata;
3749 reload_exp_table;
3673 reload_sound; 3750 reload_sound;
3674 reload_archetypes; 3751 reload_archetypes;
3675 reload_regions; 3752 reload_regions;
3676 reload_treasures; 3753 reload_treasures;
3754 reload_pod;
3677 3755
3678 trace "finished reloading resource files\n"; 3756 trace "finished reloading resource files\n";
3679} 3757}
3680 3758
3681sub reload_config { 3759sub reload_config {
3736 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3814 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3737 3815
3738 $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
3739 3817
3740 # we must not ever block the main coroutine 3818 # we must not ever block the main coroutine
3741 local $Coro::idle = sub { 3819 $Coro::idle = sub {
3742 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#
3743 (async { 3821 (async {
3744 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3822 $Coro::current->{desc} = "IDLE BUG HANDLER";
3745 EV::loop EV::LOOP_ONESHOT; 3823 EV::loop EV::LOOP_ONESHOT;
3746 })->prio (Coro::PRIO_MAX); 3824 })->prio (Coro::PRIO_MAX);
3749 evthread_start IO::AIO::poll_fileno; 3827 evthread_start IO::AIO::poll_fileno;
3750 3828
3751 cf::sync_job { 3829 cf::sync_job {
3752 cf::incloader::init (); 3830 cf::incloader::init ();
3753 3831
3832 db_init;
3833
3754 cf::init_anim; 3834 cf::init_anim;
3755 cf::init_attackmess; 3835 cf::init_attackmess;
3756 cf::init_dynamic; 3836 cf::init_dynamic;
3757 3837
3758 cf::load_settings; 3838 cf::load_settings;
3759 3839
3760 reload_resources; 3840 reload_resources;
3761 reload_config; 3841 reload_config;
3762 db_init;
3763 3842
3764 cf::init_uuid; 3843 cf::init_uuid;
3765 cf::init_signals; 3844 cf::init_signals;
3766 cf::init_skills; 3845 cf::init_skills;
3767 3846
4192 } 4271 }
4193} 4272}
4194 4273
4195{ 4274{
4196 # configure BDB 4275 # configure BDB
4276 info "initialising database";
4197 4277
4198 BDB::min_parallel 16; 4278 BDB::min_parallel 16;
4199 BDB::max_poll_reqs $TICK * 0.1; 4279 BDB::max_poll_reqs $TICK * 0.1;
4200 #$AnyEvent::BDB::WATCHER->priority (1); 4280 #$AnyEvent::BDB::WATCHER->priority (1);
4201 4281
4230 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 4310 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
4231 }; 4311 };
4232 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub { 4312 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub {
4233 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; 4313 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
4234 }; 4314 };
4315
4316 info "database initialised";
4235} 4317}
4236 4318
4237{ 4319{
4238 # configure IO::AIO 4320 # configure IO::AIO
4239 4321
4322 info "initialising aio";
4240 IO::AIO::min_parallel 8; 4323 IO::AIO::min_parallel 8;
4241 IO::AIO::max_poll_time $TICK * 0.1; 4324 IO::AIO::max_poll_time $TICK * 0.1;
4242 undef $AnyEvent::AIO::WATCHER; 4325 undef $AnyEvent::AIO::WATCHER;
4326 info "aio initialised";
4243} 4327}
4244 4328
4245our $_log_backtrace; 4329our $_log_backtrace;
4246our $_log_backtrace_last; 4330our $_log_backtrace_last;
4247 4331

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines