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.21 by root, Sun Jul 16 17:51:40 2006 UTC vs.
Revision 1.26 by root, Thu Jul 20 22:03:36 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
5use Storable; 5use Storable;
6use Opcode;
7use Safe;
8use Safe::Hole;
6 9
7use Event; 10use Event;
8$Event::Eval = 1; # no idea why this is required, but it is 11$Event::Eval = 1; # no idea why this is required, but it is
9 12
10use strict; 13use strict;
61prop_gen MAP_PROP => "cf::map"; 64prop_gen MAP_PROP => "cf::map";
62prop_gen ARCH_PROP => "cf::arch"; 65prop_gen ARCH_PROP => "cf::arch";
63 66
64# guessed hierarchies 67# guessed hierarchies
65 68
66@cf::object::player::ISA = 'cf::object'; 69@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
67@cf::object::map::ISA = 'cf::object'; 70@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
71
72# we bless all objects into derived classes to force a method lookup
73# within the Safe compartment.
74for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) {
75 no strict 'refs';
76 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
77}
68 78
69$Event::DIED = sub { 79$Event::DIED = sub {
70 warn "error in event callback: @_"; 80 warn "error in event callback: @_";
71}; 81};
72 82
160 170
161 push @exts, $pkg; 171 push @exts, $pkg;
162 $ext_pkg{$base} = $pkg; 172 $ext_pkg{$base} = $pkg;
163 173
164# no strict 'refs'; 174# no strict 'refs';
165# @{"$pkg\::ISA"} = cf::ext::; 175# @{"$pkg\::ISA"} = ext::;
166 176
167 register $base, $pkg; 177 register $base, $pkg;
168} 178}
169 179
170sub unload_extension { 180sub unload_extension {
316# load/save perl data associated with player->ob objects 326# load/save perl data associated with player->ob objects
317 327
318*on_player_load = sub { 328*on_player_load = sub {
319 my ($ob, $path) = @_; 329 my ($ob, $path) = @_;
320 330
321 if (open my $fh, "<:raw", "$path.cfperl") {
322
323 #d##TODO#remove
324
325 my $data = Storable::thaw do { local $/; <$fh> };
326
327 $data->{version} <= 1
328 or return; # too new
329
330 %$ob = %{$data->{ob}};
331 return;
332 }
333
334 for my $o ($ob, $ob->inv) { 331 for my $o ($ob, $ob->inv) {
335 if (my $value = $o->get_ob_key_value ("_perl_data")) { 332 if (my $value = $o->get_ob_key_value ("_perl_data")) {
336 $o->set_ob_key_value ("_perl_data"); 333 $o->set_ob_key_value ("_perl_data");
337 334
338 %$o = %{ Storable::thaw pack "H*", $value }; 335 %$o = %{ Storable::thaw pack "H*", $value };
343*on_player_save = sub { 340*on_player_save = sub {
344 my ($ob, $path) = @_; 341 my ($ob, $path) = @_;
345 342
346 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 343 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
347 for grep %$_, $ob, $ob->inv; 344 for grep %$_, $ob, $ob->inv;
348
349 unlink "$path.cfperl";#d##TODO#remove
350}; 345};
346
347#############################################################################
348# core extensions - in perl
349
350=item cf::player::exists $login
351
352Returns true when the given account exists.
353
354=cut
355
356sub cf::player::exists($) {
357 cf::player::find $_[0]
358 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
359}
360
361# rough implementation of a future "reply" method that works
362# with dialog boxes.
363sub cf::object::player::reply($$$;$) {
364 my ($self, $npc, $msg, $flags) = @_;
365
366 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
367
368 if ($self->{record_replies}) {
369 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
370 } else {
371 $msg = $npc->name . " says: $msg" if $npc;
372 $self->message ($msg, $flags);
373 }
374}
375
376#############################################################################
377# map scripting support
378
379our $safe = new Safe "ext";
380our $safe_hole = new Safe::Hole;
381
382$SIG{FPE} = 'IGNORE';
383
384$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
385
386# here we export the classes and methods available to script code
387
388for (
389 ["cf::object" => qw(contr)],
390 ["cf::object::player" => qw(player)],
391 ["cf::player" => qw(peaceful)],
392) {
393 no strict 'refs';
394 my ($pkg, @funs) = @$_;
395 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
396 for @funs;
397}
398
399sub safe_eval($;@) {
400 my ($code, %vars) = @_;
401
402 my $qcode = $code;
403 $qcode =~ s/"/‟/g; # not allowed in #line filenames
404 $qcode =~ s/\n/\\n/g;
405
406 local $_;
407 local @ext::cf::_safe_eval_args = values %vars;
408
409 $code =
410 "do {\n"
411 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
412 . "#line 0 \"{$qcode}\"\n"
413 . $code
414 . "\n}"
415 ;
416
417 sub_generation_inc;
418 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
419 sub_generation_inc;
420
421 wantarray ? @res : $res[0]
422}
423
424sub register_script_function {
425 my ($fun, $cb) = @_;
426
427 no strict 'refs';
428 *{"ext::$fun"} = $safe_hole->wrap ($cb);
429}
430
431#############################################################################
432# initialisation
351 433
352register "<global>", __PACKAGE__; 434register "<global>", __PACKAGE__;
353 435
354unshift @INC, maps_directory "perl"; 436unshift @INC, maps_directory "perl";
355 437

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines