From 111fd58914854bdb93c9e72566a689cfd484c0c3 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 19 Sep 2013 20:49:09 +0200 Subject: Added support for 1.6.3 and 1.6.4 protocols. No relevant changes have been detected, using the 1.6.2 protocol handler. --- source/Protocol/Protocol16x.cpp | 2 ++ source/Protocol/Protocol16x.h | 2 ++ source/Protocol/ProtocolRecognizer.cpp | 6 ++++++ source/Protocol/ProtocolRecognizer.h | 6 ++++-- 4 files changed, 14 insertions(+), 2 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol16x.cpp b/source/Protocol/Protocol16x.cpp index be5b45f19..0eac7b081 100644 --- a/source/Protocol/Protocol16x.cpp +++ b/source/Protocol/Protocol16x.cpp @@ -7,6 +7,8 @@ Implements the 1.6.x protocol classes: - release 1.6.1 protocol (#73) - cProtocol162 - release 1.6.2 protocol (#74) + - release 1.6.3 protocol (#77) - no relevant changes + - release 1.6.4 protocol (#78) - no relevant changes (others may be added later in the future for the 1.6 release series) */ diff --git a/source/Protocol/Protocol16x.h b/source/Protocol/Protocol16x.h index 077c7958b..2447f90a7 100644 --- a/source/Protocol/Protocol16x.h +++ b/source/Protocol/Protocol16x.h @@ -7,6 +7,8 @@ Declares the 1.6.x protocol classes: - release 1.6.1 protocol (#73) - cProtocol162 - release 1.6.2 protocol (#74) + - release 1.6.3 protocol (#77) - no relevant changes + - release 1.6.4 protocol (#78) - no relevant changes (others may be added later in the future for the 1.6 release series) */ diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 853018329..fe99b22e1 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -55,6 +55,8 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) case PROTO_VERSION_1_5_2: return "1.5.2"; case PROTO_VERSION_1_6_1: return "1.6.1"; case PROTO_VERSION_1_6_2: return "1.6.2"; + case PROTO_VERSION_1_6_3: return "1.6.3"; + case PROTO_VERSION_1_6_4: return "1.6.4"; } ASSERT(!"Unknown protocol version"); return Printf("Unknown protocol (%d)", a_ProtocolVersion); @@ -707,6 +709,8 @@ bool cProtocolRecognizer::TryRecognizeProtocol(void) return true; } case PROTO_VERSION_1_6_2: + case PROTO_VERSION_1_6_3: + case PROTO_VERSION_1_6_4: { m_Protocol = new cProtocol162(m_Client); return true; @@ -746,6 +750,8 @@ void cProtocolRecognizer::HandleServerPing(void) case PROTO_VERSION_1_5_2: case PROTO_VERSION_1_6_1: case PROTO_VERSION_1_6_2: + case PROTO_VERSION_1_6_3: + case PROTO_VERSION_1_6_4: { // The server list ping now has 1 more byte of "magic". Mojang just loves to complicate stuff. // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3101#Server_List_Ping_.280xFE.29 diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 2178d5e61..c53288230 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -18,8 +18,8 @@ // Adjust these if a new protocol is added or an old one is removed: -#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2" -#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74" +#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4" +#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78" @@ -42,6 +42,8 @@ public: PROTO_VERSION_1_5_2 = 61, PROTO_VERSION_1_6_1 = 73, PROTO_VERSION_1_6_2 = 74, + PROTO_VERSION_1_6_3 = 77, + PROTO_VERSION_1_6_4 = 78, PROTO_VERSION_NEXT, PROTO_VERSION_LATEST = PROTO_VERSION_NEXT - 1, ///< Automatically assigned to the last protocol version, this serves as the default for PrimaryServerVersion -- cgit v1.2.3 From f08d78c887e2ff05c2ecc2b7d16ba5b5f2ff506d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 19 Sep 2013 22:03:02 +0200 Subject: Fixed rounding errors in cProtocol125::SendExplosion(). This should fix #129. --- source/Protocol/Protocol125.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 54bd28c9f..050132917 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -436,15 +436,18 @@ void cProtocol125::SendExplosion(double a_BlockX, double a_BlockY, double a_Bloc WriteDouble (a_BlockZ); WriteFloat (a_Radius); WriteInt (a_BlocksAffected.size()); + int BlockX = (int)a_BlockX; + int BlockY = (int)a_BlockY; + int BlockZ = (int)a_BlockZ; for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(); itr != a_BlocksAffected.end(); ++itr) { - WriteByte ((Byte)(itr->x - a_BlockX)); - WriteByte ((Byte)(itr->y - a_BlockY)); - WriteByte ((Byte)(itr->z - a_BlockZ)); + WriteByte((Byte)(itr->x - BlockX)); + WriteByte((Byte)(itr->y - BlockY)); + WriteByte((Byte)(itr->z - BlockZ)); } - WriteFloat ((float)a_PlayerMotion.x); - WriteFloat ((float)a_PlayerMotion.y); - WriteFloat ((float)a_PlayerMotion.z); + WriteFloat((float)a_PlayerMotion.x); + WriteFloat((float)a_PlayerMotion.y); + WriteFloat((float)a_PlayerMotion.z); Flush(); } -- cgit v1.2.3 From 5db6213f34318031ece7e2a6765f69564b671891 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 8 Oct 2013 19:20:49 +0100 Subject: Initial Metadata Commit [SEE DESC] + Pigs, Minecarts, Sheep, Skeletons, Slimes, Villagers, Wolves, and Horses have metadata + Base code on taming wolves, shearing sheep, and taming horses + Sheep and horses have different colours when spawned --- source/Protocol/Protocol125.cpp | 220 +++++++++++++++++++++++++++++++++++----- source/Protocol/Protocol125.h | 7 +- source/Protocol/Protocol132.cpp | 3 +- 3 files changed, 195 insertions(+), 35 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 54bd28c9f..89b2c15ec 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -343,8 +343,9 @@ void cProtocol125::SendEntityMetadata(const cEntity & a_Entity) cCSLock Lock(m_CSPacket); WriteByte(PACKET_METADATA); WriteInt (a_Entity.GetUniqueID()); - AString MetaData = GetEntityMetaData(a_Entity); - SendData(MetaData.data(), MetaData.size()); + + WriteMetadata(a_Entity); + Flush(); } @@ -710,8 +711,7 @@ void cProtocol125::SendSpawnMob(const cMonster & a_Mob) WriteByte (0); WriteByte (0); WriteByte (0); - AString MetaData = GetEntityMetaData(a_Mob); - SendData (MetaData.data(), MetaData.size()); + WriteMetadata(a_Mob); Flush(); } @@ -1614,48 +1614,212 @@ int cProtocol125::ParseItem(cItem & a_Item) -AString cProtocol125::GetEntityMetaData(const cEntity & a_Entity) +void cProtocol125::WriteMetadata(const cEntity & a_Entity) { - // We should send all the metadata here - AString MetaData; - // Common metadata (index 0, byte): - MetaData.push_back(0); - MetaData.push_back(GetEntityMetadataFlags(a_Entity)); - - // TODO: Add more entity-specific metadata - - MetaData.push_back(0x7f); // End metadata - return MetaData; -} + // Common Metadata + Byte CommonMetadata = 0; - - - -char cProtocol125::GetEntityMetadataFlags(const cEntity & a_Entity) -{ - char Flags = 0; if (a_Entity.IsOnFire()) { - Flags |= 1; + CommonMetadata |= 0x1; } if (a_Entity.IsCrouched()) { - Flags |= 2; + CommonMetadata |= 0x2; } if (a_Entity.IsRiding()) { - Flags |= 4; + CommonMetadata |= 0x4; } if (a_Entity.IsSprinting()) { - Flags |= 8; + CommonMetadata |= 0x8; } if (a_Entity.IsRclking()) { - Flags |= 16; + CommonMetadata |= 0x16; + } + if (a_Entity.IsInvisible()) + { + CommonMetadata |= 0x32; + } + + WriteByte(0x0); + WriteByte(CommonMetadata); + + // Common Metadata End + // Specific Entity Metadata + + if (a_Entity.IsMinecart()) + { + WriteByte(0x51); + // No idea how Mojang makes their carts shakey shakey, so here is a complicated one-liner expression that does something similar + WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * a_Entity.LastDamage()) * 4 ); + WriteByte(0x52); + WriteInt(1); // Shaking direction, doesn't seem to affect anything + WriteByte(0x73); + WriteFloat((float)(a_Entity.LastDamage() + 10)); // Damage taken / shake effect multiplyer + } + else if (a_Entity.IsA("cCreeper")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsBlowing() ? 1 : -1); // Blowing up? + WriteByte(0x11); + WriteByte(a_Entity.IsCharged() ? 1 : 0); // Lightning-charged? + } + else if (a_Entity.IsA("cMinecartWithFurnace")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsFueled() ? 1 : 0); // Fueled? + } + else if (a_Entity.IsA("cBat")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsHanging() ? 1 : 0); // Upside down? + } + else if (a_Entity.IsA("cPig")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsSaddled() ? 1 : 0); // Saddled? + } + else if (a_Entity.IsA("cVillager")) + { + WriteByte(0x50); + WriteInt(a_Entity.GetVilType()); // What sort of TESTIFICATE? + } + else if (a_Entity.IsA("cZombie")) + { + WriteByte(0xC); + WriteByte(a_Entity.IsBabby() ? 1 : 0); // Babby zombie? + WriteByte(0xD); + WriteByte(a_Entity.IsVillZomb() ? 1 : 0); // Converted zombie? + WriteByte(0xE); + WriteByte(a_Entity.IsConvert() ? 1 : 0); // Converted-but-converting-back zombllager? + } + else if (a_Entity.IsA("cGhast")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsCharging()); // About to eject un flamé-bol? :P + } + else if (a_Entity.IsA("cArrowEntity")) + { + WriteByte(0x10); + WriteByte(a_Entity.IsCritical() ? 1 : 0); // Critical hitting arrow? + } + else if (a_Entity.IsA("cWolf")) + { + Byte WolfStatus = 0; + if (a_Entity.IsSitting()) + { + WolfStatus |= 0x1; + } + if (a_Entity.IsAngry()) + { + WolfStatus |= 0x2; + } + if (a_Entity.IsTame()) + { + WolfStatus |= 0x4; + } + WriteByte(0x10); + WriteByte(WolfStatus); + + WriteByte(0x72); + WriteFloat((float)(a_Entity.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) + WriteByte(0x13); + WriteByte(a_Entity.IsBegging() ? 1 : 0); // Ultra cute mode? + } + else if (a_Entity.IsA("cSheep")) + { + // [1](1111) + // [] = Is sheared? () = Color, from 0 to 15 + + WriteByte(0x10); + Byte SheepMetadata = 0; + SheepMetadata = a_Entity.GetFurColor(); // Fur colour + + if (a_Entity.IsSheared()) // Is sheared? + { + SheepMetadata |= 0x16; + } + WriteByte(SheepMetadata); + } + else if (a_Entity.IsA("cEnderman")) + { + WriteByte(0x10); + WriteByte(a_Entity.CarriedBlock()); // Stolen block + WriteByte(0x11); + WriteByte(a_Entity.CarriedMeta()); // Stolen metea + WriteByte(0x12); + WriteByte(a_Entity.IsScream() ? 1 : 0); // I HATE YER FACE, I SCWEAM AT YER FACE + } + else if (a_Entity.IsA("cSkeleton")) + { + WriteByte(0xD); + WriteByte(a_Entity.IsWither() ? 1 : 0); // It's a skeleton, but it's not + } + else if (a_Entity.IsA("cWitch")) + { + WriteByte(0x15); + WriteByte(a_Entity.IsNosey() ? 1 : 0); // Drinking-nose: like Squidward + } + else if ((a_Entity.IsA("cSlime")) || (a_Entity.IsA("cMagmaCube"))) + { + WriteByte(0x10); + WriteByte(a_Entity.GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + } + else if (a_Entity.IsA("cHorse")) + { + int Flags = 0; + if (a_Entity.IsTame()) + { + Flags |= 0x2; + } + if (a_Entity.IsSaddled()) + { + Flags |= 0x4; + } + if (a_Entity.IsChested()) + { + Flags |= 0x8; + } + if (a_Entity.IsBabby()) + { + Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer + } + if (a_Entity.IsEating()) + { + Flags |= 0x20; + } + if (a_Entity.IsRearing()) + { + Flags |= 0x40; + } + if (a_Entity.IsMthOpen()) + { + Flags |= 0x80; + } + WriteByte(0x50); + WriteInt(Flags); + + WriteByte(0x13); + WriteByte(a_Entity.GetHType()); // Type of horse (donkey, chestnut, etc.) + + WriteByte(0x54); + int Appearance = 0; + Appearance = a_Entity.GetHColor(); // Mask FF + Appearance |= a_Entity.GetHStyle() * 256; // Mask FF00, so multiply by 256 + WriteInt(Appearance); + + WriteByte(0x56); + WriteInt(a_Entity.GetHArmour()); // Horshey armour } - return Flags; + + // End Specific Metadata + // End Metadata Packet + + WriteByte(0x7f); } diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index c5c8cd1a0..7b493881b 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -142,11 +142,8 @@ protected: /// Parses one item, "slot" as the protocol wiki calls it, from m_ReceivedData; returns the usual ParsePacket() codes virtual int ParseItem(cItem & a_Item); - /// Returns the entity metadata representation - AString GetEntityMetaData(const cEntity & a_Entity); - - /// Returns the entity common metadata, index 0 (generic flags) - char GetEntityMetadataFlags(const cEntity & a_Entity); + /// Writes the entity metadata + void WriteMetadata(const cEntity & a_Entity); } ; diff --git a/source/Protocol/Protocol132.cpp b/source/Protocol/Protocol132.cpp index a06eb0b8b..63b838b70 100644 --- a/source/Protocol/Protocol132.cpp +++ b/source/Protocol/Protocol132.cpp @@ -416,8 +416,7 @@ void cProtocol132::SendSpawnMob(const cMonster & a_Mob) WriteShort ((short)(a_Mob.GetSpeedX() * 400)); WriteShort ((short)(a_Mob.GetSpeedY() * 400)); WriteShort ((short)(a_Mob.GetSpeedZ() * 400)); - AString MetaData = GetEntityMetaData(a_Mob); - SendData (MetaData.data(), MetaData.size()); + WriteMetadata(a_Mob); Flush(); } -- cgit v1.2.3 From 7401fc000dca2a3ff3ce61776f84e5c2d8eb1868 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 8 Oct 2013 22:21:55 +0100 Subject: Initial round of fixes * Fixed intentional misspelling of baby! :D * Better chested horse bool name * Fixed some weird continuity issues with my recent changes not being pushed up initially * Fixed derpy hexadecimal values --- source/Protocol/Protocol125.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 89b2c15ec..e2309c295 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -1638,11 +1638,11 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) } if (a_Entity.IsRclking()) { - CommonMetadata |= 0x16; + CommonMetadata |= 0x10; } if (a_Entity.IsInvisible()) { - CommonMetadata |= 0x32; + CommonMetadata |= 0x20; } WriteByte(0x0); @@ -1691,7 +1691,7 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) else if (a_Entity.IsA("cZombie")) { WriteByte(0xC); - WriteByte(a_Entity.IsBabby() ? 1 : 0); // Babby zombie? + WriteByte(a_Entity.IsBaby() ? 1 : 0); // Babby zombie? WriteByte(0xD); WriteByte(a_Entity.IsVillZomb() ? 1 : 0); // Converted zombie? WriteByte(0xE); @@ -1784,7 +1784,7 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) { Flags |= 0x8; } - if (a_Entity.IsBabby()) + if (a_Entity.IsBaby()) { Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer } -- cgit v1.2.3 From fe6fa23a97421af3d02b9faf92b8df2f73abb556 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 9 Oct 2013 21:02:59 +0100 Subject: Second round of fixes * Implemented suggestions --- source/Protocol/Protocol125.cpp | 98 ++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 36 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index e2309c295..0f5f3a616 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -24,8 +24,27 @@ Documentation: #include "../UI/Window.h" #include "../Root.h" #include "../Server.h" + +#include "../Entities/ProjectileEntity.h" +#include "../Entities/Minecart.h" #include "../Entities/FallingBlock.h" +#include "../Mobs/Monster.h" +#include "../Mobs/Creeper.h" +#include "../Mobs/Bat.h" +#include "../Mobs/Pig.h" +#include "../Mobs/Villager.h" +#include "../Mobs/Zombie.h" +#include "../Mobs/Ghast.h" +#include "../Mobs/Wolf.h" +#include "../Mobs/Sheep.h" +#include "../Mobs/Enderman.h" +#include "../Mobs/Skeleton.h" +#include "../Mobs/Witch.h" +#include "../Mobs/Slime.h" +#include "../Mobs/Magmacube.h" +#include "../Mobs/Horse.h" + @@ -1655,70 +1674,70 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) { WriteByte(0x51); // No idea how Mojang makes their carts shakey shakey, so here is a complicated one-liner expression that does something similar - WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * a_Entity.LastDamage()) * 4 ); + WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((cMinecart &)a_Entity).LastDamage()) * 4 ); WriteByte(0x52); WriteInt(1); // Shaking direction, doesn't seem to affect anything WriteByte(0x73); - WriteFloat((float)(a_Entity.LastDamage() + 10)); // Damage taken / shake effect multiplyer + WriteFloat((float)(((cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer } else if (a_Entity.IsA("cCreeper")) { WriteByte(0x10); - WriteByte(a_Entity.IsBlowing() ? 1 : -1); // Blowing up? + WriteByte(((cCreeper &)a_Entity).IsBlowing() ? 1 : -1); // Blowing up? WriteByte(0x11); - WriteByte(a_Entity.IsCharged() ? 1 : 0); // Lightning-charged? + WriteByte(((cCreeper &)a_Entity).IsCharged() ? 1 : 0); // Lightning-charged? } else if (a_Entity.IsA("cMinecartWithFurnace")) { WriteByte(0x10); - WriteByte(a_Entity.IsFueled() ? 1 : 0); // Fueled? + WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? } else if (a_Entity.IsA("cBat")) { WriteByte(0x10); - WriteByte(a_Entity.IsHanging() ? 1 : 0); // Upside down? + WriteByte(((const cBat &)a_Entity).IsHanging() ? 1 : 0); // Upside down? } else if (a_Entity.IsA("cPig")) { WriteByte(0x10); - WriteByte(a_Entity.IsSaddled() ? 1 : 0); // Saddled? + WriteByte(((const cPig &)a_Entity).IsSaddled() ? 1 : 0); // Saddled? } else if (a_Entity.IsA("cVillager")) { WriteByte(0x50); - WriteInt(a_Entity.GetVilType()); // What sort of TESTIFICATE? + WriteInt(((const cVillager &)a_Entity).GetVilType()); // What sort of TESTIFICATE? } else if (a_Entity.IsA("cZombie")) { WriteByte(0xC); - WriteByte(a_Entity.IsBaby() ? 1 : 0); // Babby zombie? + WriteByte(((const cZombie &)a_Entity).IsBaby() ? 1 : 0); // Babby zombie? WriteByte(0xD); - WriteByte(a_Entity.IsVillZomb() ? 1 : 0); // Converted zombie? + WriteByte(((const cZombie &)a_Entity).IsVillagerZombie() ? 1 : 0); // Converted zombie? WriteByte(0xE); - WriteByte(a_Entity.IsConvert() ? 1 : 0); // Converted-but-converting-back zombllager? + WriteByte(((const cZombie &)a_Entity).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? } else if (a_Entity.IsA("cGhast")) { WriteByte(0x10); - WriteByte(a_Entity.IsCharging()); // About to eject un flamé-bol? :P + WriteByte(((const cGhast &)a_Entity).IsCharging()); // About to eject un flamé-bol? :P } else if (a_Entity.IsA("cArrowEntity")) { WriteByte(0x10); - WriteByte(a_Entity.IsCritical() ? 1 : 0); // Critical hitting arrow? + WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); // Critical hitting arrow? } else if (a_Entity.IsA("cWolf")) { Byte WolfStatus = 0; - if (a_Entity.IsSitting()) + if (((const cWolf &)a_Entity).IsSitting()) { WolfStatus |= 0x1; } - if (a_Entity.IsAngry()) + if (((const cWolf &)a_Entity).IsAngry()) { WolfStatus |= 0x2; } - if (a_Entity.IsTame()) + if (((const cWolf &)a_Entity).IsTame()) { WolfStatus |= 0x4; } @@ -1728,7 +1747,7 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteByte(0x72); WriteFloat((float)(a_Entity.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) WriteByte(0x13); - WriteByte(a_Entity.IsBegging() ? 1 : 0); // Ultra cute mode? + WriteByte(((const cWolf &)a_Entity).IsBegging() ? 1 : 0); // Ultra cute mode? } else if (a_Entity.IsA("cSheep")) { @@ -1737,9 +1756,9 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteByte(0x10); Byte SheepMetadata = 0; - SheepMetadata = a_Entity.GetFurColor(); // Fur colour + SheepMetadata = ((const cSheep &)a_Entity).GetFurColor(); // Fur colour - if (a_Entity.IsSheared()) // Is sheared? + if (((const cSheep &)a_Entity).IsSheared()) // Is sheared? { SheepMetadata |= 0x16; } @@ -1748,55 +1767,62 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) else if (a_Entity.IsA("cEnderman")) { WriteByte(0x10); - WriteByte(a_Entity.CarriedBlock()); // Stolen block + WriteByte((Byte)(((cEnderman &)a_Entity).GetCarriedBlock())); // Block that he stole from your house WriteByte(0x11); - WriteByte(a_Entity.CarriedMeta()); // Stolen metea + WriteByte((Byte)(((cEnderman &)a_Entity).GetCarriedMeta())); // Meta of block that he stole from your house WriteByte(0x12); - WriteByte(a_Entity.IsScream() ? 1 : 0); // I HATE YER FACE, I SCWEAM AT YER FACE + WriteByte(((cEnderman &)a_Entity).IsScreaming() ? 1 : 0); // Screaming at your face? } else if (a_Entity.IsA("cSkeleton")) { WriteByte(0xD); - WriteByte(a_Entity.IsWither() ? 1 : 0); // It's a skeleton, but it's not + WriteByte(((const cSkeleton &)a_Entity).IsWither() ? 1 : 0); // It's a skeleton, but it's not } else if (a_Entity.IsA("cWitch")) { WriteByte(0x15); - WriteByte(a_Entity.IsNosey() ? 1 : 0); // Drinking-nose: like Squidward + WriteByte(((cWitch &)a_Entity).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything } else if ((a_Entity.IsA("cSlime")) || (a_Entity.IsA("cMagmaCube"))) { WriteByte(0x10); - WriteByte(a_Entity.GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + if (a_Entity.IsA("cSlime")) + { + WriteByte(((const cSlime &)a_Entity).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + } + else + { + WriteByte(((const cMagmaCube &)a_Entity).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + } } else if (a_Entity.IsA("cHorse")) { int Flags = 0; - if (a_Entity.IsTame()) + if (((const cHorse &)a_Entity).IsTame()) { Flags |= 0x2; } - if (a_Entity.IsSaddled()) + if (((const cHorse &)a_Entity).IsSaddled()) { Flags |= 0x4; } - if (a_Entity.IsChested()) + if (((const cHorse &)a_Entity).IsChested()) { Flags |= 0x8; } - if (a_Entity.IsBaby()) + if (((const cHorse &)a_Entity).IsBaby()) { Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer } - if (a_Entity.IsEating()) + if (((const cHorse &)a_Entity).IsEating()) { Flags |= 0x20; } - if (a_Entity.IsRearing()) + if (((const cHorse &)a_Entity).IsRearing()) { Flags |= 0x40; } - if (a_Entity.IsMthOpen()) + if (((const cHorse &)a_Entity).IsMthOpen()) { Flags |= 0x80; } @@ -1804,16 +1830,16 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteInt(Flags); WriteByte(0x13); - WriteByte(a_Entity.GetHType()); // Type of horse (donkey, chestnut, etc.) + WriteByte(((const cHorse &)a_Entity).GetHType()); // Type of horse (donkey, chestnut, etc.) WriteByte(0x54); int Appearance = 0; - Appearance = a_Entity.GetHColor(); // Mask FF - Appearance |= a_Entity.GetHStyle() * 256; // Mask FF00, so multiply by 256 + Appearance = ((const cHorse &)a_Entity).GetHColor(); // Mask FF + Appearance |= ((const cHorse &)a_Entity).GetHStyle() * 256; // Mask FF00, so multiply by 256 WriteInt(Appearance); WriteByte(0x56); - WriteInt(a_Entity.GetHArmour()); // Horshey armour + WriteInt(((const cHorse &)a_Entity).GetHArmour()); // Horshey armour } // End Specific Metadata -- cgit v1.2.3 From d7b2c534fd2e272c328b176432c922fd11a7cd85 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 11 Oct 2013 00:41:54 +0100 Subject: Third round of fixes * Split WriteMetadata into three functions for common, entity, and mob * Edited a few mob sizes to Vanilla values --- source/Protocol/Protocol125.cpp | 157 ++++++++++++++++++++++------------------ source/Protocol/Protocol125.h | 10 ++- source/Protocol/Protocol132.cpp | 6 +- 3 files changed, 101 insertions(+), 72 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 0f5f3a616..96a4e57ff 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -363,7 +363,16 @@ void cProtocol125::SendEntityMetadata(const cEntity & a_Entity) WriteByte(PACKET_METADATA); WriteInt (a_Entity.GetUniqueID()); - WriteMetadata(a_Entity); + WriteCommonMetadata(a_Entity); + if (a_Entity.IsMob()) + { + WriteMobMetadata(((const cMonster &)a_Entity)); + } + else + { + WriteEntityMetadata(a_Entity); + } + WriteByte(0x7f); Flush(); } @@ -730,7 +739,11 @@ void cProtocol125::SendSpawnMob(const cMonster & a_Mob) WriteByte (0); WriteByte (0); WriteByte (0); - WriteMetadata(a_Mob); + + WriteCommonMetadata(a_Mob); + WriteMobMetadata(a_Mob); + WriteByte(0x7f); + Flush(); } @@ -1633,10 +1646,8 @@ int cProtocol125::ParseItem(cItem & a_Item) -void cProtocol125::WriteMetadata(const cEntity & a_Entity) +void cProtocol125::WriteCommonMetadata(const cEntity & a_Entity) { - // Common Metadata - Byte CommonMetadata = 0; if (a_Entity.IsOnFire()) @@ -1666,78 +1677,91 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteByte(0x0); WriteByte(CommonMetadata); +} + + - // Common Metadata End - // Specific Entity Metadata + +void cProtocol125::WriteEntityMetadata(const cEntity & a_Entity) +{ if (a_Entity.IsMinecart()) { WriteByte(0x51); // No idea how Mojang makes their carts shakey shakey, so here is a complicated one-liner expression that does something similar - WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((cMinecart &)a_Entity).LastDamage()) * 4 ); + WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4 ); WriteByte(0x52); WriteInt(1); // Shaking direction, doesn't seem to affect anything WriteByte(0x73); - WriteFloat((float)(((cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer + WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer + + if (a_Entity.IsA("cMinecartWithFurnace")) + { + WriteByte(0x10); + WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? + } } - else if (a_Entity.IsA("cCreeper")) +} + + + + + +void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) +{ + if (a_Mob.GetMobType() == E_META_SPAWN_EGG_CREEPER) { WriteByte(0x10); - WriteByte(((cCreeper &)a_Entity).IsBlowing() ? 1 : -1); // Blowing up? + WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); // Blowing up? WriteByte(0x11); - WriteByte(((cCreeper &)a_Entity).IsCharged() ? 1 : 0); // Lightning-charged? - } - else if (a_Entity.IsA("cMinecartWithFurnace")) - { - WriteByte(0x10); - WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? + WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); // Lightning-charged? } - else if (a_Entity.IsA("cBat")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_BAT) { WriteByte(0x10); - WriteByte(((const cBat &)a_Entity).IsHanging() ? 1 : 0); // Upside down? + WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); // Upside down? } - else if (a_Entity.IsA("cPig")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_PIG) { WriteByte(0x10); - WriteByte(((const cPig &)a_Entity).IsSaddled() ? 1 : 0); // Saddled? + WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); // Saddled? } - else if (a_Entity.IsA("cVillager")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_VILLAGER) { WriteByte(0x50); - WriteInt(((const cVillager &)a_Entity).GetVilType()); // What sort of TESTIFICATE? + WriteInt(((const cVillager &)a_Mob).GetVilType()); // What sort of TESTIFICATE? } - else if (a_Entity.IsA("cZombie")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ZOMBIE) { WriteByte(0xC); - WriteByte(((const cZombie &)a_Entity).IsBaby() ? 1 : 0); // Babby zombie? + WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); // Babby zombie? WriteByte(0xD); - WriteByte(((const cZombie &)a_Entity).IsVillagerZombie() ? 1 : 0); // Converted zombie? + WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); // Converted zombie? WriteByte(0xE); - WriteByte(((const cZombie &)a_Entity).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? + WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? } - else if (a_Entity.IsA("cGhast")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_GHAST) { WriteByte(0x10); - WriteByte(((const cGhast &)a_Entity).IsCharging()); // About to eject un flamé-bol? :P + WriteByte(((const cGhast &)a_Mob).IsCharging()); // About to eject un flamé-bol? :P } - else if (a_Entity.IsA("cArrowEntity")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ARROW) { WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); // Critical hitting arrow? + WriteByte(((const cArrowEntity &)a_Mob).IsCritical() ? 1 : 0); // Critical hitting arrow? } - else if (a_Entity.IsA("cWolf")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_WOLF) { Byte WolfStatus = 0; - if (((const cWolf &)a_Entity).IsSitting()) + if (((const cWolf &)a_Mob).IsSitting()) { WolfStatus |= 0x1; } - if (((const cWolf &)a_Entity).IsAngry()) + if (((const cWolf &)a_Mob).IsAngry()) { WolfStatus |= 0x2; } - if (((const cWolf &)a_Entity).IsTame()) + if (((const cWolf &)a_Mob).IsTame()) { WolfStatus |= 0x4; } @@ -1745,84 +1769,84 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteByte(WolfStatus); WriteByte(0x72); - WriteFloat((float)(a_Entity.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) + WriteFloat((float)(a_Mob.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) WriteByte(0x13); - WriteByte(((const cWolf &)a_Entity).IsBegging() ? 1 : 0); // Ultra cute mode? + WriteByte(((const cWolf &)a_Mob).IsBegging() ? 1 : 0); // Ultra cute mode? } - else if (a_Entity.IsA("cSheep")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SHEEP) { // [1](1111) // [] = Is sheared? () = Color, from 0 to 15 WriteByte(0x10); Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Entity).GetFurColor(); // Fur colour + SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); // Fur colour - if (((const cSheep &)a_Entity).IsSheared()) // Is sheared? + if (((const cSheep &)a_Mob).IsSheared()) // Is sheared? { SheepMetadata |= 0x16; } WriteByte(SheepMetadata); } - else if (a_Entity.IsA("cEnderman")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ENDERMAN) { WriteByte(0x10); - WriteByte((Byte)(((cEnderman &)a_Entity).GetCarriedBlock())); // Block that he stole from your house + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); // Block that he stole from your house WriteByte(0x11); - WriteByte((Byte)(((cEnderman &)a_Entity).GetCarriedMeta())); // Meta of block that he stole from your house + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); // Meta of block that he stole from your house WriteByte(0x12); - WriteByte(((cEnderman &)a_Entity).IsScreaming() ? 1 : 0); // Screaming at your face? + WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); // Screaming at your face? } - else if (a_Entity.IsA("cSkeleton")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SKELETON) { WriteByte(0xD); - WriteByte(((const cSkeleton &)a_Entity).IsWither() ? 1 : 0); // It's a skeleton, but it's not + WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); // It's a skeleton, but it's not } - else if (a_Entity.IsA("cWitch")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_WITCH) { WriteByte(0x15); - WriteByte(((cWitch &)a_Entity).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything + WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything } - else if ((a_Entity.IsA("cSlime")) || (a_Entity.IsA("cMagmaCube"))) + else if ((a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) || (a_Mob.GetMobType() == E_META_SPAWN_EGG_MAGMA_CUBE)) { WriteByte(0x10); - if (a_Entity.IsA("cSlime")) + if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) { - WriteByte(((const cSlime &)a_Entity).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + WriteByte(((const cSlime &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME } else { - WriteByte(((const cMagmaCube &)a_Entity).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + WriteByte(((const cMagmaCube &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME } } - else if (a_Entity.IsA("cHorse")) + else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_HORSE) { int Flags = 0; - if (((const cHorse &)a_Entity).IsTame()) + if (((const cHorse &)a_Mob).IsTame()) { Flags |= 0x2; } - if (((const cHorse &)a_Entity).IsSaddled()) + if (((const cHorse &)a_Mob).IsSaddled()) { Flags |= 0x4; } - if (((const cHorse &)a_Entity).IsChested()) + if (((const cHorse &)a_Mob).IsChested()) { Flags |= 0x8; } - if (((const cHorse &)a_Entity).IsBaby()) + if (((const cHorse &)a_Mob).IsBaby()) { Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer } - if (((const cHorse &)a_Entity).IsEating()) + if (((const cHorse &)a_Mob).IsEating()) { Flags |= 0x20; } - if (((const cHorse &)a_Entity).IsRearing()) + if (((const cHorse &)a_Mob).IsRearing()) { Flags |= 0x40; } - if (((const cHorse &)a_Entity).IsMthOpen()) + if (((const cHorse &)a_Mob).IsMthOpen()) { Flags |= 0x80; } @@ -1830,22 +1854,17 @@ void cProtocol125::WriteMetadata(const cEntity & a_Entity) WriteInt(Flags); WriteByte(0x13); - WriteByte(((const cHorse &)a_Entity).GetHType()); // Type of horse (donkey, chestnut, etc.) + WriteByte(((const cHorse &)a_Mob).GetHType()); // Type of horse (donkey, chestnut, etc.) WriteByte(0x54); int Appearance = 0; - Appearance = ((const cHorse &)a_Entity).GetHColor(); // Mask FF - Appearance |= ((const cHorse &)a_Entity).GetHStyle() * 256; // Mask FF00, so multiply by 256 + Appearance = ((const cHorse &)a_Mob).GetHColor(); // Mask FF + Appearance |= ((const cHorse &)a_Mob).GetHStyle() * 256; // Mask FF00, so multiply by 256 WriteInt(Appearance); WriteByte(0x56); - WriteInt(((const cHorse &)a_Entity).GetHArmour()); // Horshey armour + WriteInt(((const cHorse &)a_Mob).GetHArmour()); // Horshey armour } - - // End Specific Metadata - // End Metadata Packet - - WriteByte(0x7f); } diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index 7b493881b..ae198780c 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -142,8 +142,14 @@ protected: /// Parses one item, "slot" as the protocol wiki calls it, from m_ReceivedData; returns the usual ParsePacket() codes virtual int ParseItem(cItem & a_Item); - /// Writes the entity metadata - void WriteMetadata(const cEntity & a_Entity); + /// Writes the COMMON entity metadata + void WriteCommonMetadata(const cEntity & a_Entity); + + /// Writes normal entity metadata + void WriteEntityMetadata(const cEntity & a_Entity); + + /// Writes mobile entity metadata + void WriteMobMetadata(const cMonster & a_Mob); } ; diff --git a/source/Protocol/Protocol132.cpp b/source/Protocol/Protocol132.cpp index 63b838b70..53159a3b3 100644 --- a/source/Protocol/Protocol132.cpp +++ b/source/Protocol/Protocol132.cpp @@ -416,7 +416,11 @@ void cProtocol132::SendSpawnMob(const cMonster & a_Mob) WriteShort ((short)(a_Mob.GetSpeedX() * 400)); WriteShort ((short)(a_Mob.GetSpeedY() * 400)); WriteShort ((short)(a_Mob.GetSpeedZ() * 400)); - WriteMetadata(a_Mob); + + WriteCommonMetadata(a_Mob); + WriteMobMetadata(a_Mob); + WriteByte(0x7f); + Flush(); } -- cgit v1.2.3 From ee2df34d03313ecf98110384559f46f00a05978b Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 11 Oct 2013 01:00:16 +0100 Subject: Fourth round of fixes * Switchified WriteMobMetadata * Renamed Horse functions to be better --- source/Protocol/Protocol125.cpp | 268 +++++++++++++++++++++------------------- 1 file changed, 143 insertions(+), 125 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 96a4e57ff..6d6101fb2 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -1709,161 +1709,179 @@ void cProtocol125::WriteEntityMetadata(const cEntity & a_Entity) void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) { - if (a_Mob.GetMobType() == E_META_SPAWN_EGG_CREEPER) + switch (a_Mob.GetMobType()) { - WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); // Blowing up? - WriteByte(0x11); - WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); // Lightning-charged? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_BAT) - { - WriteByte(0x10); - WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); // Upside down? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_PIG) - { - WriteByte(0x10); - WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); // Saddled? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_VILLAGER) - { - WriteByte(0x50); - WriteInt(((const cVillager &)a_Mob).GetVilType()); // What sort of TESTIFICATE? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ZOMBIE) - { - WriteByte(0xC); - WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); // Babby zombie? - WriteByte(0xD); - WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); // Converted zombie? - WriteByte(0xE); - WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_GHAST) - { - WriteByte(0x10); - WriteByte(((const cGhast &)a_Mob).IsCharging()); // About to eject un flamé-bol? :P - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ARROW) - { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Mob).IsCritical() ? 1 : 0); // Critical hitting arrow? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_WOLF) - { - Byte WolfStatus = 0; - if (((const cWolf &)a_Mob).IsSitting()) + case E_META_SPAWN_EGG_CREEPER: { - WolfStatus |= 0x1; + WriteByte(0x10); + WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); // Blowing up? + WriteByte(0x11); + WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); // Lightning-charged? + break; } - if (((const cWolf &)a_Mob).IsAngry()) + case E_META_SPAWN_EGG_BAT: { - WolfStatus |= 0x2; + WriteByte(0x10); + WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); // Upside down? + break; } - if (((const cWolf &)a_Mob).IsTame()) + case E_META_SPAWN_EGG_PIG: { - WolfStatus |= 0x4; + WriteByte(0x10); + WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); // Saddled? + break; } - WriteByte(0x10); - WriteByte(WolfStatus); - - WriteByte(0x72); - WriteFloat((float)(a_Mob.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) - WriteByte(0x13); - WriteByte(((const cWolf &)a_Mob).IsBegging() ? 1 : 0); // Ultra cute mode? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SHEEP) - { - // [1](1111) - // [] = Is sheared? () = Color, from 0 to 15 - - WriteByte(0x10); - Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); // Fur colour - - if (((const cSheep &)a_Mob).IsSheared()) // Is sheared? + case E_META_SPAWN_EGG_VILLAGER: { - SheepMetadata |= 0x16; + WriteByte(0x50); + WriteInt(((const cVillager &)a_Mob).GetVilType()); // What sort of TESTIFICATE? + break; } - WriteByte(SheepMetadata); - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_ENDERMAN) - { - WriteByte(0x10); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); // Block that he stole from your house - WriteByte(0x11); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); // Meta of block that he stole from your house - WriteByte(0x12); - WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); // Screaming at your face? - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SKELETON) - { - WriteByte(0xD); - WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); // It's a skeleton, but it's not - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_WITCH) - { - WriteByte(0x15); - WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything - } - else if ((a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) || (a_Mob.GetMobType() == E_META_SPAWN_EGG_MAGMA_CUBE)) - { - WriteByte(0x10); - if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) + case E_META_SPAWN_EGG_ZOMBIE: { - WriteByte(((const cSlime &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + WriteByte(0xC); + WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); // Babby zombie? + WriteByte(0xD); + WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); // Converted zombie? + WriteByte(0xE); + WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? + break; } - else + case E_META_SPAWN_EGG_GHAST: { - WriteByte(((const cMagmaCube &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + WriteByte(0x10); + WriteByte(((const cGhast &)a_Mob).IsCharging()); // About to eject un flamé-bol? :P + break; } - } - else if (a_Mob.GetMobType() == E_META_SPAWN_EGG_HORSE) - { - int Flags = 0; - if (((const cHorse &)a_Mob).IsTame()) + case E_META_SPAWN_EGG_ARROW: { - Flags |= 0x2; + WriteByte(0x10); + WriteByte(((const cArrowEntity &)a_Mob).IsCritical() ? 1 : 0); // Critical hitting arrow? + break; } - if (((const cHorse &)a_Mob).IsSaddled()) + case E_META_SPAWN_EGG_WOLF: { - Flags |= 0x4; + Byte WolfStatus = 0; + if (((const cWolf &)a_Mob).IsSitting()) + { + WolfStatus |= 0x1; + } + if (((const cWolf &)a_Mob).IsAngry()) + { + WolfStatus |= 0x2; + } + if (((const cWolf &)a_Mob).IsTame()) + { + WolfStatus |= 0x4; + } + WriteByte(0x10); + WriteByte(WolfStatus); + + WriteByte(0x72); + WriteFloat((float)(a_Mob.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) + WriteByte(0x13); + WriteByte(((const cWolf &)a_Mob).IsBegging() ? 1 : 0); // Ultra cute mode? + break; } - if (((const cHorse &)a_Mob).IsChested()) + case E_META_SPAWN_EGG_SHEEP: { - Flags |= 0x8; + // [1](1111) + // [] = Is sheared? () = Color, from 0 to 15 + + WriteByte(0x10); + Byte SheepMetadata = 0; + SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); // Fur colour + + if (((const cSheep &)a_Mob).IsSheared()) // Is sheared? + { + SheepMetadata |= 0x16; + } + WriteByte(SheepMetadata); + break; } - if (((const cHorse &)a_Mob).IsBaby()) + case E_META_SPAWN_EGG_ENDERMAN: { - Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer + WriteByte(0x10); + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); // Block that he stole from your house + WriteByte(0x11); + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); // Meta of block that he stole from your house + WriteByte(0x12); + WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); // Screaming at your face? + break; } - if (((const cHorse &)a_Mob).IsEating()) + case E_META_SPAWN_EGG_SKELETON: { - Flags |= 0x20; + WriteByte(0xD); + WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); // It's a skeleton, but it's not + break; } - if (((const cHorse &)a_Mob).IsRearing()) + case E_META_SPAWN_EGG_WITCH: { - Flags |= 0x40; + WriteByte(0x15); + WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything + break; } - if (((const cHorse &)a_Mob).IsMthOpen()) + case E_META_SPAWN_EGG_SLIME: + case E_META_SPAWN_EGG_MAGMA_CUBE: { - Flags |= 0x80; + WriteByte(0x10); + if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) + { + WriteByte(((const cSlime &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + } + else + { + WriteByte(((const cMagmaCube &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME + } + break; } - WriteByte(0x50); - WriteInt(Flags); + case E_META_SPAWN_EGG_HORSE: + { + int Flags = 0; + if (((const cHorse &)a_Mob).IsTame()) + { + Flags |= 0x2; + } + if (((const cHorse &)a_Mob).IsSaddled()) + { + Flags |= 0x4; + } + if (((const cHorse &)a_Mob).IsChested()) + { + Flags |= 0x8; + } + if (((const cHorse &)a_Mob).IsBaby()) + { + Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer + } + if (((const cHorse &)a_Mob).IsEating()) + { + Flags |= 0x20; + } + if (((const cHorse &)a_Mob).IsRearing()) + { + Flags |= 0x40; + } + if (((const cHorse &)a_Mob).IsMthOpen()) + { + Flags |= 0x80; + } + WriteByte(0x50); + WriteInt(Flags); - WriteByte(0x13); - WriteByte(((const cHorse &)a_Mob).GetHType()); // Type of horse (donkey, chestnut, etc.) + WriteByte(0x13); + WriteByte(((const cHorse &)a_Mob).GetHorseType()); // Type of horse (donkey, chestnut, etc.) - WriteByte(0x54); - int Appearance = 0; - Appearance = ((const cHorse &)a_Mob).GetHColor(); // Mask FF - Appearance |= ((const cHorse &)a_Mob).GetHStyle() * 256; // Mask FF00, so multiply by 256 - WriteInt(Appearance); + WriteByte(0x54); + int Appearance = 0; + Appearance = ((const cHorse &)a_Mob).GetHorseColor(); // Mask FF + Appearance |= ((const cHorse &)a_Mob).GetHorseStyle() * 256; // Mask FF00, so multiply by 256 + WriteInt(Appearance); - WriteByte(0x56); - WriteInt(((const cHorse &)a_Mob).GetHArmour()); // Horshey armour + WriteByte(0x56); + WriteInt(((const cHorse &)a_Mob).GetHorseArmour()); // Horshey armour + break; + } } } -- cgit v1.2.3 From d0acb37aedb280f0589275ace342ea6565f80aaa Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 12 Oct 2013 23:05:15 +0100 Subject: Seventh round of fixes * Fixed arrows not critical-effecting because they were in MOBS! (derp) * Used cMonster::mtXX as per xoft's suggestions --- source/Protocol/Protocol125.cpp | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 6d6101fb2..4730c3dfc 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -1701,6 +1701,12 @@ void cProtocol125::WriteEntityMetadata(const cEntity & a_Entity) WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? } } + else if (a_Entity.IsA("cArrowEntity")); + { + WriteByte(0x10); + WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); // Critical hitting arrow? + } + } @@ -1711,7 +1717,7 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) { switch (a_Mob.GetMobType()) { - case E_META_SPAWN_EGG_CREEPER: + case cMonster::mtCreeper: { WriteByte(0x10); WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); // Blowing up? @@ -1719,25 +1725,25 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); // Lightning-charged? break; } - case E_META_SPAWN_EGG_BAT: + case cMonster::mtBat: { WriteByte(0x10); WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); // Upside down? break; } - case E_META_SPAWN_EGG_PIG: + case cMonster::mtPig: { WriteByte(0x10); WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); // Saddled? break; } - case E_META_SPAWN_EGG_VILLAGER: + case cMonster::mtVillager: { WriteByte(0x50); WriteInt(((const cVillager &)a_Mob).GetVilType()); // What sort of TESTIFICATE? break; } - case E_META_SPAWN_EGG_ZOMBIE: + case cMonster::mtZombie: { WriteByte(0xC); WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); // Babby zombie? @@ -1747,19 +1753,13 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? break; } - case E_META_SPAWN_EGG_GHAST: + case cMonster::mtGhast: { WriteByte(0x10); WriteByte(((const cGhast &)a_Mob).IsCharging()); // About to eject un flamé-bol? :P break; } - case E_META_SPAWN_EGG_ARROW: - { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Mob).IsCritical() ? 1 : 0); // Critical hitting arrow? - break; - } - case E_META_SPAWN_EGG_WOLF: + case cMonster::mtWolf: { Byte WolfStatus = 0; if (((const cWolf &)a_Mob).IsSitting()) @@ -1783,7 +1783,7 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) WriteByte(((const cWolf &)a_Mob).IsBegging() ? 1 : 0); // Ultra cute mode? break; } - case E_META_SPAWN_EGG_SHEEP: + case cMonster::mtSheep: { // [1](1111) // [] = Is sheared? () = Color, from 0 to 15 @@ -1799,7 +1799,7 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) WriteByte(SheepMetadata); break; } - case E_META_SPAWN_EGG_ENDERMAN: + case cMonster::mtEnderman: { WriteByte(0x10); WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); // Block that he stole from your house @@ -1809,23 +1809,23 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); // Screaming at your face? break; } - case E_META_SPAWN_EGG_SKELETON: + case cMonster::mtSkeleton: { WriteByte(0xD); WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); // It's a skeleton, but it's not break; } - case E_META_SPAWN_EGG_WITCH: + case cMonster::mtWitch: { WriteByte(0x15); WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything break; } - case E_META_SPAWN_EGG_SLIME: - case E_META_SPAWN_EGG_MAGMA_CUBE: + case cMonster::mtSlime: + case cMonster::mtMagmaCube: { WriteByte(0x10); - if (a_Mob.GetMobType() == E_META_SPAWN_EGG_SLIME) + if (a_Mob.GetMobType() == cMonster::mtSlime) { WriteByte(((const cSlime &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME } @@ -1835,7 +1835,7 @@ void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) } break; } - case E_META_SPAWN_EGG_HORSE: + case cMonster::mtHorse: { int Flags = 0; if (((const cHorse &)a_Mob).IsTame()) -- cgit v1.2.3 From d8d2f35e9dd354fba14f8d6512e818d18d2066c2 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 13 Oct 2013 12:47:55 +0100 Subject: Eight round of fixes * Changed IsA() to *long if statement* - Removed deprecated values in Entity.h - to blazes with the plugins! * Renamed villager type enumerations to be LESS SHOUTY and more vt-y + Use vtMax for World.cpp testificate spawning --- source/Protocol/Protocol125.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 4730c3dfc..62863cd48 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -1695,18 +1695,17 @@ void cProtocol125::WriteEntityMetadata(const cEntity & a_Entity) WriteByte(0x73); WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer - if (a_Entity.IsA("cMinecartWithFurnace")) + if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) { WriteByte(0x10); WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? } } - else if (a_Entity.IsA("cArrowEntity")); + else if ((a_Entity.IsProjectile() && ((cProjectileEntity &)a_Entity).GetProjectileKind() == cProjectileEntity::pkArrow)); { WriteByte(0x10); WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); // Critical hitting arrow? } - } -- cgit v1.2.3 From d3db97301b58f761b5754224d4dad4eff49cafbf Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 23 Oct 2013 11:06:39 +0200 Subject: Removed cRoot:m_PrimaryServerVersion from Lua API. We have the accessor methods for it. --- source/Protocol/ProtocolRecognizer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index fe99b22e1..ceada1944 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -727,7 +727,7 @@ bool cProtocolRecognizer::TryRecognizeProtocol(void) void cProtocolRecognizer::HandleServerPing(void) { AString Reply; - switch (cRoot::Get()->m_PrimaryServerVersion) + switch (cRoot::Get()->GetPrimaryServerVersion()) { case PROTO_VERSION_1_2_5: case PROTO_VERSION_1_3_2: @@ -771,8 +771,8 @@ void cProtocolRecognizer::HandleServerPing(void) Printf(MaxPlayers, "%d", cRoot::Get()->GetServer()->GetMaxPlayers()); AString ProtocolVersionNum; - Printf(ProtocolVersionNum, "%d", cRoot::Get()->m_PrimaryServerVersion); - AString ProtocolVersionTxt(GetVersionTextFromInt(cRoot::Get()->m_PrimaryServerVersion)); + Printf(ProtocolVersionNum, "%d", cRoot::Get()->GetPrimaryServerVersion()); + AString ProtocolVersionTxt(GetVersionTextFromInt(cRoot::Get()->GetPrimaryServerVersion())); // Cannot use Printf() because of in-string NUL bytes. Reply = cChatColor::Delimiter; -- cgit v1.2.3 From df20c19986805380cfd728d63f2e3003331b1665 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 28 Oct 2013 13:30:24 +0100 Subject: Renamed cWindow constants to include the "wt" prefix. --- source/Protocol/Protocol125.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index fb7315468..ef40f265a 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -963,7 +963,7 @@ void cProtocol125::SendWholeInventory(const cWindow & a_Window) void cProtocol125::SendWindowClose(const cWindow & a_Window) { - if (a_Window.GetWindowType() == cWindow::Inventory) + if (a_Window.GetWindowType() == cWindow::wtInventory) { // Do not send inventory-window-close return; -- cgit v1.2.3 From dab398d5d663332527e57a7e239d223f33f4eb77 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 30 Oct 2013 23:24:46 +0100 Subject: Added 1.7 to protocol recognizer. The 1.7 protocol currently only reports server description and playercount. --- source/Protocol/Protocol.h | 21 ++++ source/Protocol/Protocol125.h | 4 +- source/Protocol/Protocol17x.cpp | 216 +++++++++++++++++++++++++++++++++ source/Protocol/Protocol17x.h | 75 ++++++++++++ source/Protocol/ProtocolRecognizer.cpp | 120 ++++++++++++++++-- source/Protocol/ProtocolRecognizer.h | 22 +++- 6 files changed, 447 insertions(+), 11 deletions(-) create mode 100644 source/Protocol/Protocol17x.cpp create mode 100644 source/Protocol/Protocol17x.h (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h index 5071f5961..466cf874b 100644 --- a/source/Protocol/Protocol.h +++ b/source/Protocol/Protocol.h @@ -186,6 +186,27 @@ protected: WriteInt(a_Vector.y); WriteInt(a_Vector.z); } + + void WriteVarInt(UInt32 a_Value) + { + // A 32-bit integer can be encoded by at most 5 bytes: + unsigned char b[5]; + int idx = 0; + do + { + b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00); + a_Value = a_Value >> 7; + idx++; + } while (a_Value > 0); + + SendData((const char *)b, idx); + } + + void WriteVarUTF8String(const AString & a_String) + { + WriteVarInt(a_String.size()); + SendData(a_String.data(), a_String.size()); + } } ; diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index ae198780c..da3f81444 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -92,9 +92,9 @@ protected: PARSE_INCOMPLETE = -3, } ; - cByteBuffer m_ReceivedData; //< Buffer for the received data + cByteBuffer m_ReceivedData; ///< Buffer for the received data - AString m_Username; //< Stored in ParseHandshake(), compared to Login username + AString m_Username; ///< Stored in ParseHandshake(), compared to Login username virtual void SendData(const char * a_Data, int a_Size) override; diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp new file mode 100644 index 000000000..b93a95e12 --- /dev/null +++ b/source/Protocol/Protocol17x.cpp @@ -0,0 +1,216 @@ + +// Protocol17x.cpp + +/* +Implements the 1.7.x protocol classes: + - cProtocol172 + - release 1.7.2 protocol (#4) +(others may be added later in the future for the 1.7 release series) +*/ + +#include "Globals.h" +#include "Protocol17x.h" +#include "../ClientHandle.h" +#include "../Root.h" +#include "../Server.h" + + + + + +cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : + super(a_Client), + m_ServerAddress(a_ServerAddress), + m_ServerPort(a_ServerPort), + m_State(a_State), + m_ReceivedData(32 KiB), + m_IsEncrypted(false) +{ +} + + + + + +void cProtocol172::DataReceived(const char * a_Data, int a_Size) +{ + if (m_IsEncrypted) + { + byte Decrypted[512]; + while (a_Size > 0) + { + int NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; + m_Decryptor.ProcessData(Decrypted, (byte *)a_Data, NumBytes); + AddReceivedData((const char *)Decrypted, NumBytes); + a_Size -= NumBytes; + a_Data += NumBytes; + } + } + else + { + AddReceivedData(a_Data, a_Size); + } +} + + + + +void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) +{ + if (!m_ReceivedData.Write(a_Data, a_Size)) + { + // Too much data in the incoming queue, report to caller: + m_Client->PacketBufferFull(); + return; + } + + // Handle all complete packets: + while (true) + { + UInt32 PacketLen; + if (!m_ReceivedData.ReadVarInt(PacketLen)) + { + // Not enough data + return; + } + if (!m_ReceivedData.CanReadBytes(PacketLen)) + { + // The full packet hasn't been received yet + return; + } + UInt32 PacketType; + UInt32 NumBytesRead = m_ReceivedData.GetReadableSpace(); + if (!m_ReceivedData.ReadVarInt(PacketType)) + { + // Not enough data + return; + } + NumBytesRead -= m_ReceivedData.GetReadableSpace(); + HandlePacket(PacketType, PacketLen - NumBytesRead); + } // while (true) +} + + + + +void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) +{ + switch (m_State) + { + case 1: + { + // Status + switch (a_PacketType) + { + case 0x00: HandlePacketStatusRequest(a_RemainingBytes); return; + } + break; + } + + case 2: + { + // Login + break; + } + + case 3: + { + // Game + break; + } + } // switch (m_State) + + // Unknown packet type, report to the client: + m_Client->PacketUnknown(a_PacketType); + m_ReceivedData.SkipRead(a_RemainingBytes); + m_ReceivedData.CommitRead(); +} + + + + + +void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) +{ + // No more bytes in this packet + ASSERT(a_RemainingBytes == 0); + m_ReceivedData.CommitRead(); + + // Send the response: + AString Response = "{\"version\":{\"name\":\"1.7.2\",\"protocol\":4},\"players\":{"; + AppendPrintf(Response, "\"max\":%u,\"online\":%u,\"sample\":[]},", + cRoot::Get()->GetServer()->GetMaxPlayers(), + cRoot::Get()->GetServer()->GetNumPlayers() + ); + AppendPrintf(Response, "\"description\":{\"text\":\"%s\"}", + cRoot::Get()->GetServer()->GetDescription().c_str() + ); + Response.append("}"); + + cByteBuffer Packet(Response.size() + 10); + Packet.WriteVarInt(0x00); // Response packet + Packet.WriteVarUTF8String(Response); + WritePacket(Packet); +} + + + + + +void cProtocol172::WritePacket(cByteBuffer & a_Packet) +{ + cCSLock Lock(m_CSPacket); + AString Pkt; + a_Packet.ReadAll(Pkt); + WriteVarInt(Pkt.size()); + SendData(Pkt.data(), Pkt.size()); + Flush(); +} + + + + + +void cProtocol172::SendData(const char * a_Data, int a_Size) +{ + m_DataToSend.append(a_Data, a_Size); +} + + + + + +void cProtocol172::Flush(void) +{ + ASSERT(m_CSPacket.IsLockedByCurrentThread()); // Did all packets lock the CS properly? + + if (m_DataToSend.empty()) + { + LOGD("Flushing empty"); + return; + } + const char * a_Data = m_DataToSend.data(); + int a_Size = m_DataToSend.size(); + if (m_IsEncrypted) + { + byte Encrypted[8192]; // Larger buffer, we may be sending lots of data (chunks) + while (a_Size > 0) + { + int NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; + m_Encryptor.ProcessData(Encrypted, (byte *)a_Data, NumBytes); + m_Client->SendData((const char *)Encrypted, NumBytes); + a_Size -= NumBytes; + a_Data += NumBytes; + } + } + else + { + m_Client->SendData(a_Data, a_Size); + } + m_DataToSend.clear(); +} + + + + + diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h new file mode 100644 index 000000000..cea39f073 --- /dev/null +++ b/source/Protocol/Protocol17x.h @@ -0,0 +1,75 @@ + +// Protocol17x.h + +/* +Declares the 1.7.x protocol classes: + - cProtocol172 + - release 1.7.2 protocol (#4) +(others may be added later in the future for the 1.7 release series) +*/ + + + + + +#pragma once + +#include "Protocol16x.h" + + + + + +class cProtocol172 : + public cProtocol162 // TODO +{ + typedef cProtocol162 super; // TODO + +public: + + cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); + + /// Called when client sends some data: + virtual void DataReceived(const char * a_Data, int a_Size) override; + +protected: + + AString m_ServerAddress; + + UInt16 m_ServerPort; + + /// State of the protocol. 1 = status, 2 = login + UInt32 m_State; + + /// Buffer for the received data + cByteBuffer m_ReceivedData; + + bool m_IsEncrypted; + CryptoPP::CFB_Mode::Decryption m_Decryptor; + CryptoPP::CFB_Mode::Encryption m_Encryptor; + + /// (Unencrypted) data to be sent to the client. Written by SendData, cleared by Flush() + AString m_DataToSend; + + + /// Adds the received (unencrypted) data to m_ReceivedData, parses complete packets + void AddReceivedData(const char * a_Data, int a_Size); + + /// Reads and handles the packet. The packet length and type have already been read. + void HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes); + + void HandlePacketStatusRequest(UInt32 a_RemainingBytes); + + /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. + void WritePacket(cByteBuffer & a_Packet); + + /// Adds unencrypted data to the outgoing data buffer + virtual void SendData(const char * a_Data, int a_Size) override; + + /// Flushes m_DataToSend through the optional encryption into the outgoing socket data + virtual void Flush(void) override; +} ; + + + + diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index ceada1944..18e9186b2 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -12,6 +12,7 @@ #include "Protocol14x.h" #include "Protocol15x.h" #include "Protocol16x.h" +#include "Protocol17x.h" #include "../ClientHandle.h" #include "../Root.h" #include "../Server.h" @@ -667,11 +668,65 @@ bool cProtocolRecognizer::TryRecognizeProtocol(void) } switch (PacketType) { - case 0x02: break; // Handshake, continue recognizing - case 0xfe: HandleServerPing(); return false; - default: return false; + case 0x02: return TryRecognizeLengthlessProtocol(); // Handshake, continue recognizing + case 0xfe: + { + // This may be either a packet length or the length-less Ping packet + Byte NextByte; + if (!m_Buffer.ReadByte(NextByte)) + { + // Not enough data for either protocol + // This could actually happen with the 1.2 / 1.3 client, but their support is fading out anyway + return false; + } + if (NextByte != 0x01) + { + // This is definitely NOT a length-less Ping packet, handle as lengthed protocol: + break; + } + if (!m_Buffer.ReadByte(NextByte)) + { + // There is no more data. Although this *could* mean TCP fragmentation, it is highly unlikely + // and rather this is a 1.4 client sending a regular Ping packet (without the following Plugin message) + SendLengthlessServerPing(); + return false; + } + if (NextByte == 0xfa) + { + // Definitely a length-less Ping followed by a Plugin message + SendLengthlessServerPing(); + return false; + } + // Definitely a lengthed Initial handshake, handle below: + break; + } + } // switch (PacketType) + + // This must be a lengthed protocol, try if it has the entire initial handshake packet: + m_Buffer.ResetRead(); + UInt32 PacketLen; + UInt32 ReadSoFar = m_Buffer.GetReadableSpace(); + if (!m_Buffer.ReadVarInt(PacketLen)) + { + // Not enough bytes for the packet length, keep waiting + return false; } - + ReadSoFar -= m_Buffer.GetReadableSpace(); + if (!m_Buffer.CanReadBytes(PacketLen)) + { + // Not enough bytes for the packet, keep waiting + return false; + } + return TryRecognizeLengthedProtocol(PacketLen - ReadSoFar); +} + + + + + +bool cProtocolRecognizer::TryRecognizeLengthlessProtocol(void) +{ + // The comm started with 0x02, which is a Handshake packet in the length-less protocol family // 1.3.2 starts with 0x02 0x39 // 1.2.5 starts with 0x02 and name is expected to less than 0x3900 long :) char ch; @@ -724,7 +779,56 @@ bool cProtocolRecognizer::TryRecognizeProtocol(void) -void cProtocolRecognizer::HandleServerPing(void) +bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRemaining) +{ + UInt32 PacketType; + UInt32 NumBytesRead = m_Buffer.GetReadableSpace(); + if (!m_Buffer.ReadVarInt(PacketType)) + { + return false; + } + if (PacketType != 0x00) + { + // Not an initial handshake packet, we don't know how to talk to them + LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, initial packet %u)", + m_Client->GetIPString().c_str(), PacketType + ); + m_Client->Kick("Unsupported protocol version"); + return false; + } + UInt32 ProtocolVersion; + if (!m_Buffer.ReadVarInt(ProtocolVersion)) + { + return false; + } + NumBytesRead -= m_Buffer.GetReadableSpace(); + switch (ProtocolVersion) + { + case PROTO_VERSION_1_7_2: + { + AString ServerAddress; + short ServerPort; + UInt32 NextState; + m_Buffer.ReadVarUTF8String(ServerAddress); + m_Buffer.ReadBEShort(ServerPort); + m_Buffer.ReadVarInt(NextState); + m_Buffer.CommitRead(); + m_Protocol = new cProtocol172(m_Client, ServerAddress, ServerPort, NextState); + return true; + } + } + LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u)", + m_Client->GetIPString().c_str(), ProtocolVersion + ); + m_Client->Kick("Unsupported protocol version"); + return false; +} + + + + + +void cProtocolRecognizer::SendLengthlessServerPing(void) { AString Reply; switch (cRoot::Get()->GetPrimaryServerVersion()) @@ -757,10 +861,12 @@ void cProtocolRecognizer::HandleServerPing(void) // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3101#Server_List_Ping_.280xFE.29 // _X 2012_10_31: I know that this needn't eat the byte, since it still may be in transit. // Who cares? We're disconnecting anyway. - if (m_Buffer.CanReadBytes(1)) + m_Buffer.ResetRead(); + if (m_Buffer.CanReadBytes(2)) { byte val; - m_Buffer.ReadByte(val); + m_Buffer.ReadByte(val); // Packet type - Serverlist ping + m_Buffer.ReadByte(val); // 0x01 magic value ASSERT(val == 0x01); } diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index c53288230..4c473a269 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -47,6 +47,9 @@ public: PROTO_VERSION_NEXT, PROTO_VERSION_LATEST = PROTO_VERSION_NEXT - 1, ///< Automatically assigned to the last protocol version, this serves as the default for PrimaryServerVersion + + // These will be kept "under" the next / latest, because the next and latest are only needed for previous protocols + PROTO_VERSION_1_7_2 = 4, } ; cProtocolRecognizer(cClientHandle * a_Client); @@ -124,8 +127,23 @@ protected: /// Tries to recognize protocol based on m_Buffer contents; returns true if recognized bool TryRecognizeProtocol(void); - /// Called when the recognizer gets a server ping packet; responds with server stats and destroys the client - void HandleServerPing(void); + /** Tries to recognize a protocol in the length-less family, based on m_Buffer; returns true if recognized. + Handles protocols before release 1.7, that didn't include packet lengths, and started with a 0x02 handshake packet + Note that length-less server ping is handled directly in TryRecognizeProtocol(), this function is called only + when the 0x02 Handshake packet has been received + */ + bool TryRecognizeLengthlessProtocol(void); + + /** Tries to recognize a protocol in the leghted family (1.7+), based on m_Buffer; returns true if recognized. + The packet length and type have already been read, type is 0 + The number of bytes remaining in the packet is passed as a_PacketLengthRemaining + **/ + bool TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRemaining); + + /** Called when the recognizer gets a length-less protocol's server ping packet + Responds with server stats and destroys the client. + */ + void SendLengthlessServerPing(void); } ; -- cgit v1.2.3 From feaea31b787070f4ea45c4829d4833d728a15d6f Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 30 Oct 2013 23:38:55 +0100 Subject: Protocol 1.7: Added status ping handling. --- source/Protocol/Protocol17x.cpp | 25 +++++++++++++++++++++++++ source/Protocol/Protocol17x.h | 2 ++ 2 files changed, 27 insertions(+) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index b93a95e12..aef1cc8c2 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -103,6 +103,7 @@ void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) switch (a_PacketType) { case 0x00: HandlePacketStatusRequest(a_RemainingBytes); return; + case 0x01: HandlePacketStatusPing (a_RemainingBytes); return; } break; } @@ -157,6 +158,30 @@ void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) +void cProtocol172::HandlePacketStatusPing(UInt32 a_RemainingBytes) +{ + ASSERT(a_RemainingBytes == 8); + if (a_RemainingBytes != 8) + { + m_Client->PacketError(0x01); + m_ReceivedData.SkipRead(a_RemainingBytes); + m_ReceivedData.CommitRead(); + return; + } + Int64 Timestamp; + m_ReceivedData.ReadBEInt64(Timestamp); + m_ReceivedData.CommitRead(); + + cByteBuffer Packet(18); + Packet.WriteVarInt(0x01); + Packet.WriteBEInt64(Timestamp); + WritePacket(Packet); +} + + + + + void cProtocol172::WritePacket(cByteBuffer & a_Packet) { cCSLock Lock(m_CSPacket); diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index cea39f073..e5597ee0b 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -58,7 +58,9 @@ protected: /// Reads and handles the packet. The packet length and type have already been read. void HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes); + // Packet handlers while in the Status state (m_State == 1) void HandlePacketStatusRequest(UInt32 a_RemainingBytes); + void HandlePacketStatusPing (UInt32 a_RemainingBytes); /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. void WritePacket(cByteBuffer & a_Packet); -- cgit v1.2.3 From 7a77986d88c9ec0bedd17e243d875e7c6190c3ee Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 31 Oct 2013 23:48:43 +0100 Subject: Protocol 1.7: Added skeleton functions for reading client packets. Untested, this is skeleton code only. --- source/Protocol/Protocol17x.cpp | 334 ++++++++++++++++++++++++++++++++++++++-- source/Protocol/Protocol17x.h | 35 ++++- 2 files changed, 349 insertions(+), 20 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index aef1cc8c2..b3e4540c7 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -13,6 +13,15 @@ Implements the 1.7.x protocol classes: #include "../ClientHandle.h" #include "../Root.h" #include "../Server.h" +#include "../Entities/Player.h" + + + + + +#define HANDLE_PACKET_READ(Proc, Type, Var) \ + Type Var; \ + m_ReceivedData.Proc(Var); @@ -79,14 +88,25 @@ void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) return; } UInt32 PacketType; - UInt32 NumBytesRead = m_ReceivedData.GetReadableSpace(); + UInt32 Mark1 = m_ReceivedData.GetReadableSpace(); if (!m_ReceivedData.ReadVarInt(PacketType)) { // Not enough data return; } - NumBytesRead -= m_ReceivedData.GetReadableSpace(); + UInt32 NumBytesRead = Mark1 - m_ReceivedData.GetReadableSpace(); HandlePacket(PacketType, PacketLen - NumBytesRead); + + if (Mark1 - m_ReceivedData.GetReadableSpace() > PacketLen) + { + // Read more than packet length, report as error + m_Client->PacketError(PacketType); + } + + // Go to packet end in any case: + m_ReceivedData.ResetRead(); + m_ReceivedData.SkipRead(PacketLen); + m_ReceivedData.CommitRead(); } // while (true) } @@ -111,12 +131,43 @@ void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) case 2: { // Login + switch (a_PacketType) + { + case 0x00: HandlePacketLoginStart(a_RemainingBytes); return; + case 0x01: HandlePacketLoginEncryptionResponse(a_RemainingBytes); return; + } break; } case 3: { // Game + switch (a_PacketType) + { + case 0x00: HandlePacketKeepAlive (a_RemainingBytes); break; + case 0x01: HandlePacketChatMessage (a_RemainingBytes); break; + case 0x02: HandlePacketUseEntity (a_RemainingBytes); break; + case 0x03: HandlePacketPlayer (a_RemainingBytes); break; + case 0x04: HandlePacketPlayerPos (a_RemainingBytes); break; + case 0x05: HandlePacketPlayerLook (a_RemainingBytes); break; + case 0x06: HandlePacketPlayerPosLook (a_RemainingBytes); break; + case 0x07: HandlePacketBlockDig (a_RemainingBytes); break; + case 0x08: HandlePacketBlockPlace (a_RemainingBytes); break; + case 0x09: HandlePacketSlotSelect (a_RemainingBytes); break; + case 0x0a: HandlePacketAnimation (a_RemainingBytes); break; + case 0x0b: HandlePacketEntityAction (a_RemainingBytes); break; + case 0x0c: HandlePacketSteerVehicle (a_RemainingBytes); break; + case 0x0d: HandlePacketWindowClose (a_RemainingBytes); break; + case 0x0e: HandlePacketWindowClick (a_RemainingBytes); break; + case 0x0f: // Confirm transaction - not used in MCS + case 0x10: HandlePacketCreativeInventoryAction(a_RemainingBytes); break; + case 0x12: HandlePacketUpdateSign (a_RemainingBytes); break; + case 0x13: HandlePacketPlayerAbilities (a_RemainingBytes); break; + case 0x14: HandlePacketTabComplete (a_RemainingBytes); break; + case 0x15: HandlePacketClientSettings (a_RemainingBytes); break; + case 0x16: HandlePacketClientStatus (a_RemainingBytes); break; + case 0x17: HandlePacketPluginMessage (a_RemainingBytes); break; + } break; } } // switch (m_State) @@ -131,6 +182,30 @@ void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) +void cProtocol172::HandlePacketStatusPing(UInt32 a_RemainingBytes) +{ + ASSERT(a_RemainingBytes == 8); + if (a_RemainingBytes != 8) + { + m_Client->PacketError(0x01); + m_ReceivedData.SkipRead(a_RemainingBytes); + m_ReceivedData.CommitRead(); + return; + } + Int64 Timestamp; + m_ReceivedData.ReadBEInt64(Timestamp); + m_ReceivedData.CommitRead(); + + cByteBuffer Packet(18); + Packet.WriteVarInt(0x01); + Packet.WriteBEInt64(Timestamp); + WritePacket(Packet); +} + + + + + void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) { // No more bytes in this packet @@ -158,24 +233,249 @@ void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) -void cProtocol172::HandlePacketStatusPing(UInt32 a_RemainingBytes) +void cProtocol172::HandlePacketLoginEncryptionResponse(UInt32 a_RemainingBytes) { - ASSERT(a_RemainingBytes == 8); - if (a_RemainingBytes != 8) - { - m_Client->PacketError(0x01); - m_ReceivedData.SkipRead(a_RemainingBytes); - m_ReceivedData.CommitRead(); - return; - } - Int64 Timestamp; - m_ReceivedData.ReadBEInt64(Timestamp); - m_ReceivedData.CommitRead(); + // TODO: Add protocol encryption +} + + + + + +void cProtocol172::HandlePacketLoginStart(UInt32 a_RemainingBytes) +{ + AString Username; + m_ReceivedData.ReadVarUTF8String(Username); - cByteBuffer Packet(18); - Packet.WriteVarInt(0x01); - Packet.WriteBEInt64(Timestamp); + // TODO: Protocol encryption should be set up here if not localhost / auth + + // Send login success: + cByteBuffer Packet(Username.size() + 30); + Packet.WriteVarInt(0x02); // Login success packet + Packet.WriteVarUTF8String(Printf("%d", m_Client->GetUniqueID())); // TODO: UUID + Packet.WriteVarUTF8String(Username); WritePacket(Packet); + + m_Client->HandleLogin(4, Username); +} + + + + + +void cProtocol172::HandlePacketAnimation(UInt32 a_RemainingBytes) +{ + HANDLE_PACKET_READ(ReadBEInt, int, EntityID); + HANDLE_PACKET_READ(ReadByte, Byte, Animation); + m_Client->HandleAnimation(Animation); +} + + + + + +void cProtocol172::HandlePacketBlockDig(UInt32 a_RemainingBytes) +{ + HANDLE_PACKET_READ(ReadByte, Byte, Status); + HANDLE_PACKET_READ(ReadBEInt, int, BlockX); + HANDLE_PACKET_READ(ReadByte, Byte, BlockY); + HANDLE_PACKET_READ(ReadBEInt, int, BlockZ); + HANDLE_PACKET_READ(ReadByte, Byte, Face); + m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, Face, Status); +} + + + + + +void cProtocol172::HandlePacketBlockPlace(UInt32 a_RemainingBytes) +{ + HANDLE_PACKET_READ(ReadBEInt, int, BlockX); + HANDLE_PACKET_READ(ReadByte, Byte, BlockY); + HANDLE_PACKET_READ(ReadBEInt, int, BlockZ); + HANDLE_PACKET_READ(ReadByte, Byte, Face); + HANDLE_PACKET_READ(ReadByte, Byte, CursorX); + HANDLE_PACKET_READ(ReadByte, Byte, CursorY); + HANDLE_PACKET_READ(ReadByte, Byte, CursorZ); + m_Client->HandleRightClick(BlockX, BlockY, BlockZ, Face, CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); +} + + + + + +void cProtocol172::HandlePacketChatMessage(UInt32 a_RemainingBytes) +{ + HANDLE_PACKET_READ(ReadVarUTF8String, AString, Message); + m_Client->HandleChat(Message); +} + + + + + +void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) +{ + HANDLE_PACKET_READ(ReadVarUTF8String, AString, Locale); + HANDLE_PACKET_READ(ReadByte, Byte, ViewDistance); + HANDLE_PACKET_READ(ReadByte, Byte, ChatFlags); + HANDLE_PACKET_READ(ReadByte, Byte, Unused); + HANDLE_PACKET_READ(ReadByte, Byte, Difficulty); + HANDLE_PACKET_READ(ReadByte, Byte, ShowCape); + // TODO +} + + + + + +void cProtocol172::HandlePacketClientStatus(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketEntityAction(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketKeepAlive(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPlayer(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPlayerAbilities(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPlayerLook(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPlayerPos(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPlayerPosLook(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketPluginMessage(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketSlotSelect(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketSteerVehicle(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketTabComplete(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketUpdateSign(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketUseEntity(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketWindowClick(UInt32 a_RemainingBytes) +{ + // TODO +} + + + + + +void cProtocol172::HandlePacketWindowClose(UInt32 a_RemainingBytes) +{ + // TODO } diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index e5597ee0b..bc197235b 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -38,7 +38,7 @@ protected: UInt16 m_ServerPort; - /// State of the protocol. 1 = status, 2 = login + /// State of the protocol. 1 = status, 2 = login, 3 = game UInt32 m_State; /// Buffer for the received data @@ -58,9 +58,38 @@ protected: /// Reads and handles the packet. The packet length and type have already been read. void HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes); - // Packet handlers while in the Status state (m_State == 1) - void HandlePacketStatusRequest(UInt32 a_RemainingBytes); + // Packet handlers while in the Status state (m_State == 1): void HandlePacketStatusPing (UInt32 a_RemainingBytes); + void HandlePacketStatusRequest(UInt32 a_RemainingBytes); + + // Packet handlers while in the Login state (m_State == 2): + void HandlePacketLoginEncryptionResponse(UInt32 a_RemainingBytes); + void HandlePacketLoginStart (UInt32 a_RemainingBytes); + + // Packet handlers while in the Game state (m_State == 3): + void HandlePacketAnimation (UInt32 a_RemainingBytes); + void HandlePacketBlockDig (UInt32 a_RemainingBytes); + void HandlePacketBlockPlace (UInt32 a_RemainingBytes); + void HandlePacketChatMessage (UInt32 a_RemainingBytes); + void HandlePacketClientSettings (UInt32 a_RemainingBytes); + void HandlePacketClientStatus (UInt32 a_RemainingBytes); + void HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes); + void HandlePacketEntityAction (UInt32 a_RemainingBytes); + void HandlePacketKeepAlive (UInt32 a_RemainingBytes); + void HandlePacketPlayer (UInt32 a_RemainingBytes); + void HandlePacketPlayerAbilities (UInt32 a_RemainingBytes); + void HandlePacketPlayerLook (UInt32 a_RemainingBytes); + void HandlePacketPlayerPos (UInt32 a_RemainingBytes); + void HandlePacketPlayerPosLook (UInt32 a_RemainingBytes); + void HandlePacketPluginMessage (UInt32 a_RemainingBytes); + void HandlePacketSlotSelect (UInt32 a_RemainingBytes); + void HandlePacketSteerVehicle (UInt32 a_RemainingBytes); + void HandlePacketTabComplete (UInt32 a_RemainingBytes); + void HandlePacketUpdateSign (UInt32 a_RemainingBytes); + void HandlePacketUseEntity (UInt32 a_RemainingBytes); + void HandlePacketWindowClick (UInt32 a_RemainingBytes); + void HandlePacketWindowClose (UInt32 a_RemainingBytes); + /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. void WritePacket(cByteBuffer & a_Packet); -- cgit v1.2.3 From b6faeaba184976eb48aeb0370169029eba3343e9 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 1 Nov 2013 16:20:15 +0100 Subject: Protocol 1.7: Implemented the first batch of sent packets. --- source/Protocol/Protocol.h | 1 + source/Protocol/Protocol125.h | 1 + source/Protocol/Protocol17x.cpp | 577 +++++++++++++++++++++++++++++++++ source/Protocol/Protocol17x.h | 71 +++- source/Protocol/ProtocolRecognizer.cpp | 10 + source/Protocol/ProtocolRecognizer.h | 1 + 6 files changed, 658 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h index 466cf874b..6bea4edbb 100644 --- a/source/Protocol/Protocol.h +++ b/source/Protocol/Protocol.h @@ -77,6 +77,7 @@ public: virtual void SendKeepAlive (int a_PingID) = 0; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) = 0; virtual void SendPickupSpawn (const cPickup & a_Pickup) = 0; + virtual void SendPlayerAbilities (void) = 0; virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) = 0; virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) = 0; virtual void SendPlayerMaxSpeed (void) = 0; ///< Informs the client of the maximum player speed (1.6.1+) diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index da3f81444..1da50a1d4 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -54,6 +54,7 @@ public: virtual void SendKeepAlive (int a_PingID) override; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; virtual void SendPickupSpawn (const cPickup & a_Pickup) override; + virtual void SendPlayerAbilities (void) override {} // This protocol doesn't support such message virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; virtual void SendPlayerMaxSpeed (void) override; diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index b3e4540c7..d4dbfb564 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -14,6 +14,9 @@ Implements the 1.7.x protocol classes: #include "../Root.h" #include "../Server.h" #include "../Entities/Player.h" +#include "../World.h" +#include "ChunkDataSerializer.h" +#include "../Entities/Pickup.h" @@ -64,6 +67,579 @@ void cProtocol172::DataReceived(const char * a_Data, int a_Size) + +void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) +{ + cByteBuffer Packet(20); + Packet.WriteVarInt(0x1b); // Attach Entity packet + Packet.WriteBEInt(a_Entity.GetUniqueID()); + Packet.WriteBEInt((a_Vehicle != NULL) ? a_Vehicle->GetUniqueID() : 0); + Packet.WriteBool(false); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) +{ + cByteBuffer Packet(30); + Packet.WriteVarInt(0x24); // Block Action packet + Packet.WriteBEInt(a_BlockX); + Packet.WriteBEShort(a_BlockY); + Packet.WriteBEInt(a_BlockZ); + Packet.WriteChar(a_Byte1); + Packet.WriteChar(a_Byte2); + Packet.WriteVarInt(a_BlockType); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) +{ + cByteBuffer Packet(20); + Packet.WriteVarInt(0x24); // Block Action packet + Packet.WriteBEInt(a_EntityID); + Packet.WriteBEInt(a_BlockX); + Packet.WriteBEInt(a_BlockY); + Packet.WriteBEInt(a_BlockZ); + Packet.WriteChar(a_Stage); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cByteBuffer Packet(20); + Packet.WriteVarInt(0x23); // Block Change packet + Packet.WriteBEInt(a_BlockX); + Packet.WriteByte(a_BlockY); + Packet.WriteBEInt(a_BlockZ); + Packet.WriteVarInt(a_BlockType); + Packet.WriteByte(a_BlockMeta); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) +{ + cByteBuffer Packet(20 + a_Changes.size() * 4); + Packet.WriteVarInt(0x22); // Multi Block Change packet + Packet.WriteBEInt(a_ChunkX); + Packet.WriteBEInt(a_ChunkZ); + Packet.WriteBEShort((short)a_Changes.size()); + Packet.WriteBEInt(a_Changes.size() * 4); + for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) + { + unsigned int Coords = itr->y | (itr->z << 8) | (itr->x << 12); + unsigned int Blocks = itr->BlockMeta | (itr->BlockType << 4); + Packet.WriteBEInt((Coords << 16) | Blocks); + } // for itr - a_Changes[] + WritePacket(Packet); +} + + + + + +void cProtocol172::SendChat(const AString & a_Message) +{ + AString Message = Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str()); + cByteBuffer Packet(20 + Message.size()); + Packet.WriteVarInt(0x02); // Chat Message packet + Packet.WriteVarUTF8String(Message); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) +{ + const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); // This contains the flags and bitmasks, too + cByteBuffer Packet(40 + ChunkData.size()); + Packet.WriteVarInt(0x21); // Chunk Data packet + Packet.WriteBEInt(a_ChunkX); + Packet.WriteBEInt(a_ChunkZ); + Packet.Write(ChunkData.data(), ChunkData.size()); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) +{ + cByteBuffer Packet(9); + Packet.WriteVarInt(0x0d); // Collect Item packet + Packet.WriteBEInt(a_Pickup.GetUniqueID()); + Packet.WriteBEInt(a_Player.GetUniqueID()); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) +{ + cByteBuffer Packet(6); + Packet.WriteVarInt(0x13); // Destroy Entities packet + Packet.WriteByte(0); + Packet.WriteBEInt(a_Entity.GetUniqueID()); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendDisconnect(const AString & a_Reason) +{ + cByteBuffer Packet(10 + a_Reason.size()); + Packet.WriteVarInt(0x40); // Disconnect packet + Packet.WriteVarUTF8String(a_Reason); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) ///< Request the client to open up the sign editor for the sign(1.6+) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityLook(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityProperties(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) +{ + // TODO +} + + + + + +void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) +{ + // TODO +} + + + + + +void cProtocol172::SendGameMode(eGameMode a_GameMode) +{ + // TODO +} + + + + + +void cProtocol172::SendHealth(void) +{ + // TODO +} + + + + + +void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item) +{ + // TODO +} + + + + + +void cProtocol172::SendKeepAlive(int a_PingID) +{ + // TODO +} + + + + + +void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) +{ + // Send the spawn position: + cByteBuffer Packet(20); + Packet.WriteVarInt(0x05); // Spawn Position packet + Packet.WriteBEInt((int)a_World.GetSpawnX()); + Packet.WriteBEInt((int)a_World.GetSpawnY()); + Packet.WriteBEInt((int)a_World.GetSpawnZ()); + WritePacket(Packet); + + // Send player abilities: + SendPlayerAbilities(); +} + + + + + +void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) +{ + // TODO +} + + + + + +void cProtocol172::SendPlayerAbilities(void) +{ + cByteBuffer Packet(20); + Packet.WriteVarInt(0x39); // Player Abilities packet + Byte Flags = 0; + if (m_Client->GetPlayer()->IsGameModeCreative()) + { + Flags |= 0x01; + } + // TODO: Other flags (god mode, flying, can fly + Packet.WriteByte(Flags); + // TODO: Packet.WriteBEFloat(m_Client->GetPlayer()->GetMaxFlyingSpeed()); + Packet.WriteBEFloat(0.05f); + Packet.WriteBEFloat((float)m_Client->GetPlayer()->GetMaxSpeed()); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) +{ + // TODO +} + + + + + +void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) +{ + cByteBuffer Packet(10 + a_Player.GetName().size()); + Packet.WriteVarInt(0x38); // Playerlist Item packet + Packet.WriteVarUTF8String(a_Player.GetName()); + Packet.WriteBool(a_IsOnline); + Packet.WriteBEShort(a_Player.GetClientHandle()->GetPing()); + WritePacket(Packet); +} + + + + + +void cProtocol172::SendPlayerMaxSpeed(void) +{ + SendPlayerAbilities(); +} + + + + + +void cProtocol172::SendPlayerMoveLook(void) +{ + // TODO +} + + + + + +void cProtocol172::SendPlayerPosition(void) +{ + // TODO +} + + + + + +void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) +{ + // TODO +} + + + + + +void cProtocol172::SendRespawn(void) +{ + // TODO +} + + + + + +void cProtocol172::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) // a_Src coords are Block * 8 +{ + // TODO +} + + + + + +void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) +{ + // TODO +} + + + + + +void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) +{ + // TODO +} + + + + + +void cProtocol172::SendSpawnMob(const cMonster & a_Mob) +{ + // TODO +} + + + + + +void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) +{ + // TODO +} + + + + + +void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) +{ + // TODO +} + + + + + +void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) +{ + // TODO +} + + + + + +void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) +{ + // TODO +} + + + + + +void cProtocol172::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + // TODO +} + + + + + +void cProtocol172::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) +{ + // TODO +} + + + + + +void cProtocol172::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) +{ + // TODO +} + + + + + +void cProtocol172::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) +{ + // TODO +} + + + + + +void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) +{ + // TODO +} + + + + + +void cProtocol172::SendWeather(eWeather a_Weather) +{ + // TODO +} + + + + + +void cProtocol172::SendWholeInventory(const cInventory & a_Inventory) +{ + // TODO +} + + + + + +void cProtocol172::SendWholeInventory(const cWindow & a_Window) +{ + // TODO +} + + + + + +void cProtocol172::SendWindowClose(const cWindow & a_Window) +{ + // TODO +} + + + + + +void cProtocol172::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +{ + // TODO +} + + + + + +void cProtocol172::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) +{ + // TODO +} + + + + + void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) { if (!m_ReceivedData.Write(a_Data, a_Size)) @@ -539,3 +1115,4 @@ void cProtocol172::Flush(void) + diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index bc197235b..f11d8e2f9 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -14,16 +14,19 @@ Declares the 1.7.x protocol classes: #pragma once -#include "Protocol16x.h" +#include "Protocol.h" +#include "../ByteBuffer.h" +#include "../../CryptoPP/modes.h" +#include "../../CryptoPP/aes.h" class cProtocol172 : - public cProtocol162 // TODO + public cProtocol // TODO { - typedef cProtocol162 super; // TODO + typedef cProtocol super; // TODO public: @@ -32,12 +35,72 @@ public: /// Called when client sends some data: virtual void DataReceived(const char * a_Data, int a_Size) override; + /// Sending stuff to clients (alphabetically sorted): + virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; + virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; + virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; + virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; + virtual void SendChat (const AString & a_Message) override; + virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; + virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) override; + virtual void SendDestroyEntity (const cEntity & a_Entity) override; + virtual void SendDisconnect (const AString & a_Reason) override; + virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) + virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; + virtual void SendEntityHeadLook (const cEntity & a_Entity) override; + virtual void SendEntityLook (const cEntity & a_Entity) override; + virtual void SendEntityMetadata (const cEntity & a_Entity) override; + virtual void SendEntityProperties (const cEntity & a_Entity) override; + virtual void SendEntityRelMove (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; + virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; + virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) override; + virtual void SendEntityVelocity (const cEntity & a_Entity) override; + virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override; + virtual void SendGameMode (eGameMode a_GameMode) override; + virtual void SendHealth (void) override; + virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; + virtual void SendKeepAlive (int a_PingID) override; + virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; + virtual void SendPickupSpawn (const cPickup & a_Pickup) override; + virtual void SendPlayerAbilities (void) override; + virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; + virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; + virtual void SendPlayerMaxSpeed (void) override; + virtual void SendPlayerMoveLook (void) override; + virtual void SendPlayerPosition (void) override; + virtual void SendPlayerSpawn (const cPlayer & a_Player) override; + virtual void SendRespawn (void) override; + virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8 + virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; + virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; + virtual void SendSpawnMob (const cMonster & a_Mob) override; + virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; + virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; + virtual void SendTabCompletionResults(const AStringVector & a_Results) override; + virtual void SendTeleportEntity (const cEntity & a_Entity) override; + virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; + virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; + virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; + virtual void SendWeather (eWeather a_Weather) override; + virtual void SendWholeInventory (const cInventory & a_Inventory) override; + virtual void SendWholeInventory (const cWindow & a_Window) override; + virtual void SendWindowClose (const cWindow & a_Window) override; + virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; + + virtual AString GetAuthServerID(void) override { return m_AuthServerID; } + protected: AString m_ServerAddress; UInt16 m_ServerPort; + AString m_AuthServerID; + /// State of the protocol. 1 = status, 2 = login, 3 = game UInt32 m_State; @@ -99,6 +162,8 @@ protected: /// Flushes m_DataToSend through the optional encryption into the outgoing socket data virtual void Flush(void) override; + + void SendCompass(const cWorld & a_World); } ; diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 18e9186b2..67f924d7e 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -385,6 +385,16 @@ void cProtocolRecognizer::SendPickupSpawn(const cPickup & a_Pickup) +void cProtocolRecognizer::SendPlayerAbilities(void) +{ + ASSERT(m_Protocol != NULL); + m_Protocol->SendPlayerAbilities(); +} + + + + + void cProtocolRecognizer::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) { ASSERT(m_Protocol != NULL); diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 4c473a269..79dc5568f 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -89,6 +89,7 @@ public: virtual void SendKeepAlive (int a_PingID) override; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; virtual void SendPickupSpawn (const cPickup & a_Pickup) override; + virtual void SendPlayerAbilities (void) override; virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; virtual void SendPlayerMaxSpeed (void) override; -- cgit v1.2.3 From 9b84d68d27fb3ce152db04fb9af51e9222f2461c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 3 Nov 2013 11:58:49 +0100 Subject: Protocol 1.7: Rewritten packet-sending to use cPacketizer. Implemented enough of the protocol that the client now spawns in the world (but cannot do anything). --- source/Protocol/Protocol17x.cpp | 475 ++++++++++++++++++++++++++-------------- source/Protocol/Protocol17x.h | 93 +++++++- 2 files changed, 402 insertions(+), 166 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index d4dbfb564..ef6049900 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -17,6 +17,8 @@ Implements the 1.7.x protocol classes: #include "../World.h" #include "ChunkDataSerializer.h" #include "../Entities/Pickup.h" +#include "../WorldStorage/FastNBT.h" +#include "../StringCompression.h" @@ -36,6 +38,8 @@ cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAdd m_ServerPort(a_ServerPort), m_State(a_State), m_ReceivedData(32 KiB), + m_OutPacketBuffer(64 KiB), + m_OutPacketLenBuffer(20), // 20 bytes is more than enough for one VarInt m_IsEncrypted(false) { } @@ -70,12 +74,10 @@ void cProtocol172::DataReceived(const char * a_Data, int a_Size) void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) { - cByteBuffer Packet(20); - Packet.WriteVarInt(0x1b); // Attach Entity packet - Packet.WriteBEInt(a_Entity.GetUniqueID()); - Packet.WriteBEInt((a_Vehicle != NULL) ? a_Vehicle->GetUniqueID() : 0); - Packet.WriteBool(false); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x1b); // Attach Entity packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteInt((a_Vehicle != NULL) ? a_Vehicle->GetUniqueID() : 0); + Pkt.WriteBool(false); } @@ -84,31 +86,27 @@ void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_ void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) { - cByteBuffer Packet(30); - Packet.WriteVarInt(0x24); // Block Action packet - Packet.WriteBEInt(a_BlockX); - Packet.WriteBEShort(a_BlockY); - Packet.WriteBEInt(a_BlockZ); - Packet.WriteChar(a_Byte1); - Packet.WriteChar(a_Byte2); - Packet.WriteVarInt(a_BlockType); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x24); // Block Action packet + Pkt.WriteInt(a_BlockX); + Pkt.WriteShort(a_BlockY); + Pkt.WriteInt(a_BlockZ); + Pkt.WriteByte(a_Byte1); + Pkt.WriteByte(a_Byte2); + Pkt.WriteVarInt(a_BlockType); } -void cProtocol172::SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) +void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) { - cByteBuffer Packet(20); - Packet.WriteVarInt(0x24); // Block Action packet - Packet.WriteBEInt(a_EntityID); - Packet.WriteBEInt(a_BlockX); - Packet.WriteBEInt(a_BlockY); - Packet.WriteBEInt(a_BlockZ); - Packet.WriteChar(a_Stage); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x24); // Block Break Animation packet + Pkt.WriteInt(a_EntityID); + Pkt.WriteInt(a_BlockX); + Pkt.WriteInt(a_BlockY); + Pkt.WriteInt(a_BlockZ); + Pkt.WriteChar(a_Stage); } @@ -117,14 +115,12 @@ void cProtocol172::SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_Block void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { - cByteBuffer Packet(20); - Packet.WriteVarInt(0x23); // Block Change packet - Packet.WriteBEInt(a_BlockX); - Packet.WriteByte(a_BlockY); - Packet.WriteBEInt(a_BlockZ); - Packet.WriteVarInt(a_BlockType); - Packet.WriteByte(a_BlockMeta); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x23); // Block Change packet + Pkt.WriteInt(a_BlockX); + Pkt.WriteByte(a_BlockY); + Pkt.WriteInt(a_BlockZ); + Pkt.WriteVarInt(a_BlockType); + Pkt.WriteByte(a_BlockMeta); } @@ -133,19 +129,17 @@ void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLO void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) { - cByteBuffer Packet(20 + a_Changes.size() * 4); - Packet.WriteVarInt(0x22); // Multi Block Change packet - Packet.WriteBEInt(a_ChunkX); - Packet.WriteBEInt(a_ChunkZ); - Packet.WriteBEShort((short)a_Changes.size()); - Packet.WriteBEInt(a_Changes.size() * 4); + cPacketizer Pkt(*this, 0x22); // Multi Block Change packet + Pkt.WriteInt(a_ChunkX); + Pkt.WriteInt(a_ChunkZ); + Pkt.WriteShort((short)a_Changes.size()); + Pkt.WriteInt(a_Changes.size() * 4); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { unsigned int Coords = itr->y | (itr->z << 8) | (itr->x << 12); unsigned int Blocks = itr->BlockMeta | (itr->BlockType << 4); - Packet.WriteBEInt((Coords << 16) | Blocks); + Pkt.WriteInt((Coords << 16) | Blocks); } // for itr - a_Changes[] - WritePacket(Packet); } @@ -154,11 +148,8 @@ void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV void cProtocol172::SendChat(const AString & a_Message) { - AString Message = Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str()); - cByteBuffer Packet(20 + Message.size()); - Packet.WriteVarInt(0x02); // Chat Message packet - Packet.WriteVarUTF8String(Message); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x02); // Chat Message packet + Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str())); } @@ -168,12 +159,10 @@ void cProtocol172::SendChat(const AString & a_Message) void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) { const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); // This contains the flags and bitmasks, too - cByteBuffer Packet(40 + ChunkData.size()); - Packet.WriteVarInt(0x21); // Chunk Data packet - Packet.WriteBEInt(a_ChunkX); - Packet.WriteBEInt(a_ChunkZ); - Packet.Write(ChunkData.data(), ChunkData.size()); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x21); // Chunk Data packet + Pkt.WriteInt(a_ChunkX); + Pkt.WriteInt(a_ChunkZ); + Pkt.WriteBuf(ChunkData.data(), ChunkData.size()); } @@ -182,11 +171,9 @@ void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerialize void cProtocol172::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) { - cByteBuffer Packet(9); - Packet.WriteVarInt(0x0d); // Collect Item packet - Packet.WriteBEInt(a_Pickup.GetUniqueID()); - Packet.WriteBEInt(a_Player.GetUniqueID()); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x0d); // Collect Item packet + Pkt.WriteInt(a_Pickup.GetUniqueID()); + Pkt.WriteInt(a_Player.GetUniqueID()); } @@ -195,11 +182,9 @@ void cProtocol172::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) { - cByteBuffer Packet(6); - Packet.WriteVarInt(0x13); // Destroy Entities packet - Packet.WriteByte(0); - Packet.WriteBEInt(a_Entity.GetUniqueID()); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x13); // Destroy Entities packet + Pkt.WriteByte(1); + Pkt.WriteInt(a_Entity.GetUniqueID()); } @@ -208,19 +193,20 @@ void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) void cProtocol172::SendDisconnect(const AString & a_Reason) { - cByteBuffer Packet(10 + a_Reason.size()); - Packet.WriteVarInt(0x40); // Disconnect packet - Packet.WriteVarUTF8String(a_Reason); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x40); + Pkt.WriteString(a_Reason); } -void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) ///< Request the client to open up the sign editor for the sign(1.6+) +void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) { - // TODO + cPacketizer Pkt(*this, 0x36); // Sign Editor Open packet + Pkt.WriteInt(a_BlockX); + Pkt.WriteInt(a_BlockY); + Pkt.WriteInt(a_BlockZ); } @@ -229,7 +215,10 @@ void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) ///< void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) { - // TODO + cPacketizer Pkt(*this, 0x04); // Entity Equipment packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteShort(a_SlotNum); + Pkt.WriteItem(a_Item); } @@ -238,7 +227,9 @@ void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) { - // TODO + cPacketizer Pkt(*this, 0x19); // Entity Head Look packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -247,7 +238,10 @@ void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) void cProtocol172::SendEntityLook(const cEntity & a_Entity) { - // TODO + cPacketizer Pkt(*this, 0x16); // Entity Look packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteByteAngle(a_Entity.GetYaw()); + Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -256,7 +250,12 @@ void cProtocol172::SendEntityLook(const cEntity & a_Entity) void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) { + /* // TODO + cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + WriteEntityMetadata(Pkt, a_Entity); + */ } @@ -265,7 +264,11 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) void cProtocol172::SendEntityProperties(const cEntity & a_Entity) { + /* + cPacketizer Pkt(*this, 0x20); // Entity Properties packet + Pkt.WriteInt(a_Entity.GetUniqueID()); // TODO + */ } @@ -274,7 +277,11 @@ void cProtocol172::SendEntityProperties(const cEntity & a_Entity) void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) { - // TODO + cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteByte(a_RelX); + Pkt.WriteByte(a_RelY); + Pkt.WriteByte(a_RelZ); } @@ -283,7 +290,13 @@ void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) { - // TODO + cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteByte(a_RelX); + Pkt.WriteByte(a_RelY); + Pkt.WriteByte(a_RelZ); + Pkt.WriteByteAngle(a_Entity.GetYaw()); + Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -292,7 +305,9 @@ void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) { - // TODO + cPacketizer Pkt(*this, 0x1a); // Entity Status packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteChar(a_Status); } @@ -301,7 +316,12 @@ void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) { - // TODO + cPacketizer Pkt(*this, 0x12); // Entity Velocity packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + // 400 = 8000 / 20 ... Conversion from our speed in m/s to 8000 m/tick + Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); + Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); + Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); } @@ -310,7 +330,21 @@ void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) { - // TODO + cPacketizer Pkt(*this, 0x27); // Explosion packet + Pkt.WriteFloat((float)a_BlockX); + Pkt.WriteFloat((float)a_BlockY); + Pkt.WriteFloat((float)a_BlockZ); + Pkt.WriteFloat((float)a_Radius); + Pkt.WriteInt(a_BlocksAffected.size()); + for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(), end = a_BlocksAffected.end(); itr != end; ++itr) + { + Pkt.WriteChar((char)itr->x); + Pkt.WriteChar((char)itr->y); + Pkt.WriteChar((char)itr->z); + } // for itr - a_BlockAffected[] + Pkt.WriteFloat((float)a_PlayerMotion.x); + Pkt.WriteFloat((float)a_PlayerMotion.y); + Pkt.WriteFloat((float)a_PlayerMotion.z); } @@ -319,7 +353,9 @@ void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_Bloc void cProtocol172::SendGameMode(eGameMode a_GameMode) { - // TODO + cPacketizer Pkt(*this, 0x2b); // Change Game State packet + Pkt.WriteByte(3); // Reason: Change game mode + Pkt.WriteFloat((float)a_GameMode); } @@ -328,7 +364,10 @@ void cProtocol172::SendGameMode(eGameMode a_GameMode) void cProtocol172::SendHealth(void) { - // TODO + cPacketizer Pkt(*this, 0x06); // Update Health packet + Pkt.WriteFloat((float)m_Client->GetPlayer()->GetHealth()); + Pkt.WriteShort(m_Client->GetPlayer()->GetFoodLevel()); + Pkt.WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); } @@ -337,7 +376,10 @@ void cProtocol172::SendHealth(void) void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item) { - // TODO + cPacketizer Pkt(*this, 0x2f); // Set Slot packet + Pkt.WriteChar(a_WindowID); + Pkt.WriteShort(a_SlotNum); + Pkt.WriteItem(a_Item); } @@ -346,7 +388,8 @@ void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt void cProtocol172::SendKeepAlive(int a_PingID) { - // TODO + cPacketizer Pkt(*this, 0x00); // Keep Alive packet + Pkt.WriteInt(a_PingID); } @@ -355,13 +398,24 @@ void cProtocol172::SendKeepAlive(int a_PingID) void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { + // Send the Join Game packet: + { + cPacketizer Pkt(*this, 0x01); // Join Game packet + Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode()); + Pkt.WriteChar((char)a_World.GetDimension()); + Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) + Pkt.WriteByte(cRoot::Get()->GetServer()->GetMaxPlayers()); + Pkt.WriteString("default"); // Level type - wtf? + } + // Send the spawn position: - cByteBuffer Packet(20); - Packet.WriteVarInt(0x05); // Spawn Position packet - Packet.WriteBEInt((int)a_World.GetSpawnX()); - Packet.WriteBEInt((int)a_World.GetSpawnY()); - Packet.WriteBEInt((int)a_World.GetSpawnZ()); - WritePacket(Packet); + { + cPacketizer Pkt(*this, 0x05); // Spawn Position packet + Pkt.WriteInt((int)a_World.GetSpawnX()); + Pkt.WriteInt((int)a_World.GetSpawnY()); + Pkt.WriteInt((int)a_World.GetSpawnZ()); + } // Send player abilities: SendPlayerAbilities(); @@ -373,7 +427,24 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) { - // TODO + { + cPacketizer Pkt(*this, 0x0e); // Spawn Object packet + Pkt.WriteInt(a_Pickup.GetUniqueID()); + Pkt.WriteByte(2); // Type = Pickup + Pkt.WriteFPInt(a_Pickup.GetPosX()); + Pkt.WriteFPInt(a_Pickup.GetPosY()); + Pkt.WriteFPInt(a_Pickup.GetPosZ()); + Pkt.WriteByteAngle(a_Pickup.GetYaw()); + Pkt.WriteByteAngle(a_Pickup.GetPitch()); + Pkt.WriteInt(0); // No object data + } + { + cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet + Pkt.WriteInt(a_Pickup.GetUniqueID()); + Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 + Pkt.WriteItem(a_Pickup.GetItem()); + Pkt.WriteByte(0x7f); // End of metadata + } } @@ -382,19 +453,17 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) void cProtocol172::SendPlayerAbilities(void) { - cByteBuffer Packet(20); - Packet.WriteVarInt(0x39); // Player Abilities packet + cPacketizer Pkt(*this, 0x39); // Player Abilities packet Byte Flags = 0; if (m_Client->GetPlayer()->IsGameModeCreative()) { Flags |= 0x01; } // TODO: Other flags (god mode, flying, can fly - Packet.WriteByte(Flags); - // TODO: Packet.WriteBEFloat(m_Client->GetPlayer()->GetMaxFlyingSpeed()); - Packet.WriteBEFloat(0.05f); - Packet.WriteBEFloat((float)m_Client->GetPlayer()->GetMaxSpeed()); - WritePacket(Packet); + Pkt.WriteByte(Flags); + // TODO: Pkt.WriteFloat(m_Client->GetPlayer()->GetMaxFlyingSpeed()); + Pkt.WriteFloat(0.05f); + Pkt.WriteFloat((float)m_Client->GetPlayer()->GetMaxSpeed()); } @@ -403,7 +472,9 @@ void cProtocol172::SendPlayerAbilities(void) void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) { - // TODO + cPacketizer Pkt(*this, 0x0b); // Animation packet + Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteChar(a_Animation); } @@ -412,12 +483,10 @@ void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animatio void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) { - cByteBuffer Packet(10 + a_Player.GetName().size()); - Packet.WriteVarInt(0x38); // Playerlist Item packet - Packet.WriteVarUTF8String(a_Player.GetName()); - Packet.WriteBool(a_IsOnline); - Packet.WriteBEShort(a_Player.GetClientHandle()->GetPing()); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x38); // Playerlist Item packet + Pkt.WriteString(a_Player.GetName()); + Pkt.WriteBool(a_IsOnline); + Pkt.WriteShort(a_Player.GetClientHandle()->GetPing()); } @@ -435,7 +504,13 @@ void cProtocol172::SendPlayerMaxSpeed(void) void cProtocol172::SendPlayerMoveLook(void) { - // TODO + cPacketizer Pkt(*this, 0x08); // Player Position And Look packet + Pkt.WriteDouble(m_Client->GetPlayer()->GetPosX()); + Pkt.WriteDouble(m_Client->GetPlayer()->GetPosY()); + Pkt.WriteDouble(m_Client->GetPlayer()->GetPosZ()); + Pkt.WriteFloat((float)m_Client->GetPlayer()->GetYaw()); + Pkt.WriteFloat((float)m_Client->GetPlayer()->GetPitch()); + Pkt.WriteBool(m_Client->GetPlayer()->IsOnGround()); } @@ -444,7 +519,8 @@ void cProtocol172::SendPlayerMoveLook(void) void cProtocol172::SendPlayerPosition(void) { - // TODO + // There is no dedicated packet for this, send the whole thing: + SendPlayerMoveLook(); } @@ -453,7 +529,21 @@ void cProtocol172::SendPlayerPosition(void) void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) { - // TODO + // Called to spawn another player for the client + cPacketizer Pkt(*this, 0x0c); // Spawn Player packet + Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteString(Printf("%d", a_Player.GetUniqueID())); // TODO: Proper UUID + Pkt.WriteString(a_Player.GetName()); + Pkt.WriteFPInt(a_Player.GetPosX()); + Pkt.WriteFPInt(a_Player.GetPosY()); + Pkt.WriteFPInt(a_Player.GetPosZ()); + Pkt.WriteByteAngle(a_Player.GetYaw()); + Pkt.WriteByteAngle(a_Player.GetPitch()); + short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; + Pkt.WriteShort(ItemType); + Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 + Pkt.WriteFloat((float)a_Player.GetHealth()); + Pkt.WriteByte(0x7f); // Metadata: end } @@ -462,7 +552,11 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) void cProtocol172::SendRespawn(void) { - // TODO + cPacketizer Pkt(*this, 0x07); // Respawn packet + Pkt.WriteInt(m_Client->GetPlayer()->GetWorld()->GetDimension()); + Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) + Pkt.WriteByte((Byte)m_Client->GetPlayer()->GetEffectiveGameMode()); + Pkt.WriteString("default"); } @@ -670,6 +764,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) // Not enough data return; } + UInt32 NumBytesRead = Mark1 - m_ReceivedData.GetReadableSpace(); HandlePacket(PacketType, PacketLen - NumBytesRead); @@ -681,6 +776,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) // Go to packet end in any case: m_ReceivedData.ResetRead(); + m_ReceivedData.ReadVarInt(PacketType); m_ReceivedData.SkipRead(PacketLen); m_ReceivedData.CommitRead(); } // while (true) @@ -720,29 +816,29 @@ void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) // Game switch (a_PacketType) { - case 0x00: HandlePacketKeepAlive (a_RemainingBytes); break; - case 0x01: HandlePacketChatMessage (a_RemainingBytes); break; - case 0x02: HandlePacketUseEntity (a_RemainingBytes); break; - case 0x03: HandlePacketPlayer (a_RemainingBytes); break; - case 0x04: HandlePacketPlayerPos (a_RemainingBytes); break; - case 0x05: HandlePacketPlayerLook (a_RemainingBytes); break; - case 0x06: HandlePacketPlayerPosLook (a_RemainingBytes); break; - case 0x07: HandlePacketBlockDig (a_RemainingBytes); break; - case 0x08: HandlePacketBlockPlace (a_RemainingBytes); break; - case 0x09: HandlePacketSlotSelect (a_RemainingBytes); break; - case 0x0a: HandlePacketAnimation (a_RemainingBytes); break; - case 0x0b: HandlePacketEntityAction (a_RemainingBytes); break; - case 0x0c: HandlePacketSteerVehicle (a_RemainingBytes); break; - case 0x0d: HandlePacketWindowClose (a_RemainingBytes); break; - case 0x0e: HandlePacketWindowClick (a_RemainingBytes); break; + case 0x00: HandlePacketKeepAlive (a_RemainingBytes); return; + case 0x01: HandlePacketChatMessage (a_RemainingBytes); return; + case 0x02: HandlePacketUseEntity (a_RemainingBytes); return; + case 0x03: HandlePacketPlayer (a_RemainingBytes); return; + case 0x04: HandlePacketPlayerPos (a_RemainingBytes); return; + case 0x05: HandlePacketPlayerLook (a_RemainingBytes); return; + case 0x06: HandlePacketPlayerPosLook (a_RemainingBytes); return; + case 0x07: HandlePacketBlockDig (a_RemainingBytes); return; + case 0x08: HandlePacketBlockPlace (a_RemainingBytes); return; + case 0x09: HandlePacketSlotSelect (a_RemainingBytes); return; + case 0x0a: HandlePacketAnimation (a_RemainingBytes); return; + case 0x0b: HandlePacketEntityAction (a_RemainingBytes); return; + case 0x0c: HandlePacketSteerVehicle (a_RemainingBytes); return; + case 0x0d: HandlePacketWindowClose (a_RemainingBytes); return; + case 0x0e: HandlePacketWindowClick (a_RemainingBytes); return; case 0x0f: // Confirm transaction - not used in MCS - case 0x10: HandlePacketCreativeInventoryAction(a_RemainingBytes); break; - case 0x12: HandlePacketUpdateSign (a_RemainingBytes); break; - case 0x13: HandlePacketPlayerAbilities (a_RemainingBytes); break; - case 0x14: HandlePacketTabComplete (a_RemainingBytes); break; - case 0x15: HandlePacketClientSettings (a_RemainingBytes); break; - case 0x16: HandlePacketClientStatus (a_RemainingBytes); break; - case 0x17: HandlePacketPluginMessage (a_RemainingBytes); break; + case 0x10: HandlePacketCreativeInventoryAction(a_RemainingBytes); return; + case 0x12: HandlePacketUpdateSign (a_RemainingBytes); return; + case 0x13: HandlePacketPlayerAbilities (a_RemainingBytes); return; + case 0x14: HandlePacketTabComplete (a_RemainingBytes); return; + case 0x15: HandlePacketClientSettings (a_RemainingBytes); return; + case 0x16: HandlePacketClientStatus (a_RemainingBytes); return; + case 0x17: HandlePacketPluginMessage (a_RemainingBytes); return; } break; } @@ -772,10 +868,8 @@ void cProtocol172::HandlePacketStatusPing(UInt32 a_RemainingBytes) m_ReceivedData.ReadBEInt64(Timestamp); m_ReceivedData.CommitRead(); - cByteBuffer Packet(18); - Packet.WriteVarInt(0x01); - Packet.WriteBEInt64(Timestamp); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x01); // Ping packet + Pkt.WriteInt64(Timestamp); } @@ -799,10 +893,8 @@ void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) ); Response.append("}"); - cByteBuffer Packet(Response.size() + 10); - Packet.WriteVarInt(0x00); // Response packet - Packet.WriteVarUTF8String(Response); - WritePacket(Packet); + cPacketizer Pkt(*this, 0x00); // Response packet + Pkt.WriteString(Response); } @@ -826,12 +918,13 @@ void cProtocol172::HandlePacketLoginStart(UInt32 a_RemainingBytes) // TODO: Protocol encryption should be set up here if not localhost / auth // Send login success: - cByteBuffer Packet(Username.size() + 30); - Packet.WriteVarInt(0x02); // Login success packet - Packet.WriteVarUTF8String(Printf("%d", m_Client->GetUniqueID())); // TODO: UUID - Packet.WriteVarUTF8String(Username); - WritePacket(Packet); + { + cPacketizer Pkt(*this, 0x02); // Login success packet + Pkt.WriteString(Printf("%d", m_Client->GetUniqueID())); // TODO: proper UUID + Pkt.WriteString(Username); + } + m_State = 3; // State = Game m_Client->HandleLogin(4, Username); } @@ -898,7 +991,7 @@ void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) HANDLE_PACKET_READ(ReadByte, Byte, Unused); HANDLE_PACKET_READ(ReadByte, Byte, Difficulty); HANDLE_PACKET_READ(ReadByte, Byte, ShowCape); - // TODO + // TODO: handle in m_Client } @@ -1074,24 +1167,6 @@ void cProtocol172::WritePacket(cByteBuffer & a_Packet) void cProtocol172::SendData(const char * a_Data, int a_Size) { - m_DataToSend.append(a_Data, a_Size); -} - - - - - -void cProtocol172::Flush(void) -{ - ASSERT(m_CSPacket.IsLockedByCurrentThread()); // Did all packets lock the CS properly? - - if (m_DataToSend.empty()) - { - LOGD("Flushing empty"); - return; - } - const char * a_Data = m_DataToSend.data(); - int a_Size = m_DataToSend.size(); if (m_IsEncrypted) { byte Encrypted[8192]; // Larger buffer, we may be sending lots of data (chunks) @@ -1108,7 +1183,6 @@ void cProtocol172::Flush(void) { m_Client->SendData(a_Data, a_Size); } - m_DataToSend.clear(); } @@ -1116,3 +1190,86 @@ void cProtocol172::Flush(void) +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cProtocol172::cPacketizer: + +cProtocol172::cPacketizer::~cPacketizer() +{ + AString DataToSend; + + // Send the packet length + UInt32 PacketLen = m_Out.GetUsedSpace(); + m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); + m_Protocol.m_OutPacketLenBuffer.ReadAll(DataToSend); + m_Protocol.SendData(DataToSend.data(), DataToSend.size()); + m_Protocol.m_OutPacketLenBuffer.CommitRead(); + + // Send the packet data: + m_Out.ReadAll(DataToSend); + m_Protocol.SendData(DataToSend.data(), DataToSend.size()); + m_Out.CommitRead(); +} + + + + + +void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) +{ + short ItemType = a_Item.m_ItemType; + ASSERT(ItemType >= -1); // Check validity of packets in debug runtime + if (ItemType <= 0) + { + // Fix, to make sure no invalid values are sent. + ItemType = -1; + } + + if (a_Item.IsEmpty()) + { + WriteShort(-1); + return; + } + + WriteShort(ItemType); + WriteByte (a_Item.m_ItemCount); + WriteShort(a_Item.m_ItemDamage); + + if (a_Item.m_Enchantments.IsEmpty()) + { + WriteShort(-1); + return; + } + + // Send the enchantments: + cFastNBTWriter Writer; + const char * TagName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench"; + a_Item.m_Enchantments.WriteToNBTCompound(Writer, TagName); + Writer.Finish(); + AString Compressed; + CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); + WriteShort(Compressed.size()); + WriteBuf(Compressed.data(), Compressed.size()); +} + + + + + +void cProtocol172::cPacketizer::WriteByteAngle(double a_Angle) +{ + WriteByte((char)(255 * a_Angle / 360)); +} + + + + + +void cProtocol172::cPacketizer::WriteFPInt(double a_Value) +{ + int Value = (int)(a_Value * 32); + WriteInt(Value); +} + + + + diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index f11d8e2f9..4be166814 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -95,6 +95,85 @@ public: protected: + /// Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed + class cPacketizer + { + public: + cPacketizer(cProtocol172 & a_Protocol, UInt32 a_PacketType) : + m_Protocol(a_Protocol), + m_Out(a_Protocol.m_OutPacketBuffer), + m_Lock(a_Protocol.m_CSPacket) + { + m_Out.WriteVarInt(a_PacketType); + } + + ~cPacketizer(); + + void WriteBool(bool a_Value) + { + m_Out.WriteBool(a_Value); + } + + void WriteByte(Byte a_Value) + { + m_Out.WriteByte(a_Value); + } + + void WriteChar(char a_Value) + { + m_Out.WriteChar(a_Value); + } + + void WriteShort(short a_Value) + { + m_Out.WriteBEShort(a_Value); + } + + void WriteInt(int a_Value) + { + m_Out.WriteBEInt(a_Value); + } + + void WriteInt64(Int64 a_Value) + { + m_Out.WriteBEInt64(a_Value); + } + + void WriteFloat(float a_Value) + { + m_Out.WriteBEFloat(a_Value); + } + + void WriteDouble(double a_Value) + { + m_Out.WriteBEDouble(a_Value); + } + + void WriteVarInt(UInt32 a_Value) + { + m_Out.WriteVarInt(a_Value); + } + + void WriteString(const AString & a_Value) + { + m_Out.WriteVarUTF8String(a_Value); + } + + void WriteBuf(const char * a_Data, int a_Size) + { + m_Out.Write(a_Data, a_Size); + } + + void WriteItem(const cItem & a_Item); + void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte + void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer + + protected: + cProtocol172 & m_Protocol; + cByteBuffer & m_Out; + cCSLock m_Lock; + } ; + AString m_ServerAddress; UInt16 m_ServerPort; @@ -107,13 +186,16 @@ protected: /// Buffer for the received data cByteBuffer m_ReceivedData; + /// Buffer for composing the outgoing packets, through cPacketizer + cByteBuffer m_OutPacketBuffer; + + /// Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) + cByteBuffer m_OutPacketLenBuffer; + bool m_IsEncrypted; CryptoPP::CFB_Mode::Decryption m_Decryptor; CryptoPP::CFB_Mode::Encryption m_Encryptor; - /// (Unencrypted) data to be sent to the client. Written by SendData, cleared by Flush() - AString m_DataToSend; - /// Adds the received (unencrypted) data to m_ReceivedData, parses complete packets void AddReceivedData(const char * a_Data, int a_Size); @@ -157,12 +239,9 @@ protected: /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. void WritePacket(cByteBuffer & a_Packet); - /// Adds unencrypted data to the outgoing data buffer + /// Sends the data to the client, encrypting them if needed. virtual void SendData(const char * a_Data, int a_Size) override; - /// Flushes m_DataToSend through the optional encryption into the outgoing socket data - virtual void Flush(void) override; - void SendCompass(const cWorld & a_World); } ; -- cgit v1.2.3 From 3002dc8bbf122a348d6aaa691ff883f453080939 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 3 Nov 2013 21:34:36 +0100 Subject: Protocol 1.7: Added client status packet. --- source/Protocol/Protocol17x.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index ef6049900..e23ee66a5 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -1000,7 +1000,28 @@ void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketClientStatus(UInt32 a_RemainingBytes) { - // TODO + HANDLE_PACKET_READ(ReadByte, Byte, ActionID); + switch (ActionID) + { + case 0: + { + // Respawn + m_Client->HandleRespawn(); + break; + } + case 1: + { + // Request stats + // TODO + break; + } + case 2: + { + // Open Inventory achievement + // TODO + break; + } + } } -- cgit v1.2.3 From d47a8ea008062b2857142cbb096a7f045308527e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 4 Nov 2013 21:20:36 +0100 Subject: Protocol 1.7: Added more client-bound packets. Untested, still more to come. --- source/Protocol/Protocol17x.cpp | 179 ++++++++++++++++++++++++++++++++++++---- source/Protocol/Protocol17x.h | 2 + 2 files changed, 166 insertions(+), 15 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index e23ee66a5..38260d046 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -10,15 +10,16 @@ Implements the 1.7.x protocol classes: #include "Globals.h" #include "Protocol17x.h" +#include "ChunkDataSerializer.h" #include "../ClientHandle.h" #include "../Root.h" #include "../Server.h" -#include "../Entities/Player.h" #include "../World.h" -#include "ChunkDataSerializer.h" -#include "../Entities/Pickup.h" #include "../WorldStorage/FastNBT.h" #include "../StringCompression.h" +#include "../Entities/FallingBlock.h" +#include "../Entities/Pickup.h" +#include "../Entities/Player.h" @@ -158,7 +159,10 @@ void cProtocol172::SendChat(const AString & a_Message) void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) { - const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); // This contains the flags and bitmasks, too + // Serialize first, before creating the Packetizer (the packetizer locks a CS) + // This contains the flags and bitmasks, too + const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); + cPacketizer Pkt(*this, 0x21); // Chunk Data packet Pkt.WriteInt(a_ChunkX); Pkt.WriteInt(a_ChunkZ); @@ -254,7 +258,7 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) // TODO cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteInt(a_Entity.GetUniqueID()); - WriteEntityMetadata(Pkt, a_Entity); + Pkt.WriteEntityMetadata(a_Entity); */ } @@ -565,7 +569,13 @@ void cProtocol172::SendRespawn(void) void cProtocol172::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) // a_Src coords are Block * 8 { - // TODO + cPacketizer Pkt(*this, 0x29); // Sound Effect packet + Pkt.WriteString(a_SoundName); + Pkt.WriteInt(a_SrcX); + Pkt.WriteInt(a_SrcY); + Pkt.WriteInt(a_SrcZ); + Pkt.WriteFloat(a_Volume); + Pkt.WriteByte((Byte)(a_Pitch * 63)); } @@ -574,7 +584,13 @@ void cProtocol172::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) { - // TODO + cPacketizer Pkt(*this, 0x28); // Effect packet + Pkt.WriteInt(a_EffectID); + Pkt.WriteInt(a_SrcX); + Pkt.WriteInt(a_SrcY); + Pkt.WriteInt(a_SrcZ); + Pkt.WriteInt(a_Data); + Pkt.WriteBool(false); } @@ -583,7 +599,18 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) { - // TODO + cPacketizer Pkt(*this, 0x0e); // Spawn Object packet + Pkt.WriteInt(a_FallingBlock.GetUniqueID()); + Pkt.WriteByte(70); // Falling block + Pkt.WriteFPInt(a_FallingBlock.GetPosX()); + Pkt.WriteFPInt(a_FallingBlock.GetPosY()); + Pkt.WriteFPInt(a_FallingBlock.GetPosZ()); + Pkt.WriteByteAngle(a_FallingBlock.GetYaw()); + Pkt.WriteByteAngle(a_FallingBlock.GetPitch()); + Pkt.WriteInt(((int)a_FallingBlock.GetBlockType()) | (((int)a_FallingBlock.GetBlockMeta()) << 12)); + Pkt.WriteShort((short)(a_FallingBlock.GetSpeedX() * 400)); + Pkt.WriteShort((short)(a_FallingBlock.GetSpeedY() * 400)); + Pkt.WriteShort((short)(a_FallingBlock.GetSpeedZ() * 400)); } @@ -592,7 +619,20 @@ void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) void cProtocol172::SendSpawnMob(const cMonster & a_Mob) { - // TODO + cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet + Pkt.WriteInt(a_Mob.GetUniqueID()); + Pkt.WriteByte((Byte)a_Mob.GetMobType()); + Pkt.WriteFPInt(a_Mob.GetPosX()); + Pkt.WriteFPInt(a_Mob.GetPosY()); + Pkt.WriteFPInt(a_Mob.GetPosZ()); + Pkt.WriteByteAngle(a_Mob.GetPitch()); + Pkt.WriteByteAngle(a_Mob.GetHeadYaw()); + Pkt.WriteByteAngle(a_Mob.GetYaw()); + Pkt.WriteShort((short)(a_Mob.GetSpeedX() * 400)); + Pkt.WriteShort((short)(a_Mob.GetSpeedY() * 400)); + Pkt.WriteShort((short)(a_Mob.GetSpeedZ() * 400)); + Pkt.WriteEntityMetadata(a_Mob); + Pkt.WriteByte(0x7f); // Metadata terminator } @@ -601,7 +641,21 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) { - // TODO + cPacketizer Pkt(*this, 0xe); // Spawn Object packet + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteByte(a_ObjectType); + Pkt.WriteFPInt(a_Entity.GetPosX()); + Pkt.WriteFPInt(a_Entity.GetPosY()); + Pkt.WriteFPInt(a_Entity.GetPosZ()); + Pkt.WriteByteAngle(a_Entity.GetYaw()); + Pkt.WriteByteAngle(a_Entity.GetPitch()); + Pkt.WriteInt(a_ObjectData); + if (a_ObjectData != 0) + { + Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); + Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); + Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); + } } @@ -610,7 +664,21 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) { - // TODO + cPacketizer Pkt(*this, 0xe); // Spawn Object packet + Pkt.WriteInt(a_Vehicle.GetUniqueID()); + Pkt.WriteByte(a_VehicleType); + Pkt.WriteFPInt(a_Vehicle.GetPosX()); + Pkt.WriteFPInt(a_Vehicle.GetPosY()); + Pkt.WriteFPInt(a_Vehicle.GetPosZ()); + Pkt.WriteByteAngle(a_Vehicle.GetYaw()); + Pkt.WriteByteAngle(a_Vehicle.GetPitch()); + Pkt.WriteInt(a_VehicleSubType); + if (a_VehicleSubType != 0) + { + Pkt.WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); + Pkt.WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); + Pkt.WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); + } } @@ -619,7 +687,17 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) { - // TODO + AString Results; + Results.reserve(500); // Make a moderate reservation to avoid excessive reallocations + for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) + { + Results.append(*itr); + Results.push_back(0); + } + + cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet + Pkt.WriteVarInt(a_Results.size()); + Pkt.WriteString(Results); } @@ -628,7 +706,13 @@ void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) { - // TODO + cPacketizer Pkt(*this, 0x18); + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteFPInt(a_Entity.GetPosX()); + Pkt.WriteFPInt(a_Entity.GetPosY()); + Pkt.WriteFPInt(a_Entity.GetPosZ()); + Pkt.WriteByteAngle(a_Entity.GetYaw()); + Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -637,7 +721,12 @@ void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) void cProtocol172::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) { - // TODO + cPacketizer Pkt(*this, 0x2c); // Spawn Global Entity packet + Pkt.WriteVarInt(0); // EntityID = 0, always + Pkt.WriteByte(1); // Type = Thunderbolt + Pkt.WriteFPInt(a_BlockX); + Pkt.WriteFPInt(a_BlockY); + Pkt.WriteFPInt(a_BlockZ); } @@ -646,7 +735,9 @@ void cProtocol172::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) void cProtocol172::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) { - // TODO + cPacketizer Pkt(*this, 0x03); + Pkt.WriteInt64(a_WorldAge); + Pkt.WriteInt64(a_TimeOfDay); } @@ -1294,3 +1385,61 @@ void cProtocol172::cPacketizer::WriteFPInt(double a_Value) + +void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) +{ + // Common metadata: + Byte Flags = 0; + if (a_Entity.IsOnFire()) + { + Flags |= 0x01; + } + if (a_Entity.IsCrouched()) + { + Flags |= 0x02; + } + if (a_Entity.IsSprinting()) + { + Flags |= 0x08; + } + if (a_Entity.IsRclking()) + { + Flags |= 0x10; + } + if (a_Entity.IsInvisible()) + { + Flags |= 0x20; + } + WriteByte(0); // Byte(0) + index 0 + WriteByte(Flags); + + switch (a_Entity.GetEntityType()) + { + case cEntity::etPlayer: break; // TODO? + case cEntity::etPickup: + { + WriteByte((5 << 5) | 10); // Slot(5) + index 10 + WriteItem(((const cPickup &)a_Entity).GetItem()); + break; + } + case cEntity::etMonster: + { + WriteMobMetadata((const cMonster &)a_Entity); + break; + } + // TODO: Other types + } +} + + + + + +void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) +{ + // TODO +} + + + + diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index 4be166814..e3a51b844 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -167,6 +167,8 @@ protected: void WriteItem(const cItem & a_Item); void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer + void WriteEntityMetadata(const cEntity & a_Entity); // Writes the metadata for the specified entity, not including the terminating 0x7f + void WriteMobMetadata(const cMonster & a_Mob); // Writes the mob-specific metadata for the specified mob protected: cProtocol172 & m_Protocol; -- cgit v1.2.3 From 88472b7ce68822c4487142d43308bd2fe9874e54 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 5 Nov 2013 18:36:58 +0100 Subject: Removed the obsolete SendWholeInventory(cInventory &) function. This won't compile because there's still a change in Protocol17x.cpp pending; the next commit will fix this. --- source/Protocol/Protocol.h | 1 - source/Protocol/Protocol125.cpp | 9 --------- source/Protocol/Protocol125.h | 1 - source/Protocol/Protocol17x.h | 1 - source/Protocol/ProtocolRecognizer.cpp | 10 ---------- source/Protocol/ProtocolRecognizer.h | 1 - 6 files changed, 23 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h index 6bea4edbb..5d66808cf 100644 --- a/source/Protocol/Protocol.h +++ b/source/Protocol/Protocol.h @@ -99,7 +99,6 @@ public: 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) = 0; virtual void SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) = 0; virtual void SendWeather (eWeather a_Weather) = 0; - virtual void SendWholeInventory (const cInventory & a_Inventory) = 0; virtual void SendWholeInventory (const cWindow & a_Window) = 0; virtual void SendWindowClose (const cWindow & a_Window) = 0; virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) = 0; diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index ef40f265a..4cb1197da 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -940,15 +940,6 @@ void cProtocol125::SendWeather(eWeather a_Weather) -void cProtocol125::SendWholeInventory(const cInventory & a_Inventory) -{ - SendWholeInventory(*(a_Inventory.GetOwner().GetWindow())); -} - - - - - void cProtocol125::SendWholeInventory(const cWindow & a_Window) { cCSLock Lock(m_CSPacket); diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index 1da50a1d4..ad61dea74 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -76,7 +76,6 @@ public: 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cInventory & a_Inventory) override; virtual void SendWholeInventory (const cWindow & a_Window) override; virtual void SendWindowClose (const cWindow & a_Window) override; virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index e3a51b844..f63cd8187 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -85,7 +85,6 @@ public: 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cInventory & a_Inventory) override; virtual void SendWholeInventory (const cWindow & a_Window) override; virtual void SendWindowClose (const cWindow & a_Window) override; virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 67f924d7e..501e8bbe0 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -605,16 +605,6 @@ void cProtocolRecognizer::SendWeather(eWeather a_Weather) -void cProtocolRecognizer::SendWholeInventory(const cInventory & a_Inventory) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWholeInventory(a_Inventory); -} - - - - - void cProtocolRecognizer::SendWholeInventory(const cWindow & a_Window) { ASSERT(m_Protocol != NULL); diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 79dc5568f..9bf550309 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -111,7 +111,6 @@ public: 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cInventory & a_Inventory) override; virtual void SendWholeInventory (const cWindow & a_Window) override; virtual void SendWindowClose (const cWindow & a_Window) override; virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; -- cgit v1.2.3 From e2e948015e426d43398205f057b99677e1b0be1f Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 5 Nov 2013 18:37:39 +0100 Subject: Protocol 1.7: More client-bound packets. Also removed the SendWholeInventory(cInventory &) function, as promised in prev commit. --- source/Protocol/Protocol17x.cpp | 44 +++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 38260d046..d0ecc5583 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -254,12 +254,10 @@ void cProtocol172::SendEntityLook(const cEntity & a_Entity) void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) { - /* - // TODO cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteInt(a_Entity.GetUniqueID()); Pkt.WriteEntityMetadata(a_Entity); - */ + Pkt.WriteByte(0x7f); // The termination byte } @@ -587,6 +585,8 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src cPacketizer Pkt(*this, 0x28); // Effect packet Pkt.WriteInt(a_EffectID); Pkt.WriteInt(a_SrcX); + // TODO: Check if this is really an int + // wiki.vg says it's a byte, but that wouldn't cover the entire range needed (Y location * 8 = 0..2048) Pkt.WriteInt(a_SrcY); Pkt.WriteInt(a_SrcZ); Pkt.WriteInt(a_Data); @@ -746,7 +746,13 @@ void cProtocol172::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) void cProtocol172::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) { - // TODO + cPacketizer Pkt(*this, 0x21); // Chunk Data packet + Pkt.WriteInt(a_ChunkX); + Pkt.WriteInt(a_ChunkZ); + Pkt.WriteBool(true); + Pkt.WriteShort(0); // Primary bitmap + Pkt.WriteShort(0); // Add bitmap + Pkt.WriteInt(0); // Compressed data size } @@ -755,16 +761,27 @@ void cProtocol172::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) void cProtocol172::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) { - // TODO + cPacketizer Pkt(*this, 0x33); + Pkt.WriteInt(a_BlockX); + Pkt.WriteShort((short)a_BlockY); + Pkt.WriteInt(a_BlockZ); + Pkt.WriteString(a_Line1); + Pkt.WriteString(a_Line2); + Pkt.WriteString(a_Line3); + Pkt.WriteString(a_Line4); } -void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) +void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ) { - // TODO + cPacketizer Pkt(*this, 0x0a); + Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteInt(a_BlockX); + Pkt.WriteByte((Byte)a_BlockY); + Pkt.WriteInt(a_BlockZ); } @@ -773,16 +790,9 @@ void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_Bloc void cProtocol172::SendWeather(eWeather a_Weather) { - // TODO -} - - - - - -void cProtocol172::SendWholeInventory(const cInventory & a_Inventory) -{ - // TODO + cPacketizer Pkt(*this, 0x2b); + Pkt.WriteByte((a_Weather == wSunny) ? 2 : 1); // begin rain / end rain + Pkt.WriteFloat(0); // unused } -- cgit v1.2.3 From cc54f4f8360b7efb81ddacfe9e9d900a7668493c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 6 Nov 2013 12:16:44 +0100 Subject: Protocol 1.7: Finished the client-bound packets. --- source/Protocol/Protocol17x.cpp | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index d0ecc5583..2b3f3b613 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -20,6 +20,7 @@ Implements the 1.7.x protocol classes: #include "../Entities/FallingBlock.h" #include "../Entities/Pickup.h" #include "../Entities/Player.h" +#include "../UI/Window.h" @@ -790,7 +791,7 @@ void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_Bloc void cProtocol172::SendWeather(eWeather a_Weather) { - cPacketizer Pkt(*this, 0x2b); + cPacketizer Pkt(*this, 0x2b); // Change Game State packet Pkt.WriteByte((a_Weather == wSunny) ? 2 : 1); // begin rain / end rain Pkt.WriteFloat(0); // unused } @@ -799,18 +800,27 @@ void cProtocol172::SendWeather(eWeather a_Weather) -void cProtocol172::SendWholeInventory(const cWindow & a_Window) +void cProtocol172::SendWholeInventory(const cWindow & a_Window) { - // TODO + cPacketizer Pkt(*this, 0x30); // Window Items packet + Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteShort(a_Window.GetNumSlots()); + cItems Slots; + a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); + for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) + { + Pkt.WriteItem(*itr); + } // for itr - Slots[] } -void cProtocol172::SendWindowClose(const cWindow & a_Window) +void cProtocol172::SendWindowClose(const cWindow & a_Window) { - // TODO + cPacketizer Pkt(*this, 0x2e); + Pkt.WriteChar(a_Window.GetWindowID()); } @@ -819,7 +829,19 @@ void cProtocol172::SendWindowClose(const cWindow & a_Window) void cProtocol172::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) { - // TODO + cPacketizer Pkt(*this, 0x2d); + Pkt.WriteChar(a_WindowID); + Pkt.WriteChar(a_WindowType); + Pkt.WriteString(a_WindowTitle); + Pkt.WriteChar(a_NumSlots); + Pkt.WriteBool(true); + /* + // TODO: + if (a_WindowType == cWindow::wtHorse) + { + Pkt.WriteInt(HorseID); + } + */ } @@ -828,7 +850,10 @@ void cProtocol172::SendWindowOpen(char a_WindowID, char a_WindowType, const AStr void cProtocol172::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) { - // TODO + cPacketizer Pkt(*this, 0x31); // Window Property packet + Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteShort(a_Property); + Pkt.WriteShort(a_Value); } -- cgit v1.2.3 From edc848439a4e481067d986b8d8d3ef3e256a8810 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 6 Nov 2013 20:48:35 +0100 Subject: Protocol 1.7: Added more server-bound packets. --- source/Protocol/Protocol17x.cpp | 157 +++++++++++++++++++++++++++++++++------- source/Protocol/Protocol17x.h | 6 ++ 2 files changed, 138 insertions(+), 25 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 2b3f3b613..9dc46d56b 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -26,7 +26,7 @@ Implements the 1.7.x protocol classes: -#define HANDLE_PACKET_READ(Proc, Type, Var) \ +#define HANDLE_READ(Proc, Type, Var) \ Type Var; \ m_ReceivedData.Proc(Var); @@ -34,6 +34,21 @@ Implements the 1.7.x protocol classes: +#define HANDLE_PACKET_READ(Proc, Type, Var) \ + Type Var; \ + { \ + if (!m_ReceivedData.Proc(Var)) \ + { \ + m_ReceivedData.CheckValid(); \ + return false; \ + } \ + m_ReceivedData.CheckValid(); \ + } + + + + + cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : super(a_Client), m_ServerAddress(a_ServerAddress), @@ -1060,8 +1075,8 @@ void cProtocol172::HandlePacketLoginStart(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketAnimation(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadBEInt, int, EntityID); - HANDLE_PACKET_READ(ReadByte, Byte, Animation); + HANDLE_READ(ReadBEInt, int, EntityID); + HANDLE_READ(ReadByte, Byte, Animation); m_Client->HandleAnimation(Animation); } @@ -1071,11 +1086,11 @@ void cProtocol172::HandlePacketAnimation(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketBlockDig(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadByte, Byte, Status); - HANDLE_PACKET_READ(ReadBEInt, int, BlockX); - HANDLE_PACKET_READ(ReadByte, Byte, BlockY); - HANDLE_PACKET_READ(ReadBEInt, int, BlockZ); - HANDLE_PACKET_READ(ReadByte, Byte, Face); + HANDLE_READ(ReadByte, Byte, Status); + HANDLE_READ(ReadBEInt, int, BlockX); + HANDLE_READ(ReadByte, Byte, BlockY); + HANDLE_READ(ReadBEInt, int, BlockZ); + HANDLE_READ(ReadByte, Byte, Face); m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, Face, Status); } @@ -1085,13 +1100,13 @@ void cProtocol172::HandlePacketBlockDig(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketBlockPlace(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadBEInt, int, BlockX); - HANDLE_PACKET_READ(ReadByte, Byte, BlockY); - HANDLE_PACKET_READ(ReadBEInt, int, BlockZ); - HANDLE_PACKET_READ(ReadByte, Byte, Face); - HANDLE_PACKET_READ(ReadByte, Byte, CursorX); - HANDLE_PACKET_READ(ReadByte, Byte, CursorY); - HANDLE_PACKET_READ(ReadByte, Byte, CursorZ); + HANDLE_READ(ReadBEInt, int, BlockX); + HANDLE_READ(ReadByte, Byte, BlockY); + HANDLE_READ(ReadBEInt, int, BlockZ); + HANDLE_READ(ReadByte, Byte, Face); + HANDLE_READ(ReadByte, Byte, CursorX); + HANDLE_READ(ReadByte, Byte, CursorY); + HANDLE_READ(ReadByte, Byte, CursorZ); m_Client->HandleRightClick(BlockX, BlockY, BlockZ, Face, CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); } @@ -1101,7 +1116,7 @@ void cProtocol172::HandlePacketBlockPlace(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketChatMessage(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadVarUTF8String, AString, Message); + HANDLE_READ(ReadVarUTF8String, AString, Message); m_Client->HandleChat(Message); } @@ -1111,12 +1126,12 @@ void cProtocol172::HandlePacketChatMessage(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadVarUTF8String, AString, Locale); - HANDLE_PACKET_READ(ReadByte, Byte, ViewDistance); - HANDLE_PACKET_READ(ReadByte, Byte, ChatFlags); - HANDLE_PACKET_READ(ReadByte, Byte, Unused); - HANDLE_PACKET_READ(ReadByte, Byte, Difficulty); - HANDLE_PACKET_READ(ReadByte, Byte, ShowCape); + HANDLE_READ(ReadVarUTF8String, AString, Locale); + HANDLE_READ(ReadByte, Byte, ViewDistance); + HANDLE_READ(ReadByte, Byte, ChatFlags); + HANDLE_READ(ReadByte, Byte, Unused); + HANDLE_READ(ReadByte, Byte, Difficulty); + HANDLE_READ(ReadByte, Byte, ShowCape); // TODO: handle in m_Client } @@ -1126,7 +1141,7 @@ void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketClientStatus(UInt32 a_RemainingBytes) { - HANDLE_PACKET_READ(ReadByte, Byte, ActionID); + HANDLE_READ(ReadByte, Byte, ActionID); switch (ActionID) { case 0: @@ -1156,7 +1171,13 @@ void cProtocol172::HandlePacketClientStatus(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEShort, short, SlotNum); + cItem Item; + if (!ReadItem(Item)) + { + return; + } + m_Client->HandleCreativeInventory(SlotNum, Item); } @@ -1165,7 +1186,10 @@ void cProtocol172::HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketEntityAction(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEInt, int, PlayerID); + HANDLE_READ(ReadByte, Byte, Action); + HANDLE_READ(ReadBEInt, int, JumpBoost); + m_Client->HandleEntityAction(PlayerID, Action); } @@ -1337,6 +1361,89 @@ void cProtocol172::SendData(const char * a_Data, int a_Size) +bool cProtocol172::ReadItem(cItem & a_Item) +{ + HANDLE_PACKET_READ(ReadBEShort, short, ItemType); + if (ItemType == -1) + { + // The item is empty, no more data follows + a_Item.Empty(); + return true; + } + a_Item.m_ItemType = ItemType; + + HANDLE_PACKET_READ(ReadChar, char, ItemCount); + HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage); + a_Item.m_ItemCount = ItemCount; + a_Item.m_ItemDamage = ItemDamage; + if (ItemCount <= 0) + { + a_Item.Empty(); + } + + HANDLE_PACKET_READ(ReadBEShort, short, MetadataLength); + if (MetadataLength <= 0) + { + return true; + } + + // Read the metadata + AString Metadata; + if (!m_ReceivedData.ReadString(Metadata, MetadataLength)) + { + return false; + } + + ParseItemMetadata(a_Item, Metadata); + return true; +} + + + + + +void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) +{ + // Uncompress the GZIPped data: + AString Uncompressed; + if (UncompressStringGZIP(a_Metadata.data(), a_Metadata.size(), Uncompressed) != Z_OK) + { + AString HexDump; + CreateHexDump(HexDump, a_Metadata.data(), a_Metadata.size(), 16); + LOGWARNING("Cannot unGZIP item metadata (%u bytes):\n%s", a_Metadata.size(), HexDump.c_str()); + return; + } + + // Parse into NBT: + cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); + if (!NBT.IsValid()) + { + AString HexDump; + CreateHexDump(HexDump, Uncompressed.data(), Uncompressed.size(), 16); + LOGWARNING("Cannot parse NBT item metadata: (%u bytes)\n%s", Uncompressed.size(), HexDump.c_str()); + return; + } + + // Load enchantments from the NBT: + for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag)) + { + if ( + (NBT.GetType(tag) == TAG_List) && + ( + (NBT.GetName(tag) == "ench") || + (NBT.GetName(tag) == "StoredEnchantments") + ) + ) + { + a_Item.m_Enchantments.ParseFromNBT(NBT, tag); + } + } +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cProtocol172::cPacketizer: diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index f63cd8187..8cfb74004 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -244,6 +244,12 @@ protected: virtual void SendData(const char * a_Data, int a_Size) override; void SendCompass(const cWorld & a_World); + + /// Reads an item out of the received data, sets a_Item to the values read. Returns false if not enough received data + bool ReadItem(cItem & a_Item); + + /// Parses item metadata as read by ReadItem(), into the item enchantments. + void ParseItemMetadata(cItem & a_Item, const AString & a_Metadata); } ; -- cgit v1.2.3 From 088f7f68fcfa27fae72110c2e5e8c15846e77548 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 6 Nov 2013 22:27:09 +0100 Subject: Protocol 1.7: Added the remaining server-bound packets. --- source/Protocol/Protocol16x.cpp | 1 - source/Protocol/Protocol17x.cpp | 113 +++++++++++++++++++++++++++++++++++----- 2 files changed, 99 insertions(+), 15 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol16x.cpp b/source/Protocol/Protocol16x.cpp index 0eac7b081..23e23d463 100644 --- a/source/Protocol/Protocol16x.cpp +++ b/source/Protocol/Protocol16x.cpp @@ -210,7 +210,6 @@ int cProtocol161::ParseSteerVehicle(void) HANDLE_PACKET_READ(ReadBEFloat, float, Forward); HANDLE_PACKET_READ(ReadBool, bool, Jump); HANDLE_PACKET_READ(ReadBool, bool, Unmount); - // TODO: m_Client->HandleSteerVehicle(...); if (Unmount) { m_Client->HandleUnmount(); diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 9dc46d56b..99cc7b52c 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -1198,7 +1198,8 @@ void cProtocol172::HandlePacketEntityAction(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketKeepAlive(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEInt, int, KeepAliveID); + m_Client->HandleKeepAlive(KeepAliveID); } @@ -1207,7 +1208,8 @@ void cProtocol172::HandlePacketKeepAlive(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPlayer(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBool, bool, IsOnGround); + // TODO: m_Client->HandlePlayerOnGround(IsOnGround); } @@ -1216,7 +1218,10 @@ void cProtocol172::HandlePacketPlayer(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPlayerAbilities(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadByte, Byte, Flags); + HANDLE_READ(ReadBEFloat, float, FlyingSpeed); + HANDLE_READ(ReadBEFloat, float, WalkingSpeed); + // TODO: m_Client->HandlePlayerAbilities(); } @@ -1225,7 +1230,10 @@ void cProtocol172::HandlePacketPlayerAbilities(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPlayerLook(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEFloat, float, Yaw); + HANDLE_READ(ReadBEFloat, float, Pitch); + HANDLE_READ(ReadBool, bool, IsOnGround); + m_Client->HandlePlayerLook(Yaw, Pitch, IsOnGround); } @@ -1234,7 +1242,12 @@ void cProtocol172::HandlePacketPlayerLook(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPlayerPos(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEDouble, double, PosX); + HANDLE_READ(ReadBEDouble, double, PosY); + HANDLE_READ(ReadBEDouble, double, Stance); + HANDLE_READ(ReadBEDouble, double, PosZ); + HANDLE_READ(ReadBool, bool, IsOnGround); + m_Client->HandlePlayerPos(PosX, PosY, PosZ, Stance, IsOnGround); } @@ -1243,7 +1256,14 @@ void cProtocol172::HandlePacketPlayerPos(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPlayerPosLook(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEDouble, double, PosX); + HANDLE_READ(ReadBEDouble, double, PosY); + HANDLE_READ(ReadBEDouble, double, Stance); + HANDLE_READ(ReadBEDouble, double, PosZ); + HANDLE_READ(ReadBEFloat, float, Yaw); + HANDLE_READ(ReadBEFloat, float, Pitch); + HANDLE_READ(ReadBool, bool, IsOnGround); + m_Client->HandlePlayerMoveLook(PosX, PosY, PosZ, Stance, Yaw, Pitch, IsOnGround); } @@ -1252,7 +1272,11 @@ void cProtocol172::HandlePacketPlayerPosLook(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketPluginMessage(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadVarUTF8String, AString, Channel); + HANDLE_READ(ReadBEShort, short, Length); + AString Data; + m_ReceivedData.ReadString(Data, Length); + // TODO: m_Client->HandlePluginMessage(Channel, Data); } @@ -1261,7 +1285,8 @@ void cProtocol172::HandlePacketPluginMessage(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketSlotSelect(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEShort, short, SlotNum); + m_Client->HandleSlotSelected(SlotNum); } @@ -1270,7 +1295,18 @@ void cProtocol172::HandlePacketSlotSelect(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketSteerVehicle(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEFloat, float, Forward); + HANDLE_READ(ReadBEFloat, float, Sideways); + HANDLE_READ(ReadBool, bool, ShouldJump); + HANDLE_READ(ReadBool, bool, ShouldUnmount); + if (ShouldUnmount) + { + m_Client->HandleUnmount(); + } + else + { + m_Client->HandleSteerVehicle(Forward, Sideways); + } } @@ -1279,7 +1315,8 @@ void cProtocol172::HandlePacketSteerVehicle(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketTabComplete(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadVarUTF8String, AString, Text); + m_Client->HandleTabCompletion(Text); } @@ -1288,7 +1325,14 @@ void cProtocol172::HandlePacketTabComplete(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketUpdateSign(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEInt, int, BlockX); + HANDLE_READ(ReadBEShort, short, BlockY); + HANDLE_READ(ReadBEInt, int, BlockZ); + HANDLE_READ(ReadVarUTF8String, AString, Line1); + HANDLE_READ(ReadVarUTF8String, AString, Line2); + HANDLE_READ(ReadVarUTF8String, AString, Line3); + HANDLE_READ(ReadVarUTF8String, AString, Line4); + m_Client->HandleUpdateSign(BlockX, BlockY, BlockZ, Line1, Line2, Line3, Line4); } @@ -1297,7 +1341,10 @@ void cProtocol172::HandlePacketUpdateSign(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketUseEntity(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadBEInt, int, EntityID); + HANDLE_READ(ReadByte, Byte, MouseButton); + // TODO: Verify that this works, wiki.vg has no info on the MouseButton values + m_Client->HandleUseEntity(EntityID, (MouseButton == 0)); } @@ -1306,7 +1353,44 @@ void cProtocol172::HandlePacketUseEntity(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketWindowClick(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadChar, char, WindowID); + HANDLE_READ(ReadBEShort, short, SlotNum); + HANDLE_READ(ReadByte, Byte, Button); + HANDLE_READ(ReadBEShort, short, TransactionID); + HANDLE_READ(ReadByte, Byte, Mode); + cItem Item; + ReadItem(Item); + + // Convert Button, Mode, SlotNum and HeldItem into eClickAction: + eClickAction Action; + switch ((Mode << 8) | Button) + { + case 0x0000: Action = (SlotNum != -999) ? caLeftClick : caLeftClickOutside; break; + case 0x0001: Action = (SlotNum != -999) ? caRightClick : caRightClickOutside; break; + case 0x0100: Action = caShiftLeftClick; break; + case 0x0101: Action = caShiftRightClick; break; + case 0x0200: Action = caNumber1; break; + case 0x0201: Action = caNumber2; break; + case 0x0202: Action = caNumber3; break; + case 0x0203: Action = caNumber4; break; + case 0x0204: Action = caNumber5; break; + case 0x0205: Action = caNumber6; break; + case 0x0206: Action = caNumber7; break; + case 0x0207: Action = caNumber8; break; + case 0x0208: Action = caNumber9; break; + case 0x0300: Action = caMiddleClick; break; + case 0x0400: Action = (SlotNum == -999) ? caLeftClickOutsideHoldNothing : caDropKey; break; + case 0x0401: Action = (SlotNum == -999) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; + case 0x0500: Action = (SlotNum == -999) ? caLeftPaintBegin : caUnknown; break; + case 0x0501: Action = (SlotNum != -999) ? caLeftPaintProgress : caUnknown; break; + case 0x0502: Action = (SlotNum == -999) ? caLeftPaintEnd : caUnknown; break; + case 0x0504: Action = (SlotNum == -999) ? caRightPaintBegin : caUnknown; break; + case 0x0505: Action = (SlotNum != -999) ? caRightPaintProgress : caUnknown; break; + case 0x0506: Action = (SlotNum == -999) ? caRightPaintEnd : caUnknown; break; + case 0x0600: Action = caDblClick; break; + } + + m_Client->HandleWindowClick(WindowID, SlotNum, Action, Item); } @@ -1315,7 +1399,8 @@ void cProtocol172::HandlePacketWindowClick(UInt32 a_RemainingBytes) void cProtocol172::HandlePacketWindowClose(UInt32 a_RemainingBytes) { - // TODO + HANDLE_READ(ReadChar, char, WindowID); + m_Client->HandleWindowClose(WindowID); } -- cgit v1.2.3 From 034a283a28cb08c654ef6bca1310ebb7c2fc5636 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 7 Nov 2013 17:11:36 +0100 Subject: Protocol 1.7: Fixed the Spawn Mob packet. This should fix #318 --- source/Protocol/Protocol17x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 99cc7b52c..ef8029433 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -636,7 +636,7 @@ void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) void cProtocol172::SendSpawnMob(const cMonster & a_Mob) { cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteInt(a_Mob.GetUniqueID()); + Pkt.WriteVarInt(a_Mob.GetUniqueID()); Pkt.WriteByte((Byte)a_Mob.GetMobType()); Pkt.WriteFPInt(a_Mob.GetPosX()); Pkt.WriteFPInt(a_Mob.GetPosY()); -- cgit v1.2.3 From 4722a90a0b99f75e7f48a417fce786e22778f574 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 7 Nov 2013 18:49:48 +0000 Subject: Possible fix for 1.7 pickup spawning --- source/Protocol/Protocol17x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index ef8029433..306b86987 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -658,7 +658,7 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) { cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteByte(a_ObjectType); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); -- cgit v1.2.3 From 21d835b594bd9d5ff20699b4195fe49b026c154b Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 7 Nov 2013 20:58:47 +0000 Subject: Fix for entity spawning in general --- source/Protocol/Protocol17x.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 306b86987..daea79578 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -447,7 +447,7 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) { { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteInt(a_Pickup.GetUniqueID()); + Pkt.WriteVarInt(a_Pickup.GetUniqueID()); Pkt.WriteByte(2); // Type = Pickup Pkt.WriteFPInt(a_Pickup.GetPosX()); Pkt.WriteFPInt(a_Pickup.GetPosY()); @@ -616,7 +616,7 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteInt(a_FallingBlock.GetUniqueID()); + Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); Pkt.WriteByte(70); // Falling block Pkt.WriteFPInt(a_FallingBlock.GetPosX()); Pkt.WriteFPInt(a_FallingBlock.GetPosY()); @@ -681,7 +681,7 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) { cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteInt(a_Vehicle.GetUniqueID()); + Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); Pkt.WriteByte(a_VehicleType); Pkt.WriteFPInt(a_Vehicle.GetPosX()); Pkt.WriteFPInt(a_Vehicle.GetPosY()); -- cgit v1.2.3 From 41451100c171af3c21e10141d6651b1ef89477b6 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 7 Nov 2013 22:33:46 +0000 Subject: Added hardcore client effect Also fixed multiple world comments issue. --- source/Protocol/Protocol17x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index daea79578..d45528c5f 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -420,7 +420,7 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { cPacketizer Pkt(*this, 0x01); // Join Game packet Pkt.WriteInt(a_Player.GetUniqueID()); - Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode()); + Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (cRoot::Get()->GetServer()->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 Pkt.WriteChar((char)a_World.GetDimension()); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) Pkt.WriteByte(cRoot::Get()->GetServer()->GetMaxPlayers()); -- cgit v1.2.3 From 64412c1fe36eb57ba771c8d2eb4092e77fec0502 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Nov 2013 20:56:19 +0100 Subject: Protocol 1.7: Copied Mob Metadata code from 1.2.5. --- source/Protocol/Protocol125.cpp | 16 +--- source/Protocol/Protocol17x.cpp | 199 +++++++++++++++++++++++++++++++++++++++- source/Protocol/Protocol17x.h | 1 + 3 files changed, 197 insertions(+), 19 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 4cb1197da..e53ab948c 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -29,21 +29,7 @@ Documentation: #include "../Entities/Minecart.h" #include "../Entities/FallingBlock.h" -#include "../Mobs/Monster.h" -#include "../Mobs/Creeper.h" -#include "../Mobs/Bat.h" -#include "../Mobs/Pig.h" -#include "../Mobs/Villager.h" -#include "../Mobs/Zombie.h" -#include "../Mobs/Ghast.h" -#include "../Mobs/Wolf.h" -#include "../Mobs/Sheep.h" -#include "../Mobs/Enderman.h" -#include "../Mobs/Skeleton.h" -#include "../Mobs/Witch.h" -#include "../Mobs/Slime.h" -#include "../Mobs/Magmacube.h" -#include "../Mobs/Horse.h" +#include "../Mobs/IncludeAllMonsters.h" diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index d45528c5f..7e4717c7a 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -20,6 +20,7 @@ Implements the 1.7.x protocol classes: #include "../Entities/FallingBlock.h" #include "../Entities/Pickup.h" #include "../Entities/Player.h" +#include "../Mobs/IncludeAllMonsters.h" #include "../UI/Window.h" @@ -282,11 +283,9 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) void cProtocol172::SendEntityProperties(const cEntity & a_Entity) { - /* cPacketizer Pkt(*this, 0x20); // Entity Properties packet Pkt.WriteInt(a_Entity.GetUniqueID()); - // TODO - */ + Pkt.WriteEntityProperties(a_Entity); } @@ -1664,7 +1663,199 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) { - // TODO + switch (a_Mob.GetMobType()) + { + case cMonster::mtCreeper: + { + WriteByte(0x10); + WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); + WriteByte(0x11); + WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); + break; + } + + case cMonster::mtBat: + { + WriteByte(0x10); + WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); + break; + } + + case cMonster::mtPig: + { + WriteByte(0x10); + WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); + break; + } + + case cMonster::mtVillager: + { + WriteByte(0x50); + WriteInt(((const cVillager &)a_Mob).GetVilType()); + break; + } + + case cMonster::mtZombie: + { + WriteByte(0x0c); + WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); + WriteByte(0x0d); + WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); + WriteByte(0x0e); + WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); + break; + } + + case cMonster::mtGhast: + { + WriteByte(0x10); + WriteByte(((const cGhast &)a_Mob).IsCharging()); + break; + } + + case cMonster::mtWolf: + { + const cWolf & Wolf = (const cWolf &)a_Mob; + Byte WolfStatus = 0; + if (Wolf.IsSitting()) + { + WolfStatus |= 0x1; + } + if (Wolf.IsAngry()) + { + WolfStatus |= 0x2; + } + if (Wolf.IsTame()) + { + WolfStatus |= 0x4; + } + WriteByte(0x10); + WriteByte(WolfStatus); + + WriteByte(0x72); + WriteFloat((float)(a_Mob.GetHealth())); + WriteByte(0x13); + WriteByte(Wolf.IsBegging() ? 1 : 0); + break; + } + + case cMonster::mtSheep: + { + WriteByte(0x10); + Byte SheepMetadata = 0; + SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); + if (((const cSheep &)a_Mob).IsSheared()) + { + SheepMetadata |= 0x10; + } + WriteByte(SheepMetadata); + break; + } + + case cMonster::mtEnderman: + { + WriteByte(0x10); + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); + WriteByte(0x11); + WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); + WriteByte(0x12); + WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); + break; + } + + case cMonster::mtSkeleton: + { + WriteByte(0x0d); + WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); + break; + } + + case cMonster::mtWitch: + { + WriteByte(0x15); + WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); + break; + } + + case cMonster::mtSlime: + { + WriteByte(0x10); + WriteByte(((const cSlime &)a_Mob).GetSize()); + break; + } + + case cMonster::mtMagmaCube: + { + WriteByte(0x10); + WriteByte(((const cMagmaCube &)a_Mob).GetSize()); + break; + } + + case cMonster::mtHorse: + { + const cHorse & Horse = (const cHorse &)a_Mob; + int Flags = 0; + if (Horse.IsTame()) + { + Flags |= 0x02; + } + if (Horse.IsSaddled()) + { + Flags |= 0x04; + } + if (Horse.IsChested()) + { + Flags |= 0x08; + } + if (Horse.IsBaby()) + { + Flags |= 0x10; + } + if (Horse.IsEating()) + { + Flags |= 0x20; + } + if (Horse.IsRearing()) + { + Flags |= 0x40; + } + if (Horse.IsMthOpen()) + { + Flags |= 0x80; + } + WriteByte(0x50); // Int at index 16 + WriteInt(Flags); + WriteByte(0x13); // Byte at index 19 + WriteByte(Horse.GetHorseType()); + WriteByte(0x54); // Int at index 20 + int Appearance = 0; + Appearance = Horse.GetHorseColor(); + Appearance |= Horse.GetHorseStyle() << 8; + WriteInt(Appearance); + WriteByte(0x56); // Int at index 22 + WriteInt(Horse.GetHorseArmour()); + break; + } + } // switch (a_Mob.GetType()) +} + + + + + +void cProtocol172::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) +{ + if (!a_Entity.IsMob()) + { + // No properties for anything else than mobs + WriteInt(0); + return; + } + const cMonster & Mob = (const cMonster &)a_Entity; + + // TODO: Send properties and modifiers based on the mob type + + WriteInt(0); // NumProperties } diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index 8cfb74004..ea7f7461f 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -168,6 +168,7 @@ protected: void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer void WriteEntityMetadata(const cEntity & a_Entity); // Writes the metadata for the specified entity, not including the terminating 0x7f void WriteMobMetadata(const cMonster & a_Mob); // Writes the mob-specific metadata for the specified mob + void WriteEntityProperties(const cEntity & a_Entity); // Writes the entity properties for the specified entity, including the Count field protected: cProtocol172 & m_Protocol; -- cgit v1.2.3 From 4707784929f5f8fa19cb015deb6fa3f346b4775c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Nov 2013 21:03:51 +0100 Subject: Protocol 1.7: Fixed using entities. The mouse buttons were swapped. --- source/Protocol/Protocol17x.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 7e4717c7a..a2b3a7a56 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -1342,8 +1342,7 @@ void cProtocol172::HandlePacketUseEntity(UInt32 a_RemainingBytes) { HANDLE_READ(ReadBEInt, int, EntityID); HANDLE_READ(ReadByte, Byte, MouseButton); - // TODO: Verify that this works, wiki.vg has no info on the MouseButton values - m_Client->HandleUseEntity(EntityID, (MouseButton == 0)); + m_Client->HandleUseEntity(EntityID, (MouseButton == 1)); } -- cgit v1.2.3 From 07fa8313b184e2a9d7666cf6f7b10d5def8dc928 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Nov 2013 21:32:14 +0100 Subject: cProtocol::SendWindowOpen() signature changed. This implements #313. --- source/Protocol/Protocol.h | 2 +- source/Protocol/Protocol125.cpp | 12 ++++++------ source/Protocol/Protocol125.h | 2 +- source/Protocol/Protocol15x.cpp | 13 +++++++------ source/Protocol/Protocol15x.h | 2 +- source/Protocol/Protocol16x.cpp | 17 +++++++++-------- source/Protocol/Protocol16x.h | 2 +- source/Protocol/Protocol17x.cpp | 23 +++++++++++++---------- source/Protocol/Protocol17x.h | 6 +++--- source/Protocol/ProtocolRecognizer.cpp | 4 ++-- source/Protocol/ProtocolRecognizer.h | 6 +++--- 11 files changed, 47 insertions(+), 42 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h index 5d66808cf..5023ea227 100644 --- a/source/Protocol/Protocol.h +++ b/source/Protocol/Protocol.h @@ -101,7 +101,7 @@ public: virtual void SendWeather (eWeather a_Weather) = 0; virtual void SendWholeInventory (const cWindow & a_Window) = 0; virtual void SendWindowClose (const cWindow & a_Window) = 0; - virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) = 0; + virtual void SendWindowOpen (const cWindow & a_Window) = 0; virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) = 0; /// Returns the ServerID used for authentication through session.minecraft.net diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index e53ab948c..9f2770815 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -956,19 +956,19 @@ void cProtocol125::SendWindowClose(const cWindow & a_Window) -void cProtocol125::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +void cProtocol125::SendWindowOpen(const cWindow & a_Window) { - if (a_WindowType < 0) + if (a_Window.GetWindowType() < 0) { // Do not send for inventory windows return; } cCSLock Lock(m_CSPacket); WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_WindowID); - WriteByte (a_WindowType); - WriteString(a_WindowTitle); - WriteByte (a_NumSlots); + WriteByte (a_Window.GetWindowID()); + WriteByte (a_Window.GetWindowType()); + WriteString(a_Window.GetWindowTitle()); + WriteByte (a_Window.GetNumNonInventorySlots()); Flush(); } diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index ad61dea74..db913bb57 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -78,7 +78,7 @@ public: virtual void SendWeather (eWeather a_Weather) override; virtual void SendWholeInventory (const cWindow & a_Window) override; virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWindowOpen (const cWindow & a_Window) override; virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; virtual AString GetAuthServerID(void) override; diff --git a/source/Protocol/Protocol15x.cpp b/source/Protocol/Protocol15x.cpp index cbae0700e..c337d26e7 100644 --- a/source/Protocol/Protocol15x.cpp +++ b/source/Protocol/Protocol15x.cpp @@ -12,6 +12,7 @@ Implements the 1.5.x protocol classes: #include "Protocol15x.h" #include "../ClientHandle.h" #include "../Item.h" +#include "../UI/Window.h" @@ -54,19 +55,19 @@ cProtocol150::cProtocol150(cClientHandle * a_Client) : -void cProtocol150::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +void cProtocol150::SendWindowOpen(const cWindow & a_Window) { - if (a_WindowType < 0) + if (a_Window.GetWindowType() < 0) { // Do not send for inventory windows return; } cCSLock Lock(m_CSPacket); WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_WindowID); - WriteByte (a_WindowType); - WriteString(a_WindowTitle); - WriteByte (a_NumSlots); + WriteByte (a_Window.GetWindowID()); + WriteByte (a_Window.GetWindowType()); + WriteString(a_Window.GetWindowTitle()); + WriteByte (a_Window.GetNumNonInventorySlots()); WriteByte (1); // Use title Flush(); } diff --git a/source/Protocol/Protocol15x.h b/source/Protocol/Protocol15x.h index 3e1547df8..e554fe130 100644 --- a/source/Protocol/Protocol15x.h +++ b/source/Protocol/Protocol15x.h @@ -28,7 +28,7 @@ class cProtocol150 : public: cProtocol150(cClientHandle * a_Client); - virtual void SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWindowOpen(const cWindow & a_Window) override; virtual int ParseWindowClick(void); } ; diff --git a/source/Protocol/Protocol16x.cpp b/source/Protocol/Protocol16x.cpp index 23e23d463..cfa27b3c4 100644 --- a/source/Protocol/Protocol16x.cpp +++ b/source/Protocol/Protocol16x.cpp @@ -17,6 +17,7 @@ Implements the 1.6.x protocol classes: #include "../ClientHandle.h" #include "../Entities/Entity.h" #include "../Entities/Player.h" +#include "../UI/Window.h" @@ -153,23 +154,23 @@ void cProtocol161::SendRespawn(void) -void cProtocol161::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +void cProtocol161::SendWindowOpen(const cWindow & a_Window) { - if (a_WindowType < 0) + if (a_Window.GetWindowType() < 0) { // Do not send for inventory windows return; } cCSLock Lock(m_CSPacket); WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_WindowID); - WriteByte (a_WindowType); - WriteString(a_WindowTitle); - WriteByte (a_NumSlots); + WriteByte (a_Window.GetWindowID()); + WriteByte (a_Window.GetWindowType()); + WriteString(a_Window.GetWindowTitle()); + WriteByte (a_Window.GetNumNonInventorySlots()); WriteByte (1); // Use title - if (a_WindowType == 11) // horse / donkey + if (a_Window.GetWindowType() == cWindow::wtAnimalChest) { - WriteInt(0); // Unknown value sent only when window type is 11 (horse / donkey) + WriteInt(0); // TODO: The animal's EntityID } Flush(); } diff --git a/source/Protocol/Protocol16x.h b/source/Protocol/Protocol16x.h index 2447f90a7..325e41c5a 100644 --- a/source/Protocol/Protocol16x.h +++ b/source/Protocol/Protocol16x.h @@ -42,7 +42,7 @@ protected: virtual void SendHealth (void) override; virtual void SendPlayerMaxSpeed(void) override; virtual void SendRespawn (void) override; - virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWindowOpen (const cWindow & a_Window) override; virtual int ParseEntityAction (void) override; virtual int ParsePlayerAbilities(void) override; diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index a2b3a7a56..eee4f7d0e 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -841,21 +841,24 @@ void cProtocol172::SendWindowClose(const cWindow & a_Window) -void cProtocol172::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +void cProtocol172::SendWindowOpen(const cWindow & a_Window) { + if (a_Window.GetWindowType() < 0) + { + // Do not send this packet for player inventory windows + return; + } + cPacketizer Pkt(*this, 0x2d); - Pkt.WriteChar(a_WindowID); - Pkt.WriteChar(a_WindowType); - Pkt.WriteString(a_WindowTitle); - Pkt.WriteChar(a_NumSlots); + Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteChar(a_Window.GetWindowType()); + Pkt.WriteString(a_Window.GetWindowTitle()); + Pkt.WriteChar(a_Window.GetNumNonInventorySlots()); Pkt.WriteBool(true); - /* - // TODO: - if (a_WindowType == cWindow::wtHorse) + if (a_Window.GetWindowType() == cWindow::wtAnimalChest) { - Pkt.WriteInt(HorseID); + Pkt.WriteInt(0); // TODO: The animal's EntityID } - */ } diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index ea7f7461f..844069403 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -85,9 +85,9 @@ public: 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWholeInventory (const cWindow & a_Window) override; + virtual void SendWindowClose (const cWindow & a_Window) override; + virtual void SendWindowOpen (const cWindow & a_Window) override; virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; virtual AString GetAuthServerID(void) override { return m_AuthServerID; } diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 501e8bbe0..6d0d71524 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -625,10 +625,10 @@ void cProtocolRecognizer::SendWindowClose(const cWindow & a_Window) -void cProtocolRecognizer::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) +void cProtocolRecognizer::SendWindowOpen(const cWindow & a_Window) { ASSERT(m_Protocol != NULL); - m_Protocol->SendWindowOpen(a_WindowID, a_WindowType, a_WindowTitle, a_NumSlots); + m_Protocol->SendWindowOpen(a_Window); } diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 9bf550309..2f217833a 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -111,9 +111,9 @@ public: 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots) override; + virtual void SendWholeInventory (const cWindow & a_Window) override; + virtual void SendWindowClose (const cWindow & a_Window) override; + virtual void SendWindowOpen (const cWindow & a_Window) override; virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; virtual AString GetAuthServerID(void) override; -- cgit v1.2.3 From efbc4a9b783f5ce734525e15f9787de1226ab5d3 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Nov 2013 22:09:39 +0100 Subject: Declaring 1.7.2 compatibility. --- source/Protocol/ProtocolRecognizer.cpp | 1 + source/Protocol/ProtocolRecognizer.h | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 6d0d71524..9234785b5 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -58,6 +58,7 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) case PROTO_VERSION_1_6_2: return "1.6.2"; case PROTO_VERSION_1_6_3: return "1.6.3"; case PROTO_VERSION_1_6_4: return "1.6.4"; + case PROTO_VERSION_1_7_2: return "1.7.2"; } ASSERT(!"Unknown protocol version"); return Printf("Unknown protocol (%d)", a_ProtocolVersion); diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 2f217833a..c085e2cd8 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -18,8 +18,8 @@ // Adjust these if a new protocol is added or an old one is removed: -#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4" -#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78" +#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.7.2" +#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78, 4" -- cgit v1.2.3 From 404034d230794df7864d7cd23605e56a4f9af264 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Nov 2013 22:40:31 +0100 Subject: Fixed sprinting in 1.7 protocol. Fixes #324 --- source/Protocol/Protocol17x.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index eee4f7d0e..9e230bafd 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -512,7 +512,23 @@ void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) void cProtocol172::SendPlayerMaxSpeed(void) { - SendPlayerAbilities(); + cPacketizer Pkt(*this, 0x20); // Entity Properties + Pkt.WriteInt(m_Client->GetPlayer()->GetUniqueID()); + Pkt.WriteInt(1); // Count + Pkt.WriteString("generic.movementSpeed"); + Pkt.WriteDouble(0.1); + if (m_Client->GetPlayer()->IsSprinting()) + { + Pkt.WriteShort(1); // Modifier count + Pkt.WriteInt64(0x662a6b8dda3e4c1c); + Pkt.WriteInt64(0x881396ea6097278d); // UUID of the modifier + Pkt.WriteDouble(0.3); + Pkt.WriteByte(2); + } + else + { + Pkt.WriteShort(0); // Modifier count + } } -- cgit v1.2.3 From 3fd71244352bae35a54be7f8a393c80a502466e4 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 10 Nov 2013 13:40:38 +0100 Subject: Fixed Player animation packet. Fixes #329. --- source/Protocol/Protocol17x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 9e230bafd..1e19385c6 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -490,7 +490,7 @@ void cProtocol172::SendPlayerAbilities(void) void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) { cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt(a_Player.GetUniqueID()); Pkt.WriteChar(a_Animation); } -- cgit v1.2.3 From fb3a175b28d3ef819876cf86e72a991f33ad5fcc Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 10 Nov 2013 14:19:02 +0100 Subject: Protocol 1.7: Attempt at fixing SoundParticleEffect packet. --- source/Protocol/Protocol17x.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 1e19385c6..ebf8f6c33 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -615,11 +615,9 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src { cPacketizer Pkt(*this, 0x28); // Effect packet Pkt.WriteInt(a_EffectID); - Pkt.WriteInt(a_SrcX); - // TODO: Check if this is really an int - // wiki.vg says it's a byte, but that wouldn't cover the entire range needed (Y location * 8 = 0..2048) - Pkt.WriteInt(a_SrcY); - Pkt.WriteInt(a_SrcZ); + Pkt.WriteInt(a_SrcX / 8); + Pkt.WriteByte(a_SrcY / 8); + Pkt.WriteInt(a_SrcZ / 8); Pkt.WriteInt(a_Data); Pkt.WriteBool(false); } -- cgit v1.2.3 From e62858ec3d027de8c5c4605913ab6261ec19d624 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Sun, 10 Nov 2013 20:12:30 +0100 Subject: Using DoWithPlayer instead of FindAndDoWithPlayer for callbacks. You are able to dye the collar. --- source/Protocol/Protocol17x.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index ebf8f6c33..5022d6dbc 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -1752,6 +1752,8 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) WriteFloat((float)(a_Mob.GetHealth())); WriteByte(0x13); WriteByte(Wolf.IsBegging() ? 1 : 0); + WriteByte(0x14); + WriteByte(Wolf.GetCollarColor()); break; } -- cgit v1.2.3 From 71abbb2f56c15634cca8615343d9699d0c50724d Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 10 Nov 2013 20:48:12 +0000 Subject: Bundled fixes [SEE DESC] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed pickups spawning in an incorrect position from a JukeBox * Pickups make a popping sound in Prtcl1.7 * Arrows make a *what sort of sound does an arrow make anyway‽* when hitting a block, and a popping sound when fired * Mobs again have metadata * Fixed Prtcl1.7 not using valid JSON to kick a client * Minecarts and arrows again have metadata --- source/Protocol/Protocol17x.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 9e230bafd..a962d238b 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -17,6 +17,7 @@ Implements the 1.7.x protocol classes: #include "../World.h" #include "../WorldStorage/FastNBT.h" #include "../StringCompression.h" +#include "../Entities/Minecart.h" #include "../Entities/FallingBlock.h" #include "../Entities/Pickup.h" #include "../Entities/Player.h" @@ -215,7 +216,7 @@ void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) void cProtocol172::SendDisconnect(const AString & a_Reason) { cPacketizer Pkt(*this, 0x40); - Pkt.WriteString(a_Reason); + Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); } @@ -615,9 +616,7 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src { cPacketizer Pkt(*this, 0x28); // Effect packet Pkt.WriteInt(a_EffectID); - Pkt.WriteInt(a_SrcX); - // TODO: Check if this is really an int - // wiki.vg says it's a byte, but that wouldn't cover the entire range needed (Y location * 8 = 0..2048) + Pkt.WriteByte(a_SrcX); Pkt.WriteInt(a_SrcY); Pkt.WriteInt(a_SrcZ); Pkt.WriteInt(a_Data); @@ -1666,12 +1665,43 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) WriteItem(((const cPickup &)a_Entity).GetItem()); break; } + case cEntity::etMinecart: + { + WriteByte(0x51); + + // The following expression makes Minecarts shake more with less health or higher damage taken + // It gets half the maximum health, and takes it away from the current health minus the half health: + /* Health: 5 | 3 - (5 - 3) = 1 (shake power) + Health: 3 | 3 - (3 - 3) = 3 + Health: 1 | 3 - (1 - 3) = 5 + */ + WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4); + WriteByte(0x52); + WriteInt(1); // Shaking direction, doesn't seem to affect anything + WriteByte(0x73); + WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer + + if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) + { + WriteByte(0x10); + WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); + } + break; + } + case cEntity::etProjectile: + { + if (((cProjectileEntity &)a_Entity).GetProjectileKind() == cProjectileEntity::pkArrow) + { + WriteByte(0x10); + WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); + } + break; + } case cEntity::etMonster: { WriteMobMetadata((const cMonster &)a_Entity); break; } - // TODO: Other types } } -- cgit v1.2.3 From 480ff3789bb2e6e99c95a1861672680a73c4c8fa Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 10 Nov 2013 22:55:42 +0100 Subject: Protocol 1.7: Fixed crashes and d/c with bad packets. Fixes #332. --- source/Protocol/Protocol17x.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index 5022d6dbc..78a55ccc0 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -215,7 +215,7 @@ void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) void cProtocol172::SendDisconnect(const AString & a_Reason) { cPacketizer Pkt(*this, 0x40); - Pkt.WriteString(a_Reason); + Pkt.WriteString(EscapeString(a_Reason)); } @@ -503,7 +503,7 @@ void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) cPacketizer Pkt(*this, 0x38); // Playerlist Item packet Pkt.WriteString(a_Player.GetName()); Pkt.WriteBool(a_IsOnline); - Pkt.WriteShort(a_Player.GetClientHandle()->GetPing()); + Pkt.WriteShort(a_IsOnline ? a_Player.GetClientHandle()->GetPing() : 0); } @@ -564,7 +564,7 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) { // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt(a_Player.GetUniqueID()); Pkt.WriteString(Printf("%d", a_Player.GetUniqueID())); // TODO: Proper UUID Pkt.WriteString(a_Player.GetName()); Pkt.WriteFPInt(a_Player.GetPosX()); -- cgit v1.2.3 From f713780db31e22345a1faf048ab948b1b4e03200 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 10 Nov 2013 22:20:25 +0000 Subject: Bundled fixes [SEE DESC] * Fixed compiler warning in Monster.cpp * Future proofed particle effects * Improved pickups, made less jittery --- source/Protocol/Protocol17x.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index a962d238b..d580be23b 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -491,7 +491,7 @@ void cProtocol172::SendPlayerAbilities(void) void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) { cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt(a_Player.GetUniqueID()); Pkt.WriteChar(a_Animation); } @@ -616,8 +616,8 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src { cPacketizer Pkt(*this, 0x28); // Effect packet Pkt.WriteInt(a_EffectID); - Pkt.WriteByte(a_SrcX); - Pkt.WriteInt(a_SrcY); + Pkt.WriteInt(a_SrcX); + Pkt.WriteByte(a_SrcY); Pkt.WriteInt(a_SrcZ); Pkt.WriteInt(a_Data); Pkt.WriteBool(false); -- cgit v1.2.3 From 347162a82f2cba75c4eb7739d4405ea78de4f0b5 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 12 Nov 2013 21:43:20 +0000 Subject: Bundled fixes [SEE DESC] * BoundingBox now returns FACE_NONE + Arrows can be picked up * Arrows dug up resume physics simulations * Added sound effects for bows, lava to stone, and arrows * Fixed SoundParticleEffect on <1.7 protocols --- source/Protocol/Protocol132.cpp | 6 +++--- source/Protocol/Protocol14x.cpp | 6 +++--- source/Protocol/Protocol17x.cpp | 10 +++++++--- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'source/Protocol') diff --git a/source/Protocol/Protocol132.cpp b/source/Protocol/Protocol132.cpp index 53159a3b3..22eac4312 100644 --- a/source/Protocol/Protocol132.cpp +++ b/source/Protocol/Protocol132.cpp @@ -392,9 +392,9 @@ void cProtocol132::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src cCSLock Lock(m_CSPacket); WriteByte(PACKET_SOUND_PARTICLE_EFFECT); WriteInt (a_EffectID); - WriteInt (a_SrcX / 8); - WriteByte(a_SrcY / 8); - WriteInt (a_SrcZ / 8); + WriteInt (a_SrcX); + WriteByte(a_SrcY); + WriteInt (a_SrcZ); WriteInt (a_Data); Flush(); } diff --git a/source/Protocol/Protocol14x.cpp b/source/Protocol/Protocol14x.cpp index ba9d7c01a..d2582458b 100644 --- a/source/Protocol/Protocol14x.cpp +++ b/source/Protocol/Protocol14x.cpp @@ -109,9 +109,9 @@ void cProtocol142::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src cCSLock Lock(m_CSPacket); WriteByte(PACKET_SOUND_PARTICLE_EFFECT); WriteInt (a_EffectID); - WriteInt (a_SrcX / 8); - WriteByte(a_SrcY / 8); - WriteInt (a_SrcZ / 8); + WriteInt (a_SrcX); + WriteByte(a_SrcY); + WriteInt (a_SrcZ); WriteInt (a_Data); WriteBool(0); Flush(); diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp index d580be23b..2ccf9230f 100644 --- a/source/Protocol/Protocol17x.cpp +++ b/source/Protocol/Protocol17x.cpp @@ -820,9 +820,13 @@ void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_Bloc void cProtocol172::SendWeather(eWeather a_Weather) { - cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte((a_Weather == wSunny) ? 2 : 1); // begin rain / end rain - Pkt.WriteFloat(0); // unused + { + cPacketizer Pkt(*this, 0x2b); // Change Game State packet + Pkt.WriteByte((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain + Pkt.WriteFloat(0); // Unused for weather + } + + // TODO: Fade effect, somehow } -- cgit v1.2.3 From 675b4aa878f16291ce33fced48a2bc7425f635ae Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sun, 24 Nov 2013 14:19:41 +0000 Subject: Moved source to src --- source/Protocol/ChunkDataSerializer.cpp | 176 --- source/Protocol/ChunkDataSerializer.h | 48 - source/Protocol/Protocol.h | 215 ---- source/Protocol/Protocol125.cpp | 1869 ------------------------------ source/Protocol/Protocol125.h | 157 --- source/Protocol/Protocol132.cpp | 950 --------------- source/Protocol/Protocol132.h | 102 -- source/Protocol/Protocol14x.cpp | 256 ----- source/Protocol/Protocol14x.h | 63 - source/Protocol/Protocol15x.cpp | 138 --- source/Protocol/Protocol15x.h | 38 - source/Protocol/Protocol16x.cpp | 268 ----- source/Protocol/Protocol16x.h | 76 -- source/Protocol/Protocol17x.cpp | 1917 ------------------------------- source/Protocol/Protocol17x.h | 258 ----- source/Protocol/ProtocolRecognizer.cpp | 905 --------------- source/Protocol/ProtocolRecognizer.h | 152 --- 17 files changed, 7588 deletions(-) delete mode 100644 source/Protocol/ChunkDataSerializer.cpp delete mode 100644 source/Protocol/ChunkDataSerializer.h delete mode 100644 source/Protocol/Protocol.h delete mode 100644 source/Protocol/Protocol125.cpp delete mode 100644 source/Protocol/Protocol125.h delete mode 100644 source/Protocol/Protocol132.cpp delete mode 100644 source/Protocol/Protocol132.h delete mode 100644 source/Protocol/Protocol14x.cpp delete mode 100644 source/Protocol/Protocol14x.h delete mode 100644 source/Protocol/Protocol15x.cpp delete mode 100644 source/Protocol/Protocol15x.h delete mode 100644 source/Protocol/Protocol16x.cpp delete mode 100644 source/Protocol/Protocol16x.h delete mode 100644 source/Protocol/Protocol17x.cpp delete mode 100644 source/Protocol/Protocol17x.h delete mode 100644 source/Protocol/ProtocolRecognizer.cpp delete mode 100644 source/Protocol/ProtocolRecognizer.h (limited to 'source/Protocol') diff --git a/source/Protocol/ChunkDataSerializer.cpp b/source/Protocol/ChunkDataSerializer.cpp deleted file mode 100644 index 2a9230fee..000000000 --- a/source/Protocol/ChunkDataSerializer.cpp +++ /dev/null @@ -1,176 +0,0 @@ - -// ChunkDataSerializer.cpp - -// Implements the cChunkDataSerializer class representing the object that can: -// - serialize chunk data to different protocol versions -// - cache such serialized data for multiple clients - -#include "Globals.h" -#include "ChunkDataSerializer.h" -#include "zlib.h" - - - - -cChunkDataSerializer::cChunkDataSerializer( - const cChunkDef::BlockTypes & a_BlockTypes, - const cChunkDef::BlockNibbles & a_BlockMetas, - const cChunkDef::BlockNibbles & a_BlockLight, - const cChunkDef::BlockNibbles & a_BlockSkyLight, - const unsigned char * a_BiomeData -) : - m_BlockTypes(a_BlockTypes), - m_BlockMetas(a_BlockMetas), - m_BlockLight(a_BlockLight), - m_BlockSkyLight(a_BlockSkyLight), - m_BiomeData(a_BiomeData) -{ -} - - - - -const AString & cChunkDataSerializer::Serialize(int a_Version) -{ - Serializations::const_iterator itr = m_Serializations.find(a_Version); - if (itr != m_Serializations.end()) - { - return itr->second; - } - - AString data; - switch (a_Version) - { - case RELEASE_1_2_5: Serialize29(data); break; - case RELEASE_1_3_2: Serialize39(data); break; - // TODO: Other protocol versions may serialize the data differently; implement here - - default: - { - LOGERROR("cChunkDataSerializer::Serialize(): Unknown version: %d", a_Version); - ASSERT(!"Unknown chunk data serialization version"); - break; - } - } - m_Serializations[a_Version] = data; - return m_Serializations[a_Version]; -} - - - - - -void cChunkDataSerializer::Serialize29(AString & a_Data) -{ - // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) - - const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; - const int MetadataOffset = sizeof(m_BlockTypes); - const int BlockLightOffset = MetadataOffset + sizeof(m_BlockMetas); - const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); - const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); - const int DataSize = BiomeOffset + BiomeDataSize; - - // Temporary buffer for the composed data: - char AllData [DataSize]; - - memcpy(AllData, m_BlockTypes, sizeof(m_BlockTypes)); - memcpy(AllData + MetadataOffset, m_BlockMetas, sizeof(m_BlockMetas)); - memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); - memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); - memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); - - // Compress the data: - // In order not to use allocation, use a fixed-size buffer, with the size - // that uses the same calculation as compressBound(): - const uLongf CompressedMaxSize = DataSize + (DataSize >> 12) + (DataSize >> 14) + (DataSize >> 25) + 16; - char CompressedBlockData[CompressedMaxSize]; - - uLongf CompressedSize = compressBound(DataSize); - - // Run-time check that our compile-time guess about CompressedMaxSize was enough: - ASSERT(CompressedSize <= CompressedMaxSize); - - compress2((Bytef*)CompressedBlockData, &CompressedSize, (const Bytef*)AllData, sizeof(AllData), Z_DEFAULT_COMPRESSION); - - // Now put all those data into a_Data: - - // "Ground-up continuous", or rather, "biome data present" flag: - a_Data.push_back('\x01'); - - // Two bitmaps; we're aways sending the full chunk with no additional data, so the bitmaps are 0xffff and 0, respectively - // Also, no endian flipping is needed because of the const values - unsigned short BitMap1 = 0xffff; - unsigned short BitMap2 = 0; - a_Data.append((const char *)&BitMap1, sizeof(short)); - a_Data.append((const char *)&BitMap2, sizeof(short)); - - Int32 CompressedSizeBE = htonl(CompressedSize); - a_Data.append((const char *)&CompressedSizeBE, sizeof(CompressedSizeBE)); - - Int32 UnusedInt32 = 0; - a_Data.append((const char *)&UnusedInt32, sizeof(UnusedInt32)); - - a_Data.append(CompressedBlockData, CompressedSize); -} - - - - - -void cChunkDataSerializer::Serialize39(AString & a_Data) -{ - // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) - - const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; - const int MetadataOffset = sizeof(m_BlockTypes); - const int BlockLightOffset = MetadataOffset + sizeof(m_BlockMetas); - const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); - const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); - const int DataSize = BiomeOffset + BiomeDataSize; - - // Temporary buffer for the composed data: - char AllData [DataSize]; - - memcpy(AllData, m_BlockTypes, sizeof(m_BlockTypes)); - memcpy(AllData + MetadataOffset, m_BlockMetas, sizeof(m_BlockMetas)); - memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); - memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); - memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); - - // Compress the data: - // In order not to use allocation, use a fixed-size buffer, with the size - // that uses the same calculation as compressBound(): - const uLongf CompressedMaxSize = DataSize + (DataSize >> 12) + (DataSize >> 14) + (DataSize >> 25) + 16; - char CompressedBlockData[CompressedMaxSize]; - - uLongf CompressedSize = compressBound(DataSize); - - // Run-time check that our compile-time guess about CompressedMaxSize was enough: - ASSERT(CompressedSize <= CompressedMaxSize); - - compress2((Bytef*)CompressedBlockData, &CompressedSize, (const Bytef*)AllData, sizeof(AllData), Z_DEFAULT_COMPRESSION); - - // Now put all those data into a_Data: - - // "Ground-up continuous", or rather, "biome data present" flag: - a_Data.push_back('\x01'); - - // Two bitmaps; we're aways sending the full chunk with no additional data, so the bitmaps are 0xffff and 0, respectively - // Also, no endian flipping is needed because of the const values - unsigned short BitMap1 = 0xffff; - unsigned short BitMap2 = 0; - a_Data.append((const char *)&BitMap1, sizeof(short)); - a_Data.append((const char *)&BitMap2, sizeof(short)); - - Int32 CompressedSizeBE = htonl(CompressedSize); - a_Data.append((const char *)&CompressedSizeBE, sizeof(CompressedSizeBE)); - - // Unlike 29, 39 doesn't have the "unused" int - - a_Data.append(CompressedBlockData, CompressedSize); -} - - - - diff --git a/source/Protocol/ChunkDataSerializer.h b/source/Protocol/ChunkDataSerializer.h deleted file mode 100644 index a42856356..000000000 --- a/source/Protocol/ChunkDataSerializer.h +++ /dev/null @@ -1,48 +0,0 @@ - -// ChunkDataSerializer.h - -// Interfaces to the cChunkDataSerializer class representing the object that can: -// - serialize chunk data to different protocol versions -// - cache such serialized data for multiple clients - - - - - -class cChunkDataSerializer -{ -protected: - const cChunkDef::BlockTypes & m_BlockTypes; - const cChunkDef::BlockNibbles & m_BlockMetas; - const cChunkDef::BlockNibbles & m_BlockLight; - const cChunkDef::BlockNibbles & m_BlockSkyLight; - const unsigned char * m_BiomeData; - - typedef std::map Serializations; - - Serializations m_Serializations; - - void Serialize29(AString & a_Data); // Release 1.2.4 and 1.2.5 - void Serialize39(AString & a_Data); // Release 1.3.1 and 1.3.2 - -public: - enum - { - RELEASE_1_2_5 = 29, - RELEASE_1_3_2 = 39, - } ; - - cChunkDataSerializer( - const cChunkDef::BlockTypes & a_BlockTypes, - const cChunkDef::BlockNibbles & a_BlockMetas, - const cChunkDef::BlockNibbles & a_BlockLight, - const cChunkDef::BlockNibbles & a_BlockSkyLight, - const unsigned char * a_BiomeData - ); - - const AString & Serialize(int a_Version); // Returns one of the internal m_Serializations[] -} ; - - - - diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h deleted file mode 100644 index 5023ea227..000000000 --- a/source/Protocol/Protocol.h +++ /dev/null @@ -1,215 +0,0 @@ - -// Protocol.h - -// Interfaces to the cProtocol class representing the generic interface that a protocol -// parser and serializer must implement - - - - - -#pragma once - -#include "../Defines.h" -#include "../Endianness.h" - - - - -class cPlayer; -class cEntity; -class cWindow; -class cInventory; -class cPawn; -class cPickup; -class cMonster; -class cChunkDataSerializer; -class cWorld; -class cFallingBlock; - - - - - -typedef unsigned char Byte; - - - - - -class cProtocol -{ -public: - cProtocol(cClientHandle * a_Client) : - m_Client(a_Client) - { - } - virtual ~cProtocol() {} - - /// Called when client sends some data - virtual void DataReceived(const char * a_Data, int a_Size) = 0; - - // Sending stuff to clients (alphabetically sorted): - virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) = 0; - virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) = 0; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) = 0; - virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0; - virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) = 0; - virtual void SendChat (const AString & a_Message) = 0; - virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) = 0; - virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) = 0; - virtual void SendDestroyEntity (const cEntity & a_Entity) = 0; - virtual void SendDisconnect (const AString & a_Reason) = 0; - virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) = 0; ///< Request the client to open up the sign editor for the sign (1.6+) - virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) = 0; - virtual void SendEntityHeadLook (const cEntity & a_Entity) = 0; - virtual void SendEntityLook (const cEntity & a_Entity) = 0; - virtual void SendEntityMetadata (const cEntity & a_Entity) = 0; - virtual void SendEntityProperties (const cEntity & a_Entity) = 0; - virtual void SendEntityRelMove (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) = 0; - virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) = 0; - virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) = 0; - virtual void SendEntityVelocity (const cEntity & a_Entity) = 0; - virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) = 0; - virtual void SendGameMode (eGameMode a_GameMode) = 0; - virtual void SendHealth (void) = 0; - virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) = 0; - virtual void SendKeepAlive (int a_PingID) = 0; - virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) = 0; - virtual void SendPickupSpawn (const cPickup & a_Pickup) = 0; - virtual void SendPlayerAbilities (void) = 0; - virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) = 0; - virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) = 0; - virtual void SendPlayerMaxSpeed (void) = 0; ///< Informs the client of the maximum player speed (1.6.1+) - virtual void SendPlayerMoveLook (void) = 0; - virtual void SendPlayerPosition (void) = 0; - virtual void SendPlayerSpawn (const cPlayer & a_Player) = 0; - virtual void SendRespawn (void) = 0; - virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) = 0; // a_Src coords are Block * 8 - virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) = 0; - virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) = 0; - virtual void SendSpawnMob (const cMonster & a_Mob) = 0; - virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) = 0; - virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) = 0; - virtual void SendTabCompletionResults(const AStringVector & a_Results) = 0; - virtual void SendTeleportEntity (const cEntity & a_Entity) = 0; - virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) = 0; - virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) = 0; - virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) = 0; - 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) = 0; - virtual void SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) = 0; - virtual void SendWeather (eWeather a_Weather) = 0; - virtual void SendWholeInventory (const cWindow & a_Window) = 0; - virtual void SendWindowClose (const cWindow & a_Window) = 0; - virtual void SendWindowOpen (const cWindow & a_Window) = 0; - virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) = 0; - - /// Returns the ServerID used for authentication through session.minecraft.net - virtual AString GetAuthServerID(void) = 0; - -protected: - cClientHandle * m_Client; - cCriticalSection m_CSPacket; //< Each SendXYZ() function must acquire this CS in order to send the whole packet at once - - /// 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, int a_Size) = 0; - - /// Called after writing each packet, enables descendants to flush their buffers - virtual void Flush(void) {}; - - // Helpers for writing partial packet data, write using SendData() - void WriteByte(Byte a_Value) - { - SendData((const char *)&a_Value, 1); - } - - void WriteShort(short a_Value) - { - a_Value = htons(a_Value); - SendData((const char *)&a_Value, 2); - } - - /* - void WriteShort(unsigned short a_Value) - { - a_Value = htons(a_Value); - SendData((const char *)&a_Value, 2); - } - */ - - void WriteInt(int a_Value) - { - a_Value = htonl(a_Value); - SendData((const char *)&a_Value, 4); - } - - void WriteUInt(unsigned int a_Value) - { - a_Value = htonl(a_Value); - SendData((const char *)&a_Value, 4); - } - - void WriteInt64 (Int64 a_Value) - { - a_Value = HostToNetwork8(&a_Value); - SendData((const char *)&a_Value, 8); - } - - void WriteFloat (float a_Value) - { - unsigned int val = HostToNetwork4(&a_Value); - SendData((const char *)&val, 4); - } - - void WriteDouble(double a_Value) - { - unsigned long long val = HostToNetwork8(&a_Value); - SendData((const char *)&val, 8); - } - - void WriteString(const AString & a_Value) - { - AString UTF16; - UTF8ToRawBEUTF16(a_Value.c_str(), a_Value.length(), UTF16); - WriteShort((unsigned short)(UTF16.size() / 2)); - SendData(UTF16.data(), UTF16.size()); - } - - void WriteBool(bool a_Value) - { - WriteByte(a_Value ? 1 : 0); - } - - void WriteVectorI(const Vector3i & a_Vector) - { - WriteInt(a_Vector.x); - WriteInt(a_Vector.y); - WriteInt(a_Vector.z); - } - - void WriteVarInt(UInt32 a_Value) - { - // A 32-bit integer can be encoded by at most 5 bytes: - unsigned char b[5]; - int idx = 0; - do - { - b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00); - a_Value = a_Value >> 7; - idx++; - } while (a_Value > 0); - - SendData((const char *)b, idx); - } - - void WriteVarUTF8String(const AString & a_String) - { - WriteVarInt(a_String.size()); - SendData(a_String.data(), a_String.size()); - } -} ; - - - - - diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp deleted file mode 100644 index 9f2770815..000000000 --- a/source/Protocol/Protocol125.cpp +++ /dev/null @@ -1,1869 +0,0 @@ - -// Protocol125.cpp - -// Implements the cProtocol125 class representing the release 1.2.5 protocol (#29) -/* -Documentation: - - protocol: http://wiki.vg/wiki/index.php?title=Protocol&oldid=2513 - - session handling: http://wiki.vg/wiki/index.php?title=Session&oldid=2262 - - slot format: http://wiki.vg/wiki/index.php?title=Slot_Data&oldid=2152 -*/ - -#include "Globals.h" - -#include "Protocol125.h" - -#include "../ClientHandle.h" -#include "../World.h" -#include "ChunkDataSerializer.h" -#include "../Entities/Entity.h" -#include "../Mobs/Monster.h" -#include "../Entities/Pickup.h" -#include "../Entities/Player.h" -#include "../ChatColor.h" -#include "../UI/Window.h" -#include "../Root.h" -#include "../Server.h" - -#include "../Entities/ProjectileEntity.h" -#include "../Entities/Minecart.h" -#include "../Entities/FallingBlock.h" - -#include "../Mobs/IncludeAllMonsters.h" - - - - - -enum -{ - PACKET_KEEP_ALIVE = 0x00, - PACKET_LOGIN = 0x01, - PACKET_HANDSHAKE = 0x02, - PACKET_CHAT = 0x03, - PACKET_UPDATE_TIME = 0x04, - PACKET_ENTITY_EQUIPMENT = 0x05, - PACKET_USE_ENTITY = 0x07, - PACKET_UPDATE_HEALTH = 0x08, - PACKET_RESPAWN = 0x09, - PACKET_PLAYER_ON_GROUND = 0x0a, - PACKET_PLAYER_POS = 0x0b, - PACKET_PLAYER_LOOK = 0x0c, - PACKET_PLAYER_MOVE_LOOK = 0x0d, - PACKET_BLOCK_DIG = 0x0e, - PACKET_BLOCK_PLACE = 0x0f, - PACKET_SLOT_SELECTED = 0x10, - PACKET_USE_BED = 0x11, - PACKET_ANIMATION = 0x12, - PACKET_PACKET_ENTITY_ACTION = 0x13, - PACKET_PLAYER_SPAWN = 0x14, - PACKET_PICKUP_SPAWN = 0x15, - PACKET_COLLECT_PICKUP = 0x16, - PACKET_SPAWN_OBJECT = 0x17, - PACKET_SPAWN_MOB = 0x18, - PACKET_ENTITY_VELOCITY = 0x1c, - PACKET_DESTROY_ENTITY = 0x1d, - PACKET_ENTITY = 0x1e, - PACKET_ENT_REL_MOVE = 0x1f, - PACKET_ENT_LOOK = 0x20, - PACKET_ENT_REL_MOVE_LOOK = 0x21, - PACKET_ENT_TELEPORT = 0x22, - PACKET_ENT_HEAD_LOOK = 0x23, - PACKET_ENT_STATUS = 0x26, - PACKET_ATTACH_ENTITY = 0x27, - PACKET_METADATA = 0x28, - PACKET_PRE_CHUNK = 0x32, - PACKET_MAP_CHUNK = 0x33, - PACKET_MULTI_BLOCK = 0x34, - PACKET_BLOCK_CHANGE = 0x35, - PACKET_BLOCK_ACTION = 0x36, - PACKET_EXPLOSION = 0x3C, - PACKET_SOUND_EFFECT = 0x3e, - PACKET_SOUND_PARTICLE_EFFECT = 0x3d, - PACKET_CHANGE_GAME_STATE = 0x46, - PACKET_THUNDERBOLT = 0x47, - PACKET_WINDOW_OPEN = 0x64, - PACKET_WINDOW_CLOSE = 0x65, - PACKET_WINDOW_CLICK = 0x66, - PACKET_INVENTORY_SLOT = 0x67, - PACKET_INVENTORY_WHOLE = 0x68, - PACKET_WINDOW_PROPERTY = 0x69, - PACKET_CREATIVE_INVENTORY_ACTION = 0x6B, - PACKET_UPDATE_SIGN = 0x82, - PACKET_PLAYER_LIST_ITEM = 0xC9, - PACKET_PLAYER_ABILITIES = 0xca, - PACKET_PLUGIN_MESSAGE = 0xfa, - PACKET_PING = 0xfe, - PACKET_DISCONNECT = 0xff -} ; - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return PARSE_INCOMPLETE; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - -typedef unsigned char Byte; - - - - - -cProtocol125::cProtocol125(cClientHandle * a_Client) : - super(a_Client), - m_ReceivedData(32 KiB) -{ -} - - - - - -void cProtocol125::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ATTACH_ENTITY); - WriteInt(a_Entity.GetUniqueID()); - WriteInt((a_Vehicle == NULL) ? -1 : a_Vehicle->GetUniqueID()); - Flush(); -} - - - - - -void cProtocol125::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) -{ - UNUSED(a_BlockType); - - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_BLOCK_ACTION); - WriteInt (a_BlockX); - WriteShort((short)a_BlockY); - WriteInt (a_BlockZ); - WriteByte (a_Byte1); - WriteByte (a_Byte2); - Flush(); -} - - - - - -void cProtocol125::SendBlockBreakAnim(int a_entityID, int a_BlockX, int a_BlockY, int a_BlockZ, char stage) -{ - // Not supported in this protocol version -} - - - - - -void cProtocol125::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_BLOCK_CHANGE); - WriteInt (a_BlockX); - WriteByte((unsigned char)a_BlockY); - WriteInt (a_BlockZ); - WriteByte(a_BlockType); - WriteByte(a_BlockMeta); - Flush(); -} - - - - - -void cProtocol125::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) -{ - cCSLock Lock(m_CSPacket); - if (a_Changes.size() == 1) - { - // Special packet for single-block changes - const sSetBlock & blk = a_Changes.front(); - SendBlockChange(a_ChunkX * cChunkDef::Width + blk.x, blk.y, a_ChunkZ * cChunkDef::Width + blk.z, blk.BlockType, blk.BlockMeta); - return; - } - - WriteByte (PACKET_MULTI_BLOCK); - WriteInt (a_ChunkX); - WriteInt (a_ChunkZ); - WriteShort((unsigned short)a_Changes.size()); - WriteUInt (sizeof(int) * a_Changes.size()); - for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) - { - unsigned int Coords = itr->y | (itr->z << 8) | (itr->x << 12); - unsigned int Blocks = itr->BlockMeta | (itr->BlockType << 4); - WriteUInt(Coords << 16 | Blocks); - } - Flush(); -} - - - - - -void cProtocol125::SendChat(const AString & a_Message) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_CHAT); - WriteString(a_Message); - Flush(); -} - - - - - -void cProtocol125::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) -{ - cCSLock Lock(m_CSPacket); - - // Send the pre-chunk: - SendPreChunk(a_ChunkX, a_ChunkZ, true); - - // Send the chunk data: - AString Serialized = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_2_5); - WriteByte(PACKET_MAP_CHUNK); - WriteInt (a_ChunkX); - WriteInt (a_ChunkZ); - SendData(Serialized.data(), Serialized.size()); - Flush(); -} - - - - - -void cProtocol125::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_COLLECT_PICKUP); - WriteInt (a_Pickup.GetUniqueID()); - WriteInt (a_Player.GetUniqueID()); - Flush(); -} - - - - - -void cProtocol125::SendDestroyEntity(const cEntity & a_Entity) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_DESTROY_ENTITY); - WriteInt (a_Entity.GetUniqueID()); - Flush(); -} - - - - - -void cProtocol125::SendDisconnect(const AString & a_Reason) -{ - cCSLock Lock(m_CSPacket); - WriteByte ((unsigned char)PACKET_DISCONNECT); - WriteString(a_Reason); - Flush(); -} - - - - - -void cProtocol125::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - // This protocol version doesn't support this packet, sign editor is invoked by the client automatically - UNUSED(a_BlockX); - UNUSED(a_BlockY); - UNUSED(a_BlockZ); -} - - - - - -void cProtocol125::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_ENTITY_EQUIPMENT); - WriteInt (a_Entity.GetUniqueID()); - WriteShort(a_SlotNum); - WriteShort(a_Item.m_ItemType); - WriteShort(a_Item.m_ItemDamage); - Flush(); -} - - - - - -void cProtocol125::SendEntityHeadLook(const cEntity & a_Entity) -{ - ASSERT(a_Entity.GetUniqueID() != m_Client->GetPlayer()->GetUniqueID()); // Must not send for self - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENT_HEAD_LOOK); - WriteInt (a_Entity.GetUniqueID()); - WriteByte((char)((a_Entity.GetHeadYaw() / 360.f) * 256)); - Flush(); -} - - - - - -void cProtocol125::SendEntityLook(const cEntity & a_Entity) -{ - ASSERT(a_Entity.GetUniqueID() != m_Client->GetPlayer()->GetUniqueID()); // Must not send for self - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENT_LOOK); - WriteInt (a_Entity.GetUniqueID()); - WriteByte((char)((a_Entity.GetRotation() / 360.f) * 256)); - WriteByte((char)((a_Entity.GetPitch() / 360.f) * 256)); - Flush(); -} - - - - - -void cProtocol125::SendEntityMetadata(const cEntity & a_Entity) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_METADATA); - WriteInt (a_Entity.GetUniqueID()); - - WriteCommonMetadata(a_Entity); - if (a_Entity.IsMob()) - { - WriteMobMetadata(((const cMonster &)a_Entity)); - } - else - { - WriteEntityMetadata(a_Entity); - } - WriteByte(0x7f); - - Flush(); -} - - - - - -void cProtocol125::SendEntityProperties(const cEntity & a_Entity) -{ - // Not supported in this protocol version -} - - - - - -void cProtocol125::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(a_Entity.GetUniqueID() != m_Client->GetPlayer()->GetUniqueID()); // Must not send for self - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENT_REL_MOVE); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(a_RelX); - WriteByte(a_RelY); - WriteByte(a_RelZ); - Flush(); -} - - - - - -void cProtocol125::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(a_Entity.GetUniqueID() != m_Client->GetPlayer()->GetUniqueID()); // Must not send for self - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENT_REL_MOVE_LOOK); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(a_RelX); - WriteByte(a_RelY); - WriteByte(a_RelZ); - WriteByte((char)((a_Entity.GetRotation() / 360.f) * 256)); - WriteByte((char)((a_Entity.GetPitch() / 360.f) * 256)); - Flush(); -} - - - - - -void cProtocol125::SendEntityStatus(const cEntity & a_Entity, char a_Status) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENT_STATUS); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(a_Status); - Flush(); -} - - - - - -void cProtocol125::SendEntityVelocity(const cEntity & a_Entity) -{ - ASSERT(a_Entity.GetUniqueID() != m_Client->GetPlayer()->GetUniqueID()); // Must not send for self - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENTITY_VELOCITY); - WriteInt (a_Entity.GetUniqueID()); - WriteShort((short) (a_Entity.GetSpeedX() * 400)); //400 = 8000 / 20 - WriteShort((short) (a_Entity.GetSpeedY() * 400)); - WriteShort((short) (a_Entity.GetSpeedZ() * 400)); - Flush(); -} - - - - - -void cProtocol125::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_EXPLOSION); - WriteDouble (a_BlockX); - WriteDouble (a_BlockY); - WriteDouble (a_BlockZ); - WriteFloat (a_Radius); - WriteInt (a_BlocksAffected.size()); - int BlockX = (int)a_BlockX; - int BlockY = (int)a_BlockY; - int BlockZ = (int)a_BlockZ; - for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(); itr != a_BlocksAffected.end(); ++itr) - { - WriteByte((Byte)(itr->x - BlockX)); - WriteByte((Byte)(itr->y - BlockY)); - WriteByte((Byte)(itr->z - BlockZ)); - } - WriteFloat((float)a_PlayerMotion.x); - WriteFloat((float)a_PlayerMotion.y); - WriteFloat((float)a_PlayerMotion.z); - Flush(); -} - - - - - -void cProtocol125::SendGameMode(eGameMode a_GameMode) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_CHANGE_GAME_STATE); - WriteByte(3); - WriteByte((char)a_GameMode); - Flush(); -} - - - - - -void cProtocol125::SendHandshake(const AString & a_ConnectionHash) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_HANDSHAKE); - WriteString(a_ConnectionHash); - Flush(); -} - - - - - -void cProtocol125::SendHealth(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_UPDATE_HEALTH); - WriteShort((short)m_Client->GetPlayer()->GetHealth()); - WriteShort(m_Client->GetPlayer()->GetFoodLevel()); - WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); - Flush(); -} - - - - - -void cProtocol125::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_INVENTORY_SLOT); - WriteByte (a_WindowID); - WriteShort(a_SlotNum); - WriteItem (a_Item); - Flush(); -} - - - - - -void cProtocol125::SendKeepAlive(int a_PingID) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_KEEP_ALIVE); - WriteInt (a_PingID); - Flush(); -} - - - - - -void cProtocol125::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - UNUSED(a_World); - cCSLock Lock(m_CSPacket); - - WriteByte (PACKET_LOGIN); - WriteInt (a_Player.GetUniqueID()); // EntityID of the player - WriteString(""); // Username, not used - WriteString("default"); // Level type - WriteInt ((int)a_Player.GetGameMode()); - WriteInt ((int)(a_World.GetDimension())); - WriteByte (2); // TODO: Difficulty - WriteByte (0); // Unused - WriteByte (60); // Client list width or something - Flush(); -} - - - - - -void cProtocol125::SendPickupSpawn(const cPickup & a_Pickup) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_PICKUP_SPAWN); - WriteInt (a_Pickup.GetUniqueID()); - WriteShort (a_Pickup.GetItem().m_ItemType); - WriteByte (a_Pickup.GetItem().m_ItemCount); - WriteShort (a_Pickup.GetItem().m_ItemDamage); - WriteVectorI((Vector3i)(a_Pickup.GetPosition() * 32)); - WriteByte ((char)(a_Pickup.GetSpeed().x * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().y * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().z * 8)); - Flush(); -} - - - - - -void cProtocol125::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ANIMATION); - WriteInt (a_Player.GetUniqueID()); - WriteByte(a_Animation); - Flush(); -} - - - - - -void cProtocol125::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) -{ - cCSLock Lock(m_CSPacket); - AString PlayerName(a_Player.GetColor()); - PlayerName.append(a_Player.GetName()); - if (PlayerName.length() > 14) - { - PlayerName.erase(14); - } - PlayerName += cChatColor::White; - - WriteByte ((unsigned char)PACKET_PLAYER_LIST_ITEM); - WriteString(PlayerName); - WriteBool (a_IsOnline); - WriteShort (a_IsOnline ? a_Player.GetClientHandle()->GetPing() : 0); - Flush(); -} - - - - - -void cProtocol125::SendPlayerMaxSpeed(void) -{ - // Not supported by this protocol version -} - - - - - -void cProtocol125::SendPlayerMoveLook(void) -{ - cCSLock Lock(m_CSPacket); - - /* - LOGD("Sending PlayerMoveLook: {%0.2f, %0.2f, %0.2f}, stance %0.2f, OnGround: %d", - m_Player->GetPosX(), m_Player->GetPosY(), m_Player->GetPosZ(), m_Player->GetStance(), m_Player->IsOnGround() ? 1 : 0 - ); - */ - - WriteByte (PACKET_PLAYER_MOVE_LOOK); - cPlayer * Player = m_Client->GetPlayer(); - WriteDouble(Player->GetPosX()); - WriteDouble(Player->GetStance() + 0.03); // Add a small amount so that the player doesn't start inside a block - WriteDouble(Player->GetPosY() + 0.03); // Add a small amount so that the player doesn't start inside a block - WriteDouble(Player->GetPosZ()); - WriteFloat ((float)(Player->GetRotation())); - WriteFloat ((float)(Player->GetPitch())); - WriteBool (Player->IsOnGround()); - Flush(); -} - - - - - -void cProtocol125::SendPlayerPosition(void) -{ - cCSLock Lock(m_CSPacket); - LOGD("Ignore send PlayerPos"); // PlayerPos is a C->S packet only now -} - - - - - -void cProtocol125::SendPlayerSpawn(const cPlayer & a_Player) -{ - const cItem & HeldItem = a_Player.GetEquippedItem(); - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_PLAYER_SPAWN); - WriteInt (a_Player.GetUniqueID()); - WriteString(a_Player.GetName()); - WriteInt ((int)(a_Player.GetPosX() * 32)); - WriteInt ((int)(a_Player.GetPosY() * 32)); - WriteInt ((int)(a_Player.GetPosZ() * 32)); - WriteByte ((char)((a_Player.GetRot().x / 360.f) * 256)); - WriteByte ((char)((a_Player.GetRot().y / 360.f) * 256)); - WriteShort (HeldItem.IsEmpty() ? 0 : HeldItem.m_ItemType); - Flush(); -} - - - - - -void cProtocol125::SendRespawn(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_RESPAWN); - WriteInt ((int)(m_Client->GetPlayer()->GetWorld()->GetDimension())); - WriteByte (2); // TODO: Difficulty; 2 = Normal - WriteByte ((char)m_Client->GetPlayer()->GetGameMode()); - WriteShort (256); // Current world height - WriteString("default"); -} - - - - - -void cProtocol125::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) -{ - // Not needed in this protocol version -} - - - - - -void cProtocol125::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) -{ - // Not implemented in this protocol version -} - - - - - -void cProtocol125::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) -{ - // This protocol version implements falling blocks using the spawn object / vehicle packet: - SendSpawnObject(a_FallingBlock, 70, a_FallingBlock.GetBlockType(), 0, 0); -} - - - - - -void cProtocol125::SendSpawnMob(const cMonster & a_Mob) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_SPAWN_MOB); - WriteInt (a_Mob.GetUniqueID()); - WriteByte (a_Mob.GetMobType()); - WriteVectorI((Vector3i)(a_Mob.GetPosition() * 32)); - WriteByte (0); - WriteByte (0); - WriteByte (0); - - WriteCommonMetadata(a_Mob); - WriteMobMetadata(a_Mob); - WriteByte(0x7f); - - Flush(); -} - - - - - -void cProtocol125::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) -{ - UNUSED(a_Yaw); - UNUSED(a_Pitch); - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_SPAWN_OBJECT); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(a_ObjectType); - WriteInt ((int)(a_Entity.GetPosX() * 32)); - WriteInt ((int)(a_Entity.GetPosY() * 32)); - WriteInt ((int)(a_Entity.GetPosZ() * 32)); - WriteByte(a_Pitch); - WriteByte(a_Yaw); - WriteInt (a_ObjectData); - if (a_ObjectData != 0) - { - WriteShort((short)(a_Entity.GetSpeedX() * 400)); - WriteShort((short)(a_Entity.GetSpeedY() * 400)); - WriteShort((short)(a_Entity.GetSpeedZ() * 400)); - } - Flush(); -} - - - - - -void cProtocol125::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_SPAWN_OBJECT); - WriteInt (a_Vehicle.GetUniqueID()); - WriteByte (a_VehicleType); - WriteInt ((int)(a_Vehicle.GetPosX() * 32)); - WriteInt ((int)(a_Vehicle.GetPosY() * 32)); - WriteInt ((int)(a_Vehicle.GetPosZ() * 32)); - WriteByte ((Byte)((a_Vehicle.GetPitch() / 360.f) * 256)); - WriteByte ((Byte)((a_Vehicle.GetRotation() / 360.f) * 256)); - WriteInt (a_VehicleSubType); - if (a_VehicleSubType != 0) - { - WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); - WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); - WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); - } - Flush(); -} - - - - - -void cProtocol125::SendTabCompletionResults(const AStringVector & a_Results) -{ - // This protocol version doesn't support tab completion - UNUSED(a_Results); -} - - - - - -void cProtocol125::SendTeleportEntity(const cEntity & a_Entity) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_ENT_TELEPORT); - WriteInt (a_Entity.GetUniqueID()); - WriteInt ((int)(floor(a_Entity.GetPosX() * 32))); - WriteInt ((int)(floor(a_Entity.GetPosY() * 32))); - WriteInt ((int)(floor(a_Entity.GetPosZ() * 32))); - WriteByte ((char)((a_Entity.GetRotation() / 360.f) * 256)); - WriteByte ((char)((a_Entity.GetPitch() / 360.f) * 256)); - Flush(); -} - - - - - -void cProtocol125::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_THUNDERBOLT); - WriteInt (0x7fffffff); // Entity ID of the thunderbolt; we use a constant one - WriteBool(true); // Unknown bool - WriteInt (a_BlockX * 32); - WriteInt (a_BlockY * 32); - WriteInt (a_BlockZ * 32); - Flush(); -} - - - - - -void cProtocol125::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_UPDATE_TIME); - // Use a_WorldAge for daycount, and a_TimeOfDay for the proper time of day: - WriteInt64((24000 * (a_WorldAge / 24000)) + (a_TimeOfDay % 24000)); - Flush(); -} - - - - - -void cProtocol125::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) -{ - cCSLock Lock(m_CSPacket); - SendPreChunk(a_ChunkX, a_ChunkZ, false); -} - - - - - -void cProtocol125::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 -) -{ - cCSLock Lock(m_CSPacket); - WriteByte ((unsigned char)PACKET_UPDATE_SIGN); - WriteInt (a_BlockX); - WriteShort ((short)a_BlockY); - WriteInt (a_BlockZ); - WriteString(a_Line1); - WriteString(a_Line2); - WriteString(a_Line3); - WriteString(a_Line4); - Flush(); -} - - - - - -void cProtocol125::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_USE_BED); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(0); // Unknown byte only 0 has been observed - WriteInt (a_BlockX); - WriteByte(a_BlockY); - WriteInt (a_BlockZ); - Flush(); -} - - - - - -void cProtocol125::SendWeather(eWeather a_Weather) -{ - cCSLock Lock(m_CSPacket); - switch( a_Weather ) - { - case eWeather_Sunny: - { - WriteByte(PACKET_CHANGE_GAME_STATE); - WriteByte(2); // Stop rain - WriteByte(0); // Unused - Flush(); - break; - } - - case eWeather_Rain: - case eWeather_ThunderStorm: - { - WriteByte(PACKET_CHANGE_GAME_STATE); - WriteByte(1); // Begin rain - WriteByte(0); // Unused - Flush(); - break; - } - } -} - - - - - -void cProtocol125::SendWholeInventory(const cWindow & a_Window) -{ - cCSLock Lock(m_CSPacket); - cItems Slots; - a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); - SendWindowSlots(a_Window.GetWindowID(), Slots.size(), &(Slots[0])); -} - - - - - -void cProtocol125::SendWindowClose(const cWindow & a_Window) -{ - if (a_Window.GetWindowType() == cWindow::wtInventory) - { - // Do not send inventory-window-close - return; - } - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_WINDOW_CLOSE); - WriteByte(a_Window.GetWindowID()); - Flush(); -} - - - - - -void cProtocol125::SendWindowOpen(const cWindow & a_Window) -{ - if (a_Window.GetWindowType() < 0) - { - // Do not send for inventory windows - return; - } - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_Window.GetWindowID()); - WriteByte (a_Window.GetWindowType()); - WriteString(a_Window.GetWindowTitle()); - WriteByte (a_Window.GetNumNonInventorySlots()); - Flush(); -} - - - - - -void cProtocol125::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_WINDOW_PROPERTY); - WriteByte (a_Window.GetWindowID()); - WriteShort(a_Property); - WriteShort(a_Value); - Flush(); -} - - - - - -AString cProtocol125::GetAuthServerID(void) -{ - // http://wiki.vg/wiki/index.php?title=Session&oldid=2262 - // The server generates a random hash and that is used for all clients, unmodified - return cRoot::Get()->GetServer()->GetServerID(); -} - - - - - -void cProtocol125::SendData(const char * a_Data, int a_Size) -{ - m_Client->SendData(a_Data, a_Size); -} - - - - - -void cProtocol125::DataReceived(const char * a_Data, int a_Size) -{ - if (!m_ReceivedData.Write(a_Data, a_Size)) - { - // Too much data in the incoming queue, report to caller: - m_Client->PacketBufferFull(); - return; - } - - // Parse and handle all complete packets in m_ReceivedData: - while (m_ReceivedData.CanReadBytes(1)) - { - unsigned char PacketType; - m_ReceivedData.ReadByte(PacketType); - switch (ParsePacket(PacketType)) - { - case PARSE_UNKNOWN: - { - // An unknown packet has been received, notify the client and abort: - m_Client->PacketUnknown(PacketType); - return; - } - case PARSE_ERROR: - { - // An error occurred while parsing a known packet, notify the client and abort: - m_Client->PacketError(PacketType); - return; - } - case PARSE_INCOMPLETE: - { - // Incomplete packet, bail out and process with the next batch of data - m_ReceivedData.ResetRead(); - return; - } - default: - { - // Packet successfully parsed, commit the read data and try again one more packet - m_ReceivedData.CommitRead(); - break; - } - } - } -} - - - - - -int cProtocol125::ParsePacket(unsigned char a_PacketType) -{ - switch (a_PacketType) - { - default: return PARSE_UNKNOWN; - case PACKET_ANIMATION: return ParseArmAnim(); - case PACKET_BLOCK_DIG: return ParseBlockDig(); - case PACKET_BLOCK_PLACE: return ParseBlockPlace(); - case PACKET_CHAT: return ParseChat(); - case PACKET_CREATIVE_INVENTORY_ACTION: return ParseCreativeInventoryAction(); - case PACKET_DISCONNECT: return ParseDisconnect(); - case PACKET_HANDSHAKE: return ParseHandshake(); - case PACKET_KEEP_ALIVE: return ParseKeepAlive(); - case PACKET_LOGIN: return ParseLogin(); - case PACKET_PACKET_ENTITY_ACTION: return ParseEntityAction(); - case PACKET_PING: return ParsePing(); - case PACKET_PLAYER_ABILITIES: return ParsePlayerAbilities(); - case PACKET_PLAYER_LOOK: return ParsePlayerLook(); - case PACKET_PLAYER_MOVE_LOOK: return ParsePlayerMoveLook(); - case PACKET_PLAYER_ON_GROUND: return ParsePlayerOnGround(); - case PACKET_PLAYER_POS: return ParsePlayerPosition(); - case PACKET_PLUGIN_MESSAGE: return ParsePluginMessage(); - case PACKET_RESPAWN: return ParseRespawn(); - case PACKET_SLOT_SELECTED: return ParseSlotSelected(); - case PACKET_UPDATE_SIGN: return ParseUpdateSign(); - case PACKET_USE_ENTITY: return ParseUseEntity(); - case PACKET_WINDOW_CLICK: return ParseWindowClick(); - case PACKET_WINDOW_CLOSE: return ParseWindowClose(); - } -} - - - - - -#define HANDLE_PACKET_PARSE(Packet) \ - { \ - int res = Packet.Parse(m_ReceivedData); \ - if (res < 0) \ - { \ - return res; \ - } \ - } - - - - - -int cProtocol125::ParseArmAnim(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, EntityID); - HANDLE_PACKET_READ(ReadChar, char, Animation); - m_Client->HandleAnimation(Animation); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseBlockDig(void) -{ - HANDLE_PACKET_READ(ReadChar, char, Status); - HANDLE_PACKET_READ(ReadBEInt, int, PosX); - HANDLE_PACKET_READ(ReadByte, Byte, PosY); - HANDLE_PACKET_READ(ReadBEInt, int, PosZ); - HANDLE_PACKET_READ(ReadChar, char, BlockFace); - m_Client->HandleLeftClick(PosX, PosY, PosZ, BlockFace, Status); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseBlockPlace(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, PosX); - HANDLE_PACKET_READ(ReadByte, Byte, PosY); - HANDLE_PACKET_READ(ReadBEInt, int, PosZ); - HANDLE_PACKET_READ(ReadChar, char, BlockFace); - - cItem HeldItem; - int res = ParseItem(HeldItem); - if (res < 0) - { - return res; - } - - // 1.2.5 didn't have any cursor position, so use 8, 8, 8, so that halfslabs and stairs work correctly and the special value is recognizable. - m_Client->HandleRightClick(PosX, PosY, PosZ, BlockFace, 8, 8, 8, HeldItem); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseChat(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Message); - m_Client->HandleChat(Message); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseCreativeInventoryAction(void) -{ - HANDLE_PACKET_READ(ReadBEShort, short, SlotNum); - cItem HeldItem; - int res = ParseItem(HeldItem); - if (res < 0) - { - return res; - } - m_Client->HandleCreativeInventory(SlotNum, HeldItem); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseDisconnect(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Reason); - m_Client->HandleDisconnect(Reason); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseEntityAction(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, EntityID); - HANDLE_PACKET_READ(ReadChar, char, ActionID); - m_Client->HandleEntityAction(EntityID, ActionID); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseHandshake(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Username); - - AStringVector UserData = StringSplit(Username, ";"); // "FakeTruth;localhost:25565" - if (UserData.empty()) - { - m_Client->Kick("Did not receive username"); - return PARSE_OK; - } - m_Username = UserData[0]; - - LOGD("HANDSHAKE %s", Username.c_str()); - - if (!m_Client->HandleHandshake( m_Username )) - { - return PARSE_OK; // Player is not allowed into the server - } - - SendHandshake(cRoot::Get()->GetServer()->GetServerID()); - LOGD("User \"%s\" was sent a handshake response", m_Username.c_str()); - - return PARSE_OK; -} - - - - - -int cProtocol125::ParseKeepAlive(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, KeepAliveID); - m_Client->HandleKeepAlive(KeepAliveID); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseLogin(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, ProtocolVersion); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Username); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, LevelType); - HANDLE_PACKET_READ(ReadBEInt, int, ServerMode); - HANDLE_PACKET_READ(ReadBEInt, int, Dimension); - HANDLE_PACKET_READ(ReadChar, char, Difficulty); - HANDLE_PACKET_READ(ReadByte, Byte, WorldHeight); - HANDLE_PACKET_READ(ReadByte, Byte, MaxPlayers); - - if (ProtocolVersion < 29) - { - m_Client->Kick("Your client is outdated!"); - return PARSE_OK; - } - else if (ProtocolVersion > 29) - { - m_Client->Kick("Your client version is higher than the server!"); - return PARSE_OK; - } - - if (m_Username.compare(Username) != 0) - { - LOGWARNING("Login Username (\"%s\") does not match Handshake username (\"%s\") for client @ \"%s\", kicking", - Username.c_str(), - m_Username.c_str(), - m_Client->GetIPString().c_str() - ); - m_Client->Kick("Hacked client"); // Don't tell them why we don't want them - return PARSE_OK; - } - - m_Client->HandleLogin(ProtocolVersion, Username); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePing(void) -{ - // Packet has no more data - m_Client->HandlePing(); - return PARSE_OK; -} - - - - - - -int cProtocol125::ParsePlayerAbilities(void) -{ - HANDLE_PACKET_READ(ReadBool, bool, Invulnerable); - HANDLE_PACKET_READ(ReadBool, bool, IsFlying); - HANDLE_PACKET_READ(ReadBool, bool, CanFly); - HANDLE_PACKET_READ(ReadBool, bool, InstaMine); - // TODO: m_Client->HandlePlayerAbilities(...); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePlayerLook(void) -{ - HANDLE_PACKET_READ(ReadBEFloat, float, Rotation); - HANDLE_PACKET_READ(ReadBEFloat, float, Pitch); - HANDLE_PACKET_READ(ReadBool, bool, IsOnGround); - m_Client->HandlePlayerLook(Rotation, Pitch, IsOnGround); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePlayerMoveLook(void) -{ - HANDLE_PACKET_READ(ReadBEDouble, double, PosX); - HANDLE_PACKET_READ(ReadBEDouble, double, PosY); - HANDLE_PACKET_READ(ReadBEDouble, double, Stance); - HANDLE_PACKET_READ(ReadBEDouble, double, PosZ); - HANDLE_PACKET_READ(ReadBEFloat, float, Rotation); - HANDLE_PACKET_READ(ReadBEFloat, float, Pitch); - HANDLE_PACKET_READ(ReadBool, bool, IsOnGround); - // LOGD("Recv PML: {%0.2f, %0.2f, %0.2f}, Stance %0.2f, Gnd: %d", PosX, PosY, PosZ, Stance, IsOnGround ? 1 : 0); - m_Client->HandlePlayerMoveLook(PosX, PosY, PosZ, Stance, Rotation, Pitch, IsOnGround); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePlayerOnGround(void) -{ - HANDLE_PACKET_READ(ReadBool, bool, IsOnGround); - // TODO: m_Client->HandleFlying(IsOnGround); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePlayerPosition(void) -{ - HANDLE_PACKET_READ(ReadBEDouble, double, PosX); - HANDLE_PACKET_READ(ReadBEDouble, double, PosY); - HANDLE_PACKET_READ(ReadBEDouble, double, Stance); - HANDLE_PACKET_READ(ReadBEDouble, double, PosZ); - HANDLE_PACKET_READ(ReadBool, bool, IsOnGround); - m_Client->HandlePlayerPos(PosX, PosY, PosZ, Stance, IsOnGround); - return PARSE_OK; -} - - - - - -int cProtocol125::ParsePluginMessage(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, ChannelName); - HANDLE_PACKET_READ(ReadBEShort, short, Length); - AString Data; - if (!m_ReceivedData.ReadString(Data, Length)) - { - m_ReceivedData.CheckValid(); - return PARSE_INCOMPLETE; - } - m_ReceivedData.CheckValid(); - - // TODO: Process the data - LOGD("Received %d bytes of plugin data on channel \"%s\".", Length, ChannelName.c_str()); - - return PARSE_OK; -} - - - - - -int cProtocol125::ParseRespawn(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, Dimension); - HANDLE_PACKET_READ(ReadChar, char, Difficulty); - HANDLE_PACKET_READ(ReadChar, char, CreativeMode); - HANDLE_PACKET_READ(ReadBEShort, short, WorldHeight); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, LevelType); - m_Client->HandleRespawn(); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseSlotSelected(void) -{ - HANDLE_PACKET_READ(ReadBEShort, short, SlotNum); - m_Client->HandleSlotSelected(SlotNum); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseUpdateSign(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, BlockX); - HANDLE_PACKET_READ(ReadBEShort, short, BlockY); - HANDLE_PACKET_READ(ReadBEInt, int, BlockZ); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Line1); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Line2); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Line3); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Line4); - m_Client->HandleUpdateSign(BlockX, BlockY, BlockZ, Line1, Line2, Line3, Line4); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseUseEntity(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, SourceEntityID); - HANDLE_PACKET_READ(ReadBEInt, int, TargetEntityID); - HANDLE_PACKET_READ(ReadBool, bool, IsLeftClick); - m_Client->HandleUseEntity(TargetEntityID, IsLeftClick); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseWindowClick(void) -{ - HANDLE_PACKET_READ(ReadChar, char, WindowID); - HANDLE_PACKET_READ(ReadBEShort, short, SlotNum); - HANDLE_PACKET_READ(ReadBool, bool, IsRightClick); - HANDLE_PACKET_READ(ReadBEShort, short, TransactionID); - HANDLE_PACKET_READ(ReadBool, bool, IsShiftPressed); - cItem HeldItem; - int res = ParseItem(HeldItem); - if (res < 0) - { - return res; - } - - // Convert IsShiftPressed, IsRightClick, SlotNum and HeldItem into eClickAction used in the newer protocols: - eClickAction Action; - if (IsRightClick) - { - if (IsShiftPressed) - { - Action = caShiftRightClick; - } - else - { - if (SlotNum == -999) - { - Action = (HeldItem.IsEmpty()) ? caRightClickOutsideHoldNothing : caRightClickOutside; - } - else - { - Action = caRightClick; - } - } - } - else - { - // IsLeftClick - if (IsShiftPressed) - { - Action = caShiftLeftClick; - } - else - { - if (SlotNum == -999) - { - Action = (HeldItem.IsEmpty()) ? caLeftClickOutsideHoldNothing : caRightClickOutside; - } - else - { - Action = caLeftClick; - } - } - } - m_Client->HandleWindowClick(WindowID, SlotNum, Action, HeldItem); - return PARSE_OK; -} - - - - - -int cProtocol125::ParseWindowClose(void) -{ - HANDLE_PACKET_READ(ReadChar, char, WindowID); - m_Client->HandleWindowClose(WindowID); - return PARSE_OK; -} - - - - - -void cProtocol125::SendPreChunk(int a_ChunkX, int a_ChunkZ, bool a_ShouldLoad) -{ - WriteByte(PACKET_PRE_CHUNK); - WriteInt (a_ChunkX); - WriteInt (a_ChunkZ); - WriteBool(a_ShouldLoad); - Flush(); -} - - - - - -void cProtocol125::SendWindowSlots(char a_WindowID, int a_NumItems, const cItem * a_Items) -{ - WriteByte (PACKET_INVENTORY_WHOLE); - WriteByte (a_WindowID); - WriteShort((short)a_NumItems); - - for (int j = 0; j < a_NumItems; j++) - { - WriteItem(a_Items[j]); - } - Flush(); -} - - - - - -void cProtocol125::WriteItem(const cItem & a_Item) -{ - short ItemType = a_Item.m_ItemType; - ASSERT(ItemType >= -1); // Check validity of packets in debug runtime - if (ItemType <= 0) - { - // Fix, to make sure no invalid values are sent. - ItemType = -1; - } - - WriteShort(ItemType); - if (a_Item.IsEmpty()) - { - return; - } - - WriteByte (a_Item.m_ItemCount); - WriteShort(a_Item.m_ItemDamage); - - if (cItem::IsEnchantable(a_Item.m_ItemType)) - { - // TODO: Implement enchantments - WriteShort(-1); - } -} - - - - - -int cProtocol125::ParseItem(cItem & a_Item) -{ - HANDLE_PACKET_READ(ReadBEShort, short, ItemType); - - if (ItemType <= -1) - { - a_Item.Empty(); - return PARSE_OK; - } - a_Item.m_ItemType = ItemType; - - HANDLE_PACKET_READ(ReadChar, char, ItemCount); - HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage); - a_Item.m_ItemCount = ItemCount; - a_Item.m_ItemDamage = ItemDamage; - if (ItemCount <= 0) - { - a_Item.Empty(); - } - - if (!cItem::IsEnchantable(ItemType)) - { - return PARSE_OK; - } - - HANDLE_PACKET_READ(ReadBEShort, short, EnchantNumBytes); - - if (EnchantNumBytes <= 0) - { - return PARSE_OK; - } - - // TODO: Enchantment not implemented yet! - if (!m_ReceivedData.SkipRead(EnchantNumBytes)) - { - return PARSE_INCOMPLETE; - } - - return PARSE_OK; -} - - - - - -void cProtocol125::WriteCommonMetadata(const cEntity & a_Entity) -{ - Byte CommonMetadata = 0; - - if (a_Entity.IsOnFire()) - { - CommonMetadata |= 0x1; - } - if (a_Entity.IsCrouched()) - { - CommonMetadata |= 0x2; - } - if (a_Entity.IsRiding()) - { - CommonMetadata |= 0x4; - } - if (a_Entity.IsSprinting()) - { - CommonMetadata |= 0x8; - } - if (a_Entity.IsRclking()) - { - CommonMetadata |= 0x10; - } - if (a_Entity.IsInvisible()) - { - CommonMetadata |= 0x20; - } - - WriteByte(0x0); - WriteByte(CommonMetadata); -} - - - - - -void cProtocol125::WriteEntityMetadata(const cEntity & a_Entity) -{ - if (a_Entity.IsMinecart()) - { - WriteByte(0x51); - // No idea how Mojang makes their carts shakey shakey, so here is a complicated one-liner expression that does something similar - WriteInt( (((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4 ); - WriteByte(0x52); - WriteInt(1); // Shaking direction, doesn't seem to affect anything - WriteByte(0x73); - WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer - - if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) - { - WriteByte(0x10); - WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); // Fueled? - } - } - else if ((a_Entity.IsProjectile() && ((cProjectileEntity &)a_Entity).GetProjectileKind() == cProjectileEntity::pkArrow)) - { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); // Critical hitting arrow? - } -} - - - - - -void cProtocol125::WriteMobMetadata(const cMonster & a_Mob) -{ - switch (a_Mob.GetMobType()) - { - case cMonster::mtCreeper: - { - WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); // Blowing up? - WriteByte(0x11); - WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); // Lightning-charged? - break; - } - case cMonster::mtBat: - { - WriteByte(0x10); - WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); // Upside down? - break; - } - case cMonster::mtPig: - { - WriteByte(0x10); - WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); // Saddled? - break; - } - case cMonster::mtVillager: - { - WriteByte(0x50); - WriteInt(((const cVillager &)a_Mob).GetVilType()); // What sort of TESTIFICATE? - break; - } - case cMonster::mtZombie: - { - WriteByte(0xC); - WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); // Babby zombie? - WriteByte(0xD); - WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); // Converted zombie? - WriteByte(0xE); - WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); // Converted-but-converting-back zombllager? - break; - } - case cMonster::mtGhast: - { - WriteByte(0x10); - WriteByte(((const cGhast &)a_Mob).IsCharging()); // About to eject un flamé-bol? :P - break; - } - case cMonster::mtWolf: - { - Byte WolfStatus = 0; - if (((const cWolf &)a_Mob).IsSitting()) - { - WolfStatus |= 0x1; - } - if (((const cWolf &)a_Mob).IsAngry()) - { - WolfStatus |= 0x2; - } - if (((const cWolf &)a_Mob).IsTame()) - { - WolfStatus |= 0x4; - } - WriteByte(0x10); - WriteByte(WolfStatus); - - WriteByte(0x72); - WriteFloat((float)(a_Mob.GetHealth())); // Tail health-o-meter (only shown when tamed, by the way) - WriteByte(0x13); - WriteByte(((const cWolf &)a_Mob).IsBegging() ? 1 : 0); // Ultra cute mode? - break; - } - case cMonster::mtSheep: - { - // [1](1111) - // [] = Is sheared? () = Color, from 0 to 15 - - WriteByte(0x10); - Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); // Fur colour - - if (((const cSheep &)a_Mob).IsSheared()) // Is sheared? - { - SheepMetadata |= 0x16; - } - WriteByte(SheepMetadata); - break; - } - case cMonster::mtEnderman: - { - WriteByte(0x10); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); // Block that he stole from your house - WriteByte(0x11); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); // Meta of block that he stole from your house - WriteByte(0x12); - WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); // Screaming at your face? - break; - } - case cMonster::mtSkeleton: - { - WriteByte(0xD); - WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); // It's a skeleton, but it's not - break; - } - case cMonster::mtWitch: - { - WriteByte(0x15); - WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); // Aggravated? Doesn't seem to do anything - break; - } - case cMonster::mtSlime: - case cMonster::mtMagmaCube: - { - WriteByte(0x10); - if (a_Mob.GetMobType() == cMonster::mtSlime) - { - WriteByte(((const cSlime &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME - } - else - { - WriteByte(((const cMagmaCube &)a_Mob).GetSize()); // Size of slime - HEWGE, meh, cute BABBY SLIME - } - break; - } - case cMonster::mtHorse: - { - int Flags = 0; - if (((const cHorse &)a_Mob).IsTame()) - { - Flags |= 0x2; - } - if (((const cHorse &)a_Mob).IsSaddled()) - { - Flags |= 0x4; - } - if (((const cHorse &)a_Mob).IsChested()) - { - Flags |= 0x8; - } - if (((const cHorse &)a_Mob).IsBaby()) - { - Flags |= 0x10; // IsBred flag, according to wiki.vg - don't think it does anything in multiplayer - } - if (((const cHorse &)a_Mob).IsEating()) - { - Flags |= 0x20; - } - if (((const cHorse &)a_Mob).IsRearing()) - { - Flags |= 0x40; - } - if (((const cHorse &)a_Mob).IsMthOpen()) - { - Flags |= 0x80; - } - WriteByte(0x50); - WriteInt(Flags); - - WriteByte(0x13); - WriteByte(((const cHorse &)a_Mob).GetHorseType()); // Type of horse (donkey, chestnut, etc.) - - WriteByte(0x54); - int Appearance = 0; - Appearance = ((const cHorse &)a_Mob).GetHorseColor(); // Mask FF - Appearance |= ((const cHorse &)a_Mob).GetHorseStyle() * 256; // Mask FF00, so multiply by 256 - WriteInt(Appearance); - - WriteByte(0x56); - WriteInt(((const cHorse &)a_Mob).GetHorseArmour()); // Horshey armour - break; - } - } -} - - - - diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h deleted file mode 100644 index db913bb57..000000000 --- a/source/Protocol/Protocol125.h +++ /dev/null @@ -1,157 +0,0 @@ - -// Protocol125.h - -// Interfaces to the cProtocol125 class representing the release 1.2.5 protocol (#29) - - - - - -#pragma once - -#include "Protocol.h" -#include "../ByteBuffer.h" - - - - - -class cProtocol125 : - public cProtocol -{ - typedef cProtocol super; -public: - cProtocol125(cClientHandle * a_Client); - - /// Called when client sends some data: - virtual void DataReceived(const char * a_Data, int a_Size) override; - - /// Sending stuff to clients (alphabetically sorted): - virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; - virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; - virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; - virtual void SendChat (const AString & a_Message) override; - virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; - virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) override; - virtual void SendDestroyEntity (const cEntity & a_Entity) override; - virtual void SendDisconnect (const AString & a_Reason) override; - virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) - virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; - virtual void SendEntityHeadLook (const cEntity & a_Entity) override; - virtual void SendEntityLook (const cEntity & a_Entity) override; - virtual void SendEntityMetadata (const cEntity & a_Entity) override; - virtual void SendEntityProperties (const cEntity & a_Entity) override; - virtual void SendEntityRelMove (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) override; - virtual void SendEntityVelocity (const cEntity & a_Entity) override; - virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override; - virtual void SendGameMode (eGameMode a_GameMode) override; - virtual void SendHealth (void) override; - virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; - virtual void SendKeepAlive (int a_PingID) override; - virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; - virtual void SendPickupSpawn (const cPickup & a_Pickup) override; - virtual void SendPlayerAbilities (void) override {} // This protocol doesn't support such message - virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; - virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; - virtual void SendPlayerMaxSpeed (void) override; - virtual void SendPlayerMoveLook (void) override; - virtual void SendPlayerPosition (void) override; - virtual void SendPlayerSpawn (const cPlayer & a_Player) override; - virtual void SendRespawn (void) override; - virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8 - virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; - virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; - virtual void SendSpawnMob (const cMonster & a_Mob) override; - virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; - virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; - virtual void SendTabCompletionResults(const AStringVector & a_Results) override; - virtual void SendTeleportEntity (const cEntity & a_Entity) override; - virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; - virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; - virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; - virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (const cWindow & a_Window) override; - virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; - - virtual AString GetAuthServerID(void) override; - -protected: - /// Results of packet-parsing: - enum { - PARSE_OK = 1, - PARSE_ERROR = -1, - PARSE_UNKNOWN = -2, - PARSE_INCOMPLETE = -3, - } ; - - cByteBuffer m_ReceivedData; ///< Buffer for the received data - - AString m_Username; ///< Stored in ParseHandshake(), compared to Login username - - virtual void SendData(const char * a_Data, int a_Size) override; - - /// Sends the Handshake packet - void SendHandshake(const AString & a_ConnectionHash); - - /// Parse the packet of the specified type from m_ReceivedData (switch into ParseXYZ() ) - virtual int ParsePacket(unsigned char a_PacketType); - - // Specific packet parsers: - virtual int ParseArmAnim (void); - virtual int ParseBlockDig (void); - virtual int ParseBlockPlace (void); - virtual int ParseChat (void); - virtual int ParseCreativeInventoryAction(void); - virtual int ParseDisconnect (void); - virtual int ParseEntityAction (void); - virtual int ParseHandshake (void); - virtual int ParseKeepAlive (void); - virtual int ParseLogin (void); - virtual int ParsePing (void); - virtual int ParsePlayerAbilities (void); - virtual int ParsePlayerLook (void); - virtual int ParsePlayerMoveLook (void); - virtual int ParsePlayerOnGround (void); - virtual int ParsePlayerPosition (void); - virtual int ParsePluginMessage (void); - virtual int ParseRespawn (void); - virtual int ParseSlotSelected (void); - virtual int ParseUpdateSign (void); - virtual int ParseUseEntity (void); - virtual int ParseWindowClick (void); - virtual int ParseWindowClose (void); - - // Utility functions: - /// Writes a "pre-chunk" packet - void SendPreChunk(int a_ChunkX, int a_ChunkZ, bool a_ShouldLoad); - - /// Writes a "set window items" packet with the specified params - void SendWindowSlots(char a_WindowID, int a_NumItems, const cItem * a_Items); - - /// Writes one item, "slot" as the protocol wiki calls it - virtual void WriteItem(const cItem & a_Item); - - /// Parses one item, "slot" as the protocol wiki calls it, from m_ReceivedData; returns the usual ParsePacket() codes - virtual int ParseItem(cItem & a_Item); - - /// Writes the COMMON entity metadata - void WriteCommonMetadata(const cEntity & a_Entity); - - /// Writes normal entity metadata - void WriteEntityMetadata(const cEntity & a_Entity); - - /// Writes mobile entity metadata - void WriteMobMetadata(const cMonster & a_Mob); -} ; - - - - diff --git a/source/Protocol/Protocol132.cpp b/source/Protocol/Protocol132.cpp deleted file mode 100644 index 22eac4312..000000000 --- a/source/Protocol/Protocol132.cpp +++ /dev/null @@ -1,950 +0,0 @@ - -// Protocol132.cpp - -// Implements the cProtocol132 class representing the release 1.3.2 protocol (#39) - -#include "Globals.h" -#include "Protocol132.h" -#include "../Root.h" -#include "../Server.h" -#include "../World.h" -#include "../ClientHandle.h" -#include "../../CryptoPP/randpool.h" -#include "../Item.h" -#include "ChunkDataSerializer.h" -#include "../Entities/Player.h" -#include "../Mobs/Monster.h" -#include "../UI/Window.h" -#include "../Entities/Pickup.h" -#include "../WorldStorage/FastNBT.h" -#include "../StringCompression.h" - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return PARSE_INCOMPLETE; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - -typedef unsigned char Byte; - - - - - -using namespace CryptoPP; - - - - - -const int MAX_ENC_LEN = 512; // Maximum size of the encrypted message; should be 128, but who knows... - - - - - -enum -{ - PACKET_KEEP_ALIVE = 0x00, - PACKET_LOGIN = 0x01, - PACKET_ENTITY_EQUIPMENT = 0x05, - PACKET_COMPASS = 0x06, - PACKET_PLAYER_SPAWN = 0x14, - PACKET_COLLECT_PICKUP = 0x16, - PACKET_SPAWN_MOB = 0x18, - PACKET_DESTROY_ENTITIES = 0x1d, - PACKET_CHUNK_DATA = 0x33, - PACKET_BLOCK_CHANGE = 0x35, - PACKET_BLOCK_ACTION = 0x36, - PACKET_BLOCK_BREAK_ANIM = 0x37, - PACKET_SOUND_EFFECT = 0x3e, - PACKET_SOUND_PARTICLE_EFFECT = 0x3d, - PACKET_TAB_COMPLETION = 0xcb, - PACKET_LOCALE_VIEW_DISTANCE = 0xcc, - PACKET_CLIENT_STATUSES = 0xcd, - PACKET_ENCRYPTION_KEY_RESP = 0xfc, -} ; - - - - - -// Converts a raw 160-bit SHA1 digest into a Java Hex representation -// According to http://wiki.vg/wiki/index.php?title=Protocol_Encryption&oldid=2802 -static void DigestToJava(byte a_Digest[20], AString & a_Out) -{ - bool IsNegative = (a_Digest[0] >= 0x80); - if (IsNegative) - { - // Two's complement: - bool carry = true; // Add one to the whole number - for (int i = 19; i >= 0; i--) - { - a_Digest[i] = ~a_Digest[i]; - if (carry) - { - carry = (a_Digest[i] == 0xff); - a_Digest[i]++; - } - } - } - a_Out.clear(); - a_Out.reserve(40); - for (int i = 0; i < 20; i++) - { - AppendPrintf(a_Out, "%02x", a_Digest[i]); - } - while ((a_Out.length() > 0) && (a_Out[0] == '0')) - { - a_Out.erase(0, 1); - } - if (IsNegative) - { - a_Out.insert(0, "-"); - } -} - - - - - -/* -// Self-test the hash formatting for known values: -// sha1(Notch) : 4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48 -// sha1(jeb_) : -7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1 -// sha1(simon) : 88e16a1019277b15d58faf0541e11910eb756f6 - -class Test -{ -public: - Test(void) - { - AString DigestNotch, DigestJeb, DigestSimon; - byte Digest[20]; - CryptoPP::SHA1 Checksum; - Checksum.Update((const byte *)"Notch", 5); - Checksum.Final(Digest); - DigestToJava(Digest, DigestNotch); - Checksum.Restart(); - Checksum.Update((const byte *)"jeb_", 4); - Checksum.Final(Digest); - DigestToJava(Digest, DigestJeb); - Checksum.Restart(); - Checksum.Update((const byte *)"simon", 5); - Checksum.Final(Digest); - DigestToJava(Digest, DigestSimon); - printf("Notch: \"%s\"", DigestNotch.c_str()); - printf("jeb_: \"%s\"", DigestJeb.c_str()); - printf("simon: \"%s\"", DigestSimon.c_str()); - } -} test; -*/ - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol132: - -cProtocol132::cProtocol132(cClientHandle * a_Client) : - super(a_Client), - m_IsEncrypted(false) -{ -} - - - - - -cProtocol132::~cProtocol132() -{ - if (!m_DataToSend.empty()) - { - LOGD("There are %d unsent bytes while deleting cProtocol132", m_DataToSend.size()); - } -} - - - - - -void cProtocol132::DataReceived(const char * a_Data, int a_Size) -{ - if (m_IsEncrypted) - { - byte Decrypted[512]; - while (a_Size > 0) - { - int NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; - m_Decryptor.ProcessData(Decrypted, (byte *)a_Data, NumBytes); - super::DataReceived((const char *)Decrypted, NumBytes); - a_Size -= NumBytes; - a_Data += NumBytes; - } - } - else - { - super::DataReceived(a_Data, a_Size); - } -} - - - - - -void cProtocol132::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_BLOCK_ACTION); - WriteInt (a_BlockX); - WriteShort((short)a_BlockY); - WriteInt (a_BlockZ); - WriteByte (a_Byte1); - WriteByte (a_Byte2); - WriteShort(a_BlockType); - Flush(); -} - - - - - -void cProtocol132::SendBlockBreakAnim(int a_entityID, int a_BlockX, int a_BlockY, int a_BlockZ, char stage) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_BLOCK_BREAK_ANIM); - WriteInt (a_entityID); - WriteInt (a_BlockX); - WriteInt (a_BlockY); - WriteInt (a_BlockZ); - WriteByte (stage); - Flush(); -} - - - - - -void cProtocol132::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_BLOCK_CHANGE); - WriteInt (a_BlockX); - WriteByte ((unsigned char)a_BlockY); - WriteInt (a_BlockZ); - WriteShort(a_BlockType); - WriteByte (a_BlockMeta); - Flush(); -} - - - - - -void cProtocol132::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) -{ - cCSLock Lock(m_CSPacket); - - // Pre-chunk not used in 1.3.2. Finally. - - // Send the chunk data: - AString Serialized = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); - WriteByte(PACKET_CHUNK_DATA); - WriteInt (a_ChunkX); - WriteInt (a_ChunkZ); - SendData(Serialized.data(), Serialized.size()); - Flush(); -} - - - - - -void cProtocol132::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_COLLECT_PICKUP); - WriteInt (a_Pickup.GetUniqueID()); - WriteInt (a_Player.GetUniqueID()); - Flush(); - - // Also send the "pop" sound effect with a somewhat random pitch (fast-random using EntityID ;) - SendSoundEffect( - "random.pop", - (int)(a_Pickup.GetPosX() * 8), (int)(a_Pickup.GetPosY() * 8), (int)(a_Pickup.GetPosZ() * 8), - 0.5, (float)(0.75 + ((float)((a_Pickup.GetUniqueID() * 23) % 32)) / 64) - ); -} - - - - - -void cProtocol132::SendDestroyEntity(const cEntity & a_Entity) -{ - if (a_Entity.GetUniqueID() == m_Client->GetPlayer()->GetUniqueID()) - { - // Do not send "destroy self" to the client, the client would crash (FS #254) - return; - } - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_DESTROY_ENTITIES); - WriteByte(1); // entity count - WriteInt (a_Entity.GetUniqueID()); - Flush(); -} - - - - - -void cProtocol132::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_ENTITY_EQUIPMENT); - WriteInt (a_Entity.GetUniqueID()); - WriteShort(a_SlotNum); - WriteItem (a_Item); - Flush(); -} - - - - - -void cProtocol132::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_LOGIN); - WriteInt (a_Player.GetUniqueID()); // EntityID of the player - WriteString("default"); // Level type - WriteByte ((int)a_Player.GetGameMode()); - WriteByte ((Byte)(a_World.GetDimension())); - WriteByte (2); // TODO: Difficulty - WriteByte (0); // Unused, used to be world height - WriteByte (8); // Client list width or something - Flush(); - - SendCompass(a_World); -} - - - - - -void cProtocol132::SendPlayerSpawn(const cPlayer & a_Player) -{ - const cItem & HeldItem = a_Player.GetEquippedItem(); - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_PLAYER_SPAWN); - WriteInt (a_Player.GetUniqueID()); - WriteString(a_Player.GetName()); - WriteInt ((int)(a_Player.GetPosX() * 32)); - WriteInt ((int)(a_Player.GetPosY() * 32)); - WriteInt ((int)(a_Player.GetPosZ() * 32)); - WriteByte ((char)((a_Player.GetRot().x / 360.f) * 256)); - WriteByte ((char)((a_Player.GetRot().y / 360.f) * 256)); - WriteShort (HeldItem.IsEmpty() ? 0 : HeldItem.m_ItemType); - // Player metadata: just use a default metadata value, since the client doesn't like starting without any metadata: - WriteByte (0); // Index 0, byte (flags) - WriteByte (0); // Flags, empty - WriteByte (0x7f); // End of metadata - Flush(); -} - - - - - -void cProtocol132::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_SOUND_EFFECT); - WriteString (a_SoundName); - WriteInt (a_SrcX); - WriteInt (a_SrcY); - WriteInt (a_SrcZ); - WriteFloat (a_Volume); - WriteByte ((char)(a_Pitch * 63.0f)); - Flush(); -} - - - - - -void cProtocol132::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_SOUND_PARTICLE_EFFECT); - WriteInt (a_EffectID); - WriteInt (a_SrcX); - WriteByte(a_SrcY); - WriteInt (a_SrcZ); - WriteInt (a_Data); - Flush(); -} - - - - - -void cProtocol132::SendSpawnMob(const cMonster & a_Mob) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_SPAWN_MOB); - WriteInt (a_Mob.GetUniqueID()); - WriteByte (a_Mob.GetMobType()); - WriteVectorI((Vector3i)(a_Mob.GetPosition() * 32)); - WriteByte ((Byte)((a_Mob.GetRotation() / 360.f) * 256)); - WriteByte ((Byte)((a_Mob.GetPitch() / 360.f) * 256)); - WriteByte ((Byte)((a_Mob.GetHeadYaw() / 360.f) * 256)); - WriteShort ((short)(a_Mob.GetSpeedX() * 400)); - WriteShort ((short)(a_Mob.GetSpeedY() * 400)); - WriteShort ((short)(a_Mob.GetSpeedZ() * 400)); - - WriteCommonMetadata(a_Mob); - WriteMobMetadata(a_Mob); - WriteByte(0x7f); - - Flush(); -} - - - - - -void cProtocol132::SendTabCompletionResults(const AStringVector & a_Results) -{ - if (a_Results.empty()) - { - // No results to send - return; - } - - AString Serialized(a_Results[0]); - for (AStringVector::const_iterator itr = a_Results.begin() + 1, end = a_Results.end(); itr != end; ++itr) - { - Serialized.push_back(0); - Serialized.append(*itr); - } // for itr - a_Results[] - - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_TAB_COMPLETION); - WriteString(Serialized); - Flush(); -} - - - - - -void cProtocol132::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) -{ - // Unloading the chunk is done by sending a "map chunk" packet - // with IncludeInitialize set to true and primary bitmap set to 0: - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_CHUNK_DATA); - WriteInt (a_ChunkX); - WriteInt (a_ChunkZ); - WriteBool(true); // IncludeInitialize - WriteShort(0); // Primary bitmap - WriteShort(0); // Add bitmap - WriteInt(0); - Flush(); -} - - - - - -void cProtocol132::SendWholeInventory(const cWindow & a_Window) -{ - // 1.3.2 requires player inventory slots to be sent as SetSlot packets, - // otherwise it sometimes fails to update the window - - // Send the entire window: - super::SendWholeInventory(a_Window); - - // Send the player inventory and hotbar: - const cInventory & Inventory = m_Client->GetPlayer()->GetInventory(); - int BaseOffset = a_Window.GetNumSlots() - (cInventory::invNumSlots - cInventory::invInventoryOffset); // Number of non-inventory slots - char WindowID = a_Window.GetWindowID(); - for (int i = 0; i < cInventory::invInventoryCount; i++) - { - SendInventorySlot(WindowID, BaseOffset + i, Inventory.GetInventorySlot(i)); - } // for i - Inventory[] - BaseOffset += cInventory::invInventoryCount; - for (int i = 0; i < cInventory::invHotbarCount; i++) - { - SendInventorySlot(WindowID, BaseOffset + i, Inventory.GetHotbarSlot(i)); - } // for i - Hotbar[] - - // Send even the item being dragged: - SendInventorySlot(-1, -1, m_Client->GetPlayer()->GetDraggingItem()); -} - - - - - -AString cProtocol132::GetAuthServerID(void) -{ - // http://wiki.vg/wiki/index.php?title=Session&oldid=2615 - // Server uses SHA1 to mix ServerID, Client secret and server public key together - // The mixing is done in StartEncryption, the result is in m_AuthServerID - - return m_AuthServerID; -} - - - - - -int cProtocol132::ParsePacket(unsigned char a_PacketType) -{ - switch (a_PacketType) - { - default: return super::ParsePacket(a_PacketType); // off-load previously known packets into cProtocol125 - case PACKET_CLIENT_STATUSES: return ParseClientStatuses(); - case PACKET_ENCRYPTION_KEY_RESP: return ParseEncryptionKeyResponse(); - case PACKET_LOCALE_VIEW_DISTANCE: return ParseLocaleViewDistance(); - case PACKET_TAB_COMPLETION: return ParseTabCompletion(); - } -} - - - - - -int cProtocol132::ParseBlockPlace(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, PosX); - HANDLE_PACKET_READ(ReadByte, Byte, PosY); - HANDLE_PACKET_READ(ReadBEInt, int, PosZ); - HANDLE_PACKET_READ(ReadChar, char, BlockFace); - - cItem HeldItem; - int res = ParseItem(HeldItem); - if (res < 0) - { - return res; - } - - HANDLE_PACKET_READ(ReadChar, char, CursorX); - HANDLE_PACKET_READ(ReadChar, char, CursorY); - HANDLE_PACKET_READ(ReadChar, char, CursorZ); - - m_Client->HandleRightClick(PosX, PosY, PosZ, BlockFace, CursorX, CursorY, CursorZ, HeldItem); - return PARSE_OK; -} - - - - - -int cProtocol132::ParseHandshake(void) -{ - HANDLE_PACKET_READ(ReadByte, Byte, ProtocolVersion); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Username); - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, ServerHost); - HANDLE_PACKET_READ(ReadBEInt, int, ServerPort); - m_Username = Username; - - if (!m_Client->HandleHandshake( m_Username )) - { - return PARSE_OK; // Player is not allowed into the server - } - - // Send a 0xFD Encryption Key Request http://wiki.vg/Protocol#0xFD - CryptoPP::StringSink sink(m_ServerPublicKey); // GCC won't allow inline instantiation in the following line, damned temporary refs - cRoot::Get()->GetServer()->GetPublicKey().Save(sink); - SendEncryptionKeyRequest(); - - return PARSE_OK; -} - - - - - -int cProtocol132::ParseClientStatuses(void) -{ - HANDLE_PACKET_READ(ReadByte, byte, Status); - if ((Status & 1) == 0) - { - m_Client->HandleLogin(39, m_Username); - } - else - { - m_Client->HandleRespawn(); - } - return PARSE_OK; -} - - - - - -int cProtocol132::ParseEncryptionKeyResponse(void) -{ - HANDLE_PACKET_READ(ReadBEShort, short, EncKeyLength); - AString EncKey; - if (!m_ReceivedData.ReadString(EncKey, EncKeyLength)) - { - return PARSE_INCOMPLETE; - } - HANDLE_PACKET_READ(ReadBEShort, short, EncNonceLength); - AString EncNonce; - if (!m_ReceivedData.ReadString(EncNonce, EncNonceLength)) - { - return PARSE_INCOMPLETE; - } - if ((EncKeyLength > MAX_ENC_LEN) || (EncNonceLength > MAX_ENC_LEN)) - { - LOGD("Too long encryption"); - m_Client->Kick("Hacked client"); - return PARSE_OK; - } - - HandleEncryptionKeyResponse(EncKey, EncNonce); - return PARSE_OK; -} - - - - - -int cProtocol132::ParseLocaleViewDistance(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Locale); - HANDLE_PACKET_READ(ReadChar, char, ViewDistance); - HANDLE_PACKET_READ(ReadChar, char, ChatFlags); - HANDLE_PACKET_READ(ReadChar, char, ClientDifficulty); - // TODO: m_Client->HandleLocale(Locale); - // TODO: m_Client->HandleViewDistance(ViewDistance); - // TODO: m_Client->HandleChatFlags(ChatFlags); - // Ignoring client difficulty - return PARSE_OK; -} - - - - - -int cProtocol132::ParseLogin(void) -{ - // Login packet not used in 1.3.2 - return PARSE_ERROR; -} - - - - - -int cProtocol132::ParsePlayerAbilities(void) -{ - HANDLE_PACKET_READ(ReadBool, bool, Flags); - HANDLE_PACKET_READ(ReadChar, char, FlyingSpeed); - HANDLE_PACKET_READ(ReadChar, char, WalkingSpeed); - // TODO: m_Client->HandlePlayerAbilities(...); - return PARSE_OK; -} - - - - - -int cProtocol132::ParseTabCompletion(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Text); - m_Client->HandleTabCompletion(Text); - return PARSE_OK; -} - - - - - -void cProtocol132::SendData(const char * a_Data, int a_Size) -{ - m_DataToSend.append(a_Data, a_Size); -} - - - - - -void cProtocol132::Flush(void) -{ - ASSERT(m_CSPacket.IsLockedByCurrentThread()); // Did all packets lock the CS properly? - - if (m_DataToSend.empty()) - { - LOGD("Flushing empty"); - return; - } - const char * a_Data = m_DataToSend.data(); - int a_Size = m_DataToSend.size(); - if (m_IsEncrypted) - { - byte Encrypted[8192]; // Larger buffer, we may be sending lots of data (chunks) - while (a_Size > 0) - { - int NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; - m_Encryptor.ProcessData(Encrypted, (byte *)a_Data, NumBytes); - super::SendData((const char *)Encrypted, NumBytes); - a_Size -= NumBytes; - a_Data += NumBytes; - } - } - else - { - super::SendData(a_Data, a_Size); - } - m_DataToSend.clear(); -} - - - - - -void cProtocol132::WriteItem(const cItem & a_Item) -{ - short ItemType = a_Item.m_ItemType; - ASSERT(ItemType >= -1); // Check validity of packets in debug runtime - if (ItemType <= 0) - { - // Fix, to make sure no invalid values are sent. - ItemType = -1; - } - - if (a_Item.IsEmpty()) - { - WriteShort(-1); - return; - } - - WriteShort(ItemType); - WriteByte (a_Item.m_ItemCount); - WriteShort(a_Item.m_ItemDamage); - - if (a_Item.m_Enchantments.IsEmpty()) - { - WriteShort(-1); - return; - } - - // Send the enchantments: - cFastNBTWriter Writer; - const char * TagName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench"; - a_Item.m_Enchantments.WriteToNBTCompound(Writer, TagName); - Writer.Finish(); - AString Compressed; - CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort(Compressed.size()); - SendData(Compressed.data(), Compressed.size()); -} - - - - - -int cProtocol132::ParseItem(cItem & a_Item) -{ - HANDLE_PACKET_READ(ReadBEShort, short, ItemType); - - if (ItemType <= -1) - { - a_Item.Empty(); - return PARSE_OK; - } - a_Item.m_ItemType = ItemType; - - HANDLE_PACKET_READ(ReadChar, char, ItemCount); - HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage); - a_Item.m_ItemCount = ItemCount; - a_Item.m_ItemDamage = ItemDamage; - if (ItemCount <= 0) - { - a_Item.Empty(); - } - - HANDLE_PACKET_READ(ReadBEShort, short, MetadataLength); - if (MetadataLength <= 0) - { - return PARSE_OK; - } - - // Read the metadata - AString Metadata; - Metadata.resize(MetadataLength); - if (!m_ReceivedData.ReadBuf((void *)Metadata.data(), MetadataLength)) - { - return PARSE_INCOMPLETE; - } - - return ParseItemMetadata(a_Item, Metadata); -} - - - - - -int cProtocol132::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) -{ - // Uncompress the GZIPped data: - AString Uncompressed; - if (UncompressStringGZIP(a_Metadata.data(), a_Metadata.size(), Uncompressed) != Z_OK) - { - AString HexDump; - CreateHexDump(HexDump, a_Metadata.data(), a_Metadata.size(), 16); - LOG("Cannot unGZIP item metadata:\n%s", HexDump.c_str()); - return PARSE_ERROR; - } - - // Parse into NBT: - cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); - if (!NBT.IsValid()) - { - AString HexDump; - CreateHexDump(HexDump, Uncompressed.data(), Uncompressed.size(), 16); - LOG("Cannot parse NBT item metadata:\n%s", HexDump.c_str()); - return PARSE_ERROR; - } - - // Load enchantments from the NBT: - for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag)) - { - if ( - (NBT.GetType(tag) == TAG_List) && - ( - (NBT.GetName(tag) == "ench") || - (NBT.GetName(tag) == "StoredEnchantments") - ) - ) - { - a_Item.m_Enchantments.ParseFromNBT(NBT, tag); - } - } - - return PARSE_OK; -} - - - - - -void cProtocol132::SendCompass(const cWorld & a_World) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_COMPASS); - WriteInt((int)(a_World.GetSpawnX())); - WriteInt((int)(a_World.GetSpawnY())); - WriteInt((int)(a_World.GetSpawnZ())); - Flush(); -} - - - - - -void cProtocol132::SendEncryptionKeyRequest(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte((char)0xfd); - WriteString(cRoot::Get()->GetServer()->GetServerID()); - WriteShort((short)m_ServerPublicKey.size()); - SendData(m_ServerPublicKey.data(), m_ServerPublicKey.size()); - WriteShort(4); - WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) - Flush(); -} - - - - - -void cProtocol132::HandleEncryptionKeyResponse(const AString & a_EncKey, const AString & a_EncNonce) -{ - // Decrypt EncNonce using privkey - RSAES::Decryptor rsaDecryptor(cRoot::Get()->GetServer()->GetPrivateKey()); - time_t CurTime = time(NULL); - CryptoPP::RandomPool rng; - rng.Put((const byte *)&CurTime, sizeof(CurTime)); - byte DecryptedNonce[MAX_ENC_LEN]; - DecodingResult res = rsaDecryptor.Decrypt(rng, (const byte *)a_EncNonce.data(), a_EncNonce.size(), DecryptedNonce); - if (!res.isValidCoding || (res.messageLength != 4)) - { - LOGD("Bad nonce length"); - m_Client->Kick("Hacked client"); - return; - } - if (ntohl(*((int *)DecryptedNonce)) != (unsigned)(uintptr_t)this) - { - LOGD("Bad nonce value"); - m_Client->Kick("Hacked client"); - return; - } - - // Decrypt the symmetric encryption key using privkey: - byte DecryptedKey[MAX_ENC_LEN]; - res = rsaDecryptor.Decrypt(rng, (const byte *)a_EncKey.data(), a_EncKey.size(), DecryptedKey); - if (!res.isValidCoding || (res.messageLength != 16)) - { - LOGD("Bad key length"); - m_Client->Kick("Hacked client"); - return; - } - - { - // Send encryption key response: - cCSLock Lock(m_CSPacket); - WriteByte((char)0xfc); - WriteShort(0); - WriteShort(0); - Flush(); - } - - StartEncryption(DecryptedKey); - return; -} - - - - - -void cProtocol132::StartEncryption(const byte * a_Key) -{ - m_Encryptor.SetKey(a_Key, 16, MakeParameters(Name::IV(), ConstByteArrayParameter(a_Key, 16))(Name::FeedbackSize(), 1)); - m_Decryptor.SetKey(a_Key, 16, MakeParameters(Name::IV(), ConstByteArrayParameter(a_Key, 16))(Name::FeedbackSize(), 1)); - m_IsEncrypted = true; - - // Prepare the m_AuthServerID: - CryptoPP::SHA1 Checksum; - AString ServerID = cRoot::Get()->GetServer()->GetServerID(); - Checksum.Update((const byte *)ServerID.c_str(), ServerID.length()); - Checksum.Update(a_Key, 16); - Checksum.Update((const byte *)m_ServerPublicKey.c_str(), m_ServerPublicKey.length()); - byte Digest[20]; - Checksum.Final(Digest); - DigestToJava(Digest, m_AuthServerID); -} - - - - diff --git a/source/Protocol/Protocol132.h b/source/Protocol/Protocol132.h deleted file mode 100644 index dc4d8aeef..000000000 --- a/source/Protocol/Protocol132.h +++ /dev/null @@ -1,102 +0,0 @@ - -// Protocol132.h - -// Interfaces to the cProtocol132 class representing the release 1.3.2 protocol (#39) - - - - - -#pragma once - -#include "Protocol125.h" -#include "../../CryptoPP/modes.h" -#include "../../CryptoPP/aes.h" - - - - - -class cProtocol132 : - public cProtocol125 -{ - typedef cProtocol125 super; -public: - - cProtocol132(cClientHandle * a_Client); - virtual ~cProtocol132(); - - /// Called when client sends some data: - virtual void DataReceived(const char * a_Data, int a_Size) override; - - // Sending commands (alphabetically sorted): - virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; - virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; - virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) override; - virtual void SendDestroyEntity (const cEntity & a_Entity) override; - virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; - virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; - virtual void SendPlayerSpawn (const cPlayer & a_Player) override; - virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8 - virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; - virtual void SendSpawnMob (const cMonster & a_Mob) override; - virtual void SendTabCompletionResults(const AStringVector & a_Results) override; - virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - - virtual AString GetAuthServerID(void) override; - - /// Handling of the additional packets: - virtual int ParsePacket(unsigned char a_PacketType) override; - - // Modified packets: - virtual int ParseBlockPlace (void) override; - virtual int ParseHandshake (void) override; - virtual int ParseLogin (void) override; - virtual int ParsePlayerAbilities(void) override; - - // New packets: - virtual int ParseClientStatuses (void); - virtual int ParseEncryptionKeyResponse(void); - virtual int ParseLocaleViewDistance (void); - virtual int ParseTabCompletion (void); - -protected: - bool m_IsEncrypted; - CryptoPP::CFB_Mode::Decryption m_Decryptor; - CryptoPP::CFB_Mode::Encryption m_Encryptor; - AString m_DataToSend; - - /// The ServerID used for session authentication; set in StartEncryption(), used in GetAuthServerID() - AString m_AuthServerID; - - /// The server's public key, as used by SendEncryptionKeyRequest() and StartEncryption() - AString m_ServerPublicKey; - - virtual void SendData(const char * a_Data, int a_Size) override; - - // DEBUG: - virtual void Flush(void) override; - - // Items in slots are sent differently - virtual void WriteItem(const cItem & a_Item) override; - virtual int ParseItem(cItem & a_Item) override; - - /// Parses the metadata that may come with the item. - int ParseItemMetadata(cItem & a_Item, const AString & a_Metadata); - - virtual void SendCompass(const cWorld & a_World); - virtual void SendEncryptionKeyRequest(void); - - /// Decrypts the key and nonce, checks nonce, starts the symmetric encryption - void HandleEncryptionKeyResponse(const AString & a_EncKey, const AString & a_EncNonce); - - /// Starts the symmetric encryption with the specified key; also sets m_AuthServerID - void StartEncryption(const byte * a_Key); -} ; - - - - diff --git a/source/Protocol/Protocol14x.cpp b/source/Protocol/Protocol14x.cpp deleted file mode 100644 index d2582458b..000000000 --- a/source/Protocol/Protocol14x.cpp +++ /dev/null @@ -1,256 +0,0 @@ - -// Protocol14x.cpp - -/* -Implements the 1.4.x protocol classes representing these protocols: -- cProtocol142: - - release 1.4.2 protocol (#47) - - release 1.4.4 protocol (#49) - the same protocol class is used, because the only difference is in a packet that MCServer doesn't implement yet (ITEM_DATA) - - release 1.4.5 protocol (same as 1.4.4) -- cProtocol146: - - release 1.4.6 protocol (#51) -*/ - -#include "Globals.h" -#include "Protocol14x.h" -#include "../Root.h" -#include "../Server.h" -#include "../ClientHandle.h" -#include "../../CryptoPP/randpool.h" -#include "../Item.h" -#include "ChunkDataSerializer.h" -#include "../Entities/Player.h" -#include "../Mobs/Monster.h" -#include "../UI/Window.h" -#include "../Entities/Pickup.h" -#include "../Entities/FallingBlock.h" - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return PARSE_INCOMPLETE; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - - -enum -{ - PACKET_UPDATE_TIME = 0x04, - PACKET_PICKUP_SPAWN = 0x15, - PACKET_SPAWN_OBJECT = 0x17, - PACKET_ENTITY_METADATA = 0x28, - PACKET_SOUND_PARTICLE_EFFECT = 0x3d -} ; - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol142: - -cProtocol142::cProtocol142(cClientHandle * a_Client) : - super(a_Client) -{ -} - - - - - -int cProtocol142::ParseLocaleViewDistance(void) -{ - HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Locale); - HANDLE_PACKET_READ(ReadChar, char, ViewDistance); - HANDLE_PACKET_READ(ReadChar, char, ChatFlags); - HANDLE_PACKET_READ(ReadChar, char, ClientDifficulty); - HANDLE_PACKET_READ(ReadChar, char, ShouldShowCape); // <-- new in 1.4.2 - // TODO: m_Client->HandleLocale(Locale); - // TODO: m_Client->HandleViewDistance(ViewDistance); - // TODO: m_Client->HandleChatFlags(ChatFlags); - // Ignoring client difficulty - return PARSE_OK; -} - - - - - -void cProtocol142::SendPickupSpawn(const cPickup & a_Pickup) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_PICKUP_SPAWN); - WriteInt (a_Pickup.GetUniqueID()); - WriteItem (a_Pickup.GetItem()); - WriteVectorI((Vector3i)(a_Pickup.GetPosition() * 32)); - WriteByte ((char)(a_Pickup.GetSpeed().x * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().y * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().z * 8)); - Flush(); -} - - - - - -void cProtocol142::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_SOUND_PARTICLE_EFFECT); - WriteInt (a_EffectID); - WriteInt (a_SrcX); - WriteByte(a_SrcY); - WriteInt (a_SrcZ); - WriteInt (a_Data); - WriteBool(0); - Flush(); -} - - - - - -void cProtocol142::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_UPDATE_TIME); - WriteInt64(a_WorldAge); - WriteInt64(a_TimeOfDay); - Flush(); -} - - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol146: - -cProtocol146::cProtocol146(cClientHandle * a_Client) : - super(a_Client) -{ -} - - - - - -void cProtocol146::SendPickupSpawn(const cPickup & a_Pickup) -{ - ASSERT(!a_Pickup.GetItem().IsEmpty()); - - cCSLock Lock(m_CSPacket); - - // Send a SPAWN_OBJECT packet for the base entity: - WriteByte(PACKET_SPAWN_OBJECT); - WriteInt (a_Pickup.GetUniqueID()); - WriteByte(0x02); - WriteInt ((int)(a_Pickup.GetPosX() * 32)); - WriteInt ((int)(a_Pickup.GetPosY() * 32)); - WriteInt ((int)(a_Pickup.GetPosZ() * 32)); - WriteInt (1); - WriteShort((short)(a_Pickup.GetSpeed().x * 32)); - WriteShort((short)(a_Pickup.GetSpeed().y * 32)); - WriteShort((short)(a_Pickup.GetSpeed().z * 32)); - WriteByte(0); - WriteByte(0); - - // Send a ENTITY_METADATA packet with the slot info: - WriteByte(PACKET_ENTITY_METADATA); - WriteInt(a_Pickup.GetUniqueID()); - WriteByte(0xaa); // a slot value at index 10 - WriteItem(a_Pickup.GetItem()); - WriteByte(0x7f); // End of metadata - Flush(); -} - - - - - -void cProtocol146::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) -{ - // Send a spawn object / vehicle packet - cCSLock Lock(m_CSPacket); - - WriteByte(PACKET_SPAWN_OBJECT); - WriteInt (a_FallingBlock.GetUniqueID()); - WriteByte(70); - WriteInt ((int)(a_FallingBlock.GetPosX() * 32)); - WriteInt ((int)(a_FallingBlock.GetPosY() * 32)); - WriteInt ((int)(a_FallingBlock.GetPosZ() * 32)); - WriteByte (0); // Pitch - WriteByte (0); // Yaw - WriteInt (a_FallingBlock.GetBlockType()); // data indicator = blocktype - WriteShort((short)(a_FallingBlock.GetSpeedX() * 400)); - WriteShort((short)(a_FallingBlock.GetSpeedY() * 400)); - WriteShort((short)(a_FallingBlock.GetSpeedZ() * 400)); - Flush(); -} - - - - - -void cProtocol146::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_SPAWN_OBJECT); - WriteInt (a_Entity.GetUniqueID()); - WriteByte(a_ObjectType); - WriteInt ((int)(a_Entity.GetPosX() * 32)); - WriteInt ((int)(a_Entity.GetPosY() * 32)); - WriteInt ((int)(a_Entity.GetPosZ() * 32)); - WriteByte(a_Pitch); - WriteByte(a_Yaw); - WriteInt (a_ObjectData); - if (a_ObjectData != 0) - { - WriteShort((short)(a_Entity.GetSpeedX() * 400)); - WriteShort((short)(a_Entity.GetSpeedY() * 400)); - WriteShort((short)(a_Entity.GetSpeedZ() * 400)); - } - Flush(); -} - - - - - -void cProtocol146::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_SPAWN_OBJECT); - WriteInt (a_Vehicle.GetUniqueID()); - WriteByte (a_VehicleType); - WriteInt ((int)(a_Vehicle.GetPosX() * 32)); - WriteInt ((int)(a_Vehicle.GetPosY() * 32)); - WriteInt ((int)(a_Vehicle.GetPosZ() * 32)); - WriteByte ((Byte)((a_Vehicle.GetPitch() / 360.f) * 256)); - WriteByte ((Byte)((a_Vehicle.GetRotation() / 360.f) * 256)); - WriteInt (a_VehicleSubType); - if (a_VehicleSubType != 0) - { - WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); - WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); - WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); - } - Flush(); -} - - - - - diff --git a/source/Protocol/Protocol14x.h b/source/Protocol/Protocol14x.h deleted file mode 100644 index ca497bbc1..000000000 --- a/source/Protocol/Protocol14x.h +++ /dev/null @@ -1,63 +0,0 @@ - -// Protocol14x.h - -/* -Interfaces to the 1.4.x protocol classes representing these protocols: -- cProtocol142: - - release 1.4.2 protocol (#47) - - release 1.4.4 protocol (#49) - the same protocol class is used, because the only difference is in a packet that MCServer doesn't implement yet (ITEM_DATA) - - release 1.4.5 protocol (same as 1.4.4) -- cProtocol146: - - release 1.4.6 protocol (#51) -*/ - - - - - -#pragma once - -#include "Protocol132.h" - - - - - -class cProtocol142 : - public cProtocol132 -{ - typedef cProtocol132 super; - -public: - cProtocol142(cClientHandle * a_Client); - - // Sending commands (alphabetically sorted): - virtual void SendPickupSpawn (const cPickup & a_Pickup) override; - virtual void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; - virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; - - // Specific packet parsers: - virtual int ParseLocaleViewDistance(void) override; -} ; - - - - - -class cProtocol146 : - public cProtocol142 -{ - typedef cProtocol142 super; - -public: - cProtocol146(cClientHandle * a_Client); - - virtual void SendPickupSpawn (const cPickup & a_Pickup) override; - virtual void SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) override; - virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; - virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; -} ; - - - - diff --git a/source/Protocol/Protocol15x.cpp b/source/Protocol/Protocol15x.cpp deleted file mode 100644 index c337d26e7..000000000 --- a/source/Protocol/Protocol15x.cpp +++ /dev/null @@ -1,138 +0,0 @@ - -// Protocol15x.cpp - -/* -Implements the 1.5.x protocol classes: - - cProtocol150 - - release 1.5 protocol (#60) - - release 1.5.2 protocol (#61, no relevant changes found) -*/ - -#include "Globals.h" -#include "Protocol15x.h" -#include "../ClientHandle.h" -#include "../Item.h" -#include "../UI/Window.h" - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return PARSE_INCOMPLETE; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - - -enum -{ - PACKET_WINDOW_OPEN = 0x64, -} ; - - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol150: - -cProtocol150::cProtocol150(cClientHandle * a_Client) : - super(a_Client) -{ -} - - - - - -void cProtocol150::SendWindowOpen(const cWindow & a_Window) -{ - if (a_Window.GetWindowType() < 0) - { - // Do not send for inventory windows - return; - } - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_Window.GetWindowID()); - WriteByte (a_Window.GetWindowType()); - WriteString(a_Window.GetWindowTitle()); - WriteByte (a_Window.GetNumNonInventorySlots()); - WriteByte (1); // Use title - Flush(); -} - - - - - -int cProtocol150::ParseWindowClick(void) -{ - HANDLE_PACKET_READ(ReadChar, char, WindowID); - HANDLE_PACKET_READ(ReadBEShort, short, SlotNum); - HANDLE_PACKET_READ(ReadByte, Byte, Button); - HANDLE_PACKET_READ(ReadBEShort, short, TransactionID); - HANDLE_PACKET_READ(ReadByte, Byte, Mode); - cItem HeldItem; - int res = ParseItem(HeldItem); - if (res < 0) - { - return res; - } - - // Convert Button, Mode, SlotNum and HeldItem into eClickAction: - eClickAction Action; - switch ((Mode << 8) | Button) - { - case 0x0000: Action = (SlotNum != -999) ? caLeftClick : caLeftClickOutside; break; - case 0x0001: Action = (SlotNum != -999) ? caRightClick : caRightClickOutside; break; - case 0x0100: Action = caShiftLeftClick; break; - case 0x0101: Action = caShiftRightClick; break; - case 0x0200: Action = caNumber1; break; - case 0x0201: Action = caNumber2; break; - case 0x0202: Action = caNumber3; break; - case 0x0203: Action = caNumber4; break; - case 0x0204: Action = caNumber5; break; - case 0x0205: Action = caNumber6; break; - case 0x0206: Action = caNumber7; break; - case 0x0207: Action = caNumber8; break; - case 0x0208: Action = caNumber9; break; - case 0x0300: Action = caMiddleClick; break; - case 0x0400: Action = (SlotNum == -999) ? caLeftClickOutsideHoldNothing : caDropKey; break; - case 0x0401: Action = (SlotNum == -999) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; - case 0x0500: Action = (SlotNum == -999) ? caLeftPaintBegin : caUnknown; break; - case 0x0501: Action = (SlotNum != -999) ? caLeftPaintProgress : caUnknown; break; - case 0x0502: Action = (SlotNum == -999) ? caLeftPaintEnd : caUnknown; break; - case 0x0504: Action = (SlotNum == -999) ? caRightPaintBegin : caUnknown; break; - case 0x0505: Action = (SlotNum != -999) ? caRightPaintProgress : caUnknown; break; - case 0x0506: Action = (SlotNum == -999) ? caRightPaintEnd : caUnknown; break; - case 0x0600: Action = caDblClick; break; - } - - if (Action == caUnknown) - { - LOGWARNING("Received an unknown click action combination: Mode = %d, Button = %d, Slot = %d, HeldItem = %s. Ignoring packet.", - Mode, Button, SlotNum, ItemToFullString(HeldItem).c_str() - ); - ASSERT(!"Unknown click action"); - return PARSE_OK; - } - - m_Client->HandleWindowClick(WindowID, SlotNum, Action, HeldItem); - return PARSE_OK; -} - - - - - diff --git a/source/Protocol/Protocol15x.h b/source/Protocol/Protocol15x.h deleted file mode 100644 index e554fe130..000000000 --- a/source/Protocol/Protocol15x.h +++ /dev/null @@ -1,38 +0,0 @@ - -// Protocol15x.h - -/* -Declares the 1.5.x protocol classes: - - cProtocol150 - - release 1.5 and 1.5.1 protocol (#60) - - release 1.5.2 protocol (#61; no relevant changes found) -*/ - - - - - -#pragma once - -#include "Protocol14x.h" - - - - - -class cProtocol150 : - public cProtocol146 -{ - typedef cProtocol146 super; - -public: - cProtocol150(cClientHandle * a_Client); - - virtual void SendWindowOpen(const cWindow & a_Window) override; - - virtual int ParseWindowClick(void); -} ; - - - - diff --git a/source/Protocol/Protocol16x.cpp b/source/Protocol/Protocol16x.cpp deleted file mode 100644 index cfa27b3c4..000000000 --- a/source/Protocol/Protocol16x.cpp +++ /dev/null @@ -1,268 +0,0 @@ - -// Protocol16x.cpp - -/* -Implements the 1.6.x protocol classes: - - cProtocol161 - - release 1.6.1 protocol (#73) - - cProtocol162 - - release 1.6.2 protocol (#74) - - release 1.6.3 protocol (#77) - no relevant changes - - release 1.6.4 protocol (#78) - no relevant changes -(others may be added later in the future for the 1.6 release series) -*/ - -#include "Globals.h" -#include "Protocol16x.h" -#include "../ClientHandle.h" -#include "../Entities/Entity.h" -#include "../Entities/Player.h" -#include "../UI/Window.h" - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return PARSE_INCOMPLETE; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - - -enum -{ - PACKET_CHAT = 0x03, - PACKET_UPDATE_HEALTH = 0x08, - PACKET_STEER_VEHICLE = 0x1b, - PACKET_ATTACH_ENTITY = 0x27, - PACKET_ENTITY_PROPERTIES = 0x2c, - PACKET_WINDOW_OPEN = 0x64, - PACKET_TILE_EDITOR_OPEN = 0x85, - PACKET_PLAYER_ABILITIES = 0xca, -} ; - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol161: - -cProtocol161::cProtocol161(cClientHandle * a_Client) : - super(a_Client) -{ -} - - - - - -void cProtocol161::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ATTACH_ENTITY); - WriteInt(a_Entity.GetUniqueID()); - WriteInt((a_Vehicle == NULL) ? -1 : a_Vehicle->GetUniqueID()); - WriteBool(false); // TODO: "Should use leash?" -> no - Flush(); -} - - - - - -void cProtocol161::SendChat(const AString & a_Message) -{ - super::SendChat(Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str())); -} - - - - - -void cProtocol161::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_TILE_EDITOR_OPEN); - WriteByte(0); - WriteInt(a_BlockX); - WriteInt(a_BlockY); - WriteInt(a_BlockZ); - Flush(); -} - - - - - -void cProtocol161::SendGameMode(eGameMode a_GameMode) -{ - super::SendGameMode(a_GameMode); - SendPlayerMaxSpeed(); -} - - - - - -void cProtocol161::SendHealth(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_UPDATE_HEALTH); - WriteFloat((float)m_Client->GetPlayer()->GetHealth()); - WriteShort(m_Client->GetPlayer()->GetFoodLevel()); - WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); - Flush(); -} - - - - - -void cProtocol161::SendPlayerMaxSpeed(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENTITY_PROPERTIES); - WriteInt(m_Client->GetPlayer()->GetUniqueID()); - WriteInt(1); - WriteString("generic.movementSpeed"); - WriteDouble(m_Client->GetPlayer()->GetMaxSpeed()); - Flush(); -} - - - - - -void cProtocol161::SendRespawn(void) -{ - // Besides sending the respawn, we need to also send the player max speed, otherwise the client reverts to super-fast - super::SendRespawn(); - SendPlayerMaxSpeed(); -} - - - - - -void cProtocol161::SendWindowOpen(const cWindow & a_Window) -{ - if (a_Window.GetWindowType() < 0) - { - // Do not send for inventory windows - return; - } - cCSLock Lock(m_CSPacket); - WriteByte (PACKET_WINDOW_OPEN); - WriteByte (a_Window.GetWindowID()); - WriteByte (a_Window.GetWindowType()); - WriteString(a_Window.GetWindowTitle()); - WriteByte (a_Window.GetNumNonInventorySlots()); - WriteByte (1); // Use title - if (a_Window.GetWindowType() == cWindow::wtAnimalChest) - { - WriteInt(0); // TODO: The animal's EntityID - } - Flush(); -} - - - - - -int cProtocol161::ParseEntityAction(void) -{ - HANDLE_PACKET_READ(ReadBEInt, int, EntityID); - HANDLE_PACKET_READ(ReadChar, char, ActionID); - HANDLE_PACKET_READ(ReadBEInt, int, UnknownHorseVal); - m_Client->HandleEntityAction(EntityID, ActionID); - return PARSE_OK; -} - - - - - -int cProtocol161::ParsePlayerAbilities(void) -{ - HANDLE_PACKET_READ(ReadByte, Byte, Flags); - HANDLE_PACKET_READ(ReadBEFloat, float, FlyingSpeed); - HANDLE_PACKET_READ(ReadBEFloat, float, WalkingSpeed); - // TODO: m_Client->HandlePlayerAbilities(...); - return PARSE_OK; -} - - - - - -int cProtocol161::ParseSteerVehicle(void) -{ - HANDLE_PACKET_READ(ReadBEFloat, float, Sideways); - HANDLE_PACKET_READ(ReadBEFloat, float, Forward); - HANDLE_PACKET_READ(ReadBool, bool, Jump); - HANDLE_PACKET_READ(ReadBool, bool, Unmount); - if (Unmount) - { - m_Client->HandleUnmount(); - } - else - { - m_Client->HandleSteerVehicle(Forward, Sideways); - } - return PARSE_OK; -} - - - - - -int cProtocol161::ParsePacket(unsigned char a_PacketType) -{ - switch (a_PacketType) - { - case PACKET_STEER_VEHICLE: return ParseSteerVehicle(); - default: return super::ParsePacket(a_PacketType); - } -} - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol162: - -cProtocol162::cProtocol162(cClientHandle * a_Client) : - super(a_Client) -{ -} - - - - - -void cProtocol162::SendPlayerMaxSpeed(void) -{ - cCSLock Lock(m_CSPacket); - WriteByte(PACKET_ENTITY_PROPERTIES); - WriteInt(m_Client->GetPlayer()->GetUniqueID()); - WriteInt(1); - WriteString("generic.movementSpeed"); - WriteDouble(m_Client->GetPlayer()->GetMaxSpeed()); - WriteShort(0); - Flush(); -} - - - - diff --git a/source/Protocol/Protocol16x.h b/source/Protocol/Protocol16x.h deleted file mode 100644 index 325e41c5a..000000000 --- a/source/Protocol/Protocol16x.h +++ /dev/null @@ -1,76 +0,0 @@ - -// Protocol16x.h - -/* -Declares the 1.6.x protocol classes: - - cProtocol161 - - release 1.6.1 protocol (#73) - - cProtocol162 - - release 1.6.2 protocol (#74) - - release 1.6.3 protocol (#77) - no relevant changes - - release 1.6.4 protocol (#78) - no relevant changes -(others may be added later in the future for the 1.6 release series) -*/ - - - - - -#pragma once - -#include "Protocol15x.h" - - - - - -class cProtocol161 : - public cProtocol150 -{ - typedef cProtocol150 super; - -public: - cProtocol161(cClientHandle * a_Client); - -protected: - - // cProtocol150 overrides: - virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; - virtual void SendChat (const AString & a_Message) override; - virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) - virtual void SendGameMode (eGameMode a_GameMode) override; - virtual void SendHealth (void) override; - virtual void SendPlayerMaxSpeed(void) override; - virtual void SendRespawn (void) override; - virtual void SendWindowOpen (const cWindow & a_Window) override; - - virtual int ParseEntityAction (void) override; - virtual int ParsePlayerAbilities(void) override; - - // New packets: - virtual int ParseSteerVehicle(void); - - // Enable new packets' handling - virtual int ParsePacket(unsigned char a_PacketType) override; -} ; - - - - - -class cProtocol162 : - public cProtocol161 -{ - typedef cProtocol161 super; - -public: - cProtocol162(cClientHandle * a_Client); - -protected: - // cProtocol161 overrides: - virtual void SendPlayerMaxSpeed(void) override; -} ; - - - - diff --git a/source/Protocol/Protocol17x.cpp b/source/Protocol/Protocol17x.cpp deleted file mode 100644 index 628b8e071..000000000 --- a/source/Protocol/Protocol17x.cpp +++ /dev/null @@ -1,1917 +0,0 @@ - -// Protocol17x.cpp - -/* -Implements the 1.7.x protocol classes: - - cProtocol172 - - release 1.7.2 protocol (#4) -(others may be added later in the future for the 1.7 release series) -*/ - -#include "Globals.h" -#include "Protocol17x.h" -#include "ChunkDataSerializer.h" -#include "../ClientHandle.h" -#include "../Root.h" -#include "../Server.h" -#include "../World.h" -#include "../WorldStorage/FastNBT.h" -#include "../StringCompression.h" -#include "../Entities/Minecart.h" -#include "../Entities/FallingBlock.h" -#include "../Entities/Pickup.h" -#include "../Entities/Player.h" -#include "../Mobs/IncludeAllMonsters.h" -#include "../UI/Window.h" - - - - - -#define HANDLE_READ(Proc, Type, Var) \ - Type Var; \ - m_ReceivedData.Proc(Var); - - - - - -#define HANDLE_PACKET_READ(Proc, Type, Var) \ - Type Var; \ - { \ - if (!m_ReceivedData.Proc(Var)) \ - { \ - m_ReceivedData.CheckValid(); \ - return false; \ - } \ - m_ReceivedData.CheckValid(); \ - } - - - - - -cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : - super(a_Client), - m_ServerAddress(a_ServerAddress), - m_ServerPort(a_ServerPort), - m_State(a_State), - m_ReceivedData(32 KiB), - m_OutPacketBuffer(64 KiB), - m_OutPacketLenBuffer(20), // 20 bytes is more than enough for one VarInt - m_IsEncrypted(false) -{ -} - - - - - -void cProtocol172::DataReceived(const char * a_Data, int a_Size) -{ - if (m_IsEncrypted) - { - byte Decrypted[512]; - while (a_Size > 0) - { - int NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; - m_Decryptor.ProcessData(Decrypted, (byte *)a_Data, NumBytes); - AddReceivedData((const char *)Decrypted, NumBytes); - a_Size -= NumBytes; - a_Data += NumBytes; - } - } - else - { - AddReceivedData(a_Data, a_Size); - } -} - - - - - -void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) -{ - cPacketizer Pkt(*this, 0x1b); // Attach Entity packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteInt((a_Vehicle != NULL) ? a_Vehicle->GetUniqueID() : 0); - Pkt.WriteBool(false); -} - - - - - -void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) -{ - cPacketizer Pkt(*this, 0x24); // Block Action packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteShort(a_BlockY); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteByte(a_Byte1); - Pkt.WriteByte(a_Byte2); - Pkt.WriteVarInt(a_BlockType); -} - - - - - -void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) -{ - cPacketizer Pkt(*this, 0x24); // Block Break Animation packet - Pkt.WriteInt(a_EntityID); - Pkt.WriteInt(a_BlockX); - Pkt.WriteInt(a_BlockY); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteChar(a_Stage); -} - - - - - -void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cPacketizer Pkt(*this, 0x23); // Block Change packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteByte(a_BlockY); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteVarInt(a_BlockType); - Pkt.WriteByte(a_BlockMeta); -} - - - - - -void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) -{ - cPacketizer Pkt(*this, 0x22); // Multi Block Change packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); - Pkt.WriteShort((short)a_Changes.size()); - Pkt.WriteInt(a_Changes.size() * 4); - for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) - { - unsigned int Coords = itr->y | (itr->z << 8) | (itr->x << 12); - unsigned int Blocks = itr->BlockMeta | (itr->BlockType << 4); - Pkt.WriteInt((Coords << 16) | Blocks); - } // for itr - a_Changes[] -} - - - - - -void cProtocol172::SendChat(const AString & a_Message) -{ - cPacketizer Pkt(*this, 0x02); // Chat Message packet - Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str())); -} - - - - - -void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) -{ - // Serialize first, before creating the Packetizer (the packetizer locks a CS) - // This contains the flags and bitmasks, too - const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2); - - cPacketizer Pkt(*this, 0x21); // Chunk Data packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); - Pkt.WriteBuf(ChunkData.data(), ChunkData.size()); -} - - - - - -void cProtocol172::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) -{ - cPacketizer Pkt(*this, 0x0d); // Collect Item packet - Pkt.WriteInt(a_Pickup.GetUniqueID()); - Pkt.WriteInt(a_Player.GetUniqueID()); -} - - - - - -void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x13); // Destroy Entities packet - Pkt.WriteByte(1); - Pkt.WriteInt(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol172::SendDisconnect(const AString & a_Reason) -{ - cPacketizer Pkt(*this, 0x40); - Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); -} - - - - - -void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - cPacketizer Pkt(*this, 0x36); // Sign Editor Open packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteInt(a_BlockY); - Pkt.WriteInt(a_BlockZ); -} - - - - - -void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - cPacketizer Pkt(*this, 0x04); // Entity Equipment packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); -} - - - - - -void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x19); // Entity Head Look packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); -} - - - - - -void cProtocol172::SendEntityLook(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x16); // Entity Look packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); -} - - - - - -void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteEntityMetadata(a_Entity); - Pkt.WriteByte(0x7f); // The termination byte -} - - - - - -void cProtocol172::SendEntityProperties(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x20); // Entity Properties packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteEntityProperties(a_Entity); -} - - - - - -void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); -} - - - - - -void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); -} - - - - - -void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) -{ - cPacketizer Pkt(*this, 0x1a); // Entity Status packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_Status); -} - - - - - -void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x12); // Entity Velocity packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - // 400 = 8000 / 20 ... Conversion from our speed in m/s to 8000 m/tick - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); -} - - - - - -void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) -{ - cPacketizer Pkt(*this, 0x27); // Explosion packet - Pkt.WriteFloat((float)a_BlockX); - Pkt.WriteFloat((float)a_BlockY); - Pkt.WriteFloat((float)a_BlockZ); - Pkt.WriteFloat((float)a_Radius); - Pkt.WriteInt(a_BlocksAffected.size()); - for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(), end = a_BlocksAffected.end(); itr != end; ++itr) - { - Pkt.WriteChar((char)itr->x); - Pkt.WriteChar((char)itr->y); - Pkt.WriteChar((char)itr->z); - } // for itr - a_BlockAffected[] - Pkt.WriteFloat((float)a_PlayerMotion.x); - Pkt.WriteFloat((float)a_PlayerMotion.y); - Pkt.WriteFloat((float)a_PlayerMotion.z); -} - - - - - -void cProtocol172::SendGameMode(eGameMode a_GameMode) -{ - cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte(3); // Reason: Change game mode - Pkt.WriteFloat((float)a_GameMode); -} - - - - - -void cProtocol172::SendHealth(void) -{ - cPacketizer Pkt(*this, 0x06); // Update Health packet - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetHealth()); - Pkt.WriteShort(m_Client->GetPlayer()->GetFoodLevel()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); -} - - - - - -void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item) -{ - cPacketizer Pkt(*this, 0x2f); // Set Slot packet - Pkt.WriteChar(a_WindowID); - Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); -} - - - - - -void cProtocol172::SendKeepAlive(int a_PingID) -{ - cPacketizer Pkt(*this, 0x00); // Keep Alive packet - Pkt.WriteInt(a_PingID); -} - - - - - -void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - // Send the Join Game packet: - { - cPacketizer Pkt(*this, 0x01); // Join Game packet - Pkt.WriteInt(a_Player.GetUniqueID()); - Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (cRoot::Get()->GetServer()->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteChar((char)a_World.GetDimension()); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte(cRoot::Get()->GetServer()->GetMaxPlayers()); - Pkt.WriteString("default"); // Level type - wtf? - } - - // Send the spawn position: - { - cPacketizer Pkt(*this, 0x05); // Spawn Position packet - Pkt.WriteInt((int)a_World.GetSpawnX()); - Pkt.WriteInt((int)a_World.GetSpawnY()); - Pkt.WriteInt((int)a_World.GetSpawnZ()); - } - - // Send player abilities: - SendPlayerAbilities(); -} - - - - - -void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) -{ - { - cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_Pickup.GetUniqueID()); - Pkt.WriteByte(2); // Type = Pickup - Pkt.WriteFPInt(a_Pickup.GetPosX()); - Pkt.WriteFPInt(a_Pickup.GetPosY()); - Pkt.WriteFPInt(a_Pickup.GetPosZ()); - Pkt.WriteByteAngle(a_Pickup.GetYaw()); - Pkt.WriteByteAngle(a_Pickup.GetPitch()); - Pkt.WriteInt(0); // No object data - } - { - cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteInt(a_Pickup.GetUniqueID()); - Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 - Pkt.WriteItem(a_Pickup.GetItem()); - Pkt.WriteByte(0x7f); // End of metadata - } -} - - - - - -void cProtocol172::SendPlayerAbilities(void) -{ - cPacketizer Pkt(*this, 0x39); // Player Abilities packet - Byte Flags = 0; - if (m_Client->GetPlayer()->IsGameModeCreative()) - { - Flags |= 0x01; - } - // TODO: Other flags (god mode, flying, can fly - Pkt.WriteByte(Flags); - // TODO: Pkt.WriteFloat(m_Client->GetPlayer()->GetMaxFlyingSpeed()); - Pkt.WriteFloat(0.05f); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetMaxSpeed()); -} - - - - - -void cProtocol172::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) -{ - cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); - Pkt.WriteChar(a_Animation); -} - - - - - -void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) -{ - cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteString(a_Player.GetName()); - Pkt.WriteBool(a_IsOnline); - Pkt.WriteShort(a_IsOnline ? a_Player.GetClientHandle()->GetPing() : 0); -} - - - - - -void cProtocol172::SendPlayerMaxSpeed(void) -{ - cPacketizer Pkt(*this, 0x20); // Entity Properties - Pkt.WriteInt(m_Client->GetPlayer()->GetUniqueID()); - Pkt.WriteInt(1); // Count - Pkt.WriteString("generic.movementSpeed"); - Pkt.WriteDouble(0.1); - if (m_Client->GetPlayer()->IsSprinting()) - { - Pkt.WriteShort(1); // Modifier count - Pkt.WriteInt64(0x662a6b8dda3e4c1c); - Pkt.WriteInt64(0x881396ea6097278d); // UUID of the modifier - Pkt.WriteDouble(0.3); - Pkt.WriteByte(2); - } - else - { - Pkt.WriteShort(0); // Modifier count - } -} - - - - - -void cProtocol172::SendPlayerMoveLook(void) -{ - cPacketizer Pkt(*this, 0x08); // Player Position And Look packet - Pkt.WriteDouble(m_Client->GetPlayer()->GetPosX()); - Pkt.WriteDouble(m_Client->GetPlayer()->GetPosY()); - Pkt.WriteDouble(m_Client->GetPlayer()->GetPosZ()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetYaw()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetPitch()); - Pkt.WriteBool(m_Client->GetPlayer()->IsOnGround()); -} - - - - - -void cProtocol172::SendPlayerPosition(void) -{ - // There is no dedicated packet for this, send the whole thing: - SendPlayerMoveLook(); -} - - - - - -void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) -{ - // Called to spawn another player for the client - cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); - Pkt.WriteString(Printf("%d", a_Player.GetUniqueID())); // TODO: Proper UUID - Pkt.WriteString(a_Player.GetName()); - Pkt.WriteFPInt(a_Player.GetPosX()); - Pkt.WriteFPInt(a_Player.GetPosY()); - Pkt.WriteFPInt(a_Player.GetPosZ()); - Pkt.WriteByteAngle(a_Player.GetYaw()); - Pkt.WriteByteAngle(a_Player.GetPitch()); - short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; - Pkt.WriteShort(ItemType); - Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 - Pkt.WriteFloat((float)a_Player.GetHealth()); - Pkt.WriteByte(0x7f); // Metadata: end -} - - - - - -void cProtocol172::SendRespawn(void) -{ - cPacketizer Pkt(*this, 0x07); // Respawn packet - Pkt.WriteInt(m_Client->GetPlayer()->GetWorld()->GetDimension()); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte((Byte)m_Client->GetPlayer()->GetEffectiveGameMode()); - Pkt.WriteString("default"); -} - - - - - -void cProtocol172::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) // a_Src coords are Block * 8 -{ - cPacketizer Pkt(*this, 0x29); // Sound Effect packet - Pkt.WriteString(a_SoundName); - Pkt.WriteInt(a_SrcX); - Pkt.WriteInt(a_SrcY); - Pkt.WriteInt(a_SrcZ); - Pkt.WriteFloat(a_Volume); - Pkt.WriteByte((Byte)(a_Pitch * 63)); -} - - - - - -void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) -{ - cPacketizer Pkt(*this, 0x28); // Effect packet - Pkt.WriteInt(a_EffectID); - Pkt.WriteInt(a_SrcX); - Pkt.WriteByte(a_SrcY); - Pkt.WriteInt(a_SrcZ); - Pkt.WriteInt(a_Data); - Pkt.WriteBool(false); -} - - - - - -void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) -{ - cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); - Pkt.WriteByte(70); // Falling block - Pkt.WriteFPInt(a_FallingBlock.GetPosX()); - Pkt.WriteFPInt(a_FallingBlock.GetPosY()); - Pkt.WriteFPInt(a_FallingBlock.GetPosZ()); - Pkt.WriteByteAngle(a_FallingBlock.GetYaw()); - Pkt.WriteByteAngle(a_FallingBlock.GetPitch()); - Pkt.WriteInt(((int)a_FallingBlock.GetBlockType()) | (((int)a_FallingBlock.GetBlockMeta()) << 12)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedZ() * 400)); -} - - - - - -void cProtocol172::SendSpawnMob(const cMonster & a_Mob) -{ - cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteVarInt(a_Mob.GetUniqueID()); - Pkt.WriteByte((Byte)a_Mob.GetMobType()); - Pkt.WriteFPInt(a_Mob.GetPosX()); - Pkt.WriteFPInt(a_Mob.GetPosY()); - Pkt.WriteFPInt(a_Mob.GetPosZ()); - Pkt.WriteByteAngle(a_Mob.GetPitch()); - Pkt.WriteByteAngle(a_Mob.GetHeadYaw()); - Pkt.WriteByteAngle(a_Mob.GetYaw()); - Pkt.WriteShort((short)(a_Mob.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedZ() * 400)); - Pkt.WriteEntityMetadata(a_Mob); - Pkt.WriteByte(0x7f); // Metadata terminator -} - - - - - -void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) -{ - cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_ObjectType); - Pkt.WriteFPInt(a_Entity.GetPosX()); - Pkt.WriteFPInt(a_Entity.GetPosY()); - Pkt.WriteFPInt(a_Entity.GetPosZ()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteInt(a_ObjectData); - if (a_ObjectData != 0) - { - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); - } -} - - - - - -void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) -{ - cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); - Pkt.WriteByte(a_VehicleType); - Pkt.WriteFPInt(a_Vehicle.GetPosX()); - Pkt.WriteFPInt(a_Vehicle.GetPosY()); - Pkt.WriteFPInt(a_Vehicle.GetPosZ()); - Pkt.WriteByteAngle(a_Vehicle.GetYaw()); - Pkt.WriteByteAngle(a_Vehicle.GetPitch()); - Pkt.WriteInt(a_VehicleSubType); - if (a_VehicleSubType != 0) - { - Pkt.WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); - } -} - - - - - -void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) -{ - AString Results; - Results.reserve(500); // Make a moderate reservation to avoid excessive reallocations - for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) - { - Results.append(*itr); - Results.push_back(0); - } - - cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet - Pkt.WriteVarInt(a_Results.size()); - Pkt.WriteString(Results); -} - - - - - -void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x18); - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteFPInt(a_Entity.GetPosX()); - Pkt.WriteFPInt(a_Entity.GetPosY()); - Pkt.WriteFPInt(a_Entity.GetPosZ()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); -} - - - - - -void cProtocol172::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - cPacketizer Pkt(*this, 0x2c); // Spawn Global Entity packet - Pkt.WriteVarInt(0); // EntityID = 0, always - Pkt.WriteByte(1); // Type = Thunderbolt - Pkt.WriteFPInt(a_BlockX); - Pkt.WriteFPInt(a_BlockY); - Pkt.WriteFPInt(a_BlockZ); -} - - - - - -void cProtocol172::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) -{ - cPacketizer Pkt(*this, 0x03); - Pkt.WriteInt64(a_WorldAge); - Pkt.WriteInt64(a_TimeOfDay); -} - - - - - -void cProtocol172::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) -{ - cPacketizer Pkt(*this, 0x21); // Chunk Data packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); - Pkt.WriteBool(true); - Pkt.WriteShort(0); // Primary bitmap - Pkt.WriteShort(0); // Add bitmap - Pkt.WriteInt(0); // Compressed data size -} - - - - - -void cProtocol172::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) -{ - cPacketizer Pkt(*this, 0x33); - Pkt.WriteInt(a_BlockX); - Pkt.WriteShort((short)a_BlockY); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteString(a_Line1); - Pkt.WriteString(a_Line2); - Pkt.WriteString(a_Line3); - Pkt.WriteString(a_Line4); -} - - - - - -void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ) -{ - cPacketizer Pkt(*this, 0x0a); - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteInt(a_BlockX); - Pkt.WriteByte((Byte)a_BlockY); - Pkt.WriteInt(a_BlockZ); -} - - - - - -void cProtocol172::SendWeather(eWeather a_Weather) -{ - { - cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain - Pkt.WriteFloat(0); // Unused for weather - } - - // TODO: Fade effect, somehow -} - - - - - -void cProtocol172::SendWholeInventory(const cWindow & a_Window) -{ - cPacketizer Pkt(*this, 0x30); // Window Items packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Window.GetNumSlots()); - cItems Slots; - a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); - for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) - { - Pkt.WriteItem(*itr); - } // for itr - Slots[] -} - - - - - -void cProtocol172::SendWindowClose(const cWindow & a_Window) -{ - cPacketizer Pkt(*this, 0x2e); - Pkt.WriteChar(a_Window.GetWindowID()); -} - - - - - -void cProtocol172::SendWindowOpen(const cWindow & a_Window) -{ - if (a_Window.GetWindowType() < 0) - { - // Do not send this packet for player inventory windows - return; - } - - cPacketizer Pkt(*this, 0x2d); - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteChar(a_Window.GetWindowType()); - Pkt.WriteString(a_Window.GetWindowTitle()); - Pkt.WriteChar(a_Window.GetNumNonInventorySlots()); - Pkt.WriteBool(true); - if (a_Window.GetWindowType() == cWindow::wtAnimalChest) - { - Pkt.WriteInt(0); // TODO: The animal's EntityID - } -} - - - - - -void cProtocol172::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) -{ - cPacketizer Pkt(*this, 0x31); // Window Property packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Property); - Pkt.WriteShort(a_Value); -} - - - - - -void cProtocol172::AddReceivedData(const char * a_Data, int a_Size) -{ - if (!m_ReceivedData.Write(a_Data, a_Size)) - { - // Too much data in the incoming queue, report to caller: - m_Client->PacketBufferFull(); - return; - } - - // Handle all complete packets: - while (true) - { - UInt32 PacketLen; - if (!m_ReceivedData.ReadVarInt(PacketLen)) - { - // Not enough data - return; - } - if (!m_ReceivedData.CanReadBytes(PacketLen)) - { - // The full packet hasn't been received yet - return; - } - UInt32 PacketType; - UInt32 Mark1 = m_ReceivedData.GetReadableSpace(); - if (!m_ReceivedData.ReadVarInt(PacketType)) - { - // Not enough data - return; - } - - UInt32 NumBytesRead = Mark1 - m_ReceivedData.GetReadableSpace(); - HandlePacket(PacketType, PacketLen - NumBytesRead); - - if (Mark1 - m_ReceivedData.GetReadableSpace() > PacketLen) - { - // Read more than packet length, report as error - m_Client->PacketError(PacketType); - } - - // Go to packet end in any case: - m_ReceivedData.ResetRead(); - m_ReceivedData.ReadVarInt(PacketType); - m_ReceivedData.SkipRead(PacketLen); - m_ReceivedData.CommitRead(); - } // while (true) -} - - - - -void cProtocol172::HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes) -{ - switch (m_State) - { - case 1: - { - // Status - switch (a_PacketType) - { - case 0x00: HandlePacketStatusRequest(a_RemainingBytes); return; - case 0x01: HandlePacketStatusPing (a_RemainingBytes); return; - } - break; - } - - case 2: - { - // Login - switch (a_PacketType) - { - case 0x00: HandlePacketLoginStart(a_RemainingBytes); return; - case 0x01: HandlePacketLoginEncryptionResponse(a_RemainingBytes); return; - } - break; - } - - case 3: - { - // Game - switch (a_PacketType) - { - case 0x00: HandlePacketKeepAlive (a_RemainingBytes); return; - case 0x01: HandlePacketChatMessage (a_RemainingBytes); return; - case 0x02: HandlePacketUseEntity (a_RemainingBytes); return; - case 0x03: HandlePacketPlayer (a_RemainingBytes); return; - case 0x04: HandlePacketPlayerPos (a_RemainingBytes); return; - case 0x05: HandlePacketPlayerLook (a_RemainingBytes); return; - case 0x06: HandlePacketPlayerPosLook (a_RemainingBytes); return; - case 0x07: HandlePacketBlockDig (a_RemainingBytes); return; - case 0x08: HandlePacketBlockPlace (a_RemainingBytes); return; - case 0x09: HandlePacketSlotSelect (a_RemainingBytes); return; - case 0x0a: HandlePacketAnimation (a_RemainingBytes); return; - case 0x0b: HandlePacketEntityAction (a_RemainingBytes); return; - case 0x0c: HandlePacketSteerVehicle (a_RemainingBytes); return; - case 0x0d: HandlePacketWindowClose (a_RemainingBytes); return; - case 0x0e: HandlePacketWindowClick (a_RemainingBytes); return; - case 0x0f: // Confirm transaction - not used in MCS - case 0x10: HandlePacketCreativeInventoryAction(a_RemainingBytes); return; - case 0x12: HandlePacketUpdateSign (a_RemainingBytes); return; - case 0x13: HandlePacketPlayerAbilities (a_RemainingBytes); return; - case 0x14: HandlePacketTabComplete (a_RemainingBytes); return; - case 0x15: HandlePacketClientSettings (a_RemainingBytes); return; - case 0x16: HandlePacketClientStatus (a_RemainingBytes); return; - case 0x17: HandlePacketPluginMessage (a_RemainingBytes); return; - } - break; - } - } // switch (m_State) - - // Unknown packet type, report to the client: - m_Client->PacketUnknown(a_PacketType); - m_ReceivedData.SkipRead(a_RemainingBytes); - m_ReceivedData.CommitRead(); -} - - - - - -void cProtocol172::HandlePacketStatusPing(UInt32 a_RemainingBytes) -{ - ASSERT(a_RemainingBytes == 8); - if (a_RemainingBytes != 8) - { - m_Client->PacketError(0x01); - m_ReceivedData.SkipRead(a_RemainingBytes); - m_ReceivedData.CommitRead(); - return; - } - Int64 Timestamp; - m_ReceivedData.ReadBEInt64(Timestamp); - m_ReceivedData.CommitRead(); - - cPacketizer Pkt(*this, 0x01); // Ping packet - Pkt.WriteInt64(Timestamp); -} - - - - - -void cProtocol172::HandlePacketStatusRequest(UInt32 a_RemainingBytes) -{ - // No more bytes in this packet - ASSERT(a_RemainingBytes == 0); - m_ReceivedData.CommitRead(); - - // Send the response: - AString Response = "{\"version\":{\"name\":\"1.7.2\",\"protocol\":4},\"players\":{"; - AppendPrintf(Response, "\"max\":%u,\"online\":%u,\"sample\":[]},", - cRoot::Get()->GetServer()->GetMaxPlayers(), - cRoot::Get()->GetServer()->GetNumPlayers() - ); - AppendPrintf(Response, "\"description\":{\"text\":\"%s\"}", - cRoot::Get()->GetServer()->GetDescription().c_str() - ); - Response.append("}"); - - cPacketizer Pkt(*this, 0x00); // Response packet - Pkt.WriteString(Response); -} - - - - - -void cProtocol172::HandlePacketLoginEncryptionResponse(UInt32 a_RemainingBytes) -{ - // TODO: Add protocol encryption -} - - - - - -void cProtocol172::HandlePacketLoginStart(UInt32 a_RemainingBytes) -{ - AString Username; - m_ReceivedData.ReadVarUTF8String(Username); - - // TODO: Protocol encryption should be set up here if not localhost / auth - - // Send login success: - { - cPacketizer Pkt(*this, 0x02); // Login success packet - Pkt.WriteString(Printf("%d", m_Client->GetUniqueID())); // TODO: proper UUID - Pkt.WriteString(Username); - } - - m_State = 3; // State = Game - m_Client->HandleLogin(4, Username); -} - - - - - -void cProtocol172::HandlePacketAnimation(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, EntityID); - HANDLE_READ(ReadByte, Byte, Animation); - m_Client->HandleAnimation(Animation); -} - - - - - -void cProtocol172::HandlePacketBlockDig(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadByte, Byte, Status); - HANDLE_READ(ReadBEInt, int, BlockX); - HANDLE_READ(ReadByte, Byte, BlockY); - HANDLE_READ(ReadBEInt, int, BlockZ); - HANDLE_READ(ReadByte, Byte, Face); - m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, Face, Status); -} - - - - - -void cProtocol172::HandlePacketBlockPlace(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, BlockX); - HANDLE_READ(ReadByte, Byte, BlockY); - HANDLE_READ(ReadBEInt, int, BlockZ); - HANDLE_READ(ReadByte, Byte, Face); - HANDLE_READ(ReadByte, Byte, CursorX); - HANDLE_READ(ReadByte, Byte, CursorY); - HANDLE_READ(ReadByte, Byte, CursorZ); - m_Client->HandleRightClick(BlockX, BlockY, BlockZ, Face, CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); -} - - - - - -void cProtocol172::HandlePacketChatMessage(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadVarUTF8String, AString, Message); - m_Client->HandleChat(Message); -} - - - - - -void cProtocol172::HandlePacketClientSettings(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadVarUTF8String, AString, Locale); - HANDLE_READ(ReadByte, Byte, ViewDistance); - HANDLE_READ(ReadByte, Byte, ChatFlags); - HANDLE_READ(ReadByte, Byte, Unused); - HANDLE_READ(ReadByte, Byte, Difficulty); - HANDLE_READ(ReadByte, Byte, ShowCape); - // TODO: handle in m_Client -} - - - - - -void cProtocol172::HandlePacketClientStatus(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadByte, Byte, ActionID); - switch (ActionID) - { - case 0: - { - // Respawn - m_Client->HandleRespawn(); - break; - } - case 1: - { - // Request stats - // TODO - break; - } - case 2: - { - // Open Inventory achievement - // TODO - break; - } - } -} - - - - - -void cProtocol172::HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEShort, short, SlotNum); - cItem Item; - if (!ReadItem(Item)) - { - return; - } - m_Client->HandleCreativeInventory(SlotNum, Item); -} - - - - - -void cProtocol172::HandlePacketEntityAction(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, PlayerID); - HANDLE_READ(ReadByte, Byte, Action); - HANDLE_READ(ReadBEInt, int, JumpBoost); - m_Client->HandleEntityAction(PlayerID, Action); -} - - - - - -void cProtocol172::HandlePacketKeepAlive(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, KeepAliveID); - m_Client->HandleKeepAlive(KeepAliveID); -} - - - - - -void cProtocol172::HandlePacketPlayer(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBool, bool, IsOnGround); - // TODO: m_Client->HandlePlayerOnGround(IsOnGround); -} - - - - - -void cProtocol172::HandlePacketPlayerAbilities(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadByte, Byte, Flags); - HANDLE_READ(ReadBEFloat, float, FlyingSpeed); - HANDLE_READ(ReadBEFloat, float, WalkingSpeed); - // TODO: m_Client->HandlePlayerAbilities(); -} - - - - - -void cProtocol172::HandlePacketPlayerLook(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEFloat, float, Yaw); - HANDLE_READ(ReadBEFloat, float, Pitch); - HANDLE_READ(ReadBool, bool, IsOnGround); - m_Client->HandlePlayerLook(Yaw, Pitch, IsOnGround); -} - - - - - -void cProtocol172::HandlePacketPlayerPos(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEDouble, double, PosX); - HANDLE_READ(ReadBEDouble, double, PosY); - HANDLE_READ(ReadBEDouble, double, Stance); - HANDLE_READ(ReadBEDouble, double, PosZ); - HANDLE_READ(ReadBool, bool, IsOnGround); - m_Client->HandlePlayerPos(PosX, PosY, PosZ, Stance, IsOnGround); -} - - - - - -void cProtocol172::HandlePacketPlayerPosLook(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEDouble, double, PosX); - HANDLE_READ(ReadBEDouble, double, PosY); - HANDLE_READ(ReadBEDouble, double, Stance); - HANDLE_READ(ReadBEDouble, double, PosZ); - HANDLE_READ(ReadBEFloat, float, Yaw); - HANDLE_READ(ReadBEFloat, float, Pitch); - HANDLE_READ(ReadBool, bool, IsOnGround); - m_Client->HandlePlayerMoveLook(PosX, PosY, PosZ, Stance, Yaw, Pitch, IsOnGround); -} - - - - - -void cProtocol172::HandlePacketPluginMessage(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadVarUTF8String, AString, Channel); - HANDLE_READ(ReadBEShort, short, Length); - AString Data; - m_ReceivedData.ReadString(Data, Length); - // TODO: m_Client->HandlePluginMessage(Channel, Data); -} - - - - - -void cProtocol172::HandlePacketSlotSelect(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEShort, short, SlotNum); - m_Client->HandleSlotSelected(SlotNum); -} - - - - - -void cProtocol172::HandlePacketSteerVehicle(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEFloat, float, Forward); - HANDLE_READ(ReadBEFloat, float, Sideways); - HANDLE_READ(ReadBool, bool, ShouldJump); - HANDLE_READ(ReadBool, bool, ShouldUnmount); - if (ShouldUnmount) - { - m_Client->HandleUnmount(); - } - else - { - m_Client->HandleSteerVehicle(Forward, Sideways); - } -} - - - - - -void cProtocol172::HandlePacketTabComplete(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadVarUTF8String, AString, Text); - m_Client->HandleTabCompletion(Text); -} - - - - - -void cProtocol172::HandlePacketUpdateSign(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, BlockX); - HANDLE_READ(ReadBEShort, short, BlockY); - HANDLE_READ(ReadBEInt, int, BlockZ); - HANDLE_READ(ReadVarUTF8String, AString, Line1); - HANDLE_READ(ReadVarUTF8String, AString, Line2); - HANDLE_READ(ReadVarUTF8String, AString, Line3); - HANDLE_READ(ReadVarUTF8String, AString, Line4); - m_Client->HandleUpdateSign(BlockX, BlockY, BlockZ, Line1, Line2, Line3, Line4); -} - - - - - -void cProtocol172::HandlePacketUseEntity(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadBEInt, int, EntityID); - HANDLE_READ(ReadByte, Byte, MouseButton); - m_Client->HandleUseEntity(EntityID, (MouseButton == 1)); -} - - - - - -void cProtocol172::HandlePacketWindowClick(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadChar, char, WindowID); - HANDLE_READ(ReadBEShort, short, SlotNum); - HANDLE_READ(ReadByte, Byte, Button); - HANDLE_READ(ReadBEShort, short, TransactionID); - HANDLE_READ(ReadByte, Byte, Mode); - cItem Item; - ReadItem(Item); - - // Convert Button, Mode, SlotNum and HeldItem into eClickAction: - eClickAction Action; - switch ((Mode << 8) | Button) - { - case 0x0000: Action = (SlotNum != -999) ? caLeftClick : caLeftClickOutside; break; - case 0x0001: Action = (SlotNum != -999) ? caRightClick : caRightClickOutside; break; - case 0x0100: Action = caShiftLeftClick; break; - case 0x0101: Action = caShiftRightClick; break; - case 0x0200: Action = caNumber1; break; - case 0x0201: Action = caNumber2; break; - case 0x0202: Action = caNumber3; break; - case 0x0203: Action = caNumber4; break; - case 0x0204: Action = caNumber5; break; - case 0x0205: Action = caNumber6; break; - case 0x0206: Action = caNumber7; break; - case 0x0207: Action = caNumber8; break; - case 0x0208: Action = caNumber9; break; - case 0x0300: Action = caMiddleClick; break; - case 0x0400: Action = (SlotNum == -999) ? caLeftClickOutsideHoldNothing : caDropKey; break; - case 0x0401: Action = (SlotNum == -999) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; - case 0x0500: Action = (SlotNum == -999) ? caLeftPaintBegin : caUnknown; break; - case 0x0501: Action = (SlotNum != -999) ? caLeftPaintProgress : caUnknown; break; - case 0x0502: Action = (SlotNum == -999) ? caLeftPaintEnd : caUnknown; break; - case 0x0504: Action = (SlotNum == -999) ? caRightPaintBegin : caUnknown; break; - case 0x0505: Action = (SlotNum != -999) ? caRightPaintProgress : caUnknown; break; - case 0x0506: Action = (SlotNum == -999) ? caRightPaintEnd : caUnknown; break; - case 0x0600: Action = caDblClick; break; - } - - m_Client->HandleWindowClick(WindowID, SlotNum, Action, Item); -} - - - - - -void cProtocol172::HandlePacketWindowClose(UInt32 a_RemainingBytes) -{ - HANDLE_READ(ReadChar, char, WindowID); - m_Client->HandleWindowClose(WindowID); -} - - - - - -void cProtocol172::WritePacket(cByteBuffer & a_Packet) -{ - cCSLock Lock(m_CSPacket); - AString Pkt; - a_Packet.ReadAll(Pkt); - WriteVarInt(Pkt.size()); - SendData(Pkt.data(), Pkt.size()); - Flush(); -} - - - - - -void cProtocol172::SendData(const char * a_Data, int a_Size) -{ - if (m_IsEncrypted) - { - byte Encrypted[8192]; // Larger buffer, we may be sending lots of data (chunks) - while (a_Size > 0) - { - int NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; - m_Encryptor.ProcessData(Encrypted, (byte *)a_Data, NumBytes); - m_Client->SendData((const char *)Encrypted, NumBytes); - a_Size -= NumBytes; - a_Data += NumBytes; - } - } - else - { - m_Client->SendData(a_Data, a_Size); - } -} - - - - - - -bool cProtocol172::ReadItem(cItem & a_Item) -{ - HANDLE_PACKET_READ(ReadBEShort, short, ItemType); - if (ItemType == -1) - { - // The item is empty, no more data follows - a_Item.Empty(); - return true; - } - a_Item.m_ItemType = ItemType; - - HANDLE_PACKET_READ(ReadChar, char, ItemCount); - HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage); - a_Item.m_ItemCount = ItemCount; - a_Item.m_ItemDamage = ItemDamage; - if (ItemCount <= 0) - { - a_Item.Empty(); - } - - HANDLE_PACKET_READ(ReadBEShort, short, MetadataLength); - if (MetadataLength <= 0) - { - return true; - } - - // Read the metadata - AString Metadata; - if (!m_ReceivedData.ReadString(Metadata, MetadataLength)) - { - return false; - } - - ParseItemMetadata(a_Item, Metadata); - return true; -} - - - - - -void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) -{ - // Uncompress the GZIPped data: - AString Uncompressed; - if (UncompressStringGZIP(a_Metadata.data(), a_Metadata.size(), Uncompressed) != Z_OK) - { - AString HexDump; - CreateHexDump(HexDump, a_Metadata.data(), a_Metadata.size(), 16); - LOGWARNING("Cannot unGZIP item metadata (%u bytes):\n%s", a_Metadata.size(), HexDump.c_str()); - return; - } - - // Parse into NBT: - cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); - if (!NBT.IsValid()) - { - AString HexDump; - CreateHexDump(HexDump, Uncompressed.data(), Uncompressed.size(), 16); - LOGWARNING("Cannot parse NBT item metadata: (%u bytes)\n%s", Uncompressed.size(), HexDump.c_str()); - return; - } - - // Load enchantments from the NBT: - for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag)) - { - if ( - (NBT.GetType(tag) == TAG_List) && - ( - (NBT.GetName(tag) == "ench") || - (NBT.GetName(tag) == "StoredEnchantments") - ) - ) - { - a_Item.m_Enchantments.ParseFromNBT(NBT, tag); - } - } -} - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cProtocol172::cPacketizer: - -cProtocol172::cPacketizer::~cPacketizer() -{ - AString DataToSend; - - // Send the packet length - UInt32 PacketLen = m_Out.GetUsedSpace(); - m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); - m_Protocol.m_OutPacketLenBuffer.ReadAll(DataToSend); - m_Protocol.SendData(DataToSend.data(), DataToSend.size()); - m_Protocol.m_OutPacketLenBuffer.CommitRead(); - - // Send the packet data: - m_Out.ReadAll(DataToSend); - m_Protocol.SendData(DataToSend.data(), DataToSend.size()); - m_Out.CommitRead(); -} - - - - - -void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) -{ - short ItemType = a_Item.m_ItemType; - ASSERT(ItemType >= -1); // Check validity of packets in debug runtime - if (ItemType <= 0) - { - // Fix, to make sure no invalid values are sent. - ItemType = -1; - } - - if (a_Item.IsEmpty()) - { - WriteShort(-1); - return; - } - - WriteShort(ItemType); - WriteByte (a_Item.m_ItemCount); - WriteShort(a_Item.m_ItemDamage); - - if (a_Item.m_Enchantments.IsEmpty()) - { - WriteShort(-1); - return; - } - - // Send the enchantments: - cFastNBTWriter Writer; - const char * TagName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench"; - a_Item.m_Enchantments.WriteToNBTCompound(Writer, TagName); - Writer.Finish(); - AString Compressed; - CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort(Compressed.size()); - WriteBuf(Compressed.data(), Compressed.size()); -} - - - - - -void cProtocol172::cPacketizer::WriteByteAngle(double a_Angle) -{ - WriteByte((char)(255 * a_Angle / 360)); -} - - - - - -void cProtocol172::cPacketizer::WriteFPInt(double a_Value) -{ - int Value = (int)(a_Value * 32); - WriteInt(Value); -} - - - - - -void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) -{ - // Common metadata: - Byte Flags = 0; - if (a_Entity.IsOnFire()) - { - Flags |= 0x01; - } - if (a_Entity.IsCrouched()) - { - Flags |= 0x02; - } - if (a_Entity.IsSprinting()) - { - Flags |= 0x08; - } - if (a_Entity.IsRclking()) - { - Flags |= 0x10; - } - if (a_Entity.IsInvisible()) - { - Flags |= 0x20; - } - WriteByte(0); // Byte(0) + index 0 - WriteByte(Flags); - - switch (a_Entity.GetEntityType()) - { - case cEntity::etPlayer: break; // TODO? - case cEntity::etPickup: - { - WriteByte((5 << 5) | 10); // Slot(5) + index 10 - WriteItem(((const cPickup &)a_Entity).GetItem()); - break; - } - case cEntity::etMinecart: - { - WriteByte(0x51); - - // The following expression makes Minecarts shake more with less health or higher damage taken - // It gets half the maximum health, and takes it away from the current health minus the half health: - /* Health: 5 | 3 - (5 - 3) = 1 (shake power) - Health: 3 | 3 - (3 - 3) = 3 - Health: 1 | 3 - (1 - 3) = 5 - */ - WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4); - WriteByte(0x52); - WriteInt(1); // Shaking direction, doesn't seem to affect anything - WriteByte(0x73); - WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer - - if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) - { - WriteByte(0x10); - WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); - } - break; - } - case cEntity::etProjectile: - { - if (((cProjectileEntity &)a_Entity).GetProjectileKind() == cProjectileEntity::pkArrow) - { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); - } - break; - } - case cEntity::etMonster: - { - WriteMobMetadata((const cMonster &)a_Entity); - break; - } - } -} - - - - - -void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) -{ - switch (a_Mob.GetMobType()) - { - case cMonster::mtCreeper: - { - WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); - WriteByte(0x11); - WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); - break; - } - - case cMonster::mtBat: - { - WriteByte(0x10); - WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); - break; - } - - case cMonster::mtPig: - { - WriteByte(0x10); - WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); - break; - } - - case cMonster::mtVillager: - { - WriteByte(0x50); - WriteInt(((const cVillager &)a_Mob).GetVilType()); - break; - } - - case cMonster::mtZombie: - { - WriteByte(0x0c); - WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); - WriteByte(0x0d); - WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); - WriteByte(0x0e); - WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); - break; - } - - case cMonster::mtGhast: - { - WriteByte(0x10); - WriteByte(((const cGhast &)a_Mob).IsCharging()); - break; - } - - case cMonster::mtWolf: - { - const cWolf & Wolf = (const cWolf &)a_Mob; - Byte WolfStatus = 0; - if (Wolf.IsSitting()) - { - WolfStatus |= 0x1; - } - if (Wolf.IsAngry()) - { - WolfStatus |= 0x2; - } - if (Wolf.IsTame()) - { - WolfStatus |= 0x4; - } - WriteByte(0x10); - WriteByte(WolfStatus); - - WriteByte(0x72); - WriteFloat((float)(a_Mob.GetHealth())); - WriteByte(0x13); - WriteByte(Wolf.IsBegging() ? 1 : 0); - WriteByte(0x14); - WriteByte(Wolf.GetCollarColor()); - break; - } - - case cMonster::mtSheep: - { - WriteByte(0x10); - Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); - if (((const cSheep &)a_Mob).IsSheared()) - { - SheepMetadata |= 0x10; - } - WriteByte(SheepMetadata); - break; - } - - case cMonster::mtEnderman: - { - WriteByte(0x10); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); - WriteByte(0x11); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); - WriteByte(0x12); - WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); - break; - } - - case cMonster::mtSkeleton: - { - WriteByte(0x0d); - WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); - break; - } - - case cMonster::mtWitch: - { - WriteByte(0x15); - WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); - break; - } - - case cMonster::mtSlime: - { - WriteByte(0x10); - WriteByte(((const cSlime &)a_Mob).GetSize()); - break; - } - - case cMonster::mtMagmaCube: - { - WriteByte(0x10); - WriteByte(((const cMagmaCube &)a_Mob).GetSize()); - break; - } - - case cMonster::mtHorse: - { - const cHorse & Horse = (const cHorse &)a_Mob; - int Flags = 0; - if (Horse.IsTame()) - { - Flags |= 0x02; - } - if (Horse.IsSaddled()) - { - Flags |= 0x04; - } - if (Horse.IsChested()) - { - Flags |= 0x08; - } - if (Horse.IsBaby()) - { - Flags |= 0x10; - } - if (Horse.IsEating()) - { - Flags |= 0x20; - } - if (Horse.IsRearing()) - { - Flags |= 0x40; - } - if (Horse.IsMthOpen()) - { - Flags |= 0x80; - } - WriteByte(0x50); // Int at index 16 - WriteInt(Flags); - WriteByte(0x13); // Byte at index 19 - WriteByte(Horse.GetHorseType()); - WriteByte(0x54); // Int at index 20 - int Appearance = 0; - Appearance = Horse.GetHorseColor(); - Appearance |= Horse.GetHorseStyle() << 8; - WriteInt(Appearance); - WriteByte(0x56); // Int at index 22 - WriteInt(Horse.GetHorseArmour()); - break; - } - } // switch (a_Mob.GetType()) -} - - - - - -void cProtocol172::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) -{ - if (!a_Entity.IsMob()) - { - // No properties for anything else than mobs - WriteInt(0); - return; - } - const cMonster & Mob = (const cMonster &)a_Entity; - - // TODO: Send properties and modifiers based on the mob type - - WriteInt(0); // NumProperties -} - - - - diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h deleted file mode 100644 index 844069403..000000000 --- a/source/Protocol/Protocol17x.h +++ /dev/null @@ -1,258 +0,0 @@ - -// Protocol17x.h - -/* -Declares the 1.7.x protocol classes: - - cProtocol172 - - release 1.7.2 protocol (#4) -(others may be added later in the future for the 1.7 release series) -*/ - - - - - -#pragma once - -#include "Protocol.h" -#include "../ByteBuffer.h" -#include "../../CryptoPP/modes.h" -#include "../../CryptoPP/aes.h" - - - - - -class cProtocol172 : - public cProtocol // TODO -{ - typedef cProtocol super; // TODO - -public: - - cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); - - /// Called when client sends some data: - virtual void DataReceived(const char * a_Data, int a_Size) override; - - /// Sending stuff to clients (alphabetically sorted): - virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; - virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; - virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; - virtual void SendChat (const AString & a_Message) override; - virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; - virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) override; - virtual void SendDestroyEntity (const cEntity & a_Entity) override; - virtual void SendDisconnect (const AString & a_Reason) override; - virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) - virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; - virtual void SendEntityHeadLook (const cEntity & a_Entity) override; - virtual void SendEntityLook (const cEntity & a_Entity) override; - virtual void SendEntityMetadata (const cEntity & a_Entity) override; - virtual void SendEntityProperties (const cEntity & a_Entity) override; - virtual void SendEntityRelMove (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) override; - virtual void SendEntityVelocity (const cEntity & a_Entity) override; - virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override; - virtual void SendGameMode (eGameMode a_GameMode) override; - virtual void SendHealth (void) override; - virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; - virtual void SendKeepAlive (int a_PingID) override; - virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; - virtual void SendPickupSpawn (const cPickup & a_Pickup) override; - virtual void SendPlayerAbilities (void) override; - virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; - virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; - virtual void SendPlayerMaxSpeed (void) override; - virtual void SendPlayerMoveLook (void) override; - virtual void SendPlayerPosition (void) override; - virtual void SendPlayerSpawn (const cPlayer & a_Player) override; - virtual void SendRespawn (void) override; - virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8 - virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; - virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; - virtual void SendSpawnMob (const cMonster & a_Mob) override; - virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; - virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; - virtual void SendTabCompletionResults(const AStringVector & a_Results) override; - virtual void SendTeleportEntity (const cEntity & a_Entity) override; - virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; - virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; - virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; - virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (const cWindow & a_Window) override; - virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; - - virtual AString GetAuthServerID(void) override { return m_AuthServerID; } - -protected: - - /// Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed - class cPacketizer - { - public: - cPacketizer(cProtocol172 & a_Protocol, UInt32 a_PacketType) : - m_Protocol(a_Protocol), - m_Out(a_Protocol.m_OutPacketBuffer), - m_Lock(a_Protocol.m_CSPacket) - { - m_Out.WriteVarInt(a_PacketType); - } - - ~cPacketizer(); - - void WriteBool(bool a_Value) - { - m_Out.WriteBool(a_Value); - } - - void WriteByte(Byte a_Value) - { - m_Out.WriteByte(a_Value); - } - - void WriteChar(char a_Value) - { - m_Out.WriteChar(a_Value); - } - - void WriteShort(short a_Value) - { - m_Out.WriteBEShort(a_Value); - } - - void WriteInt(int a_Value) - { - m_Out.WriteBEInt(a_Value); - } - - void WriteInt64(Int64 a_Value) - { - m_Out.WriteBEInt64(a_Value); - } - - void WriteFloat(float a_Value) - { - m_Out.WriteBEFloat(a_Value); - } - - void WriteDouble(double a_Value) - { - m_Out.WriteBEDouble(a_Value); - } - - void WriteVarInt(UInt32 a_Value) - { - m_Out.WriteVarInt(a_Value); - } - - void WriteString(const AString & a_Value) - { - m_Out.WriteVarUTF8String(a_Value); - } - - void WriteBuf(const char * a_Data, int a_Size) - { - m_Out.Write(a_Data, a_Size); - } - - void WriteItem(const cItem & a_Item); - void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte - void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer - void WriteEntityMetadata(const cEntity & a_Entity); // Writes the metadata for the specified entity, not including the terminating 0x7f - void WriteMobMetadata(const cMonster & a_Mob); // Writes the mob-specific metadata for the specified mob - void WriteEntityProperties(const cEntity & a_Entity); // Writes the entity properties for the specified entity, including the Count field - - protected: - cProtocol172 & m_Protocol; - cByteBuffer & m_Out; - cCSLock m_Lock; - } ; - - AString m_ServerAddress; - - UInt16 m_ServerPort; - - AString m_AuthServerID; - - /// State of the protocol. 1 = status, 2 = login, 3 = game - UInt32 m_State; - - /// Buffer for the received data - cByteBuffer m_ReceivedData; - - /// Buffer for composing the outgoing packets, through cPacketizer - cByteBuffer m_OutPacketBuffer; - - /// Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) - cByteBuffer m_OutPacketLenBuffer; - - bool m_IsEncrypted; - CryptoPP::CFB_Mode::Decryption m_Decryptor; - CryptoPP::CFB_Mode::Encryption m_Encryptor; - - - /// Adds the received (unencrypted) data to m_ReceivedData, parses complete packets - void AddReceivedData(const char * a_Data, int a_Size); - - /// Reads and handles the packet. The packet length and type have already been read. - void HandlePacket(UInt32 a_PacketType, UInt32 a_RemainingBytes); - - // Packet handlers while in the Status state (m_State == 1): - void HandlePacketStatusPing (UInt32 a_RemainingBytes); - void HandlePacketStatusRequest(UInt32 a_RemainingBytes); - - // Packet handlers while in the Login state (m_State == 2): - void HandlePacketLoginEncryptionResponse(UInt32 a_RemainingBytes); - void HandlePacketLoginStart (UInt32 a_RemainingBytes); - - // Packet handlers while in the Game state (m_State == 3): - void HandlePacketAnimation (UInt32 a_RemainingBytes); - void HandlePacketBlockDig (UInt32 a_RemainingBytes); - void HandlePacketBlockPlace (UInt32 a_RemainingBytes); - void HandlePacketChatMessage (UInt32 a_RemainingBytes); - void HandlePacketClientSettings (UInt32 a_RemainingBytes); - void HandlePacketClientStatus (UInt32 a_RemainingBytes); - void HandlePacketCreativeInventoryAction(UInt32 a_RemainingBytes); - void HandlePacketEntityAction (UInt32 a_RemainingBytes); - void HandlePacketKeepAlive (UInt32 a_RemainingBytes); - void HandlePacketPlayer (UInt32 a_RemainingBytes); - void HandlePacketPlayerAbilities (UInt32 a_RemainingBytes); - void HandlePacketPlayerLook (UInt32 a_RemainingBytes); - void HandlePacketPlayerPos (UInt32 a_RemainingBytes); - void HandlePacketPlayerPosLook (UInt32 a_RemainingBytes); - void HandlePacketPluginMessage (UInt32 a_RemainingBytes); - void HandlePacketSlotSelect (UInt32 a_RemainingBytes); - void HandlePacketSteerVehicle (UInt32 a_RemainingBytes); - void HandlePacketTabComplete (UInt32 a_RemainingBytes); - void HandlePacketUpdateSign (UInt32 a_RemainingBytes); - void HandlePacketUseEntity (UInt32 a_RemainingBytes); - void HandlePacketWindowClick (UInt32 a_RemainingBytes); - void HandlePacketWindowClose (UInt32 a_RemainingBytes); - - - /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. - void WritePacket(cByteBuffer & a_Packet); - - /// Sends the data to the client, encrypting them if needed. - virtual void SendData(const char * a_Data, int a_Size) override; - - void SendCompass(const cWorld & a_World); - - /// Reads an item out of the received data, sets a_Item to the values read. Returns false if not enough received data - bool ReadItem(cItem & a_Item); - - /// Parses item metadata as read by ReadItem(), into the item enchantments. - void ParseItemMetadata(cItem & a_Item, const AString & a_Metadata); -} ; - - - - diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp deleted file mode 100644 index 9234785b5..000000000 --- a/source/Protocol/ProtocolRecognizer.cpp +++ /dev/null @@ -1,905 +0,0 @@ - -// ProtocolRecognizer.cpp - -// Implements the cProtocolRecognizer class representing the meta-protocol that recognizes possibly multiple -// protocol versions and redirects everything to them - -#include "Globals.h" - -#include "ProtocolRecognizer.h" -#include "Protocol125.h" -#include "Protocol132.h" -#include "Protocol14x.h" -#include "Protocol15x.h" -#include "Protocol16x.h" -#include "Protocol17x.h" -#include "../ClientHandle.h" -#include "../Root.h" -#include "../Server.h" -#include "../World.h" -#include "../ChatColor.h" - - - - - -cProtocolRecognizer::cProtocolRecognizer(cClientHandle * a_Client) : - super(a_Client), - m_Protocol(NULL), - m_Buffer(512) -{ -} - - - - - -cProtocolRecognizer::~cProtocolRecognizer() -{ - delete m_Protocol; -} - - - - - -AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) -{ - switch (a_ProtocolVersion) - { - case PROTO_VERSION_1_2_5: return "1.2.5"; - case PROTO_VERSION_1_3_2: return "1.3.2"; - case PROTO_VERSION_1_4_2: return "1.4.2"; - case PROTO_VERSION_1_4_4: return "1.4.4"; - case PROTO_VERSION_1_4_6: return "1.4.6"; - case PROTO_VERSION_1_5_0: return "1.5"; - case PROTO_VERSION_1_5_2: return "1.5.2"; - case PROTO_VERSION_1_6_1: return "1.6.1"; - case PROTO_VERSION_1_6_2: return "1.6.2"; - case PROTO_VERSION_1_6_3: return "1.6.3"; - case PROTO_VERSION_1_6_4: return "1.6.4"; - case PROTO_VERSION_1_7_2: return "1.7.2"; - } - ASSERT(!"Unknown protocol version"); - return Printf("Unknown protocol (%d)", a_ProtocolVersion); -} - - - - - -void cProtocolRecognizer::DataReceived(const char * a_Data, int a_Size) -{ - if (m_Protocol == NULL) - { - if (!m_Buffer.Write(a_Data, a_Size)) - { - m_Client->Kick("Unsupported protocol version"); - return; - } - - if (!TryRecognizeProtocol()) - { - return; - } - - // The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing: - AString Dump; - m_Buffer.ResetRead(); - m_Buffer.ReadAll(Dump); - m_Protocol->DataReceived(Dump.data(), Dump.size()); - } - else - { - m_Protocol->DataReceived(a_Data, a_Size); - } -} - - - - - -void cProtocolRecognizer::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendAttachEntity(a_Entity, a_Vehicle); -} - - - - - -void cProtocolRecognizer::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendBlockAction(a_BlockX, a_BlockY, a_BlockZ, a_Byte1, a_Byte2, a_BlockType); -} - - - - - -void cProtocolRecognizer::SendBlockBreakAnim(int a_entityID, int a_BlockX, int a_BlockY, int a_BlockZ, char stage) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendBlockBreakAnim(a_entityID, a_BlockX, a_BlockY, a_BlockZ, stage); -} - - - - - -void cProtocolRecognizer::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendBlockChange(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta); -} - - - - - -void cProtocolRecognizer::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendBlockChanges(a_ChunkX, a_ChunkZ, a_Changes); -} - - - - - -void cProtocolRecognizer::SendChat(const AString & a_Message) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendChat(a_Message); -} - - - - - -void cProtocolRecognizer::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendChunkData(a_ChunkX, a_ChunkZ, a_Serializer); -} - - - - - -void cProtocolRecognizer::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendCollectPickup(a_Pickup, a_Player); -} - - - - - -void cProtocolRecognizer::SendDestroyEntity(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendDestroyEntity(a_Entity); -} - - - - - -void cProtocolRecognizer::SendDisconnect(const AString & a_Reason) -{ - if (m_Protocol != NULL) - { - m_Protocol->SendDisconnect(a_Reason); - } - else - { - // This is used when the client sends a server-ping, respond with the default packet: - WriteByte ((char)0xff); // PACKET_DISCONNECT - WriteString(a_Reason); - } -} - - - - -void cProtocolRecognizer::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEditSign(a_BlockX, a_BlockY, a_BlockZ); -} - - - - - -void cProtocolRecognizer::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityEquipment(a_Entity, a_SlotNum, a_Item); -} - - - - - -void cProtocolRecognizer::SendEntityHeadLook(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityHeadLook(a_Entity); -} - - - - - -void cProtocolRecognizer::SendEntityLook(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityLook(a_Entity); -} - - - - - -void cProtocolRecognizer::SendEntityMetadata(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityMetadata(a_Entity); -} - - - - - -void cProtocolRecognizer::SendEntityProperties(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityProperties(a_Entity); -} - - - - - -void cProtocolRecognizer::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityRelMove(a_Entity, a_RelX, a_RelY, a_RelZ); -} - - - - - -void cProtocolRecognizer::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityRelMoveLook(a_Entity, a_RelX, a_RelY, a_RelZ); -} - - - - - -void cProtocolRecognizer::SendEntityStatus(const cEntity & a_Entity, char a_Status) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityStatus(a_Entity, a_Status); -} - - - - - -void cProtocolRecognizer::SendEntityVelocity(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendEntityVelocity(a_Entity); -} - - - - - -void cProtocolRecognizer::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendExplosion(a_BlockX,a_BlockY,a_BlockZ,a_Radius, a_BlocksAffected, a_PlayerMotion); -} - - - - - -void cProtocolRecognizer::SendGameMode(eGameMode a_GameMode) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendGameMode(a_GameMode); -} - - - - - -void cProtocolRecognizer::SendHealth(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendHealth(); -} - - - - - -void cProtocolRecognizer::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWindowProperty(a_Window, a_Property, a_Value); -} - - - - - -void cProtocolRecognizer::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendInventorySlot(a_WindowID, a_SlotNum, a_Item); -} - - - - - -void cProtocolRecognizer::SendKeepAlive(int a_PingID) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendKeepAlive(a_PingID); -} - - - - - -void cProtocolRecognizer::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendLogin(a_Player, a_World); -} - - - - - -void cProtocolRecognizer::SendPickupSpawn(const cPickup & a_Pickup) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPickupSpawn(a_Pickup); -} - - - - - -void cProtocolRecognizer::SendPlayerAbilities(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerAbilities(); -} - - - - - -void cProtocolRecognizer::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerAnimation(a_Player, a_Animation); -} - - - - - -void cProtocolRecognizer::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerListItem(a_Player, a_IsOnline); -} - - - - - -void cProtocolRecognizer::SendPlayerMaxSpeed(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerMaxSpeed(); -} - - - - - -void cProtocolRecognizer::SendPlayerMoveLook(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerMoveLook(); -} - - - - - -void cProtocolRecognizer::SendPlayerPosition(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerPosition(); -} - - - - - -void cProtocolRecognizer::SendPlayerSpawn(const cPlayer & a_Player) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendPlayerSpawn(a_Player); -} - - - - - -void cProtocolRecognizer::SendRespawn(void) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendRespawn(); -} - - - - - -void cProtocolRecognizer::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSoundEffect(a_SoundName, a_SrcX, a_SrcY, a_SrcZ, a_Volume, a_Pitch); -} - - - - - -void cProtocolRecognizer::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSoundParticleEffect(a_EffectID, a_SrcX, a_SrcY, a_SrcZ, a_Data); -} - - - - - -void cProtocolRecognizer::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSpawnFallingBlock(a_FallingBlock); -} - - - - - -void cProtocolRecognizer::SendSpawnMob(const cMonster & a_Mob) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSpawnMob(a_Mob); -} - - - - - -void cProtocolRecognizer::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSpawnObject(a_Entity, a_ObjectType, a_ObjectData, a_Yaw, a_Pitch); -} - - - - - -void cProtocolRecognizer::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendSpawnVehicle(a_Vehicle, a_VehicleType, a_VehicleSubType); -} - - - - - -void cProtocolRecognizer::SendTabCompletionResults(const AStringVector & a_Results) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendTabCompletionResults(a_Results); -} - - - - - -void cProtocolRecognizer::SendTeleportEntity(const cEntity & a_Entity) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendTeleportEntity(a_Entity); -} - - - - - -void cProtocolRecognizer::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendThunderbolt(a_BlockX, a_BlockY, a_BlockZ); -} - - - - - -void cProtocolRecognizer::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendTimeUpdate(a_WorldAge, a_TimeOfDay); -} - - - - - -void cProtocolRecognizer::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendUnloadChunk(a_ChunkX, a_ChunkZ); -} - - - - - -void cProtocolRecognizer::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) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendUpdateSign(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4); -} - - - - - -void cProtocolRecognizer::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendUseBed(a_Entity, a_BlockX, a_BlockY, a_BlockZ); -} - - - - - -void cProtocolRecognizer::SendWeather(eWeather a_Weather) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWeather(a_Weather); -} - - - - - -void cProtocolRecognizer::SendWholeInventory(const cWindow & a_Window) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWholeInventory(a_Window); -} - - - - - -void cProtocolRecognizer::SendWindowClose(const cWindow & a_Window) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWindowClose(a_Window); -} - - - - - -void cProtocolRecognizer::SendWindowOpen(const cWindow & a_Window) -{ - ASSERT(m_Protocol != NULL); - m_Protocol->SendWindowOpen(a_Window); -} - - - - - -AString cProtocolRecognizer::GetAuthServerID(void) -{ - ASSERT(m_Protocol != NULL); - return m_Protocol->GetAuthServerID(); -} - - - - - -void cProtocolRecognizer::SendData(const char * a_Data, int a_Size) -{ - // This is used only when handling the server ping - m_Client->SendData(a_Data, a_Size); -} - - - - - -bool cProtocolRecognizer::TryRecognizeProtocol(void) -{ - // NOTE: If a new protocol is added or an old one is removed, adjust MCS_CLIENT_VERSIONS and - // MCS_PROTOCOL_VERSIONS macros in the header file, as well as PROTO_VERSION_LATEST macro - - // The first packet should be a Handshake, 0x02: - unsigned char PacketType; - if (!m_Buffer.ReadByte(PacketType)) - { - return false; - } - switch (PacketType) - { - case 0x02: return TryRecognizeLengthlessProtocol(); // Handshake, continue recognizing - case 0xfe: - { - // This may be either a packet length or the length-less Ping packet - Byte NextByte; - if (!m_Buffer.ReadByte(NextByte)) - { - // Not enough data for either protocol - // This could actually happen with the 1.2 / 1.3 client, but their support is fading out anyway - return false; - } - if (NextByte != 0x01) - { - // This is definitely NOT a length-less Ping packet, handle as lengthed protocol: - break; - } - if (!m_Buffer.ReadByte(NextByte)) - { - // There is no more data. Although this *could* mean TCP fragmentation, it is highly unlikely - // and rather this is a 1.4 client sending a regular Ping packet (without the following Plugin message) - SendLengthlessServerPing(); - return false; - } - if (NextByte == 0xfa) - { - // Definitely a length-less Ping followed by a Plugin message - SendLengthlessServerPing(); - return false; - } - // Definitely a lengthed Initial handshake, handle below: - break; - } - } // switch (PacketType) - - // This must be a lengthed protocol, try if it has the entire initial handshake packet: - m_Buffer.ResetRead(); - UInt32 PacketLen; - UInt32 ReadSoFar = m_Buffer.GetReadableSpace(); - if (!m_Buffer.ReadVarInt(PacketLen)) - { - // Not enough bytes for the packet length, keep waiting - return false; - } - ReadSoFar -= m_Buffer.GetReadableSpace(); - if (!m_Buffer.CanReadBytes(PacketLen)) - { - // Not enough bytes for the packet, keep waiting - return false; - } - return TryRecognizeLengthedProtocol(PacketLen - ReadSoFar); -} - - - - - -bool cProtocolRecognizer::TryRecognizeLengthlessProtocol(void) -{ - // The comm started with 0x02, which is a Handshake packet in the length-less protocol family - // 1.3.2 starts with 0x02 0x39 - // 1.2.5 starts with 0x02 and name is expected to less than 0x3900 long :) - char ch; - if (!m_Buffer.ReadChar(ch)) - { - return false; - } - switch (ch) - { - case PROTO_VERSION_1_3_2: - { - m_Protocol = new cProtocol132(m_Client); - return true; - } - case PROTO_VERSION_1_4_2: - case PROTO_VERSION_1_4_4: - { - m_Protocol = new cProtocol142(m_Client); - return true; - } - case PROTO_VERSION_1_4_6: - { - m_Protocol = new cProtocol146(m_Client); - return true; - } - case PROTO_VERSION_1_5_0: - case PROTO_VERSION_1_5_2: - { - m_Protocol = new cProtocol150(m_Client); - return true; - } - case PROTO_VERSION_1_6_1: - { - m_Protocol = new cProtocol161(m_Client); - return true; - } - case PROTO_VERSION_1_6_2: - case PROTO_VERSION_1_6_3: - case PROTO_VERSION_1_6_4: - { - m_Protocol = new cProtocol162(m_Client); - return true; - } - } - m_Protocol = new cProtocol125(m_Client); - return true; -} - - - - - -bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRemaining) -{ - UInt32 PacketType; - UInt32 NumBytesRead = m_Buffer.GetReadableSpace(); - if (!m_Buffer.ReadVarInt(PacketType)) - { - return false; - } - if (PacketType != 0x00) - { - // Not an initial handshake packet, we don't know how to talk to them - LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, initial packet %u)", - m_Client->GetIPString().c_str(), PacketType - ); - m_Client->Kick("Unsupported protocol version"); - return false; - } - UInt32 ProtocolVersion; - if (!m_Buffer.ReadVarInt(ProtocolVersion)) - { - return false; - } - NumBytesRead -= m_Buffer.GetReadableSpace(); - switch (ProtocolVersion) - { - case PROTO_VERSION_1_7_2: - { - AString ServerAddress; - short ServerPort; - UInt32 NextState; - m_Buffer.ReadVarUTF8String(ServerAddress); - m_Buffer.ReadBEShort(ServerPort); - m_Buffer.ReadVarInt(NextState); - m_Buffer.CommitRead(); - m_Protocol = new cProtocol172(m_Client, ServerAddress, ServerPort, NextState); - return true; - } - } - LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u)", - m_Client->GetIPString().c_str(), ProtocolVersion - ); - m_Client->Kick("Unsupported protocol version"); - return false; -} - - - - - -void cProtocolRecognizer::SendLengthlessServerPing(void) -{ - AString Reply; - switch (cRoot::Get()->GetPrimaryServerVersion()) - { - case PROTO_VERSION_1_2_5: - case PROTO_VERSION_1_3_2: - { - // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3099#Server_List_Ping_.280xFE.29 - Printf(Reply, "%s%s%i%s%i", - cRoot::Get()->GetServer()->GetDescription().c_str(), - cChatColor::Delimiter.c_str(), - cRoot::Get()->GetServer()->GetNumPlayers(), - cChatColor::Delimiter.c_str(), - cRoot::Get()->GetServer()->GetMaxPlayers() - ); - break; - } - - case PROTO_VERSION_1_4_2: - case PROTO_VERSION_1_4_4: - case PROTO_VERSION_1_4_6: - case PROTO_VERSION_1_5_0: - case PROTO_VERSION_1_5_2: - case PROTO_VERSION_1_6_1: - case PROTO_VERSION_1_6_2: - case PROTO_VERSION_1_6_3: - case PROTO_VERSION_1_6_4: - { - // The server list ping now has 1 more byte of "magic". Mojang just loves to complicate stuff. - // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3101#Server_List_Ping_.280xFE.29 - // _X 2012_10_31: I know that this needn't eat the byte, since it still may be in transit. - // Who cares? We're disconnecting anyway. - m_Buffer.ResetRead(); - if (m_Buffer.CanReadBytes(2)) - { - byte val; - m_Buffer.ReadByte(val); // Packet type - Serverlist ping - m_Buffer.ReadByte(val); // 0x01 magic value - ASSERT(val == 0x01); - } - - // http://wiki.vg/wiki/index.php?title=Server_List_Ping&oldid=3100 - AString NumPlayers; - Printf(NumPlayers, "%d", cRoot::Get()->GetServer()->GetNumPlayers()); - AString MaxPlayers; - Printf(MaxPlayers, "%d", cRoot::Get()->GetServer()->GetMaxPlayers()); - - AString ProtocolVersionNum; - Printf(ProtocolVersionNum, "%d", cRoot::Get()->GetPrimaryServerVersion()); - AString ProtocolVersionTxt(GetVersionTextFromInt(cRoot::Get()->GetPrimaryServerVersion())); - - // Cannot use Printf() because of in-string NUL bytes. - Reply = cChatColor::Delimiter; - Reply.append("1"); - Reply.push_back(0); - Reply.append(ProtocolVersionNum); - Reply.push_back(0); - Reply.append(ProtocolVersionTxt); - Reply.push_back(0); - Reply.append(cRoot::Get()->GetServer()->GetDescription()); - Reply.push_back(0); - Reply.append(NumPlayers); - Reply.push_back(0); - Reply.append(MaxPlayers); - break; - } - } // switch (m_PrimaryServerVersion) - m_Client->Kick(Reply); -} - - - - diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h deleted file mode 100644 index c085e2cd8..000000000 --- a/source/Protocol/ProtocolRecognizer.h +++ /dev/null @@ -1,152 +0,0 @@ - -// ProtocolRecognizer.h - -// Interfaces to the cProtocolRecognizer class representing the meta-protocol that recognizes possibly multiple -// protocol versions and redirects everything to them - - - - - -#pragma once - -#include "Protocol.h" -#include "../ByteBuffer.h" - - - - - -// Adjust these if a new protocol is added or an old one is removed: -#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.7.2" -#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78, 4" - - - - - -class cProtocolRecognizer : - public cProtocol -{ - typedef cProtocol super; - -public: - enum - { - PROTO_VERSION_1_2_5 = 29, - PROTO_VERSION_1_3_2 = 39, - PROTO_VERSION_1_4_2 = 47, - PROTO_VERSION_1_4_4 = 49, - PROTO_VERSION_1_4_6 = 51, - PROTO_VERSION_1_5_0 = 60, - PROTO_VERSION_1_5_2 = 61, - PROTO_VERSION_1_6_1 = 73, - PROTO_VERSION_1_6_2 = 74, - PROTO_VERSION_1_6_3 = 77, - PROTO_VERSION_1_6_4 = 78, - - PROTO_VERSION_NEXT, - PROTO_VERSION_LATEST = PROTO_VERSION_NEXT - 1, ///< Automatically assigned to the last protocol version, this serves as the default for PrimaryServerVersion - - // These will be kept "under" the next / latest, because the next and latest are only needed for previous protocols - PROTO_VERSION_1_7_2 = 4, - } ; - - cProtocolRecognizer(cClientHandle * a_Client); - virtual ~cProtocolRecognizer(); - - /// Translates protocol version number into protocol version text: 49 -> "1.4.4" - static AString GetVersionTextFromInt(int a_ProtocolVersion); - - /// Called when client sends some data: - virtual void DataReceived(const char * a_Data, int a_Size) override; - - /// Sending stuff to clients (alphabetically sorted): - virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; - virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; - virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; - virtual void SendChat (const AString & a_Message) override; - virtual void SendChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) override; - virtual void SendCollectPickup (const cPickup & a_Pickup, const cPlayer & a_Player) override; - virtual void SendDestroyEntity (const cEntity & a_Entity) override; - virtual void SendDisconnect (const AString & a_Reason) override; - virtual void SendEditSign (int a_BlockX, int a_BlockY, int a_BlockZ) override; ///< Request the client to open up the sign editor for the sign (1.6+) - virtual void SendEntityEquipment (const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) override; - virtual void SendEntityHeadLook (const cEntity & a_Entity) override; - virtual void SendEntityLook (const cEntity & a_Entity) override; - virtual void SendEntityMetadata (const cEntity & a_Entity) override; - virtual void SendEntityProperties (const cEntity & a_Entity) override; - virtual void SendEntityRelMove (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityRelMoveLook (const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) override; - virtual void SendEntityStatus (const cEntity & a_Entity, char a_Status) override; - virtual void SendEntityVelocity (const cEntity & a_Entity) override; - virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override; - virtual void SendGameMode (eGameMode a_GameMode) override; - virtual void SendHealth (void) override; - virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; - virtual void SendKeepAlive (int a_PingID) override; - virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; - virtual void SendPickupSpawn (const cPickup & a_Pickup) override; - virtual void SendPlayerAbilities (void) override; - virtual void SendPlayerAnimation (const cPlayer & a_Player, char a_Animation) override; - virtual void SendPlayerListItem (const cPlayer & a_Player, bool a_IsOnline) override; - virtual void SendPlayerMaxSpeed (void) override; - virtual void SendPlayerMoveLook (void) override; - virtual void SendPlayerPosition (void) override; - virtual void SendPlayerSpawn (const cPlayer & a_Player) override; - virtual void SendRespawn (void) override; - virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; - virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override; - virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; - virtual void SendSpawnMob (const cMonster & a_Mob) override; - virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; - virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; - virtual void SendTabCompletionResults(const AStringVector & a_Results) override; - virtual void SendTeleportEntity (const cEntity & a_Entity) override; - virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; - virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; - virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) 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 SendUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ ) override; - virtual void SendWeather (eWeather a_Weather) override; - virtual void SendWholeInventory (const cWindow & a_Window) override; - virtual void SendWindowClose (const cWindow & a_Window) override; - virtual void SendWindowOpen (const cWindow & a_Window) override; - virtual void SendWindowProperty (const cWindow & a_Window, short a_Property, short a_Value) override; - - virtual AString GetAuthServerID(void) override; - - virtual void SendData(const char * a_Data, int a_Size) override; - -protected: - cProtocol * m_Protocol; //< The recognized protocol - cByteBuffer m_Buffer; //< Buffer for the incoming data until we recognize the protocol - - /// Tries to recognize protocol based on m_Buffer contents; returns true if recognized - bool TryRecognizeProtocol(void); - - /** Tries to recognize a protocol in the length-less family, based on m_Buffer; returns true if recognized. - Handles protocols before release 1.7, that didn't include packet lengths, and started with a 0x02 handshake packet - Note that length-less server ping is handled directly in TryRecognizeProtocol(), this function is called only - when the 0x02 Handshake packet has been received - */ - bool TryRecognizeLengthlessProtocol(void); - - /** Tries to recognize a protocol in the leghted family (1.7+), based on m_Buffer; returns true if recognized. - The packet length and type have already been read, type is 0 - The number of bytes remaining in the packet is passed as a_PacketLengthRemaining - **/ - bool TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRemaining); - - /** Called when the recognizer gets a length-less protocol's server ping packet - Responds with server stats and destroys the client. - */ - void SendLengthlessServerPing(void); -} ; - - - - - -- cgit v1.2.3