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.289 by root, Sat Jun 30 03:00:54 2007 UTC vs.
Revision 1.298 by root, Sun Jul 8 14:50:07 2007 UTC

248 248
249Converts a perl data structure into its JSON representation. 249Converts a perl data structure into its JSON representation.
250 250
251=cut 251=cut
252 252
253our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max 253our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
254 254
255sub to_json ($) { $json_coder->encode ($_[0]) } 255sub to_json ($) { $json_coder->encode ($_[0]) }
256sub from_json ($) { $json_coder->decode ($_[0]) } 256sub from_json ($) { $json_coder->decode ($_[0]) }
257 257
258=item cf::lock_wait $string 258=item cf::lock_wait $string
421 421
422 if (my $pid = fork) { 422 if (my $pid = fork) {
423 close $fh2; 423 close $fh2;
424 424
425 my $res = (Coro::Handle::unblock $fh1)->readline (undef); 425 my $res = (Coro::Handle::unblock $fh1)->readline (undef);
426 warn "pst<$res>" unless $res =~ /^pst/;
426 $res = Coro::Storable::thaw $res; 427 $res = Coro::Storable::thaw $res;
427 428
428 waitpid $pid, 0; # should not block anymore, we expect the child to simply behave 429 waitpid $pid, 0; # should not block anymore, we expect the child to simply behave
429 430
430 die $$res unless "ARRAY" eq ref $res; 431 Carp::confess $$res unless "ARRAY" eq ref $res;
431 432
432 return wantarray ? @$res : $res->[-1]; 433 return wantarray ? @$res : $res->[-1];
433 } else { 434 } else {
434 reset_signals; 435 reset_signals;
435 local $SIG{__WARN__}; 436 local $SIG{__WARN__};
436 local $SIG{__DIE__}; 437 local $SIG{__DIE__};
438 local $Coro::idle;
439 $Coro::current->prio (Coro::PRIO_MAX);
437 eval { 440 eval {
438 close $fh1; 441 close $fh1;
439 442
440 my @res = eval { $cb->(@args) }; 443 my @res = eval { $cb->(@args) };
441 syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); 444
445 open my $fh, ">", \my $buf
446 or die "fork_call: cannot open fh-to-buf in child : $!";
447 Storable::store_fd +($@ ? \"$@" : \@res), $fh;
448 close $fh;
449
450 warn "writing ", length $buf;
451 my $x;
452 (length $buf) == ($x = syswrite $fh2, $buf)
453 or warn "error writing ".(length $buf)." != $x\n";
454 close $fh2;
442 }; 455 };
443 456
444 warn $@ if $@; 457 warn $@ if $@;
445 _exit 0; 458 _exit 0;
446 } 459 }
1086 cf::override; 1099 cf::override;
1087 }, 1100 },
1088 on_extcmd => sub { 1101 on_extcmd => sub {
1089 my ($pl, $buf) = @_; 1102 my ($pl, $buf) = @_;
1090 1103
1091 my $msg = eval { from_json $buf }; 1104 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1092 1105
1093 if (ref $msg) { 1106 if (ref $msg) {
1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1107 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
1095 if (my %reply = $cb->($pl, $msg)) { 1108 if (my %reply = $cb->($pl, $msg)) {
1096 $pl->ext_reply ($msg->{msgid}, %reply); 1109 $pl->ext_reply ($msg->{msgid}, %reply);
1400 # replace G<male|female> tags 1413 # replace G<male|female> tags
1401 || s{G<([^>|]*)\|([^>]*)>}{ 1414 || s{G<([^>|]*)\|([^>]*)>}{
1402 $self->gender ? $2 : $1 1415 $self->gender ? $2 : $1
1403 }ge 1416 }ge
1404 # replace H<hint text> 1417 # replace H<hint text>
1405 || s/H<([^\>]*)>/<fg name="lightblue">[$1]<\/fg>/g; 1418 || s{H<([^\>]*)>}
1419 {
1420 ("<fg name=\"lightblue\">[$1 (Use hintmode to suppress hints)]</fg>",
1421 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1422 "")
1423 [$self->{hintmode}]
1424 }ge;
1406 1425
1407 # create single paragraphs (very hackish) 1426 # create single paragraphs (very hackish)
1408 s/(?<=\S)\n(?=\w)/ /g; 1427 s/(?<=\S)\n(?=\w)/ /g;
1409 1428
1429 # compress some whitespace
1430 s/\s+\n/\n/g; # ws line-ends
1431 s/\n\n+/\n/g; # double lines
1432 s/^\n+//; # beginning lines
1433 s/\n+$//; # ending lines
1434
1410 $_ 1435 $_
1436}
1437
1438sub hintmode {
1439 $_[0]{hintmode} = $_[1] if @_ > 1;
1440 $_[0]{hintmode}
1411} 1441}
1412 1442
1413=item $player->ext_reply ($msgid, %msg) 1443=item $player->ext_reply ($msgid, %msg)
1414 1444
1415Sends an ext reply to the player. 1445Sends an ext reply to the player.
1418 1448
1419sub ext_reply($$%) { 1449sub ext_reply($$%) {
1420 my ($self, $id, %msg) = @_; 1450 my ($self, $id, %msg) = @_;
1421 1451
1422 $msg{msgid} = $id; 1452 $msg{msgid} = $id;
1423 $self->send ("ext " . cf::to_json \%msg); 1453 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1424} 1454}
1425 1455
1426=item $player->ext_event ($type, %msg) 1456=item $player->ext_event ($type, %msg)
1427 1457
1428Sends an ext event to the client. 1458Sends an ext event to the client.
2340 my $hp = $exit->stats->hp; 2370 my $hp = $exit->stats->hp;
2341 my $sp = $exit->stats->sp; 2371 my $sp = $exit->stats->sp;
2342 2372
2343 $self->enter_link; 2373 $self->enter_link;
2344 2374
2375 # if exit is damned, update players death & WoR home-position
2376 $self->contr->savebed ($slaying, $hp, $sp)
2377 if $exit->flag (FLAG_DAMNED);
2378
2345 (async { 2379 (async {
2346 $self->deactivate_recursive; # just to be sure 2380 $self->deactivate_recursive; # just to be sure
2347 unless (eval { 2381 unless (eval {
2348 $self->goto ($slaying, $hp, $sp); 2382 $self->goto ($slaying, $hp, $sp);
2349 2383
2390sub cf::client::send_msg { 2424sub cf::client::send_msg {
2391 my ($self, $color, $type, $msg, @extra) = @_; 2425 my ($self, $color, $type, $msg, @extra) = @_;
2392 2426
2393 $msg = $self->pl->expand_cfpod ($msg); 2427 $msg = $self->pl->expand_cfpod ($msg);
2394 2428
2429 return unless @extra || length $msg;
2430
2395 if ($self->can_msg) { 2431 if ($self->can_msg) {
2396 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2432 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2397 } else { 2433 } else {
2398 # replace some tags by gcfclient-compatible ones 2434 # replace some tags by gcfclient-compatible ones
2399 for ($msg) { 2435 for ($msg) {
2400 1 while 2436 1 while
2401 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2437 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2424 2460
2425sub cf::client::ext_event($$%) { 2461sub cf::client::ext_event($$%) {
2426 my ($self, $type, %msg) = @_; 2462 my ($self, $type, %msg) = @_;
2427 2463
2428 $msg{msgtype} = "event_$type"; 2464 $msg{msgtype} = "event_$type";
2429 $self->send_packet ("ext " . cf::to_json \%msg); 2465 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2430} 2466}
2431 2467
2432=item $success = $client->query ($flags, "text", \&cb) 2468=item $success = $client->query ($flags, "text", \&cb)
2433 2469
2434Queues a query to the client, calling the given callback with 2470Queues a query to the client, calling the given callback with
2457 2493
2458 1 2494 1
2459} 2495}
2460 2496
2461cf::client->attach ( 2497cf::client->attach (
2498 on_connect => sub {
2499 my ($ns) = @_;
2500
2501 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2502 },
2462 on_reply => sub { 2503 on_reply => sub {
2463 my ($ns, $msg) = @_; 2504 my ($ns, $msg) = @_;
2464 2505
2465 # this weird shuffling is so that direct followup queries 2506 # this weird shuffling is so that direct followup queries
2466 # get handled first 2507 # get handled first
2481 } 2522 }
2482 }, 2523 },
2483 on_exticmd => sub { 2524 on_exticmd => sub {
2484 my ($ns, $buf) = @_; 2525 my ($ns, $buf) = @_;
2485 2526
2486 my $msg = eval { from_json $buf }; 2527 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2487 2528
2488 if (ref $msg) { 2529 if (ref $msg) {
2489 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2530 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2490 if (my %reply = $cb->($ns, $msg)) { 2531 if (my %reply = $cb->($ns, $msg)) {
2491 $reply{msgid} = $msg->{msgid}; 2532 $reply{msgid} = $msg->{msgid};
2492 $ns->send ("ext " . cf::to_json \%reply); 2533 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2493 } 2534 }
2494 } 2535 }
2495 } else { 2536 } else {
2496 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2537 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2497 } 2538 }
2554 2595
2555=pod 2596=pod
2556 2597
2557The following functions and methods are available within a safe environment: 2598The following functions and methods are available within a safe environment:
2558 2599
2559 cf::object contr pay_amount pay_player map 2600 cf::object
2601 contr pay_amount pay_player map x y force_find force_add
2602 insert remove
2603
2560 cf::object::player player 2604 cf::object::player
2561 cf::player peaceful 2605 player
2562 cf::map trigger 2606
2607 cf::player
2608 peaceful
2609
2610 cf::map
2611 trigger
2563 2612
2564=cut 2613=cut
2565 2614
2566for ( 2615for (
2567 ["cf::object" => qw(contr pay_amount pay_player map)], 2616 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2617 insert remove)],
2568 ["cf::object::player" => qw(player)], 2618 ["cf::object::player" => qw(player)],
2569 ["cf::player" => qw(peaceful)], 2619 ["cf::player" => qw(peaceful)],
2570 ["cf::map" => qw(trigger)], 2620 ["cf::map" => qw(trigger)],
2571) { 2621) {
2572 no strict 'refs'; 2622 no strict 'refs';

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines