Exemple #1
0
        public PacketLogEntry Read()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            try
            {
                DateTime stamp;

                try
                {
                    stamp = DateTimeOffset.FromUnixTimeMilliseconds(
                        _reader.ReadInt64()).LocalDateTime;
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new InvalidDataException();
                }

                var id = _reader.ReadInt32();

                if (!Servers.ContainsKey(id))
                {
                    throw new InvalidDataException();
                }

                var direction = (Direction)_reader.ReadByte();

                if (!Enum.IsDefined(typeof(Direction), direction))
                {
                    throw new InvalidDataException();
                }

                var opCode = _reader.ReadUInt16();

                if (!Messages.Game.OpCodeToName.ContainsKey(opCode))
                {
                    throw new InvalidDataException();
                }

                var length  = _reader.ReadUInt16();
                var payload = _reader.ReadBytes(length);

                return(new PacketLogEntry(stamp, id, direction, opCode, payload));
            }
            catch (EndOfStreamException)
            {
                return(null);
            }
        }
        static object DeserializePrimitive(TeraBinaryReader reader, Type type)
        {
            object value;

            if (type == typeof(bool))
            {
                value = reader.ReadBoolean();
            }
            else if (type == typeof(byte))
            {
                value = reader.ReadByte();
            }
            else if (type == typeof(sbyte))
            {
                value = reader.ReadSByte();
            }
            else if (type == typeof(ushort))
            {
                value = reader.ReadUInt16();
            }
            else if (type == typeof(short))
            {
                value = reader.ReadInt16();
            }
            else if (type == typeof(uint))
            {
                value = reader.ReadUInt32();
            }
            else if (type == typeof(int))
            {
                value = reader.ReadInt32();
            }
            else if (type == typeof(ulong))
            {
                value = reader.ReadUInt64();
            }
            else if (type == typeof(long))
            {
                value = reader.ReadInt64();
            }
            else if (type == typeof(float))
            {
                value = reader.ReadSingle();
            }
            else if (type == typeof(Vector3))
            {
                value = reader.ReadVector3();
            }
            else if (type == typeof(EntityId))
            {
                value = reader.ReadEntityId();
            }
            else if (type == typeof(SkillId))
            {
                value = reader.ReadSkillId();
            }
            else if (type == typeof(Angle))
            {
                value = reader.ReadAngle();
            }
            else
            {
                throw Assert.Unreachable();
            }

            return(value);
        }