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.33 by root, Mon Aug 21 01:51:23 2006 UTC vs.
Revision 1.36 by root, Thu Aug 24 14:04:29 2006 UTC

16our %COMMAND; 16our %COMMAND;
17our @EVENT; 17our @EVENT;
18our %PROP_TYPE; 18our %PROP_TYPE;
19our %PROP_IDX; 19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
21 25
22BEGIN { 26BEGIN {
23 @EVENT = map lc, @EVENT; 27 @EVENT = map lc, @EVENT;
24 28
25 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
227 1 231 1
228 } or warn "$ext not loaded: $@"; 232 } or warn "$ext not loaded: $@";
229 } 233 }
230} 234}
231 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
232register_command "perl-reload", 0, sub { 297register_command "perl-reload", 0, sub {
233 my ($who, $arg) = @_; 298 my ($who, $arg) = @_;
234 299
235 if ($who->flag (FLAG_WIZ)) { 300 if ($who->flag (FLAG_WIZ)) {
236 $who->message ("reloading..."); 301 _perl_reload {
237 302 warn $_[0];
238 warn "reloading...\n"; 303 $who->message ($_[0]);
239 eval {
240 # 1. cancel all watchers
241 $_->cancel for Event::all_watchers;
242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
246 unload_extension $_;
247 }
248
249 # 3. unload all modules loaded from $LIBDIR
250 while (my ($k, $v) = each %INC) {
251 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
252
253 $who->message ("removing <$k>");
254 delete $INC{$k};
255
256 $k =~ s/\.pm$//;
257 $k =~ s/\//::/g;
258
259 if (my $cb = $k->can ("unload_module")) {
260 $cb->();
261 }
262
263 Symbol::delete_package $k;
264 }
265
266 # 4. get rid of ext::, as good as possible
267 Symbol::delete_package "ext::$_"
268 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
269
270 # 5. remove register_script_function callbacks
271 # TODO
272
273 # 6. unload cf.pm "a bit"
274 delete $INC{"cf.pm"};
275
276 # don't, removes xs symbols, too
277 #Symbol::delete_package __PACKAGE__;
278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
281 require cf;
282 }; 304 };
283 warn $@ if $@;
284 $who->message ($@) if $@;
285 warn "reloaded\n";
286
287 $who->message ("reloaded");
288 } else {
289 $who->message ("Intruder Alert!");
290 } 305 }
291}; 306};
292 307
293############################################################################# 308#############################################################################
294# utility functions 309# utility functions
410 for grep %$_, all_objects $ob; 425 for grep %$_, all_objects $ob;
411}; 426};
412 427
413############################################################################# 428#############################################################################
414# core extensions - in perl 429# core extensions - in perl
415
416my $delta_timer = Event->timer (
417 parked => 1,
418 prio => Event::PRIO_HIGH,
419 cb => sub { Event::unloop (undef) },
420);
421
422sub sleep_delta($) {
423 $delta_timer->at (Event::time + $_[0]);
424 $delta_timer->start;
425 Event::loop;
426}
427 430
428=item cf::player::exists $login 431=item cf::player::exists $login
429 432
430Returns true when the given account exists. 433Returns true when the given account exists.
431 434
527 no strict 'refs'; 530 no strict 'refs';
528 *{"ext::$fun"} = $safe_hole->wrap ($cb); 531 *{"ext::$fun"} = $safe_hole->wrap ($cb);
529} 532}
530 533
531############################################################################# 534#############################################################################
535# the server's main()
536
537sub run {
538 Event::loop;
539}
540
541#############################################################################
532# initialisation 542# initialisation
533 543
534register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
535 545
536unshift @INC, $LIBDIR; 546unshift @INC, $LIBDIR;
537 547
538load_extensions; 548load_extensions;
539 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
5401 5671
541 568

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines