public int ReadInt(ref int Index) { if (IsCorrupted) { return(0); } switch (Protocol) { case HProtocols.Modern: return(BigEndian.DecypherInt(Body[Index++], Body[Index++], Body[Index++], Body[Index++])); case HProtocols.Ancient: { int Value = Ancient.DecypherInt(Body, Index); Index += Ancient.CypherInt(Value).Length; return(Value); } default: return(0); } }
public static byte[] ConstructBody(HDestination destination, HProtocol protocol, params object[] chunks) { var buffer = new List <byte>(); bool isAncient = (protocol == HProtocol.Ancient); for (int i = 0; i < chunks.Length; i++) { object chunk = chunks[i]; if (chunk == null) { throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i)); } var data = chunk as byte[]; if (data != null) { buffer.AddRange(data); } else { switch (Type.GetTypeCode(chunk.GetType())) { case TypeCode.Int32: { var value = (int)chunk; buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherInt(value) : Modern.CypherInt(value)); break; } case TypeCode.Boolean: { var value = (bool)chunk; buffer.Add(isAncient ? (byte)(value ? 73 : 72) : Convert.ToByte(value)); break; } case TypeCode.Byte: { var value = (byte)chunk; buffer.Add(value); break; } default: { string value = chunk.ToString(); if (!isAncient || destination == HDestination.Server) { ushort valueLength = (ushort)value.Length; buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(valueLength) : Modern.CypherShort(valueLength)); buffer.AddRange(Encoding.Default.GetBytes(value)); } else { buffer.AddRange(Encoding.Default.GetBytes(value)); buffer.Add(2); } break; } } } } return(buffer.ToArray()); }
public static byte[] ConstructBody(HDestinations Destination, HProtocols Protocol, params object[] Chunks) { if (Protocol == HProtocols.Unknown) { throw new Exception("You must specify a supported HProtocols value for this method. ( Ancient / Modern )"); } if (Protocol == HProtocols.Ancient && Destination == HDestinations.Unknown) { throw new Exception("Cannot construct the body of an Ancient type packet without a valid HDestinations value. ( Client / Server )"); } List <byte> Buffer = new List <byte>(); bool IsAncient = Protocol == HProtocols.Ancient; for (int i = 0; i < Chunks.Length; i++) { object Chunk = Chunks[i]; if (Chunk == null) { throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i)); } if (Chunk is byte[]) { Buffer.AddRange((byte[])Chunk); } else { switch (Type.GetTypeCode(Chunk.GetType())) { case TypeCode.Int32: { int Value = (int)Chunk; Buffer.AddRange(IsAncient ? Ancient.CypherInt(Value) : BigEndian.CypherInt(Value)); break; } default: case TypeCode.String: { string Value = Chunk.ToString(); if (!IsAncient || Destination == HDestinations.Server) { Buffer.AddRange(IsAncient ? Ancient.CypherShort((ushort)Value.Length) : BigEndian.CypherShort((ushort)Value.Length)); Buffer.AddRange(Encoding.Default.GetBytes(Value)); } else { Buffer.AddRange(Encoding.Default.GetBytes(Value)); Buffer.Add(2); } break; } case TypeCode.Boolean: { bool Value = (bool)Chunk; Buffer.Add(IsAncient ? (byte)(Value ? 73 : 72) : Convert.ToByte(Value)); break; } case TypeCode.Byte: { byte Value = (byte)Chunk; Buffer.Add(Value); break; } } } } return(Buffer.ToArray()); }