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.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
73 78
74@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 79@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
75 80
76# 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
77# within the Safe compartment. 82# 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)) { 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)) {
79 no strict 'refs'; 89 no strict 'refs';
80 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 90 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
81} 91}
82 92
83$Event::DIED = sub { 93$Event::DIED = sub {
85}; 95};
86 96
87my %ext_pkg; 97my %ext_pkg;
88my @exts; 98my @exts;
89my @hook; 99my @hook;
90my %command;
91my %extcmd;
92 100
93=head2 UTILITY FUNCTIONS 101=head2 UTILITY FUNCTIONS
94 102
95=over 4 103=over 4
96 104
144 152
145=item $map->attach ($attachment, key => $value...) 153=item $map->attach ($attachment, key => $value...)
146 154
147=item $map->detach ($attachment) 155=item $map->detach ($attachment)
148 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
149Attach/detach a pre-registered attachment to a map. 163Attach/detach a pre-registered attachment to a map.
150 164
151=item $bool = $object->attached ($name) 165=item $bool = $object->attached ($name)
152 166
153=item $bool = $player->attached ($name) 167=item $bool = $player->attached ($name)
168
169=item $bool = $client->attached ($name)
154 170
155=item $bool = $map->attached ($name) 171=item $bool = $map->attached ($name)
156 172
157Checks wether the named attachment is currently attached to the object. 173Checks wether the named attachment is currently attached to the object.
158 174
205 221
206=item cf::attach_to_players ... 222=item cf::attach_to_players ...
207 223
208Attach handlers to all players. 224Attach handlers to all players.
209 225
226=item cf::attach_to_clients ...
227
228Attach handlers to all players.
229
210=item cf::attach_to_maps ... 230=item cf::attach_to_maps ...
211 231
212Attach handlers to all maps. 232Attach handlers to all maps.
213 233
214=item cf:register_attachment $name, ... 234=item cf:register_attachment $name, ...
230 250
231# 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
232our @CB_GLOBAL = (); # registry for all global events 252our @CB_GLOBAL = (); # registry for all global events
233our @CB_OBJECT = (); # all objects (should not be used except in emergency) 253our @CB_OBJECT = (); # all objects (should not be used except in emergency)
234our @CB_PLAYER = (); 254our @CB_PLAYER = ();
255our @CB_CLIENT = ();
235our @CB_TYPE = (); # registry for type (cf-object class) based events 256our @CB_TYPE = (); # registry for type (cf-object class) based events
236our @CB_MAP = (); 257our @CB_MAP = ();
237 258
238my %attachment; 259my %attachment;
239 260
322 $res 343 $res
323} 344}
324 345
325*cf::object::attach = 346*cf::object::attach =
326*cf::player::attach = 347*cf::player::attach =
348*cf::client::attach =
327*cf::map::attach = sub { 349*cf::map::attach = sub {
328 my ($obj, $name, %arg) = @_; 350 my ($obj, $name, %arg) = @_;
329 351
330 _attach_attachment $obj, $name, %arg; 352 _attach_attachment $obj, $name, %arg;
331}; 353};
332 354
333# all those should be optimised 355# all those should be optimised
334*cf::object::detach = 356*cf::object::detach =
335*cf::player::detach = 357*cf::player::detach =
358*cf::client::detach =
336*cf::map::detach = sub { 359*cf::map::detach = sub {
337 my ($obj, $name) = @_; 360 my ($obj, $name) = @_;
338 361
339 delete $obj->{_attachment}{$name}; 362 delete $obj->{_attachment}{$name};
340 reattach ($obj); 363 reattach ($obj);
341}; 364};
342 365
343*cf::object::attached = 366*cf::object::attached =
344*cf::player::attached = 367*cf::player::attached =
368*cf::client::attached =
345*cf::map::attached = sub { 369*cf::map::attached = sub {
346 my ($obj, $name) = @_; 370 my ($obj, $name) = @_;
347 371
348 exists $obj->{_attachment}{$name} 372 exists $obj->{_attachment}{$name}
349}; 373};
365 389
366sub attach_to_players { 390sub attach_to_players {
367 _attach @CB_PLAYER, KLASS_PLAYER, @_ 391 _attach @CB_PLAYER, KLASS_PLAYER, @_
368} 392}
369 393
394sub attach_to_clients {
395 _attach @CB_CLIENT, KLASS_CLIENT, @_
396}
397
370sub attach_to_maps { 398sub attach_to_maps {
371 _attach @CB_MAP, KLASS_MAP, @_ 399 _attach @CB_MAP, KLASS_MAP, @_
372} 400}
373 401
374sub register_attachment { 402sub register_attachment {
379 407
380sub register_player_attachment { 408sub register_player_attachment {
381 my $name = shift; 409 my $name = shift;
382 410
383 $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, @_]];
384} 418}
385 419
386sub register_map_attachment { 420sub register_map_attachment {
387 my $name = shift; 421 my $name = shift;
388 422
424 458
425=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) 459=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
426 460
427=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) 461=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
428 462
463=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...)
464
429=item $bool = $map->invoke (EVENT_MAP_XXX, ...) 465=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
430 466
431Generate a global/object/player/map-specific event with the given arguments. 467Generate a global/object/player/map-specific event with the given arguments.
432 468
433This 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
442 478
443=head2 METHODS VALID FOR ALL CORE OBJECTS 479=head2 METHODS VALID FOR ALL CORE OBJECTS
444 480
445=over 4 481=over 4
446 482
447=item $object->valid, $player->valid, $map->valid 483=item $object->valid, $player->valid, $client->valid, $map->valid
448 484
449Just 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
450C-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
451valid C counterpart anymore you get an exception at runtime. This method 487valid C counterpart anymore you get an exception at runtime. This method
452can 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
456 492
457=cut 493=cut
458 494
459*cf::object::valid = 495*cf::object::valid =
460*cf::player::valid = 496*cf::player::valid =
497*cf::client::valid =
461*cf::map::valid = \&cf::_valid; 498*cf::map::valid = \&cf::_valid;
462 499
463############################################################################# 500#############################################################################
464# object support 501# object support
465 502
567 if exists $src->{_attachment}; 604 if exists $src->{_attachment};
568 }, 605 },
569; 606;
570 607
571############################################################################# 608#############################################################################
572# old plug-in events 609# command handling &c
573 610
574sub inject_event { 611=item cf::register_command $name => \&callback($ob,$args);
575 my $extension = shift;
576 my $event_code = shift;
577 612
578 my $cb = $hook[$event_code]{$extension} 613Register a callback for execution when the client sends the user command
579 or return; 614$name.
580 615
581 &$cb 616=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 617
603sub register_command { 618sub register_command {
604 my ($name, $time, $cb) = @_; 619 my ($name, $cb) = @_;
605 620
606 my $caller = caller; 621 my $caller = caller;
607 #warn "registering command '$name/$time' to '$caller'"; 622 #warn "registering command '$name/$time' to '$caller'";
608 623
609 push @{ $command{$name} }, [$time, $cb, $caller]; 624 push @{ $COMMAND{$name} }, [$caller, $cb];
610 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
611} 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
612 635
613sub register_extcmd { 636sub register_extcmd {
614 my ($name, $cb) = @_; 637 my ($name, $cb) = @_;
615 638
616 my $caller = caller; 639 my $caller = caller;
617 #warn "registering extcmd '$name' to '$caller'"; 640 #warn "registering extcmd '$name' to '$caller'";
618 641
619 $extcmd{$name} = [$cb, $caller]; 642 $EXTCMD{$name} = [$cb, $caller];
620} 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;
621 676
622sub register { 677sub register {
623 my ($base, $pkg) = @_; 678 my ($base, $pkg) = @_;
624 679
625 #TODO 680 #TODO
668# for my $idx (0 .. $#PLUGIN_EVENT) { 723# for my $idx (0 .. $#PLUGIN_EVENT) {
669# delete $hook[$idx]{$pkg}; 724# delete $hook[$idx]{$pkg};
670# } 725# }
671 726
672 # remove commands 727 # remove commands
673 for my $name (keys %command) { 728 for my $name (keys %COMMAND) {
674 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 729 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
675 730
676 if (@cb) { 731 if (@cb) {
677 $command{$name} = \@cb; 732 $COMMAND{$name} = \@cb;
678 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
679 } else { 733 } else {
680 delete $command{$name};
681 delete $COMMAND{"$name\000"}; 734 delete $COMMAND{$name};
682 } 735 }
683 } 736 }
684 737
685 # remove extcmds 738 # remove extcmds
686 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 739 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
687 delete $extcmd{$name}; 740 delete $EXTCMD{$name};
688 } 741 }
689 742
690 if (my $cb = $pkg->can ("unload")) { 743 if (my $cb = $pkg->can ("unload")) {
691 eval { 744 eval {
692 $cb->($pkg); 745 $cb->($pkg);
696 749
697 Symbol::delete_package $pkg; 750 Symbol::delete_package $pkg;
698} 751}
699 752
700sub load_extensions { 753sub load_extensions {
701 my $LIBDIR = maps_directory "perl";
702
703 for my $ext (<$LIBDIR/*.ext>) { 754 for my $ext (<$LIBDIR/*.ext>) {
704 next unless -r $ext; 755 next unless -r $ext;
705 eval { 756 eval {
706 load_extension $ext; 757 load_extension $ext;
707 1 758 1
708 } or warn "$ext not loaded: $@"; 759 } or warn "$ext not loaded: $@";
709 } 760 }
710} 761}
711 762
712############################################################################# 763#############################################################################
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
736#############################################################################
737# load/save/clean perl data associated with a map 764# load/save/clean perl data associated with a map
738 765
739*cf::mapsupport::on_clean = sub { 766*cf::mapsupport::on_clean = sub {
740 my ($map) = @_; 767 my ($map) = @_;
741 768
866 893
867=pod 894=pod
868 895
869The following fucntions and emthods are available within a safe environment: 896The following fucntions and emthods are available within a safe environment:
870 897
871 cf::object contr pay_amount pay_player 898 cf::object contr pay_amount pay_player map
872 cf::object::player player 899 cf::object::player player
873 cf::player peaceful 900 cf::player peaceful
901 cf::map trigger
874 902
875=cut 903=cut
876 904
877for ( 905for (
878 ["cf::object" => qw(contr pay_amount pay_player)], 906 ["cf::object" => qw(contr pay_amount pay_player map)],
879 ["cf::object::player" => qw(player)], 907 ["cf::object::player" => qw(player)],
880 ["cf::player" => qw(peaceful)], 908 ["cf::player" => qw(peaceful)],
909 ["cf::map" => qw(trigger)],
881) { 910) {
882 no strict 'refs'; 911 no strict 'refs';
883 my ($pkg, @funs) = @$_; 912 my ($pkg, @funs) = @$_;
884 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 913 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
885 for @funs; 914 for @funs;
1025 sub db_sync() { 1054 sub db_sync() {
1026 db_save if $dirty; 1055 db_save if $dirty;
1027 undef $dirty; 1056 undef $dirty;
1028 } 1057 }
1029 1058
1030 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 {
1031 db_sync; 1060 db_sync;
1032 }); 1061 });
1033 1062
1034 sub db_dirty() { 1063 sub db_dirty() {
1035 $dirty = 1; 1064 $dirty = 1;
1085 1114
1086 $msg->("reloading..."); 1115 $msg->("reloading...");
1087 1116
1088 eval { 1117 eval {
1089 # cancel all watchers 1118 # cancel all watchers
1090 $_->cancel for Event::all_watchers; 1119 for (Event::all_watchers) {
1120 $_->cancel if $_->data & WF_AUTOCANCEL;
1121 }
1091 1122
1092 # unload all extensions 1123 # unload all extensions
1093 for (@exts) { 1124 for (@exts) {
1094 $msg->("unloading <$_>"); 1125 $msg->("unloading <$_>");
1095 unload_extension $_; 1126 unload_extension $_;
1155 warn $_[0]; 1186 warn $_[0];
1156 print "$_[0]\n"; 1187 print "$_[0]\n";
1157 }; 1188 };
1158} 1189}
1159 1190
1191register "<global>", __PACKAGE__;
1192
1160register_command "perl-reload", 0, sub { 1193register_command "perl-reload" => sub {
1161 my ($who, $arg) = @_; 1194 my ($who, $arg) = @_;
1162 1195
1163 if ($who->flag (FLAG_WIZ)) { 1196 if ($who->flag (FLAG_WIZ)) {
1164 _perl_reload { 1197 _perl_reload {
1165 warn $_[0]; 1198 warn $_[0];
1166 $who->message ($_[0]); 1199 $who->message ($_[0]);
1167 }; 1200 };
1168 } 1201 }
1169}; 1202};
1170 1203
1171register "<global>", __PACKAGE__;
1172
1173unshift @INC, $LIBDIR; 1204unshift @INC, $LIBDIR;
1174 1205
1175$TICK_WATCHER = Event->timer ( 1206$TICK_WATCHER = Event->timer (
1176 prio => 1, 1207 prio => 0,
1177 async => 1,
1178 at => $NEXT_TICK || 1, 1208 at => $NEXT_TICK || 1,
1209 data => WF_AUTOCANCEL,
1179 cb => sub { 1210 cb => sub {
1180 cf::server_tick; # one server iteration 1211 cf::server_tick; # one server iteration
1181 1212
1182 my $NOW = Event::time; 1213 my $NOW = Event::time;
1183 $NEXT_TICK += $TICK; 1214 $NEXT_TICK += $TICK;
1193IO::AIO::max_poll_time $TICK * 0.2; 1224IO::AIO::max_poll_time $TICK * 0.2;
1194 1225
1195Event->io (fd => IO::AIO::poll_fileno, 1226Event->io (fd => IO::AIO::poll_fileno,
1196 poll => 'r', 1227 poll => 'r',
1197 prio => 5, 1228 prio => 5,
1229 data => WF_AUTOCANCEL,
1198 cb => \&IO::AIO::poll_cb); 1230 cb => \&IO::AIO::poll_cb);
1199 1231
12001 12321
1201 1233

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines