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 5.10.0; |
25 | use utf8; |
26 | use utf8; |
… | |
… | |
31 | use EV; |
32 | use EV; |
32 | use Opcode; |
33 | use Opcode; |
33 | use Safe; |
34 | use Safe; |
34 | use Safe::Hole; |
35 | use Safe::Hole; |
35 | use Storable (); |
36 | use Storable (); |
|
|
37 | use Carp (); |
36 | |
38 | |
37 | use Guard (); |
39 | use Guard (); |
38 | use Coro (); |
40 | use Coro (); |
39 | use Coro::State; |
41 | use Coro::State; |
40 | use Coro::Handle; |
42 | use Coro::Handle; |
… | |
… | |
51 | use Coro::Util (); |
53 | use Coro::Util (); |
52 | |
54 | |
53 | use JSON::XS 2.01 (); |
55 | use JSON::XS 2.01 (); |
54 | use BDB (); |
56 | use BDB (); |
55 | use Data::Dumper; |
57 | use Data::Dumper; |
56 | use Digest::MD5; |
|
|
57 | use Fcntl; |
58 | use Fcntl; |
58 | use YAML (); |
59 | use YAML::XS (); |
59 | use IO::AIO (); |
60 | use IO::AIO (); |
60 | use Time::HiRes; |
61 | use Time::HiRes; |
61 | use Compress::LZF; |
62 | use Compress::LZF; |
62 | use Digest::MD5 (); |
63 | use Digest::MD5 (); |
63 | |
64 | |
… | |
… | |
71 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
72 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
72 | |
73 | |
73 | # make sure c-lzf reinitialises itself |
74 | # make sure c-lzf reinitialises itself |
74 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
75 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
75 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
76 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
77 | |
|
|
78 | # strictly for debugging |
|
|
79 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
76 | |
80 | |
77 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
81 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
78 | |
82 | |
79 | our %COMMAND = (); |
83 | our %COMMAND = (); |
80 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
… | |
… | |
85 | our %EXT_CORO = (); # coroutines bound to extensions |
89 | our %EXT_CORO = (); # coroutines bound to extensions |
86 | our %EXT_MAP = (); # pluggable maps |
90 | our %EXT_MAP = (); # pluggable maps |
87 | |
91 | |
88 | 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 |
89 | our @EVENT; |
93 | our @EVENT; |
|
|
94 | our @REFLECT; # set by XS |
|
|
95 | our %REFLECT; # set by us |
90 | |
96 | |
91 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
92 | our $DATADIR = datadir; |
98 | our $DATADIR = datadir; |
93 | our $LIBDIR = "$DATADIR/ext"; |
99 | our $LIBDIR = "$DATADIR/ext"; |
94 | our $PODDIR = "$DATADIR/pod"; |
100 | our $PODDIR = "$DATADIR/pod"; |
… | |
… | |
112 | our $BDB_DEADLOCK_WATCHER; |
118 | our $BDB_DEADLOCK_WATCHER; |
113 | our $BDB_CHECKPOINT_WATCHER; |
119 | our $BDB_CHECKPOINT_WATCHER; |
114 | our $BDB_TRICKLE_WATCHER; |
120 | our $BDB_TRICKLE_WATCHER; |
115 | our $DB_ENV; |
121 | our $DB_ENV; |
116 | |
122 | |
117 | our @EXTRA_MODULES = qw(pod mapscript); |
123 | our @EXTRA_MODULES = qw(pod match mapscript); |
118 | |
124 | |
119 | our %CFG; |
125 | our %CFG; |
120 | |
126 | |
121 | our $UPTIME; $UPTIME ||= time; |
127 | our $UPTIME; $UPTIME ||= time; |
122 | our $RUNTIME; |
128 | our $RUNTIME; |
… | |
… | |
134 | our $JITTER; # average jitter |
140 | our $JITTER; # average jitter |
135 | our $TICK_START; # for load detecting purposes |
141 | our $TICK_START; # for load detecting purposes |
136 | |
142 | |
137 | our @POST_INIT; |
143 | our @POST_INIT; |
138 | |
144 | |
139 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
145 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
146 | our $REALLY_UNLOOP; # never set to true, please :) |
140 | |
147 | |
141 | binmode STDOUT; |
148 | binmode STDOUT; |
142 | binmode STDERR; |
149 | binmode STDERR; |
143 | |
150 | |
144 | # read virtual server time, if available |
151 | # read virtual server time, if available |
… | |
… | |
157 | |
164 | |
158 | sub cf::map::normalise; |
165 | sub cf::map::normalise; |
159 | |
166 | |
160 | ############################################################################# |
167 | ############################################################################# |
161 | |
168 | |
|
|
169 | %REFLECT = (); |
|
|
170 | for (@REFLECT) { |
|
|
171 | my $reflect = JSON::XS::decode_json $_; |
|
|
172 | $REFLECT{$reflect->{class}} = $reflect; |
|
|
173 | } |
|
|
174 | |
|
|
175 | # this is decidedly evil |
|
|
176 | $REFLECT{object}{flags} = { map +($_ => undef), grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"} }; |
|
|
177 | |
|
|
178 | ############################################################################# |
|
|
179 | |
162 | =head2 GLOBAL VARIABLES |
180 | =head2 GLOBAL VARIABLES |
163 | |
181 | |
164 | =over 4 |
182 | =over 4 |
165 | |
183 | |
166 | =item $cf::UPTIME |
184 | =item $cf::UPTIME |
… | |
… | |
210 | returns directly I<after> the tick processing (and consequently, can only wake one process |
228 | returns directly I<after> the tick processing (and consequently, can only wake one process |
211 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
229 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
212 | |
230 | |
213 | =item @cf::INVOKE_RESULTS |
231 | =item @cf::INVOKE_RESULTS |
214 | |
232 | |
215 | This array contains the results of the last C<invoke ()> call. When |
233 | This array contains the results of the last C<invoke ()> call. When |
216 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
234 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
217 | that call. |
235 | that call. |
218 | |
236 | |
|
|
237 | =item %cf::REFLECT |
|
|
238 | |
|
|
239 | Contains, for each (C++) class name, a hash reference with information |
|
|
240 | about object members (methods, scalars, arrays and flags) and other |
|
|
241 | metadata, which is useful for introspection. |
|
|
242 | |
219 | =back |
243 | =back |
220 | |
244 | |
221 | =cut |
245 | =cut |
222 | |
246 | |
223 | BEGIN { |
247 | $Coro::State::WARNHOOK = sub { |
224 | *CORE::GLOBAL::warn = sub { |
|
|
225 | my $msg = join "", @_; |
248 | my $msg = join "", @_; |
226 | |
249 | |
227 | $msg .= "\n" |
250 | $msg .= "\n" |
228 | unless $msg =~ /\n$/; |
251 | unless $msg =~ /\n$/; |
229 | |
252 | |
230 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
253 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
231 | |
254 | |
232 | LOG llevError, $msg; |
255 | LOG llevError, $msg; |
233 | }; |
256 | }; |
234 | } |
|
|
235 | |
257 | |
236 | $Coro::State::DIEHOOK = sub { |
258 | $Coro::State::DIEHOOK = sub { |
237 | return unless $^S eq 0; # "eq", not "==" |
259 | return unless $^S eq 0; # "eq", not "==" |
|
|
260 | |
|
|
261 | warn Carp::longmess $_[0]; |
238 | |
262 | |
239 | if ($Coro::current == $Coro::main) {#d# |
263 | if ($Coro::current == $Coro::main) {#d# |
240 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
264 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
241 | return;#d# |
265 | return;#d# |
242 | }#d# |
266 | }#d# |
243 | |
267 | |
244 | # kill coroutine otherwise |
268 | # kill coroutine otherwise |
245 | warn Carp::longmess $_[0]; |
|
|
246 | Coro::terminate |
269 | Coro::terminate |
247 | }; |
270 | }; |
248 | |
|
|
249 | $SIG{__DIE__} = sub { }; #d#? |
|
|
250 | |
271 | |
251 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
272 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
252 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
273 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
253 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
274 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
254 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
275 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
268 | )) { |
289 | )) { |
269 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
290 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
270 | } |
291 | } |
271 | |
292 | |
272 | $EV::DIED = sub { |
293 | $EV::DIED = sub { |
273 | warn "error in event callback: @_"; |
294 | Carp::cluck "error in event callback: @_"; |
274 | }; |
295 | }; |
275 | |
296 | |
276 | ############################################################################# |
297 | ############################################################################# |
277 | |
298 | |
278 | =head2 UTILITY FUNCTIONS |
299 | =head2 UTILITY FUNCTIONS |
… | |
… | |
402 | |
423 | |
403 | =cut |
424 | =cut |
404 | |
425 | |
405 | our @SLOT_QUEUE; |
426 | our @SLOT_QUEUE; |
406 | our $SLOT_QUEUE; |
427 | our $SLOT_QUEUE; |
|
|
428 | our $SLOT_DECAY = 0.9; |
407 | |
429 | |
408 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
430 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
409 | $SLOT_QUEUE = Coro::async { |
431 | $SLOT_QUEUE = Coro::async { |
410 | $Coro::current->desc ("timeslot manager"); |
432 | $Coro::current->desc ("timeslot manager"); |
411 | |
433 | |
412 | my $signal = new Coro::Signal; |
434 | my $signal = new Coro::Signal; |
|
|
435 | my $busy; |
413 | |
436 | |
414 | while () { |
437 | while () { |
415 | next_job: |
438 | next_job: |
|
|
439 | |
416 | my $avail = cf::till_tick; |
440 | my $avail = cf::till_tick; |
417 | if ($avail > 0.01) { |
441 | |
418 | for (0 .. $#SLOT_QUEUE) { |
442 | for (0 .. $#SLOT_QUEUE) { |
419 | if ($SLOT_QUEUE[$_][0] < $avail) { |
443 | if ($SLOT_QUEUE[$_][0] <= $avail) { |
|
|
444 | $busy = 0; |
420 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
445 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
421 | $job->[2]->send; |
446 | $job->[2]->send; |
422 | Coro::cede; |
447 | Coro::cede; |
423 | goto next_job; |
448 | goto next_job; |
424 | } |
449 | } else { |
|
|
450 | $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; |
425 | } |
451 | } |
426 | } |
452 | } |
427 | |
453 | |
428 | if (@SLOT_QUEUE) { |
454 | if (@SLOT_QUEUE) { |
429 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
455 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
430 | push @cf::WAIT_FOR_TICK, $signal; |
456 | push @cf::WAIT_FOR_TICK, $signal; |
431 | $signal->wait; |
457 | $signal->wait; |
432 | } else { |
458 | } else { |
|
|
459 | $busy = 0; |
433 | Coro::schedule; |
460 | Coro::schedule; |
434 | } |
461 | } |
435 | } |
462 | } |
436 | }; |
463 | }; |
437 | |
464 | |
438 | sub get_slot($;$$) { |
465 | sub get_slot($;$$) { |
439 | return if tick_inhibit || $Coro::current == $Coro::main; |
466 | return if tick_inhibit || $Coro::current == $Coro::main; |
440 | |
467 | |
441 | my ($time, $pri, $name) = @_; |
468 | my ($time, $pri, $name) = @_; |
442 | |
469 | |
443 | $time = $TICK * .6 if $time > $TICK * .6; |
470 | $time = clamp $time, 0.01, $TICK * .6; |
|
|
471 | |
444 | my $sig = new Coro::Signal; |
472 | my $sig = new Coro::Signal; |
445 | |
473 | |
446 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
474 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
447 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
475 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
448 | $SLOT_QUEUE->ready; |
476 | $SLOT_QUEUE->ready; |
… | |
… | |
475 | |
503 | |
476 | sub sync_job(&) { |
504 | sub sync_job(&) { |
477 | my ($job) = @_; |
505 | my ($job) = @_; |
478 | |
506 | |
479 | if ($Coro::current == $Coro::main) { |
507 | if ($Coro::current == $Coro::main) { |
480 | my $time = EV::time; |
508 | my $time = AE::time; |
481 | |
509 | |
482 | # this is the main coro, too bad, we have to block |
510 | # this is the main coro, too bad, we have to block |
483 | # till the operation succeeds, freezing the server :/ |
511 | # till the operation succeeds, freezing the server :/ |
484 | |
512 | |
485 | LOG llevError, Carp::longmess "sync job";#d# |
513 | LOG llevError, Carp::longmess "sync job";#d# |
… | |
… | |
502 | } else { |
530 | } else { |
503 | EV::loop EV::LOOP_ONESHOT; |
531 | EV::loop EV::LOOP_ONESHOT; |
504 | } |
532 | } |
505 | } |
533 | } |
506 | |
534 | |
507 | my $time = EV::time - $time; |
535 | my $time = AE::time - $time; |
508 | |
536 | |
509 | $TICK_START += $time; # do not account sync jobs to server load |
537 | $TICK_START += $time; # do not account sync jobs to server load |
510 | |
538 | |
511 | wantarray ? @res : $res[0] |
539 | wantarray ? @res : $res[0] |
512 | } else { |
540 | } else { |
… | |
… | |
1282 | } |
1310 | } |
1283 | |
1311 | |
1284 | use File::Glob (); |
1312 | use File::Glob (); |
1285 | |
1313 | |
1286 | cf::player->attach ( |
1314 | cf::player->attach ( |
1287 | on_command => sub { |
1315 | on_unknown_command => sub { |
1288 | my ($pl, $name, $params) = @_; |
1316 | my ($pl, $name, $params) = @_; |
1289 | |
1317 | |
1290 | my $cb = $COMMAND{$name} |
1318 | my $cb = $COMMAND{$name} |
1291 | or return; |
1319 | or return; |
1292 | |
1320 | |
… | |
… | |
1371 | . "\n};\n1"; |
1399 | . "\n};\n1"; |
1372 | |
1400 | |
1373 | $todo{$base} = \%ext; |
1401 | $todo{$base} = \%ext; |
1374 | } |
1402 | } |
1375 | |
1403 | |
|
|
1404 | my $pass = 0; |
1376 | my %done; |
1405 | my %done; |
1377 | while (%todo) { |
1406 | while (%todo) { |
1378 | my $progress; |
1407 | my $progress; |
1379 | |
1408 | |
|
|
1409 | ++$pass; |
|
|
1410 | |
|
|
1411 | ext: |
1380 | while (my ($k, $v) = each %todo) { |
1412 | while (my ($k, $v) = each %todo) { |
1381 | for (split /,\s*/, $v->{meta}{depends}) { |
1413 | for (split /,\s*/, $v->{meta}{depends}) { |
1382 | goto skip |
1414 | next ext |
1383 | unless exists $done{$_}; |
1415 | unless exists $done{$_}; |
1384 | } |
1416 | } |
1385 | |
1417 | |
1386 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1418 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1387 | |
1419 | |
1388 | unless (eval $v->{source}) { |
1420 | my $active = eval $v->{source}; |
|
|
1421 | |
|
|
1422 | if (length $@) { |
1389 | my $msg = $@ ? "$v->{path}: $@\n" |
1423 | warn "$v->{path}: $@\n"; |
1390 | : "$v->{base}: extension inactive.\n"; |
|
|
1391 | |
1424 | |
1392 | if (exists $v->{meta}{mandatory}) { |
|
|
1393 | warn $msg; |
|
|
1394 | cf::cleanup "mandatory extension failed to load, exiting."; |
1425 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1395 | } |
1426 | if exists $v->{meta}{mandatory}; |
1396 | |
1427 | } else { |
1397 | warn $msg; |
1428 | $done{$k} = delete $todo{$k}; |
|
|
1429 | push @EXTS, $v->{pkg}; |
|
|
1430 | $progress = 1; |
|
|
1431 | |
|
|
1432 | warn "$v->{base}: extension inactive.\n" |
|
|
1433 | unless $active; |
1398 | } |
1434 | } |
1399 | |
|
|
1400 | $done{$k} = delete $todo{$k}; |
|
|
1401 | push @EXTS, $v->{pkg}; |
|
|
1402 | $progress = 1; |
|
|
1403 | } |
1435 | } |
1404 | |
1436 | |
1405 | skip: |
1437 | unless ($progress) { |
1406 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1438 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1407 | unless $progress; |
1439 | |
|
|
1440 | while (my ($k, $v) = each %todo) { |
|
|
1441 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1442 | if exists $v->{meta}{mandatory}; |
|
|
1443 | } |
|
|
1444 | } |
1408 | } |
1445 | } |
1409 | }; |
1446 | }; |
1410 | } |
1447 | } |
1411 | |
1448 | |
1412 | ############################################################################# |
1449 | ############################################################################# |
… | |
… | |
1496 | $cf::PLAYER{$login} = $pl |
1533 | $cf::PLAYER{$login} = $pl |
1497 | } |
1534 | } |
1498 | } |
1535 | } |
1499 | } |
1536 | } |
1500 | |
1537 | |
|
|
1538 | cf::player->attach ( |
|
|
1539 | on_load => sub { |
|
|
1540 | my ($pl, $path) = @_; |
|
|
1541 | |
|
|
1542 | # restore slots saved in save, below |
|
|
1543 | my $slots = delete $pl->{_slots}; |
|
|
1544 | |
|
|
1545 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1546 | $pl->combat_ob ($slots->[1]); |
|
|
1547 | $pl->ranged_ob ($slots->[2]); |
|
|
1548 | }, |
|
|
1549 | ); |
|
|
1550 | |
1501 | sub save($) { |
1551 | sub save($) { |
1502 | my ($pl) = @_; |
1552 | my ($pl) = @_; |
1503 | |
1553 | |
1504 | return if $pl->{deny_save}; |
1554 | return if $pl->{deny_save}; |
1505 | |
1555 | |
… | |
… | |
1510 | |
1560 | |
1511 | aio_mkdir playerdir $pl, 0770; |
1561 | aio_mkdir playerdir $pl, 0770; |
1512 | $pl->{last_save} = $cf::RUNTIME; |
1562 | $pl->{last_save} = $cf::RUNTIME; |
1513 | |
1563 | |
1514 | cf::get_slot 0.01; |
1564 | cf::get_slot 0.01; |
|
|
1565 | |
|
|
1566 | # save slots, to be restored later |
|
|
1567 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1515 | |
1568 | |
1516 | $pl->save_pl ($path); |
1569 | $pl->save_pl ($path); |
1517 | cf::cede_to_tick; |
1570 | cf::cede_to_tick; |
1518 | } |
1571 | } |
1519 | |
1572 | |
… | |
… | |
1732 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1785 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1733 | |
1786 | |
1734 | # mit "rum" bekleckern, nicht |
1787 | # mit "rum" bekleckern, nicht |
1735 | $self->_create_random_map ( |
1788 | $self->_create_random_map ( |
1736 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1789 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1737 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1790 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1738 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1791 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1739 | $rmp->{exit_on_final_map}, |
1792 | $rmp->{exit_on_final_map}, |
1740 | $rmp->{xsize}, $rmp->{ysize}, |
1793 | $rmp->{xsize}, $rmp->{ysize}, |
1741 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1794 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1742 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1795 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
1988 | |
2041 | |
1989 | $cf::MAP{$path} = $map |
2042 | $cf::MAP{$path} = $map |
1990 | } |
2043 | } |
1991 | } |
2044 | } |
1992 | |
2045 | |
1993 | sub pre_load { } |
2046 | sub pre_load { } |
1994 | sub post_load { } |
2047 | #sub post_load { } # XS |
1995 | |
2048 | |
1996 | sub load { |
2049 | sub load { |
1997 | my ($self) = @_; |
2050 | my ($self) = @_; |
1998 | |
2051 | |
1999 | local $self->{deny_reset} = 1; # loading can take a long time |
2052 | local $self->{deny_reset} = 1; # loading can take a long time |
… | |
… | |
2056 | } |
2109 | } |
2057 | |
2110 | |
2058 | $self->post_load; |
2111 | $self->post_load; |
2059 | } |
2112 | } |
2060 | |
2113 | |
|
|
2114 | # customize the map for a given player, i.e. |
|
|
2115 | # return the _real_ map. used by e.g. per-player |
|
|
2116 | # maps to change the path to ~playername/mappath |
2061 | sub customise_for { |
2117 | sub customise_for { |
2062 | my ($self, $ob) = @_; |
2118 | my ($self, $ob) = @_; |
2063 | |
2119 | |
2064 | return find "~" . $ob->name . "/" . $self->{path} |
2120 | return find "~" . $ob->name . "/" . $self->{path} |
2065 | if $self->per_player; |
2121 | if $self->per_player; |
… | |
… | |
2219 | |
2275 | |
2220 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2276 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2221 | |
2277 | |
2222 | return if $self->players; |
2278 | return if $self->players; |
2223 | |
2279 | |
2224 | warn "resetting map ", $self->path; |
2280 | warn "resetting map ", $self->path, "\n"; |
2225 | |
2281 | |
2226 | $self->in_memory (cf::MAP_SWAPPED); |
2282 | $self->in_memory (cf::MAP_SWAPPED); |
2227 | |
2283 | |
2228 | # need to save uniques path |
2284 | # need to save uniques path |
2229 | unless ($self->{deny_save}) { |
2285 | unless ($self->{deny_save}) { |
… | |
… | |
2320 | : normalise $_ |
2376 | : normalise $_ |
2321 | } @{ aio_readdir $UNIQUEDIR or [] } |
2377 | } @{ aio_readdir $UNIQUEDIR or [] } |
2322 | ] |
2378 | ] |
2323 | } |
2379 | } |
2324 | |
2380 | |
|
|
2381 | =item cf::map::static_maps |
|
|
2382 | |
|
|
2383 | Returns an arrayref if paths of all static maps (all preinstalled F<.map> |
|
|
2384 | file in the shared directory excluding F</styles> and F</editor>). May |
|
|
2385 | block. |
|
|
2386 | |
|
|
2387 | =cut |
|
|
2388 | |
|
|
2389 | sub static_maps() { |
|
|
2390 | my @dirs = ""; |
|
|
2391 | my @maps; |
|
|
2392 | |
|
|
2393 | while (@dirs) { |
|
|
2394 | my $dir = shift @dirs; |
|
|
2395 | |
|
|
2396 | next if $dir eq "/styles" || $dir eq "/editor"; |
|
|
2397 | |
|
|
2398 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
|
|
2399 | or return; |
|
|
2400 | |
|
|
2401 | for (@$files) { |
|
|
2402 | s/\.map$// or next; |
|
|
2403 | utf8::decode $_; |
|
|
2404 | push @maps, "$dir/$_"; |
|
|
2405 | } |
|
|
2406 | |
|
|
2407 | push @dirs, map "$dir/$_", @$dirs; |
|
|
2408 | } |
|
|
2409 | |
|
|
2410 | \@maps |
|
|
2411 | } |
|
|
2412 | |
2325 | =back |
2413 | =back |
2326 | |
2414 | |
2327 | =head3 cf::object |
2415 | =head3 cf::object |
2328 | |
2416 | |
2329 | =cut |
2417 | =cut |
… | |
… | |
2524 | ($x, $y) = (-1, -1) |
2612 | ($x, $y) = (-1, -1) |
2525 | unless (defined $x) && (defined $y); |
2613 | unless (defined $x) && (defined $y); |
2526 | |
2614 | |
2527 | # use -1 or undef as default coordinates, not 0, 0 |
2615 | # use -1 or undef as default coordinates, not 0, 0 |
2528 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2616 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2529 | if $x <=0 && $y <= 0; |
2617 | if $x <= 0 && $y <= 0; |
2530 | |
2618 | |
2531 | $map->load; |
2619 | $map->load; |
2532 | $map->load_neighbours; |
2620 | $map->load_neighbours; |
2533 | |
2621 | |
2534 | return unless $self->contr->active; |
2622 | return unless $self->contr->active; |
2535 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2536 | $self->activate_recursive; |
|
|
2537 | |
2623 | |
2538 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2624 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2539 | $self->enter_map ($map, $x, $y); |
2625 | $self->enter_map ($map, $x, $y); |
|
|
2626 | |
|
|
2627 | # only activate afterwards, to support waiting in hooks |
|
|
2628 | $self->activate_recursive; |
2540 | } |
2629 | } |
2541 | |
2630 | |
2542 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2631 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2543 | |
2632 | |
2544 | Moves the player to the given map-path and coordinates by first freezing |
2633 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2732 | |
2821 | |
2733 | utf8::encode $text; |
2822 | utf8::encode $text; |
2734 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2823 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2735 | } |
2824 | } |
2736 | |
2825 | |
|
|
2826 | =item $client->send_big_packet ($pkt) |
|
|
2827 | |
|
|
2828 | Like C<send_packet>, but tries to compress large packets, and fragments |
|
|
2829 | them as required. |
|
|
2830 | |
|
|
2831 | =cut |
|
|
2832 | |
|
|
2833 | our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64; |
|
|
2834 | |
|
|
2835 | sub cf::client::send_big_packet { |
|
|
2836 | my ($self, $pkt) = @_; |
|
|
2837 | |
|
|
2838 | # try lzf for large packets |
|
|
2839 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2840 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2841 | |
|
|
2842 | # split very large packets |
|
|
2843 | if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) { |
|
|
2844 | $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt; |
|
|
2845 | $pkt = "frag"; |
|
|
2846 | } |
|
|
2847 | |
|
|
2848 | $self->send_packet ($pkt); |
|
|
2849 | } |
|
|
2850 | |
2737 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2851 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2738 | |
2852 | |
2739 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2853 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2740 | client if neccessary. C<$type> should be a string identifying the type of |
2854 | client if neccessary. C<$type> should be a string identifying the type of |
2741 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2855 | the message, with C<log> being the default. If C<$color> is negative, suppress |
… | |
… | |
2743 | |
2857 | |
2744 | =cut |
2858 | =cut |
2745 | |
2859 | |
2746 | # non-persistent channels (usually the info channel) |
2860 | # non-persistent channels (usually the info channel) |
2747 | our %CHANNEL = ( |
2861 | our %CHANNEL = ( |
|
|
2862 | "c/motd" => { |
|
|
2863 | id => "infobox", |
|
|
2864 | title => "MOTD", |
|
|
2865 | reply => undef, |
|
|
2866 | tooltip => "The message of the day", |
|
|
2867 | }, |
2748 | "c/identify" => { |
2868 | "c/identify" => { |
2749 | id => "infobox", |
2869 | id => "infobox", |
2750 | title => "Identify", |
2870 | title => "Identify", |
2751 | reply => undef, |
2871 | reply => undef, |
2752 | tooltip => "Items recently identified", |
2872 | tooltip => "Items recently identified", |
… | |
… | |
2754 | "c/examine" => { |
2874 | "c/examine" => { |
2755 | id => "infobox", |
2875 | id => "infobox", |
2756 | title => "Examine", |
2876 | title => "Examine", |
2757 | reply => undef, |
2877 | reply => undef, |
2758 | tooltip => "Signs and other items you examined", |
2878 | tooltip => "Signs and other items you examined", |
|
|
2879 | }, |
|
|
2880 | "c/shopinfo" => { |
|
|
2881 | id => "infobox", |
|
|
2882 | title => "Shop Info", |
|
|
2883 | reply => undef, |
|
|
2884 | tooltip => "What your bargaining skill tells you about the shop", |
2759 | }, |
2885 | }, |
2760 | "c/book" => { |
2886 | "c/book" => { |
2761 | id => "infobox", |
2887 | id => "infobox", |
2762 | title => "Book", |
2888 | title => "Book", |
2763 | reply => undef, |
2889 | reply => undef, |
… | |
… | |
2879 | my $pkt = "msg " |
3005 | my $pkt = "msg " |
2880 | . $self->{json_coder}->encode ( |
3006 | . $self->{json_coder}->encode ( |
2881 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
3007 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2882 | ); |
3008 | ); |
2883 | |
3009 | |
2884 | # try lzf for large packets |
|
|
2885 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2886 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2887 | |
|
|
2888 | # split very large packets |
|
|
2889 | if (8192 < length $pkt and $self->{can_lzf}) { |
|
|
2890 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
|
|
2891 | $pkt = "frag"; |
|
|
2892 | } |
|
|
2893 | |
|
|
2894 | $self->send_packet ($pkt); |
3010 | $self->send_big_packet ($pkt); |
2895 | } |
3011 | } |
2896 | |
3012 | |
2897 | =item $client->ext_msg ($type, @msg) |
3013 | =item $client->ext_msg ($type, @msg) |
2898 | |
3014 | |
2899 | Sends an ext event to the client. |
3015 | Sends an ext event to the client. |
… | |
… | |
2902 | |
3018 | |
2903 | sub cf::client::ext_msg($$@) { |
3019 | sub cf::client::ext_msg($$@) { |
2904 | my ($self, $type, @msg) = @_; |
3020 | my ($self, $type, @msg) = @_; |
2905 | |
3021 | |
2906 | if ($self->extcmd == 2) { |
3022 | if ($self->extcmd == 2) { |
2907 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
3023 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2908 | } elsif ($self->extcmd == 1) { # TODO: remove |
3024 | } elsif ($self->extcmd == 1) { # TODO: remove |
2909 | push @msg, msgtype => "event_$type"; |
3025 | push @msg, msgtype => "event_$type"; |
2910 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3026 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2911 | } |
3027 | } |
2912 | } |
3028 | } |
2913 | |
3029 | |
2914 | =item $client->ext_reply ($msgid, @msg) |
3030 | =item $client->ext_reply ($msgid, @msg) |
2915 | |
3031 | |
… | |
… | |
2919 | |
3035 | |
2920 | sub cf::client::ext_reply($$@) { |
3036 | sub cf::client::ext_reply($$@) { |
2921 | my ($self, $id, @msg) = @_; |
3037 | my ($self, $id, @msg) = @_; |
2922 | |
3038 | |
2923 | if ($self->extcmd == 2) { |
3039 | if ($self->extcmd == 2) { |
2924 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3040 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2925 | } elsif ($self->extcmd == 1) { |
3041 | } elsif ($self->extcmd == 1) { |
2926 | #TODO: version 1, remove |
3042 | #TODO: version 1, remove |
2927 | unshift @msg, msgtype => "reply", msgid => $id; |
3043 | unshift @msg, msgtype => "reply", msgid => $id; |
2928 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3044 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2929 | } |
3045 | } |
2930 | } |
3046 | } |
2931 | |
3047 | |
2932 | =item $success = $client->query ($flags, "text", \&cb) |
3048 | =item $success = $client->query ($flags, "text", \&cb) |
2933 | |
3049 | |
… | |
… | |
3034 | |
3150 | |
3035 | $coro |
3151 | $coro |
3036 | } |
3152 | } |
3037 | |
3153 | |
3038 | cf::client->attach ( |
3154 | cf::client->attach ( |
3039 | on_destroy => sub { |
3155 | on_client_destroy => sub { |
3040 | my ($ns) = @_; |
3156 | my ($ns) = @_; |
3041 | |
3157 | |
3042 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3158 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3043 | }, |
3159 | }, |
3044 | ); |
3160 | ); |
… | |
… | |
3060 | our $safe_hole = new Safe::Hole; |
3176 | our $safe_hole = new Safe::Hole; |
3061 | |
3177 | |
3062 | $SIG{FPE} = 'IGNORE'; |
3178 | $SIG{FPE} = 'IGNORE'; |
3063 | |
3179 | |
3064 | $safe->permit_only (Opcode::opset qw( |
3180 | $safe->permit_only (Opcode::opset qw( |
3065 | :base_core :base_mem :base_orig :base_math |
3181 | :base_core :base_mem :base_orig :base_math :base_loop |
3066 | grepstart grepwhile mapstart mapwhile |
3182 | grepstart grepwhile mapstart mapwhile |
3067 | sort time |
3183 | sort time |
3068 | )); |
3184 | )); |
3069 | |
3185 | |
3070 | # here we export the classes and methods available to script code |
3186 | # here we export the classes and methods available to script code |
… | |
… | |
3122 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3238 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3123 | $qcode =~ s/\n/\\n/g; |
3239 | $qcode =~ s/\n/\\n/g; |
3124 | |
3240 | |
3125 | %vars = (_dummy => 0) unless %vars; |
3241 | %vars = (_dummy => 0) unless %vars; |
3126 | |
3242 | |
|
|
3243 | my @res; |
3127 | local $_; |
3244 | local $_; |
3128 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
3129 | |
3245 | |
3130 | my $eval = |
3246 | my $eval = |
3131 | "do {\n" |
3247 | "do {\n" |
3132 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3248 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3133 | . "#line 0 \"{$qcode}\"\n" |
3249 | . "#line 0 \"{$qcode}\"\n" |
3134 | . $code |
3250 | . $code |
3135 | . "\n}" |
3251 | . "\n}" |
3136 | ; |
3252 | ; |
3137 | |
3253 | |
|
|
3254 | if ($CFG{safe_eval}) { |
3138 | sub_generation_inc; |
3255 | sub_generation_inc; |
|
|
3256 | local @safe::cf::_safe_eval_args = values %vars; |
3139 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3257 | @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3140 | sub_generation_inc; |
3258 | sub_generation_inc; |
|
|
3259 | } else { |
|
|
3260 | local @cf::_safe_eval_args = values %vars; |
|
|
3261 | @res = wantarray ? eval eval : scalar eval $eval; |
|
|
3262 | } |
3141 | |
3263 | |
3142 | if ($@) { |
3264 | if ($@) { |
3143 | warn "$@"; |
3265 | warn "$@"; |
3144 | warn "while executing safe code '$code'\n"; |
3266 | warn "while executing safe code '$code'\n"; |
3145 | warn "with arguments " . (join " ", %vars) . "\n"; |
3267 | warn "with arguments " . (join " ", %vars) . "\n"; |
… | |
… | |
3164 | =cut |
3286 | =cut |
3165 | |
3287 | |
3166 | sub register_script_function { |
3288 | sub register_script_function { |
3167 | my ($fun, $cb) = @_; |
3289 | my ($fun, $cb) = @_; |
3168 | |
3290 | |
3169 | no strict 'refs'; |
3291 | $fun = "safe::$fun" if $CFG{safe_eval}; |
3170 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
3292 | *$fun = $safe_hole->wrap ($cb); |
3171 | } |
3293 | } |
3172 | |
3294 | |
3173 | =back |
3295 | =back |
3174 | |
3296 | |
3175 | =cut |
3297 | =cut |
… | |
… | |
3196 | |
3318 | |
3197 | $facedata->{version} == 2 |
3319 | $facedata->{version} == 2 |
3198 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3320 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3199 | |
3321 | |
3200 | # patch in the exptable |
3322 | # patch in the exptable |
|
|
3323 | my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]); |
3201 | $facedata->{resource}{"res/exp_table"} = { |
3324 | $facedata->{resource}{"res/exp_table"} = { |
3202 | type => FT_RSRC, |
3325 | type => FT_RSRC, |
3203 | data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), |
3326 | data => $exp_table, |
|
|
3327 | hash => (Digest::MD5::md5 $exp_table), |
3204 | }; |
3328 | }; |
3205 | cf::cede_to_tick; |
3329 | cf::cede_to_tick; |
3206 | |
3330 | |
3207 | { |
3331 | { |
3208 | my $faces = $facedata->{faceinfo}; |
3332 | my $faces = $facedata->{faceinfo}; |
… | |
… | |
3210 | while (my ($face, $info) = each %$faces) { |
3334 | while (my ($face, $info) = each %$faces) { |
3211 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3335 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3212 | |
3336 | |
3213 | cf::face::set_visibility $idx, $info->{visibility}; |
3337 | cf::face::set_visibility $idx, $info->{visibility}; |
3214 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3338 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3215 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3339 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3216 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3340 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3217 | |
3341 | |
3218 | cf::cede_to_tick; |
3342 | cf::cede_to_tick; |
3219 | } |
3343 | } |
3220 | |
3344 | |
3221 | while (my ($face, $info) = each %$faces) { |
3345 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3245 | |
3369 | |
3246 | cf::anim::invalidate_all; # d'oh |
3370 | cf::anim::invalidate_all; # d'oh |
3247 | } |
3371 | } |
3248 | |
3372 | |
3249 | { |
3373 | { |
3250 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3251 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3252 | my $res = $facedata->{resource}; |
3374 | my $res = $facedata->{resource}; |
3253 | |
3375 | |
3254 | while (my ($name, $info) = each %$res) { |
3376 | while (my ($name, $info) = each %$res) { |
3255 | if (defined $info->{type}) { |
3377 | if (defined $info->{type}) { |
3256 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3378 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3257 | my $data; |
|
|
3258 | |
3379 | |
3259 | if ($info->{type} & 1) { |
3380 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3260 | # prepend meta info |
|
|
3261 | |
|
|
3262 | my $meta = $enc->encode ({ |
|
|
3263 | name => $name, |
|
|
3264 | %{ $info->{meta} || {} }, |
|
|
3265 | }); |
|
|
3266 | |
|
|
3267 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3268 | } else { |
|
|
3269 | $data = $info->{data}; |
|
|
3270 | } |
|
|
3271 | |
|
|
3272 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3273 | cf::face::set_type $idx, $info->{type}; |
3381 | cf::face::set_type $idx, $info->{type}; |
3274 | } else { |
3382 | } else { |
3275 | $RESOURCE{$name} = $info; |
3383 | $RESOURCE{$name} = $info; |
3276 | } |
3384 | } |
3277 | |
3385 | |
… | |
… | |
3361 | |
3469 | |
3362 | warn "finished reloading resource files\n"; |
3470 | warn "finished reloading resource files\n"; |
3363 | } |
3471 | } |
3364 | |
3472 | |
3365 | sub reload_config { |
3473 | sub reload_config { |
|
|
3474 | warn "reloading config file...\n"; |
|
|
3475 | |
3366 | open my $fh, "<:utf8", "$CONFDIR/config" |
3476 | open my $fh, "<:utf8", "$CONFDIR/config" |
3367 | or return; |
3477 | or return; |
3368 | |
3478 | |
3369 | local $/; |
3479 | local $/; |
3370 | *CFG = YAML::Load <$fh>; |
3480 | *CFG = YAML::XS::Load scalar <$fh>; |
3371 | |
3481 | |
3372 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3482 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3373 | |
3483 | |
3374 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3484 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3375 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3485 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3379 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3489 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3380 | and die "WARNING: m(un)lockall failed: $!\n"; |
3490 | and die "WARNING: m(un)lockall failed: $!\n"; |
3381 | }; |
3491 | }; |
3382 | warn $@ if $@; |
3492 | warn $@ if $@; |
3383 | } |
3493 | } |
|
|
3494 | |
|
|
3495 | warn "finished reloading resource files\n"; |
3384 | } |
3496 | } |
3385 | |
3497 | |
3386 | sub pidfile() { |
3498 | sub pidfile() { |
3387 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3499 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3388 | or die "$PIDFILE: $!"; |
3500 | or die "$PIDFILE: $!"; |
… | |
… | |
3398 | my $pid = <$fh>; |
3510 | my $pid = <$fh>; |
3399 | kill 9, $pid if $pid > 0; |
3511 | kill 9, $pid if $pid > 0; |
3400 | |
3512 | |
3401 | seek $fh, 0, 0; |
3513 | seek $fh, 0, 0; |
3402 | print $fh $$; |
3514 | print $fh $$; |
|
|
3515 | } |
|
|
3516 | |
|
|
3517 | sub main_loop { |
|
|
3518 | warn "EV::loop starting\n"; |
|
|
3519 | if (1) { |
|
|
3520 | EV::loop; |
|
|
3521 | } |
|
|
3522 | warn "EV::loop returned\n"; |
|
|
3523 | goto &main_loop unless $REALLY_UNLOOP; |
3403 | } |
3524 | } |
3404 | |
3525 | |
3405 | sub main { |
3526 | sub main { |
3406 | cf::init_globals; # initialise logging |
3527 | cf::init_globals; # initialise logging |
3407 | |
3528 | |
… | |
… | |
3427 | }; |
3548 | }; |
3428 | |
3549 | |
3429 | evthread_start IO::AIO::poll_fileno; |
3550 | evthread_start IO::AIO::poll_fileno; |
3430 | |
3551 | |
3431 | cf::sync_job { |
3552 | cf::sync_job { |
|
|
3553 | cf::load_settings; |
|
|
3554 | cf::load_materials; |
|
|
3555 | |
3432 | reload_resources; |
3556 | reload_resources; |
3433 | reload_config; |
3557 | reload_config; |
3434 | db_init; |
3558 | db_init; |
3435 | |
3559 | |
3436 | cf::load_settings; |
|
|
3437 | cf::load_materials; |
|
|
3438 | cf::init_uuid; |
3560 | cf::init_uuid; |
3439 | cf::init_signals; |
3561 | cf::init_signals; |
3440 | cf::init_commands; |
|
|
3441 | cf::init_skills; |
3562 | cf::init_skills; |
3442 | |
3563 | |
3443 | cf::init_beforeplay; |
3564 | cf::init_beforeplay; |
3444 | |
3565 | |
3445 | atomic; |
3566 | atomic; |
… | |
… | |
3447 | load_extensions; |
3568 | load_extensions; |
3448 | |
3569 | |
3449 | utime time, time, $RUNTIMEFILE; |
3570 | utime time, time, $RUNTIMEFILE; |
3450 | |
3571 | |
3451 | # no (long-running) fork's whatsoever before this point(!) |
3572 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3573 | use POSIX (); |
3452 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3574 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3453 | |
3575 | |
3454 | (pop @POST_INIT)->(0) while @POST_INIT; |
3576 | (pop @POST_INIT)->(0) while @POST_INIT; |
3455 | }; |
3577 | }; |
3456 | |
3578 | |
3457 | EV::loop; |
3579 | main_loop; |
3458 | } |
3580 | } |
3459 | |
3581 | |
3460 | ############################################################################# |
3582 | ############################################################################# |
3461 | # initialisation and cleanup |
3583 | # initialisation and cleanup |
3462 | |
3584 | |
3463 | # install some emergency cleanup handlers |
3585 | # install some emergency cleanup handlers |
3464 | BEGIN { |
3586 | BEGIN { |
3465 | our %SIGWATCHER = (); |
3587 | our %SIGWATCHER = (); |
3466 | for my $signal (qw(INT HUP TERM)) { |
3588 | for my $signal (qw(INT HUP TERM)) { |
3467 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3589 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3468 | cf::cleanup "SIG$signal"; |
3590 | cf::cleanup "SIG$signal"; |
3469 | }; |
3591 | }; |
3470 | } |
3592 | } |
3471 | } |
3593 | } |
3472 | |
3594 | |
3473 | sub write_runtime_sync { |
3595 | sub write_runtime_sync { |
|
|
3596 | my $t0 = AE::time; |
|
|
3597 | |
3474 | # first touch the runtime file to show we are still running: |
3598 | # first touch the runtime file to show we are still running: |
3475 | # the fsync below can take a very very long time. |
3599 | # the fsync below can take a very very long time. |
3476 | |
3600 | |
3477 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3601 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3478 | |
3602 | |
3479 | my $guard = cf::lock_acquire "write_runtime"; |
3603 | my $guard = cf::lock_acquire "write_runtime"; |
3480 | |
3604 | |
3481 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3605 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3482 | or return; |
3606 | or return; |
3483 | |
3607 | |
3484 | my $value = $cf::RUNTIME + 90 + 10; |
3608 | my $value = $cf::RUNTIME + 90 + 10; |
3485 | # 10 is the runtime save interval, for a monotonic clock |
3609 | # 10 is the runtime save interval, for a monotonic clock |
3486 | # 60 allows for the watchdog to kill the server. |
3610 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3499 | or return; |
3623 | or return; |
3500 | |
3624 | |
3501 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3625 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3502 | and return; |
3626 | and return; |
3503 | |
3627 | |
3504 | warn "runtime file written.\n"; |
3628 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3505 | |
3629 | |
3506 | 1 |
3630 | 1 |
3507 | } |
3631 | } |
3508 | |
3632 | |
3509 | our $uuid_lock; |
3633 | our $uuid_lock; |
… | |
… | |
3647 | return; |
3771 | return; |
3648 | } |
3772 | } |
3649 | |
3773 | |
3650 | return if $RELOAD++; |
3774 | return if $RELOAD++; |
3651 | |
3775 | |
3652 | my $t1 = EV::time; |
3776 | my $t1 = AE::time; |
3653 | |
3777 | |
3654 | while ($RELOAD) { |
3778 | while ($RELOAD) { |
3655 | warn "reloading..."; |
3779 | warn "reloading..."; |
3656 | |
3780 | |
3657 | warn "entering sync_job"; |
3781 | warn "entering sync_job"; |
… | |
… | |
3728 | |
3852 | |
3729 | warn "unload completed, starting to reload now"; |
3853 | warn "unload completed, starting to reload now"; |
3730 | |
3854 | |
3731 | warn "reloading cf.pm"; |
3855 | warn "reloading cf.pm"; |
3732 | require cf; |
3856 | require cf; |
3733 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3857 | cf::_connect_to_perl_1; |
3734 | |
3858 | |
3735 | warn "loading config and database again"; |
3859 | warn "loading config and database again"; |
3736 | cf::reload_config; |
3860 | cf::reload_config; |
3737 | |
3861 | |
3738 | warn "loading extensions"; |
3862 | warn "loading extensions"; |
… | |
… | |
3760 | |
3884 | |
3761 | warn "reloaded"; |
3885 | warn "reloaded"; |
3762 | --$RELOAD; |
3886 | --$RELOAD; |
3763 | } |
3887 | } |
3764 | |
3888 | |
3765 | $t1 = EV::time - $t1; |
3889 | $t1 = AE::time - $t1; |
3766 | warn "reload completed in ${t1}s\n"; |
3890 | warn "reload completed in ${t1}s\n"; |
3767 | }; |
3891 | }; |
3768 | |
3892 | |
3769 | our $RELOAD_WATCHER; # used only during reload |
3893 | our $RELOAD_WATCHER; # used only during reload |
3770 | |
3894 | |
… | |
… | |
3773 | # coro crashes during coro_state_free->destroy here. |
3897 | # coro crashes during coro_state_free->destroy here. |
3774 | |
3898 | |
3775 | $RELOAD_WATCHER ||= cf::async { |
3899 | $RELOAD_WATCHER ||= cf::async { |
3776 | Coro::AIO::aio_wait cache_extensions; |
3900 | Coro::AIO::aio_wait cache_extensions; |
3777 | |
3901 | |
3778 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3902 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3779 | do_reload_perl; |
3903 | do_reload_perl; |
3780 | undef $RELOAD_WATCHER; |
3904 | undef $RELOAD_WATCHER; |
3781 | }; |
3905 | }; |
3782 | }; |
3906 | }; |
3783 | } |
3907 | } |
… | |
… | |
3800 | |
3924 | |
3801 | our @WAIT_FOR_TICK; |
3925 | our @WAIT_FOR_TICK; |
3802 | our @WAIT_FOR_TICK_BEGIN; |
3926 | our @WAIT_FOR_TICK_BEGIN; |
3803 | |
3927 | |
3804 | sub wait_for_tick { |
3928 | sub wait_for_tick { |
3805 | return if tick_inhibit || $Coro::current == $Coro::main; |
3929 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3806 | |
3930 | |
3807 | my $signal = new Coro::Signal; |
3931 | my $signal = new Coro::Signal; |
3808 | push @WAIT_FOR_TICK, $signal; |
3932 | push @WAIT_FOR_TICK, $signal; |
3809 | $signal->wait; |
3933 | $signal->wait; |
3810 | } |
3934 | } |
3811 | |
3935 | |
3812 | sub wait_for_tick_begin { |
3936 | sub wait_for_tick_begin { |
3813 | return if tick_inhibit || $Coro::current == $Coro::main; |
3937 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3814 | |
3938 | |
3815 | my $signal = new Coro::Signal; |
3939 | my $signal = new Coro::Signal; |
3816 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3940 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3817 | $signal->wait; |
3941 | $signal->wait; |
3818 | } |
3942 | } |
… | |
… | |
3823 | unless ++$bug_warning > 10; |
3947 | unless ++$bug_warning > 10; |
3824 | return; |
3948 | return; |
3825 | } |
3949 | } |
3826 | |
3950 | |
3827 | cf::server_tick; # one server iteration |
3951 | cf::server_tick; # one server iteration |
|
|
3952 | |
|
|
3953 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3828 | |
3954 | |
3829 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3955 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3830 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3956 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3831 | Coro::async_pool { |
3957 | Coro::async_pool { |
3832 | $Coro::current->{desc} = "runtime saver"; |
3958 | $Coro::current->{desc} = "runtime saver"; |
… | |
… | |
3855 | } |
3981 | } |
3856 | |
3982 | |
3857 | { |
3983 | { |
3858 | # configure BDB |
3984 | # configure BDB |
3859 | |
3985 | |
3860 | BDB::min_parallel 8; |
3986 | BDB::min_parallel 16; |
3861 | BDB::max_poll_reqs $TICK * 0.1; |
3987 | BDB::max_poll_reqs $TICK * 0.1; |
3862 | $AnyEvent::BDB::WATCHER->priority (1); |
3988 | $AnyEvent::BDB::WATCHER->priority (1); |
3863 | |
3989 | |
3864 | unless ($DB_ENV) { |
3990 | unless ($DB_ENV) { |
3865 | $DB_ENV = BDB::db_env_create; |
3991 | $DB_ENV = BDB::db_env_create; |
… | |
… | |
3946 | } |
4072 | } |
3947 | } |
4073 | } |
3948 | |
4074 | |
3949 | # load additional modules |
4075 | # load additional modules |
3950 | require "cf/$_.pm" for @EXTRA_MODULES; |
4076 | require "cf/$_.pm" for @EXTRA_MODULES; |
|
|
4077 | cf::_connect_to_perl_2; |
3951 | |
4078 | |
3952 | END { cf::emergency_save } |
4079 | END { cf::emergency_save } |
3953 | |
4080 | |
3954 | 1 |
4081 | 1 |
3955 | |
4082 | |