1 | package cf; |
1 | package cf; |
|
|
2 | |
|
|
3 | use utf8; |
|
|
4 | use strict; |
2 | |
5 | |
3 | use Symbol; |
6 | use Symbol; |
4 | use List::Util; |
7 | use List::Util; |
5 | use Storable; |
8 | use Storable; |
6 | use Opcode; |
9 | use Opcode; |
7 | use Safe; |
10 | use Safe; |
8 | use Safe::Hole; |
11 | use Safe::Hole; |
9 | |
12 | |
|
|
13 | use Coro 3.3; |
|
|
14 | use Coro::Event; |
|
|
15 | use Coro::Timer; |
|
|
16 | use Coro::Signal; |
|
|
17 | use Coro::Semaphore; |
|
|
18 | |
10 | use IO::AIO (); |
19 | use IO::AIO 2.3; |
11 | use YAML::Syck (); |
20 | use YAML::Syck (); |
12 | use Time::HiRes; |
21 | use Time::HiRes; |
13 | use Event; |
22 | |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
23 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
15 | |
24 | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
25 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
26 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
27 | |
19 | use strict; |
28 | $Coro::main->prio (2); # run main coroutine ("the server") with very high priority |
20 | |
29 | |
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
30 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
22 | |
31 | |
23 | our %COMMAND = (); |
32 | our %COMMAND = (); |
24 | our %COMMAND_TIME = (); |
33 | our %COMMAND_TIME = (); |
25 | our %EXTCMD = (); |
34 | our %EXTCMD = (); |
26 | |
35 | |
27 | _init_vars; |
|
|
28 | |
|
|
29 | our @EVENT; |
36 | our @EVENT; |
30 | our $LIBDIR = datadir . "/ext"; |
37 | our $LIBDIR = datadir . "/ext"; |
31 | |
38 | |
32 | our $TICK = MAX_TIME * 1e-6; |
39 | our $TICK = MAX_TIME * 1e-6; |
33 | our $TICK_WATCHER; |
40 | our $TICK_WATCHER; |
34 | our $NEXT_TICK; |
41 | our $NEXT_TICK; |
|
|
42 | our $NOW; |
35 | |
43 | |
36 | our %CFG; |
44 | our %CFG; |
37 | |
45 | |
38 | our $UPTIME; $UPTIME ||= time; |
46 | our $UPTIME; $UPTIME ||= time; |
|
|
47 | our $RUNTIME; |
|
|
48 | |
|
|
49 | our %MAP; # all maps |
|
|
50 | our $LINK_MAP; # the special {link} map |
|
|
51 | our $FREEZE; |
|
|
52 | |
|
|
53 | binmode STDOUT; |
|
|
54 | binmode STDERR; |
|
|
55 | |
|
|
56 | # read virtual server time, if available |
|
|
57 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
|
|
58 | open my $fh, "<", cf::localdir . "/runtime" |
|
|
59 | or die "unable to read runtime file: $!"; |
|
|
60 | $RUNTIME = <$fh> + 0.; |
|
|
61 | } |
|
|
62 | |
|
|
63 | mkdir cf::localdir; |
|
|
64 | mkdir cf::localdir . "/" . cf::playerdir; |
|
|
65 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
66 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
67 | |
|
|
68 | our %EXT_CORO; |
39 | |
69 | |
40 | ############################################################################# |
70 | ############################################################################# |
41 | |
71 | |
42 | =head2 GLOBAL VARIABLES |
72 | =head2 GLOBAL VARIABLES |
43 | |
73 | |
44 | =over 4 |
74 | =over 4 |
45 | |
75 | |
46 | =item $cf::UPTIME |
76 | =item $cf::UPTIME |
47 | |
77 | |
48 | The timestamp of the server start (so not actually an uptime). |
78 | The timestamp of the server start (so not actually an uptime). |
|
|
79 | |
|
|
80 | =item $cf::RUNTIME |
|
|
81 | |
|
|
82 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
|
|
83 | every server tick. |
49 | |
84 | |
50 | =item $cf::LIBDIR |
85 | =item $cf::LIBDIR |
51 | |
86 | |
52 | The perl library directory, where extensions and cf-specific modules can |
87 | The perl library directory, where extensions and cf-specific modules can |
53 | be found. It will be added to C<@INC> automatically. |
88 | be found. It will be added to C<@INC> automatically. |
|
|
89 | |
|
|
90 | =item $cf::NOW |
|
|
91 | |
|
|
92 | The time of the last (current) server tick. |
54 | |
93 | |
55 | =item $cf::TICK |
94 | =item $cf::TICK |
56 | |
95 | |
57 | The interval between server ticks, in seconds. |
96 | The interval between server ticks, in seconds. |
58 | |
97 | |
… | |
… | |
66 | =cut |
105 | =cut |
67 | |
106 | |
68 | BEGIN { |
107 | BEGIN { |
69 | *CORE::GLOBAL::warn = sub { |
108 | *CORE::GLOBAL::warn = sub { |
70 | my $msg = join "", @_; |
109 | my $msg = join "", @_; |
|
|
110 | utf8::encode $msg; |
|
|
111 | |
71 | $msg .= "\n" |
112 | $msg .= "\n" |
72 | unless $msg =~ /\n$/; |
113 | unless $msg =~ /\n$/; |
73 | |
114 | |
74 | print STDERR "cfperl: $msg"; |
|
|
75 | LOG llevError, "cfperl: $msg"; |
115 | LOG llevError, "cfperl: $msg"; |
76 | }; |
116 | }; |
77 | } |
117 | } |
78 | |
118 | |
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
119 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
124 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
85 | |
125 | |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
126 | # we bless all objects into (empty) derived classes to force a method lookup |
87 | # within the Safe compartment. |
127 | # within the Safe compartment. |
88 | for my $pkg (qw( |
128 | for my $pkg (qw( |
89 | cf::global |
129 | cf::global cf::attachable |
90 | cf::object cf::object::player |
130 | cf::object cf::object::player |
91 | cf::client cf::player |
131 | cf::client cf::player |
92 | cf::arch cf::living |
132 | cf::arch cf::living |
93 | cf::map cf::party cf::region |
133 | cf::map cf::party cf::region |
94 | )) { |
134 | )) { |
… | |
… | |
130 | =cut |
170 | =cut |
131 | |
171 | |
132 | sub to_json($) { |
172 | sub to_json($) { |
133 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
173 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
134 | JSON::Syck::Dump $_[0] |
174 | JSON::Syck::Dump $_[0] |
|
|
175 | } |
|
|
176 | |
|
|
177 | =item $coro = cf::coro { BLOCK } |
|
|
178 | |
|
|
179 | Creates and returns a new coro. This coro is automcatially being canceled |
|
|
180 | when the extension calling this is being unloaded. |
|
|
181 | |
|
|
182 | =cut |
|
|
183 | |
|
|
184 | sub coro(&) { |
|
|
185 | my $cb = shift; |
|
|
186 | |
|
|
187 | my $coro; $coro = async { |
|
|
188 | eval { |
|
|
189 | $cb->(); |
|
|
190 | }; |
|
|
191 | warn $@ if $@; |
|
|
192 | }; |
|
|
193 | |
|
|
194 | $coro->on_destroy (sub { |
|
|
195 | delete $EXT_CORO{$coro+0}; |
|
|
196 | }); |
|
|
197 | $EXT_CORO{$coro+0} = $coro; |
|
|
198 | |
|
|
199 | $coro |
135 | } |
200 | } |
136 | |
201 | |
137 | =back |
202 | =back |
138 | |
203 | |
139 | =cut |
204 | =cut |
… | |
… | |
249 | my ($self, $victim) = @_; |
314 | my ($self, $victim) = @_; |
250 | ... |
315 | ... |
251 | } |
316 | } |
252 | } |
317 | } |
253 | |
318 | |
|
|
319 | =item $attachable->valid |
|
|
320 | |
|
|
321 | Just because you have a perl object does not mean that the corresponding |
|
|
322 | C-level object still exists. If you try to access an object that has no |
|
|
323 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
324 | can be used to test for existence of the C object part without causing an |
|
|
325 | exception. |
|
|
326 | |
254 | =cut |
327 | =cut |
255 | |
328 | |
256 | # the following variables are defined in .xs and must not be re-created |
329 | # the following variables are defined in .xs and must not be re-created |
257 | our @CB_GLOBAL = (); # registry for all global events |
330 | our @CB_GLOBAL = (); # registry for all global events |
|
|
331 | our @CB_ATTACHABLE = (); # registry for all attachables |
258 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
332 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
259 | our @CB_PLAYER = (); |
333 | our @CB_PLAYER = (); |
260 | our @CB_CLIENT = (); |
334 | our @CB_CLIENT = (); |
261 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
335 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
262 | our @CB_MAP = (); |
336 | our @CB_MAP = (); |
263 | |
337 | |
264 | my %attachment; |
338 | my %attachment; |
265 | |
339 | |
266 | sub _attach_cb($$$$) { |
340 | sub _attach_cb($$$$) { |
267 | my ($registry, $event, $prio, $cb) = @_; |
341 | my ($registry, $event, $prio, $cb) = @_; |
… | |
… | |
272 | |
346 | |
273 | @{$registry->[$event]} = sort |
347 | @{$registry->[$event]} = sort |
274 | { $a->[0] cmp $b->[0] } |
348 | { $a->[0] cmp $b->[0] } |
275 | @{$registry->[$event] || []}, $cb; |
349 | @{$registry->[$event] || []}, $cb; |
276 | } |
350 | } |
|
|
351 | |
|
|
352 | # hack |
|
|
353 | my %attachable_klass = map +($_ => 1), KLASS_OBJECT, KLASS_CLIENT, KLASS_PLAYER, KLASS_MAP; |
277 | |
354 | |
278 | # attach handles attaching event callbacks |
355 | # attach handles attaching event callbacks |
279 | # the only thing the caller has to do is pass the correct |
356 | # the only thing the caller has to do is pass the correct |
280 | # registry (== where the callback attaches to). |
357 | # registry (== where the callback attaches to). |
281 | sub _attach { |
358 | sub _attach { |
… | |
… | |
283 | |
360 | |
284 | my $object_type; |
361 | my $object_type; |
285 | my $prio = 0; |
362 | my $prio = 0; |
286 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
363 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
287 | |
364 | |
|
|
365 | #TODO: get rid of this hack |
|
|
366 | if ($attachable_klass{$klass}) { |
|
|
367 | %cb_id = (%cb_id, map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == KLASS_ATTACHABLE, 0 .. $#EVENT); |
|
|
368 | } |
|
|
369 | |
288 | while (@arg) { |
370 | while (@arg) { |
289 | my $type = shift @arg; |
371 | my $type = shift @arg; |
290 | |
372 | |
291 | if ($type eq "prio") { |
373 | if ($type eq "prio") { |
292 | $prio = shift @arg; |
374 | $prio = shift @arg; |
… | |
… | |
367 | my ($obj, $name) = @_; |
449 | my ($obj, $name) = @_; |
368 | |
450 | |
369 | exists $obj->{_attachment}{$name} |
451 | exists $obj->{_attachment}{$name} |
370 | } |
452 | } |
371 | |
453 | |
372 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
454 | for my $klass (qw(ATTACHABLE GLOBAL OBJECT PLAYER CLIENT MAP)) { |
373 | eval "#line " . __LINE__ . " 'cf.pm' |
455 | eval "#line " . __LINE__ . " 'cf.pm' |
374 | sub cf::\L$klass\E::_attach_registry { |
456 | sub cf::\L$klass\E::_attach_registry { |
375 | (\\\@CB_$klass, KLASS_$klass) |
457 | (\\\@CB_$klass, KLASS_$klass) |
376 | } |
458 | } |
377 | |
459 | |
… | |
… | |
413 | } |
495 | } |
414 | |
496 | |
415 | 0 |
497 | 0 |
416 | } |
498 | } |
417 | |
499 | |
418 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
500 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
419 | |
501 | |
420 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
421 | |
|
|
422 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
423 | |
|
|
424 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
502 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
425 | |
503 | |
426 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
427 | |
|
|
428 | Generate a global/object/player/map-specific event with the given arguments. |
504 | Generate an object-specific event with the given arguments. |
429 | |
505 | |
430 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
506 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
431 | removed in future versions), and there is no public API to access override |
507 | removed in future versions), and there is no public API to access override |
432 | results (if you must, access C<@cf::invoke_results> directly). |
508 | results (if you must, access C<@cf::invoke_results> directly). |
433 | |
509 | |
434 | =back |
510 | =back |
435 | |
511 | |
436 | =cut |
512 | =cut |
437 | |
513 | |
438 | ############################################################################# |
514 | ############################################################################# |
439 | |
|
|
440 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
441 | |
|
|
442 | Attachable objects includes objects, players, clients and maps. |
|
|
443 | |
|
|
444 | =over 4 |
|
|
445 | |
|
|
446 | =item $object->valid |
|
|
447 | |
|
|
448 | Just because you have a perl object does not mean that the corresponding |
|
|
449 | C-level object still exists. If you try to access an object that has no |
|
|
450 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
451 | can be used to test for existence of the C object part without causing an |
|
|
452 | exception. |
|
|
453 | |
|
|
454 | =back |
|
|
455 | |
|
|
456 | =cut |
|
|
457 | |
|
|
458 | ############################################################################# |
|
|
459 | # object support |
515 | # object support |
460 | |
516 | |
461 | sub instantiate { |
|
|
462 | my ($obj, $data) = @_; |
|
|
463 | |
|
|
464 | $data = from_json $data; |
|
|
465 | |
|
|
466 | for (@$data) { |
|
|
467 | my ($name, $args) = @$_; |
|
|
468 | |
|
|
469 | $obj->attach ($name, %{$args || {} }); |
|
|
470 | } |
|
|
471 | } |
|
|
472 | |
|
|
473 | # basically do the same as instantiate, without calling instantiate |
|
|
474 | sub reattach { |
517 | sub reattach { |
|
|
518 | # basically do the same as instantiate, without calling instantiate |
475 | my ($obj) = @_; |
519 | my ($obj) = @_; |
|
|
520 | |
476 | my $registry = $obj->registry; |
521 | my $registry = $obj->registry; |
477 | |
522 | |
478 | @$registry = (); |
523 | @$registry = (); |
479 | |
524 | |
480 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
525 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
… | |
… | |
489 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
534 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
490 | } |
535 | } |
491 | } |
536 | } |
492 | } |
537 | } |
493 | |
538 | |
|
|
539 | cf::attachable->attach ( |
|
|
540 | prio => -1000000, |
|
|
541 | on_instantiate => sub { |
|
|
542 | my ($obj, $data) = @_; |
|
|
543 | |
|
|
544 | $data = from_json $data; |
|
|
545 | |
|
|
546 | for (@$data) { |
|
|
547 | my ($name, $args) = @$_; |
|
|
548 | |
|
|
549 | $obj->attach ($name, %{$args || {} }); |
|
|
550 | } |
|
|
551 | }, |
|
|
552 | on_reattach => \&reattach, |
|
|
553 | on_clone => sub { |
|
|
554 | my ($src, $dst) = @_; |
|
|
555 | |
|
|
556 | @{$dst->registry} = @{$src->registry}; |
|
|
557 | |
|
|
558 | %$dst = %$src; |
|
|
559 | |
|
|
560 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
561 | if exists $src->{_attachment}; |
|
|
562 | }, |
|
|
563 | ); |
|
|
564 | |
494 | sub object_freezer_save { |
565 | sub object_freezer_save { |
495 | my ($filename, $rdata, $objs) = @_; |
566 | my ($filename, $rdata, $objs) = @_; |
496 | |
567 | |
497 | if (length $$rdata) { |
568 | if (length $$rdata) { |
498 | warn sprintf "saving %s (%d,%d)\n", |
569 | warn sprintf "saving %s (%d,%d)\n", |
… | |
… | |
547 | } |
618 | } |
548 | |
619 | |
549 | () |
620 | () |
550 | } |
621 | } |
551 | |
622 | |
552 | cf::object->attach ( |
|
|
553 | prio => -1000000, |
|
|
554 | on_clone => sub { |
|
|
555 | my ($src, $dst) = @_; |
|
|
556 | |
|
|
557 | @{$dst->registry} = @{$src->registry}; |
|
|
558 | |
|
|
559 | %$dst = %$src; |
|
|
560 | |
|
|
561 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
562 | if exists $src->{_attachment}; |
|
|
563 | }, |
|
|
564 | ); |
|
|
565 | |
|
|
566 | ############################################################################# |
623 | ############################################################################# |
567 | # command handling &c |
624 | # command handling &c |
568 | |
625 | |
569 | =item cf::register_command $name => \&callback($ob,$args); |
626 | =item cf::register_command $name => \&callback($ob,$args); |
570 | |
627 | |
… | |
… | |
895 | |
952 | |
896 | if (@{ $ns->{query_queue} } == @$queue) { |
953 | if (@{ $ns->{query_queue} } == @$queue) { |
897 | if (@$queue) { |
954 | if (@$queue) { |
898 | $ns->send_packet ($ns->{query_queue}[0][0]); |
955 | $ns->send_packet ($ns->{query_queue}[0][0]); |
899 | } else { |
956 | } else { |
900 | $ns->state (ST_PLAYING); |
957 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
901 | } |
958 | } |
902 | } |
959 | } |
|
|
960 | }, |
|
|
961 | ); |
|
|
962 | |
|
|
963 | =item $client->coro (\&cb) |
|
|
964 | |
|
|
965 | Create a new coroutine, running the specified callback. The coroutine will |
|
|
966 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
|
|
967 | or loss of connection). |
|
|
968 | |
|
|
969 | =cut |
|
|
970 | |
|
|
971 | sub cf::client::coro { |
|
|
972 | my ($self, $cb) = @_; |
|
|
973 | |
|
|
974 | my $coro; $coro = async { |
|
|
975 | eval { |
|
|
976 | $cb->(); |
|
|
977 | }; |
|
|
978 | warn $@ if $@; |
|
|
979 | }; |
|
|
980 | |
|
|
981 | $coro->on_destroy (sub { |
|
|
982 | delete $self->{_coro}{$coro+0}; |
|
|
983 | }); |
|
|
984 | |
|
|
985 | $self->{_coro}{$coro+0} = $coro; |
|
|
986 | |
|
|
987 | $coro |
|
|
988 | } |
|
|
989 | |
|
|
990 | cf::client->attach ( |
|
|
991 | on_destroy => sub { |
|
|
992 | my ($ns) = @_; |
|
|
993 | |
|
|
994 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
903 | }, |
995 | }, |
904 | ); |
996 | ); |
905 | |
997 | |
906 | =back |
998 | =back |
907 | |
999 | |
… | |
… | |
1141 | } |
1233 | } |
1142 | |
1234 | |
1143 | ############################################################################# |
1235 | ############################################################################# |
1144 | # initialisation |
1236 | # initialisation |
1145 | |
1237 | |
1146 | sub _perl_reload(&) { |
1238 | sub _perl_reload() { |
1147 | my ($msg) = @_; |
1239 | warn "reloading..."; |
1148 | |
|
|
1149 | $msg->("reloading..."); |
|
|
1150 | |
1240 | |
1151 | eval { |
1241 | eval { |
|
|
1242 | local $FREEZE = 1; |
|
|
1243 | |
|
|
1244 | cf::emergency_save; |
|
|
1245 | |
1152 | # cancel all watchers |
1246 | # cancel all watchers |
1153 | for (Event::all_watchers) { |
1247 | for (Event::all_watchers) { |
1154 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1248 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1155 | } |
1249 | } |
1156 | |
1250 | |
|
|
1251 | # cancel all extension coros |
|
|
1252 | $_->cancel for values %EXT_CORO; |
|
|
1253 | %EXT_CORO = (); |
|
|
1254 | |
1157 | # unload all extensions |
1255 | # unload all extensions |
1158 | for (@exts) { |
1256 | for (@exts) { |
1159 | $msg->("unloading <$_>"); |
1257 | warn "unloading <$_>"; |
1160 | unload_extension $_; |
1258 | unload_extension $_; |
1161 | } |
1259 | } |
1162 | |
1260 | |
1163 | # unload all modules loaded from $LIBDIR |
1261 | # unload all modules loaded from $LIBDIR |
1164 | while (my ($k, $v) = each %INC) { |
1262 | while (my ($k, $v) = each %INC) { |
1165 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1263 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1166 | |
1264 | |
1167 | $msg->("removing <$k>"); |
1265 | warn "removing <$k>"; |
1168 | delete $INC{$k}; |
1266 | delete $INC{$k}; |
1169 | |
1267 | |
1170 | $k =~ s/\.pm$//; |
1268 | $k =~ s/\.pm$//; |
1171 | $k =~ s/\//::/g; |
1269 | $k =~ s/\//::/g; |
1172 | |
1270 | |
… | |
… | |
1177 | Symbol::delete_package $k; |
1275 | Symbol::delete_package $k; |
1178 | } |
1276 | } |
1179 | |
1277 | |
1180 | # sync database to disk |
1278 | # sync database to disk |
1181 | cf::db_sync; |
1279 | cf::db_sync; |
|
|
1280 | IO::AIO::flush; |
1182 | |
1281 | |
1183 | # get rid of safe::, as good as possible |
1282 | # get rid of safe::, as good as possible |
1184 | Symbol::delete_package "safe::$_" |
1283 | Symbol::delete_package "safe::$_" |
1185 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
1284 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
1186 | |
1285 | |
1187 | # remove register_script_function callbacks |
1286 | # remove register_script_function callbacks |
1188 | # TODO |
1287 | # TODO |
1189 | |
1288 | |
1190 | # unload cf.pm "a bit" |
1289 | # unload cf.pm "a bit" |
… | |
… | |
1193 | # don't, removes xs symbols, too, |
1292 | # don't, removes xs symbols, too, |
1194 | # and global variables created in xs |
1293 | # and global variables created in xs |
1195 | #Symbol::delete_package __PACKAGE__; |
1294 | #Symbol::delete_package __PACKAGE__; |
1196 | |
1295 | |
1197 | # reload cf.pm |
1296 | # reload cf.pm |
1198 | $msg->("reloading cf.pm"); |
1297 | warn "reloading cf.pm"; |
1199 | require cf; |
1298 | require cf; |
|
|
1299 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
1200 | |
1300 | |
1201 | # load config and database again |
1301 | # load config and database again |
1202 | cf::cfg_load; |
1302 | cf::cfg_load; |
1203 | cf::db_load; |
1303 | cf::db_load; |
1204 | |
1304 | |
1205 | # load extensions |
1305 | # load extensions |
1206 | $msg->("load extensions"); |
1306 | warn "load extensions"; |
1207 | cf::load_extensions; |
1307 | cf::load_extensions; |
1208 | |
1308 | |
1209 | # reattach attachments to objects |
1309 | # reattach attachments to objects |
1210 | $msg->("reattach"); |
1310 | warn "reattach"; |
1211 | _global_reattach; |
1311 | _global_reattach; |
1212 | }; |
1312 | }; |
1213 | $msg->($@) if $@; |
1313 | warn $@ if $@; |
1214 | |
1314 | |
1215 | $msg->("reloaded"); |
1315 | warn "reloaded"; |
1216 | }; |
1316 | }; |
1217 | |
1317 | |
1218 | sub perl_reload() { |
1318 | sub perl_reload() { |
1219 | _perl_reload { |
1319 | _perl_reload; |
1220 | warn $_[0]; |
|
|
1221 | print "$_[0]\n"; |
|
|
1222 | }; |
|
|
1223 | } |
1320 | } |
1224 | |
1321 | |
1225 | register "<global>", __PACKAGE__; |
1322 | register "<global>", __PACKAGE__; |
1226 | |
1323 | |
1227 | register_command "perl-reload" => sub { |
1324 | register_command "perl-reload" => sub { |
1228 | my ($who, $arg) = @_; |
1325 | my ($who, $arg) = @_; |
1229 | |
1326 | |
1230 | if ($who->flag (FLAG_WIZ)) { |
1327 | if ($who->flag (FLAG_WIZ)) { |
|
|
1328 | $who->message ("reloading..."); |
1231 | _perl_reload { |
1329 | _perl_reload; |
1232 | warn $_[0]; |
|
|
1233 | $who->message ($_[0]); |
|
|
1234 | }; |
|
|
1235 | } |
1330 | } |
1236 | }; |
1331 | }; |
1237 | |
1332 | |
1238 | unshift @INC, $LIBDIR; |
1333 | unshift @INC, $LIBDIR; |
1239 | |
1334 | |
1240 | $TICK_WATCHER = Event->timer ( |
1335 | $TICK_WATCHER = Event->timer ( |
|
|
1336 | reentrant => 0, |
1241 | prio => 0, |
1337 | prio => 0, |
1242 | at => $NEXT_TICK || 1, |
1338 | at => $NEXT_TICK || $TICK, |
1243 | data => WF_AUTOCANCEL, |
1339 | data => WF_AUTOCANCEL, |
1244 | cb => sub { |
1340 | cb => sub { |
|
|
1341 | unless ($FREEZE) { |
1245 | cf::server_tick; # one server iteration |
1342 | cf::server_tick; # one server iteration |
|
|
1343 | $RUNTIME += $TICK; |
|
|
1344 | } |
1246 | |
1345 | |
1247 | my $NOW = Event::time; |
|
|
1248 | $NEXT_TICK += $TICK; |
1346 | $NEXT_TICK += $TICK; |
1249 | |
1347 | |
1250 | # if we are delayed by four ticks or more, skip them all |
1348 | # if we are delayed by four ticks or more, skip them all |
1251 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1349 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
1252 | |
1350 | |
1253 | $TICK_WATCHER->at ($NEXT_TICK); |
1351 | $TICK_WATCHER->at ($NEXT_TICK); |
1254 | $TICK_WATCHER->start; |
1352 | $TICK_WATCHER->start; |
1255 | }, |
1353 | }, |
1256 | ); |
1354 | ); |
… | |
… | |
1261 | poll => 'r', |
1359 | poll => 'r', |
1262 | prio => 5, |
1360 | prio => 5, |
1263 | data => WF_AUTOCANCEL, |
1361 | data => WF_AUTOCANCEL, |
1264 | cb => \&IO::AIO::poll_cb); |
1362 | cb => \&IO::AIO::poll_cb); |
1265 | |
1363 | |
|
|
1364 | # we must not ever block the main coroutine |
|
|
1365 | $Coro::idle = sub { |
|
|
1366 | #Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
|
|
1367 | warn "FATAL: Coro::idle was called, major BUG\n"; |
|
|
1368 | (Coro::unblock_sub { |
|
|
1369 | Event::one_event; |
|
|
1370 | })->(); |
|
|
1371 | }; |
|
|
1372 | |
1266 | 1 |
1373 | 1 |
1267 | |
1374 | |