--- deliantra/server/lib/cf.pm 2006/08/24 14:05:22 1.37 +++ deliantra/server/lib/cf.pm 2006/08/26 23:36:32 1.45 @@ -13,7 +13,7 @@ use strict; -our %COMMAND; +our %COMMAND = (); our @EVENT; our %PROP_TYPE; our %PROP_IDX; @@ -24,8 +24,6 @@ our $NEXT_TICK; BEGIN { - @EVENT = map lc, @EVENT; - *CORE::GLOBAL::warn = sub { my $msg = join "", @_; $msg .= "\n" @@ -70,16 +68,13 @@ prop_gen MAP_PROP => "cf::map"; prop_gen ARCH_PROP => "cf::arch"; -# guessed hierarchies - -@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; -@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; +@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; -# we bless all objects into derived classes to force a method lookup +# we bless all objects into (empty) 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 cf::arch)) { +for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { no strict 'refs'; - @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; + @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } $Event::DIED = sub { @@ -92,6 +87,235 @@ my %command; my %extcmd; +############################################################################# +# utility functions + +use JSON::Syck (); # TODO# replace by JSON::PC once working + +sub from_json($) { + $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs + JSON::Syck::Load $_[0] +} + +sub to_json($) { + $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs + JSON::Syck::Dump $_[0] +} + +############################################################################# +# "new" plug-in system + +=item cf::object::attach ... # NYI + +=item cf::attach_global ... + +=item cf::attach_to_type $object_type, ... + +=item cf::attach_to_objects ... + +=item cf::attach_to_players ... + +=item cf::attach_to_maps ... + +=item cf:register_attachment $name, ... + + prio => $number, # lower is earlier + on_xxx => \&cb, + package => package::, + +=cut + +# the following variables are defined in .xs and must not be re-created +our @CB_GLOBAL = (); # registry for all global events +our @CB_OBJECT = (); # all objects (should not be used except in emergency) +our @CB_PLAYER = (); +our @CB_TYPE = (); # registry for type (cf-object class) based events +our @CB_MAP = (); + +my %attachment; + +sub _attach_cb($\%$$$) { + my ($registry, $undo, $event, $prio, $cb) = @_; + + use sort 'stable'; + + $cb = [$prio, $cb]; + + @{$registry->[$event]} = sort + { $a->[0] cmp $b->[0] } + @{$registry->[$event] || []}, $cb; + + push @{$undo->{cb}}, [$event, $cb]; +} + +# attach handles attaching event callbacks +# the only thing the caller has to do is pass the correct +# registry (== where the callback attaches to). +sub _attach(\@$@) { + my ($registry, $klass, @arg) = @_; + + my $prio = 0; + + my %undo = ( + registry => $registry, + cb => [], + ); + + my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; + + while (@arg) { + my $type = shift @arg; + + if ($type eq "prio") { + $prio = shift @arg; + + } elsif ($type eq "package") { + my $pkg = shift @arg; + + while (my ($name, $id) = each %cb_id) { + if (my $cb = $pkg->can ($name)) { + _attach_cb $registry, %undo, $id, $prio, $cb; + } + } + + } elsif (exists $cb_id{$type}) { + _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; + + } elsif (ref $type) { + warn "attaching objects not supported, ignoring.\n"; + + } else { + shift @arg; + warn "attach argument '$type' not supported, ignoring.\n"; + } + } + + \%undo +} + +sub cf::object::attach { + die; +} + +sub attach_global { + _attach @CB_GLOBAL, KLASS_GLOBAL, @_ +} + +sub attach_to_type { + my $type = shift; + + _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ +} + +sub attach_to_objects { + _attach @CB_OBJECT, KLASS_OBJECT, @_ +} + +sub attach_to_players { + _attach @CB_PLAYER, KLASS_PLAYER, @_ +} + +sub attach_to_maps { + _attach @CB_MAP, KLASS_MAP, @_ +} + +sub register_attachment { + my $name = shift; + + $attachment{$name} = [@_]; +} + +our $override; +our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? + +sub override { + $override = 1; + @invoke_results = (); +} + +sub do_invoke { + my $event = shift; + my $callbacks = shift; + + @invoke_results = (); + + local $override; + + for (@$callbacks) { + eval { &{$_->[1]} }; + + if ($@) { + warn "$@"; + warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; + override; + } + + return 1 if $override; + } + + 0 +} + +############################################################################# +# object support + +sub instantiate { + my ($obj, $data) = @_; + my $registry = $obj->registry; + + $data = from_json $data; + + for (@$data) { + my ($pri, $name, @args) = @$_; + + if (my $attach = $attachment{$name}) { + _attach @$registry, KLASS_OBJECT, @$attach; + + if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { + for (@$cb) { + eval { $_->[1]->($obj, @args); }; + if ($@) { + warn "$@"; + warn "... while processing '$name' instantiate with args <@args>\n"; + } + } + } + } else { + warn "object uses attachment $name that is not available, postponing.\n"; + } + + push @{$obj->{_attachment}}, $name; + } +} + +# basically do the same as instantiate, without calling instantiate +sub reattach { + my ($obj) = @_; + my $registry = $obj->registry; + + warn "reattach<@_, $_>\n"; +} + +attach_to_objects + prio => -1000000, + on_clone => sub { + my ($src, $dst) = @_; + + @{$dst->registry} = @{$src->registry}; + warn "registry clone ", join ":", @{$src->registry};#d# + + %$dst = %$src; + + $dst->{_attachment} = [@{ $src->{_attachment} }] + if exists $src->{_attachment}; + + warn "clone<@_>\n";#d# + }, +; + +############################################################################# +# old plug-in events + sub inject_event { my $extension = shift; my $event_code = shift; @@ -143,12 +367,7 @@ 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; - } - } + #TODO } sub load_extension { @@ -158,7 +377,7 @@ my $base = $1; my $pkg = $1; $pkg =~ s/[^[:word:]]/_/g; - $pkg = "cf::ext::$pkg"; + $pkg = "ext::$pkg"; warn "loading '$path' into '$pkg'\n"; @@ -189,9 +408,10 @@ warn "removing extension $pkg\n"; # remove hooks - for my $idx (0 .. $#EVENT) { - delete $hook[$idx]{$pkg}; - } + #TODO +# for my $idx (0 .. $#PLUGIN_EVENT) { +# delete $hook[$idx]{$pkg}; +# } # remove commands for my $name (keys %command) { @@ -211,7 +431,7 @@ delete $extcmd{$name}; } - if (my $cb = $pkg->can ("on_unload")) { + if (my $cb = $pkg->can ("unload")) { eval { $cb->($pkg); 1 @@ -265,9 +485,9 @@ Symbol::delete_package $k; } - # 4. get rid of ext::, as good as possible - Symbol::delete_package "ext::$_" - for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); + # 4. get rid of safe::, as good as possible + Symbol::delete_package "safe::$_" + for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); # 5. remove register_script_function callbacks # TODO @@ -275,7 +495,8 @@ # 6. unload cf.pm "a bit" delete $INC{"cf.pm"}; - # don't, removes xs symbols, too + # don't, removes xs symbols, too, + # and global variables created in xs #Symbol::delete_package __PACKAGE__; # 7. reload cf.pm @@ -306,46 +527,33 @@ }; ############################################################################# -# utility functions - -use JSON::Syck (); # TODO# replace by JSON::PC once working - -sub from_json($) { - $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs - JSON::Syck::Load $_[0] -} - -sub to_json($) { - $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs - JSON::Syck::Dump $_[0] -} - -############################################################################# # extcmd framework, basically convert ext # into pkg::->on_extcmd_arg1 (...) while shortcutting a few -sub on_extcmd { - my ($pl, $buf) = @_; - - my $msg = eval { from_json $buf }; - - if (ref $msg) { - if (my $cb = $extcmd{$msg->{msgtype}}) { - if (my %reply = $cb->[0]->($pl, $msg)) { - $pl->ext_reply ($msg->{msgid}, %reply); +attach_to_players + on_extcmd => sub { + my ($pl, $buf) = @_; + + my $msg = eval { from_json $buf }; + + if (ref $msg) { + if (my $cb = $extcmd{$msg->{msgtype}}) { + if (my %reply = $cb->[0]->($pl, $msg)) { + $pl->ext_reply ($msg->{msgid}, %reply); + } } + } else { + warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; } - } else { - warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; - } - 1 -} + cf::override; + }, +; ############################################################################# # load/save/clean perl data associated with a map -*on_mapclean = sub { +*cf::mapsupport::on_clean = sub { my ($map) = @_; my $path = $map->tmpname; @@ -354,8 +562,8 @@ unlink "$path.cfperl"; }; -*on_mapin = -*on_mapload = sub { +*cf::mapsupport::on_swapin = +*cf::mapsupport::on_load = sub { my ($map) = @_; my $path = $map->tmpname; @@ -372,7 +580,7 @@ $map->_set_obs ($data->{obs}); }; -*on_mapout = sub { +*cf::mapsupport::on_swapout = sub { my ($map) = @_; my $path = $map->tmpname; @@ -399,6 +607,8 @@ } }; +attach_to_maps prio => -10000, package => cf::mapsupport::; + ############################################################################# # load/save perl data associated with player->ob objects @@ -406,24 +616,25 @@ @_, map all_objects ($_->inv), @_ } -*on_player_load = sub { - my ($ob, $path) = @_; - - for my $o (all_objects $ob) { - if (my $value = $o->get_ob_key_value ("_perl_data")) { - $o->set_ob_key_value ("_perl_data"); +attach_to_players + on_load => sub { + my ($pl, $path) = @_; + + for my $o (all_objects $pl->ob) { + if (my $value = $o->get_ob_key_value ("_perl_data")) { + $o->set_ob_key_value ("_perl_data"); - %$o = %{ Storable::thaw pack "H*", $value }; + %$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 %$_, all_objects $ob; -}; + }, + on_save => sub { + my ($pl, $path) = @_; + + $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) + for grep %$_, all_objects $pl->ob; + }, +; ############################################################################# # core extensions - in perl @@ -479,7 +690,7 @@ ############################################################################# # map scripting support -our $safe = new Safe "ext"; +our $safe = new Safe "safe"; our $safe_hole = new Safe::Hole; $SIG{FPE} = 'IGNORE'; @@ -495,7 +706,7 @@ ) { no strict 'refs'; my ($pkg, @funs) = @$_; - *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) + *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) for @funs; } @@ -507,9 +718,9 @@ $qcode =~ s/\n/\\n/g; local $_; - local @ext::cf::_safe_eval_args = values %vars; + local @safe::cf::_safe_eval_args = values %vars; - $code = + my $eval = "do {\n" . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" . "#line 0 \"{$qcode}\"\n" @@ -518,9 +729,15 @@ ; sub_generation_inc; - my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); + my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); sub_generation_inc; + if ($@) { + warn "$@"; + warn "while executing safe code '$code'\n"; + warn "with arguments " . (join " ", %vars) . "\n"; + } + wantarray ? @res : $res[0] } @@ -528,13 +745,13 @@ my ($fun, $cb) = @_; no strict 'refs'; - *{"ext::$fun"} = $safe_hole->wrap ($cb); + *{"safe::$fun"} = $safe_hole->wrap ($cb); } ############################################################################# # the server's main() -sub run { +sub main { Event::loop; }