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; |
… | |
… | |
160 | |
163 | |
161 | push @exts, $pkg; |
164 | push @exts, $pkg; |
162 | $ext_pkg{$base} = $pkg; |
165 | $ext_pkg{$base} = $pkg; |
163 | |
166 | |
164 | # no strict 'refs'; |
167 | # no strict 'refs'; |
165 | # @{"$pkg\::ISA"} = cf::ext::; |
168 | # @{"$pkg\::ISA"} = ext::; |
166 | |
169 | |
167 | register $base, $pkg; |
170 | register $base, $pkg; |
168 | } |
171 | } |
169 | |
172 | |
170 | sub unload_extension { |
173 | sub unload_extension { |
… | |
… | |
350 | }; |
353 | }; |
351 | |
354 | |
352 | ############################################################################# |
355 | ############################################################################# |
353 | # core extensions - in perl |
356 | # core extensions - in perl |
354 | |
357 | |
|
|
358 | =item cf::player::exists $login |
|
|
359 | |
|
|
360 | Returns true when the given account exists. |
|
|
361 | |
|
|
362 | =cut |
|
|
363 | |
|
|
364 | sub cf::player::exists($) { |
|
|
365 | cf::player::find $_[0] |
|
|
366 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
367 | } |
|
|
368 | |
355 | # rough implementation of a future "reply" method that works |
369 | # rough implementation of a future "reply" method that works |
356 | # with dialog boxes. |
370 | # with dialog boxes. |
357 | sub cf::object::player::reply($$;$) { |
371 | sub cf::object::player::reply($$$;$) { |
358 | my ($self, $msg, $flags) = @_; |
372 | my ($self, $npc, $msg, $flags) = @_; |
359 | |
373 | |
360 | $flags = cf::NDI_WHITE unless @_ >= 3; |
374 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
375 | |
|
|
376 | $msg = $npc->name . " says: $msg" if $npc; |
361 | |
377 | |
362 | $self->message ($msg, $flags); |
378 | $self->message ($msg, $flags); |
363 | } |
379 | } |
364 | |
380 | |
365 | ############################################################################# |
381 | ############################################################################# |
|
|
382 | # map scripting support |
|
|
383 | |
|
|
384 | our $safe = new Safe "ext"; |
|
|
385 | our $safe_hole = new Safe::Hole; |
|
|
386 | |
|
|
387 | $SIG{FPE} = 'IGNORE'; |
|
|
388 | |
|
|
389 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
390 | |
|
|
391 | # here we would export the classes and methods available to script code |
|
|
392 | #@ext::cf::object::player::ISA = @cf::object::player::ISA; |
|
|
393 | #@ext::cf::object::map::ISA = @cf::object::map::ISA; |
|
|
394 | |
|
|
395 | sub safe_eval($;@) { |
|
|
396 | my ($code, %vars) = @_; |
|
|
397 | |
|
|
398 | my $qcode = $code; |
|
|
399 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
400 | $qcode =~ s/\n/\\n/g; |
|
|
401 | |
|
|
402 | local $_; |
|
|
403 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
404 | |
|
|
405 | $safe->reval ( |
|
|
406 | "do {\n" |
|
|
407 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
408 | . "#line 0 \"{$qcode}\"\n" |
|
|
409 | . $code |
|
|
410 | . "\n}" |
|
|
411 | ) |
|
|
412 | } |
|
|
413 | |
|
|
414 | sub register_script_function { |
|
|
415 | my ($fun, $cb) = @_; |
|
|
416 | |
|
|
417 | no strict 'refs'; |
|
|
418 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
419 | } |
|
|
420 | |
|
|
421 | ############################################################################# |
366 | # initialisation |
422 | # initialisation |
367 | |
423 | |
368 | register "<global>", __PACKAGE__; |
424 | register "<global>", __PACKAGE__; |
369 | |
425 | |
370 | unshift @INC, maps_directory "perl"; |
426 | unshift @INC, maps_directory "perl"; |