… | |
… | |
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 | ############################################################################# |
|
|
115 | |
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
116 | =head2 EVENTS AND OBJECT ATTACHMENTS |
75 | |
117 | |
76 | =over 4 |
118 | =over 4 |
77 | |
119 | |
78 | =item $object->attach ($attachment, key => $value...) |
120 | =item $object->attach ($attachment, key => $value...) |
79 | |
121 | |
… | |
… | |
379 | removed in future versions), and there is no public API to access override |
421 | removed in future versions), and there is no public API to access override |
380 | results (if you must, access C<@cf::invoke_results> directly). |
422 | results (if you must, access C<@cf::invoke_results> directly). |
381 | |
423 | |
382 | =back |
424 | =back |
383 | |
425 | |
384 | =head2 methods valid for all pointers |
426 | ############################################################################# |
|
|
427 | |
|
|
428 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
385 | |
429 | |
386 | =over 4 |
430 | =over 4 |
387 | |
431 | |
388 | =item $object->valid |
432 | =item $object->valid, $player->valid, $map->valid |
389 | |
|
|
390 | =item $player->valid |
|
|
391 | |
|
|
392 | =item $map->valid |
|
|
393 | |
433 | |
394 | Just because you have a perl object does not mean that the corresponding |
434 | 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 |
435 | 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 |
436 | 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 |
437 | can be used to test for existence of the C object part without causing an |
… | |
… | |
704 | } |
744 | } |
705 | }, |
745 | }, |
706 | ; |
746 | ; |
707 | |
747 | |
708 | ############################################################################# |
748 | ############################################################################# |
709 | # core extensions - in perl |
749 | |
|
|
750 | =head2 CORE EXTENSIONS |
|
|
751 | |
|
|
752 | Functions and methods that extend core crossfire objects. |
|
|
753 | |
|
|
754 | =over 4 |
710 | |
755 | |
711 | =item cf::player::exists $login |
756 | =item cf::player::exists $login |
712 | |
757 | |
713 | Returns true when the given account exists. |
758 | Returns true when the given account exists. |
714 | |
759 | |
… | |
… | |
754 | $msg{msgid} = $id; |
799 | $msg{msgid} = $id; |
755 | |
800 | |
756 | $self->send ("ext " . to_json \%msg); |
801 | $self->send ("ext " . to_json \%msg); |
757 | } |
802 | } |
758 | |
803 | |
|
|
804 | =back |
|
|
805 | |
|
|
806 | =cut |
|
|
807 | |
759 | ############################################################################# |
808 | ############################################################################# |
760 | # map scripting support |
809 | |
|
|
810 | =head2 SAFE SCRIPTING |
|
|
811 | |
|
|
812 | Functions that provide a safe environment to compile and execute |
|
|
813 | snippets of perl code without them endangering the safety of the server |
|
|
814 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
815 | is not available in the safe scripting environment, and the number of |
|
|
816 | functions and methods that cna be called is greatly reduced. |
|
|
817 | |
|
|
818 | =cut |
761 | |
819 | |
762 | our $safe = new Safe "safe"; |
820 | our $safe = new Safe "safe"; |
763 | our $safe_hole = new Safe::Hole; |
821 | our $safe_hole = new Safe::Hole; |
764 | |
822 | |
765 | $SIG{FPE} = 'IGNORE'; |
823 | $SIG{FPE} = 'IGNORE'; |
766 | |
824 | |
767 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
825 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
768 | |
826 | |
769 | # here we export the classes and methods available to script code |
827 | # here we export the classes and methods available to script code |
|
|
828 | |
|
|
829 | =pod |
|
|
830 | |
|
|
831 | The following fucntions and emthods are available within a safe environment: |
|
|
832 | |
|
|
833 | cf::object contr pay_amount pay_player |
|
|
834 | cf::object::player player |
|
|
835 | cf::player peaceful |
|
|
836 | |
|
|
837 | =cut |
770 | |
838 | |
771 | for ( |
839 | for ( |
772 | ["cf::object" => qw(contr pay_amount pay_player)], |
840 | ["cf::object" => qw(contr pay_amount pay_player)], |
773 | ["cf::object::player" => qw(player)], |
841 | ["cf::object::player" => qw(player)], |
774 | ["cf::player" => qw(peaceful)], |
842 | ["cf::player" => qw(peaceful)], |
… | |
… | |
777 | my ($pkg, @funs) = @$_; |
845 | my ($pkg, @funs) = @$_; |
778 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
846 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
779 | for @funs; |
847 | for @funs; |
780 | } |
848 | } |
781 | |
849 | |
|
|
850 | =over 4 |
|
|
851 | |
|
|
852 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
853 | |
|
|
854 | Compiled and executes the given perl code snippet. additional var/value |
|
|
855 | pairs result in temporary local (my) scalar variables of the given name |
|
|
856 | that are available in the code snippet. Example: |
|
|
857 | |
|
|
858 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
859 | |
|
|
860 | =cut |
|
|
861 | |
782 | sub safe_eval($;@) { |
862 | sub safe_eval($;@) { |
783 | my ($code, %vars) = @_; |
863 | my ($code, %vars) = @_; |
784 | |
864 | |
785 | my $qcode = $code; |
865 | my $qcode = $code; |
786 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
866 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
829 | my ($fun, $cb) = @_; |
909 | my ($fun, $cb) = @_; |
830 | |
910 | |
831 | no strict 'refs'; |
911 | no strict 'refs'; |
832 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
912 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
833 | } |
913 | } |
|
|
914 | |
|
|
915 | =back |
834 | |
916 | |
835 | ############################################################################# |
917 | ############################################################################# |
836 | |
918 | |
837 | =head2 EXTENSION DATABASE SUPPORT |
919 | =head2 EXTENSION DATABASE SUPPORT |
838 | |
920 | |