From 01a61a9962d76e0cd68054f10d399a95bce934a1 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Tue, 1 Aug 2023 00:01:56 -0500 Subject: [PATCH] Fixed some bugs around generating logic for reading/writing lists. --- .../RSequenceLengthSourceAttributeTests.cs | 17 ++ .../RSequenceLengthSourceAttributeTests.cs | 176 ++++++++++++++++++ ...RSequenceUntilEndOfStreamAttributeTests.cs | 153 ++------------- .../WLengthOfSequenceAttributeTests.cs | 8 +- .../SequenceLengthSourceGeneratorTests.cs | 8 +- Schema/lib/System/IO/Endianness.cs | 1 + Schema/lib/System/IO/IDataReader.cs | 4 +- .../IO/reader/EndianBinaryBufferedStream.cs | 10 +- .../System/IO/reader/EndianBinaryReader.cs | 1 + .../reader/EndianBinaryReader_Endianness.cs | 11 +- .../IO/reader/EndianBinaryReader_Numbers.cs | 2 +- .../IO/reader/EndianBinaryReader_Position.cs | 2 + .../EndianBinaryReader_SpecialNumbers.cs | 4 - .../IO/reader/EndianBinaryReader_Strings.cs | 8 +- .../System/IO/writer/EndianBinaryWriter.cs | 9 +- .../IO/writer/EndianBinaryWriter_Delayed.cs | 18 +- .../writer/EndianBinaryWriter_Endianness.cs | 18 +- .../IO/writer/EndianBinaryWriter_Numbers.cs | 29 +++ .../IO/writer/EndianBinaryWriter_Position.cs | 9 +- .../EndianBinaryWriter_SpecialNumbers.cs | 28 ++- .../IO/writer/EndianBinaryWriter_Strings.cs | 26 ++- .../System/IO/writer/IEndianBinaryWriter.cs | 99 +++++----- .../src/binary/BinarySchemaStructureParser.cs | 10 + .../src/binary/attributes/BMemberAttribute.cs | 2 +- .../text/BinarySchemaReaderGenerator.cs | 31 ++- .../text/BinarySchemaWriterGenerator.cs | 74 +++----- 26 files changed, 467 insertions(+), 291 deletions(-) create mode 100644 Schema Build Tests/attributes/sequence/RSequenceLengthSourceAttributeTests.cs create mode 100644 Schema Tests/binary/attributes/sequence/RSequenceLengthSourceAttributeTests.cs diff --git a/Schema Build Tests/attributes/sequence/RSequenceLengthSourceAttributeTests.cs b/Schema Build Tests/attributes/sequence/RSequenceLengthSourceAttributeTests.cs new file mode 100644 index 0000000..4e61bf5 --- /dev/null +++ b/Schema Build Tests/attributes/sequence/RSequenceLengthSourceAttributeTests.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +using schema.binary; +using schema.binary.attributes; + +namespace build.attributes.sequence { + public partial class RSequenceLengthSourceAttributeTests { + [BinarySchema] + public partial class ReadonlyListClass : IBinaryConvertible { + [WLengthOfSequence(nameof(Values))] + private uint count_; + + [RSequenceLengthSource(nameof(count_))] + public readonly List Values = new(); + } + } +} \ No newline at end of file diff --git a/Schema Tests/binary/attributes/sequence/RSequenceLengthSourceAttributeTests.cs b/Schema Tests/binary/attributes/sequence/RSequenceLengthSourceAttributeTests.cs new file mode 100644 index 0000000..8dd34eb --- /dev/null +++ b/Schema Tests/binary/attributes/sequence/RSequenceLengthSourceAttributeTests.cs @@ -0,0 +1,176 @@ +using NUnit.Framework; + + +namespace schema.binary.attributes { + internal class RSequenceUntilEndOfStreamAttributeTests { + [Test] + public void TestByteArrayUntilEndOfStream() { + BinarySchemaTestUtil.AssertGenerated(@" +using schema.binary; +using schema.binary.attributes; + +namespace foo.bar { + [BinarySchema] + public partial class Wrapper : IBinaryConvertible { + [RSequenceUntilEndOfStream] + public byte[] Field { get; set; } + } +}", + @"using System; +using System.Collections.Generic; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Read(IEndianBinaryReader er) { + this.Field = er.ReadBytes(er.Length - er.Position); + } + } +} +", + @"using System; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Write(ISubEndianBinaryWriter ew) { + ew.WriteBytes(this.Field); + } + } +} +"); + } + + [Test] + public void TestByteListUntilEndOfStream() { + BinarySchemaTestUtil.AssertGenerated(@" +using System.Collections.Generic; + +using schema.binary; +using schema.binary.attributes; + +namespace foo.bar { + [BinarySchema] + public partial class Wrapper : IBinaryConvertible { + [RSequenceUntilEndOfStream] + public List Field { get; } = new(); + } +}", + @"using System; +using System.Collections.Generic; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Read(IEndianBinaryReader er) { + { + this.Field.Clear(); + while (!er.Eof) { + this.Field.Add(er.ReadByte()); + } + } + } + } +} +", + @"using System; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Write(ISubEndianBinaryWriter ew) { + for (var i = 0; i < this.Field.Count; ++i) { + ew.WriteByte(this.Field[i]); + } + } + } +} +"); + } + + [Test] + public void TestIntArrayUntilEndOfStream() { + BinarySchemaTestUtil.AssertGenerated(@" +using schema.binary; +using schema.binary.attributes; + +namespace foo.bar { + [BinarySchema] + public partial class Wrapper : IBinaryConvertible { + [RSequenceUntilEndOfStream] + public int[] Field { get; set; } + } +}", + @"using System; +using System.Collections.Generic; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Read(IEndianBinaryReader er) { + this.Field = er.ReadInt32s((er.Length - er.Position) / 4); + } + } +} +", + @"using System; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Write(ISubEndianBinaryWriter ew) { + ew.WriteInt32s(this.Field); + } + } +} +"); + } + + [Test] + public void TestIntListUntilEndOfStream() { + BinarySchemaTestUtil.AssertGenerated(@" +using System.Collections.Generic; + +using schema.binary; +using schema.binary.attributes; + +namespace foo.bar { + [BinarySchema] + public partial class Wrapper : IBinaryConvertible { + [RSequenceUntilEndOfStream] + public List Field { get; } = new(); + } +}", + @"using System; +using System.Collections.Generic; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Read(IEndianBinaryReader er) { + { + this.Field.Clear(); + while (!er.Eof) { + this.Field.Add(er.ReadInt32()); + } + } + } + } +} +", + @"using System; +using System.IO; + +namespace foo.bar { + public partial class Wrapper { + public void Write(ISubEndianBinaryWriter ew) { + for (var i = 0; i < this.Field.Count; ++i) { + ew.WriteInt32(this.Field[i]); + } + } + } +} +"); + } + } +} \ No newline at end of file diff --git a/Schema Tests/binary/attributes/sequence/RSequenceUntilEndOfStreamAttributeTests.cs b/Schema Tests/binary/attributes/sequence/RSequenceUntilEndOfStreamAttributeTests.cs index 7e08d3f..f30e8f9 100644 --- a/Schema Tests/binary/attributes/sequence/RSequenceUntilEndOfStreamAttributeTests.cs +++ b/Schema Tests/binary/attributes/sequence/RSequenceUntilEndOfStreamAttributeTests.cs @@ -2,155 +2,35 @@ namespace schema.binary.attributes { - internal class RSequenceUntilEndOfStreamAttributeTests { + internal class RSequenceLengthSourceAttributeTests { [Test] - public void TestByteArrayUntilEndOfStream() { - BinarySchemaTestUtil.AssertGenerated(@" -using schema.binary; -using schema.binary.attributes; - -namespace foo.bar { - [BinarySchema] - public partial class Wrapper : IBinaryConvertible { - [RSequenceUntilEndOfStream] - public byte[] Field { get; set; } - } -}", - @"using System; -using System.Collections.Generic; -using System.IO; - -namespace foo.bar { - public partial class Wrapper { - public void Read(IEndianBinaryReader er) { - this.Field = er.ReadBytes(er.Length - er.Position); - } - } -} -", - @"using System; -using System.IO; - -namespace foo.bar { - public partial class Wrapper { - public void Write(ISubEndianBinaryWriter ew) { - ew.WriteBytes(this.Field); - } - } -} -"); - } - - [Test] - public void TestByteListUntilEndOfStream() { + public void TestReadonlyList() { BinarySchemaTestUtil.AssertGenerated(@" using System.Collections.Generic; - using schema.binary; using schema.binary.attributes; namespace foo.bar { [BinarySchema] - public partial class Wrapper : IBinaryConvertible { - [RSequenceUntilEndOfStream] - public List Field { get; } = new(); - } -}", - @"using System; -using System.Collections.Generic; -using System.IO; + public partial class ReadonlyListClass : IBinaryConvertible { + [WLengthOfSequence(nameof(Values))] + private uint count_; -namespace foo.bar { - public partial class Wrapper { - public void Read(IEndianBinaryReader er) { - { - this.Field.Clear(); - while (!er.Eof) { - this.Field.Add(er.ReadByte()); - } - } - } - } -} -", - @"using System; -using System.IO; - -namespace foo.bar { - public partial class Wrapper { - public void Write(ISubEndianBinaryWriter ew) { - ew.WriteBytes(this.Field); - } - } -} -"); - } - - [Test] - public void TestIntArrayUntilEndOfStream() { - BinarySchemaTestUtil.AssertGenerated(@" -using schema.binary; -using schema.binary.attributes; - -namespace foo.bar { - [BinarySchema] - public partial class Wrapper : IBinaryConvertible { - [RSequenceUntilEndOfStream] - public int[] Field { get; set; } + [RSequenceLengthSource(nameof(count_))] + public readonly List Values = new(); } }", @"using System; -using System.Collections.Generic; using System.IO; +using schema.util.sequences; namespace foo.bar { - public partial class Wrapper { + public partial class ReadonlyListClass { public void Read(IEndianBinaryReader er) { - this.Field = er.ReadInt32s((er.Length - er.Position) / 4); - } - } -} -", - @"using System; -using System.IO; - -namespace foo.bar { - public partial class Wrapper { - public void Write(ISubEndianBinaryWriter ew) { - ew.WriteInt32s(this.Field); - } - } -} -"); - } - - [Test] - public void TestIntListUntilEndOfStream() { - BinarySchemaTestUtil.AssertGenerated(@" -using System.Collections.Generic; - -using schema.binary; -using schema.binary.attributes; - -namespace foo.bar { - [BinarySchema] - public partial class Wrapper : IBinaryConvertible { - [RSequenceUntilEndOfStream] - public List Field { get; } = new(); - } -}", - @"using System; -using System.Collections.Generic; -using System.IO; - -namespace foo.bar { - public partial class Wrapper { - public void Read(IEndianBinaryReader er) { - { - this.Field.Clear(); - while (!er.Eof) { - this.Field.Add(er.ReadInt32()); - } + this.count_ = er.ReadUInt32(); + SequencesUtil.ResizeSequenceInPlace(this.Values, (int) this.count_); + for (var i = 0; i < this.Values.Count; ++i) { + this.Values[i] = er.ReadInt32(); } } } @@ -160,9 +40,12 @@ public void Read(IEndianBinaryReader er) { using System.IO; namespace foo.bar { - public partial class Wrapper { + public partial class ReadonlyListClass { public void Write(ISubEndianBinaryWriter ew) { - ew.WriteInt32s(this.Field); + ew.WriteUInt32((uint) Values.Count); + for (var i = 0; i < this.Values.Count; ++i) { + ew.WriteInt32(this.Values[i]); + } } } } diff --git a/Schema Tests/binary/attributes/sequence/WLengthOfSequenceAttributeTests.cs b/Schema Tests/binary/attributes/sequence/WLengthOfSequenceAttributeTests.cs index 63b186e..7b9d2a4 100644 --- a/Schema Tests/binary/attributes/sequence/WLengthOfSequenceAttributeTests.cs +++ b/Schema Tests/binary/attributes/sequence/WLengthOfSequenceAttributeTests.cs @@ -123,7 +123,9 @@ public void Read(IEndianBinaryReader er) { this.Sequence1 = SequencesUtil.CloneAndResizeSequence(this.Sequence1, this.Length); er.ReadBytes(this.Sequence1); SequencesUtil.ResizeSequenceInPlace(this.Sequence2, this.Length); - er.ReadBytes(this.Sequence2); + for (var i = 0; i < this.Sequence2.Count; ++i) { + this.Sequence2[i] = er.ReadByte(); + } } } } @@ -138,7 +140,9 @@ public void Write(ISubEndianBinaryWriter ew) { Asserts.AllEqual(Sequence1.Length, Sequence2.Count); ew.WriteInt32(Sequence1.Length); ew.WriteBytes(this.Sequence1); - ew.WriteBytes(this.Sequence2); + for (var i = 0; i < this.Sequence2.Count; ++i) { + ew.WriteByte(this.Sequence2[i]); + } } } } diff --git a/Schema Tests/binary/generator/SequenceLengthSourceGeneratorTests.cs b/Schema Tests/binary/generator/SequenceLengthSourceGeneratorTests.cs index 4824333..5d6cda4 100644 --- a/Schema Tests/binary/generator/SequenceLengthSourceGeneratorTests.cs +++ b/Schema Tests/binary/generator/SequenceLengthSourceGeneratorTests.cs @@ -54,7 +54,9 @@ public void Read(IEndianBinaryReader er) { if (this.Toggle) { this.IfBooleanList = new System.Collections.Generic.List(); SequencesUtil.ResizeSequenceInPlace(this.IfBooleanList, 3); - er.ReadInt32s(this.IfBooleanList); + for (var i = 0; i < this.IfBooleanList.Count; ++i) { + this.IfBooleanList[i] = er.ReadInt32(); + } } else { this.IfBooleanList = null; @@ -75,7 +77,9 @@ public void Write(ISubEndianBinaryWriter ew) { ew.WriteInt32s(this.IfBooleanArray); } if (this.Toggle) { - ew.WriteInt32s(this.IfBooleanList); + for (var i = 0; i < this.IfBooleanList.Count; ++i) { + ew.WriteInt32(this.IfBooleanList[i]); + } } } } diff --git a/Schema/lib/System/IO/Endianness.cs b/Schema/lib/System/IO/Endianness.cs index b70d88a..4e98765 100644 --- a/Schema/lib/System/IO/Endianness.cs +++ b/Schema/lib/System/IO/Endianness.cs @@ -95,6 +95,7 @@ private void UpdateReverse_() { this.Endianness != EndiannessUtil.SystemEndianness; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static (EndiannessSource, Endianness)? PickSuperior_( (EndiannessSource, Endianness)? prev, (EndiannessSource, Endianness) next) { diff --git a/Schema/lib/System/IO/IDataReader.cs b/Schema/lib/System/IO/IDataReader.cs index 813293e..fbd2f75 100644 --- a/Schema/lib/System/IO/IDataReader.cs +++ b/Schema/lib/System/IO/IDataReader.cs @@ -1,4 +1,6 @@ -namespace System.IO { +using System.Collections.Generic; + +namespace System.IO { public interface IDataReader { long Position { get; set; } long Length { get; } diff --git a/Schema/lib/System/IO/reader/EndianBinaryBufferedStream.cs b/Schema/lib/System/IO/reader/EndianBinaryBufferedStream.cs index 983169d..0f2e612 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryBufferedStream.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryBufferedStream.cs @@ -12,9 +12,10 @@ public EndianBinaryBufferedStream(Endianness? endianness) { public Stream BaseStream { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get; + get; set; } + public byte[] Buffer { get; private set; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -63,7 +64,6 @@ public void FillBuffer(Span buffer, int? optStride = null) { } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Read() where T : unmanaged { Read(out T val); @@ -85,8 +85,10 @@ public unsafe void Read(out T val) where T : unmanaged { } - - public Endianness Endianness => this.endiannessImpl_.Endianness; + public Endianness Endianness { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.endiannessImpl_.Endianness; + } public bool IsOppositeEndiannessOfSystem { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader.cs b/Schema/lib/System/IO/reader/EndianBinaryReader.cs index d802d92..5589697 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader.cs @@ -46,6 +46,7 @@ private void Init_(Stream baseStream, Endianness? endianness) { this.Dispose(false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Close() => Dispose(); public void Dispose() { diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader_Endianness.cs b/Schema/lib/System/IO/reader/EndianBinaryReader_Endianness.cs index c8b957e..560bccd 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader_Endianness.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader_Endianness.cs @@ -2,10 +2,15 @@ namespace System.IO { public sealed partial class EndianBinaryReader { - public Endianness Endianness => this.BufferedStream_.Endianness; + public Endianness Endianness { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.BufferedStream_.Endianness; + } - public bool IsOppositeEndiannessOfSystem - => this.BufferedStream_.IsOppositeEndiannessOfSystem; + public bool IsOppositeEndiannessOfSystem { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.BufferedStream_.IsOppositeEndiannessOfSystem; + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushStructureEndianness(Endianness endianness) diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader_Numbers.cs b/Schema/lib/System/IO/reader/EndianBinaryReader_Numbers.cs index 0e5677f..577c049 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader_Numbers.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader_Numbers.cs @@ -156,7 +156,7 @@ public long[] ReadInt64s(long count) { var newArray = new long[count]; this.ReadInt64s(newArray); return newArray; - } + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ReadInt64s(long[] dst, int start, int length) diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader_Position.cs b/Schema/lib/System/IO/reader/EndianBinaryReader_Position.cs index 0513a78..12d2314 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader_Position.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader_Position.cs @@ -6,9 +6,11 @@ public sealed partial class EndianBinaryReader { public long Position { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.positionManagerImpl_.Position; + [MethodImpl(MethodImplOptions.AggressiveInlining)] set => this.positionManagerImpl_.Position = value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AssertPosition(long expectedPosition) { EndianBinaryReader.Assert_(expectedPosition, this.Position); } diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader_SpecialNumbers.cs b/Schema/lib/System/IO/reader/EndianBinaryReader_SpecialNumbers.cs index 4b9fa9d..dc5f115 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader_SpecialNumbers.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader_SpecialNumbers.cs @@ -1,7 +1,5 @@ using System.Runtime.CompilerServices; -using CommunityToolkit.HighPerformance; - namespace System.IO { public sealed partial class EndianBinaryReader { @@ -38,7 +36,6 @@ public void ReadInt24s(Span dst) { } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AssertUInt24(uint expectedValue) => EndianBinaryReader.Assert_(expectedValue, this.ReadUInt24()); @@ -134,7 +131,6 @@ public void ReadSn8s(Span dst) { } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AssertUn8(float expectedValue) => EndianBinaryReader.AssertAlmost_(expectedValue, this.ReadUn8()); diff --git a/Schema/lib/System/IO/reader/EndianBinaryReader_Strings.cs b/Schema/lib/System/IO/reader/EndianBinaryReader_Strings.cs index 08c9050..f000e38 100644 --- a/Schema/lib/System/IO/reader/EndianBinaryReader_Strings.cs +++ b/Schema/lib/System/IO/reader/EndianBinaryReader_Strings.cs @@ -4,7 +4,6 @@ using CommunityToolkit.HighPerformance; using schema.binary.attributes; -using schema.util; namespace System.IO { public sealed partial class EndianBinaryReader { @@ -54,7 +53,8 @@ public void ReadChars(StringEncodingType encodingType, => this.ReadChars(encodingType, dst.AsSpan(start, length)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe void ReadChars(StringEncodingType encodingType, Span dst) { + public unsafe void ReadChars(StringEncodingType encodingType, + Span dst) { if (dst.Length == 0) { return; } @@ -65,7 +65,8 @@ public unsafe void ReadChars(StringEncodingType encodingType, Span dst) { var maxByteCount = encoding.GetMaxByteCount(dst.Length); Span buffer = stackalloc byte[maxByteCount]; - var bufferPtr = (byte*) Unsafe.AsPointer(ref buffer.GetPinnableReference()); + var bufferPtr = + (byte*) Unsafe.AsPointer(ref buffer.GetPinnableReference()); var dstPtr = (char*) Unsafe.AsPointer(ref dst.GetPinnableReference()); var decoder = encoding.GetDecoder(); @@ -94,6 +95,7 @@ public unsafe void ReadChars(StringEncodingType encodingType, Span dst) { this.Position = basePosition; } + public string ReadUpTo(char endToken) { var remainingCharacters = this.Length - this.Position; diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter.cs index 8999f6d..f1e0daf 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter.cs @@ -4,6 +4,7 @@ // MVID: DAEF8B62-698B-42D0-BEDD-3770EB8C9FE8 // Assembly location: R:\Documents\CSharpWorkspace\Pikmin2Utility\MKDS Course Modifier\MKDS Course Modifier.exe +using System.Runtime.CompilerServices; using System.Threading.Tasks; using schema.binary.io; @@ -25,7 +26,7 @@ public EndianBinaryWriter(Endianness endianness) { this.localPositionStack_.Push(Task.FromResult(0L)); } - private EndianBinaryWriter(Endianness? endianness, + private EndianBinaryWriter(Endianness? endianness, ISubDelayedContentOutputStream impl) { this.impl_ = impl as IDelayedContentOutputStream; this.localPositionStack_.Push(Task.FromResult(0L)); @@ -35,11 +36,11 @@ private EndianBinaryWriter(Endianness? endianness, this.Dispose(false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Align(uint amt) => this.impl_.Align(amt); - public void Close() { - this.Dispose(); - } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Close() => this.Dispose(); public void Dispose() { this.Dispose(true); diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_Delayed.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_Delayed.cs index ea7669f..3117779 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_Delayed.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_Delayed.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace System.IO { @@ -15,18 +16,25 @@ public Task GetLocalPosition() return result[0] - result[1]; }); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task GetAbsolutePosition() => this.impl_.GetAbsolutePosition(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task GetAbsoluteLength() => this.impl_.GetAbsoluteLength(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task GetStartPositionOfSubStream() => this.impl_.GetStartPositionOfSubStream(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task GetPositionInSubStream() => this.impl_.GetPositionInSubStream(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task GetLengthOfSubStream() => this.impl_.GetLengthOfSubStream(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task CompleteAndCopyToDelayed(Stream stream) => this.impl_.CompleteAndCopyToDelayed(stream); @@ -58,48 +66,56 @@ private void WriteBufferDelayed_(Task delayedBytes, delayedBytesLength); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteByteDelayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => new[] { valueTask.Result }), Task.FromResult((long) sizeof(byte))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSByteDelayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => new[] { (byte) valueTask.Result }), Task.FromResult((long) sizeof(sbyte))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt16Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => BitConverter.GetBytes(valueTask.Result)), Task.FromResult((long) sizeof(short))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt16Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => BitConverter.GetBytes(valueTask.Result)), Task.FromResult((long) sizeof(ushort))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt32Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => BitConverter.GetBytes(valueTask.Result)), Task.FromResult((long) sizeof(int))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt32Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => BitConverter.GetBytes(valueTask.Result)), Task.FromResult((long) sizeof(uint))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt64Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( valueTask => BitConverter.GetBytes(valueTask.Result)), Task.FromResult((long) sizeof(long))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt64Delayed(Task delayedValue) => this.WriteBufferDelayed_( delayedValue.ContinueWith( diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_Endianness.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_Endianness.cs index e94a8b3..e890637 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_Endianness.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_Endianness.cs @@ -1,16 +1,26 @@ -namespace System.IO { +using System.Runtime.CompilerServices; + +namespace System.IO { public sealed partial class EndianBinaryWriter : IEndiannessStack { - public Endianness Endianness => this.impl_.Endianness; + public Endianness Endianness { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.impl_.Endianness; + } - public bool IsOppositeEndiannessOfSystem - => this.impl_.IsOppositeEndiannessOfSystem; + public bool IsOppositeEndiannessOfSystem { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.impl_.IsOppositeEndiannessOfSystem; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushStructureEndianness(Endianness endianness) => this.impl_.PushStructureEndianness(endianness); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushMemberEndianness(Endianness endianness) => this.impl_.PushMemberEndianness(endianness); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PopEndianness() => this.impl_.PopEndianness(); } } \ No newline at end of file diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_Numbers.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_Numbers.cs index c307e62..1ae7fd7 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_Numbers.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_Numbers.cs @@ -2,93 +2,122 @@ namespace System.IO { public sealed partial class EndianBinaryWriter { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteByte(byte value) => this.impl_.WriteByte(value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteBytes(ReadOnlySpan values) => this.impl_.WriteBytes(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteBytes(byte[] values, int offset, int count) => this.impl_.WriteBytes(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSByte(sbyte value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSBytes(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSBytes(sbyte[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt16(short value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt16s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt16s(short[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt16(ushort value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt16s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt16s(ushort[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt32(int value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt32s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt32s(int[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt32(uint value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt32s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt32s(uint[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt64(long value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt64s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt64s(long[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt64(ulong value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt64s(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUInt64s(ulong[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSingle(float value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSingles(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSingles(float[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteDouble(double value) => this.impl_.WriteAndFlip(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteDoubles(ReadOnlySpan values) => this.impl_.WriteAndFlip(values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteDoubles(double[] values, int offset, int count) => this.impl_.WriteAndFlip(values, offset, count); } diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_Position.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_Position.cs index ef97d27..c93cb99 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_Position.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_Position.cs @@ -21,6 +21,7 @@ public Task GetPointerToMemberRelativeToScope( if (fullPath.Length > 0) { fullPath += "."; } + fullPath += memberPath; return this.startPositions_.Get(fullPath); } @@ -31,6 +32,7 @@ public Task GetSizeOfMemberRelativeToScope( if (fullPath.Length > 0) { fullPath += "."; } + fullPath += memberPath; var startTask = this.startPositions_.Get(fullPath); var endTask = this.endPositions_.Get(fullPath); @@ -63,15 +65,16 @@ private string GetCurrentScope_() { if (totalString.Length > 0) { totalString.Append("."); } + totalString.Append(scope); } + return totalString.ToString(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void PushLocalSpace() { - this.localPositionStack_.Push(this.impl_.GetAbsolutePosition()); - } + public void PushLocalSpace() + => this.localPositionStack_.Push(this.impl_.GetAbsolutePosition()); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PopLocalSpace() { diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_SpecialNumbers.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_SpecialNumbers.cs index 870b9c2..8af2718 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_SpecialNumbers.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_SpecialNumbers.cs @@ -1,20 +1,20 @@ -using System.Buffers.Binary; -using System.Drawing; +using System.Runtime.CompilerServices; using CommunityToolkit.HighPerformance; -using static schema.binary.BinarySchemaStructureParser; - namespace System.IO { public sealed partial class EndianBinaryWriter { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteInt24(int value) { var ptr = &value; this.WriteInt24s(new Span(ptr, 1)); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteInt24s(int[] values, int offset, int count) => this.WriteInt24s(values.AsSpan(offset, count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteInt24s(ReadOnlySpan values) { foreach (var value in values) { var ptr = &value; @@ -24,14 +24,17 @@ public unsafe void WriteInt24s(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteUInt24(uint value) { var ptr = &value; this.WriteUInt24s(new Span(ptr, 1)); } - public void WriteUInt24s(uint[] values, int offset, int count) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void WriteUInt24s(uint[] values, int offset, int count) => this.WriteUInt24s(values.AsSpan(offset, count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteUInt24s(ReadOnlySpan values) { foreach (var value in values) { var ptr = &value; @@ -41,14 +44,17 @@ public unsafe void WriteUInt24s(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteHalf(float value) { var ptr = &value; this.WriteHalfs(new Span(ptr, 1)); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteHalfs(float[] values, int offset, int count) => this.WriteHalfs(values.AsSpan(offset, count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteHalfs(ReadOnlySpan values) { foreach (var value in values) { var half = new Half(value); @@ -57,14 +63,17 @@ public void WriteHalfs(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn8(float value) { var un8 = (byte) (value * 255f); this.WriteByte(un8); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn8s(float[] values, int offset, int count) => this.WriteUn8s(values.AsSpan()); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn8s(ReadOnlySpan values) { foreach (var value in values) { this.WriteUn8(value); @@ -72,14 +81,17 @@ public void WriteUn8s(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn8(float value) { var sn8 = (byte) (value * (255f / 2)); this.WriteByte(sn8); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn8s(float[] values, int offset, int count) => this.WriteSn8s(values.AsSpan()); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn8s(ReadOnlySpan values) { foreach (var value in values) { this.WriteSn8(value); @@ -87,14 +99,17 @@ public void WriteSn8s(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn16(float value) { var un16 = (ushort) (value * 65535f); this.WriteUInt16(un16); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn16s(float[] values, int offset, int count) => this.WriteUn16s(values.AsSpan()); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteUn16s(ReadOnlySpan values) { foreach (var value in values) { this.WriteUn16(value); @@ -102,14 +117,17 @@ public void WriteUn16s(ReadOnlySpan values) { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn16(float value) { var sn16 = (short) (value * (65535f / 2)); this.WriteInt16(sn16); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn16s(float[] values, int offset, int count) => this.WriteSn16s(values.AsSpan()); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSn16s(ReadOnlySpan values) { foreach (var value in values) { this.WriteSn16(value); diff --git a/Schema/lib/System/IO/writer/EndianBinaryWriter_Strings.cs b/Schema/lib/System/IO/writer/EndianBinaryWriter_Strings.cs index 134e157..18d2978 100644 --- a/Schema/lib/System/IO/writer/EndianBinaryWriter_Strings.cs +++ b/Schema/lib/System/IO/writer/EndianBinaryWriter_Strings.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Text; using schema.binary.attributes; @@ -18,6 +19,14 @@ public void WriteChars(char[] values, int offset, int count) public void WriteChars(ReadOnlySpan values) => this.WriteChars(StringEncodingType.ASCII, values); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void WriteChars(IReadOnlyList values) + => this.WriteChars(StringEncodingType.ASCII, values); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void WriteChars(IReadOnlyList values, int offset, int count) + => this.WriteChars(StringEncodingType.ASCII, values, offset, count); + public unsafe void WriteChar(StringEncodingType encodingType, char value) { var ptr = &value; @@ -31,6 +40,7 @@ public unsafe void WriteChar(StringEncodingType encodingType, char value) { this.WriteBytes(dstSpan); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteChars(StringEncodingType encodingType, char[] values, int offset, @@ -50,6 +60,20 @@ public void WriteChars(StringEncodingType encodingType, this.WriteBytes(dstSpan); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void WriteChars(StringEncodingType encodingType, + IReadOnlyList values) + => this.WriteChars(encodingType, values, 0, values.Count); + + public void WriteChars(StringEncodingType encodingType, + IReadOnlyList values, + int offset, + int count) { + for (var i = offset; i < offset + count; ++i) { + this.WriteChar(encodingType, values[i]); + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteString(string value) diff --git a/Schema/lib/System/IO/writer/IEndianBinaryWriter.cs b/Schema/lib/System/IO/writer/IEndianBinaryWriter.cs index 502fa25..1122f04 100644 --- a/Schema/lib/System/IO/writer/IEndianBinaryWriter.cs +++ b/Schema/lib/System/IO/writer/IEndianBinaryWriter.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using schema.binary.attributes; @@ -18,54 +19,6 @@ public interface ISubEndianBinaryWriter : IEndiannessStack, ILocalSpaceStack { void WriteSBytes(ReadOnlySpan values); void WriteSBytes(sbyte[] values, int offset, int count); - void WriteChar(char value); - void WriteChars(ReadOnlySpan values); - void WriteChars(char[] values, int offset, int count); - - void WriteChar(StringEncodingType encodingType, char value); - void WriteChars(StringEncodingType encodingType, ReadOnlySpan values); - - void WriteChars(StringEncodingType encodingType, - char[] values, - int offset, - int count); - - void WriteString(string value); - void WriteStringNT(string value); - - void WriteString(StringEncodingType encodingType, string value); - void WriteStringNT(StringEncodingType encodingType, string value); - - void WriteStringWithExactLength(string value, int length); - - void WriteStringWithExactLength(StringEncodingType encodingType, - string value, - int length); - - void WriteDouble(double value); - void WriteDoubles(ReadOnlySpan values); - void WriteDoubles(double[] values, int offset, int count); - - void WriteHalf(float value); - void WriteHalfs(ReadOnlySpan values); - void WriteHalfs(float[] values, int offset, int count); - - void WriteSingle(float value); - void WriteSingles(ReadOnlySpan values); - void WriteSingles(float[] values, int offset, int count); - - void WriteInt24(int value); - void WriteInt24s(ReadOnlySpan values); - void WriteInt24s(int[] values, int offset, int count); - - void WriteInt32(int value); - void WriteInt32s(ReadOnlySpan values); - void WriteInt32s(int[] values, int offset, int count); - - void WriteInt64(long value); - void WriteInt64s(ReadOnlySpan values); - void WriteInt64s(long[] values, int offset, int count); - void WriteInt16(short value); void WriteInt16s(ReadOnlySpan values); void WriteInt16s(short[] values, int offset, int count); @@ -74,18 +27,42 @@ void WriteStringWithExactLength(StringEncodingType encodingType, void WriteUInt16s(ReadOnlySpan values); void WriteUInt16s(ushort[] values, int offset, int count); + void WriteInt24(int value); + void WriteInt24s(ReadOnlySpan values); + void WriteInt24s(int[] values, int offset, int count); + void WriteUInt24(uint value); void WriteUInt24s(ReadOnlySpan values); void WriteUInt24s(uint[] values, int offset, int count); + void WriteInt32(int value); + void WriteInt32s(ReadOnlySpan values); + void WriteInt32s(int[] values, int offset, int count); + void WriteUInt32(uint value); void WriteUInt32s(ReadOnlySpan values); void WriteUInt32s(uint[] values, int offset, int count); + void WriteInt64(long value); + void WriteInt64s(ReadOnlySpan values); + void WriteInt64s(long[] values, int offset, int count); + void WriteUInt64(ulong value); void WriteUInt64s(ReadOnlySpan values); void WriteUInt64s(ulong[] values, int offset, int count); + void WriteHalf(float value); + void WriteHalfs(ReadOnlySpan values); + void WriteHalfs(float[] values, int offset, int count); + + void WriteSingle(float value); + void WriteSingles(ReadOnlySpan values); + void WriteSingles(float[] values, int offset, int count); + + void WriteDouble(double value); + void WriteDoubles(ReadOnlySpan values); + void WriteDoubles(double[] values, int offset, int count); + void WriteUn8(float value); void WriteUn8s(ReadOnlySpan values); void WriteUn8s(float[] values, int offset, int count); @@ -102,6 +79,30 @@ void WriteStringWithExactLength(StringEncodingType encodingType, void WriteSn16s(ReadOnlySpan values); void WriteSn16s(float[] values, int offset, int count); + void WriteChar(char value); + void WriteChars(ReadOnlySpan values); + void WriteChars(char[] values, int offset, int count); + + void WriteChar(StringEncodingType encodingType, char value); + void WriteChars(StringEncodingType encodingType, ReadOnlySpan values); + + void WriteChars(StringEncodingType encodingType, + char[] values, + int offset, + int count); + + void WriteString(string value); + void WriteStringNT(string value); + + void WriteString(StringEncodingType encodingType, string value); + void WriteStringNT(StringEncodingType encodingType, string value); + + void WriteStringWithExactLength(string value, int length); + + void WriteStringWithExactLength(StringEncodingType encodingType, + string value, + int length); + void Close(); diff --git a/Schema/src/binary/BinarySchemaStructureParser.cs b/Schema/src/binary/BinarySchemaStructureParser.cs index c2859f7..ad15a91 100644 --- a/Schema/src/binary/BinarySchemaStructureParser.cs +++ b/Schema/src/binary/BinarySchemaStructureParser.cs @@ -101,6 +101,16 @@ public enum SequenceType { READ_ONLY_SEQUENCE, } + public static class SequenceTypeExtensions { + public static bool IsArray(this SequenceType sequenceType) + => sequenceType is SequenceType.MUTABLE_ARRAY + or SequenceType.IMMUTABLE_ARRAY; + + public static bool IsSequence(this SequenceType sequenceType) + => sequenceType is SequenceType.MUTABLE_SEQUENCE + or SequenceType.READ_ONLY_SEQUENCE; + } + public enum SequenceLengthSourceType { UNSPECIFIED, IMMEDIATE_VALUE, diff --git a/Schema/src/binary/attributes/BMemberAttribute.cs b/Schema/src/binary/attributes/BMemberAttribute.cs index 7e44255..7e5d44f 100644 --- a/Schema/src/binary/attributes/BMemberAttribute.cs +++ b/Schema/src/binary/attributes/BMemberAttribute.cs @@ -247,7 +247,7 @@ public IMemberReference AssertIsBool() { public IMemberReference AssertIsSequence() { if (!this.IsSequence) { - Asserts.Fail($"Expected {this.Name} to refer to an sequence!"); + Asserts.Fail($"Expected {this.Name} to refer to a sequence!"); } return this; diff --git a/Schema/src/binary/text/BinarySchemaReaderGenerator.cs b/Schema/src/binary/text/BinarySchemaReaderGenerator.cs index a3822e4..1b6c6d4 100644 --- a/Schema/src/binary/text/BinarySchemaReaderGenerator.cs +++ b/Schema/src/binary/text/BinarySchemaReaderGenerator.cs @@ -551,31 +551,29 @@ private static void ReadIntoArray_( cbsb, member, () => { - var sequenceType = - Asserts.CastNonnull( - member.MemberType as - ISequenceMemberType); + var sequenceMemberType = + Asserts.CastNonnull(member.MemberType as ISequenceMemberType); + var sequenceTypeInfo = sequenceMemberType.SequenceTypeInfo; + var sequenceType = sequenceTypeInfo.SequenceType; - if (sequenceType.SequenceTypeInfo.SequenceType is SequenceType - .MUTABLE_SEQUENCE or SequenceType.READ_ONLY_SEQUENCE) { + if (sequenceType.IsSequence()) { cbsb.WriteLine($"this.{member.Name}.Read(er);"); return; } - var elementType = sequenceType.ElementType; + var elementType = sequenceMemberType.ElementType; if (elementType is IGenericMemberType genericElementType) { elementType = genericElementType.ConstraintType; } - if (elementType is IPrimitiveMemberType - primitiveElementType) { + if (elementType is IPrimitiveMemberType primitiveElementType) { // Primitives that don't need to be cast are the easiest to read. - if (!primitiveElementType.UseAltFormat) { - var label = - SchemaGeneratorUtil.GetPrimitiveLabel( - primitiveElementType.PrimitiveType); + if (!primitiveElementType.UseAltFormat && + sequenceType.IsArray()) { + var label = SchemaGeneratorUtil.GetPrimitiveLabel( + primitiveElementType.PrimitiveType); if (!primitiveElementType.IsReadOnly) { cbsb.WriteLine( $"er.Read{label}s(this.{member.Name});"); @@ -589,8 +587,7 @@ member.MemberType as // Primitives that *do* need to be cast have to be read individually. if (!primitiveElementType.IsReadOnly) { - var arrayLengthName = - sequenceType.SequenceTypeInfo.LengthName; + var arrayLengthName = sequenceTypeInfo.LengthName; cbsb.EnterBlock( $"for (var i = 0; i < this.{member.Name}.{arrayLengthName}; ++i)") @@ -624,8 +621,7 @@ member.MemberType as cbsb.WriteLine("e.Read(er);"); cbsb.ExitBlock(); } else { - var arrayLengthName = - sequenceType.SequenceTypeInfo.LengthName; + var arrayLengthName = sequenceTypeInfo.LengthName; cbsb.EnterBlock( $"for (var i = 0; i < this.{member.Name}.{arrayLengthName}; ++i)"); cbsb.WriteLine( @@ -708,7 +704,6 @@ private static string GetAssertPrimitiveText_( primitiveType != SchemaPrimitiveTypesUtil.GetUnderlyingPrimitiveType( altFormat.AsPrimitiveType()); - } var castText = ""; diff --git a/Schema/src/binary/text/BinarySchemaWriterGenerator.cs b/Schema/src/binary/text/BinarySchemaWriterGenerator.cs index cfc130b..6767a09 100644 --- a/Schema/src/binary/text/BinarySchemaWriterGenerator.cs +++ b/Schema/src/binary/text/BinarySchemaWriterGenerator.cs @@ -146,7 +146,7 @@ private static void WriteValueMember_( break; } case ISequenceMemberType: { - BinarySchemaWriterGenerator.WriteArray_(cbsb, sourceSymbol, member); + BinarySchemaWriterGenerator.WriteArray_(cbsb, member); break; } default: @@ -377,73 +377,65 @@ private static void WriteStructure_( private static void WriteArray_( ICurlyBracketTextWriter cbsb, - ITypeSymbol sourceSymbol, ISchemaValueMember member) { - var arrayType = + var sequenceMemberType = Asserts.CastNonnull(member.MemberType as ISequenceMemberType); - if (arrayType.LengthSourceType != SequenceLengthSourceType.READ_ONLY) { + if (sequenceMemberType.LengthSourceType != + SequenceLengthSourceType.READ_ONLY) { var isImmediate = - arrayType.LengthSourceType == + sequenceMemberType.LengthSourceType == SequenceLengthSourceType.IMMEDIATE_VALUE; if (isImmediate) { - var arrayLengthName = arrayType.SequenceTypeInfo.LengthName; + var arrayLengthName = sequenceMemberType.SequenceTypeInfo.LengthName; var arrayLengthAccessor = $"this.{member.Name}.{arrayLengthName}"; cbsb.WriteLine( - $"{GetWritePrimitiveText_(SchemaPrimitiveType.INT32, arrayType.ImmediateLengthType.AsNumberType(), arrayLengthAccessor)};"); + $"{GetWritePrimitiveText_(SchemaPrimitiveType.INT32, sequenceMemberType.ImmediateLengthType.AsNumberType(), arrayLengthAccessor)};"); } } - BinarySchemaWriterGenerator.WriteIntoArray_(cbsb, sourceSymbol, member); + BinarySchemaWriterGenerator.WriteIntoArray_(cbsb, member); } - private static void WriteIntoArray_( - ICurlyBracketTextWriter cbsb, - ITypeSymbol sourceSymbol, - ISchemaValueMember member) { + private static void WriteIntoArray_(ICurlyBracketTextWriter cbsb, + ISchemaValueMember member) { HandleMemberEndiannessAndTracking_( cbsb, member, () => { - var sequenceType = - Asserts.CastNonnull( - member.MemberType as - ISequenceMemberType); + var sequenceMemberType = + Asserts.CastNonnull(member.MemberType as ISequenceMemberType); + var sequenceTypeInfo = sequenceMemberType.SequenceTypeInfo; + var sequenceType = sequenceTypeInfo.SequenceType; - if (sequenceType.SequenceTypeInfo.SequenceType is SequenceType - .MUTABLE_SEQUENCE or SequenceType.READ_ONLY_SEQUENCE) { + if (sequenceType.IsSequence()) { cbsb.WriteLine($"this.{member.Name}.Write(ew);"); return; } var elementType = - sequenceType.ElementType; + sequenceMemberType.ElementType; if (elementType is IGenericMemberType genericElementType) { elementType = genericElementType .ConstraintType; } - if (elementType is - IPrimitiveMemberType - primitiveElementType) { + if (elementType is IPrimitiveMemberType primitiveElementType) { // Primitives that don't need to be cast are the easiest to write. - if (!primitiveElementType - .UseAltFormat) { + if (!primitiveElementType.UseAltFormat && + sequenceType.IsArray()) { var label = - SchemaGeneratorUtil - .GetPrimitiveLabel( - primitiveElementType - .PrimitiveType); + SchemaGeneratorUtil.GetPrimitiveLabel( + primitiveElementType.PrimitiveType); cbsb.WriteLine( $"ew.Write{label}s(this.{member.Name});"); return; } // Primitives that *do* need to be cast have to be written individually. - var arrayLengthName = - sequenceType.SequenceTypeInfo.LengthName; + var arrayLengthName = sequenceTypeInfo.LengthName; cbsb.EnterBlock( $"for (var i = 0; i < this.{member.Name}.{arrayLengthName}; ++i)") .WriteLine( @@ -452,33 +444,15 @@ member.MemberType as return; } - if (elementType is - IStructureMemberType - structureElementType) { - //if (structureElementType.IsReferenceType) { + if (elementType is IStructureMemberType) { cbsb.EnterBlock( $"foreach (var e in this.{member.Name})") .WriteLine("e.Write(ew);") .ExitBlock(); - // TODO: Do value types need to be read like below? - /*} - // Value types (mainly structs) have to be pulled out, read, then put - // back in. - else { - var arrayLengthName = arrayType.SequenceType == SequenceType.ARRAY - ? "Length" - : "Count"; - cbsb.EnterBlock( - $"for (var i = 0; i < this.{member.Name}.{arrayLengthName}; ++i)") - .WriteLine($"var e = this.{member.Name}[i];") - .WriteLine("e.Read(ew);") - .WriteLine($"this.{member.Name}[i] = e;") - .ExitBlock(); - }*/ return; } - // Anything that makes it down here probably isn't meant to be read. + // Anything that makes it down here probably isn't meant to be written. throw new NotImplementedException(); }); }