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.77 by root, Sun Nov 5 11:13:01 2006 UTC vs.
Revision 1.83 by root, Mon Dec 11 01:30:41 2006 UTC

28our $TICK_WATCHER; 28our $TICK_WATCHER;
29our $NEXT_TICK; 29our $NEXT_TICK;
30 30
31our %CFG; 31our %CFG;
32 32
33our $uptime; 33our $uptime;#d#
34 34our $UPTIME;
35$uptime ||= time; 35$UPTIME ||= $uptime;#d#
36$UPTIME ||= time;
36 37
37############################################################################# 38#############################################################################
38 39
39=head2 GLOBAL VARIABLES 40=head2 GLOBAL VARIABLES
40 41
41=over 4 42=over 4
43
44=item $cf::UPTIME
45
46The timestamp of the server start (so not actually an uptime).
42 47
43=item $cf::LIBDIR 48=item $cf::LIBDIR
44 49
45The perl library directory, where extensions and cf-specific modules can 50The perl library directory, where extensions and cf-specific modules can
46be found. It will be added to C<@INC> automatically. 51be found. It will be added to C<@INC> automatically.
523 unlink $filename; 528 unlink $filename;
524 unlink "$filename.pst"; 529 unlink "$filename.pst";
525 } 530 }
526} 531}
527 532
533sub object_freezer_as_string {
534 my ($rdata, $objs) = @_;
535
536 use Data::Dumper;
537
538 $$rdata . Dumper $objs
539}
540
528sub object_thawer_load { 541sub object_thawer_load {
529 my ($filename) = @_; 542 my ($filename) = @_;
530 543
531 local $/; 544 local $/;
532 545
634 . "#line 1 \"$path\"\n{\n" 647 . "#line 1 \"$path\"\n{\n"
635 . (do { local $/; <$fh> }) 648 . (do { local $/; <$fh> })
636 . "\n};\n1"; 649 . "\n};\n1";
637 650
638 eval $source 651 eval $source
639 or die "$path: $@"; 652 or die $@ ? "$path: $@\n"
653 : "extension disabled.\n";
640 654
641 push @exts, $pkg; 655 push @exts, $pkg;
642 $ext_pkg{$base} = $pkg; 656 $ext_pkg{$base} = $pkg;
643 657
644# no strict 'refs'; 658# no strict 'refs';
775sub cf::player::exists($) { 789sub cf::player::exists($) {
776 cf::player::find $_[0] 790 cf::player::find $_[0]
777 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 791 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
778} 792}
779 793
780=item $object->reply ($npc, $msg[, $flags]) 794=item $player_object->reply ($npc, $msg[, $flags])
781 795
782Sends a message to the player, as if the npc C<$npc> replied. C<$npc> 796Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
783can be C<undef>. Does the right thing when the player is currently in a 797can be C<undef>. Does the right thing when the player is currently in a
784dialogue with the given NPC character. 798dialogue with the given NPC character.
785 799
812 $msg{msgid} = $id; 826 $msg{msgid} = $id;
813 827
814 $self->send ("ext " . to_json \%msg); 828 $self->send ("ext " . to_json \%msg);
815} 829}
816 830
817=back 831=item $player_object->may ("access")
832
833Returns wether the given player is authorized to access resource "access"
834(e.g. "command_wizcast").
835
836=cut
837
838sub cf::object::player::may {
839 my ($self, $access) = @_;
840
841 $self->flag (cf::FLAG_WIZ) ||
842 (ref $cf::CFG{"may_$access"}
843 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
844 : $cf::CFG{"may_$access"})
845}
818 846
819=cut 847=cut
820 848
821############################################################################# 849#############################################################################
822 850
824 852
825Functions that provide a safe environment to compile and execute 853Functions that provide a safe environment to compile and execute
826snippets of perl code without them endangering the safety of the server 854snippets of perl code without them endangering the safety of the server
827itself. Looping constructs, I/O operators and other built-in functionality 855itself. Looping constructs, I/O operators and other built-in functionality
828is not available in the safe scripting environment, and the number of 856is not available in the safe scripting environment, and the number of
829functions and methods that cna be called is greatly reduced. 857functions and methods that can be called is greatly reduced.
830 858
831=cut 859=cut
832 860
833our $safe = new Safe "safe"; 861our $safe = new Safe "safe";
834our $safe_hole = new Safe::Hole; 862our $safe_hole = new Safe::Hole;
970 998
971Immediately write the database to disk I<if it is dirty>. 999Immediately write the database to disk I<if it is dirty>.
972 1000
973=cut 1001=cut
974 1002
1003our $DB;
1004
975{ 1005{
976 my $db;
977 my $path = cf::localdir . "/database.pst"; 1006 my $path = cf::localdir . "/database.pst";
978 1007
979 sub db_load() { 1008 sub db_load() {
980 warn "loading database $path\n";#d# remove later 1009 warn "loading database $path\n";#d# remove later
981 $db = stat $path ? Storable::retrieve $path : { }; 1010 $DB = stat $path ? Storable::retrieve $path : { };
982 } 1011 }
983 1012
984 my $pid; 1013 my $pid;
985 1014
986 sub db_save() { 1015 sub db_save() {
987 warn "saving database $path\n";#d# remove later 1016 warn "saving database $path\n";#d# remove later
988 waitpid $pid, 0 if $pid; 1017 waitpid $pid, 0 if $pid;
989 if (0 == ($pid = fork)) { 1018 if (0 == ($pid = fork)) {
990 $db->{_meta}{version} = 1; 1019 $DB->{_meta}{version} = 1;
991 Storable::nstore $db, "$path~"; 1020 Storable::nstore $DB, "$path~";
992 rename "$path~", $path; 1021 rename "$path~", $path;
993 cf::_exit 0 if defined $pid; 1022 cf::_exit 0 if defined $pid;
994 } 1023 }
995 } 1024 }
996 1025
1010 $idle->start; 1039 $idle->start;
1011 } 1040 }
1012 1041
1013 sub db_get($;$) { 1042 sub db_get($;$) {
1014 @_ >= 2 1043 @_ >= 2
1015 ? $db->{$_[0]}{$_[1]} 1044 ? $DB->{$_[0]}{$_[1]}
1016 : ($db->{$_[0]} ||= { }) 1045 : ($DB->{$_[0]} ||= { })
1017 } 1046 }
1018 1047
1019 sub db_put($$;$) { 1048 sub db_put($$;$) {
1020 if (@_ >= 3) { 1049 if (@_ >= 3) {
1021 $db->{$_[0]}{$_[1]} = $_[2]; 1050 $DB->{$_[0]}{$_[1]} = $_[2];
1022 } else { 1051 } else {
1023 $db->{$_[0]} = $_[1]; 1052 $DB->{$_[0]} = $_[1];
1024 } 1053 }
1025 db_dirty; 1054 db_dirty;
1026 } 1055 }
1027 1056
1028 attach_global 1057 attach_global
1154 cf::server_tick; # one server iteration 1183 cf::server_tick; # one server iteration
1155 1184
1156 my $NOW = Event::time; 1185 my $NOW = Event::time;
1157 $NEXT_TICK += $TICK; 1186 $NEXT_TICK += $TICK;
1158 1187
1159 # if we are delayed by four ticks, skip them all 1188 # if we are delayed by four ticks or more, skip them all
1160 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; 1189 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
1161 1190
1162 $TICK_WATCHER->at ($NEXT_TICK); 1191 $TICK_WATCHER->at ($NEXT_TICK);
1163 $TICK_WATCHER->start; 1192 $TICK_WATCHER->start;
1164 }, 1193 },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines