… | |
… | |
353 | for (@$callbacks) { |
353 | for (@$callbacks) { |
354 | eval { &{$_->[1]} }; |
354 | eval { &{$_->[1]} }; |
355 | |
355 | |
356 | if ($@) { |
356 | if ($@) { |
357 | warn "$@"; |
357 | warn "$@"; |
358 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
359 | override; |
359 | override; |
360 | } |
360 | } |
361 | |
361 | |
362 | return 1 if $override; |
362 | return 1 if $override; |
363 | } |
363 | } |
… | |
… | |
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::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 | ############################################################################# |
900 | # the server's main() |
925 | # the server's main() |
901 | |
926 | |
902 | sub main { |
927 | sub main { |
|
|
928 | db_load; |
903 | load_extensions; |
929 | load_extensions; |
904 | Event::loop; |
930 | Event::loop; |
905 | } |
931 | } |
906 | |
932 | |
907 | ############################################################################# |
933 | ############################################################################# |
908 | # initialisation |
934 | # initialisation |
|
|
935 | |
|
|
936 | sub _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 | |
|
|
1005 | sub perl_reload() { |
|
|
1006 | _perl_reload { |
|
|
1007 | warn $_[0]; |
|
|
1008 | print "$_[0]\n"; |
|
|
1009 | }; |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | register_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 | }; |
909 | |
1022 | |
910 | register "<global>", __PACKAGE__; |
1023 | register "<global>", __PACKAGE__; |
911 | |
1024 | |
912 | unshift @INC, $LIBDIR; |
1025 | unshift @INC, $LIBDIR; |
913 | |
1026 | |