Beispiel #1
0
        public static SetProperty Parse(string propertyName, int index, BinaryReader reader, int size, out int overhead)
        {
            var result = new SetProperty(propertyName, index)
            {
                Type = reader.ReadLengthPrefixedString()
            };

            overhead = result.Type.Length + 6;

            var unk = reader.ReadByte();

            Trace.Assert(unk == 0);

            var unk2 = reader.ReadInt32();

            Trace.Assert(unk2 == 0);

            switch (result.Type)
            {
            case NameProperty.TypeName:
            {
                var count = reader.ReadInt32();
                for (var i = 0; i < count; i++)
                {
                    var value = reader.ReadLengthPrefixedString();
                    result.Elements.Add(new NameProperty($"Element {i}")
                        {
                            Value = value
                        });
                }
            }
            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;

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


            return(result);
        }
        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;

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

            case Int64Property.TypeName:
                overhead = 1;
                result   = Int64Property.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);
        }