… | |
… | |
21 | |
21 | |
22 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
23 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
24 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
25 | |
25 | |
|
|
26 | our %CFG; |
|
|
27 | |
|
|
28 | ############################################################################# |
|
|
29 | |
|
|
30 | =head2 GLOBAL VARIABLES |
|
|
31 | |
|
|
32 | =over 4 |
|
|
33 | |
|
|
34 | =item $cf::LIBDIR |
|
|
35 | |
|
|
36 | The perl library directory, where extensions and cf-specific modules can |
|
|
37 | be found. It will be added to C<@INC> automatically. |
|
|
38 | |
|
|
39 | =item $cf::TICK |
|
|
40 | |
|
|
41 | The interval between server ticks, in seconds. |
|
|
42 | |
|
|
43 | =item %cf::CFG |
|
|
44 | |
|
|
45 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
|
|
46 | from wherever your confdir points to. |
|
|
47 | |
|
|
48 | =back |
|
|
49 | |
|
|
50 | =cut |
|
|
51 | |
26 | BEGIN { |
52 | BEGIN { |
27 | *CORE::GLOBAL::warn = sub { |
53 | *CORE::GLOBAL::warn = sub { |
28 | my $msg = join "", @_; |
54 | my $msg = join "", @_; |
29 | $msg .= "\n" |
55 | $msg .= "\n" |
30 | unless $msg =~ /\n$/; |
56 | unless $msg =~ /\n$/; |
… | |
… | |
51 | my @exts; |
77 | my @exts; |
52 | my @hook; |
78 | my @hook; |
53 | my %command; |
79 | my %command; |
54 | my %extcmd; |
80 | my %extcmd; |
55 | |
81 | |
56 | ############################################################################# |
82 | =head2 UTILITY FUNCTIONS |
57 | # utility functions |
83 | |
|
|
84 | =over 4 |
|
|
85 | |
|
|
86 | =cut |
58 | |
87 | |
59 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
88 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
89 | |
|
|
90 | =item $ref = cf::from_json $json |
|
|
91 | |
|
|
92 | Converts a JSON string into the corresponding perl data structure. |
|
|
93 | |
|
|
94 | =cut |
60 | |
95 | |
61 | sub from_json($) { |
96 | sub from_json($) { |
62 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
97 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
63 | JSON::Syck::Load $_[0] |
98 | JSON::Syck::Load $_[0] |
64 | } |
99 | } |
65 | |
100 | |
|
|
101 | =item $json = cf::to_json $ref |
|
|
102 | |
|
|
103 | Converts a perl data structure into its JSON representation. |
|
|
104 | |
|
|
105 | =cut |
|
|
106 | |
66 | sub to_json($) { |
107 | sub to_json($) { |
67 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
108 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
68 | JSON::Syck::Dump $_[0] |
109 | JSON::Syck::Dump $_[0] |
69 | } |
110 | } |
70 | |
111 | |
71 | ############################################################################# |
112 | =back |
72 | # "new" plug-in system |
|
|
73 | |
113 | |
|
|
114 | =cut |
|
|
115 | |
|
|
116 | ############################################################################# |
|
|
117 | |
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
118 | =head2 EVENTS AND OBJECT ATTACHMENTS |
75 | |
119 | |
76 | =over 4 |
120 | =over 4 |
77 | |
121 | |
78 | =item $object->attach ($attachment, key => $value...) |
122 | =item $object->attach ($attachment, key => $value...) |
79 | |
123 | |
… | |
… | |
379 | removed in future versions), and there is no public API to access override |
423 | removed in future versions), and there is no public API to access override |
380 | results (if you must, access C<@cf::invoke_results> directly). |
424 | results (if you must, access C<@cf::invoke_results> directly). |
381 | |
425 | |
382 | =back |
426 | =back |
383 | |
427 | |
384 | =head2 methods valid for all pointers |
428 | =cut |
|
|
429 | |
|
|
430 | ############################################################################# |
|
|
431 | |
|
|
432 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
385 | |
433 | |
386 | =over 4 |
434 | =over 4 |
387 | |
435 | |
388 | =item $object->valid |
436 | =item $object->valid, $player->valid, $map->valid |
389 | |
|
|
390 | =item $player->valid |
|
|
391 | |
|
|
392 | =item $map->valid |
|
|
393 | |
437 | |
394 | Just because you have a perl object does not mean that the corresponding |
438 | Just because you have a perl object does not mean that the corresponding |
395 | C-level object still exists. If you try to access an object that has no |
439 | C-level object still exists. If you try to access an object that has no |
396 | valid C counterpart anymore you get an exception at runtime. This method |
440 | valid C counterpart anymore you get an exception at runtime. This method |
397 | can be used to test for existence of the C object part without causing an |
441 | can be used to test for existence of the C object part without causing an |
… | |
… | |
704 | } |
748 | } |
705 | }, |
749 | }, |
706 | ; |
750 | ; |
707 | |
751 | |
708 | ############################################################################# |
752 | ############################################################################# |
709 | # core extensions - in perl |
753 | |
|
|
754 | =head2 CORE EXTENSIONS |
|
|
755 | |
|
|
756 | Functions and methods that extend core crossfire objects. |
|
|
757 | |
|
|
758 | =over 4 |
710 | |
759 | |
711 | =item cf::player::exists $login |
760 | =item cf::player::exists $login |
712 | |
761 | |
713 | Returns true when the given account exists. |
762 | Returns true when the given account exists. |
714 | |
763 | |
… | |
… | |
754 | $msg{msgid} = $id; |
803 | $msg{msgid} = $id; |
755 | |
804 | |
756 | $self->send ("ext " . to_json \%msg); |
805 | $self->send ("ext " . to_json \%msg); |
757 | } |
806 | } |
758 | |
807 | |
|
|
808 | =back |
|
|
809 | |
|
|
810 | =cut |
|
|
811 | |
759 | ############################################################################# |
812 | ############################################################################# |
760 | # map scripting support |
813 | |
|
|
814 | =head2 SAFE SCRIPTING |
|
|
815 | |
|
|
816 | Functions that provide a safe environment to compile and execute |
|
|
817 | snippets of perl code without them endangering the safety of the server |
|
|
818 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
819 | is not available in the safe scripting environment, and the number of |
|
|
820 | functions and methods that cna be called is greatly reduced. |
|
|
821 | |
|
|
822 | =cut |
761 | |
823 | |
762 | our $safe = new Safe "safe"; |
824 | our $safe = new Safe "safe"; |
763 | our $safe_hole = new Safe::Hole; |
825 | our $safe_hole = new Safe::Hole; |
764 | |
826 | |
765 | $SIG{FPE} = 'IGNORE'; |
827 | $SIG{FPE} = 'IGNORE'; |
766 | |
828 | |
767 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
829 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
768 | |
830 | |
769 | # here we export the classes and methods available to script code |
831 | # here we export the classes and methods available to script code |
|
|
832 | |
|
|
833 | =pod |
|
|
834 | |
|
|
835 | The following fucntions and emthods are available within a safe environment: |
|
|
836 | |
|
|
837 | cf::object contr pay_amount pay_player |
|
|
838 | cf::object::player player |
|
|
839 | cf::player peaceful |
|
|
840 | |
|
|
841 | =cut |
770 | |
842 | |
771 | for ( |
843 | for ( |
772 | ["cf::object" => qw(contr pay_amount pay_player)], |
844 | ["cf::object" => qw(contr pay_amount pay_player)], |
773 | ["cf::object::player" => qw(player)], |
845 | ["cf::object::player" => qw(player)], |
774 | ["cf::player" => qw(peaceful)], |
846 | ["cf::player" => qw(peaceful)], |
… | |
… | |
777 | my ($pkg, @funs) = @$_; |
849 | my ($pkg, @funs) = @$_; |
778 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
850 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
779 | for @funs; |
851 | for @funs; |
780 | } |
852 | } |
781 | |
853 | |
|
|
854 | =over 4 |
|
|
855 | |
|
|
856 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
857 | |
|
|
858 | Compiled and executes the given perl code snippet. additional var/value |
|
|
859 | pairs result in temporary local (my) scalar variables of the given name |
|
|
860 | that are available in the code snippet. Example: |
|
|
861 | |
|
|
862 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
863 | |
|
|
864 | =cut |
|
|
865 | |
782 | sub safe_eval($;@) { |
866 | sub safe_eval($;@) { |
783 | my ($code, %vars) = @_; |
867 | my ($code, %vars) = @_; |
784 | |
868 | |
785 | my $qcode = $code; |
869 | my $qcode = $code; |
786 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
870 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
808 | } |
892 | } |
809 | |
893 | |
810 | wantarray ? @res : $res[0] |
894 | wantarray ? @res : $res[0] |
811 | } |
895 | } |
812 | |
896 | |
|
|
897 | =item cf::register_script_function $function => $cb |
|
|
898 | |
|
|
899 | Register a function that can be called from within map/npc scripts. The |
|
|
900 | function should be reasonably secure and should be put into a package name |
|
|
901 | like the extension. |
|
|
902 | |
|
|
903 | Example: register a function that gets called whenever a map script calls |
|
|
904 | C<rent::overview>, as used by the C<rent> extension. |
|
|
905 | |
|
|
906 | cf::register_script_function "rent::overview" => sub { |
|
|
907 | ... |
|
|
908 | }; |
|
|
909 | |
|
|
910 | =cut |
|
|
911 | |
813 | sub register_script_function { |
912 | sub register_script_function { |
814 | my ($fun, $cb) = @_; |
913 | my ($fun, $cb) = @_; |
815 | |
914 | |
816 | no strict 'refs'; |
915 | no strict 'refs'; |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
916 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
818 | } |
917 | } |
|
|
918 | |
|
|
919 | =back |
|
|
920 | |
|
|
921 | =cut |
819 | |
922 | |
820 | ############################################################################# |
923 | ############################################################################# |
821 | |
924 | |
822 | =head2 EXTENSION DATABASE SUPPORT |
925 | =head2 EXTENSION DATABASE SUPPORT |
823 | |
926 | |