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; |
11 | |
14 | |
12 | our %COMMAND; |
15 | our %COMMAND; |
13 | our @EVENT; |
16 | our @EVENT; |
14 | our %PROP_TYPE; |
17 | our %PROP_TYPE; |
15 | our %PROP_IDX; |
18 | our %PROP_IDX; |
|
|
19 | our $LIBDIR = maps_directory "perl"; |
16 | |
20 | |
17 | BEGIN { |
21 | BEGIN { |
18 | @EVENT = map lc, @EVENT; |
22 | @EVENT = map lc, @EVENT; |
19 | |
23 | |
20 | *CORE::GLOBAL::warn = sub { |
24 | *CORE::GLOBAL::warn = sub { |
… | |
… | |
61 | prop_gen MAP_PROP => "cf::map"; |
65 | prop_gen MAP_PROP => "cf::map"; |
62 | prop_gen ARCH_PROP => "cf::arch"; |
66 | prop_gen ARCH_PROP => "cf::arch"; |
63 | |
67 | |
64 | # guessed hierarchies |
68 | # guessed hierarchies |
65 | |
69 | |
66 | @cf::object::player::ISA = 'cf::object'; |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
67 | @cf::object::map::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
72 | |
|
|
73 | # we bless all objects into derived classes to force a method lookup |
|
|
74 | # within the Safe compartment. |
|
|
75 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
|
|
76 | no strict 'refs'; |
|
|
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
|
|
78 | } |
68 | |
79 | |
69 | $Event::DIED = sub { |
80 | $Event::DIED = sub { |
70 | warn "error in event callback: @_"; |
81 | warn "error in event callback: @_"; |
71 | }; |
82 | }; |
72 | |
83 | |
… | |
… | |
160 | |
171 | |
161 | push @exts, $pkg; |
172 | push @exts, $pkg; |
162 | $ext_pkg{$base} = $pkg; |
173 | $ext_pkg{$base} = $pkg; |
163 | |
174 | |
164 | # no strict 'refs'; |
175 | # no strict 'refs'; |
165 | # @{"$pkg\::ISA"} = cf::ext::; |
176 | # @{"$pkg\::ISA"} = ext::; |
166 | |
177 | |
167 | register $base, $pkg; |
178 | register $base, $pkg; |
168 | } |
179 | } |
169 | |
180 | |
170 | sub unload_extension { |
181 | sub unload_extension { |
… | |
… | |
193 | # remove extcmds |
204 | # remove extcmds |
194 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
205 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
195 | delete $extcmd{$name}; |
206 | delete $extcmd{$name}; |
196 | } |
207 | } |
197 | |
208 | |
|
|
209 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
210 | eval { |
|
|
211 | $cb->($pkg); |
|
|
212 | 1 |
|
|
213 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
214 | } |
|
|
215 | |
198 | Symbol::delete_package $pkg; |
216 | Symbol::delete_package $pkg; |
199 | } |
217 | } |
200 | |
218 | |
201 | sub load_extensions { |
219 | sub load_extensions { |
202 | my $LIBDIR = maps_directory "perl"; |
220 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
216 | if ($who->flag (FLAG_WIZ)) { |
234 | if ($who->flag (FLAG_WIZ)) { |
217 | $who->message ("reloading..."); |
235 | $who->message ("reloading..."); |
218 | |
236 | |
219 | warn "reloading...\n"; |
237 | warn "reloading...\n"; |
220 | eval { |
238 | eval { |
|
|
239 | # 1. cancel all watchers |
221 | $_->cancel for Event::all_watchers; |
240 | $_->cancel for Event::all_watchers; |
222 | |
241 | |
|
|
242 | # 2. unload all extensions |
|
|
243 | for (@exts) { |
|
|
244 | $who->message ("unloading <$_>"); |
223 | unload_extension $_ for @exts; |
245 | unload_extension $_; |
|
|
246 | } |
|
|
247 | |
|
|
248 | # 3. unload all modules loaded from $LIBDIR |
|
|
249 | while (my ($k, $v) = each %INC) { |
|
|
250 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
251 | |
|
|
252 | $who->message ("removing <$k>"); |
|
|
253 | delete $INC{$k}; |
|
|
254 | |
|
|
255 | $k =~ s/\.pm$//; |
|
|
256 | $k =~ s/\//::/g; |
|
|
257 | |
|
|
258 | if (my $cb = $k->can ("unload_module")) { |
|
|
259 | $cb->(); |
|
|
260 | } |
|
|
261 | |
|
|
262 | Symbol::delete_package $k; |
|
|
263 | } |
|
|
264 | |
|
|
265 | # 4. get rid of ext::, as good as possible |
|
|
266 | Symbol::delete_package "ext::$_" |
|
|
267 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
268 | |
|
|
269 | # 5. remove register_script_function callbacks |
|
|
270 | # TODO |
|
|
271 | |
|
|
272 | # 6. unload cf.pm "a bit" |
224 | delete $INC{"cf.pm"}; |
273 | delete $INC{"cf.pm"}; |
225 | |
274 | |
226 | # don't, removes xs symbols, too |
275 | # don't, removes xs symbols, too |
227 | #Symbol::delete_package $pkg; |
276 | #Symbol::delete_package __PACKAGE__; |
228 | |
277 | |
|
|
278 | # 7. reload cf.pm |
|
|
279 | $who->message ("reloading cf.pm"); |
229 | require cf; |
280 | require cf; |
230 | }; |
281 | }; |
231 | warn $@ if $@; |
282 | warn $@ if $@; |
232 | $who->message ($@) if $@; |
283 | $who->message ($@) if $@; |
233 | warn "reloaded\n"; |
284 | warn "reloaded\n"; |
… | |
… | |
237 | $who->message ("Intruder Alert!"); |
288 | $who->message ("Intruder Alert!"); |
238 | } |
289 | } |
239 | }; |
290 | }; |
240 | |
291 | |
241 | ############################################################################# |
292 | ############################################################################# |
|
|
293 | # utility functions |
|
|
294 | |
|
|
295 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
296 | |
|
|
297 | sub from_json($) { |
|
|
298 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
299 | JSON::Syck::Load $_[0] |
|
|
300 | } |
|
|
301 | |
|
|
302 | sub to_json($) { |
|
|
303 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
304 | JSON::Syck::Dump $_[0] |
|
|
305 | } |
|
|
306 | |
|
|
307 | ############################################################################# |
242 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
308 | # extcmd framework, basically convert ext <msg> |
243 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
309 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
244 | |
310 | |
245 | sub on_extcmd { |
311 | sub on_extcmd { |
246 | my ($pl, $buf) = @_; |
312 | my ($pl, $buf) = @_; |
247 | |
313 | |
248 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
314 | my $msg = eval { from_json $buf }; |
249 | |
315 | |
250 | $extcmd{$type}[0]->($pl, $buf) |
316 | if (ref $msg) { |
251 | if $extcmd{$type}; |
317 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
318 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
319 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
320 | } |
|
|
321 | } |
|
|
322 | } else { |
|
|
323 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
324 | } |
|
|
325 | |
|
|
326 | 1 |
252 | } |
327 | } |
253 | |
328 | |
254 | ############################################################################# |
329 | ############################################################################# |
255 | # load/save/clean perl data associated with a map |
330 | # load/save/clean perl data associated with a map |
256 | |
331 | |
… | |
… | |
312 | # load/save perl data associated with player->ob objects |
387 | # load/save perl data associated with player->ob objects |
313 | |
388 | |
314 | *on_player_load = sub { |
389 | *on_player_load = sub { |
315 | my ($ob, $path) = @_; |
390 | my ($ob, $path) = @_; |
316 | |
391 | |
317 | if (open my $fh, "<:raw", "$path.cfperl") { |
|
|
318 | |
|
|
319 | #d##TODO#remove |
|
|
320 | |
|
|
321 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
322 | |
|
|
323 | $data->{version} <= 1 |
|
|
324 | or return; # too new |
|
|
325 | |
|
|
326 | %$ob = %{$data->{ob}}; |
|
|
327 | return; |
|
|
328 | } |
|
|
329 | |
|
|
330 | for my $o ($ob, $ob->inv) { |
392 | for my $o ($ob, $ob->inv) { |
331 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
393 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
332 | $o->set_ob_key_value ("_perl_data"); |
394 | $o->set_ob_key_value ("_perl_data"); |
333 | |
395 | |
334 | %$o = %{ Storable::thaw pack "H*", $value }; |
396 | %$o = %{ Storable::thaw pack "H*", $value }; |
… | |
… | |
339 | *on_player_save = sub { |
401 | *on_player_save = sub { |
340 | my ($ob, $path) = @_; |
402 | my ($ob, $path) = @_; |
341 | |
403 | |
342 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
404 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
343 | for grep %$_, $ob, $ob->inv; |
405 | for grep %$_, $ob, $ob->inv; |
344 | |
|
|
345 | unlink "$path.cfperl";#d##TODO#remove |
|
|
346 | }; |
406 | }; |
|
|
407 | |
|
|
408 | ############################################################################# |
|
|
409 | # core extensions - in perl |
|
|
410 | |
|
|
411 | =item cf::player::exists $login |
|
|
412 | |
|
|
413 | Returns true when the given account exists. |
|
|
414 | |
|
|
415 | =cut |
|
|
416 | |
|
|
417 | sub cf::player::exists($) { |
|
|
418 | cf::player::find $_[0] |
|
|
419 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
420 | } |
|
|
421 | |
|
|
422 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
423 | |
|
|
424 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
425 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
426 | dialogue with the given NPC character. |
|
|
427 | |
|
|
428 | =cut |
|
|
429 | |
|
|
430 | # rough implementation of a future "reply" method that works |
|
|
431 | # with dialog boxes. |
|
|
432 | sub cf::object::player::reply($$$;$) { |
|
|
433 | my ($self, $npc, $msg, $flags) = @_; |
|
|
434 | |
|
|
435 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
436 | |
|
|
437 | if ($self->{record_replies}) { |
|
|
438 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
439 | } else { |
|
|
440 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
441 | $self->message ($msg, $flags); |
|
|
442 | } |
|
|
443 | } |
|
|
444 | |
|
|
445 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
446 | |
|
|
447 | Sends an ext reply to the player. |
|
|
448 | |
|
|
449 | =cut |
|
|
450 | |
|
|
451 | sub cf::player::ext_reply($$$%) { |
|
|
452 | my ($self, $id, %msg) = @_; |
|
|
453 | |
|
|
454 | $msg{msgid} = $id; |
|
|
455 | |
|
|
456 | $self->send ("ext " . to_json \%msg); |
|
|
457 | } |
|
|
458 | |
|
|
459 | ############################################################################# |
|
|
460 | # map scripting support |
|
|
461 | |
|
|
462 | our $safe = new Safe "ext"; |
|
|
463 | our $safe_hole = new Safe::Hole; |
|
|
464 | |
|
|
465 | $SIG{FPE} = 'IGNORE'; |
|
|
466 | |
|
|
467 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
468 | |
|
|
469 | # here we export the classes and methods available to script code |
|
|
470 | |
|
|
471 | for ( |
|
|
472 | ["cf::object" => qw(contr pay_amount pay_player)], |
|
|
473 | ["cf::object::player" => qw(player)], |
|
|
474 | ["cf::player" => qw(peaceful)], |
|
|
475 | ) { |
|
|
476 | no strict 'refs'; |
|
|
477 | my ($pkg, @funs) = @$_; |
|
|
478 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
479 | for @funs; |
|
|
480 | } |
|
|
481 | |
|
|
482 | sub safe_eval($;@) { |
|
|
483 | my ($code, %vars) = @_; |
|
|
484 | |
|
|
485 | my $qcode = $code; |
|
|
486 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
487 | $qcode =~ s/\n/\\n/g; |
|
|
488 | |
|
|
489 | local $_; |
|
|
490 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
491 | |
|
|
492 | $code = |
|
|
493 | "do {\n" |
|
|
494 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
495 | . "#line 0 \"{$qcode}\"\n" |
|
|
496 | . $code |
|
|
497 | . "\n}" |
|
|
498 | ; |
|
|
499 | |
|
|
500 | sub_generation_inc; |
|
|
501 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
502 | sub_generation_inc; |
|
|
503 | |
|
|
504 | wantarray ? @res : $res[0] |
|
|
505 | } |
|
|
506 | |
|
|
507 | sub register_script_function { |
|
|
508 | my ($fun, $cb) = @_; |
|
|
509 | |
|
|
510 | no strict 'refs'; |
|
|
511 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
512 | } |
|
|
513 | |
|
|
514 | ############################################################################# |
|
|
515 | # initialisation |
347 | |
516 | |
348 | register "<global>", __PACKAGE__; |
517 | register "<global>", __PACKAGE__; |
349 | |
518 | |
350 | unshift @INC, maps_directory "perl"; |
519 | unshift @INC, $LIBDIR; |
351 | |
520 | |
352 | load_extensions; |
521 | load_extensions; |
353 | |
522 | |
354 | 1 |
523 | 1 |
355 | |
524 | |