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; |
|
|
9 | |
|
|
10 | use Event; |
|
|
11 | $Event::Eval = 1; # no idea why this is required, but it is |
6 | |
12 | |
7 | use strict; |
13 | use strict; |
8 | |
14 | |
9 | our %COMMAND; |
15 | our %COMMAND; |
10 | our @EVENT; |
16 | our @EVENT; |
… | |
… | |
58 | prop_gen MAP_PROP => "cf::map"; |
64 | prop_gen MAP_PROP => "cf::map"; |
59 | prop_gen ARCH_PROP => "cf::arch"; |
65 | prop_gen ARCH_PROP => "cf::arch"; |
60 | |
66 | |
61 | # guessed hierarchies |
67 | # guessed hierarchies |
62 | |
68 | |
63 | @cf::object::player::ISA = 'cf::object'; |
69 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
64 | @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 | } |
|
|
78 | |
|
|
79 | $Event::DIED = sub { |
|
|
80 | warn "error in event callback: @_"; |
|
|
81 | }; |
65 | |
82 | |
66 | my %ext_pkg; |
83 | my %ext_pkg; |
67 | my @exts; |
84 | my @exts; |
68 | my @hook; |
85 | my @hook; |
69 | my %command; |
86 | my %command; |
… | |
… | |
153 | |
170 | |
154 | push @exts, $pkg; |
171 | push @exts, $pkg; |
155 | $ext_pkg{$base} = $pkg; |
172 | $ext_pkg{$base} = $pkg; |
156 | |
173 | |
157 | # no strict 'refs'; |
174 | # no strict 'refs'; |
158 | # @{"$pkg\::ISA"} = cf::ext::; |
175 | # @{"$pkg\::ISA"} = ext::; |
159 | |
176 | |
160 | register $base, $pkg; |
177 | register $base, $pkg; |
161 | } |
178 | } |
162 | |
179 | |
163 | sub unload_extension { |
180 | sub unload_extension { |
164 | my ($pkg) = @_; |
181 | my ($pkg) = @_; |
165 | |
182 | |
166 | warn "removing extension $pkg\n"; |
183 | warn "removing extension $pkg\n"; |
|
|
184 | |
|
|
185 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
186 | $cb->($pkg); |
|
|
187 | } |
167 | |
188 | |
168 | # remove hooks |
189 | # remove hooks |
169 | for my $idx (0 .. $#EVENT) { |
190 | for my $idx (0 .. $#EVENT) { |
170 | delete $hook[$idx]{$pkg}; |
191 | delete $hook[$idx]{$pkg}; |
171 | } |
192 | } |
… | |
… | |
209 | if ($who->flag (FLAG_WIZ)) { |
230 | if ($who->flag (FLAG_WIZ)) { |
210 | $who->message ("reloading..."); |
231 | $who->message ("reloading..."); |
211 | |
232 | |
212 | warn "reloading...\n"; |
233 | warn "reloading...\n"; |
213 | eval { |
234 | eval { |
|
|
235 | $_->cancel for Event::all_watchers; |
|
|
236 | |
214 | unload_extension $_ for @exts; |
237 | unload_extension $_ for @exts; |
215 | delete $INC{"cf.pm"}; |
238 | delete $INC{"cf.pm"}; |
216 | |
239 | |
217 | # don't, removes xs symbols, too |
240 | # don't, removes xs symbols, too |
218 | #Symbol::delete_package $pkg; |
241 | #Symbol::delete_package __PACKAGE__; |
219 | |
242 | |
220 | require cf; |
243 | require cf; |
221 | }; |
244 | }; |
222 | warn $@ if $@; |
245 | warn $@ if $@; |
223 | $who->message ($@) if $@; |
246 | $who->message ($@) if $@; |
… | |
… | |
303 | # load/save perl data associated with player->ob objects |
326 | # load/save perl data associated with player->ob objects |
304 | |
327 | |
305 | *on_player_load = sub { |
328 | *on_player_load = sub { |
306 | my ($ob, $path) = @_; |
329 | my ($ob, $path) = @_; |
307 | |
330 | |
308 | if (open my $fh, "<:raw", "$path.cfperl") { |
|
|
309 | |
|
|
310 | #d##TODO#remove |
|
|
311 | |
|
|
312 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
313 | |
|
|
314 | $data->{version} <= 1 |
|
|
315 | or return; # too new |
|
|
316 | |
|
|
317 | %$ob = %{$data->{ob}}; |
|
|
318 | return; |
|
|
319 | } |
|
|
320 | |
|
|
321 | for my $o ($ob, $ob->inv) { |
331 | for my $o ($ob, $ob->inv) { |
322 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
332 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
323 | $o->set_ob_key_value ("_perl_data"); |
333 | $o->set_ob_key_value ("_perl_data"); |
324 | |
334 | |
325 | %$o = %{ Storable::thaw pack "H*", $value }; |
335 | %$o = %{ Storable::thaw pack "H*", $value }; |
… | |
… | |
330 | *on_player_save = sub { |
340 | *on_player_save = sub { |
331 | my ($ob, $path) = @_; |
341 | my ($ob, $path) = @_; |
332 | |
342 | |
333 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
343 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
334 | for grep %$_, $ob, $ob->inv; |
344 | for grep %$_, $ob, $ob->inv; |
335 | |
|
|
336 | unlink "$path.cfperl";#d##TODO#remove |
|
|
337 | }; |
345 | }; |
|
|
346 | |
|
|
347 | ############################################################################# |
|
|
348 | # core extensions - in perl |
|
|
349 | |
|
|
350 | =item cf::player::exists $login |
|
|
351 | |
|
|
352 | Returns true when the given account exists. |
|
|
353 | |
|
|
354 | =cut |
|
|
355 | |
|
|
356 | sub cf::player::exists($) { |
|
|
357 | cf::player::find $_[0] |
|
|
358 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
359 | } |
|
|
360 | |
|
|
361 | # rough implementation of a future "reply" method that works |
|
|
362 | # with dialog boxes. |
|
|
363 | sub cf::object::player::reply($$$;$) { |
|
|
364 | my ($self, $npc, $msg, $flags) = @_; |
|
|
365 | |
|
|
366 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
367 | |
|
|
368 | if ($self->{record_replies}) { |
|
|
369 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
370 | } else { |
|
|
371 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
372 | $self->message ($msg, $flags); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
|
|
376 | ############################################################################# |
|
|
377 | # map scripting support |
|
|
378 | |
|
|
379 | our $safe = new Safe "ext"; |
|
|
380 | our $safe_hole = new Safe::Hole; |
|
|
381 | |
|
|
382 | $SIG{FPE} = 'IGNORE'; |
|
|
383 | |
|
|
384 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
385 | |
|
|
386 | # here we export the classes and methods available to script code |
|
|
387 | |
|
|
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 | } |
|
|
398 | |
|
|
399 | sub safe_eval($;@) { |
|
|
400 | my ($code, %vars) = @_; |
|
|
401 | |
|
|
402 | my $qcode = $code; |
|
|
403 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
404 | $qcode =~ s/\n/\\n/g; |
|
|
405 | |
|
|
406 | local $_; |
|
|
407 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
408 | |
|
|
409 | $code = |
|
|
410 | "do {\n" |
|
|
411 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
412 | . "#line 0 \"{$qcode}\"\n" |
|
|
413 | . $code |
|
|
414 | . "\n}" |
|
|
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] |
|
|
422 | } |
|
|
423 | |
|
|
424 | sub register_script_function { |
|
|
425 | my ($fun, $cb) = @_; |
|
|
426 | |
|
|
427 | no strict 'refs'; |
|
|
428 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
429 | } |
|
|
430 | |
|
|
431 | ############################################################################# |
|
|
432 | # initialisation |
338 | |
433 | |
339 | register "<global>", __PACKAGE__; |
434 | register "<global>", __PACKAGE__; |
340 | |
435 | |
|
|
436 | unshift @INC, maps_directory "perl"; |
|
|
437 | |
341 | load_extensions; |
438 | load_extensions; |
342 | |
439 | |
343 | 1 |
440 | 1 |
344 | |
441 | |