diff options
author | Tiger Wang <ziwei.tiger@outlook.com> | 2020-07-18 19:57:23 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@outlook.com> | 2020-07-18 20:23:10 +0200 |
commit | 5141d05ba676df9584831ffa9ff6e8ff8905887d (patch) | |
tree | 34a516e598fb0aeece8d728ad8f978d3d00fd0d0 | |
parent | 1.13 items support (diff) | |
download | cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar.gz cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar.bz2 cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar.lz cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar.xz cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.tar.zst cuberite-5141d05ba676df9584831ffa9ff6e8ff8905887d.zip |
-rw-r--r-- | src/Protocol/Protocol.h | 19 | ||||
-rw-r--r-- | src/Protocol/ProtocolRecognizer.cpp | 52 | ||||
-rw-r--r-- | src/Protocol/ProtocolRecognizer.h | 18 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_10.cpp | 44 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_10.h | 7 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_11.cpp | 92 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_11.h | 11 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_12.cpp | 154 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_12.h | 23 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_13.cpp | 53 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_13.h | 4 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_8.cpp | 19 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_8.h | 3 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_9.cpp | 188 | ||||
-rw-r--r-- | src/Protocol/Protocol_1_9.h | 29 | ||||
-rw-r--r-- | src/Root.cpp | 2 |
16 files changed, 182 insertions, 536 deletions
diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 61d098601..96e837bb0 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -326,6 +326,22 @@ public: Pose }; + enum class Version + { + Version_1_8_0 = 47, + Version_1_9_0 = 107, + Version_1_9_1 = 108, + Version_1_9_2 = 109, + Version_1_9_4 = 110, + Version_1_10_0 = 210, + Version_1_11_0 = 315, + Version_1_11_1 = 316, + Version_1_12 = 335, + Version_1_12_1 = 338, + Version_1_12_2 = 340, + Version_1_13 = 393 + }; + /** Called when client sends some data */ virtual void DataReceived(const char * a_Data, size_t a_Size) = 0; @@ -438,6 +454,9 @@ protected: /** Returns the protocol-specific packet ID given the protocol-agnostic packet enum. */ virtual UInt32 GetPacketID(ePacketType a_Packet) = 0; + /** Returns the current protocol's version, for handling status requests. */ + virtual Version GetProtocolVersion() = 0; + /** A generic data-sending routine, all outgoing packet data needs to be routed through this so that descendants may override it. */ virtual void SendData(const char * a_Data, size_t a_Size) = 0; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 0e93aa98f..6dc1c8bd8 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -58,22 +58,22 @@ cMultiVersionProtocol::cMultiVersionProtocol() : -AString cMultiVersionProtocol::GetVersionTextFromInt(int a_ProtocolVersion) +AString cMultiVersionProtocol::GetVersionTextFromInt(cProtocol::Version a_ProtocolVersion) { switch (a_ProtocolVersion) { - 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"; - case PROTO_VERSION_1_10_0: return "1.10"; - case PROTO_VERSION_1_11_0: return "1.11"; - case PROTO_VERSION_1_11_1: return "1.11.1"; - case PROTO_VERSION_1_12: return "1.12"; - case PROTO_VERSION_1_12_1: return "1.12.1"; - case PROTO_VERSION_1_12_2: return "1.12.2"; - case PROTO_VERSION_1_13: return "1.13"; + case cProtocol::Version::Version_1_8_0: return "1.8"; + case cProtocol::Version::Version_1_9_0: return "1.9"; + case cProtocol::Version::Version_1_9_1: return "1.9.1"; + case cProtocol::Version::Version_1_9_2: return "1.9.2"; + case cProtocol::Version::Version_1_9_4: return "1.9.4"; + case cProtocol::Version::Version_1_10_0: return "1.10"; + case cProtocol::Version::Version_1_11_0: return "1.11"; + case cProtocol::Version::Version_1_11_1: return "1.11.1"; + case cProtocol::Version::Version_1_12: return "1.12"; + case cProtocol::Version::Version_1_12_1: return "1.12.1"; + case cProtocol::Version::Version_1_12_2: return "1.12.2"; + case cProtocol::Version::Version_1_13: return "1.13"; } ASSERT(!"Unknown protocol version"); return Printf("Unknown protocol (%d)", a_ProtocolVersion); @@ -281,20 +281,20 @@ std::unique_ptr<cProtocol> cMultiVersionProtocol::TryRecognizeLengthedProtocol(c // All good, eat up the data: m_Buffer.CommitRead(); - switch (ProtocolVersion) + switch (static_cast<cProtocol::Version>(ProtocolVersion)) { - case PROTO_VERSION_1_8_0: return std::make_unique<cProtocol_1_8_0>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_9_0: return std::make_unique<cProtocol_1_9_0>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_9_1: return std::make_unique<cProtocol_1_9_1>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_9_2: return std::make_unique<cProtocol_1_9_2>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_9_4: return std::make_unique<cProtocol_1_9_4>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_10_0: return std::make_unique<cProtocol_1_10_0>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_11_0: return std::make_unique<cProtocol_1_11_0>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_11_1: return std::make_unique<cProtocol_1_11_1>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_12: return std::make_unique<cProtocol_1_12>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_12_1: return std::make_unique<cProtocol_1_12_1>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_12_2: return std::make_unique<cProtocol_1_12_2>(&a_Client, ServerAddress, ServerPort, NextState); - case PROTO_VERSION_1_13: return std::make_unique<cProtocol_1_13>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_8_0: return std::make_unique<cProtocol_1_8_0>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_9_0: return std::make_unique<cProtocol_1_9_0>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_9_1: return std::make_unique<cProtocol_1_9_1>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_9_2: return std::make_unique<cProtocol_1_9_2>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_9_4: return std::make_unique<cProtocol_1_9_4>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_10_0: return std::make_unique<cProtocol_1_10_0>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_11_0: return std::make_unique<cProtocol_1_11_0>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_11_1: return std::make_unique<cProtocol_1_11_1>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_12: return std::make_unique<cProtocol_1_12>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_12_1: return std::make_unique<cProtocol_1_12_1>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_12_2: return std::make_unique<cProtocol_1_12_2>(&a_Client, ServerAddress, ServerPort, NextState); + case cProtocol::Version::Version_1_13: return std::make_unique<cProtocol_1_13>(&a_Client, ServerAddress, ServerPort, NextState); default: { LOGD("Client \"%s\" uses an unsupported protocol (lengthed, version %u (0x%x))", diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 9aa31572e..b19adaefe 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -21,26 +21,10 @@ class cMultiVersionProtocol { public: - enum - { - PROTO_VERSION_1_8_0 = 47, - PROTO_VERSION_1_9_0 = 107, - PROTO_VERSION_1_9_1 = 108, - PROTO_VERSION_1_9_2 = 109, - PROTO_VERSION_1_9_4 = 110, - PROTO_VERSION_1_10_0 = 210, - PROTO_VERSION_1_11_0 = 315, - PROTO_VERSION_1_11_1 = 316, - PROTO_VERSION_1_12 = 335, - PROTO_VERSION_1_12_1 = 338, - PROTO_VERSION_1_12_2 = 340, - PROTO_VERSION_1_13 = 393 - }; - cMultiVersionProtocol(); /** Translates protocol version number into protocol version text: 49 -> "1.4.4" */ - static AString GetVersionTextFromInt(int a_ProtocolVersion); + static AString GetVersionTextFromInt(cProtocol::Version a_ProtocolVersion); /** Returns if we contain a concrete protocol corresponding to the client's protocol version. */ bool VersionRecognitionSuccessful() diff --git a/src/Protocol/Protocol_1_10.cpp b/src/Protocol/Protocol_1_10.cpp index 300b4d381..3ace0fe43 100644 --- a/src/Protocol/Protocol_1_10.cpp +++ b/src/Protocol/Protocol_1_10.cpp @@ -332,54 +332,18 @@ void cProtocol_1_10_0::SendSoundEffect(const AString & a_SoundName, double a_X, -void cProtocol_1_10_0::HandlePacketResourcePackStatus(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_10_0::GetProtocolVersion() { - HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Status); + return Version::Version_1_10_0; } -void cProtocol_1_10_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +void cProtocol_1_10_0::HandlePacketResourcePackStatus(cByteBuffer & a_ByteBuffer) { - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.10"; - Version["protocol"] = 210; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - AString Response = JsonUtils::WriteFastString(ResponseValue); - - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Status); } diff --git a/src/Protocol/Protocol_1_10.h b/src/Protocol/Protocol_1_10.h index 9e2849d6f..8c4fc1f14 100644 --- a/src/Protocol/Protocol_1_10.h +++ b/src/Protocol/Protocol_1_10.h @@ -28,12 +28,13 @@ public: cProtocol_1_10_0(cClientHandle * a_Client, const AString &a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); +protected: + virtual void SendSoundEffect(const AString & a_SoundName, double a_X, double a_Y, double a_Z, float a_Volume, float a_Pitch) override; + /** Returns 1.10. */ + virtual Version GetProtocolVersion() override; virtual void HandlePacketResourcePackStatus(cByteBuffer & a_ByteBuffer) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; - -protected: virtual void WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) override; virtual void WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) override; virtual void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity) override; diff --git a/src/Protocol/Protocol_1_11.cpp b/src/Protocol/Protocol_1_11.cpp index 84151b4a9..20b26b31a 100644 --- a/src/Protocol/Protocol_1_11.cpp +++ b/src/Protocol/Protocol_1_11.cpp @@ -540,6 +540,15 @@ void cProtocol_1_11_0::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int +cProtocol::Version cProtocol_1_11_0::GetProtocolVersion() +{ + return Version::Version_1_11_0; +} + + + + + UInt32 cProtocol_1_11_0::GetProtocolMobType(eMonsterType a_MobType) { switch (a_MobType) @@ -607,50 +616,6 @@ void cProtocol_1_11_0::HandlePacketBlockPlace(cByteBuffer & a_ByteBuffer) -void cProtocol_1_11_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.11"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_11_0; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); -} - - - - - void cProtocol_1_11_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) { using namespace Metadata_1_11; @@ -1229,42 +1194,7 @@ cProtocol_1_11_1::cProtocol_1_11_1(cClientHandle * a_Client, const AString & a_S -void cProtocol_1_11_1::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_11_1::GetProtocolVersion() { - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.11.1"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_11_1; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); + return Version::Version_1_11_1; } diff --git a/src/Protocol/Protocol_1_11.h b/src/Protocol/Protocol_1_11.h index a4d763d47..6a348d5fc 100644 --- a/src/Protocol/Protocol_1_11.h +++ b/src/Protocol/Protocol_1_11.h @@ -30,19 +30,21 @@ public: cProtocol_1_11_0(cClientHandle * a_Client, const AString &a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); +protected: + virtual void SendCollectEntity(const cEntity & a_Collected, const cEntity & a_Collector, unsigned a_Count) override; virtual void SendHideTitle (void) override; virtual void SendResetTitle (void) override; virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendTitleTimes (int a_FadeInTicks, int a_DisplayTicks, int a_FadeOutTicks) override; -protected: + /** Returns 1.11. */ + virtual Version GetProtocolVersion() override; /** Converts eMonsterType to protocol-specific mob IDs */ virtual UInt32 GetProtocolMobType(eMonsterType a_MobType) override; virtual void HandlePacketBlockPlace (cByteBuffer & a_ByteBuffer) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; virtual void WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) override; virtual void WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) override; @@ -62,5 +64,8 @@ public: cProtocol_1_11_1(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; +protected: + + /** Returns 1.11.1. */ + virtual Version GetProtocolVersion() override; }; diff --git a/src/Protocol/Protocol_1_12.cpp b/src/Protocol/Protocol_1_12.cpp index 0402cdaa3..804e26b40 100644 --- a/src/Protocol/Protocol_1_12.cpp +++ b/src/Protocol/Protocol_1_12.cpp @@ -327,50 +327,6 @@ cProtocol_1_12::cProtocol_1_12(cClientHandle * a_Client, const AString & a_Serve -void cProtocol_1_12::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.12"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_12; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); -} - - - - - void cProtocol_1_12::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) { using namespace Metadata_1_12; @@ -1058,6 +1014,15 @@ void cProtocol_1_12::HandlePacketAdvancementTab(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_12::GetProtocolVersion() +{ + return Version::Version_1_12; +} + + + + + bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { switch (m_State) @@ -1205,43 +1170,9 @@ UInt32 cProtocol_1_12_1::GetPacketID(ePacketType a_Packet) -void cProtocol_1_12_1::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_12_1::GetProtocolVersion() { - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.12.1"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_12_1; - - // 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()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); + return Version::Version_1_12_1; } @@ -1343,7 +1274,25 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT //////////////////////////////////////////////////////////////////////////////// -// cProtocol_1_12_2: +// cProtocol_1_12_2:: + +cProtocol_1_12_2::cProtocol_1_12_2(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : + Super(a_Client, a_ServerAddress, a_ServerPort, a_State) +{ +} + + + + + +cProtocol::Version cProtocol_1_12_2::GetProtocolVersion() +{ + return Version::Version_1_12_2; +} + + + + void cProtocol_1_12_2::HandlePacketKeepAlive(cByteBuffer & a_ByteBuffer) { @@ -1362,49 +1311,6 @@ void cProtocol_1_12_2::HandlePacketKeepAlive(cByteBuffer & a_ByteBuffer) -void cProtocol_1_12_2::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.12.2"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_12_2; - - // 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()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); -} - - - - - void cProtocol_1_12_2::SendKeepAlive(UInt32 a_PingID) { // Drop the packet if the protocol is not in the Game state yet (caused a client crash): diff --git a/src/Protocol/Protocol_1_12.h b/src/Protocol/Protocol_1_12.h index c1b81955a..266fc03bb 100644 --- a/src/Protocol/Protocol_1_12.h +++ b/src/Protocol/Protocol_1_12.h @@ -34,17 +34,17 @@ public: cProtocol_1_12(cClientHandle * a_Client, const AString &a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); protected: + + virtual UInt32 GetPacketID(ePacketType a_Packet) override; + + /** Returns 1.12. */ + virtual Version GetProtocolVersion() override; virtual bool HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) override; virtual void HandlePacketAdvancementTab(cByteBuffer & a_ByteBuffer); virtual void HandleCraftRecipe(cByteBuffer & a_ByteBuffer); virtual void HandlePacketCraftingBookData(cByteBuffer & a_ByteBuffer); - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; virtual void WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) override; virtual void WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) override; - -protected: - - virtual UInt32 GetPacketID(ePacketType a_Packet) override; }; @@ -61,10 +61,12 @@ public: cProtocol_1_12_1(cClientHandle * a_Client, const AString &a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); protected: + virtual UInt32 GetPacketID(ePacketType a_Packet) override; + /** Returns 1.12.1. */ + virtual Version GetProtocolVersion() override; virtual bool HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; }; @@ -78,14 +80,13 @@ class cProtocol_1_12_2: public: - cProtocol_1_12_2(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State): - Super(a_Client, a_ServerAddress, a_ServerPort, a_State) - { - } + cProtocol_1_12_2(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); protected: + + /** Returns 1.12.2. */ + virtual Version GetProtocolVersion() override; virtual void HandlePacketKeepAlive(cByteBuffer & a_ByteBuffer) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; virtual void SendKeepAlive(UInt32 a_PingID) override; virtual void SendUnlockRecipe(UInt32 a_RecipeID) override; virtual void SendInitRecipes(UInt32 a_RecipeID) override; diff --git a/src/Protocol/Protocol_1_13.cpp b/src/Protocol/Protocol_1_13.cpp index 74e663f3d..f26b1900a 100644 --- a/src/Protocol/Protocol_1_13.cpp +++ b/src/Protocol/Protocol_1_13.cpp @@ -197,6 +197,15 @@ void cProtocol_1_13::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) +cProtocol::Version cProtocol_1_13::GetProtocolVersion() +{ + return Version::Version_1_13; +} + + + + + bool cProtocol_1_13::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { if (m_State != 3) @@ -250,50 +259,6 @@ bool cProtocol_1_13::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp -void cProtocol_1_13::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.13"; - Version["protocol"] = cMultiVersionProtocol::PROTO_VERSION_1_13; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - // Serialize the response into a packet: - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); -} - - - - - void cProtocol_1_13::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Channel); diff --git a/src/Protocol/Protocol_1_13.h b/src/Protocol/Protocol_1_13.h index 0f9ad721e..b315768ba 100644 --- a/src/Protocol/Protocol_1_13.h +++ b/src/Protocol/Protocol_1_13.h @@ -52,9 +52,11 @@ protected: virtual void SendTabCompletionResults (const AStringVector & a_Results) override; virtual void SendUpdateBlockEntity (cBlockEntity & a_BlockEntity) override; + /** Returns 1.13. */ + virtual Version GetProtocolVersion() override; + // Packet receiving: virtual bool HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; virtual void HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) override; // Outgoing packet type translation: diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index a01fb0424..1c2792461 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -2159,6 +2159,15 @@ UInt32 cProtocol_1_8_0::GetPacketID(ePacketType a_PacketType) +cProtocol::Version cProtocol_1_8_0::GetProtocolVersion() +{ + return Version::Version_1_8_0; +} + + + + + bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { switch (m_State) @@ -2270,8 +2279,9 @@ void cProtocol_1_8_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) // Version: Json::Value Version; - Version["name"] = "Cuberite 1.8"; - Version["protocol"] = 47; + const auto ProtocolVersion = GetProtocolVersion(); + Version["name"] = "Cuberite " + cMultiVersionProtocol::GetVersionTextFromInt(ProtocolVersion); + Version["protocol"] = static_cast<std::underlying_type_t<cProtocol::Version>>(ProtocolVersion); // Players: Json::Value Players; @@ -2294,10 +2304,9 @@ void cProtocol_1_8_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); } - auto Response = JsonUtils::WriteFastString(ResponseValue); - + // Serialize the response into a packet: cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); + Pkt.WriteString(JsonUtils::WriteFastString(ResponseValue)); } diff --git a/src/Protocol/Protocol_1_8.h b/src/Protocol/Protocol_1_8.h index a8232104b..b62e3129f 100644 --- a/src/Protocol/Protocol_1_8.h +++ b/src/Protocol/Protocol_1_8.h @@ -160,6 +160,9 @@ protected: /** Nobody inherits 1.8, so it doesn't use this method */ virtual UInt32 GetPacketID(ePacketType a_Packet) override; + /** Returns 1.8. */ + virtual Version GetProtocolVersion() override; + /** Converts eMonsterType to protocol-specific mob types */ virtual UInt32 GetProtocolMobType(eMonsterType a_MobType); diff --git a/src/Protocol/Protocol_1_9.cpp b/src/Protocol/Protocol_1_9.cpp index e17832175..90e770134 100644 --- a/src/Protocol/Protocol_1_9.cpp +++ b/src/Protocol/Protocol_1_9.cpp @@ -581,6 +581,15 @@ UInt32 cProtocol_1_9_0::GetPacketID(cProtocol::ePacketType a_Packet) +cProtocol::Version cProtocol_1_9_0::GetProtocolVersion() +{ + return Version::Version_1_9_0; +} + + + + + bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { switch (m_State) @@ -673,51 +682,6 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy -void cProtocol_1_9_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.9"; - Version["protocol"] = 107; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - auto Response = JsonUtils::WriteFastString(ResponseValue); - - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); -} - - - - - void cProtocol_1_9_0::HandlePacketAnimation(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarInt, Int32, Hand); @@ -2251,45 +2215,9 @@ void cProtocol_1_9_1::SendLogin(const cPlayer & a_Player, const cWorld & a_World -void cProtocol_1_9_1::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_9_1::GetProtocolVersion() { - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.9.1"; - Version["protocol"] = 108; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - AString Response = JsonUtils::WriteFastString(ResponseValue); - - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); + return Version::Version_1_9_1; } @@ -2308,45 +2236,9 @@ cProtocol_1_9_2::cProtocol_1_9_2(cClientHandle * a_Client, const AString & a_Ser -void cProtocol_1_9_2::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +cProtocol::Version cProtocol_1_9_2::GetProtocolVersion() { - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.9.2"; - Version["protocol"] = 109; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - AString Response = JsonUtils::WriteFastString(ResponseValue); - - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); + return Version::Version_1_9_2; } @@ -2365,51 +2257,6 @@ cProtocol_1_9_4::cProtocol_1_9_4(cClientHandle * a_Client, const AString & a_Ser -void cProtocol_1_9_4::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast<signed>(Server->GetNumPlayers()); - auto MaxPlayers = static_cast<signed>(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.9.4"; - Version["protocol"] = 110; - - // 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; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - AString Response = JsonUtils::WriteFastString(ResponseValue); - - cPacketizer Pkt(*this, pktStatusResponse); - Pkt.WriteString(Response); -} - - - - - void cProtocol_1_9_4::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) { ASSERT(m_State == 3); // In game mode? @@ -2462,6 +2309,15 @@ void cProtocol_1_9_4::SendUpdateSign(int a_BlockX, int a_BlockY, int a_BlockZ, c +cProtocol::Version cProtocol_1_9_4::GetProtocolVersion() +{ + return Version::Version_1_9_4; +} + + + + + UInt32 cProtocol_1_9_4::GetPacketID(cProtocol::ePacketType a_Packet) { switch (a_Packet) diff --git a/src/Protocol/Protocol_1_9.h b/src/Protocol/Protocol_1_9.h index a09317190..cb96f6962 100644 --- a/src/Protocol/Protocol_1_9.h +++ b/src/Protocol/Protocol_1_9.h @@ -68,16 +68,16 @@ protected: bool m_IsTeleportIdConfirmed; UInt32 m_OutstandingTeleportId; - /** Get the packet ID for a given packet */ + /** Get the packet ID for a given packet. */ virtual UInt32 GetPacketID(ePacketType a_Packet) override; + /** Returns 1.9. */ + virtual Version GetProtocolVersion() override; + /** Reads and handles the packet. The packet length and type have already been read. Returns true if the packet was understood, false if it was an unknown packet. */ virtual bool HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) override; - // Packet handlers while in the Status state (m_State == 1): - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; - // Packet handlers while in the Game state (m_State == 3): virtual void HandlePacketAnimation (cByteBuffer & a_ByteBuffer) override; virtual void HandlePacketBlockDig (cByteBuffer & a_ByteBuffer) override; @@ -158,10 +158,12 @@ public: cProtocol_1_9_1(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); - // cProtocol_1_9_0 overrides: +protected: + virtual void SendLogin(const cPlayer & a_Player, const cWorld & a_World) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; + /** Returns 1.9.1. */ + virtual Version GetProtocolVersion() override; } ; @@ -178,9 +180,10 @@ public: cProtocol_1_9_2(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); - // cProtocol_1_9_1 overrides: - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; +protected: + /** Returns 1.9.2. */ + virtual Version GetProtocolVersion() override; } ; @@ -197,14 +200,12 @@ public: cProtocol_1_9_4(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); - // cProtocol_1_9_2 overrides: +protected: + virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; virtual void SendUpdateSign (int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4) override; - virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; - -protected: - + /** Returns 1.9.4. */ + virtual Version GetProtocolVersion() override; virtual UInt32 GetPacketID(ePacketType a_Packet) override; - } ; diff --git a/src/Root.cpp b/src/Root.cpp index 5d643f3b2..e2d4f71c3 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -929,7 +929,7 @@ bool cRoot::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback a_Cal AString cRoot::GetProtocolVersionTextFromInt(int a_ProtocolVersion) { - return cMultiVersionProtocol::GetVersionTextFromInt(a_ProtocolVersion); + return cMultiVersionProtocol::GetVersionTextFromInt(static_cast<cProtocol::Version>(a_ProtocolVersion)); } |