--- deliantra/server/lib/cf.pm 2006/08/09 10:57:28 1.29 +++ deliantra/server/lib/cf.pm 2006/08/24 13:13:49 1.34 @@ -7,6 +7,7 @@ use Safe; use Safe::Hole; +use Time::HiRes; use Event; $Event::Eval = 1; # no idea why this is required, but it is @@ -72,7 +73,7 @@ # we bless all objects into 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)) { +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; } @@ -183,10 +184,6 @@ warn "removing extension $pkg\n"; - if (my $cb = $pkg->can ("on_unload")) { - $cb->($pkg); - } - # remove hooks for my $idx (0 .. $#EVENT) { delete $hook[$idx]{$pkg}; @@ -210,6 +207,13 @@ delete $extcmd{$name}; } + if (my $cb = $pkg->can ("on_unload")) { + eval { + $cb->($pkg); + 1 + } or warn "$pkg unloaded, but with errors: $@"; + } + Symbol::delete_package $pkg; } @@ -383,10 +387,14 @@ ############################################################################# # load/save perl data associated with player->ob objects +sub all_objects(@) { + @_, map all_objects ($_->inv), @_ +} + *on_player_load = sub { my ($ob, $path) = @_; - for my $o ($ob, $ob->inv) { + for my $o (all_objects $ob) { if (my $value = $o->get_ob_key_value ("_perl_data")) { $o->set_ob_key_value ("_perl_data"); @@ -399,7 +407,7 @@ my ($ob, $path) = @_; $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) - for grep %$_, $ob, $ob->inv; + for grep %$_, all_objects $ob; }; ############################################################################# @@ -509,6 +517,28 @@ } ############################################################################# +# the server's main() + +sub run { + my $tick = MAX_TIME * 1e-6; + my $next = Event::time; + my $timer = Event->timer (at => $next, cb => sub { + cf::server_tick; # one server iteration + + $next += $tick; + my $NOW = Event::time; + + # if we are delayd by > 0.25 second, skip ticks + $next = $NOW if $NOW >= $next + .25; + + $_[0]->w->at ($next); + $_[0]->w->start; + }); + + Event::loop; +} + +############################################################################# # initialisation register "", __PACKAGE__;