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.83 by root, Mon Dec 11 01:30:41 2006 UTC vs.
Revision 1.85 by root, Mon Dec 11 22:56:57 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;
29our $NEXT_TICK; 32our $NEXT_TICK;
30 33
31our %CFG; 34our %CFG;
32 35
33our $uptime;#d# 36our $UPTIME; $UPTIME ||= time;
34our $UPTIME;
35$UPTIME ||= $uptime;#d#
36$UPTIME ||= time;
37 37
38############################################################################# 38#############################################################################
39 39
40=head2 GLOBAL VARIABLES 40=head2 GLOBAL VARIABLES
41 41
88}; 88};
89 89
90my %ext_pkg; 90my %ext_pkg;
91my @exts; 91my @exts;
92my @hook; 92my @hook;
93my %command;
94my %extcmd;
95 93
96=head2 UTILITY FUNCTIONS 94=head2 UTILITY FUNCTIONS
97 95
98=over 4 96=over 4
99 97
570 if exists $src->{_attachment}; 568 if exists $src->{_attachment};
571 }, 569 },
572; 570;
573 571
574############################################################################# 572#############################################################################
575# old plug-in events 573# command handling &c
576 574
577sub inject_event { 575=item cf::register_command $name => \&callback($ob,$args);
578 my $extension = shift;
579 my $event_code = shift;
580 576
581 my $cb = $hook[$event_code]{$extension} 577Register a callback for execution when the client sends the user command
582 or return; 578$name.
583 579
584 &$cb 580=cut
585}
586
587sub 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
596sub 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 581
606sub register_command { 582sub register_command {
607 my ($name, $time, $cb) = @_; 583 my ($name, $cb) = @_;
608 584
609 my $caller = caller; 585 my $caller = caller;
610 #warn "registering command '$name/$time' to '$caller'"; 586 #warn "registering command '$name/$time' to '$caller'";
611 587
612 push @{ $command{$name} }, [$time, $cb, $caller]; 588 push @{ $COMMAND{$name} }, [$caller, $cb];
613 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
614} 589}
590
591=item cf::register_extcmd $name => \&callback($pl,$packet);
592
593Register a callbackf ro execution when the client sends an extcmd packet.
594
595If the callback returns something, it is sent back as if reply was being
596called.
597
598=cut
615 599
616sub register_extcmd { 600sub register_extcmd {
617 my ($name, $cb) = @_; 601 my ($name, $cb) = @_;
618 602
619 my $caller = caller; 603 my $caller = caller;
620 #warn "registering extcmd '$name' to '$caller'"; 604 #warn "registering extcmd '$name' to '$caller'";
621 605
622 $extcmd{$name} = [$cb, $caller]; 606 $EXTCMD{$name} = [$cb, $caller];
623} 607}
608
609attach_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;
624 640
625sub register { 641sub register {
626 my ($base, $pkg) = @_; 642 my ($base, $pkg) = @_;
627 643
628 #TODO 644 #TODO
671# for my $idx (0 .. $#PLUGIN_EVENT) { 687# for my $idx (0 .. $#PLUGIN_EVENT) {
672# delete $hook[$idx]{$pkg}; 688# delete $hook[$idx]{$pkg};
673# } 689# }
674 690
675 # remove commands 691 # remove commands
676 for my $name (keys %command) { 692 for my $name (keys %COMMAND) {
677 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 693 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
678 694
679 if (@cb) { 695 if (@cb) {
680 $command{$name} = \@cb; 696 $COMMAND{$name} = \@cb;
681 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
682 } else { 697 } else {
683 delete $command{$name};
684 delete $COMMAND{"$name\000"}; 698 delete $COMMAND{$name};
685 } 699 }
686 } 700 }
687 701
688 # remove extcmds 702 # remove extcmds
689 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 703 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
690 delete $extcmd{$name}; 704 delete $EXTCMD{$name};
691 } 705 }
692 706
693 if (my $cb = $pkg->can ("unload")) { 707 if (my $cb = $pkg->can ("unload")) {
694 eval { 708 eval {
695 $cb->($pkg); 709 $cb->($pkg);
709 load_extension $ext; 723 load_extension $ext;
710 1 724 1
711 } or warn "$ext not loaded: $@"; 725 } or warn "$ext not loaded: $@";
712 } 726 }
713} 727}
714
715#############################################################################
716# extcmd framework, basically convert ext <msg>
717# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
718
719attach_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 728
739############################################################################# 729#############################################################################
740# load/save/clean perl data associated with a map 730# load/save/clean perl data associated with a map
741 731
742*cf::mapsupport::on_clean = sub { 732*cf::mapsupport::on_clean = sub {
1158 warn $_[0]; 1148 warn $_[0];
1159 print "$_[0]\n"; 1149 print "$_[0]\n";
1160 }; 1150 };
1161} 1151}
1162 1152
1153register "<global>", __PACKAGE__;
1154
1163register_command "perl-reload", 0, sub { 1155register_command "perl-reload" => sub {
1164 my ($who, $arg) = @_; 1156 my ($who, $arg) = @_;
1165 1157
1166 if ($who->flag (FLAG_WIZ)) { 1158 if ($who->flag (FLAG_WIZ)) {
1167 _perl_reload { 1159 _perl_reload {
1168 warn $_[0]; 1160 warn $_[0];
1169 $who->message ($_[0]); 1161 $who->message ($_[0]);
1170 }; 1162 };
1171 } 1163 }
1172}; 1164};
1173
1174register "<global>", __PACKAGE__;
1175 1165
1176unshift @INC, $LIBDIR; 1166unshift @INC, $LIBDIR;
1177 1167
1178$TICK_WATCHER = Event->timer ( 1168$TICK_WATCHER = Event->timer (
1179 prio => 1, 1169 prio => 1,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines