… | |
… | |
11 | use Event; |
11 | use 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 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | _reload_1; |
16 | _init_vars; |
17 | |
17 | |
18 | our %COMMAND = (); |
18 | our %COMMAND = (); |
19 | our @EVENT; |
19 | our @EVENT; |
20 | our %PROP_TYPE; |
|
|
21 | our %PROP_IDX; |
|
|
22 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
23 | |
21 | |
24 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
25 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
26 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
… | |
… | |
33 | |
31 | |
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 | |
|
|
39 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
40 | |
|
|
41 | # generate property mutators |
|
|
42 | sub 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 | |
|
|
66 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
67 | # CFAPI_OBJECT_ANIMATION? |
|
|
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
69 | |
|
|
70 | prop_gen MAP_PROP => "cf::map"; |
|
|
71 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
72 | |
36 | |
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. |
… | |
… | |
389 | for (@$callbacks) { |
353 | for (@$callbacks) { |
390 | eval { &{$_->[1]} }; |
354 | eval { &{$_->[1]} }; |
391 | |
355 | |
392 | if ($@) { |
356 | if ($@) { |
393 | warn "$@"; |
357 | warn "$@"; |
394 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
395 | override; |
359 | override; |
396 | } |
360 | } |
397 | |
361 | |
398 | return 1 if $override; |
362 | return 1 if $override; |
399 | } |
363 | } |
… | |
… | |
678 | load_extension $ext; |
642 | load_extension $ext; |
679 | 1 |
643 | 1 |
680 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
681 | } |
645 | } |
682 | } |
646 | } |
683 | |
|
|
684 | sub _perl_reload(&) { |
|
|
685 | my ($msg) = @_; |
|
|
686 | |
|
|
687 | $msg->("reloading..."); |
|
|
688 | |
|
|
689 | eval { |
|
|
690 | # 1. cancel all watchers |
|
|
691 | $_->cancel for Event::all_watchers; |
|
|
692 | |
|
|
693 | # 2. unload all extensions |
|
|
694 | for (@exts) { |
|
|
695 | $msg->("unloading <$_>"); |
|
|
696 | unload_extension $_; |
|
|
697 | } |
|
|
698 | |
|
|
699 | # 3. unload all modules loaded from $LIBDIR |
|
|
700 | while (my ($k, $v) = each %INC) { |
|
|
701 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
702 | |
|
|
703 | $msg->("removing <$k>"); |
|
|
704 | delete $INC{$k}; |
|
|
705 | |
|
|
706 | $k =~ s/\.pm$//; |
|
|
707 | $k =~ s/\//::/g; |
|
|
708 | |
|
|
709 | if (my $cb = $k->can ("unload_module")) { |
|
|
710 | $cb->(); |
|
|
711 | } |
|
|
712 | |
|
|
713 | Symbol::delete_package $k; |
|
|
714 | } |
|
|
715 | |
|
|
716 | # 4. get rid of safe::, as good as possible |
|
|
717 | Symbol::delete_package "safe::$_" |
|
|
718 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
719 | |
|
|
720 | # 5. remove register_script_function callbacks |
|
|
721 | # TODO |
|
|
722 | |
|
|
723 | # 6. unload cf.pm "a bit" |
|
|
724 | delete $INC{"cf.pm"}; |
|
|
725 | |
|
|
726 | # don't, removes xs symbols, too, |
|
|
727 | # and global variables created in xs |
|
|
728 | #Symbol::delete_package __PACKAGE__; |
|
|
729 | |
|
|
730 | # 7. reload cf.pm |
|
|
731 | $msg->("reloading cf.pm"); |
|
|
732 | require cf; |
|
|
733 | |
|
|
734 | $msg->("load extensions"); |
|
|
735 | cf::load_extensions; |
|
|
736 | }; |
|
|
737 | $msg->($@) if $@; |
|
|
738 | |
|
|
739 | $msg->("reloaded"); |
|
|
740 | }; |
|
|
741 | |
|
|
742 | sub perl_reload() { |
|
|
743 | _perl_reload { |
|
|
744 | warn $_[0]; |
|
|
745 | print "$_[0]\n"; |
|
|
746 | }; |
|
|
747 | } |
|
|
748 | |
|
|
749 | register_command "perl-reload", 0, sub { |
|
|
750 | my ($who, $arg) = @_; |
|
|
751 | |
|
|
752 | if ($who->flag (FLAG_WIZ)) { |
|
|
753 | _perl_reload { |
|
|
754 | warn $_[0]; |
|
|
755 | $who->message ($_[0]); |
|
|
756 | }; |
|
|
757 | } |
|
|
758 | }; |
|
|
759 | |
647 | |
760 | ############################################################################# |
648 | ############################################################################# |
761 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
762 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
763 | |
651 | |
… | |
… | |
928 | no strict 'refs'; |
816 | no strict 'refs'; |
929 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
930 | } |
818 | } |
931 | |
819 | |
932 | ############################################################################# |
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 | ############################################################################# |
933 | # the server's main() |
925 | # the server's main() |
934 | |
926 | |
935 | sub main { |
927 | sub main { |
|
|
928 | db_load; |
936 | load_extensions; |
929 | load_extensions; |
937 | Event::loop; |
930 | Event::loop; |
938 | } |
931 | } |
939 | |
932 | |
940 | ############################################################################# |
933 | ############################################################################# |
941 | # 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 | }; |
942 | |
1022 | |
943 | register "<global>", __PACKAGE__; |
1023 | register "<global>", __PACKAGE__; |
944 | |
1024 | |
945 | unshift @INC, $LIBDIR; |
1025 | unshift @INC, $LIBDIR; |
946 | |
1026 | |
… | |
… | |
959 | $TICK_WATCHER->at ($NEXT_TICK); |
1039 | $TICK_WATCHER->at ($NEXT_TICK); |
960 | $TICK_WATCHER->start; |
1040 | $TICK_WATCHER->start; |
961 | }, |
1041 | }, |
962 | ); |
1042 | ); |
963 | |
1043 | |
964 | _reload_2; |
|
|
965 | |
|
|
966 | 1 |
1044 | 1 |
967 | |
1045 | |