… | |
… | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
|
|
8 | use Socket; |
8 | use Storable; |
9 | use Storable; |
|
|
10 | use Event; |
9 | use Opcode; |
11 | use Opcode; |
10 | use Safe; |
12 | use Safe; |
11 | use Safe::Hole; |
13 | use Safe::Hole; |
12 | |
14 | |
13 | use Coro 3.5 (); |
15 | use Coro 3.61 (); |
|
|
16 | use Coro::State; |
|
|
17 | use Coro::Handle; |
14 | use Coro::Event; |
18 | use Coro::Event; |
15 | use Coro::Timer; |
19 | use Coro::Timer; |
16 | use Coro::Signal; |
20 | use Coro::Signal; |
17 | use Coro::Semaphore; |
21 | use Coro::Semaphore; |
18 | use Coro::AIO; |
22 | use Coro::AIO; |
|
|
23 | use Coro::Storable; |
19 | |
24 | |
20 | use BDB (); |
25 | use BDB (); |
21 | use Data::Dumper; |
26 | use Data::Dumper; |
22 | use Digest::MD5; |
27 | use Digest::MD5; |
23 | use Fcntl; |
28 | use Fcntl; |
|
|
29 | use YAML::Syck (); |
24 | use IO::AIO 2.32 (); |
30 | use IO::AIO 2.32 (); |
25 | use YAML::Syck (); |
|
|
26 | use Time::HiRes; |
31 | use Time::HiRes; |
27 | use Compress::LZF; |
32 | use Compress::LZF; |
28 | |
33 | |
|
|
34 | # configure various modules to our taste |
|
|
35 | # |
|
|
36 | $Storable::canonical = 1; # reduce rsync transfers |
|
|
37 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
29 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
38 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
30 | |
39 | |
31 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
40 | $Event::Eval = 1; # no idea why this is required, but it is |
32 | |
|
|
33 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
34 | |
41 | |
35 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
42 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
36 | $YAML::Syck::ImplicitUnicode = 1; |
43 | $YAML::Syck::ImplicitUnicode = 1; |
37 | |
44 | |
38 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
45 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
46 | |
|
|
47 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
39 | |
48 | |
40 | our %COMMAND = (); |
49 | our %COMMAND = (); |
41 | our %COMMAND_TIME = (); |
50 | our %COMMAND_TIME = (); |
42 | |
51 | |
43 | our @EXTS = (); # list of extension package names |
52 | our @EXTS = (); # list of extension package names |
44 | our %EXTCMD = (); |
53 | our %EXTCMD = (); |
45 | our %EXT_CORO = (); # coroutines bound to extensions |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
46 | our %EXT_MAP = (); # pluggable maps |
55 | our %EXT_MAP = (); # pluggable maps |
47 | |
56 | |
|
|
57 | our $RELOAD; # number of reloads so far |
48 | our @EVENT; |
58 | our @EVENT; |
49 | our $LIBDIR = datadir . "/ext"; |
|
|
50 | |
59 | |
51 | our $TICK = MAX_TIME * 1e-6; |
60 | our $CONFDIR = confdir; |
|
|
61 | our $DATADIR = datadir; |
|
|
62 | our $LIBDIR = "$DATADIR/ext"; |
|
|
63 | our $PODDIR = "$DATADIR/pod"; |
|
|
64 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
65 | our $LOCALDIR = localdir; |
|
|
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
|
|
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
|
|
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
|
|
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
|
|
70 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
71 | |
|
|
72 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
52 | our $TICK_WATCHER; |
73 | our $TICK_WATCHER; |
53 | our $AIO_POLL_WATCHER; |
74 | our $AIO_POLL_WATCHER; |
54 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
75 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
55 | our $NEXT_TICK; |
76 | our $NEXT_TICK; |
56 | our $NOW; |
77 | our $NOW; |
… | |
… | |
65 | our $RUNTIME; |
86 | our $RUNTIME; |
66 | |
87 | |
67 | our %PLAYER; # all users |
88 | our %PLAYER; # all users |
68 | our %MAP; # all maps |
89 | our %MAP; # all maps |
69 | our $LINK_MAP; # the special {link} map, which is always available |
90 | our $LINK_MAP; # the special {link} map, which is always available |
70 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
71 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
|
|
72 | |
|
|
73 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
74 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
|
|
75 | |
91 | |
76 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
92 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
77 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
93 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
78 | |
94 | |
79 | binmode STDOUT; |
95 | binmode STDOUT; |
80 | binmode STDERR; |
96 | binmode STDERR; |
81 | |
97 | |
82 | # read virtual server time, if available |
98 | # read virtual server time, if available |
83 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
99 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
84 | open my $fh, "<", cf::localdir . "/runtime" |
100 | open my $fh, "<", "$LOCALDIR/runtime" |
85 | or die "unable to read runtime file: $!"; |
101 | or die "unable to read runtime file: $!"; |
86 | $RUNTIME = <$fh> + 0.; |
102 | $RUNTIME = <$fh> + 0.; |
87 | } |
103 | } |
88 | |
104 | |
89 | mkdir cf::localdir; |
105 | mkdir $_ |
90 | mkdir cf::localdir . "/" . cf::playerdir; |
106 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
91 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
92 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
93 | mkdir $RANDOM_MAPS; |
|
|
94 | mkdir $BDB_ENV_DIR; |
|
|
95 | |
107 | |
96 | our $EMERGENCY_POSITION; |
108 | our $EMERGENCY_POSITION; |
97 | |
109 | |
98 | sub cf::map::normalise; |
110 | sub cf::map::normalise; |
99 | |
111 | |
… | |
… | |
110 | =item $cf::RUNTIME |
122 | =item $cf::RUNTIME |
111 | |
123 | |
112 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
124 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
113 | every server tick. |
125 | every server tick. |
114 | |
126 | |
115 | =item $cf::LIBDIR |
127 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
|
|
128 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
|
|
129 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
116 | |
130 | |
117 | The perl library directory, where extensions and cf-specific modules can |
131 | Various directories - "/etc", read-only install directory, perl-library |
118 | be found. It will be added to C<@INC> automatically. |
132 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
|
|
133 | unique-items directory, player file directory, random maps directory and |
|
|
134 | database environment. |
119 | |
135 | |
120 | =item $cf::NOW |
136 | =item $cf::NOW |
121 | |
137 | |
122 | The time of the last (current) server tick. |
138 | The time of the last (current) server tick. |
123 | |
139 | |
… | |
… | |
128 | =item %cf::CFG |
144 | =item %cf::CFG |
129 | |
145 | |
130 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
146 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
131 | from wherever your confdir points to. |
147 | from wherever your confdir points to. |
132 | |
148 | |
133 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
149 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
134 | |
150 | |
135 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
151 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
136 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
152 | returns directly I<after> the tick processing (and consequently, can only wake one process |
137 | processing has been done. Call C<< ->wait >> on them to maximise the |
153 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
138 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
139 | |
154 | |
140 | =back |
155 | =back |
141 | |
156 | |
142 | =cut |
157 | =cut |
143 | |
158 | |
144 | BEGIN { |
159 | BEGIN { |
145 | *CORE::GLOBAL::warn = sub { |
160 | *CORE::GLOBAL::warn = sub { |
146 | my $msg = join "", @_; |
161 | my $msg = join "", @_; |
147 | utf8::encode $msg; |
|
|
148 | |
162 | |
149 | $msg .= "\n" |
163 | $msg .= "\n" |
150 | unless $msg =~ /\n$/; |
164 | unless $msg =~ /\n$/; |
151 | |
165 | |
|
|
166 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
|
|
167 | |
|
|
168 | utf8::encode $msg; |
152 | LOG llevError, $msg; |
169 | LOG llevError, $msg; |
153 | }; |
170 | }; |
154 | } |
171 | } |
155 | |
172 | |
156 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
173 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
202 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
219 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
203 | $d |
220 | $d |
204 | } || "[unable to dump $_[0]: '$@']"; |
221 | } || "[unable to dump $_[0]: '$@']"; |
205 | } |
222 | } |
206 | |
223 | |
207 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
224 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
208 | |
225 | |
209 | =item $ref = cf::from_json $json |
226 | =item $ref = cf::from_json $json |
210 | |
227 | |
211 | Converts a JSON string into the corresponding perl data structure. |
228 | Converts a JSON string into the corresponding perl data structure. |
212 | |
229 | |
213 | =cut |
|
|
214 | |
|
|
215 | sub from_json($) { |
|
|
216 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
217 | JSON::Syck::Load $_[0] |
|
|
218 | } |
|
|
219 | |
|
|
220 | =item $json = cf::to_json $ref |
230 | =item $json = cf::to_json $ref |
221 | |
231 | |
222 | Converts a perl data structure into its JSON representation. |
232 | Converts a perl data structure into its JSON representation. |
223 | |
|
|
224 | =cut |
|
|
225 | |
|
|
226 | sub to_json($) { |
|
|
227 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
228 | JSON::Syck::Dump $_[0] |
|
|
229 | } |
|
|
230 | |
233 | |
231 | =item cf::lock_wait $string |
234 | =item cf::lock_wait $string |
232 | |
235 | |
233 | Wait until the given lock is available. See cf::lock_acquire. |
236 | Wait until the given lock is available. See cf::lock_acquire. |
234 | |
237 | |
… | |
… | |
240 | returned. |
243 | returned. |
241 | |
244 | |
242 | Lock names should begin with a unique identifier (for example, cf::map::find |
245 | Lock names should begin with a unique identifier (for example, cf::map::find |
243 | uses map_find and cf::map::load uses map_load). |
246 | uses map_find and cf::map::load uses map_load). |
244 | |
247 | |
|
|
248 | =item $locked = cf::lock_active $string |
|
|
249 | |
|
|
250 | Return true if the lock is currently active, i.e. somebody has locked it. |
|
|
251 | |
245 | =cut |
252 | =cut |
246 | |
253 | |
247 | our %LOCK; |
254 | our %LOCK; |
248 | |
255 | |
249 | sub lock_wait($) { |
256 | sub lock_wait($) { |
… | |
… | |
266 | |
273 | |
267 | Coro::guard { |
274 | Coro::guard { |
268 | # wake up all waiters, to be on the safe side |
275 | # wake up all waiters, to be on the safe side |
269 | $_->ready for @{ delete $LOCK{$key} }; |
276 | $_->ready for @{ delete $LOCK{$key} }; |
270 | } |
277 | } |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub lock_active($) { |
|
|
281 | my ($key) = @_; |
|
|
282 | |
|
|
283 | ! ! $LOCK{$key} |
271 | } |
284 | } |
272 | |
285 | |
273 | sub freeze_mainloop { |
286 | sub freeze_mainloop { |
274 | return unless $TICK_WATCHER->is_active; |
287 | return unless $TICK_WATCHER->is_active; |
275 | |
288 | |
… | |
… | |
354 | |
367 | |
355 | $coro |
368 | $coro |
356 | } |
369 | } |
357 | |
370 | |
358 | sub write_runtime { |
371 | sub write_runtime { |
359 | my $runtime = cf::localdir . "/runtime"; |
372 | my $guard = cf::lock_acquire "write_runtime"; |
|
|
373 | |
|
|
374 | my $runtime = "$LOCALDIR/runtime"; |
360 | |
375 | |
361 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
376 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
362 | or return; |
377 | or return; |
363 | |
378 | |
364 | my $value = $cf::RUNTIME + 90 + 10; |
379 | my $value = $cf::RUNTIME + 90 + 10; |
… | |
… | |
377 | |
392 | |
378 | aio_rename "$runtime~", $runtime |
393 | aio_rename "$runtime~", $runtime |
379 | and return; |
394 | and return; |
380 | |
395 | |
381 | 1 |
396 | 1 |
|
|
397 | } |
|
|
398 | |
|
|
399 | =item cf::datalog type => key => value, ... |
|
|
400 | |
|
|
401 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
402 | |
|
|
403 | =cut |
|
|
404 | |
|
|
405 | sub datalog($@) { |
|
|
406 | my ($type, %kv) = @_; |
|
|
407 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
382 | } |
408 | } |
383 | |
409 | |
384 | =back |
410 | =back |
385 | |
411 | |
386 | =cut |
412 | =cut |
… | |
… | |
785 | } |
811 | } |
786 | } else { |
812 | } else { |
787 | aio_unlink $filename; |
813 | aio_unlink $filename; |
788 | aio_unlink "$filename.pst"; |
814 | aio_unlink "$filename.pst"; |
789 | } |
815 | } |
790 | |
|
|
791 | #d##TODO# nuke non .map-files if exist |
|
|
792 | if ($filename =~ s/\.map$//) { |
|
|
793 | aio_unlink $filename; |
|
|
794 | aio_unlink "$filename.pst"; |
|
|
795 | } |
|
|
796 | } |
816 | } |
797 | } |
817 | } |
798 | |
818 | |
799 | sub object_freezer_as_string { |
819 | sub object_freezer_as_string { |
800 | my ($rdata, $objs) = @_; |
820 | my ($rdata, $objs) = @_; |
… | |
… | |
806 | |
826 | |
807 | sub object_thawer_load { |
827 | sub object_thawer_load { |
808 | my ($filename) = @_; |
828 | my ($filename) = @_; |
809 | |
829 | |
810 | my ($data, $av); |
830 | my ($data, $av); |
811 | |
|
|
812 | #d#TODO remove .map if file does not exist |
|
|
813 | aio_stat $filename and $filename =~ s/\.map$//; |
|
|
814 | |
831 | |
815 | (aio_load $filename, $data) >= 0 |
832 | (aio_load $filename, $data) >= 0 |
816 | or return; |
833 | or return; |
817 | |
834 | |
818 | unless (aio_stat "$filename.pst") { |
835 | unless (aio_stat "$filename.pst") { |
… | |
… | |
957 | Returns the given player object, loading it if necessary (might block). |
974 | Returns the given player object, loading it if necessary (might block). |
958 | |
975 | |
959 | =cut |
976 | =cut |
960 | |
977 | |
961 | sub playerdir($) { |
978 | sub playerdir($) { |
962 | cf::localdir |
979 | "$PLAYERDIR/" |
963 | . "/" |
|
|
964 | . cf::playerdir |
|
|
965 | . "/" |
|
|
966 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
980 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
967 | } |
981 | } |
968 | |
982 | |
969 | sub path($) { |
983 | sub path($) { |
970 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
984 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
971 | |
985 | |
972 | (playerdir $login) . "/$login.pl" |
986 | (playerdir $login) . "/playerdata" |
973 | } |
987 | } |
974 | |
988 | |
975 | sub find_active($) { |
989 | sub find_active($) { |
976 | $cf::PLAYER{$_[0]} |
990 | $cf::PLAYER{$_[0]} |
977 | and $cf::PLAYER{$_[0]}->active |
991 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
990 | my $login = $_[0]; |
1004 | my $login = $_[0]; |
991 | |
1005 | |
992 | my $guard = cf::lock_acquire "user_find:$login"; |
1006 | my $guard = cf::lock_acquire "user_find:$login"; |
993 | |
1007 | |
994 | $cf::PLAYER{$_[0]} || do { |
1008 | $cf::PLAYER{$_[0]} || do { |
|
|
1009 | # rename old playerfiles to new ones |
|
|
1010 | #TODO: remove when no longer required |
|
|
1011 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
1012 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
1013 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
1014 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
1015 | |
995 | my $pl = load_pl path $login |
1016 | my $pl = load_pl path $login |
996 | or return; |
1017 | or return; |
997 | $cf::PLAYER{$login} = $pl |
1018 | $cf::PLAYER{$login} = $pl |
998 | } |
1019 | } |
999 | } |
1020 | } |
… | |
… | |
1036 | |
1057 | |
1037 | =cut |
1058 | =cut |
1038 | |
1059 | |
1039 | sub quit_character { |
1060 | sub quit_character { |
1040 | my ($pl) = @_; |
1061 | my ($pl) = @_; |
|
|
1062 | |
|
|
1063 | my $name = $pl->ob->name; |
1041 | |
1064 | |
1042 | $pl->{deny_save} = 1; |
1065 | $pl->{deny_save} = 1; |
1043 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1066 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1044 | |
1067 | |
1045 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1068 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
… | |
… | |
1050 | my $path = playerdir $pl; |
1073 | my $path = playerdir $pl; |
1051 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1074 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1052 | aio_rename $path, $temp; |
1075 | aio_rename $path, $temp; |
1053 | delete $cf::PLAYER{$pl->ob->name}; |
1076 | delete $cf::PLAYER{$pl->ob->name}; |
1054 | $pl->destroy; |
1077 | $pl->destroy; |
|
|
1078 | |
|
|
1079 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1080 | |
|
|
1081 | # nuke player maps |
|
|
1082 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1083 | |
1055 | IO::AIO::aio_rmtree $temp; |
1084 | IO::AIO::aio_rmtree $temp; |
|
|
1085 | } |
|
|
1086 | |
|
|
1087 | =item $pl->kick |
|
|
1088 | |
|
|
1089 | Kicks a player out of the game. This destroys the connection. |
|
|
1090 | |
|
|
1091 | =cut |
|
|
1092 | |
|
|
1093 | sub kick { |
|
|
1094 | my ($pl, $kicker) = @_; |
|
|
1095 | |
|
|
1096 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1097 | $pl->killer ("kicked"); |
|
|
1098 | $pl->ns->destroy; |
1056 | } |
1099 | } |
1057 | |
1100 | |
1058 | =item cf::player::list_logins |
1101 | =item cf::player::list_logins |
1059 | |
1102 | |
1060 | Returns am arrayref of all valid playernames in the system, can take a |
1103 | Returns am arrayref of all valid playernames in the system, can take a |
1061 | while and may block, so not sync_job-capable, ever. |
1104 | while and may block, so not sync_job-capable, ever. |
1062 | |
1105 | |
1063 | =cut |
1106 | =cut |
1064 | |
1107 | |
1065 | sub list_logins { |
1108 | sub list_logins { |
1066 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
1109 | my $dirs = aio_readdir $PLAYERDIR |
1067 | or return []; |
1110 | or return []; |
1068 | |
1111 | |
1069 | my @logins; |
1112 | my @logins; |
1070 | |
1113 | |
1071 | for my $login (@$dirs) { |
1114 | for my $login (@$dirs) { |
… | |
… | |
1106 | } |
1149 | } |
1107 | |
1150 | |
1108 | \@paths |
1151 | \@paths |
1109 | } |
1152 | } |
1110 | |
1153 | |
1111 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1154 | =item $player->ext_reply ($msgid, %msg) |
1112 | |
1155 | |
1113 | Sends an ext reply to the player. |
1156 | Sends an ext reply to the player. |
1114 | |
1157 | |
1115 | =cut |
1158 | =cut |
1116 | |
1159 | |
1117 | sub ext_reply($$$%) { |
1160 | sub ext_reply($$%) { |
1118 | my ($self, $id, %msg) = @_; |
1161 | my ($self, $id, %msg) = @_; |
1119 | |
1162 | |
1120 | $msg{msgid} = $id; |
1163 | $msg{msgid} = $id; |
1121 | |
1164 | |
1122 | $self->send ("ext " . cf::to_json \%msg); |
1165 | $self->send ("ext " . cf::to_json \%msg); |
1123 | } |
1166 | } |
1124 | |
1167 | |
1125 | package cf; |
1168 | =item $player->ext_event ($type, %msg) |
|
|
1169 | |
|
|
1170 | Sends an ext event to the client. |
|
|
1171 | |
|
|
1172 | =cut |
|
|
1173 | |
|
|
1174 | sub ext_event($$%) { |
|
|
1175 | my ($self, $type, %msg) = @_; |
|
|
1176 | |
|
|
1177 | $self->ns->ext_event ($type, %msg); |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | =head3 cf::region |
|
|
1181 | |
|
|
1182 | =over 4 |
|
|
1183 | |
|
|
1184 | =cut |
|
|
1185 | |
|
|
1186 | package cf::region; |
|
|
1187 | |
|
|
1188 | =item cf::region::find_by_path $path |
|
|
1189 | |
|
|
1190 | Tries to decuce the probable region for a map knowing only its path. |
|
|
1191 | |
|
|
1192 | =cut |
|
|
1193 | |
|
|
1194 | sub find_by_path($) { |
|
|
1195 | my ($path) = @_; |
|
|
1196 | |
|
|
1197 | my ($match, $specificity); |
|
|
1198 | |
|
|
1199 | for my $region (list) { |
|
|
1200 | if ($region->match && $path =~ $region->match) { |
|
|
1201 | ($match, $specificity) = ($region, $region->specificity) |
|
|
1202 | if $region->specificity > $specificity; |
|
|
1203 | } |
|
|
1204 | } |
|
|
1205 | |
|
|
1206 | $match |
|
|
1207 | } |
1126 | |
1208 | |
1127 | =back |
1209 | =back |
1128 | |
|
|
1129 | |
1210 | |
1130 | =head3 cf::map |
1211 | =head3 cf::map |
1131 | |
1212 | |
1132 | =over 4 |
1213 | =over 4 |
1133 | |
1214 | |
… | |
… | |
1271 | |
1352 | |
1272 | # the original (read-only) location |
1353 | # the original (read-only) location |
1273 | sub load_path { |
1354 | sub load_path { |
1274 | my ($self) = @_; |
1355 | my ($self) = @_; |
1275 | |
1356 | |
1276 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1357 | "$MAPDIR/$self->{path}.map" |
1277 | } |
1358 | } |
1278 | |
1359 | |
1279 | # the temporary/swap location |
1360 | # the temporary/swap location |
1280 | sub save_path { |
1361 | sub save_path { |
1281 | my ($self) = @_; |
1362 | my ($self) = @_; |
1282 | |
1363 | |
1283 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1364 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1284 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1365 | "$TMPDIR/$path.map" |
1285 | } |
1366 | } |
1286 | |
1367 | |
1287 | # the unique path, undef == no special unique path |
1368 | # the unique path, undef == no special unique path |
1288 | sub uniq_path { |
1369 | sub uniq_path { |
1289 | my ($self) = @_; |
1370 | my ($self) = @_; |
1290 | |
1371 | |
1291 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1372 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1292 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1373 | "$UNIQUEDIR/$path" |
1293 | } |
1374 | } |
1294 | |
1375 | |
1295 | # and all this just because we cannot iterate over |
1376 | # and all this just because we cannot iterate over |
1296 | # all maps in C++... |
1377 | # all maps in C++... |
1297 | sub change_all_map_light { |
1378 | sub change_all_map_light { |
… | |
… | |
1305 | my ($self) = @_; |
1386 | my ($self) = @_; |
1306 | |
1387 | |
1307 | utf8::encode (my $save = $self->save_path); |
1388 | utf8::encode (my $save = $self->save_path); |
1308 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1389 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1309 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1390 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1310 | |
|
|
1311 | #d#TODO remove .map and also nuke |
|
|
1312 | $save =~ s/\.map// or return;#d# |
|
|
1313 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1314 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
|
|
1315 | } |
1391 | } |
1316 | |
1392 | |
1317 | sub load_header_from($) { |
1393 | sub load_header_from($) { |
1318 | my ($self, $path) = @_; |
1394 | my ($self, $path) = @_; |
1319 | |
1395 | |
… | |
… | |
1369 | $self->load_header_orig |
1445 | $self->load_header_orig |
1370 | or return; |
1446 | or return; |
1371 | $self->prepare_orig; |
1447 | $self->prepare_orig; |
1372 | } |
1448 | } |
1373 | |
1449 | |
|
|
1450 | $self->default_region (cf::region::find_by_path $self->{path}) |
|
|
1451 | unless $self->default_region; |
|
|
1452 | |
1374 | 1 |
1453 | 1 |
1375 | } |
1454 | } |
1376 | |
1455 | |
1377 | sub find; |
1456 | sub find; |
1378 | sub find { |
1457 | sub find { |
… | |
… | |
1411 | my ($self) = @_; |
1490 | my ($self) = @_; |
1412 | |
1491 | |
1413 | local $self->{deny_reset} = 1; # loading can take a long time |
1492 | local $self->{deny_reset} = 1; # loading can take a long time |
1414 | |
1493 | |
1415 | my $path = $self->{path}; |
1494 | my $path = $self->{path}; |
|
|
1495 | |
|
|
1496 | { |
1416 | my $guard = cf::lock_acquire "map_load:$path"; |
1497 | my $guard = cf::lock_acquire "map_load:$path"; |
1417 | |
1498 | |
1418 | return if $self->in_memory != cf::MAP_SWAPPED; |
1499 | return if $self->in_memory != cf::MAP_SWAPPED; |
1419 | |
1500 | |
1420 | $self->in_memory (cf::MAP_LOADING); |
1501 | $self->in_memory (cf::MAP_LOADING); |
1421 | |
1502 | |
1422 | $self->alloc; |
1503 | $self->alloc; |
1423 | |
1504 | |
1424 | $self->pre_load; |
1505 | $self->pre_load; |
1425 | |
|
|
1426 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1427 | or return; |
|
|
1428 | |
|
|
1429 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1430 | if delete $self->{load_original}; |
|
|
1431 | |
|
|
1432 | if (my $uniq = $self->uniq_path) { |
|
|
1433 | utf8::encode $uniq; |
|
|
1434 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1435 | $self->clear_unique_items; |
|
|
1436 | $self->_load_objects ($uniq, 0); |
|
|
1437 | } |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | Coro::cede; |
|
|
1441 | |
|
|
1442 | # now do the right thing for maps |
|
|
1443 | $self->link_multipart_objects; |
|
|
1444 | |
|
|
1445 | unless ($self->{deny_activate}) { |
|
|
1446 | $self->decay_objects; |
|
|
1447 | $self->fix_auto_apply; |
|
|
1448 | $self->update_buttons; |
|
|
1449 | Coro::cede; |
1506 | Coro::cede; |
1450 | $self->set_darkness_map; |
1507 | |
|
|
1508 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1509 | or return; |
|
|
1510 | |
|
|
1511 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1512 | if delete $self->{load_original}; |
|
|
1513 | |
|
|
1514 | if (my $uniq = $self->uniq_path) { |
|
|
1515 | utf8::encode $uniq; |
|
|
1516 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1517 | $self->clear_unique_items; |
|
|
1518 | $self->_load_objects ($uniq, 0); |
|
|
1519 | } |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | Coro::cede; |
|
|
1523 | # now do the right thing for maps |
|
|
1524 | $self->link_multipart_objects; |
1451 | $self->difficulty ($self->estimate_difficulty) |
1525 | $self->difficulty ($self->estimate_difficulty) |
1452 | unless $self->difficulty; |
1526 | unless $self->difficulty; |
1453 | Coro::cede; |
1527 | Coro::cede; |
|
|
1528 | |
|
|
1529 | unless ($self->{deny_activate}) { |
|
|
1530 | $self->decay_objects; |
|
|
1531 | $self->fix_auto_apply; |
|
|
1532 | $self->update_buttons; |
|
|
1533 | Coro::cede; |
|
|
1534 | $self->set_darkness_map; |
|
|
1535 | Coro::cede; |
1454 | $self->activate; |
1536 | $self->activate; |
|
|
1537 | } |
|
|
1538 | |
|
|
1539 | $self->in_memory (cf::MAP_IN_MEMORY); |
1455 | } |
1540 | } |
1456 | |
1541 | |
1457 | $self->post_load; |
1542 | $self->post_load; |
1458 | |
|
|
1459 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
1460 | } |
1543 | } |
1461 | |
1544 | |
1462 | sub customise_for { |
1545 | sub customise_for { |
1463 | my ($self, $ob) = @_; |
1546 | my ($self, $ob) = @_; |
1464 | |
1547 | |
… | |
… | |
1655 | instantiated unique items. May block. |
1738 | instantiated unique items. May block. |
1656 | |
1739 | |
1657 | =cut |
1740 | =cut |
1658 | |
1741 | |
1659 | sub unique_maps() { |
1742 | sub unique_maps() { |
1660 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
1743 | my $files = aio_readdir $UNIQUEDIR |
1661 | or return; |
1744 | or return; |
1662 | |
1745 | |
1663 | my @paths; |
1746 | my @paths; |
1664 | |
1747 | |
1665 | for (@$files) { |
1748 | for (@$files) { |
… | |
… | |
1921 | |
2004 | |
1922 | $rmp->{random_seed} ||= $exit->random_seed; |
2005 | $rmp->{random_seed} ||= $exit->random_seed; |
1923 | |
2006 | |
1924 | my $data = cf::to_json $rmp; |
2007 | my $data = cf::to_json $rmp; |
1925 | my $md5 = Digest::MD5::md5_hex $data; |
2008 | my $md5 = Digest::MD5::md5_hex $data; |
1926 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
2009 | my $meta = "$RANDOMDIR/$md5.meta"; |
1927 | |
2010 | |
1928 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2011 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1929 | aio_write $fh, 0, (length $data), $data, 0; |
2012 | aio_write $fh, 0, (length $data), $data, 0; |
1930 | undef $fh; |
2013 | undef $fh; |
1931 | aio_rename "$meta~", $meta; |
2014 | aio_rename "$meta~", $meta; |
… | |
… | |
1940 | |
2023 | |
1941 | return unless $self->type == cf::PLAYER; |
2024 | return unless $self->type == cf::PLAYER; |
1942 | |
2025 | |
1943 | if ($exit->slaying eq "/!") { |
2026 | if ($exit->slaying eq "/!") { |
1944 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
2027 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
2028 | # the problem is that $exit might not survive long enough |
|
|
2029 | # so it needs to be done right now, right here |
1945 | cf::sync_job { prepare_random_map $exit }; |
2030 | cf::sync_job { prepare_random_map $exit }; |
1946 | } |
2031 | } |
1947 | |
2032 | |
1948 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2033 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1949 | my $hp = $exit->stats->hp; |
2034 | my $hp = $exit->stats->hp; |
… | |
… | |
1957 | $self->goto ($slaying, $hp, $sp); |
2042 | $self->goto ($slaying, $hp, $sp); |
1958 | |
2043 | |
1959 | 1; |
2044 | 1; |
1960 | }) { |
2045 | }) { |
1961 | $self->message ("Something went wrong deep within the crossfire server. " |
2046 | $self->message ("Something went wrong deep within the crossfire server. " |
1962 | . "I'll try to bring you back to the map you were before. " |
2047 | . "I'll try to bring you back to the map you were before. " |
1963 | . "Please report this to the dungeon master!", |
2048 | . "Please report this to the dungeon master!", |
1964 | cf::NDI_UNIQUE | cf::NDI_RED); |
2049 | cf::NDI_UNIQUE | cf::NDI_RED); |
1965 | |
2050 | |
1966 | warn "ERROR in enter_exit: $@"; |
2051 | warn "ERROR in enter_exit: $@"; |
1967 | $self->leave_link; |
2052 | $self->leave_link; |
1968 | } |
2053 | } |
1969 | })->prio (1); |
2054 | })->prio (1); |
… | |
… | |
1982 | |
2067 | |
1983 | sub cf::client::send_drawinfo { |
2068 | sub cf::client::send_drawinfo { |
1984 | my ($self, $text, $flags) = @_; |
2069 | my ($self, $text, $flags) = @_; |
1985 | |
2070 | |
1986 | utf8::encode $text; |
2071 | utf8::encode $text; |
1987 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
2072 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1988 | } |
2073 | } |
1989 | |
2074 | |
|
|
2075 | =item $client->ext_event ($type, %msg) |
|
|
2076 | |
|
|
2077 | Sends an exti event to the client. |
|
|
2078 | |
|
|
2079 | =cut |
|
|
2080 | |
|
|
2081 | sub cf::client::ext_event($$%) { |
|
|
2082 | my ($self, $type, %msg) = @_; |
|
|
2083 | |
|
|
2084 | $msg{msgtype} = "event_$type"; |
|
|
2085 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2086 | } |
1990 | |
2087 | |
1991 | =item $success = $client->query ($flags, "text", \&cb) |
2088 | =item $success = $client->query ($flags, "text", \&cb) |
1992 | |
2089 | |
1993 | Queues a query to the client, calling the given callback with |
2090 | Queues a query to the client, calling the given callback with |
1994 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2091 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2091 | |
2188 | |
2092 | # here we export the classes and methods available to script code |
2189 | # here we export the classes and methods available to script code |
2093 | |
2190 | |
2094 | =pod |
2191 | =pod |
2095 | |
2192 | |
2096 | The following fucntions and emthods are available within a safe environment: |
2193 | The following functions and methods are available within a safe environment: |
2097 | |
2194 | |
2098 | cf::object contr pay_amount pay_player map |
2195 | cf::object contr pay_amount pay_player map |
2099 | cf::object::player player |
2196 | cf::object::player player |
2100 | cf::player peaceful |
2197 | cf::player peaceful |
2101 | cf::map trigger |
2198 | cf::map trigger |
… | |
… | |
2186 | ############################################################################# |
2283 | ############################################################################# |
2187 | |
2284 | |
2188 | =head2 EXTENSION DATABASE SUPPORT |
2285 | =head2 EXTENSION DATABASE SUPPORT |
2189 | |
2286 | |
2190 | Crossfire maintains a very simple database for extension use. It can |
2287 | Crossfire maintains a very simple database for extension use. It can |
2191 | currently store anything that can be serialised using Storable, which |
2288 | currently store binary data only (use Compress::LZF::sfreeze_cr/sthaw to |
2192 | excludes objects. |
2289 | convert to/from binary). |
2193 | |
2290 | |
2194 | The parameter C<$family> should best start with the name of the extension |
2291 | The parameter C<$family> should best start with the name of the extension |
2195 | using it, it should be unique. |
2292 | using it, it should be unique. |
2196 | |
2293 | |
2197 | =over 4 |
2294 | =over 4 |
… | |
… | |
2220 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2317 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2221 | cf::cleanup "db_open(db): $!" if $!; |
2318 | cf::cleanup "db_open(db): $!" if $!; |
2222 | }; |
2319 | }; |
2223 | cf::cleanup "db_open(db): $@" if $@; |
2320 | cf::cleanup "db_open(db): $@" if $@; |
2224 | }; |
2321 | }; |
2225 | |
|
|
2226 | my $path = cf::localdir . "/database.pst"; |
|
|
2227 | if (stat $path) { |
|
|
2228 | cf::sync_job { |
|
|
2229 | my $pst = Storable::retrieve $path; |
|
|
2230 | |
|
|
2231 | cf::db_put (board => data => $pst->{board}); |
|
|
2232 | cf::db_put (guildrules => data => $pst->{guildrules}); |
|
|
2233 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2234 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2235 | |
|
|
2236 | unlink $path; |
|
|
2237 | }; |
|
|
2238 | } |
|
|
2239 | } |
2322 | } |
2240 | } |
2323 | } |
2241 | |
2324 | |
2242 | sub db_get($$) { |
2325 | sub db_get($$) { |
2243 | my $key = "$_[0]/$_[1]"; |
2326 | my $key = "$_[0]/$_[1]"; |
2244 | |
2327 | |
2245 | cf::sync_job { |
2328 | cf::sync_job { |
2246 | BDB::db_get $DB, undef, $key, my $data; |
2329 | BDB::db_get $DB, undef, $key, my $data; |
2247 | |
2330 | |
2248 | $! ? () |
2331 | $! ? () |
2249 | : Compress::LZF::sthaw $data |
2332 | : $data |
2250 | } |
2333 | } |
2251 | } |
2334 | } |
2252 | |
2335 | |
2253 | sub db_put($$$) { |
2336 | sub db_put($$$) { |
2254 | BDB::dbreq_pri 4; |
2337 | BDB::dbreq_pri 4; |
2255 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2338 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
2256 | } |
2339 | } |
|
|
2340 | |
|
|
2341 | =item cf::cache $id => [$paths...], $processversion => $process |
|
|
2342 | |
|
|
2343 | Generic caching function that returns the value of the resource $id, |
|
|
2344 | caching and regenerating as required. |
|
|
2345 | |
|
|
2346 | This function can block. |
|
|
2347 | |
|
|
2348 | =cut |
|
|
2349 | |
|
|
2350 | sub cache { |
|
|
2351 | my ($id, $src, $processversion, $process) = @_; |
|
|
2352 | |
|
|
2353 | my $meta = |
|
|
2354 | join "\x00", |
|
|
2355 | $processversion, |
|
|
2356 | map { |
|
|
2357 | aio_stat $_ |
|
|
2358 | and Carp::croak "$_: $!"; |
|
|
2359 | |
|
|
2360 | ($_, (stat _)[7,9]) |
|
|
2361 | } @$src; |
|
|
2362 | |
|
|
2363 | my $dbmeta = db_get cache => "$id/meta"; |
|
|
2364 | if ($dbmeta ne $meta) { |
|
|
2365 | # changed, we may need to process |
|
|
2366 | |
|
|
2367 | my @data; |
|
|
2368 | my $md5; |
|
|
2369 | |
|
|
2370 | for (0 .. $#$src) { |
|
|
2371 | 0 <= aio_load $src->[$_], $data[$_] |
|
|
2372 | or Carp::croak "$src->[$_]: $!"; |
|
|
2373 | } |
|
|
2374 | |
|
|
2375 | # if processing is expensive, check |
|
|
2376 | # checksum first |
|
|
2377 | if (1) { |
|
|
2378 | $md5 = |
|
|
2379 | join "\x00", |
|
|
2380 | $processversion, |
|
|
2381 | map { |
|
|
2382 | Coro::cede; |
|
|
2383 | ($src->[$_], Digest::MD5::md5_hex $data[$_]) |
|
|
2384 | } 0.. $#$src; |
|
|
2385 | |
|
|
2386 | |
|
|
2387 | my $dbmd5 = db_get cache => "$id/md5"; |
|
|
2388 | if ($dbmd5 eq $md5) { |
|
|
2389 | db_put cache => "$id/meta", $meta; |
|
|
2390 | |
|
|
2391 | return db_get cache => "$id/data"; |
|
|
2392 | } |
|
|
2393 | } |
|
|
2394 | |
|
|
2395 | my $t1 = Time::HiRes::time; |
|
|
2396 | my $data = $process->(\@data); |
|
|
2397 | my $t2 = Time::HiRes::time; |
|
|
2398 | |
|
|
2399 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
|
|
2400 | |
|
|
2401 | db_put cache => "$id/data", $data; |
|
|
2402 | db_put cache => "$id/md5" , $md5; |
|
|
2403 | db_put cache => "$id/meta", $meta; |
|
|
2404 | |
|
|
2405 | return $data; |
|
|
2406 | } |
|
|
2407 | |
|
|
2408 | db_get cache => "$id/data" |
|
|
2409 | } |
|
|
2410 | |
|
|
2411 | =item fork_call { }, $args |
|
|
2412 | |
|
|
2413 | Executes the given code block with the given arguments in a seperate |
|
|
2414 | process, returning the results. Everything must be serialisable with |
|
|
2415 | Coro::Storable. May, of course, block. Note that the executed sub may |
|
|
2416 | never block itself or use any form of Event handling. |
|
|
2417 | |
|
|
2418 | =cut |
|
|
2419 | |
|
|
2420 | sub fork_call(&@) { |
|
|
2421 | my ($cb, @args) = @_; |
|
|
2422 | |
|
|
2423 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
2424 | # or die "socketpair: $!"; |
|
|
2425 | pipe my $fh1, my $fh2 |
|
|
2426 | or die "pipe: $!"; |
|
|
2427 | |
|
|
2428 | if (my $pid = fork) { |
|
|
2429 | close $fh2; |
|
|
2430 | |
|
|
2431 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
|
|
2432 | $res = Coro::Storable::thaw $res; |
|
|
2433 | |
|
|
2434 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
|
|
2435 | |
|
|
2436 | die $$res unless "ARRAY" eq ref $res; |
|
|
2437 | |
|
|
2438 | return wantarray ? @$res : $res->[-1]; |
|
|
2439 | } else { |
|
|
2440 | reset_signals; |
|
|
2441 | local $SIG{__WARN__}; |
|
|
2442 | local $SIG{__DIE__}; |
|
|
2443 | eval { |
|
|
2444 | close $fh1; |
|
|
2445 | |
|
|
2446 | my @res = eval { $cb->(@args) }; |
|
|
2447 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
|
|
2448 | }; |
|
|
2449 | |
|
|
2450 | warn $@ if $@; |
|
|
2451 | _exit 0; |
|
|
2452 | } |
|
|
2453 | } |
|
|
2454 | |
|
|
2455 | |
2257 | |
2456 | |
2258 | ############################################################################# |
2457 | ############################################################################# |
2259 | # the server's init and main functions |
2458 | # the server's init and main functions |
2260 | |
2459 | |
2261 | sub load_resources { |
2460 | sub load_facedata($) { |
2262 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2461 | my ($path) = @_; |
|
|
2462 | |
|
|
2463 | warn "loading facedata from $path\n"; |
|
|
2464 | |
|
|
2465 | my $facedata; |
|
|
2466 | 0 < aio_load $path, $facedata |
|
|
2467 | or die "$path: $!"; |
|
|
2468 | |
|
|
2469 | $facedata = Coro::Storable::thaw $facedata; |
|
|
2470 | |
|
|
2471 | $facedata->{version} == 2 |
|
|
2472 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
|
|
2473 | |
|
|
2474 | { |
|
|
2475 | my $faces = $facedata->{faceinfo}; |
|
|
2476 | |
|
|
2477 | while (my ($face, $info) = each %$faces) { |
|
|
2478 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
2479 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
|
|
2480 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2481 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
|
|
2482 | Coro::cede; |
|
|
2483 | } |
|
|
2484 | |
|
|
2485 | while (my ($face, $info) = each %$faces) { |
|
|
2486 | next unless $info->{smooth}; |
|
|
2487 | my $idx = cf::face::find $face |
|
|
2488 | or next; |
|
|
2489 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2490 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
|
|
2491 | } else { |
|
|
2492 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
|
|
2493 | } |
|
|
2494 | Coro::cede; |
|
|
2495 | } |
|
|
2496 | } |
|
|
2497 | |
|
|
2498 | { |
|
|
2499 | my $anims = $facedata->{animinfo}; |
|
|
2500 | |
|
|
2501 | while (my ($anim, $info) = each %$anims) { |
|
|
2502 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2503 | Coro::cede; |
|
|
2504 | } |
|
|
2505 | |
|
|
2506 | cf::anim::invalidate_all; # d'oh |
|
|
2507 | } |
|
|
2508 | |
|
|
2509 | 1 |
|
|
2510 | } |
|
|
2511 | |
|
|
2512 | sub reload_regions { |
|
|
2513 | load_resource_file "$MAPDIR/regions" |
2263 | or die "unable to load regions file\n";#d# |
2514 | or die "unable to load regions file\n"; |
|
|
2515 | } |
|
|
2516 | |
|
|
2517 | sub reload_facedata { |
|
|
2518 | load_facedata "$DATADIR/facedata" |
|
|
2519 | or die "unable to load facedata\n"; |
|
|
2520 | } |
|
|
2521 | |
|
|
2522 | sub reload_archetypes { |
|
|
2523 | load_resource_file "$DATADIR/archetypes" |
|
|
2524 | or die "unable to load archetypes\n"; |
|
|
2525 | } |
|
|
2526 | |
|
|
2527 | sub reload_treasures { |
|
|
2528 | load_resource_file "$DATADIR/treasures" |
|
|
2529 | or die "unable to load treasurelists\n"; |
|
|
2530 | } |
|
|
2531 | |
|
|
2532 | sub reload_resources { |
|
|
2533 | warn "reloading resource files...\n"; |
|
|
2534 | |
|
|
2535 | reload_regions; |
|
|
2536 | reload_facedata; |
|
|
2537 | reload_archetypes; |
|
|
2538 | reload_treasures; |
|
|
2539 | |
|
|
2540 | warn "finished reloading resource files\n"; |
|
|
2541 | } |
|
|
2542 | |
|
|
2543 | sub init { |
|
|
2544 | reload_resources; |
2264 | } |
2545 | } |
2265 | |
2546 | |
2266 | sub cfg_load { |
2547 | sub cfg_load { |
2267 | open my $fh, "<:utf8", cf::confdir . "/config" |
2548 | open my $fh, "<:utf8", "$CONFDIR/config" |
2268 | or return; |
2549 | or return; |
2269 | |
2550 | |
2270 | local $/; |
2551 | local $/; |
2271 | *CFG = YAML::Syck::Load <$fh>; |
2552 | *CFG = YAML::Syck::Load <$fh>; |
2272 | |
2553 | |
… | |
… | |
2280 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2561 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2281 | and die "WARNING: m(un)lockall failed: $!\n"; |
2562 | and die "WARNING: m(un)lockall failed: $!\n"; |
2282 | }; |
2563 | }; |
2283 | warn $@ if $@; |
2564 | warn $@ if $@; |
2284 | } |
2565 | } |
2285 | } |
|
|
2286 | |
|
|
2287 | sub init { |
|
|
2288 | load_resources; |
|
|
2289 | } |
2566 | } |
2290 | |
2567 | |
2291 | sub main { |
2568 | sub main { |
2292 | # we must not ever block the main coroutine |
2569 | # we must not ever block the main coroutine |
2293 | local $Coro::idle = sub { |
2570 | local $Coro::idle = sub { |
… | |
… | |
2361 | |
2638 | |
2362 | warn Carp::longmess "post_cleanup backtrace" |
2639 | warn Carp::longmess "post_cleanup backtrace" |
2363 | if $make_core; |
2640 | if $make_core; |
2364 | } |
2641 | } |
2365 | |
2642 | |
2366 | sub reload() { |
2643 | sub do_reload_perl() { |
2367 | # can/must only be called in main |
2644 | # can/must only be called in main |
2368 | if ($Coro::current != $Coro::main) { |
2645 | if ($Coro::current != $Coro::main) { |
2369 | warn "can only reload from main coroutine"; |
2646 | warn "can only reload from main coroutine"; |
2370 | return; |
2647 | return; |
2371 | } |
2648 | } |
… | |
… | |
2396 | Coro::cede; |
2673 | Coro::cede; |
2397 | last unless IO::AIO::nreqs || BDB::nreqs; |
2674 | last unless IO::AIO::nreqs || BDB::nreqs; |
2398 | warn "iterate..."; |
2675 | warn "iterate..."; |
2399 | } |
2676 | } |
2400 | |
2677 | |
|
|
2678 | ++$RELOAD; |
|
|
2679 | |
2401 | warn "cancelling all extension coros"; |
2680 | warn "cancelling all extension coros"; |
2402 | $_->cancel for values %EXT_CORO; |
2681 | $_->cancel for values %EXT_CORO; |
2403 | %EXT_CORO = (); |
2682 | %EXT_CORO = (); |
2404 | |
2683 | |
2405 | warn "removing commands"; |
2684 | warn "removing commands"; |
… | |
… | |
2444 | Symbol::delete_package "safe::$_" |
2723 | Symbol::delete_package "safe::$_" |
2445 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2724 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2446 | |
2725 | |
2447 | warn "unloading cf.pm \"a bit\""; |
2726 | warn "unloading cf.pm \"a bit\""; |
2448 | delete $INC{"cf.pm"}; |
2727 | delete $INC{"cf.pm"}; |
|
|
2728 | delete $INC{"cf/pod.pm"}; |
2449 | |
2729 | |
2450 | # don't, removes xs symbols, too, |
2730 | # don't, removes xs symbols, too, |
2451 | # and global variables created in xs |
2731 | # and global variables created in xs |
2452 | #Symbol::delete_package __PACKAGE__; |
2732 | #Symbol::delete_package __PACKAGE__; |
2453 | |
2733 | |
… | |
… | |
2462 | |
2742 | |
2463 | warn "loading extensions"; |
2743 | warn "loading extensions"; |
2464 | cf::load_extensions; |
2744 | cf::load_extensions; |
2465 | |
2745 | |
2466 | warn "reattaching attachments to objects/players"; |
2746 | warn "reattaching attachments to objects/players"; |
2467 | _global_reattach; |
2747 | _global_reattach; # objects, sockets |
2468 | warn "reattaching attachments to maps"; |
2748 | warn "reattaching attachments to maps"; |
2469 | reattach $_ for values %MAP; |
2749 | reattach $_ for values %MAP; |
2470 | |
2750 | warn "reattaching attachments to players"; |
2471 | warn "loading reloadable resources"; |
2751 | reattach $_ for values %PLAYER; |
2472 | load_resources; |
|
|
2473 | |
2752 | |
2474 | warn "leaving sync_job"; |
2753 | warn "leaving sync_job"; |
2475 | |
2754 | |
2476 | 1 |
2755 | 1 |
2477 | } or do { |
2756 | } or do { |
… | |
… | |
2483 | warn "reloaded"; |
2762 | warn "reloaded"; |
2484 | }; |
2763 | }; |
2485 | |
2764 | |
2486 | our $RELOAD_WATCHER; # used only during reload |
2765 | our $RELOAD_WATCHER; # used only during reload |
2487 | |
2766 | |
|
|
2767 | sub reload_perl() { |
|
|
2768 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2769 | # coro crashes during coro_state_free->destroy here. |
|
|
2770 | |
|
|
2771 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2772 | reentrant => 0, |
|
|
2773 | after => 0, |
|
|
2774 | data => WF_AUTOCANCEL, |
|
|
2775 | cb => sub { |
|
|
2776 | do_reload_perl; |
|
|
2777 | undef $RELOAD_WATCHER; |
|
|
2778 | }, |
|
|
2779 | ); |
|
|
2780 | } |
|
|
2781 | |
2488 | register_command "reload" => sub { |
2782 | register_command "reload" => sub { |
2489 | my ($who, $arg) = @_; |
2783 | my ($who, $arg) = @_; |
2490 | |
2784 | |
2491 | if ($who->flag (FLAG_WIZ)) { |
2785 | if ($who->flag (FLAG_WIZ)) { |
2492 | $who->message ("reloading server."); |
2786 | $who->message ("reloading server."); |
2493 | |
2787 | async { reload_perl }; |
2494 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2495 | # coro crashes during coro_state_free->destroy here. |
|
|
2496 | |
|
|
2497 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2498 | reentrant => 0, |
|
|
2499 | after => 0, |
|
|
2500 | data => WF_AUTOCANCEL, |
|
|
2501 | cb => sub { |
|
|
2502 | reload; |
|
|
2503 | undef $RELOAD_WATCHER; |
|
|
2504 | }, |
|
|
2505 | ); |
|
|
2506 | } |
2788 | } |
2507 | }; |
2789 | }; |
2508 | |
2790 | |
2509 | unshift @INC, $LIBDIR; |
2791 | unshift @INC, $LIBDIR; |
2510 | |
2792 | |
2511 | my $bug_warning = 0; |
2793 | my $bug_warning = 0; |
|
|
2794 | |
|
|
2795 | our @WAIT_FOR_TICK; |
|
|
2796 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2797 | |
|
|
2798 | sub wait_for_tick { |
|
|
2799 | return unless $TICK_WATCHER->is_active; |
|
|
2800 | return if $Coro::current == $Coro::main; |
|
|
2801 | |
|
|
2802 | my $signal = new Coro::Signal; |
|
|
2803 | push @WAIT_FOR_TICK, $signal; |
|
|
2804 | $signal->wait; |
|
|
2805 | } |
|
|
2806 | |
|
|
2807 | sub wait_for_tick_begin { |
|
|
2808 | return unless $TICK_WATCHER->is_active; |
|
|
2809 | return if $Coro::current == $Coro::main; |
|
|
2810 | |
|
|
2811 | my $signal = new Coro::Signal; |
|
|
2812 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2813 | $signal->wait; |
|
|
2814 | } |
2512 | |
2815 | |
2513 | $TICK_WATCHER = Event->timer ( |
2816 | $TICK_WATCHER = Event->timer ( |
2514 | reentrant => 0, |
2817 | reentrant => 0, |
2515 | parked => 1, |
2818 | parked => 1, |
2516 | prio => 0, |
2819 | prio => 0, |
… | |
… | |
2524 | } |
2827 | } |
2525 | |
2828 | |
2526 | $NOW = Event::time; |
2829 | $NOW = Event::time; |
2527 | |
2830 | |
2528 | cf::server_tick; # one server iteration |
2831 | cf::server_tick; # one server iteration |
|
|
2832 | |
2529 | $RUNTIME += $TICK; |
2833 | $RUNTIME += $TICK; |
2530 | $NEXT_TICK += $TICK; |
2834 | $NEXT_TICK += $TICK; |
2531 | |
2835 | |
2532 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2836 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2533 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2837 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2535 | write_runtime |
2839 | write_runtime |
2536 | or warn "ERROR: unable to write runtime file: $!"; |
2840 | or warn "ERROR: unable to write runtime file: $!"; |
2537 | }; |
2841 | }; |
2538 | } |
2842 | } |
2539 | |
2843 | |
2540 | |
|
|
2541 | $WAIT_FOR_TICK->broadcast; |
|
|
2542 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2543 | |
|
|
2544 | # my $AFTER = Event::time; |
2844 | # my $AFTER = Event::time; |
2545 | # warn $AFTER - $NOW;#d# |
2845 | # warn $AFTER - $NOW;#d# |
2546 | |
2846 | |
2547 | # if we are delayed by four ticks or more, skip them all |
2847 | # if we are delayed by four ticks or more, skip them all |
2548 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2848 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2549 | |
2849 | |
2550 | $TICK_WATCHER->at ($NEXT_TICK); |
2850 | $TICK_WATCHER->at ($NEXT_TICK); |
2551 | $TICK_WATCHER->start; |
2851 | $TICK_WATCHER->start; |
|
|
2852 | |
|
|
2853 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2854 | $sig->send; |
|
|
2855 | } |
|
|
2856 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2857 | $sig->send; |
|
|
2858 | } |
|
|
2859 | |
|
|
2860 | _post_tick; |
2552 | }, |
2861 | }, |
2553 | ); |
2862 | ); |
2554 | |
2863 | |
2555 | { |
2864 | { |
2556 | BDB::max_poll_time $TICK * 0.1; |
2865 | BDB::max_poll_time $TICK * 0.1; |
… | |
… | |
2584 | |
2893 | |
2585 | cf::sync_job { |
2894 | cf::sync_job { |
2586 | eval { |
2895 | eval { |
2587 | BDB::db_env_open |
2896 | BDB::db_env_open |
2588 | $DB_ENV, |
2897 | $DB_ENV, |
2589 | $BDB_ENV_DIR, |
2898 | $BDBDIR, |
2590 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2899 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2591 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2900 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2592 | 0666; |
2901 | 0666; |
2593 | |
2902 | |
2594 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
2903 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
2595 | |
2904 | |
2596 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2905 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2597 | $DB_ENV->set_lk_detect; |
2906 | $DB_ENV->set_lk_detect; |
2598 | }; |
2907 | }; |
2599 | |
2908 | |
… | |
… | |
2615 | prio => 6, |
2924 | prio => 6, |
2616 | cb => \&IO::AIO::poll_cb, |
2925 | cb => \&IO::AIO::poll_cb, |
2617 | ); |
2926 | ); |
2618 | } |
2927 | } |
2619 | |
2928 | |
|
|
2929 | my $_log_backtrace; |
|
|
2930 | |
|
|
2931 | sub _log_backtrace { |
|
|
2932 | my ($msg, @addr) = @_; |
|
|
2933 | |
|
|
2934 | $msg =~ s/\n//; |
|
|
2935 | |
|
|
2936 | # limit the # of concurrent backtraces |
|
|
2937 | if ($_log_backtrace < 2) { |
|
|
2938 | ++$_log_backtrace; |
|
|
2939 | async { |
|
|
2940 | my @bt = fork_call { |
|
|
2941 | @addr = map { sprintf "%x", $_ } @addr; |
|
|
2942 | my $self = (-f "/proc/$$/exe") ? "/proc/$$/exe" : $^X; |
|
|
2943 | open my $fh, "exec addr2line -C -f -i -e \Q$self\E @addr 2>&1 |" |
|
|
2944 | or die "addr2line: $!"; |
|
|
2945 | |
|
|
2946 | my @funcs; |
|
|
2947 | my @res = <$fh>; |
|
|
2948 | chomp for @res; |
|
|
2949 | while (@res) { |
|
|
2950 | my ($func, $line) = splice @res, 0, 2, (); |
|
|
2951 | push @funcs, "[$func] $line"; |
|
|
2952 | } |
|
|
2953 | |
|
|
2954 | @funcs |
|
|
2955 | }; |
|
|
2956 | |
|
|
2957 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
2958 | LOG llevInfo, "[ABT] $_\n" for @bt; |
|
|
2959 | --$_log_backtrace; |
|
|
2960 | }; |
|
|
2961 | } else { |
|
|
2962 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
2963 | LOG llevInfo, "[ABT] [suppressed]\n"; |
|
|
2964 | } |
|
|
2965 | } |
|
|
2966 | |
|
|
2967 | # load additional modules |
|
|
2968 | use cf::pod; |
|
|
2969 | |
2620 | END { cf::emergency_save } |
2970 | END { cf::emergency_save } |
2621 | |
2971 | |
2622 | 1 |
2972 | 1 |
2623 | |
2973 | |