… | |
… | |
14 | |
14 | |
15 | our %COMMAND; |
15 | our %COMMAND; |
16 | our @EVENT; |
16 | our @EVENT; |
17 | our %PROP_TYPE; |
17 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
18 | our %PROP_IDX; |
|
|
19 | our $LIBDIR = maps_directory "perl"; |
19 | |
20 | |
20 | BEGIN { |
21 | BEGIN { |
21 | @EVENT = map lc, @EVENT; |
22 | @EVENT = map lc, @EVENT; |
22 | |
23 | |
23 | *CORE::GLOBAL::warn = sub { |
24 | *CORE::GLOBAL::warn = sub { |
… | |
… | |
69 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
70 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
71 | |
72 | |
72 | # we bless all objects into derived classes to force a method lookup |
73 | # we bless all objects into derived classes to force a method lookup |
73 | # within the Safe compartment. |
74 | # within the Safe compartment. |
74 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { |
75 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
75 | no strict 'refs'; |
76 | no strict 'refs'; |
76 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
77 | } |
78 | } |
78 | |
79 | |
79 | $Event::DIED = sub { |
80 | $Event::DIED = sub { |
… | |
… | |
179 | |
180 | |
180 | sub unload_extension { |
181 | sub unload_extension { |
181 | my ($pkg) = @_; |
182 | my ($pkg) = @_; |
182 | |
183 | |
183 | warn "removing extension $pkg\n"; |
184 | warn "removing extension $pkg\n"; |
184 | |
|
|
185 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
186 | $cb->($pkg); |
|
|
187 | } |
|
|
188 | |
185 | |
189 | # remove hooks |
186 | # remove hooks |
190 | for my $idx (0 .. $#EVENT) { |
187 | for my $idx (0 .. $#EVENT) { |
191 | delete $hook[$idx]{$pkg}; |
188 | delete $hook[$idx]{$pkg}; |
192 | } |
189 | } |
… | |
… | |
207 | # remove extcmds |
204 | # remove extcmds |
208 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
205 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
209 | delete $extcmd{$name}; |
206 | delete $extcmd{$name}; |
210 | } |
207 | } |
211 | |
208 | |
|
|
209 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
210 | eval { |
|
|
211 | $cb->($pkg); |
|
|
212 | 1 |
|
|
213 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
214 | } |
|
|
215 | |
212 | Symbol::delete_package $pkg; |
216 | Symbol::delete_package $pkg; |
213 | } |
217 | } |
214 | |
218 | |
215 | sub load_extensions { |
219 | sub load_extensions { |
216 | my $LIBDIR = maps_directory "perl"; |
220 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
230 | if ($who->flag (FLAG_WIZ)) { |
234 | if ($who->flag (FLAG_WIZ)) { |
231 | $who->message ("reloading..."); |
235 | $who->message ("reloading..."); |
232 | |
236 | |
233 | warn "reloading...\n"; |
237 | warn "reloading...\n"; |
234 | eval { |
238 | eval { |
|
|
239 | # 1. cancel all watchers |
235 | $_->cancel for Event::all_watchers; |
240 | $_->cancel for Event::all_watchers; |
236 | |
241 | |
|
|
242 | # 2. unload all extensions |
|
|
243 | for (@exts) { |
|
|
244 | $who->message ("unloading <$_>"); |
237 | unload_extension $_ for @exts; |
245 | unload_extension $_; |
|
|
246 | } |
|
|
247 | |
|
|
248 | # 3. unload all modules loaded from $LIBDIR |
|
|
249 | while (my ($k, $v) = each %INC) { |
|
|
250 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
251 | |
|
|
252 | $who->message ("removing <$k>"); |
|
|
253 | delete $INC{$k}; |
|
|
254 | |
|
|
255 | $k =~ s/\.pm$//; |
|
|
256 | $k =~ s/\//::/g; |
|
|
257 | |
|
|
258 | if (my $cb = $k->can ("unload_module")) { |
|
|
259 | $cb->(); |
|
|
260 | } |
|
|
261 | |
|
|
262 | Symbol::delete_package $k; |
|
|
263 | } |
|
|
264 | |
|
|
265 | # 4. get rid of ext::, as good as possible |
|
|
266 | Symbol::delete_package "ext::$_" |
|
|
267 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
268 | |
|
|
269 | # 5. remove register_script_function callbacks |
|
|
270 | # TODO |
|
|
271 | |
|
|
272 | # 6. unload cf.pm "a bit" |
238 | delete $INC{"cf.pm"}; |
273 | delete $INC{"cf.pm"}; |
239 | |
274 | |
240 | # don't, removes xs symbols, too |
275 | # don't, removes xs symbols, too |
241 | #Symbol::delete_package __PACKAGE__; |
276 | #Symbol::delete_package __PACKAGE__; |
242 | |
277 | |
|
|
278 | # 7. reload cf.pm |
|
|
279 | $who->message ("reloading cf.pm"); |
243 | require cf; |
280 | require cf; |
244 | }; |
281 | }; |
245 | warn $@ if $@; |
282 | warn $@ if $@; |
246 | $who->message ($@) if $@; |
283 | $who->message ($@) if $@; |
247 | warn "reloaded\n"; |
284 | warn "reloaded\n"; |
… | |
… | |
251 | $who->message ("Intruder Alert!"); |
288 | $who->message ("Intruder Alert!"); |
252 | } |
289 | } |
253 | }; |
290 | }; |
254 | |
291 | |
255 | ############################################################################# |
292 | ############################################################################# |
|
|
293 | # utility functions |
|
|
294 | |
|
|
295 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
296 | |
|
|
297 | sub from_json($) { |
|
|
298 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
299 | JSON::Syck::Load $_[0] |
|
|
300 | } |
|
|
301 | |
|
|
302 | sub to_json($) { |
|
|
303 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
304 | JSON::Syck::Dump $_[0] |
|
|
305 | } |
|
|
306 | |
|
|
307 | ############################################################################# |
256 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
308 | # extcmd framework, basically convert ext <msg> |
257 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
309 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
258 | |
310 | |
259 | sub on_extcmd { |
311 | sub on_extcmd { |
260 | my ($pl, $buf) = @_; |
312 | my ($pl, $buf) = @_; |
261 | |
313 | |
262 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
314 | my $msg = eval { from_json $buf }; |
263 | |
315 | |
264 | $extcmd{$type}[0]->($pl, $buf) |
316 | if (ref $msg) { |
265 | if $extcmd{$type}; |
317 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
318 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
319 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
320 | } |
|
|
321 | } |
|
|
322 | } else { |
|
|
323 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
324 | } |
|
|
325 | |
|
|
326 | 1 |
266 | } |
327 | } |
267 | |
328 | |
268 | ############################################################################# |
329 | ############################################################################# |
269 | # load/save/clean perl data associated with a map |
330 | # load/save/clean perl data associated with a map |
270 | |
331 | |
… | |
… | |
356 | sub cf::player::exists($) { |
417 | sub cf::player::exists($) { |
357 | cf::player::find $_[0] |
418 | cf::player::find $_[0] |
358 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
419 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
359 | } |
420 | } |
360 | |
421 | |
|
|
422 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
423 | |
|
|
424 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
425 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
426 | dialogue with the given NPC character. |
|
|
427 | |
|
|
428 | =cut |
|
|
429 | |
361 | # rough implementation of a future "reply" method that works |
430 | # rough implementation of a future "reply" method that works |
362 | # with dialog boxes. |
431 | # with dialog boxes. |
363 | sub cf::object::player::reply($$$;$) { |
432 | sub cf::object::player::reply($$$;$) { |
364 | my ($self, $npc, $msg, $flags) = @_; |
433 | my ($self, $npc, $msg, $flags) = @_; |
365 | |
434 | |
… | |
… | |
371 | $msg = $npc->name . " says: $msg" if $npc; |
440 | $msg = $npc->name . " says: $msg" if $npc; |
372 | $self->message ($msg, $flags); |
441 | $self->message ($msg, $flags); |
373 | } |
442 | } |
374 | } |
443 | } |
375 | |
444 | |
|
|
445 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
446 | |
|
|
447 | Sends an ext reply to the player. |
|
|
448 | |
|
|
449 | =cut |
|
|
450 | |
|
|
451 | sub cf::player::ext_reply($$$%) { |
|
|
452 | my ($self, $id, %msg) = @_; |
|
|
453 | |
|
|
454 | $msg{msgid} = $id; |
|
|
455 | |
|
|
456 | $self->send ("ext " . to_json \%msg); |
|
|
457 | } |
|
|
458 | |
376 | ############################################################################# |
459 | ############################################################################# |
377 | # map scripting support |
460 | # map scripting support |
378 | |
461 | |
379 | our $safe = new Safe "ext"; |
462 | our $safe = new Safe "ext"; |
380 | our $safe_hole = new Safe::Hole; |
463 | our $safe_hole = new Safe::Hole; |
… | |
… | |
384 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
467 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
385 | |
468 | |
386 | # here we export the classes and methods available to script code |
469 | # here we export the classes and methods available to script code |
387 | |
470 | |
388 | for ( |
471 | for ( |
389 | ["cf::object" => qw(contr)], |
472 | ["cf::object" => qw(contr pay_amount pay_player)], |
390 | ["cf::object::player" => qw(player)], |
473 | ["cf::object::player" => qw(player)], |
391 | ["cf::player" => qw(peaceful)], |
474 | ["cf::player" => qw(peaceful)], |
392 | ) { |
475 | ) { |
393 | no strict 'refs'; |
476 | no strict 'refs'; |
394 | my ($pkg, @funs) = @$_; |
477 | my ($pkg, @funs) = @$_; |
… | |
… | |
431 | ############################################################################# |
514 | ############################################################################# |
432 | # initialisation |
515 | # initialisation |
433 | |
516 | |
434 | register "<global>", __PACKAGE__; |
517 | register "<global>", __PACKAGE__; |
435 | |
518 | |
436 | unshift @INC, maps_directory "perl"; |
519 | unshift @INC, $LIBDIR; |
437 | |
520 | |
438 | load_extensions; |
521 | load_extensions; |
439 | |
522 | |
440 | 1 |
523 | 1 |
441 | |
524 | |