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