… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use Time::HiRes; |
10 | use Event; |
11 | use Event; |
11 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | |
13 | |
13 | use strict; |
14 | use strict; |
14 | |
15 | |
… | |
… | |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
72 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
72 | |
73 | |
73 | # we bless all objects into derived classes to force a method lookup |
74 | # we bless all objects into derived classes to force a method lookup |
74 | # within the Safe compartment. |
75 | # 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)) { |
76 | 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 | no strict 'refs'; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | } |
79 | } |
79 | |
80 | |
80 | $Event::DIED = sub { |
81 | $Event::DIED = sub { |
… | |
… | |
180 | |
181 | |
181 | sub unload_extension { |
182 | sub unload_extension { |
182 | my ($pkg) = @_; |
183 | my ($pkg) = @_; |
183 | |
184 | |
184 | warn "removing extension $pkg\n"; |
185 | warn "removing extension $pkg\n"; |
185 | |
|
|
186 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
187 | $cb->($pkg); |
|
|
188 | } |
|
|
189 | |
186 | |
190 | # remove hooks |
187 | # remove hooks |
191 | for my $idx (0 .. $#EVENT) { |
188 | for my $idx (0 .. $#EVENT) { |
192 | delete $hook[$idx]{$pkg}; |
189 | delete $hook[$idx]{$pkg}; |
193 | } |
190 | } |
… | |
… | |
208 | # remove extcmds |
205 | # remove extcmds |
209 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
206 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | delete $extcmd{$name}; |
207 | delete $extcmd{$name}; |
211 | } |
208 | } |
212 | |
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 | |
213 | Symbol::delete_package $pkg; |
217 | Symbol::delete_package $pkg; |
214 | } |
218 | } |
215 | |
219 | |
216 | sub load_extensions { |
220 | sub load_extensions { |
217 | my $LIBDIR = maps_directory "perl"; |
221 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
285 | $who->message ("Intruder Alert!"); |
289 | $who->message ("Intruder Alert!"); |
286 | } |
290 | } |
287 | }; |
291 | }; |
288 | |
292 | |
289 | ############################################################################# |
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 | ############################################################################# |
290 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
309 | # extcmd framework, basically convert ext <msg> |
291 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
310 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
292 | |
311 | |
293 | sub on_extcmd { |
312 | sub on_extcmd { |
294 | my ($pl, $buf) = @_; |
313 | my ($pl, $buf) = @_; |
295 | |
314 | |
296 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
315 | my $msg = eval { from_json $buf }; |
297 | |
316 | |
298 | $extcmd{$type}[0]->($pl, $buf) |
317 | if (ref $msg) { |
299 | 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 |
300 | } |
328 | } |
301 | |
329 | |
302 | ############################################################################# |
330 | ############################################################################# |
303 | # load/save/clean perl data associated with a map |
331 | # load/save/clean perl data associated with a map |
304 | |
332 | |
… | |
… | |
357 | }; |
385 | }; |
358 | |
386 | |
359 | ############################################################################# |
387 | ############################################################################# |
360 | # load/save perl data associated with player->ob objects |
388 | # load/save perl data associated with player->ob objects |
361 | |
389 | |
|
|
390 | sub all_objects(@) { |
|
|
391 | @_, map all_objects ($_->inv), @_ |
|
|
392 | } |
|
|
393 | |
362 | *on_player_load = sub { |
394 | *on_player_load = sub { |
363 | my ($ob, $path) = @_; |
395 | my ($ob, $path) = @_; |
364 | |
396 | |
365 | for my $o ($ob, $ob->inv) { |
397 | for my $o (all_objects $ob) { |
366 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
398 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
367 | $o->set_ob_key_value ("_perl_data"); |
399 | $o->set_ob_key_value ("_perl_data"); |
368 | |
400 | |
369 | %$o = %{ Storable::thaw pack "H*", $value }; |
401 | %$o = %{ Storable::thaw pack "H*", $value }; |
370 | } |
402 | } |
… | |
… | |
373 | |
405 | |
374 | *on_player_save = sub { |
406 | *on_player_save = sub { |
375 | my ($ob, $path) = @_; |
407 | my ($ob, $path) = @_; |
376 | |
408 | |
377 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
409 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
378 | for grep %$_, $ob, $ob->inv; |
410 | for grep %$_, all_objects $ob; |
379 | }; |
411 | }; |
380 | |
412 | |
381 | ############################################################################# |
413 | ############################################################################# |
382 | # core extensions - in perl |
414 | # core extensions - in perl |
|
|
415 | |
|
|
416 | my $delta_timer = Event->timer ( |
|
|
417 | parked => 1, |
|
|
418 | prio => Event::PRIO_HIGH, |
|
|
419 | cb => sub { Event::unloop (undef) }, |
|
|
420 | ); |
|
|
421 | |
|
|
422 | sub sleep_delta($) { |
|
|
423 | $delta_timer->at (Event::time + $_[0]); |
|
|
424 | $delta_timer->start; |
|
|
425 | Event::loop; |
|
|
426 | } |
383 | |
427 | |
384 | =item cf::player::exists $login |
428 | =item cf::player::exists $login |
385 | |
429 | |
386 | Returns true when the given account exists. |
430 | Returns true when the given account exists. |
387 | |
431 | |
… | |
… | |
389 | |
433 | |
390 | sub cf::player::exists($) { |
434 | sub cf::player::exists($) { |
391 | cf::player::find $_[0] |
435 | cf::player::find $_[0] |
392 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
436 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
393 | } |
437 | } |
|
|
438 | |
|
|
439 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
440 | |
|
|
441 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
442 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
443 | dialogue with the given NPC character. |
|
|
444 | |
|
|
445 | =cut |
394 | |
446 | |
395 | # rough implementation of a future "reply" method that works |
447 | # rough implementation of a future "reply" method that works |
396 | # with dialog boxes. |
448 | # with dialog boxes. |
397 | sub cf::object::player::reply($$$;$) { |
449 | sub cf::object::player::reply($$$;$) { |
398 | my ($self, $npc, $msg, $flags) = @_; |
450 | my ($self, $npc, $msg, $flags) = @_; |
… | |
… | |
403 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
455 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
404 | } else { |
456 | } else { |
405 | $msg = $npc->name . " says: $msg" if $npc; |
457 | $msg = $npc->name . " says: $msg" if $npc; |
406 | $self->message ($msg, $flags); |
458 | $self->message ($msg, $flags); |
407 | } |
459 | } |
|
|
460 | } |
|
|
461 | |
|
|
462 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
463 | |
|
|
464 | Sends an ext reply to the player. |
|
|
465 | |
|
|
466 | =cut |
|
|
467 | |
|
|
468 | sub cf::player::ext_reply($$$%) { |
|
|
469 | my ($self, $id, %msg) = @_; |
|
|
470 | |
|
|
471 | $msg{msgid} = $id; |
|
|
472 | |
|
|
473 | $self->send ("ext " . to_json \%msg); |
408 | } |
474 | } |
409 | |
475 | |
410 | ############################################################################# |
476 | ############################################################################# |
411 | # map scripting support |
477 | # map scripting support |
412 | |
478 | |