From 6c26878ad78427700523dc15e59abd408c6517a1 Mon Sep 17 00:00:00 2001 From: Eric Freed Date: Sat, 26 Aug 2023 20:07:35 -0400 Subject: [PATCH] Added int overloads, add bool functionality to ByteTag --- SharpNBT/Tags/BoolTag.cs | 1 + SharpNBT/Tags/ByteTag.cs | 54 ++++++++++++++++++++++++++++++++-- SharpNBT/Tags/EnumerableTag.cs | 10 ++++--- SharpNBT/Tags/ShortTag.cs | 6 ++++ 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/SharpNBT/Tags/BoolTag.cs b/SharpNBT/Tags/BoolTag.cs index 1f23fb7..2e98395 100644 --- a/SharpNBT/Tags/BoolTag.cs +++ b/SharpNBT/Tags/BoolTag.cs @@ -12,6 +12,7 @@ namespace SharpNBT; /// actually serialized as. /// [PublicAPI][Serializable] +[Obsolete("Use the IsBool and Bool properties of ByteTag. This class will be removed in a future release.")] public class BoolTag : Tag { private const string TRUE = "true"; diff --git a/SharpNBT/Tags/ByteTag.cs b/SharpNBT/Tags/ByteTag.cs index 9c1c8e1..c70124d 100644 --- a/SharpNBT/Tags/ByteTag.cs +++ b/SharpNBT/Tags/ByteTag.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.Serialization; -using System.Text; using JetBrains.Annotations; namespace SharpNBT; @@ -16,6 +15,22 @@ namespace SharpNBT; [PublicAPI][Serializable] public class ByteTag : Tag { + /// + /// Gets a flag indicating if this was assigned a value. + /// + public bool IsBool { get; private set; } + + /// + public new byte Value + { + get => base.Value; + set + { + base.Value = value; + IsBool = false; + } + } + /// /// Gets or sets the value of this tag as an unsigned value. /// @@ -26,7 +41,24 @@ public class ByteTag : Tag public sbyte SignedValue { get => unchecked((sbyte)Value); - set => Value = unchecked((byte)value); + set + { + Value = unchecked((byte)value); + IsBool = false; + } + } + + /// + /// Gets or sets the value of this tag as a boolean value. + /// + public bool Bool + { + get => Value != 0; + set + { + Value = value ? (byte)1 : (byte)0; + IsBool = true; + } } /// @@ -37,6 +69,17 @@ public class ByteTag : Tag public ByteTag(string? name, byte value) : base(TagType.Byte, name, value) { } + + /// + /// The use of is for convenience only. + public ByteTag(string? name, int value) : base(TagType.Byte, name, unchecked((byte) (value & 0xFF))) + { + } + + /// + public ByteTag(string? name, bool value) : base(TagType.Byte, name, value ? (byte) 1 : (byte) 0) + { + } /// [CLSCompliant(false)] @@ -62,6 +105,13 @@ public class ByteTag : Tag /// The tag to convert. /// The tag represented as a . public static implicit operator byte(ByteTag tag) => tag.Value; + + /// + /// Implicit conversion of this tag to a . + /// + /// The tag to convert. + /// The tag represented as a . + public static implicit operator bool(ByteTag tag) => tag.Bool; /// /// Implicit conversion of this tag to a . diff --git a/SharpNBT/Tags/EnumerableTag.cs b/SharpNBT/Tags/EnumerableTag.cs index 3c6db0e..c54b89a 100644 --- a/SharpNBT/Tags/EnumerableTag.cs +++ b/SharpNBT/Tags/EnumerableTag.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; using System.Text; using JetBrains.Annotations; @@ -18,6 +19,7 @@ public abstract class EnumerableTag : Tag, IList /// /// Internal list implementation. /// + [ItemNotNull] private readonly List internalList = new List(); /// @@ -99,7 +101,7 @@ public abstract class EnumerableTag : Tag, IList /// The is read-only. /// [SuppressMessage("ReSharper", "AnnotationConflictInHierarchy")] - public virtual void Add([NotNull] T item) => internalList.Add(item); + public virtual void Add([DisallowNull] T item) => internalList.Add(item); /// /// Adds the elements of the specified collection to the . @@ -108,7 +110,7 @@ public abstract class EnumerableTag : Tag, IList public void AddRange([ItemNotNull] IEnumerable items) { foreach (var item in items) - Add(item); + Add(item!); } /// Inserts an item to the at the specified index. @@ -119,7 +121,7 @@ public abstract class EnumerableTag : Tag, IList /// The is read-only. /// [SuppressMessage("ReSharper", "AnnotationConflictInHierarchy")] - public virtual void Insert(int index, [NotNull] T item) => internalList.Insert(index, item); + public virtual void Insert(int index, [DisallowNull] T item) => internalList.Insert(index, item); /// Gets or sets the element at the specified index. /// The zero-based index of the element to get or set. @@ -128,7 +130,7 @@ public abstract class EnumerableTag : Tag, IList /// The property is set and the is read-only. /// The element at the specified index. /// - [NotNull] + [DisallowNull] public virtual T this[int index] { get => internalList[index]; diff --git a/SharpNBT/Tags/ShortTag.cs b/SharpNBT/Tags/ShortTag.cs index 1ad6dc6..6cb95d7 100644 --- a/SharpNBT/Tags/ShortTag.cs +++ b/SharpNBT/Tags/ShortTag.cs @@ -31,6 +31,12 @@ public class ShortTag : Tag public ShortTag(string? name, short value) : base(TagType.Short, name, value) { } + + /// + /// The use of is for convenience only. + public ShortTag(string? name, int value) : base(TagType.Short, name, unchecked((byte) (value & 0xFFFF))) + { + } /// [CLSCompliant(false)]