Skip to content

Commit

Permalink
Removed SerializableAttribute and ISerializable interface from tags. …
Browse files Browse the repository at this point in the history
…Improved JSON output.
  • Loading branch information
ForeverZer0 committed Aug 27, 2023
1 parent 5d974ee commit 11f3c4d
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 326 deletions.
20 changes: 1 addition & 19 deletions SharpNBT/Tags/ArrayTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using JetBrains.Annotations;

Expand All @@ -13,7 +11,7 @@ namespace SharpNBT;
/// Base class for NBT tags that contain a fixed-size array of numeric types.
/// </summary>
/// <typeparam name="T">A value type that implements <see cref="INumber{TSelf}"/>.</typeparam>
[PublicAPI][Serializable]
[PublicAPI]
public abstract class ArrayTag<T> : Tag, IReadOnlyList<T> where T : unmanaged, INumber<T>
{
/// <summary>
Expand All @@ -35,22 +33,6 @@ protected ArrayTag(TagType type, string? name, T[] value) : base(type, name)
array = value;
}

/// <inheritdoc />
protected ArrayTag(SerializationInfo info, StreamingContext context) : base(info, context)
{
var _ = info.GetInt32("count");
var value = info.GetValue("values", typeof(T[])) as T[];
array = value ?? Array.Empty<T>();
}

/// <inheritdoc />
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("count", array.Length);
info.AddValue("values", array);
}

/// <inheritdoc />
public IEnumerator<T> GetEnumerator()
{
Expand Down
24 changes: 14 additions & 10 deletions SharpNBT/Tags/BoolTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Runtime.Serialization;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;
Expand All @@ -11,7 +11,7 @@ namespace SharpNBT;
/// This tag type does not exist in the NBT specification, and is included for convenience to differentiate it from the <see cref="ByteTag"/> that it is
/// actually serialized as.
/// </remarks>
[PublicAPI][Serializable]
[PublicAPI]
[Obsolete("Use the IsBool and Bool properties of ByteTag. This class will be removed in a future version.")]
public class BoolTag : Tag
{
Expand All @@ -26,16 +26,20 @@ public BoolTag(string? name, bool value) : base(TagType.Byte, name)
{
Value = value;
}

/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected BoolTag(SerializationInfo info, StreamingContext context) : base(info, context)

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
writer.WriteBoolean(Name, Value);
}
else
{
writer.WriteBooleanValue(Value);
}
}

/// <inheritdoc cref="object.ToString"/>
public override string ToString() => $"TAG_Byte({PrettyName}): {(Value ? "true" : "false")}";

Expand Down
28 changes: 18 additions & 10 deletions SharpNBT/Tags/ByteArrayTag.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;
Expand All @@ -13,7 +13,7 @@ namespace SharpNBT;
/// While this class uses the CLS compliant <see cref="byte"/> (0..255), the NBT specification uses a signed value with a range of -128..127, so ensure
/// the bits are equivalent for your values.
/// </remarks>
[PublicAPI][Serializable]
[PublicAPI]
public class ByteArrayTag : ArrayTag<byte>
{
/// <summary>
Expand Down Expand Up @@ -51,16 +51,24 @@ public ByteArrayTag(string? name, IEnumerable<byte> values) : base(TagType.ByteA
public ByteArrayTag(string? name, ReadOnlySpan<byte> values) : base(TagType.ByteArray, name, values.ToArray())
{
}

/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected ByteArrayTag(SerializationInfo info, StreamingContext context) : base(info, context)

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
writer.WriteStartArray(Name);
}
else
{
writer.WriteStartArray();
}

for (var i = 0; i < Count; i++)
writer.WriteNumberValue(this[i]);
writer.WriteEndArray();
}

/// <inheritdoc cref="object.ToString"/>
public override string ToString()
{
Expand Down
27 changes: 17 additions & 10 deletions SharpNBT/Tags/ByteTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Runtime.Serialization;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;
Expand Down Expand Up @@ -86,22 +86,29 @@ public ByteTag(string? name, bool value) : base(TagType.Byte, name, value ? (byt
public ByteTag(string? name, sbyte value) : base(TagType.Byte, name, unchecked((byte) value))
{
}

/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected ByteTag(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

/// <inheritdoc cref="object.ToString"/>
public override string ToString()
{
object obj = IsBool ? Bool : Value;
return $"TAG_Byte({PrettyName}): {obj}";
}

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
if (IsBool)
writer.WriteBoolean(Name, Bool);
else
writer.WriteNumber(Name, Value);
}
else
{
writer.WriteNumberValue(Value);
}
}

/// <summary>
/// Implicit conversion of this tag to a <see cref="byte"/>.
Expand Down
57 changes: 28 additions & 29 deletions SharpNBT/Tags/CompoundTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;
Expand All @@ -16,7 +16,7 @@ namespace SharpNBT;
/// This along with the <see cref="ListTag"/> class define the structure of the NBT format. Children are not order-dependent, nor is order guaranteed. The
/// closing <see cref="EndTag"/> does not require to be explicitly added, it will be added automatically during serialization.
/// </remarks>
[PublicAPI][Serializable]
[PublicAPI]
public class CompoundTag : Tag, IDictionary<string, Tag>, ICollection<Tag>
{
private readonly Dictionary<string, Tag> dict;
Expand All @@ -42,25 +42,7 @@ public CompoundTag(string? name, IEnumerable<Tag> values) : this(name)
dict.Add(value.Name!, AssertName(value));
}
}

/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected CompoundTag(SerializationInfo info, StreamingContext context) : base(info, context)
{
var result = info.GetValue("children", typeof(Dictionary<string, Tag>)) as Dictionary<string, Tag>;
dict = result ?? new Dictionary<string, Tag>();
}

/// <inheritdoc />
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("children", dict);
}


/// <inheritdoc />
void ICollection<KeyValuePair<string, Tag>>.Add(KeyValuePair<string, Tag> item) => dict.Add(item.Key, item.Value);

Expand Down Expand Up @@ -157,6 +139,23 @@ public TTag Get<TTag>(string name) where TTag : Tag
return (TTag)dict[name];
}

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
writer.WriteStartObject(Name);
}
else
{
writer.WriteStartObject();
}

foreach (var child in dict.Values)
child.WriteJson(writer, true);
writer.WriteEndObject();
}

/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
/// <footer><a href="https://docs.microsoft.com/en-us/dotnet/api/System.Object.ToString?view=netcore-5.0">`Object.ToString` on docs.microsoft.com</a></footer>
Expand Down Expand Up @@ -184,18 +183,18 @@ public string PrettyPrinted(string? indent = " ")
/// <param name="name">The name of the tag to search for.</param>
/// <param name="recursive"><see langword="true"/> to recursively search children, otherwise <see langword="false"/> to only search direct descendants.</param>
/// <returns>The first tag found with <paramref name="name"/>, otherwise <see langword="null"/> if none was found.</returns>
public Tag? Find(string name, bool recursive = false)
public TTag? Find<TTag>(string name, bool recursive = false) where TTag : Tag
{
foreach (var tag in dict.Values)
foreach (var (key, value) in dict)
{
if (string.CompareOrdinal(name, tag.Name) == 0)
return tag;
if (string.CompareOrdinal(name, key) == 0 && value is TTag result)
return result;

if (recursive && tag is CompoundTag child)
if (recursive && value is CompoundTag child)
{
var result = child.Find(name, true);
if (result != null)
return result;
var nested = child.Find<TTag>(name, recursive);
if (nested != null)
return nested;
}
}

Expand Down
23 changes: 13 additions & 10 deletions SharpNBT/Tags/DoubleTag.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Runtime.Serialization;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;

/// <summary>
/// A tag that contains a single IEEE-754 double-precision floating point number.
/// </summary>
[PublicAPI][Serializable]
[PublicAPI]
public class DoubleTag : NumericTag<double>
{
/// <summary>
Expand All @@ -18,14 +17,18 @@ public class DoubleTag : NumericTag<double>
public DoubleTag(string? name, double value) : base(TagType.Double, name, value)
{
}

/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected DoubleTag(SerializationInfo info, StreamingContext context) : base(info, context)

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
writer.WriteNumber(Name, Value);
}
else
{
writer.WriteNumberValue(Value);
}
}

/// <inheritdoc cref="object.ToString"/>
Expand Down
9 changes: 8 additions & 1 deletion SharpNBT/Tags/EndTag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;
Expand All @@ -15,7 +16,13 @@ public sealed class EndTag : Tag
public EndTag() : base(TagType.End, null)
{
}


/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
// Do nothing
}

/// <inheritdoc cref="object.ToString"/>
public override string ToString() => $"TAG_End";

Expand Down
22 changes: 13 additions & 9 deletions SharpNBT/Tags/FloatTag.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Runtime.Serialization;
using System.Text.Json;
using JetBrains.Annotations;

namespace SharpNBT;

/// <summary>
/// A tag that contains a single IEEE-754 single-precision floating point number.
/// </summary>
[PublicAPI][Serializable]
[PublicAPI]
public class FloatTag : NumericTag<float>
{
/// <summary>
Expand All @@ -18,13 +17,18 @@ public class FloatTag : NumericTag<float>
public FloatTag(string? name, float value) : base(TagType.Float, name, value)
{
}
/// <summary>
/// Required constructor for ISerializable implementation.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to describing this instance.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization.</param>
protected FloatTag(SerializationInfo info, StreamingContext context) : base(info, context)

/// <inheritdoc />
protected internal override void WriteJson(Utf8JsonWriter writer, bool named = true)
{
if (named && Name != null)
{
writer.WriteNumber(Name, Value);
}
else
{
writer.WriteNumberValue(Value);
}
}

/// <inheritdoc cref="object.ToString"/>
Expand Down
Loading

0 comments on commit 11f3c4d

Please sign in to comment.