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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
5 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
7 | # it under the terms of the GNU General Public License as published by |
7 | # the terms of the Affero GNU General Public License as published by the |
8 | # the Free Software Foundation, either version 3 of the License, or |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
9 | # (at your option) any later version. |
9 | # option) any later version. |
10 | # |
10 | # |
11 | # This program is distributed in the hope that it will be useful, |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | # GNU General Public License for more details. |
14 | # GNU General Public License for more details. |
15 | # |
15 | # |
16 | # You should have received a copy of the GNU General Public License |
16 | # You should have received a copy of the Affero GNU General Public License |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | # and the GNU General Public License along with this program. If not, see |
|
|
18 | # <http://www.gnu.org/licenses/>. |
18 | # |
19 | # |
19 | # 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> |
20 | # |
21 | # |
21 | |
22 | |
22 | package cf; |
23 | package cf; |
23 | |
24 | |
24 | use 5.10.0; |
25 | use common::sense; |
25 | use utf8; |
|
|
26 | use strict "vars", "subs"; |
|
|
27 | |
26 | |
28 | use Symbol; |
27 | use Symbol; |
29 | use List::Util; |
28 | use List::Util; |
30 | use Socket; |
29 | use Socket; |
31 | use EV; |
30 | use EV; |
32 | use Opcode; |
31 | use Opcode; |
33 | use Safe; |
32 | use Safe; |
34 | use Safe::Hole; |
33 | use Safe::Hole; |
35 | use Storable (); |
34 | use Storable (); |
|
|
35 | use Carp (); |
36 | |
36 | |
37 | use Guard (); |
37 | use Guard (); |
38 | use Coro (); |
38 | use Coro (); |
39 | use Coro::State; |
39 | use Coro::State; |
40 | use Coro::Handle; |
40 | use Coro::Handle; |
41 | use Coro::EV; |
41 | use Coro::EV; |
42 | use Coro::AnyEvent; |
42 | use Coro::AnyEvent; |
43 | use Coro::Timer; |
43 | use Coro::Timer; |
44 | use Coro::Signal; |
44 | use Coro::Signal; |
45 | use Coro::Semaphore; |
45 | use Coro::Semaphore; |
|
|
46 | use Coro::SemaphoreSet; |
46 | use Coro::AnyEvent; |
47 | use Coro::AnyEvent; |
47 | use Coro::AIO; |
48 | use Coro::AIO; |
48 | use Coro::BDB 1.6; |
49 | use Coro::BDB 1.6; |
49 | use Coro::Storable; |
50 | use Coro::Storable; |
50 | use Coro::Util (); |
51 | use Coro::Util (); |
51 | |
52 | |
52 | use JSON::XS 2.01 (); |
53 | use JSON::XS 2.01 (); |
53 | use BDB (); |
54 | use BDB (); |
54 | use Data::Dumper; |
55 | use Data::Dumper; |
55 | use Digest::MD5; |
|
|
56 | use Fcntl; |
56 | use Fcntl; |
57 | use YAML (); |
57 | use YAML::XS (); |
58 | use IO::AIO (); |
58 | use IO::AIO (); |
59 | use Time::HiRes; |
59 | use Time::HiRes; |
60 | use Compress::LZF; |
60 | use Compress::LZF; |
61 | use Digest::MD5 (); |
61 | use Digest::MD5 (); |
62 | |
62 | |
… | |
… | |
71 | |
71 | |
72 | # make sure c-lzf reinitialises itself |
72 | # make sure c-lzf reinitialises itself |
73 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
73 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
74 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
74 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
75 | |
75 | |
|
|
76 | # strictly for debugging |
|
|
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
|
|
78 | |
76 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
80 | |
|
|
81 | our @ORIG_INC; |
77 | |
82 | |
78 | our %COMMAND = (); |
83 | our %COMMAND = (); |
79 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
80 | |
85 | |
81 | our @EXTS = (); # list of extension package names |
86 | our @EXTS = (); # list of extension package names |
… | |
… | |
84 | our %EXT_CORO = (); # coroutines bound to extensions |
89 | our %EXT_CORO = (); # coroutines bound to extensions |
85 | our %EXT_MAP = (); # pluggable maps |
90 | our %EXT_MAP = (); # pluggable maps |
86 | |
91 | |
87 | our $RELOAD; # number of reloads so far, non-zero while in reload |
92 | our $RELOAD; # number of reloads so far, non-zero while in reload |
88 | our @EVENT; |
93 | our @EVENT; |
|
|
94 | our @REFLECT; # set by XS |
|
|
95 | our %REFLECT; # set by us |
89 | |
96 | |
90 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
91 | our $DATADIR = datadir; |
98 | our $DATADIR = datadir; |
92 | our $LIBDIR = "$DATADIR/ext"; |
99 | our $LIBDIR = "$DATADIR/ext"; |
93 | our $PODDIR = "$DATADIR/pod"; |
100 | our $PODDIR = "$DATADIR/pod"; |
… | |
… | |
99 | our $RANDOMDIR = "$LOCALDIR/random"; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
100 | our $BDBDIR = "$LOCALDIR/db"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
101 | our $PIDFILE = "$LOCALDIR/pid"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
102 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
103 | |
110 | |
104 | our %RESOURCE; |
111 | our %RESOURCE; # unused |
|
|
112 | |
|
|
113 | our $OUTPUT_RATE_MIN = 3000; |
|
|
114 | our $OUTPUT_RATE_MAX = 1000000; |
|
|
115 | |
|
|
116 | our $MAX_LINKS = 32; # how many chained exits to follow |
|
|
117 | our $VERBOSE_IO = 1; |
105 | |
118 | |
106 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
119 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
107 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
120 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
108 | our $NEXT_TICK; |
121 | our $NEXT_TICK; |
109 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
122 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
110 | |
123 | |
111 | our $BDB_DEADLOCK_WATCHER; |
124 | our $BDB_DEADLOCK_WATCHER; |
112 | our $BDB_CHECKPOINT_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
113 | our $BDB_TRICKLE_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
114 | our $DB_ENV; |
127 | our $DB_ENV; |
115 | |
128 | |
|
|
129 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
|
|
130 | |
116 | our %CFG; |
131 | our %CFG; |
117 | |
132 | |
118 | our $UPTIME; $UPTIME ||= time; |
133 | our $UPTIME; $UPTIME ||= time; |
119 | our $RUNTIME; |
134 | our $RUNTIME; |
120 | our $NOW; |
135 | our $NOW; |
… | |
… | |
131 | our $JITTER; # average jitter |
146 | our $JITTER; # average jitter |
132 | our $TICK_START; # for load detecting purposes |
147 | our $TICK_START; # for load detecting purposes |
133 | |
148 | |
134 | our @POST_INIT; |
149 | our @POST_INIT; |
135 | |
150 | |
136 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
151 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
152 | our $REALLY_UNLOOP; # never set to true, please :) |
137 | |
153 | |
138 | binmode STDOUT; |
154 | binmode STDOUT; |
139 | binmode STDERR; |
155 | binmode STDERR; |
140 | |
156 | |
141 | # read virtual server time, if available |
157 | # read virtual server time, if available |
… | |
… | |
143 | open my $fh, "<", $RUNTIMEFILE |
159 | open my $fh, "<", $RUNTIMEFILE |
144 | or die "unable to read $RUNTIMEFILE file: $!"; |
160 | or die "unable to read $RUNTIMEFILE file: $!"; |
145 | $RUNTIME = <$fh> + 0.; |
161 | $RUNTIME = <$fh> + 0.; |
146 | } |
162 | } |
147 | |
163 | |
|
|
164 | eval "sub TICK() { $TICK } 1" or die; |
|
|
165 | |
148 | mkdir $_ |
166 | mkdir $_ |
149 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
167 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
150 | |
168 | |
151 | our $EMERGENCY_POSITION; |
169 | our $EMERGENCY_POSITION; |
152 | |
170 | |
153 | sub cf::map::normalise; |
171 | sub cf::map::normalise; |
|
|
172 | |
|
|
173 | sub in_main() { |
|
|
174 | $Coro::current == $Coro::main |
|
|
175 | } |
|
|
176 | |
|
|
177 | ############################################################################# |
|
|
178 | |
|
|
179 | %REFLECT = (); |
|
|
180 | for (@REFLECT) { |
|
|
181 | my $reflect = JSON::XS::decode_json $_; |
|
|
182 | $REFLECT{$reflect->{class}} = $reflect; |
|
|
183 | } |
|
|
184 | |
|
|
185 | # this is decidedly evil |
|
|
186 | $REFLECT{object}{flags} = { map +($_ => undef), grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"} }; |
154 | |
187 | |
155 | ############################################################################# |
188 | ############################################################################# |
156 | |
189 | |
157 | =head2 GLOBAL VARIABLES |
190 | =head2 GLOBAL VARIABLES |
158 | |
191 | |
… | |
… | |
200 | from wherever your confdir points to. |
233 | from wherever your confdir points to. |
201 | |
234 | |
202 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
203 | |
236 | |
204 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
205 | returns directly I<after> the tick processing (and consequently, can only wake one process |
238 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
206 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
207 | |
240 | |
|
|
241 | =cut |
|
|
242 | |
|
|
243 | sub wait_for_tick(); |
|
|
244 | sub wait_for_tick_begin(); |
|
|
245 | |
208 | =item @cf::INVOKE_RESULTS |
246 | =item @cf::INVOKE_RESULTS |
209 | |
247 | |
210 | This array contains the results of the last C<invoke ()> call. When |
248 | This array contains the results of the last C<invoke ()> call. When |
211 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
249 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
212 | that call. |
250 | that call. |
213 | |
251 | |
|
|
252 | =item %cf::REFLECT |
|
|
253 | |
|
|
254 | Contains, for each (C++) class name, a hash reference with information |
|
|
255 | about object members (methods, scalars, arrays and flags) and other |
|
|
256 | metadata, which is useful for introspection. |
|
|
257 | |
214 | =back |
258 | =back |
215 | |
259 | |
216 | =cut |
260 | =cut |
217 | |
261 | |
218 | BEGIN { |
262 | sub error(@) { LOG llevError, join "", @_ } |
219 | *CORE::GLOBAL::warn = sub { |
263 | sub warn (@) { LOG llevWarn , join "", @_ } |
|
|
264 | sub info (@) { LOG llevInfo , join "", @_ } |
|
|
265 | sub debug(@) { LOG llevDebug, join "", @_ } |
|
|
266 | sub trace(@) { LOG llevTrace, join "", @_ } |
|
|
267 | |
|
|
268 | $Coro::State::WARNHOOK = sub { |
220 | my $msg = join "", @_; |
269 | my $msg = join "", @_; |
221 | |
270 | |
222 | $msg .= "\n" |
271 | $msg .= "\n" |
223 | unless $msg =~ /\n$/; |
272 | unless $msg =~ /\n$/; |
224 | |
273 | |
225 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
274 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
226 | |
275 | |
227 | LOG llevError, $msg; |
276 | LOG llevWarn, $msg; |
228 | }; |
277 | }; |
229 | } |
|
|
230 | |
278 | |
231 | $Coro::State::DIEHOOK = sub { |
279 | $Coro::State::DIEHOOK = sub { |
232 | return unless $^S eq 0; # "eq", not "==" |
280 | return unless $^S eq 0; # "eq", not "==" |
233 | |
281 | |
234 | if ($Coro::current == $Coro::main) {#d# |
282 | error Carp::longmess $_[0]; |
|
|
283 | |
|
|
284 | if (in_main) {#d# |
235 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
285 | error "DIEHOOK called in main context, Coro bug?\n";#d# |
236 | return;#d# |
286 | return;#d# |
237 | }#d# |
287 | }#d# |
238 | |
288 | |
239 | # kill coroutine otherwise |
289 | # kill coroutine otherwise |
240 | warn Carp::longmess $_[0]; |
|
|
241 | Coro::terminate |
290 | Coro::terminate |
242 | }; |
291 | }; |
243 | |
|
|
244 | $SIG{__DIE__} = sub { }; #d#? |
|
|
245 | |
292 | |
246 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
293 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
247 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
294 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
248 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
295 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
249 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
296 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
263 | )) { |
310 | )) { |
264 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
311 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
265 | } |
312 | } |
266 | |
313 | |
267 | $EV::DIED = sub { |
314 | $EV::DIED = sub { |
268 | warn "error in event callback: @_"; |
315 | Carp::cluck "error in event callback: @_"; |
269 | }; |
316 | }; |
270 | |
317 | |
271 | ############################################################################# |
318 | ############################################################################# |
272 | |
319 | |
273 | =head2 UTILITY FUNCTIONS |
320 | =head2 UTILITY FUNCTIONS |
… | |
… | |
310 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
311 | |
358 | |
312 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
313 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
314 | |
361 | |
|
|
362 | =item $ref = cf::yaml_load $scalar |
|
|
363 | |
|
|
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
365 | |
|
|
366 | =cut |
|
|
367 | |
|
|
368 | sub fork_call(&@); |
|
|
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
372 | } |
|
|
373 | |
315 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
316 | |
375 | |
317 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
318 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
319 | |
378 | |
320 | The cdoeblock will have a single boolean argument to indicate whether this |
379 | The codeblock will have a single boolean argument to indicate whether this |
321 | is a reload or not. |
380 | is a reload or not. |
322 | |
381 | |
323 | =cut |
382 | =cut |
324 | |
383 | |
325 | sub post_init(&) { |
384 | sub post_init(&) { |
326 | push @POST_INIT, shift; |
385 | push @POST_INIT, shift; |
|
|
386 | } |
|
|
387 | |
|
|
388 | sub _post_init { |
|
|
389 | trace "running post_init jobs"; |
|
|
390 | |
|
|
391 | # run them in parallel... |
|
|
392 | |
|
|
393 | my @join; |
|
|
394 | |
|
|
395 | while () { |
|
|
396 | push @join, map &Coro::async ($_, 0), @POST_INIT; |
|
|
397 | @POST_INIT = (); |
|
|
398 | |
|
|
399 | @join or last; |
|
|
400 | |
|
|
401 | (pop @join)->join; |
|
|
402 | } |
327 | } |
403 | } |
328 | |
404 | |
329 | =item cf::lock_wait $string |
405 | =item cf::lock_wait $string |
330 | |
406 | |
331 | Wait until the given lock is available. See cf::lock_acquire. |
407 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
347 | |
423 | |
348 | Return true if the lock is currently active, i.e. somebody has locked it. |
424 | Return true if the lock is currently active, i.e. somebody has locked it. |
349 | |
425 | |
350 | =cut |
426 | =cut |
351 | |
427 | |
352 | our %LOCK; |
428 | our $LOCKS = new Coro::SemaphoreSet; |
353 | our %LOCKER;#d# |
|
|
354 | |
429 | |
355 | sub lock_wait($) { |
430 | sub lock_wait($) { |
356 | my ($key) = @_; |
431 | $LOCKS->wait ($_[0]); |
357 | |
|
|
358 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
359 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
360 | return;#d# |
|
|
361 | }#d# |
|
|
362 | |
|
|
363 | # wait for lock, if any |
|
|
364 | while ($LOCK{$key}) { |
|
|
365 | #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>"; |
|
|
366 | push @{ $LOCK{$key} }, $Coro::current; |
|
|
367 | Coro::schedule; |
|
|
368 | } |
|
|
369 | } |
432 | } |
370 | |
433 | |
371 | sub lock_acquire($) { |
434 | sub lock_acquire($) { |
372 | my ($key) = @_; |
435 | $LOCKS->guard ($_[0]) |
373 | |
|
|
374 | # wait, to be sure we are not locked |
|
|
375 | lock_wait $key; |
|
|
376 | |
|
|
377 | $LOCK{$key} = []; |
|
|
378 | $LOCKER{$key} = $Coro::current;#d# |
|
|
379 | |
|
|
380 | Guard::guard { |
|
|
381 | delete $LOCKER{$key};#d# |
|
|
382 | # wake up all waiters, to be on the safe side |
|
|
383 | $_->ready for @{ delete $LOCK{$key} }; |
|
|
384 | } |
|
|
385 | } |
436 | } |
386 | |
437 | |
387 | sub lock_active($) { |
438 | sub lock_active($) { |
388 | my ($key) = @_; |
439 | $LOCKS->count ($_[0]) < 1 |
389 | |
|
|
390 | ! ! $LOCK{$key} |
|
|
391 | } |
440 | } |
392 | |
441 | |
393 | sub freeze_mainloop { |
442 | sub freeze_mainloop { |
394 | tick_inhibit_inc; |
443 | tick_inhibit_inc; |
395 | |
444 | |
… | |
… | |
397 | } |
446 | } |
398 | |
447 | |
399 | =item cf::periodic $interval, $cb |
448 | =item cf::periodic $interval, $cb |
400 | |
449 | |
401 | Like EV::periodic, but randomly selects a starting point so that the actions |
450 | Like EV::periodic, but randomly selects a starting point so that the actions |
402 | get spread over timer. |
451 | get spread over time. |
403 | |
452 | |
404 | =cut |
453 | =cut |
405 | |
454 | |
406 | sub periodic($$) { |
455 | sub periodic($$) { |
407 | my ($interval, $cb) = @_; |
456 | my ($interval, $cb) = @_; |
… | |
… | |
424 | |
473 | |
425 | =cut |
474 | =cut |
426 | |
475 | |
427 | our @SLOT_QUEUE; |
476 | our @SLOT_QUEUE; |
428 | our $SLOT_QUEUE; |
477 | our $SLOT_QUEUE; |
|
|
478 | our $SLOT_DECAY = 0.9; |
429 | |
479 | |
430 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
480 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
431 | $SLOT_QUEUE = Coro::async { |
481 | $SLOT_QUEUE = Coro::async { |
432 | $Coro::current->desc ("timeslot manager"); |
482 | $Coro::current->desc ("timeslot manager"); |
433 | |
483 | |
434 | my $signal = new Coro::Signal; |
484 | my $signal = new Coro::Signal; |
|
|
485 | my $busy; |
435 | |
486 | |
436 | while () { |
487 | while () { |
437 | next_job: |
488 | next_job: |
|
|
489 | |
438 | my $avail = cf::till_tick; |
490 | my $avail = cf::till_tick; |
439 | if ($avail > 0.01) { |
491 | |
440 | for (0 .. $#SLOT_QUEUE) { |
492 | for (0 .. $#SLOT_QUEUE) { |
441 | if ($SLOT_QUEUE[$_][0] < $avail) { |
493 | if ($SLOT_QUEUE[$_][0] <= $avail) { |
|
|
494 | $busy = 0; |
442 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
495 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
443 | $job->[2]->send; |
496 | $job->[2]->send; |
444 | Coro::cede; |
497 | Coro::cede; |
445 | goto next_job; |
498 | goto next_job; |
446 | } |
499 | } else { |
|
|
500 | $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; |
447 | } |
501 | } |
448 | } |
502 | } |
449 | |
503 | |
450 | if (@SLOT_QUEUE) { |
504 | if (@SLOT_QUEUE) { |
451 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
505 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
452 | push @cf::WAIT_FOR_TICK, $signal; |
506 | push @cf::WAIT_FOR_TICK, $signal; |
453 | $signal->wait; |
507 | $signal->wait; |
454 | } else { |
508 | } else { |
|
|
509 | $busy = 0; |
455 | Coro::schedule; |
510 | Coro::schedule; |
456 | } |
511 | } |
457 | } |
512 | } |
458 | }; |
513 | }; |
459 | |
514 | |
460 | sub get_slot($;$$) { |
515 | sub get_slot($;$$) { |
461 | return if tick_inhibit || $Coro::current == $Coro::main; |
516 | return if tick_inhibit || $Coro::current == $Coro::main; |
462 | |
517 | |
463 | my ($time, $pri, $name) = @_; |
518 | my ($time, $pri, $name) = @_; |
464 | |
519 | |
465 | $time = $TICK * .6 if $time > $TICK * .6; |
520 | $time = clamp $time, 0.01, $TICK * .6; |
|
|
521 | |
466 | my $sig = new Coro::Signal; |
522 | my $sig = new Coro::Signal; |
467 | |
523 | |
468 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
524 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
469 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
525 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
470 | $SLOT_QUEUE->ready; |
526 | $SLOT_QUEUE->ready; |
… | |
… | |
496 | =cut |
552 | =cut |
497 | |
553 | |
498 | sub sync_job(&) { |
554 | sub sync_job(&) { |
499 | my ($job) = @_; |
555 | my ($job) = @_; |
500 | |
556 | |
501 | if ($Coro::current == $Coro::main) { |
557 | if (in_main) { |
502 | my $time = EV::time; |
558 | my $time = AE::time; |
503 | |
559 | |
504 | # this is the main coro, too bad, we have to block |
560 | # this is the main coro, too bad, we have to block |
505 | # till the operation succeeds, freezing the server :/ |
561 | # till the operation succeeds, freezing the server :/ |
506 | |
562 | |
507 | LOG llevError, Carp::longmess "sync job";#d# |
563 | #LOG llevError, Carp::longmess "sync job";#d# |
508 | |
564 | |
509 | my $freeze_guard = freeze_mainloop; |
565 | my $freeze_guard = freeze_mainloop; |
510 | |
566 | |
511 | my $busy = 1; |
567 | my $busy = 1; |
512 | my @res; |
568 | my @res; |
513 | |
569 | |
514 | (async { |
570 | (async { |
515 | $Coro::current->desc ("sync job coro"); |
571 | $Coro::current->desc ("sync job coro"); |
516 | @res = eval { $job->() }; |
572 | @res = eval { $job->() }; |
517 | warn $@ if $@; |
573 | error $@ if $@; |
518 | undef $busy; |
574 | undef $busy; |
519 | })->prio (Coro::PRIO_MAX); |
575 | })->prio (Coro::PRIO_MAX); |
520 | |
576 | |
521 | while ($busy) { |
577 | while ($busy) { |
522 | if (Coro::nready) { |
578 | if (Coro::nready) { |
… | |
… | |
524 | } else { |
580 | } else { |
525 | EV::loop EV::LOOP_ONESHOT; |
581 | EV::loop EV::LOOP_ONESHOT; |
526 | } |
582 | } |
527 | } |
583 | } |
528 | |
584 | |
529 | my $time = EV::time - $time; |
585 | my $time = AE::time - $time; |
530 | |
586 | |
531 | $TICK_START += $time; # do not account sync jobs to server load |
587 | $TICK_START += $time; # do not account sync jobs to server load |
532 | |
588 | |
533 | wantarray ? @res : $res[0] |
589 | wantarray ? @res : $res[0] |
534 | } else { |
590 | } else { |
… | |
… | |
556 | $EXT_CORO{$coro+0} = $coro; |
612 | $EXT_CORO{$coro+0} = $coro; |
557 | |
613 | |
558 | $coro |
614 | $coro |
559 | } |
615 | } |
560 | |
616 | |
561 | =item fork_call { }, $args |
617 | =item fork_call { }, @args |
562 | |
618 | |
563 | Executes the given code block with the given arguments in a seperate |
619 | Executes the given code block with the given arguments in a seperate |
564 | process, returning the results. Everything must be serialisable with |
620 | process, returning the results. Everything must be serialisable with |
565 | Coro::Storable. May, of course, block. Note that the executed sub may |
621 | Coro::Storable. May, of course, block. Note that the executed sub may |
566 | never block itself or use any form of event handling. |
622 | never block itself or use any form of event handling. |
567 | |
623 | |
568 | =cut |
624 | =cut |
569 | |
625 | |
|
|
626 | sub post_fork { |
|
|
627 | reset_signals; |
|
|
628 | } |
|
|
629 | |
570 | sub fork_call(&@) { |
630 | sub fork_call(&@) { |
571 | my ($cb, @args) = @_; |
631 | my ($cb, @args) = @_; |
572 | |
632 | |
573 | # we seemingly have to make a local copy of the whole thing, |
633 | # we seemingly have to make a local copy of the whole thing, |
574 | # otherwise perl prematurely frees the stuff :/ |
634 | # otherwise perl prematurely frees the stuff :/ |
575 | # TODO: investigate and fix (likely this will be rather laborious) |
635 | # TODO: investigate and fix (likely this will be rather laborious) |
576 | |
636 | |
577 | my @res = Coro::Util::fork_eval { |
637 | my @res = Coro::Util::fork_eval { |
578 | reset_signals; |
638 | cf::post_fork; |
579 | &$cb |
639 | &$cb |
580 | }, @args; |
640 | } @args; |
581 | |
641 | |
582 | wantarray ? @res : $res[-1] |
642 | wantarray ? @res : $res[-1] |
|
|
643 | } |
|
|
644 | |
|
|
645 | sub objinfo { |
|
|
646 | ( |
|
|
647 | "counter value" => cf::object::object_count, |
|
|
648 | "objects created" => cf::object::create_count, |
|
|
649 | "objects destroyed" => cf::object::destroy_count, |
|
|
650 | "freelist size" => cf::object::free_count, |
|
|
651 | "allocated objects" => cf::object::objects_size, |
|
|
652 | "active objects" => cf::object::actives_size, |
|
|
653 | ) |
583 | } |
654 | } |
584 | |
655 | |
585 | =item $coin = coin_from_name $name |
656 | =item $coin = coin_from_name $name |
586 | |
657 | |
587 | =cut |
658 | =cut |
… | |
… | |
624 | within each server. |
695 | within each server. |
625 | |
696 | |
626 | =cut |
697 | =cut |
627 | |
698 | |
628 | sub db_table($) { |
699 | sub db_table($) { |
|
|
700 | cf::error "db_get called from main context" |
|
|
701 | if $Coro::current == $Coro::main; |
|
|
702 | |
629 | my ($name) = @_; |
703 | my ($name) = @_; |
630 | my $db = BDB::db_create $DB_ENV; |
704 | my $db = BDB::db_create $DB_ENV; |
631 | |
705 | |
632 | eval { |
706 | eval { |
633 | $db->set_flags (BDB::CHKSUM); |
707 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
643 | } |
717 | } |
644 | |
718 | |
645 | our $DB; |
719 | our $DB; |
646 | |
720 | |
647 | sub db_init { |
721 | sub db_init { |
648 | cf::sync_job { |
|
|
649 | $DB ||= db_table "db"; |
722 | $DB ||= db_table "db"; |
650 | }; |
|
|
651 | } |
723 | } |
652 | |
724 | |
653 | sub db_get($$) { |
725 | sub db_get($$) { |
654 | my $key = "$_[0]/$_[1]"; |
726 | my $key = "$_[0]/$_[1]"; |
655 | |
727 | |
656 | cf::sync_job { |
728 | cf::error "db_get called from main context" |
|
|
729 | if $Coro::current == $Coro::main; |
|
|
730 | |
657 | BDB::db_get $DB, undef, $key, my $data; |
731 | BDB::db_get $DB, undef, $key, my $data; |
658 | |
732 | |
659 | $! ? () |
733 | $! ? () |
660 | : $data |
734 | : $data |
661 | } |
|
|
662 | } |
735 | } |
663 | |
736 | |
664 | sub db_put($$$) { |
737 | sub db_put($$$) { |
665 | BDB::dbreq_pri 4; |
738 | BDB::dbreq_pri 4; |
666 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
739 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
722 | |
795 | |
723 | my $t1 = Time::HiRes::time; |
796 | my $t1 = Time::HiRes::time; |
724 | my $data = $process->(\@data); |
797 | my $data = $process->(\@data); |
725 | my $t2 = Time::HiRes::time; |
798 | my $t2 = Time::HiRes::time; |
726 | |
799 | |
727 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
800 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
728 | |
801 | |
729 | db_put cache => "$id/data", $data; |
802 | db_put cache => "$id/data", $data; |
730 | db_put cache => "$id/md5" , $md5; |
803 | db_put cache => "$id/md5" , $md5; |
731 | db_put cache => "$id/meta", $meta; |
804 | db_put cache => "$id/meta", $meta; |
732 | |
805 | |
… | |
… | |
742 | |
815 | |
743 | =cut |
816 | =cut |
744 | |
817 | |
745 | sub datalog($@) { |
818 | sub datalog($@) { |
746 | my ($type, %kv) = @_; |
819 | my ($type, %kv) = @_; |
747 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
820 | info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
748 | } |
821 | } |
749 | |
822 | |
750 | =back |
823 | =back |
751 | |
824 | |
752 | =cut |
825 | =cut |
… | |
… | |
947 | |
1020 | |
948 | } elsif (exists $cb_id{$type}) { |
1021 | } elsif (exists $cb_id{$type}) { |
949 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
1022 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
950 | |
1023 | |
951 | } elsif (ref $type) { |
1024 | } elsif (ref $type) { |
952 | warn "attaching objects not supported, ignoring.\n"; |
1025 | error "attaching objects not supported, ignoring.\n"; |
953 | |
1026 | |
954 | } else { |
1027 | } else { |
955 | shift @arg; |
1028 | shift @arg; |
956 | warn "attach argument '$type' not supported, ignoring.\n"; |
1029 | error "attach argument '$type' not supported, ignoring.\n"; |
957 | } |
1030 | } |
958 | } |
1031 | } |
959 | } |
1032 | } |
960 | |
1033 | |
961 | sub _object_attach { |
1034 | sub _object_attach { |
… | |
… | |
971 | _attach $registry, $klass, @attach; |
1044 | _attach $registry, $klass, @attach; |
972 | } |
1045 | } |
973 | |
1046 | |
974 | $obj->{$name} = \%arg; |
1047 | $obj->{$name} = \%arg; |
975 | } else { |
1048 | } else { |
976 | warn "object uses attachment '$name' which is not available, postponing.\n"; |
1049 | info "object uses attachment '$name' which is not available, postponing.\n"; |
977 | } |
1050 | } |
978 | |
1051 | |
979 | $obj->{_attachment}{$name} = undef; |
1052 | $obj->{_attachment}{$name} = undef; |
980 | } |
1053 | } |
981 | |
1054 | |
… | |
… | |
1040 | |
1113 | |
1041 | for (@$callbacks) { |
1114 | for (@$callbacks) { |
1042 | eval { &{$_->[1]} }; |
1115 | eval { &{$_->[1]} }; |
1043 | |
1116 | |
1044 | if ($@) { |
1117 | if ($@) { |
1045 | warn "$@"; |
|
|
1046 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1118 | error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1047 | override; |
1119 | override; |
1048 | } |
1120 | } |
1049 | |
1121 | |
1050 | return 1 if $override; |
1122 | return 1 if $override; |
1051 | } |
1123 | } |
… | |
… | |
1130 | for (@$attach) { |
1202 | for (@$attach) { |
1131 | my ($klass, @attach) = @$_; |
1203 | my ($klass, @attach) = @$_; |
1132 | _attach $registry, $klass, @attach; |
1204 | _attach $registry, $klass, @attach; |
1133 | } |
1205 | } |
1134 | } else { |
1206 | } else { |
1135 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
1207 | info "object uses attachment '$name' that is not available, postponing.\n"; |
1136 | } |
1208 | } |
1137 | } |
1209 | } |
1138 | } |
1210 | } |
1139 | |
1211 | |
1140 | cf::attachable->attach ( |
1212 | cf::attachable->attach ( |
… | |
… | |
1167 | my ($filename, $rdata, $objs) = @_; |
1239 | my ($filename, $rdata, $objs) = @_; |
1168 | |
1240 | |
1169 | sync_job { |
1241 | sync_job { |
1170 | if (length $$rdata) { |
1242 | if (length $$rdata) { |
1171 | utf8::decode (my $decname = $filename); |
1243 | utf8::decode (my $decname = $filename); |
1172 | warn sprintf "saving %s (%d,%d)\n", |
1244 | trace sprintf "saving %s (%d,%d)\n", |
1173 | $decname, length $$rdata, scalar @$objs; |
1245 | $decname, length $$rdata, scalar @$objs |
|
|
1246 | if $VERBOSE_IO; |
1174 | |
1247 | |
1175 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1248 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1176 | aio_chmod $fh, SAVE_MODE; |
1249 | aio_chmod $fh, SAVE_MODE; |
1177 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1250 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1178 | aio_fsync $fh if $cf::USE_FSYNC; |
1251 | if ($cf::USE_FSYNC) { |
|
|
1252 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1253 | aio_fsync $fh; |
|
|
1254 | } |
1179 | aio_close $fh; |
1255 | aio_close $fh; |
1180 | |
1256 | |
1181 | if (@$objs) { |
1257 | if (@$objs) { |
1182 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1258 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1183 | aio_chmod $fh, SAVE_MODE; |
1259 | aio_chmod $fh, SAVE_MODE; |
1184 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1260 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1185 | aio_write $fh, 0, (length $data), $data, 0; |
1261 | aio_write $fh, 0, (length $data), $data, 0; |
1186 | aio_fsync $fh if $cf::USE_FSYNC; |
1262 | if ($cf::USE_FSYNC) { |
|
|
1263 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1264 | aio_fsync $fh; |
|
|
1265 | } |
1187 | aio_close $fh; |
1266 | aio_close $fh; |
1188 | aio_rename "$filename.pst~", "$filename.pst"; |
1267 | aio_rename "$filename.pst~", "$filename.pst"; |
1189 | } |
1268 | } |
1190 | } else { |
1269 | } else { |
1191 | aio_unlink "$filename.pst"; |
1270 | aio_unlink "$filename.pst"; |
… | |
… | |
1194 | aio_rename "$filename~", $filename; |
1273 | aio_rename "$filename~", $filename; |
1195 | |
1274 | |
1196 | $filename =~ s%/[^/]+$%%; |
1275 | $filename =~ s%/[^/]+$%%; |
1197 | aio_pathsync $filename if $cf::USE_FSYNC; |
1276 | aio_pathsync $filename if $cf::USE_FSYNC; |
1198 | } else { |
1277 | } else { |
1199 | warn "FATAL: $filename~: $!\n"; |
1278 | error "unable to save objects: $filename~: $!\n"; |
1200 | } |
1279 | } |
1201 | } else { |
1280 | } else { |
1202 | aio_unlink $filename; |
1281 | aio_unlink $filename; |
1203 | aio_unlink "$filename.pst"; |
1282 | aio_unlink "$filename.pst"; |
1204 | } |
1283 | } |
… | |
… | |
1228 | my $st = eval { Coro::Storable::thaw $av }; |
1307 | my $st = eval { Coro::Storable::thaw $av }; |
1229 | $av = $st->{objs}; |
1308 | $av = $st->{objs}; |
1230 | } |
1309 | } |
1231 | |
1310 | |
1232 | utf8::decode (my $decname = $filename); |
1311 | utf8::decode (my $decname = $filename); |
1233 | warn sprintf "loading %s (%d,%d)\n", |
1312 | trace sprintf "loading %s (%d,%d)\n", |
1234 | $decname, length $data, scalar @{$av || []}; |
1313 | $decname, length $data, scalar @{$av || []} |
|
|
1314 | if $VERBOSE_IO; |
1235 | |
1315 | |
1236 | ($data, $av) |
1316 | ($data, $av) |
1237 | } |
1317 | } |
1238 | |
1318 | |
1239 | =head2 COMMAND CALLBACKS |
1319 | =head2 COMMAND CALLBACKS |
… | |
… | |
1298 | } |
1378 | } |
1299 | |
1379 | |
1300 | use File::Glob (); |
1380 | use File::Glob (); |
1301 | |
1381 | |
1302 | cf::player->attach ( |
1382 | cf::player->attach ( |
1303 | on_command => sub { |
1383 | on_unknown_command => sub { |
1304 | my ($pl, $name, $params) = @_; |
1384 | my ($pl, $name, $params) = @_; |
1305 | |
1385 | |
1306 | my $cb = $COMMAND{$name} |
1386 | my $cb = $COMMAND{$name} |
1307 | or return; |
1387 | or return; |
1308 | |
1388 | |
… | |
… | |
1331 | |
1411 | |
1332 | $pl->ext_reply ($reply, @reply) |
1412 | $pl->ext_reply ($reply, @reply) |
1333 | if $reply; |
1413 | if $reply; |
1334 | |
1414 | |
1335 | } else { |
1415 | } else { |
1336 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1416 | error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1337 | } |
1417 | } |
1338 | |
1418 | |
1339 | cf::override; |
1419 | cf::override; |
1340 | }, |
1420 | }, |
1341 | ); |
1421 | ); |
1342 | |
1422 | |
1343 | # "readahead" all extensions |
1423 | # "readahead" all extensions |
1344 | sub cache_extensions { |
1424 | sub cache_extensions { |
1345 | my $grp = IO::AIO::aio_group; |
1425 | my $grp = IO::AIO::aio_group; |
1346 | |
1426 | |
1347 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1427 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1348 | for (grep /\.ext$/, @{$_[0]}) { |
1428 | for (grep /\.ext$/, @{$_[0]}) { |
1349 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1429 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1350 | } |
1430 | } |
1351 | }; |
1431 | }; |
1352 | |
1432 | |
1353 | $grp |
1433 | $grp |
1354 | } |
1434 | } |
1355 | |
1435 | |
1356 | sub load_extensions { |
1436 | sub load_extensions { |
|
|
1437 | info "loading extensions..."; |
|
|
1438 | |
1357 | cf::sync_job { |
1439 | cf::sync_job { |
1358 | my %todo; |
1440 | my %todo; |
1359 | |
1441 | |
1360 | for my $path (<$LIBDIR/*.ext>) { |
1442 | for my $path (<$LIBDIR/*.ext>) { |
1361 | next unless -r $path; |
1443 | next unless -r $path; |
… | |
… | |
1379 | |
1461 | |
1380 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1462 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1381 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1463 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1382 | |
1464 | |
1383 | $ext{source} = |
1465 | $ext{source} = |
1384 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1466 | "package $pkg; use common::sense;\n" |
1385 | . "#line 1 \"$path\"\n{\n" |
1467 | . "#line 1 \"$path\"\n{\n" |
1386 | . $source |
1468 | . $source |
1387 | . "\n};\n1"; |
1469 | . "\n};\n1"; |
1388 | |
1470 | |
1389 | $todo{$base} = \%ext; |
1471 | $todo{$base} = \%ext; |
1390 | } |
1472 | } |
1391 | |
1473 | |
|
|
1474 | my $pass = 0; |
1392 | my %done; |
1475 | my %done; |
1393 | while (%todo) { |
1476 | while (%todo) { |
1394 | my $progress; |
1477 | my $progress; |
1395 | |
1478 | |
|
|
1479 | ++$pass; |
|
|
1480 | |
|
|
1481 | ext: |
1396 | while (my ($k, $v) = each %todo) { |
1482 | while (my ($k, $v) = each %todo) { |
1397 | for (split /,\s*/, $v->{meta}{depends}) { |
1483 | for (split /,\s*/, $v->{meta}{depends}) { |
1398 | goto skip |
1484 | next ext |
1399 | unless exists $done{$_}; |
1485 | unless exists $done{$_}; |
1400 | } |
1486 | } |
1401 | |
1487 | |
1402 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1488 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1403 | |
1489 | |
1404 | unless (eval $v->{source}) { |
1490 | my $active = eval $v->{source}; |
|
|
1491 | |
|
|
1492 | if (length $@) { |
1405 | my $msg = $@ ? "$v->{path}: $@\n" |
1493 | error "$v->{path}: $@\n"; |
1406 | : "$v->{base}: extension inactive.\n"; |
|
|
1407 | |
1494 | |
1408 | if (exists $v->{meta}{mandatory}) { |
|
|
1409 | warn $msg; |
|
|
1410 | cf::cleanup "mandatory extension failed to load, exiting."; |
1495 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1411 | } |
1496 | if exists $v->{meta}{mandatory}; |
1412 | |
1497 | |
1413 | warn $msg; |
1498 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
|
|
1499 | delete $todo{$k}; |
|
|
1500 | } else { |
|
|
1501 | $done{$k} = delete $todo{$k}; |
|
|
1502 | push @EXTS, $v->{pkg}; |
|
|
1503 | $progress = 1; |
|
|
1504 | |
|
|
1505 | info "$v->{base}: extension inactive.\n" |
|
|
1506 | unless $active; |
1414 | } |
1507 | } |
1415 | |
|
|
1416 | $done{$k} = delete $todo{$k}; |
|
|
1417 | push @EXTS, $v->{pkg}; |
|
|
1418 | $progress = 1; |
|
|
1419 | } |
1508 | } |
1420 | |
1509 | |
1421 | skip: |
1510 | unless ($progress) { |
1422 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1511 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1423 | unless $progress; |
1512 | |
|
|
1513 | while (my ($k, $v) = each %todo) { |
|
|
1514 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1515 | if exists $v->{meta}{mandatory}; |
|
|
1516 | } |
|
|
1517 | } |
1424 | } |
1518 | } |
1425 | }; |
1519 | }; |
1426 | } |
1520 | } |
1427 | |
1521 | |
1428 | ############################################################################# |
1522 | ############################################################################# |
… | |
… | |
1512 | $cf::PLAYER{$login} = $pl |
1606 | $cf::PLAYER{$login} = $pl |
1513 | } |
1607 | } |
1514 | } |
1608 | } |
1515 | } |
1609 | } |
1516 | |
1610 | |
|
|
1611 | cf::player->attach ( |
|
|
1612 | on_load => sub { |
|
|
1613 | my ($pl, $path) = @_; |
|
|
1614 | |
|
|
1615 | # restore slots saved in save, below |
|
|
1616 | my $slots = delete $pl->{_slots}; |
|
|
1617 | |
|
|
1618 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1619 | $pl->combat_ob ($slots->[1]); |
|
|
1620 | $pl->ranged_ob ($slots->[2]); |
|
|
1621 | }, |
|
|
1622 | ); |
|
|
1623 | |
1517 | sub save($) { |
1624 | sub save($) { |
1518 | my ($pl) = @_; |
1625 | my ($pl) = @_; |
1519 | |
1626 | |
1520 | return if $pl->{deny_save}; |
1627 | return if $pl->{deny_save}; |
1521 | |
1628 | |
… | |
… | |
1526 | |
1633 | |
1527 | aio_mkdir playerdir $pl, 0770; |
1634 | aio_mkdir playerdir $pl, 0770; |
1528 | $pl->{last_save} = $cf::RUNTIME; |
1635 | $pl->{last_save} = $cf::RUNTIME; |
1529 | |
1636 | |
1530 | cf::get_slot 0.01; |
1637 | cf::get_slot 0.01; |
|
|
1638 | |
|
|
1639 | # save slots, to be restored later |
|
|
1640 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1531 | |
1641 | |
1532 | $pl->save_pl ($path); |
1642 | $pl->save_pl ($path); |
1533 | cf::cede_to_tick; |
1643 | cf::cede_to_tick; |
1534 | } |
1644 | } |
1535 | |
1645 | |
… | |
… | |
1569 | my $name = $pl->ob->name; |
1679 | my $name = $pl->ob->name; |
1570 | |
1680 | |
1571 | $pl->{deny_save} = 1; |
1681 | $pl->{deny_save} = 1; |
1572 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1682 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1573 | |
1683 | |
1574 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1684 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1575 | $pl->deactivate; |
1685 | $pl->deactivate; |
|
|
1686 | |
1576 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1687 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1577 | $pl->ob->check_score; |
|
|
1578 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1688 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1689 | ext::highscore::check ($pl->ob); |
|
|
1690 | |
1579 | $pl->ns->destroy if $pl->ns; |
1691 | $pl->ns->destroy if $pl->ns; |
1580 | |
1692 | |
1581 | my $path = playerdir $pl; |
1693 | my $path = playerdir $pl; |
1582 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1694 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1583 | aio_rename $path, $temp; |
1695 | aio_rename $path, $temp; |
… | |
… | |
1637 | \@logins |
1749 | \@logins |
1638 | } |
1750 | } |
1639 | |
1751 | |
1640 | =item $player->maps |
1752 | =item $player->maps |
1641 | |
1753 | |
|
|
1754 | =item cf::player::maps $login |
|
|
1755 | |
1642 | Returns an arrayref of map paths that are private for this |
1756 | Returns an arrayref of map paths that are private for this |
1643 | player. May block. |
1757 | player. May block. |
1644 | |
1758 | |
1645 | =cut |
1759 | =cut |
1646 | |
1760 | |
… | |
… | |
1708 | =cut |
1822 | =cut |
1709 | |
1823 | |
1710 | sub find_by_path($) { |
1824 | sub find_by_path($) { |
1711 | my ($path) = @_; |
1825 | my ($path) = @_; |
1712 | |
1826 | |
|
|
1827 | $path =~ s/^~[^\/]*//; # skip ~login |
|
|
1828 | |
1713 | my ($match, $specificity); |
1829 | my ($match, $specificity); |
1714 | |
1830 | |
1715 | for my $region (list) { |
1831 | for my $region (list) { |
1716 | if ($region->{match} && $path =~ $region->{match}) { |
1832 | if ($region->{match} && $path =~ $region->{match}) { |
1717 | ($match, $specificity) = ($region, $region->specificity) |
1833 | ($match, $specificity) = ($region, $region->specificity) |
… | |
… | |
1748 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1864 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1749 | |
1865 | |
1750 | # mit "rum" bekleckern, nicht |
1866 | # mit "rum" bekleckern, nicht |
1751 | $self->_create_random_map ( |
1867 | $self->_create_random_map ( |
1752 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1868 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1753 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1869 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1754 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1870 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1755 | $rmp->{exit_on_final_map}, |
1871 | $rmp->{exit_on_final_map}, |
1756 | $rmp->{xsize}, $rmp->{ysize}, |
1872 | $rmp->{xsize}, $rmp->{ysize}, |
1757 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1873 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1758 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1874 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
1773 | |
1889 | |
1774 | sub register { |
1890 | sub register { |
1775 | my (undef, $regex, $prio) = @_; |
1891 | my (undef, $regex, $prio) = @_; |
1776 | my $pkg = caller; |
1892 | my $pkg = caller; |
1777 | |
1893 | |
1778 | no strict; |
|
|
1779 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1894 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1780 | |
1895 | |
1781 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1896 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1782 | } |
1897 | } |
1783 | |
1898 | |
1784 | # also paths starting with '/' |
1899 | # also paths starting with '/' |
1785 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1900 | $EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}]; |
1786 | |
1901 | |
1787 | sub thawer_merge { |
1902 | sub thawer_merge { |
1788 | my ($self, $merge) = @_; |
1903 | my ($self, $merge) = @_; |
1789 | |
1904 | |
1790 | # we have to keep some variables in memory intact |
1905 | # we have to keep some variables in memory intact |
… | |
… | |
1795 | } |
1910 | } |
1796 | |
1911 | |
1797 | sub normalise { |
1912 | sub normalise { |
1798 | my ($path, $base) = @_; |
1913 | my ($path, $base) = @_; |
1799 | |
1914 | |
1800 | $path = "$path"; # make sure its a string |
1915 | $path = "$path"; # make sure it's a string |
1801 | |
1916 | |
1802 | $path =~ s/\.map$//; |
1917 | $path =~ s/\.map$//; |
1803 | |
1918 | |
1804 | # map plan: |
1919 | # map plan: |
1805 | # |
1920 | # |
… | |
… | |
1844 | $self->init; # pass $1 etc. |
1959 | $self->init; # pass $1 etc. |
1845 | return $self; |
1960 | return $self; |
1846 | } |
1961 | } |
1847 | } |
1962 | } |
1848 | |
1963 | |
1849 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1964 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1850 | () |
1965 | () |
1851 | } |
1966 | } |
1852 | |
1967 | |
1853 | sub init { |
1968 | sub init { |
1854 | my ($self) = @_; |
1969 | my ($self) = @_; |
… | |
… | |
1886 | sub uniq_path { |
2001 | sub uniq_path { |
1887 | my ($self) = @_; |
2002 | my ($self) = @_; |
1888 | |
2003 | |
1889 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
2004 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1890 | "$UNIQUEDIR/$path" |
2005 | "$UNIQUEDIR/$path" |
1891 | } |
|
|
1892 | |
|
|
1893 | # and all this just because we cannot iterate over |
|
|
1894 | # all maps in C++... |
|
|
1895 | sub change_all_map_light { |
|
|
1896 | my ($change) = @_; |
|
|
1897 | |
|
|
1898 | $_->change_map_light ($change) |
|
|
1899 | for grep $_->outdoor, values %cf::MAP; |
|
|
1900 | } |
2006 | } |
1901 | |
2007 | |
1902 | sub decay_objects { |
2008 | sub decay_objects { |
1903 | my ($self) = @_; |
2009 | my ($self) = @_; |
1904 | |
2010 | |
… | |
… | |
1986 | |
2092 | |
1987 | sub find; |
2093 | sub find; |
1988 | sub find { |
2094 | sub find { |
1989 | my ($path, $origin) = @_; |
2095 | my ($path, $origin) = @_; |
1990 | |
2096 | |
|
|
2097 | cf::cede_to_tick; |
|
|
2098 | |
1991 | $path = normalise $path, $origin && $origin->path; |
2099 | $path = normalise $path, $origin && $origin->path; |
1992 | |
2100 | |
1993 | cf::lock_wait "map_data:$path";#d#remove |
2101 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1994 | cf::lock_wait "map_find:$path"; |
2102 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1995 | |
2103 | |
1996 | $cf::MAP{$path} || do { |
2104 | $cf::MAP{$path} || do { |
1997 | my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1998 | my $guard2 = cf::lock_acquire "map_find:$path"; |
|
|
1999 | |
|
|
2000 | my $map = new_from_path cf::map $path |
2105 | my $map = new_from_path cf::map $path |
2001 | or return; |
2106 | or return; |
2002 | |
2107 | |
2003 | $map->{last_save} = $cf::RUNTIME; |
2108 | $map->{last_save} = $cf::RUNTIME; |
2004 | |
2109 | |
… | |
… | |
2016 | |
2121 | |
2017 | $cf::MAP{$path} = $map |
2122 | $cf::MAP{$path} = $map |
2018 | } |
2123 | } |
2019 | } |
2124 | } |
2020 | |
2125 | |
2021 | sub pre_load { } |
2126 | sub pre_load { } |
2022 | sub post_load { } |
2127 | #sub post_load { } # XS |
2023 | |
2128 | |
2024 | sub load { |
2129 | sub load { |
2025 | my ($self) = @_; |
2130 | my ($self) = @_; |
2026 | |
2131 | |
2027 | local $self->{deny_reset} = 1; # loading can take a long time |
2132 | local $self->{deny_reset} = 1; # loading can take a long time |
… | |
… | |
2072 | unless ($self->{deny_activate}) { |
2177 | unless ($self->{deny_activate}) { |
2073 | $self->decay_objects; |
2178 | $self->decay_objects; |
2074 | $self->fix_auto_apply; |
2179 | $self->fix_auto_apply; |
2075 | $self->update_buttons; |
2180 | $self->update_buttons; |
2076 | cf::cede_to_tick; |
2181 | cf::cede_to_tick; |
2077 | $self->set_darkness_map; |
|
|
2078 | cf::cede_to_tick; |
|
|
2079 | $self->activate; |
2182 | $self->activate; |
2080 | } |
2183 | } |
2081 | |
2184 | |
2082 | $self->{last_save} = $cf::RUNTIME; |
2185 | $self->{last_save} = $cf::RUNTIME; |
2083 | $self->last_access ($cf::RUNTIME); |
2186 | $self->last_access ($cf::RUNTIME); |
… | |
… | |
2086 | } |
2189 | } |
2087 | |
2190 | |
2088 | $self->post_load; |
2191 | $self->post_load; |
2089 | } |
2192 | } |
2090 | |
2193 | |
|
|
2194 | # customize the map for a given player, i.e. |
|
|
2195 | # return the _real_ map. used by e.g. per-player |
|
|
2196 | # maps to change the path to ~playername/mappath |
2091 | sub customise_for { |
2197 | sub customise_for { |
2092 | my ($self, $ob) = @_; |
2198 | my ($self, $ob) = @_; |
2093 | |
2199 | |
2094 | return find "~" . $ob->name . "/" . $self->{path} |
2200 | return find "~" . $ob->name . "/" . $self->{path} |
2095 | if $self->per_player; |
2201 | if $self->per_player; |
… | |
… | |
2111 | or next; |
2217 | or next; |
2112 | $neigh = find $neigh, $map |
2218 | $neigh = find $neigh, $map |
2113 | or next; |
2219 | or next; |
2114 | $neigh->load; |
2220 | $neigh->load; |
2115 | |
2221 | |
|
|
2222 | # now find the diagonal neighbours |
2116 | push @neigh, |
2223 | push @neigh, |
2117 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2224 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2118 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2225 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2119 | } |
2226 | } |
2120 | |
2227 | |
… | |
… | |
2127 | } |
2234 | } |
2128 | |
2235 | |
2129 | sub find_sync { |
2236 | sub find_sync { |
2130 | my ($path, $origin) = @_; |
2237 | my ($path, $origin) = @_; |
2131 | |
2238 | |
2132 | cf::sync_job { find $path, $origin } |
2239 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2240 | if $Coro::current == $Coro::main; |
|
|
2241 | |
|
|
2242 | find $path, $origin |
2133 | } |
2243 | } |
2134 | |
2244 | |
2135 | sub do_load_sync { |
2245 | sub do_load_sync { |
2136 | my ($map) = @_; |
2246 | my ($map) = @_; |
2137 | |
2247 | |
2138 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2248 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2139 | if $Coro::current == $Coro::main; |
2249 | if $Coro::current == $Coro::main; |
2140 | |
2250 | |
2141 | cf::sync_job { $map->load }; |
2251 | $map->load; |
2142 | } |
2252 | } |
2143 | |
2253 | |
2144 | our %MAP_PREFETCH; |
2254 | our %MAP_PREFETCH; |
2145 | our $MAP_PREFETCHER = undef; |
2255 | our $MAP_PREFETCHER = undef; |
2146 | |
2256 | |
… | |
… | |
2172 | $MAP_PREFETCHER->prio (6); |
2282 | $MAP_PREFETCHER->prio (6); |
2173 | |
2283 | |
2174 | () |
2284 | () |
2175 | } |
2285 | } |
2176 | |
2286 | |
|
|
2287 | # common code, used by both ->save and ->swapout |
2177 | sub save { |
2288 | sub _save { |
2178 | my ($self) = @_; |
2289 | my ($self) = @_; |
2179 | |
|
|
2180 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2181 | |
2290 | |
2182 | $self->{last_save} = $cf::RUNTIME; |
2291 | $self->{last_save} = $cf::RUNTIME; |
2183 | |
2292 | |
2184 | return unless $self->dirty; |
2293 | return unless $self->dirty; |
2185 | |
2294 | |
… | |
… | |
2205 | } else { |
2314 | } else { |
2206 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2315 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2207 | } |
2316 | } |
2208 | } |
2317 | } |
2209 | |
2318 | |
|
|
2319 | sub save { |
|
|
2320 | my ($self) = @_; |
|
|
2321 | |
|
|
2322 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2323 | |
|
|
2324 | $self->_save; |
|
|
2325 | } |
|
|
2326 | |
2210 | sub swap_out { |
2327 | sub swap_out { |
2211 | my ($self) = @_; |
2328 | my ($self) = @_; |
2212 | |
2329 | |
2213 | # save first because save cedes |
|
|
2214 | $self->save; |
|
|
2215 | |
|
|
2216 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2330 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2217 | |
2331 | |
2218 | return if $self->players; |
|
|
2219 | return if $self->in_memory != cf::MAP_ACTIVE; |
2332 | return if $self->in_memory != cf::MAP_ACTIVE; |
2220 | return if $self->{deny_save}; |
2333 | return if $self->{deny_save}; |
|
|
2334 | return if $self->players; |
2221 | |
2335 | |
|
|
2336 | # first deactivate the map and "unlink" it from the core |
|
|
2337 | $self->deactivate; |
|
|
2338 | $_->clear_links_to ($self) for values %cf::MAP; |
2222 | $self->in_memory (cf::MAP_SWAPPED); |
2339 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2340 | |
|
|
2341 | # then atomically save |
|
|
2342 | $self->_save; |
|
|
2343 | |
|
|
2344 | # then free the map |
|
|
2345 | $self->clear; |
|
|
2346 | } |
|
|
2347 | |
|
|
2348 | sub reset_at { |
|
|
2349 | my ($self) = @_; |
|
|
2350 | |
|
|
2351 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2352 | return 1e99 if $self->{deny_reset}; |
|
|
2353 | |
|
|
2354 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2355 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2356 | |
|
|
2357 | $time + $to |
|
|
2358 | } |
|
|
2359 | |
|
|
2360 | sub should_reset { |
|
|
2361 | my ($self) = @_; |
|
|
2362 | |
|
|
2363 | $self->reset_at <= $cf::RUNTIME |
|
|
2364 | } |
|
|
2365 | |
|
|
2366 | sub reset { |
|
|
2367 | my ($self) = @_; |
|
|
2368 | |
|
|
2369 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2370 | |
|
|
2371 | return if $self->players; |
|
|
2372 | |
|
|
2373 | cf::trace "resetting map ", $self->path, "\n"; |
|
|
2374 | |
|
|
2375 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2376 | |
|
|
2377 | # need to save uniques path |
|
|
2378 | unless ($self->{deny_save}) { |
|
|
2379 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2380 | |
|
|
2381 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2382 | if $uniq; |
|
|
2383 | } |
|
|
2384 | |
|
|
2385 | delete $cf::MAP{$self->path}; |
2223 | |
2386 | |
2224 | $self->deactivate; |
2387 | $self->deactivate; |
2225 | $_->clear_links_to ($self) for values %cf::MAP; |
2388 | $_->clear_links_to ($self) for values %cf::MAP; |
2226 | $self->clear; |
2389 | $self->clear; |
2227 | } |
|
|
2228 | |
|
|
2229 | sub reset_at { |
|
|
2230 | my ($self) = @_; |
|
|
2231 | |
|
|
2232 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2233 | return 1e99 if $self->{deny_reset}; |
|
|
2234 | |
|
|
2235 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2236 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2237 | |
|
|
2238 | $time + $to |
|
|
2239 | } |
|
|
2240 | |
|
|
2241 | sub should_reset { |
|
|
2242 | my ($self) = @_; |
|
|
2243 | |
|
|
2244 | $self->reset_at <= $cf::RUNTIME |
|
|
2245 | } |
|
|
2246 | |
|
|
2247 | sub reset { |
|
|
2248 | my ($self) = @_; |
|
|
2249 | |
|
|
2250 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2251 | |
|
|
2252 | return if $self->players; |
|
|
2253 | |
|
|
2254 | warn "resetting map ", $self->path; |
|
|
2255 | |
|
|
2256 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2257 | |
|
|
2258 | # need to save uniques path |
|
|
2259 | unless ($self->{deny_save}) { |
|
|
2260 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2261 | |
|
|
2262 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2263 | if $uniq; |
|
|
2264 | } |
|
|
2265 | |
|
|
2266 | delete $cf::MAP{$self->path}; |
|
|
2267 | |
|
|
2268 | $self->deactivate; |
|
|
2269 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2270 | $self->clear; |
|
|
2271 | |
2390 | |
2272 | $self->unlink_save; |
2391 | $self->unlink_save; |
2273 | $self->destroy; |
2392 | $self->destroy; |
2274 | } |
2393 | } |
2275 | |
2394 | |
… | |
… | |
2283 | |
2402 | |
2284 | delete $cf::MAP{$self->path}; |
2403 | delete $cf::MAP{$self->path}; |
2285 | |
2404 | |
2286 | $self->unlink_save; |
2405 | $self->unlink_save; |
2287 | |
2406 | |
2288 | bless $self, "cf::map"; |
2407 | bless $self, "cf::map::wrap"; |
2289 | delete $self->{deny_reset}; |
2408 | delete $self->{deny_reset}; |
2290 | $self->{deny_save} = 1; |
2409 | $self->{deny_save} = 1; |
2291 | $self->reset_timeout (1); |
2410 | $self->reset_timeout (1); |
2292 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2411 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2293 | |
2412 | |
… | |
… | |
2350 | : normalise $_ |
2469 | : normalise $_ |
2351 | } @{ aio_readdir $UNIQUEDIR or [] } |
2470 | } @{ aio_readdir $UNIQUEDIR or [] } |
2352 | ] |
2471 | ] |
2353 | } |
2472 | } |
2354 | |
2473 | |
|
|
2474 | =item cf::map::static_maps |
|
|
2475 | |
|
|
2476 | Returns an arrayref if paths of all static maps (all preinstalled F<.map> |
|
|
2477 | file in the shared directory excluding F</styles> and F</editor>). May |
|
|
2478 | block. |
|
|
2479 | |
|
|
2480 | =cut |
|
|
2481 | |
|
|
2482 | sub static_maps() { |
|
|
2483 | my @dirs = ""; |
|
|
2484 | my @maps; |
|
|
2485 | |
|
|
2486 | while (@dirs) { |
|
|
2487 | my $dir = shift @dirs; |
|
|
2488 | |
|
|
2489 | next if $dir eq "/styles" || $dir eq "/editor"; |
|
|
2490 | |
|
|
2491 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
|
|
2492 | or return; |
|
|
2493 | |
|
|
2494 | for (@$files) { |
|
|
2495 | s/\.map$// or next; |
|
|
2496 | utf8::decode $_; |
|
|
2497 | push @maps, "$dir/$_"; |
|
|
2498 | } |
|
|
2499 | |
|
|
2500 | push @dirs, map "$dir/$_", @$dirs; |
|
|
2501 | } |
|
|
2502 | |
|
|
2503 | \@maps |
|
|
2504 | } |
|
|
2505 | |
2355 | =back |
2506 | =back |
2356 | |
2507 | |
2357 | =head3 cf::object |
2508 | =head3 cf::object |
2358 | |
2509 | |
2359 | =cut |
2510 | =cut |
… | |
… | |
2424 | |
2575 | |
2425 | our $SAY_CHANNEL = { |
2576 | our $SAY_CHANNEL = { |
2426 | id => "say", |
2577 | id => "say", |
2427 | title => "Map", |
2578 | title => "Map", |
2428 | reply => "say ", |
2579 | reply => "say ", |
2429 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
2580 | tooltip => "Things said to and replied from NPCs near you and other players on the same map only.", |
2430 | }; |
2581 | }; |
2431 | |
2582 | |
2432 | our $CHAT_CHANNEL = { |
2583 | our $CHAT_CHANNEL = { |
2433 | id => "chat", |
2584 | id => "chat", |
2434 | title => "Chat", |
2585 | title => "Chat", |
… | |
… | |
2491 | |
2642 | |
2492 | Freezes the player and moves him/her to a special map (C<{link}>). |
2643 | Freezes the player and moves him/her to a special map (C<{link}>). |
2493 | |
2644 | |
2494 | The player should be reasonably safe there for short amounts of time (e.g. |
2645 | The player should be reasonably safe there for short amounts of time (e.g. |
2495 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2646 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2496 | though, as the palyer cannot control the character while it is on the link |
2647 | though, as the player cannot control the character while it is on the link |
2497 | map. |
2648 | map. |
2498 | |
2649 | |
2499 | Will never block. |
2650 | Will never block. |
2500 | |
2651 | |
2501 | =item $player_object->leave_link ($map, $x, $y) |
2652 | =item $player_object->leave_link ($map, $x, $y) |
… | |
… | |
2522 | sub cf::object::player::enter_link { |
2673 | sub cf::object::player::enter_link { |
2523 | my ($self) = @_; |
2674 | my ($self) = @_; |
2524 | |
2675 | |
2525 | $self->deactivate_recursive; |
2676 | $self->deactivate_recursive; |
2526 | |
2677 | |
|
|
2678 | ++$self->{_link_recursion}; |
|
|
2679 | |
2527 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2680 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2528 | |
2681 | |
2529 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2682 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2530 | if $self->map && $self->map->{path} ne "{link}"; |
2683 | if $self->map && $self->map->{path} ne "{link}"; |
2531 | |
2684 | |
2532 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
2685 | $self->enter_map ($LINK_MAP || link_map, 3, 3); |
2533 | } |
2686 | } |
2534 | |
2687 | |
2535 | sub cf::object::player::leave_link { |
2688 | sub cf::object::player::leave_link { |
2536 | my ($self, $map, $x, $y) = @_; |
2689 | my ($self, $map, $x, $y) = @_; |
2537 | |
2690 | |
… | |
… | |
2554 | ($x, $y) = (-1, -1) |
2707 | ($x, $y) = (-1, -1) |
2555 | unless (defined $x) && (defined $y); |
2708 | unless (defined $x) && (defined $y); |
2556 | |
2709 | |
2557 | # use -1 or undef as default coordinates, not 0, 0 |
2710 | # use -1 or undef as default coordinates, not 0, 0 |
2558 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2711 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2559 | if $x <=0 && $y <= 0; |
2712 | if $x <= 0 && $y <= 0; |
2560 | |
2713 | |
2561 | $map->load; |
2714 | $map->load; |
2562 | $map->load_neighbours; |
2715 | $map->load_neighbours; |
2563 | |
2716 | |
2564 | return unless $self->contr->active; |
2717 | return unless $self->contr->active; |
2565 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2566 | $self->activate_recursive; |
|
|
2567 | |
2718 | |
2568 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2719 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2569 | $self->enter_map ($map, $x, $y); |
2720 | if ($self->enter_map ($map, $x, $y)) { |
2570 | } |
2721 | # entering was successful |
|
|
2722 | delete $self->{_link_recursion}; |
|
|
2723 | # only activate afterwards, to support waiting in hooks |
|
|
2724 | $self->activate_recursive; |
|
|
2725 | } |
2571 | |
2726 | |
|
|
2727 | } |
|
|
2728 | |
2572 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2729 | =item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]]) |
2573 | |
2730 | |
2574 | Moves the player to the given map-path and coordinates by first freezing |
2731 | Moves the player to the given map-path and coordinates by first freezing |
2575 | her, loading and preparing them map, calling the provided $check callback |
2732 | her, loading and preparing them map, calling the provided $check callback |
2576 | that has to return the map if sucecssful, and then unfreezes the player on |
2733 | that has to return the map if sucecssful, and then unfreezes the player on |
2577 | the new (success) or old (failed) map position. In either case, $done will |
2734 | the new (success) or old (failed) map position. In either case, $done will |
… | |
… | |
2584 | |
2741 | |
2585 | our $GOTOGEN; |
2742 | our $GOTOGEN; |
2586 | |
2743 | |
2587 | sub cf::object::player::goto { |
2744 | sub cf::object::player::goto { |
2588 | my ($self, $path, $x, $y, $check, $done) = @_; |
2745 | my ($self, $path, $x, $y, $check, $done) = @_; |
|
|
2746 | |
|
|
2747 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
|
|
2748 | error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
|
|
2749 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
|
|
2750 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
|
|
2751 | } |
2589 | |
2752 | |
2590 | # do generation counting so two concurrent goto's will be executed in-order |
2753 | # do generation counting so two concurrent goto's will be executed in-order |
2591 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2754 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2592 | |
2755 | |
2593 | $self->enter_link; |
2756 | $self->enter_link; |
… | |
… | |
2617 | my $map = eval { |
2780 | my $map = eval { |
2618 | my $map = defined $path ? cf::map::find $path : undef; |
2781 | my $map = defined $path ? cf::map::find $path : undef; |
2619 | |
2782 | |
2620 | if ($map) { |
2783 | if ($map) { |
2621 | $map = $map->customise_for ($self); |
2784 | $map = $map->customise_for ($self); |
2622 | $map = $check->($map) if $check && $map; |
2785 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2623 | } else { |
2786 | } else { |
2624 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2787 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2625 | } |
2788 | } |
2626 | |
2789 | |
2627 | $map |
2790 | $map |
… | |
… | |
2635 | if ($gen == $self->{_goto_generation}) { |
2798 | if ($gen == $self->{_goto_generation}) { |
2636 | delete $self->{_goto_generation}; |
2799 | delete $self->{_goto_generation}; |
2637 | $self->leave_link ($map, $x, $y); |
2800 | $self->leave_link ($map, $x, $y); |
2638 | } |
2801 | } |
2639 | |
2802 | |
2640 | $done->() if $done; |
2803 | $done->($self) if $done; |
2641 | })->prio (1); |
2804 | })->prio (1); |
2642 | } |
2805 | } |
2643 | |
2806 | |
2644 | =item $player_object->enter_exit ($exit_object) |
2807 | =item $player_object->enter_exit ($exit_object) |
2645 | |
2808 | |
… | |
… | |
2738 | $self->message ("Something went wrong deep within the deliantra server. " |
2901 | $self->message ("Something went wrong deep within the deliantra server. " |
2739 | . "I'll try to bring you back to the map you were before. " |
2902 | . "I'll try to bring you back to the map you were before. " |
2740 | . "Please report this to the dungeon master!", |
2903 | . "Please report this to the dungeon master!", |
2741 | cf::NDI_UNIQUE | cf::NDI_RED); |
2904 | cf::NDI_UNIQUE | cf::NDI_RED); |
2742 | |
2905 | |
2743 | warn "ERROR in enter_exit: $@"; |
2906 | error "ERROR in enter_exit: $@"; |
2744 | $self->leave_link; |
2907 | $self->leave_link; |
2745 | } |
2908 | } |
2746 | })->prio (1); |
2909 | })->prio (1); |
2747 | } |
2910 | } |
2748 | |
2911 | |
… | |
… | |
2760 | sub cf::client::send_drawinfo { |
2923 | sub cf::client::send_drawinfo { |
2761 | my ($self, $text, $flags) = @_; |
2924 | my ($self, $text, $flags) = @_; |
2762 | |
2925 | |
2763 | utf8::encode $text; |
2926 | utf8::encode $text; |
2764 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2927 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
|
|
2928 | } |
|
|
2929 | |
|
|
2930 | =item $client->send_big_packet ($pkt) |
|
|
2931 | |
|
|
2932 | Like C<send_packet>, but tries to compress large packets, and fragments |
|
|
2933 | them as required. |
|
|
2934 | |
|
|
2935 | =cut |
|
|
2936 | |
|
|
2937 | our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64; |
|
|
2938 | |
|
|
2939 | sub cf::client::send_big_packet { |
|
|
2940 | my ($self, $pkt) = @_; |
|
|
2941 | |
|
|
2942 | # try lzf for large packets |
|
|
2943 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2944 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2945 | |
|
|
2946 | # split very large packets |
|
|
2947 | if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) { |
|
|
2948 | $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt; |
|
|
2949 | $pkt = "frag"; |
|
|
2950 | } |
|
|
2951 | |
|
|
2952 | $self->send_packet ($pkt); |
2765 | } |
2953 | } |
2766 | |
2954 | |
2767 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2955 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2768 | |
2956 | |
2769 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2957 | Send a drawinfo or msg packet to the client, formatting the msg for the |
… | |
… | |
2773 | |
2961 | |
2774 | =cut |
2962 | =cut |
2775 | |
2963 | |
2776 | # non-persistent channels (usually the info channel) |
2964 | # non-persistent channels (usually the info channel) |
2777 | our %CHANNEL = ( |
2965 | our %CHANNEL = ( |
|
|
2966 | "c/motd" => { |
|
|
2967 | id => "infobox", |
|
|
2968 | title => "MOTD", |
|
|
2969 | reply => undef, |
|
|
2970 | tooltip => "The message of the day", |
|
|
2971 | }, |
2778 | "c/identify" => { |
2972 | "c/identify" => { |
2779 | id => "infobox", |
2973 | id => "infobox", |
2780 | title => "Identify", |
2974 | title => "Identify", |
2781 | reply => undef, |
2975 | reply => undef, |
2782 | tooltip => "Items recently identified", |
2976 | tooltip => "Items recently identified", |
… | |
… | |
2784 | "c/examine" => { |
2978 | "c/examine" => { |
2785 | id => "infobox", |
2979 | id => "infobox", |
2786 | title => "Examine", |
2980 | title => "Examine", |
2787 | reply => undef, |
2981 | reply => undef, |
2788 | tooltip => "Signs and other items you examined", |
2982 | tooltip => "Signs and other items you examined", |
|
|
2983 | }, |
|
|
2984 | "c/shopinfo" => { |
|
|
2985 | id => "infobox", |
|
|
2986 | title => "Shop Info", |
|
|
2987 | reply => undef, |
|
|
2988 | tooltip => "What your bargaining skill tells you about the shop", |
2789 | }, |
2989 | }, |
2790 | "c/book" => { |
2990 | "c/book" => { |
2791 | id => "infobox", |
2991 | id => "infobox", |
2792 | title => "Book", |
2992 | title => "Book", |
2793 | reply => undef, |
2993 | reply => undef, |
… | |
… | |
2809 | id => "infobox", |
3009 | id => "infobox", |
2810 | title => "Body Parts", |
3010 | title => "Body Parts", |
2811 | reply => undef, |
3011 | reply => undef, |
2812 | tooltip => "Shows which body parts you posess and are available", |
3012 | tooltip => "Shows which body parts you posess and are available", |
2813 | }, |
3013 | }, |
|
|
3014 | "c/statistics" => { |
|
|
3015 | id => "infobox", |
|
|
3016 | title => "Statistics", |
|
|
3017 | reply => undef, |
|
|
3018 | tooltip => "Shows your primary statistics", |
|
|
3019 | }, |
2814 | "c/skills" => { |
3020 | "c/skills" => { |
2815 | id => "infobox", |
3021 | id => "infobox", |
2816 | title => "Skills", |
3022 | title => "Skills", |
2817 | reply => undef, |
3023 | reply => undef, |
2818 | tooltip => "Shows your experience per skill and item power", |
3024 | tooltip => "Shows your experience per skill and item power", |
|
|
3025 | }, |
|
|
3026 | "c/shopitems" => { |
|
|
3027 | id => "infobox", |
|
|
3028 | title => "Shop Items", |
|
|
3029 | reply => undef, |
|
|
3030 | tooltip => "Shows the items currently for sale in this shop", |
|
|
3031 | }, |
|
|
3032 | "c/resistances" => { |
|
|
3033 | id => "infobox", |
|
|
3034 | title => "Resistances", |
|
|
3035 | reply => undef, |
|
|
3036 | tooltip => "Shows your resistances", |
|
|
3037 | }, |
|
|
3038 | "c/pets" => { |
|
|
3039 | id => "infobox", |
|
|
3040 | title => "Pets", |
|
|
3041 | reply => undef, |
|
|
3042 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
3043 | }, |
|
|
3044 | "c/perceiveself" => { |
|
|
3045 | id => "infobox", |
|
|
3046 | title => "Perceive Self", |
|
|
3047 | reply => undef, |
|
|
3048 | tooltip => "You gained detailed knowledge about yourself", |
2819 | }, |
3049 | }, |
2820 | "c/uptime" => { |
3050 | "c/uptime" => { |
2821 | id => "infobox", |
3051 | id => "infobox", |
2822 | title => "Uptime", |
3052 | title => "Uptime", |
2823 | reply => undef, |
3053 | reply => undef, |
… | |
… | |
2833 | id => "party", |
3063 | id => "party", |
2834 | title => "Party", |
3064 | title => "Party", |
2835 | reply => "gsay ", |
3065 | reply => "gsay ", |
2836 | tooltip => "Messages and chat related to your party", |
3066 | tooltip => "Messages and chat related to your party", |
2837 | }, |
3067 | }, |
|
|
3068 | "c/death" => { |
|
|
3069 | id => "death", |
|
|
3070 | title => "Death", |
|
|
3071 | reply => undef, |
|
|
3072 | tooltip => "Reason for and more info about your most recent death", |
|
|
3073 | }, |
|
|
3074 | "c/say" => $SAY_CHANNEL, |
|
|
3075 | "c/chat" => $CHAT_CHANNEL, |
2838 | ); |
3076 | ); |
2839 | |
3077 | |
2840 | sub cf::client::send_msg { |
3078 | sub cf::client::send_msg { |
2841 | my ($self, $channel, $msg, $color, @extra) = @_; |
3079 | my ($self, $channel, $msg, $color, @extra) = @_; |
2842 | |
3080 | |
… | |
… | |
2847 | |
3085 | |
2848 | # check predefined channels, for the benefit of C |
3086 | # check predefined channels, for the benefit of C |
2849 | if ($CHANNEL{$channel}) { |
3087 | if ($CHANNEL{$channel}) { |
2850 | $channel = $CHANNEL{$channel}; |
3088 | $channel = $CHANNEL{$channel}; |
2851 | |
3089 | |
2852 | $self->ext_msg (channel_info => $channel) |
3090 | $self->ext_msg (channel_info => $channel); |
2853 | if $self->can_msg; |
|
|
2854 | |
|
|
2855 | $channel = $channel->{id}; |
3091 | $channel = $channel->{id}; |
2856 | |
3092 | |
2857 | } elsif (ref $channel) { |
3093 | } elsif (ref $channel) { |
2858 | # send meta info to client, if not yet sent |
3094 | # send meta info to client, if not yet sent |
2859 | unless (exists $self->{channel}{$channel->{id}}) { |
3095 | unless (exists $self->{channel}{$channel->{id}}) { |
2860 | $self->{channel}{$channel->{id}} = $channel; |
3096 | $self->{channel}{$channel->{id}} = $channel; |
2861 | $self->ext_msg (channel_info => $channel) |
3097 | $self->ext_msg (channel_info => $channel); |
2862 | if $self->can_msg; |
|
|
2863 | } |
3098 | } |
2864 | |
3099 | |
2865 | $channel = $channel->{id}; |
3100 | $channel = $channel->{id}; |
2866 | } |
3101 | } |
2867 | |
3102 | |
2868 | return unless @extra || length $msg; |
3103 | return unless @extra || length $msg; |
2869 | |
3104 | |
2870 | if ($self->can_msg) { |
|
|
2871 | # default colour, mask it out |
3105 | # default colour, mask it out |
2872 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
3106 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2873 | if $color & cf::NDI_DEF; |
3107 | if $color & cf::NDI_DEF; |
2874 | |
3108 | |
2875 | my $pkt = "msg " |
3109 | my $pkt = "msg " |
2876 | . $self->{json_coder}->encode ( |
3110 | . $self->{json_coder}->encode ( |
2877 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
3111 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2878 | ); |
3112 | ); |
2879 | |
3113 | |
2880 | # try lzf for large packets |
|
|
2881 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2882 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2883 | |
|
|
2884 | # split very large packets |
|
|
2885 | if (8192 < length $pkt and $self->{can_lzf}) { |
|
|
2886 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
|
|
2887 | $pkt = "frag"; |
|
|
2888 | } |
|
|
2889 | |
|
|
2890 | $self->send_packet ($pkt); |
3114 | $self->send_big_packet ($pkt); |
2891 | } else { |
|
|
2892 | if ($color >= 0) { |
|
|
2893 | # replace some tags by gcfclient-compatible ones |
|
|
2894 | for ($msg) { |
|
|
2895 | 1 while |
|
|
2896 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2897 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2898 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2899 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2900 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2901 | } |
|
|
2902 | |
|
|
2903 | $color &= cf::NDI_COLOR_MASK; |
|
|
2904 | |
|
|
2905 | utf8::encode $msg; |
|
|
2906 | |
|
|
2907 | if (0 && $msg =~ /\[/) { |
|
|
2908 | # COMMAND/INFO |
|
|
2909 | $self->send_packet ("drawextinfo $color 10 8 $msg") |
|
|
2910 | } else { |
|
|
2911 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
|
|
2912 | $self->send_packet ("drawinfo $color $msg") |
|
|
2913 | } |
|
|
2914 | } |
|
|
2915 | } |
|
|
2916 | } |
3115 | } |
2917 | |
3116 | |
2918 | =item $client->ext_msg ($type, @msg) |
3117 | =item $client->ext_msg ($type, @msg) |
2919 | |
3118 | |
2920 | Sends an ext event to the client. |
3119 | Sends an ext event to the client. |
… | |
… | |
2923 | |
3122 | |
2924 | sub cf::client::ext_msg($$@) { |
3123 | sub cf::client::ext_msg($$@) { |
2925 | my ($self, $type, @msg) = @_; |
3124 | my ($self, $type, @msg) = @_; |
2926 | |
3125 | |
2927 | if ($self->extcmd == 2) { |
3126 | if ($self->extcmd == 2) { |
2928 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
3127 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2929 | } elsif ($self->extcmd == 1) { # TODO: remove |
3128 | } elsif ($self->extcmd == 1) { # TODO: remove |
2930 | push @msg, msgtype => "event_$type"; |
3129 | push @msg, msgtype => "event_$type"; |
2931 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3130 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2932 | } |
3131 | } |
2933 | } |
3132 | } |
2934 | |
3133 | |
2935 | =item $client->ext_reply ($msgid, @msg) |
3134 | =item $client->ext_reply ($msgid, @msg) |
2936 | |
3135 | |
… | |
… | |
2940 | |
3139 | |
2941 | sub cf::client::ext_reply($$@) { |
3140 | sub cf::client::ext_reply($$@) { |
2942 | my ($self, $id, @msg) = @_; |
3141 | my ($self, $id, @msg) = @_; |
2943 | |
3142 | |
2944 | if ($self->extcmd == 2) { |
3143 | if ($self->extcmd == 2) { |
2945 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3144 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2946 | } elsif ($self->extcmd == 1) { |
3145 | } elsif ($self->extcmd == 1) { |
2947 | #TODO: version 1, remove |
3146 | #TODO: version 1, remove |
2948 | unshift @msg, msgtype => "reply", msgid => $id; |
3147 | unshift @msg, msgtype => "reply", msgid => $id; |
2949 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3148 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2950 | } |
3149 | } |
2951 | } |
3150 | } |
2952 | |
3151 | |
2953 | =item $success = $client->query ($flags, "text", \&cb) |
3152 | =item $success = $client->query ($flags, "text", \&cb) |
2954 | |
3153 | |
… | |
… | |
3025 | |
3224 | |
3026 | $ns->ext_reply ($reply, @reply) |
3225 | $ns->ext_reply ($reply, @reply) |
3027 | if $reply; |
3226 | if $reply; |
3028 | |
3227 | |
3029 | } else { |
3228 | } else { |
3030 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3229 | error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3031 | } |
3230 | } |
3032 | |
3231 | |
3033 | cf::override; |
3232 | cf::override; |
3034 | }, |
3233 | }, |
3035 | ); |
3234 | ); |
… | |
… | |
3055 | |
3254 | |
3056 | $coro |
3255 | $coro |
3057 | } |
3256 | } |
3058 | |
3257 | |
3059 | cf::client->attach ( |
3258 | cf::client->attach ( |
3060 | on_destroy => sub { |
3259 | on_client_destroy => sub { |
3061 | my ($ns) = @_; |
3260 | my ($ns) = @_; |
3062 | |
3261 | |
3063 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3262 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3064 | }, |
3263 | }, |
3065 | ); |
3264 | ); |
… | |
… | |
3081 | our $safe_hole = new Safe::Hole; |
3280 | our $safe_hole = new Safe::Hole; |
3082 | |
3281 | |
3083 | $SIG{FPE} = 'IGNORE'; |
3282 | $SIG{FPE} = 'IGNORE'; |
3084 | |
3283 | |
3085 | $safe->permit_only (Opcode::opset qw( |
3284 | $safe->permit_only (Opcode::opset qw( |
3086 | :base_core :base_mem :base_orig :base_math |
3285 | :base_core :base_mem :base_orig :base_math :base_loop |
3087 | grepstart grepwhile mapstart mapwhile |
3286 | grepstart grepwhile mapstart mapwhile |
3088 | sort time |
3287 | sort time |
3089 | )); |
3288 | )); |
3090 | |
3289 | |
3091 | # here we export the classes and methods available to script code |
3290 | # here we export the classes and methods available to script code |
… | |
… | |
3095 | The following functions and methods are available within a safe environment: |
3294 | The following functions and methods are available within a safe environment: |
3096 | |
3295 | |
3097 | cf::object |
3296 | cf::object |
3098 | contr pay_amount pay_player map x y force_find force_add destroy |
3297 | contr pay_amount pay_player map x y force_find force_add destroy |
3099 | insert remove name archname title slaying race decrease split |
3298 | insert remove name archname title slaying race decrease split |
|
|
3299 | value |
3100 | |
3300 | |
3101 | cf::object::player |
3301 | cf::object::player |
3102 | player |
3302 | player |
3103 | |
3303 | |
3104 | cf::player |
3304 | cf::player |
… | |
… | |
3110 | =cut |
3310 | =cut |
3111 | |
3311 | |
3112 | for ( |
3312 | for ( |
3113 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3313 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3114 | insert remove inv nrof name archname title slaying race |
3314 | insert remove inv nrof name archname title slaying race |
3115 | decrease split destroy change_exp)], |
3315 | decrease split destroy change_exp value msg lore send_msg)], |
3116 | ["cf::object::player" => qw(player)], |
3316 | ["cf::object::player" => qw(player)], |
3117 | ["cf::player" => qw(peaceful)], |
3317 | ["cf::player" => qw(peaceful send_msg)], |
3118 | ["cf::map" => qw(trigger)], |
3318 | ["cf::map" => qw(trigger)], |
3119 | ) { |
3319 | ) { |
3120 | no strict 'refs'; |
|
|
3121 | my ($pkg, @funs) = @$_; |
3320 | my ($pkg, @funs) = @$_; |
3122 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3321 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3123 | for @funs; |
3322 | for @funs; |
3124 | } |
3323 | } |
3125 | |
3324 | |
… | |
… | |
3140 | |
3339 | |
3141 | my $qcode = $code; |
3340 | my $qcode = $code; |
3142 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3341 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3143 | $qcode =~ s/\n/\\n/g; |
3342 | $qcode =~ s/\n/\\n/g; |
3144 | |
3343 | |
|
|
3344 | %vars = (_dummy => 0) unless %vars; |
|
|
3345 | |
|
|
3346 | my @res; |
3145 | local $_; |
3347 | local $_; |
3146 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
3147 | |
3348 | |
3148 | my $eval = |
3349 | my $eval = |
3149 | "do {\n" |
3350 | "do {\n" |
3150 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3351 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3151 | . "#line 0 \"{$qcode}\"\n" |
3352 | . "#line 0 \"{$qcode}\"\n" |
3152 | . $code |
3353 | . $code |
3153 | . "\n}" |
3354 | . "\n}" |
3154 | ; |
3355 | ; |
3155 | |
3356 | |
|
|
3357 | if ($CFG{safe_eval}) { |
3156 | sub_generation_inc; |
3358 | sub_generation_inc; |
|
|
3359 | local @safe::cf::_safe_eval_args = values %vars; |
3157 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3360 | @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3158 | sub_generation_inc; |
3361 | sub_generation_inc; |
|
|
3362 | } else { |
|
|
3363 | local @cf::_safe_eval_args = values %vars; |
|
|
3364 | @res = wantarray ? eval eval : scalar eval $eval; |
|
|
3365 | } |
3159 | |
3366 | |
3160 | if ($@) { |
3367 | if ($@) { |
3161 | warn "$@"; |
3368 | warn "$@", |
3162 | warn "while executing safe code '$code'\n"; |
3369 | "while executing safe code '$code'\n", |
3163 | warn "with arguments " . (join " ", %vars) . "\n"; |
3370 | "with arguments " . (join " ", %vars) . "\n"; |
3164 | } |
3371 | } |
3165 | |
3372 | |
3166 | wantarray ? @res : $res[0] |
3373 | wantarray ? @res : $res[0] |
3167 | } |
3374 | } |
3168 | |
3375 | |
… | |
… | |
3182 | =cut |
3389 | =cut |
3183 | |
3390 | |
3184 | sub register_script_function { |
3391 | sub register_script_function { |
3185 | my ($fun, $cb) = @_; |
3392 | my ($fun, $cb) = @_; |
3186 | |
3393 | |
3187 | no strict 'refs'; |
3394 | $fun = "safe::$fun" if $CFG{safe_eval}; |
3188 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
3395 | *$fun = $safe_hole->wrap ($cb); |
3189 | } |
3396 | } |
3190 | |
3397 | |
3191 | =back |
3398 | =back |
3192 | |
3399 | |
3193 | =cut |
3400 | =cut |
… | |
… | |
3202 | # for this (global event?) |
3409 | # for this (global event?) |
3203 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3410 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3204 | |
3411 | |
3205 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3412 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3206 | |
3413 | |
3207 | warn "loading facedata from $path\n"; |
3414 | trace "loading facedata from $path\n"; |
3208 | |
3415 | |
3209 | my $facedata; |
|
|
3210 | 0 < aio_load $path, $facedata |
3416 | 0 < aio_load $path, my $facedata |
3211 | or die "$path: $!"; |
3417 | or die "$path: $!"; |
3212 | |
3418 | |
3213 | $facedata = Coro::Storable::thaw $facedata; |
3419 | $facedata = Coro::Storable::thaw $facedata; |
3214 | |
3420 | |
3215 | $facedata->{version} == 2 |
3421 | $facedata->{version} == 2 |
3216 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3422 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3217 | |
3423 | |
3218 | # patch in the exptable |
3424 | # patch in the exptable |
|
|
3425 | my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]); |
3219 | $facedata->{resource}{"res/exp_table"} = { |
3426 | $facedata->{resource}{"res/exp_table"} = { |
3220 | type => FT_RSRC, |
3427 | type => FT_RSRC, |
3221 | data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), |
3428 | data => $exp_table, |
|
|
3429 | hash => (Digest::MD5::md5 $exp_table), |
3222 | }; |
3430 | }; |
3223 | cf::cede_to_tick; |
3431 | cf::cede_to_tick; |
3224 | |
3432 | |
3225 | { |
3433 | { |
3226 | my $faces = $facedata->{faceinfo}; |
3434 | my $faces = $facedata->{faceinfo}; |
… | |
… | |
3228 | while (my ($face, $info) = each %$faces) { |
3436 | while (my ($face, $info) = each %$faces) { |
3229 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3437 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3230 | |
3438 | |
3231 | cf::face::set_visibility $idx, $info->{visibility}; |
3439 | cf::face::set_visibility $idx, $info->{visibility}; |
3232 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3440 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3233 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3441 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3234 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3442 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3235 | |
3443 | |
3236 | cf::cede_to_tick; |
3444 | cf::cede_to_tick; |
3237 | } |
3445 | } |
3238 | |
3446 | |
3239 | while (my ($face, $info) = each %$faces) { |
3447 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3244 | |
3452 | |
3245 | if (my $smooth = cf::face::find $info->{smooth}) { |
3453 | if (my $smooth = cf::face::find $info->{smooth}) { |
3246 | cf::face::set_smooth $idx, $smooth; |
3454 | cf::face::set_smooth $idx, $smooth; |
3247 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3455 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3248 | } else { |
3456 | } else { |
3249 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3457 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3250 | } |
3458 | } |
3251 | |
3459 | |
3252 | cf::cede_to_tick; |
3460 | cf::cede_to_tick; |
3253 | } |
3461 | } |
3254 | } |
3462 | } |
… | |
… | |
3263 | |
3471 | |
3264 | cf::anim::invalidate_all; # d'oh |
3472 | cf::anim::invalidate_all; # d'oh |
3265 | } |
3473 | } |
3266 | |
3474 | |
3267 | { |
3475 | { |
3268 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3269 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3270 | my $res = $facedata->{resource}; |
3476 | my $res = $facedata->{resource}; |
3271 | |
3477 | |
3272 | while (my ($name, $info) = each %$res) { |
3478 | while (my ($name, $info) = each %$res) { |
3273 | if (defined $info->{type}) { |
3479 | if (defined $info->{type}) { |
3274 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3480 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3275 | my $data; |
|
|
3276 | |
3481 | |
3277 | if ($info->{type} & 1) { |
3482 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3278 | # prepend meta info |
|
|
3279 | |
|
|
3280 | my $meta = $enc->encode ({ |
|
|
3281 | name => $name, |
|
|
3282 | %{ $info->{meta} || {} }, |
|
|
3283 | }); |
|
|
3284 | |
|
|
3285 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3286 | } else { |
|
|
3287 | $data = $info->{data}; |
|
|
3288 | } |
|
|
3289 | |
|
|
3290 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3291 | cf::face::set_type $idx, $info->{type}; |
3483 | cf::face::set_type $idx, $info->{type}; |
3292 | } else { |
3484 | } else { |
3293 | $RESOURCE{$name} = $info; |
3485 | $RESOURCE{$name} = $info; # unused |
3294 | } |
3486 | } |
3295 | |
3487 | |
3296 | cf::cede_to_tick; |
3488 | cf::cede_to_tick; |
3297 | } |
3489 | } |
3298 | } |
3490 | } |
3299 | |
3491 | |
3300 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3492 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3301 | |
3493 | |
3302 | 1 |
3494 | 1 |
3303 | } |
3495 | } |
3304 | |
|
|
3305 | cf::global->attach (on_resource_update => sub { |
|
|
3306 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3307 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3308 | |
|
|
3309 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3310 | my $sound = $soundconf->{compat}[$_] |
|
|
3311 | or next; |
|
|
3312 | |
|
|
3313 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3314 | cf::sound::set $sound->[0] => $face; |
|
|
3315 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3316 | } |
|
|
3317 | |
|
|
3318 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3319 | my $face = cf::face::find "sound/$v"; |
|
|
3320 | cf::sound::set $k => $face; |
|
|
3321 | } |
|
|
3322 | } |
|
|
3323 | }); |
|
|
3324 | |
3496 | |
3325 | register_exticmd fx_want => sub { |
3497 | register_exticmd fx_want => sub { |
3326 | my ($ns, $want) = @_; |
3498 | my ($ns, $want) = @_; |
3327 | |
3499 | |
3328 | while (my ($k, $v) = each %$want) { |
3500 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3367 | sub reload_treasures { |
3539 | sub reload_treasures { |
3368 | load_resource_file "$DATADIR/treasures" |
3540 | load_resource_file "$DATADIR/treasures" |
3369 | or die "unable to load treasurelists\n"; |
3541 | or die "unable to load treasurelists\n"; |
3370 | } |
3542 | } |
3371 | |
3543 | |
|
|
3544 | sub reload_sound { |
|
|
3545 | trace "loading sound config from $DATADIR/sound\n"; |
|
|
3546 | |
|
|
3547 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3548 | or die "$DATADIR/sound $!"; |
|
|
3549 | |
|
|
3550 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
|
|
3551 | |
|
|
3552 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3553 | my $sound = $soundconf->{compat}[$_] |
|
|
3554 | or next; |
|
|
3555 | |
|
|
3556 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3557 | cf::sound::set $sound->[0] => $face; |
|
|
3558 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3559 | } |
|
|
3560 | |
|
|
3561 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3562 | my $face = cf::face::find "sound/$v"; |
|
|
3563 | cf::sound::set $k => $face; |
|
|
3564 | } |
|
|
3565 | } |
|
|
3566 | |
3372 | sub reload_resources { |
3567 | sub reload_resources { |
3373 | warn "reloading resource files...\n"; |
3568 | trace "reloading resource files...\n"; |
3374 | |
3569 | |
|
|
3570 | reload_exp_table; |
|
|
3571 | reload_materials; |
3375 | reload_facedata; |
3572 | reload_facedata; |
|
|
3573 | reload_sound; |
3376 | reload_archetypes; |
3574 | reload_archetypes; |
3377 | reload_regions; |
3575 | reload_regions; |
3378 | reload_treasures; |
3576 | reload_treasures; |
3379 | |
3577 | |
3380 | warn "finished reloading resource files\n"; |
3578 | trace "finished reloading resource files\n"; |
3381 | } |
3579 | } |
3382 | |
3580 | |
3383 | sub reload_config { |
3581 | sub reload_config { |
3384 | open my $fh, "<:utf8", "$CONFDIR/config" |
3582 | trace "reloading config file...\n"; |
3385 | or return; |
|
|
3386 | |
3583 | |
3387 | local $/; |
3584 | 0 < aio_load "$CONFDIR/config", my $config |
3388 | *CFG = YAML::Load <$fh>; |
3585 | or die "$CONFDIR/config: $!"; |
3389 | |
3586 | |
|
|
3587 | utf8::decode $config; |
|
|
3588 | *CFG = yaml_load $config; |
|
|
3589 | |
3390 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3590 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3391 | |
3591 | |
3392 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3592 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3393 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3593 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3394 | |
3594 | |
3395 | if (exists $CFG{mlockall}) { |
3595 | if (exists $CFG{mlockall}) { |
… | |
… | |
3418 | |
3618 | |
3419 | seek $fh, 0, 0; |
3619 | seek $fh, 0, 0; |
3420 | print $fh $$; |
3620 | print $fh $$; |
3421 | } |
3621 | } |
3422 | |
3622 | |
|
|
3623 | sub main_loop { |
|
|
3624 | trace "EV::loop starting\n"; |
|
|
3625 | if (1) { |
|
|
3626 | EV::loop; |
|
|
3627 | } |
|
|
3628 | trace "EV::loop returned\n"; |
|
|
3629 | goto &main_loop unless $REALLY_UNLOOP; |
|
|
3630 | } |
|
|
3631 | |
3423 | sub main { |
3632 | sub main { |
3424 | cf::init_globals; # initialise logging |
3633 | cf::init_globals; # initialise logging |
3425 | |
3634 | |
3426 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3635 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3427 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3636 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3428 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3637 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3429 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3638 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3430 | |
|
|
3431 | cf::init_experience; |
|
|
3432 | cf::init_anim; |
|
|
3433 | cf::init_attackmess; |
|
|
3434 | cf::init_dynamic; |
|
|
3435 | cf::init_block; |
|
|
3436 | |
3639 | |
3437 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3640 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3438 | |
3641 | |
3439 | # we must not ever block the main coroutine |
3642 | # we must not ever block the main coroutine |
3440 | local $Coro::idle = sub { |
3643 | local $Coro::idle = sub { |
… | |
… | |
3446 | }; |
3649 | }; |
3447 | |
3650 | |
3448 | evthread_start IO::AIO::poll_fileno; |
3651 | evthread_start IO::AIO::poll_fileno; |
3449 | |
3652 | |
3450 | cf::sync_job { |
3653 | cf::sync_job { |
|
|
3654 | cf::incloader::init (); |
|
|
3655 | |
|
|
3656 | cf::init_anim; |
|
|
3657 | cf::init_attackmess; |
|
|
3658 | cf::init_dynamic; |
|
|
3659 | |
|
|
3660 | cf::load_settings; |
|
|
3661 | |
3451 | reload_resources; |
3662 | reload_resources; |
3452 | reload_config; |
3663 | reload_config; |
3453 | db_init; |
3664 | db_init; |
3454 | |
3665 | |
3455 | cf::load_settings; |
|
|
3456 | cf::load_materials; |
|
|
3457 | cf::init_uuid; |
3666 | cf::init_uuid; |
3458 | cf::init_signals; |
3667 | cf::init_signals; |
3459 | cf::init_commands; |
|
|
3460 | cf::init_skills; |
3668 | cf::init_skills; |
3461 | |
3669 | |
3462 | cf::init_beforeplay; |
3670 | cf::init_beforeplay; |
3463 | |
3671 | |
3464 | atomic; |
3672 | atomic; |
… | |
… | |
3466 | load_extensions; |
3674 | load_extensions; |
3467 | |
3675 | |
3468 | utime time, time, $RUNTIMEFILE; |
3676 | utime time, time, $RUNTIMEFILE; |
3469 | |
3677 | |
3470 | # no (long-running) fork's whatsoever before this point(!) |
3678 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3679 | use POSIX (); |
3471 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3680 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3472 | |
3681 | |
3473 | (pop @POST_INIT)->(0) while @POST_INIT; |
3682 | cf::_post_init 0; |
3474 | }; |
3683 | }; |
3475 | |
3684 | |
3476 | EV::loop; |
3685 | cf::object::thawer::errors_are_fatal 0; |
|
|
3686 | info "parse errors in files are no longer fatal from this point on.\n"; |
|
|
3687 | |
|
|
3688 | my $free_main; $free_main = EV::idle sub { |
|
|
3689 | undef $free_main; |
|
|
3690 | undef &main; # free gobs of memory :) |
|
|
3691 | }; |
|
|
3692 | |
|
|
3693 | goto &main_loop; |
3477 | } |
3694 | } |
3478 | |
3695 | |
3479 | ############################################################################# |
3696 | ############################################################################# |
3480 | # initialisation and cleanup |
3697 | # initialisation and cleanup |
3481 | |
3698 | |
3482 | # install some emergency cleanup handlers |
3699 | # install some emergency cleanup handlers |
3483 | BEGIN { |
3700 | BEGIN { |
3484 | our %SIGWATCHER = (); |
3701 | our %SIGWATCHER = (); |
3485 | for my $signal (qw(INT HUP TERM)) { |
3702 | for my $signal (qw(INT HUP TERM)) { |
3486 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3703 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3487 | cf::cleanup "SIG$signal"; |
3704 | cf::cleanup "SIG$signal"; |
3488 | }; |
3705 | }; |
3489 | } |
3706 | } |
3490 | } |
3707 | } |
3491 | |
3708 | |
3492 | sub write_runtime_sync { |
3709 | sub write_runtime_sync { |
|
|
3710 | my $t0 = AE::time; |
|
|
3711 | |
3493 | # first touch the runtime file to show we are still running: |
3712 | # first touch the runtime file to show we are still running: |
3494 | # the fsync below can take a very very long time. |
3713 | # the fsync below can take a very very long time. |
3495 | |
3714 | |
3496 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3715 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3497 | |
3716 | |
3498 | my $guard = cf::lock_acquire "write_runtime"; |
3717 | my $guard = cf::lock_acquire "write_runtime"; |
3499 | |
3718 | |
3500 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3719 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3501 | or return; |
3720 | or return; |
3502 | |
3721 | |
3503 | my $value = $cf::RUNTIME + 90 + 10; |
3722 | my $value = $cf::RUNTIME + 90 + 10; |
3504 | # 10 is the runtime save interval, for a monotonic clock |
3723 | # 10 is the runtime save interval, for a monotonic clock |
3505 | # 60 allows for the watchdog to kill the server. |
3724 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3518 | or return; |
3737 | or return; |
3519 | |
3738 | |
3520 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3739 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3521 | and return; |
3740 | and return; |
3522 | |
3741 | |
3523 | warn "runtime file written.\n"; |
3742 | trace sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3524 | |
3743 | |
3525 | 1 |
3744 | 1 |
3526 | } |
3745 | } |
3527 | |
3746 | |
3528 | our $uuid_lock; |
3747 | our $uuid_lock; |
… | |
… | |
3540 | or return; |
3759 | or return; |
3541 | |
3760 | |
3542 | my $value = uuid_seq uuid_cur; |
3761 | my $value = uuid_seq uuid_cur; |
3543 | |
3762 | |
3544 | unless ($value) { |
3763 | unless ($value) { |
3545 | warn "cowardly refusing to write zero uuid value!\n"; |
3764 | info "cowardly refusing to write zero uuid value!\n"; |
3546 | return; |
3765 | return; |
3547 | } |
3766 | } |
3548 | |
3767 | |
3549 | my $value = uuid_str $value + $uuid_skip; |
3768 | my $value = uuid_str $value + $uuid_skip; |
3550 | $uuid_skip = 0; |
3769 | $uuid_skip = 0; |
… | |
… | |
3560 | or return; |
3779 | or return; |
3561 | |
3780 | |
3562 | aio_rename "$uuid~", $uuid |
3781 | aio_rename "$uuid~", $uuid |
3563 | and return; |
3782 | and return; |
3564 | |
3783 | |
3565 | warn "uuid file written ($value).\n"; |
3784 | trace "uuid file written ($value).\n"; |
3566 | |
3785 | |
3567 | 1 |
3786 | 1 |
3568 | |
3787 | |
3569 | } |
3788 | } |
3570 | |
3789 | |
… | |
… | |
3576 | } |
3795 | } |
3577 | |
3796 | |
3578 | sub emergency_save() { |
3797 | sub emergency_save() { |
3579 | my $freeze_guard = cf::freeze_mainloop; |
3798 | my $freeze_guard = cf::freeze_mainloop; |
3580 | |
3799 | |
3581 | warn "emergency_perl_save: enter\n"; |
3800 | info "emergency_perl_save: enter\n"; |
|
|
3801 | |
|
|
3802 | # this is a trade-off: we want to be very quick here, so |
|
|
3803 | # save all maps without fsync, and later call a global sync |
|
|
3804 | # (which in turn might be very very slow) |
|
|
3805 | local $USE_FSYNC = 0; |
3582 | |
3806 | |
3583 | cf::sync_job { |
3807 | cf::sync_job { |
3584 | # this is a trade-off: we want to be very quick here, so |
3808 | cf::write_runtime_sync; # external watchdog should not bark |
3585 | # save all maps without fsync, and later call a global sync |
|
|
3586 | # (which in turn might be very very slow) |
|
|
3587 | local $USE_FSYNC = 0; |
|
|
3588 | |
3809 | |
3589 | # use a peculiar iteration method to avoid tripping on perl |
3810 | # use a peculiar iteration method to avoid tripping on perl |
3590 | # refcount bugs in for. also avoids problems with players |
3811 | # refcount bugs in for. also avoids problems with players |
3591 | # and maps saved/destroyed asynchronously. |
3812 | # and maps saved/destroyed asynchronously. |
3592 | warn "emergency_perl_save: begin player save\n"; |
3813 | info "emergency_perl_save: begin player save\n"; |
3593 | for my $login (keys %cf::PLAYER) { |
3814 | for my $login (keys %cf::PLAYER) { |
3594 | my $pl = $cf::PLAYER{$login} or next; |
3815 | my $pl = $cf::PLAYER{$login} or next; |
3595 | $pl->valid or next; |
3816 | $pl->valid or next; |
3596 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3817 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3597 | $pl->save; |
3818 | $pl->save; |
3598 | } |
3819 | } |
3599 | warn "emergency_perl_save: end player save\n"; |
3820 | info "emergency_perl_save: end player save\n"; |
3600 | |
3821 | |
|
|
3822 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3823 | |
3601 | warn "emergency_perl_save: begin map save\n"; |
3824 | info "emergency_perl_save: begin map save\n"; |
3602 | for my $path (keys %cf::MAP) { |
3825 | for my $path (keys %cf::MAP) { |
3603 | my $map = $cf::MAP{$path} or next; |
3826 | my $map = $cf::MAP{$path} or next; |
3604 | $map->valid or next; |
3827 | $map->valid or next; |
3605 | $map->save; |
3828 | $map->save; |
3606 | } |
3829 | } |
3607 | warn "emergency_perl_save: end map save\n"; |
3830 | info "emergency_perl_save: end map save\n"; |
3608 | |
3831 | |
|
|
3832 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3833 | |
3609 | warn "emergency_perl_save: begin database checkpoint\n"; |
3834 | info "emergency_perl_save: begin database checkpoint\n"; |
3610 | BDB::db_env_txn_checkpoint $DB_ENV; |
3835 | BDB::db_env_txn_checkpoint $DB_ENV; |
3611 | warn "emergency_perl_save: end database checkpoint\n"; |
3836 | info "emergency_perl_save: end database checkpoint\n"; |
3612 | |
3837 | |
3613 | warn "emergency_perl_save: begin write uuid\n"; |
3838 | info "emergency_perl_save: begin write uuid\n"; |
3614 | write_uuid_sync 1; |
3839 | write_uuid_sync 1; |
3615 | warn "emergency_perl_save: end write uuid\n"; |
3840 | info "emergency_perl_save: end write uuid\n"; |
|
|
3841 | |
|
|
3842 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3843 | |
|
|
3844 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3845 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3846 | |
|
|
3847 | info "emergency_perl_save: starting sync\n"; |
|
|
3848 | IO::AIO::aio_sync sub { |
|
|
3849 | info "emergency_perl_save: finished sync\n"; |
|
|
3850 | }; |
|
|
3851 | |
|
|
3852 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3853 | |
|
|
3854 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3855 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3856 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3857 | } |
|
|
3858 | |
|
|
3859 | cf::write_runtime_sync; # external watchdog should not bark |
3616 | }; |
3860 | }; |
3617 | |
3861 | |
3618 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3619 | IO::AIO::aio_sync sub { |
|
|
3620 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3621 | }; |
|
|
3622 | |
|
|
3623 | warn "emergency_perl_save: leave\n"; |
3862 | info "emergency_perl_save: leave\n"; |
3624 | } |
3863 | } |
3625 | |
3864 | |
3626 | sub post_cleanup { |
3865 | sub post_cleanup { |
3627 | my ($make_core) = @_; |
3866 | my ($make_core) = @_; |
3628 | |
3867 | |
|
|
3868 | IO::AIO::flush; |
|
|
3869 | |
3629 | warn Carp::longmess "post_cleanup backtrace" |
3870 | error Carp::longmess "post_cleanup backtrace" |
3630 | if $make_core; |
3871 | if $make_core; |
3631 | |
3872 | |
3632 | my $fh = pidfile; |
3873 | my $fh = pidfile; |
3633 | unlink $PIDFILE if <$fh> == $$; |
3874 | unlink $PIDFILE if <$fh> == $$; |
3634 | } |
3875 | } |
… | |
… | |
3654 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3895 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3655 | for my $name (keys %$leaf_symtab) { |
3896 | for my $name (keys %$leaf_symtab) { |
3656 | _gv_clear *{"$pkg$name"}; |
3897 | _gv_clear *{"$pkg$name"}; |
3657 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3898 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3658 | } |
3899 | } |
3659 | warn "cleared package $pkg\n";#d# |
|
|
3660 | } |
3900 | } |
3661 | |
3901 | |
3662 | sub do_reload_perl() { |
3902 | sub do_reload_perl() { |
3663 | # can/must only be called in main |
3903 | # can/must only be called in main |
3664 | if ($Coro::current != $Coro::main) { |
3904 | unless (in_main) { |
3665 | warn "can only reload from main coroutine"; |
3905 | error "can only reload from main coroutine"; |
3666 | return; |
3906 | return; |
3667 | } |
3907 | } |
3668 | |
3908 | |
3669 | return if $RELOAD++; |
3909 | return if $RELOAD++; |
3670 | |
3910 | |
3671 | my $t1 = EV::time; |
3911 | my $t1 = AE::time; |
3672 | |
3912 | |
3673 | while ($RELOAD) { |
3913 | while ($RELOAD) { |
3674 | warn "reloading..."; |
3914 | cf::get_slot 0.1, -1, "reload_perl"; |
|
|
3915 | info "perl_reload: reloading..."; |
3675 | |
3916 | |
3676 | warn "entering sync_job"; |
3917 | trace "perl_reload: entering sync_job"; |
3677 | |
3918 | |
3678 | cf::sync_job { |
3919 | cf::sync_job { |
3679 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3680 | cf::emergency_save; |
3920 | #cf::emergency_save; |
3681 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3682 | |
3921 | |
3683 | warn "syncing database to disk"; |
|
|
3684 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3685 | |
|
|
3686 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
3687 | |
|
|
3688 | warn "flushing outstanding aio requests"; |
|
|
3689 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3690 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3691 | } |
|
|
3692 | |
|
|
3693 | warn "cancelling all extension coros"; |
3922 | trace "perl_reload: cancelling all extension coros"; |
3694 | $_->cancel for values %EXT_CORO; |
3923 | $_->cancel for values %EXT_CORO; |
3695 | %EXT_CORO = (); |
3924 | %EXT_CORO = (); |
3696 | |
3925 | |
3697 | warn "removing commands"; |
3926 | trace "perl_reload: removing commands"; |
3698 | %COMMAND = (); |
3927 | %COMMAND = (); |
3699 | |
3928 | |
3700 | warn "removing ext/exti commands"; |
3929 | trace "perl_reload: removing ext/exti commands"; |
3701 | %EXTCMD = (); |
3930 | %EXTCMD = (); |
3702 | %EXTICMD = (); |
3931 | %EXTICMD = (); |
3703 | |
3932 | |
3704 | warn "unloading/nuking all extensions"; |
3933 | trace "perl_reload: unloading/nuking all extensions"; |
3705 | for my $pkg (@EXTS) { |
3934 | for my $pkg (@EXTS) { |
3706 | warn "... unloading $pkg"; |
3935 | trace "... unloading $pkg"; |
3707 | |
3936 | |
3708 | if (my $cb = $pkg->can ("unload")) { |
3937 | if (my $cb = $pkg->can ("unload")) { |
3709 | eval { |
3938 | eval { |
3710 | $cb->($pkg); |
3939 | $cb->($pkg); |
3711 | 1 |
3940 | 1 |
3712 | } or warn "$pkg unloaded, but with errors: $@"; |
3941 | } or error "$pkg unloaded, but with errors: $@"; |
3713 | } |
3942 | } |
3714 | |
3943 | |
3715 | warn "... clearing $pkg"; |
3944 | trace "... clearing $pkg"; |
3716 | clear_package $pkg; |
3945 | clear_package $pkg; |
3717 | } |
3946 | } |
3718 | |
3947 | |
3719 | warn "unloading all perl modules loaded from $LIBDIR"; |
3948 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3720 | while (my ($k, $v) = each %INC) { |
3949 | while (my ($k, $v) = each %INC) { |
3721 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3950 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3722 | |
3951 | |
3723 | warn "... unloading $k"; |
3952 | trace "... unloading $k"; |
3724 | delete $INC{$k}; |
3953 | delete $INC{$k}; |
3725 | |
3954 | |
3726 | $k =~ s/\.pm$//; |
3955 | $k =~ s/\.pm$//; |
3727 | $k =~ s/\//::/g; |
3956 | $k =~ s/\//::/g; |
3728 | |
3957 | |
… | |
… | |
3731 | } |
3960 | } |
3732 | |
3961 | |
3733 | clear_package $k; |
3962 | clear_package $k; |
3734 | } |
3963 | } |
3735 | |
3964 | |
3736 | warn "getting rid of safe::, as good as possible"; |
3965 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3737 | clear_package "safe::$_" |
3966 | clear_package "safe::$_" |
3738 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3967 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3739 | |
3968 | |
3740 | warn "unloading cf.pm \"a bit\""; |
3969 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3741 | delete $INC{"cf.pm"}; |
3970 | delete $INC{"cf.pm"}; |
3742 | delete $INC{"cf/pod.pm"}; |
3971 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3743 | |
3972 | |
3744 | # don't, removes xs symbols, too, |
3973 | # don't, removes xs symbols, too, |
3745 | # and global variables created in xs |
3974 | # and global variables created in xs |
3746 | #clear_package __PACKAGE__; |
3975 | #clear_package __PACKAGE__; |
3747 | |
3976 | |
3748 | warn "unload completed, starting to reload now"; |
3977 | info "perl_reload: unload completed, starting to reload now"; |
3749 | |
3978 | |
3750 | warn "reloading cf.pm"; |
3979 | trace "perl_reload: reloading cf.pm"; |
3751 | require cf; |
3980 | require cf; |
3752 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3981 | cf::_connect_to_perl_1; |
3753 | |
3982 | |
3754 | warn "loading config and database again"; |
3983 | trace "perl_reload: loading config and database again"; |
3755 | cf::reload_config; |
3984 | cf::reload_config; |
3756 | |
3985 | |
3757 | warn "loading extensions"; |
3986 | trace "perl_reload: loading extensions"; |
3758 | cf::load_extensions; |
3987 | cf::load_extensions; |
3759 | |
3988 | |
3760 | if ($REATTACH_ON_RELOAD) { |
3989 | if ($REATTACH_ON_RELOAD) { |
3761 | warn "reattaching attachments to objects/players"; |
3990 | trace "perl_reload: reattaching attachments to objects/players"; |
3762 | _global_reattach; # objects, sockets |
3991 | _global_reattach; # objects, sockets |
3763 | warn "reattaching attachments to maps"; |
3992 | trace "perl_reload: reattaching attachments to maps"; |
3764 | reattach $_ for values %MAP; |
3993 | reattach $_ for values %MAP; |
3765 | warn "reattaching attachments to players"; |
3994 | trace "perl_reload: reattaching attachments to players"; |
3766 | reattach $_ for values %PLAYER; |
3995 | reattach $_ for values %PLAYER; |
3767 | } |
3996 | } |
3768 | |
3997 | |
3769 | warn "running post_init jobs"; |
3998 | cf::_post_init 1; |
3770 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3771 | |
3999 | |
3772 | warn "leaving sync_job"; |
4000 | trace "perl_reload: leaving sync_job"; |
3773 | |
4001 | |
3774 | 1 |
4002 | 1 |
3775 | } or do { |
4003 | } or do { |
3776 | warn $@; |
4004 | error $@; |
3777 | cf::cleanup "error while reloading, exiting."; |
4005 | cf::cleanup "perl_reload: error, exiting."; |
3778 | }; |
4006 | }; |
3779 | |
4007 | |
3780 | warn "reloaded"; |
|
|
3781 | --$RELOAD; |
4008 | --$RELOAD; |
3782 | } |
4009 | } |
3783 | |
4010 | |
3784 | $t1 = EV::time - $t1; |
4011 | $t1 = AE::time - $t1; |
3785 | warn "reload completed in ${t1}s\n"; |
4012 | info "perl_reload: completed in ${t1}s\n"; |
3786 | }; |
4013 | }; |
3787 | |
4014 | |
3788 | our $RELOAD_WATCHER; # used only during reload |
4015 | our $RELOAD_WATCHER; # used only during reload |
3789 | |
4016 | |
3790 | sub reload_perl() { |
4017 | sub reload_perl() { |
… | |
… | |
3792 | # coro crashes during coro_state_free->destroy here. |
4019 | # coro crashes during coro_state_free->destroy here. |
3793 | |
4020 | |
3794 | $RELOAD_WATCHER ||= cf::async { |
4021 | $RELOAD_WATCHER ||= cf::async { |
3795 | Coro::AIO::aio_wait cache_extensions; |
4022 | Coro::AIO::aio_wait cache_extensions; |
3796 | |
4023 | |
3797 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
4024 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3798 | do_reload_perl; |
4025 | do_reload_perl; |
3799 | undef $RELOAD_WATCHER; |
4026 | undef $RELOAD_WATCHER; |
3800 | }; |
4027 | }; |
3801 | }; |
4028 | }; |
3802 | } |
4029 | } |
… | |
… | |
3811 | reload_perl; |
4038 | reload_perl; |
3812 | }; |
4039 | }; |
3813 | } |
4040 | } |
3814 | }; |
4041 | }; |
3815 | |
4042 | |
3816 | unshift @INC, $LIBDIR; |
4043 | ############################################################################# |
3817 | |
4044 | |
3818 | my $bug_warning = 0; |
4045 | my $bug_warning = 0; |
3819 | |
4046 | |
3820 | our @WAIT_FOR_TICK; |
4047 | our @WAIT_FOR_TICK; |
3821 | our @WAIT_FOR_TICK_BEGIN; |
4048 | our @WAIT_FOR_TICK_BEGIN; |
3822 | |
4049 | |
3823 | sub wait_for_tick { |
4050 | sub wait_for_tick() { |
3824 | return if tick_inhibit || $Coro::current == $Coro::main; |
4051 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3825 | |
4052 | |
3826 | my $signal = new Coro::Signal; |
4053 | my $signal = new Coro::Signal; |
3827 | push @WAIT_FOR_TICK, $signal; |
4054 | push @WAIT_FOR_TICK, $signal; |
3828 | $signal->wait; |
4055 | $signal->wait; |
3829 | } |
4056 | } |
3830 | |
4057 | |
3831 | sub wait_for_tick_begin { |
4058 | sub wait_for_tick_begin() { |
3832 | return if tick_inhibit || $Coro::current == $Coro::main; |
4059 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3833 | |
4060 | |
3834 | my $signal = new Coro::Signal; |
4061 | my $signal = new Coro::Signal; |
3835 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4062 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3836 | $signal->wait; |
4063 | $signal->wait; |
3837 | } |
4064 | } |
… | |
… | |
3843 | return; |
4070 | return; |
3844 | } |
4071 | } |
3845 | |
4072 | |
3846 | cf::server_tick; # one server iteration |
4073 | cf::server_tick; # one server iteration |
3847 | |
4074 | |
|
|
4075 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
|
|
4076 | |
3848 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4077 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3849 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4078 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3850 | Coro::async_pool { |
4079 | Coro::async_pool { |
3851 | $Coro::current->{desc} = "runtime saver"; |
4080 | $Coro::current->{desc} = "runtime saver"; |
3852 | write_runtime_sync |
4081 | write_runtime_sync |
3853 | or warn "ERROR: unable to write runtime file: $!"; |
4082 | or error "ERROR: unable to write runtime file: $!"; |
3854 | }; |
4083 | }; |
3855 | } |
4084 | } |
3856 | |
4085 | |
3857 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4086 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3858 | $sig->send; |
4087 | $sig->send; |
… | |
… | |
3866 | |
4095 | |
3867 | if (0) { |
4096 | if (0) { |
3868 | if ($NEXT_TICK) { |
4097 | if ($NEXT_TICK) { |
3869 | my $jitter = $TICK_START - $NEXT_TICK; |
4098 | my $jitter = $TICK_START - $NEXT_TICK; |
3870 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4099 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
3871 | warn "jitter $JITTER\n";#d# |
4100 | debug "jitter $JITTER\n";#d# |
3872 | } |
4101 | } |
3873 | } |
4102 | } |
3874 | } |
4103 | } |
3875 | |
4104 | |
3876 | { |
4105 | { |
3877 | # configure BDB |
4106 | # configure BDB |
3878 | |
4107 | |
3879 | BDB::min_parallel 8; |
4108 | BDB::min_parallel 16; |
3880 | BDB::max_poll_reqs $TICK * 0.1; |
4109 | BDB::max_poll_reqs $TICK * 0.1; |
3881 | $AnyEvent::BDB::WATCHER->priority (1); |
4110 | $AnyEvent::BDB::WATCHER->priority (1); |
3882 | |
4111 | |
3883 | unless ($DB_ENV) { |
4112 | unless ($DB_ENV) { |
3884 | $DB_ENV = BDB::db_env_create; |
4113 | $DB_ENV = BDB::db_env_create; |
… | |
… | |
3964 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4193 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3965 | } |
4194 | } |
3966 | } |
4195 | } |
3967 | |
4196 | |
3968 | # load additional modules |
4197 | # load additional modules |
3969 | use cf::pod; |
4198 | require "cf/$_.pm" for @EXTRA_MODULES; |
|
|
4199 | cf::_connect_to_perl_2; |
3970 | |
4200 | |
3971 | END { cf::emergency_save } |
4201 | END { cf::emergency_save } |
3972 | |
4202 | |
3973 | 1 |
4203 | 1 |
3974 | |
4204 | |