--- deliantra/server/lib/cf.pm 2006/08/27 16:15:12 1.46 +++ deliantra/server/lib/cf.pm 2006/09/08 16:51:44 1.62 @@ -13,10 +13,10 @@ use strict; +_reload_1; + our %COMMAND = (); our @EVENT; -our %PROP_TYPE; -our %PROP_IDX; our $LIBDIR = maps_directory "perl"; our $TICK = MAX_TIME * 1e-6; @@ -34,45 +34,11 @@ }; } -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 # 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; } @@ -105,17 +71,35 @@ ############################################################################# # "new" plug-in system -=item $object->attach ($attachment, ...) +=head3 EVENTS AND OBJECT ATTACHMENTS + +=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...) -Attach a pre-registered attachment to an object. +=item $map->detach ($attachment) -=item $player->attach ($attachment, ...) +Attach/detach a pre-registered attachment to a map. -Attach a pre-registered attachment to a player. +=item $bool = $object->attached ($name) -=item $map->attach ($attachment, ...) # 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 ... @@ -152,9 +136,11 @@ =back -=item cf::attach_to_type $object_type, ... +=item cf::attach_to_type $object_type, $subtype, ... -Attach handlers for a specific object type (e.g. TRANSPORT). +Attach handlers for a specific object type (e.g. TRANSPORT) and +subtype. If C<$subtype> is zero or undef, matches all objects of the given +type. =item cf::attach_to_objects ... @@ -172,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 @@ -243,51 +242,56 @@ } sub _attach_attachment { - my ($klass, $obj, $name, @args) = q_; + my ($obj, $name, %arg) = @_; + + return if exists $obj->{_attachment}{$name}; my $res; if (my $attach = $attachment{$name}) { my $registry = $obj->registry; - $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"; - } - } + for (@$attach) { + my ($klass, @attach) = @$_; + $res = _attach @$registry, $klass, @attach; } + + $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 KLASS_OBJECT, $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, @_ @@ -295,8 +299,9 @@ sub attach_to_type { my $type = shift; + my $subtype = shift; - _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ + _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ } sub attach_to_objects { @@ -314,7 +319,19 @@ sub register_attachment { my $name = shift; - $attachment{$name} = [@_]; + $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; @@ -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 @@ -358,54 +415,80 @@ for (@$data) { my ($name, $args) = @$_; - attach $obj, $name, @{$args || [] }; + + $obj->attach ($name, %{$args || {} }); } } # basically do the same as instantiate, without calling instantiate sub reattach { - warn "reattach<@_>\n";#d# my ($obj) = @_; my $registry = $obj->registry; - for my $name (@{ $obj->{_attachment} }) { + @$registry = (); + + delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; + + for my $name (keys %{ $obj->{_attachment} || {} }) { if (my $attach = $attachment{$name}) { - _attach @$registry, KLASS_OBJECT, @$attach; + for (@$attach) { + my ($klass, @attach) = @$_; + _attach @$registry, $klass, @attach; + } } else { warn "object uses attachment '$name' that is not available, postponing.\n"; } } - - warn "reattach<@_, $_>\n"; } sub object_freezer_save { - my ($filename, $objs) = @_; - warn "freeze $filename\n";#d# - use Data::Dumper; print Dumper $objs; - - $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; + 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"; + } + + rename "$filename~", $filename; + } else { + warn "FATAL: $filename~: $!\n"; + } } else { unlink $filename; + unlink "$filename.pst"; } } sub object_thawer_load { my ($filename) = @_; - warn "thaw $filename\n";#d# + local $/; - open my $fh, "<:raw:perlio", "$filename.pst" - or return; + 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); + } - eval { local $/; (Storable::thaw <$fh>)->{objs} } + () } attach_to_objects @@ -414,14 +497,11 @@ my ($src, $dst) = @_; @{$dst->registry} = @{$src->registry}; - warn "registry clone ", join ":", @{$src->registry};#d# %$dst = %$src; - $dst->{_attachment} = [@{ $src->{_attachment} }] + %{$dst->{_attachment}} = %{$src->{_attachment}} if exists $src->{_attachment}; - - warn "clone<@_>\n";#d# }, ; @@ -614,6 +694,9 @@ # 7. reload cf.pm $msg->("reloading cf.pm"); require cf; + + $msg->("load extensions"); + cf::load_extensions; }; $msg->($@) if $@; @@ -671,28 +754,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::; ############################################################################# @@ -702,6 +766,7 @@ @_, map all_objects ($_->inv), @_ } +# TODO: compatibility cruft, remove when no longer needed attach_to_players on_load => sub { my ($pl, $path) = @_; @@ -832,6 +897,7 @@ # the server's main() sub main { + load_extensions; Event::loop; } @@ -842,8 +908,6 @@ unshift @INC, $LIBDIR; -load_extensions; - $TICK_WATCHER = Event->timer ( prio => 1, at => $NEXT_TICK || 1, @@ -861,5 +925,7 @@ }, ); +_reload_2; + 1