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.287 by root, Mon Jun 25 05:43:45 2007 UTC vs.
Revision 1.292 by root, Tue Jul 3 00:54:57 2007 UTC

20use Coro::Signal; 20use Coro::Signal;
21use Coro::Semaphore; 21use Coro::Semaphore;
22use Coro::AIO; 22use Coro::AIO;
23use Coro::Storable; 23use Coro::Storable;
24 24
25use JSON::XS 1.4 ();
25use BDB (); 26use BDB ();
26use Data::Dumper; 27use Data::Dumper;
27use Digest::MD5; 28use Digest::MD5;
28use Fcntl; 29use Fcntl;
29use YAML::Syck (); 30use YAML::Syck ();
237 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 238 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
238 $d 239 $d
239 } || "[unable to dump $_[0]: '$@']"; 240 } || "[unable to dump $_[0]: '$@']";
240} 241}
241 242
242use JSON::XS ();
243
244=item $ref = cf::from_json $json 243=item $ref = cf::from_json $json
245 244
246Converts a JSON string into the corresponding perl data structure. 245Converts a JSON string into the corresponding perl data structure.
247 246
248=item $json = cf::to_json $ref 247=item $json = cf::to_json $ref
249 248
250Converts a perl data structure into its JSON representation. 249Converts a perl data structure into its JSON representation.
251 250
252=cut 251=cut
253 252
254our $json_coder = JSON::XS->new->convert_blessed->utf8; 253our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
255 254
256sub to_json ($) { $json_coder->encode ($_[0]) } 255sub to_json ($) { $json_coder->encode ($_[0]) }
257sub from_json ($) { $json_coder->decode ($_[0]) } 256sub from_json ($) { $json_coder->decode ($_[0]) }
258 257
259=item cf::lock_wait $string 258=item cf::lock_wait $string
1087 cf::override; 1086 cf::override;
1088 }, 1087 },
1089 on_extcmd => sub { 1088 on_extcmd => sub {
1090 my ($pl, $buf) = @_; 1089 my ($pl, $buf) = @_;
1091 1090
1092 my $msg = eval { from_json $buf }; 1091 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1093 1092
1094 if (ref $msg) { 1093 if (ref $msg) {
1095 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
1096 if (my %reply = $cb->($pl, $msg)) { 1095 if (my %reply = $cb->($pl, $msg)) {
1097 $pl->ext_reply ($msg->{msgid}, %reply); 1096 $pl->ext_reply ($msg->{msgid}, %reply);
1401 # replace G<male|female> tags 1400 # replace G<male|female> tags
1402 || s{G<([^>|]*)\|([^>]*)>}{ 1401 || s{G<([^>|]*)\|([^>]*)>}{
1403 $self->gender ? $2 : $1 1402 $self->gender ? $2 : $1
1404 }ge 1403 }ge
1405 # replace H<hint text> 1404 # replace H<hint text>
1406 || s/H<([^\>]*)>/<fg name="lightblue">[$1]<\/fg>/g; 1405 || s{H<([^\>]*)>}
1406 {
1407 ("<fg name=\"lightblue\">[$1 (Use hintmode to suppress hints)]</fg>",
1408 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1409 "")
1410 [$self->{hintmode}]
1411 }ge;
1407 1412
1408 # create single paragraphs (very hackish) 1413 # create single paragraphs (very hackish)
1409 s/(?<=\S)\n(?=\w)/ /g; 1414 s/(?<=\S)\n(?=\w)/ /g;
1410 1415
1416 # compress some whitespace
1417 1 while s/\s*\n\s*\n\s*/\n/;
1418
1411 $_ 1419 $_
1420}
1421
1422sub hintmode {
1423 $_[0]{hintmode} = $_[1] if @_ > 1;
1424 $_[0]{hintmode}
1412} 1425}
1413 1426
1414=item $player->ext_reply ($msgid, %msg) 1427=item $player->ext_reply ($msgid, %msg)
1415 1428
1416Sends an ext reply to the player. 1429Sends an ext reply to the player.
1419 1432
1420sub ext_reply($$%) { 1433sub ext_reply($$%) {
1421 my ($self, $id, %msg) = @_; 1434 my ($self, $id, %msg) = @_;
1422 1435
1423 $msg{msgid} = $id; 1436 $msg{msgid} = $id;
1424 $self->send ("ext " . cf::to_json \%msg); 1437 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1425} 1438}
1426 1439
1427=item $player->ext_event ($type, %msg) 1440=item $player->ext_event ($type, %msg)
1428 1441
1429Sends an ext event to the client. 1442Sends an ext event to the client.
2392 my ($self, $color, $type, $msg, @extra) = @_; 2405 my ($self, $color, $type, $msg, @extra) = @_;
2393 2406
2394 $msg = $self->pl->expand_cfpod ($msg); 2407 $msg = $self->pl->expand_cfpod ($msg);
2395 2408
2396 if ($self->can_msg) { 2409 if ($self->can_msg) {
2397 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2410 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2398 } else { 2411 } else {
2399 # replace some tags by gcfclient-compatible ones 2412 # replace some tags by gcfclient-compatible ones
2400 for ($msg) { 2413 for ($msg) {
2401 1 while 2414 1 while
2402 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2415 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2425 2438
2426sub cf::client::ext_event($$%) { 2439sub cf::client::ext_event($$%) {
2427 my ($self, $type, %msg) = @_; 2440 my ($self, $type, %msg) = @_;
2428 2441
2429 $msg{msgtype} = "event_$type"; 2442 $msg{msgtype} = "event_$type";
2430 $self->send_packet ("ext " . cf::to_json \%msg); 2443 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2431} 2444}
2432 2445
2433=item $success = $client->query ($flags, "text", \&cb) 2446=item $success = $client->query ($flags, "text", \&cb)
2434 2447
2435Queues a query to the client, calling the given callback with 2448Queues a query to the client, calling the given callback with
2458 2471
2459 1 2472 1
2460} 2473}
2461 2474
2462cf::client->attach ( 2475cf::client->attach (
2476 on_connect => sub {
2477 my ($ns) = @_;
2478
2479 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2480 },
2463 on_reply => sub { 2481 on_reply => sub {
2464 my ($ns, $msg) = @_; 2482 my ($ns, $msg) = @_;
2465 2483
2466 # this weird shuffling is so that direct followup queries 2484 # this weird shuffling is so that direct followup queries
2467 # get handled first 2485 # get handled first
2482 } 2500 }
2483 }, 2501 },
2484 on_exticmd => sub { 2502 on_exticmd => sub {
2485 my ($ns, $buf) = @_; 2503 my ($ns, $buf) = @_;
2486 2504
2487 my $msg = eval { from_json $buf }; 2505 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2488 2506
2489 if (ref $msg) { 2507 if (ref $msg) {
2490 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2508 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2491 if (my %reply = $cb->($ns, $msg)) { 2509 if (my %reply = $cb->($ns, $msg)) {
2492 $reply{msgid} = $msg->{msgid}; 2510 $reply{msgid} = $msg->{msgid};
2493 $ns->send ("ext " . cf::to_json \%reply); 2511 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2494 } 2512 }
2495 } 2513 }
2496 } else { 2514 } else {
2497 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2515 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2498 } 2516 }
2709 load_facedata "$DATADIR/facedata" 2727 load_facedata "$DATADIR/facedata"
2710 or die "unable to load facedata\n"; 2728 or die "unable to load facedata\n";
2711} 2729}
2712 2730
2713sub reload_archetypes { 2731sub reload_archetypes {
2732 load_resource_file "$DATADIR/archetypes"
2733 or die "unable to load archetypes\n";
2734 #d# NEED to laod twice to resolve forward references
2735 # this really needs to be done in an extra post-pass
2736 # (which needs to be synchronous, so solve it differently)
2714 load_resource_file "$DATADIR/archetypes" 2737 load_resource_file "$DATADIR/archetypes"
2715 or die "unable to load archetypes\n"; 2738 or die "unable to load archetypes\n";
2716} 2739}
2717 2740
2718sub reload_treasures { 2741sub reload_treasures {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines