ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.27 by root, Fri Jul 21 00:51:39 2006 UTC vs.
Revision 1.33 by root, Mon Aug 21 01:51:23 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use 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
13use strict; 14use 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.
75for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 76for 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
181sub unload_extension { 182sub 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
216sub load_extensions { 220sub 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
296use JSON::Syck (); # TODO# replace by JSON::PC once working
297
298sub from_json($) {
299 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
300 JSON::Syck::Load $_[0]
301}
302
303sub 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
293sub on_extcmd { 312sub 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
390sub 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
416my $delta_timer = Event->timer (
417 parked => 1,
418 prio => Event::PRIO_HIGH,
419 cb => sub { Event::unloop (undef) },
420);
421
422sub 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
386Returns true when the given account exists. 430Returns true when the given account exists.
387 431
389 433
390sub cf::player::exists($) { 434sub 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
441Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
442can be C<undef>. Does the right thing when the player is currently in a
443dialogue 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.
397sub cf::object::player::reply($$$;$) { 449sub 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
464Sends an ext reply to the player.
465
466=cut
467
468sub 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines