--- deliantra/server/lib/cf.pm 2007/06/25 07:40:53 1.288 +++ deliantra/server/lib/cf.pm 2007/07/03 06:00:48 1.294 @@ -250,7 +250,7 @@ =cut -our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max +our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max sub to_json ($) { $json_coder->encode ($_[0]) } sub from_json ($) { $json_coder->decode ($_[0]) } @@ -1088,7 +1088,7 @@ on_extcmd => sub { my ($pl, $buf) = @_; - my $msg = eval { from_json $buf }; + my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; if (ref $msg) { if (my $cb = $EXTCMD{$msg->{msgtype}}) { @@ -1402,14 +1402,31 @@ $self->gender ? $2 : $1 }ge # replace H - || s/H<([^\>]*)>/[$1]<\/fg>/g; + || s{H<([^\>]*)>} + { + ("[$1 (Use hintmode to suppress hints)]", + "[Hint suppressed, see hintmode]", + "") + [$self->{hintmode}] + }ge; # create single paragraphs (very hackish) s/(?<=\S)\n(?=\w)/ /g; + # compress some whitespace + 1 while s/\s*\n\s*\n\s*/\n/; + + s/^\s+//; + s/\s+$//; + $_ } +sub hintmode { + $_[0]{hintmode} = $_[1] if @_ > 1; + $_[0]{hintmode} +} + =item $player->ext_reply ($msgid, %msg) Sends an ext reply to the player. @@ -1420,7 +1437,7 @@ my ($self, $id, %msg) = @_; $msg{msgid} = $id; - $self->send ("ext " . cf::to_json \%msg); + $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg)); } =item $player->ext_event ($type, %msg) @@ -2392,8 +2409,10 @@ $msg = $self->pl->expand_cfpod ($msg); + return unless @extra || length $msg; + if ($self->can_msg) { - $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); + $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); } else { # replace some tags by gcfclient-compatible ones for ($msg) { @@ -2426,7 +2445,7 @@ my ($self, $type, %msg) = @_; $msg{msgtype} = "event_$type"; - $self->send_packet ("ext " . cf::to_json \%msg); + $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); } =item $success = $client->query ($flags, "text", \&cb) @@ -2459,6 +2478,11 @@ } cf::client->attach ( + on_connect => sub { + my ($ns) = @_; + + $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed; + }, on_reply => sub { my ($ns, $msg) = @_; @@ -2483,13 +2507,13 @@ on_exticmd => sub { my ($ns, $buf) = @_; - my $msg = eval { from_json $buf }; + my $msg = eval { $ns->{json_coder}->decode ($buf) }; if (ref $msg) { if (my $cb = $EXTICMD{$msg->{msgtype}}) { if (my %reply = $cb->($ns, $msg)) { $reply{msgid} = $msg->{msgid}; - $ns->send ("ext " . cf::to_json \%reply); + $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); } } } else { @@ -2712,6 +2736,11 @@ sub reload_archetypes { load_resource_file "$DATADIR/archetypes" or die "unable to load archetypes\n"; + #d# NEED to laod twice to resolve forward references + # this really needs to be done in an extra post-pass + # (which needs to be synchronous, so solve it differently) + load_resource_file "$DATADIR/archetypes" + or die "unable to load archetypes\n"; } sub reload_treasures {