Skip to content

Commit

Permalink
Replaced hand-escaped JSON (#5508)
Browse files Browse the repository at this point in the history
  • Loading branch information
Griezn committed Jun 8, 2023
1 parent 5da44fa commit 4fe144d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -27,6 +27,7 @@ FakeTruth (founder)
feyokorenhof
Gareth Nelson
GefaketHD
Griezn (Seppe Degryse)
HaoTNN
havel06 (Michal Havlíček)
hle0
Expand Down
12 changes: 12 additions & 0 deletions src/JsonUtils.cpp
Expand Up @@ -40,4 +40,16 @@ bool ParseString(const AString & a_JsonStr, Json::Value & a_Root, AString * a_Er
return Reader->parse(Doc, Doc + a_JsonStr.size(), &a_Root, a_ErrorMsg);
}





AString SerializeSingleValueJsonObject(
const AString & a_Key, const AString & a_Value)
{
Json::Value root;
root[a_Key] = a_Value;
return JsonUtils::WriteFastString(root);
}

} // namespace JsonUtils
3 changes: 3 additions & 0 deletions src/JsonUtils.h
Expand Up @@ -14,4 +14,7 @@ AString WriteStyledString(const Json::Value & a_Root);

bool ParseString(const AString & a_JsonStr, Json::Value & a_Root, AString * a_ErrorMsg = nullptr);

/** Creates a Json string representing an object with the specified single value. */
extern AString SerializeSingleValueJsonObject(const AString & a_Key, const AString & a_Value);

}
2 changes: 1 addition & 1 deletion src/Protocol/ProtocolRecognizer.cpp
Expand Up @@ -229,7 +229,7 @@ void cMultiVersionProtocol::SendDisconnect(cClientHandle & a_Client, const AStri
return;
}

const AString Message = fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason));
const AString Message = JsonUtils::SerializeSingleValueJsonObject("text", a_Reason);
const auto PacketID = GetPacketID(cProtocol::ePacketType::pktDisconnectDuringLogin);
cByteBuffer Out(
cByteBuffer::GetVarIntSize(PacketID) +
Expand Down
3 changes: 2 additions & 1 deletion src/Protocol/Protocol_1_14.cpp
Expand Up @@ -9,6 +9,7 @@ Implements the 1.14 protocol classes:
#include "Globals.h"
#include "Protocol_1_14.h"
#include "Packetizer.h"
#include "JsonUtils.h"
#include "../Root.h"
#include "../Server.h"
#include "../World.h"
Expand Down Expand Up @@ -440,7 +441,7 @@ void cProtocol_1_14::SendWindowOpen(const cWindow & a_Window)
}
}

Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_Window.GetWindowTitle()));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", a_Window.GetWindowTitle()));
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/Protocol/Protocol_1_8.cpp
Expand Up @@ -313,7 +313,7 @@ void cProtocol_1_8_0::SendChat(const AString & a_Message, eChatType a_Type)
{
ASSERT(m_State == 3); // In game mode?

SendChatRaw(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Message)), a_Type);
SendChatRaw(JsonUtils::SerializeSingleValueJsonObject("text", a_Message), a_Type);
}


Expand Down Expand Up @@ -433,13 +433,13 @@ void cProtocol_1_8_0::SendDisconnect(const AString & a_Reason)
case State::Login:
{
cPacketizer Pkt(*this, pktDisconnectDuringLogin);
Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason)));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", a_Reason));
break;
}
case State::Game:
{
cPacketizer Pkt(*this, pktDisconnectDuringGame);
Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason)));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", a_Reason));
break;
}
default:
Expand Down Expand Up @@ -1103,7 +1103,7 @@ void cProtocol_1_8_0::SendPlayerListUpdateDisplayName(const cPlayer & a_Player,
else
{
Pkt.WriteBool(true);
Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_CustomName));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", a_CustomName));
}
}

Expand Down Expand Up @@ -1659,9 +1659,7 @@ void cProtocol_1_8_0::SendUpdateSign(Vector3i a_BlockPos, const AString & a_Line
AString Lines[] = { a_Line1, a_Line2, a_Line3, a_Line4 };
for (size_t i = 0; i < ARRAYCOUNT(Lines); i++)
{
Json::Value RootValue;
RootValue["text"] = Lines[i];
Pkt.WriteString(JsonUtils::WriteFastString(RootValue));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", Lines[i]));
}
}

Expand Down Expand Up @@ -1750,7 +1748,7 @@ void cProtocol_1_8_0::SendWindowOpen(const cWindow & a_Window)
cPacketizer Pkt(*this, pktWindowOpen);
Pkt.WriteBEUInt8(static_cast<UInt8>(a_Window.GetWindowID()));
Pkt.WriteString(a_Window.GetWindowTypeName());
Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_Window.GetWindowTitle()));
Pkt.WriteString(JsonUtils::SerializeSingleValueJsonObject("text", a_Window.GetWindowTitle()));

switch (a_Window.GetWindowType())
{
Expand Down Expand Up @@ -3138,7 +3136,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cFastNBTWriter & a_Writer, const cBlockEn
a_Writer.AddString("CustomName", "@");
if (!CommandBlockEntity.GetLastOutput().empty())
{
a_Writer.AddString("LastOutput", fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), CommandBlockEntity.GetLastOutput()));
a_Writer.AddString("LastOutput", JsonUtils::SerializeSingleValueJsonObject("text", CommandBlockEntity.GetLastOutput()));
}
break;
}
Expand Down

0 comments on commit 4fe144d

Please sign in to comment.