1 | # |
1 | # |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | # |
3 | # |
4 | # Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
5 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
7 | # the terms of the Affero GNU General Public License as published by the |
7 | # the terms of the Affero GNU General Public License as published by the |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
9 | # option) any later version. |
9 | # option) any later version. |
… | |
… | |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
21 | # |
21 | # |
22 | |
22 | |
23 | package cf; |
23 | package cf; |
24 | |
24 | |
25 | use 5.10.0; |
25 | use common::sense; |
26 | use utf8; |
|
|
27 | use strict qw(vars subs); |
|
|
28 | |
26 | |
29 | use Symbol; |
27 | use Symbol; |
30 | use List::Util; |
28 | use List::Util; |
31 | use Socket; |
29 | use Socket; |
32 | use EV; |
30 | use EV; |
… | |
… | |
78 | # strictly for debugging |
76 | # strictly for debugging |
79 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
80 | |
78 | |
81 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
82 | |
80 | |
|
|
81 | our @ORIG_INC; |
|
|
82 | |
83 | our %COMMAND = (); |
83 | our %COMMAND = (); |
84 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
85 | |
85 | |
86 | our @EXTS = (); # list of extension package names |
86 | our @EXTS = (); # list of extension package names |
87 | our %EXTCMD = (); |
87 | our %EXTCMD = (); |
… | |
… | |
93 | our @EVENT; |
93 | our @EVENT; |
94 | our @REFLECT; # set by XS |
94 | our @REFLECT; # set by XS |
95 | our %REFLECT; # set by us |
95 | our %REFLECT; # set by us |
96 | |
96 | |
97 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
|
|
98 | |
98 | our $DATADIR = datadir; |
99 | our $DATADIR = datadir; |
99 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $PODDIR = "$DATADIR/pod"; |
101 | our $PODDIR = "$DATADIR/pod"; |
101 | our $MAPDIR = "$DATADIR/" . mapdir; |
102 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
103 | |
102 | our $LOCALDIR = localdir; |
104 | our $LOCALDIR = localdir; |
103 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
105 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
104 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
106 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
105 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
107 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
109 | our $BDBDIR = "$LOCALDIR/db"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
110 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
111 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
112 | |
111 | our %RESOURCE; |
113 | #our %RESOURCE; # unused |
112 | |
114 | |
113 | our $OUTPUT_RATE_MIN = 3000; |
115 | our $OUTPUT_RATE_MIN = 3000; |
114 | our $OUTPUT_RATE_MAX = 1000000; |
116 | our $OUTPUT_RATE_MAX = 1000000; |
115 | |
117 | |
116 | our $MAX_LINKS = 32; # how many chained exits to follow |
118 | our $MAX_LINKS = 32; # how many chained exits to follow |
117 | our $VERBOSE_IO = 0;#d# |
119 | our $VERBOSE_IO = 1; |
118 | |
120 | |
119 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
121 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
120 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
122 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
121 | our $NEXT_TICK; |
123 | our $NEXT_TICK; |
122 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
124 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
… | |
… | |
124 | our $BDB_DEADLOCK_WATCHER; |
126 | our $BDB_DEADLOCK_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
127 | our $BDB_CHECKPOINT_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
128 | our $BDB_TRICKLE_WATCHER; |
127 | our $DB_ENV; |
129 | our $DB_ENV; |
128 | |
130 | |
129 | our @EXTRA_MODULES = qw(pod match mapscript); |
131 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
130 | |
132 | |
131 | our %CFG; |
133 | our %CFG; |
132 | |
134 | |
133 | our $UPTIME; $UPTIME ||= time; |
135 | our $UPTIME; $UPTIME ||= time; |
134 | our $RUNTIME; |
136 | our $RUNTIME = 0; |
|
|
137 | our $SERVER_TICK = 0; |
135 | our $NOW; |
138 | our $NOW; |
136 | |
139 | |
137 | our (%PLAYER, %PLAYER_LOADING); # all users |
140 | our (%PLAYER, %PLAYER_LOADING); # all users |
138 | our (%MAP, %MAP_LOADING ); # all maps |
141 | our (%MAP, %MAP_LOADING ); # all maps |
139 | our $LINK_MAP; # the special {link} map, which is always available |
142 | our $LINK_MAP; # the special {link} map, which is always available |
… | |
… | |
148 | |
151 | |
149 | our @POST_INIT; |
152 | our @POST_INIT; |
150 | |
153 | |
151 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
154 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
152 | our $REALLY_UNLOOP; # never set to true, please :) |
155 | our $REALLY_UNLOOP; # never set to true, please :) |
|
|
156 | |
|
|
157 | our $WAIT_FOR_TICK = new Coro::Signal; |
|
|
158 | our @WAIT_FOR_TICK_BEGIN; |
153 | |
159 | |
154 | binmode STDOUT; |
160 | binmode STDOUT; |
155 | binmode STDERR; |
161 | binmode STDERR; |
156 | |
162 | |
157 | # read virtual server time, if available |
163 | # read virtual server time, if available |
… | |
… | |
191 | |
197 | |
192 | =over 4 |
198 | =over 4 |
193 | |
199 | |
194 | =item $cf::UPTIME |
200 | =item $cf::UPTIME |
195 | |
201 | |
196 | The timestamp of the server start (so not actually an uptime). |
202 | The timestamp of the server start (so not actually an "uptime"). |
|
|
203 | |
|
|
204 | =item $cf::SERVER_TICK |
|
|
205 | |
|
|
206 | An unsigned integer that starts at zero when the server is started and is |
|
|
207 | incremented on every tick. |
|
|
208 | |
|
|
209 | =item $cf::NOW |
|
|
210 | |
|
|
211 | The (real) time of the last (current) server tick - updated before and |
|
|
212 | after tick processing, so this is useful only as a rough "what time is it |
|
|
213 | now" estimate. |
|
|
214 | |
|
|
215 | =item $cf::TICK |
|
|
216 | |
|
|
217 | The interval between each server tick, in seconds. |
197 | |
218 | |
198 | =item $cf::RUNTIME |
219 | =item $cf::RUNTIME |
199 | |
220 | |
200 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
221 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
201 | every server tick. |
222 | every server tick. |
… | |
… | |
207 | Various directories - "/etc", read-only install directory, perl-library |
228 | Various directories - "/etc", read-only install directory, perl-library |
208 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
229 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
209 | unique-items directory, player file directory, random maps directory and |
230 | unique-items directory, player file directory, random maps directory and |
210 | database environment. |
231 | database environment. |
211 | |
232 | |
212 | =item $cf::NOW |
|
|
213 | |
|
|
214 | The time of the last (current) server tick. |
|
|
215 | |
|
|
216 | =item $cf::TICK |
|
|
217 | |
|
|
218 | The interval between server ticks, in seconds. |
|
|
219 | |
|
|
220 | =item $cf::LOADAVG |
233 | =item $cf::LOADAVG |
221 | |
234 | |
222 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
235 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
223 | (none) and 1 (overloaded), indicating how much time is spent on processing |
236 | (none) and 1 (overloaded), indicating how much time is spent on processing |
224 | objects per tick. Healthy values are < 0.5. |
237 | objects per tick. Healthy values are < 0.5. |
… | |
… | |
233 | from wherever your confdir points to. |
246 | from wherever your confdir points to. |
234 | |
247 | |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
248 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
236 | |
249 | |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
250 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
238 | returns directly I<after> the tick processing (and consequently, can only wake one process |
251 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
252 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
253 | |
|
|
254 | Note that cf::Wait_for_tick will immediately return when the server is not |
|
|
255 | ticking, making it suitable for small pauses in threads that need to run |
|
|
256 | when the server is paused. If that is not applicable (i.e. you I<really> |
|
|
257 | want to wait, use C<$cf::WAIT_FOR_TICK>). |
|
|
258 | |
|
|
259 | =item $cf::WAIT_FOR_TICK |
|
|
260 | |
|
|
261 | Note that C<cf::wait_for_tick> is probably the correct thing to use. This |
|
|
262 | variable contains a L<Coro::Signal> that is broadcats after every server |
|
|
263 | tick. Calling C<< ->wait >> on it will suspend the caller until after the |
|
|
264 | next server tick. |
|
|
265 | |
|
|
266 | =cut |
|
|
267 | |
|
|
268 | sub wait_for_tick(); |
|
|
269 | sub wait_for_tick_begin(); |
240 | |
270 | |
241 | =item @cf::INVOKE_RESULTS |
271 | =item @cf::INVOKE_RESULTS |
242 | |
272 | |
243 | This array contains the results of the last C<invoke ()> call. When |
273 | This array contains the results of the last C<invoke ()> call. When |
244 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
274 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
… | |
… | |
252 | |
282 | |
253 | =back |
283 | =back |
254 | |
284 | |
255 | =cut |
285 | =cut |
256 | |
286 | |
|
|
287 | sub error(@) { LOG llevError, join "", @_ } |
|
|
288 | sub warn (@) { LOG llevWarn , join "", @_ } |
|
|
289 | sub info (@) { LOG llevInfo , join "", @_ } |
|
|
290 | sub debug(@) { LOG llevDebug, join "", @_ } |
|
|
291 | sub trace(@) { LOG llevTrace, join "", @_ } |
|
|
292 | |
257 | $Coro::State::WARNHOOK = sub { |
293 | $Coro::State::WARNHOOK = sub { |
258 | my $msg = join "", @_; |
294 | my $msg = join "", @_; |
259 | |
295 | |
260 | $msg .= "\n" |
296 | $msg .= "\n" |
261 | unless $msg =~ /\n$/; |
297 | unless $msg =~ /\n$/; |
262 | |
298 | |
263 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
299 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
264 | |
300 | |
265 | LOG llevError, $msg; |
301 | LOG llevWarn, $msg; |
266 | }; |
302 | }; |
267 | |
303 | |
268 | $Coro::State::DIEHOOK = sub { |
304 | $Coro::State::DIEHOOK = sub { |
269 | return unless $^S eq 0; # "eq", not "==" |
305 | return unless $^S eq 0; # "eq", not "==" |
270 | |
306 | |
271 | warn Carp::longmess $_[0]; |
307 | error Carp::longmess $_[0]; |
272 | |
308 | |
273 | if (in_main) {#d# |
309 | if (in_main) {#d# |
274 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
310 | error "DIEHOOK called in main context, Coro bug?\n";#d# |
275 | return;#d# |
311 | return;#d# |
276 | }#d# |
312 | }#d# |
277 | |
313 | |
278 | # kill coroutine otherwise |
314 | # kill coroutine otherwise |
279 | Coro::terminate |
315 | Coro::terminate |
… | |
… | |
301 | } |
337 | } |
302 | |
338 | |
303 | $EV::DIED = sub { |
339 | $EV::DIED = sub { |
304 | Carp::cluck "error in event callback: @_"; |
340 | Carp::cluck "error in event callback: @_"; |
305 | }; |
341 | }; |
|
|
342 | |
|
|
343 | ############################################################################# |
|
|
344 | |
|
|
345 | sub fork_call(&@); |
|
|
346 | sub get_slot($;$$); |
306 | |
347 | |
307 | ############################################################################# |
348 | ############################################################################# |
308 | |
349 | |
309 | =head2 UTILITY FUNCTIONS |
350 | =head2 UTILITY FUNCTIONS |
310 | |
351 | |
… | |
… | |
331 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
372 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
332 | $d |
373 | $d |
333 | } || "[unable to dump $_[0]: '$@']"; |
374 | } || "[unable to dump $_[0]: '$@']"; |
334 | } |
375 | } |
335 | |
376 | |
|
|
377 | =item $scalar = cf::load_file $path |
|
|
378 | |
|
|
379 | Loads the given file from path and returns its contents. Croaks on error |
|
|
380 | and can block. |
|
|
381 | |
|
|
382 | =cut |
|
|
383 | |
|
|
384 | sub load_file($) { |
|
|
385 | 0 <= aio_load $_[0], my $data |
|
|
386 | or Carp::croak "$_[0]: $!"; |
|
|
387 | |
|
|
388 | $data |
|
|
389 | } |
|
|
390 | |
|
|
391 | =item $success = cf::replace_file $path, $data, $sync |
|
|
392 | |
|
|
393 | Atomically replaces the file at the given $path with new $data, and |
|
|
394 | optionally $sync the data to disk before replacing the file. |
|
|
395 | |
|
|
396 | =cut |
|
|
397 | |
|
|
398 | sub replace_file($$;$) { |
|
|
399 | my ($path, $data, $sync) = @_; |
|
|
400 | |
|
|
401 | my $lock = cf::lock_acquire ("replace_file:$path"); |
|
|
402 | |
|
|
403 | my $fh = aio_open "$path~", Fcntl::O_WRONLY | Fcntl::O_CREAT | Fcntl::O_TRUNC, 0644 |
|
|
404 | or return; |
|
|
405 | |
|
|
406 | $data = $data->() if ref $data; |
|
|
407 | |
|
|
408 | length $data == aio_write $fh, 0, (length $data), $data, 0 |
|
|
409 | or return; |
|
|
410 | |
|
|
411 | !$sync |
|
|
412 | or !aio_fsync $fh |
|
|
413 | or return; |
|
|
414 | |
|
|
415 | aio_close $fh |
|
|
416 | and return; |
|
|
417 | |
|
|
418 | aio_rename "$path~", $path |
|
|
419 | and return; |
|
|
420 | |
|
|
421 | if ($sync) { |
|
|
422 | $path =~ s%/[^/]*$%%; |
|
|
423 | aio_pathsync $path; |
|
|
424 | } |
|
|
425 | |
|
|
426 | 1 |
|
|
427 | } |
|
|
428 | |
336 | =item $ref = cf::decode_json $json |
429 | =item $ref = cf::decode_json $json |
337 | |
430 | |
338 | Converts a JSON string into the corresponding perl data structure. |
431 | Converts a JSON string into the corresponding perl data structure. |
339 | |
432 | |
340 | =item $json = cf::encode_json $ref |
433 | =item $json = cf::encode_json $ref |
… | |
… | |
346 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
439 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
347 | |
440 | |
348 | sub encode_json($) { $json_coder->encode ($_[0]) } |
441 | sub encode_json($) { $json_coder->encode ($_[0]) } |
349 | sub decode_json($) { $json_coder->decode ($_[0]) } |
442 | sub decode_json($) { $json_coder->decode ($_[0]) } |
350 | |
443 | |
|
|
444 | =item $ref = cf::decode_storable $scalar |
|
|
445 | |
|
|
446 | Same as Coro::Storable::thaw, so blocks. |
|
|
447 | |
|
|
448 | =cut |
|
|
449 | |
|
|
450 | BEGIN { *decode_storable = \&Coro::Storable::thaw } |
|
|
451 | |
|
|
452 | =item $ref = cf::decode_yaml $scalar |
|
|
453 | |
|
|
454 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
455 | |
|
|
456 | =cut |
|
|
457 | |
|
|
458 | sub decode_yaml($) { |
|
|
459 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
460 | } |
|
|
461 | |
|
|
462 | =item $scalar = cf::unlzf $scalar |
|
|
463 | |
|
|
464 | Same as Compress::LZF::compress, but takes server ticks into account, so |
|
|
465 | blocks. |
|
|
466 | |
|
|
467 | =cut |
|
|
468 | |
|
|
469 | sub unlzf($) { |
|
|
470 | # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine) |
|
|
471 | cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf"; |
|
|
472 | Compress::LZF::decompress $_[0] |
|
|
473 | } |
|
|
474 | |
351 | =item cf::post_init { BLOCK } |
475 | =item cf::post_init { BLOCK } |
352 | |
476 | |
353 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
477 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
354 | but I<before> the server starts ticking again. |
478 | but I<before> the server starts ticking again. |
355 | |
479 | |
356 | The cdoeblock will have a single boolean argument to indicate whether this |
480 | The codeblock will have a single boolean argument to indicate whether this |
357 | is a reload or not. |
481 | is a reload or not. |
358 | |
482 | |
359 | =cut |
483 | =cut |
360 | |
484 | |
361 | sub post_init(&) { |
485 | sub post_init(&) { |
362 | push @POST_INIT, shift; |
486 | push @POST_INIT, shift; |
|
|
487 | } |
|
|
488 | |
|
|
489 | sub _post_init { |
|
|
490 | trace "running post_init jobs"; |
|
|
491 | |
|
|
492 | # run them in parallel... |
|
|
493 | |
|
|
494 | my @join; |
|
|
495 | |
|
|
496 | while () { |
|
|
497 | push @join, map &Coro::async ($_, 0), @POST_INIT; |
|
|
498 | @POST_INIT = (); |
|
|
499 | |
|
|
500 | @join or last; |
|
|
501 | |
|
|
502 | (pop @join)->join; |
|
|
503 | } |
363 | } |
504 | } |
364 | |
505 | |
365 | =item cf::lock_wait $string |
506 | =item cf::lock_wait $string |
366 | |
507 | |
367 | Wait until the given lock is available. See cf::lock_acquire. |
508 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
420 | EV::periodic $start, $interval, 0, $cb |
561 | EV::periodic $start, $interval, 0, $cb |
421 | } |
562 | } |
422 | |
563 | |
423 | =item cf::get_slot $time[, $priority[, $name]] |
564 | =item cf::get_slot $time[, $priority[, $name]] |
424 | |
565 | |
425 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
566 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
426 | This call blocks and returns only when you have at least C<$time> seconds |
567 | (default: 0): This call blocks and returns only when you have at least |
427 | of cpu time till the next tick. The slot is only valid till the next cede. |
568 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
|
|
569 | till the next cede. |
|
|
570 | |
|
|
571 | Background jobs should use a priority les than zero, interactive jobs |
|
|
572 | should use 100 or more. |
428 | |
573 | |
429 | The optional C<$name> can be used to identify the job to run. It might be |
574 | The optional C<$name> can be used to identify the job to run. It might be |
430 | used for statistical purposes and should identify the same time-class. |
575 | used for statistical purposes and should identify the same time-class. |
431 | |
576 | |
432 | Useful for short background jobs. |
577 | Useful for short background jobs. |
… | |
… | |
461 | } |
606 | } |
462 | } |
607 | } |
463 | |
608 | |
464 | if (@SLOT_QUEUE) { |
609 | if (@SLOT_QUEUE) { |
465 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
610 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
466 | push @cf::WAIT_FOR_TICK, $signal; |
611 | $WAIT_FOR_TICK->wait; |
467 | $signal->wait; |
|
|
468 | } else { |
612 | } else { |
469 | $busy = 0; |
613 | $busy = 0; |
470 | Coro::schedule; |
614 | Coro::schedule; |
471 | } |
615 | } |
472 | } |
616 | } |
… | |
… | |
512 | =cut |
656 | =cut |
513 | |
657 | |
514 | sub sync_job(&) { |
658 | sub sync_job(&) { |
515 | my ($job) = @_; |
659 | my ($job) = @_; |
516 | |
660 | |
517 | if ($Coro::current == $Coro::main) { |
661 | if (in_main) { |
518 | my $time = AE::time; |
662 | my $time = AE::time; |
519 | |
663 | |
520 | # this is the main coro, too bad, we have to block |
664 | # this is the main coro, too bad, we have to block |
521 | # till the operation succeeds, freezing the server :/ |
665 | # till the operation succeeds, freezing the server :/ |
522 | |
666 | |
523 | LOG llevError, Carp::longmess "sync job";#d# |
667 | #LOG llevError, Carp::longmess "sync job";#d# |
524 | |
668 | |
525 | my $freeze_guard = freeze_mainloop; |
669 | my $freeze_guard = freeze_mainloop; |
526 | |
670 | |
527 | my $busy = 1; |
671 | my $busy = 1; |
528 | my @res; |
672 | my @res; |
529 | |
673 | |
530 | (async { |
674 | (async { |
531 | $Coro::current->desc ("sync job coro"); |
675 | $Coro::current->desc ("sync job coro"); |
532 | @res = eval { $job->() }; |
676 | @res = eval { $job->() }; |
533 | warn $@ if $@; |
677 | error $@ if $@; |
534 | undef $busy; |
678 | undef $busy; |
535 | })->prio (Coro::PRIO_MAX); |
679 | })->prio (Coro::PRIO_MAX); |
536 | |
680 | |
537 | while ($busy) { |
681 | while ($busy) { |
538 | if (Coro::nready) { |
682 | if (Coro::nready) { |
… | |
… | |
572 | $EXT_CORO{$coro+0} = $coro; |
716 | $EXT_CORO{$coro+0} = $coro; |
573 | |
717 | |
574 | $coro |
718 | $coro |
575 | } |
719 | } |
576 | |
720 | |
577 | =item fork_call { }, $args |
721 | =item fork_call { }, @args |
578 | |
722 | |
579 | Executes the given code block with the given arguments in a seperate |
723 | Executes the given code block with the given arguments in a seperate |
580 | process, returning the results. Everything must be serialisable with |
724 | process, returning the results. Everything must be serialisable with |
581 | Coro::Storable. May, of course, block. Note that the executed sub may |
725 | Coro::Storable. May, of course, block. Note that the executed sub may |
582 | never block itself or use any form of event handling. |
726 | never block itself or use any form of event handling. |
583 | |
727 | |
584 | =cut |
728 | =cut |
585 | |
729 | |
|
|
730 | sub post_fork { |
|
|
731 | reset_signals; |
|
|
732 | } |
|
|
733 | |
586 | sub fork_call(&@) { |
734 | sub fork_call(&@) { |
587 | my ($cb, @args) = @_; |
735 | my ($cb, @args) = @_; |
588 | |
736 | |
589 | # we seemingly have to make a local copy of the whole thing, |
737 | # we seemingly have to make a local copy of the whole thing, |
590 | # otherwise perl prematurely frees the stuff :/ |
738 | # otherwise perl prematurely frees the stuff :/ |
591 | # TODO: investigate and fix (likely this will be rather laborious) |
739 | # TODO: investigate and fix (likely this will be rather laborious) |
592 | |
740 | |
593 | my @res = Coro::Util::fork_eval { |
741 | my @res = Coro::Util::fork_eval { |
594 | reset_signals; |
742 | cf::post_fork; |
595 | &$cb |
743 | &$cb |
596 | }, @args; |
744 | } @args; |
597 | |
745 | |
598 | wantarray ? @res : $res[-1] |
746 | wantarray ? @res : $res[-1] |
599 | } |
747 | } |
600 | |
748 | |
601 | sub objinfo { |
749 | sub objinfo { |
… | |
… | |
651 | within each server. |
799 | within each server. |
652 | |
800 | |
653 | =cut |
801 | =cut |
654 | |
802 | |
655 | sub db_table($) { |
803 | sub db_table($) { |
|
|
804 | cf::error "db_get called from main context" |
|
|
805 | if $Coro::current == $Coro::main; |
|
|
806 | |
656 | my ($name) = @_; |
807 | my ($name) = @_; |
657 | my $db = BDB::db_create $DB_ENV; |
808 | my $db = BDB::db_create $DB_ENV; |
658 | |
809 | |
659 | eval { |
810 | eval { |
660 | $db->set_flags (BDB::CHKSUM); |
811 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
670 | } |
821 | } |
671 | |
822 | |
672 | our $DB; |
823 | our $DB; |
673 | |
824 | |
674 | sub db_init { |
825 | sub db_init { |
675 | cf::sync_job { |
|
|
676 | $DB ||= db_table "db"; |
826 | $DB ||= db_table "db"; |
677 | }; |
|
|
678 | } |
827 | } |
679 | |
828 | |
680 | sub db_get($$) { |
829 | sub db_get($$) { |
681 | my $key = "$_[0]/$_[1]"; |
830 | my $key = "$_[0]/$_[1]"; |
682 | |
831 | |
683 | cf::sync_job { |
832 | cf::error "db_get called from main context" |
|
|
833 | if $Coro::current == $Coro::main; |
|
|
834 | |
684 | BDB::db_get $DB, undef, $key, my $data; |
835 | BDB::db_get $DB, undef, $key, my $data; |
685 | |
836 | |
686 | $! ? () |
837 | $! ? () |
687 | : $data |
838 | : $data |
688 | } |
|
|
689 | } |
839 | } |
690 | |
840 | |
691 | sub db_put($$$) { |
841 | sub db_put($$$) { |
692 | BDB::dbreq_pri 4; |
842 | BDB::dbreq_pri 4; |
693 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
843 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
721 | |
871 | |
722 | my @data; |
872 | my @data; |
723 | my $md5; |
873 | my $md5; |
724 | |
874 | |
725 | for (0 .. $#$src) { |
875 | for (0 .. $#$src) { |
726 | 0 <= aio_load $src->[$_], $data[$_] |
876 | $data[$_] = load_file $src->[$_]; |
727 | or Carp::croak "$src->[$_]: $!"; |
|
|
728 | } |
877 | } |
729 | |
878 | |
730 | # if processing is expensive, check |
879 | # if processing is expensive, check |
731 | # checksum first |
880 | # checksum first |
732 | if (1) { |
881 | if (1) { |
… | |
… | |
749 | |
898 | |
750 | my $t1 = Time::HiRes::time; |
899 | my $t1 = Time::HiRes::time; |
751 | my $data = $process->(\@data); |
900 | my $data = $process->(\@data); |
752 | my $t2 = Time::HiRes::time; |
901 | my $t2 = Time::HiRes::time; |
753 | |
902 | |
754 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
903 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
755 | |
904 | |
756 | db_put cache => "$id/data", $data; |
905 | db_put cache => "$id/data", $data; |
757 | db_put cache => "$id/md5" , $md5; |
906 | db_put cache => "$id/md5" , $md5; |
758 | db_put cache => "$id/meta", $meta; |
907 | db_put cache => "$id/meta", $meta; |
759 | |
908 | |
… | |
… | |
769 | |
918 | |
770 | =cut |
919 | =cut |
771 | |
920 | |
772 | sub datalog($@) { |
921 | sub datalog($@) { |
773 | my ($type, %kv) = @_; |
922 | my ($type, %kv) = @_; |
774 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
923 | info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
775 | } |
924 | } |
776 | |
925 | |
777 | =back |
926 | =back |
778 | |
927 | |
779 | =cut |
928 | =cut |
… | |
… | |
974 | |
1123 | |
975 | } elsif (exists $cb_id{$type}) { |
1124 | } elsif (exists $cb_id{$type}) { |
976 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
1125 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
977 | |
1126 | |
978 | } elsif (ref $type) { |
1127 | } elsif (ref $type) { |
979 | warn "attaching objects not supported, ignoring.\n"; |
1128 | error "attaching objects not supported, ignoring.\n"; |
980 | |
1129 | |
981 | } else { |
1130 | } else { |
982 | shift @arg; |
1131 | shift @arg; |
983 | warn "attach argument '$type' not supported, ignoring.\n"; |
1132 | error "attach argument '$type' not supported, ignoring.\n"; |
984 | } |
1133 | } |
985 | } |
1134 | } |
986 | } |
1135 | } |
987 | |
1136 | |
988 | sub _object_attach { |
1137 | sub _object_attach { |
… | |
… | |
998 | _attach $registry, $klass, @attach; |
1147 | _attach $registry, $klass, @attach; |
999 | } |
1148 | } |
1000 | |
1149 | |
1001 | $obj->{$name} = \%arg; |
1150 | $obj->{$name} = \%arg; |
1002 | } else { |
1151 | } else { |
1003 | warn "object uses attachment '$name' which is not available, postponing.\n"; |
1152 | info "object uses attachment '$name' which is not available, postponing.\n"; |
1004 | } |
1153 | } |
1005 | |
1154 | |
1006 | $obj->{_attachment}{$name} = undef; |
1155 | $obj->{_attachment}{$name} = undef; |
1007 | } |
1156 | } |
1008 | |
1157 | |
… | |
… | |
1067 | |
1216 | |
1068 | for (@$callbacks) { |
1217 | for (@$callbacks) { |
1069 | eval { &{$_->[1]} }; |
1218 | eval { &{$_->[1]} }; |
1070 | |
1219 | |
1071 | if ($@) { |
1220 | if ($@) { |
1072 | warn "$@"; |
|
|
1073 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1221 | error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1074 | override; |
1222 | override; |
1075 | } |
1223 | } |
1076 | |
1224 | |
1077 | return 1 if $override; |
1225 | return 1 if $override; |
1078 | } |
1226 | } |
… | |
… | |
1157 | for (@$attach) { |
1305 | for (@$attach) { |
1158 | my ($klass, @attach) = @$_; |
1306 | my ($klass, @attach) = @$_; |
1159 | _attach $registry, $klass, @attach; |
1307 | _attach $registry, $klass, @attach; |
1160 | } |
1308 | } |
1161 | } else { |
1309 | } else { |
1162 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
1310 | info "object uses attachment '$name' that is not available, postponing.\n"; |
1163 | } |
1311 | } |
1164 | } |
1312 | } |
1165 | } |
1313 | } |
1166 | |
1314 | |
1167 | cf::attachable->attach ( |
1315 | cf::attachable->attach ( |
… | |
… | |
1194 | my ($filename, $rdata, $objs) = @_; |
1342 | my ($filename, $rdata, $objs) = @_; |
1195 | |
1343 | |
1196 | sync_job { |
1344 | sync_job { |
1197 | if (length $$rdata) { |
1345 | if (length $$rdata) { |
1198 | utf8::decode (my $decname = $filename); |
1346 | utf8::decode (my $decname = $filename); |
1199 | warn sprintf "saving %s (%d,%d)\n", |
1347 | trace sprintf "saving %s (%d,%d)\n", |
1200 | $decname, length $$rdata, scalar @$objs |
1348 | $decname, length $$rdata, scalar @$objs |
1201 | if $VERBOSE_IO; |
1349 | if $VERBOSE_IO; |
1202 | |
1350 | |
1203 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1351 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1204 | aio_chmod $fh, SAVE_MODE; |
1352 | aio_chmod $fh, SAVE_MODE; |
1205 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1353 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
… | |
… | |
1228 | aio_rename "$filename~", $filename; |
1376 | aio_rename "$filename~", $filename; |
1229 | |
1377 | |
1230 | $filename =~ s%/[^/]+$%%; |
1378 | $filename =~ s%/[^/]+$%%; |
1231 | aio_pathsync $filename if $cf::USE_FSYNC; |
1379 | aio_pathsync $filename if $cf::USE_FSYNC; |
1232 | } else { |
1380 | } else { |
1233 | warn "unable to save objects: $filename~: $!\n"; |
1381 | error "unable to save objects: $filename~: $!\n"; |
1234 | } |
1382 | } |
1235 | } else { |
1383 | } else { |
1236 | aio_unlink $filename; |
1384 | aio_unlink $filename; |
1237 | aio_unlink "$filename.pst"; |
1385 | aio_unlink "$filename.pst"; |
1238 | } |
1386 | } |
… | |
… | |
1262 | my $st = eval { Coro::Storable::thaw $av }; |
1410 | my $st = eval { Coro::Storable::thaw $av }; |
1263 | $av = $st->{objs}; |
1411 | $av = $st->{objs}; |
1264 | } |
1412 | } |
1265 | |
1413 | |
1266 | utf8::decode (my $decname = $filename); |
1414 | utf8::decode (my $decname = $filename); |
1267 | warn sprintf "loading %s (%d,%d)\n", |
1415 | trace sprintf "loading %s (%d,%d)\n", |
1268 | $decname, length $data, scalar @{$av || []} |
1416 | $decname, length $data, scalar @{$av || []} |
1269 | if $VERBOSE_IO; |
1417 | if $VERBOSE_IO; |
1270 | |
1418 | |
1271 | ($data, $av) |
1419 | ($data, $av) |
1272 | } |
1420 | } |
1273 | |
1421 | |
… | |
… | |
1351 | my ($pl, $buf) = @_; |
1499 | my ($pl, $buf) = @_; |
1352 | |
1500 | |
1353 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1501 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1354 | |
1502 | |
1355 | if (ref $msg) { |
1503 | if (ref $msg) { |
1356 | my ($type, $reply, @payload) = |
1504 | my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash |
1357 | "ARRAY" eq ref $msg |
|
|
1358 | ? @$msg |
|
|
1359 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
1360 | |
1505 | |
1361 | my @reply; |
1506 | my @reply; |
1362 | |
1507 | |
1363 | if (my $cb = $EXTCMD{$type}) { |
1508 | if (my $cb = $EXTCMD{$type}) { |
1364 | @reply = $cb->($pl, @payload); |
1509 | @reply = $cb->($pl, @payload); |
… | |
… | |
1366 | |
1511 | |
1367 | $pl->ext_reply ($reply, @reply) |
1512 | $pl->ext_reply ($reply, @reply) |
1368 | if $reply; |
1513 | if $reply; |
1369 | |
1514 | |
1370 | } else { |
1515 | } else { |
1371 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1516 | error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1372 | } |
1517 | } |
1373 | |
1518 | |
1374 | cf::override; |
1519 | cf::override; |
1375 | }, |
1520 | }, |
1376 | ); |
1521 | ); |
… | |
… | |
1387 | |
1532 | |
1388 | $grp |
1533 | $grp |
1389 | } |
1534 | } |
1390 | |
1535 | |
1391 | sub load_extensions { |
1536 | sub load_extensions { |
|
|
1537 | info "loading extensions..."; |
|
|
1538 | |
1392 | cf::sync_job { |
1539 | cf::sync_job { |
1393 | my %todo; |
1540 | my %todo; |
1394 | |
1541 | |
1395 | for my $path (<$LIBDIR/*.ext>) { |
1542 | for my $path (<$LIBDIR/*.ext>) { |
1396 | next unless -r $path; |
1543 | next unless -r $path; |
… | |
… | |
1414 | |
1561 | |
1415 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1562 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1416 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1563 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1417 | |
1564 | |
1418 | $ext{source} = |
1565 | $ext{source} = |
1419 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1566 | "package $pkg; use common::sense;\n" |
1420 | . "#line 1 \"$path\"\n{\n" |
1567 | . "#line 1 \"$path\"\n{\n" |
1421 | . $source |
1568 | . $source |
1422 | . "\n};\n1"; |
1569 | . "\n};\n1"; |
1423 | |
1570 | |
1424 | $todo{$base} = \%ext; |
1571 | $todo{$base} = \%ext; |
… | |
… | |
1436 | for (split /,\s*/, $v->{meta}{depends}) { |
1583 | for (split /,\s*/, $v->{meta}{depends}) { |
1437 | next ext |
1584 | next ext |
1438 | unless exists $done{$_}; |
1585 | unless exists $done{$_}; |
1439 | } |
1586 | } |
1440 | |
1587 | |
1441 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1588 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1442 | |
1589 | |
1443 | my $active = eval $v->{source}; |
1590 | my $active = eval $v->{source}; |
1444 | |
1591 | |
1445 | if (length $@) { |
1592 | if (length $@) { |
1446 | warn "$v->{path}: $@\n"; |
1593 | error "$v->{path}: $@\n"; |
1447 | |
1594 | |
1448 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1595 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1449 | if exists $v->{meta}{mandatory}; |
1596 | if exists $v->{meta}{mandatory}; |
1450 | |
1597 | |
1451 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
1598 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
… | |
… | |
1453 | } else { |
1600 | } else { |
1454 | $done{$k} = delete $todo{$k}; |
1601 | $done{$k} = delete $todo{$k}; |
1455 | push @EXTS, $v->{pkg}; |
1602 | push @EXTS, $v->{pkg}; |
1456 | $progress = 1; |
1603 | $progress = 1; |
1457 | |
1604 | |
1458 | warn "$v->{base}: extension inactive.\n" |
1605 | info "$v->{base}: extension inactive.\n" |
1459 | unless $active; |
1606 | unless $active; |
1460 | } |
1607 | } |
1461 | } |
1608 | } |
1462 | |
1609 | |
1463 | unless ($progress) { |
1610 | unless ($progress) { |
… | |
… | |
1465 | |
1612 | |
1466 | while (my ($k, $v) = each %todo) { |
1613 | while (my ($k, $v) = each %todo) { |
1467 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1614 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1468 | if exists $v->{meta}{mandatory}; |
1615 | if exists $v->{meta}{mandatory}; |
1469 | } |
1616 | } |
|
|
1617 | |
|
|
1618 | last; |
1470 | } |
1619 | } |
1471 | } |
1620 | } |
1472 | }; |
1621 | }; |
1473 | } |
1622 | } |
1474 | |
1623 | |
… | |
… | |
1632 | my $name = $pl->ob->name; |
1781 | my $name = $pl->ob->name; |
1633 | |
1782 | |
1634 | $pl->{deny_save} = 1; |
1783 | $pl->{deny_save} = 1; |
1635 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1784 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1636 | |
1785 | |
1637 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1786 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1638 | $pl->deactivate; |
1787 | $pl->deactivate; |
|
|
1788 | |
1639 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1789 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1640 | $pl->ob->check_score; |
|
|
1641 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1790 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1791 | ext::highscore::check ($pl->ob); |
|
|
1792 | |
1642 | $pl->ns->destroy if $pl->ns; |
1793 | $pl->ns->destroy if $pl->ns; |
1643 | |
1794 | |
1644 | my $path = playerdir $pl; |
1795 | my $path = playerdir $pl; |
1645 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1796 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1646 | aio_rename $path, $temp; |
1797 | aio_rename $path, $temp; |
… | |
… | |
1812 | sub generate_random_map { |
1963 | sub generate_random_map { |
1813 | my ($self, $rmp) = @_; |
1964 | my ($self, $rmp) = @_; |
1814 | |
1965 | |
1815 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1966 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1816 | |
1967 | |
1817 | # mit "rum" bekleckern, nicht |
|
|
1818 | $self->_create_random_map ( |
1968 | $self->_create_random_map ($rmp); |
1819 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
|
|
1820 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
|
|
1821 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
|
|
1822 | $rmp->{exit_on_final_map}, |
|
|
1823 | $rmp->{xsize}, $rmp->{ysize}, |
|
|
1824 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
|
|
1825 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
|
|
1826 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
|
|
1827 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
|
|
1828 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
|
|
1829 | (cf::region::find $rmp->{region}), $rmp->{custom} |
|
|
1830 | ) |
|
|
1831 | } |
1969 | } |
1832 | |
1970 | |
1833 | =item cf::map->register ($regex, $prio) |
1971 | =item cf::map->register ($regex, $prio) |
1834 | |
1972 | |
1835 | Register a handler for the map path matching the given regex at the |
1973 | Register a handler for the map path matching the given regex at the |
… | |
… | |
1840 | |
1978 | |
1841 | sub register { |
1979 | sub register { |
1842 | my (undef, $regex, $prio) = @_; |
1980 | my (undef, $regex, $prio) = @_; |
1843 | my $pkg = caller; |
1981 | my $pkg = caller; |
1844 | |
1982 | |
1845 | no strict; |
|
|
1846 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1983 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1847 | |
1984 | |
1848 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1985 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1849 | } |
1986 | } |
1850 | |
1987 | |
… | |
… | |
1862 | } |
1999 | } |
1863 | |
2000 | |
1864 | sub normalise { |
2001 | sub normalise { |
1865 | my ($path, $base) = @_; |
2002 | my ($path, $base) = @_; |
1866 | |
2003 | |
1867 | $path = "$path"; # make sure its a string |
2004 | $path = "$path"; # make sure it's a string |
1868 | |
2005 | |
1869 | $path =~ s/\.map$//; |
2006 | $path =~ s/\.map$//; |
1870 | |
2007 | |
1871 | # map plan: |
2008 | # map plan: |
1872 | # |
2009 | # |
… | |
… | |
1887 | $base =~ s{[^/]+/?$}{}; |
2024 | $base =~ s{[^/]+/?$}{}; |
1888 | $path = "$base/$path"; |
2025 | $path = "$base/$path"; |
1889 | } |
2026 | } |
1890 | |
2027 | |
1891 | for ($path) { |
2028 | for ($path) { |
1892 | redo if s{//}{/}; |
|
|
1893 | redo if s{/\.?/}{/}; |
2029 | redo if s{/\.?/}{/}; |
1894 | redo if s{/[^/]+/\.\./}{/}; |
2030 | redo if s{/[^/]+/\.\./}{/}; |
1895 | } |
2031 | } |
1896 | |
2032 | |
1897 | $path |
2033 | $path |
… | |
… | |
1911 | $self->init; # pass $1 etc. |
2047 | $self->init; # pass $1 etc. |
1912 | return $self; |
2048 | return $self; |
1913 | } |
2049 | } |
1914 | } |
2050 | } |
1915 | |
2051 | |
1916 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
2052 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1917 | () |
2053 | () |
1918 | } |
2054 | } |
1919 | |
2055 | |
|
|
2056 | # may re-bless or do other evil things |
1920 | sub init { |
2057 | sub init { |
1921 | my ($self) = @_; |
2058 | my ($self) = @_; |
1922 | |
2059 | |
1923 | $self |
2060 | $self |
1924 | } |
2061 | } |
… | |
… | |
1989 | $self->{load_path} = $path; |
2126 | $self->{load_path} = $path; |
1990 | |
2127 | |
1991 | 1 |
2128 | 1 |
1992 | } |
2129 | } |
1993 | |
2130 | |
|
|
2131 | # used to laod the header of an original map |
1994 | sub load_header_orig { |
2132 | sub load_header_orig { |
1995 | my ($self) = @_; |
2133 | my ($self) = @_; |
1996 | |
2134 | |
1997 | $self->load_header_from ($self->load_path) |
2135 | $self->load_header_from ($self->load_path) |
1998 | } |
2136 | } |
1999 | |
2137 | |
|
|
2138 | # used to laod the header of an instantiated map |
2000 | sub load_header_temp { |
2139 | sub load_header_temp { |
2001 | my ($self) = @_; |
2140 | my ($self) = @_; |
2002 | |
2141 | |
2003 | $self->load_header_from ($self->save_path) |
2142 | $self->load_header_from ($self->save_path) |
2004 | } |
2143 | } |
2005 | |
2144 | |
|
|
2145 | # called after loading the header from an instantiated map |
2006 | sub prepare_temp { |
2146 | sub prepare_temp { |
2007 | my ($self) = @_; |
2147 | my ($self) = @_; |
2008 | |
2148 | |
2009 | $self->last_access ((delete $self->{last_access}) |
2149 | $self->last_access ((delete $self->{last_access}) |
2010 | || $cf::RUNTIME); #d# |
2150 | || $cf::RUNTIME); #d# |
2011 | # safety |
2151 | # safety |
2012 | $self->{instantiate_time} = $cf::RUNTIME |
2152 | $self->{instantiate_time} = $cf::RUNTIME |
2013 | if $self->{instantiate_time} > $cf::RUNTIME; |
2153 | if $self->{instantiate_time} > $cf::RUNTIME; |
2014 | } |
2154 | } |
2015 | |
2155 | |
|
|
2156 | # called after loading the header from an original map |
2016 | sub prepare_orig { |
2157 | sub prepare_orig { |
2017 | my ($self) = @_; |
2158 | my ($self) = @_; |
2018 | |
2159 | |
2019 | $self->{load_original} = 1; |
2160 | $self->{load_original} = 1; |
2020 | $self->{instantiate_time} = $cf::RUNTIME; |
2161 | $self->{instantiate_time} = $cf::RUNTIME; |
… | |
… | |
2044 | |
2185 | |
2045 | sub find; |
2186 | sub find; |
2046 | sub find { |
2187 | sub find { |
2047 | my ($path, $origin) = @_; |
2188 | my ($path, $origin) = @_; |
2048 | |
2189 | |
|
|
2190 | cf::cede_to_tick; |
|
|
2191 | |
2049 | $path = normalise $path, $origin && $origin->path; |
2192 | $path = normalise $path, $origin; |
2050 | |
2193 | |
2051 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2194 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2052 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2195 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2053 | |
2196 | |
2054 | $cf::MAP{$path} || do { |
2197 | $cf::MAP{$path} || do { |
… | |
… | |
2085 | |
2228 | |
2086 | { |
2229 | { |
2087 | my $guard = cf::lock_acquire "map_data:$path"; |
2230 | my $guard = cf::lock_acquire "map_data:$path"; |
2088 | |
2231 | |
2089 | return unless $self->valid; |
2232 | return unless $self->valid; |
2090 | return unless $self->in_memory == cf::MAP_SWAPPED; |
2233 | return unless $self->state == cf::MAP_SWAPPED; |
2091 | |
|
|
2092 | $self->in_memory (cf::MAP_LOADING); |
|
|
2093 | |
2234 | |
2094 | $self->alloc; |
2235 | $self->alloc; |
2095 | |
2236 | |
2096 | $self->pre_load; |
2237 | $self->pre_load; |
2097 | cf::cede_to_tick; |
2238 | cf::cede_to_tick; |
2098 | |
2239 | |
|
|
2240 | if (exists $self->{load_path}) { |
2099 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2241 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2100 | $f->skip_block; |
2242 | $f->skip_block; |
2101 | $self->_load_objects ($f) |
2243 | $self->_load_objects ($f) |
2102 | or return; |
2244 | or return; |
2103 | |
2245 | |
2104 | $self->post_load_original |
2246 | $self->post_load_original |
2105 | if delete $self->{load_original}; |
2247 | if delete $self->{load_original}; |
2106 | |
2248 | |
2107 | if (my $uniq = $self->uniq_path) { |
2249 | if (my $uniq = $self->uniq_path) { |
2108 | utf8::encode $uniq; |
2250 | utf8::encode $uniq; |
2109 | unless (aio_stat $uniq) { |
2251 | unless (aio_stat $uniq) { |
2110 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2252 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2111 | $self->clear_unique_items; |
2253 | $self->clear_unique_items; |
2112 | $self->_load_objects ($f); |
2254 | $self->_load_objects ($f); |
2113 | $f->resolve_delayed_derefs; |
2255 | $f->resolve_delayed_derefs; |
|
|
2256 | } |
2114 | } |
2257 | } |
2115 | } |
2258 | } |
2116 | } |
|
|
2117 | |
2259 | |
2118 | $f->resolve_delayed_derefs; |
2260 | $f->resolve_delayed_derefs; |
|
|
2261 | } else { |
|
|
2262 | $self->post_load_original |
|
|
2263 | if delete $self->{load_original}; |
|
|
2264 | } |
|
|
2265 | |
|
|
2266 | $self->state (cf::MAP_INACTIVE); |
2119 | |
2267 | |
2120 | cf::cede_to_tick; |
2268 | cf::cede_to_tick; |
2121 | # now do the right thing for maps |
2269 | # now do the right thing for maps |
2122 | $self->link_multipart_objects; |
2270 | $self->link_multipart_objects; |
2123 | $self->difficulty ($self->estimate_difficulty) |
2271 | $self->difficulty ($self->estimate_difficulty) |
… | |
… | |
2127 | unless ($self->{deny_activate}) { |
2275 | unless ($self->{deny_activate}) { |
2128 | $self->decay_objects; |
2276 | $self->decay_objects; |
2129 | $self->fix_auto_apply; |
2277 | $self->fix_auto_apply; |
2130 | $self->update_buttons; |
2278 | $self->update_buttons; |
2131 | cf::cede_to_tick; |
2279 | cf::cede_to_tick; |
2132 | $self->activate; |
2280 | #$self->activate; # no longer activate maps automatically |
2133 | } |
2281 | } |
2134 | |
2282 | |
2135 | $self->{last_save} = $cf::RUNTIME; |
2283 | $self->{last_save} = $cf::RUNTIME; |
2136 | $self->last_access ($cf::RUNTIME); |
2284 | $self->last_access ($cf::RUNTIME); |
2137 | |
|
|
2138 | $self->in_memory (cf::MAP_ACTIVE); |
|
|
2139 | } |
2285 | } |
2140 | |
2286 | |
2141 | $self->post_load; |
2287 | $self->post_load; |
|
|
2288 | |
|
|
2289 | 1 |
2142 | } |
2290 | } |
2143 | |
2291 | |
2144 | # customize the map for a given player, i.e. |
2292 | # customize the map for a given player, i.e. |
2145 | # return the _real_ map. used by e.g. per-player |
2293 | # return the _real_ map. used by e.g. per-player |
2146 | # maps to change the path to ~playername/mappath |
2294 | # maps to change the path to ~playername/mappath |
… | |
… | |
2154 | # if $self->per_party; |
2302 | # if $self->per_party; |
2155 | |
2303 | |
2156 | $self |
2304 | $self |
2157 | } |
2305 | } |
2158 | |
2306 | |
2159 | # find and load all maps in the 3x3 area around a map |
|
|
2160 | sub load_neighbours { |
|
|
2161 | my ($map) = @_; |
|
|
2162 | |
|
|
2163 | my @neigh; # diagonal neighbours |
|
|
2164 | |
|
|
2165 | for (0 .. 3) { |
|
|
2166 | my $neigh = $map->tile_path ($_) |
|
|
2167 | or next; |
|
|
2168 | $neigh = find $neigh, $map |
|
|
2169 | or next; |
|
|
2170 | $neigh->load; |
|
|
2171 | |
|
|
2172 | # now find the diagonal neighbours |
|
|
2173 | push @neigh, |
|
|
2174 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
2175 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
2176 | } |
|
|
2177 | |
|
|
2178 | for (grep defined $_->[0], @neigh) { |
|
|
2179 | my ($path, $origin) = @$_; |
|
|
2180 | my $neigh = find $path, $origin |
|
|
2181 | or next; |
|
|
2182 | $neigh->load; |
|
|
2183 | } |
|
|
2184 | } |
|
|
2185 | |
|
|
2186 | sub find_sync { |
2307 | sub find_sync { |
2187 | my ($path, $origin) = @_; |
2308 | my ($path, $origin) = @_; |
2188 | |
2309 | |
2189 | cf::sync_job { find $path, $origin } |
2310 | # it's a bug to call this from the main context |
|
|
2311 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2312 | if $Coro::current == $Coro::main; |
|
|
2313 | |
|
|
2314 | find $path, $origin |
2190 | } |
2315 | } |
2191 | |
2316 | |
2192 | sub do_load_sync { |
2317 | sub do_load_sync { |
2193 | my ($map) = @_; |
2318 | my ($map) = @_; |
2194 | |
2319 | |
|
|
2320 | # it's a bug to call this from the main context |
2195 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2321 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2196 | if $Coro::current == $Coro::main; |
2322 | if $Coro::current == $Coro::main; |
2197 | |
2323 | |
2198 | cf::sync_job { $map->load }; |
2324 | $map->load; |
2199 | } |
2325 | } |
2200 | |
2326 | |
2201 | our %MAP_PREFETCH; |
2327 | our %MAP_PREFETCH; |
2202 | our $MAP_PREFETCHER = undef; |
2328 | our $MAP_PREFETCHER = undef; |
2203 | |
2329 | |
2204 | sub find_async { |
2330 | sub find_async { |
2205 | my ($path, $origin, $load) = @_; |
2331 | my ($path, $origin, $load) = @_; |
2206 | |
2332 | |
2207 | $path = normalise $path, $origin && $origin->{path}; |
2333 | $path = normalise $path, $origin; |
2208 | |
2334 | |
2209 | if (my $map = $cf::MAP{$path}) { |
2335 | if (my $map = $cf::MAP{$path}) { |
2210 | return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; |
2336 | return $map if !$load || $map->linkable; |
2211 | } |
2337 | } |
2212 | |
2338 | |
2213 | $MAP_PREFETCH{$path} |= $load; |
2339 | $MAP_PREFETCH{$path} |= $load; |
2214 | |
2340 | |
2215 | $MAP_PREFETCHER ||= cf::async { |
2341 | $MAP_PREFETCHER ||= cf::async { |
… | |
… | |
2274 | sub swap_out { |
2400 | sub swap_out { |
2275 | my ($self) = @_; |
2401 | my ($self) = @_; |
2276 | |
2402 | |
2277 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2403 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2278 | |
2404 | |
2279 | return if $self->in_memory != cf::MAP_ACTIVE; |
2405 | return if !$self->linkable; |
2280 | return if $self->{deny_save}; |
2406 | return if $self->{deny_save}; |
2281 | return if $self->players; |
2407 | return if $self->players; |
2282 | |
2408 | |
2283 | # first deactivate the map and "unlink" it from the core |
2409 | # first deactivate the map and "unlink" it from the core |
2284 | $self->deactivate; |
2410 | $self->deactivate; |
2285 | $_->clear_links_to ($self) for values %cf::MAP; |
2411 | $_->clear_links_to ($self) for values %cf::MAP; |
2286 | $self->in_memory (cf::MAP_SWAPPED); |
2412 | $self->state (cf::MAP_SWAPPED); |
2287 | |
2413 | |
2288 | # then atomically save |
2414 | # then atomically save |
2289 | $self->_save; |
2415 | $self->_save; |
2290 | |
2416 | |
2291 | # then free the map |
2417 | # then free the map |
… | |
… | |
2315 | |
2441 | |
2316 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2442 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2317 | |
2443 | |
2318 | return if $self->players; |
2444 | return if $self->players; |
2319 | |
2445 | |
2320 | warn "resetting map ", $self->path, "\n"; |
2446 | cf::trace "resetting map ", $self->path, "\n"; |
2321 | |
2447 | |
2322 | $self->in_memory (cf::MAP_SWAPPED); |
2448 | $self->state (cf::MAP_SWAPPED); |
2323 | |
2449 | |
2324 | # need to save uniques path |
2450 | # need to save uniques path |
2325 | unless ($self->{deny_save}) { |
2451 | unless ($self->{deny_save}) { |
2326 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2452 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2327 | |
2453 | |
… | |
… | |
2657 | # use -1 or undef as default coordinates, not 0, 0 |
2783 | # use -1 or undef as default coordinates, not 0, 0 |
2658 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2784 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2659 | if $x <= 0 && $y <= 0; |
2785 | if $x <= 0 && $y <= 0; |
2660 | |
2786 | |
2661 | $map->load; |
2787 | $map->load; |
2662 | $map->load_neighbours; |
|
|
2663 | |
2788 | |
2664 | return unless $self->contr->active; |
2789 | return unless $self->contr->active; |
2665 | |
2790 | |
2666 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2791 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2667 | if ($self->enter_map ($map, $x, $y)) { |
2792 | if ($self->enter_map ($map, $x, $y)) { |
… | |
… | |
2690 | |
2815 | |
2691 | sub cf::object::player::goto { |
2816 | sub cf::object::player::goto { |
2692 | my ($self, $path, $x, $y, $check, $done) = @_; |
2817 | my ($self, $path, $x, $y, $check, $done) = @_; |
2693 | |
2818 | |
2694 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
2819 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
2695 | warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
2820 | error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
2696 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
2821 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
2697 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
2822 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
2698 | } |
2823 | } |
2699 | |
2824 | |
2700 | # do generation counting so two concurrent goto's will be executed in-order |
2825 | # do generation counting so two concurrent goto's will be executed in-order |
… | |
… | |
2723 | ($path, $x, $y) = (undef, undef, undef); |
2848 | ($path, $x, $y) = (undef, undef, undef); |
2724 | } |
2849 | } |
2725 | } |
2850 | } |
2726 | |
2851 | |
2727 | my $map = eval { |
2852 | my $map = eval { |
2728 | my $map = defined $path ? cf::map::find $path : undef; |
2853 | my $map = defined $path ? cf::map::find $path, $self->map : undef; |
2729 | |
2854 | |
2730 | if ($map) { |
2855 | if ($map) { |
2731 | $map = $map->customise_for ($self); |
2856 | $map = $map->customise_for ($self); |
2732 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2857 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2733 | } else { |
2858 | } else { |
… | |
… | |
2823 | $Coro::current->{desc} = "enter_exit"; |
2948 | $Coro::current->{desc} = "enter_exit"; |
2824 | |
2949 | |
2825 | unless (eval { |
2950 | unless (eval { |
2826 | $self->deactivate_recursive; # just to be sure |
2951 | $self->deactivate_recursive; # just to be sure |
2827 | |
2952 | |
2828 | # random map handling |
|
|
2829 | { |
|
|
2830 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2831 | |
|
|
2832 | prepare_random_map $exit |
|
|
2833 | if $exit->slaying eq "/!"; |
|
|
2834 | } |
|
|
2835 | |
|
|
2836 | my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2953 | my $map = cf::map::normalise $exit->slaying, $exit->map; |
2837 | my $x = $exit->stats->hp; |
2954 | my $x = $exit->stats->hp; |
2838 | my $y = $exit->stats->sp; |
2955 | my $y = $exit->stats->sp; |
|
|
2956 | |
|
|
2957 | # special map handling |
|
|
2958 | my $slaying = $exit->slaying; |
|
|
2959 | |
|
|
2960 | # special map handling |
|
|
2961 | if ($slaying eq "/!") { |
|
|
2962 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2963 | |
|
|
2964 | prepare_random_map $exit |
|
|
2965 | if $exit->slaying eq "/!"; # need to re-check after getting the lock |
|
|
2966 | |
|
|
2967 | $map = $exit->slaying; |
|
|
2968 | |
|
|
2969 | } elsif ($slaying eq '!up') { |
|
|
2970 | $map = $exit->map->tile_path (cf::TILE_UP); |
|
|
2971 | $x = $exit->x; |
|
|
2972 | $y = $exit->y; |
|
|
2973 | |
|
|
2974 | } elsif ($slaying eq '!down') { |
|
|
2975 | $map = $exit->map->tile_path (cf::TILE_DOWN); |
|
|
2976 | $x = $exit->x; |
|
|
2977 | $y = $exit->y; |
|
|
2978 | } |
2839 | |
2979 | |
2840 | $self->goto ($map, $x, $y); |
2980 | $self->goto ($map, $x, $y); |
2841 | |
2981 | |
2842 | # if exit is damned, update players death & WoR home-position |
2982 | # if exit is damned, update players death & WoR home-position |
2843 | $self->contr->savebed ($map, $x, $y) |
2983 | $self->contr->savebed ($map, $x, $y) |
… | |
… | |
2848 | $self->message ("Something went wrong deep within the deliantra server. " |
2988 | $self->message ("Something went wrong deep within the deliantra server. " |
2849 | . "I'll try to bring you back to the map you were before. " |
2989 | . "I'll try to bring you back to the map you were before. " |
2850 | . "Please report this to the dungeon master!", |
2990 | . "Please report this to the dungeon master!", |
2851 | cf::NDI_UNIQUE | cf::NDI_RED); |
2991 | cf::NDI_UNIQUE | cf::NDI_RED); |
2852 | |
2992 | |
2853 | warn "ERROR in enter_exit: $@"; |
2993 | error "ERROR in enter_exit: $@"; |
2854 | $self->leave_link; |
2994 | $self->leave_link; |
2855 | } |
2995 | } |
2856 | })->prio (1); |
2996 | })->prio (1); |
2857 | } |
2997 | } |
2858 | |
2998 | |
… | |
… | |
3085 | =cut |
3225 | =cut |
3086 | |
3226 | |
3087 | sub cf::client::ext_reply($$@) { |
3227 | sub cf::client::ext_reply($$@) { |
3088 | my ($self, $id, @msg) = @_; |
3228 | my ($self, $id, @msg) = @_; |
3089 | |
3229 | |
3090 | if ($self->extcmd == 2) { |
3230 | return unless $self->extcmd == 2; |
|
|
3231 | |
3091 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3232 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3092 | } elsif ($self->extcmd == 1) { |
|
|
3093 | #TODO: version 1, remove |
|
|
3094 | unshift @msg, msgtype => "reply", msgid => $id; |
|
|
3095 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
|
|
3096 | } |
|
|
3097 | } |
3233 | } |
3098 | |
3234 | |
3099 | =item $success = $client->query ($flags, "text", \&cb) |
3235 | =item $success = $client->query ($flags, "text", \&cb) |
3100 | |
3236 | |
3101 | Queues a query to the client, calling the given callback with |
3237 | Queues a query to the client, calling the given callback with |
… | |
… | |
3156 | my ($ns, $buf) = @_; |
3292 | my ($ns, $buf) = @_; |
3157 | |
3293 | |
3158 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3294 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3159 | |
3295 | |
3160 | if (ref $msg) { |
3296 | if (ref $msg) { |
3161 | my ($type, $reply, @payload) = |
3297 | my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash |
3162 | "ARRAY" eq ref $msg |
|
|
3163 | ? @$msg |
|
|
3164 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
3165 | |
3298 | |
3166 | my @reply; |
3299 | my @reply; |
3167 | |
3300 | |
3168 | if (my $cb = $EXTICMD{$type}) { |
3301 | if (my $cb = $EXTICMD{$type}) { |
3169 | @reply = $cb->($ns, @payload); |
3302 | @reply = $cb->($ns, @payload); |
… | |
… | |
3171 | |
3304 | |
3172 | $ns->ext_reply ($reply, @reply) |
3305 | $ns->ext_reply ($reply, @reply) |
3173 | if $reply; |
3306 | if $reply; |
3174 | |
3307 | |
3175 | } else { |
3308 | } else { |
3176 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3309 | error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3177 | } |
3310 | } |
3178 | |
3311 | |
3179 | cf::override; |
3312 | cf::override; |
3180 | }, |
3313 | }, |
3181 | ); |
3314 | ); |
… | |
… | |
3262 | decrease split destroy change_exp value msg lore send_msg)], |
3395 | decrease split destroy change_exp value msg lore send_msg)], |
3263 | ["cf::object::player" => qw(player)], |
3396 | ["cf::object::player" => qw(player)], |
3264 | ["cf::player" => qw(peaceful send_msg)], |
3397 | ["cf::player" => qw(peaceful send_msg)], |
3265 | ["cf::map" => qw(trigger)], |
3398 | ["cf::map" => qw(trigger)], |
3266 | ) { |
3399 | ) { |
3267 | no strict 'refs'; |
|
|
3268 | my ($pkg, @funs) = @$_; |
3400 | my ($pkg, @funs) = @$_; |
3269 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3401 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3270 | for @funs; |
3402 | for @funs; |
3271 | } |
3403 | } |
3272 | |
3404 | |
… | |
… | |
3311 | local @cf::_safe_eval_args = values %vars; |
3443 | local @cf::_safe_eval_args = values %vars; |
3312 | @res = wantarray ? eval eval : scalar eval $eval; |
3444 | @res = wantarray ? eval eval : scalar eval $eval; |
3313 | } |
3445 | } |
3314 | |
3446 | |
3315 | if ($@) { |
3447 | if ($@) { |
3316 | warn "$@"; |
3448 | warn "$@", |
3317 | warn "while executing safe code '$code'\n"; |
3449 | "while executing safe code '$code'\n", |
3318 | warn "with arguments " . (join " ", %vars) . "\n"; |
3450 | "with arguments " . (join " ", %vars) . "\n"; |
3319 | } |
3451 | } |
3320 | |
3452 | |
3321 | wantarray ? @res : $res[0] |
3453 | wantarray ? @res : $res[0] |
3322 | } |
3454 | } |
3323 | |
3455 | |
… | |
… | |
3357 | # for this (global event?) |
3489 | # for this (global event?) |
3358 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3490 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3359 | |
3491 | |
3360 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3492 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3361 | |
3493 | |
3362 | warn "loading facedata from $path\n"; |
3494 | trace "loading facedata from $path\n"; |
3363 | |
3495 | |
3364 | my $facedata; |
3496 | my $facedata = decode_storable load_file $path; |
3365 | 0 < aio_load $path, $facedata |
|
|
3366 | or die "$path: $!"; |
|
|
3367 | |
|
|
3368 | $facedata = Coro::Storable::thaw $facedata; |
|
|
3369 | |
3497 | |
3370 | $facedata->{version} == 2 |
3498 | $facedata->{version} == 2 |
3371 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3499 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3372 | |
3500 | |
3373 | # patch in the exptable |
3501 | # patch in the exptable |
… | |
… | |
3387 | |
3515 | |
3388 | cf::face::set_visibility $idx, $info->{visibility}; |
3516 | cf::face::set_visibility $idx, $info->{visibility}; |
3389 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3517 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3390 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3518 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3391 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3519 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
|
|
3520 | cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; |
3392 | |
3521 | |
3393 | cf::cede_to_tick; |
3522 | cf::cede_to_tick; |
3394 | } |
3523 | } |
3395 | |
3524 | |
3396 | while (my ($face, $info) = each %$faces) { |
3525 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3401 | |
3530 | |
3402 | if (my $smooth = cf::face::find $info->{smooth}) { |
3531 | if (my $smooth = cf::face::find $info->{smooth}) { |
3403 | cf::face::set_smooth $idx, $smooth; |
3532 | cf::face::set_smooth $idx, $smooth; |
3404 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3533 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3405 | } else { |
3534 | } else { |
3406 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3535 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3407 | } |
3536 | } |
3408 | |
3537 | |
3409 | cf::cede_to_tick; |
3538 | cf::cede_to_tick; |
3410 | } |
3539 | } |
3411 | } |
3540 | } |
… | |
… | |
3423 | |
3552 | |
3424 | { |
3553 | { |
3425 | my $res = $facedata->{resource}; |
3554 | my $res = $facedata->{resource}; |
3426 | |
3555 | |
3427 | while (my ($name, $info) = each %$res) { |
3556 | while (my ($name, $info) = each %$res) { |
3428 | if (defined $info->{type}) { |
3557 | if (defined (my $type = $info->{type})) { |
|
|
3558 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3429 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3559 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3430 | |
3560 | |
3431 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3561 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3432 | cf::face::set_type $idx, $info->{type}; |
3562 | cf::face::set_type $idx, $type; |
|
|
3563 | cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3433 | } else { |
3564 | } else { |
3434 | $RESOURCE{$name} = $info; |
3565 | # $RESOURCE{$name} = $info; # unused |
3435 | } |
3566 | } |
3436 | |
3567 | |
3437 | cf::cede_to_tick; |
3568 | cf::cede_to_tick; |
3438 | } |
3569 | } |
3439 | } |
3570 | } |
3440 | |
3571 | |
3441 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3572 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3442 | |
3573 | |
3443 | 1 |
3574 | 1 |
3444 | } |
3575 | } |
3445 | |
|
|
3446 | cf::global->attach (on_resource_update => sub { |
|
|
3447 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3448 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3449 | |
|
|
3450 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3451 | my $sound = $soundconf->{compat}[$_] |
|
|
3452 | or next; |
|
|
3453 | |
|
|
3454 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3455 | cf::sound::set $sound->[0] => $face; |
|
|
3456 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3457 | } |
|
|
3458 | |
|
|
3459 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3460 | my $face = cf::face::find "sound/$v"; |
|
|
3461 | cf::sound::set $k => $face; |
|
|
3462 | } |
|
|
3463 | } |
|
|
3464 | }); |
|
|
3465 | |
3576 | |
3466 | register_exticmd fx_want => sub { |
3577 | register_exticmd fx_want => sub { |
3467 | my ($ns, $want) = @_; |
3578 | my ($ns, $want) = @_; |
3468 | |
3579 | |
3469 | while (my ($k, $v) = each %$want) { |
3580 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3508 | sub reload_treasures { |
3619 | sub reload_treasures { |
3509 | load_resource_file "$DATADIR/treasures" |
3620 | load_resource_file "$DATADIR/treasures" |
3510 | or die "unable to load treasurelists\n"; |
3621 | or die "unable to load treasurelists\n"; |
3511 | } |
3622 | } |
3512 | |
3623 | |
|
|
3624 | sub reload_sound { |
|
|
3625 | trace "loading sound config from $DATADIR/sound\n"; |
|
|
3626 | |
|
|
3627 | my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound"); |
|
|
3628 | |
|
|
3629 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3630 | my $sound = $soundconf->{compat}[$_] |
|
|
3631 | or next; |
|
|
3632 | |
|
|
3633 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3634 | cf::sound::set $sound->[0] => $face; |
|
|
3635 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3636 | } |
|
|
3637 | |
|
|
3638 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3639 | my $face = cf::face::find "sound/$v"; |
|
|
3640 | cf::sound::set $k => $face; |
|
|
3641 | } |
|
|
3642 | } |
|
|
3643 | |
3513 | sub reload_resources { |
3644 | sub reload_resources { |
3514 | warn "reloading resource files...\n"; |
3645 | trace "reloading resource files...\n"; |
3515 | |
3646 | |
|
|
3647 | reload_exp_table; |
|
|
3648 | reload_materials; |
3516 | reload_facedata; |
3649 | reload_facedata; |
|
|
3650 | reload_sound; |
3517 | reload_archetypes; |
3651 | reload_archetypes; |
3518 | reload_regions; |
3652 | reload_regions; |
3519 | reload_treasures; |
3653 | reload_treasures; |
3520 | |
3654 | |
3521 | warn "finished reloading resource files\n"; |
3655 | trace "finished reloading resource files\n"; |
3522 | } |
3656 | } |
3523 | |
3657 | |
3524 | sub reload_config { |
3658 | sub reload_config { |
3525 | warn "reloading config file...\n"; |
3659 | trace "reloading config file...\n"; |
3526 | |
3660 | |
3527 | open my $fh, "<:utf8", "$CONFDIR/config" |
3661 | my $config = load_file "$CONFDIR/config"; |
3528 | or return; |
3662 | utf8::decode $config; |
3529 | |
3663 | *CFG = decode_yaml $config; |
3530 | local $/; |
|
|
3531 | *CFG = YAML::XS::Load scalar <$fh>; |
|
|
3532 | |
3664 | |
3533 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3665 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3534 | |
3666 | |
3535 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3667 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3536 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3668 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3540 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3672 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3541 | and die "WARNING: m(un)lockall failed: $!\n"; |
3673 | and die "WARNING: m(un)lockall failed: $!\n"; |
3542 | }; |
3674 | }; |
3543 | warn $@ if $@; |
3675 | warn $@ if $@; |
3544 | } |
3676 | } |
3545 | |
|
|
3546 | warn "finished reloading resource files\n"; |
|
|
3547 | } |
3677 | } |
3548 | |
3678 | |
3549 | sub pidfile() { |
3679 | sub pidfile() { |
3550 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3680 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3551 | or die "$PIDFILE: $!"; |
3681 | or die "$PIDFILE: $!"; |
… | |
… | |
3564 | seek $fh, 0, 0; |
3694 | seek $fh, 0, 0; |
3565 | print $fh $$; |
3695 | print $fh $$; |
3566 | } |
3696 | } |
3567 | |
3697 | |
3568 | sub main_loop { |
3698 | sub main_loop { |
3569 | warn "EV::loop starting\n"; |
3699 | trace "EV::loop starting\n"; |
3570 | if (1) { |
3700 | if (1) { |
3571 | EV::loop; |
3701 | EV::loop; |
3572 | } |
3702 | } |
3573 | warn "EV::loop returned\n"; |
3703 | trace "EV::loop returned\n"; |
3574 | goto &main_loop unless $REALLY_UNLOOP; |
3704 | goto &main_loop unless $REALLY_UNLOOP; |
3575 | } |
3705 | } |
3576 | |
3706 | |
3577 | sub main { |
3707 | sub main { |
3578 | cf::init_globals; # initialise logging |
3708 | cf::init_globals; # initialise logging |
3579 | |
3709 | |
3580 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3710 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3581 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3711 | LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3582 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3712 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3583 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3713 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3584 | |
3714 | |
3585 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3715 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3586 | |
3716 | |
… | |
… | |
3594 | }; |
3724 | }; |
3595 | |
3725 | |
3596 | evthread_start IO::AIO::poll_fileno; |
3726 | evthread_start IO::AIO::poll_fileno; |
3597 | |
3727 | |
3598 | cf::sync_job { |
3728 | cf::sync_job { |
3599 | cf::init_experience; |
3729 | cf::incloader::init (); |
|
|
3730 | |
3600 | cf::init_anim; |
3731 | cf::init_anim; |
3601 | cf::init_attackmess; |
3732 | cf::init_attackmess; |
3602 | cf::init_dynamic; |
3733 | cf::init_dynamic; |
3603 | |
3734 | |
3604 | cf::load_settings; |
3735 | cf::load_settings; |
3605 | cf::load_materials; |
|
|
3606 | |
3736 | |
3607 | reload_resources; |
3737 | reload_resources; |
3608 | reload_config; |
3738 | reload_config; |
3609 | db_init; |
3739 | db_init; |
3610 | |
3740 | |
… | |
… | |
3622 | |
3752 | |
3623 | # no (long-running) fork's whatsoever before this point(!) |
3753 | # no (long-running) fork's whatsoever before this point(!) |
3624 | use POSIX (); |
3754 | use POSIX (); |
3625 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3755 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3626 | |
3756 | |
3627 | (pop @POST_INIT)->(0) while @POST_INIT; |
3757 | cf::_post_init 0; |
3628 | }; |
3758 | }; |
3629 | |
3759 | |
3630 | cf::object::thawer::errors_are_fatal 0; |
3760 | cf::object::thawer::errors_are_fatal 0; |
3631 | warn "parse errors in files are no longer fatal from this point on.\n"; |
3761 | info "parse errors in files are no longer fatal from this point on.\n"; |
3632 | |
3762 | |
|
|
3763 | AE::postpone { |
|
|
3764 | undef &main; # free gobs of memory :) |
|
|
3765 | }; |
|
|
3766 | |
3633 | main_loop; |
3767 | goto &main_loop; |
3634 | } |
3768 | } |
3635 | |
3769 | |
3636 | ############################################################################# |
3770 | ############################################################################# |
3637 | # initialisation and cleanup |
3771 | # initialisation and cleanup |
3638 | |
3772 | |
… | |
… | |
3677 | or return; |
3811 | or return; |
3678 | |
3812 | |
3679 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3813 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3680 | and return; |
3814 | and return; |
3681 | |
3815 | |
3682 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3816 | trace sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3683 | |
3817 | |
3684 | 1 |
3818 | 1 |
3685 | } |
3819 | } |
3686 | |
3820 | |
3687 | our $uuid_lock; |
3821 | our $uuid_lock; |
… | |
… | |
3699 | or return; |
3833 | or return; |
3700 | |
3834 | |
3701 | my $value = uuid_seq uuid_cur; |
3835 | my $value = uuid_seq uuid_cur; |
3702 | |
3836 | |
3703 | unless ($value) { |
3837 | unless ($value) { |
3704 | warn "cowardly refusing to write zero uuid value!\n"; |
3838 | info "cowardly refusing to write zero uuid value!\n"; |
3705 | return; |
3839 | return; |
3706 | } |
3840 | } |
3707 | |
3841 | |
3708 | my $value = uuid_str $value + $uuid_skip; |
3842 | my $value = uuid_str $value + $uuid_skip; |
3709 | $uuid_skip = 0; |
3843 | $uuid_skip = 0; |
… | |
… | |
3719 | or return; |
3853 | or return; |
3720 | |
3854 | |
3721 | aio_rename "$uuid~", $uuid |
3855 | aio_rename "$uuid~", $uuid |
3722 | and return; |
3856 | and return; |
3723 | |
3857 | |
3724 | warn "uuid file written ($value).\n"; |
3858 | trace "uuid file written ($value).\n"; |
3725 | |
3859 | |
3726 | 1 |
3860 | 1 |
3727 | |
3861 | |
3728 | } |
3862 | } |
3729 | |
3863 | |
… | |
… | |
3735 | } |
3869 | } |
3736 | |
3870 | |
3737 | sub emergency_save() { |
3871 | sub emergency_save() { |
3738 | my $freeze_guard = cf::freeze_mainloop; |
3872 | my $freeze_guard = cf::freeze_mainloop; |
3739 | |
3873 | |
3740 | warn "emergency_perl_save: enter\n"; |
3874 | info "emergency_perl_save: enter\n"; |
|
|
3875 | |
|
|
3876 | # this is a trade-off: we want to be very quick here, so |
|
|
3877 | # save all maps without fsync, and later call a global sync |
|
|
3878 | # (which in turn might be very very slow) |
|
|
3879 | local $USE_FSYNC = 0; |
3741 | |
3880 | |
3742 | cf::sync_job { |
3881 | cf::sync_job { |
3743 | # this is a trade-off: we want to be very quick here, so |
3882 | cf::write_runtime_sync; # external watchdog should not bark |
3744 | # save all maps without fsync, and later call a global sync |
|
|
3745 | # (which in turn might be very very slow) |
|
|
3746 | local $USE_FSYNC = 0; |
|
|
3747 | |
3883 | |
3748 | # use a peculiar iteration method to avoid tripping on perl |
3884 | # use a peculiar iteration method to avoid tripping on perl |
3749 | # refcount bugs in for. also avoids problems with players |
3885 | # refcount bugs in for. also avoids problems with players |
3750 | # and maps saved/destroyed asynchronously. |
3886 | # and maps saved/destroyed asynchronously. |
3751 | warn "emergency_perl_save: begin player save\n"; |
3887 | info "emergency_perl_save: begin player save\n"; |
3752 | for my $login (keys %cf::PLAYER) { |
3888 | for my $login (keys %cf::PLAYER) { |
3753 | my $pl = $cf::PLAYER{$login} or next; |
3889 | my $pl = $cf::PLAYER{$login} or next; |
3754 | $pl->valid or next; |
3890 | $pl->valid or next; |
3755 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3891 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3756 | $pl->save; |
3892 | $pl->save; |
3757 | } |
3893 | } |
3758 | warn "emergency_perl_save: end player save\n"; |
3894 | info "emergency_perl_save: end player save\n"; |
3759 | |
3895 | |
|
|
3896 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3897 | |
3760 | warn "emergency_perl_save: begin map save\n"; |
3898 | info "emergency_perl_save: begin map save\n"; |
3761 | for my $path (keys %cf::MAP) { |
3899 | for my $path (keys %cf::MAP) { |
3762 | my $map = $cf::MAP{$path} or next; |
3900 | my $map = $cf::MAP{$path} or next; |
3763 | $map->valid or next; |
3901 | $map->valid or next; |
3764 | $map->save; |
3902 | $map->save; |
3765 | } |
3903 | } |
3766 | warn "emergency_perl_save: end map save\n"; |
3904 | info "emergency_perl_save: end map save\n"; |
3767 | |
3905 | |
|
|
3906 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3907 | |
3768 | warn "emergency_perl_save: begin database checkpoint\n"; |
3908 | info "emergency_perl_save: begin database checkpoint\n"; |
3769 | BDB::db_env_txn_checkpoint $DB_ENV; |
3909 | BDB::db_env_txn_checkpoint $DB_ENV; |
3770 | warn "emergency_perl_save: end database checkpoint\n"; |
3910 | info "emergency_perl_save: end database checkpoint\n"; |
3771 | |
3911 | |
3772 | warn "emergency_perl_save: begin write uuid\n"; |
3912 | info "emergency_perl_save: begin write uuid\n"; |
3773 | write_uuid_sync 1; |
3913 | write_uuid_sync 1; |
3774 | warn "emergency_perl_save: end write uuid\n"; |
3914 | info "emergency_perl_save: end write uuid\n"; |
|
|
3915 | |
|
|
3916 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3917 | |
|
|
3918 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3919 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3920 | |
|
|
3921 | info "emergency_perl_save: starting sync\n"; |
|
|
3922 | IO::AIO::aio_sync sub { |
|
|
3923 | info "emergency_perl_save: finished sync\n"; |
|
|
3924 | }; |
|
|
3925 | |
|
|
3926 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3927 | |
|
|
3928 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3929 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3930 | Coro::AnyEvent::sleep 0.01; # let the sync_job do it's thing |
|
|
3931 | } |
|
|
3932 | |
|
|
3933 | cf::write_runtime_sync; # external watchdog should not bark |
3775 | }; |
3934 | }; |
3776 | |
3935 | |
3777 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3778 | IO::AIO::aio_sync sub { |
|
|
3779 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3780 | }; |
|
|
3781 | |
|
|
3782 | warn "emergency_perl_save: leave\n"; |
3936 | info "emergency_perl_save: leave\n"; |
3783 | } |
3937 | } |
3784 | |
3938 | |
3785 | sub post_cleanup { |
3939 | sub post_cleanup { |
3786 | my ($make_core) = @_; |
3940 | my ($make_core) = @_; |
3787 | |
3941 | |
|
|
3942 | IO::AIO::flush; |
|
|
3943 | |
3788 | warn Carp::longmess "post_cleanup backtrace" |
3944 | error Carp::longmess "post_cleanup backtrace" |
3789 | if $make_core; |
3945 | if $make_core; |
3790 | |
3946 | |
3791 | my $fh = pidfile; |
3947 | my $fh = pidfile; |
3792 | unlink $PIDFILE if <$fh> == $$; |
3948 | unlink $PIDFILE if <$fh> == $$; |
3793 | } |
3949 | } |
… | |
… | |
3817 | } |
3973 | } |
3818 | } |
3974 | } |
3819 | |
3975 | |
3820 | sub do_reload_perl() { |
3976 | sub do_reload_perl() { |
3821 | # can/must only be called in main |
3977 | # can/must only be called in main |
3822 | if (in_main) { |
3978 | unless (in_main) { |
3823 | warn "can only reload from main coroutine"; |
3979 | error "can only reload from main coroutine"; |
3824 | return; |
3980 | return; |
3825 | } |
3981 | } |
3826 | |
3982 | |
3827 | return if $RELOAD++; |
3983 | return if $RELOAD++; |
3828 | |
3984 | |
3829 | my $t1 = AE::time; |
3985 | my $t1 = AE::time; |
3830 | |
3986 | |
3831 | while ($RELOAD) { |
3987 | while ($RELOAD) { |
3832 | warn "reloading..."; |
3988 | cf::get_slot 0.1, -1, "reload_perl"; |
|
|
3989 | info "perl_reload: reloading..."; |
3833 | |
3990 | |
3834 | warn "entering sync_job"; |
3991 | trace "perl_reload: entering sync_job"; |
3835 | |
3992 | |
3836 | cf::sync_job { |
3993 | cf::sync_job { |
3837 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3838 | cf::emergency_save; |
3994 | #cf::emergency_save; |
3839 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3840 | |
3995 | |
3841 | warn "syncing database to disk"; |
|
|
3842 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3843 | |
|
|
3844 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
3845 | |
|
|
3846 | warn "flushing outstanding aio requests"; |
|
|
3847 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3848 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3849 | } |
|
|
3850 | |
|
|
3851 | warn "cancelling all extension coros"; |
3996 | trace "perl_reload: cancelling all extension coros"; |
3852 | $_->cancel for values %EXT_CORO; |
3997 | $_->cancel for values %EXT_CORO; |
3853 | %EXT_CORO = (); |
3998 | %EXT_CORO = (); |
3854 | |
3999 | |
3855 | warn "removing commands"; |
4000 | trace "perl_reload: removing commands"; |
3856 | %COMMAND = (); |
4001 | %COMMAND = (); |
3857 | |
4002 | |
3858 | warn "removing ext/exti commands"; |
4003 | trace "perl_reload: removing ext/exti commands"; |
3859 | %EXTCMD = (); |
4004 | %EXTCMD = (); |
3860 | %EXTICMD = (); |
4005 | %EXTICMD = (); |
3861 | |
4006 | |
3862 | warn "unloading/nuking all extensions"; |
4007 | trace "perl_reload: unloading/nuking all extensions"; |
3863 | for my $pkg (@EXTS) { |
4008 | for my $pkg (@EXTS) { |
3864 | warn "... unloading $pkg"; |
4009 | trace "... unloading $pkg"; |
3865 | |
4010 | |
3866 | if (my $cb = $pkg->can ("unload")) { |
4011 | if (my $cb = $pkg->can ("unload")) { |
3867 | eval { |
4012 | eval { |
3868 | $cb->($pkg); |
4013 | $cb->($pkg); |
3869 | 1 |
4014 | 1 |
3870 | } or warn "$pkg unloaded, but with errors: $@"; |
4015 | } or error "$pkg unloaded, but with errors: $@"; |
3871 | } |
4016 | } |
3872 | |
4017 | |
3873 | warn "... clearing $pkg"; |
4018 | trace "... clearing $pkg"; |
3874 | clear_package $pkg; |
4019 | clear_package $pkg; |
3875 | } |
4020 | } |
3876 | |
4021 | |
3877 | warn "unloading all perl modules loaded from $LIBDIR"; |
4022 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3878 | while (my ($k, $v) = each %INC) { |
4023 | while (my ($k, $v) = each %INC) { |
3879 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
4024 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3880 | |
4025 | |
3881 | warn "... unloading $k"; |
4026 | trace "... unloading $k"; |
3882 | delete $INC{$k}; |
4027 | delete $INC{$k}; |
3883 | |
4028 | |
3884 | $k =~ s/\.pm$//; |
4029 | $k =~ s/\.pm$//; |
3885 | $k =~ s/\//::/g; |
4030 | $k =~ s/\//::/g; |
3886 | |
4031 | |
… | |
… | |
3889 | } |
4034 | } |
3890 | |
4035 | |
3891 | clear_package $k; |
4036 | clear_package $k; |
3892 | } |
4037 | } |
3893 | |
4038 | |
3894 | warn "getting rid of safe::, as good as possible"; |
4039 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3895 | clear_package "safe::$_" |
4040 | clear_package "safe::$_" |
3896 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
4041 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3897 | |
4042 | |
3898 | warn "unloading cf.pm \"a bit\""; |
4043 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3899 | delete $INC{"cf.pm"}; |
4044 | delete $INC{"cf.pm"}; |
3900 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
4045 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3901 | |
4046 | |
3902 | # don't, removes xs symbols, too, |
4047 | # don't, removes xs symbols, too, |
3903 | # and global variables created in xs |
4048 | # and global variables created in xs |
3904 | #clear_package __PACKAGE__; |
4049 | #clear_package __PACKAGE__; |
3905 | |
4050 | |
3906 | warn "unload completed, starting to reload now"; |
4051 | info "perl_reload: unload completed, starting to reload now"; |
3907 | |
4052 | |
3908 | warn "reloading cf.pm"; |
4053 | trace "perl_reload: reloading cf.pm"; |
3909 | require cf; |
4054 | require cf; |
3910 | cf::_connect_to_perl_1; |
4055 | cf::_connect_to_perl_1; |
3911 | |
4056 | |
3912 | warn "loading config and database again"; |
4057 | trace "perl_reload: loading config and database again"; |
3913 | cf::reload_config; |
4058 | cf::reload_config; |
3914 | |
4059 | |
3915 | warn "loading extensions"; |
4060 | trace "perl_reload: loading extensions"; |
3916 | cf::load_extensions; |
4061 | cf::load_extensions; |
3917 | |
4062 | |
3918 | if ($REATTACH_ON_RELOAD) { |
4063 | if ($REATTACH_ON_RELOAD) { |
3919 | warn "reattaching attachments to objects/players"; |
4064 | trace "perl_reload: reattaching attachments to objects/players"; |
3920 | _global_reattach; # objects, sockets |
4065 | _global_reattach; # objects, sockets |
3921 | warn "reattaching attachments to maps"; |
4066 | trace "perl_reload: reattaching attachments to maps"; |
3922 | reattach $_ for values %MAP; |
4067 | reattach $_ for values %MAP; |
3923 | warn "reattaching attachments to players"; |
4068 | trace "perl_reload: reattaching attachments to players"; |
3924 | reattach $_ for values %PLAYER; |
4069 | reattach $_ for values %PLAYER; |
3925 | } |
4070 | } |
3926 | |
4071 | |
3927 | warn "running post_init jobs"; |
4072 | cf::_post_init 1; |
3928 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3929 | |
4073 | |
3930 | warn "leaving sync_job"; |
4074 | trace "perl_reload: leaving sync_job"; |
3931 | |
4075 | |
3932 | 1 |
4076 | 1 |
3933 | } or do { |
4077 | } or do { |
3934 | warn $@; |
4078 | error $@; |
3935 | cf::cleanup "error while reloading, exiting."; |
4079 | cf::cleanup "perl_reload: error, exiting."; |
3936 | }; |
4080 | }; |
3937 | |
4081 | |
3938 | warn "reloaded"; |
|
|
3939 | --$RELOAD; |
4082 | --$RELOAD; |
3940 | } |
4083 | } |
3941 | |
4084 | |
3942 | $t1 = AE::time - $t1; |
4085 | $t1 = AE::time - $t1; |
3943 | warn "reload completed in ${t1}s\n"; |
4086 | info "perl_reload: completed in ${t1}s\n"; |
3944 | }; |
4087 | }; |
3945 | |
4088 | |
3946 | our $RELOAD_WATCHER; # used only during reload |
4089 | our $RELOAD_WATCHER; # used only during reload |
3947 | |
4090 | |
3948 | sub reload_perl() { |
4091 | sub reload_perl() { |
… | |
… | |
3969 | reload_perl; |
4112 | reload_perl; |
3970 | }; |
4113 | }; |
3971 | } |
4114 | } |
3972 | }; |
4115 | }; |
3973 | |
4116 | |
3974 | unshift @INC, $LIBDIR; |
4117 | ############################################################################# |
3975 | |
4118 | |
3976 | my $bug_warning = 0; |
4119 | my $bug_warning = 0; |
3977 | |
4120 | |
3978 | our @WAIT_FOR_TICK; |
|
|
3979 | our @WAIT_FOR_TICK_BEGIN; |
|
|
3980 | |
|
|
3981 | sub wait_for_tick { |
4121 | sub wait_for_tick() { |
3982 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4122 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
3983 | |
4123 | |
3984 | my $signal = new Coro::Signal; |
4124 | $WAIT_FOR_TICK->wait; |
3985 | push @WAIT_FOR_TICK, $signal; |
|
|
3986 | $signal->wait; |
|
|
3987 | } |
4125 | } |
3988 | |
4126 | |
3989 | sub wait_for_tick_begin { |
4127 | sub wait_for_tick_begin() { |
3990 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4128 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
3991 | |
4129 | |
3992 | my $signal = new Coro::Signal; |
4130 | my $signal = new Coro::Signal; |
3993 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4131 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3994 | $signal->wait; |
4132 | $signal->wait; |
3995 | } |
4133 | } |
… | |
… | |
3999 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4137 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4000 | unless ++$bug_warning > 10; |
4138 | unless ++$bug_warning > 10; |
4001 | return; |
4139 | return; |
4002 | } |
4140 | } |
4003 | |
4141 | |
4004 | cf::server_tick; # one server iteration |
4142 | cf::one_tick; # one server iteration |
4005 | |
4143 | |
4006 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4144 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4007 | |
4145 | |
4008 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4146 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4009 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4147 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4010 | Coro::async_pool { |
4148 | Coro::async_pool { |
4011 | $Coro::current->{desc} = "runtime saver"; |
4149 | $Coro::current->{desc} = "runtime saver"; |
4012 | write_runtime_sync |
4150 | write_runtime_sync |
4013 | or warn "ERROR: unable to write runtime file: $!"; |
4151 | or error "ERROR: unable to write runtime file: $!"; |
4014 | }; |
4152 | }; |
4015 | } |
4153 | } |
4016 | |
4154 | |
4017 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4155 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4018 | $sig->send; |
4156 | $sig->send; |
4019 | } |
4157 | } |
4020 | while (my $sig = shift @WAIT_FOR_TICK) { |
4158 | $WAIT_FOR_TICK->broadcast; |
4021 | $sig->send; |
|
|
4022 | } |
|
|
4023 | |
4159 | |
4024 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4160 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4025 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4161 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4026 | |
4162 | |
4027 | if (0) { |
4163 | if (0) { |
4028 | if ($NEXT_TICK) { |
4164 | if ($NEXT_TICK) { |
4029 | my $jitter = $TICK_START - $NEXT_TICK; |
4165 | my $jitter = $TICK_START - $NEXT_TICK; |
4030 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4166 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4031 | warn "jitter $JITTER\n";#d# |
4167 | debug "jitter $JITTER\n";#d# |
4032 | } |
4168 | } |
4033 | } |
4169 | } |
4034 | } |
4170 | } |
4035 | |
4171 | |
4036 | { |
4172 | { |
4037 | # configure BDB |
4173 | # configure BDB |
4038 | |
4174 | |
4039 | BDB::min_parallel 16; |
4175 | BDB::min_parallel 16; |
4040 | BDB::max_poll_reqs $TICK * 0.1; |
4176 | BDB::max_poll_reqs $TICK * 0.1; |
4041 | $AnyEvent::BDB::WATCHER->priority (1); |
4177 | #$AnyEvent::BDB::WATCHER->priority (1); |
4042 | |
4178 | |
4043 | unless ($DB_ENV) { |
4179 | unless ($DB_ENV) { |
4044 | $DB_ENV = BDB::db_env_create; |
4180 | $DB_ENV = BDB::db_env_create; |
4045 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4181 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4046 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4182 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
… | |
… | |
4081 | IO::AIO::min_parallel 8; |
4217 | IO::AIO::min_parallel 8; |
4082 | IO::AIO::max_poll_time $TICK * 0.1; |
4218 | IO::AIO::max_poll_time $TICK * 0.1; |
4083 | undef $AnyEvent::AIO::WATCHER; |
4219 | undef $AnyEvent::AIO::WATCHER; |
4084 | } |
4220 | } |
4085 | |
4221 | |
4086 | my $_log_backtrace; |
4222 | our $_log_backtrace; |
|
|
4223 | our $_log_backtrace_last; |
4087 | |
4224 | |
4088 | sub _log_backtrace { |
4225 | sub _log_backtrace { |
4089 | my ($msg, @addr) = @_; |
4226 | my ($msg, @addr) = @_; |
4090 | |
4227 | |
4091 | $msg =~ s/\n//; |
4228 | $msg =~ s/\n$//; |
4092 | |
4229 | |
|
|
4230 | if ($_log_backtrace_last eq $msg) { |
|
|
4231 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
4232 | LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; |
4093 | # limit the # of concurrent backtraces |
4233 | # limit the # of concurrent backtraces |
4094 | if ($_log_backtrace < 2) { |
4234 | } elsif ($_log_backtrace < 2) { |
|
|
4235 | $_log_backtrace_last = $msg; |
4095 | ++$_log_backtrace; |
4236 | ++$_log_backtrace; |
4096 | my $perl_bt = Carp::longmess $msg; |
4237 | my $perl_bt = Carp::longmess $msg; |
4097 | async { |
4238 | async { |
4098 | $Coro::current->{desc} = "abt $msg"; |
4239 | $Coro::current->{desc} = "abt $msg"; |
4099 | |
4240 | |
… | |
… | |
4119 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4260 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4120 | --$_log_backtrace; |
4261 | --$_log_backtrace; |
4121 | }; |
4262 | }; |
4122 | } else { |
4263 | } else { |
4123 | LOG llevInfo, "[ABT] $msg\n"; |
4264 | LOG llevInfo, "[ABT] $msg\n"; |
4124 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4265 | LOG llevInfo, "[ABT] [overload, suppressed]\n"; |
4125 | } |
4266 | } |
4126 | } |
4267 | } |
4127 | |
4268 | |
4128 | # load additional modules |
4269 | # load additional modules |
4129 | require "cf/$_.pm" for @EXTRA_MODULES; |
4270 | require "cf/$_.pm" for @EXTRA_MODULES; |