… | |
… | |
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 |
… | |
… | |
518 | |
533 | |
519 | ############################################################################# |
534 | ############################################################################# |
520 | # the server's main() |
535 | # the server's main() |
521 | |
536 | |
522 | sub run { |
537 | sub run { |
523 | my $tick = MAX_TIME * 1e-6; |
538 | Event::loop; |
524 | my $next = Event::time; |
539 | } |
525 | my $timer = Event->timer (at => $next, cb => sub { |
540 | |
|
|
541 | ############################################################################# |
|
|
542 | # initialisation |
|
|
543 | |
|
|
544 | register "<global>", __PACKAGE__; |
|
|
545 | |
|
|
546 | unshift @INC, $LIBDIR; |
|
|
547 | |
|
|
548 | load_extensions; |
|
|
549 | |
|
|
550 | $TICK_WATCHER = Event->timer ( |
|
|
551 | prio => 1, |
|
|
552 | at => $NEXT_TICK || 1, |
|
|
553 | cb => sub { |
526 | cf::server_tick; # one server iteration |
554 | cf::server_tick; # one server iteration |
527 | |
555 | |
528 | $next += $tick; |
|
|
529 | my $NOW = Event::time; |
556 | my $NOW = Event::time; |
|
|
557 | $NEXT_TICK += $TICK; |
530 | |
558 | |
531 | # if we are delayd by > 0.25 second, skip ticks |
559 | # if we are delayed by > 0.25 second, skip ticks |
532 | $next = $NOW if $NOW >= $next + .25; |
560 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; |
533 | |
561 | |
534 | $_[0]->w->at ($next); |
562 | $TICK_WATCHER->at ($NEXT_TICK); |
535 | $_[0]->w->start; |
563 | $TICK_WATCHER->start; |
536 | }); |
564 | }, |
537 | |
565 | ); |
538 | Event::loop; |
|
|
539 | } |
|
|
540 | |
|
|
541 | ############################################################################# |
|
|
542 | # initialisation |
|
|
543 | |
|
|
544 | register "<global>", __PACKAGE__; |
|
|
545 | |
|
|
546 | unshift @INC, $LIBDIR; |
|
|
547 | |
|
|
548 | load_extensions; |
|
|
549 | |
566 | |
550 | 1 |
567 | 1 |
551 | |
568 | |