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.76 by root, Mon Oct 2 15:28:36 2006 UTC vs.
Revision 1.82 by root, Sun Dec 3 20:59:52 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use IO::AIO ();
10use YAML::Syck (); 11use YAML::Syck ();
11use Time::HiRes; 12use Time::HiRes;
12use Event; 13use Event;
13$Event::Eval = 1; # no idea why this is required, but it is 14$Event::Eval = 1; # no idea why this is required, but it is
14 15
522 unlink $filename; 523 unlink $filename;
523 unlink "$filename.pst"; 524 unlink "$filename.pst";
524 } 525 }
525} 526}
526 527
528sub object_freezer_as_string {
529 my ($rdata, $objs) = @_;
530
531 use Data::Dumper;
532
533 $$rdata . Dumper $objs
534}
535
527sub object_thawer_load { 536sub object_thawer_load {
528 my ($filename) = @_; 537 my ($filename) = @_;
529 538
530 local $/; 539 local $/;
531 540
633 . "#line 1 \"$path\"\n{\n" 642 . "#line 1 \"$path\"\n{\n"
634 . (do { local $/; <$fh> }) 643 . (do { local $/; <$fh> })
635 . "\n};\n1"; 644 . "\n};\n1";
636 645
637 eval $source 646 eval $source
638 or die "$path: $@"; 647 or die $@ ? "$path: $@\n"
648 : "extension disabled.\n";
639 649
640 push @exts, $pkg; 650 push @exts, $pkg;
641 $ext_pkg{$base} = $pkg; 651 $ext_pkg{$base} = $pkg;
642 652
643# no strict 'refs'; 653# no strict 'refs';
774sub cf::player::exists($) { 784sub cf::player::exists($) {
775 cf::player::find $_[0] 785 cf::player::find $_[0]
776 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 786 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
777} 787}
778 788
779=item $object->reply ($npc, $msg[, $flags]) 789=item $player_object->reply ($npc, $msg[, $flags])
780 790
781Sends a message to the player, as if the npc C<$npc> replied. C<$npc> 791Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
782can be C<undef>. Does the right thing when the player is currently in a 792can be C<undef>. Does the right thing when the player is currently in a
783dialogue with the given NPC character. 793dialogue with the given NPC character.
784 794
811 $msg{msgid} = $id; 821 $msg{msgid} = $id;
812 822
813 $self->send ("ext " . to_json \%msg); 823 $self->send ("ext " . to_json \%msg);
814} 824}
815 825
816=back 826=item $player_object->may ("access")
827
828Returns wether the given player is authorized to access resource "access"
829(e.g. "command_wizcast").
830
831=cut
832
833sub cf::object::player::may {
834 my ($self, $access) = @_;
835
836 $self->flag (cf::FLAG_WIZ) ||
837 (ref $cf::CFG{"may_$access"}
838 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
839 : $cf::CFG{"may_$access"})
840}
817 841
818=cut 842=cut
819 843
820############################################################################# 844#############################################################################
821 845
823 847
824Functions that provide a safe environment to compile and execute 848Functions that provide a safe environment to compile and execute
825snippets of perl code without them endangering the safety of the server 849snippets of perl code without them endangering the safety of the server
826itself. Looping constructs, I/O operators and other built-in functionality 850itself. Looping constructs, I/O operators and other built-in functionality
827is not available in the safe scripting environment, and the number of 851is not available in the safe scripting environment, and the number of
828functions and methods that cna be called is greatly reduced. 852functions and methods that can be called is greatly reduced.
829 853
830=cut 854=cut
831 855
832our $safe = new Safe "safe"; 856our $safe = new Safe "safe";
833our $safe_hole = new Safe::Hole; 857our $safe_hole = new Safe::Hole;
969 993
970Immediately write the database to disk I<if it is dirty>. 994Immediately write the database to disk I<if it is dirty>.
971 995
972=cut 996=cut
973 997
998our $DB;
999
974{ 1000{
975 my $db;
976 my $path = cf::localdir . "/database.pst"; 1001 my $path = cf::localdir . "/database.pst";
977 1002
978 sub db_load() { 1003 sub db_load() {
979 warn "loading database $path\n";#d# remove later 1004 warn "loading database $path\n";#d# remove later
980 $db = stat $path ? Storable::retrieve $path : { }; 1005 $DB = stat $path ? Storable::retrieve $path : { };
981 } 1006 }
982 1007
983 my $pid; 1008 my $pid;
984 1009
985 sub db_save() { 1010 sub db_save() {
986 warn "saving database $path\n";#d# remove later 1011 warn "saving database $path\n";#d# remove later
987 waitpid $pid, 0 if $pid; 1012 waitpid $pid, 0 if $pid;
988 if (0 == ($pid = fork)) { 1013 if (0 == ($pid = fork)) {
989 $db->{_meta}{version} = 1; 1014 $DB->{_meta}{version} = 1;
990 Storable::nstore $db, "$path~"; 1015 Storable::nstore $DB, "$path~";
991 rename "$path~", $path; 1016 rename "$path~", $path;
992 cf::_exit 0 if defined $pid; 1017 cf::_exit 0 if defined $pid;
993 } 1018 }
994 } 1019 }
995 1020
1009 $idle->start; 1034 $idle->start;
1010 } 1035 }
1011 1036
1012 sub db_get($;$) { 1037 sub db_get($;$) {
1013 @_ >= 2 1038 @_ >= 2
1014 ? $db->{$_[0]}{$_[1]} 1039 ? $DB->{$_[0]}{$_[1]}
1015 : ($db->{$_[0]} ||= { }) 1040 : ($DB->{$_[0]} ||= { })
1016 } 1041 }
1017 1042
1018 sub db_put($$;$) { 1043 sub db_put($$;$) {
1019 if (@_ >= 3) { 1044 if (@_ >= 3) {
1020 $db->{$_[0]}{$_[1]} = $_[2]; 1045 $DB->{$_[0]}{$_[1]} = $_[2];
1021 } else { 1046 } else {
1022 $db->{$_[0]} = $_[1]; 1047 $DB->{$_[0]} = $_[1];
1023 } 1048 }
1024 db_dirty; 1049 db_dirty;
1025 } 1050 }
1026 1051
1027 attach_global 1052 attach_global
1144register "<global>", __PACKAGE__; 1169register "<global>", __PACKAGE__;
1145 1170
1146unshift @INC, $LIBDIR; 1171unshift @INC, $LIBDIR;
1147 1172
1148$TICK_WATCHER = Event->timer ( 1173$TICK_WATCHER = Event->timer (
1149 prio => 1, 1174 prio => 1,
1175 async => 1,
1150 at => $NEXT_TICK || 1, 1176 at => $NEXT_TICK || 1,
1151 cb => sub { 1177 cb => sub {
1152 cf::server_tick; # one server iteration 1178 cf::server_tick; # one server iteration
1153 1179
1154 my $NOW = Event::time; 1180 my $NOW = Event::time;
1155 $NEXT_TICK += $TICK; 1181 $NEXT_TICK += $TICK;
1156 1182
1157 # if we are delayed by four ticks, skip them all 1183 # if we are delayed by four ticks or more, skip them all
1158 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; 1184 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
1159 1185
1160 $TICK_WATCHER->at ($NEXT_TICK); 1186 $TICK_WATCHER->at ($NEXT_TICK);
1161 $TICK_WATCHER->start; 1187 $TICK_WATCHER->start;
1162 }, 1188 },
1163); 1189);
1164 1190
1191IO::AIO::max_poll_time $TICK * 0.2;
1192
1193Event->io (fd => IO::AIO::poll_fileno,
1194 poll => 'r',
1195 prio => 5,
1196 cb => \&IO::AIO::poll_cb);
1197
11651 11981
1166 1199

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines