… | |
… | |
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 | |
… | |
… | |
379 | }; |
407 | }; |
380 | |
408 | |
381 | ############################################################################# |
409 | ############################################################################# |
382 | # core extensions - in perl |
410 | # core extensions - in perl |
383 | |
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 | |
384 | =item cf::player::exists $login |
424 | =item cf::player::exists $login |
385 | |
425 | |
386 | Returns true when the given account exists. |
426 | Returns true when the given account exists. |
387 | |
427 | |
388 | =cut |
428 | =cut |
389 | |
429 | |
390 | sub cf::player::exists($) { |
430 | sub cf::player::exists($) { |
391 | cf::player::find $_[0] |
431 | cf::player::find $_[0] |
392 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
432 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
393 | } |
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 |
394 | |
442 | |
395 | # rough implementation of a future "reply" method that works |
443 | # rough implementation of a future "reply" method that works |
396 | # with dialog boxes. |
444 | # with dialog boxes. |
397 | sub cf::object::player::reply($$$;$) { |
445 | sub cf::object::player::reply($$$;$) { |
398 | my ($self, $npc, $msg, $flags) = @_; |
446 | my ($self, $npc, $msg, $flags) = @_; |
… | |
… | |
403 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
451 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
404 | } else { |
452 | } else { |
405 | $msg = $npc->name . " says: $msg" if $npc; |
453 | $msg = $npc->name . " says: $msg" if $npc; |
406 | $self->message ($msg, $flags); |
454 | $self->message ($msg, $flags); |
407 | } |
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); |
408 | } |
470 | } |
409 | |
471 | |
410 | ############################################################################# |
472 | ############################################################################# |
411 | # map scripting support |
473 | # map scripting support |
412 | |
474 | |