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.64 by root, Sun Sep 10 00:51:24 2006 UTC vs.
Revision 1.69 by root, Mon Sep 18 01:10:35 2006 UTC

642 load_extension $ext; 642 load_extension $ext;
643 1 643 1
644 } or warn "$ext not loaded: $@"; 644 } or warn "$ext not loaded: $@";
645 } 645 }
646} 646}
647
648sub _perl_reload(&) {
649 my ($msg) = @_;
650
651 $msg->("reloading...");
652
653 eval {
654 # 1. cancel all watchers
655 $_->cancel for Event::all_watchers;
656
657 # 2. unload all extensions
658 for (@exts) {
659 $msg->("unloading <$_>");
660 unload_extension $_;
661 }
662
663 # 3. unload all modules loaded from $LIBDIR
664 while (my ($k, $v) = each %INC) {
665 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
666
667 $msg->("removing <$k>");
668 delete $INC{$k};
669
670 $k =~ s/\.pm$//;
671 $k =~ s/\//::/g;
672
673 if (my $cb = $k->can ("unload_module")) {
674 $cb->();
675 }
676
677 Symbol::delete_package $k;
678 }
679
680 # 4. get rid of safe::, as good as possible
681 Symbol::delete_package "safe::$_"
682 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
683
684 # 5. remove register_script_function callbacks
685 # TODO
686
687 # 6. unload cf.pm "a bit"
688 delete $INC{"cf.pm"};
689
690 # don't, removes xs symbols, too,
691 # and global variables created in xs
692 #Symbol::delete_package __PACKAGE__;
693
694 # 7. reload cf.pm
695 $msg->("reloading cf.pm");
696 require cf;
697
698 $msg->("load extensions");
699 cf::load_extensions;
700
701 $msg->("reattach");
702 _global_reattach;
703 };
704 $msg->($@) if $@;
705
706 $msg->("reloaded");
707};
708
709sub perl_reload() {
710 _perl_reload {
711 warn $_[0];
712 print "$_[0]\n";
713 };
714}
715
716register_command "perl-reload", 0, sub {
717 my ($who, $arg) = @_;
718
719 if ($who->flag (FLAG_WIZ)) {
720 _perl_reload {
721 warn $_[0];
722 $who->message ($_[0]);
723 };
724 }
725};
726 647
727############################################################################# 648#############################################################################
728# extcmd framework, basically convert ext <msg> 649# extcmd framework, basically convert ext <msg>
729# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 650# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
730 651
887 } 808 }
888 809
889 wantarray ? @res : $res[0] 810 wantarray ? @res : $res[0]
890} 811}
891 812
813=item cf::register_script_function $function => $cb
814
815Register a function that can be called from within map/npc scripts. The
816function should be reasonably secure and should be put into a package name
817like the extension.
818
819Example: register a function that gets called whenever a map script calls
820C<rent::overview>, as used by the C<rent> extension.
821
822 cf::register_script_function "rent::overview" => sub {
823 ...
824 };
825
826=cut
827
892sub register_script_function { 828sub register_script_function {
893 my ($fun, $cb) = @_; 829 my ($fun, $cb) = @_;
894 830
895 no strict 'refs'; 831 no strict 'refs';
896 *{"safe::$fun"} = $safe_hole->wrap ($cb); 832 *{"safe::$fun"} = $safe_hole->wrap ($cb);
897} 833}
898 834
899############################################################################# 835#############################################################################
836
837=head2 EXTENSION DATABASE SUPPORT
838
839Crossfire maintains a very simple database for extension use. It can
840currently store anything that can be serialised using Storable, which
841excludes objects.
842
843The parameter C<$family> should best start with the name of the extension
844using it, it should be unique.
845
846=over 4
847
848=item $hashref = cf::db_get $family
849
850Return a hashref for use by the extension C<$family>, which can be
851modified. After modifications, you have to call C<cf::db_dirty> or
852C<cf::db_sync>.
853
854=item $value = cf::db_get $family => $key
855
856Returns a single value from the database
857
858=item cf::db_put $family => $hashref
859
860Stores the given family hashref into the database. Updates are delayed, if
861you want the data to be synced to disk immediately, use C<cf::db_sync>.
862
863=item cf::db_put $family => $key => $value
864
865Stores the given C<$value> in the family hash. Updates are delayed, if you
866want the data to be synced to disk immediately, use C<cf::db_sync>.
867
868=item cf::db_dirty
869
870Marks the database as dirty, to be updated at a later time.
871
872=item cf::db_sync
873
874Immediately write the database to disk I<if it is dirty>.
875
876=cut
877
878{
879 my $db;
880 my $path = cf::localdir . "/database.pst";
881
882 sub db_load() {
883 warn "loading database $path\n";#d# remove later
884 $db = stat $path ? Storable::retrieve $path : { };
885 }
886
887 my $pid;
888
889 sub db_save() {
890 warn "saving database $path\n";#d# remove later
891 waitpid $pid, 0 if $pid;
892 if (0 == ($pid = fork)) {
893 $db->{_meta}{version} = 1;
894 Storable::nstore $db, "$path~";
895 rename "$path~", $path;
896 cf::_exit 0 if defined $pid;
897 }
898 }
899
900 my $dirty;
901
902 sub db_sync() {
903 db_save if $dirty;
904 undef $dirty;
905 }
906
907 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub {
908 db_sync;
909 });
910
911 sub db_dirty() {
912 $dirty = 1;
913 $idle->start;
914 }
915
916 sub db_get($;$) {
917 @_ >= 2
918 ? $db->{$_[0]}{$_[1]}
919 : ($db->{$_[0]} ||= { })
920 }
921
922 sub db_put($$;$) {
923 if (@_ >= 3) {
924 $db->{$_[0]}{$_[1]} = $_[2];
925 } else {
926 $db->{$_[0]} = $_[1];
927 }
928 db_dirty;
929 }
930
931 attach_global
932 prio => 10000,
933 on_cleanup => sub {
934 db_sync;
935 },
936 ;
937}
938
939#############################################################################
900# the server's main() 940# the server's main()
901 941
902sub main { 942sub main {
943 db_load;
903 load_extensions; 944 load_extensions;
904 Event::loop; 945 Event::loop;
905} 946}
906 947
907############################################################################# 948#############################################################################
908# initialisation 949# initialisation
950
951sub _perl_reload(&) {
952 my ($msg) = @_;
953
954 $msg->("reloading...");
955
956 eval {
957 # cancel all watchers
958 $_->cancel for Event::all_watchers;
959
960 # unload all extensions
961 for (@exts) {
962 $msg->("unloading <$_>");
963 unload_extension $_;
964 }
965
966 # unload all modules loaded from $LIBDIR
967 while (my ($k, $v) = each %INC) {
968 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
969
970 $msg->("removing <$k>");
971 delete $INC{$k};
972
973 $k =~ s/\.pm$//;
974 $k =~ s/\//::/g;
975
976 if (my $cb = $k->can ("unload_module")) {
977 $cb->();
978 }
979
980 Symbol::delete_package $k;
981 }
982
983 # sync database to disk
984 cf::db_sync;
985
986 # get rid of safe::, as good as possible
987 Symbol::delete_package "safe::$_"
988 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
989
990 # remove register_script_function callbacks
991 # TODO
992
993 # unload cf.pm "a bit"
994 delete $INC{"cf.pm"};
995
996 # don't, removes xs symbols, too,
997 # and global variables created in xs
998 #Symbol::delete_package __PACKAGE__;
999
1000 # reload cf.pm
1001 $msg->("reloading cf.pm");
1002 require cf;
1003
1004 # load database again
1005 cf::db_load;
1006
1007 # load extensions
1008 $msg->("load extensions");
1009 cf::load_extensions;
1010
1011 # reattach attachments to objects
1012 $msg->("reattach");
1013 _global_reattach;
1014 };
1015 $msg->($@) if $@;
1016
1017 $msg->("reloaded");
1018};
1019
1020sub perl_reload() {
1021 _perl_reload {
1022 warn $_[0];
1023 print "$_[0]\n";
1024 };
1025}
1026
1027register_command "perl-reload", 0, sub {
1028 my ($who, $arg) = @_;
1029
1030 if ($who->flag (FLAG_WIZ)) {
1031 _perl_reload {
1032 warn $_[0];
1033 $who->message ($_[0]);
1034 };
1035 }
1036};
909 1037
910register "<global>", __PACKAGE__; 1038register "<global>", __PACKAGE__;
911 1039
912unshift @INC, $LIBDIR; 1040unshift @INC, $LIBDIR;
913 1041

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines