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.79 by root, Tue Nov 7 14:58:35 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
523 unlink $filename; 526 unlink $filename;
524 unlink "$filename.pst"; 527 unlink "$filename.pst";
525 } 528 }
526} 529}
527 530
531sub object_freezer_as_string {
532 my ($rdata, $objs) = @_;
533
534 use Data::Dumper;
535
536 $$rdata . Dumper $objs
537}
538
528sub object_thawer_load { 539sub object_thawer_load {
529 my ($filename) = @_; 540 my ($filename) = @_;
530 541
531 local $/; 542 local $/;
532 543
557 if exists $src->{_attachment}; 568 if exists $src->{_attachment};
558 }, 569 },
559; 570;
560 571
561############################################################################# 572#############################################################################
562# old plug-in events 573# command handling &c
563 574
564sub inject_event { 575=item cf::register_command $name => \&callback($ob,$args);
565 my $extension = shift;
566 my $event_code = shift;
567 576
568 my $cb = $hook[$event_code]{$extension} 577Register a callback for execution when the client sends the user command
569 or return; 578$name.
570 579
571 &$cb 580=cut
572}
573
574sub inject_global_event {
575 my $event = shift;
576
577 my $cb = $hook[$event]
578 or return;
579
580 List::Util::max map &$_, values %$cb
581}
582
583sub inject_command {
584 my ($name, $obj, $params) = @_;
585
586 for my $cmd (@{ $command{$name} }) {
587 $cmd->[1]->($obj, $params);
588 }
589
590 -1
591}
592 581
593sub register_command { 582sub register_command {
594 my ($name, $time, $cb) = @_; 583 my ($name, $cb) = @_;
595 584
596 my $caller = caller; 585 my $caller = caller;
597 #warn "registering command '$name/$time' to '$caller'"; 586 #warn "registering command '$name/$time' to '$caller'";
598 587
599 push @{ $command{$name} }, [$time, $cb, $caller]; 588 push @{ $COMMAND{$name} }, [$caller, $cb];
600 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
601} 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
602 599
603sub register_extcmd { 600sub register_extcmd {
604 my ($name, $cb) = @_; 601 my ($name, $cb) = @_;
605 602
606 my $caller = caller; 603 my $caller = caller;
607 #warn "registering extcmd '$name' to '$caller'"; 604 #warn "registering extcmd '$name' to '$caller'";
608 605
609 $extcmd{$name} = [$cb, $caller]; 606 $EXTCMD{$name} = [$cb, $caller];
610} 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;
611 640
612sub register { 641sub register {
613 my ($base, $pkg) = @_; 642 my ($base, $pkg) = @_;
614 643
615 #TODO 644 #TODO
634 . "#line 1 \"$path\"\n{\n" 663 . "#line 1 \"$path\"\n{\n"
635 . (do { local $/; <$fh> }) 664 . (do { local $/; <$fh> })
636 . "\n};\n1"; 665 . "\n};\n1";
637 666
638 eval $source 667 eval $source
639 or die "$path: $@"; 668 or die $@ ? "$path: $@\n"
669 : "extension disabled.\n";
640 670
641 push @exts, $pkg; 671 push @exts, $pkg;
642 $ext_pkg{$base} = $pkg; 672 $ext_pkg{$base} = $pkg;
643 673
644# no strict 'refs'; 674# no strict 'refs';
657# for my $idx (0 .. $#PLUGIN_EVENT) { 687# for my $idx (0 .. $#PLUGIN_EVENT) {
658# delete $hook[$idx]{$pkg}; 688# delete $hook[$idx]{$pkg};
659# } 689# }
660 690
661 # remove commands 691 # remove commands
662 for my $name (keys %command) { 692 for my $name (keys %COMMAND) {
663 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 693 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
664 694
665 if (@cb) { 695 if (@cb) {
666 $command{$name} = \@cb; 696 $COMMAND{$name} = \@cb;
667 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
668 } else { 697 } else {
669 delete $command{$name};
670 delete $COMMAND{"$name\000"}; 698 delete $COMMAND{$name};
671 } 699 }
672 } 700 }
673 701
674 # remove extcmds 702 # remove extcmds
675 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 703 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
676 delete $extcmd{$name}; 704 delete $EXTCMD{$name};
677 } 705 }
678 706
679 if (my $cb = $pkg->can ("unload")) { 707 if (my $cb = $pkg->can ("unload")) {
680 eval { 708 eval {
681 $cb->($pkg); 709 $cb->($pkg);
695 load_extension $ext; 723 load_extension $ext;
696 1 724 1
697 } or warn "$ext not loaded: $@"; 725 } or warn "$ext not loaded: $@";
698 } 726 }
699} 727}
700
701#############################################################################
702# extcmd framework, basically convert ext <msg>
703# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
704
705attach_to_players
706 on_extcmd => sub {
707 my ($pl, $buf) = @_;
708
709 my $msg = eval { from_json $buf };
710
711 if (ref $msg) {
712 if (my $cb = $extcmd{$msg->{msgtype}}) {
713 if (my %reply = $cb->[0]->($pl, $msg)) {
714 $pl->ext_reply ($msg->{msgid}, %reply);
715 }
716 }
717 } else {
718 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
719 }
720
721 cf::override;
722 },
723;
724 728
725############################################################################# 729#############################################################################
726# load/save/clean perl data associated with a map 730# load/save/clean perl data associated with a map
727 731
728*cf::mapsupport::on_clean = sub { 732*cf::mapsupport::on_clean = sub {
1144 warn $_[0]; 1148 warn $_[0];
1145 print "$_[0]\n"; 1149 print "$_[0]\n";
1146 }; 1150 };
1147} 1151}
1148 1152
1153register "<global>", __PACKAGE__;
1154
1149register_command "perl-reload", 0, sub { 1155register_command "perl-reload" => sub {
1150 my ($who, $arg) = @_; 1156 my ($who, $arg) = @_;
1151 1157
1152 if ($who->flag (FLAG_WIZ)) { 1158 if ($who->flag (FLAG_WIZ)) {
1153 _perl_reload { 1159 _perl_reload {
1154 warn $_[0]; 1160 warn $_[0];
1155 $who->message ($_[0]); 1161 $who->message ($_[0]);
1156 }; 1162 };
1157 } 1163 }
1158}; 1164};
1159
1160register "<global>", __PACKAGE__;
1161 1165
1162unshift @INC, $LIBDIR; 1166unshift @INC, $LIBDIR;
1163 1167
1164$TICK_WATCHER = Event->timer ( 1168$TICK_WATCHER = Event->timer (
1165 prio => 1, 1169 prio => 1,
1177 $TICK_WATCHER->at ($NEXT_TICK); 1181 $TICK_WATCHER->at ($NEXT_TICK);
1178 $TICK_WATCHER->start; 1182 $TICK_WATCHER->start;
1179 }, 1183 },
1180); 1184);
1181 1185
1182eval { IO::AIO::max_poll_time $TICK * 0.2 }; #d# remove eval after restart 1186IO::AIO::max_poll_time $TICK * 0.2;
1183 1187
1184Event->io (fd => IO::AIO::poll_fileno, 1188Event->io (fd => IO::AIO::poll_fileno,
1185 poll => 'r', 1189 poll => 'r',
1186 prio => 5, 1190 prio => 5,
1187 cb => \&IO::AIO::poll_cb); 1191 cb => \&IO::AIO::poll_cb);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines