… | |
… | |
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 | } |
… | |
… | |
476 | } |
440 | } |
477 | } |
441 | } |
478 | } |
442 | } |
479 | |
443 | |
480 | sub object_freezer_save { |
444 | sub object_freezer_save { |
481 | my ($filename, $objs) = @_; |
445 | my ($filename, $rdata, $objs) = @_; |
482 | |
446 | |
483 | if (@$objs) { |
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
|
|
450 | |
484 | open my $fh, ">:raw", "$filename.pst~"; |
451 | if (open my $fh, ">:raw", "$filename~") { |
|
|
452 | chmod SAVE_MODE, $fh; |
|
|
453 | syswrite $fh, $$rdata; |
|
|
454 | close $fh; |
|
|
455 | |
|
|
456 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
457 | chmod SAVE_MODE, $fh; |
485 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
486 | close $fh; |
459 | close $fh; |
487 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
488 | rename "$filename.pst~", "$filename.pst"; |
460 | rename "$filename.pst~", "$filename.pst"; |
|
|
461 | } else { |
|
|
462 | unlink "$filename.pst"; |
|
|
463 | } |
|
|
464 | |
|
|
465 | rename "$filename~", $filename; |
|
|
466 | } else { |
|
|
467 | warn "FATAL: $filename~: $!\n"; |
|
|
468 | } |
489 | } else { |
469 | } else { |
|
|
470 | unlink $filename; |
490 | unlink "$filename.pst"; |
471 | unlink "$filename.pst"; |
491 | } |
472 | } |
492 | |
|
|
493 | chmod SAVE_MODE, "$filename~"; |
|
|
494 | rename "$filename~", $filename; |
|
|
495 | } |
473 | } |
496 | |
474 | |
497 | sub object_thawer_load { |
475 | sub object_thawer_load { |
498 | my ($filename) = @_; |
476 | my ($filename) = @_; |
499 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
500 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
501 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
502 | |
490 | |
503 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
504 | } |
492 | } |
505 | |
493 | |
506 | attach_to_objects |
494 | attach_to_objects |
507 | prio => -1000000, |
495 | prio => -1000000, |
508 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
654 | load_extension $ext; |
642 | load_extension $ext; |
655 | 1 |
643 | 1 |
656 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
657 | } |
645 | } |
658 | } |
646 | } |
659 | |
|
|
660 | sub _perl_reload(&) { |
|
|
661 | my ($msg) = @_; |
|
|
662 | |
|
|
663 | $msg->("reloading..."); |
|
|
664 | |
|
|
665 | eval { |
|
|
666 | # 1. cancel all watchers |
|
|
667 | $_->cancel for Event::all_watchers; |
|
|
668 | |
|
|
669 | # 2. unload all extensions |
|
|
670 | for (@exts) { |
|
|
671 | $msg->("unloading <$_>"); |
|
|
672 | unload_extension $_; |
|
|
673 | } |
|
|
674 | |
|
|
675 | # 3. unload all modules loaded from $LIBDIR |
|
|
676 | while (my ($k, $v) = each %INC) { |
|
|
677 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
678 | |
|
|
679 | $msg->("removing <$k>"); |
|
|
680 | delete $INC{$k}; |
|
|
681 | |
|
|
682 | $k =~ s/\.pm$//; |
|
|
683 | $k =~ s/\//::/g; |
|
|
684 | |
|
|
685 | if (my $cb = $k->can ("unload_module")) { |
|
|
686 | $cb->(); |
|
|
687 | } |
|
|
688 | |
|
|
689 | Symbol::delete_package $k; |
|
|
690 | } |
|
|
691 | |
|
|
692 | # 4. get rid of safe::, as good as possible |
|
|
693 | Symbol::delete_package "safe::$_" |
|
|
694 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
695 | |
|
|
696 | # 5. remove register_script_function callbacks |
|
|
697 | # TODO |
|
|
698 | |
|
|
699 | # 6. unload cf.pm "a bit" |
|
|
700 | delete $INC{"cf.pm"}; |
|
|
701 | |
|
|
702 | # don't, removes xs symbols, too, |
|
|
703 | # and global variables created in xs |
|
|
704 | #Symbol::delete_package __PACKAGE__; |
|
|
705 | |
|
|
706 | # 7. reload cf.pm |
|
|
707 | $msg->("reloading cf.pm"); |
|
|
708 | require cf; |
|
|
709 | }; |
|
|
710 | $msg->($@) if $@; |
|
|
711 | |
|
|
712 | $msg->("reloaded"); |
|
|
713 | }; |
|
|
714 | |
|
|
715 | sub perl_reload() { |
|
|
716 | _perl_reload { |
|
|
717 | warn $_[0]; |
|
|
718 | print "$_[0]\n"; |
|
|
719 | }; |
|
|
720 | } |
|
|
721 | |
|
|
722 | register_command "perl-reload", 0, sub { |
|
|
723 | my ($who, $arg) = @_; |
|
|
724 | |
|
|
725 | if ($who->flag (FLAG_WIZ)) { |
|
|
726 | _perl_reload { |
|
|
727 | warn $_[0]; |
|
|
728 | $who->message ($_[0]); |
|
|
729 | }; |
|
|
730 | } |
|
|
731 | }; |
|
|
732 | |
647 | |
733 | ############################################################################# |
648 | ############################################################################# |
734 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
735 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
736 | |
651 | |
… | |
… | |
764 | defined $path or return; |
679 | defined $path or return; |
765 | |
680 | |
766 | unlink "$path.pst"; |
681 | unlink "$path.pst"; |
767 | }; |
682 | }; |
768 | |
683 | |
769 | # old style persistent data, TODO: remove #d# |
|
|
770 | *cf::mapsupport::on_swapin = sub { |
|
|
771 | my ($map) = @_; |
|
|
772 | |
|
|
773 | my $path = $map->tmpname; |
|
|
774 | $path = $map->path unless defined $path; |
|
|
775 | |
|
|
776 | warn "$path.cfperl\n";#d# |
|
|
777 | |
|
|
778 | open my $fh, "<:raw", "$path.cfperl" |
|
|
779 | or return; # no perl data |
|
|
780 | |
|
|
781 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
782 | |
|
|
783 | $data->{version} <= 1 |
|
|
784 | or return; # too new |
|
|
785 | |
|
|
786 | $map->_set_obs ($data->{obs}); |
|
|
787 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
788 | }; |
|
|
789 | |
|
|
790 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
684 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
791 | |
685 | |
792 | ############################################################################# |
686 | ############################################################################# |
793 | # load/save perl data associated with player->ob objects |
687 | # load/save perl data associated with player->ob objects |
794 | |
688 | |
795 | sub all_objects(@) { |
689 | sub all_objects(@) { |
796 | @_, map all_objects ($_->inv), @_ |
690 | @_, map all_objects ($_->inv), @_ |
797 | } |
691 | } |
798 | |
692 | |
|
|
693 | # TODO: compatibility cruft, remove when no longer needed |
799 | attach_to_players |
694 | attach_to_players |
800 | on_load => sub { |
695 | on_load => sub { |
801 | my ($pl, $path) = @_; |
696 | my ($pl, $path) = @_; |
802 | |
697 | |
803 | for my $o (all_objects $pl->ob) { |
698 | for my $o (all_objects $pl->ob) { |
… | |
… | |
921 | no strict 'refs'; |
816 | no strict 'refs'; |
922 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
923 | } |
818 | } |
924 | |
819 | |
925 | ############################################################################# |
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 | ############################################################################# |
926 | # the server's main() |
925 | # the server's main() |
927 | |
926 | |
928 | sub main { |
927 | sub main { |
|
|
928 | db_load; |
|
|
929 | load_extensions; |
929 | Event::loop; |
930 | Event::loop; |
930 | } |
931 | } |
931 | |
932 | |
932 | ############################################################################# |
933 | ############################################################################# |
933 | # initialisation |
934 | # initialisation |
934 | |
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 | }; |
|
|
1022 | |
935 | register "<global>", __PACKAGE__; |
1023 | register "<global>", __PACKAGE__; |
936 | |
1024 | |
937 | unshift @INC, $LIBDIR; |
1025 | unshift @INC, $LIBDIR; |
938 | |
|
|
939 | load_extensions; |
|
|
940 | |
1026 | |
941 | $TICK_WATCHER = Event->timer ( |
1027 | $TICK_WATCHER = Event->timer ( |
942 | prio => 1, |
1028 | prio => 1, |
943 | at => $NEXT_TICK || 1, |
1029 | at => $NEXT_TICK || 1, |
944 | cb => sub { |
1030 | cb => sub { |
… | |
… | |
953 | $TICK_WATCHER->at ($NEXT_TICK); |
1039 | $TICK_WATCHER->at ($NEXT_TICK); |
954 | $TICK_WATCHER->start; |
1040 | $TICK_WATCHER->start; |
955 | }, |
1041 | }, |
956 | ); |
1042 | ); |
957 | |
1043 | |
958 | _reload_2; |
|
|
959 | |
|
|
960 | 1 |
1044 | 1 |
961 | |
1045 | |