Beispiel #1
0
        public static byte[] Construct(ushort Header, 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;

            if (IsAncient && Destination == HDestinations.Server)
            {
                Buffer.Add(64);
            }
            Buffer.AddRange(IsAncient ? Ancient.CypherShort(Header) : BigEndian.CypherShort(Header));

            Buffer.AddRange(ConstructBody(Destination, Protocol, Chunks));

            if (!IsAncient || Destination == HDestinations.Server)
            {
                Buffer.InsertRange(IsAncient ? 1 : 0, IsAncient ? Ancient.CypherShort((ushort)(Buffer.Count - 1)) : BigEndian.CypherInt(Buffer.Count));
            }
            else if (Buffer[Buffer.Count - 1] != 1)
            {
                Buffer.Add(1);
            }

            return(Buffer.ToArray());
        }
Beispiel #2
0
        public static byte[] Construct(ushort header, params object[] chunks)
        {
            byte[] body = chunks != null && chunks.Length > 0 ? Encode(chunks) : new byte[0];
            byte[] data = new byte[6 + body.Length];

            Buffer.BlockCopy(BigEndian.CypherInt(body.Length + 2), 0, data, 0, 4);
            Buffer.BlockCopy(BigEndian.CypherShort(header), 0, data, 4, 2);
            Buffer.BlockCopy(body, 0, data, 6, body.Length);

            return(data);
        }
Beispiel #3
0
        public static byte[] Encode(params object[] chunks)
        {
            if (chunks == null || chunks.Length < 1)
            {
                throw new NullReferenceException();
            }

            var buffer = new List <byte>();

            for (int i = 0; i < chunks.Length; i++)
            {
                object chunk = chunks[i];
                if (chunk == null)
                {
                    throw new NullReferenceException();
                }

                switch (Type.GetTypeCode(chunk.GetType()))
                {
                case TypeCode.Byte: buffer.Add((byte)chunk); break;

                case TypeCode.Boolean: buffer.Add(Convert.ToByte((bool)chunk)); break;

                case TypeCode.Int32: buffer.AddRange(BigEndian.CypherInt((int)chunk)); break;

                case TypeCode.UInt16: buffer.AddRange(BigEndian.CypherShort((ushort)chunk)); break;

                default:
                case TypeCode.String:
                {
                    byte[] data = chunk as byte[];
                    if (data == null)
                    {
                        string value = chunk.ToString();
                        value = value.Replace("\\r", "\r");
                        value = value.Replace("\\n", "\n");

                        data = new byte[2 + Encoding.UTF8.GetByteCount(value)];
                        Buffer.BlockCopy(BigEndian.CypherShort((ushort)(data.Length - 2)), 0, data, 0, 2);
                        Buffer.BlockCopy(Encoding.UTF8.GetBytes(value), 0, data, 2, data.Length - 2);
                    }
                    buffer.AddRange(data);
                    break;
                }
                }
            }
            return(buffer.ToArray());
        }
Beispiel #4
0
        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());
        }