… | |
… | |
64 | prop_gen MAP_PROP => "cf::map"; |
64 | prop_gen MAP_PROP => "cf::map"; |
65 | prop_gen ARCH_PROP => "cf::arch"; |
65 | prop_gen ARCH_PROP => "cf::arch"; |
66 | |
66 | |
67 | # guessed hierarchies |
67 | # guessed hierarchies |
68 | |
68 | |
69 | @cf::object::player::ISA = 'cf::object'; |
69 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
70 | @cf::object::map::ISA = 'cf::object'; |
70 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
71 | |
|
|
72 | # we bless all objects into derived classes to force a method lookup |
|
|
73 | # 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 | no strict 'refs'; |
|
|
76 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
|
|
77 | } |
71 | |
78 | |
72 | $Event::DIED = sub { |
79 | $Event::DIED = sub { |
73 | warn "error in event callback: @_"; |
80 | warn "error in event callback: @_"; |
74 | }; |
81 | }; |
75 | |
82 | |
… | |
… | |
319 | # load/save perl data associated with player->ob objects |
326 | # load/save perl data associated with player->ob objects |
320 | |
327 | |
321 | *on_player_load = sub { |
328 | *on_player_load = sub { |
322 | my ($ob, $path) = @_; |
329 | my ($ob, $path) = @_; |
323 | |
330 | |
324 | if (open my $fh, "<:raw", "$path.cfperl") { |
|
|
325 | |
|
|
326 | #d##TODO#remove |
|
|
327 | |
|
|
328 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
329 | |
|
|
330 | $data->{version} <= 1 |
|
|
331 | or return; # too new |
|
|
332 | |
|
|
333 | %$ob = %{$data->{ob}}; |
|
|
334 | return; |
|
|
335 | } |
|
|
336 | |
|
|
337 | for my $o ($ob, $ob->inv) { |
331 | for my $o ($ob, $ob->inv) { |
338 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
332 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
339 | $o->set_ob_key_value ("_perl_data"); |
333 | $o->set_ob_key_value ("_perl_data"); |
340 | |
334 | |
341 | %$o = %{ Storable::thaw pack "H*", $value }; |
335 | %$o = %{ Storable::thaw pack "H*", $value }; |
… | |
… | |
346 | *on_player_save = sub { |
340 | *on_player_save = sub { |
347 | my ($ob, $path) = @_; |
341 | my ($ob, $path) = @_; |
348 | |
342 | |
349 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
343 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
350 | for grep %$_, $ob, $ob->inv; |
344 | for grep %$_, $ob, $ob->inv; |
351 | |
|
|
352 | unlink "$path.cfperl";#d##TODO#remove |
|
|
353 | }; |
345 | }; |
354 | |
346 | |
355 | ############################################################################# |
347 | ############################################################################# |
356 | # core extensions - in perl |
348 | # core extensions - in perl |
357 | |
349 | |
… | |
… | |
389 | |
381 | |
390 | $SIG{FPE} = 'IGNORE'; |
382 | $SIG{FPE} = 'IGNORE'; |
391 | |
383 | |
392 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
384 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
393 | |
385 | |
394 | # here we would export the classes and methods available to script code |
386 | # here we export the classes and methods available to script code |
395 | #@ext::cf::object::player::ISA = @cf::object::player::ISA; |
387 | |
396 | #@ext::cf::object::map::ISA = @cf::object::map::ISA; |
388 | for ( |
|
|
389 | ["cf::object" => qw(contr)], |
|
|
390 | ["cf::object::player" => qw(player)], |
|
|
391 | ["cf::player" => qw(peaceful)], |
|
|
392 | ) { |
|
|
393 | no strict 'refs'; |
|
|
394 | my ($pkg, @funs) = @$_; |
|
|
395 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
396 | for @funs; |
|
|
397 | } |
397 | |
398 | |
398 | sub safe_eval($;@) { |
399 | sub safe_eval($;@) { |
399 | my ($code, %vars) = @_; |
400 | my ($code, %vars) = @_; |
400 | |
401 | |
401 | my $qcode = $code; |
402 | my $qcode = $code; |
… | |
… | |
403 | $qcode =~ s/\n/\\n/g; |
404 | $qcode =~ s/\n/\\n/g; |
404 | |
405 | |
405 | local $_; |
406 | local $_; |
406 | local @ext::cf::_safe_eval_args = values %vars; |
407 | local @ext::cf::_safe_eval_args = values %vars; |
407 | |
408 | |
408 | $safe->reval ( |
409 | $code = |
409 | "do {\n" |
410 | "do {\n" |
410 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
411 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
411 | . "#line 0 \"{$qcode}\"\n" |
412 | . "#line 0 \"{$qcode}\"\n" |
412 | . $code |
413 | . $code |
413 | . "\n}" |
414 | . "\n}" |
414 | ) |
415 | ; |
|
|
416 | |
|
|
417 | sub_generation_inc; |
|
|
418 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
419 | sub_generation_inc; |
|
|
420 | |
|
|
421 | wantarray ? @res : $res[0] |
415 | } |
422 | } |
416 | |
423 | |
417 | sub register_script_function { |
424 | sub register_script_function { |
418 | my ($fun, $cb) = @_; |
425 | my ($fun, $cb) = @_; |
419 | |
426 | |