commit 0cbce3c1656737b5662660a7fd2c2249644381d7 Author: femsci Date: Tue Nov 14 15:42:11 2023 +0100 Core + binary enumerator diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afec7c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/obj/ +**/bin/ diff --git a/Nyanbyte.Sysnya.sln b/Nyanbyte.Sysnya.sln new file mode 100644 index 0000000..d7bb1f5 --- /dev/null +++ b/Nyanbyte.Sysnya.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4F71CCC2-5D82-4981-AF1F-2F93AAC70C3D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nyanbyte.Sysnya", "src\Nyanbyte.Sysnya\Nyanbyte.Sysnya.csproj", "{6225FBC7-2010-43BA-B574-B3BEFFF3C9C9}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{0BC78264-4FFC-4C73-A4AE-E713BF26149E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nyanbyte.Sysnya.CoreTests", "test\Nyanbyte.Sysnya.CoreTests\Nyanbyte.Sysnya.CoreTests.csproj", "{9312239C-98B5-43D8-9696-25AF91392FF0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6225FBC7-2010-43BA-B574-B3BEFFF3C9C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6225FBC7-2010-43BA-B574-B3BEFFF3C9C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6225FBC7-2010-43BA-B574-B3BEFFF3C9C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6225FBC7-2010-43BA-B574-B3BEFFF3C9C9}.Release|Any CPU.Build.0 = Release|Any CPU + {9312239C-98B5-43D8-9696-25AF91392FF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9312239C-98B5-43D8-9696-25AF91392FF0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9312239C-98B5-43D8-9696-25AF91392FF0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9312239C-98B5-43D8-9696-25AF91392FF0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {6225FBC7-2010-43BA-B574-B3BEFFF3C9C9} = {4F71CCC2-5D82-4981-AF1F-2F93AAC70C3D} + {9312239C-98B5-43D8-9696-25AF91392FF0} = {0BC78264-4FFC-4C73-A4AE-E713BF26149E} + EndGlobalSection +EndGlobal diff --git a/src/Nyanbyte.Sysnya/Binary/BinaryEnumerator.cs b/src/Nyanbyte.Sysnya/Binary/BinaryEnumerator.cs new file mode 100644 index 0000000..7648489 --- /dev/null +++ b/src/Nyanbyte.Sysnya/Binary/BinaryEnumerator.cs @@ -0,0 +1,49 @@ +using System.Buffers.Binary; + +namespace Nyanbyte.Sysnya.Binary; + +public class BinaryEnumerator +{ + public BinaryEnumerator(ReadOnlyMemory memory, bool bigEndian = true) + { + _fullData = memory; + _bigEndian = bigEndian; + } + + //endianness := true -> big; false -> smol + private bool _bigEndian = true; + private readonly ReadOnlyMemory _fullData; + private int _position = 0; + + public BinaryEnumerator Skip(int length) + { + _position += length; + return this; + } + + public int ReadInt32() + { + int value = _bigEndian ? BinaryPrimitives.ReadInt32BigEndian(_fullData.Span[_position..(_position + 4)]) : BinaryPrimitives.ReadInt32LittleEndian(_fullData.Span[_position..(_position + 4)]); + _position += 4; + return value; + } + + public BinaryEnumerator ReadInt32(out int value) + { + value = ReadInt32(); + return this; + } + + public long ReadInt64() + { + long value = _bigEndian ? BinaryPrimitives.ReadInt64BigEndian(_fullData.Span[_position..(_position + 8)]) : BinaryPrimitives.ReadInt64LittleEndian(_fullData.Span[_position..(_position + 8)]); + _position += 8; + return value; + } + + public BinaryEnumerator ReadInt64(out long value) + { + value = ReadInt64(); + return this; + } +} diff --git a/src/Nyanbyte.Sysnya/Binary/BinaryStructMapper.cs b/src/Nyanbyte.Sysnya/Binary/BinaryStructMapper.cs new file mode 100644 index 0000000..d3273fb --- /dev/null +++ b/src/Nyanbyte.Sysnya/Binary/BinaryStructMapper.cs @@ -0,0 +1,25 @@ +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; + +namespace Nyanbyte.Sysnya.Binary; + +public class BinaryStructMapperBuilder +{ + public BinaryStructMapperBuilder(T obj) + { + ArgumentNullException.ThrowIfNull(obj); + _obj = obj; + } + + [NotNull] + private readonly T _obj; + private readonly Stack>> _funcStack = new(); + + public BinaryStructMapperBuilder BindU32(Expression> property) + { + var member = property.Body as MemberExpression; + ArgumentNullException.ThrowIfNull(member); + _funcStack.Push(m => _obj.GetType().GetProperty(member.Member.Name)!.SetValue(_obj, m)); + return this; + } +} diff --git a/src/Nyanbyte.Sysnya/Nyanbyte.Sysnya.csproj b/src/Nyanbyte.Sysnya/Nyanbyte.Sysnya.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/src/Nyanbyte.Sysnya/Nyanbyte.Sysnya.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/test/Nyanbyte.Sysnya.CoreTests/Binary/BinaryReaderTests.cs b/test/Nyanbyte.Sysnya.CoreTests/Binary/BinaryReaderTests.cs new file mode 100644 index 0000000..4c62b1d --- /dev/null +++ b/test/Nyanbyte.Sysnya.CoreTests/Binary/BinaryReaderTests.cs @@ -0,0 +1,42 @@ +using Nyanbyte.Sysnya.Binary; + +namespace Nyanbyte.Sysnya.CoreTests.Binary; + +public class BinaryReaderTests +{ + [Fact] + public void BinaryEnumeratorReadTestsBigEndian() + { + // Given + byte[] memory = { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; + BinaryEnumerator reader = new(memory, true); + + // When + reader.ReadInt32(out int valA); + reader.ReadInt32(out int valB); + reader.ReadInt64(out long valC); + + // Then + Assert.Equal(0x1, valA); + Assert.Equal(0x1000000, valB); + Assert.Equal(0x1, valC); + } + + [Fact] + public void BinaryEnumeratorReadTestsLittleEndian() + { + // Given + byte[] memory = { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; + BinaryEnumerator reader = new(memory, false); + + // When + reader.ReadInt32(out int valA); + reader.ReadInt32(out int valB); + reader.ReadInt64(out long valC); + + // Then + Assert.Equal(0x1000000, valA); + Assert.Equal(0x1, valB); + Assert.Equal(0x100000000000000, valC); + } +} diff --git a/test/Nyanbyte.Sysnya.CoreTests/Nyanbyte.Sysnya.CoreTests.csproj b/test/Nyanbyte.Sysnya.CoreTests/Nyanbyte.Sysnya.CoreTests.csproj new file mode 100644 index 0000000..e56193c --- /dev/null +++ b/test/Nyanbyte.Sysnya.CoreTests/Nyanbyte.Sysnya.CoreTests.csproj @@ -0,0 +1,28 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/test/Nyanbyte.Sysnya.CoreTests/Usings.cs b/test/Nyanbyte.Sysnya.CoreTests/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/test/Nyanbyte.Sysnya.CoreTests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file