ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.84 by root, Mon Dec 11 02:54:57 2006 UTC vs.
Revision 1.86 by root, Thu Dec 14 05:09:32 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines