Skip to content

Commit

Permalink
Merge pull request #9 from snowberry-software/develop
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
VNNCC authored Jan 12, 2025
2 parents b8ecb72 + 3a48fc0 commit 6807d5a
Show file tree
Hide file tree
Showing 46 changed files with 1,919 additions and 464 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ writer.BaseStream.Position = 0;
using var reader = new EndianStreamReader(stream);

_ = reader.ReadInt32(EndianType.BIG);
_ = reader.ReadLong(EndianType.BIG);
_ = reader.ReadInt64(EndianType.BIG);
_ = reader.ReadFloat(EndianType.BIG);
_ = reader.ReadUInt32(EndianType.BIG);
_ = reader.ReadDouble(EndianType.BIG);
Expand All @@ -79,8 +79,8 @@ var buffer = new byte[] { ... };
int offset = ...;
var endianType = EndianType.BIG;

_ = BinaryEndianConverter.ToLong(buffer, endianType);
_ = BinaryEndianConverter.ToLong(buffer, offset, endianType);
_ = BinaryEndianConverter.ToInt64(buffer, endianType);
_ = BinaryEndianConverter.ToInt64(buffer, offset, endianType);
```

## Supported data types
Expand Down
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="8.1.4" />
<PackageReference Include="Nuke.Common" Version="9.0.3" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/FrameworkCompatibility.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project>
<!-- Framework Compatibility -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.6.0" />
<PackageReference Include="IndexRange" Version="1.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
</ItemGroup>
</Project>
15 changes: 2 additions & 13 deletions src/NuGetPackage.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>

<Authors>Snowberry Software</Authors>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<VersionPrefix>$(AssemblyVersion)</VersionPrefix>

<IsPackable>true</IsPackable>
Expand All @@ -14,7 +14,7 @@

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>true</IncludeBuildOutput>
<Copyright>Copyright © 2024 Snowberry Software</Copyright>
<Copyright>Copyright © 2025 Snowberry Software</Copyright>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageProjectUrl>https://github.com/snowberry-software/Snowberry.IO</PackageProjectUrl>

Expand Down Expand Up @@ -49,15 +49,4 @@
<!-- For GitHub -->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<!-- Framework Compatibility -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.6.0" />
<PackageReference Include="IndexRange" Version="1.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public long BitConverter_Int64()
[Benchmark]
public unsafe long BinaryEndianConverter_Int64()
{
return BinaryEndianConverter.ToLong(_data.AsSpan(), EndianType.LITTLE);
return BinaryEndianConverter.ToInt64(_data.AsSpan(), EndianType.LITTLE);
}

[Benchmark]
Expand All @@ -33,7 +33,7 @@ public long BitConverter_Offset_Int64()
[Benchmark]
public unsafe long BinaryEndianConverter_Offset_Int64()
{
return BinaryEndianConverter.ToLong(_data.AsSpan(), 4, EndianType.LITTLE);
return BinaryEndianConverter.ToInt64(_data.AsSpan(), 4, EndianType.LITTLE);
}

[Benchmark]
Expand All @@ -47,7 +47,7 @@ public long BitConverter_BigE_Int64()
[Benchmark]
public unsafe long BinaryEndianConverter_BigE_Int64()
{
return BinaryEndianConverter.ToLong(_data.AsSpan(), EndianType.BIG);
return BinaryEndianConverter.ToInt64(_data.AsSpan(), EndianType.BIG);
}

[Benchmark]
Expand All @@ -61,6 +61,6 @@ public long BitConverter_Offset_BigE_Int64()
[Benchmark]
public unsafe long BinaryEndianConverter_Offset_BigE_Int64()
{
return BinaryEndianConverter.ToLong(_data.AsSpan(), 4, EndianType.BIG);
return BinaryEndianConverter.ToInt64(_data.AsSpan(), 4, EndianType.BIG);
}
}
6 changes: 3 additions & 3 deletions src/Snowberry.IO.Common/BinaryEndianConverter.Offsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static partial class BinaryEndianConverter
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
#endif
public static unsafe long ToLong(Span<byte> data, int offset, EndianType endian)
public static unsafe long ToInt64(Span<byte> data, int offset, EndianType endian)
{
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<long>(ref data[offset]);
Expand All @@ -23,7 +23,7 @@ public static unsafe long ToLong(Span<byte> data, int offset, EndianType endian)
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
#endif
public static unsafe ulong ToULong(Span<byte> data, int offset, EndianType endian)
public static unsafe ulong ToUInt64(Span<byte> data, int offset, EndianType endian)
{
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<ulong>(ref data[offset]);
Expand Down Expand Up @@ -137,7 +137,7 @@ public static unsafe double ToDouble(Span<byte> data, int offset, EndianType end
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<double>(ref data[offset]);

long temp = ToLong(data, offset, EndianType.BIG);
long temp = ToInt64(data, offset, EndianType.BIG);
return BitConverter.Int64BitsToDouble(temp);
}

Expand Down
22 changes: 11 additions & 11 deletions src/Snowberry.IO.Common/BinaryEndianConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static partial class BinaryEndianConverter

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe int ToInt32(Span<byte> data, EndianType endian)
{
Expand All @@ -25,7 +25,7 @@ public static unsafe int ToInt32(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe uint ToUInt32(Span<byte> data, EndianType endian)
{
Expand All @@ -38,7 +38,7 @@ public static unsafe uint ToUInt32(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe ushort ToUInt16(Span<byte> data, EndianType endian)
{
Expand All @@ -51,7 +51,7 @@ public static unsafe ushort ToUInt16(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe short ToInt16(Span<byte> data, EndianType endian)
{
Expand All @@ -64,7 +64,7 @@ public static unsafe short ToInt16(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe float ToFloat(Span<byte> data, EndianType endian)
{
Expand All @@ -82,22 +82,22 @@ public static unsafe float ToFloat(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe double ToDouble(Span<byte> data, EndianType endian)
{
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<double>(ref MemoryMarshal.GetReference(data));

long temp = ToLong(data, EndianType.BIG);
long temp = ToInt64(data, EndianType.BIG);
return BitConverter.Int64BitsToDouble(temp);
}

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe long ToLong(Span<byte> data, EndianType endian)
public static unsafe long ToInt64(Span<byte> data, EndianType endian)
{
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<long>(ref MemoryMarshal.GetReference(data));
Expand All @@ -109,9 +109,9 @@ public static unsafe long ToLong(Span<byte> data, EndianType endian)

#if NETCOREAPP3_0_OR_GREATER
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
#endif
public static unsafe ulong ToULong(Span<byte> data, EndianType endian)
public static unsafe ulong ToUInt64(Span<byte> data, EndianType endian)
{
if (endian == EndianType.LITTLE)
return Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data));
Expand Down
8 changes: 1 addition & 7 deletions src/Snowberry.IO.Common/Interfaces/IBinaryModelMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snowberry.IO.Common.Interfaces;
namespace Snowberry.IO.Common.Interfaces;

/// <summary>
/// Used for metadata about a binary model.
Expand Down
3 changes: 1 addition & 2 deletions src/Snowberry.IO.Common/Reader/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public abstract class Analyzer
/// <param name="reader">The current reader instance.</param>
/// <param name="buffer">The span of bytes to analyze.</param>
/// <param name="amount">The amount of bytes that were read.</param>
/// <param name="offset">The offset in the <paramref name="buffer"/> for the new read data, otherwise <see langword="-1"/>.</param>
public abstract void AnalyzeReadBytes(IEndianReader reader, Span<byte> buffer, int amount, long offset = -1);
public abstract void AnalyzeReadBytes(IEndianReader reader, Span<byte> buffer, int amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ public partial interface IEndianReader
/// of bytes requested if that many bytes are not currently available, or zero if the end of the stream is reached.
/// </returns>
int Read(Span<byte> buffer);

/// <summary>
/// Reads bytes from the current stream and advances the position within the stream until the <paramref name="buffer" /> is filled.
/// </summary>
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.</param>
void ReadExactly(Span<byte> buffer);
}
32 changes: 20 additions & 12 deletions src/Snowberry.IO.Common/Reader/Interfaces/IEndianReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public partial interface IEndianReader : IDisposable
/// <summary>
/// Reads a single byte from the current stream.
/// </summary>
/// <remarks>
/// This method does not check if the current <see cref="Position"/> is greater than the current <see cref="Length"/>.
/// Avoid using this method in loops as it can lead to infinite loops if the end of the stream is reached.
/// </remarks>
/// <returns>The byte read.</returns>
byte ReadByte();

/// <summary>
/// Reads a signed byte from the current stream.
/// </summary>
/// <returns>A signed byte read from the current stream.</returns>
sbyte ReadSByte();

/// <summary>
/// Reads a single byte from the current stream, returning -1 if the <see cref="Position"/> is greater than the <see cref="Length"/>.
/// </summary>
Expand All @@ -51,6 +53,14 @@ public partial interface IEndianReader : IDisposable
/// </returns>
int Read(byte[] buffer, int offset, int byteCount);

/// <summary>
/// Reads bytes from the current stream and advances the position within the stream until the specified number of bytes is read.
/// </summary>
/// <param name="buffer">The byte array to which the data will be read. When this method returns, the specified region of this array is replaced by the bytes read from the current stream.</param>
/// <param name="offset">The zero-based byte offset in the <paramref name="buffer"/> at which to begin storing the data read from the stream.</param>
/// <param name="byteCount">The exact number of bytes to read from the stream.</param>
void ReadExactly(byte[] buffer, int offset, int byteCount);

/// <summary>
/// Reads a specified number of bytes from the current stream into the internal buffer.
/// </summary>
Expand All @@ -66,14 +76,14 @@ public partial interface IEndianReader : IDisposable
/// Reads a specified number of bytes from the current stream.
/// </summary>
/// <param name="count">The number of bytes to read.</param>
/// <returns>A byte array containing the read bytes.</returns>
/// <returns>A byte array containing the read bytes. This might be less than the number of bytes requested if the end of the stream is reached.</returns>
byte[] ReadBytes(int count);

/// <summary>
/// Reads a null-terminated string from the current stream.
/// </summary>
/// <returns>A zero-terminated string, or <see langword="null"/> if the end of the stream is reached.</returns>
string? ReadCString();
string ReadCString();

/// <summary>
/// Reads a fixed-size null-terminated string from the current stream.
Expand All @@ -85,13 +95,13 @@ public partial interface IEndianReader : IDisposable
/// <param name="size">The size of the string to read.</param>
/// <param name="adjustPosition">Automatically adjust the <see cref="Position"/> by the unread count of <paramref name="size"/>.</param>
/// <returns>The fixed-size string.</returns>
string? ReadSizedCString(int size, bool adjustPosition = true);
string ReadSizedCString(int size, bool adjustPosition = true);

/// <summary>
/// Reads a size-prefixed string from the current stream.
/// </summary>
/// <returns>The size-prefixed string, or <see langword="null"/> if the end of the stream is reached.</returns>
string? ReadString();
string ReadString();

/// <summary>
/// Reads a line of characters from the current stream.
Expand Down Expand Up @@ -123,14 +133,14 @@ public partial interface IEndianReader : IDisposable
/// </summary>
/// <param name="endian">The endianness to use.</param>
/// <returns>The read 64-bit signed integer.</returns>
long ReadLong(EndianType endian = EndianType.LITTLE);
long ReadInt64(EndianType endian = EndianType.LITTLE);

/// <summary>
/// Reads a 64-bit unsigned integer from the current stream with the specified endianness.
/// </summary>
/// <param name="endian">The endianness to use.</param>
/// <returns>The read 64-bit unsigned integer.</returns>
ulong ReadULong(EndianType endian = EndianType.LITTLE);
ulong ReadUInt64(EndianType endian = EndianType.LITTLE);

/// <summary>
/// Reads a 32-bit unsigned integer from the current stream with the specified endianness.
Expand Down Expand Up @@ -268,9 +278,7 @@ public partial interface IEndianReader : IDisposable
/// <summary>
/// Gets the internal buffer of the reader.
/// </summary>
#pragma warning disable CA1819 // Properties should not return arrays
byte[] Buffer { get; }
#pragma warning restore CA1819 // Properties should not return arrays

/// <summary>
/// Gets a value indicating whether the reader is disposed.
Expand Down
2 changes: 0 additions & 2 deletions src/Snowberry.IO.Common/Reader/RegionRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ namespace Snowberry.IO.Common.Reader;
/// </summary>
public struct RegionRange : IEquatable<RegionRange>
{
#pragma warning disable CA1051 // Do not declare visible instance fields
public long StartPosition;
public long Size;
#pragma warning restore CA1051 // Do not declare visible instance fields

/// <summary>
/// Uses the <see cref="Range"/> to create a new <see cref="RegionRange"/>.
Expand Down
2 changes: 2 additions & 0 deletions src/Snowberry.IO.Common/Snowberry.IO.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<Import Project="../FrameworkCompatibility.props" />
</Project>
12 changes: 12 additions & 0 deletions src/Snowberry.IO.SingleFile.CLI/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using CommandLine;

namespace Snowberry.IO.SingleFile.CLI;

internal class Options
{
[Option('i', "input", Required = true, HelpText = "Sets the input single file path.")]
public string InputFilePath { get; set; } = string.Empty;

[Option('o', "output", Required = false, HelpText = "Sets the output directory.")]
public string OutputDirectory { get; set; } = string.Empty;
}
Loading

0 comments on commit 6807d5a

Please sign in to comment.