diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | Tools/ProtoProxy/Connection.cpp | 128 | ||||
-rw-r--r-- | source/Bindings.cpp | 102 | ||||
-rw-r--r-- | source/Bindings.h | 2 | ||||
-rw-r--r-- | source/ByteBuffer.cpp | 2 | ||||
-rw-r--r-- | source/ClientHandle.cpp | 4 | ||||
-rw-r--r-- | source/Entities/Entity.cpp | 4 | ||||
-rw-r--r-- | source/Entities/Entity.h | 6 | ||||
-rw-r--r-- | source/Entities/Player.h | 4 | ||||
-rw-r--r-- | source/Protocol/Protocol17x.cpp | 475 | ||||
-rw-r--r-- | source/Protocol/Protocol17x.h | 93 |
11 files changed, 538 insertions, 286 deletions
@@ -10,7 +10,7 @@ Installation To install MCServer, you can either download the repository and compile it, or download a pre-compiled version. -If you are cloning the repository, you are required to recursively download the submodules also with `git clone -r`. When pulling, you should use `git pull --recurse-submodules`. You might want to set the base forms as aliases to these. +After you've cloned the repository, you probably want to pull down the submodules (core plugins, some dependencies). This can be achieved with `git submodule init` and then on a regular basis (to keep up to date) `git submodule update`. Compilation instructions are available in the COMPILING file. @@ -27,3 +27,5 @@ Other Stuff ----------- For other stuff, including plugins and discussion, check the [forums](http://forum.mc-server.org) and [wiki](http://mc-server.org/wiki/). + +Earn bitcoins for commits or donate to reward the MCServer developers: [![tip for next commit](http://tip4commit.com/projects/74.svg)](http://tip4commit.com/projects/74) diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index b99415900..627b42052 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -143,112 +143,6 @@ typedef unsigned char Byte; -enum -{ - // client-bound packets: - PACKET_C_KEEPALIVE = 0x00, - PACKET_C_JOIN_GAME = 0x01, - PACKET_C_CHAT_MESSAGE = 0x02, - - // server-bound packets: - PACKET_S_KEEPALIVE = 0x00, // Also the initial handshake, as the very first packet - PACKET_S_CHAT_MESSAGE = 0x01, - - PACKET_TIME_UPDATE = 0x03, - PACKET_ENTITY_EQUIPMENT = 0x04, - PACKET_SPAWN_POSITION = 0x05, - PACKET_UPDATE_HEALTH = 0x06, - PACKET_USE_ENTITY = 0x07, - PACKET_PLAYER_ON_GROUND = 0x0a, - PACKET_PLAYER_POSITION = 0x0b, - PACKET_PLAYER_LOOK = 0x0c, - PACKET_PLAYER_POSITION_LOOK = 0x0d, - PACKET_BLOCK_DIG = 0x0e, - PACKET_BLOCK_PLACE = 0x0f, - PACKET_SLOT_SELECT = 0x10, - PACKET_PLAYER_ANIMATION = 0x12, - PACKET_ENTITY_ACTION = 0x13, - PACKET_SPAWN_NAMED_ENTITY = 0x14, - PACKET_SPAWN_PICKUP = 0x15, - PACKET_COLLECT_PICKUP = 0x16, - PACKET_SPAWN_OBJECT_VEHICLE = 0x17, - PACKET_SPAWN_MOB = 0x18, - PACKET_SPAWN_PAINTING = 0x19, - PACKET_SPAWN_EXPERIENCE_ORB = 0x1a, - PACKET_ENTITY_VELOCITY = 0x1c, - PACKET_DESTROY_ENTITIES = 0x1d, - PACKET_ENTITY = 0x1e, - PACKET_ENTITY_RELATIVE_MOVE = 0x1f, - PACKET_ENTITY_LOOK = 0x20, - PACKET_ENTITY_RELATIVE_MOVE_LOOK = 0x21, - PACKET_ENTITY_TELEPORT = 0x22, - PACKET_ENTITY_HEAD_LOOK = 0x23, - PACKET_ENTITY_STATUS = 0x26, - PACKET_ATTACH_ENTITY = 0x27, - PACKET_ENTITY_METADATA = 0x28, - PACKET_ENTITY_EFFECT = 0x29, - PACKET_ENTITY_EFFECT_REMOVE = 0x2a, - PACKET_SET_EXPERIENCE = 0x2b, - PACKET_ENTITY_PROPERTIES = 0x2c, - PACKET_MAP_CHUNK = 0x33, - PACKET_MULTI_BLOCK_CHANGE = 0x34, - PACKET_BLOCK_CHANGE = 0x35, - PACKET_BLOCK_ACTION = 0x36, - PACKET_MAP_CHUNK_BULK = 0x38, - PACKET_EXPLOSION = 0x3c, - PACKET_SOUND_EFFECT = 0x3d, - PACKET_NAMED_SOUND_EFFECT = 0x3e, - PACKET_CHANGE_GAME_STATE = 0x46, - PACKET_WINDOW_OPEN = 0x64, - PACKET_WINDOW_CLOSE = 0x65, - PACKET_WINDOW_CLICK = 0x66, - PACKET_SET_SLOT = 0x67, - PACKET_WINDOW_CONTENTS = 0x68, - PACKET_CREATIVE_INVENTORY_ACTION = 0x6b, - PACKET_UPDATE_SIGN = 0x82, - PACKET_UPDATE_TILE_ENTITY = 0x84, - PACKET_PLAYER_LIST_ITEM = 0xc9, - PACKET_PLAYER_ABILITIES = 0xca, - PACKET_INCREMENT_STATISTIC = 0xc8, - PACKET_TAB_COMPLETION = 0xcb, - PACKET_LOCALE_AND_VIEW = 0xcc, - PACKET_CLIENT_STATUSES = 0xcd, - PACKET_PLUGIN_MESSAGE = 0xfa, - PACKET_ENCRYPTION_KEY_RESPONSE = 0xfc, - PACKET_ENCRYPTION_KEY_REQUEST = 0xfd, - PACKET_PING = 0xfe, - PACKET_KICK = 0xff, - - // Synonyms: - PACKET_DISCONNECT = PACKET_KICK, -} ; - - - - -enum -{ - OBJECT_BOAT = 1, - OBJECT_MINECART = 10, - OBJECT_MINECART_STORAGE = 11, - OBJECT_MINECART_POWERED = 12, - OBJECT_MINECART_TNT = 13, - OBJECT_MINECART_HOPPER = 14, - OBJECT_TNT = 50, - OBJECT_ENDERCRYSTAL = 51, - OBJECT_ARROW = 60, - OBJECT_SNOWBALL = 61, - OBJECT_EGG = 62, - OBJECT_FALLING_BLOCK = 70, - OBJECT_EYE_OF_ENDER = 72, - OBJECT_DRAGON_EGG = 74, - OBJECT_FISHING_FLOAT = 90, -} ; - - - - - AString PrintableAbsIntTriplet(int a_X, int a_Y, int a_Z, double a_Divisor = 32) { return Printf("<%d, %d, %d> ~ {%.02f, %.02f, %.02f}", @@ -718,6 +612,16 @@ bool cConnection::DecodeServersPackets(const char * a_Data, int a_Size) ) { // Not a complete packet yet + m_ServerBuffer.ResetRead(); + break; + } + if (PacketLen == 0) + { + m_ServerBuffer.ResetRead(); + AString All; + m_ServerBuffer.ReadAll(All); + DataLog(All.data(), All.size(), "====== Received a bad packet length? Inspect the contents below ======"); + m_ServerBuffer.CommitRead(); // Try to recover by marking everything as read break; } UInt32 PacketType, PacketReadSoFar; @@ -1467,11 +1371,11 @@ bool cConnection::HandleServerBlockChange(void) bool cConnection::HandleServerChangeGameState(void) { - HANDLE_SERVER_PACKET_READ(ReadChar, char, Reason); - HANDLE_SERVER_PACKET_READ(ReadChar, char, Data); + HANDLE_SERVER_PACKET_READ(ReadChar, char, Reason); + HANDLE_SERVER_PACKET_READ(ReadBEFloat, float, Data); Log("Received a PACKET_CHANGE_GAME_STATE from the server:"); Log(" Reason = %d", Reason); - Log(" Data = %d", Data); + Log(" Data = %f", Data); COPY_TO_CLIENT(); return true; } @@ -1996,6 +1900,7 @@ bool cConnection::HandleServerMapChunkBulk(void) // TODO: Save the compressed data into a file for later analysis COPY_TO_CLIENT(); + Sleep(50); return true; } @@ -2324,7 +2229,9 @@ bool cConnection::HandleServerSpawnObjectVehicle(void) m_ServerBuffer.ResetRead(); m_ServerBuffer.ReadAll(Buffer); m_ServerBuffer.ResetRead(); - m_ServerBuffer.SkipRead(1); + UInt32 PacketLen, PacketType; + m_ServerBuffer.ReadVarInt(PacketLen); + m_ServerBuffer.ReadVarInt(PacketType); if (Buffer.size() > 128) { // Only log up to 128 bytes @@ -2683,6 +2590,7 @@ bool cConnection::HandleServerWindowOpen(void) bool cConnection::HandleServerUnknownPacket(UInt32 a_PacketType, UInt32 a_PacketLen, UInt32 a_PacketReadSoFar) { AString Data; + ASSERT(a_PacketLen >= a_PacketReadSoFar); if (!m_ServerBuffer.ReadString(Data, a_PacketLen - a_PacketReadSoFar)) { return false; diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 8259eda81..a052d1062 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 10/28/13 13:11:03. +** Generated automatically by tolua++-1.0.92 on 11/02/13 17:43:37. */ #ifndef __cplusplus @@ -5220,6 +5220,38 @@ static int tolua_AllToLua_cEntity_GetRotation00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* method: GetYaw of class cEntity */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cEntity_GetYaw00 +static int tolua_AllToLua_cEntity_GetYaw00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const cEntity",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const cEntity* self = (const cEntity*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetYaw'", NULL); +#endif + { + double tolua_ret = (double) self->GetYaw(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'GetYaw'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: GetPitch of class cEntity */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cEntity_GetPitch00 static int tolua_AllToLua_cEntity_GetPitch00(lua_State* tolua_S) @@ -5879,6 +5911,39 @@ static int tolua_AllToLua_cEntity_SetRotation00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* method: SetYaw of class cEntity */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cEntity_SetYaw00 +static int tolua_AllToLua_cEntity_SetYaw00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cEntity",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + cEntity* self = (cEntity*) tolua_tousertype(tolua_S,1,0); + double a_Yaw = ((double) tolua_tonumber(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetYaw'", NULL); +#endif + { + self->SetYaw(a_Yaw); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'SetYaw'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: SetPitch of class cEntity */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cEntity_SetPitch00 static int tolua_AllToLua_cEntity_SetPitch00(lua_State* tolua_S) @@ -7990,6 +8055,38 @@ static int tolua_AllToLua_cPlayer_GetGameMode00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* method: GetEffectiveGameMode of class cPlayer */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cPlayer_GetEffectiveGameMode00 +static int tolua_AllToLua_cPlayer_GetEffectiveGameMode00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const cPlayer",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const cPlayer* self = (const cPlayer*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetEffectiveGameMode'", NULL); +#endif + { + eGameMode tolua_ret = (eGameMode) self->GetEffectiveGameMode(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'GetEffectiveGameMode'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: SetGameMode of class cPlayer */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cPlayer_SetGameMode00 static int tolua_AllToLua_cPlayer_SetGameMode00(lua_State* tolua_S) @@ -29965,6 +30062,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"GetPosZ",tolua_AllToLua_cEntity_GetPosZ00); tolua_function(tolua_S,"GetRot",tolua_AllToLua_cEntity_GetRot00); tolua_function(tolua_S,"GetRotation",tolua_AllToLua_cEntity_GetRotation00); + tolua_function(tolua_S,"GetYaw",tolua_AllToLua_cEntity_GetYaw00); tolua_function(tolua_S,"GetPitch",tolua_AllToLua_cEntity_GetPitch00); tolua_function(tolua_S,"GetRoll",tolua_AllToLua_cEntity_GetRoll00); tolua_function(tolua_S,"GetLookVector",tolua_AllToLua_cEntity_GetLookVector00); @@ -29985,6 +30083,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"SetPosition",tolua_AllToLua_cEntity_SetPosition01); tolua_function(tolua_S,"SetRot",tolua_AllToLua_cEntity_SetRot00); tolua_function(tolua_S,"SetRotation",tolua_AllToLua_cEntity_SetRotation00); + tolua_function(tolua_S,"SetYaw",tolua_AllToLua_cEntity_SetYaw00); tolua_function(tolua_S,"SetPitch",tolua_AllToLua_cEntity_SetPitch00); tolua_function(tolua_S,"SetRoll",tolua_AllToLua_cEntity_SetRoll00); tolua_function(tolua_S,"SetSpeed",tolua_AllToLua_cEntity_SetSpeed00); @@ -30058,6 +30157,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"GetThrowStartPos",tolua_AllToLua_cPlayer_GetThrowStartPos00); tolua_function(tolua_S,"GetThrowSpeed",tolua_AllToLua_cPlayer_GetThrowSpeed00); tolua_function(tolua_S,"GetGameMode",tolua_AllToLua_cPlayer_GetGameMode00); + tolua_function(tolua_S,"GetEffectiveGameMode",tolua_AllToLua_cPlayer_GetEffectiveGameMode00); tolua_function(tolua_S,"SetGameMode",tolua_AllToLua_cPlayer_SetGameMode00); tolua_function(tolua_S,"IsGameModeCreative",tolua_AllToLua_cPlayer_IsGameModeCreative00); tolua_function(tolua_S,"IsGameModeSurvival",tolua_AllToLua_cPlayer_IsGameModeSurvival00); diff --git a/source/Bindings.h b/source/Bindings.h index 411e608d9..455307a89 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /*
** Lua binding: AllToLua
-** Generated automatically by tolua++-1.0.92 on 10/28/13 13:11:04.
+** Generated automatically by tolua++-1.0.92 on 11/02/13 17:43:38.
*/
/* Exported function */
diff --git a/source/ByteBuffer.cpp b/source/ByteBuffer.cpp index bccd1c48b..1cdd2f430 100644 --- a/source/ByteBuffer.cpp +++ b/source/ByteBuffer.cpp @@ -195,7 +195,7 @@ int cByteBuffer::GetUsedSpace(void) const { CHECK_THREAD; CheckValid(); - return m_BufferSize - GetFreeSpace(); + return m_BufferSize - GetFreeSpace() - 1; } diff --git a/source/ClientHandle.cpp b/source/ClientHandle.cpp index ea8b48f9d..6860a29ca 100644 --- a/source/ClientHandle.cpp +++ b/source/ClientHandle.cpp @@ -1470,7 +1470,7 @@ void cClientHandle::Tick(float a_Dt) } // If the chunk the player's in was just sent, spawn the player: - if (m_HasSentPlayerChunk && (m_State != csPlaying)) + if (m_HasSentPlayerChunk && (m_State != csPlaying) && !IsDestroying()) { if (!cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*m_Player)) { @@ -2138,7 +2138,7 @@ void cClientHandle::PacketUnknown(unsigned char a_PacketType) LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_IPString.c_str()); AString Reason; - Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", a_PacketType); + Printf(Reason, "Unknown [C->S] PacketType: 0x%02x", a_PacketType); SendDisconnect(Reason); Destroy(); } diff --git a/source/Entities/Entity.cpp b/source/Entities/Entity.cpp index d465c75bd..3bea7bc01 100644 --- a/source/Entities/Entity.cpp +++ b/source/Entities/Entity.cpp @@ -1178,9 +1178,9 @@ void cEntity::SetMass(double a_Mass) -void cEntity::SetRotation(double a_Rotation) +void cEntity::SetYaw(double a_Yaw) { - m_Rot.x = a_Rotation; + m_Rot.x = a_Yaw; m_bDirtyOrientation = true; WrapRotation(); } diff --git a/source/Entities/Entity.h b/source/Entities/Entity.h index c6b70a7fc..dafda7826 100644 --- a/source/Entities/Entity.h +++ b/source/Entities/Entity.h @@ -151,7 +151,8 @@ public: double GetPosY (void) const { return m_Pos.y; } double GetPosZ (void) const { return m_Pos.z; } const Vector3d & GetRot (void) const { return m_Rot; } - double GetRotation (void) const { return m_Rot.x; } + double GetRotation (void) const { return m_Rot.x; } // OBSOLETE, use GetYaw() instead + double GetYaw (void) const { return m_Rot.x; } double GetPitch (void) const { return m_Rot.y; } double GetRoll (void) const { return m_Rot.z; } Vector3d GetLookVector(void) const; @@ -173,7 +174,8 @@ public: void SetPosition(double a_PosX, double a_PosY, double a_PosZ); void SetPosition(const Vector3d & a_Pos) { SetPosition(a_Pos.x, a_Pos.y, a_Pos.z); } void SetRot (const Vector3f & a_Rot); - void SetRotation(double a_Rotation); + void SetRotation(double a_Rotation) { SetYaw(a_Rotation); } // OBSOLETE, use SetYaw() instead + void SetYaw (double a_Yaw); void SetPitch (double a_Pitch); void SetRoll (double a_Roll); void SetSpeed (double a_SpeedX, double a_SpeedY, double a_SpeedZ); diff --git a/source/Entities/Player.h b/source/Entities/Player.h index 449a63231..01efa3681 100644 --- a/source/Entities/Player.h +++ b/source/Entities/Player.h @@ -4,6 +4,7 @@ #include "Pawn.h" #include "../Inventory.h" #include "../Defines.h" +#include "../World.h" @@ -99,6 +100,9 @@ public: /// Returns the current gamemode. Partly OBSOLETE, you should use IsGameModeXXX() functions wherever applicable eGameMode GetGameMode(void) const { return m_GameMode; } + /// Returns the current effective gamemode (inherited gamemode is resolved before returning) + eGameMode GetEffectiveGameMode(void) const { return (m_GameMode == gmNotSet) ? m_World->GetGameMode() : m_GameMode; } + /** Sets the gamemode for the player. The gamemode may be gmNotSet, in that case the player inherits the world's gamemode. Updates the gamemode on the client (sends the packet) 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<CryptoPP::AES>::Decryption m_Decryptor; CryptoPP::CFB_Mode<CryptoPP::AES>::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); } ; |