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.612 by root, Wed Nov 21 12:47:09 2012 UTC vs.
Revision 1.624 by root, Sat Nov 17 23:40:02 2018 UTC

1# 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG. 2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3# 3#
4# Copyright (©) 2018 Marc Alexander Lehmann / the Deliantra team
4# Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5# Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 6#
6# Deliantra is free software: you can redistribute it and/or modify it under 7# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 8# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 9# Free Software Foundation, either version 3 of the License, or (at your
57use JSON::XS 2.01 (); 58use JSON::XS 2.01 ();
58use BDB (); 59use BDB ();
59use Data::Dumper; 60use Data::Dumper;
60use Fcntl; 61use Fcntl;
61use YAML::XS (); 62use YAML::XS ();
63use CBOR::XS ();
62use IO::AIO (); 64use IO::AIO ();
63use Compress::LZF; 65use Compress::LZF;
64use Digest::MD5 (); 66use Digest::MD5 ();
65 67
66AnyEvent::detect; 68AnyEvent::detect;
225=item $cf::RUNTIME 227=item $cf::RUNTIME
226 228
227The time this server has run, starts at 0 and is increased by $cf::TICK on 229The time this server has run, starts at 0 and is increased by $cf::TICK on
228every server tick. 230every server tick.
229 231
230=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR 232=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR
231$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR 233$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR
232$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR 234$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR
233 235
234Various directories - "/etc", read-only install directory, perl-library 236Various directories - "/etc", read-only install directory, perl-library
235directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 237directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
464 466
465sub decode_yaml($) { 467sub decode_yaml($) {
466 fork_call { YAML::XS::Load $_[0] } @_ 468 fork_call { YAML::XS::Load $_[0] } @_
467} 469}
468 470
471=item $scalar = cf::decode_cbor $scalar
472
473Same as CBOR::XS::decode_cbor, but takes server ticks into account, so
474blocks. For small amounts of data, C<CBOR::XS::decode_cbor> is the better
475alternative.
476
477=cut
478
479sub decode_cbor($) {
480 # we assume 10mb/s minimum decoding speed (on a ~2ghz machine)
481 cf::get_slot +(length $_[0]) / 10_000_000, 0, "decode_cbor";
482 CBOR::XS::decode_cbor $_[0]
483}
484
469=item $scalar = cf::unlzf $scalar 485=item $scalar = cf::unlzf $scalar
470 486
471Same as Compress::LZF::compress, but takes server ticks into account, so 487Same as Compress::LZF::compress, but takes server ticks into account, so
472blocks. 488blocks.
473 489
510 } 526 }
511} 527}
512 528
513=item cf::lock_wait $string 529=item cf::lock_wait $string
514 530
515Wait until the given lock is available. See cf::lock_acquire. 531Wait until the given lock is available. See cf::lock_acquire.
516 532
517=item my $lock = cf::lock_acquire $string 533=item my $lock = cf::lock_acquire $string
518 534
519Wait until the given lock is available and then acquires it and returns 535Wait until the given lock is available and then acquires it and returns
520a L<Guard> object. If the guard object gets destroyed (goes out of scope, 536a L<Guard> object. If the guard object gets destroyed (goes out of scope,
649BEGIN { *async = \&Coro::async_pool } 665BEGIN { *async = \&Coro::async_pool }
650 666
651=item cf::sync_job { BLOCK } 667=item cf::sync_job { BLOCK }
652 668
653The design of Deliantra requires that the main coroutine ($Coro::main) 669The design of Deliantra requires that the main coroutine ($Coro::main)
654is always able to handle events or runnable, as Deliantra is only 670is always able to handle events or is runnable, as Deliantra is only
655partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not 671partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not
656acceptable. 672acceptable.
657 673
658If it must be done, put the blocking parts into C<sync_job>. This will run 674If it must be done, put the blocking parts into C<sync_job>. This will run
659the given BLOCK in another coroutine while waiting for the result. The 675the given BLOCK in another coroutine while waiting for the result. The
687 703
688 while ($busy) { 704 while ($busy) {
689 if (Coro::nready) { 705 if (Coro::nready) {
690 Coro::cede_notself; 706 Coro::cede_notself;
691 } else { 707 } else {
692 EV::loop EV::LOOP_ONESHOT; 708 EV::run EV::RUN_ONCE;
693 } 709 }
694 } 710 }
695 711
696 my $time = AE::time - $time; 712 my $time = AE::time - $time;
697 713
2384 2400
2385 $MAP_PREFETCHER ||= cf::async { 2401 $MAP_PREFETCHER ||= cf::async {
2386 $Coro::current->{desc} = "map prefetcher"; 2402 $Coro::current->{desc} = "map prefetcher";
2387 2403
2388 while (%MAP_PREFETCH) { 2404 while (%MAP_PREFETCH) {
2389 while (my ($k, $v) = each %MAP_PREFETCH) { 2405 for my $k (keys %MAP_PREFETCH) {
2390 if (my $map = find $k) { 2406 if (my $map = find $k) {
2391 $map->load if $v; 2407 $map->load if $MAP_PREFETCH{$k};
2392 } 2408 }
2393 2409
2394 delete $MAP_PREFETCH{$k}; 2410 delete $MAP_PREFETCH{$k};
2395 } 2411 }
2396 } 2412 }
2742=item $player_object->may ("access") 2758=item $player_object->may ("access")
2743 2759
2744Returns wether the given player is authorized to access resource "access" 2760Returns wether the given player is authorized to access resource "access"
2745(e.g. "command_wizcast"). 2761(e.g. "command_wizcast").
2746 2762
2763This is implemented by checking a config setting of C<may_access> where
2764C<access> is replaced by the access string. The following alternatives are
2765possible (and are tested in order):
2766
2767=over 4
2768
2769=item * Player is DM
2770
2771The request will succeed.
2772
2773=item * may_access is an array reference
2774
2775If either the player nickname or UUID is in the array, the request will
2776succeed, otherwise it will fail.
2777
2778=item * may_access is a true value
2779
2780The request will succeed.
2781
2782=item * may_access is missing or false
2783
2784The request will fail.
2785
2786=back
2787
2747=cut 2788=cut
2748 2789
2749sub cf::object::player::may { 2790sub cf::object::player::may {
2750 my ($self, $access) = @_; 2791 my ($self, $access) = @_;
2751 2792
2752 $self->flag (cf::FLAG_WIZ) || 2793 $self->flag (cf::FLAG_WIZ) ||
2753 (ref $cf::CFG{"may_$access"} 2794 (ref $cf::CFG{"may_$access"}
2754 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 2795 ? scalar grep $self->name eq $_ || $self->uuid eq $_, @{$cf::CFG{"may_$access"}}
2755 : $cf::CFG{"may_$access"}) 2796 : $cf::CFG{"may_$access"})
2756} 2797}
2757 2798
2758=item $player_object->enter_link 2799=item $player_object->enter_link
2759 2800
2762The player should be reasonably safe there for short amounts of time (e.g. 2803The player should be reasonably safe there for short amounts of time (e.g.
2763for loading a map). You I<MUST> call C<leave_link> as soon as possible, 2804for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2764though, as the player cannot control the character while it is on the link 2805though, as the player cannot control the character while it is on the link
2765map. 2806map.
2766 2807
2767Will never block. 2808This method will never block, which is the whole reaosn for it's
2809existance: you can I<always> put a player onto the link map, which is the
2810only place to put objects that is guaranteed to exist.
2811
2812A typical usage pattern is to call C<enter_link> synchronously from the
2813server, then start a new thread, do your blocking stuff there and then
2814call C<leave_link> from that thread.
2768 2815
2769=item $player_object->leave_link ($map, $x, $y) 2816=item $player_object->leave_link ($map, $x, $y)
2770 2817
2771Moves the player out of the special C<{link}> map onto the specified 2818Moves the player out of the special C<{link}> map onto the specified
2772map. If the map is not valid (or omitted), the player will be moved back 2819map. If the map is not valid (or omitted), the player will be moved back
3124 }, 3171 },
3125 "c/body" => { 3172 "c/body" => {
3126 id => "infobox", 3173 id => "infobox",
3127 title => "Body Parts", 3174 title => "Body Parts",
3128 reply => undef, 3175 reply => undef,
3129 tooltip => "Shows which body parts you posess and are available", 3176 tooltip => "Shows which body parts you possess and are available",
3130 }, 3177 },
3131 "c/statistics" => { 3178 "c/statistics" => {
3132 id => "infobox", 3179 id => "infobox",
3133 title => "Statistics", 3180 title => "Statistics",
3134 reply => undef, 3181 reply => undef,
3286 3333
3287sub cf::client::update_command_faces { 3334sub cf::client::update_command_faces {
3288 my ($self) = @_; 3335 my ($self) = @_;
3289 3336
3290 my @faces = grep $_, 3337 my @faces = grep $_,
3338 $COMMAND_FACE{preferred},
3291 $COMMAND_FACE{standard}, 3339 $COMMAND_FACE{standard},
3292 $COMMAND_FACE{emote},
3293 $COMMAND_FACE{skill}, 3340 $COMMAND_FACE{skill},
3294 $self->pl->ob->flag (cf::FLAG_WIZ) ? $COMMAND_FACE{dm} : (), 3341 $self->pl->ob->flag (cf::FLAG_WIZ) ? $COMMAND_FACE{dm} : (),
3342 $COMMAND_FACE{emote},
3295 ; 3343 ;
3296 3344
3297 $self->send_face ($_) 3345 $self->send_face ($_)
3298 for @faces; 3346 for @faces;
3299 $self->flush_fx; 3347 $self->flush_fx;
3883 seek $fh, 0, 0; 3931 seek $fh, 0, 0;
3884 print $fh $$; 3932 print $fh $$;
3885} 3933}
3886 3934
3887sub main_loop { 3935sub main_loop {
3888 trace "EV::loop starting\n"; 3936 trace "EV::run starting\n";
3889 if (1) { 3937 if (1) {
3890 EV::loop; 3938 EV::run;
3891 } 3939 }
3892 trace "EV::loop returned\n"; 3940 trace "EV::run returned\n";
3893 goto &main_loop unless $REALLY_UNLOOP; 3941 goto &main_loop unless $REALLY_UNLOOP;
3894} 3942}
3895 3943
3896sub main { 3944sub main {
3897 cf::init_globals; # initialise logging 3945 cf::init_globals; # initialise logging
3898 3946
3899 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3947 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3948 LOG llevInfo, "Copyright (C) 2017-2018 Marc Alexander Lehmann / the Deliantra team.";
3900 LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3949 LOG llevInfo, "Copyright (C) 2005-2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3901 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3950 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3902 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3951 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3903 3952
3904 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3953 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3905 3954
3906 # we must not ever block the main coroutine 3955 # we must not ever block the main coroutine
3907 $Coro::idle = sub { 3956 $Coro::idle = sub {
3908 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3957 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3909 (async { 3958 (async {
3910 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3959 $Coro::current->{desc} = "IDLE BUG HANDLER";
3911 EV::loop EV::LOOP_ONESHOT; 3960 EV::run EV::RUN_ONCE;
3912 })->prio (Coro::PRIO_MAX); 3961 })->prio (Coro::PRIO_MAX);
3913 }; 3962 };
3914 3963
3915 evthread_start IO::AIO::poll_fileno; 3964 evthread_start IO::AIO::poll_fileno;
3916 3965

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines