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.92 by root, Thu Dec 21 06:42:28 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
21sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
22
23our %COMMAND = ();
24our %COMMAND_TIME = ();
25our %EXTCMD = ();
26
21_init_vars; 27_init_vars;
22 28
23our %COMMAND = ();
24our @EVENT; 29our @EVENT;
25our $LIBDIR = maps_directory "perl"; 30our $LIBDIR = datadir . "/ext";
26 31
27our $TICK = MAX_TIME * 1e-6; 32our $TICK = MAX_TIME * 1e-6;
28our $TICK_WATCHER; 33our $TICK_WATCHER;
29our $NEXT_TICK; 34our $NEXT_TICK;
30 35
31our %CFG; 36our %CFG;
32 37
33our $uptime;#d# 38our $UPTIME; $UPTIME ||= time;
34our $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.
81for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { 83for 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
90my %ext_pkg; 97my %ext_pkg;
91my @exts; 98my @exts;
92my @hook; 99my @hook;
93my %command;
94my %extcmd;
95 100
96=head2 UTILITY FUNCTIONS 101=head2 UTILITY FUNCTIONS
97 102
98=over 4 103=over 4
99 104
147 152
148=item $map->attach ($attachment, key => $value...) 153=item $map->attach ($attachment, key => $value...)
149 154
150=item $map->detach ($attachment) 155=item $map->detach ($attachment)
151 156
157Attach/detach a pre-registered attachment to a client.
158
159=item $client->attach ($attachment, key => $value...)
160
161=item $client->detach ($attachment)
162
152Attach/detach a pre-registered attachment to a map. 163Attach/detach a pre-registered attachment to a map.
153 164
154=item $bool = $object->attached ($name) 165=item $bool = $object->attached ($name)
155 166
156=item $bool = $player->attached ($name) 167=item $bool = $player->attached ($name)
168
169=item $bool = $client->attached ($name)
157 170
158=item $bool = $map->attached ($name) 171=item $bool = $map->attached ($name)
159 172
160Checks wether the named attachment is currently attached to the object. 173Checks wether the named attachment is currently attached to the object.
161 174
208 221
209=item cf::attach_to_players ... 222=item cf::attach_to_players ...
210 223
211Attach handlers to all players. 224Attach handlers to all players.
212 225
226=item cf::attach_to_clients ...
227
228Attach handlers to all players.
229
213=item cf::attach_to_maps ... 230=item cf::attach_to_maps ...
214 231
215Attach handlers to all maps. 232Attach handlers to all maps.
216 233
217=item cf:register_attachment $name, ... 234=item cf:register_attachment $name, ...
233 250
234# the following variables are defined in .xs and must not be re-created 251# the following variables are defined in .xs and must not be re-created
235our @CB_GLOBAL = (); # registry for all global events 252our @CB_GLOBAL = (); # registry for all global events
236our @CB_OBJECT = (); # all objects (should not be used except in emergency) 253our @CB_OBJECT = (); # all objects (should not be used except in emergency)
237our @CB_PLAYER = (); 254our @CB_PLAYER = ();
255our @CB_CLIENT = ();
238our @CB_TYPE = (); # registry for type (cf-object class) based events 256our @CB_TYPE = (); # registry for type (cf-object class) based events
239our @CB_MAP = (); 257our @CB_MAP = ();
240 258
241my %attachment; 259my %attachment;
242 260
325 $res 343 $res
326} 344}
327 345
328*cf::object::attach = 346*cf::object::attach =
329*cf::player::attach = 347*cf::player::attach =
348*cf::client::attach =
330*cf::map::attach = sub { 349*cf::map::attach = sub {
331 my ($obj, $name, %arg) = @_; 350 my ($obj, $name, %arg) = @_;
332 351
333 _attach_attachment $obj, $name, %arg; 352 _attach_attachment $obj, $name, %arg;
334}; 353};
335 354
336# all those should be optimised 355# all those should be optimised
337*cf::object::detach = 356*cf::object::detach =
338*cf::player::detach = 357*cf::player::detach =
358*cf::client::detach =
339*cf::map::detach = sub { 359*cf::map::detach = sub {
340 my ($obj, $name) = @_; 360 my ($obj, $name) = @_;
341 361
342 delete $obj->{_attachment}{$name}; 362 delete $obj->{_attachment}{$name};
343 reattach ($obj); 363 reattach ($obj);
344}; 364};
345 365
346*cf::object::attached = 366*cf::object::attached =
347*cf::player::attached = 367*cf::player::attached =
368*cf::client::attached =
348*cf::map::attached = sub { 369*cf::map::attached = sub {
349 my ($obj, $name) = @_; 370 my ($obj, $name) = @_;
350 371
351 exists $obj->{_attachment}{$name} 372 exists $obj->{_attachment}{$name}
352}; 373};
368 389
369sub attach_to_players { 390sub attach_to_players {
370 _attach @CB_PLAYER, KLASS_PLAYER, @_ 391 _attach @CB_PLAYER, KLASS_PLAYER, @_
371} 392}
372 393
394sub attach_to_clients {
395 _attach @CB_CLIENT, KLASS_CLIENT, @_
396}
397
373sub attach_to_maps { 398sub attach_to_maps {
374 _attach @CB_MAP, KLASS_MAP, @_ 399 _attach @CB_MAP, KLASS_MAP, @_
375} 400}
376 401
377sub register_attachment { 402sub register_attachment {
382 407
383sub register_player_attachment { 408sub register_player_attachment {
384 my $name = shift; 409 my $name = shift;
385 410
386 $attachment{$name} = [[KLASS_PLAYER, @_]]; 411 $attachment{$name} = [[KLASS_PLAYER, @_]];
412}
413
414sub register_client_attachment {
415 my $name = shift;
416
417 $attachment{$name} = [[KLASS_CLIENT, @_]];
387} 418}
388 419
389sub register_map_attachment { 420sub register_map_attachment {
390 my $name = shift; 421 my $name = shift;
391 422
427 458
428=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) 459=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
429 460
430=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) 461=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
431 462
463=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...)
464
432=item $bool = $map->invoke (EVENT_MAP_XXX, ...) 465=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
433 466
434Generate a global/object/player/map-specific event with the given arguments. 467Generate a global/object/player/map-specific event with the given arguments.
435 468
436This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be 469This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
445 478
446=head2 METHODS VALID FOR ALL CORE OBJECTS 479=head2 METHODS VALID FOR ALL CORE OBJECTS
447 480
448=over 4 481=over 4
449 482
450=item $object->valid, $player->valid, $map->valid 483=item $object->valid, $player->valid, $client->valid, $map->valid
451 484
452Just because you have a perl object does not mean that the corresponding 485Just because you have a perl object does not mean that the corresponding
453C-level object still exists. If you try to access an object that has no 486C-level object still exists. If you try to access an object that has no
454valid C counterpart anymore you get an exception at runtime. This method 487valid C counterpart anymore you get an exception at runtime. This method
455can be used to test for existence of the C object part without causing an 488can be used to test for existence of the C object part without causing an
459 492
460=cut 493=cut
461 494
462*cf::object::valid = 495*cf::object::valid =
463*cf::player::valid = 496*cf::player::valid =
497*cf::client::valid =
464*cf::map::valid = \&cf::_valid; 498*cf::map::valid = \&cf::_valid;
465 499
466############################################################################# 500#############################################################################
467# object support 501# object support
468 502
570 if exists $src->{_attachment}; 604 if exists $src->{_attachment};
571 }, 605 },
572; 606;
573 607
574############################################################################# 608#############################################################################
575# old plug-in events 609# command handling &c
576 610
577sub inject_event { 611=item cf::register_command $name => \&callback($ob,$args);
578 my $extension = shift;
579 my $event_code = shift;
580 612
581 my $cb = $hook[$event_code]{$extension} 613Register a callback for execution when the client sends the user command
582 or return; 614$name.
583 615
584 &$cb 616=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 617
606sub register_command { 618sub register_command {
607 my ($name, $time, $cb) = @_; 619 my ($name, $cb) = @_;
608 620
609 my $caller = caller; 621 my $caller = caller;
610 #warn "registering command '$name/$time' to '$caller'"; 622 #warn "registering command '$name/$time' to '$caller'";
611 623
612 push @{ $command{$name} }, [$time, $cb, $caller]; 624 push @{ $COMMAND{$name} }, [$caller, $cb];
613 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
614} 625}
626
627=item cf::register_extcmd $name => \&callback($pl,$packet);
628
629Register a callbackf ro execution when the client sends an extcmd packet.
630
631If the callback returns something, it is sent back as if reply was being
632called.
633
634=cut
615 635
616sub register_extcmd { 636sub register_extcmd {
617 my ($name, $cb) = @_; 637 my ($name, $cb) = @_;
618 638
619 my $caller = caller; 639 my $caller = caller;
620 #warn "registering extcmd '$name' to '$caller'"; 640 #warn "registering extcmd '$name' to '$caller'";
621 641
622 $extcmd{$name} = [$cb, $caller]; 642 $EXTCMD{$name} = [$cb, $caller];
623} 643}
644
645attach_to_players
646 on_command => sub {
647 my ($pl, $name, $params) = @_;
648
649 my $cb = $COMMAND{$name}
650 or return;
651
652 for my $cmd (@$cb) {
653 $cmd->[1]->($pl->ob, $params);
654 }
655
656 cf::override;
657 },
658 on_extcmd => sub {
659 my ($pl, $buf) = @_;
660
661 my $msg = eval { from_json $buf };
662
663 if (ref $msg) {
664 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
665 if (my %reply = $cb->[0]->($pl, $msg)) {
666 $pl->ext_reply ($msg->{msgid}, %reply);
667 }
668 }
669 } else {
670 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
671 }
672
673 cf::override;
674 },
675;
624 676
625sub register { 677sub register {
626 my ($base, $pkg) = @_; 678 my ($base, $pkg) = @_;
627 679
628 #TODO 680 #TODO
671# for my $idx (0 .. $#PLUGIN_EVENT) { 723# for my $idx (0 .. $#PLUGIN_EVENT) {
672# delete $hook[$idx]{$pkg}; 724# delete $hook[$idx]{$pkg};
673# } 725# }
674 726
675 # remove commands 727 # remove commands
676 for my $name (keys %command) { 728 for my $name (keys %COMMAND) {
677 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 729 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
678 730
679 if (@cb) { 731 if (@cb) {
680 $command{$name} = \@cb; 732 $COMMAND{$name} = \@cb;
681 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
682 } else { 733 } else {
683 delete $command{$name};
684 delete $COMMAND{"$name\000"}; 734 delete $COMMAND{$name};
685 } 735 }
686 } 736 }
687 737
688 # remove extcmds 738 # remove extcmds
689 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 739 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
690 delete $extcmd{$name}; 740 delete $EXTCMD{$name};
691 } 741 }
692 742
693 if (my $cb = $pkg->can ("unload")) { 743 if (my $cb = $pkg->can ("unload")) {
694 eval { 744 eval {
695 $cb->($pkg); 745 $cb->($pkg);
699 749
700 Symbol::delete_package $pkg; 750 Symbol::delete_package $pkg;
701} 751}
702 752
703sub load_extensions { 753sub load_extensions {
704 my $LIBDIR = maps_directory "perl";
705
706 for my $ext (<$LIBDIR/*.ext>) { 754 for my $ext (<$LIBDIR/*.ext>) {
707 next unless -r $ext; 755 next unless -r $ext;
708 eval { 756 eval {
709 load_extension $ext; 757 load_extension $ext;
710 1 758 1
711 } or warn "$ext not loaded: $@"; 759 } or warn "$ext not loaded: $@";
712 } 760 }
713} 761}
714 762
715############################################################################# 763#############################################################################
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
739#############################################################################
740# load/save/clean perl data associated with a map 764# load/save/clean perl data associated with a map
741 765
742*cf::mapsupport::on_clean = sub { 766*cf::mapsupport::on_clean = sub {
743 my ($map) = @_; 767 my ($map) = @_;
744 768
869 893
870=pod 894=pod
871 895
872The following fucntions and emthods are available within a safe environment: 896The following fucntions and emthods are available within a safe environment:
873 897
874 cf::object contr pay_amount pay_player 898 cf::object contr pay_amount pay_player map
875 cf::object::player player 899 cf::object::player player
876 cf::player peaceful 900 cf::player peaceful
901 cf::map trigger
877 902
878=cut 903=cut
879 904
880for ( 905for (
881 ["cf::object" => qw(contr pay_amount pay_player)], 906 ["cf::object" => qw(contr pay_amount pay_player map)],
882 ["cf::object::player" => qw(player)], 907 ["cf::object::player" => qw(player)],
883 ["cf::player" => qw(peaceful)], 908 ["cf::player" => qw(peaceful)],
909 ["cf::map" => qw(trigger)],
884) { 910) {
885 no strict 'refs'; 911 no strict 'refs';
886 my ($pkg, @funs) = @$_; 912 my ($pkg, @funs) = @$_;
887 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 913 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
888 for @funs; 914 for @funs;
1028 sub db_sync() { 1054 sub db_sync() {
1029 db_save if $dirty; 1055 db_save if $dirty;
1030 undef $dirty; 1056 undef $dirty;
1031 } 1057 }
1032 1058
1033 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { 1059 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub {
1034 db_sync; 1060 db_sync;
1035 }); 1061 });
1036 1062
1037 sub db_dirty() { 1063 sub db_dirty() {
1038 $dirty = 1; 1064 $dirty = 1;
1088 1114
1089 $msg->("reloading..."); 1115 $msg->("reloading...");
1090 1116
1091 eval { 1117 eval {
1092 # cancel all watchers 1118 # cancel all watchers
1093 $_->cancel for Event::all_watchers; 1119 for (Event::all_watchers) {
1120 $_->cancel if $_->data & WF_AUTOCANCEL;
1121 }
1094 1122
1095 # unload all extensions 1123 # unload all extensions
1096 for (@exts) { 1124 for (@exts) {
1097 $msg->("unloading <$_>"); 1125 $msg->("unloading <$_>");
1098 unload_extension $_; 1126 unload_extension $_;
1158 warn $_[0]; 1186 warn $_[0];
1159 print "$_[0]\n"; 1187 print "$_[0]\n";
1160 }; 1188 };
1161} 1189}
1162 1190
1191register "<global>", __PACKAGE__;
1192
1163register_command "perl-reload", 0, sub { 1193register_command "perl-reload" => sub {
1164 my ($who, $arg) = @_; 1194 my ($who, $arg) = @_;
1165 1195
1166 if ($who->flag (FLAG_WIZ)) { 1196 if ($who->flag (FLAG_WIZ)) {
1167 _perl_reload { 1197 _perl_reload {
1168 warn $_[0]; 1198 warn $_[0];
1169 $who->message ($_[0]); 1199 $who->message ($_[0]);
1170 }; 1200 };
1171 } 1201 }
1172}; 1202};
1173 1203
1174register "<global>", __PACKAGE__;
1175
1176unshift @INC, $LIBDIR; 1204unshift @INC, $LIBDIR;
1177 1205
1178$TICK_WATCHER = Event->timer ( 1206$TICK_WATCHER = Event->timer (
1179 prio => 1, 1207 prio => 0,
1180 async => 1,
1181 at => $NEXT_TICK || 1, 1208 at => $NEXT_TICK || 1,
1209 data => WF_AUTOCANCEL,
1182 cb => sub { 1210 cb => sub {
1183 cf::server_tick; # one server iteration 1211 cf::server_tick; # one server iteration
1184 1212
1185 my $NOW = Event::time; 1213 my $NOW = Event::time;
1186 $NEXT_TICK += $TICK; 1214 $NEXT_TICK += $TICK;
1196IO::AIO::max_poll_time $TICK * 0.2; 1224IO::AIO::max_poll_time $TICK * 0.2;
1197 1225
1198Event->io (fd => IO::AIO::poll_fileno, 1226Event->io (fd => IO::AIO::poll_fileno,
1199 poll => 'r', 1227 poll => 'r',
1200 prio => 5, 1228 prio => 5,
1229 data => WF_AUTOCANCEL,
1201 cb => \&IO::AIO::poll_cb); 1230 cb => \&IO::AIO::poll_cb);
1202 1231
12031 12321
1204 1233

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines