-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to read vectors/matrices directly via Schema.
- Loading branch information
1 parent
18f6a3b
commit dd1be2b
Showing
11 changed files
with
310 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Numerics; | ||
|
||
using NUnit.Framework; | ||
|
||
using schema.testing; | ||
|
||
|
||
namespace schema.binary; | ||
|
||
public partial class KnownStructTests { | ||
[BinarySchema] | ||
private partial class Vector2Wrapper : IBinaryConvertible { | ||
public Vector2 Value { get; private set; } | ||
} | ||
|
||
[Test] | ||
public void TestReadingVector2() { | ||
using var br = SchemaMemoryStream.From([123f, 456f]).GetBinaryReader(); | ||
Assert.AreEqual(new Vector2(123, 456), br.ReadNew<Vector2Wrapper>().Value); | ||
} | ||
|
||
|
||
[BinarySchema] | ||
private partial class Vector4Wrapper : IBinaryConvertible { | ||
public Vector4 Value { get; private set; } | ||
} | ||
|
||
[Test] | ||
public void TestReadingVector4() { | ||
using var br = SchemaMemoryStream.From([12f, 23f, 34f, 45f]) | ||
.GetBinaryReader(); | ||
Assert.AreEqual(new Vector4(12, 23, 34, 45), br.ReadNew<Vector4Wrapper>().Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using NUnit.Framework; | ||
|
||
|
||
namespace schema.binary.text; | ||
|
||
internal class KnownStructTests { | ||
[Test] | ||
[TestCase("Vector2")] | ||
[TestCase("Vector3")] | ||
[TestCase("Vector4")] | ||
[TestCase("Matrix3x2")] | ||
[TestCase("Matrix4x4")] | ||
public void TestSystemNumerics(string knownStructName) { | ||
BinarySchemaTestUtil.AssertGenerated( | ||
$$""" | ||
using System.Numerics; | ||
using schema.binary; | ||
namespace foo.bar { | ||
[BinarySchema] | ||
public partial class Wrapper { | ||
public {{knownStructName}} Field { get; set; } | ||
public {{knownStructName}} PrivateField { get; private set; } | ||
} | ||
} | ||
""", | ||
$$""" | ||
using System; | ||
using schema.binary; | ||
namespace foo.bar { | ||
public partial class Wrapper { | ||
public void Read(IBinaryReader br) { | ||
this.Field = br.Read{{knownStructName}}(); | ||
this.PrivateField = br.Read{{knownStructName}}(); | ||
} | ||
} | ||
} | ||
""", | ||
$$""" | ||
using System; | ||
using schema.binary; | ||
namespace foo.bar { | ||
public partial class Wrapper { | ||
public void Write(IBinaryWriter bw) { | ||
bw.Write{{knownStructName}}(this.Field); | ||
bw.Write{{knownStructName}}(this.PrivateField); | ||
} | ||
} | ||
} | ||
"""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
using CommunityToolkit.HighPerformance; | ||
|
||
namespace schema.binary; | ||
|
||
public static partial class BinaryReaderExtensions { | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static unsafe T ReadFloatArrayStruct_<T>(this IBinaryReader br) | ||
where T : unmanaged { | ||
T value; | ||
|
||
T* ptr = &value; | ||
var span = new Span<T>(ptr, 1); | ||
br.ReadSingles(span.Cast<T, float>()); | ||
|
||
return value; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Vector2 ReadVector2(this IBinaryReader br) | ||
=> br.ReadFloatArrayStruct_<Vector2>(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Vector3 ReadVector3(this IBinaryReader br) | ||
=> br.ReadFloatArrayStruct_<Vector3>(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Vector4 ReadVector4(this IBinaryReader br) | ||
=> br.ReadFloatArrayStruct_<Vector4>(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Matrix3x2 ReadMatrix3x2(this IBinaryReader br) | ||
=> br.ReadFloatArrayStruct_<Matrix3x2>(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Matrix4x4 ReadMatrix4x4(this IBinaryReader br) | ||
=> br.ReadFloatArrayStruct_<Matrix4x4>(); | ||
} |
2 changes: 1 addition & 1 deletion
2
...c/binary/reader/BinaryReaderExtensions.cs → ...nary/reader/BinaryReaderExtensions_New.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.