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; |
|
|
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 (Coro::PRIO_MIN); |
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 | |
|
|
27 | _init_vars; |
|
|
28 | |
35 | |
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; |
… | |
… | |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
91 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
85 | |
92 | |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
93 | # we bless all objects into (empty) derived classes to force a method lookup |
87 | # within the Safe compartment. |
94 | # within the Safe compartment. |
88 | for my $pkg (qw( |
95 | for my $pkg (qw( |
89 | cf::global |
96 | cf::global cf::attachable |
90 | cf::object cf::object::player |
97 | cf::object cf::object::player |
91 | cf::client cf::player |
98 | cf::client cf::player |
92 | cf::arch cf::living |
99 | cf::arch cf::living |
93 | cf::map cf::party cf::region |
100 | cf::map cf::party cf::region |
94 | )) { |
101 | )) { |
… | |
… | |
249 | my ($self, $victim) = @_; |
256 | my ($self, $victim) = @_; |
250 | ... |
257 | ... |
251 | } |
258 | } |
252 | } |
259 | } |
253 | |
260 | |
|
|
261 | =item $attachable->valid |
|
|
262 | |
|
|
263 | Just because you have a perl object does not mean that the corresponding |
|
|
264 | C-level object still exists. If you try to access an object that has no |
|
|
265 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
266 | can be used to test for existence of the C object part without causing an |
|
|
267 | exception. |
|
|
268 | |
254 | =cut |
269 | =cut |
255 | |
270 | |
256 | # the following variables are defined in .xs and must not be re-created |
271 | # the following variables are defined in .xs and must not be re-created |
257 | our @CB_GLOBAL = (); # registry for all global events |
272 | our @CB_GLOBAL = (); # registry for all global events |
|
|
273 | our @CB_ATTACHABLE = (); # registry for all attachables |
258 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
274 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
259 | our @CB_PLAYER = (); |
275 | our @CB_PLAYER = (); |
260 | our @CB_CLIENT = (); |
276 | our @CB_CLIENT = (); |
261 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
277 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
262 | our @CB_MAP = (); |
278 | our @CB_MAP = (); |
263 | |
279 | |
264 | my %attachment; |
280 | my %attachment; |
265 | |
281 | |
266 | sub _attach_cb($$$$) { |
282 | sub _attach_cb($$$$) { |
267 | my ($registry, $event, $prio, $cb) = @_; |
283 | my ($registry, $event, $prio, $cb) = @_; |
… | |
… | |
272 | |
288 | |
273 | @{$registry->[$event]} = sort |
289 | @{$registry->[$event]} = sort |
274 | { $a->[0] cmp $b->[0] } |
290 | { $a->[0] cmp $b->[0] } |
275 | @{$registry->[$event] || []}, $cb; |
291 | @{$registry->[$event] || []}, $cb; |
276 | } |
292 | } |
|
|
293 | |
|
|
294 | # hack |
|
|
295 | my %attachable_klass = map +($_ => 1), KLASS_OBJECT, KLASS_CLIENT, KLASS_PLAYER, KLASS_MAP; |
277 | |
296 | |
278 | # attach handles attaching event callbacks |
297 | # attach handles attaching event callbacks |
279 | # the only thing the caller has to do is pass the correct |
298 | # the only thing the caller has to do is pass the correct |
280 | # registry (== where the callback attaches to). |
299 | # registry (== where the callback attaches to). |
281 | sub _attach { |
300 | sub _attach { |
… | |
… | |
283 | |
302 | |
284 | my $object_type; |
303 | my $object_type; |
285 | my $prio = 0; |
304 | my $prio = 0; |
286 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
305 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
287 | |
306 | |
|
|
307 | #TODO: get rid of this hack |
|
|
308 | if ($attachable_klass{$klass}) { |
|
|
309 | %cb_id = (%cb_id, map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == KLASS_ATTACHABLE, 0 .. $#EVENT); |
|
|
310 | } |
|
|
311 | |
288 | while (@arg) { |
312 | while (@arg) { |
289 | my $type = shift @arg; |
313 | my $type = shift @arg; |
290 | |
314 | |
291 | if ($type eq "prio") { |
315 | if ($type eq "prio") { |
292 | $prio = shift @arg; |
316 | $prio = shift @arg; |
… | |
… | |
367 | my ($obj, $name) = @_; |
391 | my ($obj, $name) = @_; |
368 | |
392 | |
369 | exists $obj->{_attachment}{$name} |
393 | exists $obj->{_attachment}{$name} |
370 | } |
394 | } |
371 | |
395 | |
372 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
396 | for my $klass (qw(ATTACHABLE GLOBAL OBJECT PLAYER CLIENT MAP)) { |
373 | eval "#line " . __LINE__ . " 'cf.pm' |
397 | eval "#line " . __LINE__ . " 'cf.pm' |
374 | sub cf::\L$klass\E::_attach_registry { |
398 | sub cf::\L$klass\E::_attach_registry { |
375 | (\\\@CB_$klass, KLASS_$klass) |
399 | (\\\@CB_$klass, KLASS_$klass) |
376 | } |
400 | } |
377 | |
401 | |
… | |
… | |
413 | } |
437 | } |
414 | |
438 | |
415 | 0 |
439 | 0 |
416 | } |
440 | } |
417 | |
441 | |
418 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
442 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
419 | |
443 | |
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, ...) |
444 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
425 | |
445 | |
426 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
427 | |
|
|
428 | Generate a global/object/player/map-specific event with the given arguments. |
446 | Generate an object-specific event with the given arguments. |
429 | |
447 | |
430 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
448 | 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 |
449 | removed in future versions), and there is no public API to access override |
432 | results (if you must, access C<@cf::invoke_results> directly). |
450 | results (if you must, access C<@cf::invoke_results> directly). |
433 | |
451 | |
434 | =back |
452 | =back |
435 | |
453 | |
436 | =cut |
454 | =cut |
437 | |
455 | |
438 | ############################################################################# |
456 | ############################################################################# |
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 |
457 | # object support |
460 | |
458 | |
461 | sub instantiate { |
459 | cf::attachable->attach ( |
|
|
460 | prio => -1000000, |
|
|
461 | on_instantiate => sub { |
462 | my ($obj, $data) = @_; |
462 | my ($obj, $data) = @_; |
463 | |
463 | |
464 | $data = from_json $data; |
464 | $data = from_json $data; |
465 | |
465 | |
466 | for (@$data) { |
466 | for (@$data) { |
467 | my ($name, $args) = @$_; |
467 | my ($name, $args) = @$_; |
468 | |
468 | |
469 | $obj->attach ($name, %{$args || {} }); |
469 | $obj->attach ($name, %{$args || {} }); |
|
|
470 | } |
470 | } |
471 | }, |
471 | } |
472 | on_reattach => sub { |
472 | |
|
|
473 | # basically do the same as instantiate, without calling instantiate |
473 | # basically do the same as instantiate, without calling instantiate |
474 | sub reattach { |
|
|
475 | my ($obj) = @_; |
474 | my ($obj) = @_; |
476 | my $registry = $obj->registry; |
475 | my $registry = $obj->registry; |
477 | |
476 | |
478 | @$registry = (); |
477 | @$registry = (); |
479 | |
478 | |
480 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
479 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
481 | |
480 | |
482 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
481 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
483 | if (my $attach = $attachment{$name}) { |
482 | if (my $attach = $attachment{$name}) { |
484 | for (@$attach) { |
483 | for (@$attach) { |
485 | my ($klass, @attach) = @$_; |
484 | my ($klass, @attach) = @$_; |
486 | _attach $registry, $klass, @attach; |
485 | _attach $registry, $klass, @attach; |
|
|
486 | } |
|
|
487 | } else { |
|
|
488 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
487 | } |
489 | } |
488 | } else { |
|
|
489 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
490 | } |
490 | } |
491 | } |
491 | }, |
492 | } |
492 | on_clone => sub { |
|
|
493 | my ($src, $dst) = @_; |
|
|
494 | |
|
|
495 | @{$dst->registry} = @{$src->registry}; |
|
|
496 | |
|
|
497 | %$dst = %$src; |
|
|
498 | |
|
|
499 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
500 | if exists $src->{_attachment}; |
|
|
501 | }, |
|
|
502 | ); |
493 | |
503 | |
494 | sub object_freezer_save { |
504 | sub object_freezer_save { |
495 | my ($filename, $rdata, $objs) = @_; |
505 | my ($filename, $rdata, $objs) = @_; |
496 | |
506 | |
497 | if (length $$rdata) { |
507 | if (length $$rdata) { |
… | |
… | |
547 | } |
557 | } |
548 | |
558 | |
549 | () |
559 | () |
550 | } |
560 | } |
551 | |
561 | |
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 | ############################################################################# |
562 | ############################################################################# |
567 | # command handling &c |
563 | # command handling &c |
568 | |
564 | |
569 | =item cf::register_command $name => \&callback($ob,$args); |
565 | =item cf::register_command $name => \&callback($ob,$args); |
570 | |
566 | |
… | |
… | |
895 | |
891 | |
896 | if (@{ $ns->{query_queue} } == @$queue) { |
892 | if (@{ $ns->{query_queue} } == @$queue) { |
897 | if (@$queue) { |
893 | if (@$queue) { |
898 | $ns->send_packet ($ns->{query_queue}[0][0]); |
894 | $ns->send_packet ($ns->{query_queue}[0][0]); |
899 | } else { |
895 | } else { |
900 | $ns->state (ST_PLAYING); |
896 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
901 | } |
897 | } |
902 | } |
898 | } |
|
|
899 | }, |
|
|
900 | ); |
|
|
901 | |
|
|
902 | =item $client->coro (\&cb) |
|
|
903 | |
|
|
904 | Create a new coroutine, running the specified callback. The coroutine will |
|
|
905 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
|
|
906 | or loss of connection). |
|
|
907 | |
|
|
908 | =cut |
|
|
909 | |
|
|
910 | sub cf::client::coro { |
|
|
911 | my ($self, $cb) = @_; |
|
|
912 | |
|
|
913 | my $coro; $coro = async { |
|
|
914 | eval { |
|
|
915 | $cb->(); |
|
|
916 | }; |
|
|
917 | warn $@ if $@; |
|
|
918 | delete $self->{_coro}{$coro+0}; |
|
|
919 | }; |
|
|
920 | |
|
|
921 | $self->{_coro}{$coro+0} = $coro; |
|
|
922 | } |
|
|
923 | |
|
|
924 | cf::client->attach ( |
|
|
925 | on_destroy => sub { |
|
|
926 | my ($ns) = @_; |
|
|
927 | |
|
|
928 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
903 | }, |
929 | }, |
904 | ); |
930 | ); |
905 | |
931 | |
906 | =back |
932 | =back |
907 | |
933 | |
… | |
… | |
1195 | #Symbol::delete_package __PACKAGE__; |
1221 | #Symbol::delete_package __PACKAGE__; |
1196 | |
1222 | |
1197 | # reload cf.pm |
1223 | # reload cf.pm |
1198 | $msg->("reloading cf.pm"); |
1224 | $msg->("reloading cf.pm"); |
1199 | require cf; |
1225 | require cf; |
|
|
1226 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
|
|
1227 | |
1200 | |
1228 | |
1201 | # load config and database again |
1229 | # load config and database again |
1202 | cf::cfg_load; |
1230 | cf::cfg_load; |
1203 | cf::db_load; |
1231 | cf::db_load; |
1204 | |
1232 | |