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.82 by root, Sun Dec 3 20:59:52 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; 36our $UPTIME; $UPTIME ||= time;
34
35$uptime ||= time;
36 37
37############################################################################# 38#############################################################################
38 39
39=head2 GLOBAL VARIABLES 40=head2 GLOBAL VARIABLES
40 41
41=over 4 42=over 4
43
44=item $cf::UPTIME
45
46The timestamp of the server start (so not actually an uptime).
42 47
43=item $cf::LIBDIR 48=item $cf::LIBDIR
44 49
45The perl library directory, where extensions and cf-specific modules can 50The perl library directory, where extensions and cf-specific modules can
46be found. It will be added to C<@INC> automatically. 51be found. It will be added to C<@INC> automatically.
83}; 88};
84 89
85my %ext_pkg; 90my %ext_pkg;
86my @exts; 91my @exts;
87my @hook; 92my @hook;
88my %command;
89my %extcmd;
90 93
91=head2 UTILITY FUNCTIONS 94=head2 UTILITY FUNCTIONS
92 95
93=over 4 96=over 4
94 97
565 if exists $src->{_attachment}; 568 if exists $src->{_attachment};
566 }, 569 },
567; 570;
568 571
569############################################################################# 572#############################################################################
570# old plug-in events 573# command handling &c
571 574
572sub inject_event { 575=item cf::register_command $name => \&callback($ob,$args);
573 my $extension = shift;
574 my $event_code = shift;
575 576
576 my $cb = $hook[$event_code]{$extension} 577Register a callback for execution when the client sends the user command
577 or return; 578$name.
578 579
579 &$cb 580=cut
580}
581
582sub inject_global_event {
583 my $event = shift;
584
585 my $cb = $hook[$event]
586 or return;
587
588 List::Util::max map &$_, values %$cb
589}
590
591sub inject_command {
592 my ($name, $obj, $params) = @_;
593
594 for my $cmd (@{ $command{$name} }) {
595 $cmd->[1]->($obj, $params);
596 }
597
598 -1
599}
600 581
601sub register_command { 582sub register_command {
602 my ($name, $time, $cb) = @_; 583 my ($name, $cb) = @_;
603 584
604 my $caller = caller; 585 my $caller = caller;
605 #warn "registering command '$name/$time' to '$caller'"; 586 #warn "registering command '$name/$time' to '$caller'";
606 587
607 push @{ $command{$name} }, [$time, $cb, $caller]; 588 push @{ $COMMAND{$name} }, [$caller, $cb];
608 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
609} 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
610 599
611sub register_extcmd { 600sub register_extcmd {
612 my ($name, $cb) = @_; 601 my ($name, $cb) = @_;
613 602
614 my $caller = caller; 603 my $caller = caller;
615 #warn "registering extcmd '$name' to '$caller'"; 604 #warn "registering extcmd '$name' to '$caller'";
616 605
617 $extcmd{$name} = [$cb, $caller]; 606 $EXTCMD{$name} = [$cb, $caller];
618} 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;
619 640
620sub register { 641sub register {
621 my ($base, $pkg) = @_; 642 my ($base, $pkg) = @_;
622 643
623 #TODO 644 #TODO
666# for my $idx (0 .. $#PLUGIN_EVENT) { 687# for my $idx (0 .. $#PLUGIN_EVENT) {
667# delete $hook[$idx]{$pkg}; 688# delete $hook[$idx]{$pkg};
668# } 689# }
669 690
670 # remove commands 691 # remove commands
671 for my $name (keys %command) { 692 for my $name (keys %COMMAND) {
672 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 693 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
673 694
674 if (@cb) { 695 if (@cb) {
675 $command{$name} = \@cb; 696 $COMMAND{$name} = \@cb;
676 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
677 } else { 697 } else {
678 delete $command{$name};
679 delete $COMMAND{"$name\000"}; 698 delete $COMMAND{$name};
680 } 699 }
681 } 700 }
682 701
683 # remove extcmds 702 # remove extcmds
684 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 703 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
685 delete $extcmd{$name}; 704 delete $EXTCMD{$name};
686 } 705 }
687 706
688 if (my $cb = $pkg->can ("unload")) { 707 if (my $cb = $pkg->can ("unload")) {
689 eval { 708 eval {
690 $cb->($pkg); 709 $cb->($pkg);
704 load_extension $ext; 723 load_extension $ext;
705 1 724 1
706 } or warn "$ext not loaded: $@"; 725 } or warn "$ext not loaded: $@";
707 } 726 }
708} 727}
709
710#############################################################################
711# extcmd framework, basically convert ext <msg>
712# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
713
714attach_to_players
715 on_extcmd => sub {
716 my ($pl, $buf) = @_;
717
718 my $msg = eval { from_json $buf };
719
720 if (ref $msg) {
721 if (my $cb = $extcmd{$msg->{msgtype}}) {
722 if (my %reply = $cb->[0]->($pl, $msg)) {
723 $pl->ext_reply ($msg->{msgid}, %reply);
724 }
725 }
726 } else {
727 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
728 }
729
730 cf::override;
731 },
732;
733 728
734############################################################################# 729#############################################################################
735# load/save/clean perl data associated with a map 730# load/save/clean perl data associated with a map
736 731
737*cf::mapsupport::on_clean = sub { 732*cf::mapsupport::on_clean = sub {
1153 warn $_[0]; 1148 warn $_[0];
1154 print "$_[0]\n"; 1149 print "$_[0]\n";
1155 }; 1150 };
1156} 1151}
1157 1152
1153register "<global>", __PACKAGE__;
1154
1158register_command "perl-reload", 0, sub { 1155register_command "perl-reload" => sub {
1159 my ($who, $arg) = @_; 1156 my ($who, $arg) = @_;
1160 1157
1161 if ($who->flag (FLAG_WIZ)) { 1158 if ($who->flag (FLAG_WIZ)) {
1162 _perl_reload { 1159 _perl_reload {
1163 warn $_[0]; 1160 warn $_[0];
1164 $who->message ($_[0]); 1161 $who->message ($_[0]);
1165 }; 1162 };
1166 } 1163 }
1167}; 1164};
1168
1169register "<global>", __PACKAGE__;
1170 1165
1171unshift @INC, $LIBDIR; 1166unshift @INC, $LIBDIR;
1172 1167
1173$TICK_WATCHER = Event->timer ( 1168$TICK_WATCHER = Event->timer (
1174 prio => 1, 1169 prio => 1,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines