ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.69 by root, Mon Sep 18 01:10:35 2006 UTC vs.
Revision 1.70 by root, Sun Oct 1 10:55:37 2006 UTC

21 21
22our $TICK = MAX_TIME * 1e-6; 22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 23our $TICK_WATCHER;
24our $NEXT_TICK; 24our $NEXT_TICK;
25 25
26our %CFG;
27
28#############################################################################
29
30=head2 GLOBAL VARIABLES
31
32=over 4
33
34=item $cf::LIBDIR
35
36The perl library directory, where extensions and cf-specific modules can
37be found. It will be added to C<@INC> automatically.
38
39=item $cf::TICK
40
41The interval between server ticks, in seconds.
42
43=item %cf::CFG
44
45Configuration for the server, loaded from C</etc/crossfire/config>, or
46from wherever your confdir points to.
47
48=back
49
50=cut
51
26BEGIN { 52BEGIN {
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$/;
51my @exts; 77my @exts;
52my @hook; 78my @hook;
53my %command; 79my %command;
54my %extcmd; 80my %extcmd;
55 81
56############################################################################# 82=head2 UTILITY FUNCTIONS
57# utility functions 83
84=over 4
85
86=cut
58 87
59use JSON::Syck (); # TODO# replace by JSON::PC once working 88use JSON::Syck (); # TODO# replace by JSON::PC once working
89
90=item $ref = cf::from_json $json
91
92Converts a JSON string into the corresponding perl data structure.
93
94=cut
60 95
61sub from_json($) { 96sub 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
103Converts a perl data structure into its JSON representation.
104
105=cut
106
66sub to_json($) { 107sub 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
379removed in future versions), and there is no public API to access override 421removed in future versions), and there is no public API to access override
380results (if you must, access C<@cf::invoke_results> directly). 422results (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
394Just because you have a perl object does not mean that the corresponding 434Just because you have a perl object does not mean that the corresponding
395C-level object still exists. If you try to access an object that has no 435C-level object still exists. If you try to access an object that has no
396valid C counterpart anymore you get an exception at runtime. This method 436valid C counterpart anymore you get an exception at runtime. This method
397can be used to test for existence of the C object part without causing an 437can 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
752Functions 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
713Returns true when the given account exists. 758Returns 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
812Functions that provide a safe environment to compile and execute
813snippets of perl code without them endangering the safety of the server
814itself. Looping constructs, I/O operators and other built-in functionality
815is not available in the safe scripting environment, and the number of
816functions and methods that cna be called is greatly reduced.
817
818=cut
761 819
762our $safe = new Safe "safe"; 820our $safe = new Safe "safe";
763our $safe_hole = new Safe::Hole; 821our $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
831The 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
771for ( 839for (
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
854Compiled and executes the given perl code snippet. additional var/value
855pairs result in temporary local (my) scalar variables of the given name
856that are available in the code snippet. Example:
857
858 my $five = safe_eval '$first + $second', first => 1, second => 4;
859
860=cut
861
782sub safe_eval($;@) { 862sub 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines