From 4fe144de352434ff6e98a4269777b82e8b03f4d0 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Thu, 8 Jun 2023 08:13:08 +0200 Subject: Replaced hand-escaped JSON (#5508) --- src/JsonUtils.cpp | 12 ++++++++++++ src/JsonUtils.h | 3 +++ src/Protocol/ProtocolRecognizer.cpp | 2 +- src/Protocol/Protocol_1_14.cpp | 3 ++- src/Protocol/Protocol_1_8.cpp | 16 +++++++--------- 5 files changed, 25 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/JsonUtils.cpp b/src/JsonUtils.cpp index 6c70e70d3..2c6c8efbc 100644 --- a/src/JsonUtils.cpp +++ b/src/JsonUtils.cpp @@ -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 diff --git a/src/JsonUtils.h b/src/JsonUtils.h index d315dbed8..fc35e9f0a 100644 --- a/src/JsonUtils.h +++ b/src/JsonUtils.h @@ -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); + } diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 8bfc18330..fa106342f 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -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) + diff --git a/src/Protocol/Protocol_1_14.cpp b/src/Protocol/Protocol_1_14.cpp index 09ae54cce..31393120e 100644 --- a/src/Protocol/Protocol_1_14.cpp +++ b/src/Protocol/Protocol_1_14.cpp @@ -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" @@ -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())); } } diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index 2e4e900fc..d7aa6a4e2 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -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); } @@ -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: @@ -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)); } } @@ -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])); } } @@ -1750,7 +1748,7 @@ void cProtocol_1_8_0::SendWindowOpen(const cWindow & a_Window) cPacketizer Pkt(*this, pktWindowOpen); Pkt.WriteBEUInt8(static_cast(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()) { @@ -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; } -- cgit v1.2.3