From 4aa2959e7e1956f8e1698a18a45934e427e00cc5 Mon Sep 17 00:00:00 2001 From: ForeverZer0 Date: Tue, 24 Aug 2021 18:36:10 -0400 Subject: [PATCH] Code cleanup, added annotations --- README.md | 24 ++++++++++++++++++------ SharpNBT/EndianExtensions.cs | 8 ++++---- SharpNBT/Tags/ByteArrayTag.cs | 2 -- SharpNBT/ZLib/ZLibStream.cs | 10 +++++----- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 48d11b5..38efca5 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,15 @@ [![.NET](https://github.com/ForeverZer0/SharpNBT/actions/workflows/dotnet.yml/badge.svg)](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 -* **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. -* **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. -* **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: +* **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. +* **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. +* **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 5.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.Android 10.0 * Unity 2021.2.0b6 -* No-dependencies. -* Includes a `TagBuilder` class for an even more simple way of building a complete tag from scratch with POD types. \ No newline at end of file +* **No Dependencies:** No reliance on third-party libraries. +* **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); +``` diff --git a/SharpNBT/EndianExtensions.cs b/SharpNBT/EndianExtensions.cs index f068f1f..8e91f26 100644 --- a/SharpNBT/EndianExtensions.cs +++ b/SharpNBT/EndianExtensions.cs @@ -45,11 +45,11 @@ namespace SharpNBT /// [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong SwapEndian(this ulong val) + public static ulong SwapEndian(this ulong value) { - val = ((val << 8) & 0xFF00FF00FF00FF00UL ) | ((val >> 8) & 0x00FF00FF00FF00FFUL ); - val = ((val << 16) & 0xFFFF0000FFFF0000UL ) | ((val >> 16) & 0x0000FFFF0000FFFFUL ); - return (val << 32) | (val >> 32); + value = ((value << 8) & 0xFF00FF00FF00FF00UL) | ((value >> 8) & 0x00FF00FF00FF00FFUL); + value = ((value << 16) & 0xFFFF0000FFFF0000UL) | ((value >> 16) & 0x0000FFFF0000FFFFUL); + return (value << 32) | (value >> 32); } /// diff --git a/SharpNBT/Tags/ByteArrayTag.cs b/SharpNBT/Tags/ByteArrayTag.cs index 62ec320..0b068fa 100644 --- a/SharpNBT/Tags/ByteArrayTag.cs +++ b/SharpNBT/Tags/ByteArrayTag.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; using System.Runtime.Serialization; using JetBrains.Annotations; diff --git a/SharpNBT/ZLib/ZLibStream.cs b/SharpNBT/ZLib/ZLibStream.cs index 2c702cb..651f9cf 100644 --- a/SharpNBT/ZLib/ZLibStream.cs +++ b/SharpNBT/ZLib/ZLibStream.cs @@ -30,7 +30,7 @@ namespace SharpNBT.ZLib /// /// A instance to be compressed. /// The level of compression to use. - 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 /// /// A instance to be compressed or uncompressed. /// The type of compression to use. - 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 /// A instance to be compressed. /// The level of compression to use. /// Indicates if the should be left open after this is closed. - public ZLibStream(Stream stream, CompressionLevel level, bool leaveOpen) + public ZLibStream([NotNull] Stream stream, CompressionLevel level, bool leaveOpen) { compressionMode = CompressionMode.Compress; this.leaveOpen = leaveOpen; @@ -65,7 +65,7 @@ namespace SharpNBT.ZLib /// A instance to be compressed or uncompressed. /// The type of compression to use. /// Indicates if the should be left open after this is closed. - public ZLibStream(Stream stream, CompressionMode mode, bool leaveOpen) + public ZLibStream([NotNull] Stream stream, CompressionMode mode, bool leaveOpen) { compressionMode = mode; this.leaveOpen = leaveOpen; @@ -233,7 +233,7 @@ namespace SharpNBT.ZLib /// The stream does not support writing. /// The stream has been disposed. /// The stream is currently in use by a previous write operation. - 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); adler32.Update(new ReadOnlySpan(buffer, offset, count));