--- deliantra/server/lib/cf.pm 2006/08/26 08:44:06 1.44 +++ deliantra/server/lib/cf.pm 2006/08/26 23:36:32 1.45 @@ -15,7 +15,6 @@ our %COMMAND = (); our @EVENT; -our @PLUGIN_EVENT; our %PROP_TYPE; our %PROP_IDX; our $LIBDIR = maps_directory "perl"; @@ -25,8 +24,6 @@ our $NEXT_TICK; BEGIN { - @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; - *CORE::GLOBAL::warn = sub { my $msg = join "", @_; $msg .= "\n" @@ -71,14 +68,11 @@ prop_gen MAP_PROP => "cf::map"; prop_gen ARCH_PROP => "cf::arch"; -# guessed hierarchies - @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; -@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; # 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'; @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } @@ -94,18 +88,18 @@ my %extcmd; ############################################################################# -# object support +# utility functions -sub reattach { - warn "reattach<@_>\n"; -} +use JSON::Syck (); # TODO# replace by JSON::PC once working -sub instantiate { - warn "instantiate<@_>\n"; +sub from_json($) { + $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs + JSON::Syck::Load $_[0] } -sub clone { - warn "clone<@_>\n"; +sub to_json($) { + $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs + JSON::Syck::Dump $_[0] } ############################################################################# @@ -115,7 +109,7 @@ =item cf::attach_global ... -=item cf::attach_to_type ... +=item cf::attach_to_type $object_type, ... =item cf::attach_to_objects ... @@ -123,6 +117,8 @@ =item cf::attach_to_maps ... +=item cf:register_attachment $name, ... + prio => $number, # lower is earlier on_xxx => \&cb, package => package::, @@ -131,11 +127,13 @@ # the following variables are defined in .xs and must not be re-created our @CB_GLOBAL = (); # registry for all global events -our @CB_OBJECT = (); +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) = @_; @@ -153,8 +151,8 @@ # 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) = @_; +sub _attach(\@$@) { + my ($registry, $klass, @arg) = @_; my $prio = 0; @@ -165,14 +163,14 @@ my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; - while (@$arg) { - my $type = shift @$arg; + while (@arg) { + my $type = shift @arg; if ($type eq "prio") { - $prio = shift @$arg; + $prio = shift @arg; } elsif ($type eq "package") { - my $pkg = shift @$arg; + my $pkg = shift @arg; while (my ($name, $id) = each %cb_id) { if (my $cb = $pkg->can ($name)) { @@ -181,13 +179,13 @@ } } elsif (exists $cb_id{$type}) { - _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; + _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; } elsif (ref $type) { warn "attaching objects not supported, ignoring.\n"; } else { - shift @$arg; + shift @arg; warn "attach argument '$type' not supported, ignoring.\n"; } } @@ -205,6 +203,7 @@ sub attach_to_type { my $type = shift; + _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ } @@ -220,16 +219,26 @@ _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 +sub override { + $override = 1; + @invoke_results = (); } -sub invoke { +sub do_invoke { my $event = shift; my $callbacks = shift; + @invoke_results = (); + local $override; for (@$callbacks) { @@ -248,6 +257,63 @@ } ############################################################################# +# 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 { @@ -301,12 +367,7 @@ sub register { my ($base, $pkg) = @_; - for my $idx (0 .. $#PLUGIN_EVENT) { - if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { - #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; - $hook[$idx]{$base} = $ref; - } - } + #TODO } sub load_extension { @@ -347,9 +408,10 @@ warn "removing extension $pkg\n"; # remove hooks - for my $idx (0 .. $#PLUGIN_EVENT) { - delete $hook[$idx]{$pkg}; - } + #TODO +# for my $idx (0 .. $#PLUGIN_EVENT) { +# delete $hook[$idx]{$pkg}; +# } # remove commands for my $name (keys %command) { @@ -425,7 +487,7 @@ # 4. get rid of safe::, as good as possible Symbol::delete_package "safe::$_" - for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); + for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); # 5. remove register_script_function callbacks # TODO @@ -465,21 +527,6 @@ }; ############################################################################# -# 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