Added int overloads, add bool functionality to ByteTag

This commit is contained in:
Eric Freed 2023-08-26 20:07:35 -04:00
parent 70a5dd2c62
commit 6c26878ad7
4 changed files with 65 additions and 6 deletions

View File

@ -12,6 +12,7 @@ namespace SharpNBT;
/// actually serialized as.
/// </remarks>
[PublicAPI][Serializable]
[Obsolete("Use the IsBool and Bool properties of ByteTag. This class will be removed in a future release.")]
public class BoolTag : Tag<bool>
{
private const string TRUE = "true";

View File

@ -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<byte>
{
/// <summary>
/// Gets a flag indicating if this <see cref="ByteTag"/> was assigned a <see cref="bool"/> value.
/// </summary>
public bool IsBool { get; private set; }
/// <inheritdoc cref="Tag{T}.Value"/>
public new byte Value
{
get => base.Value;
set
{
base.Value = value;
IsBool = false;
}
}
/// <summary>
/// Gets or sets the value of this tag as an unsigned value.
/// </summary>
@ -26,7 +41,24 @@ public class ByteTag : Tag<byte>
public sbyte SignedValue
{
get => unchecked((sbyte)Value);
set => Value = unchecked((byte)value);
set
{
Value = unchecked((byte)value);
IsBool = false;
}
}
/// <summary>
/// Gets or sets the value of this tag as a boolean value.
/// </summary>
public bool Bool
{
get => Value != 0;
set
{
Value = value ? (byte)1 : (byte)0;
IsBool = true;
}
}
/// <summary>
@ -37,6 +69,17 @@ public class ByteTag : Tag<byte>
public ByteTag(string? name, byte value) : base(TagType.Byte, name, value)
{
}
/// <inheritdoc cref="ByteTag(string,byte)"/>
/// <remarks>The use of <see cref="int"/> is for convenience only.</remarks>
public ByteTag(string? name, int value) : base(TagType.Byte, name, unchecked((byte) (value & 0xFF)))
{
}
/// <inheritdoc cref="ByteTag(string,byte)"/>
public ByteTag(string? name, bool value) : base(TagType.Byte, name, value ? (byte) 1 : (byte) 0)
{
}
/// <inheritdoc cref="ByteTag(string,byte)"/>
[CLSCompliant(false)]
@ -62,6 +105,13 @@ public class ByteTag : Tag<byte>
/// <param name="tag">The tag to convert.</param>
/// <returns>The tag represented as a <see cref="byte"/>.</returns>
public static implicit operator byte(ByteTag tag) => tag.Value;
/// <summary>
/// Implicit conversion of this tag to a <see cref="bool"/>.
/// </summary>
/// <param name="tag">The tag to convert.</param>
/// <returns>The tag represented as a <see cref="byte"/>.</returns>
public static implicit operator bool(ByteTag tag) => tag.Bool;
/// <summary>
/// Implicit conversion of this tag to a <see cref="sbyte"/>.

View File

@ -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<T> : Tag, IList<T>
/// <summary>
/// Internal list implementation.
/// </summary>
[ItemNotNull]
private readonly List<T> internalList = new List<T>();
/// <summary>
@ -99,7 +101,7 @@ public abstract class EnumerableTag<T> : Tag, IList<T>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.</exception>
/// <footer><a href="https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.ICollection-1.Add?view=netcore-5.0">`ICollection.Add` on docs.microsoft.com</a></footer>
[SuppressMessage("ReSharper", "AnnotationConflictInHierarchy")]
public virtual void Add([NotNull] T item) => internalList.Add(item);
public virtual void Add([DisallowNull] T item) => internalList.Add(item);
/// <summary>
/// Adds the elements of the specified collection to the <see cref="EnumerableTag{T}"/>.
@ -108,7 +110,7 @@ public abstract class EnumerableTag<T> : Tag, IList<T>
public void AddRange([ItemNotNull] IEnumerable<T> items)
{
foreach (var item in items)
Add(item);
Add(item!);
}
/// <summary>Inserts an item to the <see cref="T:System.Collections.Generic.IList`1" /> at the specified index.</summary>
@ -119,7 +121,7 @@ public abstract class EnumerableTag<T> : Tag, IList<T>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1" /> is read-only.</exception>
/// <footer><a href="https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IList-1.Insert?view=netcore-5.0">`IList.Insert` on docs.microsoft.com</a></footer>
[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);
/// <summary>Gets or sets the element at the specified index.</summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
@ -128,7 +130,7 @@ public abstract class EnumerableTag<T> : Tag, IList<T>
/// <exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IList`1" /> is read-only.</exception>
/// <returns>The element at the specified index.</returns>
/// <footer><a href="https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IList-1.Item?view=netcore-5.0">`IList.Item` on docs.microsoft.com</a></footer>
[NotNull]
[DisallowNull]
public virtual T this[int index]
{
get => internalList[index];

View File

@ -31,6 +31,12 @@ public class ShortTag : Tag<short>
public ShortTag(string? name, short value) : base(TagType.Short, name, value)
{
}
/// <inheritdoc cref="ByteTag(string,byte)"/>
/// <remarks>The use of <see cref="int"/> is for convenience only.</remarks>
public ShortTag(string? name, int value) : base(TagType.Short, name, unchecked((byte) (value & 0xFFFF)))
{
}
/// <inheritdoc cref="ShortTag(string,short)"/>
[CLSCompliant(false)]