--- deliantra/server/lib/cf.pm 2006/08/28 16:52:51 1.52 +++ deliantra/server/lib/cf.pm 2006/08/30 16:30:37 1.59 @@ -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 ... @@ -179,6 +197,11 @@ 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 @@ -257,6 +280,8 @@ sub _attach_attachment { my ($obj, $name, %arg) = @_; + return if exists $obj->{_attachment}{$name}; + my $res; if (my $attach = $attachment{$name}) { @@ -278,23 +303,31 @@ $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 $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 $obj, $name, %arg; -} + exists $obj->{_attachment}{$name} +}; sub attach_global { _attach @CB_GLOBAL, KLASS_GLOBAL, @_ @@ -325,6 +358,12 @@ $attachment{$name} = [[KLASS_OBJECT, @_]]; } +sub register_player_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_PLAYER, @_]]; +} + sub register_map_attachment { my $name = shift; @@ -362,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 @@ -399,20 +478,26 @@ } sub object_freezer_save { - my ($filename, $objs) = @_; + my ($filename, $rdata, $objs) = @_; - if (@$objs) { - open my $fh, ">:raw", "$filename.pst~"; - syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; + if (open my $fh, ">:raw", "$filename~") { + chmod SAVE_MODE, $fh; + syswrite $fh, $$rdata; close $fh; - chmod SAVE_MODE, "$filename.pst~"; - rename "$filename.pst~", "$filename.pst"; + + 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"; + } + + rename "$filename~", $filename; } else { - unlink "$filename.pst"; + warn "FATAL: $filename~: $!\n"; } - - chmod SAVE_MODE, "$filename~"; - rename "$filename~", $filename; } sub object_thawer_load { @@ -687,26 +772,6 @@ unlink "$path.pst"; }; -# old style persistent data, TODO: remove #d# -*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}); - $map->invoke (EVENT_MAP_UPGRADE); -}; - attach_to_maps prio => -10000, package => cf::mapsupport::; #############################################################################