diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-31 16:17:09 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-31 16:17:09 +0200 |
commit | f721ed0e3c6be33670fe330c029a2d4c3353f635 (patch) | |
tree | 5afad5a292171d546d9ded16de6151a014883d84 /src/packet | |
parent | 2017-05-28 (diff) | |
download | AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar.gz AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar.bz2 AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar.lz AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar.xz AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.tar.zst AltCraft-f721ed0e3c6be33670fe330c029a2d4c3353f635.zip |
Diffstat (limited to 'src/packet')
-rw-r--r-- | src/packet/Field.cpp | 5 | ||||
-rw-r--r-- | src/packet/Field.hpp | 10 | ||||
-rw-r--r-- | src/packet/FieldParser.cpp | 43 | ||||
-rw-r--r-- | src/packet/FieldParser.hpp | 18 | ||||
-rw-r--r-- | src/packet/Packet.cpp | 19 | ||||
-rw-r--r-- | src/packet/Packet.hpp | 2 | ||||
-rw-r--r-- | src/packet/PacketParser.cpp | 24 |
7 files changed, 61 insertions, 60 deletions
diff --git a/src/packet/Field.cpp b/src/packet/Field.cpp index 69402f1..9be2469 100644 --- a/src/packet/Field.cpp +++ b/src/packet/Field.cpp @@ -1,4 +1,3 @@ -#include <cmath> #include "Field.hpp" Field::Field() { @@ -59,7 +58,7 @@ int Field::GetVarInt() { void Field::SetVarInt(int value) { Clear(); - m_type = VarInt; + m_type = VarIntType; m_data = new byte[5]; m_dataLength = VarIntWrite(value, m_data); } @@ -252,7 +251,7 @@ void Field::SetDouble(double value) { size_t Field::GetFieldLength(FieldType type) { switch (type) { - case Unknown: + case UnknownType: return 0; case Boolean: return 1; diff --git a/src/packet/Field.hpp b/src/packet/Field.hpp index 8be9c9b..c33cd1c 100644 --- a/src/packet/Field.hpp +++ b/src/packet/Field.hpp @@ -2,8 +2,10 @@ #include <cstddef> #include <cstdint> +#include <cmath> #include <string> #include <vector> + #include "../utility/utility.h" #include "../utility/Vector.hpp" @@ -11,7 +13,7 @@ typedef unsigned char byte; typedef signed char sbyte; enum FieldType { - Unknown = 0, + UnknownType = 0, Boolean, //Bool Byte8_t, //int8_t UnsignedByte, //uint8_t @@ -28,7 +30,7 @@ enum FieldType { String = 100, //std::string Chat, //std::string - VarInt, //int32_t + VarIntType, //int32_t VarLong, //int64_t ChunkSection, //byte* EntityMetadata, //byte* @@ -55,7 +57,7 @@ public: void CopyToBuff(byte *ptr); - void SetRaw(byte *ptr, size_t len = 0, FieldType type = Unknown); + void SetRaw(byte *ptr, size_t len = 0, FieldType type = UnknownType); FieldType GetType(); @@ -114,6 +116,6 @@ public: private: size_t m_dataLength = 0; byte *m_data = nullptr; - FieldType m_type = Unknown; + FieldType m_type = UnknownType; std::vector<Field> m_childs; }; diff --git a/src/packet/FieldParser.cpp b/src/packet/FieldParser.cpp index 500a973..295e78f 100644 --- a/src/packet/FieldParser.cpp +++ b/src/packet/FieldParser.cpp @@ -2,26 +2,26 @@ Field FieldParser::Parse(FieldType type, byte *data, size_t len) { switch (type) { - case VarInt: + case VarIntType: return ParseVarInt(data, len); case Boolean: - return ParseBool(data, len); + return ParseBool(data); case String: - return ParseString(data, len); + return ParseString(data); case Long: - return ParseLong(data, len); + return ParseLong(data); case Int: - return ParseInt(data, len); + return ParseInt(data); case UnsignedByte: - return ParseUByte(data, len); + return ParseUByte(data); case Byte8_t: - return ParseByte(data, len); + return ParseByte(data); case Float: - return ParseFloat(data, len); + return ParseFloat(data); case Position: - return ParsePosition(data, len); + return ParsePosition(data); case Double: - return ParseDouble(data, len); + return ParseDouble(data); case ByteArray: return ParseByteArray(data, len); default: @@ -29,23 +29,23 @@ Field FieldParser::Parse(FieldType type, byte *data, size_t len) { } } -Field FieldParser::ParseString(byte *data, size_t len) { +Field FieldParser::ParseString(byte *data) { Field fLen = ParseVarInt(data, 0); Field f; f.SetRaw(data, fLen.GetLength() + fLen.GetVarInt(), String); return f; } -Field FieldParser::ParseBool(byte *data, size_t len) { +Field FieldParser::ParseBool(byte *data) { Field f; - f.SetRaw(data,1,Boolean); + f.SetRaw(data, 1, Boolean); return f; } Field FieldParser::ParseVarInt(byte *data, size_t len) { if (len != 0) { Field f; - f.SetRaw(data, len, VarInt); + f.SetRaw(data, len, VarIntType); return f; } int val = VarIntRead(data, len); @@ -54,43 +54,43 @@ Field FieldParser::ParseVarInt(byte *data, size_t len) { return f; } -Field FieldParser::ParseLong(byte *data, size_t len) { +Field FieldParser::ParseLong(byte *data) { Field f; f.SetRaw(data, 8, Long); return f; } -Field FieldParser::ParseInt(byte *data, size_t len) { +Field FieldParser::ParseInt(byte *data) { Field f; f.SetRaw(data, 4, Int); return f; } -Field FieldParser::ParseUByte(byte *data, size_t len) { +Field FieldParser::ParseUByte(byte *data) { Field f; f.SetRaw(data, 1, UnsignedByte); return f; } -Field FieldParser::ParseByte(byte *data, size_t len) { +Field FieldParser::ParseByte(byte *data) { Field f; f.SetRaw(data, 1, Byte8_t); return f; } -Field FieldParser::ParseFloat(byte *data, size_t len) { +Field FieldParser::ParseFloat(byte *data) { Field f; f.SetRaw(data, 4, Float); return f; } -Field FieldParser::ParsePosition(byte *data, size_t len) { +Field FieldParser::ParsePosition(byte *data) { Field f; f.SetRaw(data, 8, Position); return f; } -Field FieldParser::ParseDouble(byte *data, size_t len) { +Field FieldParser::ParseDouble(byte *data) { Field f; f.SetRaw(data, 8, Double); return f; @@ -101,6 +101,5 @@ Field FieldParser::ParseByteArray(byte *data, size_t len) { throw 119; Field f; f.SetRaw(data, len, Byte8_t); - //f.SetRaw(data, len, ByteArray); return f; } diff --git a/src/packet/FieldParser.hpp b/src/packet/FieldParser.hpp index 274ab9e..f256b34 100644 --- a/src/packet/FieldParser.hpp +++ b/src/packet/FieldParser.hpp @@ -6,25 +6,25 @@ class FieldParser { public: static Field ParseVarInt(byte *data, size_t len); - static Field ParseBool(byte *data, size_t len); + static Field ParseBool(byte *data); - static Field ParseString(byte *data, size_t len); + static Field ParseString(byte *data); static Field Parse(FieldType type, byte* data, size_t len=0); - static Field ParseLong(byte *data, size_t len); + static Field ParseLong(byte *data); - static Field ParseInt(byte *data, size_t len); + static Field ParseInt(byte *data); - static Field ParseUByte(byte *data, size_t len); + static Field ParseUByte(byte *data); - static Field ParseByte(byte *data, size_t len); + static Field ParseByte(byte *data); - static Field ParseFloat(byte *data, size_t len); + static Field ParseFloat(byte *data); - static Field ParsePosition(byte *data, size_t len); + static Field ParsePosition(byte *data); - static Field ParseDouble(byte *data, size_t len); + static Field ParseDouble(byte *data); static Field ParseByteArray(byte *data, size_t len); };
\ No newline at end of file diff --git a/src/packet/Packet.cpp b/src/packet/Packet.cpp index 695e371..68cc3c3 100644 --- a/src/packet/Packet.cpp +++ b/src/packet/Packet.cpp @@ -23,9 +23,9 @@ void Packet::swap(Packet &other) { void Packet::CopyToBuff(byte *ptr) { m_fields[0].SetVarInt(GetLength() - m_fields[0].GetLength()); - for (int i = 0; i < m_fields.size(); i++) { - m_fields[i].CopyToBuff(ptr); - ptr += m_fields[i].GetLength(); + for (auto &it:m_fields) { + it.CopyToBuff(ptr); + ptr += it.GetLength(); } } @@ -44,28 +44,29 @@ void Packet::ParseField(FieldType type, size_t len) { } Packet::Packet(byte *data) { - Field fLen = FieldParser::Parse(VarInt, data); + Field fLen = FieldParser::Parse(VarIntType, data); data += fLen.GetLength(); - Field fId = FieldParser::Parse(VarInt, data); + Field fId = FieldParser::Parse(VarIntType, data); data += fId.GetLength(); m_dataLength = fLen.GetVarInt() - fId.GetLength(); m_data = new byte[m_dataLength]; - std::copy(data,data+m_dataLength,m_data); + std::copy(data, data + m_dataLength, m_data); m_parsePtr = m_data; m_fields.push_back(fLen); m_fields.push_back(fId); } Field &Packet::GetField(int id) { - if (id < -2 || id >= m_fields.size() - 2) + if (id < -2 || id >= (int) m_fields.size() - 2) throw 111; return m_fields[id + 2]; } size_t Packet::GetLength() { size_t len = 0; - for (int i = 0; i < m_fields.size(); i++) - len += m_fields[i].GetLength(); + for (auto &it:m_fields) { + len += it.GetLength(); + } return len + m_dataLength; } diff --git a/src/packet/Packet.hpp b/src/packet/Packet.hpp index 67e95e5..68a5d5e 100644 --- a/src/packet/Packet.hpp +++ b/src/packet/Packet.hpp @@ -30,7 +30,7 @@ enum PacketsClientBound{ ChatMessage, MultiBlockChange, ConfirmTransaction, - CloseWindow, + CloseWindowEvent, OpenWindow, WindowItems, WindowProperty, diff --git a/src/packet/PacketParser.cpp b/src/packet/PacketParser.cpp index 488c812..a609011 100644 --- a/src/packet/PacketParser.cpp +++ b/src/packet/PacketParser.cpp @@ -21,7 +21,8 @@ void PacketParser::Parse(Packet &packet, ConnectionState state, bool ClientBound } void PacketParser::ParseServerBound(Packet &packet, ConnectionState state) { - throw 107; + if (packet.GetLength() != state) + throw 107; } void PacketParser::ParseLogin(Packet &packet) { @@ -32,9 +33,8 @@ void PacketParser::ParseLogin(Packet &packet) { case 0x02: ParseLogin0x02(packet); break; - default: - { - int i = packet.GetId(); + default: { + //throw 112; } } @@ -94,7 +94,7 @@ void PacketParser::ParsePlay0x23(Packet &packet) { } void PacketParser::ParsePlay0x1F(Packet &packet) { - packet.ParseField(VarInt); + packet.ParseField(VarIntType); } void PacketParser::ParsePlay0x0D(Packet &packet) { @@ -118,7 +118,7 @@ void PacketParser::ParsePlay0x2E(Packet &packet) { packet.ParseField(Float); packet.ParseField(Float); packet.ParseField(Byte8_t); - packet.ParseField(VarInt); + packet.ParseField(VarIntType); } void PacketParser::ParsePlay0x1A(Packet &packet) { @@ -129,19 +129,19 @@ void PacketParser::ParsePlay0x20(Packet &packet) { packet.ParseField(Int); packet.ParseField(Int); packet.ParseField(Boolean); - packet.ParseField(VarInt); - packet.ParseField(VarInt); + packet.ParseField(VarIntType); + packet.ParseField(VarIntType); packet.ParseField(ByteArray, packet.GetField(4).GetVarInt()); - packet.ParseField(VarInt); + packet.ParseField(VarIntType); //packet.ParseField(NbtTag); //packet.GetField(7).SetArray(packet.GetField(6).GetVarInt()); } void PacketParser::ParsePlay0x07(Packet &packet) { - packet.ParseField(VarInt); + packet.ParseField(VarIntType); packet.AddField(Field()); - for (int i=0;i<packet.GetField(0).GetVarInt();i++){ + for (int i = 0; i < packet.GetField(0).GetVarInt(); i++) { packet.ParseFieldArray(packet.GetField(1), String, 0); - packet.ParseFieldArray(packet.GetField(1), VarInt, 0); + packet.ParseFieldArray(packet.GetField(1), VarIntType, 0); } } |