--- deliantra/server/lib/cf.pm 2006/08/15 18:07:25 1.32 +++ deliantra/server/lib/cf.pm 2006/08/25 15:25:12 1.41 @@ -13,14 +13,19 @@ use strict; -our %COMMAND; +our %COMMAND = (); our @EVENT; +our @PLUGIN_EVENT; our %PROP_TYPE; our %PROP_IDX; our $LIBDIR = maps_directory "perl"; +our $TICK = MAX_TIME * 1e-6; +our $TICK_WATCHER; +our $NEXT_TICK; + BEGIN { - @EVENT = map lc, @EVENT; + @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; *CORE::GLOBAL::warn = sub { my $msg = join "", @_; @@ -68,14 +73,14 @@ # guessed hierarchies -@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; -@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; +@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; +@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; -# we bless all objects into derived classes to force a method lookup +# 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::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { no strict 'refs'; - @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; + @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } $Event::DIED = sub { @@ -88,6 +93,148 @@ my %command; my %extcmd; +############################################################################# +# "new" plug-in system + +=item cf::object::attach ... # NYI + +=item cf::attach_global ... + +=item cf::attach_to_type ... + +=item cf::attach_to_objects ... + +=item cf::attach_to_players ... + +=item cf::attach_to_maps ... + + prio => $number, # lower is earlier + on_xxx => \&cb, + package => package::, + +=cut + +# the following variables are defined in .xs and must not be re-created +our @CB_GLOBAL = (); # registry for all global events +our @CB_OBJECT = (); +our @CB_PLAYER = (); +our @CB_TYPE = (); # registry for type (cf-object class) based events +our @CB_MAP = (); + +sub _attach_cb($\%$$$) { + my ($registry, $undo, $event, $prio, $cb) = @_; + + use sort 'stable'; + + $cb = [$prio, $cb]; + + @{$registry->[$event]} = sort + { $a->[0] cmp $b->[0] } + @{$registry->[$event] || []}, $cb; + + push @{$undo->{cb}}, [$event, $cb]; +} + +# attach handles attaching event callbacks +# the only thing the caller has to do is pass the correct +# registry (== where the callback attaches to). +sub _attach(\@$\@) { + my ($registry, $klass, $arg) = @_; + + my $prio = 0; + + my %undo = ( + registry => $registry, + cb => [], + ); + + my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; + + while (@$arg) { + my $type = shift @$arg; + + if ($type eq "prio") { + $prio = shift @$arg; + + } elsif ($type eq "package") { + my $pkg = shift @$arg; + + while (my ($name, $id) = each %cb_id) { + if (my $cb = $pkg->can ($name)) { + _attach_cb $registry, %undo, $id, $prio, $cb; + } + } + + } elsif (exists $cb_id{$type}) { + _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; + + } elsif (ref $type) { + warn "attaching objects not supported, ignoring.\n"; + + } else { + shift @$arg; + warn "attach argument '$type' not supported, ignoring.\n"; + } + } + + \%undo +} + +sub cf::object::attach { + die; +} + +sub attach_global { + _attach @CB_GLOBAL, KLASS_GLOBAL, @_ +} + +sub attach_to_type { + my $type = shift; + _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ +} + +sub attach_to_objects { + _attach @CB_OBJECT, KLASS_OBJECT, @_ +} + +sub attach_to_players { + _attach @CB_PLAYER, KLASS_PLAYER, @_ +} + +sub attach_to_maps { + _attach @CB_MAP, KLASS_MAP, @_ +} + +our $override; + +sub override() { + $override = 1 +} + +sub invoke { + my $event = shift; + my $callbacks = shift; + + local $override; + + for (@$callbacks) { + eval { &{$_->[1]} }; + + if ($@) { + warn "$@"; + warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; + override; + } + + return 1 if $override; + } + + 0 +} + +############################################################################# +# old plug-in events + sub inject_event { my $extension = shift; my $event_code = shift; @@ -139,9 +286,9 @@ sub register { my ($base, $pkg) = @_; - for my $idx (0 .. $#EVENT) { - if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { - #warn "registering $EVENT[$idx] hook to '$pkg'\n"; + for my $idx (0 .. $#PLUGIN_EVENT) { + if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { + #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; $hook[$idx]{$base} = $ref; } } @@ -154,7 +301,7 @@ my $base = $1; my $pkg = $1; $pkg =~ s/[^[:word:]]/_/g; - $pkg = "cf::ext::$pkg"; + $pkg = "ext::$pkg"; warn "loading '$path' into '$pkg'\n"; @@ -185,7 +332,7 @@ warn "removing extension $pkg\n"; # remove hooks - for my $idx (0 .. $#EVENT) { + for my $idx (0 .. $#PLUGIN_EVENT) { delete $hook[$idx]{$pkg}; } @@ -229,64 +376,76 @@ } } -register_command "perl-reload", 0, sub { - my ($who, $arg) = @_; +sub _perl_reload(&) { + my ($msg) = @_; - if ($who->flag (FLAG_WIZ)) { - $who->message ("reloading..."); + $msg->("reloading..."); - warn "reloading...\n"; - eval { - # 1. cancel all watchers - $_->cancel for Event::all_watchers; + eval { + # 1. cancel all watchers + $_->cancel for Event::all_watchers; + + # 2. unload all extensions + for (@exts) { + $msg->("unloading <$_>"); + unload_extension $_; + } + + # 3. unload all modules loaded from $LIBDIR + while (my ($k, $v) = each %INC) { + next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; - # 2. unload all extensions - for (@exts) { - $who->message ("unloading <$_>"); - unload_extension $_; + $msg->("removing <$k>"); + delete $INC{$k}; + + $k =~ s/\.pm$//; + $k =~ s/\//::/g; + + if (my $cb = $k->can ("unload_module")) { + $cb->(); } - # 3. unload all modules loaded from $LIBDIR - while (my ($k, $v) = each %INC) { - next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; + Symbol::delete_package $k; + } - $who->message ("removing <$k>"); - delete $INC{$k}; + # 4. get rid of safe::, as good as possible + Symbol::delete_package "safe::$_" + for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); + + # 5. remove register_script_function callbacks + # TODO + + # 6. unload cf.pm "a bit" + delete $INC{"cf.pm"}; + + # don't, removes xs symbols, too, + # and global variables created in xs + #Symbol::delete_package __PACKAGE__; + + # 7. reload cf.pm + $msg->("reloading cf.pm"); + require cf; + }; + $msg->($@) if $@; - $k =~ s/\.pm$//; - $k =~ s/\//::/g; + $msg->("reloaded"); +}; - if (my $cb = $k->can ("unload_module")) { - $cb->(); - } +sub perl_reload() { + _perl_reload { + warn $_[0]; + print "$_[0]\n"; + }; +} - Symbol::delete_package $k; - } +register_command "perl-reload", 0, sub { + my ($who, $arg) = @_; - # 4. get rid of ext::, as good as possible - Symbol::delete_package "ext::$_" - for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); - - # 5. remove register_script_function callbacks - # TODO - - # 6. unload cf.pm "a bit" - delete $INC{"cf.pm"}; - - # don't, removes xs symbols, too - #Symbol::delete_package __PACKAGE__; - - # 7. reload cf.pm - $who->message ("reloading cf.pm"); - require cf; + if ($who->flag (FLAG_WIZ)) { + _perl_reload { + warn $_[0]; + $who->message ($_[0]); }; - warn $@ if $@; - $who->message ($@) if $@; - warn "reloaded\n"; - - $who->message ("reloaded"); - } else { - $who->message ("Intruder Alert!"); } }; @@ -330,7 +489,7 @@ ############################################################################# # load/save/clean perl data associated with a map -*on_mapclean = sub { +*cf::mapsupport::on_clean = sub { my ($map) = @_; my $path = $map->tmpname; @@ -339,8 +498,8 @@ unlink "$path.cfperl"; }; -*on_mapin = -*on_mapload = sub { +*cf::mapsupport::on_swapin = +*cf::mapsupport::on_load = sub { my ($map) = @_; my $path = $map->tmpname; @@ -357,7 +516,7 @@ $map->_set_obs ($data->{obs}); }; -*on_mapout = sub { +*cf::mapsupport::on_swapout = sub { my ($map) = @_; my $path = $map->tmpname; @@ -384,43 +543,38 @@ } }; +attach_to_maps prio => -10000, package => cf::mapsupport::; + ############################################################################# # load/save perl data associated with player->ob objects -*on_player_load = sub { - my ($ob, $path) = @_; - - for my $o ($ob, $ob->inv) { - if (my $value = $o->get_ob_key_value ("_perl_data")) { - $o->set_ob_key_value ("_perl_data"); +sub all_objects(@) { + @_, map all_objects ($_->inv), @_ +} - %$o = %{ Storable::thaw pack "H*", $value }; - } - } -}; +attach_to_players + on_load => sub { + my ($pl, $path) = @_; -*on_player_save = sub { - my ($ob, $path) = @_; + for my $o (all_objects $pl->ob) { + if (my $value = $o->get_ob_key_value ("_perl_data")) { + $o->set_ob_key_value ("_perl_data"); - $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) - for grep %$_, $ob, $ob->inv; -}; + %$o = %{ Storable::thaw pack "H*", $value }; + } + } + }, + on_save => sub { + my ($pl, $path) = @_; + + $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) + for grep %$_, all_objects $pl->ob; + }, +; ############################################################################# # core extensions - in perl -my $delta_timer = Event->timer ( - parked => 1, - prio => Event::PRIO_HIGH, - cb => sub { Event::unloop (undef) }, -); - -sub sleep_delta($) { - $delta_timer->at (Event::time + $_[0]); - $delta_timer->start; - Event::loop; -} - =item cf::player::exists $login Returns true when the given account exists. @@ -488,7 +642,7 @@ ) { no strict 'refs'; my ($pkg, @funs) = @$_; - *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) + *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) for @funs; } @@ -500,7 +654,7 @@ $qcode =~ s/\n/\\n/g; local $_; - local @ext::cf::_safe_eval_args = values %vars; + local @safe::cf::_safe_eval_args = values %vars; $code = "do {\n" @@ -521,7 +675,14 @@ my ($fun, $cb) = @_; no strict 'refs'; - *{"ext::$fun"} = $safe_hole->wrap ($cb); + *{"safe::$fun"} = $safe_hole->wrap ($cb); +} + +############################################################################# +# the server's main() + +sub main { + Event::loop; } ############################################################################# @@ -533,5 +694,22 @@ load_extensions; +$TICK_WATCHER = Event->timer ( + prio => 1, + at => $NEXT_TICK || 1, + cb => sub { + cf::server_tick; # one server iteration + + my $NOW = Event::time; + $NEXT_TICK += $TICK; + + # if we are delayed by four ticks, skip them all + $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; + + $TICK_WATCHER->at ($NEXT_TICK); + $TICK_WATCHER->start; + }, +); + 1