Skip to content

Commit

Permalink
Fixed a bug in the new length logic that required casting uints to ints.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Jul 15, 2023
1 parent 9b9d876 commit 9f03c42
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Schema Tests/SchemaReaderGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public void Read(IEndianBinaryReader er) {" +
er.ReadInt32s(this.constLengthIntValues);
{
var c = er.ReadUInt32();
this.intValues = SequencesUtil.ResizeSequence(this.intValues, c);
this.intValues = SequencesUtil.ResizeSequence(this.intValues, (int) c);
}
er.ReadInt32s(this.intValues);" +
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public partial class AlignWrapper {
public void Read(IEndianBinaryReader er) {
{
var c = er.ReadUInt32();
this.Field = SequencesUtil.ResizeSequence(this.Field, c);
this.Field = SequencesUtil.ResizeSequence(this.Field, (int) c);
}
er.Align(2);
er.ReadInt32s(this.Field);
Expand Down
41 changes: 41 additions & 0 deletions Schema Tests/binary/build/SequenceBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ public void TestWriteAndRead() {
Assert.AreEqual(expectedSw, actualSw);
}



[BinarySchema]
public partial class Uint32LengthSequenceWrapper : IBinaryConvertible {
[SequenceLengthSource(SchemaIntegerType.UINT32)]
public int[] Values { get; set; }

public override bool Equals(object other) {
if (other is Uint32LengthSequenceWrapper otherSequenceWrapper) {
return this.Values.SequenceEqual(otherSequenceWrapper.Values);
}

return false;
}
}

[Test]
public void TestWriteAndReadWithUint32Length() {
var expectedSw = new Uint32LengthSequenceWrapper {
Values = new[] { 1, 2, 3, 4, 5, 9, 8, 7, 6 }
};

var ms = new MemoryStream();

var endianness = Endianness.BigEndian;
var ew = new EndianBinaryWriter(endianness);

expectedSw.Write(ew);
ew.CompleteAndCopyToDelayed(ms).Wait();

ms.Position = 0;
var er = new EndianBinaryReader(ms, endianness);
var actualSw = er.ReadNew<Uint32LengthSequenceWrapper>();

Assert.AreEqual(expectedSw, actualSw);
}



[BinarySchema]
public partial struct SchemaStruct : IBinaryConvertible {
public int Value { get; set; }
Expand Down Expand Up @@ -127,6 +166,8 @@ public void TestWriteAndReadStructArray() {
Assert.True(expectedSw.Values.SequenceEqual(actualSws));
}



[BinarySchema]
public partial class StructListSequenceWrapper : IBinaryConvertible
{
Expand Down
2 changes: 1 addition & 1 deletion Schema Tests/binary/generator/IChildOfGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace foo.bar {
public partial class Parent {
public void Read(IEndianBinaryReader er) {
this.Length = er.ReadUInt32();
this.Child = SequencesUtil.ResizeSequence(this.Child, this.Length);
this.Child = SequencesUtil.ResizeSequence(this.Child, (int) this.Length);
foreach (var e in this.Child) {
e.Parent = this;
e.Read(er);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class ConstLengthWrapper : IBinaryConvertible {
public List<int>? IfBooleanList { get; set; }
}
}",
@"using System;
@"using System;
using System.Collections.Generic;
using System.IO;
using schema.util.sequences;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void Read(IEndianBinaryReader er) {
}
}
",
@"using System;
@"using System;
using System.IO;
namespace foo.bar {
public partial class ConstLengthWrapper {
Expand Down
14 changes: 12 additions & 2 deletions Schema/src/binary/text/BinarySchemaReaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,16 @@ arrayType.ElementType is IPrimitiveMemberType
SequenceLengthSourceType.CONST_LENGTH => $"{arrayType.ConstLength}",
};

var castText = "";
if ((isImmediate &&
arrayType.ImmediateLengthType == SchemaIntegerType.UINT32) ||
(arrayType.LengthSourceType ==
SequenceLengthSourceType.OTHER_MEMBER &&
(arrayType.LengthMember.MemberType as IPrimitiveMemberType)!
.PrimitiveType == SchemaPrimitiveType.UINT32)) {
castText = "(int) ";
}

if (isImmediate) {
var readType = SchemaGeneratorUtil.GetIntLabel(
arrayType.ImmediateLengthType);
Expand All @@ -566,10 +576,10 @@ arrayType.SequenceTypeInfo.SequenceType is SequenceType.MUTABLE_LIST
or SequenceType.MUTABLE_SEQUENCE;
if (inPlace) {
cbsb.WriteLine(
$"SequencesUtil.ResizeSequenceInPlace(this.{member.Name}, {lengthName});");
$"SequencesUtil.ResizeSequenceInPlace(this.{member.Name}, {castText}{lengthName});");
} else {
cbsb.WriteLine(
$"this.{member.Name} = SequencesUtil.ResizeSequence(this.{member.Name}, {lengthName});");
$"this.{member.Name} = SequencesUtil.ResizeSequence(this.{member.Name}, {castText}{lengthName});");
}

if (isImmediate) {
Expand Down

0 comments on commit 9f03c42

Please sign in to comment.