--- deliantra/server/lib/cf.pm 2006/08/28 14:05:24 1.51 +++ deliantra/server/lib/cf.pm 2006/09/10 00:51:24 1.64 @@ -13,12 +13,10 @@ use strict; -_reload_1; +_init_vars; our %COMMAND = (); our @EVENT; -our %PROP_TYPE; -our %PROP_IDX; our $LIBDIR = maps_directory "perl"; our $TICK = MAX_TIME * 1e-6; @@ -36,40 +34,6 @@ }; } -my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! - -# generate property mutators -sub prop_gen { - my ($prefix, $class) = @_; - - no strict 'refs'; - - for my $prop (keys %PROP_TYPE) { - $prop =~ /^\Q$prefix\E_(.*$)/ or next; - my $sub = lc $1; - - my $type = $PROP_TYPE{$prop}; - my $idx = $PROP_IDX {$prop}; - - *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { - $_[0]->get_property ($type, $idx) - }; - - *{"$class\::set_$sub"} = sub { - $_[0]->set_property ($type, $idx, $_[1]); - } unless $ignore_set{$prop}; - } -} - -# auto-generate most of the API - -prop_gen OBJECT_PROP => "cf::object"; -# CFAPI_OBJECT_ANIMATION? -prop_gen PLAYER_PROP => "cf::object::player"; - -prop_gen MAP_PROP => "cf::map"; -prop_gen ARCH_PROP => "cf::arch"; - @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; # we bless all objects into (empty) derived classes to force a method lookup @@ -107,17 +71,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 $bool = $object->attached ($name) -=item $map->attach ($attachment, key => $value...) # not yet persistent +=item $bool = $player->attached ($name) -Attach a pre-registered attachment to a map. +=item $bool = $map->attached ($name) + +Checks wether the named attachment is currently attached to the object. =item cf::attach_global ... @@ -176,6 +158,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 +244,8 @@ sub _attach_attachment { my ($obj, $name, %arg) = @_; + return if exists $obj->{_attachment}{$name}; + my $res; if (my $attach = $attachment{$name}) { @@ -270,23 +267,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 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 +322,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? @@ -338,7 +355,7 @@ if ($@) { warn "$@"; - warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; + warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; override; } @@ -348,6 +365,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 @@ -385,29 +442,53 @@ } sub object_freezer_save { - my ($filename, $objs) = @_; + my ($filename, $rdata, $objs) = @_; + + 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.pst~"; - syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; - close $fh; - chmod SAVE_MODE, "$filename.pst~"; - rename "$filename.pst~", "$filename.pst"; + rename "$filename~", $filename; + } else { + warn "FATAL: $filename~: $!\n"; + } } else { + unlink $filename; unlink "$filename.pst"; } - - chmod SAVE_MODE, "$filename~"; - rename "$filename~", $filename; } sub object_thawer_load { my ($filename) = @_; - open my $fh, "<:raw:perlio", "$filename.pst" - or return; + local $/; + + my $av; - eval { local $/; (Storable::thaw <$fh>)->{objs} } + #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 @@ -613,6 +694,12 @@ # 7. reload cf.pm $msg->("reloading cf.pm"); require cf; + + $msg->("load extensions"); + cf::load_extensions; + + $msg->("reattach"); + _global_reattach; }; $msg->($@) if $@; @@ -673,26 +760,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::; ############################################################################# @@ -702,6 +769,7 @@ @_, map all_objects ($_->inv), @_ } +# TODO: compatibility cruft, remove when no longer needed attach_to_players on_load => sub { my ($pl, $path) = @_; @@ -832,6 +900,7 @@ # the server's main() sub main { + load_extensions; Event::loop; } @@ -842,8 +911,6 @@ unshift @INC, $LIBDIR; -load_extensions; - $TICK_WATCHER = Event->timer ( prio => 1, at => $NEXT_TICK || 1, @@ -861,7 +928,5 @@ }, ); -_reload_2; - 1