--- deliantra/server/lib/cf.pm 2006/06/12 13:25:27 1.16 +++ deliantra/server/lib/cf.pm 2006/07/19 22:19:19 1.23 @@ -3,6 +3,12 @@ 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 use strict; @@ -63,6 +69,10 @@ @cf::object::player::ISA = 'cf::object'; @cf::object::map::ISA = 'cf::object'; +$Event::DIED = sub { + warn "error in event callback: @_"; +}; + my %ext_pkg; my @exts; my @hook; @@ -155,7 +165,7 @@ $ext_pkg{$base} = $pkg; # no strict 'refs'; -# @{"$pkg\::ISA"} = cf::ext::; +# @{"$pkg\::ISA"} = ext::; register $base, $pkg; } @@ -165,6 +175,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}; @@ -211,11 +225,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; }; @@ -336,8 +352,79 @@ 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; + + $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 would export the classes and methods available to script code +#@ext::cf::object::player::ISA = @cf::object::player::ISA; +#@ext::cf::object::map::ISA = @cf::object::map::ISA; + +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; + + $safe->reval ( + "do {\n" + . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" + . "#line 0 \"{$qcode}\"\n" + . $code + . "\n}" + ) +} + +sub register_script_function { + my ($fun, $cb) = @_; + + no strict 'refs'; + *{"ext::$fun"} = $safe_hole->wrap ($cb); +} + +############################################################################# +# initialisation + register "", __PACKAGE__; +unshift @INC, maps_directory "perl"; + load_extensions; 1