--- deliantra/server/lib/cf.pm 2006/07/16 17:15:39 1.19 +++ deliantra/server/lib/cf.pm 2006/07/20 22:03:36 1.26 @@ -3,6 +3,9 @@ use Symbol; use List::Util; use Storable; +use Opcode; +use Safe; +use Safe::Hole; use Event; $Event::Eval = 1; # no idea why this is required, but it is @@ -63,8 +66,15 @@ # guessed hierarchies -@cf::object::player::ISA = 'cf::object'; -@cf::object::map::ISA = 'cf::object'; +@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; +@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; + +# we bless all objects into derived classes to force a method lookup +# within the Safe compartment. +for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { + no strict 'refs'; + @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; +} $Event::DIED = sub { warn "error in event callback: @_"; @@ -162,7 +172,7 @@ $ext_pkg{$base} = $pkg; # no strict 'refs'; -# @{"$pkg\::ISA"} = cf::ext::; +# @{"$pkg\::ISA"} = ext::; register $base, $pkg; } @@ -172,6 +182,10 @@ warn "removing extension $pkg\n"; + if (my $cb = $pkg->can ("on_unload")) { + $cb->($pkg); + } + # remove hooks for my $idx (0 .. $#EVENT) { delete $hook[$idx]{$pkg}; @@ -218,11 +232,13 @@ warn "reloading...\n"; eval { + $_->cancel for Event::all_watchers; + unload_extension $_ for @exts; delete $INC{"cf.pm"}; # don't, removes xs symbols, too - #Symbol::delete_package $pkg; + #Symbol::delete_package __PACKAGE__; require cf; }; @@ -312,19 +328,6 @@ *on_player_load = sub { my ($ob, $path) = @_; - if (open my $fh, "<:raw", "$path.cfperl") { - - #d##TODO#remove - - my $data = Storable::thaw do { local $/; <$fh> }; - - $data->{version} <= 1 - or return; # too new - - %$ob = %{$data->{ob}}; - return; - } - for my $o ($ob, $ob->inv) { if (my $value = $o->get_ob_key_value ("_perl_data")) { $o->set_ob_key_value ("_perl_data"); @@ -339,10 +342,95 @@ $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) for grep %$_, $ob, $ob->inv; - - unlink "$path.cfperl";#d##TODO#remove }; +############################################################################# +# core extensions - in perl + +=item cf::player::exists $login + +Returns true when the given account exists. + +=cut + +sub cf::player::exists($) { + cf::player::find $_[0] + or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; +} + +# rough implementation of a future "reply" method that works +# with dialog boxes. +sub cf::object::player::reply($$$;$) { + my ($self, $npc, $msg, $flags) = @_; + + $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; + + if ($self->{record_replies}) { + push @{ $self->{record_replies} }, [$npc, $msg, $flags]; + } else { + $msg = $npc->name . " says: $msg" if $npc; + $self->message ($msg, $flags); + } +} + +############################################################################# +# map scripting support + +our $safe = new Safe "ext"; +our $safe_hole = new Safe::Hole; + +$SIG{FPE} = 'IGNORE'; + +$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); + +# here we export the classes and methods available to script code + +for ( + ["cf::object" => qw(contr)], + ["cf::object::player" => qw(player)], + ["cf::player" => qw(peaceful)], +) { + no strict 'refs'; + my ($pkg, @funs) = @$_; + *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) + for @funs; +} + +sub safe_eval($;@) { + my ($code, %vars) = @_; + + my $qcode = $code; + $qcode =~ s/"/‟/g; # not allowed in #line filenames + $qcode =~ s/\n/\\n/g; + + local $_; + local @ext::cf::_safe_eval_args = values %vars; + + $code = + "do {\n" + . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" + . "#line 0 \"{$qcode}\"\n" + . $code + . "\n}" + ; + + sub_generation_inc; + my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); + sub_generation_inc; + + wantarray ? @res : $res[0] +} + +sub register_script_function { + my ($fun, $cb) = @_; + + no strict 'refs'; + *{"ext::$fun"} = $safe_hole->wrap ($cb); +} + +############################################################################# +# initialisation + register "", __PACKAGE__; unshift @INC, maps_directory "perl";