… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Storable; |
8 | use Storable; |
|
|
9 | use Event; |
9 | use Opcode; |
10 | use Opcode; |
10 | use Safe; |
11 | use Safe; |
11 | use Safe::Hole; |
12 | use Safe::Hole; |
12 | |
13 | |
13 | use Coro 3.5 (); |
14 | use Coro 3.61 (); |
|
|
15 | use Coro::State; |
14 | use Coro::Event; |
16 | use Coro::Event; |
15 | use Coro::Timer; |
17 | use Coro::Timer; |
16 | use Coro::Signal; |
18 | use Coro::Signal; |
17 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
18 | use Coro::AIO; |
20 | use Coro::AIO; |
|
|
21 | use Coro::Storable; |
19 | |
22 | |
|
|
23 | use BDB (); |
20 | use Data::Dumper; |
24 | use Data::Dumper; |
21 | use Digest::MD5; |
25 | use Digest::MD5; |
22 | use Fcntl; |
26 | use Fcntl; |
|
|
27 | use YAML::Syck (); |
23 | use IO::AIO 2.32 (); |
28 | use IO::AIO 2.32 (); |
24 | use YAML::Syck (); |
|
|
25 | use Time::HiRes; |
29 | use Time::HiRes; |
|
|
30 | use Compress::LZF; |
26 | |
31 | |
|
|
32 | # configure various modules to our taste |
|
|
33 | # |
|
|
34 | $Storable::canonical = 1; # reduce rsync transfers |
|
|
35 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
|
|
36 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
37 | |
27 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
38 | $Event::Eval = 1; # no idea why this is required, but it is |
28 | |
|
|
29 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
30 | |
39 | |
31 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
40 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
32 | $YAML::Syck::ImplicitUnicode = 1; |
41 | $YAML::Syck::ImplicitUnicode = 1; |
33 | |
42 | |
34 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
43 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
44 | |
|
|
45 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
35 | |
46 | |
36 | our %COMMAND = (); |
47 | our %COMMAND = (); |
37 | our %COMMAND_TIME = (); |
48 | our %COMMAND_TIME = (); |
38 | |
49 | |
39 | our @EXTS = (); # list of extension package names |
50 | our @EXTS = (); # list of extension package names |
40 | our %EXTCMD = (); |
51 | our %EXTCMD = (); |
41 | our %EXT_CORO = (); # coroutines bound to extensions |
52 | our %EXT_CORO = (); # coroutines bound to extensions |
42 | our %EXT_MAP = (); # pluggable maps |
53 | our %EXT_MAP = (); # pluggable maps |
43 | |
54 | |
|
|
55 | our $RELOAD; # number of reloads so far |
44 | our @EVENT; |
56 | our @EVENT; |
45 | our $LIBDIR = datadir . "/ext"; |
57 | our $LIBDIR = datadir . "/ext"; |
46 | |
58 | |
47 | our $TICK = MAX_TIME * 1e-6; |
59 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
48 | our $TICK_WATCHER; |
60 | our $TICK_WATCHER; |
49 | our $AIO_POLL_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
50 | our $WRITE_RUNTIME_WATCHER; |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
51 | our $NEXT_TICK; |
63 | our $NEXT_TICK; |
52 | our $NOW; |
64 | our $NOW; |
|
|
65 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
|
|
66 | |
|
|
67 | our $BDB_POLL_WATCHER; |
|
|
68 | our $DB_ENV; |
53 | |
69 | |
54 | our %CFG; |
70 | our %CFG; |
55 | |
71 | |
56 | our $UPTIME; $UPTIME ||= time; |
72 | our $UPTIME; $UPTIME ||= time; |
57 | our $RUNTIME; |
73 | our $RUNTIME; |
58 | |
74 | |
59 | our %PLAYER; # all users |
75 | our %PLAYER; # all users |
60 | our %MAP; # all maps |
76 | our %MAP; # all maps |
61 | our $LINK_MAP; # the special {link} map, which is always available |
77 | our $LINK_MAP; # the special {link} map, which is always available |
62 | our $RANDOM_MAPS = cf::localdir . "/random"; |
78 | our $RANDOM_MAPS = cf::localdir . "/random"; |
63 | |
79 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
64 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
65 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
|
|
66 | |
80 | |
67 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
81 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
68 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
82 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
69 | |
83 | |
70 | binmode STDOUT; |
84 | binmode STDOUT; |
… | |
… | |
80 | mkdir cf::localdir; |
94 | mkdir cf::localdir; |
81 | mkdir cf::localdir . "/" . cf::playerdir; |
95 | mkdir cf::localdir . "/" . cf::playerdir; |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
96 | mkdir cf::localdir . "/" . cf::tmpdir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
97 | mkdir cf::localdir . "/" . cf::uniquedir; |
84 | mkdir $RANDOM_MAPS; |
98 | mkdir $RANDOM_MAPS; |
|
|
99 | mkdir $BDB_ENV_DIR; |
85 | |
100 | |
86 | our $EMERGENCY_POSITION; |
101 | our $EMERGENCY_POSITION; |
87 | |
102 | |
88 | sub cf::map::normalise; |
103 | sub cf::map::normalise; |
89 | |
104 | |
… | |
… | |
118 | =item %cf::CFG |
133 | =item %cf::CFG |
119 | |
134 | |
120 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
135 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
121 | from wherever your confdir points to. |
136 | from wherever your confdir points to. |
122 | |
137 | |
123 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
138 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
124 | |
139 | |
125 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
140 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
126 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
141 | returns directly I<after> the tick processing (and consequently, can only wake one process |
127 | processing has been done. Call C<< ->wait >> on them to maximise the |
142 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
128 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
129 | |
143 | |
130 | =back |
144 | =back |
131 | |
145 | |
132 | =cut |
146 | =cut |
133 | |
147 | |
… | |
… | |
192 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
206 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
193 | $d |
207 | $d |
194 | } || "[unable to dump $_[0]: '$@']"; |
208 | } || "[unable to dump $_[0]: '$@']"; |
195 | } |
209 | } |
196 | |
210 | |
197 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
211 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
198 | |
212 | |
199 | =item $ref = cf::from_json $json |
213 | =item $ref = cf::from_json $json |
200 | |
214 | |
201 | Converts a JSON string into the corresponding perl data structure. |
215 | Converts a JSON string into the corresponding perl data structure. |
202 | |
216 | |
203 | =cut |
|
|
204 | |
|
|
205 | sub from_json($) { |
|
|
206 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
207 | JSON::Syck::Load $_[0] |
|
|
208 | } |
|
|
209 | |
|
|
210 | =item $json = cf::to_json $ref |
217 | =item $json = cf::to_json $ref |
211 | |
218 | |
212 | Converts a perl data structure into its JSON representation. |
219 | Converts a perl data structure into its JSON representation. |
213 | |
|
|
214 | =cut |
|
|
215 | |
|
|
216 | sub to_json($) { |
|
|
217 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
218 | JSON::Syck::Dump $_[0] |
|
|
219 | } |
|
|
220 | |
220 | |
221 | =item cf::lock_wait $string |
221 | =item cf::lock_wait $string |
222 | |
222 | |
223 | Wait until the given lock is available. See cf::lock_acquire. |
223 | Wait until the given lock is available. See cf::lock_acquire. |
224 | |
224 | |
… | |
… | |
263 | sub freeze_mainloop { |
263 | sub freeze_mainloop { |
264 | return unless $TICK_WATCHER->is_active; |
264 | return unless $TICK_WATCHER->is_active; |
265 | |
265 | |
266 | my $guard = Coro::guard { |
266 | my $guard = Coro::guard { |
267 | $TICK_WATCHER->start; |
267 | $TICK_WATCHER->start; |
268 | $WRITE_RUNTIME_WATCHER->start; |
|
|
269 | }; |
268 | }; |
270 | $WRITE_RUNTIME_WATCHER->stop; |
|
|
271 | $TICK_WATCHER->stop; |
269 | $TICK_WATCHER->stop; |
272 | $guard |
270 | $guard |
273 | } |
271 | } |
274 | |
272 | |
275 | =item cf::async { BLOCK } |
273 | =item cf::async { BLOCK } |
… | |
… | |
346 | |
344 | |
347 | $coro |
345 | $coro |
348 | } |
346 | } |
349 | |
347 | |
350 | sub write_runtime { |
348 | sub write_runtime { |
|
|
349 | my $guard = cf::lock_acquire "write_runtime"; |
|
|
350 | |
351 | my $runtime = cf::localdir . "/runtime"; |
351 | my $runtime = cf::localdir . "/runtime"; |
352 | |
352 | |
353 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
353 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
354 | or return; |
354 | or return; |
355 | |
355 | |
… | |
… | |
358 | # 60 allows for the watchdog to kill the server. |
358 | # 60 allows for the watchdog to kill the server. |
359 | |
359 | |
360 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
360 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
361 | and return; |
361 | and return; |
362 | |
362 | |
|
|
363 | # always fsync - this file is important |
363 | aio_fsync $fh |
364 | aio_fsync $fh |
364 | and return; |
365 | and return; |
365 | |
366 | |
366 | close $fh |
367 | close $fh |
367 | or return; |
368 | or return; |
368 | |
369 | |
369 | aio_rename "$runtime~", $runtime |
370 | aio_rename "$runtime~", $runtime |
370 | and return; |
371 | and return; |
371 | |
372 | |
372 | 1 |
373 | 1 |
|
|
374 | } |
|
|
375 | |
|
|
376 | =item cf::datalog type => key => value, ... |
|
|
377 | |
|
|
378 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
379 | |
|
|
380 | =cut |
|
|
381 | |
|
|
382 | sub datalog($@) { |
|
|
383 | my ($type, %kv) = @_; |
|
|
384 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
373 | } |
385 | } |
374 | |
386 | |
375 | =back |
387 | =back |
376 | |
388 | |
377 | =cut |
389 | =cut |
… | |
… | |
752 | $filename, length $$rdata, scalar @$objs; |
764 | $filename, length $$rdata, scalar @$objs; |
753 | |
765 | |
754 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
766 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
755 | chmod SAVE_MODE, $fh; |
767 | chmod SAVE_MODE, $fh; |
756 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
768 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
757 | aio_fsync $fh; |
769 | aio_fsync $fh if $cf::USE_FSYNC; |
758 | close $fh; |
770 | close $fh; |
759 | |
771 | |
760 | if (@$objs) { |
772 | if (@$objs) { |
761 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
773 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
762 | chmod SAVE_MODE, $fh; |
774 | chmod SAVE_MODE, $fh; |
763 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
775 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
764 | aio_write $fh, 0, (length $data), $data, 0; |
776 | aio_write $fh, 0, (length $data), $data, 0; |
765 | aio_fsync $fh; |
777 | aio_fsync $fh if $cf::USE_FSYNC; |
766 | close $fh; |
778 | close $fh; |
767 | aio_rename "$filename.pst~", "$filename.pst"; |
779 | aio_rename "$filename.pst~", "$filename.pst"; |
768 | } |
780 | } |
769 | } else { |
781 | } else { |
770 | aio_unlink "$filename.pst"; |
782 | aio_unlink "$filename.pst"; |
… | |
… | |
776 | } |
788 | } |
777 | } else { |
789 | } else { |
778 | aio_unlink $filename; |
790 | aio_unlink $filename; |
779 | aio_unlink "$filename.pst"; |
791 | aio_unlink "$filename.pst"; |
780 | } |
792 | } |
781 | |
|
|
782 | #d##TODO# nuke non .map-files if exist |
|
|
783 | if ($filename =~ s/\.map$//) { |
|
|
784 | aio_unlink $filename; |
|
|
785 | aio_unlink "$filename.pst"; |
|
|
786 | } |
|
|
787 | } |
793 | } |
788 | } |
794 | } |
789 | |
795 | |
790 | sub object_freezer_as_string { |
796 | sub object_freezer_as_string { |
791 | my ($rdata, $objs) = @_; |
797 | my ($rdata, $objs) = @_; |
… | |
… | |
797 | |
803 | |
798 | sub object_thawer_load { |
804 | sub object_thawer_load { |
799 | my ($filename) = @_; |
805 | my ($filename) = @_; |
800 | |
806 | |
801 | my ($data, $av); |
807 | my ($data, $av); |
802 | |
|
|
803 | #d#TODO remove .map if file does not exist |
|
|
804 | aio_stat $filename and $filename =~ s/\.map$//; |
|
|
805 | |
808 | |
806 | (aio_load $filename, $data) >= 0 |
809 | (aio_load $filename, $data) >= 0 |
807 | or return; |
810 | or return; |
808 | |
811 | |
809 | unless (aio_stat "$filename.pst") { |
812 | unless (aio_stat "$filename.pst") { |
… | |
… | |
958 | } |
961 | } |
959 | |
962 | |
960 | sub path($) { |
963 | sub path($) { |
961 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
964 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
962 | |
965 | |
963 | (playerdir $login) . "/$login.pl" |
966 | (playerdir $login) . "/playerdata" |
964 | } |
967 | } |
965 | |
968 | |
966 | sub find_active($) { |
969 | sub find_active($) { |
967 | $cf::PLAYER{$_[0]} |
970 | $cf::PLAYER{$_[0]} |
968 | and $cf::PLAYER{$_[0]}->active |
971 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
981 | my $login = $_[0]; |
984 | my $login = $_[0]; |
982 | |
985 | |
983 | my $guard = cf::lock_acquire "user_find:$login"; |
986 | my $guard = cf::lock_acquire "user_find:$login"; |
984 | |
987 | |
985 | $cf::PLAYER{$_[0]} || do { |
988 | $cf::PLAYER{$_[0]} || do { |
|
|
989 | # rename old playerfiles to new ones |
|
|
990 | #TODO: remove when no longer required |
|
|
991 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
992 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
993 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
994 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
995 | |
986 | my $pl = load_pl path $login |
996 | my $pl = load_pl path $login |
987 | or return; |
997 | or return; |
988 | $cf::PLAYER{$login} = $pl |
998 | $cf::PLAYER{$login} = $pl |
989 | } |
999 | } |
990 | } |
1000 | } |
… | |
… | |
1027 | |
1037 | |
1028 | =cut |
1038 | =cut |
1029 | |
1039 | |
1030 | sub quit_character { |
1040 | sub quit_character { |
1031 | my ($pl) = @_; |
1041 | my ($pl) = @_; |
|
|
1042 | |
|
|
1043 | my $name = $pl->ob->name; |
1032 | |
1044 | |
1033 | $pl->{deny_save} = 1; |
1045 | $pl->{deny_save} = 1; |
1034 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1046 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1035 | |
1047 | |
1036 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1048 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
… | |
… | |
1041 | my $path = playerdir $pl; |
1053 | my $path = playerdir $pl; |
1042 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1054 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1043 | aio_rename $path, $temp; |
1055 | aio_rename $path, $temp; |
1044 | delete $cf::PLAYER{$pl->ob->name}; |
1056 | delete $cf::PLAYER{$pl->ob->name}; |
1045 | $pl->destroy; |
1057 | $pl->destroy; |
|
|
1058 | |
|
|
1059 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1060 | |
|
|
1061 | # nuke player maps |
|
|
1062 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1063 | |
1046 | IO::AIO::aio_rmtree $temp; |
1064 | IO::AIO::aio_rmtree $temp; |
|
|
1065 | } |
|
|
1066 | |
|
|
1067 | =item $pl->kick |
|
|
1068 | |
|
|
1069 | Kicks a player out of the game. This destroys the connection. |
|
|
1070 | |
|
|
1071 | =cut |
|
|
1072 | |
|
|
1073 | sub kick { |
|
|
1074 | my ($pl, $kicker) = @_; |
|
|
1075 | |
|
|
1076 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1077 | $pl->killer ("kicked"); |
|
|
1078 | $pl->ns->destroy; |
1047 | } |
1079 | } |
1048 | |
1080 | |
1049 | =item cf::player::list_logins |
1081 | =item cf::player::list_logins |
1050 | |
1082 | |
1051 | Returns am arrayref of all valid playernames in the system, can take a |
1083 | Returns am arrayref of all valid playernames in the system, can take a |
… | |
… | |
1097 | } |
1129 | } |
1098 | |
1130 | |
1099 | \@paths |
1131 | \@paths |
1100 | } |
1132 | } |
1101 | |
1133 | |
1102 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1134 | =item $player->ext_reply ($msgid, %msg) |
1103 | |
1135 | |
1104 | Sends an ext reply to the player. |
1136 | Sends an ext reply to the player. |
1105 | |
1137 | |
1106 | =cut |
1138 | =cut |
1107 | |
1139 | |
1108 | sub ext_reply($$$%) { |
1140 | sub ext_reply($$%) { |
1109 | my ($self, $id, %msg) = @_; |
1141 | my ($self, $id, %msg) = @_; |
1110 | |
1142 | |
1111 | $msg{msgid} = $id; |
1143 | $msg{msgid} = $id; |
1112 | |
1144 | |
1113 | $self->send ("ext " . cf::to_json \%msg); |
1145 | $self->send ("ext " . cf::to_json \%msg); |
1114 | } |
1146 | } |
1115 | |
1147 | |
1116 | package cf; |
1148 | =item $player->ext_event ($type, %msg) |
|
|
1149 | |
|
|
1150 | Sends an ext event to the client. |
|
|
1151 | |
|
|
1152 | =cut |
|
|
1153 | |
|
|
1154 | sub ext_event($$%) { |
|
|
1155 | my ($self, $type, %msg) = @_; |
|
|
1156 | |
|
|
1157 | $self->ns->ext_event ($type, %msg); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | =head3 cf::region |
|
|
1161 | |
|
|
1162 | =over 4 |
|
|
1163 | |
|
|
1164 | =cut |
|
|
1165 | |
|
|
1166 | package cf::region; |
|
|
1167 | |
|
|
1168 | =item cf::region::find_by_path $path |
|
|
1169 | |
|
|
1170 | Tries to decuce the probable region for a map knowing only its path. |
|
|
1171 | |
|
|
1172 | =cut |
|
|
1173 | |
|
|
1174 | sub find_by_path($) { |
|
|
1175 | my ($path) = @_; |
|
|
1176 | |
|
|
1177 | my ($match, $specificity); |
|
|
1178 | |
|
|
1179 | for my $region (list) { |
|
|
1180 | if ($region->match && $path =~ $region->match) { |
|
|
1181 | ($match, $specificity) = ($region, $region->specificity) |
|
|
1182 | if $region->specificity > $specificity; |
|
|
1183 | } |
|
|
1184 | } |
|
|
1185 | |
|
|
1186 | $match |
|
|
1187 | } |
1117 | |
1188 | |
1118 | =back |
1189 | =back |
1119 | |
|
|
1120 | |
1190 | |
1121 | =head3 cf::map |
1191 | =head3 cf::map |
1122 | |
1192 | |
1123 | =over 4 |
1193 | =over 4 |
1124 | |
1194 | |
… | |
… | |
1296 | my ($self) = @_; |
1366 | my ($self) = @_; |
1297 | |
1367 | |
1298 | utf8::encode (my $save = $self->save_path); |
1368 | utf8::encode (my $save = $self->save_path); |
1299 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1369 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1300 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1370 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1301 | |
|
|
1302 | #d#TODO remove .map and also nuke |
|
|
1303 | $save =~ s/\.map// or return;#d# |
|
|
1304 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1305 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
|
|
1306 | } |
1371 | } |
1307 | |
1372 | |
1308 | sub load_header_from($) { |
1373 | sub load_header_from($) { |
1309 | my ($self, $path) = @_; |
1374 | my ($self, $path) = @_; |
1310 | |
1375 | |
… | |
… | |
1360 | $self->load_header_orig |
1425 | $self->load_header_orig |
1361 | or return; |
1426 | or return; |
1362 | $self->prepare_orig; |
1427 | $self->prepare_orig; |
1363 | } |
1428 | } |
1364 | |
1429 | |
|
|
1430 | $self->default_region (cf::region::find_by_path $self->{path}) |
|
|
1431 | unless $self->default_region; |
|
|
1432 | |
1365 | 1 |
1433 | 1 |
1366 | } |
1434 | } |
1367 | |
1435 | |
1368 | sub find; |
1436 | sub find; |
1369 | sub find { |
1437 | sub find { |
… | |
… | |
1411 | $self->in_memory (cf::MAP_LOADING); |
1479 | $self->in_memory (cf::MAP_LOADING); |
1412 | |
1480 | |
1413 | $self->alloc; |
1481 | $self->alloc; |
1414 | |
1482 | |
1415 | $self->pre_load; |
1483 | $self->pre_load; |
|
|
1484 | Coro::cede; |
1416 | |
1485 | |
1417 | $self->_load_objects ($self->{load_path}, 1) |
1486 | $self->_load_objects ($self->{load_path}, 1) |
1418 | or return; |
1487 | or return; |
1419 | |
1488 | |
1420 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1489 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
… | |
… | |
1427 | $self->_load_objects ($uniq, 0); |
1496 | $self->_load_objects ($uniq, 0); |
1428 | } |
1497 | } |
1429 | } |
1498 | } |
1430 | |
1499 | |
1431 | Coro::cede; |
1500 | Coro::cede; |
1432 | |
|
|
1433 | # now do the right thing for maps |
1501 | # now do the right thing for maps |
1434 | $self->link_multipart_objects; |
1502 | $self->link_multipart_objects; |
|
|
1503 | Coro::cede; |
1435 | |
1504 | |
1436 | unless ($self->{deny_activate}) { |
1505 | unless ($self->{deny_activate}) { |
1437 | $self->decay_objects; |
1506 | $self->decay_objects; |
1438 | $self->fix_auto_apply; |
1507 | $self->fix_auto_apply; |
1439 | $self->update_buttons; |
1508 | $self->update_buttons; |
… | |
… | |
1441 | $self->set_darkness_map; |
1510 | $self->set_darkness_map; |
1442 | $self->difficulty ($self->estimate_difficulty) |
1511 | $self->difficulty ($self->estimate_difficulty) |
1443 | unless $self->difficulty; |
1512 | unless $self->difficulty; |
1444 | Coro::cede; |
1513 | Coro::cede; |
1445 | $self->activate; |
1514 | $self->activate; |
|
|
1515 | Coro::cede; |
1446 | } |
1516 | } |
1447 | |
1517 | |
1448 | $self->post_load; |
1518 | $self->post_load; |
|
|
1519 | Coro::cede; |
1449 | |
1520 | |
1450 | $self->in_memory (cf::MAP_IN_MEMORY); |
1521 | $self->in_memory (cf::MAP_IN_MEMORY); |
1451 | } |
1522 | } |
1452 | |
1523 | |
1453 | sub customise_for { |
1524 | sub customise_for { |
… | |
… | |
1598 | return if $self->players; |
1669 | return if $self->players; |
1599 | return if $self->isa ("ext::map_per_player");#d# |
1670 | return if $self->isa ("ext::map_per_player");#d# |
1600 | |
1671 | |
1601 | warn "resetting map ", $self->path;#d# |
1672 | warn "resetting map ", $self->path;#d# |
1602 | |
1673 | |
|
|
1674 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1675 | |
|
|
1676 | # need to save uniques path |
|
|
1677 | unless ($self->{deny_save}) { |
|
|
1678 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
1679 | |
|
|
1680 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
1681 | if $uniq; |
|
|
1682 | } |
|
|
1683 | |
1603 | delete $cf::MAP{$self->path}; |
1684 | delete $cf::MAP{$self->path}; |
1604 | |
1685 | |
1605 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1606 | $self->clear; |
1686 | $self->clear; |
1607 | |
1687 | |
1608 | $_->clear_links_to ($self) for values %cf::MAP; |
1688 | $_->clear_links_to ($self) for values %cf::MAP; |
1609 | |
1689 | |
1610 | $self->unlink_save; |
1690 | $self->unlink_save; |
… | |
… | |
1800 | $map->load; |
1880 | $map->load; |
1801 | $map->load_diag; |
1881 | $map->load_diag; |
1802 | |
1882 | |
1803 | return unless $self->contr->active; |
1883 | return unless $self->contr->active; |
1804 | $self->activate_recursive; |
1884 | $self->activate_recursive; |
|
|
1885 | |
|
|
1886 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
1805 | $self->enter_map ($map, $x, $y); |
1887 | $self->enter_map ($map, $x, $y); |
1806 | } |
1888 | } |
1807 | |
1889 | |
1808 | cf::player->attach ( |
1890 | cf::player->attach ( |
1809 | on_logout => sub { |
1891 | on_logout => sub { |
… | |
… | |
1822 | # try to abort aborted map switching on player login :) |
1904 | # try to abort aborted map switching on player login :) |
1823 | # should happen only on crashes |
1905 | # should happen only on crashes |
1824 | if ($pl->ob->{_link_pos}) { |
1906 | if ($pl->ob->{_link_pos}) { |
1825 | $pl->ob->enter_link; |
1907 | $pl->ob->enter_link; |
1826 | (async { |
1908 | (async { |
1827 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1828 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1829 | $pl->ob->reply (undef, |
1909 | $pl->ob->reply (undef, |
1830 | "There was an internal problem at your last logout, " |
1910 | "There was an internal problem at your last logout, " |
1831 | . "the server will try to bring you to your intended destination in a second.", |
1911 | . "the server will try to bring you to your intended destination in a second.", |
1832 | cf::NDI_RED); |
1912 | cf::NDI_RED); |
|
|
1913 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1914 | # and this sleep increases chances of the player not ending up in scorn |
1833 | Coro::Timer::sleep 1; |
1915 | Coro::Timer::sleep 1; |
1834 | $pl->ob->leave_link; |
1916 | $pl->ob->leave_link; |
1835 | })->prio (2); |
1917 | })->prio (2); |
1836 | } |
1918 | } |
1837 | }, |
1919 | }, |
… | |
… | |
1920 | |
2002 | |
1921 | return unless $self->type == cf::PLAYER; |
2003 | return unless $self->type == cf::PLAYER; |
1922 | |
2004 | |
1923 | if ($exit->slaying eq "/!") { |
2005 | if ($exit->slaying eq "/!") { |
1924 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
2006 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
2007 | # the problem is that $exit might not survive long enough |
|
|
2008 | # so it needs to be done right now, right here |
1925 | cf::sync_job { prepare_random_map $exit }; |
2009 | cf::sync_job { prepare_random_map $exit }; |
1926 | } |
2010 | } |
1927 | |
2011 | |
1928 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2012 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1929 | my $hp = $exit->stats->hp; |
2013 | my $hp = $exit->stats->hp; |
… | |
… | |
1937 | $self->goto ($slaying, $hp, $sp); |
2021 | $self->goto ($slaying, $hp, $sp); |
1938 | |
2022 | |
1939 | 1; |
2023 | 1; |
1940 | }) { |
2024 | }) { |
1941 | $self->message ("Something went wrong deep within the crossfire server. " |
2025 | $self->message ("Something went wrong deep within the crossfire server. " |
1942 | . "I'll try to bring you back to the map you were before. " |
2026 | . "I'll try to bring you back to the map you were before. " |
1943 | . "Please report this to the dungeon master!", |
2027 | . "Please report this to the dungeon master!", |
1944 | cf::NDI_UNIQUE | cf::NDI_RED); |
2028 | cf::NDI_UNIQUE | cf::NDI_RED); |
1945 | |
2029 | |
1946 | warn "ERROR in enter_exit: $@"; |
2030 | warn "ERROR in enter_exit: $@"; |
1947 | $self->leave_link; |
2031 | $self->leave_link; |
1948 | } |
2032 | } |
1949 | })->prio (1); |
2033 | })->prio (1); |
… | |
… | |
1962 | |
2046 | |
1963 | sub cf::client::send_drawinfo { |
2047 | sub cf::client::send_drawinfo { |
1964 | my ($self, $text, $flags) = @_; |
2048 | my ($self, $text, $flags) = @_; |
1965 | |
2049 | |
1966 | utf8::encode $text; |
2050 | utf8::encode $text; |
1967 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
2051 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1968 | } |
2052 | } |
1969 | |
2053 | |
|
|
2054 | =item $client->ext_event ($type, %msg) |
|
|
2055 | |
|
|
2056 | Sends an exti event to the client. |
|
|
2057 | |
|
|
2058 | =cut |
|
|
2059 | |
|
|
2060 | sub cf::client::ext_event($$%) { |
|
|
2061 | my ($self, $type, %msg) = @_; |
|
|
2062 | |
|
|
2063 | $msg{msgtype} = "event_$type"; |
|
|
2064 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2065 | } |
1970 | |
2066 | |
1971 | =item $success = $client->query ($flags, "text", \&cb) |
2067 | =item $success = $client->query ($flags, "text", \&cb) |
1972 | |
2068 | |
1973 | Queues a query to the client, calling the given callback with |
2069 | Queues a query to the client, calling the given callback with |
1974 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2070 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2071 | |
2167 | |
2072 | # here we export the classes and methods available to script code |
2168 | # here we export the classes and methods available to script code |
2073 | |
2169 | |
2074 | =pod |
2170 | =pod |
2075 | |
2171 | |
2076 | The following fucntions and emthods are available within a safe environment: |
2172 | The following functions and methods are available within a safe environment: |
2077 | |
2173 | |
2078 | cf::object contr pay_amount pay_player map |
2174 | cf::object contr pay_amount pay_player map |
2079 | cf::object::player player |
2175 | cf::object::player player |
2080 | cf::player peaceful |
2176 | cf::player peaceful |
2081 | cf::map trigger |
2177 | cf::map trigger |
… | |
… | |
2174 | The parameter C<$family> should best start with the name of the extension |
2270 | The parameter C<$family> should best start with the name of the extension |
2175 | using it, it should be unique. |
2271 | using it, it should be unique. |
2176 | |
2272 | |
2177 | =over 4 |
2273 | =over 4 |
2178 | |
2274 | |
2179 | =item $hashref = cf::db_get $family |
|
|
2180 | |
|
|
2181 | Return a hashref for use by the extension C<$family>, which can be |
|
|
2182 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
2183 | C<cf::db_sync>. |
|
|
2184 | |
|
|
2185 | =item $value = cf::db_get $family => $key |
2275 | =item $value = cf::db_get $family => $key |
2186 | |
2276 | |
2187 | Returns a single value from the database |
2277 | Returns a single value from the database. |
2188 | |
|
|
2189 | =item cf::db_put $family => $hashref |
|
|
2190 | |
|
|
2191 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
2192 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2193 | |
2278 | |
2194 | =item cf::db_put $family => $key => $value |
2279 | =item cf::db_put $family => $key => $value |
2195 | |
2280 | |
2196 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
2281 | Stores the given C<$value> in the family. |
2197 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2198 | |
|
|
2199 | =item cf::db_dirty |
|
|
2200 | |
|
|
2201 | Marks the database as dirty, to be updated at a later time. |
|
|
2202 | |
|
|
2203 | =item cf::db_sync |
|
|
2204 | |
|
|
2205 | Immediately write the database to disk I<if it is dirty>. |
|
|
2206 | |
2282 | |
2207 | =cut |
2283 | =cut |
2208 | |
2284 | |
2209 | our $DB; |
2285 | our $DB; |
2210 | |
2286 | |
2211 | { |
2287 | sub db_init { |
|
|
2288 | unless ($DB) { |
|
|
2289 | $DB = BDB::db_create $DB_ENV; |
|
|
2290 | |
|
|
2291 | cf::sync_job { |
|
|
2292 | eval { |
|
|
2293 | $DB->set_flags (BDB::CHKSUM); |
|
|
2294 | |
|
|
2295 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
2296 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
2297 | cf::cleanup "db_open(db): $!" if $!; |
|
|
2298 | }; |
|
|
2299 | cf::cleanup "db_open(db): $@" if $@; |
|
|
2300 | }; |
|
|
2301 | |
2212 | my $path = cf::localdir . "/database.pst"; |
2302 | my $path = cf::localdir . "/database.pst"; |
|
|
2303 | if (stat $path) { |
|
|
2304 | cf::sync_job { |
|
|
2305 | my $pst = Storable::retrieve $path; |
2213 | |
2306 | |
2214 | sub db_load() { |
2307 | cf::db_put (board => data => $pst->{board}); |
2215 | $DB = stat $path ? Storable::retrieve $path : { }; |
2308 | cf::db_put (guildrules => data => $pst->{guildrules}); |
2216 | } |
2309 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2310 | BDB::db_env_txn_checkpoint $DB_ENV; |
2217 | |
2311 | |
2218 | my $pid; |
2312 | unlink $path; |
2219 | |
|
|
2220 | sub db_save() { |
|
|
2221 | waitpid $pid, 0 if $pid; |
|
|
2222 | if (0 == ($pid = fork)) { |
|
|
2223 | $DB->{_meta}{version} = 1; |
|
|
2224 | Storable::nstore $DB, "$path~"; |
|
|
2225 | rename "$path~", $path; |
|
|
2226 | cf::_exit 0 if defined $pid; |
|
|
2227 | } |
2313 | }; |
2228 | } |
|
|
2229 | |
|
|
2230 | my $dirty; |
|
|
2231 | |
|
|
2232 | sub db_sync() { |
|
|
2233 | db_save if $dirty; |
|
|
2234 | undef $dirty; |
|
|
2235 | } |
|
|
2236 | |
|
|
2237 | my $idle = Event->idle ( |
|
|
2238 | reentrant => 0, |
|
|
2239 | min => 10, |
|
|
2240 | max => 20, |
|
|
2241 | repeat => 0, |
|
|
2242 | data => WF_AUTOCANCEL, |
|
|
2243 | cb => \&db_sync, |
|
|
2244 | ); |
|
|
2245 | |
|
|
2246 | sub db_dirty() { |
|
|
2247 | $dirty = 1; |
|
|
2248 | $idle->start; |
|
|
2249 | } |
|
|
2250 | |
|
|
2251 | sub db_get($;$) { |
|
|
2252 | @_ >= 2 |
|
|
2253 | ? $DB->{$_[0]}{$_[1]} |
|
|
2254 | : ($DB->{$_[0]} ||= { }) |
|
|
2255 | } |
|
|
2256 | |
|
|
2257 | sub db_put($$;$) { |
|
|
2258 | if (@_ >= 3) { |
|
|
2259 | $DB->{$_[0]}{$_[1]} = $_[2]; |
|
|
2260 | } else { |
|
|
2261 | $DB->{$_[0]} = $_[1]; |
|
|
2262 | } |
|
|
2263 | db_dirty; |
|
|
2264 | } |
|
|
2265 | |
|
|
2266 | cf::global->attach ( |
|
|
2267 | prio => 10000, |
|
|
2268 | on_cleanup => sub { |
|
|
2269 | db_sync; |
|
|
2270 | }, |
2314 | } |
2271 | ); |
2315 | } |
|
|
2316 | } |
|
|
2317 | |
|
|
2318 | sub db_get($$) { |
|
|
2319 | my $key = "$_[0]/$_[1]"; |
|
|
2320 | |
|
|
2321 | cf::sync_job { |
|
|
2322 | BDB::db_get $DB, undef, $key, my $data; |
|
|
2323 | |
|
|
2324 | $! ? () |
|
|
2325 | : Compress::LZF::sthaw $data |
|
|
2326 | } |
|
|
2327 | } |
|
|
2328 | |
|
|
2329 | sub db_put($$$) { |
|
|
2330 | BDB::dbreq_pri 4; |
|
|
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2272 | } |
2332 | } |
2273 | |
2333 | |
2274 | ############################################################################# |
2334 | ############################################################################# |
2275 | # the server's init and main functions |
2335 | # the server's init and main functions |
2276 | |
2336 | |
|
|
2337 | sub load_facedata { |
|
|
2338 | my $path = sprintf "%s/facedata", cf::datadir; |
|
|
2339 | |
|
|
2340 | warn "loading facedata from $path\n"; |
|
|
2341 | |
|
|
2342 | my $facedata; |
|
|
2343 | 0 < aio_load $path, $facedata |
|
|
2344 | or die "$path: $!"; |
|
|
2345 | |
|
|
2346 | $facedata = Coro::Storable::thaw $facedata; |
|
|
2347 | |
|
|
2348 | $facedata->{version} == 2 |
|
|
2349 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
|
|
2350 | |
|
|
2351 | { |
|
|
2352 | my $faces = $facedata->{faceinfo}; |
|
|
2353 | |
|
|
2354 | while (my ($face, $info) = each %$faces) { |
|
|
2355 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
2356 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
|
|
2357 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2358 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
|
|
2359 | Coro::cede; |
|
|
2360 | } |
|
|
2361 | |
|
|
2362 | while (my ($face, $info) = each %$faces) { |
|
|
2363 | next unless $info->{smooth}; |
|
|
2364 | my $idx = cf::face::find $face |
|
|
2365 | or next; |
|
|
2366 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2367 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
|
|
2368 | } else { |
|
|
2369 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
|
|
2370 | } |
|
|
2371 | Coro::cede; |
|
|
2372 | } |
|
|
2373 | } |
|
|
2374 | |
|
|
2375 | { |
|
|
2376 | my $anims = $facedata->{animinfo}; |
|
|
2377 | |
|
|
2378 | while (my ($anim, $info) = each %$anims) { |
|
|
2379 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2380 | Coro::cede; |
|
|
2381 | } |
|
|
2382 | |
|
|
2383 | cf::anim::invalidate_all; # d'oh |
|
|
2384 | } |
|
|
2385 | |
|
|
2386 | 1 |
|
|
2387 | } |
|
|
2388 | |
|
|
2389 | sub load_archetypes { |
|
|
2390 | load_archetype_file sprintf "%s/archetypes", cf::datadir; # remove when stable |
|
|
2391 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | sub load_treasures { |
|
|
2395 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2396 | } |
|
|
2397 | |
2277 | sub load_resources { |
2398 | sub reload_resources { |
|
|
2399 | warn "reloading resource files...\n"; |
|
|
2400 | |
2278 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2401 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2279 | or die "unable to load regions file\n";#d# |
2402 | or die "unable to load regions file\n"; |
|
|
2403 | load_facedata |
|
|
2404 | or die "unable to load facedata\n"; |
|
|
2405 | load_archetypes |
|
|
2406 | or die "unable to load archetypes\n"; |
|
|
2407 | load_treasures |
|
|
2408 | or die "unable to load treasurelists\n"; |
|
|
2409 | |
|
|
2410 | warn "finished reloading resource files\n"; |
|
|
2411 | } |
|
|
2412 | |
|
|
2413 | sub init { |
|
|
2414 | reload_resources; |
2280 | } |
2415 | } |
2281 | |
2416 | |
2282 | sub cfg_load { |
2417 | sub cfg_load { |
2283 | open my $fh, "<:utf8", cf::confdir . "/config" |
2418 | open my $fh, "<:utf8", cf::confdir . "/config" |
2284 | or return; |
2419 | or return; |
… | |
… | |
2296 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2431 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2297 | and die "WARNING: m(un)lockall failed: $!\n"; |
2432 | and die "WARNING: m(un)lockall failed: $!\n"; |
2298 | }; |
2433 | }; |
2299 | warn $@ if $@; |
2434 | warn $@ if $@; |
2300 | } |
2435 | } |
2301 | } |
|
|
2302 | |
|
|
2303 | sub init { |
|
|
2304 | load_resources; |
|
|
2305 | } |
2436 | } |
2306 | |
2437 | |
2307 | sub main { |
2438 | sub main { |
2308 | # we must not ever block the main coroutine |
2439 | # we must not ever block the main coroutine |
2309 | local $Coro::idle = sub { |
2440 | local $Coro::idle = sub { |
… | |
… | |
2312 | Event::one_event; |
2443 | Event::one_event; |
2313 | })->prio (Coro::PRIO_MAX); |
2444 | })->prio (Coro::PRIO_MAX); |
2314 | }; |
2445 | }; |
2315 | |
2446 | |
2316 | cfg_load; |
2447 | cfg_load; |
2317 | db_load; |
2448 | db_init; |
2318 | load_extensions; |
2449 | load_extensions; |
2319 | |
2450 | |
2320 | $TICK_WATCHER->start; |
2451 | $TICK_WATCHER->start; |
2321 | Event::loop; |
2452 | Event::loop; |
2322 | } |
2453 | } |
… | |
… | |
2361 | my $map = $cf::MAP{$path} or next; |
2492 | my $map = $cf::MAP{$path} or next; |
2362 | $map->valid or next; |
2493 | $map->valid or next; |
2363 | $map->save; |
2494 | $map->save; |
2364 | } |
2495 | } |
2365 | warn "end emergency map save\n"; |
2496 | warn "end emergency map save\n"; |
|
|
2497 | |
|
|
2498 | warn "begin emergency database checkpoint\n"; |
|
|
2499 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2500 | warn "end emergency database checkpoint\n"; |
2366 | }; |
2501 | }; |
2367 | |
2502 | |
2368 | warn "leave emergency perl save\n"; |
2503 | warn "leave emergency perl save\n"; |
|
|
2504 | } |
|
|
2505 | |
|
|
2506 | sub post_cleanup { |
|
|
2507 | my ($make_core) = @_; |
|
|
2508 | |
|
|
2509 | warn Carp::longmess "post_cleanup backtrace" |
|
|
2510 | if $make_core; |
2369 | } |
2511 | } |
2370 | |
2512 | |
2371 | sub reload() { |
2513 | sub reload() { |
2372 | # can/must only be called in main |
2514 | # can/must only be called in main |
2373 | if ($Coro::current != $Coro::main) { |
2515 | if ($Coro::current != $Coro::main) { |
… | |
… | |
2375 | return; |
2517 | return; |
2376 | } |
2518 | } |
2377 | |
2519 | |
2378 | warn "reloading..."; |
2520 | warn "reloading..."; |
2379 | |
2521 | |
2380 | warn "cancelling server ticker"; |
2522 | warn "entering sync_job"; |
2381 | $TICK_WATCHER->cancel; |
|
|
2382 | |
2523 | |
|
|
2524 | cf::sync_job { |
|
|
2525 | cf::write_runtime; # external watchdog should not bark |
2383 | cf::emergency_save; |
2526 | cf::emergency_save; |
|
|
2527 | cf::write_runtime; # external watchdog should not bark |
2384 | |
2528 | |
2385 | eval { |
2529 | warn "syncing database to disk"; |
|
|
2530 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2531 | |
2386 | # if anything goes wrong in here, we should simply crash as we already saved |
2532 | # if anything goes wrong in here, we should simply crash as we already saved |
2387 | |
|
|
2388 | warn "syncing database to disk"; |
|
|
2389 | cf::db_sync; |
|
|
2390 | |
2533 | |
2391 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2534 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2392 | for (Event::all_watchers) { |
2535 | for (Event::all_watchers) { |
2393 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2536 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2394 | } |
2537 | } |
2395 | |
2538 | |
2396 | warn "flushing outstanding aio requests"; |
2539 | warn "flushing outstanding aio requests"; |
2397 | for (;;) { |
2540 | for (;;) { |
|
|
2541 | BDB::flush; |
2398 | IO::AIO::flush; |
2542 | IO::AIO::flush; |
2399 | Coro::cede; |
2543 | Coro::cede; |
2400 | last unless IO::AIO::nreqs; |
2544 | last unless IO::AIO::nreqs || BDB::nreqs; |
2401 | warn "iterate..."; |
2545 | warn "iterate..."; |
2402 | } |
2546 | } |
|
|
2547 | |
|
|
2548 | ++$RELOAD; |
2403 | |
2549 | |
2404 | warn "cancelling all extension coros"; |
2550 | warn "cancelling all extension coros"; |
2405 | $_->cancel for values %EXT_CORO; |
2551 | $_->cancel for values %EXT_CORO; |
2406 | %EXT_CORO = (); |
2552 | %EXT_CORO = (); |
2407 | |
2553 | |
… | |
… | |
2460 | require cf; |
2606 | require cf; |
2461 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2607 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2462 | |
2608 | |
2463 | warn "loading config and database again"; |
2609 | warn "loading config and database again"; |
2464 | cf::cfg_load; |
2610 | cf::cfg_load; |
2465 | cf::db_load; |
|
|
2466 | |
2611 | |
2467 | warn "loading extensions"; |
2612 | warn "loading extensions"; |
2468 | cf::load_extensions; |
2613 | cf::load_extensions; |
2469 | |
2614 | |
2470 | warn "reattaching attachments to objects/players"; |
2615 | warn "reattaching attachments to objects/players"; |
2471 | _global_reattach; |
2616 | _global_reattach; # objects, sockets |
2472 | warn "reattaching attachments to maps"; |
2617 | warn "reattaching attachments to maps"; |
2473 | reattach $_ for values %MAP; |
2618 | reattach $_ for values %MAP; |
|
|
2619 | warn "reattaching attachments to players"; |
|
|
2620 | reattach $_ for values %PLAYER; |
2474 | |
2621 | |
2475 | warn "loading reloadable resources"; |
2622 | warn "loading reloadable resources"; |
2476 | load_resources; |
2623 | reload_resources; |
2477 | |
2624 | |
2478 | warn "restarting server ticker"; |
2625 | warn "leaving sync_job"; |
2479 | |
2626 | |
2480 | $TICK_WATCHER->start; |
2627 | 1 |
2481 | }; |
2628 | } or do { |
2482 | |
|
|
2483 | if ($@) { |
|
|
2484 | warn $@; |
2629 | warn $@; |
2485 | warn "error while reloading, exiting."; |
2630 | warn "error while reloading, exiting."; |
2486 | exit 1; |
2631 | exit 1; |
2487 | } |
2632 | }; |
2488 | |
2633 | |
2489 | warn "reloaded"; |
2634 | warn "reloaded"; |
2490 | }; |
2635 | }; |
2491 | |
2636 | |
2492 | our $RELOAD_WATCHER; # used only during reload |
2637 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2513 | }; |
2658 | }; |
2514 | |
2659 | |
2515 | unshift @INC, $LIBDIR; |
2660 | unshift @INC, $LIBDIR; |
2516 | |
2661 | |
2517 | my $bug_warning = 0; |
2662 | my $bug_warning = 0; |
|
|
2663 | |
|
|
2664 | our @WAIT_FOR_TICK; |
|
|
2665 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2666 | |
|
|
2667 | sub wait_for_tick { |
|
|
2668 | return unless $TICK_WATCHER->is_active; |
|
|
2669 | return if $Coro::current == $Coro::main; |
|
|
2670 | |
|
|
2671 | my $signal = new Coro::Signal; |
|
|
2672 | push @WAIT_FOR_TICK, $signal; |
|
|
2673 | $signal->wait; |
|
|
2674 | } |
|
|
2675 | |
|
|
2676 | sub wait_for_tick_begin { |
|
|
2677 | return unless $TICK_WATCHER->is_active; |
|
|
2678 | return if $Coro::current == $Coro::main; |
|
|
2679 | |
|
|
2680 | my $signal = new Coro::Signal; |
|
|
2681 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2682 | $signal->wait; |
|
|
2683 | } |
2518 | |
2684 | |
2519 | $TICK_WATCHER = Event->timer ( |
2685 | $TICK_WATCHER = Event->timer ( |
2520 | reentrant => 0, |
2686 | reentrant => 0, |
2521 | parked => 1, |
2687 | parked => 1, |
2522 | prio => 0, |
2688 | prio => 0, |
… | |
… | |
2530 | } |
2696 | } |
2531 | |
2697 | |
2532 | $NOW = Event::time; |
2698 | $NOW = Event::time; |
2533 | |
2699 | |
2534 | cf::server_tick; # one server iteration |
2700 | cf::server_tick; # one server iteration |
|
|
2701 | |
2535 | $RUNTIME += $TICK; |
2702 | $RUNTIME += $TICK; |
2536 | $NEXT_TICK += $TICK; |
2703 | $NEXT_TICK += $TICK; |
2537 | |
2704 | |
2538 | $WAIT_FOR_TICK->broadcast; |
2705 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2539 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2706 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2540 | |
2707 | Coro::async_pool { |
2541 | Event::sweep; |
2708 | write_runtime |
2542 | Coro::cede_notself; |
2709 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2710 | }; |
2543 | |
2711 | } |
|
|
2712 | |
2544 | # my $AFTER = Event::time; |
2713 | # my $AFTER = Event::time; |
2545 | # warn $AFTER - $NOW;#d# |
2714 | # warn $AFTER - $NOW;#d# |
2546 | |
2715 | |
2547 | # if we are delayed by four ticks or more, skip them all |
2716 | # if we are delayed by four ticks or more, skip them all |
2548 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2717 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2549 | |
2718 | |
2550 | $TICK_WATCHER->at ($NEXT_TICK); |
2719 | $TICK_WATCHER->at ($NEXT_TICK); |
2551 | $TICK_WATCHER->start; |
2720 | $TICK_WATCHER->start; |
|
|
2721 | |
|
|
2722 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2723 | $sig->send; |
|
|
2724 | } |
|
|
2725 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2726 | $sig->send; |
|
|
2727 | } |
|
|
2728 | |
|
|
2729 | _post_tick; |
2552 | }, |
2730 | }, |
2553 | ); |
2731 | ); |
2554 | |
2732 | |
|
|
2733 | { |
|
|
2734 | BDB::max_poll_time $TICK * 0.1; |
|
|
2735 | $BDB_POLL_WATCHER = Event->io ( |
|
|
2736 | reentrant => 0, |
|
|
2737 | fd => BDB::poll_fileno, |
|
|
2738 | poll => 'r', |
|
|
2739 | prio => 0, |
|
|
2740 | data => WF_AUTOCANCEL, |
|
|
2741 | cb => \&BDB::poll_cb, |
|
|
2742 | ); |
|
|
2743 | BDB::min_parallel 8; |
|
|
2744 | |
|
|
2745 | BDB::set_sync_prepare { |
|
|
2746 | my $status; |
|
|
2747 | my $current = $Coro::current; |
|
|
2748 | ( |
|
|
2749 | sub { |
|
|
2750 | $status = $!; |
|
|
2751 | $current->ready; undef $current; |
|
|
2752 | }, |
|
|
2753 | sub { |
|
|
2754 | Coro::schedule while defined $current; |
|
|
2755 | $! = $status; |
|
|
2756 | }, |
|
|
2757 | ) |
|
|
2758 | }; |
|
|
2759 | |
|
|
2760 | unless ($DB_ENV) { |
|
|
2761 | $DB_ENV = BDB::db_env_create; |
|
|
2762 | |
|
|
2763 | cf::sync_job { |
|
|
2764 | eval { |
|
|
2765 | BDB::db_env_open |
|
|
2766 | $DB_ENV, |
|
|
2767 | $BDB_ENV_DIR, |
|
|
2768 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
|
|
2769 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
|
|
2770 | 0666; |
|
|
2771 | |
|
|
2772 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
|
|
2773 | |
|
|
2774 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
|
|
2775 | $DB_ENV->set_lk_detect; |
|
|
2776 | }; |
|
|
2777 | |
|
|
2778 | cf::cleanup "db_env_open(db): $@" if $@; |
|
|
2779 | }; |
|
|
2780 | } |
|
|
2781 | } |
|
|
2782 | |
|
|
2783 | { |
|
|
2784 | IO::AIO::min_parallel 8; |
|
|
2785 | |
|
|
2786 | undef $Coro::AIO::WATCHER; |
2555 | IO::AIO::max_poll_time $TICK * 0.1; |
2787 | IO::AIO::max_poll_time $TICK * 0.1; |
2556 | |
|
|
2557 | undef $Coro::AIO::WATCHER; |
|
|
2558 | $AIO_POLL_WATCHER = Event->io ( |
2788 | $AIO_POLL_WATCHER = Event->io ( |
2559 | reentrant => 0, |
2789 | reentrant => 0, |
|
|
2790 | data => WF_AUTOCANCEL, |
2560 | fd => IO::AIO::poll_fileno, |
2791 | fd => IO::AIO::poll_fileno, |
2561 | poll => 'r', |
2792 | poll => 'r', |
2562 | prio => 6, |
2793 | prio => 6, |
2563 | data => WF_AUTOCANCEL, |
|
|
2564 | cb => \&IO::AIO::poll_cb, |
2794 | cb => \&IO::AIO::poll_cb, |
2565 | ); |
2795 | ); |
2566 | |
2796 | } |
2567 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2568 | reentrant => 0, |
|
|
2569 | data => WF_AUTOCANCEL, |
|
|
2570 | after => 1, |
|
|
2571 | interval => 10, |
|
|
2572 | prio => 6, # keep it lowest so it acts like a watchdog |
|
|
2573 | cb => Coro::unblock_sub { |
|
|
2574 | write_runtime |
|
|
2575 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2576 | }, |
|
|
2577 | ); |
|
|
2578 | |
2797 | |
2579 | END { cf::emergency_save } |
2798 | END { cf::emergency_save } |
2580 | |
2799 | |
2581 | 1 |
2800 | 1 |
2582 | |
2801 | |