… | |
… | |
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 | |
… | |
… | |
766 | sub cf::player::exists($) { |
775 | sub cf::player::exists($) { |
767 | cf::player::find $_[0] |
776 | cf::player::find $_[0] |
768 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
777 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
769 | } |
778 | } |
770 | |
779 | |
771 | =item $player->reply ($npc, $msg[, $flags]) |
780 | =item $player_object->reply ($npc, $msg[, $flags]) |
772 | |
781 | |
773 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
782 | 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 |
783 | can be C<undef>. Does the right thing when the player is currently in a |
775 | dialogue with the given NPC character. |
784 | dialogue with the given NPC character. |
776 | |
785 | |
… | |
… | |
803 | $msg{msgid} = $id; |
812 | $msg{msgid} = $id; |
804 | |
813 | |
805 | $self->send ("ext " . to_json \%msg); |
814 | $self->send ("ext " . to_json \%msg); |
806 | } |
815 | } |
807 | |
816 | |
808 | =back |
817 | =item $player_object->may ("access") |
|
|
818 | |
|
|
819 | Returns wether the given player is authorized to access resource "access" |
|
|
820 | (e.g. "command_wizcast"). |
|
|
821 | |
|
|
822 | =cut |
|
|
823 | |
|
|
824 | sub cf::object::player::may { |
|
|
825 | my ($self, $access) = @_; |
|
|
826 | |
|
|
827 | $self->flag (cf::FLAG_WIZ) || |
|
|
828 | (ref $cf::CFG{"may_$access"} |
|
|
829 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
|
|
830 | : $cf::CFG{"may_$access"}) |
|
|
831 | } |
809 | |
832 | |
810 | =cut |
833 | =cut |
811 | |
834 | |
812 | ############################################################################# |
835 | ############################################################################# |
813 | |
836 | |
… | |
… | |
815 | |
838 | |
816 | Functions that provide a safe environment to compile and execute |
839 | Functions that provide a safe environment to compile and execute |
817 | snippets of perl code without them endangering the safety of the server |
840 | snippets of perl code without them endangering the safety of the server |
818 | itself. Looping constructs, I/O operators and other built-in functionality |
841 | itself. Looping constructs, I/O operators and other built-in functionality |
819 | is not available in the safe scripting environment, and the number of |
842 | is not available in the safe scripting environment, and the number of |
820 | functions and methods that cna be called is greatly reduced. |
843 | functions and methods that can be called is greatly reduced. |
821 | |
844 | |
822 | =cut |
845 | =cut |
823 | |
846 | |
824 | our $safe = new Safe "safe"; |
847 | our $safe = new Safe "safe"; |
825 | our $safe_hole = new Safe::Hole; |
848 | our $safe_hole = new Safe::Hole; |
… | |
… | |
961 | |
984 | |
962 | Immediately write the database to disk I<if it is dirty>. |
985 | Immediately write the database to disk I<if it is dirty>. |
963 | |
986 | |
964 | =cut |
987 | =cut |
965 | |
988 | |
|
|
989 | our $DB; |
|
|
990 | |
966 | { |
991 | { |
967 | my $db; |
|
|
968 | my $path = cf::localdir . "/database.pst"; |
992 | my $path = cf::localdir . "/database.pst"; |
969 | |
993 | |
970 | sub db_load() { |
994 | sub db_load() { |
971 | warn "loading database $path\n";#d# remove later |
995 | warn "loading database $path\n";#d# remove later |
972 | $db = stat $path ? Storable::retrieve $path : { }; |
996 | $DB = stat $path ? Storable::retrieve $path : { }; |
973 | } |
997 | } |
974 | |
998 | |
975 | my $pid; |
999 | my $pid; |
976 | |
1000 | |
977 | sub db_save() { |
1001 | sub db_save() { |
978 | warn "saving database $path\n";#d# remove later |
1002 | warn "saving database $path\n";#d# remove later |
979 | waitpid $pid, 0 if $pid; |
1003 | waitpid $pid, 0 if $pid; |
980 | if (0 == ($pid = fork)) { |
1004 | if (0 == ($pid = fork)) { |
981 | $db->{_meta}{version} = 1; |
1005 | $DB->{_meta}{version} = 1; |
982 | Storable::nstore $db, "$path~"; |
1006 | Storable::nstore $DB, "$path~"; |
983 | rename "$path~", $path; |
1007 | rename "$path~", $path; |
984 | cf::_exit 0 if defined $pid; |
1008 | cf::_exit 0 if defined $pid; |
985 | } |
1009 | } |
986 | } |
1010 | } |
987 | |
1011 | |
… | |
… | |
1001 | $idle->start; |
1025 | $idle->start; |
1002 | } |
1026 | } |
1003 | |
1027 | |
1004 | sub db_get($;$) { |
1028 | sub db_get($;$) { |
1005 | @_ >= 2 |
1029 | @_ >= 2 |
1006 | ? $db->{$_[0]}{$_[1]} |
1030 | ? $DB->{$_[0]}{$_[1]} |
1007 | : ($db->{$_[0]} ||= { }) |
1031 | : ($DB->{$_[0]} ||= { }) |
1008 | } |
1032 | } |
1009 | |
1033 | |
1010 | sub db_put($$;$) { |
1034 | sub db_put($$;$) { |
1011 | if (@_ >= 3) { |
1035 | if (@_ >= 3) { |
1012 | $db->{$_[0]}{$_[1]} = $_[2]; |
1036 | $DB->{$_[0]}{$_[1]} = $_[2]; |
1013 | } else { |
1037 | } else { |
1014 | $db->{$_[0]} = $_[1]; |
1038 | $DB->{$_[0]} = $_[1]; |
1015 | } |
1039 | } |
1016 | db_dirty; |
1040 | db_dirty; |
1017 | } |
1041 | } |
1018 | |
1042 | |
1019 | attach_global |
1043 | attach_global |
… | |
… | |
1025 | } |
1049 | } |
1026 | |
1050 | |
1027 | ############################################################################# |
1051 | ############################################################################# |
1028 | # the server's main() |
1052 | # the server's main() |
1029 | |
1053 | |
|
|
1054 | sub cfg_load { |
|
|
1055 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1056 | or return; |
|
|
1057 | |
|
|
1058 | local $/; |
|
|
1059 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1060 | } |
|
|
1061 | |
1030 | sub main { |
1062 | sub main { |
|
|
1063 | cfg_load; |
1031 | db_load; |
1064 | db_load; |
1032 | load_extensions; |
1065 | load_extensions; |
1033 | Event::loop; |
1066 | Event::loop; |
1034 | } |
1067 | } |
1035 | |
1068 | |
… | |
… | |
1087 | |
1120 | |
1088 | # reload cf.pm |
1121 | # reload cf.pm |
1089 | $msg->("reloading cf.pm"); |
1122 | $msg->("reloading cf.pm"); |
1090 | require cf; |
1123 | require cf; |
1091 | |
1124 | |
1092 | # load database again |
1125 | # load config and database again |
|
|
1126 | cf::cfg_load; |
1093 | cf::db_load; |
1127 | cf::db_load; |
1094 | |
1128 | |
1095 | # load extensions |
1129 | # load extensions |
1096 | $msg->("load extensions"); |
1130 | $msg->("load extensions"); |
1097 | cf::load_extensions; |
1131 | cf::load_extensions; |
… | |
… | |
1126 | register "<global>", __PACKAGE__; |
1160 | register "<global>", __PACKAGE__; |
1127 | |
1161 | |
1128 | unshift @INC, $LIBDIR; |
1162 | unshift @INC, $LIBDIR; |
1129 | |
1163 | |
1130 | $TICK_WATCHER = Event->timer ( |
1164 | $TICK_WATCHER = Event->timer ( |
1131 | prio => 1, |
1165 | prio => 1, |
|
|
1166 | async => 1, |
1132 | at => $NEXT_TICK || 1, |
1167 | at => $NEXT_TICK || 1, |
1133 | cb => sub { |
1168 | cb => sub { |
1134 | cf::server_tick; # one server iteration |
1169 | cf::server_tick; # one server iteration |
1135 | |
1170 | |
1136 | my $NOW = Event::time; |
1171 | my $NOW = Event::time; |
1137 | $NEXT_TICK += $TICK; |
1172 | $NEXT_TICK += $TICK; |
1138 | |
1173 | |
1139 | # if we are delayed by four ticks, skip them all |
1174 | # if we are delayed by four ticks or more, skip them all |
1140 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1175 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1141 | |
1176 | |
1142 | $TICK_WATCHER->at ($NEXT_TICK); |
1177 | $TICK_WATCHER->at ($NEXT_TICK); |
1143 | $TICK_WATCHER->start; |
1178 | $TICK_WATCHER->start; |
1144 | }, |
1179 | }, |
1145 | ); |
1180 | ); |
1146 | |
1181 | |
|
|
1182 | eval { IO::AIO::max_poll_time $TICK * 0.2 }; #d# remove eval after restart |
|
|
1183 | |
|
|
1184 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1185 | poll => 'r', |
|
|
1186 | prio => 5, |
|
|
1187 | cb => \&IO::AIO::poll_cb); |
|
|
1188 | |
1147 | 1 |
1189 | 1 |
1148 | |
1190 | |