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; |
… | |
… | |
60 | |
66 | |
61 | # guessed hierarchies |
67 | # guessed hierarchies |
62 | |
68 | |
63 | @cf::object::player::ISA = 'cf::object'; |
69 | @cf::object::player::ISA = 'cf::object'; |
64 | @cf::object::map::ISA = 'cf::object'; |
70 | @cf::object::map::ISA = 'cf::object'; |
|
|
71 | |
|
|
72 | $Event::DIED = sub { |
|
|
73 | warn "error in event callback: @_"; |
|
|
74 | }; |
65 | |
75 | |
66 | my %ext_pkg; |
76 | my %ext_pkg; |
67 | my @exts; |
77 | my @exts; |
68 | my @hook; |
78 | my @hook; |
69 | my %command; |
79 | my %command; |
… | |
… | |
153 | |
163 | |
154 | push @exts, $pkg; |
164 | push @exts, $pkg; |
155 | $ext_pkg{$base} = $pkg; |
165 | $ext_pkg{$base} = $pkg; |
156 | |
166 | |
157 | # no strict 'refs'; |
167 | # no strict 'refs'; |
158 | # @{"$pkg\::ISA"} = cf::ext::; |
168 | # @{"$pkg\::ISA"} = ext::; |
159 | |
169 | |
160 | register $base, $pkg; |
170 | register $base, $pkg; |
161 | } |
171 | } |
162 | |
172 | |
163 | sub unload_extension { |
173 | sub unload_extension { |
164 | my ($pkg) = @_; |
174 | my ($pkg) = @_; |
165 | |
175 | |
166 | warn "removing extension $pkg\n"; |
176 | warn "removing extension $pkg\n"; |
|
|
177 | |
|
|
178 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
179 | $cb->($pkg); |
|
|
180 | } |
167 | |
181 | |
168 | # remove hooks |
182 | # remove hooks |
169 | for my $idx (0 .. $#EVENT) { |
183 | for my $idx (0 .. $#EVENT) { |
170 | delete $hook[$idx]{$pkg}; |
184 | delete $hook[$idx]{$pkg}; |
171 | } |
185 | } |
… | |
… | |
209 | if ($who->flag (FLAG_WIZ)) { |
223 | if ($who->flag (FLAG_WIZ)) { |
210 | $who->message ("reloading..."); |
224 | $who->message ("reloading..."); |
211 | |
225 | |
212 | warn "reloading...\n"; |
226 | warn "reloading...\n"; |
213 | eval { |
227 | eval { |
|
|
228 | $_->cancel for Event::all_watchers; |
|
|
229 | |
214 | unload_extension $_ for @exts; |
230 | unload_extension $_ for @exts; |
215 | delete $INC{"cf.pm"}; |
231 | delete $INC{"cf.pm"}; |
216 | |
232 | |
217 | # don't, removes xs symbols, too |
233 | # don't, removes xs symbols, too |
218 | #Symbol::delete_package $pkg; |
234 | #Symbol::delete_package __PACKAGE__; |
219 | |
235 | |
220 | require cf; |
236 | require cf; |
221 | }; |
237 | }; |
222 | warn $@ if $@; |
238 | warn $@ if $@; |
223 | $who->message ($@) if $@; |
239 | $who->message ($@) if $@; |
… | |
… | |
334 | for grep %$_, $ob, $ob->inv; |
350 | for grep %$_, $ob, $ob->inv; |
335 | |
351 | |
336 | unlink "$path.cfperl";#d##TODO#remove |
352 | unlink "$path.cfperl";#d##TODO#remove |
337 | }; |
353 | }; |
338 | |
354 | |
|
|
355 | ############################################################################# |
|
|
356 | # core extensions - in perl |
|
|
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 | |
|
|
369 | # rough implementation of a future "reply" method that works |
|
|
370 | # with dialog boxes. |
|
|
371 | sub cf::object::player::reply($$$;$) { |
|
|
372 | my ($self, $npc, $msg, $flags) = @_; |
|
|
373 | |
|
|
374 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
375 | |
|
|
376 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
377 | |
|
|
378 | $self->message ($msg, $flags); |
|
|
379 | } |
|
|
380 | |
|
|
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 | ############################################################################# |
|
|
422 | # initialisation |
|
|
423 | |
339 | register "<global>", __PACKAGE__; |
424 | register "<global>", __PACKAGE__; |
340 | |
425 | |
341 | unshift @INC, maps_directory "perl"; |
426 | unshift @INC, maps_directory "perl"; |
342 | |
427 | |
343 | load_extensions; |
428 | load_extensions; |