… | |
… | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
17 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
18 | |
19 | use strict; |
19 | use strict; |
20 | |
20 | |
|
|
21 | our %COMMAND = (); |
|
|
22 | our %COMMAND_TIME = (); |
|
|
23 | our %EXTCMD = (); |
|
|
24 | |
21 | _init_vars; |
25 | _init_vars; |
22 | |
26 | |
23 | our %COMMAND = (); |
|
|
24 | our @EVENT; |
27 | our @EVENT; |
25 | our $LIBDIR = maps_directory "perl"; |
28 | our $LIBDIR = maps_directory "perl"; |
26 | |
29 | |
27 | our $TICK = MAX_TIME * 1e-6; |
30 | our $TICK = MAX_TIME * 1e-6; |
28 | our $TICK_WATCHER; |
31 | our $TICK_WATCHER; |
… | |
… | |
85 | }; |
88 | }; |
86 | |
89 | |
87 | my %ext_pkg; |
90 | my %ext_pkg; |
88 | my @exts; |
91 | my @exts; |
89 | my @hook; |
92 | my @hook; |
90 | my %command; |
|
|
91 | my %extcmd; |
|
|
92 | |
93 | |
93 | =head2 UTILITY FUNCTIONS |
94 | =head2 UTILITY FUNCTIONS |
94 | |
95 | |
95 | =over 4 |
96 | =over 4 |
96 | |
97 | |
… | |
… | |
567 | if exists $src->{_attachment}; |
568 | if exists $src->{_attachment}; |
568 | }, |
569 | }, |
569 | ; |
570 | ; |
570 | |
571 | |
571 | ############################################################################# |
572 | ############################################################################# |
572 | # old plug-in events |
573 | # command handling &c |
573 | |
574 | |
574 | sub inject_event { |
575 | =item cf::register_command $name => \&callback($ob,$args); |
575 | my $extension = shift; |
|
|
576 | my $event_code = shift; |
|
|
577 | |
576 | |
578 | my $cb = $hook[$event_code]{$extension} |
577 | Register a callback for execution when the client sends the user command |
579 | or return; |
578 | $name. |
580 | |
579 | |
581 | &$cb |
580 | =cut |
582 | } |
|
|
583 | |
|
|
584 | sub inject_global_event { |
|
|
585 | my $event = shift; |
|
|
586 | |
|
|
587 | my $cb = $hook[$event] |
|
|
588 | or return; |
|
|
589 | |
|
|
590 | List::Util::max map &$_, values %$cb |
|
|
591 | } |
|
|
592 | |
|
|
593 | sub inject_command { |
|
|
594 | my ($name, $obj, $params) = @_; |
|
|
595 | |
|
|
596 | for my $cmd (@{ $command{$name} }) { |
|
|
597 | $cmd->[1]->($obj, $params); |
|
|
598 | } |
|
|
599 | |
|
|
600 | -1 |
|
|
601 | } |
|
|
602 | |
581 | |
603 | sub register_command { |
582 | sub register_command { |
604 | my ($name, $time, $cb) = @_; |
583 | my ($name, $cb) = @_; |
605 | |
584 | |
606 | my $caller = caller; |
585 | my $caller = caller; |
607 | #warn "registering command '$name/$time' to '$caller'"; |
586 | #warn "registering command '$name/$time' to '$caller'"; |
608 | |
587 | |
609 | push @{ $command{$name} }, [$time, $cb, $caller]; |
588 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
610 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
|
|
611 | } |
589 | } |
|
|
590 | |
|
|
591 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
|
|
592 | |
|
|
593 | Register a callbackf ro execution when the client sends an extcmd packet. |
|
|
594 | |
|
|
595 | If the callback returns something, it is sent back as if reply was being |
|
|
596 | called. |
|
|
597 | |
|
|
598 | =cut |
612 | |
599 | |
613 | sub register_extcmd { |
600 | sub register_extcmd { |
614 | my ($name, $cb) = @_; |
601 | my ($name, $cb) = @_; |
615 | |
602 | |
616 | my $caller = caller; |
603 | my $caller = caller; |
617 | #warn "registering extcmd '$name' to '$caller'"; |
604 | #warn "registering extcmd '$name' to '$caller'"; |
618 | |
605 | |
619 | $extcmd{$name} = [$cb, $caller]; |
606 | $EXTCMD{$name} = [$cb, $caller]; |
620 | } |
607 | } |
|
|
608 | |
|
|
609 | attach_to_players |
|
|
610 | on_command => sub { |
|
|
611 | my ($pl, $name, $params) = @_; |
|
|
612 | |
|
|
613 | my $cb = $COMMAND{$name} |
|
|
614 | or return; |
|
|
615 | |
|
|
616 | for my $cmd (@$cb) { |
|
|
617 | $cmd->[1]->($pl->ob, $params); |
|
|
618 | } |
|
|
619 | |
|
|
620 | cf::override; |
|
|
621 | }, |
|
|
622 | on_extcmd => sub { |
|
|
623 | my ($pl, $buf) = @_; |
|
|
624 | |
|
|
625 | my $msg = eval { from_json $buf }; |
|
|
626 | |
|
|
627 | if (ref $msg) { |
|
|
628 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
|
|
629 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
630 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
631 | } |
|
|
632 | } |
|
|
633 | } else { |
|
|
634 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
635 | } |
|
|
636 | |
|
|
637 | cf::override; |
|
|
638 | }, |
|
|
639 | ; |
621 | |
640 | |
622 | sub register { |
641 | sub register { |
623 | my ($base, $pkg) = @_; |
642 | my ($base, $pkg) = @_; |
624 | |
643 | |
625 | #TODO |
644 | #TODO |
… | |
… | |
668 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
687 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
669 | # delete $hook[$idx]{$pkg}; |
688 | # delete $hook[$idx]{$pkg}; |
670 | # } |
689 | # } |
671 | |
690 | |
672 | # remove commands |
691 | # remove commands |
673 | for my $name (keys %command) { |
692 | for my $name (keys %COMMAND) { |
674 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
693 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
675 | |
694 | |
676 | if (@cb) { |
695 | if (@cb) { |
677 | $command{$name} = \@cb; |
696 | $COMMAND{$name} = \@cb; |
678 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
679 | } else { |
697 | } else { |
680 | delete $command{$name}; |
|
|
681 | delete $COMMAND{"$name\000"}; |
698 | delete $COMMAND{$name}; |
682 | } |
699 | } |
683 | } |
700 | } |
684 | |
701 | |
685 | # remove extcmds |
702 | # remove extcmds |
686 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
703 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
687 | delete $extcmd{$name}; |
704 | delete $EXTCMD{$name}; |
688 | } |
705 | } |
689 | |
706 | |
690 | if (my $cb = $pkg->can ("unload")) { |
707 | if (my $cb = $pkg->can ("unload")) { |
691 | eval { |
708 | eval { |
692 | $cb->($pkg); |
709 | $cb->($pkg); |
… | |
… | |
706 | load_extension $ext; |
723 | load_extension $ext; |
707 | 1 |
724 | 1 |
708 | } or warn "$ext not loaded: $@"; |
725 | } or warn "$ext not loaded: $@"; |
709 | } |
726 | } |
710 | } |
727 | } |
711 | |
|
|
712 | ############################################################################# |
|
|
713 | # extcmd framework, basically convert ext <msg> |
|
|
714 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
715 | |
|
|
716 | attach_to_players |
|
|
717 | on_extcmd => sub { |
|
|
718 | my ($pl, $buf) = @_; |
|
|
719 | |
|
|
720 | my $msg = eval { from_json $buf }; |
|
|
721 | |
|
|
722 | if (ref $msg) { |
|
|
723 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
724 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
725 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
726 | } |
|
|
727 | } |
|
|
728 | } else { |
|
|
729 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
730 | } |
|
|
731 | |
|
|
732 | cf::override; |
|
|
733 | }, |
|
|
734 | ; |
|
|
735 | |
728 | |
736 | ############################################################################# |
729 | ############################################################################# |
737 | # load/save/clean perl data associated with a map |
730 | # load/save/clean perl data associated with a map |
738 | |
731 | |
739 | *cf::mapsupport::on_clean = sub { |
732 | *cf::mapsupport::on_clean = sub { |
… | |
… | |
1155 | warn $_[0]; |
1148 | warn $_[0]; |
1156 | print "$_[0]\n"; |
1149 | print "$_[0]\n"; |
1157 | }; |
1150 | }; |
1158 | } |
1151 | } |
1159 | |
1152 | |
|
|
1153 | register "<global>", __PACKAGE__; |
|
|
1154 | |
1160 | register_command "perl-reload", 0, sub { |
1155 | register_command "perl-reload" => sub { |
1161 | my ($who, $arg) = @_; |
1156 | my ($who, $arg) = @_; |
1162 | |
1157 | |
1163 | if ($who->flag (FLAG_WIZ)) { |
1158 | if ($who->flag (FLAG_WIZ)) { |
1164 | _perl_reload { |
1159 | _perl_reload { |
1165 | warn $_[0]; |
1160 | warn $_[0]; |
1166 | $who->message ($_[0]); |
1161 | $who->message ($_[0]); |
1167 | }; |
1162 | }; |
1168 | } |
1163 | } |
1169 | }; |
1164 | }; |
1170 | |
|
|
1171 | register "<global>", __PACKAGE__; |
|
|
1172 | |
1165 | |
1173 | unshift @INC, $LIBDIR; |
1166 | unshift @INC, $LIBDIR; |
1174 | |
1167 | |
1175 | $TICK_WATCHER = Event->timer ( |
1168 | $TICK_WATCHER = Event->timer ( |
1176 | prio => 1, |
1169 | prio => 1, |