private Property.IVariant DeserializeVariant(MemoryStream input)
        {
            var endian = this.Endian;
            var type   = input.ReadValueEnum <VariantType>();

            switch (type)
            {
            case VariantType.Integer:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.IntegerVariant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.Value = input.ReadValueS32(endian);
                return(variant);
            }

            case VariantType.Float:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.FloatVariant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.Value = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.String:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.StringVariant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.StringVariant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.Value = input.ReadStringZ(Encoding.ASCII);
                return(variant);
            }

            case VariantType.Vector2:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.Vector2Variant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector2Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Vector3:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.Vector3Variant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector3Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                variant.Z = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Vector4:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.Vector4Variant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector4Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                variant.Z = input.ReadValueF32(endian);
                variant.W = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Matrix3x3:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.Matrix3x3Variant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Matrix3x3Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.M11 = input.ReadValueF32(endian);
                variant.M12 = input.ReadValueF32(endian);
                variant.M13 = input.ReadValueF32(endian);
                variant.M21 = input.ReadValueF32(endian);
                variant.M22 = input.ReadValueF32(endian);
                variant.M23 = input.ReadValueF32(endian);
                variant.M31 = input.ReadValueF32(endian);
                variant.M32 = input.ReadValueF32(endian);
                variant.M33 = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Matrix4x4:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.Matrix4x4Variant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Matrix4x4Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.M11 = input.ReadValueF32(endian);
                variant.M12 = input.ReadValueF32(endian);
                variant.M13 = input.ReadValueF32(endian);
                variant.M14 = input.ReadValueF32(endian);
                variant.M21 = input.ReadValueF32(endian);
                variant.M22 = input.ReadValueF32(endian);
                variant.M23 = input.ReadValueF32(endian);
                variant.M24 = input.ReadValueF32(endian);
                variant.M31 = input.ReadValueF32(endian);
                variant.M32 = input.ReadValueF32(endian);
                variant.M33 = input.ReadValueF32(endian);
                variant.M34 = input.ReadValueF32(endian);
                variant.M41 = input.ReadValueF32(endian);
                variant.M42 = input.ReadValueF32(endian);
                variant.M43 = input.ReadValueF32(endian);
                variant.M44 = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Integers:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.IntegersVariant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                var size = input.ReadValueS32(endian);
                if ((size % 4) != 0)
                {
                    throw new FormatException();
                }

                var variant = new Property.IntegersVariant();
                int count   = size / 4;
                variant.Values.Clear();
                for (int i = 0; i < count; i++)
                {
                    variant.Values.Add(input.ReadValueS32(endian));
                }
                return(variant);
            }

            case VariantType.Floats:
            {
                var offset = input.ReadValueS32(endian);
                if (offset == -1)
                {
                    return(new Property.FloatsVariant());
                }
                input.Seek(offset, SeekOrigin.Begin);

                var size = input.ReadValueS32(endian);
                if ((size % 4) != 0)
                {
                    throw new FormatException();
                }

                var variant = new Property.FloatsVariant();
                int count   = size / 4;
                variant.Values.Clear();
                for (int i = 0; i < count; i++)
                {
                    variant.Values.Add(input.ReadValueF32(endian));
                }
                return(variant);
            }
            }

            throw new FormatException("unsupported variant type " + type.ToString());
        }
Пример #2
0
        private Property.IVariant DeserializeVariant(Stream input)
        {
            var endian = this.Endian;
            var type   = (VariantType)input.ReadValueU8();

            switch (type)
            {
            case VariantType.Integer:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.IntegerVariant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.Value = input.ReadValueS32(endian);
                return(variant);
            }

            case VariantType.Float:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.FloatVariant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.Value = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.String:
            {
                var    variant = new Property.StringVariant();
                ushort length  = input.ReadValueU16(endian);
                variant.Value = input.ReadString(length, true, Encoding.ASCII);
                return(variant);
            }

            case VariantType.Vector2:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector2Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Vector3:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector3Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                variant.Z = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Vector4:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Vector4Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.X = input.ReadValueF32(endian);
                variant.Y = input.ReadValueF32(endian);
                variant.Z = input.ReadValueF32(endian);
                variant.W = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Matrix3x3:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Matrix3x3Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.M11 = input.ReadValueF32(endian);
                variant.M12 = input.ReadValueF32(endian);
                variant.M13 = input.ReadValueF32(endian);
                variant.M21 = input.ReadValueF32(endian);
                variant.M22 = input.ReadValueF32(endian);
                variant.M23 = input.ReadValueF32(endian);
                variant.M31 = input.ReadValueF32(endian);
                variant.M32 = input.ReadValueF32(endian);
                variant.M33 = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Matrix4x3:
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                var variant = new Property.Matrix4x3Variant();
                // ReSharper restore UseObjectOrCollectionInitializer
                variant.M11 = input.ReadValueF32(endian);
                variant.M12 = input.ReadValueF32(endian);
                variant.M13 = input.ReadValueF32(endian);
                variant.M21 = input.ReadValueF32(endian);
                variant.M22 = input.ReadValueF32(endian);
                variant.M23 = input.ReadValueF32(endian);
                variant.M31 = input.ReadValueF32(endian);
                variant.M32 = input.ReadValueF32(endian);
                variant.M33 = input.ReadValueF32(endian);
                variant.M41 = input.ReadValueF32(endian);
                variant.M42 = input.ReadValueF32(endian);
                variant.M43 = input.ReadValueF32(endian);
                return(variant);
            }

            case VariantType.Integers:
            {
                var variant = new Property.IntegersVariant();
                int count   = input.ReadValueS32(endian);
                variant.Values.Clear();
                for (int i = 0; i < count; i++)
                {
                    variant.Values.Add(input.ReadValueS32(endian));
                }
                return(variant);
            }

            case VariantType.Floats:
            {
                var variant = new Property.FloatsVariant();
                int count   = input.ReadValueS32(endian);
                variant.Values.Clear();
                for (int i = 0; i < count; i++)
                {
                    variant.Values.Add(input.ReadValueF32(endian));
                }
                return(variant);
            }
            }

            throw new FormatException("unsupported variant type " + type.ToString());
        }