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; |
… | |
… | |
1151 | $decname, length $$rdata, scalar @$objs; |
1179 | $decname, length $$rdata, scalar @$objs; |
1152 | |
1180 | |
1153 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1181 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1154 | aio_chmod $fh, SAVE_MODE; |
1182 | aio_chmod $fh, SAVE_MODE; |
1155 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1183 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1156 | aio_fsync $fh if $cf::USE_FSYNC; |
1184 | if ($cf::USE_FSYNC) { |
|
|
1185 | 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; |
|
|
1186 | aio_fsync $fh; |
|
|
1187 | } |
1157 | aio_close $fh; |
1188 | aio_close $fh; |
1158 | |
1189 | |
1159 | if (@$objs) { |
1190 | if (@$objs) { |
1160 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1191 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1161 | aio_chmod $fh, SAVE_MODE; |
1192 | aio_chmod $fh, SAVE_MODE; |
1162 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1193 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1163 | aio_write $fh, 0, (length $data), $data, 0; |
1194 | aio_write $fh, 0, (length $data), $data, 0; |
1164 | aio_fsync $fh if $cf::USE_FSYNC; |
1195 | if ($cf::USE_FSYNC) { |
|
|
1196 | 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; |
|
|
1197 | aio_fsync $fh; |
|
|
1198 | } |
1165 | aio_close $fh; |
1199 | aio_close $fh; |
1166 | aio_rename "$filename.pst~", "$filename.pst"; |
1200 | aio_rename "$filename.pst~", "$filename.pst"; |
1167 | } |
1201 | } |
1168 | } else { |
1202 | } else { |
1169 | aio_unlink "$filename.pst"; |
1203 | aio_unlink "$filename.pst"; |
… | |
… | |
1276 | } |
1310 | } |
1277 | |
1311 | |
1278 | use File::Glob (); |
1312 | use File::Glob (); |
1279 | |
1313 | |
1280 | cf::player->attach ( |
1314 | cf::player->attach ( |
1281 | on_command => sub { |
1315 | on_unknown_command => sub { |
1282 | my ($pl, $name, $params) = @_; |
1316 | my ($pl, $name, $params) = @_; |
1283 | |
1317 | |
1284 | my $cb = $COMMAND{$name} |
1318 | my $cb = $COMMAND{$name} |
1285 | or return; |
1319 | or return; |
1286 | |
1320 | |
… | |
… | |
1320 | |
1354 | |
1321 | # "readahead" all extensions |
1355 | # "readahead" all extensions |
1322 | sub cache_extensions { |
1356 | sub cache_extensions { |
1323 | my $grp = IO::AIO::aio_group; |
1357 | my $grp = IO::AIO::aio_group; |
1324 | |
1358 | |
1325 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1359 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1326 | for (grep /\.ext$/, @{$_[0]}) { |
1360 | for (grep /\.ext$/, @{$_[0]}) { |
1327 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1361 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1328 | } |
1362 | } |
1329 | }; |
1363 | }; |
1330 | |
1364 | |
… | |
… | |
1365 | . "\n};\n1"; |
1399 | . "\n};\n1"; |
1366 | |
1400 | |
1367 | $todo{$base} = \%ext; |
1401 | $todo{$base} = \%ext; |
1368 | } |
1402 | } |
1369 | |
1403 | |
|
|
1404 | my $pass = 0; |
1370 | my %done; |
1405 | my %done; |
1371 | while (%todo) { |
1406 | while (%todo) { |
1372 | my $progress; |
1407 | my $progress; |
1373 | |
1408 | |
|
|
1409 | ++$pass; |
|
|
1410 | |
|
|
1411 | ext: |
1374 | while (my ($k, $v) = each %todo) { |
1412 | while (my ($k, $v) = each %todo) { |
1375 | for (split /,\s*/, $v->{meta}{depends}) { |
1413 | for (split /,\s*/, $v->{meta}{depends}) { |
1376 | goto skip |
1414 | next ext |
1377 | unless exists $done{$_}; |
1415 | unless exists $done{$_}; |
1378 | } |
1416 | } |
1379 | |
1417 | |
1380 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1418 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1381 | |
1419 | |
1382 | unless (eval $v->{source}) { |
1420 | my $active = eval $v->{source}; |
|
|
1421 | |
|
|
1422 | if (length $@) { |
1383 | my $msg = $@ ? "$v->{path}: $@\n" |
1423 | warn "$v->{path}: $@\n"; |
1384 | : "$v->{base}: extension inactive.\n"; |
|
|
1385 | |
1424 | |
1386 | if (exists $v->{meta}{mandatory}) { |
|
|
1387 | warn $msg; |
|
|
1388 | cf::cleanup "mandatory extension failed to load, exiting."; |
1425 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1389 | } |
1426 | if exists $v->{meta}{mandatory}; |
1390 | |
1427 | } else { |
1391 | 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; |
1392 | } |
1434 | } |
1393 | |
|
|
1394 | $done{$k} = delete $todo{$k}; |
|
|
1395 | push @EXTS, $v->{pkg}; |
|
|
1396 | $progress = 1; |
|
|
1397 | } |
1435 | } |
1398 | |
1436 | |
1399 | skip: |
1437 | unless ($progress) { |
1400 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1438 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1401 | 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 | } |
1402 | } |
1445 | } |
1403 | }; |
1446 | }; |
1404 | } |
1447 | } |
1405 | |
1448 | |
1406 | ############################################################################# |
1449 | ############################################################################# |
… | |
… | |
1726 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1769 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1727 | |
1770 | |
1728 | # mit "rum" bekleckern, nicht |
1771 | # mit "rum" bekleckern, nicht |
1729 | $self->_create_random_map ( |
1772 | $self->_create_random_map ( |
1730 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1773 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1731 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1774 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1732 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1775 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1733 | $rmp->{exit_on_final_map}, |
1776 | $rmp->{exit_on_final_map}, |
1734 | $rmp->{xsize}, $rmp->{ysize}, |
1777 | $rmp->{xsize}, $rmp->{ysize}, |
1735 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1778 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1736 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1779 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
1982 | |
2025 | |
1983 | $cf::MAP{$path} = $map |
2026 | $cf::MAP{$path} = $map |
1984 | } |
2027 | } |
1985 | } |
2028 | } |
1986 | |
2029 | |
1987 | sub pre_load { } |
2030 | sub pre_load { } |
1988 | sub post_load { } |
2031 | #sub post_load { } # XS |
1989 | |
2032 | |
1990 | sub load { |
2033 | sub load { |
1991 | my ($self) = @_; |
2034 | my ($self) = @_; |
1992 | |
2035 | |
1993 | local $self->{deny_reset} = 1; # loading can take a long time |
2036 | local $self->{deny_reset} = 1; # loading can take a long time |
… | |
… | |
2050 | } |
2093 | } |
2051 | |
2094 | |
2052 | $self->post_load; |
2095 | $self->post_load; |
2053 | } |
2096 | } |
2054 | |
2097 | |
|
|
2098 | # customize the map for a given player, i.e. |
|
|
2099 | # return the _real_ map. used by e.g. per-player |
|
|
2100 | # maps to change the path to ~playername/mappath |
2055 | sub customise_for { |
2101 | sub customise_for { |
2056 | my ($self, $ob) = @_; |
2102 | my ($self, $ob) = @_; |
2057 | |
2103 | |
2058 | return find "~" . $ob->name . "/" . $self->{path} |
2104 | return find "~" . $ob->name . "/" . $self->{path} |
2059 | if $self->per_player; |
2105 | if $self->per_player; |
… | |
… | |
2213 | |
2259 | |
2214 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2260 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2215 | |
2261 | |
2216 | return if $self->players; |
2262 | return if $self->players; |
2217 | |
2263 | |
2218 | warn "resetting map ", $self->path; |
2264 | warn "resetting map ", $self->path, "\n"; |
2219 | |
2265 | |
2220 | $self->in_memory (cf::MAP_SWAPPED); |
2266 | $self->in_memory (cf::MAP_SWAPPED); |
2221 | |
2267 | |
2222 | # need to save uniques path |
2268 | # need to save uniques path |
2223 | unless ($self->{deny_save}) { |
2269 | unless ($self->{deny_save}) { |
… | |
… | |
2314 | : normalise $_ |
2360 | : normalise $_ |
2315 | } @{ aio_readdir $UNIQUEDIR or [] } |
2361 | } @{ aio_readdir $UNIQUEDIR or [] } |
2316 | ] |
2362 | ] |
2317 | } |
2363 | } |
2318 | |
2364 | |
|
|
2365 | =item cf::map::static_maps |
|
|
2366 | |
|
|
2367 | Returns an arrayref if paths of all static maps (all preinstalled F<.map> |
|
|
2368 | file in the shared directory excluding F</styles> and F</editor>). May |
|
|
2369 | block. |
|
|
2370 | |
|
|
2371 | =cut |
|
|
2372 | |
|
|
2373 | sub static_maps() { |
|
|
2374 | my @dirs = ""; |
|
|
2375 | my @maps; |
|
|
2376 | |
|
|
2377 | while (@dirs) { |
|
|
2378 | my $dir = shift @dirs; |
|
|
2379 | |
|
|
2380 | next if $dir eq "/styles" || $dir eq "/editor"; |
|
|
2381 | |
|
|
2382 | my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 |
|
|
2383 | or return; |
|
|
2384 | |
|
|
2385 | for (@$files) { |
|
|
2386 | s/\.map$// or next; |
|
|
2387 | utf8::decode $_; |
|
|
2388 | push @maps, "$dir/$_"; |
|
|
2389 | } |
|
|
2390 | |
|
|
2391 | push @dirs, map "$dir/$_", @$dirs; |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | \@maps |
|
|
2395 | } |
|
|
2396 | |
2319 | =back |
2397 | =back |
2320 | |
2398 | |
2321 | =head3 cf::object |
2399 | =head3 cf::object |
2322 | |
2400 | |
2323 | =cut |
2401 | =cut |
… | |
… | |
2518 | ($x, $y) = (-1, -1) |
2596 | ($x, $y) = (-1, -1) |
2519 | unless (defined $x) && (defined $y); |
2597 | unless (defined $x) && (defined $y); |
2520 | |
2598 | |
2521 | # use -1 or undef as default coordinates, not 0, 0 |
2599 | # use -1 or undef as default coordinates, not 0, 0 |
2522 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2600 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2523 | if $x <=0 && $y <= 0; |
2601 | if $x <= 0 && $y <= 0; |
2524 | |
2602 | |
2525 | $map->load; |
2603 | $map->load; |
2526 | $map->load_neighbours; |
2604 | $map->load_neighbours; |
2527 | |
2605 | |
2528 | return unless $self->contr->active; |
2606 | return unless $self->contr->active; |
2529 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2530 | $self->activate_recursive; |
|
|
2531 | |
2607 | |
2532 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2608 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2533 | $self->enter_map ($map, $x, $y); |
2609 | $self->enter_map ($map, $x, $y); |
|
|
2610 | |
|
|
2611 | # only activate afterwards, to support waiting in hooks |
|
|
2612 | $self->activate_recursive; |
2534 | } |
2613 | } |
2535 | |
2614 | |
2536 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2615 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2537 | |
2616 | |
2538 | Moves the player to the given map-path and coordinates by first freezing |
2617 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2726 | |
2805 | |
2727 | utf8::encode $text; |
2806 | utf8::encode $text; |
2728 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2807 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2729 | } |
2808 | } |
2730 | |
2809 | |
|
|
2810 | =item $client->send_big_packet ($pkt) |
|
|
2811 | |
|
|
2812 | Like C<send_packet>, but tries to compress large packets, and fragments |
|
|
2813 | them as required. |
|
|
2814 | |
|
|
2815 | =cut |
|
|
2816 | |
|
|
2817 | our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64; |
|
|
2818 | |
|
|
2819 | sub cf::client::send_big_packet { |
|
|
2820 | my ($self, $pkt) = @_; |
|
|
2821 | |
|
|
2822 | # try lzf for large packets |
|
|
2823 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2824 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2825 | |
|
|
2826 | # split very large packets |
|
|
2827 | if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) { |
|
|
2828 | $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt; |
|
|
2829 | $pkt = "frag"; |
|
|
2830 | } |
|
|
2831 | |
|
|
2832 | $self->send_packet ($pkt); |
|
|
2833 | } |
|
|
2834 | |
2731 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2835 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2732 | |
2836 | |
2733 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2837 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2734 | client if neccessary. C<$type> should be a string identifying the type of |
2838 | client if neccessary. C<$type> should be a string identifying the type of |
2735 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2839 | the message, with C<log> being the default. If C<$color> is negative, suppress |
… | |
… | |
2737 | |
2841 | |
2738 | =cut |
2842 | =cut |
2739 | |
2843 | |
2740 | # non-persistent channels (usually the info channel) |
2844 | # non-persistent channels (usually the info channel) |
2741 | our %CHANNEL = ( |
2845 | our %CHANNEL = ( |
|
|
2846 | "c/motd" => { |
|
|
2847 | id => "infobox", |
|
|
2848 | title => "MOTD", |
|
|
2849 | reply => undef, |
|
|
2850 | tooltip => "The message of the day", |
|
|
2851 | }, |
2742 | "c/identify" => { |
2852 | "c/identify" => { |
2743 | id => "infobox", |
2853 | id => "infobox", |
2744 | title => "Identify", |
2854 | title => "Identify", |
2745 | reply => undef, |
2855 | reply => undef, |
2746 | tooltip => "Items recently identified", |
2856 | tooltip => "Items recently identified", |
… | |
… | |
2748 | "c/examine" => { |
2858 | "c/examine" => { |
2749 | id => "infobox", |
2859 | id => "infobox", |
2750 | title => "Examine", |
2860 | title => "Examine", |
2751 | reply => undef, |
2861 | reply => undef, |
2752 | tooltip => "Signs and other items you examined", |
2862 | tooltip => "Signs and other items you examined", |
|
|
2863 | }, |
|
|
2864 | "c/shopinfo" => { |
|
|
2865 | id => "infobox", |
|
|
2866 | title => "Shop Info", |
|
|
2867 | reply => undef, |
|
|
2868 | tooltip => "What your bargaining skill tells you about the shop", |
2753 | }, |
2869 | }, |
2754 | "c/book" => { |
2870 | "c/book" => { |
2755 | id => "infobox", |
2871 | id => "infobox", |
2756 | title => "Book", |
2872 | title => "Book", |
2757 | reply => undef, |
2873 | reply => undef, |
… | |
… | |
2785 | id => "infobox", |
2901 | id => "infobox", |
2786 | title => "Skills", |
2902 | title => "Skills", |
2787 | reply => undef, |
2903 | reply => undef, |
2788 | tooltip => "Shows your experience per skill and item power", |
2904 | tooltip => "Shows your experience per skill and item power", |
2789 | }, |
2905 | }, |
|
|
2906 | "c/shopitems" => { |
|
|
2907 | id => "infobox", |
|
|
2908 | title => "Shop Items", |
|
|
2909 | reply => undef, |
|
|
2910 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2911 | }, |
2790 | "c/resistances" => { |
2912 | "c/resistances" => { |
2791 | id => "infobox", |
2913 | id => "infobox", |
2792 | title => "Resistances", |
2914 | title => "Resistances", |
2793 | reply => undef, |
2915 | reply => undef, |
2794 | tooltip => "Shows your resistances", |
2916 | tooltip => "Shows your resistances", |
… | |
… | |
2796 | "c/pets" => { |
2918 | "c/pets" => { |
2797 | id => "infobox", |
2919 | id => "infobox", |
2798 | title => "Pets", |
2920 | title => "Pets", |
2799 | reply => undef, |
2921 | reply => undef, |
2800 | tooltip => "Shows information abotu your pets/a specific pet", |
2922 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2923 | }, |
|
|
2924 | "c/perceiveself" => { |
|
|
2925 | id => "infobox", |
|
|
2926 | title => "Perceive Self", |
|
|
2927 | reply => undef, |
|
|
2928 | tooltip => "You gained detailed knowledge about yourself", |
2801 | }, |
2929 | }, |
2802 | "c/uptime" => { |
2930 | "c/uptime" => { |
2803 | id => "infobox", |
2931 | id => "infobox", |
2804 | title => "Uptime", |
2932 | title => "Uptime", |
2805 | reply => undef, |
2933 | reply => undef, |
… | |
… | |
2861 | my $pkt = "msg " |
2989 | my $pkt = "msg " |
2862 | . $self->{json_coder}->encode ( |
2990 | . $self->{json_coder}->encode ( |
2863 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2991 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2864 | ); |
2992 | ); |
2865 | |
2993 | |
2866 | # try lzf for large packets |
|
|
2867 | $pkt = "lzf " . Compress::LZF::compress $pkt |
|
|
2868 | if 1024 <= length $pkt and $self->{can_lzf}; |
|
|
2869 | |
|
|
2870 | # split very large packets |
|
|
2871 | if (8192 < length $pkt and $self->{can_lzf}) { |
|
|
2872 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
|
|
2873 | $pkt = "frag"; |
|
|
2874 | } |
|
|
2875 | |
|
|
2876 | $self->send_packet ($pkt); |
2994 | $self->send_big_packet ($pkt); |
2877 | } |
2995 | } |
2878 | |
2996 | |
2879 | =item $client->ext_msg ($type, @msg) |
2997 | =item $client->ext_msg ($type, @msg) |
2880 | |
2998 | |
2881 | Sends an ext event to the client. |
2999 | Sends an ext event to the client. |
… | |
… | |
2884 | |
3002 | |
2885 | sub cf::client::ext_msg($$@) { |
3003 | sub cf::client::ext_msg($$@) { |
2886 | my ($self, $type, @msg) = @_; |
3004 | my ($self, $type, @msg) = @_; |
2887 | |
3005 | |
2888 | if ($self->extcmd == 2) { |
3006 | if ($self->extcmd == 2) { |
2889 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
3007 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
2890 | } elsif ($self->extcmd == 1) { # TODO: remove |
3008 | } elsif ($self->extcmd == 1) { # TODO: remove |
2891 | push @msg, msgtype => "event_$type"; |
3009 | push @msg, msgtype => "event_$type"; |
2892 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3010 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2893 | } |
3011 | } |
2894 | } |
3012 | } |
2895 | |
3013 | |
2896 | =item $client->ext_reply ($msgid, @msg) |
3014 | =item $client->ext_reply ($msgid, @msg) |
2897 | |
3015 | |
… | |
… | |
2901 | |
3019 | |
2902 | sub cf::client::ext_reply($$@) { |
3020 | sub cf::client::ext_reply($$@) { |
2903 | my ($self, $id, @msg) = @_; |
3021 | my ($self, $id, @msg) = @_; |
2904 | |
3022 | |
2905 | if ($self->extcmd == 2) { |
3023 | if ($self->extcmd == 2) { |
2906 | $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3024 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
2907 | } elsif ($self->extcmd == 1) { |
3025 | } elsif ($self->extcmd == 1) { |
2908 | #TODO: version 1, remove |
3026 | #TODO: version 1, remove |
2909 | unshift @msg, msgtype => "reply", msgid => $id; |
3027 | unshift @msg, msgtype => "reply", msgid => $id; |
2910 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
3028 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
2911 | } |
3029 | } |
2912 | } |
3030 | } |
2913 | |
3031 | |
2914 | =item $success = $client->query ($flags, "text", \&cb) |
3032 | =item $success = $client->query ($flags, "text", \&cb) |
2915 | |
3033 | |
… | |
… | |
3016 | |
3134 | |
3017 | $coro |
3135 | $coro |
3018 | } |
3136 | } |
3019 | |
3137 | |
3020 | cf::client->attach ( |
3138 | cf::client->attach ( |
3021 | on_destroy => sub { |
3139 | on_client_destroy => sub { |
3022 | my ($ns) = @_; |
3140 | my ($ns) = @_; |
3023 | |
3141 | |
3024 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3142 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3025 | }, |
3143 | }, |
3026 | ); |
3144 | ); |
… | |
… | |
3042 | our $safe_hole = new Safe::Hole; |
3160 | our $safe_hole = new Safe::Hole; |
3043 | |
3161 | |
3044 | $SIG{FPE} = 'IGNORE'; |
3162 | $SIG{FPE} = 'IGNORE'; |
3045 | |
3163 | |
3046 | $safe->permit_only (Opcode::opset qw( |
3164 | $safe->permit_only (Opcode::opset qw( |
3047 | :base_core :base_mem :base_orig :base_math |
3165 | :base_core :base_mem :base_orig :base_math :base_loop |
3048 | grepstart grepwhile mapstart mapwhile |
3166 | grepstart grepwhile mapstart mapwhile |
3049 | sort time |
3167 | sort time |
3050 | )); |
3168 | )); |
3051 | |
3169 | |
3052 | # here we export the classes and methods available to script code |
3170 | # here we export the classes and methods available to script code |
… | |
… | |
3104 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3222 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3105 | $qcode =~ s/\n/\\n/g; |
3223 | $qcode =~ s/\n/\\n/g; |
3106 | |
3224 | |
3107 | %vars = (_dummy => 0) unless %vars; |
3225 | %vars = (_dummy => 0) unless %vars; |
3108 | |
3226 | |
|
|
3227 | my @res; |
3109 | local $_; |
3228 | local $_; |
3110 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
3111 | |
3229 | |
3112 | my $eval = |
3230 | my $eval = |
3113 | "do {\n" |
3231 | "do {\n" |
3114 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3232 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3115 | . "#line 0 \"{$qcode}\"\n" |
3233 | . "#line 0 \"{$qcode}\"\n" |
3116 | . $code |
3234 | . $code |
3117 | . "\n}" |
3235 | . "\n}" |
3118 | ; |
3236 | ; |
3119 | |
3237 | |
|
|
3238 | if ($CFG{safe_eval}) { |
3120 | sub_generation_inc; |
3239 | sub_generation_inc; |
|
|
3240 | local @safe::cf::_safe_eval_args = values %vars; |
3121 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3241 | @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3122 | sub_generation_inc; |
3242 | sub_generation_inc; |
|
|
3243 | } else { |
|
|
3244 | local @cf::_safe_eval_args = values %vars; |
|
|
3245 | @res = wantarray ? eval eval : scalar eval $eval; |
|
|
3246 | } |
3123 | |
3247 | |
3124 | if ($@) { |
3248 | if ($@) { |
3125 | warn "$@"; |
3249 | warn "$@"; |
3126 | warn "while executing safe code '$code'\n"; |
3250 | warn "while executing safe code '$code'\n"; |
3127 | warn "with arguments " . (join " ", %vars) . "\n"; |
3251 | warn "with arguments " . (join " ", %vars) . "\n"; |
… | |
… | |
3146 | =cut |
3270 | =cut |
3147 | |
3271 | |
3148 | sub register_script_function { |
3272 | sub register_script_function { |
3149 | my ($fun, $cb) = @_; |
3273 | my ($fun, $cb) = @_; |
3150 | |
3274 | |
3151 | no strict 'refs'; |
3275 | $fun = "safe::$fun" if $CFG{safe_eval}; |
3152 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
3276 | *$fun = $safe_hole->wrap ($cb); |
3153 | } |
3277 | } |
3154 | |
3278 | |
3155 | =back |
3279 | =back |
3156 | |
3280 | |
3157 | =cut |
3281 | =cut |
… | |
… | |
3178 | |
3302 | |
3179 | $facedata->{version} == 2 |
3303 | $facedata->{version} == 2 |
3180 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3304 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3181 | |
3305 | |
3182 | # patch in the exptable |
3306 | # patch in the exptable |
|
|
3307 | my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]); |
3183 | $facedata->{resource}{"res/exp_table"} = { |
3308 | $facedata->{resource}{"res/exp_table"} = { |
3184 | type => FT_RSRC, |
3309 | type => FT_RSRC, |
3185 | data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), |
3310 | data => $exp_table, |
|
|
3311 | hash => (Digest::MD5::md5 $exp_table), |
3186 | }; |
3312 | }; |
3187 | cf::cede_to_tick; |
3313 | cf::cede_to_tick; |
3188 | |
3314 | |
3189 | { |
3315 | { |
3190 | my $faces = $facedata->{faceinfo}; |
3316 | my $faces = $facedata->{faceinfo}; |
… | |
… | |
3192 | while (my ($face, $info) = each %$faces) { |
3318 | while (my ($face, $info) = each %$faces) { |
3193 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3319 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3194 | |
3320 | |
3195 | cf::face::set_visibility $idx, $info->{visibility}; |
3321 | cf::face::set_visibility $idx, $info->{visibility}; |
3196 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3322 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3197 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3323 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3198 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3324 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3199 | |
3325 | |
3200 | cf::cede_to_tick; |
3326 | cf::cede_to_tick; |
3201 | } |
3327 | } |
3202 | |
3328 | |
3203 | while (my ($face, $info) = each %$faces) { |
3329 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3227 | |
3353 | |
3228 | cf::anim::invalidate_all; # d'oh |
3354 | cf::anim::invalidate_all; # d'oh |
3229 | } |
3355 | } |
3230 | |
3356 | |
3231 | { |
3357 | { |
3232 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3233 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3234 | my $res = $facedata->{resource}; |
3358 | my $res = $facedata->{resource}; |
3235 | |
3359 | |
3236 | while (my ($name, $info) = each %$res) { |
3360 | while (my ($name, $info) = each %$res) { |
3237 | if (defined $info->{type}) { |
3361 | if (defined $info->{type}) { |
3238 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3362 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3239 | my $data; |
|
|
3240 | |
3363 | |
3241 | if ($info->{type} & 1) { |
3364 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3242 | # prepend meta info |
|
|
3243 | |
|
|
3244 | my $meta = $enc->encode ({ |
|
|
3245 | name => $name, |
|
|
3246 | %{ $info->{meta} || {} }, |
|
|
3247 | }); |
|
|
3248 | |
|
|
3249 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3250 | } else { |
|
|
3251 | $data = $info->{data}; |
|
|
3252 | } |
|
|
3253 | |
|
|
3254 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3255 | cf::face::set_type $idx, $info->{type}; |
3365 | cf::face::set_type $idx, $info->{type}; |
3256 | } else { |
3366 | } else { |
3257 | $RESOURCE{$name} = $info; |
3367 | $RESOURCE{$name} = $info; |
3258 | } |
3368 | } |
3259 | |
3369 | |
… | |
… | |
3343 | |
3453 | |
3344 | warn "finished reloading resource files\n"; |
3454 | warn "finished reloading resource files\n"; |
3345 | } |
3455 | } |
3346 | |
3456 | |
3347 | sub reload_config { |
3457 | sub reload_config { |
|
|
3458 | warn "reloading config file...\n"; |
|
|
3459 | |
3348 | open my $fh, "<:utf8", "$CONFDIR/config" |
3460 | open my $fh, "<:utf8", "$CONFDIR/config" |
3349 | or return; |
3461 | or return; |
3350 | |
3462 | |
3351 | local $/; |
3463 | local $/; |
3352 | *CFG = YAML::Load <$fh>; |
3464 | *CFG = YAML::XS::Load scalar <$fh>; |
3353 | |
3465 | |
3354 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3466 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3355 | |
3467 | |
3356 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3468 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3357 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3469 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3361 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3473 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3362 | and die "WARNING: m(un)lockall failed: $!\n"; |
3474 | and die "WARNING: m(un)lockall failed: $!\n"; |
3363 | }; |
3475 | }; |
3364 | warn $@ if $@; |
3476 | warn $@ if $@; |
3365 | } |
3477 | } |
|
|
3478 | |
|
|
3479 | warn "finished reloading resource files\n"; |
3366 | } |
3480 | } |
3367 | |
3481 | |
3368 | sub pidfile() { |
3482 | sub pidfile() { |
3369 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3483 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3370 | or die "$PIDFILE: $!"; |
3484 | or die "$PIDFILE: $!"; |
… | |
… | |
3380 | my $pid = <$fh>; |
3494 | my $pid = <$fh>; |
3381 | kill 9, $pid if $pid > 0; |
3495 | kill 9, $pid if $pid > 0; |
3382 | |
3496 | |
3383 | seek $fh, 0, 0; |
3497 | seek $fh, 0, 0; |
3384 | print $fh $$; |
3498 | print $fh $$; |
|
|
3499 | } |
|
|
3500 | |
|
|
3501 | sub main_loop { |
|
|
3502 | warn "EV::loop starting\n"; |
|
|
3503 | if (1) { |
|
|
3504 | EV::loop; |
|
|
3505 | } |
|
|
3506 | warn "EV::loop returned\n"; |
|
|
3507 | goto &main_loop unless $REALLY_UNLOOP; |
3385 | } |
3508 | } |
3386 | |
3509 | |
3387 | sub main { |
3510 | sub main { |
3388 | cf::init_globals; # initialise logging |
3511 | cf::init_globals; # initialise logging |
3389 | |
3512 | |
… | |
… | |
3409 | }; |
3532 | }; |
3410 | |
3533 | |
3411 | evthread_start IO::AIO::poll_fileno; |
3534 | evthread_start IO::AIO::poll_fileno; |
3412 | |
3535 | |
3413 | cf::sync_job { |
3536 | cf::sync_job { |
|
|
3537 | cf::load_settings; |
|
|
3538 | cf::load_materials; |
|
|
3539 | |
3414 | reload_resources; |
3540 | reload_resources; |
3415 | reload_config; |
3541 | reload_config; |
3416 | db_init; |
3542 | db_init; |
3417 | |
3543 | |
3418 | cf::load_settings; |
|
|
3419 | cf::load_materials; |
|
|
3420 | cf::init_uuid; |
3544 | cf::init_uuid; |
3421 | cf::init_signals; |
3545 | cf::init_signals; |
3422 | cf::init_commands; |
3546 | cf::init_commands; |
3423 | cf::init_skills; |
3547 | cf::init_skills; |
3424 | |
3548 | |
… | |
… | |
3429 | load_extensions; |
3553 | load_extensions; |
3430 | |
3554 | |
3431 | utime time, time, $RUNTIMEFILE; |
3555 | utime time, time, $RUNTIMEFILE; |
3432 | |
3556 | |
3433 | # no (long-running) fork's whatsoever before this point(!) |
3557 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3558 | use POSIX (); |
3434 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3559 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3435 | |
3560 | |
3436 | (pop @POST_INIT)->(0) while @POST_INIT; |
3561 | (pop @POST_INIT)->(0) while @POST_INIT; |
3437 | }; |
3562 | }; |
3438 | |
3563 | |
3439 | EV::loop; |
3564 | main_loop; |
3440 | } |
3565 | } |
3441 | |
3566 | |
3442 | ############################################################################# |
3567 | ############################################################################# |
3443 | # initialisation and cleanup |
3568 | # initialisation and cleanup |
3444 | |
3569 | |
… | |
… | |
3451 | }; |
3576 | }; |
3452 | } |
3577 | } |
3453 | } |
3578 | } |
3454 | |
3579 | |
3455 | sub write_runtime_sync { |
3580 | sub write_runtime_sync { |
|
|
3581 | my $t0 = EV::time; |
|
|
3582 | |
3456 | # first touch the runtime file to show we are still running: |
3583 | # first touch the runtime file to show we are still running: |
3457 | # the fsync below can take a very very long time. |
3584 | # the fsync below can take a very very long time. |
3458 | |
3585 | |
3459 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3586 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3460 | |
3587 | |
3461 | my $guard = cf::lock_acquire "write_runtime"; |
3588 | my $guard = cf::lock_acquire "write_runtime"; |
3462 | |
3589 | |
3463 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3590 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3464 | or return; |
3591 | or return; |
3465 | |
3592 | |
3466 | my $value = $cf::RUNTIME + 90 + 10; |
3593 | my $value = $cf::RUNTIME + 90 + 10; |
3467 | # 10 is the runtime save interval, for a monotonic clock |
3594 | # 10 is the runtime save interval, for a monotonic clock |
3468 | # 60 allows for the watchdog to kill the server. |
3595 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3481 | or return; |
3608 | or return; |
3482 | |
3609 | |
3483 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3610 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3484 | and return; |
3611 | and return; |
3485 | |
3612 | |
3486 | warn "runtime file written.\n"; |
3613 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3487 | |
3614 | |
3488 | 1 |
3615 | 1 |
3489 | } |
3616 | } |
3490 | |
3617 | |
3491 | our $uuid_lock; |
3618 | our $uuid_lock; |
… | |
… | |
3710 | |
3837 | |
3711 | warn "unload completed, starting to reload now"; |
3838 | warn "unload completed, starting to reload now"; |
3712 | |
3839 | |
3713 | warn "reloading cf.pm"; |
3840 | warn "reloading cf.pm"; |
3714 | require cf; |
3841 | require cf; |
3715 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3842 | cf::_connect_to_perl_1; |
3716 | |
3843 | |
3717 | warn "loading config and database again"; |
3844 | warn "loading config and database again"; |
3718 | cf::reload_config; |
3845 | cf::reload_config; |
3719 | |
3846 | |
3720 | warn "loading extensions"; |
3847 | warn "loading extensions"; |
… | |
… | |
3782 | |
3909 | |
3783 | our @WAIT_FOR_TICK; |
3910 | our @WAIT_FOR_TICK; |
3784 | our @WAIT_FOR_TICK_BEGIN; |
3911 | our @WAIT_FOR_TICK_BEGIN; |
3785 | |
3912 | |
3786 | sub wait_for_tick { |
3913 | sub wait_for_tick { |
3787 | return if tick_inhibit || $Coro::current == $Coro::main; |
3914 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3788 | |
3915 | |
3789 | my $signal = new Coro::Signal; |
3916 | my $signal = new Coro::Signal; |
3790 | push @WAIT_FOR_TICK, $signal; |
3917 | push @WAIT_FOR_TICK, $signal; |
3791 | $signal->wait; |
3918 | $signal->wait; |
3792 | } |
3919 | } |
3793 | |
3920 | |
3794 | sub wait_for_tick_begin { |
3921 | sub wait_for_tick_begin { |
3795 | return if tick_inhibit || $Coro::current == $Coro::main; |
3922 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3796 | |
3923 | |
3797 | my $signal = new Coro::Signal; |
3924 | my $signal = new Coro::Signal; |
3798 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3925 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3799 | $signal->wait; |
3926 | $signal->wait; |
3800 | } |
3927 | } |
… | |
… | |
3805 | unless ++$bug_warning > 10; |
3932 | unless ++$bug_warning > 10; |
3806 | return; |
3933 | return; |
3807 | } |
3934 | } |
3808 | |
3935 | |
3809 | cf::server_tick; # one server iteration |
3936 | cf::server_tick; # one server iteration |
|
|
3937 | |
|
|
3938 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3810 | |
3939 | |
3811 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3940 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3812 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3941 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3813 | Coro::async_pool { |
3942 | Coro::async_pool { |
3814 | $Coro::current->{desc} = "runtime saver"; |
3943 | $Coro::current->{desc} = "runtime saver"; |
… | |
… | |
3837 | } |
3966 | } |
3838 | |
3967 | |
3839 | { |
3968 | { |
3840 | # configure BDB |
3969 | # configure BDB |
3841 | |
3970 | |
3842 | BDB::min_parallel 8; |
3971 | BDB::min_parallel 16; |
3843 | BDB::max_poll_reqs $TICK * 0.1; |
3972 | BDB::max_poll_reqs $TICK * 0.1; |
3844 | $AnyEvent::BDB::WATCHER->priority (1); |
3973 | $AnyEvent::BDB::WATCHER->priority (1); |
3845 | |
3974 | |
3846 | unless ($DB_ENV) { |
3975 | unless ($DB_ENV) { |
3847 | $DB_ENV = BDB::db_env_create; |
3976 | $DB_ENV = BDB::db_env_create; |
… | |
… | |
3928 | } |
4057 | } |
3929 | } |
4058 | } |
3930 | |
4059 | |
3931 | # load additional modules |
4060 | # load additional modules |
3932 | require "cf/$_.pm" for @EXTRA_MODULES; |
4061 | require "cf/$_.pm" for @EXTRA_MODULES; |
|
|
4062 | cf::_connect_to_perl_2; |
3933 | |
4063 | |
3934 | END { cf::emergency_save } |
4064 | END { cf::emergency_save } |
3935 | |
4065 | |
3936 | 1 |
4066 | 1 |
3937 | |
4067 | |