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.462 by root, Tue Dec 23 06:58:23 2008 UTC vs.
Revision 1.473 by root, Fri Jun 26 16:32:10 2009 UTC

21 21
22package cf; 22package cf;
23 23
24use 5.10.0; 24use 5.10.0;
25use utf8; 25use utf8;
26use strict "vars", "subs"; 26use strict qw(vars subs);
27 27
28use Symbol; 28use Symbol;
29use List::Util; 29use List::Util;
30use Socket; 30use Socket;
31use EV; 31use EV;
105our %RESOURCE; 105our %RESOURCE;
106 106
107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
109our $NEXT_TICK; 109our $NEXT_TICK;
110our $USE_FSYNC = 1; # use fsync to write maps - default off 110our $USE_FSYNC = 1; # use fsync to write maps - default on
111 111
112our $BDB_DEADLOCK_WATCHER; 112our $BDB_DEADLOCK_WATCHER;
113our $BDB_CHECKPOINT_WATCHER; 113our $BDB_CHECKPOINT_WATCHER;
114our $BDB_TRICKLE_WATCHER; 114our $BDB_TRICKLE_WATCHER;
115our $DB_ENV; 115our $DB_ENV;
116
117our @EXTRA_MODULES = qw(pod mapscript);
116 118
117our %CFG; 119our %CFG;
118 120
119our $UPTIME; $UPTIME ||= time; 121our $UPTIME; $UPTIME ||= time;
120our $RUNTIME; 122our $RUNTIME;
143unless ($RUNTIME || !-e $RUNTIMEFILE) { 145unless ($RUNTIME || !-e $RUNTIMEFILE) {
144 open my $fh, "<", $RUNTIMEFILE 146 open my $fh, "<", $RUNTIMEFILE
145 or die "unable to read $RUNTIMEFILE file: $!"; 147 or die "unable to read $RUNTIMEFILE file: $!";
146 $RUNTIME = <$fh> + 0.; 148 $RUNTIME = <$fh> + 0.;
147} 149}
150
151eval "sub TICK() { $TICK } 1" or die;
148 152
149mkdir $_ 153mkdir $_
150 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 154 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
151 155
152our $EMERGENCY_POSITION; 156our $EMERGENCY_POSITION;
1147 $decname, length $$rdata, scalar @$objs; 1151 $decname, length $$rdata, scalar @$objs;
1148 1152
1149 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1153 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1150 aio_chmod $fh, SAVE_MODE; 1154 aio_chmod $fh, SAVE_MODE;
1151 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1155 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1152 aio_fsync $fh if $cf::USE_FSYNC; 1156 if ($cf::USE_FSYNC) {
1157 aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER;
1158 aio_fsync $fh;
1159 }
1153 aio_close $fh; 1160 aio_close $fh;
1154 1161
1155 if (@$objs) { 1162 if (@$objs) {
1156 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1163 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1157 aio_chmod $fh, SAVE_MODE; 1164 aio_chmod $fh, SAVE_MODE;
1158 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; 1165 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs };
1159 aio_write $fh, 0, (length $data), $data, 0; 1166 aio_write $fh, 0, (length $data), $data, 0;
1160 aio_fsync $fh if $cf::USE_FSYNC; 1167 if ($cf::USE_FSYNC) {
1168 aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER;
1169 aio_fsync $fh;
1170 }
1161 aio_close $fh; 1171 aio_close $fh;
1162 aio_rename "$filename.pst~", "$filename.pst"; 1172 aio_rename "$filename.pst~", "$filename.pst";
1163 } 1173 }
1164 } else { 1174 } else {
1165 aio_unlink "$filename.pst"; 1175 aio_unlink "$filename.pst";
1316 1326
1317# "readahead" all extensions 1327# "readahead" all extensions
1318sub cache_extensions { 1328sub cache_extensions {
1319 my $grp = IO::AIO::aio_group; 1329 my $grp = IO::AIO::aio_group;
1320 1330
1321 add $grp IO::AIO::aio_readdir $LIBDIR, sub { 1331 add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub {
1322 for (grep /\.ext$/, @{$_[0]}) { 1332 for (grep /\.ext$/, @{$_[0]}) {
1323 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; 1333 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1324 } 1334 }
1325 }; 1335 };
1326 1336
2384 2394
2385our $SAY_CHANNEL = { 2395our $SAY_CHANNEL = {
2386 id => "say", 2396 id => "say",
2387 title => "Map", 2397 title => "Map",
2388 reply => "say ", 2398 reply => "say ",
2389 tooltip => "Things said to and replied from npcs near you and other players on the same map only.", 2399 tooltip => "Things said to and replied from NPCs near you and other players on the same map only.",
2390}; 2400};
2391 2401
2392our $CHAT_CHANNEL = { 2402our $CHAT_CHANNEL = {
2393 id => "chat", 2403 id => "chat",
2394 title => "Chat", 2404 title => "Chat",
2769 id => "infobox", 2779 id => "infobox",
2770 title => "Body Parts", 2780 title => "Body Parts",
2771 reply => undef, 2781 reply => undef,
2772 tooltip => "Shows which body parts you posess and are available", 2782 tooltip => "Shows which body parts you posess and are available",
2773 }, 2783 },
2784 "c/statistics" => {
2785 id => "infobox",
2786 title => "Statistics",
2787 reply => undef,
2788 tooltip => "Shows your primary statistics",
2789 },
2774 "c/skills" => { 2790 "c/skills" => {
2775 id => "infobox", 2791 id => "infobox",
2776 title => "Skills", 2792 title => "Skills",
2777 reply => undef, 2793 reply => undef,
2778 tooltip => "Shows your experience per skill and item power", 2794 tooltip => "Shows your experience per skill and item power",
2795 },
2796 "c/shopitems" => {
2797 id => "infobox",
2798 title => "Shop Items",
2799 reply => undef,
2800 tooltip => "Shows the items currently for sale in this shop",
2801 },
2802 "c/resistances" => {
2803 id => "infobox",
2804 title => "Resistances",
2805 reply => undef,
2806 tooltip => "Shows your resistances",
2807 },
2808 "c/pets" => {
2809 id => "infobox",
2810 title => "Pets",
2811 reply => undef,
2812 tooltip => "Shows information abotu your pets/a specific pet",
2813 },
2814 "c/perceiveself" => {
2815 id => "infobox",
2816 title => "Perceive Self",
2817 reply => undef,
2818 tooltip => "You gained detailed knowledge about yourself",
2779 }, 2819 },
2780 "c/uptime" => { 2820 "c/uptime" => {
2781 id => "infobox", 2821 id => "infobox",
2782 title => "Uptime", 2822 title => "Uptime",
2783 reply => undef, 2823 reply => undef,
2793 id => "party", 2833 id => "party",
2794 title => "Party", 2834 title => "Party",
2795 reply => "gsay ", 2835 reply => "gsay ",
2796 tooltip => "Messages and chat related to your party", 2836 tooltip => "Messages and chat related to your party",
2797 }, 2837 },
2838 "c/death" => {
2839 id => "death",
2840 title => "Death",
2841 reply => undef,
2842 tooltip => "Reason for and more info about your most recent death",
2843 },
2798 "c/say" => $SAY_CHANNEL, 2844 "c/say" => $SAY_CHANNEL,
2799 "c/chat" => $CHAT_CHANNEL, 2845 "c/chat" => $CHAT_CHANNEL,
2800); 2846);
2801 2847
2802sub cf::client::send_msg { 2848sub cf::client::send_msg {
2809 2855
2810 # check predefined channels, for the benefit of C 2856 # check predefined channels, for the benefit of C
2811 if ($CHANNEL{$channel}) { 2857 if ($CHANNEL{$channel}) {
2812 $channel = $CHANNEL{$channel}; 2858 $channel = $CHANNEL{$channel};
2813 2859
2814 $self->ext_msg (channel_info => $channel) 2860 $self->ext_msg (channel_info => $channel);
2815 if $self->can_msg;
2816
2817 $channel = $channel->{id}; 2861 $channel = $channel->{id};
2818 2862
2819 } elsif (ref $channel) { 2863 } elsif (ref $channel) {
2820 # send meta info to client, if not yet sent 2864 # send meta info to client, if not yet sent
2821 unless (exists $self->{channel}{$channel->{id}}) { 2865 unless (exists $self->{channel}{$channel->{id}}) {
2822 $self->{channel}{$channel->{id}} = $channel; 2866 $self->{channel}{$channel->{id}} = $channel;
2823 $self->ext_msg (channel_info => $channel) 2867 $self->ext_msg (channel_info => $channel);
2824 if $self->can_msg;
2825 } 2868 }
2826 2869
2827 $channel = $channel->{id}; 2870 $channel = $channel->{id};
2828 } 2871 }
2829 2872
2830 return unless @extra || length $msg; 2873 return unless @extra || length $msg;
2831 2874
2832 if ($self->can_msg) {
2833 # default colour, mask it out 2875 # default colour, mask it out
2834 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) 2876 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2835 if $color & cf::NDI_DEF; 2877 if $color & cf::NDI_DEF;
2836 2878
2837 my $pkt = "msg " 2879 my $pkt = "msg "
2838 . $self->{json_coder}->encode ( 2880 . $self->{json_coder}->encode (
2839 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2881 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2840 ); 2882 );
2841 2883
2842 # try lzf for large packets 2884 # try lzf for large packets
2843 $pkt = "lzf " . Compress::LZF::compress $pkt 2885 $pkt = "lzf " . Compress::LZF::compress $pkt
2844 if 1024 <= length $pkt and $self->{can_lzf}; 2886 if 1024 <= length $pkt and $self->{can_lzf};
2845 2887
2846 # split very large packets 2888 # split very large packets
2847 if (8192 < length $pkt and $self->{can_lzf}) { 2889 if (8192 < length $pkt and $self->{can_lzf}) {
2848 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; 2890 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2849 $pkt = "frag"; 2891 $pkt = "frag";
2850 } 2892 }
2851 2893
2852 $self->send_packet ($pkt); 2894 $self->send_packet ($pkt);
2853 } else {
2854 if ($color >= 0) {
2855 # replace some tags by gcfclient-compatible ones
2856 for ($msg) {
2857 1 while
2858 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2859 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2860 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2861 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2862 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2863 }
2864
2865 $color &= cf::NDI_COLOR_MASK;
2866
2867 utf8::encode $msg;
2868
2869 if (0 && $msg =~ /\[/) {
2870 # COMMAND/INFO
2871 $self->send_packet ("drawextinfo $color 10 8 $msg")
2872 } else {
2873 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2874 $self->send_packet ("drawinfo $color $msg")
2875 }
2876 }
2877 }
2878} 2895}
2879 2896
2880=item $client->ext_msg ($type, @msg) 2897=item $client->ext_msg ($type, @msg)
2881 2898
2882Sends an ext event to the client. 2899Sends an ext event to the client.
3057The following functions and methods are available within a safe environment: 3074The following functions and methods are available within a safe environment:
3058 3075
3059 cf::object 3076 cf::object
3060 contr pay_amount pay_player map x y force_find force_add destroy 3077 contr pay_amount pay_player map x y force_find force_add destroy
3061 insert remove name archname title slaying race decrease split 3078 insert remove name archname title slaying race decrease split
3079 value
3062 3080
3063 cf::object::player 3081 cf::object::player
3064 player 3082 player
3065 3083
3066 cf::player 3084 cf::player
3072=cut 3090=cut
3073 3091
3074for ( 3092for (
3075 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 3093 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
3076 insert remove inv nrof name archname title slaying race 3094 insert remove inv nrof name archname title slaying race
3077 decrease split destroy change_exp)], 3095 decrease split destroy change_exp value msg lore send_msg)],
3078 ["cf::object::player" => qw(player)], 3096 ["cf::object::player" => qw(player)],
3079 ["cf::player" => qw(peaceful)], 3097 ["cf::player" => qw(peaceful send_msg)],
3080 ["cf::map" => qw(trigger)], 3098 ["cf::map" => qw(trigger)],
3081) { 3099) {
3082 no strict 'refs'; 3100 no strict 'refs';
3083 my ($pkg, @funs) = @$_; 3101 my ($pkg, @funs) = @$_;
3084 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3102 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3101 my ($code, %vars) = @_; 3119 my ($code, %vars) = @_;
3102 3120
3103 my $qcode = $code; 3121 my $qcode = $code;
3104 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3122 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3105 $qcode =~ s/\n/\\n/g; 3123 $qcode =~ s/\n/\\n/g;
3124
3125 %vars = (_dummy => 0) unless %vars;
3106 3126
3107 local $_; 3127 local $_;
3108 local @safe::cf::_safe_eval_args = values %vars; 3128 local @safe::cf::_safe_eval_args = values %vars;
3109 3129
3110 my $eval = 3130 my $eval =
3698 clear_package "safe::$_" 3718 clear_package "safe::$_"
3699 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3719 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3700 3720
3701 warn "unloading cf.pm \"a bit\""; 3721 warn "unloading cf.pm \"a bit\"";
3702 delete $INC{"cf.pm"}; 3722 delete $INC{"cf.pm"};
3703 delete $INC{"cf/pod.pm"}; 3723 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3704 3724
3705 # don't, removes xs symbols, too, 3725 # don't, removes xs symbols, too,
3706 # and global variables created in xs 3726 # and global variables created in xs
3707 #clear_package __PACKAGE__; 3727 #clear_package __PACKAGE__;
3708 3728
3925 LOG llevInfo, "[ABT] [suppressed]\n"; 3945 LOG llevInfo, "[ABT] [suppressed]\n";
3926 } 3946 }
3927} 3947}
3928 3948
3929# load additional modules 3949# load additional modules
3930use cf::pod; 3950require "cf/$_.pm" for @EXTRA_MODULES;
3931 3951
3932END { cf::emergency_save } 3952END { cf::emergency_save }
3933 3953
39341 39541
3935 3955

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines