ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.29 by root, Wed Aug 9 10:57:28 2006 UTC vs.
Revision 1.34 by root, Thu Aug 24 13:13:49 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use Event;
11$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
12 13
13use strict; 14use strict;
14 15
70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 71@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
71@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 72@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
72 73
73# we bless all objects into derived classes to force a method lookup 74# we bless all objects into derived classes to force a method lookup
74# within the Safe compartment. 75# within the Safe compartment.
75for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 76for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
76 no strict 'refs'; 77 no strict 'refs';
77 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 78 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78} 79}
79 80
80$Event::DIED = sub { 81$Event::DIED = sub {
180 181
181sub unload_extension { 182sub unload_extension {
182 my ($pkg) = @_; 183 my ($pkg) = @_;
183 184
184 warn "removing extension $pkg\n"; 185 warn "removing extension $pkg\n";
185
186 if (my $cb = $pkg->can ("on_unload")) {
187 $cb->($pkg);
188 }
189 186
190 # remove hooks 187 # remove hooks
191 for my $idx (0 .. $#EVENT) { 188 for my $idx (0 .. $#EVENT) {
192 delete $hook[$idx]{$pkg}; 189 delete $hook[$idx]{$pkg};
193 } 190 }
206 } 203 }
207 204
208 # remove extcmds 205 # remove extcmds
209 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 206 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
210 delete $extcmd{$name}; 207 delete $extcmd{$name};
208 }
209
210 if (my $cb = $pkg->can ("on_unload")) {
211 eval {
212 $cb->($pkg);
213 1
214 } or warn "$pkg unloaded, but with errors: $@";
211 } 215 }
212 216
213 Symbol::delete_package $pkg; 217 Symbol::delete_package $pkg;
214} 218}
215 219
381}; 385};
382 386
383############################################################################# 387#############################################################################
384# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
385 389
390sub all_objects(@) {
391 @_, map all_objects ($_->inv), @_
392}
393
386*on_player_load = sub { 394*on_player_load = sub {
387 my ($ob, $path) = @_; 395 my ($ob, $path) = @_;
388 396
389 for my $o ($ob, $ob->inv) { 397 for my $o (all_objects $ob) {
390 if (my $value = $o->get_ob_key_value ("_perl_data")) { 398 if (my $value = $o->get_ob_key_value ("_perl_data")) {
391 $o->set_ob_key_value ("_perl_data"); 399 $o->set_ob_key_value ("_perl_data");
392 400
393 %$o = %{ Storable::thaw pack "H*", $value }; 401 %$o = %{ Storable::thaw pack "H*", $value };
394 } 402 }
397 405
398*on_player_save = sub { 406*on_player_save = sub {
399 my ($ob, $path) = @_; 407 my ($ob, $path) = @_;
400 408
401 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 409 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
402 for grep %$_, $ob, $ob->inv; 410 for grep %$_, all_objects $ob;
403}; 411};
404 412
405############################################################################# 413#############################################################################
406# core extensions - in perl 414# core extensions - in perl
407 415
507 no strict 'refs'; 515 no strict 'refs';
508 *{"ext::$fun"} = $safe_hole->wrap ($cb); 516 *{"ext::$fun"} = $safe_hole->wrap ($cb);
509} 517}
510 518
511############################################################################# 519#############################################################################
520# the server's main()
521
522sub run {
523 my $tick = MAX_TIME * 1e-6;
524 my $next = Event::time;
525 my $timer = Event->timer (at => $next, cb => sub {
526 cf::server_tick; # one server iteration
527
528 $next += $tick;
529 my $NOW = Event::time;
530
531 # if we are delayd by > 0.25 second, skip ticks
532 $next = $NOW if $NOW >= $next + .25;
533
534 $_[0]->w->at ($next);
535 $_[0]->w->start;
536 });
537
538 Event::loop;
539}
540
541#############################################################################
512# initialisation 542# initialisation
513 543
514register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
515 545
516unshift @INC, $LIBDIR; 546unshift @INC, $LIBDIR;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines