Code cleanup, added annotations
This commit is contained in:
parent
fb5465ab7c
commit
4aa2959e7e
24
README.md
24
README.md
|
@ -2,13 +2,15 @@
|
||||||
|
|
||||||
[](https://github.com/ForeverZer0/SharpNBT/actions/workflows/dotnet.yml)
|
[](https://github.com/ForeverZer0/SharpNBT/actions/workflows/dotnet.yml)
|
||||||
|
|
||||||
A CLS-compliant implementation of the Named Binary Tag (NBT) specification, written in pure C# with no external dependencies and targeting .NET Standard 2.1 to support a wide variety of .NET implementations on all platforms.
|
A CLS-compliant implementation of the Named Binary Tag (NBT) specifications (Java/Bedrock), written in pure C# with no external dependencies and targeting a wide variety of .NET implementations and languages on all platforms.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* **Ease-of-use:** The structure revolves around a class inherited from a standard `Stream` object, which works and behaves exactly as would be expected for reading/writing NBT tags, no need to waste time frequently consulting API documentation.
|
* **Java/Bedrock Support:** Supports all NBT protocols used by different versions of Minecraft, including: Java, Bedrock (file protocol), and Bedrock (network protocol), including full support for either GZip or ZLib compression.
|
||||||
* **Performance:** Leverages the power of C# 8.0 language features, including `Span`, `MemoryMarshal`, etc. This allows for a type-safe way to reinterpret raw buffers without pointers or making unnecessary copies of buffers, a common pitfall with serialization in a type-safe language.
|
* **Ease-of-use:** An intuitive API design, following the style and conventions of the .NET runtime, with full Intellisense for every public member: Spend more time being productive and less time digging through documentation.
|
||||||
* **Cross-platform and cross-language (CLR) support:** Supports any CLR language (i.e. C#, Visual Basic, F#, etc.) for the following runtime versions or greater:
|
* **Performance:** Leverages the power of modern C# language features, including `Span`, `MemoryMarshal`, etc. This allows for a type-safe way to reinterpret raw buffers without pointers or making unnecessary copies of buffers, a common pitfall with serialization in type-safe languages.
|
||||||
|
* **Concurrency:** Includes standard async/await concurrency patterns for reading and writing.
|
||||||
|
* **Cross-platform and cross-language support:** Fully CLR compliant and build against .NET STandard 2.1, allowing supports for any CLR language (i.e. C#, Visual Basic, F#, etc.) for the following runtime versions or greater:
|
||||||
* .NET Standard 2.1
|
* .NET Standard 2.1
|
||||||
* .NET 5.0
|
* .NET 5.0
|
||||||
* .NET Core 3.0
|
* .NET Core 3.0
|
||||||
|
@ -17,5 +19,15 @@ A CLS-compliant implementation of the Named Binary Tag (NBT) specification, writ
|
||||||
* Xamarin.Mac 5.16
|
* Xamarin.Mac 5.16
|
||||||
* Xamarin.Android 10.0
|
* Xamarin.Android 10.0
|
||||||
* Unity 2021.2.0b6
|
* Unity 2021.2.0b6
|
||||||
* No-dependencies.
|
* **No Dependencies:** No reliance on third-party libraries.
|
||||||
* Includes a `TagBuilder` class for an even more simple way of building a complete tag from scratch with POD types.
|
* **Format Conversion:** Can convert NBT to/from JSON and XML formats.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Reading
|
||||||
|
|
||||||
|
Reading an NBT document can be as simple as a one-liner:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
CompoundTag tag = NbtFile.Read("/path/to/file.nbt", FormatOptions.Java, CompressionType.AutoDetect);
|
||||||
|
```
|
||||||
|
|
|
@ -45,11 +45,11 @@ namespace SharpNBT
|
||||||
/// <inheritdoc cref="SwapEndian(short)"/>
|
/// <inheritdoc cref="SwapEndian(short)"/>
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong SwapEndian(this ulong val)
|
public static ulong SwapEndian(this ulong value)
|
||||||
{
|
{
|
||||||
val = ((val << 8) & 0xFF00FF00FF00FF00UL ) | ((val >> 8) & 0x00FF00FF00FF00FFUL );
|
value = ((value << 8) & 0xFF00FF00FF00FF00UL) | ((value >> 8) & 0x00FF00FF00FF00FFUL);
|
||||||
val = ((val << 16) & 0xFFFF0000FFFF0000UL ) | ((val >> 16) & 0x0000FFFF0000FFFFUL );
|
value = ((value << 16) & 0xFFFF0000FFFF0000UL) | ((value >> 16) & 0x0000FFFF0000FFFFUL);
|
||||||
return (val << 32) | (val >> 32);
|
return (value << 32) | (value >> 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="SwapEndian(short)"/>
|
/// <inheritdoc cref="SwapEndian(short)"/>
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace SharpNBT.ZLib
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stream">A <see cref="Stream"/> instance to be compressed.</param>
|
/// <param name="stream">A <see cref="Stream"/> instance to be compressed.</param>
|
||||||
/// <param name="level">The level of compression to use.</param>
|
/// <param name="level">The level of compression to use.</param>
|
||||||
public ZLibStream(Stream stream, CompressionLevel level) : this(stream, level, false)
|
public ZLibStream([NotNull] Stream stream, CompressionLevel level) : this(stream, level, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ namespace SharpNBT.ZLib
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stream">A <see cref="Stream"/> instance to be compressed or uncompressed.</param>
|
/// <param name="stream">A <see cref="Stream"/> instance to be compressed or uncompressed.</param>
|
||||||
/// <param name="mode">The type of compression to use.</param>
|
/// <param name="mode">The type of compression to use.</param>
|
||||||
public ZLibStream(Stream stream, CompressionMode mode) : this(stream, mode, false)
|
public ZLibStream([NotNull] Stream stream, CompressionMode mode) : this(stream, mode, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ namespace SharpNBT.ZLib
|
||||||
/// <param name="stream">A <see cref="Stream"/> instance to be compressed.</param>
|
/// <param name="stream">A <see cref="Stream"/> instance to be compressed.</param>
|
||||||
/// <param name="level">The level of compression to use.</param>
|
/// <param name="level">The level of compression to use.</param>
|
||||||
/// <param name="leaveOpen">Indicates if the <paramref name="stream"/> should be left open after this <see cref="ZLibStream"/> is closed.</param>
|
/// <param name="leaveOpen">Indicates if the <paramref name="stream"/> should be left open after this <see cref="ZLibStream"/> is closed.</param>
|
||||||
public ZLibStream(Stream stream, CompressionLevel level, bool leaveOpen)
|
public ZLibStream([NotNull] Stream stream, CompressionLevel level, bool leaveOpen)
|
||||||
{
|
{
|
||||||
compressionMode = CompressionMode.Compress;
|
compressionMode = CompressionMode.Compress;
|
||||||
this.leaveOpen = leaveOpen;
|
this.leaveOpen = leaveOpen;
|
||||||
|
@ -65,7 +65,7 @@ namespace SharpNBT.ZLib
|
||||||
/// <param name="stream">A <see cref="Stream"/> instance to be compressed or uncompressed.</param>
|
/// <param name="stream">A <see cref="Stream"/> instance to be compressed or uncompressed.</param>
|
||||||
/// <param name="mode">The type of compression to use.</param>
|
/// <param name="mode">The type of compression to use.</param>
|
||||||
/// <param name="leaveOpen">Indicates if the <paramref name="stream"/> should be left open after this <see cref="ZLibStream"/> is closed.</param>
|
/// <param name="leaveOpen">Indicates if the <paramref name="stream"/> should be left open after this <see cref="ZLibStream"/> is closed.</param>
|
||||||
public ZLibStream(Stream stream, CompressionMode mode, bool leaveOpen)
|
public ZLibStream([NotNull] Stream stream, CompressionMode mode, bool leaveOpen)
|
||||||
{
|
{
|
||||||
compressionMode = mode;
|
compressionMode = mode;
|
||||||
this.leaveOpen = leaveOpen;
|
this.leaveOpen = leaveOpen;
|
||||||
|
@ -233,7 +233,7 @@ namespace SharpNBT.ZLib
|
||||||
/// <exception cref="T:System.NotSupportedException">The stream does not support writing.</exception>
|
/// <exception cref="T:System.NotSupportedException">The stream does not support writing.</exception>
|
||||||
/// <exception cref="T:System.ObjectDisposedException">The stream has been disposed.</exception>
|
/// <exception cref="T:System.ObjectDisposedException">The stream has been disposed.</exception>
|
||||||
/// <exception cref="T:System.InvalidOperationException">The stream is currently in use by a previous write operation.</exception>
|
/// <exception cref="T:System.InvalidOperationException">The stream is currently in use by a previous write operation.</exception>
|
||||||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
public override async Task WriteAsync([NotNull] byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await DeflateStream.WriteAsync(buffer, offset, count, cancellationToken);
|
await DeflateStream.WriteAsync(buffer, offset, count, cancellationToken);
|
||||||
adler32.Update(new ReadOnlySpan<byte>(buffer, offset, count));
|
adler32.Update(new ReadOnlySpan<byte>(buffer, offset, count));
|
||||||
|
|
Loading…
Reference in New Issue