From 51efe12e547126266cf47f4be7efe1a394e7512d Mon Sep 17 00:00:00 2001 From: ForeverZer0 Date: Wed, 25 Aug 2021 04:52:05 -0400 Subject: [PATCH] Moved BaseStream field to TagIO --- SharpNBT/SharpNBT.csproj | 4 ++++ SharpNBT/TagBuilder.cs | 21 ++------------------- SharpNBT/TagIO.cs | 11 ++++++++++- SharpNBT/TagReader.cs | 20 +++++++++++--------- SharpNBT/TagWriter.cs | 9 +-------- 5 files changed, 28 insertions(+), 37 deletions(-) diff --git a/SharpNBT/SharpNBT.csproj b/SharpNBT/SharpNBT.csproj index bc72746..b1c5d3e 100644 --- a/SharpNBT/SharpNBT.csproj +++ b/SharpNBT/SharpNBT.csproj @@ -12,6 +12,10 @@ icon.png nbt;named binary tag;minecraft;serialization;java;bedrock;pocket edition;varint;varlong + + + bin\Release\netstandard2.1\SharpNBT.xml + diff --git a/SharpNBT/TagBuilder.cs b/SharpNBT/TagBuilder.cs index e16912a..601ffba 100644 --- a/SharpNBT/TagBuilder.cs +++ b/SharpNBT/TagBuilder.cs @@ -243,17 +243,7 @@ namespace SharpNBT /// public TagBuilder AddIntArray([NotNull] IEnumerable values) => AddIntArray(null, values.ToArray()); - - - - - - - - - - - + /// /// Adds a new with the specified to the tree at the current depth. /// @@ -274,14 +264,7 @@ namespace SharpNBT /// public TagBuilder AddLongArray([NotNull] IEnumerable values) => AddLongArray(null, values.ToArray()); - - - - - - - - + /// /// Adds an existing object to the tree at the current depth. /// diff --git a/SharpNBT/TagIO.cs b/SharpNBT/TagIO.cs index 20249af..8d02311 100644 --- a/SharpNBT/TagIO.cs +++ b/SharpNBT/TagIO.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Threading.Tasks; using JetBrains.Annotations; @@ -10,6 +11,12 @@ namespace SharpNBT [PublicAPI] public abstract class TagIO : IDisposable { + /// + /// Gets the underlying stream this instance is operating on. + /// + [NotNull] + protected Stream BaseStream { get; } + /// /// Gets a flag indicating if byte swapping is required for numeric values, accounting for both the endianness of the host machine and the /// specified . @@ -32,8 +39,10 @@ namespace SharpNBT /// public FormatOptions FormatOptions { get; } - protected TagIO(FormatOptions options) + protected TagIO([NotNull] Stream stream, FormatOptions options) { + BaseStream = stream ?? throw new ArgumentNullException(nameof(stream)); + if (options.HasFlag(FormatOptions.BigEndian)) SwapEndian = BitConverter.IsLittleEndian; else if (options.HasFlag(FormatOptions.LittleEndian)) diff --git a/SharpNBT/TagReader.cs b/SharpNBT/TagReader.cs index 016d44e..588d798 100644 --- a/SharpNBT/TagReader.cs +++ b/SharpNBT/TagReader.cs @@ -18,14 +18,11 @@ namespace SharpNBT /// public event TagReaderCallback TagRead; - public event TagReaderCallback TagEncountered; - /// - /// Gets the underlying stream this is operating on. + /// Occurs when a tag has been encountered in the stream, after reading the first byte to determine its . /// - [NotNull] - protected Stream BaseStream { get; } - + public event TagReaderCallback TagEncountered; + private readonly bool leaveOpen; /// @@ -36,9 +33,8 @@ namespace SharpNBT /// /// to leave the object open after disposing the /// object; otherwise, . - public TagReader([NotNull] Stream stream, FormatOptions options, bool leaveOpen = false) : base(options) + public TagReader([NotNull] Stream stream, FormatOptions options, bool leaveOpen = false) : base(stream, options) { - BaseStream = stream ?? throw new ArgumentNullException(nameof(stream)); if (!stream.CanRead) throw new IOException("Stream is not opened for reading."); this.leaveOpen = leaveOpen; @@ -447,11 +443,17 @@ namespace SharpNBT } /// - /// Invokes the event when a tag has been fully deserialized from the . + /// Invokes the event when a tag has been fully deserialized from the . /// /// The deserialized instance. protected virtual void OnTagRead(Tag tag) => TagRead?.Invoke(this, new TagEventArgs(tag.Type, tag)); + /// + /// Invokes the event when the stream is positioned at the beginning of a an unread tag. + /// + /// The type of tag next to be read from the stream. + /// Flag indicating if this tag is named. + /// When handled by an event subscriber, returns a parsed instance, otherwise returns . [CanBeNull] protected virtual Tag OnTagEncountered(TagType type, bool named) { diff --git a/SharpNBT/TagWriter.cs b/SharpNBT/TagWriter.cs index 36c2b63..e80aceb 100644 --- a/SharpNBT/TagWriter.cs +++ b/SharpNBT/TagWriter.cs @@ -16,12 +16,6 @@ namespace SharpNBT public class TagWriter : TagIO { private readonly bool leaveOpen; - - /// - /// Gets the underlying stream this is operating on. - /// - [NotNull] - protected Stream BaseStream { get; } /// /// Creates a new instance of the class from the given . @@ -31,9 +25,8 @@ namespace SharpNBT /// /// to leave the object open after disposing the /// object; otherwise, . - public TagWriter([NotNull] Stream stream, FormatOptions options, bool leaveOpen = false) : base(options) + public TagWriter([NotNull] Stream stream, FormatOptions options, bool leaveOpen = false) : base(stream, options) { - BaseStream = stream ?? throw new ArgumentNullException(nameof(stream)); if (!stream.CanWrite) throw new IOException("Stream is not opened for writing."); this.leaveOpen = leaveOpen;