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