--- deliantra/server/lib/cf.pm 2006/02/04 20:38:29 1.3 +++ deliantra/server/lib/cf.pm 2006/07/19 22:19:19 1.23 @@ -2,6 +2,13 @@ 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; @@ -23,6 +30,8 @@ }; } +my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! + # generate property mutators sub prop_gen { my ($prefix, $class) = @_; @@ -42,7 +51,7 @@ *{"$class\::set_$sub"} = sub { $_[0]->set_property ($type, $idx, $_[1]); - }; + } unless $ignore_set{$prop}; } } @@ -60,19 +69,33 @@ @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; my %command; +my %extcmd; sub inject_event { - my ($data) = @_; + my $extension = shift; + my $event_code = shift; - my $cb = $hook[$data->{event_code}] + my $cb = $hook[$event_code]{$extension} or return; - $_->($data) for values %$cb; + &$cb +} + +sub inject_global_event { + my $event = shift; - () + my $cb = $hook[$event] + or return; + + List::Util::max map &$_, values %$cb } sub inject_command { @@ -89,15 +112,37 @@ my ($name, $time, $cb) = @_; my $caller = 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"; + $hook[$idx]{$base} = $ref; + } + } +} + sub load_extension { my ($path) = @_; $path =~ /([^\/\\]+)\.ext$/ or die "$path"; + my $base = $1; my $pkg = $1; $pkg =~ s/[^[:word:]]/_/g; $pkg = "cf::ext::$pkg"; @@ -117,17 +162,12 @@ or die "$path: $@"; push @exts, $pkg; + $ext_pkg{$base} = $pkg; - no strict 'refs'; - -# @{"$pkg\::ISA"} = cf::ext::; +# no strict 'refs'; +# @{"$pkg\::ISA"} = ext::; - for my $idx (0 .. $#EVENT) { - if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { - warn "registering $EVENT[$idx] hook\n"; - $hook[$idx]{$pkg} = $ref; - } - } + register $base, $pkg; } sub unload_extension { @@ -135,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}; @@ -153,6 +197,11 @@ } } + # remove extcmds + for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { + delete $extcmd{$name}; + } + Symbol::delete_package $pkg; } @@ -175,9 +224,19 @@ $who->message ("reloading..."); warn "reloading...\n"; - unload_extension $_ for @exts; - @exts = 0; - load_extensions; + eval { + $_->cancel for Event::all_watchers; + + unload_extension $_ for @exts; + delete $INC{"cf.pm"}; + + # don't, removes xs symbols, too + #Symbol::delete_package __PACKAGE__; + + require cf; + }; + warn $@ if $@; + $who->message ($@) if $@; warn "reloaded\n"; $who->message ("reloaded"); @@ -186,6 +245,186 @@ } }; +############################################################################# +# extcmd framework, basically convert ext arg1 args +# into pkg::->on_extcmd_arg1 (...) while shortcutting a few + +sub on_extcmd { + my ($pl, $buf) = @_; + + my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; + + $extcmd{$type}[0]->($pl, $buf) + if $extcmd{$type}; +} + +############################################################################# +# load/save/clean perl data associated with a map + +*on_mapclean = sub { + my ($map) = @_; + + my $path = $map->tmpname; + defined $path or return; + + unlink "$path.cfperl"; +}; + +*on_mapin = +*on_mapload = sub { + my ($map) = @_; + + my $path = $map->tmpname; + $path = $map->path unless defined $path; + + open my $fh, "<:raw", "$path.cfperl" + or return; # no perl data + + my $data = Storable::thaw do { local $/; <$fh> }; + + $data->{version} <= 1 + or return; # too new + + $map->_set_obs ($data->{obs}); +}; + +*on_mapout = sub { + my ($map) = @_; + + my $path = $map->tmpname; + $path = $map->path unless defined $path; + + my $obs = $map->_get_obs; + + if (defined $obs) { + open my $fh, ">:raw", "$path.cfperl" + or die "$path.cfperl: $!"; + + stat $path; + + print $fh Storable::nfreeze { + size => (stat _)[7], + time => (stat _)[9], + version => 1, + obs => $obs, + }; + + chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* + } else { + unlink "$path.cfperl"; + } +}; + +############################################################################# +# load/save perl data associated with player->ob objects + +*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"); + + %$o = %{ Storable::thaw pack "H*", $value }; + } + } +}; + +*on_player_save = sub { + my ($ob, $path) = @_; + + $_->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; + + $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