--- deliantra/server/lib/cf.pm 2006/06/11 15:02:16 1.15 +++ 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; @@ -102,19 +112,27 @@ my ($name, $time, $cb) = @_; my $caller = caller; - - warn "registering command '$name/$time' to '$caller'"; + #warn "registering command '$name/$time' to '$caller'"; push @{ $command{$name} }, [$time, $cb, $caller]; $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; } +sub register_extcmd { + my ($name, $cb) = @_; + + my $caller = caller; + #warn "registering extcmd '$name' to '$caller'"; + + $extcmd{$name} = [$cb, $caller]; +} + sub register { my ($base, $pkg) = @_; for my $idx (0 .. $#EVENT) { if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { - warn "registering $EVENT[$idx] hook to '$pkg'\n"; + #warn "registering $EVENT[$idx] hook to '$pkg'\n"; $hook[$idx]{$base} = $ref; } } @@ -147,7 +165,7 @@ $ext_pkg{$base} = $pkg; # no strict 'refs'; -# @{"$pkg\::ISA"} = cf::ext::; +# @{"$pkg\::ISA"} = ext::; register $base, $pkg; } @@ -157,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}; @@ -176,19 +198,10 @@ } # remove extcmds - for my $name (keys %command) { - my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; - - if (@cb) { - $command{$name} = \@cb; - $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; - } else { - delete $command{$name}; - delete $COMMAND{"$name\000"}; - } + for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { + delete $extcmd{$name}; } - Symbol::delete_package $pkg; } @@ -212,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; }; @@ -237,11 +252,10 @@ sub on_extcmd { my ($pl, $buf) = @_; - my ($id, $pkg, $name, $data) = split / /, $buf, 4; + my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; - if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { - $method->($pl, $id, $buf); - } + $extcmd{$type}[0]->($pl, $buf) + if $extcmd{$type}; } ############################################################################# @@ -338,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