… | |
… | |
808 | } |
808 | } |
809 | |
809 | |
810 | wantarray ? @res : $res[0] |
810 | wantarray ? @res : $res[0] |
811 | } |
811 | } |
812 | |
812 | |
|
|
813 | =item cf::register_script_function $function => $cb |
|
|
814 | |
|
|
815 | Register a function that can be called from within map/npc scripts. The |
|
|
816 | function should be reasonably secure and should be put into a package name |
|
|
817 | like the extension. |
|
|
818 | |
|
|
819 | Example: register a function that gets called whenever a map script calls |
|
|
820 | C<rent::overview>, as used by the C<rent> extension. |
|
|
821 | |
|
|
822 | cf::register_script_function "rent::overview" => sub { |
|
|
823 | ... |
|
|
824 | }; |
|
|
825 | |
|
|
826 | =cut |
|
|
827 | |
813 | sub register_script_function { |
828 | sub register_script_function { |
814 | my ($fun, $cb) = @_; |
829 | my ($fun, $cb) = @_; |
815 | |
830 | |
816 | no strict 'refs'; |
831 | no strict 'refs'; |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
832 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
… | |
… | |
860 | |
875 | |
861 | =cut |
876 | =cut |
862 | |
877 | |
863 | { |
878 | { |
864 | my $db; |
879 | my $db; |
865 | my $path = cf::datadir . "/database.pst"; |
880 | my $path = cf::localdir . "/database.pst"; |
866 | |
881 | |
867 | sub db_load() { |
882 | sub db_load() { |
868 | warn "loading database $path\n";#d# remove later |
883 | warn "loading database $path\n";#d# remove later |
869 | $db = stat $path ? Storable::retrieve $path : { }; |
884 | $db = stat $path ? Storable::retrieve $path : { }; |
870 | } |
885 | } |
… | |
… | |
872 | my $pid; |
887 | my $pid; |
873 | |
888 | |
874 | sub db_save() { |
889 | sub db_save() { |
875 | warn "saving database $path\n";#d# remove later |
890 | warn "saving database $path\n";#d# remove later |
876 | waitpid $pid, 0 if $pid; |
891 | waitpid $pid, 0 if $pid; |
877 | unless ($pid = fork) { |
892 | if (0 == ($pid = fork)) { |
878 | $db->{_meta}{version} = 1; |
893 | $db->{_meta}{version} = 1; |
879 | Storable::nstore $db, "$path~"; |
894 | Storable::nstore $db, "$path~"; |
880 | rename "$path~", $path; |
895 | rename "$path~", $path; |
881 | cf::_exit 0 if defined $pid; |
896 | cf::_exit 0 if defined $pid; |
882 | } |
897 | } |
… | |
… | |
910 | } else { |
925 | } else { |
911 | $db->{$_[0]} = $_[1]; |
926 | $db->{$_[0]} = $_[1]; |
912 | } |
927 | } |
913 | db_dirty; |
928 | db_dirty; |
914 | } |
929 | } |
|
|
930 | |
|
|
931 | attach_global |
|
|
932 | prio => 10000, |
|
|
933 | on_cleanup => sub { |
|
|
934 | db_sync; |
|
|
935 | }, |
|
|
936 | ; |
915 | } |
937 | } |
916 | |
938 | |
917 | ############################################################################# |
939 | ############################################################################# |
918 | # the server's main() |
940 | # the server's main() |
919 | |
941 | |