… | |
… | |
352 | # remove extcmds |
352 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
354 | delete $extcmd{$name}; |
355 | } |
355 | } |
356 | |
356 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
357 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
358 | eval { |
359 | $cb->($pkg); |
359 | $cb->($pkg); |
360 | 1 |
360 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
362 | } |
… | |
… | |
466 | |
466 | |
467 | ############################################################################# |
467 | ############################################################################# |
468 | # extcmd framework, basically convert ext <msg> |
468 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
470 | |
471 | sub on_extcmd { |
471 | attach_global |
|
|
472 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
473 | my ($pl, $buf) = @_; |
473 | |
474 | |
474 | my $msg = eval { from_json $buf }; |
475 | my $msg = eval { from_json $buf }; |
475 | |
476 | |
476 | if (ref $msg) { |
477 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
480 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
481 | } |
480 | } |
482 | } |
|
|
483 | } else { |
|
|
484 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
481 | } |
485 | } |
482 | } else { |
486 | |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
487 | cf::override; |
484 | } |
488 | }, |
485 | |
489 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
490 | |
489 | ############################################################################# |
491 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
492 | # load/save/clean perl data associated with a map |
491 | |
493 | |
492 | *cf::mapsupport::on_clean = sub { |
494 | *cf::mapsupport::on_clean = sub { |
… | |
… | |
624 | } |
626 | } |
625 | |
627 | |
626 | ############################################################################# |
628 | ############################################################################# |
627 | # map scripting support |
629 | # map scripting support |
628 | |
630 | |
629 | our $safe = new Safe "ext"; |
631 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
632 | our $safe_hole = new Safe::Hole; |
631 | |
633 | |
632 | $SIG{FPE} = 'IGNORE'; |
634 | $SIG{FPE} = 'IGNORE'; |
633 | |
635 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
636 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
656 | $qcode =~ s/\n/\\n/g; |
655 | |
657 | |
656 | local $_; |
658 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
659 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
660 | |
659 | $code = |
661 | my $eval = |
660 | "do {\n" |
662 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
663 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
664 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
665 | . $code |
664 | . "\n}" |
666 | . "\n}" |
665 | ; |
667 | ; |
666 | |
668 | |
667 | sub_generation_inc; |
669 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
670 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
671 | sub_generation_inc; |
|
|
672 | |
|
|
673 | if ($@) { |
|
|
674 | warn "$@"; |
|
|
675 | warn "while executing safe code '$code'\n"; |
|
|
676 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
677 | } |
670 | |
678 | |
671 | wantarray ? @res : $res[0] |
679 | wantarray ? @res : $res[0] |
672 | } |
680 | } |
673 | |
681 | |
674 | sub register_script_function { |
682 | sub register_script_function { |