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.31 by elmex, Mon Aug 14 04:22:04 2006 UTC vs.
Revision 1.36 by root, Thu Aug 24 14:04:29 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
15our %COMMAND; 16our %COMMAND;
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";
21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
20 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 {
226 1 231 1
227 } or warn "$ext not loaded: $@"; 232 } or warn "$ext not loaded: $@";
228 } 233 }
229} 234}
230 235
236sub _perl_reload(&) {
237 my ($msg) = @_;
238
239 $msg->("reloading...");
240
241 eval {
242 # 1. cancel all watchers
243 $_->cancel for Event::all_watchers;
244
245 # 2. unload all extensions
246 for (@exts) {
247 $msg->("unloading <$_>");
248 unload_extension $_;
249 }
250
251 # 3. unload all modules loaded from $LIBDIR
252 while (my ($k, $v) = each %INC) {
253 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
254
255 $msg->("removing <$k>");
256 delete $INC{$k};
257
258 $k =~ s/\.pm$//;
259 $k =~ s/\//::/g;
260
261 if (my $cb = $k->can ("unload_module")) {
262 $cb->();
263 }
264
265 Symbol::delete_package $k;
266 }
267
268 # 4. get rid of ext::, as good as possible
269 Symbol::delete_package "ext::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271
272 # 5. remove register_script_function callbacks
273 # TODO
274
275 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"};
277
278 # don't, removes xs symbols, too
279 #Symbol::delete_package __PACKAGE__;
280
281 # 7. reload cf.pm
282 $msg->("reloading cf.pm");
283 require cf;
284 };
285 $msg->($@) if $@;
286
287 $msg->("reloaded");
288};
289
290sub perl_reload() {
291 _perl_reload {
292 warn $_[0];
293 print "$_[0]\n";
294 };
295}
296
231register_command "perl-reload", 0, sub { 297register_command "perl-reload", 0, sub {
232 my ($who, $arg) = @_; 298 my ($who, $arg) = @_;
233 299
234 if ($who->flag (FLAG_WIZ)) { 300 if ($who->flag (FLAG_WIZ)) {
235 $who->message ("reloading..."); 301 _perl_reload {
236 302 warn $_[0];
237 warn "reloading...\n"; 303 $who->message ($_[0]);
238 eval {
239 # 1. cancel all watchers
240 $_->cancel for Event::all_watchers;
241
242 # 2. unload all extensions
243 for (@exts) {
244 $who->message ("unloading <$_>");
245 unload_extension $_;
246 }
247
248 # 3. unload all modules loaded from $LIBDIR
249 while (my ($k, $v) = each %INC) {
250 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
251
252 $who->message ("removing <$k>");
253 delete $INC{$k};
254
255 $k =~ s/\.pm$//;
256 $k =~ s/\//::/g;
257
258 if (my $cb = $k->can ("unload_module")) {
259 $cb->();
260 }
261
262 Symbol::delete_package $k;
263 }
264
265 # 4. get rid of ext::, as good as possible
266 Symbol::delete_package "ext::$_"
267 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
268
269 # 5. remove register_script_function callbacks
270 # TODO
271
272 # 6. unload cf.pm "a bit"
273 delete $INC{"cf.pm"};
274
275 # don't, removes xs symbols, too
276 #Symbol::delete_package __PACKAGE__;
277
278 # 7. reload cf.pm
279 $who->message ("reloading cf.pm");
280 require cf;
281 }; 304 };
282 warn $@ if $@;
283 $who->message ($@) if $@;
284 warn "reloaded\n";
285
286 $who->message ("reloaded");
287 } else {
288 $who->message ("Intruder Alert!");
289 } 305 }
290}; 306};
291 307
292############################################################################# 308#############################################################################
293# utility functions 309# utility functions
384}; 400};
385 401
386############################################################################# 402#############################################################################
387# load/save perl data associated with player->ob objects 403# load/save perl data associated with player->ob objects
388 404
405sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_
407}
408
389*on_player_load = sub { 409*on_player_load = sub {
390 my ($ob, $path) = @_; 410 my ($ob, $path) = @_;
391 411
392 for my $o ($ob, $ob->inv) { 412 for my $o (all_objects $ob) {
393 if (my $value = $o->get_ob_key_value ("_perl_data")) { 413 if (my $value = $o->get_ob_key_value ("_perl_data")) {
394 $o->set_ob_key_value ("_perl_data"); 414 $o->set_ob_key_value ("_perl_data");
395 415
396 %$o = %{ Storable::thaw pack "H*", $value }; 416 %$o = %{ Storable::thaw pack "H*", $value };
397 } 417 }
400 420
401*on_player_save = sub { 421*on_player_save = sub {
402 my ($ob, $path) = @_; 422 my ($ob, $path) = @_;
403 423
404 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
405 for grep %$_, $ob, $ob->inv; 425 for grep %$_, all_objects $ob;
406}; 426};
407 427
408############################################################################# 428#############################################################################
409# core extensions - in perl 429# core extensions - in perl
410 430
510 no strict 'refs'; 530 no strict 'refs';
511 *{"ext::$fun"} = $safe_hole->wrap ($cb); 531 *{"ext::$fun"} = $safe_hole->wrap ($cb);
512} 532}
513 533
514############################################################################# 534#############################################################################
535# the server's main()
536
537sub run {
538 Event::loop;
539}
540
541#############################################################################
515# initialisation 542# initialisation
516 543
517register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
518 545
519unshift @INC, $LIBDIR; 546unshift @INC, $LIBDIR;
520 547
521load_extensions; 548load_extensions;
522 549
550$TICK_WATCHER = Event->timer (
551 prio => 1,
552 at => $NEXT_TICK || 1,
553 cb => sub {
554 cf::server_tick; # one server iteration
555
556 my $NOW = Event::time;
557 $NEXT_TICK += $TICK;
558
559 # if we are delayed by > 0.25 second, skip ticks
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25;
561
562 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start;
564 },
565);
566
5231 5671
524 568

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines