--- deliantra/server/lib/cf.pm 2006/08/27 13:02:04 1.47 +++ deliantra/server/lib/cf.pm 2006/09/03 22:45:56 1.61 @@ -74,7 +74,7 @@ # 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::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 cf::living)) { no strict 'refs'; @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } @@ -107,17 +107,35 @@ ############################################################################# # "new" plug-in system -=item $object->attach ($attachment, ...) +=head3 EVENTS AND OBJECT ATTACHMENTS -Attach a pre-registered attachment to an object. +=over 4 + +=item $object->attach ($attachment, key => $value...) + +=item $object->detach ($attachment) + +Attach/detach a pre-registered attachment to an object. + +=item $player->attach ($attachment, key => $value...) + +=item $player->detach ($attachment) + +Attach/detach a pre-registered attachment to a player. + +=item $map->attach ($attachment, key => $value...) + +=item $map->detach ($attachment) + +Attach/detach a pre-registered attachment to a map. -=item $player->attach ($attachment, ...) +=item $bool = $object->attached ($name) -Attach a pre-registered attachment to a player. +=item $bool = $player->attached ($name) -=item $map->attach ($attachment, ...) # not yet persistent +=item $bool = $map->attached ($name) -Attach a pre-registered attachment to a map. +Checks wether the named attachment is currently attached to the object. =item cf::attach_global ... @@ -176,6 +194,19 @@ =item cf:register_attachment $name, ... +Register an attachment by name through which objects can refer to this +attachment. + +=item cf:register_player_attachment $name, ... + +Register an attachment by name through which players can refer to this +attachment. + +=item cf:register_map_attachment $name, ... + +Register an attachment by name through which maps can refer to this +attachment. + =cut # the following variables are defined in .xs and must not be re-created @@ -247,7 +278,9 @@ } sub _attach_attachment { - my ($obj, $name, @args) = @_; + my ($obj, $name, %arg) = @_; + + return if exists $obj->{_attachment}{$name}; my $res; @@ -259,42 +292,42 @@ $res = _attach @$registry, $klass, @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"; - } - } - } + $obj->{$name} = \%arg; } else { warn "object uses attachment '$name' that is not available, postponing.\n"; } - push @{$obj->{_attachment}}, $name; + $obj->{_attachment}{$name} = undef; $res->{attachment} = $name; $res } -sub cf::object::attach { - my ($obj, $name, @args) = @_; +*cf::object::attach = +*cf::player::attach = +*cf::map::attach = sub { + my ($obj, $name, %arg) = @_; - _attach_attachment $obj, $name, @args; -} + _attach_attachment $obj, $name, %arg; +}; -sub cf::player::attach { - my ($obj, $name, @args) = @_; +# all those should be optimised +*cf::object::detach = +*cf::player::detach = +*cf::map::detach = sub { + my ($obj, $name) = @_; - _attach_attachment KLASS_PLAYER, $obj, $name, @args; -} + delete $obj->{_attachment}{$name}; + reattach ($obj); +}; -sub cf::map::attach { - my ($obj, $name, @args) = @_; +*cf::object::attached = +*cf::player::attached = +*cf::map::attached = sub { + my ($obj, $name) = @_; - _attach_attachment KLASS_MAP, $obj, $name, @args; -} + exists $obj->{_attachment}{$name} +}; sub attach_global { _attach @CB_GLOBAL, KLASS_GLOBAL, @_ @@ -325,6 +358,18 @@ $attachment{$name} = [[KLASS_OBJECT, @_]]; } +sub register_player_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_PLAYER, @_]]; +} + +sub register_map_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_MAP, @_]]; +} + our $override; our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? @@ -356,6 +401,46 @@ 0 } +=item $bool = cf::invoke EVENT_GLOBAL_XXX, ... + +=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) + +=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) + +=item $bool = $map->invoke (EVENT_MAP_XXX, ...) + +Generate a global/object/player/map-specific event with the given arguments. + +This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be +removed in future versions), and there is no public API to access override +results (if you must, access C<@cf::invoke_results> directly). + +=back + +=head2 methods valid for all pointers + +=over 4 + +=item $object->valid + +=item $player->valid + +=item $map->valid + +Just because you have a perl object does not mean that the corresponding +C-level object still exists. If you try to access an object that has no +valid C counterpart anymore you get an exception at runtime. This method +can be used to test for existence of the C object part without causing an +exception. + +=back + +=cut + +*cf::object::valid = +*cf::player::valid = +*cf::map::valid = \&cf::_valid; + ############################################################################# # object support @@ -366,7 +451,8 @@ for (@$data) { my ($name, $args) = @$_; - attach $obj, $name, @{$args || [] }; + + $obj->attach ($name, %{$args || {} }); } } @@ -377,7 +463,9 @@ @$registry = (); - for my $name (@{ $obj->{_attachment} }) { + delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; + + for my $name (keys %{ $obj->{_attachment} || {} }) { if (my $attach = $attachment{$name}) { for (@$attach) { my ($klass, @attach) = @$_; @@ -390,28 +478,53 @@ } sub object_freezer_save { - my ($filename, $objs) = @_; + my ($filename, $rdata, $objs) = @_; - $filename .= ".pst"; + if (length $$rdata) { + warn sprintf "saving %s (%d,%d)\n", + $filename, length $$rdata, scalar @$objs; + + if (open my $fh, ">:raw", "$filename~") { + chmod SAVE_MODE, $fh; + syswrite $fh, $$rdata; + close $fh; + + if (@$objs && open my $fh, ">:raw", "$filename.pst~") { + chmod SAVE_MODE, $fh; + syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; + close $fh; + rename "$filename.pst~", "$filename.pst"; + } else { + unlink "$filename.pst"; + } - if (@$objs) { - open my $fh, ">:raw", "$filename~"; - chmod $fh, SAVE_MODE; - syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; - close $fh; - rename "$filename~", $filename; + rename "$filename~", $filename; + } else { + warn "FATAL: $filename~: $!\n"; + } } else { unlink $filename; + unlink "$filename.pst"; } } sub object_thawer_load { my ($filename) = @_; - open my $fh, "<:raw:perlio", "$filename.pst" - or return; + local $/; - eval { local $/; (Storable::thaw <$fh>)->{objs} } + my $av; + + #TODO: use sysread etc. + if (open my $data, "<:raw:perlio", $filename) { + $data = <$data>; + if (open my $pst, "<:raw:perlio", "$filename.pst") { + $av = eval { (Storable::thaw <$pst>)->{objs} }; + } + return ($data, $av); + } + + () } attach_to_objects @@ -423,7 +536,7 @@ %$dst = %$src; - $dst->{_attachment} = [@{ $src->{_attachment} }] + %{$dst->{_attachment}} = %{$src->{_attachment}} if exists $src->{_attachment}; }, ; @@ -617,6 +730,9 @@ # 7. reload cf.pm $msg->("reloading cf.pm"); require cf; + + $msg->("load extensions"); + cf::load_extensions; }; $msg->($@) if $@; @@ -674,28 +790,9 @@ my $path = $map->tmpname; defined $path or return; - unlink "$path.cfperl"; unlink "$path.pst"; }; -*cf::mapsupport::on_swapin = -*cf::mapsupport::on_load = 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}); -}; - attach_to_maps prio => -10000, package => cf::mapsupport::; ############################################################################# @@ -705,6 +802,7 @@ @_, map all_objects ($_->inv), @_ } +# TODO: compatibility cruft, remove when no longer needed attach_to_players on_load => sub { my ($pl, $path) = @_; @@ -835,6 +933,7 @@ # the server's main() sub main { + load_extensions; Event::loop; } @@ -845,8 +944,6 @@ unshift @INC, $LIBDIR; -load_extensions; - $TICK_WATCHER = Event->timer ( prio => 1, at => $NEXT_TICK || 1,