public override void Serialize(BinaryWriter writer, bool writeHeader = true)
        {
            if (writeHeader)
            {
                base.Serialize(writer, writeHeader);
            }

            using (var ms = new MemoryStream())
                using (var msWriter = new BinaryWriter(ms))
                {
                    switch (Type)
                    {
                    case StructProperty.TypeName:
                    {
                        StructProperty.SerializeArray(msWriter, Elements.Cast <StructProperty>().ToArray());
                    }
                    break;

                    case ObjectProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <ObjectProperty>())
                        {
                            msWriter.WriteLengthPrefixedString(prop.Root);
                            msWriter.WriteLengthPrefixedString(prop.Name);
                        }
                    }
                    break;

                    case IntProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <IntProperty>())
                        {
                            msWriter.Write(prop.Value);
                        }
                    }
                    break;

                    default:
                        throw new NotImplementedException();
                    }

                    var bytes = ms.ToArray();

                    writer.Write(bytes.Length);
                    writer.Write(Index);

                    writer.WriteLengthPrefixedString(Type);
                    writer.Write((byte)0);

                    writer.Write(bytes);
                }
        }
Exemplo n.º 2
0
        public override void Serialize(BinaryWriter writer, bool writeHeader = true)
        {
            if (writeHeader)
            {
                base.Serialize(writer, writeHeader);
            }

            using (var ms = new MemoryStream())
                using (var msWriter = new BinaryWriter(ms))
                {
                    switch (Type)
                    {
                    case StructProperty.TypeName:
                    {
                        StructProperty.SerializeArray(msWriter, Elements.Cast <StructProperty>().ToArray());
                    }
                    break;

                    case ObjectProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <ObjectProperty>())
                        {
                            msWriter.WriteLengthPrefixedString(prop.LevelName);
                            msWriter.WriteLengthPrefixedString(prop.PathName);
                        }
                    }
                    break;

                    case IntProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <IntProperty>())
                        {
                            msWriter.Write(prop.Value);
                        }
                    }
                    break;

                    case ByteProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <ByteProperty>())
                        {
                            msWriter.Write(byte.Parse(prop.Value));
                        }
                    }
                    break;

                    case EnumProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <EnumProperty>())
                        {
                            msWriter.WriteLengthPrefixedString(prop.Name);
                        }
                    }
                    break;

                    case StrProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <StrProperty>())
                        {
                            msWriter.WriteLengthPrefixedString(prop.Value);
                        }
                    }
                    break;

                    case FloatProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <FloatProperty>())
                        {
                            msWriter.Write(prop.Value);
                        }
                    }
                    break;

                    case TextProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <TextProperty>())
                        {
                            prop.Serialize(msWriter, false);
                        }
                    }
                    break;

                    case InterfaceProperty.TypeName:
                    {
                        msWriter.Write(Elements.Count);
                        foreach (var prop in Elements.Cast <InterfaceProperty>())
                        {
                            msWriter.WriteLengthPrefixedString(prop.LevelName);
                            msWriter.WriteLengthPrefixedString(prop.PathName);
                        }
                    }
                    break;

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

                    var bytes = ms.ToArray();

                    writer.Write(bytes.Length);
                    writer.Write(Index);

                    writer.WriteLengthPrefixedString(Type);
                    writer.Write((byte)0);

                    writer.Write(bytes);
                }
        }