From b7d524423c23470cd11e720eeb48368c072838cb Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 7 Feb 2012 20:49:52 +0000 Subject: Rewritten all packets to use buffers instead of direct sockets, for future cSocketThreads compatibility. Moved data sending from cPacket into cSocket git-svn-id: http://mc-server.googlecode.com/svn/trunk@240 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Endianness.h | 40 ++- source/Globals.h | 2 + source/cBlockingTCPLink.cpp | 4 +- source/cClientHandle.cpp | 61 ++-- source/cSocket.cpp | 25 +- source/cSocket.h | 8 + source/packets/cPacket.cpp | 325 +++++++++++++-------- source/packets/cPacket.h | 95 ++++-- source/packets/cPacket_13.cpp | 15 +- source/packets/cPacket_13.h | 10 +- source/packets/cPacket_AddToInventory.cpp | 19 +- source/packets/cPacket_AddToInventory.h | 8 +- source/packets/cPacket_ArmAnim.cpp | 36 +-- source/packets/cPacket_ArmAnim.h | 4 +- source/packets/cPacket_BlockAction.cpp | 42 +-- source/packets/cPacket_BlockAction.h | 17 +- source/packets/cPacket_BlockChange.cpp | 28 +- source/packets/cPacket_BlockChange.h | 7 +- source/packets/cPacket_BlockDig.cpp | 46 +-- source/packets/cPacket_BlockDig.h | 23 +- source/packets/cPacket_BlockPlace.cpp | 40 +-- source/packets/cPacket_BlockPlace.h | 18 +- source/packets/cPacket_Chat.cpp | 28 +- source/packets/cPacket_Chat.h | 12 +- source/packets/cPacket_CollectItem.cpp | 22 +- source/packets/cPacket_CollectItem.h | 11 +- source/packets/cPacket_CreativeInventoryAction.cpp | 55 ++-- source/packets/cPacket_CreativeInventoryAction.h | 12 +- source/packets/cPacket_DestroyEntity.cpp | 20 +- source/packets/cPacket_DestroyEntity.h | 15 +- source/packets/cPacket_Disconnect.cpp | 28 +- source/packets/cPacket_Disconnect.h | 14 +- source/packets/cPacket_EntityEquipment.cpp | 53 ++-- source/packets/cPacket_EntityEquipment.h | 13 +- source/packets/cPacket_EntityLook.cpp | 30 +- source/packets/cPacket_EntityLook.h | 10 +- source/packets/cPacket_EntityStatus.cpp | 17 +- source/packets/cPacket_EntityStatus.h | 9 +- source/packets/cPacket_Explosion.cpp | 55 ++-- source/packets/cPacket_Explosion.h | 10 +- source/packets/cPacket_Flying.cpp | 14 +- source/packets/cPacket_Flying.h | 10 +- source/packets/cPacket_Handshake.cpp | 34 +-- source/packets/cPacket_Handshake.h | 14 +- source/packets/cPacket_InventoryProgressBar.cpp | 19 +- source/packets/cPacket_InventoryProgressBar.h | 12 +- source/packets/cPacket_InventorySlot.cpp | 23 +- source/packets/cPacket_InventorySlot.h | 13 +- source/packets/cPacket_ItemData.cpp | 79 +++-- source/packets/cPacket_ItemData.h | 8 +- source/packets/cPacket_ItemSwitch.cpp | 29 +- source/packets/cPacket_ItemSwitch.h | 15 +- source/packets/cPacket_KeepAlive.cpp | 30 +- source/packets/cPacket_KeepAlive.h | 14 +- source/packets/cPacket_Login.cpp | 71 ++--- source/packets/cPacket_Login.h | 23 +- source/packets/cPacket_MapChunk.cpp | 53 ++-- source/packets/cPacket_MapChunk.h | 14 +- source/packets/cPacket_Metadata.cpp | 47 ++- source/packets/cPacket_Metadata.h | 12 +- source/packets/cPacket_MultiBlock.cpp | 40 +-- source/packets/cPacket_MultiBlock.h | 12 +- source/packets/cPacket_NamedEntitySpawn.cpp | 44 +-- source/packets/cPacket_NamedEntitySpawn.h | 12 +- source/packets/cPacket_NewInvalidState.cpp | 39 +-- source/packets/cPacket_NewInvalidState.h | 11 +- source/packets/cPacket_PickupSpawn.cpp | 68 ++--- source/packets/cPacket_PickupSpawn.h | 12 +- source/packets/cPacket_Ping.h | 12 +- source/packets/cPacket_PlayerListItem.cpp | 60 ++-- source/packets/cPacket_PlayerListItem.h | 22 +- source/packets/cPacket_PlayerLook.cpp | 49 ++-- source/packets/cPacket_PlayerLook.h | 22 +- source/packets/cPacket_PlayerMoveLook.cpp | 73 ++--- source/packets/cPacket_PlayerMoveLook.h | 20 +- source/packets/cPacket_PlayerPosition.cpp | 52 ++-- source/packets/cPacket_PlayerPosition.h | 23 +- source/packets/cPacket_PreChunk.cpp | 22 +- source/packets/cPacket_PreChunk.h | 17 +- source/packets/cPacket_RelativeEntityMove.cpp | 24 +- source/packets/cPacket_RelativeEntityMove.h | 12 +- source/packets/cPacket_RelativeEntityMoveLook.cpp | 29 +- source/packets/cPacket_RelativeEntityMoveLook.h | 12 +- source/packets/cPacket_Respawn.cpp | 53 ++-- source/packets/cPacket_Respawn.h | 16 +- source/packets/cPacket_SoundEffect.cpp | 41 ++- source/packets/cPacket_SoundEffect.h | 10 +- source/packets/cPacket_SpawnMob.cpp | 47 +-- source/packets/cPacket_SpawnMob.h | 25 +- source/packets/cPacket_TeleportEntity.cpp | 36 +-- source/packets/cPacket_TeleportEntity.h | 18 +- source/packets/cPacket_Thunderbolt.cpp | 42 +-- source/packets/cPacket_Thunderbolt.h | 18 +- source/packets/cPacket_TimeUpdate.cpp | 28 +- source/packets/cPacket_TimeUpdate.h | 15 +- source/packets/cPacket_UpdateHealth.cpp | 19 +- source/packets/cPacket_UpdateHealth.h | 10 +- source/packets/cPacket_UpdateSign.cpp | 61 ++-- source/packets/cPacket_UpdateSign.h | 26 +- source/packets/cPacket_UseEntity.cpp | 18 +- source/packets/cPacket_UseEntity.h | 16 +- source/packets/cPacket_WholeInventory.cpp | 41 +-- source/packets/cPacket_WholeInventory.h | 25 +- source/packets/cPacket_WindowClick.cpp | 40 +-- source/packets/cPacket_WindowClick.h | 20 +- source/packets/cPacket_WindowClose.cpp | 29 +- source/packets/cPacket_WindowClose.h | 14 +- source/packets/cPacket_WindowOpen.cpp | 26 +- source/packets/cPacket_WindowOpen.h | 21 +- 109 files changed, 1907 insertions(+), 1316 deletions(-) diff --git a/source/Endianness.h b/source/Endianness.h index 09ae3b372..d2c6a8a0a 100644 --- a/source/Endianness.h +++ b/source/Endianness.h @@ -1,14 +1,12 @@ + #pragma once -#ifdef _WIN32 - #include -#else - #include - #include -#endif + + + // Changes endianness -inline unsigned long long HostToNetwork8( void* a_Value ) +inline unsigned long long HostToNetwork8(const void* a_Value ) { unsigned long long __HostToNetwork8; memcpy( &__HostToNetwork8, a_Value, sizeof( __HostToNetwork8 ) ); @@ -16,7 +14,11 @@ inline unsigned long long HostToNetwork8( void* a_Value ) return __HostToNetwork8; } -inline unsigned int HostToNetwork4( void* a_Value ) + + + + +inline unsigned int HostToNetwork4(const void* a_Value ) { unsigned int __HostToNetwork4; memcpy( &__HostToNetwork4, a_Value, sizeof( __HostToNetwork4 ) ); @@ -24,7 +26,11 @@ inline unsigned int HostToNetwork4( void* a_Value ) return __HostToNetwork4; } -inline double NetworkToHostDouble8( void* a_Value ) + + + + +inline double NetworkToHostDouble8(const void* a_Value ) { #define ntohll(x) ((((unsigned long long)ntohl((u_long)x)) << 32) + ntohl(x >> 32)) unsigned long long buf = 0;//(*(unsigned long long*)a_Value); @@ -35,14 +41,22 @@ inline double NetworkToHostDouble8( void* a_Value ) return x; } -inline long long NetworkToHostLong8( void* a_Value ) + + + + +inline long long NetworkToHostLong8(const void * a_Value ) { unsigned long long buf = *(unsigned long long*)a_Value; buf = ntohll(buf); return *reinterpret_cast(&buf); } -inline float NetworkToHostFloat4( void* a_Value ) + + + + +inline float NetworkToHostFloat4(const void* a_Value ) { u_long buf = *(u_long*)a_Value; buf = ntohl( buf ); @@ -50,3 +64,7 @@ inline float NetworkToHostFloat4( void* a_Value ) memcpy( &x, &buf, sizeof(float) ); return x; } + + + + diff --git a/source/Globals.h b/source/Globals.h index 2b37c6df4..36d174548 100644 --- a/source/Globals.h +++ b/source/Globals.h @@ -27,6 +27,8 @@ #include #include // for mkdir #include + #include + #include #include #include #include diff --git a/source/cBlockingTCPLink.cpp b/source/cBlockingTCPLink.cpp index 5a36536bc..525f6e939 100644 --- a/source/cBlockingTCPLink.cpp +++ b/source/cBlockingTCPLink.cpp @@ -104,7 +104,7 @@ int cBlockingTCPLink::Send(char * a_Data, unsigned int a_Size, int a_Flags /* = LOGERROR("cBlockingTCPLink: Trying to send data without a valid connection!"); return -1; } - return cPacket::SendData( m_Socket, a_Data, a_Size, a_Flags ); + return m_Socket.Send(a_Data, a_Size); } @@ -119,7 +119,7 @@ int cBlockingTCPLink::SendMessage( const char* a_Message, int a_Flags /* = 0 */ LOGWARN("cBlockingTCPLink: Trying to send message without a valid connection!"); return -1; } - return cPacket::SendData( m_Socket, a_Message, strlen(a_Message), a_Flags ); + return m_Socket.Send(a_Message, strlen(a_Message)); } diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index 78fac80ae..bbf196ccb 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -190,8 +190,7 @@ cClientHandle::~cClientHandle() { cPacket_Disconnect Disconnect; Disconnect.m_Reason = "Server shut down? Kthnxbai"; - Disconnect.Send(m_Socket); - + m_Socket.Send(&Disconnect); m_Socket.CloseSocket(); } Lock.Unlock(); @@ -1586,7 +1585,7 @@ void cClientHandle::Tick(float a_Dt) if (cWorld::GetTime() - m_TimeLastPacket > 30.f) // 30 seconds time-out { cPacket_Disconnect DC("Nooooo!! You timed out! D: Come back!"); - DC.Send(m_Socket); + m_Socket.Send(&DC); cSleep::MilliSleep(1000); // Give packet some time to be received @@ -1731,7 +1730,7 @@ void cClientHandle::SendThread(void *lpParam) { LOGERROR("ERROR: Too many packets in queue for player %s !!", self->m_Username.c_str()); cPacket_Disconnect DC("Too many packets in queue."); - DC.Send(self->m_Socket); + self->m_Socket.Send(DC); cSleep::MilliSleep(1000); // Give packet some time to be received @@ -1773,7 +1772,7 @@ void cClientHandle::SendThread(void *lpParam) break; } - bool bSuccess = Packet->Send(self->m_Socket); + bool bSuccess = self->m_Socket.Send(Packet); SocketLock.Unlock(); if (!bSuccess) @@ -1806,51 +1805,67 @@ void cClientHandle::ReceiveThread(void *lpParam) cClientHandle* self = (cClientHandle*)lpParam; - char temp = 0; int iStat = 0; cSocket socket = self->GetSocket(); - while(self->m_bKeepThreadGoing) + AString Received; + while (self->m_bKeepThreadGoing) { - iStat = socket.Receive(&temp, 1, 0); - if (cSocket::IsSocketError(iStat) || iStat == 0) + char Buffer[1024]; + iStat = socket.Receive(Buffer, sizeof(Buffer), 0); + if (cSocket::IsSocketError(iStat) || (iStat == 0)) { - LOG("CLIENT DISCONNECTED (%i bytes):%s", iStat, GetWSAError().c_str()); + LOG("CLIENT DISCONNECTED (%i bytes):%s", iStat, cSocket::GetLastErrorString().c_str()); break; } - else + Received.append(Buffer, iStat); + + // Parse all complete packets in Received: + while (!Received.empty()) { - cPacket* pPacket = self->m_PacketMap[(unsigned char)temp]; + cPacket* pPacket = self->m_PacketMap[(unsigned char)Received[0]]; if (pPacket) { - if (pPacket->Parse(socket)) + int NumBytes = pPacket->Parse(Received.data() + 1, Received.size() - 1); + if (NumBytes == PACKET_ERROR) { - self->AddPacket(pPacket); - //self->HandlePendingPackets(); + LOGERROR("Protocol error while parsing packet type 0x%x; disconnecting client \"%s\"", Received[0], self->m_Username.c_str()); + cPacket_Disconnect DC("Protocol error"); + socket.Send(&DC); + + cSleep::MilliSleep(1000); // Give packet some time to be received + return; } - else + else if (NumBytes == PACKET_INCOMPLETE) { - LOGERROR("Something went wrong during PacketID 0x%02x (%s)", temp, cSocket::GetErrorString( cSocket::GetLastError() ).c_str()); - LOG("CLIENT %s DISCONNECTED", self->m_Username.c_str()); + // Not a complete packet break; } + else + { + // Packet parsed successfully, add it to internal queue: + self->AddPacket(pPacket); + // Erase the packet from the buffer: + assert(Received.size() > (size_t)NumBytes); + Received.erase(0, NumBytes + 1); + } } else { - LOG("Unknown packet: 0x%02x \'%c\' %i", (unsigned char)temp, (unsigned char)temp, (unsigned char)temp); + LOGERROR("Unknown packet type: 0x%2x", Received[0]); AString Reason; - Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", (unsigned char)temp); + Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", Received[0]); cPacket_Disconnect DC(Reason); - DC.Send(socket); + socket.Send(&DC); cSleep::MilliSleep(1000); // Give packet some time to be received break; } - } - } + } // while (!Received.empty()) + } // while (self->m_bKeepThreadGoing) self->Destroy(); diff --git a/source/cSocket.cpp b/source/cSocket.cpp index 90c31f58d..fa016762c 100644 --- a/source/cSocket.cpp +++ b/source/cSocket.cpp @@ -2,12 +2,11 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "cSocket.h" +#include "packets/cPacket.h" #ifndef _WIN32 #include #include - // #include - // #include #include //inet_ntoa() #else #define socklen_t int @@ -273,6 +272,28 @@ int cSocket::Send(const char * a_Buffer, unsigned int a_Length) +int cSocket::Send(const cPacket * a_Packet) +{ + AString Serialized; + a_Packet->Serialize(Serialized); + return Send(Serialized.data(), Serialized.size()); +} + + + + + +int cSocket::Send(const cPacket & a_Packet) +{ + AString Serialized; + a_Packet.Serialize(Serialized); + return Send(Serialized.data(), Serialized.size()); +} + + + + + unsigned short cSocket::GetPort(void) const { assert(IsValid()); diff --git a/source/cSocket.h b/source/cSocket.h index 8098714c4..7075adab5 100644 --- a/source/cSocket.h +++ b/source/cSocket.h @@ -4,6 +4,12 @@ +class cPacket; + + + + + class cSocket { public: @@ -63,6 +69,8 @@ public: int Connect(SockAddr_In & a_Address); // Returns 0 on success, !0 on failure int Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags ); int Send (const char * a_Buffer, unsigned int a_Length); + int Send (const cPacket * a_Packet); // Sends the packet, doesn't handle partial sends + int Send (const cPacket & a_Packet); // Sends the packet, doesn't handle partial sends unsigned short GetPort(void) const; // Returns 0 on failure diff --git a/source/packets/cPacket.cpp b/source/packets/cPacket.cpp index 06ae24706..284e80903 100644 --- a/source/packets/cPacket.cpp +++ b/source/packets/cPacket.cpp @@ -4,226 +4,303 @@ #include "cPacket.h" #include "../Endianness.h" -#ifdef _WIN32 - #define MSG_NOSIGNAL (0) + + + + +/* +// These checks cannot be done in preprocessor, since sizeof() is evaluated while compiling, so in preprocessing it's unknown. +// Check some basic type assumptions: +#if (sizeof(int) != 4) + #error "Bad size for int, protocol won't work" #endif -#ifdef __MAC_NA - #define MSG_NOSIGNAL (0) +#if (sizeof(float) != 4) + #error "Bad size for float, protocol won't work" #endif +#if (sizeof(double) != 8) + #error "Bad size for double, protocol won't work" +#endif +*/ + -//***************************************************************************** -// Blocking receive all function -//***************************************************************************** -int cPacket::RecvAll( cSocket & a_Socket, char* a_Data, unsigned int a_Size, int a_Options ) +int cPacket::ReadString16(const char * a_Data, int a_Size, AString & a_OutString ) { - unsigned int RequestSize = a_Size; - while(a_Size != 0) + int TotalBytes = 0; + short StrLen; + HANDLE_PACKET_READ(ReadShort, StrLen, TotalBytes); + + if (2 * StrLen > a_Size - TotalBytes) { - int Num = recv(a_Socket, a_Data, a_Size, a_Options); - if( cSocket::IsSocketError( Num ) ) - return Num; - a_Size -= Num; - a_Data += Num; + // The string is not yet complete in the buffer + return PACKET_INCOMPLETE; } - return RequestSize - a_Size; -} -//***************************************************************************** -// Own implementation of send() -//***************************************************************************** -int cPacket::SendData( cSocket & a_Socket, const char* a_Message, unsigned int a_Size, int a_Options ) -{ - return send(a_Socket, a_Message, a_Size, a_Options | MSG_NOSIGNAL ); + // Simple UTF-16 to UTF-8 conversion - discard higher bits, ignore multishort sequences: + a_OutString.clear(); + a_OutString.reserve(StrLen); + short * UTF16 = (short *)(a_Data + TotalBytes); + for ( int i = 0; i < StrLen; ++i ) + { + a_OutString.push_back( (char)ntohs(UTF16[i]) ); + } + + return TotalBytes + StrLen * sizeof(short); } -//***************************************************************************** -// New packets -//***************************************************************************** -bool cPacket::ReadString( std::string & a_OutString ) -{ - short StrLen; - if(!ReadShort( StrLen )) return false; - if( StrLen == 0 ) + +int cPacket::ReadShort(const char * a_Data, int a_Size, short & a_OutShort ) +{ + if (a_Size < 2) { - a_OutString.clear(); - return true; + return PACKET_INCOMPLETE; } + a_OutShort = ntohs(*((short *)a_Data)); + return 2; +} - char* cString = new char[StrLen]; - if( cSocket::IsSocketError( RecvAll( m_Socket, cString, StrLen, 0 ) ) ) return false; - a_OutString.assign( cString, StrLen ); - //printf("Discoved string: %s size: %i\n", a_OutString.c_str(), a_OutString.size() ); - delete [] cString; - return true; -} -bool cPacket::ReadString16( std::string & a_OutString ) -{ - short StrLen; - if(!ReadShort( StrLen )) return false; - a_OutString.clear(); - if( StrLen == 0 ) +int cPacket::ReadInteger(const char * a_Data, int a_Size, int & a_OutInteger ) +{ + if (a_Size < 4) { - return true; + return PACKET_INCOMPLETE; } + a_OutInteger = ntohl(*((int *)a_Data)); + return 4; +} - char* UTF16 = new char[StrLen*sizeof( short )]; - if( cSocket::IsSocketError( RecvAll( m_Socket, UTF16, StrLen * sizeof( short ), 0 ) ) ) return false; - for( int i = 0; i < StrLen; ++i ) - a_OutString.push_back( (char)UTF16[i*sizeof( short )+1] ); - //printf("Discoved string: %s size: %i\n", a_OutString.c_str(), a_OutString.size() ); - delete [] UTF16; - return true; -} -bool cPacket::ReadShort( short & a_OutShort ) -{ - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutShort, sizeof(short), 0 ) ) ) return false; - a_OutShort = ntohs(a_OutShort); - return true; -} -bool cPacket::ReadInteger( int & a_OutInteger ) +int cPacket::ReadInteger(const char * a_Data, int a_Size, unsigned int & a_OutInteger ) { - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutInteger, sizeof(int), 0 ) ) ) return false; - a_OutInteger = ntohl(a_OutInteger); - return true; + if (a_Size < 4) + { + return PACKET_INCOMPLETE; + } + a_OutInteger = ntohl(*((unsigned int *)a_Data)); + return 4; } -bool cPacket::ReadInteger( unsigned int & a_OutInteger ) -{ - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutInteger, sizeof(unsigned int), 0 ) ) ) return false; - a_OutInteger = ntohl(a_OutInteger); - return true; -} -bool cPacket::ReadFloat( float & a_OutFloat ) + + + +int cPacket::ReadFloat(const char * a_Data, int a_Size, float & a_OutFloat ) { - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutFloat, sizeof(float), 0 ) ) ) return false; - a_OutFloat = NetworkToHostFloat4( &a_OutFloat ); - return true; + if (a_Size < sizeof(float)) + { + return PACKET_INCOMPLETE; + } + a_OutFloat = NetworkToHostFloat4(a_Data); + return sizeof(float); } -bool cPacket::ReadDouble( double & a_OutDouble ) + + + + +int cPacket::ReadDouble(const char * a_Data, int a_Size, double & a_OutDouble ) { - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutDouble, sizeof(double), 0 ) ) ) return false; - a_OutDouble = NetworkToHostDouble8( &a_OutDouble ); - return true; + if (a_Size < sizeof(double)) + { + return PACKET_INCOMPLETE; + } + a_OutDouble = NetworkToHostDouble8(a_Data); + return sizeof(double); } -bool cPacket::ReadByte( char & a_OutByte ) + + + + +int cPacket::ReadByte(const char * a_Data, int a_Size, char & a_OutByte ) { - return !cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutByte, sizeof(char), 0 ) ); + if (a_Size < 1) + { + return PACKET_INCOMPLETE; + } + a_OutByte = *a_Data; + return 1; } -bool cPacket::ReadByte( unsigned char & a_OutByte ) + + + + +int cPacket::ReadByte(const char * a_Data, int a_Size, unsigned char & a_OutByte ) { - return !cSocket::IsSocketError(RecvAll( m_Socket, (char*)&a_OutByte, sizeof(char), 0 ) ); + if (a_Size < 1) + { + return PACKET_INCOMPLETE; + } + a_OutByte = *((unsigned char *)a_Data); + return 1; } -bool cPacket::ReadLong( long long & a_OutLong ) + + + + +int cPacket::ReadLong(const char * a_Data, int a_Size, long long & a_OutLong ) { - if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutLong, sizeof(long long), 0 ) ) ) return false; - a_OutLong = NetworkToHostLong8( &a_OutLong ); - return true; + if (a_Size < sizeof(a_OutLong)) + { + return PACKET_INCOMPLETE; + } + a_OutLong = NetworkToHostLong8(a_Data); + return sizeof(a_OutLong); } -bool cPacket::ReadBool( bool & a_OutBool ) + + + + +int cPacket::ReadBool(const char * a_Data, int a_Size, bool & a_OutBool ) { - if( cSocket::IsSocketError(RecvAll( m_Socket, (char*)&a_OutBool, sizeof(bool), 0 ) ) ) return false; - return true; + if (a_Size < sizeof(bool)) + { + return PACKET_INCOMPLETE; + } + a_OutBool = (*a_Data != 0); + return sizeof(bool); } -//***************************************************************************** -// Append variables to a c-String -//***************************************************************************** -void cPacket::AppendString( std::string & a_String, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendString(AString & a_Dst, const AString & a_String) { - AppendShort( (unsigned short)a_String.size(), a_Dst, a_Iterator ); - memcpy( a_Dst + a_Iterator, a_String.c_str(), a_String.size() ); a_Iterator += a_String.size(); + AppendShort(a_Dst, (unsigned short)a_String.size()); + a_Dst.append(a_String); } -void cPacket::AppendString16( std::string & a_String, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendString16(AString & a_Dst, const AString & a_String) { - AppendShort( (unsigned short)a_String.size(), a_Dst, a_Iterator ); - char* UTF16 = new char[ a_String.size() * sizeof( short ) ]; + AppendShort(a_Dst, (unsigned short)a_String.size()); + std::auto_ptr UTF16(new char[a_String.size() * sizeof( short ) ]); for( unsigned int i = 0; i < a_String.size(); ++i ) { - UTF16[i*sizeof( short )] = 0x00;//a_String[i]; - UTF16[i*sizeof( short )+1] = a_String[i]; + UTF16.get()[i * sizeof( short )] = 0x00; + UTF16.get()[i * sizeof( short ) + 1] = a_String[i]; } - memcpy( a_Dst + a_Iterator, UTF16, a_String.size() * sizeof( short ) ); a_Iterator += a_String.size() * sizeof( short ); - delete [] UTF16; + a_Dst.append(UTF16.get(), a_String.size() * sizeof(short)); } -void cPacket::AppendShort( short a_Short, char *a_Dst, unsigned int &a_Iterator ) + + + + +void cPacket::AppendShort(AString & a_Dst, short a_Short) { short ConvertedShort = htons( a_Short ); - memcpy( a_Dst + a_Iterator, &ConvertedShort, sizeof( short ) ); a_Iterator+=sizeof( short ); + a_Dst.append((const char *)&ConvertedShort, sizeof(short)); } -void cPacket::AppendShort( unsigned short a_Short, char *a_Dst, unsigned int &a_Iterator ) + + + + +void cPacket::AppendShort(AString & a_Dst, unsigned short a_Short) { short ConvertedShort = htons( a_Short ); - memcpy( a_Dst + a_Iterator, &ConvertedShort, sizeof( unsigned short ) ); a_Iterator+=sizeof( unsigned short ); + a_Dst.append((const char *)&ConvertedShort, sizeof(short)); } -void cPacket::AppendInteger( int a_Integer, char* a_Dst, unsigned int & a_Iterator ) + + + +void cPacket::AppendInteger(AString & a_Dst, int a_Integer) { int ConvertedInt = htonl( a_Integer ); - memcpy( a_Dst + a_Iterator, &ConvertedInt, sizeof( int ) ); a_Iterator+=sizeof( int ); + a_Dst.append((const char *)&ConvertedInt, sizeof(int)); } -void cPacket::AppendInteger( unsigned int a_Integer, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendInteger(AString & a_Dst, unsigned int a_Integer) { unsigned int ConvertedInt = htonl( a_Integer ); - memcpy( a_Dst + a_Iterator, &ConvertedInt, sizeof( unsigned int ) ); a_Iterator+=sizeof( unsigned int ); + a_Dst.append((const char *)&ConvertedInt, sizeof(int)); } -void cPacket::AppendFloat( float a_Float, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendFloat(AString & a_Dst, float a_Float) { unsigned int ConvertedFloat = HostToNetwork4(&a_Float); - memcpy( a_Dst + a_Iterator, &ConvertedFloat, sizeof(float) ); a_Iterator += sizeof(float); + a_Dst.append((const char *)&ConvertedFloat, sizeof(int)); } -void cPacket::AppendDouble( double & a_Double, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendDouble(AString & a_Dst, const double & a_Double) { unsigned long long ConvertedDouble = HostToNetwork8(&a_Double); - memcpy( a_Dst + a_Iterator, &ConvertedDouble, sizeof(double) ); a_Iterator += sizeof(double); + a_Dst.append((const char *)&ConvertedDouble, 8); } -void cPacket::AppendByte( char a_Byte, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendByte(AString & a_Dst, char a_Byte) { - a_Dst[a_Iterator] = a_Byte; a_Iterator+=sizeof(char); + a_Dst.append(&a_Byte, 1); } -void cPacket::AppendLong( long long & a_Long, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendLong(AString & a_Dst, const long long & a_Long) { unsigned long long ConvertedLong = HostToNetwork8(&a_Long); - memcpy( a_Dst + a_Iterator, &ConvertedLong, sizeof(long long) ); - a_Iterator += sizeof( long long ); + a_Dst.append((const char *)&ConvertedLong, sizeof(a_Long)); } -void cPacket::AppendBool( bool a_Bool, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendBool(AString & a_Dst, bool a_Bool) { - a_Dst[a_Iterator] = (char)a_Bool; a_Iterator+=sizeof(bool); + a_Dst.append((const char *)&a_Bool, 1); } -void cPacket::AppendData( char* a_Data, unsigned int a_Size, char* a_Dst, unsigned int & a_Iterator ) + + + + +void cPacket::AppendData(AString & a_Dst, const char * a_Data, unsigned int a_Size) { - memcpy( a_Dst + a_Iterator, a_Data, a_Size ); a_Iterator += a_Size; + a_Dst.append(a_Data, a_Size); } + + + + diff --git a/source/packets/cPacket.h b/source/packets/cPacket.h index 817aa1a27..fbdde5ac3 100644 --- a/source/packets/cPacket.h +++ b/source/packets/cPacket.h @@ -8,6 +8,28 @@ +#define PACKET_INCOMPLETE -2 +#define PACKET_ERROR -1 + + + + + +// Use this macro to simplify handling several ReadXXX in a row. It assumes that you want [a_Data, a_Size] parsed (which is true for all Parse() functions) +#define HANDLE_PACKET_READ(Proc, Var, TotalBytes) \ + { \ + int res = Proc(a_Data + TotalBytes, a_Size - TotalBytes, Var); \ + if (res < 0) \ + { \ + return res; \ + } \ + TotalBytes += res; \ + } + + + + + class cPacket { public: @@ -16,41 +38,52 @@ public: {} virtual ~cPacket() {} - virtual bool Parse( cSocket & a_Socket) {a_Socket.CloseSocket(); LOGERROR("Undefined NEW Parse function %x\n", m_PacketID ); return false; } - virtual bool Send( cSocket & a_Socket) {a_Socket.CloseSocket(); LOGERROR("Undefined NEW Send function %x\n", m_PacketID ); return false; } - virtual cPacket* Clone() const = 0; + /// Called to parse the packet. Packet type has already been read and the correct packet type created. Return the number of characters processed, PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error + virtual int Parse(const char * a_Data, int a_Size) + { + LOGERROR("Undefined Parse function for packet type 0x%x\n", m_PacketID ); + assert(!"Undefined Parse function"); + return -1; + } + + /// Called to serialize the packet into a string. Append all packet data to a_Data, including the packet type! + virtual void Serialize(AString & a_Data) const + { + LOGERROR("Undefined Serialize function for packet type 0x%x\n", m_PacketID ); + assert(!"Undefined Serialize function"); + } + + virtual cPacket * Clone() const = 0; unsigned char m_PacketID; - cSocket m_Socket; // Current socket being used + protected: - bool ReadString ( std::string & a_OutString ); - bool ReadString16( std::string & a_OutString ); - bool ReadShort ( short & a_Short ); - bool ReadInteger(int & a_OutInteger ); - bool ReadInteger(unsigned int & a_OutInteger ); - bool ReadFloat ( float & a_OutFloat ); - bool ReadDouble ( double & a_OutDouble ); - bool ReadByte ( char & a_OutByte ); - bool ReadByte ( unsigned char & a_OutByte ); - bool ReadLong ( long long & a_OutLong ); - bool ReadBool ( bool & a_OutBool ); - - void AppendString ( std::string & a_String, char* a_Dst, unsigned int & a_Iterator ); - void AppendString16 ( std::string & a_String, char* a_Dst, unsigned int & a_Iterator ); - void AppendShort ( short a_Short, char* a_Dst, unsigned int & a_Iterator ); - void AppendShort ( unsigned short a_Short, char* a_Dst, unsigned int & a_Iterator ); - void AppendInteger ( int a_Integer, char* a_Dst, unsigned int & a_Iterator ); - void AppendInteger ( unsigned int a_Integer, char* a_Dst, unsigned int & a_Iterator ); - void AppendFloat ( float a_Float, char* a_Dst, unsigned int & a_Iterator ); - void AppendDouble ( double & a_Double, char* a_Dst, unsigned int & a_Iterator ); - void AppendByte ( char a_Byte, char* a_Dst, unsigned int & a_Iterator ); - void AppendLong ( long long & a_Long, char* a_Dst, unsigned int & a_Iterator ); - void AppendBool ( bool a_Bool, char* a_Dst, unsigned int & a_Iterator ); - void AppendData ( char* a_Data, unsigned int a_Size, char* a_Dst, unsigned int & a_Iterator ); -public: - static int SendData( cSocket & a_Socket, const char* a_Message, unsigned int a_Size, int a_Options ); - static int RecvAll( cSocket & a_Socket, char* a_Data, unsigned int a_Size, int a_Options ); + // These return the number of characters processed, PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error: + static int ReadString16(const char * a_Data, int a_Size, AString & a_OutString ); + static int ReadShort (const char * a_Data, int a_Size, short & a_Short ); + static int ReadInteger (const char * a_Data, int a_Size, int & a_OutInteger ); + static int ReadInteger (const char * a_Data, int a_Size, unsigned int & a_OutInteger ); + static int ReadFloat (const char * a_Data, int a_Size, float & a_OutFloat ); + static int ReadDouble (const char * a_Data, int a_Size, double & a_OutDouble ); + static int ReadByte (const char * a_Data, int a_Size, char & a_OutByte ); + static int ReadByte (const char * a_Data, int a_Size, unsigned char & a_OutByte ); + static int ReadLong (const char * a_Data, int a_Size, long long & a_OutLong ); + static int ReadBool (const char * a_Data, int a_Size, bool & a_OutBool ); + + // These append the data into the a_Dst string: + static void AppendString ( AString & a_Dst, const AString & a_String); + static void AppendString16( AString & a_Dst, const AString & a_String); + static void AppendShort ( AString & a_Dst, short a_Short); + static void AppendShort ( AString & a_Dst, unsigned short a_Short); + static void AppendInteger ( AString & a_Dst, int a_Integer); + static void AppendInteger ( AString & a_Dst, unsigned int a_Integer); + static void AppendFloat ( AString & a_Dst, float a_Float); + static void AppendDouble ( AString & a_Dst, const double & a_Double); + static void AppendByte ( AString & a_Dst, char a_Byte); + static void AppendLong ( AString & a_Dst, const long long & a_Long); + static void AppendBool ( AString & a_Dst, bool a_Bool); + static void AppendData ( AString & a_Dst, const char * a_Data, unsigned int a_Size); }; typedef std::list PacketList; diff --git a/source/packets/cPacket_13.cpp b/source/packets/cPacket_13.cpp index bf1b47437..aebef4410 100644 --- a/source/packets/cPacket_13.cpp +++ b/source/packets/cPacket_13.cpp @@ -7,11 +7,14 @@ -bool cPacket_13::Parse(cSocket & a_Socket) +int cPacket_13::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_EntityID, TotalBytes); + HANDLE_PACKET_READ(ReadByte , m_ActionID, TotalBytes); + return TotalBytes; +} + + + - if( !ReadInteger( m_EntityID ) ) return false; - if( !ReadByte ( m_ActionID ) ) return false; - return true; -} \ No newline at end of file diff --git a/source/packets/cPacket_13.h b/source/packets/cPacket_13.h index fb001166e..361e35fe7 100644 --- a/source/packets/cPacket_13.h +++ b/source/packets/cPacket_13.h @@ -21,10 +21,14 @@ public: { m_PacketID = E_PACKET_13; } virtual cPacket* Clone() const { return new cPacket_13( *this ); } - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; - int m_EntityID; + int m_EntityID; char m_ActionID; static const unsigned int c_Size = 1; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_AddToInventory.cpp b/source/packets/cPacket_AddToInventory.cpp index da350e9b3..c8606afe0 100644 --- a/source/packets/cPacket_AddToInventory.cpp +++ b/source/packets/cPacket_AddToInventory.cpp @@ -3,29 +3,18 @@ #include "cPacket_AddToInventory.h" #include "cPacket_WholeInventory.h" -#include "../cItem.h" #include "cPacket_ItemData.h" -bool cPacket_AddToInventory::Send( cSocket & a_Socket ) +void cPacket_AddToInventory::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; + AppendByte (a_Data, m_PacketID); + cPacket_ItemData::AppendItem(a_Data, m_ItemType, m_Count, m_Life); +} - cPacket_ItemData Item; - TotalSize += Item.GetSize((short) m_ItemType); - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char) m_PacketID, Message, i ); - - Item.AppendItem(Message, i, (short) m_ItemType, m_Count, this->m_Life); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_AddToInventory.h b/source/packets/cPacket_AddToInventory.h index 5dbfcf6a5..4317607a5 100644 --- a/source/packets/cPacket_AddToInventory.h +++ b/source/packets/cPacket_AddToInventory.h @@ -14,11 +14,13 @@ public: { m_PacketID = E_ADD_TO_INV; } virtual cPacket* Clone() const { return new cPacket_AddToInventory(*this); } - bool Parse( cSocket & a_Socket ); - bool Send( cSocket & a_Socket ); + // _X: This was unimplemented, do we need it?: + // bool Parse( cSocket & a_Socket ); + + virtual void Serialize(AString & a_Data) const override; ENUM_ITEM_ID m_ItemType; char m_Count; short m_Life; static const unsigned int c_Size = 1; -}; \ No newline at end of file +}; diff --git a/source/packets/cPacket_ArmAnim.cpp b/source/packets/cPacket_ArmAnim.cpp index 03880101e..c1766e8ce 100644 --- a/source/packets/cPacket_ArmAnim.cpp +++ b/source/packets/cPacket_ArmAnim.cpp @@ -7,25 +7,25 @@ -bool cPacket_ArmAnim::Parse( cSocket & a_Socket ) +int cPacket_ArmAnim::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if(!ReadInteger(m_EntityID) ) return false; - if(!ReadByte(m_Animation) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_EntityID, TotalBytes); + HANDLE_PACKET_READ(ReadByte , m_Animation, TotalBytes); + return TotalBytes; } -bool cPacket_ArmAnim::Send( cSocket & a_Socket ) + + + + +void cPacket_ArmAnim::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_EntityID, Message, i ); - AppendByte ( m_Animation, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_EntityID); + AppendByte (a_Data, m_Animation); +} + + + + diff --git a/source/packets/cPacket_ArmAnim.h b/source/packets/cPacket_ArmAnim.h index b8e2f2139..07753ebcb 100644 --- a/source/packets/cPacket_ArmAnim.h +++ b/source/packets/cPacket_ArmAnim.h @@ -12,8 +12,8 @@ public: { m_PacketID = E_ANIMATION; } virtual cPacket* Clone() const { return new cPacket_ArmAnim(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; int m_EntityID; char m_Animation; diff --git a/source/packets/cPacket_BlockAction.cpp b/source/packets/cPacket_BlockAction.cpp index 46faf0cc6..43a279f37 100644 --- a/source/packets/cPacket_BlockAction.cpp +++ b/source/packets/cPacket_BlockAction.cpp @@ -9,28 +9,28 @@ cPacket_BlockAction::cPacket_BlockAction( const cPacket_BlockAction & a_Copy ) { - m_PacketID = E_BLOCK_ACTION; - m_PosX = a_Copy.m_PosX; - m_PosY = a_Copy.m_PosY; - m_PosZ = a_Copy.m_PosZ; - m_Byte1 = a_Copy.m_Byte1; - m_Byte2 = a_Copy.m_Byte2; + m_PacketID = E_BLOCK_ACTION; + m_PosX = a_Copy.m_PosX; + m_PosY = a_Copy.m_PosY; + m_PosZ = a_Copy.m_PosZ; + m_Byte1 = a_Copy.m_Byte1; + m_Byte2 = a_Copy.m_Byte2; } -bool cPacket_BlockAction::Send(cSocket & a_Socket) + + + + +void cPacket_BlockAction::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_PosX, Message, i ); - AppendShort ( m_PosY, Message, i ); - AppendInteger ( m_PosZ, Message, i ); - AppendByte ( m_Byte1, Message, i ); - AppendByte ( m_Byte2, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_PosX); + AppendShort (a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_Byte1); + AppendByte (a_Data, m_Byte2); } + + + + diff --git a/source/packets/cPacket_BlockAction.h b/source/packets/cPacket_BlockAction.h index 2d05f8944..c74068401 100644 --- a/source/packets/cPacket_BlockAction.h +++ b/source/packets/cPacket_BlockAction.h @@ -1,3 +1,4 @@ + #pragma once #include "cPacket.h" @@ -16,13 +17,17 @@ public: cPacket_BlockAction( const cPacket_BlockAction & a_Copy ); virtual cPacket* Clone() const { return new cPacket_BlockAction(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - int m_PosX; // Block X Coordinate + int m_PosX; // Block X Coordinate short m_PosY; // Block Y Coordinate - int m_PosZ; // Block Z Coordinate - char m_Byte1; // Varies - char m_Byte2; // Varies + int m_PosZ; // Block Z Coordinate + char m_Byte1; // Varies + char m_Byte2; // Varies static const unsigned int c_Size = 1 + 4 + 2 + 4 + 1 + 1; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_BlockChange.cpp b/source/packets/cPacket_BlockChange.cpp index 8b758f618..57c938814 100644 --- a/source/packets/cPacket_BlockChange.cpp +++ b/source/packets/cPacket_BlockChange.cpp @@ -7,20 +7,16 @@ -bool cPacket_BlockChange::Send(cSocket & a_Socket) +void cPacket_BlockChange::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendByte ( m_PosY, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendByte ( m_BlockType, Message, i ); - AppendByte ( m_BlockMeta, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_PosX); + AppendByte (a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_BlockType); + AppendByte (a_Data, m_BlockMeta); +} + + + + diff --git a/source/packets/cPacket_BlockChange.h b/source/packets/cPacket_BlockChange.h index ca118b58d..0477dc9c9 100644 --- a/source/packets/cPacket_BlockChange.h +++ b/source/packets/cPacket_BlockChange.h @@ -15,12 +15,13 @@ public: { m_PacketID = E_BLOCK_CHANGE; } virtual cPacket* Clone() const { return new cPacket_BlockChange(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - int m_PosX; + int m_PosX; char m_PosY; - int m_PosZ; + int m_PosZ; char m_BlockType; char m_BlockMeta; + static const unsigned int c_Size = 1 + 4 + 1 + 4 + 1 + 1; }; \ No newline at end of file diff --git a/source/packets/cPacket_BlockDig.cpp b/source/packets/cPacket_BlockDig.cpp index 498023508..466e77340 100644 --- a/source/packets/cPacket_BlockDig.cpp +++ b/source/packets/cPacket_BlockDig.cpp @@ -7,30 +7,30 @@ -bool cPacket_BlockDig::Send(cSocket & a_Socket) +void cPacket_BlockDig::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendByte ( m_PosY, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendByte ( m_Direction, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_PosX); + AppendByte (a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_Direction); } -bool cPacket_BlockDig::Parse(cSocket & a_Socket) + + + + +int cPacket_BlockDig::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadByte ( m_Status ) ) return false; - if( !ReadInteger( m_PosX ) ) return false; - if( !ReadByte ( m_PosY ) ) return false; - if( !ReadInteger( m_PosZ ) ) return false; - if( !ReadByte ( m_Direction ) ) return false; - return true; -} \ No newline at end of file + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadByte, m_Status, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Direction, TotalBytes); + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_BlockDig.h b/source/packets/cPacket_BlockDig.h index 7f6e1f4d2..56ddfa9ca 100644 --- a/source/packets/cPacket_BlockDig.h +++ b/source/packets/cPacket_BlockDig.h @@ -15,13 +15,18 @@ public: { m_PacketID = E_BLOCK_DIG; } //tolua_export virtual cPacket* Clone() const { return new cPacket_BlockDig(*this); } //tolua_export - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); - - char m_Status; //tolua_export - int m_PosX; //tolua_export - char m_PosY; //tolua_export - int m_PosZ; //tolua_export - char m_Direction; //tolua_export + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; + + char m_Status; // tolua_export + int m_PosX; // tolua_export + char m_PosY; // tolua_export + int m_PosZ; // tolua_export + char m_Direction; // tolua_export + static const unsigned int c_Size = 12; -}; //tolua_export \ No newline at end of file +}; //tolua_export + + + + diff --git a/source/packets/cPacket_BlockPlace.cpp b/source/packets/cPacket_BlockPlace.cpp index 6fd8cef28..6582c7890 100644 --- a/source/packets/cPacket_BlockPlace.cpp +++ b/source/packets/cPacket_BlockPlace.cpp @@ -8,29 +8,29 @@ -bool cPacket_BlockPlace::Parse(cSocket & a_Socket) +int cPacket_BlockPlace::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadInteger( m_PosX ) ) return false; - if( !ReadByte ( m_PosY ) ) return false; - if( !ReadInteger( m_PosZ ) ) return false; - if( !ReadByte ( m_Direction ) ) return false; - - /* - if( !ReadShort ( m_ItemType ) ) return false; - if( m_ItemType > -1 ) - { - if( !ReadByte ( m_Count ) ) return false; - if( !ReadShort ( m_Uses ) ) return false; - }*/ + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Direction, TotalBytes); cPacket_ItemData Item; - - Item.Parse(m_Socket); + int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + if (res < 0) + { + return res; + } + TotalBytes += res; m_ItemType = Item.m_ItemID; - m_Count = Item.m_ItemCount; - m_Uses = Item.m_ItemUses; + m_Count = Item.m_ItemCount; + m_Uses = Item.m_ItemUses; + + return TotalBytes; +} + + + - return true; -} \ No newline at end of file diff --git a/source/packets/cPacket_BlockPlace.h b/source/packets/cPacket_BlockPlace.h index fb8907622..74bf63d28 100644 --- a/source/packets/cPacket_BlockPlace.h +++ b/source/packets/cPacket_BlockPlace.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_BlockPlace : public cPacket //tolua_export { //tolua_export public: @@ -17,16 +21,20 @@ public: { m_PacketID = E_BLOCK_PLACE; } virtual cPacket* Clone() const { return new cPacket_BlockPlace(*this); } - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; - int m_PosX; //tolua_export + int m_PosX; //tolua_export char m_PosY; //tolua_export - int m_PosZ; //tolua_export + int m_PosZ; //tolua_export char m_Direction; //tolua_export short m_ItemType; //tolua_export - char m_Count; //tolua_export + char m_Count; //tolua_export short m_Uses; //tolua_export static const unsigned int c_Size = 1 + 4 + 1 + 4 + 1 + 2;// ( + 2 ) -}; //tolua_export \ No newline at end of file +}; //tolua_export + + + + diff --git a/source/packets/cPacket_Chat.cpp b/source/packets/cPacket_Chat.cpp index 251c9d7ec..04a65d18e 100644 --- a/source/packets/cPacket_Chat.cpp +++ b/source/packets/cPacket_Chat.cpp @@ -7,23 +7,23 @@ -bool cPacket_Chat::Parse( cSocket & a_Socket ) +int cPacket_Chat::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadString16( m_Message ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadString16, m_Message, TotalBytes); + return TotalBytes; } -bool cPacket_Chat::Send( cSocket & a_Socket ) + + + + +void cPacket_Chat::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_Message.size() * sizeof( short ); - char* Message = new char[TotalSize]; + AppendByte (a_Data, m_PacketID); + AppendString16(a_Data, m_Message); +} + + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendString16 ( m_Message, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_Chat.h b/source/packets/cPacket_Chat.h index 521c21a9b..4141439d9 100644 --- a/source/packets/cPacket_Chat.h +++ b/source/packets/cPacket_Chat.h @@ -10,9 +10,13 @@ public: cPacket_Chat( const std::string & a_Message ) : m_Message( a_Message) { m_PacketID = E_CHAT; } virtual cPacket* Clone() const { return new cPacket_Chat(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; - std::string m_Message; + AString m_Message; static const unsigned int c_Size = 3; // Minimum size -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_CollectItem.cpp b/source/packets/cPacket_CollectItem.cpp index cc67cef35..466eda6b7 100644 --- a/source/packets/cPacket_CollectItem.cpp +++ b/source/packets/cPacket_CollectItem.cpp @@ -7,17 +7,13 @@ -bool cPacket_CollectItem::Send( cSocket & a_Socket ) +void cPacket_CollectItem::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_CollectedID, Message, i ); - AppendInteger( m_CollectorID, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_CollectedID); + AppendInteger(a_Data, m_CollectorID); +} + + + + diff --git a/source/packets/cPacket_CollectItem.h b/source/packets/cPacket_CollectItem.h index 390da80dc..b424ab4b7 100644 --- a/source/packets/cPacket_CollectItem.h +++ b/source/packets/cPacket_CollectItem.h @@ -3,6 +3,9 @@ #include "cPacket.h" + + + class cPacket_CollectItem : public cPacket { public: @@ -12,9 +15,13 @@ public: { m_PacketID = E_COLLECT_ITEM; } virtual cPacket* Clone() const { return new cPacket_CollectItem(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; int m_CollectedID; int m_CollectorID; static const unsigned int c_Size = 1 + 4 + 4; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_CreativeInventoryAction.cpp b/source/packets/cPacket_CreativeInventoryAction.cpp index 685d488e6..14d544877 100644 --- a/source/packets/cPacket_CreativeInventoryAction.cpp +++ b/source/packets/cPacket_CreativeInventoryAction.cpp @@ -17,45 +17,52 @@ cPacket_CreativeInventoryAction::cPacket_CreativeInventoryAction( const cPacket_ m_Damage = a_Copy.m_Damage; } -bool cPacket_CreativeInventoryAction::Parse(cSocket & a_Socket) + + + + +int cPacket_CreativeInventoryAction::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadShort ( m_Slot ) ) return false; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadShort, m_Slot, TotalBytes); cPacket_ItemData Item; - - Item.Parse(m_Socket); + int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + if (res < 0) + { + return res; + } + TotalBytes += res; m_ItemID = Item.m_ItemID; m_Quantity = Item.m_ItemCount; m_Damage = Item.m_ItemUses; - - return true; + return TotalBytes; } -bool cPacket_CreativeInventoryAction::Send(cSocket & a_Socket) -{ - //LOG("InventoryChange:"); - unsigned int TotalSize = c_Size; - cPacket_ItemData Item; - TotalSize += Item.GetSize(m_ItemID); - char* Message = new char[TotalSize]; - if( m_ItemID <= 0 ) m_ItemID = -1; // Fix, to make sure no invalid values are sent. - // WARNING: HERE ITS -1, BUT IN NAMED ENTITY SPAWN PACKET ITS 0 !! - //LOG("cPacket_CreateInventoryAction: Sending Creative item ID: %i", m_ItemID ); +void cPacket_CreativeInventoryAction::Serialize(AString & a_Data) const +{ + short ItemID = m_ItemID; + assert(ItemID >= -1); // Check validity of packets in debug runtime + if (ItemID <= 0) + { + ItemID = -1; + // Fix, to make sure no invalid values are sent. + // WARNING: HERE ITS -1, BUT IN NAMED ENTITY SPAWN PACKET ITS 0 !! + } unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendShort ( m_Slot, Message, i ); + AppendByte (a_Data, m_PacketID); + AppendShort (a_Data, m_Slot); - Item.AppendItem(Message, i, m_ItemID, m_Quantity, m_Damage); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + cPacket_ItemData::AppendItem(a_Data, ItemID, m_Quantity, m_Damage); } + + + + diff --git a/source/packets/cPacket_CreativeInventoryAction.h b/source/packets/cPacket_CreativeInventoryAction.h index 4dd029a05..4fbed4e71 100644 --- a/source/packets/cPacket_CreativeInventoryAction.h +++ b/source/packets/cPacket_CreativeInventoryAction.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + //Sure itīs not Creative Inventory? class cPacket_CreativeInventoryAction : public cPacket @@ -17,8 +21,8 @@ public: cPacket_CreativeInventoryAction( const cPacket_CreativeInventoryAction & a_Copy ); virtual cPacket* Clone() const { return new cPacket_CreativeInventoryAction(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; short m_Slot; // 0 = hold 1-4 = armor short m_ItemID; @@ -27,3 +31,7 @@ public: static const unsigned int c_Size = 1 + 2; }; + + + + diff --git a/source/packets/cPacket_DestroyEntity.cpp b/source/packets/cPacket_DestroyEntity.cpp index 6e1f8a97a..cd41b380b 100644 --- a/source/packets/cPacket_DestroyEntity.cpp +++ b/source/packets/cPacket_DestroyEntity.cpp @@ -15,16 +15,16 @@ cPacket_DestroyEntity::cPacket_DestroyEntity(cEntity* a_Entity) m_UniqueID = a_Entity->GetUniqueID(); } -bool cPacket_DestroyEntity::Send( cSocket & a_Socket ) -{ - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + + +void cPacket_DestroyEntity::Serialize(AString & a_Data) const +{ + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); } + + + + diff --git a/source/packets/cPacket_DestroyEntity.h b/source/packets/cPacket_DestroyEntity.h index 7410bd8eb..5f2314e55 100644 --- a/source/packets/cPacket_DestroyEntity.h +++ b/source/packets/cPacket_DestroyEntity.h @@ -3,7 +3,15 @@ #include "cPacket.h" + + + class cEntity; + + + + + class cPacket_DestroyEntity : public cPacket { public: @@ -13,8 +21,13 @@ public: cPacket_DestroyEntity(cEntity* a_Entity); virtual cPacket* Clone() const { return new cPacket_DestroyEntity(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; int m_UniqueID; + static const unsigned int c_Size = 1 + 4; }; + + + + diff --git a/source/packets/cPacket_Disconnect.cpp b/source/packets/cPacket_Disconnect.cpp index 2e085c516..82342c303 100644 --- a/source/packets/cPacket_Disconnect.cpp +++ b/source/packets/cPacket_Disconnect.cpp @@ -7,23 +7,23 @@ -bool cPacket_Disconnect::Parse( cSocket & a_Socket ) +int cPacket_Disconnect::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadString16(m_Reason) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadString16, m_Reason, TotalBytes); + return TotalBytes; } -bool cPacket_Disconnect::Send( cSocket & a_Socket ) -{ - unsigned int TotalSize = c_Size + m_Reason.size()*sizeof(short); - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendString16 ( m_Reason, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + + +void cPacket_Disconnect::Serialize(AString & a_Data) const +{ + AppendByte (a_Data, m_PacketID); + AppendString16(a_Data, m_Reason); } + + + + diff --git a/source/packets/cPacket_Disconnect.h b/source/packets/cPacket_Disconnect.h index 7dc5ef9a9..ccb3811b5 100644 --- a/source/packets/cPacket_Disconnect.h +++ b/source/packets/cPacket_Disconnect.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Disconnect : public cPacket { public: @@ -10,9 +14,13 @@ public: cPacket_Disconnect(const AString & a_Reason) { m_PacketID = E_DISCONNECT; m_Reason = a_Reason; } virtual cPacket* Clone() const { return new cPacket_Disconnect(*this); } - bool Parse( cSocket & a_Socket ); - bool Send( cSocket & a_Socket ); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; AString m_Reason; static const unsigned int c_Size = 3; // Minimum size -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_EntityEquipment.cpp b/source/packets/cPacket_EntityEquipment.cpp index b3030ac74..a89cb9724 100644 --- a/source/packets/cPacket_EntityEquipment.cpp +++ b/source/packets/cPacket_EntityEquipment.cpp @@ -16,34 +16,33 @@ cPacket_EntityEquipment::cPacket_EntityEquipment( const cPacket_EntityEquipment m_Short = 0; } -bool cPacket_EntityEquipment::Parse(cSocket & a_Socket) + + + + +int cPacket_EntityEquipment::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadInteger( m_UniqueID ) ) return false; - if( !ReadShort ( m_Slot ) ) return false; - if( !ReadShort ( m_ItemID ) ) return false; - if( !ReadShort ( m_Short ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_Slot, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_ItemID, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_Short, TotalBytes); + return TotalBytes; } -bool cPacket_EntityEquipment::Send(cSocket & a_Socket) + + + + +void cPacket_EntityEquipment::Serialize(AString & a_Data) const { - //LOG("InventoryChange:"); - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - if( m_ItemID <= 0 ) m_ItemID = -1; // Fix, to make sure no invalid values are sent. - // WARNING: HERE ITS -1, BUT IN NAMED ENTITY SPAWN PACKET ITS 0 !! - //LOG("cPacket_EntityEquipment: Sending equipped item ID: %i", m_ItemID ); - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendShort ( m_Slot, Message, i ); - AppendShort ( m_ItemID, Message, i ); - AppendShort ( m_Short, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendShort (a_Data, m_Slot); + AppendShort (a_Data, m_ItemID); + AppendShort (a_Data, m_Short); +} + + + + diff --git a/source/packets/cPacket_EntityEquipment.h b/source/packets/cPacket_EntityEquipment.h index 1358db7ee..4beda6063 100644 --- a/source/packets/cPacket_EntityEquipment.h +++ b/source/packets/cPacket_EntityEquipment.h @@ -3,6 +3,9 @@ #include "cPacket.h" + + + class cPacket_EntityEquipment : public cPacket { public: @@ -15,8 +18,8 @@ public: cPacket_EntityEquipment( const cPacket_EntityEquipment & a_Copy ); virtual cPacket* Clone() const { return new cPacket_EntityEquipment(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; int m_UniqueID; short m_Slot; // 0 = hold 1-4 = armor @@ -24,4 +27,8 @@ public: short m_Short; static const unsigned int c_Size = 1 + 4 + 2 + 2 + 2; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_EntityLook.cpp b/source/packets/cPacket_EntityLook.cpp index 7cca43210..3e0f2bd65 100644 --- a/source/packets/cPacket_EntityLook.cpp +++ b/source/packets/cPacket_EntityLook.cpp @@ -14,22 +14,22 @@ cPacket_EntityLook::cPacket_EntityLook(cEntity* a_Entity) m_PacketID = E_ENT_LOOK; m_UniqueID = a_Entity->GetUniqueID(); - m_Rotation = (char)((a_Entity->GetRotation()/360.f)*256); - m_Pitch = (char)((a_Entity->GetPitch()/360.f)*256); + m_Rotation = (char)((a_Entity->GetRotation() / 360.f) * 256); + m_Pitch = (char)((a_Entity->GetPitch() / 360.f) * 256); } -bool cPacket_EntityLook::Send( cSocket & a_Socket ) + + + + +void cPacket_EntityLook::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendByte ( m_Rotation, Message, i ); - AppendByte ( m_Pitch, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendByte (a_Data, m_Rotation); + AppendByte (a_Data, m_Pitch); } + + + + diff --git a/source/packets/cPacket_EntityLook.h b/source/packets/cPacket_EntityLook.h index e1e7c1a93..407f9c1da 100644 --- a/source/packets/cPacket_EntityLook.h +++ b/source/packets/cPacket_EntityLook.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cEntity; class cPacket_EntityLook : public cPacket { @@ -15,7 +19,7 @@ public: cPacket_EntityLook(cEntity* a_Entity); virtual cPacket* Clone() const { return new cPacket_EntityLook(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; int m_UniqueID; char m_Rotation; @@ -23,3 +27,7 @@ public: static const unsigned int c_Size = 1 + 4 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_EntityStatus.cpp b/source/packets/cPacket_EntityStatus.cpp index aafea5c12..0fc6195b3 100644 --- a/source/packets/cPacket_EntityStatus.cpp +++ b/source/packets/cPacket_EntityStatus.cpp @@ -7,18 +7,13 @@ -bool cPacket_EntityStatus::Send(cSocket & a_Socket) +void cPacket_EntityStatus::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendByte (a_Data, m_Status); +} + - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_UniqueID, Message, i ); - AppendByte ( m_Status, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_EntityStatus.h b/source/packets/cPacket_EntityStatus.h index 53fced00a..a50a25390 100644 --- a/source/packets/cPacket_EntityStatus.h +++ b/source/packets/cPacket_EntityStatus.h @@ -3,6 +3,9 @@ #include "cPacket.h" + + + class cPacket_EntityStatus : public cPacket { public: @@ -12,7 +15,7 @@ public: { m_PacketID = E_ENT_STATUS; } virtual cPacket* Clone() const { return new cPacket_EntityStatus( *this ); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; static const char STATUS_TAKEDAMAGE = 2; static const char STATUS_DIE = 3; @@ -22,3 +25,7 @@ public: static const unsigned int c_Size = 1 + 4 + 1; }; + + + + diff --git a/source/packets/cPacket_Explosion.cpp b/source/packets/cPacket_Explosion.cpp index d2baba876..d4a244c01 100644 --- a/source/packets/cPacket_Explosion.cpp +++ b/source/packets/cPacket_Explosion.cpp @@ -15,35 +15,38 @@ cPacket_Explosion::~cPacket_Explosion() } } + + + + cPacket_Explosion::cPacket_Explosion( const cPacket_Explosion & a_Copy ) { - m_PacketID = E_EXPLOSION; - m_PosX = a_Copy.m_PosX; - m_PosY = a_Copy.m_PosY; - m_PosZ = a_Copy.m_PosZ; - m_Radius = a_Copy.m_Radius; //might not be radius - m_RecordsCount= a_Copy.m_RecordsCount; - - m_Records = new char[(m_RecordsCount * 3)]; - memcpy( m_Records, a_Copy.m_Records, (m_RecordsCount * 3) ); - + m_PacketID = E_EXPLOSION; + m_PosX = a_Copy.m_PosX; + m_PosY = a_Copy.m_PosY; + m_PosZ = a_Copy.m_PosZ; + m_Radius = a_Copy.m_Radius; //might not be radius + m_RecordsCount= a_Copy.m_RecordsCount; + + m_Records = new char[(m_RecordsCount * 3)]; + memcpy( m_Records, a_Copy.m_Records, (m_RecordsCount * 3) ); } -bool cPacket_Explosion::Send(cSocket & a_Socket) + + + + +void cPacket_Explosion::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + (m_RecordsCount * 3); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendDouble ( m_PosX, Message, i ); - AppendDouble ( m_PosY, Message, i ); - AppendDouble ( m_PosZ, Message, i ); - AppendFloat ( m_Radius, Message, i ); - AppendInteger ( m_RecordsCount, Message, i ); - AppendData ( m_Records, (m_RecordsCount * 3),Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendDouble (a_Data, m_PosX); + AppendDouble (a_Data, m_PosY); + AppendDouble (a_Data, m_PosZ); + AppendFloat (a_Data, m_Radius); + AppendInteger(a_Data, m_RecordsCount); + AppendData (a_Data, m_Records, (m_RecordsCount * 3)); } + + + + diff --git a/source/packets/cPacket_Explosion.h b/source/packets/cPacket_Explosion.h index c37783115..5121c9175 100644 --- a/source/packets/cPacket_Explosion.h +++ b/source/packets/cPacket_Explosion.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Explosion : public cPacket { public: @@ -18,7 +22,7 @@ public: ~cPacket_Explosion(); virtual cPacket* Clone() const { return new cPacket_Explosion(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; double m_PosX; // The entity ID of the thunderbolt double m_PosY; // Always true. Might have a meaning in the future... @@ -30,3 +34,7 @@ public: char* m_Records; }; + + + + diff --git a/source/packets/cPacket_Flying.cpp b/source/packets/cPacket_Flying.cpp index 44f30bbe3..c7916957a 100644 --- a/source/packets/cPacket_Flying.cpp +++ b/source/packets/cPacket_Flying.cpp @@ -7,9 +7,13 @@ -bool cPacket_Flying::Parse(cSocket & a_Socket) +int cPacket_Flying::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadBool( m_bFlying ) ) return false; - return true; -} \ No newline at end of file + int TotalBytes= 0; + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_Flying.h b/source/packets/cPacket_Flying.h index 63395204e..19b0ee213 100644 --- a/source/packets/cPacket_Flying.h +++ b/source/packets/cPacket_Flying.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Flying : public cPacket { public: @@ -12,8 +16,12 @@ public: { m_PacketID = E_FLYING; } virtual cPacket* Clone() const { return new cPacket_Flying(*this); } - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; bool m_bFlying; static const unsigned int c_Size = 2; }; + + + + diff --git a/source/packets/cPacket_Handshake.cpp b/source/packets/cPacket_Handshake.cpp index 482f25d26..2c4f058e7 100644 --- a/source/packets/cPacket_Handshake.cpp +++ b/source/packets/cPacket_Handshake.cpp @@ -7,25 +7,23 @@ -bool cPacket_Handshake::Parse(cSocket & a_Socket) +int cPacket_Handshake::Parse(const char * a_Data, int a_Size) { - //printf("Parse: NEW Handshake\n"); - m_Socket = a_Socket; - if( !ReadString16( m_Username ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadString16, m_Username, TotalBytes); + return TotalBytes; } -bool cPacket_Handshake::Send(cSocket & a_Socket) + + + + +void cPacket_Handshake::Serialize(AString & a_Data) const { - //LOG("Send: NEW Handshake %s", m_Username.c_str() ); - unsigned int TotalSize = c_Size + m_Username.size() * sizeof(short); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendString16( m_Username, Message, i ); - - bool RetVal = !cSocket::IsSocketError( cPacket::SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendString16(a_Data, m_Username); +} + + + + diff --git a/source/packets/cPacket_Handshake.h b/source/packets/cPacket_Handshake.h index f9fd7c367..e66827851 100644 --- a/source/packets/cPacket_Handshake.h +++ b/source/packets/cPacket_Handshake.h @@ -1,17 +1,25 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Handshake : public cPacket { public: cPacket_Handshake() { m_PacketID = E_HANDSHAKE; } virtual cPacket* Clone() const { return new cPacket_Handshake(*this); } - virtual bool Parse(cSocket & a_Socket); - virtual bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; std::string m_Username; static const unsigned int c_Size = 3; // Minimal size -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_InventoryProgressBar.cpp b/source/packets/cPacket_InventoryProgressBar.cpp index 4f00ef80a..9df92b390 100644 --- a/source/packets/cPacket_InventoryProgressBar.cpp +++ b/source/packets/cPacket_InventoryProgressBar.cpp @@ -7,19 +7,14 @@ -bool cPacket_InventoryProgressBar::Send(cSocket & a_Socket) +void cPacket_InventoryProgressBar::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; + AppendByte (a_Data, m_PacketID); + AppendByte (a_Data, m_WindowID); + AppendShort(a_Data, m_ProgressBar); + AppendShort(a_Data, m_Value); +} + - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_WindowID, Message, i ); - AppendShort ( m_ProgressBar, Message, i ); - AppendShort ( m_Value, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_InventoryProgressBar.h b/source/packets/cPacket_InventoryProgressBar.h index 401ec1f6e..70bafcf9e 100644 --- a/source/packets/cPacket_InventoryProgressBar.h +++ b/source/packets/cPacket_InventoryProgressBar.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_InventoryProgressBar : public cPacket { public: @@ -13,11 +17,15 @@ public: { m_PacketID = E_INVENTORY_PROGRESS; } virtual cPacket* Clone() const { return new cPacket_InventoryProgressBar(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; char m_WindowID; short m_ProgressBar; short m_Value; static const unsigned int c_Size = 1 + 1 + 2 + 2; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_InventorySlot.cpp b/source/packets/cPacket_InventorySlot.cpp index f28c39aa7..46f3bf3d4 100644 --- a/source/packets/cPacket_InventorySlot.cpp +++ b/source/packets/cPacket_InventorySlot.cpp @@ -9,26 +9,15 @@ -bool cPacket_InventorySlot::Send(cSocket & a_Socket) +void cPacket_InventorySlot::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; + AppendByte (a_Data, m_PacketID); + AppendByte (a_Data, m_WindowID); + AppendShort(a_Data, m_SlotNum); - cPacket_ItemData Item; + cPacket_ItemData::AppendItem(a_Data, m_ItemID, m_ItemCount, m_ItemUses); +} - TotalSize += Item.GetSize(m_ItemID); - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_WindowID, Message, i ); - AppendShort ( m_SlotNum, Message, i ); - - Item.AppendItem(Message, i, m_ItemID, m_ItemCount, m_ItemUses); - - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_InventorySlot.h b/source/packets/cPacket_InventorySlot.h index b02444593..cc04a0bb0 100644 --- a/source/packets/cPacket_InventorySlot.h +++ b/source/packets/cPacket_InventorySlot.h @@ -1,9 +1,14 @@ + #pragma once #include "cPacket.h" #include "../BlockID.h" + + + + class cPacket_InventorySlot : public cPacket // Set item [S -> C] ? { public: @@ -16,7 +21,7 @@ public: { m_PacketID = E_INVENTORY_SLOT; } virtual cPacket* Clone() const { return new cPacket_InventorySlot(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; char m_WindowID; short m_SlotNum; // Slot @@ -32,4 +37,8 @@ public: short m_ItemUses; static const unsigned int c_Size = 1 + 1 + 2; // Minimal size ( +1+1 = max) -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_ItemData.cpp b/source/packets/cPacket_ItemData.cpp index 851323ee4..db025a838 100644 --- a/source/packets/cPacket_ItemData.cpp +++ b/source/packets/cPacket_ItemData.cpp @@ -7,36 +7,37 @@ -bool cPacket_ItemData::Parse(cSocket & a_Socket) +int cPacket_ItemData::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadShort, m_ItemID, TotalBytes); - if( !ReadShort(m_ItemID) ) return false; - - if( m_ItemID > -1 ) + if (m_ItemID <= -1) { - if( !ReadByte(m_ItemCount) ) return false; - if( !ReadShort(m_ItemUses) ) return false; + m_ItemCount = 0; + m_ItemUses = 0; + return TotalBytes; + } - if(cItem::IsEnchantable((ENUM_ITEM_ID) m_ItemID)) - { - if( !ReadShort(m_EnchantNums) ) return false; - if( m_EnchantNums > -1 ) - { - //TODO Not implemented yet! - } - } + HANDLE_PACKET_READ(ReadByte , m_ItemCount, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_ItemUses, TotalBytes); - } - else + if (cItem::IsEnchantable((ENUM_ITEM_ID) m_ItemID)) { - m_ItemCount = 0; - m_ItemUses = 0; + HANDLE_PACKET_READ(ReadShort, m_EnchantNums, TotalBytes); + + if ( m_EnchantNums > -1 ) + { + // TODO: Enchantment not implemented yet! + } } - - return true; + return TotalBytes; } + + + + int cPacket_ItemData::GetSize(short a_ItemID) { if(a_ItemID <= -1) @@ -47,20 +48,34 @@ int cPacket_ItemData::GetSize(short a_ItemID) } -void cPacket_ItemData::AppendItem(char* a_Message, unsigned int &a_Iterator, cItem *a_Item) + + + +void cPacket_ItemData::AppendItem(AString & a_Data, const cItem * a_Item) { - return AppendItem(a_Message, a_Iterator, (short) a_Item->m_ItemID, a_Item->m_ItemCount, a_Item->m_ItemHealth); + return AppendItem(a_Data, a_Item->m_ItemID, a_Item->m_ItemCount, a_Item->m_ItemHealth); } -void cPacket_ItemData::AppendItem(char* a_Message, unsigned int &a_Iterator, short a_ItemID, char a_Quantity, short a_Damage) + + + + +void cPacket_ItemData::AppendItem(AString & a_Data, short a_ItemID, char a_Quantity, short a_Damage) { - - AppendShort ( (short) a_ItemID, a_Message, a_Iterator ); - if(a_ItemID > -1) + AppendShort(a_Data, (short) a_ItemID); + if (a_ItemID > -1) { - AppendByte ( a_Quantity, a_Message, a_Iterator ); - AppendShort ( a_Damage, a_Message, a_Iterator ); - if(cItem::IsEnchantable((ENUM_ITEM_ID) a_ItemID)) - AppendShort ( (short) -1, a_Message, a_Iterator ); + AppendByte (a_Data, a_Quantity); + AppendShort(a_Data, a_Damage); + + if (cItem::IsEnchantable((ENUM_ITEM_ID) a_ItemID)) + { + // TODO: Implement enchantments + AppendShort(a_Data, (short) -1); + } } -} \ No newline at end of file +} + + + + diff --git a/source/packets/cPacket_ItemData.h b/source/packets/cPacket_ItemData.h index 26f07b54d..6aa1eed69 100644 --- a/source/packets/cPacket_ItemData.h +++ b/source/packets/cPacket_ItemData.h @@ -15,17 +15,15 @@ public: { } - bool Parse(cSocket & a_Socket); - virtual cPacket* Clone() const { return new cPacket_ItemData(*this); } - void AppendItem(char* a_Message, unsigned int &a_Iterator, short a_ItemID, char a_Quantity, short a_Damage); + virtual int Parse(const char * a_Data, int a_Size) override; - void AppendItem(char* a_Message, unsigned int &a_Iterator, cItem *a_Item); + static void AppendItem(AString & a_Data, short a_ItemID, char a_Quantity, short a_Damage); + static void AppendItem(AString & a_Data, const cItem * a_Item); int GetSize(short a_ItemID); - // Below = item short m_ItemID; // if this is -1 the next stuff dont exist char m_ItemCount; diff --git a/source/packets/cPacket_ItemSwitch.cpp b/source/packets/cPacket_ItemSwitch.cpp index 517e1de4b..630c2ada4 100644 --- a/source/packets/cPacket_ItemSwitch.cpp +++ b/source/packets/cPacket_ItemSwitch.cpp @@ -7,24 +7,23 @@ -bool cPacket_ItemSwitch::Parse( cSocket & a_Socket ) +int cPacket_ItemSwitch::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - - if( !ReadShort ( m_SlotNum ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadShort, m_SlotNum, TotalBytes); + return TotalBytes; } -bool cPacket_ItemSwitch::Send( cSocket & a_Socket ) + + + + +void cPacket_ItemSwitch::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; + AppendByte (a_Data, m_PacketID); + AppendShort(a_Data, m_SlotNum); +} + + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendShort ( m_SlotNum, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_ItemSwitch.h b/source/packets/cPacket_ItemSwitch.h index c1342c0dd..00e37b9c8 100644 --- a/source/packets/cPacket_ItemSwitch.h +++ b/source/packets/cPacket_ItemSwitch.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_ItemSwitch : public cPacket { public: @@ -11,9 +15,14 @@ public: { m_PacketID = E_ITEM_SWITCH; } virtual cPacket* Clone() const { return new cPacket_ItemSwitch(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; short m_SlotNum; + static const unsigned int c_Size = 1 + 2; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_KeepAlive.cpp b/source/packets/cPacket_KeepAlive.cpp index 11c4efa11..cf251bf16 100644 --- a/source/packets/cPacket_KeepAlive.cpp +++ b/source/packets/cPacket_KeepAlive.cpp @@ -7,23 +7,23 @@ -bool cPacket_KeepAlive::Send(cSocket & a_Socket) +void cPacket_KeepAlive::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_KeepAliveID); +} + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_KeepAliveID, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} -bool cPacket_KeepAlive::Parse(cSocket & a_Socket) + +int cPacket_KeepAlive::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadInteger( m_KeepAliveID ) ) return false; - return true; -} \ No newline at end of file + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_KeepAliveID, TotalBytes); + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_KeepAlive.h b/source/packets/cPacket_KeepAlive.h index 5ef56c20b..3611b6e18 100644 --- a/source/packets/cPacket_KeepAlive.h +++ b/source/packets/cPacket_KeepAlive.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_KeepAlive : public cPacket { public: @@ -10,10 +14,14 @@ public: cPacket_KeepAlive(int a_PingID) { m_KeepAliveID = a_PingID; } virtual cPacket* Clone() const { return new cPacket_KeepAlive(*this); } - virtual bool Parse(cSocket & a_Socket); - virtual bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; int m_KeepAliveID; static const unsigned int c_Size = 1 + 4; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_Login.cpp b/source/packets/cPacket_Login.cpp index 60feb215a..a96d456cc 100644 --- a/source/packets/cPacket_Login.cpp +++ b/source/packets/cPacket_Login.cpp @@ -10,44 +10,45 @@ const std::string cPacket_Login::LEVEL_TYPE_DEFAULT = "DEFAULT"; const std::string cPacket_Login::LEVEL_TYPE_SUPERFLAT = "SUPERFLAT"; -bool cPacket_Login::Parse( cSocket & a_Socket ) + + + + +int cPacket_Login::Parse(const char * a_Data, int a_Size) { //printf("Parse: NEW Login\n"); - m_Socket = a_Socket; - + int TotalBytes = 0; m_Username.clear(); - - if( !ReadInteger( m_ProtocolVersion ) ) return false; - if( !ReadString16( m_Username ) ) return false; - if( !ReadLong ( m_MapSeed ) ) return false; - if( !ReadString16( m_LevelType ) ) return false; - if( !ReadInteger( m_ServerMode ) ) return false; - if( !ReadByte ( m_Dimension ) ) return false; - if( !ReadByte ( m_Difficulty ) ) return false; - if( !ReadByte ( m_WorldHeight ) ) return false; - if( !ReadByte ( m_MaxPlayers ) ) return false; - return true; + HANDLE_PACKET_READ(ReadInteger, m_ProtocolVersion, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_Username, TotalBytes); + HANDLE_PACKET_READ(ReadLong, m_MapSeed, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_LevelType, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_ServerMode, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Dimension, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Difficulty, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_WorldHeight, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_MaxPlayers, TotalBytes); + return TotalBytes; } -bool cPacket_Login::Send( cSocket & a_Socket ) + + + + +void cPacket_Login::Serialize(AString & a_Data) const { - //printf("Send: NEW Login\n"); - unsigned int TotalSize = c_Size + m_Username.size() * sizeof(short) + m_LevelType.size() * sizeof(short); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_ProtocolVersion, Message, i ); - AppendString16 ( m_Username, Message, i ); - AppendLong ( m_MapSeed, Message, i ); - AppendString16( m_LevelType, Message, i ); - AppendInteger( m_ServerMode, Message, i ); - AppendByte ( m_Dimension, Message, i ); - AppendByte ( m_Difficulty, Message, i ); - AppendByte ( m_WorldHeight, Message, i ); - AppendByte ( m_MaxPlayers, Message, i ); - - bool RetVal = !cSocket::IsSocketError( cPacket::SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger (a_Data, m_ProtocolVersion); + AppendString16(a_Data, m_Username); + AppendLong (a_Data, m_MapSeed); + AppendString16(a_Data, m_LevelType); + AppendInteger (a_Data, m_ServerMode); + AppendByte (a_Data, m_Dimension); + AppendByte (a_Data, m_Difficulty); + AppendByte (a_Data, m_WorldHeight); + AppendByte (a_Data, m_MaxPlayers); +} + + + + diff --git a/source/packets/cPacket_Login.h b/source/packets/cPacket_Login.h index c92c22838..364a268eb 100644 --- a/source/packets/cPacket_Login.h +++ b/source/packets/cPacket_Login.h @@ -1,7 +1,10 @@ + #pragma once #include "cPacket.h" -#include + + + class cPacket_Login : public cPacket //tolua_export @@ -19,21 +22,25 @@ public: { m_PacketID = E_LOGIN; } virtual cPacket* Clone() const { return new cPacket_Login(*this); } - virtual bool Parse(cSocket & a_Socket); - virtual bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; int m_ProtocolVersion; //tolua_export - std::string m_Username; //tolua_export + AString m_Username; //tolua_export long long m_MapSeed; //tolua_export - std::string m_LevelType; //tolua_export + AString m_LevelType; //tolua_export int m_ServerMode; //tolua_export char m_Dimension; //tolua_export char m_Difficulty; //tolua_export unsigned char m_WorldHeight; //tolua_export unsigned char m_MaxPlayers; //tolua_export + static const unsigned int c_Size = 1 + 4 + 2 + 8 + 2 + 4 + 1 + 1 + 1 + 1; // Minimal size - - static const std::string LEVEL_TYPE_DEFAULT; - static const std::string LEVEL_TYPE_SUPERFLAT; + static const AString LEVEL_TYPE_DEFAULT; + static const AString LEVEL_TYPE_SUPERFLAT; }; //tolua_export + + + + diff --git a/source/packets/cPacket_MapChunk.cpp b/source/packets/cPacket_MapChunk.cpp index 6bc764e09..a9448f050 100644 --- a/source/packets/cPacket_MapChunk.cpp +++ b/source/packets/cPacket_MapChunk.cpp @@ -12,12 +12,13 @@ cPacket_MapChunk::~cPacket_MapChunk() { - if( m_CompressedData ) - { - delete [] m_CompressedData; - } + delete [] m_CompressedData; } + + + + cPacket_MapChunk::cPacket_MapChunk(cChunk* a_Chunk) { m_PacketID = E_MAP_CHUNK; @@ -31,7 +32,7 @@ cPacket_MapChunk::cPacket_MapChunk(cChunk* a_Chunk) m_SizeZ = 15; uLongf CompressedSize = compressBound( cChunk::c_BlockDataSize ); - char* CompressedBlockData = new char[CompressedSize]; + char * CompressedBlockData = new char[CompressedSize]; compress2( (Bytef*)CompressedBlockData, &CompressedSize, (const Bytef*)a_Chunk->pGetBlockData(), cChunk::c_BlockDataSize, Z_DEFAULT_COMPRESSION); @@ -39,6 +40,10 @@ cPacket_MapChunk::cPacket_MapChunk(cChunk* a_Chunk) m_CompressedSize = CompressedSize; } + + + + cPacket_MapChunk::cPacket_MapChunk( const cPacket_MapChunk & a_Copy ) { m_PacketID = E_MAP_CHUNK; @@ -54,23 +59,23 @@ cPacket_MapChunk::cPacket_MapChunk( const cPacket_MapChunk & a_Copy ) memcpy( m_CompressedData, a_Copy.m_CompressedData, m_CompressedSize ); } -bool cPacket_MapChunk::Send(cSocket & a_Socket) + + + + +void cPacket_MapChunk::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_CompressedSize; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_PosX, Message, i ); - AppendShort ( m_PosY, Message, i ); - AppendInteger ( m_PosZ, Message, i ); - AppendByte ( m_SizeX, Message, i ); - AppendByte ( m_SizeY, Message, i ); - AppendByte ( m_SizeZ, Message, i ); - AppendInteger ( m_CompressedSize, Message, i ); - AppendData ( m_CompressedData, m_CompressedSize, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_PosX); + AppendShort (a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_SizeX); + AppendByte (a_Data, m_SizeY); + AppendByte (a_Data, m_SizeZ); + AppendInteger(a_Data, m_CompressedSize); + AppendData (a_Data, m_CompressedData, m_CompressedSize); +} + + + + diff --git a/source/packets/cPacket_MapChunk.h b/source/packets/cPacket_MapChunk.h index 9c5a6b58f..b4994f12b 100644 --- a/source/packets/cPacket_MapChunk.h +++ b/source/packets/cPacket_MapChunk.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cChunk; class cPacket_MapChunk : public cPacket { @@ -22,7 +26,7 @@ public: ~cPacket_MapChunk(); virtual cPacket* Clone() const { return new cPacket_MapChunk(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; int m_PosX; // In block coordinates short m_PosY; @@ -33,5 +37,9 @@ public: int m_CompressedSize; static const unsigned int c_Size = 1 + 4 + 2 + 4 + 1 + 1 + 1 + 4; - char* m_CompressedData; -}; \ No newline at end of file + char * m_CompressedData; +}; + + + + diff --git a/source/packets/cPacket_Metadata.cpp b/source/packets/cPacket_Metadata.cpp index e130f86fc..9d2d61704 100644 --- a/source/packets/cPacket_Metadata.cpp +++ b/source/packets/cPacket_Metadata.cpp @@ -17,6 +17,10 @@ cPacket_Metadata::cPacket_Metadata(int s, int id) FormPacket(); } + + + + cPacket_Metadata::cPacket_Metadata() : m_UniqueID( 0 ) , m_Type( 0 ) @@ -27,19 +31,30 @@ cPacket_Metadata::cPacket_Metadata() FormPacket(); } -cPacket_Metadata::~cPacket_Metadata() { - //if( m_MetaData ) delete [] m_MetaData; + + + + +cPacket_Metadata::~cPacket_Metadata() +{ + delete [] m_MetaData; } -void cPacket_Metadata::FormPacket() { - if( m_MetaData ) delete [] m_MetaData; + + + + +void cPacket_Metadata::FormPacket() +{ + delete [] m_MetaData; m_MetaData = new char[3]; m_MetaDataSize = 3; - //m_UniqueID = GetUniqueID(); + // m_UniqueID = GetUniqueID(); m_MetaData[0] = 0x00; m_MetaData[2] = 0x7f; - switch(m_EMetaData) { + switch(m_EMetaData) + { case cPawn::NORMAL: m_MetaData[1] = 0x00; break; @@ -65,15 +80,17 @@ void cPacket_Metadata::FormPacket() { } } -bool cPacket_Metadata::Send(cSocket & a_Socket) { - unsigned int TotalSize = c_Size + m_MetaDataSize; - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_UniqueID, Message, i ); - AppendData ( m_MetaData, m_MetaDataSize, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - return RetVal; + + +void cPacket_Metadata::Serialize(AString & a_Data) const +{ + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendData (a_Data, m_MetaData, m_MetaDataSize); } + + + + diff --git a/source/packets/cPacket_Metadata.h b/source/packets/cPacket_Metadata.h index 23d16f7df..4900bad0a 100644 --- a/source/packets/cPacket_Metadata.h +++ b/source/packets/cPacket_Metadata.h @@ -1,8 +1,13 @@ + #pragma once #include "cPacket.h" #include "../cPawn.h" + + + + class cPacket_Metadata : public cPacket { public: @@ -10,7 +15,8 @@ public: cPacket_Metadata(); ~cPacket_Metadata(); - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; + void FormPacket(); virtual cPacket* Clone() const { return new cPacket_Metadata( *this ); } @@ -22,3 +28,7 @@ public: unsigned int m_MetaDataSize; char* m_MetaData; }; + + + + diff --git a/source/packets/cPacket_MultiBlock.cpp b/source/packets/cPacket_MultiBlock.cpp index 392bc7fef..dbdc11c02 100644 --- a/source/packets/cPacket_MultiBlock.cpp +++ b/source/packets/cPacket_MultiBlock.cpp @@ -21,6 +21,10 @@ cPacket_MultiBlock::cPacket_MultiBlock( const cPacket_MultiBlock & a_Copy ) memcpy( m_BlockMetas, a_Copy.m_BlockMetas, m_NumBlocks ); } + + + + cPacket_MultiBlock::~cPacket_MultiBlock() { if( m_BlockCoordinates ) delete [] m_BlockCoordinates; @@ -28,22 +32,22 @@ cPacket_MultiBlock::~cPacket_MultiBlock() if( m_BlockMetas ) delete [] m_BlockMetas; } -bool cPacket_MultiBlock::Send(cSocket & a_Socket) + + + + +void cPacket_MultiBlock::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_NumBlocks * ( sizeof(short) + 2*sizeof(char) ); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_ChunkX, Message, i ); - AppendInteger ( m_ChunkZ, Message, i ); - AppendShort ( m_NumBlocks, Message, i ); - - AppendData ( (char*)m_BlockCoordinates,sizeof(short)*m_NumBlocks, Message, i ); - AppendData ( m_BlockTypes, sizeof(char)*m_NumBlocks, Message, i ); - AppendData ( m_BlockMetas, sizeof(char)*m_NumBlocks, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_ChunkX); + AppendInteger(a_Data, m_ChunkZ); + AppendShort (a_Data, m_NumBlocks); + + AppendData (a_Data, (char *)m_BlockCoordinates, sizeof(short) * m_NumBlocks); + AppendData (a_Data, m_BlockTypes, m_NumBlocks); + AppendData (a_Data, m_BlockMetas, m_NumBlocks); +} + + + + diff --git a/source/packets/cPacket_MultiBlock.h b/source/packets/cPacket_MultiBlock.h index 9dbebe3d2..aeabbbfed 100644 --- a/source/packets/cPacket_MultiBlock.h +++ b/source/packets/cPacket_MultiBlock.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_MultiBlock : public cPacket { public: @@ -18,7 +22,7 @@ public: ~cPacket_MultiBlock(); virtual cPacket* Clone() const { return new cPacket_MultiBlock(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; int m_ChunkX; int m_ChunkZ; @@ -29,4 +33,8 @@ public: unsigned short* m_BlockCoordinates; // x<<12 | z<<8 | y char* m_BlockTypes; char* m_BlockMetas; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_NamedEntitySpawn.cpp b/source/packets/cPacket_NamedEntitySpawn.cpp index 8718bb206..9a8fcfd14 100644 --- a/source/packets/cPacket_NamedEntitySpawn.cpp +++ b/source/packets/cPacket_NamedEntitySpawn.cpp @@ -7,26 +7,28 @@ -bool cPacket_NamedEntitySpawn::Send( cSocket & a_Socket ) +void cPacket_NamedEntitySpawn::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_PlayerName.size() * sizeof( short ); - char* Message = new char[TotalSize]; - - if( m_CurrentItem <= 0 ) m_CurrentItem = 0; // Fix, to make sure no invalid values are sent. - // WARNING: HERE ITS 0, BUT IN EQUIP PACKET ITS -1 !! - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendString16( m_PlayerName, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendInteger( m_PosY, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendByte ( m_Rotation, Message, i ); - AppendByte ( m_Pitch, Message, i ); - AppendShort ( m_CurrentItem, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + short CurrentItem = m_CurrentItem; + assert(CurrentItem > 0); + if (CurrentItem <= 0) + { + CurrentItem = 0; + // Fix, to make sure no invalid values are sent. + // WARNING: HERE ITS 0, BUT IN EQUIP PACKET ITS -1 !! + } + + AppendByte (a_Data, m_PacketID); + AppendInteger (a_Data, m_UniqueID); + AppendString16(a_Data, m_PlayerName); + AppendInteger (a_Data, m_PosX); + AppendInteger (a_Data, m_PosY); + AppendInteger (a_Data, m_PosZ); + AppendByte (a_Data, m_Rotation); + AppendByte (a_Data, m_Pitch); + AppendShort (a_Data, CurrentItem); } + + + + diff --git a/source/packets/cPacket_NamedEntitySpawn.h b/source/packets/cPacket_NamedEntitySpawn.h index 4cd690bfe..ceec10c35 100644 --- a/source/packets/cPacket_NamedEntitySpawn.h +++ b/source/packets/cPacket_NamedEntitySpawn.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_NamedEntitySpawn : public cPacket { public: @@ -17,10 +21,10 @@ public: { m_PacketID = E_NAMED_ENTITY_SPAWN; } virtual cPacket* Clone() const { return new cPacket_NamedEntitySpawn(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; int m_UniqueID; - std::string m_PlayerName; + AString m_PlayerName; int m_PosX; // Pixel position, devide by 32 for block position int m_PosY; int m_PosZ; @@ -30,3 +34,7 @@ public: static const unsigned int c_Size = 1 + 4 + 2 + 4 + 4 + 4 + 1 + 1 + 2; // Minimum size }; + + + + diff --git a/source/packets/cPacket_NewInvalidState.cpp b/source/packets/cPacket_NewInvalidState.cpp index ce9b9d12a..a243a0e51 100644 --- a/source/packets/cPacket_NewInvalidState.cpp +++ b/source/packets/cPacket_NewInvalidState.cpp @@ -10,28 +10,33 @@ cPacket_NewInvalidState::cPacket_NewInvalidState( const cPacket_NewInvalidState & a_Copy ) { m_PacketID = E_NEW_INVALID_STATE; - m_Reason = a_Copy.m_Reason; + m_Reason = a_Copy.m_Reason; m_GameMode = a_Copy.m_GameMode; } -bool cPacket_NewInvalidState::Parse(cSocket & a_Socket) { - m_Socket = a_Socket; - if( !ReadByte ( m_Reason ) ) return false; - if( !ReadByte ( m_GameMode ) ) return false; - return true; -} -bool cPacket_NewInvalidState::Send(cSocket & a_Socket) + + + +int cPacket_NewInvalidState::Parse(const char * a_Data, int a_Size) { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadByte, m_Reason, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_GameMode, TotalBytes); + return TotalBytes; +} + + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_Reason, Message, i ); - AppendByte ( m_GameMode, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + +void cPacket_NewInvalidState::Serialize(AString & a_Data) const +{ + AppendByte(a_Data, m_PacketID); + AppendByte(a_Data, m_Reason); + AppendByte(a_Data, m_GameMode); } + + + + diff --git a/source/packets/cPacket_NewInvalidState.h b/source/packets/cPacket_NewInvalidState.h index fc86840de..2c7565e63 100644 --- a/source/packets/cPacket_NewInvalidState.h +++ b/source/packets/cPacket_NewInvalidState.h @@ -1,9 +1,12 @@ + #pragma once #include "cPacket.h" + + class cPacket_NewInvalidState : public cPacket { public: @@ -14,11 +17,15 @@ public: cPacket_NewInvalidState( const cPacket_NewInvalidState & a_Copy ); virtual cPacket* Clone() const { return new cPacket_NewInvalidState(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; char m_Reason; // 0 = Invalid Bed, 1 = Begin Raining, 2 End Raining, 3 = Change Gamemode char m_GameMode; // Used only when reason = 3. 0 is survival, 1 is creative. static const unsigned int c_Size = 1 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_PickupSpawn.cpp b/source/packets/cPacket_PickupSpawn.cpp index 6ddd99911..d8970650d 100644 --- a/source/packets/cPacket_PickupSpawn.cpp +++ b/source/packets/cPacket_PickupSpawn.cpp @@ -7,41 +7,41 @@ -bool cPacket_PickupSpawn::Parse( cSocket & a_Socket ) +int cPacket_PickupSpawn::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadInteger( m_UniqueID ) ) return false; - if( !ReadShort ( m_Item ) ) return false; - if( !ReadByte ( m_Count ) ) return false; - if( !ReadShort ( m_Health ) ) return false; - if( !ReadInteger( m_PosX ) ) return false; - if( !ReadInteger( m_PosY ) ) return false; - if( !ReadInteger( m_PosZ ) ) return false; - if( !ReadByte ( m_Rotation ) ) return false; - if( !ReadByte ( m_Pitch ) ) return false; - if( !ReadByte ( m_Roll ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_Item, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Count, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_Health, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Roll, TotalBytes); + return TotalBytes; } -bool cPacket_PickupSpawn::Send( cSocket & a_Socket ) + + + + +void cPacket_PickupSpawn::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendShort ( m_Item, Message, i ); - AppendByte ( m_Count, Message, i ); - AppendShort ( m_Health, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendInteger( m_PosY, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendByte ( m_Rotation, Message, i ); - AppendByte ( m_Pitch, Message, i ); - AppendByte ( m_Roll, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendShort (a_Data, m_Item); + AppendByte (a_Data, m_Count); + AppendShort (a_Data, m_Health); + AppendInteger(a_Data, m_PosX); + AppendInteger(a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_Rotation); + AppendByte (a_Data, m_Pitch); + AppendByte (a_Data, m_Roll); +} + + + + diff --git a/source/packets/cPacket_PickupSpawn.h b/source/packets/cPacket_PickupSpawn.h index a6226474e..d46999a5a 100644 --- a/source/packets/cPacket_PickupSpawn.h +++ b/source/packets/cPacket_PickupSpawn.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_PickupSpawn : public cPacket { public: @@ -20,8 +24,8 @@ public: { m_PacketID = E_PICKUP_SPAWN; } virtual cPacket* Clone() const { return new cPacket_PickupSpawn(*this); } - bool Parse( cSocket & a_Socket ); - bool Send( cSocket & a_Socket ); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; int m_UniqueID; short m_Item; @@ -36,3 +40,7 @@ public: static const unsigned int c_Size = 1 + 4 + 2 + 1 + 2 + 4 + 4 + 4 + 1 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_Ping.h b/source/packets/cPacket_Ping.h index 68b96faa5..0a0609aeb 100644 --- a/source/packets/cPacket_Ping.h +++ b/source/packets/cPacket_Ping.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Ping : public cPacket { public: @@ -10,7 +14,11 @@ public: { m_PacketID = E_PING; } virtual cPacket* Clone() const { return new cPacket_Ping(*this); } - bool Parse(cSocket & a_Socket) { (void)a_Socket; return true; } + virtual int Parse(const char * a_Data, int a_Size) override {return 0; } static const unsigned int c_Size = 1; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_PlayerListItem.cpp b/source/packets/cPacket_PlayerListItem.cpp index 70971dc34..3f8be2bde 100644 --- a/source/packets/cPacket_PlayerListItem.cpp +++ b/source/packets/cPacket_PlayerListItem.cpp @@ -8,7 +8,7 @@ -cPacket_PlayerListItem::cPacket_PlayerListItem(std::string a_PlayerName, bool a_Online, short a_Ping) +cPacket_PlayerListItem::cPacket_PlayerListItem(const AString & a_PlayerName, bool a_Online, short a_Ping) { m_PacketID = E_PLAYER_LIST_ITEM; m_PlayerName = a_PlayerName; @@ -16,33 +16,41 @@ cPacket_PlayerListItem::cPacket_PlayerListItem(std::string a_PlayerName, bool a_ m_Ping = a_Ping; } -bool cPacket_PlayerListItem::Parse( cSocket & a_Socket ) + + + + +int cPacket_PlayerListItem::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if (!ReadString(m_PlayerName)) return false; - if (!ReadBool(m_Online)) return false; - if (!ReadShort(m_Ping)) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadString16, m_PlayerName, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_Online, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_Ping, TotalBytes); + return TotalBytes; } -bool cPacket_PlayerListItem::Send( cSocket & a_Socket ) + + + + +void cPacket_PlayerListItem::Serialize(AString & a_Data) const { - int len = m_PlayerName.length(); - int end = (len <= 16) ? len : 16; - m_PlayerName = m_PlayerName.substr(0, end); - if (len <= 14) - m_PlayerName += cChatColor::White; // mistakes happen when you code late night :P - - unsigned int TotalSize = c_Size + m_PlayerName.size()*sizeof(short); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte((char)m_PacketID, Message, i); - AppendString16(m_PlayerName, Message, i); - AppendBool(m_Online, Message, i); - AppendShort(m_Ping, Message, i); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AString PlayerName(m_PlayerName); + if (PlayerName.length() > 16) + { + PlayerName.erase(16); + } + else if (PlayerName.length() <= 14) + { + PlayerName += cChatColor::White; + } + + AppendByte (a_Data, m_PacketID); + AppendString16(a_Data, PlayerName); + AppendBool (a_Data, m_Online); + AppendShort (a_Data, m_Ping); } + + + + diff --git a/source/packets/cPacket_PlayerListItem.h b/source/packets/cPacket_PlayerListItem.h index 840b4b81f..45395c556 100644 --- a/source/packets/cPacket_PlayerListItem.h +++ b/source/packets/cPacket_PlayerListItem.h @@ -1,22 +1,30 @@ + #pragma once #include "cPacket.h" + + + class cPacket_PlayerListItem : public cPacket { public: cPacket_PlayerListItem() { m_PacketID = E_PLAYER_LIST_ITEM; } - cPacket_PlayerListItem(std::string a_PlayerName, bool a_Online, short a_Ping); + cPacket_PlayerListItem(const AString & a_PlayerName, bool a_Online, short a_Ping); - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; virtual cPacket* Clone() const { return new cPacket_PlayerListItem(*this); } - std::string m_PlayerName; // Supports chat coloring, limited to 16 characters. - bool m_Online; - short m_Ping; + AString m_PlayerName; // Supports chat coloring, limited to 16 characters. + bool m_Online; + short m_Ping; static const unsigned int c_Size = 6; // Minimal size ( 6 + string ) -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_PlayerLook.cpp b/source/packets/cPacket_PlayerLook.cpp index 30c25c074..75adcf38c 100644 --- a/source/packets/cPacket_PlayerLook.cpp +++ b/source/packets/cPacket_PlayerLook.cpp @@ -11,33 +11,36 @@ cPacket_PlayerLook::cPacket_PlayerLook( cPlayer* a_Player ) { m_PacketID = E_PLAYERLOOK; - m_Rotation = a_Player->GetRotation(); - m_Pitch = a_Player->GetPitch(); - m_bFlying = a_Player->GetFlying(); + m_Pitch = a_Player->GetPitch(); + m_bFlying = a_Player->GetFlying(); } -bool cPacket_PlayerLook::Parse( cSocket & a_Socket ) + + + + +int cPacket_PlayerLook::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadFloat( m_Rotation ) ) return false; - if( !ReadFloat( m_Pitch ) ) return false; - if( !ReadBool ( m_bFlying ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadFloat, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadFloat, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + return TotalBytes; } -bool cPacket_PlayerLook::Send( cSocket & a_Socket ) + + + + +void cPacket_PlayerLook::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendFloat ( m_Rotation, Message, i ); - AppendFloat ( m_Pitch, Message, i ); - AppendBool ( m_bFlying, Message, i ); - - bool RetVal = !cSocket::IsSocketError( cPacket::SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendFloat (a_Data, m_Rotation); + AppendFloat (a_Data, m_Pitch); + AppendBool (a_Data, m_bFlying); +} + + + + diff --git a/source/packets/cPacket_PlayerLook.h b/source/packets/cPacket_PlayerLook.h index f1be2fb8b..aed8a954d 100644 --- a/source/packets/cPacket_PlayerLook.h +++ b/source/packets/cPacket_PlayerLook.h @@ -1,9 +1,18 @@ + #pragma once #include "cPacket.h" + + + class cPlayer; + + + + + class cPacket_PlayerLook : public cPacket { public: @@ -15,11 +24,16 @@ public: cPacket_PlayerLook( cPlayer* a_Player ); virtual cPacket* Clone() const { return new cPacket_PlayerLook(*this); } - bool Parse( cSocket & a_Socket ); - bool Send( cSocket & a_Socket ); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; float m_Rotation; float m_Pitch; - bool m_bFlying; // Yeah.. wtf + bool m_bFlying; // Yeah.. wtf + static const unsigned int c_Size = 10; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_PlayerMoveLook.cpp b/source/packets/cPacket_PlayerMoveLook.cpp index 55bd8f803..4b0af62e1 100644 --- a/source/packets/cPacket_PlayerMoveLook.cpp +++ b/source/packets/cPacket_PlayerMoveLook.cpp @@ -11,47 +11,48 @@ cPacket_PlayerMoveLook::cPacket_PlayerMoveLook( cPlayer* a_Player ) { m_PacketID = E_PLAYERMOVELOOK; + m_PosX = a_Player->GetPosX(); + m_PosY = a_Player->GetPosY() + 1.65; + m_PosZ = a_Player->GetPosZ(); + m_Stance = a_Player->GetStance(); + m_Rotation = a_Player->GetRotation(); + m_Pitch = a_Player->GetPitch(); + m_bFlying = a_Player->GetFlying(); +} + - m_PosX = a_Player->GetPosX(); - m_PosY = a_Player->GetPosY() + 1.65; - m_PosZ = a_Player->GetPosZ(); - m_Stance = a_Player->GetStance(); - m_Rotation = a_Player->GetRotation(); - m_Pitch = a_Player->GetPitch(); - m_bFlying = a_Player->GetFlying(); -} -bool cPacket_PlayerMoveLook::Parse( cSocket & a_Socket ) + +int cPacket_PlayerMoveLook::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - - if( !ReadDouble( m_PosX ) ) return false; - if( !ReadDouble( m_PosY ) ) return false; - if( !ReadDouble( m_Stance ) ) return false; - if( !ReadDouble( m_PosZ ) ) return false; - if( !ReadFloat ( m_Rotation ) ) return false; - if( !ReadFloat ( m_Pitch ) ) return false; - if( !ReadBool ( m_bFlying ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadDouble, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_Stance, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadFloat, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadFloat, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + return TotalBytes; } -bool cPacket_PlayerMoveLook::Send( cSocket & a_Socket ) + + + + +void cPacket_PlayerMoveLook::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendDouble( m_PosX, Message, i ); - AppendDouble( m_PosY, Message, i ); - AppendDouble( m_Stance, Message, i ); - AppendDouble( m_PosZ, Message, i ); - AppendFloat ( m_Rotation, Message, i ); - AppendFloat ( m_Pitch, Message, i ); - AppendBool ( m_bFlying, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendDouble(a_Data, m_PosX); + AppendDouble(a_Data, m_PosY); + AppendDouble(a_Data, m_Stance); + AppendDouble(a_Data, m_PosZ); + AppendFloat (a_Data, m_Rotation); + AppendFloat (a_Data, m_Pitch); + AppendBool (a_Data, m_bFlying); } + + + + diff --git a/source/packets/cPacket_PlayerMoveLook.h b/source/packets/cPacket_PlayerMoveLook.h index ca2d51f1a..c36698839 100644 --- a/source/packets/cPacket_PlayerMoveLook.h +++ b/source/packets/cPacket_PlayerMoveLook.h @@ -1,9 +1,18 @@ + #pragma once #include "cPacket.h" + + + class cPlayer; + + + + + class cPacket_PlayerMoveLook : public cPacket { public: @@ -19,15 +28,16 @@ public: cPacket_PlayerMoveLook( cPlayer* a_Player ); virtual cPacket* Clone() const { return new cPacket_PlayerMoveLook(*this); } - virtual bool Parse(cSocket & a_Socket); - virtual bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; double m_PosX; double m_PosY; double m_Stance; double m_PosZ; - float m_Rotation; - float m_Pitch; - bool m_bFlying; // Yeah.. wtf + float m_Rotation; + float m_Pitch; + bool m_bFlying; // Yeah.. wtf + static const unsigned int c_Size = 42; }; diff --git a/source/packets/cPacket_PlayerPosition.cpp b/source/packets/cPacket_PlayerPosition.cpp index c9e6c966f..3dae343dd 100644 --- a/source/packets/cPacket_PlayerPosition.cpp +++ b/source/packets/cPacket_PlayerPosition.cpp @@ -19,31 +19,35 @@ cPacket_PlayerPosition::cPacket_PlayerPosition( cPlayer* a_Player ) m_bFlying = a_Player->GetFlying(); } -bool cPacket_PlayerPosition::Parse(cSocket & a_Socket) + + + + +int cPacket_PlayerPosition::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadDouble( m_PosX ) ) return false; - if( !ReadDouble( m_PosY ) ) return false; - if( !ReadDouble( m_Stance ) ) return false; - if( !ReadDouble( m_PosZ ) ) return false; - if( !ReadBool( m_bFlying ) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadDouble, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_Stance, TotalBytes); + HANDLE_PACKET_READ(ReadDouble, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + return TotalBytes; } -bool cPacket_PlayerPosition::Send(cSocket & a_Socket) + + + + +void cPacket_PlayerPosition::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendDouble ( m_PosX, Message, i ); - AppendDouble ( m_PosY, Message, i ); - AppendDouble ( m_Stance, Message, i ); - AppendDouble ( m_PosZ, Message, i ); - AppendBool ( m_bFlying, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendDouble (a_Data, m_PosX); + AppendDouble (a_Data, m_PosY); + AppendDouble (a_Data, m_Stance); + AppendDouble (a_Data, m_PosZ); + AppendBool (a_Data, m_bFlying); +} + + + + diff --git a/source/packets/cPacket_PlayerPosition.h b/source/packets/cPacket_PlayerPosition.h index 87fc127d5..f883b6a4d 100644 --- a/source/packets/cPacket_PlayerPosition.h +++ b/source/packets/cPacket_PlayerPosition.h @@ -1,9 +1,19 @@ + #pragma once #include "cPacket.h" + + + + class cPlayer; + + + + + class cPacket_PlayerPosition : public cPacket { public: @@ -17,13 +27,18 @@ public: { m_PacketID = E_PLAYERPOS; } virtual cPacket* Clone() const { return new cPacket_PlayerPosition(*this); } - virtual bool Parse(cSocket & a_Socket); - virtual bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; double m_PosX; double m_PosY; double m_Stance; double m_PosZ; - bool m_bFlying; // Yeah.. wtf + bool m_bFlying; // Yeah.. wtf + static const unsigned int c_Size = 34; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_PreChunk.cpp b/source/packets/cPacket_PreChunk.cpp index 8117f0ad7..a792ce124 100644 --- a/source/packets/cPacket_PreChunk.cpp +++ b/source/packets/cPacket_PreChunk.cpp @@ -7,18 +7,14 @@ -bool cPacket_PreChunk::Send(cSocket & a_Socket) +void cPacket_PreChunk::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendBool ( m_bLoad, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_PosX); + AppendInteger(a_Data, m_PosZ); + AppendBool (a_Data, m_bLoad); } + + + + diff --git a/source/packets/cPacket_PreChunk.h b/source/packets/cPacket_PreChunk.h index e1ac82f39..6e24ae5ae 100644 --- a/source/packets/cPacket_PreChunk.h +++ b/source/packets/cPacket_PreChunk.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_PreChunk: public cPacket { public: @@ -13,10 +17,15 @@ public: { m_PacketID = E_PRE_CHUNK; } virtual cPacket* Clone() const { return new cPacket_PreChunk(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - int m_PosX; - int m_PosZ; + int m_PosX; + int m_PosZ; bool m_bLoad; + static const unsigned int c_Size = 10; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_RelativeEntityMove.cpp b/source/packets/cPacket_RelativeEntityMove.cpp index 8072097b6..c4d0f3196 100644 --- a/source/packets/cPacket_RelativeEntityMove.cpp +++ b/source/packets/cPacket_RelativeEntityMove.cpp @@ -7,19 +7,15 @@ -bool cPacket_RelativeEntityMove::Send( cSocket & a_Socket ) +void cPacket_RelativeEntityMove::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendByte ( m_MoveX, Message, i ); - AppendByte ( m_MoveY, Message, i ); - AppendByte ( m_MoveZ, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendByte (a_Data, m_MoveX); + AppendByte (a_Data, m_MoveY); + AppendByte (a_Data, m_MoveZ); } + + + + diff --git a/source/packets/cPacket_RelativeEntityMove.h b/source/packets/cPacket_RelativeEntityMove.h index 5b45ccd94..9ccf0d95f 100644 --- a/source/packets/cPacket_RelativeEntityMove.h +++ b/source/packets/cPacket_RelativeEntityMove.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_RelativeEntityMove : public cPacket { public: @@ -14,12 +18,16 @@ public: { m_PacketID = E_REL_ENT_MOVE; } virtual cPacket* Clone() const { return new cPacket_RelativeEntityMove(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; - int m_UniqueID; + int m_UniqueID; char m_MoveX; // Pixels, devide by 32 for block char m_MoveY; char m_MoveZ; static const unsigned int c_Size = 1 + 4 + 1 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_RelativeEntityMoveLook.cpp b/source/packets/cPacket_RelativeEntityMoveLook.cpp index 8de1a0be7..1e8a9ae6c 100644 --- a/source/packets/cPacket_RelativeEntityMoveLook.cpp +++ b/source/packets/cPacket_RelativeEntityMoveLook.cpp @@ -7,21 +7,18 @@ -bool cPacket_RelativeEntityMoveLook::Send( cSocket & a_Socket ) +void cPacket_RelativeEntityMoveLook::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendByte ( m_MoveX, Message, i ); - AppendByte ( m_MoveY, Message, i ); - AppendByte ( m_MoveZ, Message, i ); - AppendByte ( m_Yaw, Message, i ); - AppendByte ( m_Pitch, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendByte (a_Data, m_MoveX); + AppendByte (a_Data, m_MoveY); + AppendByte (a_Data, m_MoveZ); + AppendByte (a_Data, m_Yaw); + AppendByte (a_Data, m_Pitch); } + + + + + diff --git a/source/packets/cPacket_RelativeEntityMoveLook.h b/source/packets/cPacket_RelativeEntityMoveLook.h index e918041ae..79cb1a7eb 100644 --- a/source/packets/cPacket_RelativeEntityMoveLook.h +++ b/source/packets/cPacket_RelativeEntityMoveLook.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_RelativeEntityMoveLook : public cPacket { public: @@ -16,9 +20,9 @@ public: { m_PacketID = E_REL_ENT_MOVE_LOOK; } virtual cPacket* Clone() const { return new cPacket_RelativeEntityMoveLook(*this); } - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; - int m_UniqueID; + int m_UniqueID; char m_MoveX; // Pixels, divide by 32 for block char m_MoveY; char m_MoveZ; @@ -27,3 +31,7 @@ public: static const unsigned int c_Size = 1 + 4 + 1 + 1 + 1 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_Respawn.cpp b/source/packets/cPacket_Respawn.cpp index b09659eb3..f438e8bde 100644 --- a/source/packets/cPacket_Respawn.cpp +++ b/source/packets/cPacket_Respawn.cpp @@ -7,34 +7,33 @@ -bool cPacket_Respawn::Send(cSocket & a_Socket) +void cPacket_Respawn::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_LevelType.size() * sizeof(short); - - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_World, Message, i ); - AppendByte ( m_Difficulty, Message, i ); - AppendByte ( m_CreativeMode, Message, i ); - AppendShort ( m_WorldHeight, Message, i ); - AppendLong ( m_MapSeed, Message, i ); - AppendString16 ( m_LevelType, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendByte (a_Data, m_World); + AppendByte (a_Data, m_Difficulty); + AppendByte (a_Data, m_CreativeMode); + AppendShort (a_Data, m_WorldHeight); + AppendLong (a_Data, m_MapSeed); + AppendString16(a_Data, m_LevelType); } -bool cPacket_Respawn::Parse(cSocket & a_Socket) + + + + +int cPacket_Respawn::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadByte( m_World ) ) return false; - if( !ReadByte( m_Difficulty ) ) return false; - if( !ReadByte( m_CreativeMode ) ) return false; - if( !ReadShort( m_WorldHeight ) ) return false; - if( !ReadLong( m_MapSeed ) ) return false; - if( !ReadString16( m_LevelType ) ) return false; - return true; -} \ No newline at end of file + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadByte, m_World, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_Difficulty, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_CreativeMode, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_WorldHeight, TotalBytes); + HANDLE_PACKET_READ(ReadLong, m_MapSeed, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_LevelType, TotalBytes); + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_Respawn.h b/source/packets/cPacket_Respawn.h index f0faba9f5..7f5950945 100644 --- a/source/packets/cPacket_Respawn.h +++ b/source/packets/cPacket_Respawn.h @@ -1,9 +1,13 @@ + #pragma once #include "cPacket.h" #include "cPacket_Login.h" -#include + + + + class cPacket_Respawn : public cPacket { @@ -18,15 +22,19 @@ public: { m_PacketID = E_RESPAWN; } virtual cPacket* Clone() const { return new cPacket_Respawn( *this ); } - bool Send(cSocket & a_Socket); - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; char m_World; char m_Difficulty; char m_CreativeMode; short m_WorldHeight; long long m_MapSeed; - std::string m_LevelType; + AString m_LevelType; static const unsigned int c_Size = 1 + 1 + 1 + 1 + 2 + 8 + 2; }; + + + + diff --git a/source/packets/cPacket_SoundEffect.cpp b/source/packets/cPacket_SoundEffect.cpp index 453750d55..efc9b8e6b 100644 --- a/source/packets/cPacket_SoundEffect.cpp +++ b/source/packets/cPacket_SoundEffect.cpp @@ -9,29 +9,28 @@ cPacket_SoundEffect::cPacket_SoundEffect( const cPacket_SoundEffect & a_Copy ) { - m_PacketID = E_SOUND_EFFECT; - m_SoundID = a_Copy.m_SoundID; - m_PosX = a_Copy.m_PosX; - m_PosY = a_Copy.m_PosY; - m_PosZ = a_Copy.m_PosZ; - m_SoundData = a_Copy.m_SoundData; + m_PacketID = E_SOUND_EFFECT; + m_SoundID = a_Copy.m_SoundID; + m_PosX = a_Copy.m_PosX; + m_PosY = a_Copy.m_PosY; + m_PosZ = a_Copy.m_PosZ; + m_SoundData = a_Copy.m_SoundData; } -bool cPacket_SoundEffect::Send(cSocket & a_Socket) + + + +void cPacket_SoundEffect::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_SoundID, Message, i ); - AppendInteger ( m_PosX, Message, i ); - AppendByte ( (char)m_PosY, Message, i ); - AppendInteger ( m_PosZ, Message, i ); - AppendInteger ( m_SoundData, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_SoundID); + AppendInteger(a_Data, m_PosX); + AppendByte (a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendInteger(a_Data, m_SoundData); } + + + + diff --git a/source/packets/cPacket_SoundEffect.h b/source/packets/cPacket_SoundEffect.h index 973023d41..9293d53f0 100644 --- a/source/packets/cPacket_SoundEffect.h +++ b/source/packets/cPacket_SoundEffect.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_SoundEffect : public cPacket { public: @@ -16,7 +20,7 @@ public: cPacket_SoundEffect( const cPacket_SoundEffect & a_Copy ); virtual cPacket* Clone() const { return new cPacket_SoundEffect(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; int m_SoundID; // Sound ID int m_PosX; // Block X Coordinate @@ -26,3 +30,7 @@ public: static const unsigned int c_Size = 1 + 4 + 4 + 1 + 4 + 4; }; + + + + diff --git a/source/packets/cPacket_SpawnMob.cpp b/source/packets/cPacket_SpawnMob.cpp index 417a7c004..ba0029dd3 100644 --- a/source/packets/cPacket_SpawnMob.cpp +++ b/source/packets/cPacket_SpawnMob.cpp @@ -14,11 +14,15 @@ cPacket_SpawnMob::~cPacket_SpawnMob() delete m_Pos; } + + + + cPacket_SpawnMob::cPacket_SpawnMob() : m_UniqueID( 0 ) , m_Type( 0 ) , m_Pos( new Vector3i() ) - , m_Yaw( 0 ) + , m_Yaw( 0 ) , m_Pitch( 0 ) , m_MetaDataSize( 0 ) , m_MetaData( 0 ) @@ -26,6 +30,10 @@ cPacket_SpawnMob::cPacket_SpawnMob() m_PacketID = E_SPAWN_MOB; } + + + + cPacket_SpawnMob::cPacket_SpawnMob( const cPacket_SpawnMob & a_Clone ) { m_Pos = new Vector3i(); @@ -42,24 +50,23 @@ cPacket_SpawnMob::cPacket_SpawnMob( const cPacket_SpawnMob & a_Clone ) memcpy( m_MetaData, a_Clone.m_MetaData, sizeof( char ) * m_MetaDataSize ); } -bool cPacket_SpawnMob::Send(cSocket & a_Socket) + + + + +void cPacket_SpawnMob::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_MetaDataSize; - - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_UniqueID, Message, i ); - AppendByte ( m_Type, Message, i ); - AppendInteger ( m_Pos->x, Message, i ); - AppendInteger ( m_Pos->y, Message, i ); - AppendInteger ( m_Pos->z, Message, i ); - AppendByte ( m_Yaw, Message, i ); - AppendByte ( m_Pitch, Message, i ); - AppendData ( m_MetaData, m_MetaDataSize, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger (a_Data, m_UniqueID); + AppendByte (a_Data, m_Type); + AppendInteger (a_Data, m_Pos->x); + AppendInteger (a_Data, m_Pos->y); + AppendInteger (a_Data, m_Pos->z); + AppendByte (a_Data, m_Yaw); + AppendByte (a_Data, m_Pitch); + AppendData (a_Data, m_MetaData, m_MetaDataSize); } + + + + diff --git a/source/packets/cPacket_SpawnMob.h b/source/packets/cPacket_SpawnMob.h index bd475bfb1..8a1ef2264 100644 --- a/source/packets/cPacket_SpawnMob.h +++ b/source/packets/cPacket_SpawnMob.h @@ -1,9 +1,18 @@ + #pragma once #include "cPacket.h" + + + class Vector3i; + + + + + class cPacket_SpawnMob : public cPacket { public: @@ -12,16 +21,20 @@ public: virtual cPacket* Clone() const { return new cPacket_SpawnMob( *this ); } ~cPacket_SpawnMob(); - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - int m_UniqueID; - char m_Type; + int m_UniqueID; + char m_Type; Vector3i* m_Pos; - char m_Yaw; - char m_Pitch; + char m_Yaw; + char m_Pitch; static const unsigned int c_Size = 1 + 4 + 1 + 4 + 4 + 4 + 1 + 1; // + metadata unsigned int m_MetaDataSize; - char* m_MetaData; + char * m_MetaData; }; + + + + diff --git a/source/packets/cPacket_TeleportEntity.cpp b/source/packets/cPacket_TeleportEntity.cpp index 0b735a1cb..a888f009d 100644 --- a/source/packets/cPacket_TeleportEntity.cpp +++ b/source/packets/cPacket_TeleportEntity.cpp @@ -17,25 +17,25 @@ cPacket_TeleportEntity::cPacket_TeleportEntity(cEntity* a_Client) m_PosX = (int)(a_Client->GetPosX() * 32); m_PosY = (int)(a_Client->GetPosY() * 32); m_PosZ = (int)(a_Client->GetPosZ() * 32); - m_Rotation = (char)((a_Client->GetRotation()/360.f)*256); - m_Pitch = (char)((a_Client->GetPitch()/360.f)*256); + m_Rotation = (char)((a_Client->GetRotation() / 360.f) * 256); + m_Pitch = (char)((a_Client->GetPitch() / 360.f) * 256); } -bool cPacket_TeleportEntity::Send( cSocket & a_Socket ) + + + + +void cPacket_TeleportEntity::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger( m_UniqueID, Message, i ); - AppendInteger( m_PosX, Message, i ); - AppendInteger( m_PosY, Message, i ); - AppendInteger( m_PosZ, Message, i ); - AppendByte ( m_Rotation, Message, i ); - AppendByte ( m_Pitch, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger(a_Data, m_UniqueID); + AppendInteger(a_Data, m_PosX); + AppendInteger(a_Data, m_PosY); + AppendInteger(a_Data, m_PosZ); + AppendByte (a_Data, m_Rotation); + AppendByte (a_Data, m_Pitch); } + + + + diff --git a/source/packets/cPacket_TeleportEntity.h b/source/packets/cPacket_TeleportEntity.h index 8433609bd..0cff50a85 100644 --- a/source/packets/cPacket_TeleportEntity.h +++ b/source/packets/cPacket_TeleportEntity.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cEntity; class cPacket_TeleportEntity : public cPacket { @@ -18,14 +22,18 @@ public: virtual cPacket* Clone() const { return new cPacket_TeleportEntity(*this); } cPacket_TeleportEntity(cEntity* a_Client); - bool Send( cSocket & a_Socket ); + virtual void Serialize(AString & a_Data) const override; - int m_UniqueID; - int m_PosX; // Pixel position, divide by 32 for block position - int m_PosY; - int m_PosZ; + int m_UniqueID; + int m_PosX; // Pixel position, divide by 32 for block position + int m_PosY; + int m_PosZ; char m_Rotation; char m_Pitch; static const unsigned int c_Size = 1 + 4 + 4 + 4 + 4 + 1 + 1; }; + + + + diff --git a/source/packets/cPacket_Thunderbolt.cpp b/source/packets/cPacket_Thunderbolt.cpp index 51c951a1d..81054ad8e 100644 --- a/source/packets/cPacket_Thunderbolt.cpp +++ b/source/packets/cPacket_Thunderbolt.cpp @@ -9,28 +9,28 @@ cPacket_Thunderbolt::cPacket_Thunderbolt( const cPacket_Thunderbolt & a_Copy ) { - m_PacketID = E_THUNDERBOLT; - m_UniqueID = 237183; //just a random Ent ID. I don't think this matters at all. - m_Unknown = true; - m_xLBPos = a_Copy.m_xLBPos; - m_yLBPos = a_Copy.m_yLBPos; - m_zLBPos = a_Copy.m_zLBPos; + m_PacketID = E_THUNDERBOLT; + m_UniqueID = 237183; //just a random Ent ID. I don't think this matters at all. + m_Unknown = true; + m_xLBPos = a_Copy.m_xLBPos; + m_yLBPos = a_Copy.m_yLBPos; + m_zLBPos = a_Copy.m_zLBPos; } -bool cPacket_Thunderbolt::Send(cSocket & a_Socket) + + + + +void cPacket_Thunderbolt::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_UniqueID, Message, i ); - AppendBool ( m_Unknown, Message, i ); - AppendInteger ( m_xLBPos*32, Message, i ); - AppendInteger ( m_yLBPos*32, Message, i ); - AppendInteger ( m_zLBPos*32, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; + AppendByte (a_Data, m_PacketID); + AppendInteger (a_Data, m_UniqueID); + AppendBool (a_Data, m_Unknown); + AppendInteger (a_Data, m_xLBPos * 32); + AppendInteger (a_Data, m_yLBPos * 32); + AppendInteger (a_Data, m_zLBPos * 32); } + + + + diff --git a/source/packets/cPacket_Thunderbolt.h b/source/packets/cPacket_Thunderbolt.h index 94939c543..c3c4e2c98 100644 --- a/source/packets/cPacket_Thunderbolt.h +++ b/source/packets/cPacket_Thunderbolt.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_Thunderbolt : public cPacket { public: @@ -16,13 +20,17 @@ public: cPacket_Thunderbolt( const cPacket_Thunderbolt & a_Copy ); virtual cPacket* Clone() const { return new cPacket_Thunderbolt(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - int m_UniqueID; // The entity ID of the thunderbolt + int m_UniqueID; // The entity ID of the thunderbolt bool m_Unknown; // Always true. Might have a meaning in the future... - int m_xLBPos; // Thunderbolt X as Absolute Integer - int m_yLBPos; // Thunderbolt Y as Absolute Integer - int m_zLBPos; // Thunderbolt Z as Absolute Integer + int m_xLBPos; // Thunderbolt X as Absolute Integer + int m_yLBPos; // Thunderbolt Y as Absolute Integer + int m_zLBPos; // Thunderbolt Z as Absolute Integer static const unsigned int c_Size = 1 + 4 + 1 + 4 + 4 + 4; }; + + + + diff --git a/source/packets/cPacket_TimeUpdate.cpp b/source/packets/cPacket_TimeUpdate.cpp index c1e760d62..a0bf05677 100644 --- a/source/packets/cPacket_TimeUpdate.cpp +++ b/source/packets/cPacket_TimeUpdate.cpp @@ -7,23 +7,23 @@ -bool cPacket_TimeUpdate::Parse(cSocket & a_Socket) +int cPacket_TimeUpdate::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadLong(m_Time) ) return false; - return true; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadLong, m_Time, TotalBytes); + return TotalBytes; } -bool cPacket_TimeUpdate::Send(cSocket & a_Socket) + + + + +void cPacket_TimeUpdate::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; + AppendByte(a_Data, m_PacketID); + AppendLong(a_Data, m_Time); +} + + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendLong ( m_Time, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_TimeUpdate.h b/source/packets/cPacket_TimeUpdate.h index b320321cb..1cc66c2f9 100644 --- a/source/packets/cPacket_TimeUpdate.h +++ b/source/packets/cPacket_TimeUpdate.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_TimeUpdate : public cPacket { public: @@ -12,9 +16,14 @@ public: cPacket_TimeUpdate( long long a_Time ) { m_PacketID = E_UPDATE_TIME; m_Time = a_Time; } virtual cPacket* Clone() const { return new cPacket_TimeUpdate(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; long long m_Time; + static const unsigned int c_Size = 1 + 8; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_UpdateHealth.cpp b/source/packets/cPacket_UpdateHealth.cpp index c6cec56ae..4380e25d6 100644 --- a/source/packets/cPacket_UpdateHealth.cpp +++ b/source/packets/cPacket_UpdateHealth.cpp @@ -7,19 +7,14 @@ -bool cPacket_UpdateHealth::Send(cSocket & a_Socket) +void cPacket_UpdateHealth::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; + AppendByte (a_Data, m_PacketID); + AppendShort(a_Data, m_Health); + AppendShort(a_Data, m_Food); + AppendFloat(a_Data, m_Saturation); +} + - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendShort ( m_Health, Message, i ); - AppendShort ( m_Food, Message, i ); - AppendFloat ( m_Saturation, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} diff --git a/source/packets/cPacket_UpdateHealth.h b/source/packets/cPacket_UpdateHealth.h index 49c06b1b6..3a6135e07 100644 --- a/source/packets/cPacket_UpdateHealth.h +++ b/source/packets/cPacket_UpdateHealth.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_UpdateHealth : public cPacket { public: @@ -14,7 +18,7 @@ public: cPacket_UpdateHealth( short a_Health ) { m_Health = a_Health; m_PacketID = E_UPDATE_HEALTH; } virtual cPacket* Clone() const { return new cPacket_UpdateHealth( *this ); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; short m_Health; short m_Food; @@ -22,3 +26,7 @@ public: static const unsigned int c_Size = 1 + 2 + 2 + 4; }; + + + + diff --git a/source/packets/cPacket_UpdateSign.cpp b/source/packets/cPacket_UpdateSign.cpp index 726ee5865..723fca6af 100644 --- a/source/packets/cPacket_UpdateSign.cpp +++ b/source/packets/cPacket_UpdateSign.cpp @@ -7,42 +7,35 @@ -bool cPacket_UpdateSign::Parse( cSocket & a_Socket ) +int cPacket_UpdateSign::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_Line1, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_Line2, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_Line3, TotalBytes); + HANDLE_PACKET_READ(ReadString16, m_Line4, TotalBytes); + return TotalBytes; +} + - if( !ReadInteger( m_PosX ) ) return false; - if( !ReadShort ( m_PosY ) ) return false; - if( !ReadInteger( m_PosZ ) ) return false; - if( !ReadString16 ( m_Line1 ) ) return false; - if( !ReadString16 ( m_Line2 ) ) return false; - if( !ReadString16 ( m_Line3 ) ) return false; - if( !ReadString16 ( m_Line4 ) ) return false; - return true; -} -bool cPacket_UpdateSign::Send( cSocket & a_Socket ) + +void cPacket_UpdateSign::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - TotalSize += m_Line1.size() * sizeof( short ); - TotalSize += m_Line2.size() * sizeof( short ); - TotalSize += m_Line3.size() * sizeof( short ); - TotalSize += m_Line4.size() * sizeof( short ); - - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendInteger ( m_PosX, Message, i ); - AppendShort ( m_PosY, Message, i ); - AppendInteger ( m_PosZ, Message, i ); - AppendString16 ( m_Line1, Message, i ); - AppendString16 ( m_Line2, Message, i ); - AppendString16 ( m_Line3, Message, i ); - AppendString16 ( m_Line4, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendInteger (a_Data, m_PosX); + AppendShort (a_Data, m_PosY); + AppendInteger (a_Data, m_PosZ); + AppendString16(a_Data, m_Line1); + AppendString16(a_Data, m_Line2); + AppendString16(a_Data, m_Line3); + AppendString16(a_Data, m_Line4); +} + + + + diff --git a/source/packets/cPacket_UpdateSign.h b/source/packets/cPacket_UpdateSign.h index 6c86fadfb..1e4da6471 100644 --- a/source/packets/cPacket_UpdateSign.h +++ b/source/packets/cPacket_UpdateSign.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_UpdateSign : public cPacket { public: @@ -13,16 +17,20 @@ public: { m_PacketID = E_UPDATE_SIGN; } virtual cPacket* Clone() const { return new cPacket_UpdateSign( *this ); } - bool Parse( cSocket & a_Socket ); - bool Send( cSocket & a_Socket ); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; - int m_PosX; - short m_PosY; - int m_PosZ; - std::string m_Line1; - std::string m_Line2; - std::string m_Line3; - std::string m_Line4; + int m_PosX; + short m_PosY; + int m_PosZ; + AString m_Line1; + AString m_Line2; + AString m_Line3; + AString m_Line4; static const unsigned int c_Size = 1 + 4 + 2 + 4 + 2 + 2 + 2 + 2; // minimum size }; + + + + diff --git a/source/packets/cPacket_UseEntity.cpp b/source/packets/cPacket_UseEntity.cpp index 346341fb0..b131cb4fa 100644 --- a/source/packets/cPacket_UseEntity.cpp +++ b/source/packets/cPacket_UseEntity.cpp @@ -7,11 +7,15 @@ -bool cPacket_UseEntity::Parse(cSocket & a_Socket) +int cPacket_UseEntity::Parse(const char * a_Data, int a_Size) { - m_Socket = a_Socket; - if( !ReadInteger(m_UniqueID) ) return false; - if( !ReadInteger(m_TargetID) ) return false; - if( !ReadBool (m_bLeftClick) ) return false; - return true; -} \ No newline at end of file + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadInteger, m_TargetID, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bLeftClick, TotalBytes); + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_UseEntity.h b/source/packets/cPacket_UseEntity.h index 6e5bf5691..a81352664 100644 --- a/source/packets/cPacket_UseEntity.h +++ b/source/packets/cPacket_UseEntity.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_UseEntity : public cPacket { public: @@ -13,11 +17,15 @@ public: { m_PacketID = E_USE_ENTITY; } virtual cPacket* Clone() const { return new cPacket_UseEntity(*this); } - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; - int m_UniqueID; - int m_TargetID; + int m_UniqueID; + int m_TargetID; bool m_bLeftClick; static const unsigned int c_Size = 1 + 4 + 4 + 1; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_WholeInventory.cpp b/source/packets/cPacket_WholeInventory.cpp index 130d4dc8c..5e83f481f 100644 --- a/source/packets/cPacket_WholeInventory.cpp +++ b/source/packets/cPacket_WholeInventory.cpp @@ -20,6 +20,10 @@ cPacket_WholeInventory::cPacket_WholeInventory( const cPacket_WholeInventory & a memcpy( m_Items, a_Clone.m_Items, sizeof(cItem)*m_Count ); } + + + + cPacket_WholeInventory::cPacket_WholeInventory( cInventory* a_Inventory ) { m_PacketID = E_INVENTORY_WHOLE; @@ -29,6 +33,10 @@ cPacket_WholeInventory::cPacket_WholeInventory( cInventory* a_Inventory ) memcpy( m_Items, a_Inventory->GetSlots(), sizeof(cItem)*m_Count ); } + + + + cPacket_WholeInventory::cPacket_WholeInventory( cWindow* a_Window ) { m_PacketID = E_INVENTORY_WHOLE; @@ -38,37 +46,32 @@ cPacket_WholeInventory::cPacket_WholeInventory( cWindow* a_Window ) memcpy( m_Items, a_Window->GetSlots(), sizeof(cItem)*m_Count ); } + + + + cPacket_WholeInventory::~cPacket_WholeInventory() { delete [] m_Items; } -bool cPacket_WholeInventory::Send(cSocket & a_Socket) -{ - unsigned int TotalSize = c_Size; - cPacket_ItemData Item; - for(int i = 0; i < m_Count; i++) - { - TotalSize += Item.GetSize((short) m_Items[i].m_ItemID); - } - char* Message = new char[TotalSize]; - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_WindowID, Message, i ); - AppendShort ( m_Count, Message, i ); +void cPacket_WholeInventory::Serialize(AString & a_Data) const +{ + AppendByte (a_Data, m_PacketID); + AppendByte (a_Data, m_WindowID); + AppendShort(a_Data, m_Count); - for(int j = 0; j < m_Count; j++) + for (int j = 0; j < m_Count; j++) { - Item.AppendItem(Message, i, &(m_Items[j])); + cPacket_ItemData::AppendItem(a_Data, &(m_Items[j])); } - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; } + + + \ No newline at end of file diff --git a/source/packets/cPacket_WholeInventory.h b/source/packets/cPacket_WholeInventory.h index cee102055..5d895b367 100644 --- a/source/packets/cPacket_WholeInventory.h +++ b/source/packets/cPacket_WholeInventory.h @@ -1,12 +1,22 @@ + #pragma once #include "cPacket.h" #include "../BlockID.h" + + + + class cInventory; class cWindow; class cItem; + + + + + class cPacket_WholeInventory : public cPacket // full inventory [S -> C] ? { public: @@ -23,12 +33,15 @@ public: virtual cPacket* Clone() const { return new cPacket_WholeInventory(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - char m_WindowID; // WTF? - short m_Count; // Number of items - - cItem* m_Items; // Array of m_Count items + char m_WindowID; // WTF? + short m_Count; // Number of items + cItem * m_Items; // Array of m_Count items static const unsigned int c_Size = 1 + 1 + 2; // Minimal size -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_WindowClick.cpp b/source/packets/cPacket_WindowClick.cpp index 2df2108e1..0b2d18415 100644 --- a/source/packets/cPacket_WindowClick.cpp +++ b/source/packets/cPacket_WindowClick.cpp @@ -9,31 +9,35 @@ -bool cPacket_WindowClick::Parse(cSocket & a_Socket) +int cPacket_WindowClick::Parse(const char * a_Data, int a_Size) { -// LOG("-----------INV66-----------"); - m_Socket = a_Socket; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadByte, m_WindowID, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_SlotNum, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_RightMouse, TotalBytes); + HANDLE_PACKET_READ(ReadShort, m_NumClicks, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_Bool, TotalBytes); - if( !ReadByte(m_WindowID) ) return false; - if( !ReadShort(m_SlotNum) ) return false; - if( !ReadByte(m_RightMouse) ) return false; - if( !ReadShort(m_NumClicks) ) return false; - if( !ReadBool(m_Bool) ) return false; - -// LOG("WindowID : %i", m_Type ); -// LOG("FromSlot: %i", m_SlotNum ); -// LOG("Right/Le: %i", m_RightMouse ); -// LOG("NumClick: %i", m_NumClicks ); + // LOG("WindowClick: WindowID: %i; FromSlot: %i; Right/Le: %i; NumClick: %i", m_Type, m_SlotNum, m_RightMouse, m_NumClicks ); cPacket_ItemData Item; - Item.Parse(m_Socket); + int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + if (res < 0) + { + return res; + } + TotalBytes += res; - m_ItemID = Item.m_ItemID; + m_ItemID = Item.m_ItemID; m_ItemCount = Item.m_ItemCount; - m_ItemUses = Item.m_ItemUses; + m_ItemUses = Item.m_ItemUses; m_EnchantNums = Item.m_EnchantNums; - return true; -} \ No newline at end of file + return TotalBytes; +} + + + + diff --git a/source/packets/cPacket_WindowClick.h b/source/packets/cPacket_WindowClick.h index fc380d9fc..09fc1d862 100644 --- a/source/packets/cPacket_WindowClick.h +++ b/source/packets/cPacket_WindowClick.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_WindowClick : public cPacket // [C -> S] { public: @@ -19,9 +23,9 @@ public: { m_PacketID = E_WINDOW_CLICK; } virtual cPacket* Clone() const { return new cPacket_WindowClick(*this); } - bool Parse(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; - char m_WindowID; + char m_WindowID; short m_SlotNum; // Slot // 0 = craft result // 1-4 = crafting table @@ -29,16 +33,20 @@ public: // 9-35 = inventory // 36-44 = Hot bar - char m_RightMouse; // 0 = left 1 = Right mb + char m_RightMouse; // 0 = left 1 = Right mb short m_NumClicks; // Num clicks - bool m_Bool; // unkown???????????? SHIFT clicked + bool m_Bool; // unkown???????????? SHIFT clicked // Below = item short m_ItemID; // if this is -1 the next stuff dont exist - char m_ItemCount; + char m_ItemCount; short m_ItemUses; short m_EnchantNums; static const unsigned int c_Size = 1 + 1 + 2 + 1 + 2 + 2; // Minimal size ( +1+1 = max) -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_WindowClose.cpp b/source/packets/cPacket_WindowClose.cpp index 6cd8f861a..25669bf79 100644 --- a/source/packets/cPacket_WindowClose.cpp +++ b/source/packets/cPacket_WindowClose.cpp @@ -7,28 +7,23 @@ -bool cPacket_WindowClose::Parse(cSocket & a_Socket) +int cPacket_WindowClose::Parse(const char * a_Data, int a_Size) { - //LOG("CLOSE INVENTORY PACKET"); - m_Socket = a_Socket; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadByte, m_Close, TotalBytes); + return TotalBytes; +} + - if( !ReadByte(m_Close) ) return false; - //LOG("Closed inventory?: %i", m_Close ); - return true; -} -bool cPacket_WindowClose::Send(cSocket & a_Socket) +void cPacket_WindowClose::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size; - char* Message = new char[TotalSize]; + AppendByte(a_Data, m_PacketID); + AppendByte(a_Data, m_Close); +} + + - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_Close, Message, i ); - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file diff --git a/source/packets/cPacket_WindowClose.h b/source/packets/cPacket_WindowClose.h index a2f09fd09..1063896ff 100644 --- a/source/packets/cPacket_WindowClose.h +++ b/source/packets/cPacket_WindowClose.h @@ -1,8 +1,12 @@ + #pragma once #include "cPacket.h" + + + class cPacket_WindowClose : public cPacket { public: @@ -11,10 +15,14 @@ public: { m_PacketID = E_WINDOW_CLOSE; } virtual cPacket* Clone() const { return new cPacket_WindowClose(*this); } - bool Parse(cSocket & a_Socket); - bool Send(cSocket & a_Socket); + virtual int Parse(const char * a_Data, int a_Size) override; + virtual void Serialize(AString & a_Data) const override; char m_Close; // m_Close == cWindow WindowType number static const unsigned int c_Size = 1 + 1; -}; \ No newline at end of file +}; + + + + diff --git a/source/packets/cPacket_WindowOpen.cpp b/source/packets/cPacket_WindowOpen.cpp index 46e84a943..a5c3ade7e 100644 --- a/source/packets/cPacket_WindowOpen.cpp +++ b/source/packets/cPacket_WindowOpen.cpp @@ -7,19 +7,15 @@ -bool cPacket_WindowOpen::Send(cSocket & a_Socket) +void cPacket_WindowOpen::Serialize(AString & a_Data) const { - unsigned int TotalSize = c_Size + m_WindowTitle.size() * sizeof( short ); - char* Message = new char[TotalSize]; - - unsigned int i = 0; - AppendByte ( (char)m_PacketID, Message, i ); - AppendByte ( m_WindowID, Message, i ); - AppendByte ( m_InventoryType, Message, i ); - AppendString16( m_WindowTitle, Message, i ); - AppendByte ( m_NumSlots, Message, i ); - - bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); - delete [] Message; - return RetVal; -} \ No newline at end of file + AppendByte (a_Data, m_PacketID); + AppendByte (a_Data, m_WindowID); + AppendByte (a_Data, m_InventoryType); + AppendString16(a_Data, m_WindowTitle); + AppendByte (a_Data, m_NumSlots); +} + + + + diff --git a/source/packets/cPacket_WindowOpen.h b/source/packets/cPacket_WindowOpen.h index dd9436cc5..f3b225639 100644 --- a/source/packets/cPacket_WindowOpen.h +++ b/source/packets/cPacket_WindowOpen.h @@ -1,7 +1,12 @@ + #pragma once #include "cPacket.h" + + + + class cPacket_WindowOpen : public cPacket { public: @@ -12,12 +17,16 @@ public: { m_PacketID = E_WINDOW_OPEN; } virtual cPacket* Clone() const { return new cPacket_WindowOpen(*this); } - bool Send(cSocket & a_Socket); + virtual void Serialize(AString & a_Data) const override; - char m_WindowID; - char m_InventoryType; - std::string m_WindowTitle; - char m_NumSlots; + char m_WindowID; + char m_InventoryType; + AString m_WindowTitle; + char m_NumSlots; static const unsigned int c_Size = 1 + 1 + 1 + 2 + 1; // + sizeof(string) -}; \ No newline at end of file +}; + + + + -- cgit v1.2.3