… | |
… | |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | our %COMMAND; |
16 | our %COMMAND; |
17 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
18 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
20 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
21 | our $LIBDIR = maps_directory "perl"; |
21 | |
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
|
|
26 | |
22 | BEGIN { |
27 | BEGIN { |
23 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
24 | |
29 | |
25 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
26 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
27 | $msg .= "\n" |
32 | $msg .= "\n" |
28 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
137 | } |
142 | } |
138 | |
143 | |
139 | sub register { |
144 | sub register { |
140 | my ($base, $pkg) = @_; |
145 | my ($base, $pkg) = @_; |
141 | |
146 | |
142 | for my $idx (0 .. $#EVENT) { |
147 | for my $idx (0 .. $#PLUGIN_EVENT) { |
143 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
148 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
144 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
149 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
145 | $hook[$idx]{$base} = $ref; |
150 | $hook[$idx]{$base} = $ref; |
146 | } |
151 | } |
147 | } |
152 | } |
148 | } |
153 | } |
149 | |
154 | |
… | |
… | |
183 | my ($pkg) = @_; |
188 | my ($pkg) = @_; |
184 | |
189 | |
185 | warn "removing extension $pkg\n"; |
190 | warn "removing extension $pkg\n"; |
186 | |
191 | |
187 | # remove hooks |
192 | # remove hooks |
188 | for my $idx (0 .. $#EVENT) { |
193 | for my $idx (0 .. $#PLUGIN_EVENT) { |
189 | delete $hook[$idx]{$pkg}; |
194 | delete $hook[$idx]{$pkg}; |
190 | } |
195 | } |
191 | |
196 | |
192 | # remove commands |
197 | # remove commands |
193 | for my $name (keys %command) { |
198 | for my $name (keys %command) { |
… | |
… | |
227 | 1 |
232 | 1 |
228 | } or warn "$ext not loaded: $@"; |
233 | } or warn "$ext not loaded: $@"; |
229 | } |
234 | } |
230 | } |
235 | } |
231 | |
236 | |
|
|
237 | sub _perl_reload(&) { |
|
|
238 | my ($msg) = @_; |
|
|
239 | |
|
|
240 | $msg->("reloading..."); |
|
|
241 | |
|
|
242 | eval { |
|
|
243 | # 1. cancel all watchers |
|
|
244 | $_->cancel for Event::all_watchers; |
|
|
245 | |
|
|
246 | # 2. unload all extensions |
|
|
247 | for (@exts) { |
|
|
248 | $msg->("unloading <$_>"); |
|
|
249 | unload_extension $_; |
|
|
250 | } |
|
|
251 | |
|
|
252 | # 3. unload all modules loaded from $LIBDIR |
|
|
253 | while (my ($k, $v) = each %INC) { |
|
|
254 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
255 | |
|
|
256 | $msg->("removing <$k>"); |
|
|
257 | delete $INC{$k}; |
|
|
258 | |
|
|
259 | $k =~ s/\.pm$//; |
|
|
260 | $k =~ s/\//::/g; |
|
|
261 | |
|
|
262 | if (my $cb = $k->can ("unload_module")) { |
|
|
263 | $cb->(); |
|
|
264 | } |
|
|
265 | |
|
|
266 | Symbol::delete_package $k; |
|
|
267 | } |
|
|
268 | |
|
|
269 | # 4. get rid of ext::, as good as possible |
|
|
270 | Symbol::delete_package "ext::$_" |
|
|
271 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
272 | |
|
|
273 | # 5. remove register_script_function callbacks |
|
|
274 | # TODO |
|
|
275 | |
|
|
276 | # 6. unload cf.pm "a bit" |
|
|
277 | delete $INC{"cf.pm"}; |
|
|
278 | |
|
|
279 | # don't, removes xs symbols, too |
|
|
280 | #Symbol::delete_package __PACKAGE__; |
|
|
281 | |
|
|
282 | # 7. reload cf.pm |
|
|
283 | $msg->("reloading cf.pm"); |
|
|
284 | require cf; |
|
|
285 | }; |
|
|
286 | $msg->($@) if $@; |
|
|
287 | |
|
|
288 | $msg->("reloaded"); |
|
|
289 | }; |
|
|
290 | |
|
|
291 | sub perl_reload() { |
|
|
292 | _perl_reload { |
|
|
293 | warn $_[0]; |
|
|
294 | print "$_[0]\n"; |
|
|
295 | }; |
|
|
296 | } |
|
|
297 | |
232 | register_command "perl-reload", 0, sub { |
298 | register_command "perl-reload", 0, sub { |
233 | my ($who, $arg) = @_; |
299 | my ($who, $arg) = @_; |
234 | |
300 | |
235 | if ($who->flag (FLAG_WIZ)) { |
301 | if ($who->flag (FLAG_WIZ)) { |
236 | $who->message ("reloading..."); |
302 | _perl_reload { |
237 | |
303 | warn $_[0]; |
238 | warn "reloading...\n"; |
304 | $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 | }; |
305 | }; |
283 | warn $@ if $@; |
|
|
284 | $who->message ($@) if $@; |
|
|
285 | warn "reloaded\n"; |
|
|
286 | |
|
|
287 | $who->message ("reloaded"); |
|
|
288 | } else { |
|
|
289 | $who->message ("Intruder Alert!"); |
|
|
290 | } |
306 | } |
291 | }; |
307 | }; |
292 | |
308 | |
293 | ############################################################################# |
309 | ############################################################################# |
294 | # utility functions |
310 | # utility functions |
… | |
… | |
410 | for grep %$_, all_objects $ob; |
426 | for grep %$_, all_objects $ob; |
411 | }; |
427 | }; |
412 | |
428 | |
413 | ############################################################################# |
429 | ############################################################################# |
414 | # core extensions - in perl |
430 | # core extensions - in perl |
415 | |
|
|
416 | my $delta_timer = Event->timer ( |
|
|
417 | parked => 1, |
|
|
418 | prio => Event::PRIO_HIGH, |
|
|
419 | cb => sub { Event::unloop (undef) }, |
|
|
420 | ); |
|
|
421 | |
|
|
422 | sub sleep_delta($) { |
|
|
423 | $delta_timer->at (Event::time + $_[0]); |
|
|
424 | $delta_timer->start; |
|
|
425 | Event::loop; |
|
|
426 | } |
|
|
427 | |
431 | |
428 | =item cf::player::exists $login |
432 | =item cf::player::exists $login |
429 | |
433 | |
430 | Returns true when the given account exists. |
434 | Returns true when the given account exists. |
431 | |
435 | |
… | |
… | |
527 | no strict 'refs'; |
531 | no strict 'refs'; |
528 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
532 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
529 | } |
533 | } |
530 | |
534 | |
531 | ############################################################################# |
535 | ############################################################################# |
|
|
536 | # the server's main() |
|
|
537 | |
|
|
538 | sub run { |
|
|
539 | Event::loop; |
|
|
540 | } |
|
|
541 | |
|
|
542 | ############################################################################# |
532 | # initialisation |
543 | # initialisation |
533 | |
544 | |
534 | register "<global>", __PACKAGE__; |
545 | register "<global>", __PACKAGE__; |
535 | |
546 | |
536 | unshift @INC, $LIBDIR; |
547 | unshift @INC, $LIBDIR; |
537 | |
548 | |
538 | load_extensions; |
549 | load_extensions; |
539 | |
550 | |
|
|
551 | $TICK_WATCHER = Event->timer ( |
|
|
552 | prio => 1, |
|
|
553 | at => $NEXT_TICK || 1, |
|
|
554 | cb => sub { |
|
|
555 | cf::server_tick; # one server iteration |
|
|
556 | |
|
|
557 | my $NOW = Event::time; |
|
|
558 | $NEXT_TICK += $TICK; |
|
|
559 | |
|
|
560 | # if we are delayed by four ticks, skip them all |
|
|
561 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
562 | |
|
|
563 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
564 | $TICK_WATCHER->start; |
|
|
565 | }, |
|
|
566 | ); |
|
|
567 | |
540 | 1 |
568 | 1 |
541 | |
569 | |