… | |
… | |
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 | } |
… | |
… | |
509 | } |
473 | } |
510 | |
474 | |
511 | sub object_thawer_load { |
475 | sub object_thawer_load { |
512 | my ($filename) = @_; |
476 | my ($filename) = @_; |
513 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
514 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
515 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
516 | |
490 | |
517 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
518 | } |
492 | } |
519 | |
493 | |
520 | attach_to_objects |
494 | attach_to_objects |
521 | prio => -1000000, |
495 | prio => -1000000, |
522 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
668 | load_extension $ext; |
642 | load_extension $ext; |
669 | 1 |
643 | 1 |
670 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
671 | } |
645 | } |
672 | } |
646 | } |
673 | |
|
|
674 | sub _perl_reload(&) { |
|
|
675 | my ($msg) = @_; |
|
|
676 | |
|
|
677 | $msg->("reloading..."); |
|
|
678 | |
|
|
679 | eval { |
|
|
680 | # 1. cancel all watchers |
|
|
681 | $_->cancel for Event::all_watchers; |
|
|
682 | |
|
|
683 | # 2. unload all extensions |
|
|
684 | for (@exts) { |
|
|
685 | $msg->("unloading <$_>"); |
|
|
686 | unload_extension $_; |
|
|
687 | } |
|
|
688 | |
|
|
689 | # 3. unload all modules loaded from $LIBDIR |
|
|
690 | while (my ($k, $v) = each %INC) { |
|
|
691 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
692 | |
|
|
693 | $msg->("removing <$k>"); |
|
|
694 | delete $INC{$k}; |
|
|
695 | |
|
|
696 | $k =~ s/\.pm$//; |
|
|
697 | $k =~ s/\//::/g; |
|
|
698 | |
|
|
699 | if (my $cb = $k->can ("unload_module")) { |
|
|
700 | $cb->(); |
|
|
701 | } |
|
|
702 | |
|
|
703 | Symbol::delete_package $k; |
|
|
704 | } |
|
|
705 | |
|
|
706 | # 4. get rid of safe::, as good as possible |
|
|
707 | Symbol::delete_package "safe::$_" |
|
|
708 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
709 | |
|
|
710 | # 5. remove register_script_function callbacks |
|
|
711 | # TODO |
|
|
712 | |
|
|
713 | # 6. unload cf.pm "a bit" |
|
|
714 | delete $INC{"cf.pm"}; |
|
|
715 | |
|
|
716 | # don't, removes xs symbols, too, |
|
|
717 | # and global variables created in xs |
|
|
718 | #Symbol::delete_package __PACKAGE__; |
|
|
719 | |
|
|
720 | # 7. reload cf.pm |
|
|
721 | $msg->("reloading cf.pm"); |
|
|
722 | require cf; |
|
|
723 | }; |
|
|
724 | $msg->($@) if $@; |
|
|
725 | |
|
|
726 | $msg->("reloaded"); |
|
|
727 | }; |
|
|
728 | |
|
|
729 | sub perl_reload() { |
|
|
730 | _perl_reload { |
|
|
731 | warn $_[0]; |
|
|
732 | print "$_[0]\n"; |
|
|
733 | }; |
|
|
734 | } |
|
|
735 | |
|
|
736 | register_command "perl-reload", 0, sub { |
|
|
737 | my ($who, $arg) = @_; |
|
|
738 | |
|
|
739 | if ($who->flag (FLAG_WIZ)) { |
|
|
740 | _perl_reload { |
|
|
741 | warn $_[0]; |
|
|
742 | $who->message ($_[0]); |
|
|
743 | }; |
|
|
744 | } |
|
|
745 | }; |
|
|
746 | |
647 | |
747 | ############################################################################# |
648 | ############################################################################# |
748 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
749 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
750 | |
651 | |
… | |
… | |
915 | no strict 'refs'; |
816 | no strict 'refs'; |
916 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
917 | } |
818 | } |
918 | |
819 | |
919 | ############################################################################# |
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 | kill 9, $$ if defined $pid; #d# remove when binary updated |
|
|
882 | cf::_exit 0 if defined $pid; |
|
|
883 | } |
|
|
884 | } |
|
|
885 | |
|
|
886 | my $dirty; |
|
|
887 | |
|
|
888 | sub db_sync() { |
|
|
889 | db_save if $dirty; |
|
|
890 | undef $dirty; |
|
|
891 | } |
|
|
892 | |
|
|
893 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
894 | db_sync; |
|
|
895 | }); |
|
|
896 | |
|
|
897 | sub db_dirty() { |
|
|
898 | $dirty = 1; |
|
|
899 | $idle->start; |
|
|
900 | } |
|
|
901 | |
|
|
902 | sub db_get($;$) { |
|
|
903 | @_ >= 2 |
|
|
904 | ? $db->{$_[0]}{$_[1]} |
|
|
905 | : ($db->{$_[0]} ||= { }) |
|
|
906 | } |
|
|
907 | |
|
|
908 | sub db_put($$;$) { |
|
|
909 | if (@_ >= 3) { |
|
|
910 | $db->{$_[0]}{$_[1]} = $_[2]; |
|
|
911 | } else { |
|
|
912 | $db->{$_[0]} = $_[1]; |
|
|
913 | } |
|
|
914 | db_dirty; |
|
|
915 | } |
|
|
916 | |
|
|
917 | attach_global |
|
|
918 | prio => 10000, |
|
|
919 | on_cleanup => sub { |
|
|
920 | db_sync; |
|
|
921 | }, |
|
|
922 | ; |
|
|
923 | } |
|
|
924 | |
|
|
925 | ############################################################################# |
920 | # the server's main() |
926 | # the server's main() |
921 | |
927 | |
922 | sub main { |
928 | sub main { |
|
|
929 | db_load; |
|
|
930 | load_extensions; |
923 | Event::loop; |
931 | Event::loop; |
924 | } |
932 | } |
925 | |
933 | |
926 | ############################################################################# |
934 | ############################################################################# |
927 | # initialisation |
935 | # initialisation |
928 | |
936 | |
|
|
937 | sub _perl_reload(&) { |
|
|
938 | my ($msg) = @_; |
|
|
939 | |
|
|
940 | $msg->("reloading..."); |
|
|
941 | |
|
|
942 | eval { |
|
|
943 | # cancel all watchers |
|
|
944 | $_->cancel for Event::all_watchers; |
|
|
945 | |
|
|
946 | # unload all extensions |
|
|
947 | for (@exts) { |
|
|
948 | $msg->("unloading <$_>"); |
|
|
949 | unload_extension $_; |
|
|
950 | } |
|
|
951 | |
|
|
952 | # unload all modules loaded from $LIBDIR |
|
|
953 | while (my ($k, $v) = each %INC) { |
|
|
954 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
955 | |
|
|
956 | $msg->("removing <$k>"); |
|
|
957 | delete $INC{$k}; |
|
|
958 | |
|
|
959 | $k =~ s/\.pm$//; |
|
|
960 | $k =~ s/\//::/g; |
|
|
961 | |
|
|
962 | if (my $cb = $k->can ("unload_module")) { |
|
|
963 | $cb->(); |
|
|
964 | } |
|
|
965 | |
|
|
966 | Symbol::delete_package $k; |
|
|
967 | } |
|
|
968 | |
|
|
969 | # sync database to disk |
|
|
970 | cf::db_sync; |
|
|
971 | |
|
|
972 | # get rid of safe::, as good as possible |
|
|
973 | Symbol::delete_package "safe::$_" |
|
|
974 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
975 | |
|
|
976 | # remove register_script_function callbacks |
|
|
977 | # TODO |
|
|
978 | |
|
|
979 | # unload cf.pm "a bit" |
|
|
980 | delete $INC{"cf.pm"}; |
|
|
981 | |
|
|
982 | # don't, removes xs symbols, too, |
|
|
983 | # and global variables created in xs |
|
|
984 | #Symbol::delete_package __PACKAGE__; |
|
|
985 | |
|
|
986 | # reload cf.pm |
|
|
987 | $msg->("reloading cf.pm"); |
|
|
988 | require cf; |
|
|
989 | |
|
|
990 | # load database again |
|
|
991 | cf::db_load; |
|
|
992 | |
|
|
993 | # load extensions |
|
|
994 | $msg->("load extensions"); |
|
|
995 | cf::load_extensions; |
|
|
996 | |
|
|
997 | # reattach attachments to objects |
|
|
998 | $msg->("reattach"); |
|
|
999 | _global_reattach; |
|
|
1000 | }; |
|
|
1001 | $msg->($@) if $@; |
|
|
1002 | |
|
|
1003 | $msg->("reloaded"); |
|
|
1004 | }; |
|
|
1005 | |
|
|
1006 | sub perl_reload() { |
|
|
1007 | _perl_reload { |
|
|
1008 | warn $_[0]; |
|
|
1009 | print "$_[0]\n"; |
|
|
1010 | }; |
|
|
1011 | } |
|
|
1012 | |
|
|
1013 | register_command "perl-reload", 0, sub { |
|
|
1014 | my ($who, $arg) = @_; |
|
|
1015 | |
|
|
1016 | if ($who->flag (FLAG_WIZ)) { |
|
|
1017 | _perl_reload { |
|
|
1018 | warn $_[0]; |
|
|
1019 | $who->message ($_[0]); |
|
|
1020 | }; |
|
|
1021 | } |
|
|
1022 | }; |
|
|
1023 | |
929 | register "<global>", __PACKAGE__; |
1024 | register "<global>", __PACKAGE__; |
930 | |
1025 | |
931 | unshift @INC, $LIBDIR; |
1026 | unshift @INC, $LIBDIR; |
932 | |
|
|
933 | load_extensions; |
|
|
934 | |
1027 | |
935 | $TICK_WATCHER = Event->timer ( |
1028 | $TICK_WATCHER = Event->timer ( |
936 | prio => 1, |
1029 | prio => 1, |
937 | at => $NEXT_TICK || 1, |
1030 | at => $NEXT_TICK || 1, |
938 | cb => sub { |
1031 | cb => sub { |
… | |
… | |
947 | $TICK_WATCHER->at ($NEXT_TICK); |
1040 | $TICK_WATCHER->at ($NEXT_TICK); |
948 | $TICK_WATCHER->start; |
1041 | $TICK_WATCHER->start; |
949 | }, |
1042 | }, |
950 | ); |
1043 | ); |
951 | |
1044 | |
952 | _reload_2; |
|
|
953 | |
|
|
954 | 1 |
1045 | 1 |
955 | |
1046 | |