… | |
… | |
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 | |
|
|
95 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
|
|
96 | our $LOADAVG; # same thing, but with alpha-smoothing |
|
|
97 | our $tick_start; # for load detecting purposes |
|
|
98 | |
79 | binmode STDOUT; |
99 | binmode STDOUT; |
80 | binmode STDERR; |
100 | binmode STDERR; |
81 | |
101 | |
82 | # read virtual server time, if available |
102 | # read virtual server time, if available |
83 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
103 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
84 | open my $fh, "<", cf::localdir . "/runtime" |
104 | open my $fh, "<", "$LOCALDIR/runtime" |
85 | or die "unable to read runtime file: $!"; |
105 | or die "unable to read runtime file: $!"; |
86 | $RUNTIME = <$fh> + 0.; |
106 | $RUNTIME = <$fh> + 0.; |
87 | } |
107 | } |
88 | |
108 | |
89 | mkdir cf::localdir; |
109 | mkdir $_ |
90 | mkdir cf::localdir . "/" . cf::playerdir; |
110 | 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 | |
111 | |
96 | our $EMERGENCY_POSITION; |
112 | our $EMERGENCY_POSITION; |
97 | |
113 | |
98 | sub cf::map::normalise; |
114 | sub cf::map::normalise; |
99 | |
115 | |
… | |
… | |
110 | =item $cf::RUNTIME |
126 | =item $cf::RUNTIME |
111 | |
127 | |
112 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
128 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
113 | every server tick. |
129 | every server tick. |
114 | |
130 | |
115 | =item $cf::LIBDIR |
131 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
|
|
132 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
|
|
133 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
116 | |
134 | |
117 | The perl library directory, where extensions and cf-specific modules can |
135 | Various directories - "/etc", read-only install directory, perl-library |
118 | be found. It will be added to C<@INC> automatically. |
136 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
|
|
137 | unique-items directory, player file directory, random maps directory and |
|
|
138 | database environment. |
119 | |
139 | |
120 | =item $cf::NOW |
140 | =item $cf::NOW |
121 | |
141 | |
122 | The time of the last (current) server tick. |
142 | The time of the last (current) server tick. |
123 | |
143 | |
124 | =item $cf::TICK |
144 | =item $cf::TICK |
125 | |
145 | |
126 | The interval between server ticks, in seconds. |
146 | The interval between server ticks, in seconds. |
|
|
147 | |
|
|
148 | =item $cf::LOADAVG |
|
|
149 | |
|
|
150 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
|
|
151 | (none) and 1 (overloaded), indicating how much time is spent on processing |
|
|
152 | objects per tick. Healthy values are < 0.5. |
|
|
153 | |
|
|
154 | =item $cf::LOAD |
|
|
155 | |
|
|
156 | The raw value load value from the last tick. |
127 | |
157 | |
128 | =item %cf::CFG |
158 | =item %cf::CFG |
129 | |
159 | |
130 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
160 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
131 | from wherever your confdir points to. |
161 | from wherever your confdir points to. |
132 | |
162 | |
133 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
163 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
134 | |
164 | |
135 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
165 | 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 |
166 | 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 |
167 | 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 | |
168 | |
140 | =back |
169 | =back |
141 | |
170 | |
142 | =cut |
171 | =cut |
143 | |
172 | |
144 | BEGIN { |
173 | BEGIN { |
145 | *CORE::GLOBAL::warn = sub { |
174 | *CORE::GLOBAL::warn = sub { |
146 | my $msg = join "", @_; |
175 | my $msg = join "", @_; |
147 | utf8::encode $msg; |
|
|
148 | |
176 | |
149 | $msg .= "\n" |
177 | $msg .= "\n" |
150 | unless $msg =~ /\n$/; |
178 | unless $msg =~ /\n$/; |
151 | |
179 | |
|
|
180 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
|
|
181 | |
|
|
182 | utf8::encode $msg; |
152 | LOG llevError, $msg; |
183 | LOG llevError, $msg; |
153 | }; |
184 | }; |
154 | } |
185 | } |
155 | |
186 | |
156 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
187 | @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; |
233 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
203 | $d |
234 | $d |
204 | } || "[unable to dump $_[0]: '$@']"; |
235 | } || "[unable to dump $_[0]: '$@']"; |
205 | } |
236 | } |
206 | |
237 | |
207 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
238 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
208 | |
239 | |
209 | =item $ref = cf::from_json $json |
240 | =item $ref = cf::from_json $json |
210 | |
241 | |
211 | Converts a JSON string into the corresponding perl data structure. |
242 | Converts a JSON string into the corresponding perl data structure. |
212 | |
243 | |
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 |
244 | =item $json = cf::to_json $ref |
221 | |
245 | |
222 | Converts a perl data structure into its JSON representation. |
246 | 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 | |
247 | |
231 | =item cf::lock_wait $string |
248 | =item cf::lock_wait $string |
232 | |
249 | |
233 | Wait until the given lock is available. See cf::lock_acquire. |
250 | Wait until the given lock is available. See cf::lock_acquire. |
234 | |
251 | |
… | |
… | |
240 | returned. |
257 | returned. |
241 | |
258 | |
242 | Lock names should begin with a unique identifier (for example, cf::map::find |
259 | Lock names should begin with a unique identifier (for example, cf::map::find |
243 | uses map_find and cf::map::load uses map_load). |
260 | uses map_find and cf::map::load uses map_load). |
244 | |
261 | |
|
|
262 | =item $locked = cf::lock_active $string |
|
|
263 | |
|
|
264 | Return true if the lock is currently active, i.e. somebody has locked it. |
|
|
265 | |
245 | =cut |
266 | =cut |
246 | |
267 | |
247 | our %LOCK; |
268 | our %LOCK; |
248 | |
269 | |
249 | sub lock_wait($) { |
270 | sub lock_wait($) { |
… | |
… | |
266 | |
287 | |
267 | Coro::guard { |
288 | Coro::guard { |
268 | # wake up all waiters, to be on the safe side |
289 | # wake up all waiters, to be on the safe side |
269 | $_->ready for @{ delete $LOCK{$key} }; |
290 | $_->ready for @{ delete $LOCK{$key} }; |
270 | } |
291 | } |
|
|
292 | } |
|
|
293 | |
|
|
294 | sub lock_active($) { |
|
|
295 | my ($key) = @_; |
|
|
296 | |
|
|
297 | ! ! $LOCK{$key} |
271 | } |
298 | } |
272 | |
299 | |
273 | sub freeze_mainloop { |
300 | sub freeze_mainloop { |
274 | return unless $TICK_WATCHER->is_active; |
301 | return unless $TICK_WATCHER->is_active; |
275 | |
302 | |
… | |
… | |
305 | |
332 | |
306 | sub sync_job(&) { |
333 | sub sync_job(&) { |
307 | my ($job) = @_; |
334 | my ($job) = @_; |
308 | |
335 | |
309 | if ($Coro::current == $Coro::main) { |
336 | if ($Coro::current == $Coro::main) { |
|
|
337 | my $time = Event::time; |
|
|
338 | |
310 | # this is the main coro, too bad, we have to block |
339 | # this is the main coro, too bad, we have to block |
311 | # till the operation succeeds, freezing the server :/ |
340 | # till the operation succeeds, freezing the server :/ |
312 | |
341 | |
313 | # TODO: use suspend/resume instead |
342 | # TODO: use suspend/resume instead |
314 | # (but this is cancel-safe) |
343 | # (but this is cancel-safe) |
… | |
… | |
325 | |
354 | |
326 | while ($busy) { |
355 | while ($busy) { |
327 | Coro::cede or Event::one_event; |
356 | Coro::cede or Event::one_event; |
328 | } |
357 | } |
329 | |
358 | |
|
|
359 | $time = Event::time - $time; |
|
|
360 | |
|
|
361 | LOG llevError | logBacktrace, Carp::longmess "long sync job" |
|
|
362 | if $time > $TICK * 0.5 && $TICK_WATCHER->is_active; |
|
|
363 | |
|
|
364 | $tick_start += $time; # do not account sync jobs to server load |
|
|
365 | |
330 | wantarray ? @res : $res[0] |
366 | wantarray ? @res : $res[0] |
331 | } else { |
367 | } else { |
332 | # we are in another coroutine, how wonderful, everything just works |
368 | # we are in another coroutine, how wonderful, everything just works |
333 | |
369 | |
334 | $job->() |
370 | $job->() |
… | |
… | |
354 | |
390 | |
355 | $coro |
391 | $coro |
356 | } |
392 | } |
357 | |
393 | |
358 | sub write_runtime { |
394 | sub write_runtime { |
359 | my $runtime = cf::localdir . "/runtime"; |
395 | my $runtime = "$LOCALDIR/runtime"; |
|
|
396 | |
|
|
397 | # first touch the runtime file to show we are still running: |
|
|
398 | # the fsync below can take a very very long time. |
|
|
399 | |
|
|
400 | if (my $fh = aio_open $runtime, O_WRONLY, 0) { |
|
|
401 | utime undef, undef, $fh; |
|
|
402 | } |
|
|
403 | |
|
|
404 | my $guard = cf::lock_acquire "write_runtime"; |
360 | |
405 | |
361 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
406 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
362 | or return; |
407 | or return; |
363 | |
408 | |
364 | my $value = $cf::RUNTIME + 90 + 10; |
409 | my $value = $cf::RUNTIME + 90 + 10; |
… | |
… | |
370 | |
415 | |
371 | # always fsync - this file is important |
416 | # always fsync - this file is important |
372 | aio_fsync $fh |
417 | aio_fsync $fh |
373 | and return; |
418 | and return; |
374 | |
419 | |
|
|
420 | # touch it again to show we are up-to-date |
|
|
421 | utime undef, undef, $fh; |
|
|
422 | |
375 | close $fh |
423 | close $fh |
376 | or return; |
424 | or return; |
377 | |
425 | |
378 | aio_rename "$runtime~", $runtime |
426 | aio_rename "$runtime~", $runtime |
379 | and return; |
427 | and return; |
380 | |
428 | |
|
|
429 | warn "runtime file written.\n";#d# |
|
|
430 | |
381 | 1 |
431 | 1 |
|
|
432 | } |
|
|
433 | |
|
|
434 | =item cf::datalog type => key => value, ... |
|
|
435 | |
|
|
436 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
437 | |
|
|
438 | =cut |
|
|
439 | |
|
|
440 | sub datalog($@) { |
|
|
441 | my ($type, %kv) = @_; |
|
|
442 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
382 | } |
443 | } |
383 | |
444 | |
384 | =back |
445 | =back |
385 | |
446 | |
386 | =cut |
447 | =cut |
… | |
… | |
948 | Returns the given player object, loading it if necessary (might block). |
1009 | Returns the given player object, loading it if necessary (might block). |
949 | |
1010 | |
950 | =cut |
1011 | =cut |
951 | |
1012 | |
952 | sub playerdir($) { |
1013 | sub playerdir($) { |
953 | cf::localdir |
1014 | "$PLAYERDIR/" |
954 | . "/" |
|
|
955 | . cf::playerdir |
|
|
956 | . "/" |
|
|
957 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
1015 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
958 | } |
1016 | } |
959 | |
1017 | |
960 | sub path($) { |
1018 | sub path($) { |
961 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
1019 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
962 | |
1020 | |
963 | (playerdir $login) . "/$login.pl" |
1021 | (playerdir $login) . "/playerdata" |
964 | } |
1022 | } |
965 | |
1023 | |
966 | sub find_active($) { |
1024 | sub find_active($) { |
967 | $cf::PLAYER{$_[0]} |
1025 | $cf::PLAYER{$_[0]} |
968 | and $cf::PLAYER{$_[0]}->active |
1026 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
981 | my $login = $_[0]; |
1039 | my $login = $_[0]; |
982 | |
1040 | |
983 | my $guard = cf::lock_acquire "user_find:$login"; |
1041 | my $guard = cf::lock_acquire "user_find:$login"; |
984 | |
1042 | |
985 | $cf::PLAYER{$_[0]} || do { |
1043 | $cf::PLAYER{$_[0]} || do { |
|
|
1044 | # rename old playerfiles to new ones |
|
|
1045 | #TODO: remove when no longer required |
|
|
1046 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
1047 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
1048 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
1049 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
1050 | |
986 | my $pl = load_pl path $login |
1051 | my $pl = load_pl path $login |
987 | or return; |
1052 | or return; |
988 | $cf::PLAYER{$login} = $pl |
1053 | $cf::PLAYER{$login} = $pl |
989 | } |
1054 | } |
990 | } |
1055 | } |
… | |
… | |
1027 | |
1092 | |
1028 | =cut |
1093 | =cut |
1029 | |
1094 | |
1030 | sub quit_character { |
1095 | sub quit_character { |
1031 | my ($pl) = @_; |
1096 | my ($pl) = @_; |
|
|
1097 | |
|
|
1098 | my $name = $pl->ob->name; |
1032 | |
1099 | |
1033 | $pl->{deny_save} = 1; |
1100 | $pl->{deny_save} = 1; |
1034 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1101 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1035 | |
1102 | |
1036 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1103 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
… | |
… | |
1041 | my $path = playerdir $pl; |
1108 | my $path = playerdir $pl; |
1042 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1109 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1043 | aio_rename $path, $temp; |
1110 | aio_rename $path, $temp; |
1044 | delete $cf::PLAYER{$pl->ob->name}; |
1111 | delete $cf::PLAYER{$pl->ob->name}; |
1045 | $pl->destroy; |
1112 | $pl->destroy; |
|
|
1113 | |
|
|
1114 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1115 | |
|
|
1116 | # nuke player maps |
|
|
1117 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1118 | |
1046 | IO::AIO::aio_rmtree $temp; |
1119 | IO::AIO::aio_rmtree $temp; |
|
|
1120 | } |
|
|
1121 | |
|
|
1122 | =item $pl->kick |
|
|
1123 | |
|
|
1124 | Kicks a player out of the game. This destroys the connection. |
|
|
1125 | |
|
|
1126 | =cut |
|
|
1127 | |
|
|
1128 | sub kick { |
|
|
1129 | my ($pl, $kicker) = @_; |
|
|
1130 | |
|
|
1131 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1132 | $pl->killer ("kicked"); |
|
|
1133 | $pl->ns->destroy; |
1047 | } |
1134 | } |
1048 | |
1135 | |
1049 | =item cf::player::list_logins |
1136 | =item cf::player::list_logins |
1050 | |
1137 | |
1051 | Returns am arrayref of all valid playernames in the system, can take a |
1138 | Returns am arrayref of all valid playernames in the system, can take a |
1052 | while and may block, so not sync_job-capable, ever. |
1139 | while and may block, so not sync_job-capable, ever. |
1053 | |
1140 | |
1054 | =cut |
1141 | =cut |
1055 | |
1142 | |
1056 | sub list_logins { |
1143 | sub list_logins { |
1057 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
1144 | my $dirs = aio_readdir $PLAYERDIR |
1058 | or return []; |
1145 | or return []; |
1059 | |
1146 | |
1060 | my @logins; |
1147 | my @logins; |
1061 | |
1148 | |
1062 | for my $login (@$dirs) { |
1149 | for my $login (@$dirs) { |
… | |
… | |
1097 | } |
1184 | } |
1098 | |
1185 | |
1099 | \@paths |
1186 | \@paths |
1100 | } |
1187 | } |
1101 | |
1188 | |
1102 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1189 | =item $player->ext_reply ($msgid, %msg) |
1103 | |
1190 | |
1104 | Sends an ext reply to the player. |
1191 | Sends an ext reply to the player. |
1105 | |
1192 | |
1106 | =cut |
1193 | =cut |
1107 | |
1194 | |
1108 | sub ext_reply($$$%) { |
1195 | sub ext_reply($$%) { |
1109 | my ($self, $id, %msg) = @_; |
1196 | my ($self, $id, %msg) = @_; |
1110 | |
1197 | |
1111 | $msg{msgid} = $id; |
1198 | $msg{msgid} = $id; |
1112 | |
1199 | |
1113 | $self->send ("ext " . cf::to_json \%msg); |
1200 | $self->send ("ext " . cf::to_json \%msg); |
1114 | } |
1201 | } |
1115 | |
1202 | |
1116 | package cf; |
1203 | =item $player->ext_event ($type, %msg) |
|
|
1204 | |
|
|
1205 | Sends an ext event to the client. |
|
|
1206 | |
|
|
1207 | =cut |
|
|
1208 | |
|
|
1209 | sub ext_event($$%) { |
|
|
1210 | my ($self, $type, %msg) = @_; |
|
|
1211 | |
|
|
1212 | $self->ns->ext_event ($type, %msg); |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | =head3 cf::region |
|
|
1216 | |
|
|
1217 | =over 4 |
|
|
1218 | |
|
|
1219 | =cut |
|
|
1220 | |
|
|
1221 | package cf::region; |
|
|
1222 | |
|
|
1223 | =item cf::region::find_by_path $path |
|
|
1224 | |
|
|
1225 | Tries to decuce the probable region for a map knowing only its path. |
|
|
1226 | |
|
|
1227 | =cut |
|
|
1228 | |
|
|
1229 | sub find_by_path($) { |
|
|
1230 | my ($path) = @_; |
|
|
1231 | |
|
|
1232 | my ($match, $specificity); |
|
|
1233 | |
|
|
1234 | for my $region (list) { |
|
|
1235 | if ($region->match && $path =~ $region->match) { |
|
|
1236 | ($match, $specificity) = ($region, $region->specificity) |
|
|
1237 | if $region->specificity > $specificity; |
|
|
1238 | } |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | $match |
|
|
1242 | } |
1117 | |
1243 | |
1118 | =back |
1244 | =back |
1119 | |
|
|
1120 | |
1245 | |
1121 | =head3 cf::map |
1246 | =head3 cf::map |
1122 | |
1247 | |
1123 | =over 4 |
1248 | =over 4 |
1124 | |
1249 | |
… | |
… | |
1262 | |
1387 | |
1263 | # the original (read-only) location |
1388 | # the original (read-only) location |
1264 | sub load_path { |
1389 | sub load_path { |
1265 | my ($self) = @_; |
1390 | my ($self) = @_; |
1266 | |
1391 | |
1267 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1392 | "$MAPDIR/$self->{path}.map" |
1268 | } |
1393 | } |
1269 | |
1394 | |
1270 | # the temporary/swap location |
1395 | # the temporary/swap location |
1271 | sub save_path { |
1396 | sub save_path { |
1272 | my ($self) = @_; |
1397 | my ($self) = @_; |
1273 | |
1398 | |
1274 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1399 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1275 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1400 | "$TMPDIR/$path.map" |
1276 | } |
1401 | } |
1277 | |
1402 | |
1278 | # the unique path, undef == no special unique path |
1403 | # the unique path, undef == no special unique path |
1279 | sub uniq_path { |
1404 | sub uniq_path { |
1280 | my ($self) = @_; |
1405 | my ($self) = @_; |
1281 | |
1406 | |
1282 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1407 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1283 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1408 | "$UNIQUEDIR/$path" |
1284 | } |
1409 | } |
1285 | |
1410 | |
1286 | # and all this just because we cannot iterate over |
1411 | # and all this just because we cannot iterate over |
1287 | # all maps in C++... |
1412 | # all maps in C++... |
1288 | sub change_all_map_light { |
1413 | sub change_all_map_light { |
… | |
… | |
1355 | $self->load_header_orig |
1480 | $self->load_header_orig |
1356 | or return; |
1481 | or return; |
1357 | $self->prepare_orig; |
1482 | $self->prepare_orig; |
1358 | } |
1483 | } |
1359 | |
1484 | |
|
|
1485 | $self->default_region (cf::region::find_by_path $self->{path}) |
|
|
1486 | unless $self->default_region; |
|
|
1487 | |
1360 | 1 |
1488 | 1 |
1361 | } |
1489 | } |
1362 | |
1490 | |
1363 | sub find; |
1491 | sub find; |
1364 | sub find { |
1492 | sub find { |
… | |
… | |
1397 | my ($self) = @_; |
1525 | my ($self) = @_; |
1398 | |
1526 | |
1399 | local $self->{deny_reset} = 1; # loading can take a long time |
1527 | local $self->{deny_reset} = 1; # loading can take a long time |
1400 | |
1528 | |
1401 | my $path = $self->{path}; |
1529 | my $path = $self->{path}; |
|
|
1530 | |
|
|
1531 | { |
1402 | my $guard = cf::lock_acquire "map_load:$path"; |
1532 | my $guard = cf::lock_acquire "map_load:$path"; |
1403 | |
1533 | |
1404 | return if $self->in_memory != cf::MAP_SWAPPED; |
1534 | return if $self->in_memory != cf::MAP_SWAPPED; |
1405 | |
1535 | |
1406 | $self->in_memory (cf::MAP_LOADING); |
1536 | $self->in_memory (cf::MAP_LOADING); |
1407 | |
1537 | |
1408 | $self->alloc; |
1538 | $self->alloc; |
1409 | |
1539 | |
1410 | $self->pre_load; |
1540 | $self->pre_load; |
1411 | |
|
|
1412 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1413 | or return; |
|
|
1414 | |
|
|
1415 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1416 | if delete $self->{load_original}; |
|
|
1417 | |
|
|
1418 | if (my $uniq = $self->uniq_path) { |
|
|
1419 | utf8::encode $uniq; |
|
|
1420 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1421 | $self->clear_unique_items; |
|
|
1422 | $self->_load_objects ($uniq, 0); |
|
|
1423 | } |
|
|
1424 | } |
|
|
1425 | |
|
|
1426 | Coro::cede; |
|
|
1427 | |
|
|
1428 | # now do the right thing for maps |
|
|
1429 | $self->link_multipart_objects; |
|
|
1430 | |
|
|
1431 | unless ($self->{deny_activate}) { |
|
|
1432 | $self->decay_objects; |
|
|
1433 | $self->fix_auto_apply; |
|
|
1434 | $self->update_buttons; |
|
|
1435 | Coro::cede; |
1541 | Coro::cede; |
1436 | $self->set_darkness_map; |
1542 | |
|
|
1543 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1544 | or return; |
|
|
1545 | |
|
|
1546 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1547 | if delete $self->{load_original}; |
|
|
1548 | |
|
|
1549 | if (my $uniq = $self->uniq_path) { |
|
|
1550 | utf8::encode $uniq; |
|
|
1551 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1552 | $self->clear_unique_items; |
|
|
1553 | $self->_load_objects ($uniq, 0); |
|
|
1554 | } |
|
|
1555 | } |
|
|
1556 | |
|
|
1557 | Coro::cede; |
|
|
1558 | # now do the right thing for maps |
|
|
1559 | $self->link_multipart_objects; |
1437 | $self->difficulty ($self->estimate_difficulty) |
1560 | $self->difficulty ($self->estimate_difficulty) |
1438 | unless $self->difficulty; |
1561 | unless $self->difficulty; |
1439 | Coro::cede; |
1562 | Coro::cede; |
|
|
1563 | |
|
|
1564 | unless ($self->{deny_activate}) { |
|
|
1565 | $self->decay_objects; |
|
|
1566 | $self->fix_auto_apply; |
|
|
1567 | $self->update_buttons; |
|
|
1568 | Coro::cede; |
|
|
1569 | $self->set_darkness_map; |
|
|
1570 | Coro::cede; |
1440 | $self->activate; |
1571 | $self->activate; |
|
|
1572 | } |
|
|
1573 | |
|
|
1574 | $self->in_memory (cf::MAP_IN_MEMORY); |
1441 | } |
1575 | } |
1442 | |
1576 | |
1443 | $self->post_load; |
1577 | $self->post_load; |
1444 | |
|
|
1445 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
1446 | } |
1578 | } |
1447 | |
1579 | |
1448 | sub customise_for { |
1580 | sub customise_for { |
1449 | my ($self, $ob) = @_; |
1581 | my ($self, $ob) = @_; |
1450 | |
1582 | |
… | |
… | |
1641 | instantiated unique items. May block. |
1773 | instantiated unique items. May block. |
1642 | |
1774 | |
1643 | =cut |
1775 | =cut |
1644 | |
1776 | |
1645 | sub unique_maps() { |
1777 | sub unique_maps() { |
1646 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
1778 | my $files = aio_readdir $UNIQUEDIR |
1647 | or return; |
1779 | or return; |
1648 | |
1780 | |
1649 | my @paths; |
1781 | my @paths; |
1650 | |
1782 | |
1651 | for (@$files) { |
1783 | for (@$files) { |
… | |
… | |
1907 | |
2039 | |
1908 | $rmp->{random_seed} ||= $exit->random_seed; |
2040 | $rmp->{random_seed} ||= $exit->random_seed; |
1909 | |
2041 | |
1910 | my $data = cf::to_json $rmp; |
2042 | my $data = cf::to_json $rmp; |
1911 | my $md5 = Digest::MD5::md5_hex $data; |
2043 | my $md5 = Digest::MD5::md5_hex $data; |
1912 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
2044 | my $meta = "$RANDOMDIR/$md5.meta"; |
1913 | |
2045 | |
1914 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2046 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1915 | aio_write $fh, 0, (length $data), $data, 0; |
2047 | aio_write $fh, 0, (length $data), $data, 0; |
1916 | undef $fh; |
2048 | undef $fh; |
1917 | aio_rename "$meta~", $meta; |
2049 | aio_rename "$meta~", $meta; |
… | |
… | |
1926 | |
2058 | |
1927 | return unless $self->type == cf::PLAYER; |
2059 | return unless $self->type == cf::PLAYER; |
1928 | |
2060 | |
1929 | if ($exit->slaying eq "/!") { |
2061 | if ($exit->slaying eq "/!") { |
1930 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
2062 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
2063 | # the problem is that $exit might not survive long enough |
|
|
2064 | # so it needs to be done right now, right here |
1931 | cf::sync_job { prepare_random_map $exit }; |
2065 | cf::sync_job { prepare_random_map $exit }; |
1932 | } |
2066 | } |
1933 | |
2067 | |
1934 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2068 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1935 | my $hp = $exit->stats->hp; |
2069 | my $hp = $exit->stats->hp; |
… | |
… | |
1943 | $self->goto ($slaying, $hp, $sp); |
2077 | $self->goto ($slaying, $hp, $sp); |
1944 | |
2078 | |
1945 | 1; |
2079 | 1; |
1946 | }) { |
2080 | }) { |
1947 | $self->message ("Something went wrong deep within the crossfire server. " |
2081 | $self->message ("Something went wrong deep within the crossfire server. " |
1948 | . "I'll try to bring you back to the map you were before. " |
2082 | . "I'll try to bring you back to the map you were before. " |
1949 | . "Please report this to the dungeon master!", |
2083 | . "Please report this to the dungeon master!", |
1950 | cf::NDI_UNIQUE | cf::NDI_RED); |
2084 | cf::NDI_UNIQUE | cf::NDI_RED); |
1951 | |
2085 | |
1952 | warn "ERROR in enter_exit: $@"; |
2086 | warn "ERROR in enter_exit: $@"; |
1953 | $self->leave_link; |
2087 | $self->leave_link; |
1954 | } |
2088 | } |
1955 | })->prio (1); |
2089 | })->prio (1); |
… | |
… | |
1968 | |
2102 | |
1969 | sub cf::client::send_drawinfo { |
2103 | sub cf::client::send_drawinfo { |
1970 | my ($self, $text, $flags) = @_; |
2104 | my ($self, $text, $flags) = @_; |
1971 | |
2105 | |
1972 | utf8::encode $text; |
2106 | utf8::encode $text; |
1973 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
2107 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1974 | } |
2108 | } |
1975 | |
2109 | |
|
|
2110 | =item $client->ext_event ($type, %msg) |
|
|
2111 | |
|
|
2112 | Sends an exti event to the client. |
|
|
2113 | |
|
|
2114 | =cut |
|
|
2115 | |
|
|
2116 | sub cf::client::ext_event($$%) { |
|
|
2117 | my ($self, $type, %msg) = @_; |
|
|
2118 | |
|
|
2119 | $msg{msgtype} = "event_$type"; |
|
|
2120 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2121 | } |
1976 | |
2122 | |
1977 | =item $success = $client->query ($flags, "text", \&cb) |
2123 | =item $success = $client->query ($flags, "text", \&cb) |
1978 | |
2124 | |
1979 | Queues a query to the client, calling the given callback with |
2125 | Queues a query to the client, calling the given callback with |
1980 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2126 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2077 | |
2223 | |
2078 | # here we export the classes and methods available to script code |
2224 | # here we export the classes and methods available to script code |
2079 | |
2225 | |
2080 | =pod |
2226 | =pod |
2081 | |
2227 | |
2082 | The following fucntions and emthods are available within a safe environment: |
2228 | The following functions and methods are available within a safe environment: |
2083 | |
2229 | |
2084 | cf::object contr pay_amount pay_player map |
2230 | cf::object contr pay_amount pay_player map |
2085 | cf::object::player player |
2231 | cf::object::player player |
2086 | cf::player peaceful |
2232 | cf::player peaceful |
2087 | cf::map trigger |
2233 | cf::map trigger |
… | |
… | |
2172 | ############################################################################# |
2318 | ############################################################################# |
2173 | |
2319 | |
2174 | =head2 EXTENSION DATABASE SUPPORT |
2320 | =head2 EXTENSION DATABASE SUPPORT |
2175 | |
2321 | |
2176 | Crossfire maintains a very simple database for extension use. It can |
2322 | Crossfire maintains a very simple database for extension use. It can |
2177 | currently store anything that can be serialised using Storable, which |
2323 | currently store binary data only (use Compress::LZF::sfreeze_cr/sthaw to |
2178 | excludes objects. |
2324 | convert to/from binary). |
2179 | |
2325 | |
2180 | The parameter C<$family> should best start with the name of the extension |
2326 | The parameter C<$family> should best start with the name of the extension |
2181 | using it, it should be unique. |
2327 | using it, it should be unique. |
2182 | |
2328 | |
2183 | =over 4 |
2329 | =over 4 |
… | |
… | |
2206 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2352 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2207 | cf::cleanup "db_open(db): $!" if $!; |
2353 | cf::cleanup "db_open(db): $!" if $!; |
2208 | }; |
2354 | }; |
2209 | cf::cleanup "db_open(db): $@" if $@; |
2355 | cf::cleanup "db_open(db): $@" if $@; |
2210 | }; |
2356 | }; |
2211 | |
|
|
2212 | my $path = cf::localdir . "/database.pst"; |
|
|
2213 | if (stat $path) { |
|
|
2214 | cf::sync_job { |
|
|
2215 | my $pst = Storable::retrieve $path; |
|
|
2216 | |
|
|
2217 | cf::db_put (board => data => $pst->{board}); |
|
|
2218 | cf::db_put (guildrules => data => $pst->{guildrules}); |
|
|
2219 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2220 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2221 | |
|
|
2222 | unlink $path; |
|
|
2223 | }; |
|
|
2224 | } |
|
|
2225 | } |
2357 | } |
2226 | } |
2358 | } |
2227 | |
2359 | |
2228 | sub db_get($$) { |
2360 | sub db_get($$) { |
2229 | my $key = "$_[0]/$_[1]"; |
2361 | my $key = "$_[0]/$_[1]"; |
2230 | |
2362 | |
2231 | cf::sync_job { |
2363 | cf::sync_job { |
2232 | BDB::db_get $DB, undef, $key, my $data; |
2364 | BDB::db_get $DB, undef, $key, my $data; |
2233 | |
2365 | |
2234 | $! ? () |
2366 | $! ? () |
2235 | : Compress::LZF::sthaw $data |
2367 | : $data |
2236 | } |
2368 | } |
2237 | } |
2369 | } |
2238 | |
2370 | |
2239 | sub db_put($$$) { |
2371 | sub db_put($$$) { |
2240 | BDB::dbreq_pri 4; |
2372 | BDB::dbreq_pri 4; |
2241 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2373 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
2242 | } |
2374 | } |
|
|
2375 | |
|
|
2376 | =item cf::cache $id => [$paths...], $processversion => $process |
|
|
2377 | |
|
|
2378 | Generic caching function that returns the value of the resource $id, |
|
|
2379 | caching and regenerating as required. |
|
|
2380 | |
|
|
2381 | This function can block. |
|
|
2382 | |
|
|
2383 | =cut |
|
|
2384 | |
|
|
2385 | sub cache { |
|
|
2386 | my ($id, $src, $processversion, $process) = @_; |
|
|
2387 | |
|
|
2388 | my $meta = |
|
|
2389 | join "\x00", |
|
|
2390 | $processversion, |
|
|
2391 | map { |
|
|
2392 | aio_stat $_ |
|
|
2393 | and Carp::croak "$_: $!"; |
|
|
2394 | |
|
|
2395 | ($_, (stat _)[7,9]) |
|
|
2396 | } @$src; |
|
|
2397 | |
|
|
2398 | my $dbmeta = db_get cache => "$id/meta"; |
|
|
2399 | if ($dbmeta ne $meta) { |
|
|
2400 | # changed, we may need to process |
|
|
2401 | |
|
|
2402 | my @data; |
|
|
2403 | my $md5; |
|
|
2404 | |
|
|
2405 | for (0 .. $#$src) { |
|
|
2406 | 0 <= aio_load $src->[$_], $data[$_] |
|
|
2407 | or Carp::croak "$src->[$_]: $!"; |
|
|
2408 | } |
|
|
2409 | |
|
|
2410 | # if processing is expensive, check |
|
|
2411 | # checksum first |
|
|
2412 | if (1) { |
|
|
2413 | $md5 = |
|
|
2414 | join "\x00", |
|
|
2415 | $processversion, |
|
|
2416 | map { |
|
|
2417 | Coro::cede; |
|
|
2418 | ($src->[$_], Digest::MD5::md5_hex $data[$_]) |
|
|
2419 | } 0.. $#$src; |
|
|
2420 | |
|
|
2421 | |
|
|
2422 | my $dbmd5 = db_get cache => "$id/md5"; |
|
|
2423 | if ($dbmd5 eq $md5) { |
|
|
2424 | db_put cache => "$id/meta", $meta; |
|
|
2425 | |
|
|
2426 | return db_get cache => "$id/data"; |
|
|
2427 | } |
|
|
2428 | } |
|
|
2429 | |
|
|
2430 | my $t1 = Time::HiRes::time; |
|
|
2431 | my $data = $process->(\@data); |
|
|
2432 | my $t2 = Time::HiRes::time; |
|
|
2433 | |
|
|
2434 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
|
|
2435 | |
|
|
2436 | db_put cache => "$id/data", $data; |
|
|
2437 | db_put cache => "$id/md5" , $md5; |
|
|
2438 | db_put cache => "$id/meta", $meta; |
|
|
2439 | |
|
|
2440 | return $data; |
|
|
2441 | } |
|
|
2442 | |
|
|
2443 | db_get cache => "$id/data" |
|
|
2444 | } |
|
|
2445 | |
|
|
2446 | =item fork_call { }, $args |
|
|
2447 | |
|
|
2448 | Executes the given code block with the given arguments in a seperate |
|
|
2449 | process, returning the results. Everything must be serialisable with |
|
|
2450 | Coro::Storable. May, of course, block. Note that the executed sub may |
|
|
2451 | never block itself or use any form of Event handling. |
|
|
2452 | |
|
|
2453 | =cut |
|
|
2454 | |
|
|
2455 | sub fork_call(&@) { |
|
|
2456 | my ($cb, @args) = @_; |
|
|
2457 | |
|
|
2458 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
2459 | # or die "socketpair: $!"; |
|
|
2460 | pipe my $fh1, my $fh2 |
|
|
2461 | or die "pipe: $!"; |
|
|
2462 | |
|
|
2463 | if (my $pid = fork) { |
|
|
2464 | close $fh2; |
|
|
2465 | |
|
|
2466 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
|
|
2467 | $res = Coro::Storable::thaw $res; |
|
|
2468 | |
|
|
2469 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
|
|
2470 | |
|
|
2471 | die $$res unless "ARRAY" eq ref $res; |
|
|
2472 | |
|
|
2473 | return wantarray ? @$res : $res->[-1]; |
|
|
2474 | } else { |
|
|
2475 | reset_signals; |
|
|
2476 | local $SIG{__WARN__}; |
|
|
2477 | local $SIG{__DIE__}; |
|
|
2478 | eval { |
|
|
2479 | close $fh1; |
|
|
2480 | |
|
|
2481 | my @res = eval { $cb->(@args) }; |
|
|
2482 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
|
|
2483 | }; |
|
|
2484 | |
|
|
2485 | warn $@ if $@; |
|
|
2486 | _exit 0; |
|
|
2487 | } |
|
|
2488 | } |
|
|
2489 | |
|
|
2490 | |
2243 | |
2491 | |
2244 | ############################################################################# |
2492 | ############################################################################# |
2245 | # the server's init and main functions |
2493 | # the server's init and main functions |
2246 | |
2494 | |
2247 | sub init_resources { |
2495 | sub load_facedata($) { |
2248 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2496 | my ($path) = @_; |
|
|
2497 | |
|
|
2498 | warn "loading facedata from $path\n"; |
|
|
2499 | |
|
|
2500 | my $facedata; |
|
|
2501 | 0 < aio_load $path, $facedata |
|
|
2502 | or die "$path: $!"; |
|
|
2503 | |
|
|
2504 | $facedata = Coro::Storable::thaw $facedata; |
|
|
2505 | |
|
|
2506 | $facedata->{version} == 2 |
|
|
2507 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
|
|
2508 | |
|
|
2509 | { |
|
|
2510 | my $faces = $facedata->{faceinfo}; |
|
|
2511 | |
|
|
2512 | while (my ($face, $info) = each %$faces) { |
|
|
2513 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
2514 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
|
|
2515 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2516 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
|
|
2517 | Coro::cede; |
|
|
2518 | } |
|
|
2519 | |
|
|
2520 | while (my ($face, $info) = each %$faces) { |
|
|
2521 | next unless $info->{smooth}; |
|
|
2522 | my $idx = cf::face::find $face |
|
|
2523 | or next; |
|
|
2524 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2525 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
|
|
2526 | } else { |
|
|
2527 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
|
|
2528 | } |
|
|
2529 | Coro::cede; |
|
|
2530 | } |
|
|
2531 | } |
|
|
2532 | |
|
|
2533 | { |
|
|
2534 | my $anims = $facedata->{animinfo}; |
|
|
2535 | |
|
|
2536 | while (my ($anim, $info) = each %$anims) { |
|
|
2537 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2538 | Coro::cede; |
|
|
2539 | } |
|
|
2540 | |
|
|
2541 | cf::anim::invalidate_all; # d'oh |
|
|
2542 | } |
|
|
2543 | |
|
|
2544 | 1 |
|
|
2545 | } |
|
|
2546 | |
|
|
2547 | sub reload_regions { |
|
|
2548 | load_resource_file "$MAPDIR/regions" |
2249 | or die "unable to load regions file\n";#d# |
2549 | or die "unable to load regions file\n"; |
|
|
2550 | } |
|
|
2551 | |
|
|
2552 | sub reload_facedata { |
|
|
2553 | load_facedata "$DATADIR/facedata" |
|
|
2554 | or die "unable to load facedata\n"; |
|
|
2555 | } |
|
|
2556 | |
|
|
2557 | sub reload_archetypes { |
|
|
2558 | load_resource_file "$DATADIR/archetypes" |
|
|
2559 | or die "unable to load archetypes\n"; |
|
|
2560 | } |
|
|
2561 | |
|
|
2562 | sub reload_treasures { |
|
|
2563 | load_resource_file "$DATADIR/treasures" |
|
|
2564 | or die "unable to load treasurelists\n"; |
|
|
2565 | } |
|
|
2566 | |
|
|
2567 | sub reload_resources { |
|
|
2568 | warn "reloading resource files...\n"; |
|
|
2569 | |
|
|
2570 | reload_regions; |
|
|
2571 | reload_facedata; |
|
|
2572 | reload_archetypes; |
|
|
2573 | reload_treasures; |
|
|
2574 | |
|
|
2575 | warn "finished reloading resource files\n"; |
|
|
2576 | } |
|
|
2577 | |
|
|
2578 | sub init { |
|
|
2579 | reload_resources; |
2250 | } |
2580 | } |
2251 | |
2581 | |
2252 | sub cfg_load { |
2582 | sub cfg_load { |
2253 | open my $fh, "<:utf8", cf::confdir . "/config" |
2583 | open my $fh, "<:utf8", "$CONFDIR/config" |
2254 | or return; |
2584 | or return; |
2255 | |
2585 | |
2256 | local $/; |
2586 | local $/; |
2257 | *CFG = YAML::Syck::Load <$fh>; |
2587 | *CFG = YAML::Syck::Load <$fh>; |
2258 | |
2588 | |
… | |
… | |
2266 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2596 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2267 | and die "WARNING: m(un)lockall failed: $!\n"; |
2597 | and die "WARNING: m(un)lockall failed: $!\n"; |
2268 | }; |
2598 | }; |
2269 | warn $@ if $@; |
2599 | warn $@ if $@; |
2270 | } |
2600 | } |
2271 | } |
|
|
2272 | |
|
|
2273 | sub init { |
|
|
2274 | init_resources; |
|
|
2275 | } |
2601 | } |
2276 | |
2602 | |
2277 | sub main { |
2603 | sub main { |
2278 | # we must not ever block the main coroutine |
2604 | # we must not ever block the main coroutine |
2279 | local $Coro::idle = sub { |
2605 | local $Coro::idle = sub { |
… | |
… | |
2347 | |
2673 | |
2348 | warn Carp::longmess "post_cleanup backtrace" |
2674 | warn Carp::longmess "post_cleanup backtrace" |
2349 | if $make_core; |
2675 | if $make_core; |
2350 | } |
2676 | } |
2351 | |
2677 | |
2352 | sub reload() { |
2678 | sub do_reload_perl() { |
2353 | # can/must only be called in main |
2679 | # can/must only be called in main |
2354 | if ($Coro::current != $Coro::main) { |
2680 | if ($Coro::current != $Coro::main) { |
2355 | warn "can only reload from main coroutine"; |
2681 | warn "can only reload from main coroutine"; |
2356 | return; |
2682 | return; |
2357 | } |
2683 | } |
… | |
… | |
2382 | Coro::cede; |
2708 | Coro::cede; |
2383 | last unless IO::AIO::nreqs || BDB::nreqs; |
2709 | last unless IO::AIO::nreqs || BDB::nreqs; |
2384 | warn "iterate..."; |
2710 | warn "iterate..."; |
2385 | } |
2711 | } |
2386 | |
2712 | |
|
|
2713 | ++$RELOAD; |
|
|
2714 | |
2387 | warn "cancelling all extension coros"; |
2715 | warn "cancelling all extension coros"; |
2388 | $_->cancel for values %EXT_CORO; |
2716 | $_->cancel for values %EXT_CORO; |
2389 | %EXT_CORO = (); |
2717 | %EXT_CORO = (); |
2390 | |
2718 | |
2391 | warn "removing commands"; |
2719 | warn "removing commands"; |
… | |
… | |
2430 | Symbol::delete_package "safe::$_" |
2758 | Symbol::delete_package "safe::$_" |
2431 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2759 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2432 | |
2760 | |
2433 | warn "unloading cf.pm \"a bit\""; |
2761 | warn "unloading cf.pm \"a bit\""; |
2434 | delete $INC{"cf.pm"}; |
2762 | delete $INC{"cf.pm"}; |
|
|
2763 | delete $INC{"cf/pod.pm"}; |
2435 | |
2764 | |
2436 | # don't, removes xs symbols, too, |
2765 | # don't, removes xs symbols, too, |
2437 | # and global variables created in xs |
2766 | # and global variables created in xs |
2438 | #Symbol::delete_package __PACKAGE__; |
2767 | #Symbol::delete_package __PACKAGE__; |
2439 | |
2768 | |
… | |
… | |
2448 | |
2777 | |
2449 | warn "loading extensions"; |
2778 | warn "loading extensions"; |
2450 | cf::load_extensions; |
2779 | cf::load_extensions; |
2451 | |
2780 | |
2452 | warn "reattaching attachments to objects/players"; |
2781 | warn "reattaching attachments to objects/players"; |
2453 | _global_reattach; |
2782 | _global_reattach; # objects, sockets |
2454 | warn "reattaching attachments to maps"; |
2783 | warn "reattaching attachments to maps"; |
2455 | reattach $_ for values %MAP; |
2784 | reattach $_ for values %MAP; |
2456 | |
2785 | warn "reattaching attachments to players"; |
2457 | warn "loading reloadable resources"; |
2786 | reattach $_ for values %PLAYER; |
2458 | init_resources; |
|
|
2459 | |
2787 | |
2460 | warn "leaving sync_job"; |
2788 | warn "leaving sync_job"; |
2461 | |
2789 | |
2462 | 1 |
2790 | 1 |
2463 | } or do { |
2791 | } or do { |
… | |
… | |
2469 | warn "reloaded"; |
2797 | warn "reloaded"; |
2470 | }; |
2798 | }; |
2471 | |
2799 | |
2472 | our $RELOAD_WATCHER; # used only during reload |
2800 | our $RELOAD_WATCHER; # used only during reload |
2473 | |
2801 | |
|
|
2802 | sub reload_perl() { |
|
|
2803 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2804 | # coro crashes during coro_state_free->destroy here. |
|
|
2805 | |
|
|
2806 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2807 | reentrant => 0, |
|
|
2808 | after => 0, |
|
|
2809 | data => WF_AUTOCANCEL, |
|
|
2810 | cb => sub { |
|
|
2811 | do_reload_perl; |
|
|
2812 | undef $RELOAD_WATCHER; |
|
|
2813 | }, |
|
|
2814 | ); |
|
|
2815 | } |
|
|
2816 | |
2474 | register_command "reload" => sub { |
2817 | register_command "reload" => sub { |
2475 | my ($who, $arg) = @_; |
2818 | my ($who, $arg) = @_; |
2476 | |
2819 | |
2477 | if ($who->flag (FLAG_WIZ)) { |
2820 | if ($who->flag (FLAG_WIZ)) { |
2478 | $who->message ("reloading server."); |
2821 | $who->message ("reloading server."); |
2479 | |
2822 | async { reload_perl }; |
2480 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2481 | # coro crashes during coro_state_free->destroy here. |
|
|
2482 | |
|
|
2483 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2484 | reentrant => 0, |
|
|
2485 | after => 0, |
|
|
2486 | data => WF_AUTOCANCEL, |
|
|
2487 | cb => sub { |
|
|
2488 | reload; |
|
|
2489 | undef $RELOAD_WATCHER; |
|
|
2490 | }, |
|
|
2491 | ); |
|
|
2492 | } |
2823 | } |
2493 | }; |
2824 | }; |
2494 | |
2825 | |
2495 | unshift @INC, $LIBDIR; |
2826 | unshift @INC, $LIBDIR; |
2496 | |
2827 | |
2497 | my $bug_warning = 0; |
2828 | my $bug_warning = 0; |
|
|
2829 | |
|
|
2830 | our @WAIT_FOR_TICK; |
|
|
2831 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2832 | |
|
|
2833 | sub wait_for_tick { |
|
|
2834 | return unless $TICK_WATCHER->is_active; |
|
|
2835 | return if $Coro::current == $Coro::main; |
|
|
2836 | |
|
|
2837 | my $signal = new Coro::Signal; |
|
|
2838 | push @WAIT_FOR_TICK, $signal; |
|
|
2839 | $signal->wait; |
|
|
2840 | } |
|
|
2841 | |
|
|
2842 | sub wait_for_tick_begin { |
|
|
2843 | return unless $TICK_WATCHER->is_active; |
|
|
2844 | return if $Coro::current == $Coro::main; |
|
|
2845 | |
|
|
2846 | my $signal = new Coro::Signal; |
|
|
2847 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2848 | $signal->wait; |
|
|
2849 | } |
2498 | |
2850 | |
2499 | $TICK_WATCHER = Event->timer ( |
2851 | $TICK_WATCHER = Event->timer ( |
2500 | reentrant => 0, |
2852 | reentrant => 0, |
2501 | parked => 1, |
2853 | parked => 1, |
2502 | prio => 0, |
2854 | prio => 0, |
… | |
… | |
2507 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
2859 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
2508 | unless ++$bug_warning > 10; |
2860 | unless ++$bug_warning > 10; |
2509 | return; |
2861 | return; |
2510 | } |
2862 | } |
2511 | |
2863 | |
2512 | $NOW = Event::time; |
2864 | $NOW = $tick_start = Event::time; |
2513 | |
2865 | |
2514 | cf::server_tick; # one server iteration |
2866 | cf::server_tick; # one server iteration |
|
|
2867 | |
2515 | $RUNTIME += $TICK; |
2868 | $RUNTIME += $TICK; |
2516 | $NEXT_TICK += $TICK; |
2869 | $NEXT_TICK += $TICK; |
2517 | |
2870 | |
2518 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2871 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2519 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2872 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2521 | write_runtime |
2874 | write_runtime |
2522 | or warn "ERROR: unable to write runtime file: $!"; |
2875 | or warn "ERROR: unable to write runtime file: $!"; |
2523 | }; |
2876 | }; |
2524 | } |
2877 | } |
2525 | |
2878 | |
2526 | |
|
|
2527 | $WAIT_FOR_TICK->broadcast; |
|
|
2528 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2529 | |
|
|
2530 | # my $AFTER = Event::time; |
2879 | # my $AFTER = Event::time; |
2531 | # warn $AFTER - $NOW;#d# |
2880 | # warn $AFTER - $NOW;#d# |
2532 | |
2881 | |
|
|
2882 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2883 | $sig->send; |
|
|
2884 | } |
|
|
2885 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2886 | $sig->send; |
|
|
2887 | } |
|
|
2888 | |
|
|
2889 | $NOW = Event::time; |
|
|
2890 | |
2533 | # if we are delayed by four ticks or more, skip them all |
2891 | # if we are delayed by four ticks or more, skip them all |
2534 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2892 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
2535 | |
2893 | |
2536 | $TICK_WATCHER->at ($NEXT_TICK); |
2894 | $TICK_WATCHER->at ($NEXT_TICK); |
2537 | $TICK_WATCHER->start; |
2895 | $TICK_WATCHER->start; |
|
|
2896 | |
|
|
2897 | $LOAD = ($NOW - $tick_start) / $TICK; |
|
|
2898 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
|
|
2899 | |
|
|
2900 | _post_tick; |
|
|
2901 | |
|
|
2902 | |
2538 | }, |
2903 | }, |
2539 | ); |
2904 | ); |
2540 | |
2905 | |
2541 | { |
2906 | { |
2542 | BDB::max_poll_time $TICK * 0.1; |
2907 | BDB::max_poll_time $TICK * 0.1; |
… | |
… | |
2570 | |
2935 | |
2571 | cf::sync_job { |
2936 | cf::sync_job { |
2572 | eval { |
2937 | eval { |
2573 | BDB::db_env_open |
2938 | BDB::db_env_open |
2574 | $DB_ENV, |
2939 | $DB_ENV, |
2575 | $BDB_ENV_DIR, |
2940 | $BDBDIR, |
2576 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2941 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2577 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2942 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2578 | 0666; |
2943 | 0666; |
2579 | |
2944 | |
2580 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
2945 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
2581 | |
2946 | |
2582 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2947 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2583 | $DB_ENV->set_lk_detect; |
2948 | $DB_ENV->set_lk_detect; |
2584 | }; |
2949 | }; |
2585 | |
2950 | |
… | |
… | |
2601 | prio => 6, |
2966 | prio => 6, |
2602 | cb => \&IO::AIO::poll_cb, |
2967 | cb => \&IO::AIO::poll_cb, |
2603 | ); |
2968 | ); |
2604 | } |
2969 | } |
2605 | |
2970 | |
|
|
2971 | my $_log_backtrace; |
|
|
2972 | |
|
|
2973 | sub _log_backtrace { |
|
|
2974 | my ($msg, @addr) = @_; |
|
|
2975 | |
|
|
2976 | $msg =~ s/\n//; |
|
|
2977 | |
|
|
2978 | # limit the # of concurrent backtraces |
|
|
2979 | if ($_log_backtrace < 2) { |
|
|
2980 | ++$_log_backtrace; |
|
|
2981 | async { |
|
|
2982 | my @bt = fork_call { |
|
|
2983 | @addr = map { sprintf "%x", $_ } @addr; |
|
|
2984 | my $self = (-f "/proc/$$/exe") ? "/proc/$$/exe" : $^X; |
|
|
2985 | open my $fh, "exec addr2line -C -f -i -e \Q$self\E @addr 2>&1 |" |
|
|
2986 | or die "addr2line: $!"; |
|
|
2987 | |
|
|
2988 | my @funcs; |
|
|
2989 | my @res = <$fh>; |
|
|
2990 | chomp for @res; |
|
|
2991 | while (@res) { |
|
|
2992 | my ($func, $line) = splice @res, 0, 2, (); |
|
|
2993 | push @funcs, "[$func] $line"; |
|
|
2994 | } |
|
|
2995 | |
|
|
2996 | @funcs |
|
|
2997 | }; |
|
|
2998 | |
|
|
2999 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
3000 | LOG llevInfo, "[ABT] $_\n" for @bt; |
|
|
3001 | --$_log_backtrace; |
|
|
3002 | }; |
|
|
3003 | } else { |
|
|
3004 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
3005 | LOG llevInfo, "[ABT] [suppressed]\n"; |
|
|
3006 | } |
|
|
3007 | } |
|
|
3008 | |
|
|
3009 | # load additional modules |
|
|
3010 | use cf::pod; |
|
|
3011 | |
2606 | END { cf::emergency_save } |
3012 | END { cf::emergency_save } |
2607 | |
3013 | |
2608 | 1 |
3014 | 1 |
2609 | |
3015 | |