1 | package cf; |
1 | package cf; |
2 | |
2 | |
3 | use Symbol; |
3 | use Symbol; |
4 | use List::Util; |
4 | use List::Util; |
5 | use Storable; |
5 | use Storable; |
|
|
6 | use Opcode; |
|
|
7 | use Safe; |
|
|
8 | use Safe::Hole; |
6 | |
9 | |
7 | use Event; |
10 | use Event; |
8 | $Event::Eval = 1; # no idea why this is required, but it is |
11 | $Event::Eval = 1; # no idea why this is required, but it is |
9 | |
12 | |
10 | use strict; |
13 | use strict; |
… | |
… | |
61 | prop_gen MAP_PROP => "cf::map"; |
64 | prop_gen MAP_PROP => "cf::map"; |
62 | prop_gen ARCH_PROP => "cf::arch"; |
65 | prop_gen ARCH_PROP => "cf::arch"; |
63 | |
66 | |
64 | # guessed hierarchies |
67 | # guessed hierarchies |
65 | |
68 | |
66 | @cf::object::player::ISA = 'cf::object'; |
69 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
67 | @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 | } |
68 | |
78 | |
69 | $Event::DIED = sub { |
79 | $Event::DIED = sub { |
70 | warn "error in event callback: @_"; |
80 | warn "error in event callback: @_"; |
71 | }; |
81 | }; |
72 | |
82 | |
… | |
… | |
160 | |
170 | |
161 | push @exts, $pkg; |
171 | push @exts, $pkg; |
162 | $ext_pkg{$base} = $pkg; |
172 | $ext_pkg{$base} = $pkg; |
163 | |
173 | |
164 | # no strict 'refs'; |
174 | # no strict 'refs'; |
165 | # @{"$pkg\::ISA"} = cf::ext::; |
175 | # @{"$pkg\::ISA"} = ext::; |
166 | |
176 | |
167 | register $base, $pkg; |
177 | register $base, $pkg; |
168 | } |
178 | } |
169 | |
179 | |
170 | sub unload_extension { |
180 | sub unload_extension { |
… | |
… | |
347 | for grep %$_, $ob, $ob->inv; |
357 | for grep %$_, $ob, $ob->inv; |
348 | |
358 | |
349 | unlink "$path.cfperl";#d##TODO#remove |
359 | unlink "$path.cfperl";#d##TODO#remove |
350 | }; |
360 | }; |
351 | |
361 | |
|
|
362 | ############################################################################# |
|
|
363 | # core extensions - in perl |
|
|
364 | |
|
|
365 | =item cf::player::exists $login |
|
|
366 | |
|
|
367 | Returns true when the given account exists. |
|
|
368 | |
|
|
369 | =cut |
|
|
370 | |
|
|
371 | sub cf::player::exists($) { |
|
|
372 | cf::player::find $_[0] |
|
|
373 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
374 | } |
|
|
375 | |
|
|
376 | # rough implementation of a future "reply" method that works |
|
|
377 | # with dialog boxes. |
|
|
378 | sub cf::object::player::reply($$$;$) { |
|
|
379 | my ($self, $npc, $msg, $flags) = @_; |
|
|
380 | |
|
|
381 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
382 | |
|
|
383 | if ($self->{record_replies}) { |
|
|
384 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
385 | } else { |
|
|
386 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
387 | $self->message ($msg, $flags); |
|
|
388 | } |
|
|
389 | } |
|
|
390 | |
|
|
391 | ############################################################################# |
|
|
392 | # map scripting support |
|
|
393 | |
|
|
394 | our $safe = new Safe "ext"; |
|
|
395 | our $safe_hole = new Safe::Hole; |
|
|
396 | |
|
|
397 | $SIG{FPE} = 'IGNORE'; |
|
|
398 | |
|
|
399 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
400 | |
|
|
401 | # here we export the classes and methods available to script code |
|
|
402 | |
|
|
403 | for ( |
|
|
404 | ["cf::object" => qw(contr)], |
|
|
405 | ["cf::object::player" => qw(player)], |
|
|
406 | ["cf::player" => qw(peaceful)], |
|
|
407 | ) { |
|
|
408 | no strict 'refs'; |
|
|
409 | my ($pkg, @funs) = @$_; |
|
|
410 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
411 | for @funs; |
|
|
412 | } |
|
|
413 | |
|
|
414 | sub safe_eval($;@) { |
|
|
415 | my ($code, %vars) = @_; |
|
|
416 | |
|
|
417 | my $qcode = $code; |
|
|
418 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
419 | $qcode =~ s/\n/\\n/g; |
|
|
420 | |
|
|
421 | local $_; |
|
|
422 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
423 | |
|
|
424 | $code = |
|
|
425 | "do {\n" |
|
|
426 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
427 | . "#line 0 \"{$qcode}\"\n" |
|
|
428 | . $code |
|
|
429 | . "\n}" |
|
|
430 | ; |
|
|
431 | |
|
|
432 | sub_generation_inc; |
|
|
433 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
434 | sub_generation_inc; |
|
|
435 | |
|
|
436 | wantarray ? @res : $res[0] |
|
|
437 | } |
|
|
438 | |
|
|
439 | sub register_script_function { |
|
|
440 | my ($fun, $cb) = @_; |
|
|
441 | |
|
|
442 | no strict 'refs'; |
|
|
443 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
444 | } |
|
|
445 | |
|
|
446 | ############################################################################# |
|
|
447 | # initialisation |
|
|
448 | |
352 | register "<global>", __PACKAGE__; |
449 | register "<global>", __PACKAGE__; |
353 | |
450 | |
354 | unshift @INC, maps_directory "perl"; |
451 | unshift @INC, maps_directory "perl"; |
355 | |
452 | |
356 | load_extensions; |
453 | load_extensions; |