… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use IO::AIO (); |
10 | use YAML::Syck (); |
11 | use YAML::Syck (); |
11 | use Time::HiRes; |
12 | use Time::HiRes; |
12 | use Event; |
13 | use 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 | |
|
|
528 | sub object_freezer_as_string { |
|
|
529 | my ($rdata, $objs) = @_; |
|
|
530 | |
|
|
531 | use Data::Dumper; |
|
|
532 | |
|
|
533 | $$rdata . Dumper $objs |
|
|
534 | } |
|
|
535 | |
527 | sub object_thawer_load { |
536 | sub 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'; |
… | |
… | |
774 | sub cf::player::exists($) { |
784 | sub 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 | |
781 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
791 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
782 | can be C<undef>. Does the right thing when the player is currently in a |
792 | can be C<undef>. Does the right thing when the player is currently in a |
783 | dialogue with the given NPC character. |
793 | dialogue 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 | |
|
|
828 | Returns wether the given player is authorized to access resource "access" |
|
|
829 | (e.g. "command_wizcast"). |
|
|
830 | |
|
|
831 | =cut |
|
|
832 | |
|
|
833 | sub 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 | |
824 | Functions that provide a safe environment to compile and execute |
848 | Functions that provide a safe environment to compile and execute |
825 | snippets of perl code without them endangering the safety of the server |
849 | snippets of perl code without them endangering the safety of the server |
826 | itself. Looping constructs, I/O operators and other built-in functionality |
850 | itself. Looping constructs, I/O operators and other built-in functionality |
827 | is not available in the safe scripting environment, and the number of |
851 | is not available in the safe scripting environment, and the number of |
828 | functions and methods that cna be called is greatly reduced. |
852 | functions and methods that can be called is greatly reduced. |
829 | |
853 | |
830 | =cut |
854 | =cut |
831 | |
855 | |
832 | our $safe = new Safe "safe"; |
856 | our $safe = new Safe "safe"; |
833 | our $safe_hole = new Safe::Hole; |
857 | our $safe_hole = new Safe::Hole; |
… | |
… | |
969 | |
993 | |
970 | Immediately write the database to disk I<if it is dirty>. |
994 | Immediately write the database to disk I<if it is dirty>. |
971 | |
995 | |
972 | =cut |
996 | =cut |
973 | |
997 | |
|
|
998 | our $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 |
… | |
… | |
1144 | register "<global>", __PACKAGE__; |
1169 | register "<global>", __PACKAGE__; |
1145 | |
1170 | |
1146 | unshift @INC, $LIBDIR; |
1171 | unshift @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 | |
|
|
1191 | IO::AIO::max_poll_time $TICK * 0.2; |
|
|
1192 | |
|
|
1193 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1194 | poll => 'r', |
|
|
1195 | prio => 5, |
|
|
1196 | cb => \&IO::AIO::poll_cb); |
|
|
1197 | |
1165 | 1 |
1198 | 1 |
1166 | |
1199 | |