… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use Time::HiRes; |
10 | use Event; |
11 | use 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 | |
13 | use strict; |
14 | use strict; |
14 | |
15 | |
15 | our %COMMAND; |
16 | our %COMMAND; |
16 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
17 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
20 | our %PROP_IDX; |
19 | our $LIBDIR = maps_directory "perl"; |
21 | our $LIBDIR = maps_directory "perl"; |
20 | |
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
|
|
26 | |
21 | BEGIN { |
27 | BEGIN { |
22 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
23 | |
29 | |
24 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
25 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
26 | $msg .= "\n" |
32 | $msg .= "\n" |
27 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
76 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
77 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
72 | |
78 | |
73 | # we bless all objects into derived classes to force a method lookup |
79 | # we bless all objects into derived classes to force a method lookup |
74 | # within the Safe compartment. |
80 | # within the Safe compartment. |
75 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { |
81 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
76 | no strict 'refs'; |
82 | no strict 'refs'; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
83 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | } |
84 | } |
79 | |
85 | |
80 | $Event::DIED = sub { |
86 | $Event::DIED = sub { |
… | |
… | |
136 | } |
142 | } |
137 | |
143 | |
138 | sub register { |
144 | sub register { |
139 | my ($base, $pkg) = @_; |
145 | my ($base, $pkg) = @_; |
140 | |
146 | |
141 | for my $idx (0 .. $#EVENT) { |
147 | for my $idx (0 .. $#PLUGIN_EVENT) { |
142 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
148 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
143 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
149 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
144 | $hook[$idx]{$base} = $ref; |
150 | $hook[$idx]{$base} = $ref; |
145 | } |
151 | } |
146 | } |
152 | } |
147 | } |
153 | } |
148 | |
154 | |
… | |
… | |
181 | sub unload_extension { |
187 | sub unload_extension { |
182 | my ($pkg) = @_; |
188 | my ($pkg) = @_; |
183 | |
189 | |
184 | warn "removing extension $pkg\n"; |
190 | warn "removing extension $pkg\n"; |
185 | |
191 | |
186 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
187 | $cb->($pkg); |
|
|
188 | } |
|
|
189 | |
|
|
190 | # remove hooks |
192 | # remove hooks |
191 | for my $idx (0 .. $#EVENT) { |
193 | for my $idx (0 .. $#PLUGIN_EVENT) { |
192 | delete $hook[$idx]{$pkg}; |
194 | delete $hook[$idx]{$pkg}; |
193 | } |
195 | } |
194 | |
196 | |
195 | # remove commands |
197 | # remove commands |
196 | for my $name (keys %command) { |
198 | for my $name (keys %command) { |
… | |
… | |
208 | # remove extcmds |
210 | # remove extcmds |
209 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
211 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | delete $extcmd{$name}; |
212 | delete $extcmd{$name}; |
211 | } |
213 | } |
212 | |
214 | |
|
|
215 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
216 | eval { |
|
|
217 | $cb->($pkg); |
|
|
218 | 1 |
|
|
219 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
220 | } |
|
|
221 | |
213 | Symbol::delete_package $pkg; |
222 | Symbol::delete_package $pkg; |
214 | } |
223 | } |
215 | |
224 | |
216 | sub load_extensions { |
225 | sub load_extensions { |
217 | my $LIBDIR = maps_directory "perl"; |
226 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
223 | 1 |
232 | 1 |
224 | } or warn "$ext not loaded: $@"; |
233 | } or warn "$ext not loaded: $@"; |
225 | } |
234 | } |
226 | } |
235 | } |
227 | |
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 | |
228 | register_command "perl-reload", 0, sub { |
298 | register_command "perl-reload", 0, sub { |
229 | my ($who, $arg) = @_; |
299 | my ($who, $arg) = @_; |
230 | |
300 | |
231 | if ($who->flag (FLAG_WIZ)) { |
301 | if ($who->flag (FLAG_WIZ)) { |
232 | $who->message ("reloading..."); |
302 | _perl_reload { |
233 | |
303 | warn $_[0]; |
234 | warn "reloading...\n"; |
304 | $who->message ($_[0]); |
235 | eval { |
|
|
236 | # 1. cancel all watchers |
|
|
237 | $_->cancel for Event::all_watchers; |
|
|
238 | |
|
|
239 | # 2. unload all extensions |
|
|
240 | for (@exts) { |
|
|
241 | $who->message ("unloading <$_>"); |
|
|
242 | unload_extension $_; |
|
|
243 | } |
|
|
244 | |
|
|
245 | # 3. unload all modules loaded from $LIBDIR |
|
|
246 | while (my ($k, $v) = each %INC) { |
|
|
247 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
248 | |
|
|
249 | $who->message ("removing <$k>"); |
|
|
250 | delete $INC{$k}; |
|
|
251 | |
|
|
252 | $k =~ s/\.pm$//; |
|
|
253 | $k =~ s/\//::/g; |
|
|
254 | |
|
|
255 | if (my $cb = $k->can ("unload_module")) { |
|
|
256 | $cb->(); |
|
|
257 | } |
|
|
258 | |
|
|
259 | Symbol::delete_package $k; |
|
|
260 | } |
|
|
261 | |
|
|
262 | # 4. get rid of ext::, as good as possible |
|
|
263 | Symbol::delete_package "ext::$_" |
|
|
264 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
265 | |
|
|
266 | # 5. remove register_script_function callbacks |
|
|
267 | # TODO |
|
|
268 | |
|
|
269 | # 6. unload cf.pm "a bit" |
|
|
270 | delete $INC{"cf.pm"}; |
|
|
271 | |
|
|
272 | # don't, removes xs symbols, too |
|
|
273 | #Symbol::delete_package __PACKAGE__; |
|
|
274 | |
|
|
275 | # 7. reload cf.pm |
|
|
276 | $who->message ("reloading cf.pm"); |
|
|
277 | require cf; |
|
|
278 | }; |
305 | }; |
279 | warn $@ if $@; |
|
|
280 | $who->message ($@) if $@; |
|
|
281 | warn "reloaded\n"; |
|
|
282 | |
|
|
283 | $who->message ("reloaded"); |
|
|
284 | } else { |
|
|
285 | $who->message ("Intruder Alert!"); |
|
|
286 | } |
306 | } |
287 | }; |
307 | }; |
288 | |
308 | |
289 | ############################################################################# |
309 | ############################################################################# |
290 | # utility functions |
310 | # utility functions |
… | |
… | |
381 | }; |
401 | }; |
382 | |
402 | |
383 | ############################################################################# |
403 | ############################################################################# |
384 | # load/save perl data associated with player->ob objects |
404 | # load/save perl data associated with player->ob objects |
385 | |
405 | |
|
|
406 | sub all_objects(@) { |
|
|
407 | @_, map all_objects ($_->inv), @_ |
|
|
408 | } |
|
|
409 | |
386 | *on_player_load = sub { |
410 | *on_player_load = sub { |
387 | my ($ob, $path) = @_; |
411 | my ($ob, $path) = @_; |
388 | |
412 | |
389 | for my $o ($ob, $ob->inv) { |
413 | for my $o (all_objects $ob) { |
390 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
414 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
391 | $o->set_ob_key_value ("_perl_data"); |
415 | $o->set_ob_key_value ("_perl_data"); |
392 | |
416 | |
393 | %$o = %{ Storable::thaw pack "H*", $value }; |
417 | %$o = %{ Storable::thaw pack "H*", $value }; |
394 | } |
418 | } |
… | |
… | |
397 | |
421 | |
398 | *on_player_save = sub { |
422 | *on_player_save = sub { |
399 | my ($ob, $path) = @_; |
423 | my ($ob, $path) = @_; |
400 | |
424 | |
401 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
425 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
402 | for grep %$_, $ob, $ob->inv; |
426 | for grep %$_, all_objects $ob; |
403 | }; |
427 | }; |
404 | |
428 | |
405 | ############################################################################# |
429 | ############################################################################# |
406 | # core extensions - in perl |
430 | # core extensions - in perl |
407 | |
431 | |
… | |
… | |
507 | no strict 'refs'; |
531 | no strict 'refs'; |
508 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
532 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
509 | } |
533 | } |
510 | |
534 | |
511 | ############################################################################# |
535 | ############################################################################# |
|
|
536 | # the server's main() |
|
|
537 | |
|
|
538 | sub run { |
|
|
539 | Event::loop; |
|
|
540 | } |
|
|
541 | |
|
|
542 | ############################################################################# |
512 | # initialisation |
543 | # initialisation |
513 | |
544 | |
514 | register "<global>", __PACKAGE__; |
545 | register "<global>", __PACKAGE__; |
515 | |
546 | |
516 | unshift @INC, $LIBDIR; |
547 | unshift @INC, $LIBDIR; |
517 | |
548 | |
518 | load_extensions; |
549 | load_extensions; |
519 | |
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 | |
520 | 1 |
568 | 1 |
521 | |
569 | |