public static SerializedProperty Parse(BinaryReader reader)
        {
            SerializedProperty result;

            var propertyName = reader.ReadLengthPrefixedString();

            if (propertyName == "None")
            {
                return(null);
            }

            Trace.Assert(!String.IsNullOrEmpty(propertyName));

            var fieldType = reader.ReadLengthPrefixedString();
            var size      = reader.ReadInt32();
            var index     = reader.ReadInt32();

            int overhead;
            var before = reader.BaseStream.Position;

            switch (fieldType)
            {
            case ArrayProperty.TypeName:
                result = ArrayProperty.Parse(propertyName, index, reader, size, out overhead);
                break;

            case FloatProperty.TypeName:
                overhead = 1;
                result   = FloatProperty.Parse(propertyName, index, reader);
                break;

            case IntProperty.TypeName:
                overhead = 1;
                result   = IntProperty.Parse(propertyName, index, reader);
                break;

            case ByteProperty.TypeName:
                result = ByteProperty.Parse(propertyName, index, reader, out overhead);
                break;

            case EnumProperty.TypeName:
                result = EnumProperty.Parse(propertyName, index, reader, out overhead);
                break;

            case BoolProperty.TypeName:
                overhead = 2;
                result   = BoolProperty.Parse(propertyName, index, reader);
                break;

            case StrProperty.TypeName:
                overhead = 1;
                result   = StrProperty.Parse(propertyName, index, reader);
                break;

            case NameProperty.TypeName:
                overhead = 1;
                result   = NameProperty.Parse(propertyName, index, reader);
                break;

            case ObjectProperty.TypeName:
                overhead = 1;
                result   = ObjectProperty.Parse(propertyName, index, reader);
                break;

            case StructProperty.TypeName:
                result = StructProperty.Parse(propertyName, index, reader, size, out overhead);
                break;

            case MapProperty.TypeName:
                result = MapProperty.Parse(propertyName, index, reader, size, out overhead);
                break;

            case TextProperty.TypeName:
                overhead = 1;
                result   = TextProperty.Parse(propertyName, index, reader);
                break;

            default:
                throw new NotImplementedException(fieldType);
            }

            var after     = reader.BaseStream.Position;
            var readBytes = (int)(after - before - overhead);

            if (size != readBytes)
            {
                throw new InvalidOperationException($"Expected {size} bytes read but got {readBytes}");
            }

            return(result);
        }
Esempio n. 2
0
        public static ArrayProperty Parse(string propertyName, int index, BinaryReader reader, int size, out int overhead, int buildVersion)
        {
            var result = new ArrayProperty(propertyName, index)
            {
                Type = reader.ReadLengthPrefixedString()
            };

            overhead = result.Type.Length + 6;

            byte unk = reader.ReadByte();

            Trace.Assert(unk == 0);

            switch (result.Type)
            {
            case StructProperty.TypeName:
            {
                result.Elements.AddRange(StructProperty.ParseArray(reader, buildVersion));
            }
            break;

            case ObjectProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var obj1 = reader.ReadLengthPrefixedString();
                    var obj2 = reader.ReadLengthPrefixedString();
                    result.Elements.Add(new ObjectProperty($"Element {i}", obj1, obj2));
                }
            }
            break;

            case IntProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var value = reader.ReadInt32();
                    result.Elements.Add(new IntProperty($"Element {i}")
                        {
                            Value = value
                        });
                }
            }
            break;

            case ByteProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var value = reader.ReadByte();
                    result.Elements.Add(new ByteProperty($"Element {i}")
                        {
                            Type = "None", Value = value.ToString()
                        });
                }
            }
            break;

            case EnumProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var str = reader.ReadLengthPrefixedString();
                    result.Elements.Add(new EnumProperty($"Element {i}")
                        {
                            Type = str.Split(':')[0], Name = str
                        });
                }
            }
            break;

            case StrProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var str = reader.ReadLengthPrefixedString();
                    result.Elements.Add(new StrProperty($"Element {i}")
                        {
                            Value = str
                        });
                }
            }
            break;

            case FloatProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var value = reader.ReadSingle();
                    result.Elements.Add(new FloatProperty($"Element {i}")
                        {
                            Value = value
                        });
                }
            }
            break;

            case TextProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    result.Elements.Add(TextProperty.Parse($"Element {i}", 0, reader, buildVersion, true));
                }
            }
            break;

            case InterfaceProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var obj1 = reader.ReadLengthPrefixedString();
                    var obj2 = reader.ReadLengthPrefixedString();
                    result.Elements.Add(new InterfaceProperty($"Element {i}", obj1, obj2));
                }
            }
            break;

            default:
                throw new NotImplementedException($"Parsing an array of {result.Type} is not yet supported.");
            }

            return(result);
        }