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.35 by root, Thu Aug 24 13:35:48 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
16our @EVENT; 17our @EVENT;
17our %PROP_TYPE; 18our %PROP_TYPE;
18our %PROP_IDX; 19our %PROP_IDX;
19our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
20 21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
25
21BEGIN { 26BEGIN {
22 @EVENT = map lc, @EVENT; 27 @EVENT = map lc, @EVENT;
23 28
24 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
25 my $msg = join "", @_; 30 my $msg = join "", @_;
70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
71@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
72 77
73# we bless all objects into derived classes to force a method lookup 78# we bless all objects into derived classes to force a method lookup
74# within the Safe compartment. 79# within the Safe compartment.
75for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 80for 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'; 81 no strict 'refs';
77 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78} 83}
79 84
80$Event::DIED = sub { 85$Event::DIED = sub {
180 185
181sub unload_extension { 186sub unload_extension {
182 my ($pkg) = @_; 187 my ($pkg) = @_;
183 188
184 warn "removing extension $pkg\n"; 189 warn "removing extension $pkg\n";
185
186 if (my $cb = $pkg->can ("on_unload")) {
187 $cb->($pkg);
188 }
189 190
190 # remove hooks 191 # remove hooks
191 for my $idx (0 .. $#EVENT) { 192 for my $idx (0 .. $#EVENT) {
192 delete $hook[$idx]{$pkg}; 193 delete $hook[$idx]{$pkg};
193 } 194 }
206 } 207 }
207 208
208 # remove extcmds 209 # remove extcmds
209 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
210 delete $extcmd{$name}; 211 delete $extcmd{$name};
212 }
213
214 if (my $cb = $pkg->can ("on_unload")) {
215 eval {
216 $cb->($pkg);
217 1
218 } or warn "$pkg unloaded, but with errors: $@";
211 } 219 }
212 220
213 Symbol::delete_package $pkg; 221 Symbol::delete_package $pkg;
214} 222}
215 223
381}; 389};
382 390
383############################################################################# 391#############################################################################
384# load/save perl data associated with player->ob objects 392# load/save perl data associated with player->ob objects
385 393
394sub all_objects(@) {
395 @_, map all_objects ($_->inv), @_
396}
397
386*on_player_load = sub { 398*on_player_load = sub {
387 my ($ob, $path) = @_; 399 my ($ob, $path) = @_;
388 400
389 for my $o ($ob, $ob->inv) { 401 for my $o (all_objects $ob) {
390 if (my $value = $o->get_ob_key_value ("_perl_data")) { 402 if (my $value = $o->get_ob_key_value ("_perl_data")) {
391 $o->set_ob_key_value ("_perl_data"); 403 $o->set_ob_key_value ("_perl_data");
392 404
393 %$o = %{ Storable::thaw pack "H*", $value }; 405 %$o = %{ Storable::thaw pack "H*", $value };
394 } 406 }
397 409
398*on_player_save = sub { 410*on_player_save = sub {
399 my ($ob, $path) = @_; 411 my ($ob, $path) = @_;
400 412
401 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 413 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
402 for grep %$_, $ob, $ob->inv; 414 for grep %$_, all_objects $ob;
403}; 415};
404 416
405############################################################################# 417#############################################################################
406# core extensions - in perl 418# core extensions - in perl
407 419
507 no strict 'refs'; 519 no strict 'refs';
508 *{"ext::$fun"} = $safe_hole->wrap ($cb); 520 *{"ext::$fun"} = $safe_hole->wrap ($cb);
509} 521}
510 522
511############################################################################# 523#############################################################################
524# the server's main()
525
526sub run {
527 Event::loop;
528}
529
530#############################################################################
512# initialisation 531# initialisation
513 532
514register "<global>", __PACKAGE__; 533register "<global>", __PACKAGE__;
515 534
516unshift @INC, $LIBDIR; 535unshift @INC, $LIBDIR;
517 536
518load_extensions; 537load_extensions;
519 538
539$TICK_WATCHER = Event->timer (
540 prio => 1,
541 at => $NEXT_TICK || 1,
542 cb => sub {
543 cf::server_tick; # one server iteration
544
545 my $NOW = Event::time;
546 $NEXT_TICK += $TICK;
547
548 # if we are delayed by > 0.25 second, skip ticks
549 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25;
550
551 $TICK_WATCHER->at ($NEXT_TICK);
552 $TICK_WATCHER->start;
553 },
554);
555
5201 5561
521 557

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines