… | |
… | |
90 | my %ext_pkg; |
90 | my %ext_pkg; |
91 | my @exts; |
91 | my @exts; |
92 | my @hook; |
92 | my @hook; |
93 | my %command; |
93 | my %command; |
94 | my %extcmd; |
94 | my %extcmd; |
|
|
95 | |
|
|
96 | ############################################################################# |
|
|
97 | # object support |
|
|
98 | |
|
|
99 | sub reattach { |
|
|
100 | warn "reattach<@_>\n"; |
|
|
101 | } |
|
|
102 | |
|
|
103 | sub instantiate { |
|
|
104 | warn "instantiate<@_>\n"; |
|
|
105 | } |
|
|
106 | |
|
|
107 | sub clone { |
|
|
108 | warn "clone<@_>\n"; |
|
|
109 | } |
95 | |
110 | |
96 | ############################################################################# |
111 | ############################################################################# |
97 | # "new" plug-in system |
112 | # "new" plug-in system |
98 | |
113 | |
99 | =item cf::object::attach ... # NYI |
114 | =item cf::object::attach ... # NYI |
… | |
… | |
352 | # remove extcmds |
367 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
368 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
369 | delete $extcmd{$name}; |
355 | } |
370 | } |
356 | |
371 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
372 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
373 | eval { |
359 | $cb->($pkg); |
374 | $cb->($pkg); |
360 | 1 |
375 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
376 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
377 | } |
… | |
… | |
466 | |
481 | |
467 | ############################################################################# |
482 | ############################################################################# |
468 | # extcmd framework, basically convert ext <msg> |
483 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
484 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
485 | |
471 | sub on_extcmd { |
486 | attach_to_players |
|
|
487 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
488 | my ($pl, $buf) = @_; |
473 | |
489 | |
474 | my $msg = eval { from_json $buf }; |
490 | my $msg = eval { from_json $buf }; |
475 | |
491 | |
476 | if (ref $msg) { |
492 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
493 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
494 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
495 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
496 | } |
480 | } |
497 | } |
|
|
498 | } else { |
|
|
499 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
481 | } |
500 | } |
482 | } else { |
501 | |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
502 | cf::override; |
484 | } |
503 | }, |
485 | |
504 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
505 | |
489 | ############################################################################# |
506 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
507 | # load/save/clean perl data associated with a map |
491 | |
508 | |
492 | *cf::mapsupport::on_clean = sub { |
509 | *cf::mapsupport::on_clean = sub { |
… | |
… | |
624 | } |
641 | } |
625 | |
642 | |
626 | ############################################################################# |
643 | ############################################################################# |
627 | # map scripting support |
644 | # map scripting support |
628 | |
645 | |
629 | our $safe = new Safe "ext"; |
646 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
647 | our $safe_hole = new Safe::Hole; |
631 | |
648 | |
632 | $SIG{FPE} = 'IGNORE'; |
649 | $SIG{FPE} = 'IGNORE'; |
633 | |
650 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
651 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
671 | $qcode =~ s/\n/\\n/g; |
655 | |
672 | |
656 | local $_; |
673 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
674 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
675 | |
659 | $code = |
676 | my $eval = |
660 | "do {\n" |
677 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
678 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
679 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
680 | . $code |
664 | . "\n}" |
681 | . "\n}" |
665 | ; |
682 | ; |
666 | |
683 | |
667 | sub_generation_inc; |
684 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
685 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
686 | sub_generation_inc; |
|
|
687 | |
|
|
688 | if ($@) { |
|
|
689 | warn "$@"; |
|
|
690 | warn "while executing safe code '$code'\n"; |
|
|
691 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
692 | } |
670 | |
693 | |
671 | wantarray ? @res : $res[0] |
694 | wantarray ? @res : $res[0] |
672 | } |
695 | } |
673 | |
696 | |
674 | sub register_script_function { |
697 | sub register_script_function { |