… | |
… | |
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 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
22 | |
|
|
23 | our %COMMAND = (); |
|
|
24 | our %COMMAND_TIME = (); |
|
|
25 | our %EXTCMD = (); |
|
|
26 | |
21 | _init_vars; |
27 | _init_vars; |
22 | |
28 | |
23 | our %COMMAND = (); |
|
|
24 | our @EVENT; |
29 | our @EVENT; |
25 | our $LIBDIR = maps_directory "perl"; |
30 | our $LIBDIR = datadir . "/ext"; |
26 | |
31 | |
27 | our $TICK = MAX_TIME * 1e-6; |
32 | our $TICK = MAX_TIME * 1e-6; |
28 | our $TICK_WATCHER; |
33 | our $TICK_WATCHER; |
29 | our $NEXT_TICK; |
34 | our $NEXT_TICK; |
30 | |
35 | |
31 | our %CFG; |
36 | our %CFG; |
32 | |
37 | |
33 | our $uptime;#d# |
38 | our $UPTIME; $UPTIME ||= time; |
34 | our $UPTIME; |
|
|
35 | $UPTIME ||= $uptime;#d# |
|
|
36 | $UPTIME ||= time; |
|
|
37 | |
39 | |
38 | ############################################################################# |
40 | ############################################################################# |
39 | |
41 | |
40 | =head2 GLOBAL VARIABLES |
42 | =head2 GLOBAL VARIABLES |
41 | |
43 | |
… | |
… | |
76 | |
78 | |
77 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
79 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
78 | |
80 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
81 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
82 | # within the Safe compartment. |
81 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
83 | for my $pkg (qw( |
|
|
84 | cf::object cf::object::player |
|
|
85 | cf::client cf::player |
|
|
86 | cf::arch cf::living |
|
|
87 | cf::map cf::party cf::region |
|
|
88 | )) { |
82 | no strict 'refs'; |
89 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
90 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
91 | } |
85 | |
92 | |
86 | $Event::DIED = sub { |
93 | $Event::DIED = sub { |
… | |
… | |
88 | }; |
95 | }; |
89 | |
96 | |
90 | my %ext_pkg; |
97 | my %ext_pkg; |
91 | my @exts; |
98 | my @exts; |
92 | my @hook; |
99 | my @hook; |
93 | my %command; |
|
|
94 | my %extcmd; |
|
|
95 | |
100 | |
96 | =head2 UTILITY FUNCTIONS |
101 | =head2 UTILITY FUNCTIONS |
97 | |
102 | |
98 | =over 4 |
103 | =over 4 |
99 | |
104 | |
… | |
… | |
570 | if exists $src->{_attachment}; |
575 | if exists $src->{_attachment}; |
571 | }, |
576 | }, |
572 | ; |
577 | ; |
573 | |
578 | |
574 | ############################################################################# |
579 | ############################################################################# |
575 | # old plug-in events |
580 | # command handling &c |
576 | |
581 | |
577 | sub inject_event { |
582 | =item cf::register_command $name => \&callback($ob,$args); |
578 | my $extension = shift; |
|
|
579 | my $event_code = shift; |
|
|
580 | |
583 | |
581 | my $cb = $hook[$event_code]{$extension} |
584 | Register a callback for execution when the client sends the user command |
582 | or return; |
585 | $name. |
583 | |
586 | |
584 | &$cb |
587 | =cut |
585 | } |
|
|
586 | |
|
|
587 | sub inject_global_event { |
|
|
588 | my $event = shift; |
|
|
589 | |
|
|
590 | my $cb = $hook[$event] |
|
|
591 | or return; |
|
|
592 | |
|
|
593 | List::Util::max map &$_, values %$cb |
|
|
594 | } |
|
|
595 | |
|
|
596 | sub inject_command { |
|
|
597 | my ($name, $obj, $params) = @_; |
|
|
598 | |
|
|
599 | for my $cmd (@{ $command{$name} }) { |
|
|
600 | $cmd->[1]->($obj, $params); |
|
|
601 | } |
|
|
602 | |
|
|
603 | -1 |
|
|
604 | } |
|
|
605 | |
588 | |
606 | sub register_command { |
589 | sub register_command { |
607 | my ($name, $time, $cb) = @_; |
590 | my ($name, $cb) = @_; |
608 | |
591 | |
609 | my $caller = caller; |
592 | my $caller = caller; |
610 | #warn "registering command '$name/$time' to '$caller'"; |
593 | #warn "registering command '$name/$time' to '$caller'"; |
611 | |
594 | |
612 | push @{ $command{$name} }, [$time, $cb, $caller]; |
595 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
613 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
|
|
614 | } |
596 | } |
|
|
597 | |
|
|
598 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
|
|
599 | |
|
|
600 | Register a callbackf ro execution when the client sends an extcmd packet. |
|
|
601 | |
|
|
602 | If the callback returns something, it is sent back as if reply was being |
|
|
603 | called. |
|
|
604 | |
|
|
605 | =cut |
615 | |
606 | |
616 | sub register_extcmd { |
607 | sub register_extcmd { |
617 | my ($name, $cb) = @_; |
608 | my ($name, $cb) = @_; |
618 | |
609 | |
619 | my $caller = caller; |
610 | my $caller = caller; |
620 | #warn "registering extcmd '$name' to '$caller'"; |
611 | #warn "registering extcmd '$name' to '$caller'"; |
621 | |
612 | |
622 | $extcmd{$name} = [$cb, $caller]; |
613 | $EXTCMD{$name} = [$cb, $caller]; |
623 | } |
614 | } |
|
|
615 | |
|
|
616 | attach_to_players |
|
|
617 | on_command => sub { |
|
|
618 | my ($pl, $name, $params) = @_; |
|
|
619 | |
|
|
620 | my $cb = $COMMAND{$name} |
|
|
621 | or return; |
|
|
622 | |
|
|
623 | for my $cmd (@$cb) { |
|
|
624 | $cmd->[1]->($pl->ob, $params); |
|
|
625 | } |
|
|
626 | |
|
|
627 | cf::override; |
|
|
628 | }, |
|
|
629 | on_extcmd => sub { |
|
|
630 | my ($pl, $buf) = @_; |
|
|
631 | |
|
|
632 | my $msg = eval { from_json $buf }; |
|
|
633 | |
|
|
634 | if (ref $msg) { |
|
|
635 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
|
|
636 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
637 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
638 | } |
|
|
639 | } |
|
|
640 | } else { |
|
|
641 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
642 | } |
|
|
643 | |
|
|
644 | cf::override; |
|
|
645 | }, |
|
|
646 | ; |
624 | |
647 | |
625 | sub register { |
648 | sub register { |
626 | my ($base, $pkg) = @_; |
649 | my ($base, $pkg) = @_; |
627 | |
650 | |
628 | #TODO |
651 | #TODO |
… | |
… | |
671 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
694 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
672 | # delete $hook[$idx]{$pkg}; |
695 | # delete $hook[$idx]{$pkg}; |
673 | # } |
696 | # } |
674 | |
697 | |
675 | # remove commands |
698 | # remove commands |
676 | for my $name (keys %command) { |
699 | for my $name (keys %COMMAND) { |
677 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
700 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
678 | |
701 | |
679 | if (@cb) { |
702 | if (@cb) { |
680 | $command{$name} = \@cb; |
703 | $COMMAND{$name} = \@cb; |
681 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
682 | } else { |
704 | } else { |
683 | delete $command{$name}; |
|
|
684 | delete $COMMAND{"$name\000"}; |
705 | delete $COMMAND{$name}; |
685 | } |
706 | } |
686 | } |
707 | } |
687 | |
708 | |
688 | # remove extcmds |
709 | # remove extcmds |
689 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
710 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
690 | delete $extcmd{$name}; |
711 | delete $EXTCMD{$name}; |
691 | } |
712 | } |
692 | |
713 | |
693 | if (my $cb = $pkg->can ("unload")) { |
714 | if (my $cb = $pkg->can ("unload")) { |
694 | eval { |
715 | eval { |
695 | $cb->($pkg); |
716 | $cb->($pkg); |
… | |
… | |
699 | |
720 | |
700 | Symbol::delete_package $pkg; |
721 | Symbol::delete_package $pkg; |
701 | } |
722 | } |
702 | |
723 | |
703 | sub load_extensions { |
724 | sub load_extensions { |
704 | my $LIBDIR = maps_directory "perl"; |
|
|
705 | |
|
|
706 | for my $ext (<$LIBDIR/*.ext>) { |
725 | for my $ext (<$LIBDIR/*.ext>) { |
707 | next unless -r $ext; |
726 | next unless -r $ext; |
708 | eval { |
727 | eval { |
709 | load_extension $ext; |
728 | load_extension $ext; |
710 | 1 |
729 | 1 |
711 | } or warn "$ext not loaded: $@"; |
730 | } or warn "$ext not loaded: $@"; |
712 | } |
731 | } |
713 | } |
732 | } |
714 | |
733 | |
715 | ############################################################################# |
734 | ############################################################################# |
716 | # extcmd framework, basically convert ext <msg> |
|
|
717 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
718 | |
|
|
719 | attach_to_players |
|
|
720 | on_extcmd => sub { |
|
|
721 | my ($pl, $buf) = @_; |
|
|
722 | |
|
|
723 | my $msg = eval { from_json $buf }; |
|
|
724 | |
|
|
725 | if (ref $msg) { |
|
|
726 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
727 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
728 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
729 | } |
|
|
730 | } |
|
|
731 | } else { |
|
|
732 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
733 | } |
|
|
734 | |
|
|
735 | cf::override; |
|
|
736 | }, |
|
|
737 | ; |
|
|
738 | |
|
|
739 | ############################################################################# |
|
|
740 | # load/save/clean perl data associated with a map |
735 | # load/save/clean perl data associated with a map |
741 | |
736 | |
742 | *cf::mapsupport::on_clean = sub { |
737 | *cf::mapsupport::on_clean = sub { |
743 | my ($map) = @_; |
738 | my ($map) = @_; |
744 | |
739 | |
… | |
… | |
869 | |
864 | |
870 | =pod |
865 | =pod |
871 | |
866 | |
872 | The following fucntions and emthods are available within a safe environment: |
867 | The following fucntions and emthods are available within a safe environment: |
873 | |
868 | |
874 | cf::object contr pay_amount pay_player |
869 | cf::object contr pay_amount pay_player map |
875 | cf::object::player player |
870 | cf::object::player player |
876 | cf::player peaceful |
871 | cf::player peaceful |
|
|
872 | cf::map trigger |
877 | |
873 | |
878 | =cut |
874 | =cut |
879 | |
875 | |
880 | for ( |
876 | for ( |
881 | ["cf::object" => qw(contr pay_amount pay_player)], |
877 | ["cf::object" => qw(contr pay_amount pay_player map)], |
882 | ["cf::object::player" => qw(player)], |
878 | ["cf::object::player" => qw(player)], |
883 | ["cf::player" => qw(peaceful)], |
879 | ["cf::player" => qw(peaceful)], |
|
|
880 | ["cf::map" => qw(trigger)], |
884 | ) { |
881 | ) { |
885 | no strict 'refs'; |
882 | no strict 'refs'; |
886 | my ($pkg, @funs) = @$_; |
883 | my ($pkg, @funs) = @$_; |
887 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
884 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
888 | for @funs; |
885 | for @funs; |
… | |
… | |
1028 | sub db_sync() { |
1025 | sub db_sync() { |
1029 | db_save if $dirty; |
1026 | db_save if $dirty; |
1030 | undef $dirty; |
1027 | undef $dirty; |
1031 | } |
1028 | } |
1032 | |
1029 | |
1033 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
1030 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
1034 | db_sync; |
1031 | db_sync; |
1035 | }); |
1032 | }); |
1036 | |
1033 | |
1037 | sub db_dirty() { |
1034 | sub db_dirty() { |
1038 | $dirty = 1; |
1035 | $dirty = 1; |
… | |
… | |
1088 | |
1085 | |
1089 | $msg->("reloading..."); |
1086 | $msg->("reloading..."); |
1090 | |
1087 | |
1091 | eval { |
1088 | eval { |
1092 | # cancel all watchers |
1089 | # cancel all watchers |
1093 | $_->cancel for Event::all_watchers; |
1090 | for (Event::all_watchers) { |
|
|
1091 | $_->cancel if $_->data & WF_AUTOCANCEL; |
|
|
1092 | } |
1094 | |
1093 | |
1095 | # unload all extensions |
1094 | # unload all extensions |
1096 | for (@exts) { |
1095 | for (@exts) { |
1097 | $msg->("unloading <$_>"); |
1096 | $msg->("unloading <$_>"); |
1098 | unload_extension $_; |
1097 | unload_extension $_; |
… | |
… | |
1158 | warn $_[0]; |
1157 | warn $_[0]; |
1159 | print "$_[0]\n"; |
1158 | print "$_[0]\n"; |
1160 | }; |
1159 | }; |
1161 | } |
1160 | } |
1162 | |
1161 | |
|
|
1162 | register "<global>", __PACKAGE__; |
|
|
1163 | |
1163 | register_command "perl-reload", 0, sub { |
1164 | register_command "perl-reload" => sub { |
1164 | my ($who, $arg) = @_; |
1165 | my ($who, $arg) = @_; |
1165 | |
1166 | |
1166 | if ($who->flag (FLAG_WIZ)) { |
1167 | if ($who->flag (FLAG_WIZ)) { |
1167 | _perl_reload { |
1168 | _perl_reload { |
1168 | warn $_[0]; |
1169 | warn $_[0]; |
1169 | $who->message ($_[0]); |
1170 | $who->message ($_[0]); |
1170 | }; |
1171 | }; |
1171 | } |
1172 | } |
1172 | }; |
1173 | }; |
1173 | |
1174 | |
1174 | register "<global>", __PACKAGE__; |
|
|
1175 | |
|
|
1176 | unshift @INC, $LIBDIR; |
1175 | unshift @INC, $LIBDIR; |
1177 | |
1176 | |
1178 | $TICK_WATCHER = Event->timer ( |
1177 | $TICK_WATCHER = Event->timer ( |
1179 | prio => 1, |
1178 | prio => 0, |
1180 | async => 1, |
|
|
1181 | at => $NEXT_TICK || 1, |
1179 | at => $NEXT_TICK || 1, |
|
|
1180 | data => WF_AUTOCANCEL, |
1182 | cb => sub { |
1181 | cb => sub { |
1183 | cf::server_tick; # one server iteration |
1182 | cf::server_tick; # one server iteration |
1184 | |
1183 | |
1185 | my $NOW = Event::time; |
1184 | my $NOW = Event::time; |
1186 | $NEXT_TICK += $TICK; |
1185 | $NEXT_TICK += $TICK; |
… | |
… | |
1196 | IO::AIO::max_poll_time $TICK * 0.2; |
1195 | IO::AIO::max_poll_time $TICK * 0.2; |
1197 | |
1196 | |
1198 | Event->io (fd => IO::AIO::poll_fileno, |
1197 | Event->io (fd => IO::AIO::poll_fileno, |
1199 | poll => 'r', |
1198 | poll => 'r', |
1200 | prio => 5, |
1199 | prio => 5, |
|
|
1200 | data => WF_AUTOCANCEL, |
1201 | cb => \&IO::AIO::poll_cb); |
1201 | cb => \&IO::AIO::poll_cb); |
1202 | |
1202 | |
1203 | 1 |
1203 | 1 |
1204 | |
1204 | |