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.51 by root, Mon Aug 28 14:05:24 2006 UTC vs.
Revision 1.68 by root, Tue Sep 12 23:45:16 2006 UTC

11use Event; 11use Event;
12$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
13 13
14use strict; 14use strict;
15 15
16_reload_1; 16_init_vars;
17 17
18our %COMMAND = (); 18our %COMMAND = ();
19our @EVENT; 19our @EVENT;
20our %PROP_TYPE;
21our %PROP_IDX;
22our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
23 21
24our $TICK = MAX_TIME * 1e-6; 22our $TICK = MAX_TIME * 1e-6;
25our $TICK_WATCHER; 23our $TICK_WATCHER;
26our $NEXT_TICK; 24our $NEXT_TICK;
34 print STDERR "cfperl: $msg"; 32 print STDERR "cfperl: $msg";
35 LOG llevError, "cfperl: $msg"; 33 LOG llevError, "cfperl: $msg";
36 }; 34 };
37} 35}
38 36
39my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
40
41# generate property mutators
42sub prop_gen {
43 my ($prefix, $class) = @_;
44
45 no strict 'refs';
46
47 for my $prop (keys %PROP_TYPE) {
48 $prop =~ /^\Q$prefix\E_(.*$)/ or next;
49 my $sub = lc $1;
50
51 my $type = $PROP_TYPE{$prop};
52 my $idx = $PROP_IDX {$prop};
53
54 *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub {
55 $_[0]->get_property ($type, $idx)
56 };
57
58 *{"$class\::set_$sub"} = sub {
59 $_[0]->set_property ($type, $idx, $_[1]);
60 } unless $ignore_set{$prop};
61 }
62}
63
64# auto-generate most of the API
65
66prop_gen OBJECT_PROP => "cf::object";
67# CFAPI_OBJECT_ANIMATION?
68prop_gen PLAYER_PROP => "cf::object::player";
69
70prop_gen MAP_PROP => "cf::map";
71prop_gen ARCH_PROP => "cf::arch";
72
73@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 37@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
74 38
75# we bless all objects into (empty) derived classes to force a method lookup 39# we bless all objects into (empty) derived classes to force a method lookup
76# within the Safe compartment. 40# within the Safe compartment.
77for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { 41for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) {
105} 69}
106 70
107############################################################################# 71#############################################################################
108# "new" plug-in system 72# "new" plug-in system
109 73
74=head3 EVENTS AND OBJECT ATTACHMENTS
75
76=over 4
77
110=item $object->attach ($attachment, key => $value...) 78=item $object->attach ($attachment, key => $value...)
111 79
80=item $object->detach ($attachment)
81
112Attach a pre-registered attachment to an object. 82Attach/detach a pre-registered attachment to an object.
113 83
114=item $player->attach ($attachment, key => $value...) 84=item $player->attach ($attachment, key => $value...)
115 85
86=item $player->detach ($attachment)
87
116Attach a pre-registered attachment to a player. 88Attach/detach a pre-registered attachment to a player.
117 89
118=item $map->attach ($attachment, key => $value...) # not yet persistent 90=item $map->attach ($attachment, key => $value...)
119 91
92=item $map->detach ($attachment)
93
120Attach a pre-registered attachment to a map. 94Attach/detach a pre-registered attachment to a map.
95
96=item $bool = $object->attached ($name)
97
98=item $bool = $player->attached ($name)
99
100=item $bool = $map->attached ($name)
101
102Checks wether the named attachment is currently attached to the object.
121 103
122=item cf::attach_global ... 104=item cf::attach_global ...
123 105
124Attach handlers for global events. 106Attach handlers for global events.
125 107
173=item cf::attach_to_maps ... 155=item cf::attach_to_maps ...
174 156
175Attach handlers to all maps. 157Attach handlers to all maps.
176 158
177=item cf:register_attachment $name, ... 159=item cf:register_attachment $name, ...
160
161Register an attachment by name through which objects can refer to this
162attachment.
163
164=item cf:register_player_attachment $name, ...
165
166Register an attachment by name through which players can refer to this
167attachment.
168
169=item cf:register_map_attachment $name, ...
170
171Register an attachment by name through which maps can refer to this
172attachment.
178 173
179=cut 174=cut
180 175
181# the following variables are defined in .xs and must not be re-created 176# the following variables are defined in .xs and must not be re-created
182our @CB_GLOBAL = (); # registry for all global events 177our @CB_GLOBAL = (); # registry for all global events
247} 242}
248 243
249sub _attach_attachment { 244sub _attach_attachment {
250 my ($obj, $name, %arg) = @_; 245 my ($obj, $name, %arg) = @_;
251 246
247 return if exists $obj->{_attachment}{$name};
248
252 my $res; 249 my $res;
253 250
254 if (my $attach = $attachment{$name}) { 251 if (my $attach = $attachment{$name}) {
255 my $registry = $obj->registry; 252 my $registry = $obj->registry;
256 253
268 265
269 $res->{attachment} = $name; 266 $res->{attachment} = $name;
270 $res 267 $res
271} 268}
272 269
273sub cf::object::attach { 270*cf::object::attach =
271*cf::player::attach =
272*cf::map::attach = sub {
274 my ($obj, $name, %arg) = @_; 273 my ($obj, $name, %arg) = @_;
275 274
276 _attach_attachment $obj, $name, %arg; 275 _attach_attachment $obj, $name, %arg;
277} 276};
278 277
278# all those should be optimised
279*cf::object::detach =
279sub cf::player::attach { 280*cf::player::detach =
281*cf::map::detach = sub {
280 my ($obj, $name, %arg) = @_; 282 my ($obj, $name) = @_;
281 283
282 _attach_attachment KLASS_PLAYER, $obj, $name, %arg; 284 delete $obj->{_attachment}{$name};
283} 285 reattach ($obj);
286};
284 287
285sub cf::map::attach { 288*cf::object::attached =
289*cf::player::attached =
290*cf::map::attached = sub {
286 my ($obj, $name, %arg) = @_; 291 my ($obj, $name) = @_;
287 292
288 _attach_attachment KLASS_MAP, $obj, $name, %arg; 293 exists $obj->{_attachment}{$name}
289} 294};
290 295
291sub attach_global { 296sub attach_global {
292 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 297 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
293} 298}
294 299
315 my $name = shift; 320 my $name = shift;
316 321
317 $attachment{$name} = [[KLASS_OBJECT, @_]]; 322 $attachment{$name} = [[KLASS_OBJECT, @_]];
318} 323}
319 324
325sub register_player_attachment {
326 my $name = shift;
327
328 $attachment{$name} = [[KLASS_PLAYER, @_]];
329}
330
331sub register_map_attachment {
332 my $name = shift;
333
334 $attachment{$name} = [[KLASS_MAP, @_]];
335}
336
320our $override; 337our $override;
321our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 338our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
322 339
323sub override { 340sub override {
324 $override = 1; 341 $override = 1;
336 for (@$callbacks) { 353 for (@$callbacks) {
337 eval { &{$_->[1]} }; 354 eval { &{$_->[1]} };
338 355
339 if ($@) { 356 if ($@) {
340 warn "$@"; 357 warn "$@";
341 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; 358 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
342 override; 359 override;
343 } 360 }
344 361
345 return 1 if $override; 362 return 1 if $override;
346 } 363 }
347 364
348 0 365 0
349} 366}
367
368=item $bool = cf::invoke EVENT_GLOBAL_XXX, ...
369
370=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
371
372=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
373
374=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
375
376Generate a global/object/player/map-specific event with the given arguments.
377
378This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
379removed in future versions), and there is no public API to access override
380results (if you must, access C<@cf::invoke_results> directly).
381
382=back
383
384=head2 methods valid for all pointers
385
386=over 4
387
388=item $object->valid
389
390=item $player->valid
391
392=item $map->valid
393
394Just because you have a perl object does not mean that the corresponding
395C-level object still exists. If you try to access an object that has no
396valid C counterpart anymore you get an exception at runtime. This method
397can be used to test for existence of the C object part without causing an
398exception.
399
400=back
401
402=cut
403
404*cf::object::valid =
405*cf::player::valid =
406*cf::map::valid = \&cf::_valid;
350 407
351############################################################################# 408#############################################################################
352# object support 409# object support
353 410
354sub instantiate { 411sub instantiate {
383 } 440 }
384 } 441 }
385} 442}
386 443
387sub object_freezer_save { 444sub object_freezer_save {
388 my ($filename, $objs) = @_; 445 my ($filename, $rdata, $objs) = @_;
389 446
390 if (@$objs) { 447 if (length $$rdata) {
448 warn sprintf "saving %s (%d,%d)\n",
449 $filename, length $$rdata, scalar @$objs;
450
391 open my $fh, ">:raw", "$filename.pst~"; 451 if (open my $fh, ">:raw", "$filename~") {
452 chmod SAVE_MODE, $fh;
453 syswrite $fh, $$rdata;
454 close $fh;
455
456 if (@$objs && open my $fh, ">:raw", "$filename.pst~") {
457 chmod SAVE_MODE, $fh;
392 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 458 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
393 close $fh; 459 close $fh;
394 chmod SAVE_MODE, "$filename.pst~";
395 rename "$filename.pst~", "$filename.pst"; 460 rename "$filename.pst~", "$filename.pst";
461 } else {
462 unlink "$filename.pst";
463 }
464
465 rename "$filename~", $filename;
466 } else {
467 warn "FATAL: $filename~: $!\n";
468 }
396 } else { 469 } else {
470 unlink $filename;
397 unlink "$filename.pst"; 471 unlink "$filename.pst";
398 } 472 }
399
400 chmod SAVE_MODE, "$filename~";
401 rename "$filename~", $filename;
402} 473}
403 474
404sub object_thawer_load { 475sub object_thawer_load {
405 my ($filename) = @_; 476 my ($filename) = @_;
406 477
478 local $/;
479
480 my $av;
481
482 #TODO: use sysread etc.
483 if (open my $data, "<:raw:perlio", $filename) {
484 $data = <$data>;
407 open my $fh, "<:raw:perlio", "$filename.pst" 485 if (open my $pst, "<:raw:perlio", "$filename.pst") {
408 or return; 486 $av = eval { (Storable::thaw <$pst>)->{objs} };
487 }
488 return ($data, $av);
489 }
409 490
410 eval { local $/; (Storable::thaw <$fh>)->{objs} } 491 ()
411} 492}
412 493
413attach_to_objects 494attach_to_objects
414 prio => -1000000, 495 prio => -1000000,
415 on_clone => sub { 496 on_clone => sub {
561 load_extension $ext; 642 load_extension $ext;
562 1 643 1
563 } or warn "$ext not loaded: $@"; 644 } or warn "$ext not loaded: $@";
564 } 645 }
565} 646}
566
567sub _perl_reload(&) {
568 my ($msg) = @_;
569
570 $msg->("reloading...");
571
572 eval {
573 # 1. cancel all watchers
574 $_->cancel for Event::all_watchers;
575
576 # 2. unload all extensions
577 for (@exts) {
578 $msg->("unloading <$_>");
579 unload_extension $_;
580 }
581
582 # 3. unload all modules loaded from $LIBDIR
583 while (my ($k, $v) = each %INC) {
584 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
585
586 $msg->("removing <$k>");
587 delete $INC{$k};
588
589 $k =~ s/\.pm$//;
590 $k =~ s/\//::/g;
591
592 if (my $cb = $k->can ("unload_module")) {
593 $cb->();
594 }
595
596 Symbol::delete_package $k;
597 }
598
599 # 4. get rid of safe::, as good as possible
600 Symbol::delete_package "safe::$_"
601 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
602
603 # 5. remove register_script_function callbacks
604 # TODO
605
606 # 6. unload cf.pm "a bit"
607 delete $INC{"cf.pm"};
608
609 # don't, removes xs symbols, too,
610 # and global variables created in xs
611 #Symbol::delete_package __PACKAGE__;
612
613 # 7. reload cf.pm
614 $msg->("reloading cf.pm");
615 require cf;
616 };
617 $msg->($@) if $@;
618
619 $msg->("reloaded");
620};
621
622sub perl_reload() {
623 _perl_reload {
624 warn $_[0];
625 print "$_[0]\n";
626 };
627}
628
629register_command "perl-reload", 0, sub {
630 my ($who, $arg) = @_;
631
632 if ($who->flag (FLAG_WIZ)) {
633 _perl_reload {
634 warn $_[0];
635 $who->message ($_[0]);
636 };
637 }
638};
639 647
640############################################################################# 648#############################################################################
641# extcmd framework, basically convert ext <msg> 649# extcmd framework, basically convert ext <msg>
642# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 650# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
643 651
671 defined $path or return; 679 defined $path or return;
672 680
673 unlink "$path.pst"; 681 unlink "$path.pst";
674}; 682};
675 683
676# old style persistent data, TODO: remove #d#
677*cf::mapsupport::on_swapin =
678*cf::mapsupport::on_load = sub {
679 my ($map) = @_;
680
681 my $path = $map->tmpname;
682 $path = $map->path unless defined $path;
683
684 open my $fh, "<:raw", "$path.cfperl"
685 or return; # no perl data
686
687 my $data = Storable::thaw do { local $/; <$fh> };
688
689 $data->{version} <= 1
690 or return; # too new
691
692 $map->_set_obs ($data->{obs});
693 $map->invoke (EVENT_MAP_UPGRADE);
694};
695
696attach_to_maps prio => -10000, package => cf::mapsupport::; 684attach_to_maps prio => -10000, package => cf::mapsupport::;
697 685
698############################################################################# 686#############################################################################
699# load/save perl data associated with player->ob objects 687# load/save perl data associated with player->ob objects
700 688
701sub all_objects(@) { 689sub all_objects(@) {
702 @_, map all_objects ($_->inv), @_ 690 @_, map all_objects ($_->inv), @_
703} 691}
704 692
693# TODO: compatibility cruft, remove when no longer needed
705attach_to_players 694attach_to_players
706 on_load => sub { 695 on_load => sub {
707 my ($pl, $path) = @_; 696 my ($pl, $path) = @_;
708 697
709 for my $o (all_objects $pl->ob) { 698 for my $o (all_objects $pl->ob) {
827 no strict 'refs'; 816 no strict 'refs';
828 *{"safe::$fun"} = $safe_hole->wrap ($cb); 817 *{"safe::$fun"} = $safe_hole->wrap ($cb);
829} 818}
830 819
831############################################################################# 820#############################################################################
821
822=head2 EXTENSION DATABASE SUPPORT
823
824Crossfire maintains a very simple database for extension use. It can
825currently store anything that can be serialised using Storable, which
826excludes objects.
827
828The parameter C<$family> should best start with the name of the extension
829using it, it should be unique.
830
831=over 4
832
833=item $hashref = cf::db_get $family
834
835Return a hashref for use by the extension C<$family>, which can be
836modified. After modifications, you have to call C<cf::db_dirty> or
837C<cf::db_sync>.
838
839=item $value = cf::db_get $family => $key
840
841Returns a single value from the database
842
843=item cf::db_put $family => $hashref
844
845Stores the given family hashref into the database. Updates are delayed, if
846you want the data to be synced to disk immediately, use C<cf::db_sync>.
847
848=item cf::db_put $family => $key => $value
849
850Stores the given C<$value> in the family hash. Updates are delayed, if you
851want the data to be synced to disk immediately, use C<cf::db_sync>.
852
853=item cf::db_dirty
854
855Marks the database as dirty, to be updated at a later time.
856
857=item cf::db_sync
858
859Immediately write the database to disk I<if it is dirty>.
860
861=cut
862
863{
864 my $db;
865 my $path = cf::localdir . "/database.pst";
866
867 sub db_load() {
868 warn "loading database $path\n";#d# remove later
869 $db = stat $path ? Storable::retrieve $path : { };
870 }
871
872 my $pid;
873
874 sub db_save() {
875 warn "saving database $path\n";#d# remove later
876 waitpid $pid, 0 if $pid;
877 if (0 == ($pid = fork)) {
878 $db->{_meta}{version} = 1;
879 Storable::nstore $db, "$path~";
880 rename "$path~", $path;
881 cf::_exit 0 if defined $pid;
882 }
883 }
884
885 my $dirty;
886
887 sub db_sync() {
888 db_save if $dirty;
889 undef $dirty;
890 }
891
892 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub {
893 db_sync;
894 });
895
896 sub db_dirty() {
897 $dirty = 1;
898 $idle->start;
899 }
900
901 sub db_get($;$) {
902 @_ >= 2
903 ? $db->{$_[0]}{$_[1]}
904 : ($db->{$_[0]} ||= { })
905 }
906
907 sub db_put($$;$) {
908 if (@_ >= 3) {
909 $db->{$_[0]}{$_[1]} = $_[2];
910 } else {
911 $db->{$_[0]} = $_[1];
912 }
913 db_dirty;
914 }
915
916 attach_global
917 prio => 10000,
918 on_cleanup => sub {
919 db_sync;
920 },
921 ;
922}
923
924#############################################################################
832# the server's main() 925# the server's main()
833 926
834sub main { 927sub main {
928 db_load;
929 load_extensions;
835 Event::loop; 930 Event::loop;
836} 931}
837 932
838############################################################################# 933#############################################################################
839# initialisation 934# initialisation
840 935
936sub _perl_reload(&) {
937 my ($msg) = @_;
938
939 $msg->("reloading...");
940
941 eval {
942 # cancel all watchers
943 $_->cancel for Event::all_watchers;
944
945 # unload all extensions
946 for (@exts) {
947 $msg->("unloading <$_>");
948 unload_extension $_;
949 }
950
951 # unload all modules loaded from $LIBDIR
952 while (my ($k, $v) = each %INC) {
953 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
954
955 $msg->("removing <$k>");
956 delete $INC{$k};
957
958 $k =~ s/\.pm$//;
959 $k =~ s/\//::/g;
960
961 if (my $cb = $k->can ("unload_module")) {
962 $cb->();
963 }
964
965 Symbol::delete_package $k;
966 }
967
968 # sync database to disk
969 cf::db_sync;
970
971 # get rid of safe::, as good as possible
972 Symbol::delete_package "safe::$_"
973 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
974
975 # remove register_script_function callbacks
976 # TODO
977
978 # unload cf.pm "a bit"
979 delete $INC{"cf.pm"};
980
981 # don't, removes xs symbols, too,
982 # and global variables created in xs
983 #Symbol::delete_package __PACKAGE__;
984
985 # reload cf.pm
986 $msg->("reloading cf.pm");
987 require cf;
988
989 # load database again
990 cf::db_load;
991
992 # load extensions
993 $msg->("load extensions");
994 cf::load_extensions;
995
996 # reattach attachments to objects
997 $msg->("reattach");
998 _global_reattach;
999 };
1000 $msg->($@) if $@;
1001
1002 $msg->("reloaded");
1003};
1004
1005sub perl_reload() {
1006 _perl_reload {
1007 warn $_[0];
1008 print "$_[0]\n";
1009 };
1010}
1011
1012register_command "perl-reload", 0, sub {
1013 my ($who, $arg) = @_;
1014
1015 if ($who->flag (FLAG_WIZ)) {
1016 _perl_reload {
1017 warn $_[0];
1018 $who->message ($_[0]);
1019 };
1020 }
1021};
1022
841register "<global>", __PACKAGE__; 1023register "<global>", __PACKAGE__;
842 1024
843unshift @INC, $LIBDIR; 1025unshift @INC, $LIBDIR;
844
845load_extensions;
846 1026
847$TICK_WATCHER = Event->timer ( 1027$TICK_WATCHER = Event->timer (
848 prio => 1, 1028 prio => 1,
849 at => $NEXT_TICK || 1, 1029 at => $NEXT_TICK || 1,
850 cb => sub { 1030 cb => sub {
859 $TICK_WATCHER->at ($NEXT_TICK); 1039 $TICK_WATCHER->at ($NEXT_TICK);
860 $TICK_WATCHER->start; 1040 $TICK_WATCHER->start;
861 }, 1041 },
862); 1042);
863 1043
864_reload_2;
865
8661 10441
867 1045

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines