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.295 by root, Wed Jul 4 05:08:15 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 s/\s+\n/\n/g; # ws line-ends
1418 s/\n\n+/\n/g; # double lines
1419 s/^\n+//; # beginning lines
1420 s/\n+$//; # ending lines
1421
1411 $_ 1422 $_
1423}
1424
1425sub hintmode {
1426 $_[0]{hintmode} = $_[1] if @_ > 1;
1427 $_[0]{hintmode}
1412} 1428}
1413 1429
1414=item $player->ext_reply ($msgid, %msg) 1430=item $player->ext_reply ($msgid, %msg)
1415 1431
1416Sends an ext reply to the player. 1432Sends an ext reply to the player.
1419 1435
1420sub ext_reply($$%) { 1436sub ext_reply($$%) {
1421 my ($self, $id, %msg) = @_; 1437 my ($self, $id, %msg) = @_;
1422 1438
1423 $msg{msgid} = $id; 1439 $msg{msgid} = $id;
1424 $self->send ("ext " . cf::to_json \%msg); 1440 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1425} 1441}
1426 1442
1427=item $player->ext_event ($type, %msg) 1443=item $player->ext_event ($type, %msg)
1428 1444
1429Sends an ext event to the client. 1445Sends an ext event to the client.
2391sub cf::client::send_msg { 2407sub cf::client::send_msg {
2392 my ($self, $color, $type, $msg, @extra) = @_; 2408 my ($self, $color, $type, $msg, @extra) = @_;
2393 2409
2394 $msg = $self->pl->expand_cfpod ($msg); 2410 $msg = $self->pl->expand_cfpod ($msg);
2395 2411
2412 return unless @extra || length $msg;
2413
2396 if ($self->can_msg) { 2414 if ($self->can_msg) {
2397 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2415 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2398 } else { 2416 } else {
2399 # replace some tags by gcfclient-compatible ones 2417 # replace some tags by gcfclient-compatible ones
2400 for ($msg) { 2418 for ($msg) {
2401 1 while 2419 1 while
2402 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2420 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2425 2443
2426sub cf::client::ext_event($$%) { 2444sub cf::client::ext_event($$%) {
2427 my ($self, $type, %msg) = @_; 2445 my ($self, $type, %msg) = @_;
2428 2446
2429 $msg{msgtype} = "event_$type"; 2447 $msg{msgtype} = "event_$type";
2430 $self->send_packet ("ext " . cf::to_json \%msg); 2448 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2431} 2449}
2432 2450
2433=item $success = $client->query ($flags, "text", \&cb) 2451=item $success = $client->query ($flags, "text", \&cb)
2434 2452
2435Queues a query to the client, calling the given callback with 2453Queues a query to the client, calling the given callback with
2458 2476
2459 1 2477 1
2460} 2478}
2461 2479
2462cf::client->attach ( 2480cf::client->attach (
2481 on_connect => sub {
2482 my ($ns) = @_;
2483
2484 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2485 },
2463 on_reply => sub { 2486 on_reply => sub {
2464 my ($ns, $msg) = @_; 2487 my ($ns, $msg) = @_;
2465 2488
2466 # this weird shuffling is so that direct followup queries 2489 # this weird shuffling is so that direct followup queries
2467 # get handled first 2490 # get handled first
2482 } 2505 }
2483 }, 2506 },
2484 on_exticmd => sub { 2507 on_exticmd => sub {
2485 my ($ns, $buf) = @_; 2508 my ($ns, $buf) = @_;
2486 2509
2487 my $msg = eval { from_json $buf }; 2510 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2488 2511
2489 if (ref $msg) { 2512 if (ref $msg) {
2490 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2513 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2491 if (my %reply = $cb->($ns, $msg)) { 2514 if (my %reply = $cb->($ns, $msg)) {
2492 $reply{msgid} = $msg->{msgid}; 2515 $reply{msgid} = $msg->{msgid};
2493 $ns->send ("ext " . cf::to_json \%reply); 2516 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2494 } 2517 }
2495 } 2518 }
2496 } else { 2519 } else {
2497 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2520 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2498 } 2521 }
2709 load_facedata "$DATADIR/facedata" 2732 load_facedata "$DATADIR/facedata"
2710 or die "unable to load facedata\n"; 2733 or die "unable to load facedata\n";
2711} 2734}
2712 2735
2713sub reload_archetypes { 2736sub reload_archetypes {
2737 load_resource_file "$DATADIR/archetypes"
2738 or die "unable to load archetypes\n";
2739 #d# NEED to laod twice to resolve forward references
2740 # this really needs to be done in an extra post-pass
2741 # (which needs to be synchronous, so solve it differently)
2714 load_resource_file "$DATADIR/archetypes" 2742 load_resource_file "$DATADIR/archetypes"
2715 or die "unable to load archetypes\n"; 2743 or die "unable to load archetypes\n";
2716} 2744}
2717 2745
2718sub reload_treasures { 2746sub reload_treasures {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines