From a4f327118b06ced1cd4510b7d20d34da83aa78a3 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sat, 14 May 2016 12:12:42 -0700 Subject: 1.9 / 1.9.2 / 1.9.3 / 1.9.4 protocol support (#3135) * Semistable update to 15w31a I'm going through snapshots in a sequential order since it should make things easier, and since protocol version history is written. * Update to 15w34b protocol Also, fix an issue with the Entity Equipment packet from the past version. Clients are able to connect and do stuff! * Partially update to 15w35e Chunk data doesn't work, but the client joins. I'm waiting to do chunk data because chunk data has an incomplete format until 15w36d. * Add '/blk' debug command This command lets one see what block they are looking at, and makes figuring out what's supposed to be where in a highly broken chunk possible. * Fix CRLF normalization in CheckBasicStyle.lua Normally, this doesn't cause an issue, but when running from cygwin, it detects the CR as whitespace and creates thousands of violations for every single line. Lua, when run on windows, will normalize automatically, but when run via cygwin, it won't. The bug was simply that gsub was returning a replaced version, but not changing the parameter, so the replaced version was ignored. * Update to 15w40b This includes chunk serialization. Fully functional chunk serialization for 1.9. I'm not completely happy with the chunk serialization as-is (correct use of palettes would be great), but cuberite also doesn't skip sending empty chunks so this performance optimization should probably come later. The creation of a full buffer is suboptimal, but it's the easiest way to implement this code. * Write long-by-long rather than creating a buffer This is a bit faster and should be equivalent. However, the code still doesn't look too good. * Update to 15w41a protocol This includes the new set passengers packet, which works off of the ridden entity, not the rider. That means, among other things, that information about the previously ridden vehicle is needed when detaching. So a new method with that info was added. * Update to 15w45a * 15w51b protocol * Update to 1.9.0 protocol Closes #3067. There are still a few things that need to be worked out (picking up items, effects, particles, and most importantly inventory), but in general this should work. I'll make a few more changes tomorrow to get the rest of the protocol set up, along with 1.9.1/1.9.2 (which did make a few changes). Chunks, however, _are_ working, along with most other parts of the game (placing/breaking blocks). * Fix item pickup packet not working That was a silly mistake, but at least it was an easy one. * 1.9.2 protocol support * Fix version info found in server list ping Thus, the client reports that it can connect rather than saying that the server is out of date. This required creating separate classes for 1.9.1 and 1.9.2, unfortunately. * Fix build errors generated by clang These didn't happen in MSVC. * Add protocol19x.cpp and protocol19x.h to CMakeLists * Ignore warnings in protocol19x that are ignored in protocol18x * Document BLOCK_FACE and DIG_STATUS constants * Fix BLOCK_FACE links and add separate section for DIG_STATUS * Fix bat animation and object spawning The causes of both of these are explained in #3135, but the gist is that both were typos. * Implement Use Item packet This means that buckets, bows, fishing rods, and several other similar items now work when not looking at a block. * Handle DIG_STATUS_SWAP_ITEM_IN_HAND * Add support for spawn eggs and potions The items are transformed from the 1.9 version to the 1.8 version when reading and transformed back when sending. * Remove spammy potion debug logging * Fix wolf collar color metadata The wrong type was being used, causing several clientside issues (including the screen going black). * Fix 1.9 chunk sending in the nether The nether and the end don't send skylight. * Fix clang build errors * Fix water bottles becoming mundane potions This happened because the can become splash potion bit got set incorrectly. Water bottles and mundane potions are only differentiated by the fact that water bottles have a metadata of 0, so setting that bit made it a mundane potion. Also add missing break statements to the read item NBT switch, which would otherwise break items with custom names and also cause incorrect "Unimplemented NBT data when parsing!" logging. * Copy Protocol18x as Protocol19x Aditionally, method and class names have been swapped to clean up other diffs. This commit is only added to make the following diffs more readable; it doesn't make any other changes (beyond class names). * Make thrown potions use the correct appearence This was caused by potions now using metadata. * Add missing api doc for cSplashPotionEntity::GetItem * Fix compile error in SplashPotionEntity.cpp * Fix fix of cSplashPotionEntity API doc * Temporarilly disable fall damage particles These were causing issues in 1.9 due to the changed effect ID. * Properly send a kick packet when connecting with an invalid version This means that the client no longer waits on the server screen with no indication whatsoever. However, right now the server list ping isn't implemented for unknown versions, so it'll only load "Old" on the ping. I also added a GetVarIntSize method to cByteBuffer. This helps clean up part of the code here (and I think it could clean up other parts), but it may make sense for it to be moved elsewhere (or declared in a different way). * Handle server list pings from unrecognized versions This isn't the cleanest way of writing it (it feels odd to use ProtocolRecognizer to send packets, and the addition of m_InPingForUnrecognizedVersion feels like the wrong technique), but it works and I can't think of a better way (apart from creating a full separate protocol class to handle only the ping... which would be worse). * Use cPacketizer for the disconnect packet This also should fix clang build errors. * Add 1.9.3 / 1.9.4 support * Fix incorrect indentation in APIDesc --- src/Protocol/ProtocolRecognizer.cpp | 240 ++++++++++++++++++++++++++---------- 1 file changed, 175 insertions(+), 65 deletions(-) (limited to 'src/Protocol/ProtocolRecognizer.cpp') diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index fff55b6e9..3977183e9 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -9,6 +9,8 @@ #include "ProtocolRecognizer.h" #include "Protocol17x.h" #include "Protocol18x.h" +#include "Protocol19x.h" +#include "Packetizer.h" #include "../ClientHandle.h" #include "../Root.h" #include "../Server.h" @@ -23,7 +25,8 @@ cProtocolRecognizer::cProtocolRecognizer(cClientHandle * a_Client) : super(a_Client), m_Protocol(nullptr), - m_Buffer(8192) // We need a larger buffer to support BungeeCord - it sends one huge packet at the start + m_Buffer(8192), // We need a larger buffer to support BungeeCord - it sends one huge packet at the start + m_InPingForUnrecognizedVersion(false) { } @@ -48,6 +51,10 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) case PROTO_VERSION_1_7_2: return "1.7.2"; case PROTO_VERSION_1_7_6: return "1.7.6"; case PROTO_VERSION_1_8_0: return "1.8"; + case PROTO_VERSION_1_9_0: return "1.9"; + case PROTO_VERSION_1_9_1: return "1.9.1"; + case PROTO_VERSION_1_9_2: return "1.9.2"; + case PROTO_VERSION_1_9_4: return "1.9.4"; } ASSERT(!"Unknown protocol version"); return Printf("Unknown protocol (%d)", a_ProtocolVersion); @@ -67,6 +74,33 @@ void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) return; } + if (m_InPingForUnrecognizedVersion) + { + // We already know the verison; handle it here. + UInt32 PacketLen; + UInt32 PacketID; + if (!m_Buffer.ReadVarInt32(PacketLen)) + { + return; + } + if (!m_Buffer.ReadVarInt32(PacketID)) + { + return; + } + ASSERT(PacketID == 0x01); // Ping packet + ASSERT(PacketLen == 9); // Payload of the packet ID and a UInt64 + + Int64 Data; + if (!m_Buffer.ReadBEInt64(Data)) + { + return; + } + + cPacketizer Pkt(*this, 0x01); // Pong packet + Pkt.WriteBEInt64(Data); + return; + } + if (!TryRecognizeProtocol()) { return; @@ -88,7 +122,7 @@ void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) -void cProtocolRecognizer::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) +void cProtocolRecognizer::SendAttachEntity(const cEntity & a_Entity, const cEntity & a_Vehicle) { ASSERT(m_Protocol != nullptr); m_Protocol->SendAttachEntity(a_Entity, a_Vehicle); @@ -188,6 +222,16 @@ void cProtocolRecognizer::SendDestroyEntity(const cEntity & a_Entity) +void cProtocolRecognizer::SendDetachEntity(const cEntity & a_Entity, const cEntity & a_PreviousVehicle) +{ + ASSERT(m_Protocol != nullptr); + m_Protocol->SendDetachEntity(a_Entity, a_PreviousVehicle); +} + + + + + void cProtocolRecognizer::SendDisconnect(const AString & a_Reason) { if (m_Protocol != nullptr) @@ -196,14 +240,9 @@ void cProtocolRecognizer::SendDisconnect(const AString & a_Reason) } else { - // This is used when the client sends a server-ping, respond with the default packet: - static const int Packet = 0xff; // PACKET_DISCONNECT - SendData(reinterpret_cast(&Packet), 1); // WriteByte() - - auto UTF16 = UTF8ToRawBEUTF16(a_Reason); - static const u_short Size = htons(static_cast(UTF16.size())); - SendData(reinterpret_cast(&Size), 2); // WriteShort() - SendData(reinterpret_cast(UTF16.data()), UTF16.size() * sizeof(char16_t)); // WriteString() + AString Message = Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str()); + cPacketizer Pkt(*this, 0x00); // Disconnect packet (in login state) + Pkt.WriteString(Message); } } @@ -967,88 +1006,159 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema return false; } m_Client->SetProtocolVersion(ProtocolVersion); + AString ServerAddress; + UInt16 ServerPort; + UInt32 NextState; + if (!m_Buffer.ReadVarUTF8String(ServerAddress)) + { + return false; + } + if (!m_Buffer.ReadBEUInt16(ServerPort)) + { + return false; + } + if (!m_Buffer.ReadVarInt(NextState)) + { + return false; + } + m_Buffer.CommitRead(); switch (ProtocolVersion) { case PROTO_VERSION_1_7_2: { - AString ServerAddress; - UInt16 ServerPort; - UInt32 NextState; - if (!m_Buffer.ReadVarUTF8String(ServerAddress)) - { - break; - } - if (!m_Buffer.ReadBEUInt16(ServerPort)) - { - break; - } - if (!m_Buffer.ReadVarInt(NextState)) - { - break; - } - m_Buffer.CommitRead(); m_Protocol = new cProtocol172(m_Client, ServerAddress, ServerPort, NextState); return true; } case PROTO_VERSION_1_7_6: { - AString ServerAddress; - UInt16 ServerPort; - UInt32 NextState; - if (!m_Buffer.ReadVarUTF8String(ServerAddress)) - { - break; - } - if (!m_Buffer.ReadBEUInt16(ServerPort)) - { - break; - } - if (!m_Buffer.ReadVarInt(NextState)) - { - break; - } - m_Buffer.CommitRead(); m_Protocol = new cProtocol176(m_Client, ServerAddress, ServerPort, NextState); return true; } case PROTO_VERSION_1_8_0: { - AString ServerAddress; - UInt16 ServerPort; - UInt32 NextState; - if (!m_Buffer.ReadVarUTF8String(ServerAddress)) - { - break; - } - if (!m_Buffer.ReadBEUInt16(ServerPort)) + m_Buffer.CommitRead(); + m_Protocol = new cProtocol180(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + case PROTO_VERSION_1_9_0: + { + m_Protocol = new cProtocol190(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + case PROTO_VERSION_1_9_1: + { + m_Protocol = new cProtocol191(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + case PROTO_VERSION_1_9_2: + { + m_Protocol = new cProtocol192(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + case PROTO_VERSION_1_9_4: + { + m_Protocol = new cProtocol194(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + default: + { + LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u (0x%x))", + m_Client->GetIPString().c_str(), ProtocolVersion, ProtocolVersion + ); + if (NextState != 1) { - break; + m_Client->Kick(Printf("Unsupported protocol version %u, please use one of these versions:\n" MCS_CLIENT_VERSIONS, ProtocolVersion)); + return false; } - if (!m_Buffer.ReadVarInt(NextState)) + else { - break; + m_InPingForUnrecognizedVersion = true; + + UInt32 PacketLen; + UInt32 PacketID; + if (!m_Buffer.ReadVarInt32(PacketLen)) + { + return false; + } + if (!m_Buffer.ReadVarInt32(PacketID)) + { + return false; + } + ASSERT(PacketID == 0x00); // Request packet + ASSERT(PacketLen == 1); // No payload except for packet ID + SendPingStatusResponse(); } - m_Buffer.CommitRead(); - m_Protocol = new cProtocol180(m_Client, ServerAddress, ServerPort, NextState); - return true; + return false; } } - LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u (0x%x))", - m_Client->GetIPString().c_str(), ProtocolVersion, ProtocolVersion - ); - m_Client->Kick("Unsupported protocol version"); - return false; } + void cProtocolRecognizer::SendPacket(cPacketizer & a_Pkt) { - // This function should never be called - it needs to exists so that cProtocolRecognizer can be instantiated, - // but the actual sending is done by the internal m_Protocol itself. - LOGWARNING("%s: This function shouldn't ever be called.", __FUNCTION__); - ASSERT(!"Function not to be called"); + // Writes out the packet normally. + UInt32 PacketLen = static_cast(m_OutPacketBuffer.GetUsedSpace()); + AString PacketData, CompressedPacket; + m_OutPacketBuffer.ReadAll(PacketData); + m_OutPacketBuffer.CommitRead(); + + // Compression doesn't apply to this state, send raw data: + m_OutPacketLenBuffer.WriteVarInt32(PacketLen); + AString LengthData; + m_OutPacketLenBuffer.ReadAll(LengthData); + SendData(LengthData.data(), LengthData.size()); + + // Send the packet's payload + m_OutPacketLenBuffer.CommitRead(); + SendData(PacketData.data(), PacketData.size()); +} + + + + + +void cProtocolRecognizer::SendPingStatusResponse(void) +{ + cServer * Server = cRoot::Get()->GetServer(); + AString ServerDescription = Server->GetDescription(); + int NumPlayers = Server->GetNumPlayers(); + int MaxPlayers = Server->GetMaxPlayers(); + AString Favicon = Server->GetFaviconData(); + cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); + + // Version: + Json::Value Version; + Version["name"] = "Cuberite " MCS_CLIENT_VERSIONS; + Version["protocol"] = 0; // Force client to think this is an invalid version (no other good default) + + // Players: + Json::Value Players; + Players["online"] = NumPlayers; + Players["max"] = MaxPlayers; + // TODO: Add "sample" + + // Description: + Json::Value Description; + Description["text"] = ServerDescription.c_str(); + + // Create the response: + Json::Value ResponseValue; + ResponseValue["version"] = Version; + ResponseValue["players"] = Players; + ResponseValue["description"] = Description; + if (!Favicon.empty()) + { + ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); + } + + Json::StyledWriter Writer; + AString Response = Writer.write(ResponseValue); + + cPacketizer Pkt(*this, 0x00); // Response packet + Pkt.WriteString(Response); } -- cgit v1.2.3