--- deliantra/server/lib/cf.pm 2006/08/27 17:59:26 1.49 +++ deliantra/server/lib/cf.pm 2006/08/31 06:23:19 1.60 @@ -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 +=head3 EVENTS AND OBJECT ATTACHMENTS + +=over 4 + =item $object->attach ($attachment, key => $value...) -Attach a pre-registered attachment to an object. +=item $object->detach ($attachment) + +Attach/detach a pre-registered attachment to an object. =item $player->attach ($attachment, key => $value...) -Attach a pre-registered attachment to a player. +=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 $map->attach ($attachment, key => $value...) # not yet persistent +=item $bool = $object->attached ($name) -Attach a pre-registered attachment to a map. +=item $bool = $player->attached ($name) + +=item $bool = $map->attached ($name) + +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 @@ -249,6 +280,8 @@ sub _attach_attachment { my ($obj, $name, %arg) = @_; + return if exists $obj->{_attachment}{$name}; + my $res; if (my $attach = $attachment{$name}) { @@ -264,29 +297,37 @@ 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 { +*cf::object::attach = +*cf::player::attach = +*cf::map::attach = sub { my ($obj, $name, %arg) = @_; _attach_attachment $obj, $name, %arg; -} +}; -sub cf::player::attach { - my ($obj, $name, %arg) = @_; +# all those should be optimised +*cf::object::detach = +*cf::player::detach = +*cf::map::detach = sub { + my ($obj, $name) = @_; - _attach_attachment KLASS_PLAYER, $obj, $name, %arg; -} + delete $obj->{_attachment}{$name}; + reattach ($obj); +}; -sub cf::map::attach { - my ($obj, $name, %arg) = @_; +*cf::object::attached = +*cf::player::attached = +*cf::map::attached = sub { + my ($obj, $name) = @_; - _attach_attachment KLASS_MAP, $obj, $name, %arg; -} + exists $obj->{_attachment}{$name} +}; sub attach_global { _attach @CB_GLOBAL, KLASS_GLOBAL, @_ @@ -317,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? @@ -348,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 @@ -370,9 +463,9 @@ @$registry = (); - delete $obj->{_attachment} unless @{ $obj->{_attachment} || [] }; + delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; - for my $name (@{ $obj->{_attachment} || [] }) { + for my $name (keys %{ $obj->{_attachment} || {} }) { if (my $attach = $attachment{$name}) { for (@$attach) { my ($klass, @attach) = @$_; @@ -385,18 +478,33 @@ } 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"; } } @@ -418,7 +526,7 @@ %$dst = %$src; - $dst->{_attachment} = [@{ $src->{_attachment} }] + %{$dst->{_attachment}} = %{$src->{_attachment}} if exists $src->{_attachment}; }, ; @@ -669,28 +777,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::; ############################################################################# @@ -700,6 +789,7 @@ @_, map all_objects ($_->inv), @_ } +# TODO: compatibility cruft, remove when no longer needed attach_to_players on_load => sub { my ($pl, $path) = @_;