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