diff --git a/SharpNBT/README.md b/SharpNBT/README.md new file mode 120000 index 0000000..32d46ee --- /dev/null +++ b/SharpNBT/README.md @@ -0,0 +1 @@ +../README.md \ No newline at end of file diff --git a/SharpNBT/SharpNBT.csproj b/SharpNBT/SharpNBT.csproj index bc18fdb..5f9ab06 100644 --- a/SharpNBT/SharpNBT.csproj +++ b/SharpNBT/SharpNBT.csproj @@ -13,11 +13,13 @@ nbt;named binary tag;minecraft;serialization;java;bedrock;pocket edition;varint;varlong;zlib Copyright © Eric Freed 2021 true - 1.2.0 - 1.2.0 - 1.2.0 + 1.3.1 + 1.3.1 + 1.3.1 latestmajor enable + README.md + Hotfix to correct bug with Stringified output. @@ -25,6 +27,7 @@ + diff --git a/SharpNBT/Tags/ArrayTag.cs b/SharpNBT/Tags/ArrayTag.cs index 754f73d..683938d 100644 --- a/SharpNBT/Tags/ArrayTag.cs +++ b/SharpNBT/Tags/ArrayTag.cs @@ -82,10 +82,12 @@ public abstract class ArrayTag : Tag, IReadOnlyList where T : unmanaged, I /// A reference to the first value in the underlying array. public ref T GetPinnableReference() => ref array[0] ; - private protected string Stringify(char prefix, char? suffix) + private protected string Stringify(bool named, char prefix, char? suffix) { var sb = new StringBuilder(32 + array.Length * 4); - sb.Append($"{StringifyName}:[{prefix};"); + if (named) + sb.Append($"{StringifyName}:"); + sb.Append($"[{prefix};"); for (var i = 0; i < array.Length; i++) { diff --git a/SharpNBT/Tags/BoolTag.cs b/SharpNBT/Tags/BoolTag.cs index 172a796..54528d1 100644 --- a/SharpNBT/Tags/BoolTag.cs +++ b/SharpNBT/Tags/BoolTag.cs @@ -49,12 +49,12 @@ public class BoolTag : Tag /// The tag to convert. /// The tag represented as a . public static implicit operator bool(BoolTag tag) => tag.Value; - - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{(Value ? "true" : "false")}"; + + /// + public override string Stringify(bool named = true) + { + var value = Value ? "true" : "false"; + return named ? $"{StringifyName}:{value}" : value; + } } \ No newline at end of file diff --git a/SharpNBT/Tags/ByteArrayTag.cs b/SharpNBT/Tags/ByteArrayTag.cs index 80b671c..ea91796 100644 --- a/SharpNBT/Tags/ByteArrayTag.cs +++ b/SharpNBT/Tags/ByteArrayTag.cs @@ -75,11 +75,7 @@ public class ByteArrayTag : ArrayTag var word = Count == 1 ? Strings.WordElement : Strings.WordElements; return $"TAG_Byte_Array({PrettyName}): [{Count} {word}]"; } - - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => Stringify('B', 'b'); + + /// + public override string Stringify(bool named = true) => Stringify(named, 'B', 'b'); } \ No newline at end of file diff --git a/SharpNBT/Tags/ByteTag.cs b/SharpNBT/Tags/ByteTag.cs index ec702d2..0d7ed7f 100644 --- a/SharpNBT/Tags/ByteTag.cs +++ b/SharpNBT/Tags/ByteTag.cs @@ -132,10 +132,6 @@ public class ByteTag : NumericTag [CLSCompliant(false)] public static implicit operator sbyte(ByteTag tag) => unchecked((sbyte)tag.Value); - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value}B"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}B" : $"{Value}B"; } \ No newline at end of file diff --git a/SharpNBT/Tags/CompoundTag.cs b/SharpNBT/Tags/CompoundTag.cs index 256b688..3087d6f 100644 --- a/SharpNBT/Tags/CompoundTag.cs +++ b/SharpNBT/Tags/CompoundTag.cs @@ -242,36 +242,35 @@ public class CompoundTag : Tag, IDictionary, ICollection buffer.AppendLine(space + "}"); } - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() + /// + public override string Stringify(bool named = true) { var sb = new StringBuilder(); - sb.Append($"{StringifyName}:{{"); + if (named) + sb.Append($"{StringifyName}:"); + sb.Append('{'); var i = 0; foreach (var value in dict.Values) { if (i++ > 0) sb.Append(','); - sb.Append(value); + sb.Append(value.Stringify(true)); } sb.Append('}'); return sb.ToString(); } - + /// /// Gets the string representation of this NBT tag (SNBT). /// /// Flag indicating if this is the top-level tag that should be wrapped in braces. + /// Flag indicating if the name of the NBT should be written. /// This NBT tag in SNBT format. /// - public string Stringify(bool topLevel) + public string Stringify(bool topLevel, bool named) { - var str = Stringify(); + var str = Stringify(named); return topLevel ? $"{{{str}}}" : str; } diff --git a/SharpNBT/Tags/DoubleTag.cs b/SharpNBT/Tags/DoubleTag.cs index 660241d..7a30ce4 100644 --- a/SharpNBT/Tags/DoubleTag.cs +++ b/SharpNBT/Tags/DoubleTag.cs @@ -41,10 +41,6 @@ public class DoubleTag : NumericTag /// The tag represented as a . public static implicit operator double(DoubleTag tag) => tag.Value; - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value:0.0}D"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}D" : $"{Value}D"; } \ No newline at end of file diff --git a/SharpNBT/Tags/EndTag.cs b/SharpNBT/Tags/EndTag.cs index 2bcff26..b9780a2 100644 --- a/SharpNBT/Tags/EndTag.cs +++ b/SharpNBT/Tags/EndTag.cs @@ -31,11 +31,7 @@ public sealed class EndTag : Tag { // Do nothing } - - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => string.Empty; + + /// + public override string Stringify(bool named = true) => string.Empty; } \ No newline at end of file diff --git a/SharpNBT/Tags/FloatTag.cs b/SharpNBT/Tags/FloatTag.cs index 4c0fd99..fcf4042 100644 --- a/SharpNBT/Tags/FloatTag.cs +++ b/SharpNBT/Tags/FloatTag.cs @@ -41,10 +41,6 @@ public class FloatTag : NumericTag /// The tag represented as a . public static implicit operator float(FloatTag tag) => tag.Value; - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value:0.0}F"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}F" : $"{Value}F"; } \ No newline at end of file diff --git a/SharpNBT/Tags/IntArrayTag.cs b/SharpNBT/Tags/IntArrayTag.cs index bca5e56..7128d53 100644 --- a/SharpNBT/Tags/IntArrayTag.cs +++ b/SharpNBT/Tags/IntArrayTag.cs @@ -73,11 +73,7 @@ public class IntArrayTag : ArrayTag var word = Count == 1 ? Strings.WordElement : Strings.WordElements; return $"TAG_Int_Array({PrettyName}): [{Count} {word}]"; } - - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => Stringify('I', null); + + /// + public override string Stringify(bool named = true) => Stringify(named, 'I', null); } \ No newline at end of file diff --git a/SharpNBT/Tags/IntTag.cs b/SharpNBT/Tags/IntTag.cs index 56e8e4b..9cffeb9 100644 --- a/SharpNBT/Tags/IntTag.cs +++ b/SharpNBT/Tags/IntTag.cs @@ -69,10 +69,6 @@ public class IntTag : NumericTag [CLSCompliant(false)] public static implicit operator uint(IntTag tag) => unchecked((uint)tag.Value); - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value}"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}" : $"{Value}"; } \ No newline at end of file diff --git a/SharpNBT/Tags/ListTag.cs b/SharpNBT/Tags/ListTag.cs index fc189e5..653fb1f 100644 --- a/SharpNBT/Tags/ListTag.cs +++ b/SharpNBT/Tags/ListTag.cs @@ -175,15 +175,21 @@ public class ListTag : Tag, IList /// /// This NBT tag in SNBT format. /// - public override string Stringify() + public override string Stringify(bool named = true) { - var strings = new string[Count]; - for (var i = 0; i < strings.Length; i++) - strings[i] = this[i].Stringify(); - - // TODO: Use StringBuilder - - return $"{StringifyName}:[{string.Join(',', strings)}]"; + var sb = new StringBuilder(); + if (named) + sb.Append($"{StringifyName}:"); + + sb.Append('['); + for (var i = 0; i < list.Count; i++) + { + if (i > 0) + sb.Append(','); + sb.Append(list[i].Stringify(false)); + } + sb.Append(']'); + return sb.ToString(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/SharpNBT/Tags/LongArrayTag.cs b/SharpNBT/Tags/LongArrayTag.cs index 87171a4..521ee2a 100644 --- a/SharpNBT/Tags/LongArrayTag.cs +++ b/SharpNBT/Tags/LongArrayTag.cs @@ -71,10 +71,6 @@ public class LongArrayTag : ArrayTag return $"TAG_Long_Array({PrettyName}): [{Count} {word}]"; } - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => Stringify('L', 'l'); + /// + public override string Stringify(bool named = true) => Stringify(named, 'L', 'l'); } \ No newline at end of file diff --git a/SharpNBT/Tags/LongTag.cs b/SharpNBT/Tags/LongTag.cs index e14e60b..cd07e56 100644 --- a/SharpNBT/Tags/LongTag.cs +++ b/SharpNBT/Tags/LongTag.cs @@ -69,10 +69,6 @@ public class LongTag : NumericTag [CLSCompliant(false)] public static implicit operator ulong(LongTag tag) => unchecked((ulong)tag.Value); - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value}L"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}L" : $"{Value}L"; } \ No newline at end of file diff --git a/SharpNBT/Tags/ShortTag.cs b/SharpNBT/Tags/ShortTag.cs index cb71678..623f2e5 100644 --- a/SharpNBT/Tags/ShortTag.cs +++ b/SharpNBT/Tags/ShortTag.cs @@ -75,10 +75,6 @@ public class ShortTag : NumericTag [CLSCompliant(false)] public static implicit operator ushort(ShortTag tag) => unchecked((ushort)tag.Value); - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:{Value}S"; + /// + public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}S" : $"{Value}S"; } \ No newline at end of file diff --git a/SharpNBT/Tags/StringTag.cs b/SharpNBT/Tags/StringTag.cs index ed57705..dab72a9 100644 --- a/SharpNBT/Tags/StringTag.cs +++ b/SharpNBT/Tags/StringTag.cs @@ -47,13 +47,12 @@ public class StringTag : Tag, IEquatable /// The tag to convert. /// The tag represented as a . public static implicit operator string(StringTag tag) => tag.Value; - - /// - /// Gets the string representation of this NBT tag (SNBT). - /// - /// This NBT tag in SNBT format. - /// - public override string Stringify() => $"{StringifyName}:\"{Value}\""; // TODO: Does this get properly escaped? + + /// + public override string Stringify(bool named = true) + { + return named ? $"{StringifyName}:\"{Value}\"" : $"\"{Value}\""; + } /// public bool Equals(StringTag? other) diff --git a/SharpNBT/Tags/Tag.cs b/SharpNBT/Tags/Tag.cs index ae32fb2..52d77bb 100644 --- a/SharpNBT/Tags/Tag.cs +++ b/SharpNBT/Tags/Tag.cs @@ -212,9 +212,10 @@ public abstract class Tag : IEquatable, ICloneable /// /// Gets the string representation of this NBT tag (SNBT). /// + /// Flag indicating if the name of the tag should be written. /// This NBT tag in SNBT format. /// - public abstract string Stringify(); + public abstract string Stringify(bool named = true); /// /// Gets the name in a formatted properly for SNBT.