… | |
… | |
16 | our %COMMAND; |
16 | our %COMMAND; |
17 | our @EVENT; |
17 | our @EVENT; |
18 | our %PROP_TYPE; |
18 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
19 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
|
|
21 | |
|
|
22 | our $TICK = MAX_TIME * 1e-6; |
|
|
23 | our $TICK_WATCHER; |
|
|
24 | our $NEXT_TICK; |
21 | |
25 | |
22 | BEGIN { |
26 | BEGIN { |
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 | |
|
|
236 | sub _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 | |
|
|
290 | sub perl_reload() { |
|
|
291 | _perl_reload { |
|
|
292 | warn $_[0]; |
|
|
293 | print "$_[0]\n"; |
|
|
294 | }; |
|
|
295 | } |
|
|
296 | |
232 | register_command "perl-reload", 0, sub { |
297 | register_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 |
… | |
… | |
385 | }; |
400 | }; |
386 | |
401 | |
387 | ############################################################################# |
402 | ############################################################################# |
388 | # load/save perl data associated with player->ob objects |
403 | # load/save perl data associated with player->ob objects |
389 | |
404 | |
|
|
405 | sub all_objects(@) { |
|
|
406 | @_, map all_objects ($_->inv), @_ |
|
|
407 | } |
|
|
408 | |
390 | *on_player_load = sub { |
409 | *on_player_load = sub { |
391 | my ($ob, $path) = @_; |
410 | my ($ob, $path) = @_; |
392 | |
411 | |
393 | for my $o ($ob, $ob->inv) { |
412 | for my $o (all_objects $ob) { |
394 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
413 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
395 | $o->set_ob_key_value ("_perl_data"); |
414 | $o->set_ob_key_value ("_perl_data"); |
396 | |
415 | |
397 | %$o = %{ Storable::thaw pack "H*", $value }; |
416 | %$o = %{ Storable::thaw pack "H*", $value }; |
398 | } |
417 | } |
… | |
… | |
401 | |
420 | |
402 | *on_player_save = sub { |
421 | *on_player_save = sub { |
403 | my ($ob, $path) = @_; |
422 | my ($ob, $path) = @_; |
404 | |
423 | |
405 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
424 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
406 | for grep %$_, $ob, $ob->inv; |
425 | for grep %$_, all_objects $ob; |
407 | }; |
426 | }; |
408 | |
427 | |
409 | ############################################################################# |
428 | ############################################################################# |
410 | # core extensions - in perl |
429 | # core extensions - in perl |
411 | |
|
|
412 | my $delta_timer = Event->timer ( |
|
|
413 | parked => 1, |
|
|
414 | prio => Event::PRIO_HIGH, |
|
|
415 | cb => sub { Event::unloop (undef) }, |
|
|
416 | ); |
|
|
417 | |
|
|
418 | sub sleep_delta($) { |
|
|
419 | $delta_timer->at (Event::time + $_[0]); |
|
|
420 | $delta_timer->start; |
|
|
421 | Event::loop; |
|
|
422 | } |
|
|
423 | |
430 | |
424 | =item cf::player::exists $login |
431 | =item cf::player::exists $login |
425 | |
432 | |
426 | Returns true when the given account exists. |
433 | Returns true when the given account exists. |
427 | |
434 | |
… | |
… | |
523 | no strict 'refs'; |
530 | no strict 'refs'; |
524 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
531 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
525 | } |
532 | } |
526 | |
533 | |
527 | ############################################################################# |
534 | ############################################################################# |
|
|
535 | # the server's main() |
|
|
536 | |
|
|
537 | sub run { |
|
|
538 | Event::loop; |
|
|
539 | } |
|
|
540 | |
|
|
541 | ############################################################################# |
528 | # initialisation |
542 | # initialisation |
529 | |
543 | |
530 | register "<global>", __PACKAGE__; |
544 | register "<global>", __PACKAGE__; |
531 | |
545 | |
532 | unshift @INC, $LIBDIR; |
546 | unshift @INC, $LIBDIR; |
533 | |
547 | |
534 | load_extensions; |
548 | load_extensions; |
535 | |
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 four ticks, skip them all |
|
|
560 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
561 | |
|
|
562 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
563 | $TICK_WATCHER->start; |
|
|
564 | }, |
|
|
565 | ); |
|
|
566 | |
536 | 1 |
567 | 1 |
537 | |
568 | |