… | |
… | |
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 (); |
|
|
11 | use YAML::Syck (); |
10 | use Time::HiRes; |
12 | use Time::HiRes; |
11 | use Event; |
13 | use Event; |
12 | $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 |
13 | |
15 | |
|
|
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
17 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
18 | |
14 | use strict; |
19 | use strict; |
15 | |
20 | |
16 | _init_vars; |
21 | _init_vars; |
17 | |
22 | |
18 | our %COMMAND = (); |
23 | our %COMMAND = (); |
… | |
… | |
22 | our $TICK = MAX_TIME * 1e-6; |
27 | our $TICK = MAX_TIME * 1e-6; |
23 | our $TICK_WATCHER; |
28 | our $TICK_WATCHER; |
24 | our $NEXT_TICK; |
29 | our $NEXT_TICK; |
25 | |
30 | |
26 | our %CFG; |
31 | our %CFG; |
|
|
32 | |
|
|
33 | our $uptime; |
|
|
34 | |
|
|
35 | $uptime ||= time; |
27 | |
36 | |
28 | ############################################################################# |
37 | ############################################################################# |
29 | |
38 | |
30 | =head2 GLOBAL VARIABLES |
39 | =head2 GLOBAL VARIABLES |
31 | |
40 | |
… | |
… | |
514 | unlink $filename; |
523 | unlink $filename; |
515 | unlink "$filename.pst"; |
524 | unlink "$filename.pst"; |
516 | } |
525 | } |
517 | } |
526 | } |
518 | |
527 | |
|
|
528 | sub object_freezer_as_string { |
|
|
529 | my ($rdata, $objs) = @_; |
|
|
530 | |
|
|
531 | use Data::Dumper; |
|
|
532 | |
|
|
533 | $$rdata . Dumper $objs |
|
|
534 | } |
|
|
535 | |
519 | sub object_thawer_load { |
536 | sub object_thawer_load { |
520 | my ($filename) = @_; |
537 | my ($filename) = @_; |
521 | |
538 | |
522 | local $/; |
539 | local $/; |
523 | |
540 | |
… | |
… | |
625 | . "#line 1 \"$path\"\n{\n" |
642 | . "#line 1 \"$path\"\n{\n" |
626 | . (do { local $/; <$fh> }) |
643 | . (do { local $/; <$fh> }) |
627 | . "\n};\n1"; |
644 | . "\n};\n1"; |
628 | |
645 | |
629 | eval $source |
646 | eval $source |
630 | or die "$path: $@"; |
647 | or die $@ ? "$path: $@\n" |
|
|
648 | : "extension disabled.\n"; |
631 | |
649 | |
632 | push @exts, $pkg; |
650 | push @exts, $pkg; |
633 | $ext_pkg{$base} = $pkg; |
651 | $ext_pkg{$base} = $pkg; |
634 | |
652 | |
635 | # no strict 'refs'; |
653 | # no strict 'refs'; |
… | |
… | |
766 | sub cf::player::exists($) { |
784 | sub cf::player::exists($) { |
767 | cf::player::find $_[0] |
785 | cf::player::find $_[0] |
768 | 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; |
769 | } |
787 | } |
770 | |
788 | |
771 | =item $player->reply ($npc, $msg[, $flags]) |
789 | =item $player_object->reply ($npc, $msg[, $flags]) |
772 | |
790 | |
773 | 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> |
774 | 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 |
775 | dialogue with the given NPC character. |
793 | dialogue with the given NPC character. |
776 | |
794 | |
… | |
… | |
803 | $msg{msgid} = $id; |
821 | $msg{msgid} = $id; |
804 | |
822 | |
805 | $self->send ("ext " . to_json \%msg); |
823 | $self->send ("ext " . to_json \%msg); |
806 | } |
824 | } |
807 | |
825 | |
808 | =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 | } |
809 | |
841 | |
810 | =cut |
842 | =cut |
811 | |
843 | |
812 | ############################################################################# |
844 | ############################################################################# |
813 | |
845 | |
… | |
… | |
815 | |
847 | |
816 | Functions that provide a safe environment to compile and execute |
848 | Functions that provide a safe environment to compile and execute |
817 | 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 |
818 | itself. Looping constructs, I/O operators and other built-in functionality |
850 | itself. Looping constructs, I/O operators and other built-in functionality |
819 | 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 |
820 | functions and methods that cna be called is greatly reduced. |
852 | functions and methods that can be called is greatly reduced. |
821 | |
853 | |
822 | =cut |
854 | =cut |
823 | |
855 | |
824 | our $safe = new Safe "safe"; |
856 | our $safe = new Safe "safe"; |
825 | our $safe_hole = new Safe::Hole; |
857 | our $safe_hole = new Safe::Hole; |
… | |
… | |
961 | |
993 | |
962 | Immediately write the database to disk I<if it is dirty>. |
994 | Immediately write the database to disk I<if it is dirty>. |
963 | |
995 | |
964 | =cut |
996 | =cut |
965 | |
997 | |
|
|
998 | our $DB; |
|
|
999 | |
966 | { |
1000 | { |
967 | my $db; |
|
|
968 | my $path = cf::localdir . "/database.pst"; |
1001 | my $path = cf::localdir . "/database.pst"; |
969 | |
1002 | |
970 | sub db_load() { |
1003 | sub db_load() { |
971 | warn "loading database $path\n";#d# remove later |
1004 | warn "loading database $path\n";#d# remove later |
972 | $db = stat $path ? Storable::retrieve $path : { }; |
1005 | $DB = stat $path ? Storable::retrieve $path : { }; |
973 | } |
1006 | } |
974 | |
1007 | |
975 | my $pid; |
1008 | my $pid; |
976 | |
1009 | |
977 | sub db_save() { |
1010 | sub db_save() { |
978 | warn "saving database $path\n";#d# remove later |
1011 | warn "saving database $path\n";#d# remove later |
979 | waitpid $pid, 0 if $pid; |
1012 | waitpid $pid, 0 if $pid; |
980 | if (0 == ($pid = fork)) { |
1013 | if (0 == ($pid = fork)) { |
981 | $db->{_meta}{version} = 1; |
1014 | $DB->{_meta}{version} = 1; |
982 | Storable::nstore $db, "$path~"; |
1015 | Storable::nstore $DB, "$path~"; |
983 | rename "$path~", $path; |
1016 | rename "$path~", $path; |
984 | cf::_exit 0 if defined $pid; |
1017 | cf::_exit 0 if defined $pid; |
985 | } |
1018 | } |
986 | } |
1019 | } |
987 | |
1020 | |
… | |
… | |
1001 | $idle->start; |
1034 | $idle->start; |
1002 | } |
1035 | } |
1003 | |
1036 | |
1004 | sub db_get($;$) { |
1037 | sub db_get($;$) { |
1005 | @_ >= 2 |
1038 | @_ >= 2 |
1006 | ? $db->{$_[0]}{$_[1]} |
1039 | ? $DB->{$_[0]}{$_[1]} |
1007 | : ($db->{$_[0]} ||= { }) |
1040 | : ($DB->{$_[0]} ||= { }) |
1008 | } |
1041 | } |
1009 | |
1042 | |
1010 | sub db_put($$;$) { |
1043 | sub db_put($$;$) { |
1011 | if (@_ >= 3) { |
1044 | if (@_ >= 3) { |
1012 | $db->{$_[0]}{$_[1]} = $_[2]; |
1045 | $DB->{$_[0]}{$_[1]} = $_[2]; |
1013 | } else { |
1046 | } else { |
1014 | $db->{$_[0]} = $_[1]; |
1047 | $DB->{$_[0]} = $_[1]; |
1015 | } |
1048 | } |
1016 | db_dirty; |
1049 | db_dirty; |
1017 | } |
1050 | } |
1018 | |
1051 | |
1019 | attach_global |
1052 | attach_global |
… | |
… | |
1025 | } |
1058 | } |
1026 | |
1059 | |
1027 | ############################################################################# |
1060 | ############################################################################# |
1028 | # the server's main() |
1061 | # the server's main() |
1029 | |
1062 | |
|
|
1063 | sub cfg_load { |
|
|
1064 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1065 | or return; |
|
|
1066 | |
|
|
1067 | local $/; |
|
|
1068 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1069 | } |
|
|
1070 | |
1030 | sub main { |
1071 | sub main { |
|
|
1072 | cfg_load; |
1031 | db_load; |
1073 | db_load; |
1032 | load_extensions; |
1074 | load_extensions; |
1033 | Event::loop; |
1075 | Event::loop; |
1034 | } |
1076 | } |
1035 | |
1077 | |
… | |
… | |
1087 | |
1129 | |
1088 | # reload cf.pm |
1130 | # reload cf.pm |
1089 | $msg->("reloading cf.pm"); |
1131 | $msg->("reloading cf.pm"); |
1090 | require cf; |
1132 | require cf; |
1091 | |
1133 | |
1092 | # load database again |
1134 | # load config and database again |
|
|
1135 | cf::cfg_load; |
1093 | cf::db_load; |
1136 | cf::db_load; |
1094 | |
1137 | |
1095 | # load extensions |
1138 | # load extensions |
1096 | $msg->("load extensions"); |
1139 | $msg->("load extensions"); |
1097 | cf::load_extensions; |
1140 | cf::load_extensions; |
… | |
… | |
1126 | register "<global>", __PACKAGE__; |
1169 | register "<global>", __PACKAGE__; |
1127 | |
1170 | |
1128 | unshift @INC, $LIBDIR; |
1171 | unshift @INC, $LIBDIR; |
1129 | |
1172 | |
1130 | $TICK_WATCHER = Event->timer ( |
1173 | $TICK_WATCHER = Event->timer ( |
1131 | prio => 1, |
1174 | prio => 1, |
|
|
1175 | async => 1, |
1132 | at => $NEXT_TICK || 1, |
1176 | at => $NEXT_TICK || 1, |
1133 | cb => sub { |
1177 | cb => sub { |
1134 | cf::server_tick; # one server iteration |
1178 | cf::server_tick; # one server iteration |
1135 | |
1179 | |
1136 | my $NOW = Event::time; |
1180 | my $NOW = Event::time; |
1137 | $NEXT_TICK += $TICK; |
1181 | $NEXT_TICK += $TICK; |
1138 | |
1182 | |
1139 | # if we are delayed by four ticks, skip them all |
1183 | # if we are delayed by four ticks or more, skip them all |
1140 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1184 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1141 | |
1185 | |
1142 | $TICK_WATCHER->at ($NEXT_TICK); |
1186 | $TICK_WATCHER->at ($NEXT_TICK); |
1143 | $TICK_WATCHER->start; |
1187 | $TICK_WATCHER->start; |
1144 | }, |
1188 | }, |
1145 | ); |
1189 | ); |
1146 | |
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 | |
1147 | 1 |
1198 | 1 |
1148 | |
1199 | |