Core + binary enumerator

This commit is contained in:
femsci 2023-11-14 15:42:11 +01:00
commit 0cbce3c165
Signed by: femsci
GPG key ID: 08F7911F0E650C67
8 changed files with 192 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
**/obj/
**/bin/

36
Nyanbyte.Sysnya.sln Normal file
View file

@ -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

View file

@ -0,0 +1,49 @@
using System.Buffers.Binary;
namespace Nyanbyte.Sysnya.Binary;
public class BinaryEnumerator
{
public BinaryEnumerator(ReadOnlyMemory<byte> memory, bool bigEndian = true)
{
_fullData = memory;
_bigEndian = bigEndian;
}
//endianness := true -> big; false -> smol
private bool _bigEndian = true;
private readonly ReadOnlyMemory<byte> _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;
}
}

View file

@ -0,0 +1,25 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
namespace Nyanbyte.Sysnya.Binary;
public class BinaryStructMapperBuilder<T>
{
public BinaryStructMapperBuilder(T obj)
{
ArgumentNullException.ThrowIfNull(obj);
_obj = obj;
}
[NotNull]
private readonly T _obj;
private readonly Stack<Action<Memory<byte>>> _funcStack = new();
public BinaryStructMapperBuilder<T> BindU32<M>(Expression<Func<M, T>> property)
{
var member = property.Body as MemberExpression;
ArgumentNullException.ThrowIfNull(member);
_funcStack.Push(m => _obj.GetType().GetProperty(member.Member.Name)!.SetValue(_obj, m));
return this;
}
}

View file

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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);
}
}

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Nyanbyte.Sysnya\Nyanbyte.Sysnya.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1 @@
global using Xunit;