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.71 by root, Sun Oct 1 10:59:30 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=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
379removed in future versions), and there is no public API to access override 423removed in future versions), and there is no public API to access override
380results (if you must, access C<@cf::invoke_results> directly). 424results (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
394Just because you have a perl object does not mean that the corresponding 438Just 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 439C-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 440valid 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 441can 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
756Functions 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
713Returns true when the given account exists. 762Returns 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
816Functions that provide a safe environment to compile and execute
817snippets of perl code without them endangering the safety of the server
818itself. Looping constructs, I/O operators and other built-in functionality
819is not available in the safe scripting environment, and the number of
820functions and methods that cna be called is greatly reduced.
821
822=cut
761 823
762our $safe = new Safe "safe"; 824our $safe = new Safe "safe";
763our $safe_hole = new Safe::Hole; 825our $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
835The 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
771for ( 843for (
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
858Compiled and executes the given perl code snippet. additional var/value
859pairs result in temporary local (my) scalar variables of the given name
860that are available in the code snippet. Example:
861
862 my $five = safe_eval '$first + $second', first => 1, second => 4;
863
864=cut
865
782sub safe_eval($;@) { 866sub 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
829 my ($fun, $cb) = @_; 913 my ($fun, $cb) = @_;
830 914
831 no strict 'refs'; 915 no strict 'refs';
832 *{"safe::$fun"} = $safe_hole->wrap ($cb); 916 *{"safe::$fun"} = $safe_hole->wrap ($cb);
833} 917}
918
919=back
920
921=cut
834 922
835############################################################################# 923#############################################################################
836 924
837=head2 EXTENSION DATABASE SUPPORT 925=head2 EXTENSION DATABASE SUPPORT
838 926

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines