… | |
… | |
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 | |
|
|
648 | sub _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 | |
|
|
709 | sub perl_reload() { |
|
|
710 | _perl_reload { |
|
|
711 | warn $_[0]; |
|
|
712 | print "$_[0]\n"; |
|
|
713 | }; |
|
|
714 | } |
|
|
715 | |
|
|
716 | register_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 | |
… | |
… | |
895 | no strict 'refs'; |
816 | no strict 'refs'; |
896 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
897 | } |
818 | } |
898 | |
819 | |
899 | ############################################################################# |
820 | ############################################################################# |
|
|
821 | |
|
|
822 | =head2 EXTENSION DATABASE SUPPORT |
|
|
823 | |
|
|
824 | Crossfire maintains a very simple database for extension use. It can |
|
|
825 | currently store anything that can be serialised using Storable, which |
|
|
826 | excludes objects. |
|
|
827 | |
|
|
828 | The parameter C<$family> should best start with the name of the extension |
|
|
829 | using it, it should be unique. |
|
|
830 | |
|
|
831 | =over 4 |
|
|
832 | |
|
|
833 | =item $hashref = cf::db_get $family |
|
|
834 | |
|
|
835 | Return a hashref for use by the extension C<$family>, which can be |
|
|
836 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
837 | C<cf::db_sync>. |
|
|
838 | |
|
|
839 | =item $value = cf::db_get $family => $key |
|
|
840 | |
|
|
841 | Returns a single value from the database |
|
|
842 | |
|
|
843 | =item cf::db_put $family => $hashref |
|
|
844 | |
|
|
845 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
846 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
847 | |
|
|
848 | =item cf::db_put $family => $key => $value |
|
|
849 | |
|
|
850 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
851 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
852 | |
|
|
853 | =item cf::db_dirty |
|
|
854 | |
|
|
855 | Marks the database as dirty, to be updated at a later time. |
|
|
856 | |
|
|
857 | =item cf::db_sync |
|
|
858 | |
|
|
859 | Immediately write the database to disk I<if it is dirty>. |
|
|
860 | |
|
|
861 | =cut |
|
|
862 | |
|
|
863 | { |
|
|
864 | my $db; |
|
|
865 | my $path = cf::datadir . "/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 | unless ($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 | |
|
|
917 | ############################################################################# |
900 | # the server's main() |
918 | # the server's main() |
901 | |
919 | |
902 | sub main { |
920 | sub main { |
|
|
921 | db_load; |
903 | load_extensions; |
922 | load_extensions; |
904 | Event::loop; |
923 | Event::loop; |
905 | } |
924 | } |
906 | |
925 | |
907 | ############################################################################# |
926 | ############################################################################# |
908 | # initialisation |
927 | # initialisation |
|
|
928 | |
|
|
929 | sub _perl_reload(&) { |
|
|
930 | my ($msg) = @_; |
|
|
931 | |
|
|
932 | $msg->("reloading..."); |
|
|
933 | |
|
|
934 | eval { |
|
|
935 | # cancel all watchers |
|
|
936 | $_->cancel for Event::all_watchers; |
|
|
937 | |
|
|
938 | # unload all extensions |
|
|
939 | for (@exts) { |
|
|
940 | $msg->("unloading <$_>"); |
|
|
941 | unload_extension $_; |
|
|
942 | } |
|
|
943 | |
|
|
944 | # unload all modules loaded from $LIBDIR |
|
|
945 | while (my ($k, $v) = each %INC) { |
|
|
946 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
947 | |
|
|
948 | $msg->("removing <$k>"); |
|
|
949 | delete $INC{$k}; |
|
|
950 | |
|
|
951 | $k =~ s/\.pm$//; |
|
|
952 | $k =~ s/\//::/g; |
|
|
953 | |
|
|
954 | if (my $cb = $k->can ("unload_module")) { |
|
|
955 | $cb->(); |
|
|
956 | } |
|
|
957 | |
|
|
958 | Symbol::delete_package $k; |
|
|
959 | } |
|
|
960 | |
|
|
961 | # sync database to disk |
|
|
962 | cf::db_sync; |
|
|
963 | |
|
|
964 | # get rid of safe::, as good as possible |
|
|
965 | Symbol::delete_package "safe::$_" |
|
|
966 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
967 | |
|
|
968 | # remove register_script_function callbacks |
|
|
969 | # TODO |
|
|
970 | |
|
|
971 | # unload cf.pm "a bit" |
|
|
972 | delete $INC{"cf.pm"}; |
|
|
973 | |
|
|
974 | # don't, removes xs symbols, too, |
|
|
975 | # and global variables created in xs |
|
|
976 | #Symbol::delete_package __PACKAGE__; |
|
|
977 | |
|
|
978 | # reload cf.pm |
|
|
979 | $msg->("reloading cf.pm"); |
|
|
980 | require cf; |
|
|
981 | |
|
|
982 | # load database again |
|
|
983 | cf::db_load; |
|
|
984 | |
|
|
985 | # load extensions |
|
|
986 | $msg->("load extensions"); |
|
|
987 | cf::load_extensions; |
|
|
988 | |
|
|
989 | # reattach attachments to objects |
|
|
990 | $msg->("reattach"); |
|
|
991 | _global_reattach; |
|
|
992 | }; |
|
|
993 | $msg->($@) if $@; |
|
|
994 | |
|
|
995 | $msg->("reloaded"); |
|
|
996 | }; |
|
|
997 | |
|
|
998 | sub perl_reload() { |
|
|
999 | _perl_reload { |
|
|
1000 | warn $_[0]; |
|
|
1001 | print "$_[0]\n"; |
|
|
1002 | }; |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | register_command "perl-reload", 0, sub { |
|
|
1006 | my ($who, $arg) = @_; |
|
|
1007 | |
|
|
1008 | if ($who->flag (FLAG_WIZ)) { |
|
|
1009 | _perl_reload { |
|
|
1010 | warn $_[0]; |
|
|
1011 | $who->message ($_[0]); |
|
|
1012 | }; |
|
|
1013 | } |
|
|
1014 | }; |
909 | |
1015 | |
910 | register "<global>", __PACKAGE__; |
1016 | register "<global>", __PACKAGE__; |
911 | |
1017 | |
912 | unshift @INC, $LIBDIR; |
1018 | unshift @INC, $LIBDIR; |
913 | |
1019 | |