|
|
1 | # |
|
|
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | # |
|
|
4 | # Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | # |
|
|
6 | # Deliantra is free software: you can redistribute it and/or modify |
|
|
7 | # it under the terms of the GNU General Public License as published by |
|
|
8 | # the Free Software Foundation, either version 3 of the License, or |
|
|
9 | # (at your option) any later version. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, |
|
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | # GNU General Public License for more details. |
|
|
15 | # |
|
|
16 | # You should have received a copy of the GNU General Public License |
|
|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
18 | # |
|
|
19 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
20 | # |
|
|
21 | |
1 | package cf; |
22 | package cf; |
2 | |
23 | |
3 | use utf8; |
24 | use utf8; |
4 | use strict; |
25 | use strict; |
5 | |
26 | |
6 | use Symbol; |
27 | use Symbol; |
7 | use List::Util; |
28 | use List::Util; |
8 | use Socket; |
29 | use Socket; |
9 | use EV; |
30 | use EV 3.2; |
10 | use Opcode; |
31 | use Opcode; |
11 | use Safe; |
32 | use Safe; |
12 | use Safe::Hole; |
33 | use Safe::Hole; |
13 | use Storable (); |
34 | use Storable (); |
14 | |
35 | |
15 | use Coro 4.1 (); |
36 | use Coro 4.32 (); |
16 | use Coro::State; |
37 | use Coro::State; |
17 | use Coro::Handle; |
38 | use Coro::Handle; |
18 | use Coro::EV; |
39 | use Coro::EV; |
19 | use Coro::Timer; |
40 | use Coro::Timer; |
20 | use Coro::Signal; |
41 | use Coro::Signal; |
21 | use Coro::Semaphore; |
42 | use Coro::Semaphore; |
22 | use Coro::AIO; |
43 | use Coro::AIO; |
|
|
44 | use Coro::BDB; |
23 | use Coro::Storable; |
45 | use Coro::Storable; |
24 | use Coro::Util (); |
46 | use Coro::Util (); |
25 | |
47 | |
26 | use JSON::XS (); |
48 | use JSON::XS 2.01 (); |
27 | use BDB (); |
49 | use BDB (); |
28 | use Data::Dumper; |
50 | use Data::Dumper; |
29 | use Digest::MD5; |
51 | use Digest::MD5; |
30 | use Fcntl; |
52 | use Fcntl; |
31 | use YAML::Syck (); |
53 | use YAML (); |
32 | use IO::AIO 2.51 (); |
54 | use IO::AIO 2.51 (); |
33 | use Time::HiRes; |
55 | use Time::HiRes; |
34 | use Compress::LZF; |
56 | use Compress::LZF; |
35 | use Digest::MD5 (); |
57 | use Digest::MD5 (); |
36 | |
58 | |
37 | # configure various modules to our taste |
59 | # configure various modules to our taste |
38 | # |
60 | # |
39 | $Storable::canonical = 1; # reduce rsync transfers |
61 | $Storable::canonical = 1; # reduce rsync transfers |
40 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
62 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
41 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
63 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
42 | |
|
|
43 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
44 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
45 | |
64 | |
46 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
65 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
47 | |
66 | |
48 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
67 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
49 | |
68 | |
… | |
… | |
68 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
87 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
69 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
88 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
70 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
89 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
71 | our $RANDOMDIR = "$LOCALDIR/random"; |
90 | our $RANDOMDIR = "$LOCALDIR/random"; |
72 | our $BDBDIR = "$LOCALDIR/db"; |
91 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
92 | our %RESOURCE; |
73 | |
93 | |
74 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
94 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
75 | our $TICK_WATCHER; |
|
|
76 | our $AIO_POLL_WATCHER; |
95 | our $AIO_POLL_WATCHER; |
77 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
96 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
78 | our $NEXT_TICK; |
97 | our $NEXT_TICK; |
79 | our $NOW; |
|
|
80 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
98 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
81 | |
99 | |
82 | our $BDB_POLL_WATCHER; |
100 | our $BDB_POLL_WATCHER; |
83 | our $BDB_DEADLOCK_WATCHER; |
101 | our $BDB_DEADLOCK_WATCHER; |
84 | our $BDB_CHECKPOINT_WATCHER; |
102 | our $BDB_CHECKPOINT_WATCHER; |
… | |
… | |
87 | |
105 | |
88 | our %CFG; |
106 | our %CFG; |
89 | |
107 | |
90 | our $UPTIME; $UPTIME ||= time; |
108 | our $UPTIME; $UPTIME ||= time; |
91 | our $RUNTIME; |
109 | our $RUNTIME; |
|
|
110 | our $NOW; |
92 | |
111 | |
93 | our (%PLAYER, %PLAYER_LOADING); # all users |
112 | our (%PLAYER, %PLAYER_LOADING); # all users |
94 | our (%MAP, %MAP_LOADING ); # all maps |
113 | our (%MAP, %MAP_LOADING ); # all maps |
95 | our $LINK_MAP; # the special {link} map, which is always available |
114 | our $LINK_MAP; # the special {link} map, which is always available |
96 | |
115 | |
97 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
116 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
98 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
117 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
99 | |
118 | |
100 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
119 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
101 | our $LOADAVG; # same thing, but with alpha-smoothing |
120 | our $LOADAVG; # same thing, but with alpha-smoothing |
|
|
121 | our $JITTER; # average jitter |
102 | our $tick_start; # for load detecting purposes |
122 | our $TICK_START; # for load detecting purposes |
103 | |
123 | |
104 | binmode STDOUT; |
124 | binmode STDOUT; |
105 | binmode STDERR; |
125 | binmode STDERR; |
106 | |
126 | |
107 | # read virtual server time, if available |
127 | # read virtual server time, if available |
… | |
… | |
191 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
211 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
192 | |
212 | |
193 | LOG llevError, $msg; |
213 | LOG llevError, $msg; |
194 | }; |
214 | }; |
195 | } |
215 | } |
|
|
216 | |
|
|
217 | $Coro::State::DIEHOOK = sub { |
|
|
218 | return unless $^S eq 0; # "eq", not "==" |
|
|
219 | |
|
|
220 | if ($Coro::current == $Coro::main) {#d# |
|
|
221 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
|
|
222 | return;#d# |
|
|
223 | }#d# |
|
|
224 | |
|
|
225 | # kill coroutine otherwise |
|
|
226 | warn Carp::longmess $_[0]; |
|
|
227 | Coro::terminate |
|
|
228 | }; |
|
|
229 | |
|
|
230 | $SIG{__DIE__} = sub { }; #d#? |
196 | |
231 | |
197 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
232 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
198 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
233 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
199 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
234 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
200 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
235 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
246 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
281 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
247 | $d |
282 | $d |
248 | } || "[unable to dump $_[0]: '$@']"; |
283 | } || "[unable to dump $_[0]: '$@']"; |
249 | } |
284 | } |
250 | |
285 | |
251 | =item $ref = cf::from_json $json |
286 | =item $ref = cf::decode_json $json |
252 | |
287 | |
253 | Converts a JSON string into the corresponding perl data structure. |
288 | Converts a JSON string into the corresponding perl data structure. |
254 | |
289 | |
255 | =item $json = cf::to_json $ref |
290 | =item $json = cf::encode_json $ref |
256 | |
291 | |
257 | Converts a perl data structure into its JSON representation. |
292 | Converts a perl data structure into its JSON representation. |
258 | |
293 | |
259 | =cut |
294 | =cut |
260 | |
295 | |
261 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
296 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
262 | |
297 | |
263 | sub to_json ($) { $json_coder->encode ($_[0]) } |
298 | sub encode_json($) { $json_coder->encode ($_[0]) } |
264 | sub from_json ($) { $json_coder->decode ($_[0]) } |
299 | sub decode_json($) { $json_coder->decode ($_[0]) } |
265 | |
300 | |
266 | =item cf::lock_wait $string |
301 | =item cf::lock_wait $string |
267 | |
302 | |
268 | Wait until the given lock is available. See cf::lock_acquire. |
303 | Wait until the given lock is available. See cf::lock_acquire. |
269 | |
304 | |
… | |
… | |
325 | |
360 | |
326 | ! ! $LOCK{$key} |
361 | ! ! $LOCK{$key} |
327 | } |
362 | } |
328 | |
363 | |
329 | sub freeze_mainloop { |
364 | sub freeze_mainloop { |
330 | return unless $TICK_WATCHER->is_active; |
365 | tick_inhibit_inc; |
331 | |
366 | |
332 | my $guard = Coro::guard { |
367 | Coro::guard \&tick_inhibit_dec; |
333 | $TICK_WATCHER->start; |
|
|
334 | }; |
|
|
335 | $TICK_WATCHER->stop; |
|
|
336 | $guard |
|
|
337 | } |
368 | } |
338 | |
369 | |
339 | =item cf::periodic $interval, $cb |
370 | =item cf::periodic $interval, $cb |
340 | |
371 | |
341 | Like EV::periodic, but randomly selects a starting point so that the actions |
372 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
442 | # this is the main coro, too bad, we have to block |
473 | # this is the main coro, too bad, we have to block |
443 | # till the operation succeeds, freezing the server :/ |
474 | # till the operation succeeds, freezing the server :/ |
444 | |
475 | |
445 | LOG llevError, Carp::longmess "sync job";#d# |
476 | LOG llevError, Carp::longmess "sync job";#d# |
446 | |
477 | |
447 | # TODO: use suspend/resume instead |
|
|
448 | # (but this is cancel-safe) |
|
|
449 | my $freeze_guard = freeze_mainloop; |
478 | my $freeze_guard = freeze_mainloop; |
450 | |
479 | |
451 | my $busy = 1; |
480 | my $busy = 1; |
452 | my @res; |
481 | my @res; |
453 | |
482 | |
… | |
… | |
464 | } else { |
493 | } else { |
465 | EV::loop EV::LOOP_ONESHOT; |
494 | EV::loop EV::LOOP_ONESHOT; |
466 | } |
495 | } |
467 | } |
496 | } |
468 | |
497 | |
469 | $time = EV::time - $time; |
498 | my $time = EV::time - $time; |
470 | |
499 | |
471 | LOG llevError | logBacktrace, Carp::longmess "long sync job" |
|
|
472 | if $time > $TICK * 0.5 && $TICK_WATCHER->is_active; |
|
|
473 | |
|
|
474 | $tick_start += $time; # do not account sync jobs to server load |
500 | $TICK_START += $time; # do not account sync jobs to server load |
475 | |
501 | |
476 | wantarray ? @res : $res[0] |
502 | wantarray ? @res : $res[0] |
477 | } else { |
503 | } else { |
478 | # we are in another coroutine, how wonderful, everything just works |
504 | # we are in another coroutine, how wonderful, everything just works |
479 | |
505 | |
… | |
… | |
967 | } |
993 | } |
968 | |
994 | |
969 | 0 |
995 | 0 |
970 | } |
996 | } |
971 | |
997 | |
972 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
998 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
973 | |
999 | |
974 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
1000 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
975 | |
1001 | |
976 | Generate an object-specific event with the given arguments. |
1002 | Generate an object-specific event with the given arguments. |
977 | |
1003 | |
… | |
… | |
1055 | cf::attachable->attach ( |
1081 | cf::attachable->attach ( |
1056 | prio => -1000000, |
1082 | prio => -1000000, |
1057 | on_instantiate => sub { |
1083 | on_instantiate => sub { |
1058 | my ($obj, $data) = @_; |
1084 | my ($obj, $data) = @_; |
1059 | |
1085 | |
1060 | $data = from_json $data; |
1086 | $data = decode_json $data; |
1061 | |
1087 | |
1062 | for (@$data) { |
1088 | for (@$data) { |
1063 | my ($name, $args) = @$_; |
1089 | my ($name, $args) = @$_; |
1064 | |
1090 | |
1065 | $obj->attach ($name, %{$args || {} }); |
1091 | $obj->attach ($name, %{$args || {} }); |
… | |
… | |
1302 | my $msg = $@ ? "$v->{path}: $@\n" |
1328 | my $msg = $@ ? "$v->{path}: $@\n" |
1303 | : "$v->{base}: extension inactive.\n"; |
1329 | : "$v->{base}: extension inactive.\n"; |
1304 | |
1330 | |
1305 | if (exists $v->{meta}{mandatory}) { |
1331 | if (exists $v->{meta}{mandatory}) { |
1306 | warn $msg; |
1332 | warn $msg; |
1307 | warn "mandatory extension failed to load, exiting.\n"; |
1333 | cf::cleanup "mandatory extension failed to load, exiting."; |
1308 | exit 1; |
|
|
1309 | } |
1334 | } |
1310 | |
1335 | |
1311 | warn $msg; |
1336 | warn $msg; |
1312 | } |
1337 | } |
1313 | |
1338 | |
… | |
… | |
2638 | $rmp->{origin_y} = $exit->y; |
2663 | $rmp->{origin_y} = $exit->y; |
2639 | } |
2664 | } |
2640 | |
2665 | |
2641 | $rmp->{random_seed} ||= $exit->random_seed; |
2666 | $rmp->{random_seed} ||= $exit->random_seed; |
2642 | |
2667 | |
2643 | my $data = cf::to_json $rmp; |
2668 | my $data = cf::encode_json $rmp; |
2644 | my $md5 = Digest::MD5::md5_hex $data; |
2669 | my $md5 = Digest::MD5::md5_hex $data; |
2645 | my $meta = "$RANDOMDIR/$md5.meta"; |
2670 | my $meta = "$RANDOMDIR/$md5.meta"; |
2646 | |
2671 | |
2647 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2672 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2648 | aio_write $fh, 0, (length $data), $data, 0; |
2673 | aio_write $fh, 0, (length $data), $data, 0; |
… | |
… | |
3148 | { |
3173 | { |
3149 | my $faces = $facedata->{faceinfo}; |
3174 | my $faces = $facedata->{faceinfo}; |
3150 | |
3175 | |
3151 | while (my ($face, $info) = each %$faces) { |
3176 | while (my ($face, $info) = each %$faces) { |
3152 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3177 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3178 | |
3153 | cf::face::set_visibility $idx, $info->{visibility}; |
3179 | cf::face::set_visibility $idx, $info->{visibility}; |
3154 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3180 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3155 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3181 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3156 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3182 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3157 | |
3183 | |
3158 | cf::cede_to_tick; |
3184 | cf::cede_to_tick; |
3159 | } |
3185 | } |
3160 | |
3186 | |
3161 | while (my ($face, $info) = each %$faces) { |
3187 | while (my ($face, $info) = each %$faces) { |
3162 | next unless $info->{smooth}; |
3188 | next unless $info->{smooth}; |
|
|
3189 | |
3163 | my $idx = cf::face::find $face |
3190 | my $idx = cf::face::find $face |
3164 | or next; |
3191 | or next; |
|
|
3192 | |
3165 | if (my $smooth = cf::face::find $info->{smooth}) { |
3193 | if (my $smooth = cf::face::find $info->{smooth}) { |
3166 | cf::face::set_smooth $idx, $smooth; |
3194 | cf::face::set_smooth $idx, $smooth; |
3167 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3195 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3168 | } else { |
3196 | } else { |
3169 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3197 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3187 | { |
3215 | { |
3188 | # TODO: for gcfclient pleasure, we should give resources |
3216 | # TODO: for gcfclient pleasure, we should give resources |
3189 | # that gcfclient doesn't grok a >10000 face index. |
3217 | # that gcfclient doesn't grok a >10000 face index. |
3190 | my $res = $facedata->{resource}; |
3218 | my $res = $facedata->{resource}; |
3191 | |
3219 | |
3192 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3193 | |
|
|
3194 | while (my ($name, $info) = each %$res) { |
3220 | while (my ($name, $info) = each %$res) { |
|
|
3221 | if (defined $info->{type}) { |
3195 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3222 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3196 | my $data; |
3223 | my $data; |
3197 | |
3224 | |
3198 | if ($info->{type} & 1) { |
3225 | if ($info->{type} & 1) { |
3199 | # prepend meta info |
3226 | # prepend meta info |
3200 | |
3227 | |
3201 | my $meta = $enc->encode ({ |
3228 | my $meta = $enc->encode ({ |
3202 | name => $name, |
3229 | name => $name, |
3203 | %{ $info->{meta} || {} }, |
3230 | %{ $info->{meta} || {} }, |
3204 | }); |
3231 | }); |
3205 | |
3232 | |
3206 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3233 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3234 | } else { |
|
|
3235 | $data = $info->{data}; |
|
|
3236 | } |
|
|
3237 | |
|
|
3238 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3239 | cf::face::set_type $idx, $info->{type}; |
3207 | } else { |
3240 | } else { |
3208 | $data = $info->{data}; |
3241 | $RESOURCE{$name} = $info; |
3209 | } |
3242 | } |
3210 | |
3243 | |
3211 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3212 | cf::face::set_type $idx, $info->{type}; |
|
|
3213 | |
|
|
3214 | cf::cede_to_tick; |
3244 | cf::cede_to_tick; |
3215 | } |
3245 | } |
3216 | |
|
|
3217 | if ($soundconf) { |
|
|
3218 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
3219 | |
|
|
3220 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3221 | my $sound = $soundconf->{compat}[$_] |
|
|
3222 | or next; |
|
|
3223 | |
|
|
3224 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3225 | cf::sound::set $sound->[0] => $face; |
|
|
3226 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3227 | } |
|
|
3228 | |
|
|
3229 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3230 | my $face = cf::face::find "sound/$v"; |
|
|
3231 | cf::sound::set $k => $face; |
|
|
3232 | } |
|
|
3233 | } |
|
|
3234 | } |
3246 | } |
|
|
3247 | |
|
|
3248 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3235 | |
3249 | |
3236 | 1 |
3250 | 1 |
3237 | } |
3251 | } |
|
|
3252 | |
|
|
3253 | cf::global->attach (on_resource_update => sub { |
|
|
3254 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3255 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3256 | |
|
|
3257 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3258 | my $sound = $soundconf->{compat}[$_] |
|
|
3259 | or next; |
|
|
3260 | |
|
|
3261 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3262 | cf::sound::set $sound->[0] => $face; |
|
|
3263 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3264 | } |
|
|
3265 | |
|
|
3266 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3267 | my $face = cf::face::find "sound/$v"; |
|
|
3268 | cf::sound::set $k => $face; |
|
|
3269 | } |
|
|
3270 | } |
|
|
3271 | }); |
3238 | |
3272 | |
3239 | register_exticmd fx_want => sub { |
3273 | register_exticmd fx_want => sub { |
3240 | my ($ns, $want) = @_; |
3274 | my ($ns, $want) = @_; |
3241 | |
3275 | |
3242 | while (my ($k, $v) = each %$want) { |
3276 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3297 | sub reload_config { |
3331 | sub reload_config { |
3298 | open my $fh, "<:utf8", "$CONFDIR/config" |
3332 | open my $fh, "<:utf8", "$CONFDIR/config" |
3299 | or return; |
3333 | or return; |
3300 | |
3334 | |
3301 | local $/; |
3335 | local $/; |
3302 | *CFG = YAML::Syck::Load <$fh>; |
3336 | *CFG = YAML::Load <$fh>; |
3303 | |
3337 | |
3304 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3338 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3305 | |
3339 | |
3306 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3340 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3307 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3341 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3327 | |
3361 | |
3328 | reload_config; |
3362 | reload_config; |
3329 | db_init; |
3363 | db_init; |
3330 | load_extensions; |
3364 | load_extensions; |
3331 | |
3365 | |
3332 | $TICK_WATCHER->start; |
|
|
3333 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3366 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
|
|
3367 | evthread_start IO::AIO::poll_fileno; |
3334 | EV::loop; |
3368 | EV::loop; |
3335 | } |
3369 | } |
3336 | |
3370 | |
3337 | ############################################################################# |
3371 | ############################################################################# |
3338 | # initialisation and cleanup |
3372 | # initialisation and cleanup |
… | |
… | |
3535 | warn "leaving sync_job"; |
3569 | warn "leaving sync_job"; |
3536 | |
3570 | |
3537 | 1 |
3571 | 1 |
3538 | } or do { |
3572 | } or do { |
3539 | warn $@; |
3573 | warn $@; |
3540 | warn "error while reloading, exiting."; |
3574 | cf::cleanup "error while reloading, exiting."; |
3541 | exit 1; |
|
|
3542 | }; |
3575 | }; |
3543 | |
3576 | |
3544 | warn "reloaded"; |
3577 | warn "reloaded"; |
3545 | }; |
3578 | }; |
3546 | |
3579 | |
… | |
… | |
3549 | sub reload_perl() { |
3582 | sub reload_perl() { |
3550 | # doing reload synchronously and two reloads happen back-to-back, |
3583 | # doing reload synchronously and two reloads happen back-to-back, |
3551 | # coro crashes during coro_state_free->destroy here. |
3584 | # coro crashes during coro_state_free->destroy here. |
3552 | |
3585 | |
3553 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
3586 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
|
|
3587 | do_reload_perl; |
3554 | undef $RELOAD_WATCHER; |
3588 | undef $RELOAD_WATCHER; |
3555 | do_reload_perl; |
|
|
3556 | }; |
3589 | }; |
3557 | } |
3590 | } |
3558 | |
3591 | |
3559 | register_command "reload" => sub { |
3592 | register_command "reload" => sub { |
3560 | my ($who, $arg) = @_; |
3593 | my ($who, $arg) = @_; |
… | |
… | |
3574 | |
3607 | |
3575 | our @WAIT_FOR_TICK; |
3608 | our @WAIT_FOR_TICK; |
3576 | our @WAIT_FOR_TICK_BEGIN; |
3609 | our @WAIT_FOR_TICK_BEGIN; |
3577 | |
3610 | |
3578 | sub wait_for_tick { |
3611 | sub wait_for_tick { |
3579 | return unless $TICK_WATCHER->is_active; |
3612 | return if tick_inhibit; |
3580 | return if $Coro::current == $Coro::main; |
3613 | return if $Coro::current == $Coro::main; |
3581 | |
3614 | |
3582 | my $signal = new Coro::Signal; |
3615 | my $signal = new Coro::Signal; |
3583 | push @WAIT_FOR_TICK, $signal; |
3616 | push @WAIT_FOR_TICK, $signal; |
3584 | $signal->wait; |
3617 | $signal->wait; |
3585 | } |
3618 | } |
3586 | |
3619 | |
3587 | sub wait_for_tick_begin { |
3620 | sub wait_for_tick_begin { |
3588 | return unless $TICK_WATCHER->is_active; |
3621 | return if tick_inhibit; |
3589 | return if $Coro::current == $Coro::main; |
3622 | return if $Coro::current == $Coro::main; |
3590 | |
3623 | |
3591 | my $signal = new Coro::Signal; |
3624 | my $signal = new Coro::Signal; |
3592 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3625 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3593 | $signal->wait; |
3626 | $signal->wait; |
3594 | } |
3627 | } |
3595 | |
3628 | |
3596 | $TICK_WATCHER = EV::periodic_ns 0, $TICK, 0, sub { |
3629 | sub tick { |
3597 | if ($Coro::current != $Coro::main) { |
3630 | if ($Coro::current != $Coro::main) { |
3598 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3631 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3599 | unless ++$bug_warning > 10; |
3632 | unless ++$bug_warning > 10; |
3600 | return; |
3633 | return; |
3601 | } |
3634 | } |
3602 | |
3635 | |
3603 | $NOW = $tick_start = EV::now; |
|
|
3604 | |
|
|
3605 | cf::server_tick; # one server iteration |
3636 | cf::server_tick; # one server iteration |
3606 | |
3637 | |
3607 | $RUNTIME += $TICK; |
|
|
3608 | $NEXT_TICK += $TICK; |
|
|
3609 | |
|
|
3610 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3638 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3611 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3639 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3612 | Coro::async_pool { |
3640 | Coro::async_pool { |
3613 | $Coro::current->{desc} = "runtime saver"; |
3641 | $Coro::current->{desc} = "runtime saver"; |
3614 | write_runtime |
3642 | write_runtime |
3615 | or warn "ERROR: unable to write runtime file: $!"; |
3643 | or warn "ERROR: unable to write runtime file: $!"; |
3616 | }; |
3644 | }; |
… | |
… | |
3621 | } |
3649 | } |
3622 | while (my $sig = shift @WAIT_FOR_TICK) { |
3650 | while (my $sig = shift @WAIT_FOR_TICK) { |
3623 | $sig->send; |
3651 | $sig->send; |
3624 | } |
3652 | } |
3625 | |
3653 | |
3626 | $LOAD = ($NOW - $tick_start) / $TICK; |
3654 | $LOAD = ($NOW - $TICK_START) / $TICK; |
3627 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3655 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3628 | |
3656 | |
3629 | _post_tick; |
3657 | if (0) { |
3630 | }; |
3658 | if ($NEXT_TICK) { |
3631 | $TICK_WATCHER->priority (EV::MAXPRI); |
3659 | my $jitter = $TICK_START - $NEXT_TICK; |
|
|
3660 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
|
|
3661 | warn "jitter $JITTER\n";#d# |
|
|
3662 | } |
|
|
3663 | } |
|
|
3664 | } |
3632 | |
3665 | |
3633 | { |
3666 | { |
|
|
3667 | # configure BDB |
|
|
3668 | |
3634 | BDB::min_parallel 8; |
3669 | BDB::min_parallel 8; |
3635 | BDB::max_poll_time $TICK * 0.1; |
3670 | BDB::max_poll_reqs $TICK * 0.1; |
3636 | $BDB_POLL_WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
3671 | $Coro::BDB::WATCHER->priority (1); |
3637 | |
|
|
3638 | BDB::set_sync_prepare { |
|
|
3639 | my $status; |
|
|
3640 | my $current = $Coro::current; |
|
|
3641 | ( |
|
|
3642 | sub { |
|
|
3643 | $status = $!; |
|
|
3644 | $current->ready; undef $current; |
|
|
3645 | }, |
|
|
3646 | sub { |
|
|
3647 | Coro::schedule while defined $current; |
|
|
3648 | $! = $status; |
|
|
3649 | }, |
|
|
3650 | ) |
|
|
3651 | }; |
|
|
3652 | |
3672 | |
3653 | unless ($DB_ENV) { |
3673 | unless ($DB_ENV) { |
3654 | $DB_ENV = BDB::db_env_create; |
3674 | $DB_ENV = BDB::db_env_create; |
3655 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3675 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3656 | | BDB::LOG_AUTOREMOVE, 1); |
3676 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3683 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3703 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3684 | }; |
3704 | }; |
3685 | } |
3705 | } |
3686 | |
3706 | |
3687 | { |
3707 | { |
|
|
3708 | # configure IO::AIO |
|
|
3709 | |
3688 | IO::AIO::min_parallel 8; |
3710 | IO::AIO::min_parallel 8; |
3689 | |
|
|
3690 | undef $Coro::AIO::WATCHER; |
|
|
3691 | IO::AIO::max_poll_time $TICK * 0.1; |
3711 | IO::AIO::max_poll_time $TICK * 0.1; |
3692 | $AIO_POLL_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
3712 | $Coro::AIO::WATCHER->priority (1); |
3693 | } |
3713 | } |
3694 | |
3714 | |
3695 | my $_log_backtrace; |
3715 | my $_log_backtrace; |
3696 | |
3716 | |
3697 | sub _log_backtrace { |
3717 | sub _log_backtrace { |