Ejemplo n.º 1
0
        public Task DispatchIncomingPacket(UncompressedPacket packet)
        {
            switch (status)
            {
            case SessionStatus.Status:
            case SessionStatus.Login:
            case SessionStatus.Play:
            {
                IPacketSerializable pack = null;

                foreach (var it in list[(int)status])
                {
                    if (it.packetId == packet.packetId)
                    {
                        pack = it.Clone() as IPacketSerializable;
                        break;
                    }
                }

                if (pack != null)
                {
                    try
                    {
                        using (var br = new NetworkReader(packet.data))
                            pack.Deserialize(br);

                        this.InvokeDispatchIncomingPacket(status, pack);
                    }
                    catch (System.NotImplementedException e)
                    {
                        UnityEngine.Debug.LogException(e);
                    }
                }
                else
                {
                    this.InvokeDispatchInvalidPacket(status, packet);
                }
            }
            break;
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        public static bool Deserialize(Type type, byte[] data, ref int position, out object value)
        {
            if (type == typeof(byte))
            {
                value = data[position];
                position++;
            }
            else if (type.GetInterfaces().Contains(typeof(IPacketSerializable)))
            {
                IPacketSerializable serializable = (IPacketSerializable)Activator.CreateInstance(type);
                serializable.Deserialize(data, ref position);
                value = serializable;
            }
            else if (type.IsEnum)
            {
                EnumPackageType packageType = (EnumPackageType)type.GetCustomAttribute(typeof(EnumPackageType), false);
                if (packageType == null)
                {
                    Console.WriteLine("Error: Enum \"" + type.FullName + "\" doesn't have EnumPackageType attribute");
                    value = null;
                    return(false);
                }

                object obj;
                if (!Deserialize(packageType.type, data, ref position, out obj))
                {
                    value = null;
                    return(false);
                }

                value = DynamicCast(DynamicCast(obj, type.GetEnumUnderlyingType()), type);
            }
            else if (type == typeof(bool))
            {
                value = BitConverter.ToBoolean(data, position);
                position++;
            }
            else if (type == typeof(short))
            {
                value     = BitConverter.ToInt16(data, position);
                position += 2;
            }
            else if (type == typeof(ushort))
            {
                value     = BitConverter.ToUInt16(data, position);
                position += 2;
            }
            else if (type == typeof(int))
            {
                value     = BitConverter.ToInt32(data, position);
                position += 4;
            }
            else if (type == typeof(uint))
            {
                value     = BitConverter.ToUInt32(data, position);
                position += 4;
            }
            else if (type == typeof(long))
            {
                value     = BitConverter.ToInt64(data, position);
                position += 8;
            }
            else if (type == typeof(ulong))
            {
                value     = BitConverter.ToUInt64(data, position);
                position += 8;
            }
            else if (type == typeof(float))
            {
                value     = BitConverter.ToSingle(data, position);
                position += 4;
            }
            else if (type == typeof(double))
            {
                value     = BitConverter.ToDouble(data, position);
                position += 8;
            }
            else
            {
                value = null;
                return(false);
            }

            return(true);
        }