From c9522fb740200ccef6230cec452c48efb31e5394 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 11 May 2023 22:05:17 +0200 Subject: Removed all Printf-family functions from StringUtils. Replaced them with fmt::format calls, including changes to the format strings. Also changed the format strings to use FMT_STRING, so that the format is checked compile-time against the arguments. Also fixed code-style violations already present in the code. --- src/Protocol/ForgeHandshake.cpp | 22 +++++++------- src/Protocol/MojangAPI.cpp | 2 +- src/Protocol/Packetizer.cpp | 2 +- src/Protocol/ProtocolRecognizer.cpp | 11 +++---- src/Protocol/Protocol_1_14.cpp | 2 +- src/Protocol/Protocol_1_8.cpp | 58 ++++++++++++++++++++----------------- 6 files changed, 52 insertions(+), 45 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/ForgeHandshake.cpp b/src/Protocol/ForgeHandshake.cpp index 34e044fbc..bf58acf9e 100644 --- a/src/Protocol/ForgeHandshake.cpp +++ b/src/Protocol/ForgeHandshake.cpp @@ -142,7 +142,7 @@ AStringMap cForgeHandshake::ParseModList(const ContiguousByteBufferView a_Data) if (a_Data.size() < 4) { - SetError(Printf("ParseModList invalid packet, missing length (size = %zu)", a_Data.size())); + SetError(fmt::format(FMT_STRING("ParseModList invalid packet, missing length (size = {})"), a_Data.size())); return Mods; } @@ -160,12 +160,12 @@ AStringMap cForgeHandshake::ParseModList(const ContiguousByteBufferView a_Data) AString Name, Version; if (!Buf.ReadVarUTF8String(Name)) { - SetError(Printf("ParseModList failed to read mod name at i = %d", i)); + SetError(fmt::format(FMT_STRING("ParseModList failed to read mod name at i = {}"), i)); break; } if (!Buf.ReadVarUTF8String(Version)) { - SetError(Printf("ParseModList failed to read mod version at i = %d", i)); + SetError(fmt::format(FMT_STRING("ParseModList failed to read mod version at i = {}"), i)); break; } Mods.insert({Name, Version}); @@ -186,12 +186,12 @@ void cForgeHandshake::HandleClientHello(cClientHandle & a_Client, const Contiguo LOGD("Received ClientHello with FML protocol version %d", FmlProtocolVersion); if (FmlProtocolVersion != 2) { - SetError(Printf("Unsupported FML client protocol version received in ClientHello: %d", FmlProtocolVersion)); + SetError(fmt::format(FMT_STRING("Unsupported FML client protocol version received in ClientHello: {}"), FmlProtocolVersion)); } } else { - SetError(Printf("Received unexpected length of ClientHello: %zu", a_Data.size())); + SetError(fmt::format(FMT_STRING("Received unexpected length of ClientHello: {}"), a_Data.size())); } } @@ -207,10 +207,10 @@ void cForgeHandshake::HandleModList(cClientHandle & a_Client, const ContiguousBy AString ClientModsString; for (auto & item: ClientMods) { - AppendPrintf(ClientModsString, "%s@%s, ", item.first.c_str(), item.second.c_str()); + ClientModsString.append(fmt::format(FMT_STRING("{}@{}, "), item.first, item.second)); } - LOG("Client connected with %zu mods: %s", ClientMods.size(), ClientModsString.c_str()); + LOG("Client connected with %zu mods: %s", ClientMods.size(), ClientModsString); a_Client.m_ForgeMods = ClientMods; @@ -252,7 +252,7 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle & a_Client, const Contigu { if (a_Data.size() != 2) { - SetError(Printf("Unexpected HandshakeAck packet length: %zu", a_Data.size())); + SetError(fmt::format(FMT_STRING("Unexpected HandshakeAck packet length: {}"), a_Data.size())); return; } @@ -331,7 +331,7 @@ void cForgeHandshake::DataReceived(cClientHandle & a_Client, const ContiguousByt { if (!IsForgeClient) { - SetError(Printf("Received unexpected Forge data from non-Forge client (%zu bytes)", a_Data.size())); + SetError(fmt::format(FMT_STRING("Received unexpected Forge data from non-Forge client ({} bytes)"), a_Data.size())); return; } if (m_Errored) @@ -342,7 +342,7 @@ void cForgeHandshake::DataReceived(cClientHandle & a_Client, const ContiguousByt if (a_Data.size() <= 1) { - SetError(Printf("Received unexpectedly short Forge data (%zu bytes)", a_Data.size())); + SetError(fmt::format(FMT_STRING("Received unexpectedly short Forge data ({} bytes)"), a_Data.size())); return; } @@ -355,7 +355,7 @@ void cForgeHandshake::DataReceived(cClientHandle & a_Client, const ContiguousByt default: { - SetError(Printf("Unexpected Forge packet %d received", Discriminator)); + SetError(fmt::format(FMT_STRING("Unexpected Forge packet {0} (0x{0:x}) received"), Discriminator)); return; } } diff --git a/src/Protocol/MojangAPI.cpp b/src/Protocol/MojangAPI.cpp index d1914f4d3..6cedf66d2 100644 --- a/src/Protocol/MojangAPI.cpp +++ b/src/Protocol/MojangAPI.cpp @@ -657,7 +657,7 @@ void cMojangAPI::QueryNamesToUUIDs(AStringVector & a_NamesToQuery) Request += "User-Agent: Cuberite\r\n"; Request += "Connection: close\r\n"; Request += "Content-Type: application/json\r\n"; - Request += Printf("Content-Length: %u\r\n", static_cast(RequestBody.length())); + Request += fmt::format(FMT_STRING("Content-Length: {}\r\n"), RequestBody.length()); Request += "\r\n"; Request += RequestBody; diff --git a/src/Protocol/Packetizer.cpp b/src/Protocol/Packetizer.cpp index e7c5e7b87..abc10d332 100644 --- a/src/Protocol/Packetizer.cpp +++ b/src/Protocol/Packetizer.cpp @@ -135,5 +135,5 @@ AString cPacketizer::PacketTypeToStr(cProtocol::ePacketType a_PacketType) case cProtocol::pktWindowOpen: return "pktWindowOpen"; case cProtocol::pktWindowProperty: return "pktWindowProperty"; } - return Printf("Unknown packet type: 0x%02x", a_PacketType); + return fmt::format(FMT_STRING("Unknown packet type: 0x{:02x}"), a_PacketType); } diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index ffe839dac..8bfc18330 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -73,7 +73,7 @@ AString cMultiVersionProtocol::GetVersionTextFromInt(cProtocol::Version a_Protoc } ASSERT(!"Unknown protocol version"); - return Printf("Unknown protocol (%d)", a_ProtocolVersion); + return fmt::format(FMT_STRING("Unknown protocol ({})"), a_ProtocolVersion); } @@ -229,7 +229,7 @@ void cMultiVersionProtocol::SendDisconnect(cClientHandle & a_Client, const AStri return; } - const AString Message = Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str()); + const AString Message = fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason)); const auto PacketID = GetPacketID(cProtocol::ePacketType::pktDisconnectDuringLogin); cByteBuffer Out( cByteBuffer::GetVarIntSize(PacketID) + @@ -348,13 +348,14 @@ std::unique_ptr cMultiVersionProtocol::TryRecognizeLengthedProtocol(c default: { LOGD("Client \"%s\" uses an unsupported protocol (lengthed, version %u (0x%x))", - a_Client.GetIPString().c_str(), ProtocolVersion, ProtocolVersion + a_Client.GetIPString(), ProtocolVersion, ProtocolVersion ); if (NextState != cProtocol::State::Status) { throw TriedToJoinWithUnsupportedProtocolException( - Printf("Unsupported protocol version %u.\nTry connecting with Minecraft " MCS_CLIENT_VERSIONS, ProtocolVersion) + fmt::format(FMT_STRING("Unsupported protocol version {}.\nTry connecting with Minecraft {}"), + ProtocolVersion, MCS_CLIENT_VERSIONS) ); } @@ -441,7 +442,7 @@ void cMultiVersionProtocol::HandlePacketStatusRequest(cClientHandle & a_Client) ResponseValue["description"] = Description; if (!Favicon.empty()) { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); + ResponseValue["favicon"] = "data:image/png;base64," + Favicon; } AString Response = JsonUtils::WriteFastString(ResponseValue); diff --git a/src/Protocol/Protocol_1_14.cpp b/src/Protocol/Protocol_1_14.cpp index 97be8175c..09ae54cce 100644 --- a/src/Protocol/Protocol_1_14.cpp +++ b/src/Protocol/Protocol_1_14.cpp @@ -440,7 +440,7 @@ void cProtocol_1_14::SendWindowOpen(const cWindow & a_Window) } } - Pkt.WriteString(Printf("{\"text\":\"%s\"}", a_Window.GetWindowTitle().c_str())); + Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_Window.GetWindowTitle())); } } diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index d56375a8d..2e4e900fc 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -126,7 +126,7 @@ cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_Ser cFile::CreateFolder("CommLogs"); AString IP(a_Client->GetIPString()); ReplaceString(IP, ":", "_"); - AString FileName = Printf("CommLogs/%x_%d__%s.log", + auto FileName = fmt::format(FMT_STRING("CommLogs/{:x}_{}__{}.log"), static_cast(time(nullptr)), sCounter++, IP.c_str() @@ -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(Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str()), a_Type); + SendChatRaw(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(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(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); + Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason))); break; } case State::Game: { cPacketizer Pkt(*this, pktDisconnectDuringGame); - Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); + Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), EscapeString(a_Reason))); break; } default: @@ -1103,7 +1103,7 @@ void cProtocol_1_8_0::SendPlayerListUpdateDisplayName(const cPlayer & a_Player, else { Pkt.WriteBool(true); - Pkt.WriteString(Printf("{\"text\":\"%s\"}", a_CustomName.c_str())); + Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_CustomName)); } } @@ -1750,7 +1750,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(Printf("{\"text\":\"%s\"}", a_Window.GetWindowTitle().c_str())); + Pkt.WriteString(fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), a_Window.GetWindowTitle())); switch (a_Window.GetWindowType()) { @@ -2217,7 +2217,7 @@ void cProtocol_1_8_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) m_Client->ForgeAugmentServerListPing(ResponseValue); if (!Favicon.empty()) { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); + ResponseValue["favicon"] = "data:image/png;base64," + Favicon; } // Serialize the response into a packet: @@ -3086,10 +3086,11 @@ void cProtocol_1_8_0::SendPacket(cPacketizer & a_Pkt) AString Hex; ASSERT(PacketData.size() > 0); CreateHexDump(Hex, PacketData.data(), PacketData.size(), 16); - m_CommLogFile.Printf("Outgoing packet: type %s (translated to 0x%02x), length %u (0x%04x), state %d. Payload (incl. type):\n%s\n", + m_CommLogFile.Write(fmt::format( + FMT_STRING("Outgoing packet: type {} (translated to 0x{:02x}), length {} (0x{:04x}), state {}. Payload (incl. type):\n{}\n"), cPacketizer::PacketTypeToStr(a_Pkt.GetPacketType()), GetPacketID(a_Pkt.GetPacketType()), PacketData.size(), PacketData.size(), m_State, Hex - ); + )); /* // Useful for debugging a new protocol: LOGD("Outgoing packet: type %s (translated to 0x%02x), length %u (0x%04x), state %d. Payload (incl. type):\n%s\n", @@ -3137,7 +3138,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cFastNBTWriter & a_Writer, const cBlockEn a_Writer.AddString("CustomName", "@"); if (!CommandBlockEntity.GetLastOutput().empty()) { - a_Writer.AddString("LastOutput", Printf("{\"text\":\"%s\"}", CommandBlockEntity.GetLastOutput().c_str())); + a_Writer.AddString("LastOutput", fmt::format(FMT_STRING("{{\"text\":\"{}\"}}"), CommandBlockEntity.GetLastOutput())); } break; } @@ -3811,15 +3812,17 @@ void cProtocol_1_8_0::AddReceivedData(cByteBuffer & a_Buffer, const ContiguousBy ASSERT(a_Buffer.GetReadableSpace() == OldReadableSpace); AString Hex; CreateHexDump(Hex, AllData.data(), AllData.size(), 16); - m_CommLogFile.Printf("Incoming data, %zu (0x%zx) unparsed bytes already present in buffer:\n%s\n", - AllData.size(), AllData.size(), Hex.c_str() - ); + m_CommLogFile.Write(fmt::format( + FMT_STRING("Incoming data, {0} (0x{0:x}) unparsed bytes already present in buffer:\n{1}\n"), + AllData.size(), Hex + )); } AString Hex; CreateHexDump(Hex, a_Data.data(), a_Data.size(), 16); - m_CommLogFile.Printf("Incoming data: %zu (0x%zx) bytes: \n%s\n", - a_Data.size(), a_Data.size(), Hex.c_str() - ); + m_CommLogFile.Write(fmt::format( + FMT_STRING("Incoming data: {0} (0x{0:x}) bytes: \n{1}\n"), + a_Data.size(), Hex + )); m_CommLogFile.Flush(); } @@ -3902,9 +3905,10 @@ void cProtocol_1_8_0::AddReceivedData(cByteBuffer & a_Buffer, const ContiguousBy ASSERT(a_Buffer.GetReadableSpace() == OldReadableSpace); AString Hex; CreateHexDump(Hex, AllData.data(), AllData.size(), 16); - m_CommLogFile.Printf("There are %zu (0x%zx) bytes of non-parse-able data left in the buffer:\n%s", - a_Buffer.GetReadableSpace(), a_Buffer.GetReadableSpace(), Hex.c_str() - ); + m_CommLogFile.Write(fmt::format( + FMT_STRING("There are {0} (0x{0:x}) bytes of non-parse-able data left in the buffer:\n{1}"), + a_Buffer.GetReadableSpace(), Hex + )); m_CommLogFile.Flush(); } } @@ -4157,9 +4161,10 @@ void cProtocol_1_8_0::HandlePacket(cByteBuffer & a_Buffer) PacketData.resize(PacketData.size() - 1); AString PacketDataHex; CreateHexDump(PacketDataHex, PacketData.data(), PacketData.size(), 16); - m_CommLogFile.Printf("Next incoming packet is type %u (0x%x), length %u (0x%x) at state %d. Payload:\n%s\n", - PacketType, PacketType, a_Buffer.GetUsedSpace(), a_Buffer.GetUsedSpace(), m_State, PacketDataHex.c_str() - ); + m_CommLogFile.Write(fmt::format( + FMT_STRING("Next incoming packet is type {0} (0x{0:x}), length {1} (0x{1:x}) at state {2}. Payload:\n{3}\n"), + PacketType, a_Buffer.GetUsedSpace(), m_State, PacketDataHex + )); } if (!HandlePacket(a_Buffer, PacketType)) @@ -4181,7 +4186,7 @@ void cProtocol_1_8_0::HandlePacket(cByteBuffer & a_Buffer) // Put a message in the comm log: if (g_ShouldLogCommIn && m_CommLogFile.IsOpen()) { - m_CommLogFile.Printf("^^^^^^ Unhandled packet ^^^^^^\n\n\n"); + m_CommLogFile.Write("^^^^^^ Unhandled packet ^^^^^^\n\n\n"); } return; @@ -4198,9 +4203,10 @@ void cProtocol_1_8_0::HandlePacket(cByteBuffer & a_Buffer) // Put a message in the comm log: if (g_ShouldLogCommIn && m_CommLogFile.IsOpen()) { - m_CommLogFile.Printf("^^^^^^ Wrong number of bytes read for this packet (exp %d left, got %zu left) ^^^^^^\n\n\n", - 1, a_Buffer.GetReadableSpace() - ); + m_CommLogFile.Write(fmt::format( + FMT_STRING("^^^^^^ Wrong number of bytes read for this packet (exp 1 left, got {} left) ^^^^^^\n\n\n"), + a_Buffer.GetReadableSpace() + )); m_CommLogFile.Flush(); } -- cgit v1.2.3