fixed bug with Stringify giving ToString output

This commit is contained in:
Eric Freed 2023-09-01 01:54:21 -04:00
parent f22ae236b6
commit 82399f243b
18 changed files with 73 additions and 102 deletions

1
SharpNBT/README.md Symbolic link
View File

@ -0,0 +1 @@
../README.md

View File

@ -13,11 +13,13 @@
<PackageTags>nbt;named binary tag;minecraft;serialization;java;bedrock;pocket edition;varint;varlong;zlib</PackageTags> <PackageTags>nbt;named binary tag;minecraft;serialization;java;bedrock;pocket edition;varint;varlong;zlib</PackageTags>
<Copyright>Copyright © Eric Freed 2021</Copyright> <Copyright>Copyright © Eric Freed 2021</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>1.2.0</PackageVersion> <PackageVersion>1.3.1</PackageVersion>
<AssemblyVersion>1.2.0</AssemblyVersion> <AssemblyVersion>1.3.1</AssemblyVersion>
<FileVersion>1.2.0</FileVersion> <FileVersion>1.3.1</FileVersion>
<LangVersion>latestmajor</LangVersion> <LangVersion>latestmajor</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>Hotfix to correct bug with Stringified output.</PackageReleaseNotes>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@ -25,6 +27,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Include="README.md" Pack="true" Visible="true" PackagePath="/"/>
<None Include="icon.png" Pack="true" Visible="true" PackagePath="" /> <None Include="icon.png" Pack="true" Visible="true" PackagePath="" />
</ItemGroup> </ItemGroup>

View File

@ -82,10 +82,12 @@ public abstract class ArrayTag<T> : Tag, IReadOnlyList<T> where T : unmanaged, I
/// <returns>A reference to the first value in the underlying array.</returns> /// <returns>A reference to the first value in the underlying array.</returns>
public ref T GetPinnableReference() => ref array[0] ; 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); 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++) for (var i = 0; i < array.Length; i++)
{ {

View File

@ -49,12 +49,12 @@ public class BoolTag : Tag
/// <param name="tag">The tag to convert.</param> /// <param name="tag">The tag to convert.</param>
/// <returns>The tag represented as a <see cref="byte"/>.</returns> /// <returns>The tag represented as a <see cref="byte"/>.</returns>
public static implicit operator bool(BoolTag tag) => tag.Value; public static implicit operator bool(BoolTag tag) => tag.Value;
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true)
/// </summary> {
/// <returns>This NBT tag in SNBT format.</returns> var value = Value ? "true" : "false";
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/> return named ? $"{StringifyName}:{value}" : value;
public override string Stringify() => $"{StringifyName}:{(Value ? "true" : "false")}"; }
} }

View File

@ -75,11 +75,7 @@ public class ByteArrayTag : ArrayTag<byte>
var word = Count == 1 ? Strings.WordElement : Strings.WordElements; var word = Count == 1 ? Strings.WordElement : Strings.WordElements;
return $"TAG_Byte_Array({PrettyName}): [{Count} {word}]"; return $"TAG_Byte_Array({PrettyName}): [{Count} {word}]";
} }
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => Stringify(named, 'B', 'b');
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => Stringify('B', 'b');
} }

View File

@ -132,10 +132,6 @@ public class ByteTag : NumericTag<byte>
[CLSCompliant(false)] [CLSCompliant(false)]
public static implicit operator sbyte(ByteTag tag) => unchecked((sbyte)tag.Value); public static implicit operator sbyte(ByteTag tag) => unchecked((sbyte)tag.Value);
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}B" : $"{Value}B";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value}B";
} }

View File

@ -242,36 +242,35 @@ public class CompoundTag : Tag, IDictionary<string, Tag>, ICollection<Tag>
buffer.AppendLine(space + "}"); buffer.AppendLine(space + "}");
} }
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true)
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify()
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append($"{StringifyName}:{{"); if (named)
sb.Append($"{StringifyName}:");
sb.Append('{');
var i = 0; var i = 0;
foreach (var value in dict.Values) foreach (var value in dict.Values)
{ {
if (i++ > 0) if (i++ > 0)
sb.Append(','); sb.Append(',');
sb.Append(value); sb.Append(value.Stringify(true));
} }
sb.Append('}'); sb.Append('}');
return sb.ToString(); return sb.ToString();
} }
/// <summary> /// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT). /// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </summary> /// </summary>
/// <param name="topLevel">Flag indicating if this is the top-level tag that should be wrapped in braces.</param> /// <param name="topLevel">Flag indicating if this is the top-level tag that should be wrapped in braces.</param>
/// <param name="named">Flag indicating if the name of the NBT should be written.</param>
/// <returns>This NBT tag in SNBT format.</returns> /// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/> /// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#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; return topLevel ? $"{{{str}}}" : str;
} }

View File

@ -41,10 +41,6 @@ public class DoubleTag : NumericTag<double>
/// <returns>The tag represented as a <see cref="double"/>.</returns> /// <returns>The tag represented as a <see cref="double"/>.</returns>
public static implicit operator double(DoubleTag tag) => tag.Value; public static implicit operator double(DoubleTag tag) => tag.Value;
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}D" : $"{Value}D";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value:0.0}D";
} }

View File

@ -31,11 +31,7 @@ public sealed class EndTag : Tag
{ {
// Do nothing // Do nothing
} }
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => string.Empty;
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => string.Empty;
} }

View File

@ -41,10 +41,6 @@ public class FloatTag : NumericTag<float>
/// <returns>The tag represented as a <see cref="float"/>.</returns> /// <returns>The tag represented as a <see cref="float"/>.</returns>
public static implicit operator float(FloatTag tag) => tag.Value; public static implicit operator float(FloatTag tag) => tag.Value;
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}F" : $"{Value}F";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value:0.0}F";
} }

View File

@ -73,11 +73,7 @@ public class IntArrayTag : ArrayTag<int>
var word = Count == 1 ? Strings.WordElement : Strings.WordElements; var word = Count == 1 ? Strings.WordElement : Strings.WordElements;
return $"TAG_Int_Array({PrettyName}): [{Count} {word}]"; return $"TAG_Int_Array({PrettyName}): [{Count} {word}]";
} }
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => Stringify(named, 'I', null);
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => Stringify('I', null);
} }

View File

@ -69,10 +69,6 @@ public class IntTag : NumericTag<int>
[CLSCompliant(false)] [CLSCompliant(false)]
public static implicit operator uint(IntTag tag) => unchecked((uint)tag.Value); public static implicit operator uint(IntTag tag) => unchecked((uint)tag.Value);
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}" : $"{Value}";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value}";
} }

View File

@ -175,15 +175,21 @@ public class ListTag : Tag, IList<Tag>
/// </summary> /// </summary>
/// <returns>This NBT tag in SNBT format.</returns> /// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/> /// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() public override string Stringify(bool named = true)
{ {
var strings = new string[Count]; var sb = new StringBuilder();
for (var i = 0; i < strings.Length; i++) if (named)
strings[i] = this[i].Stringify(); sb.Append($"{StringifyName}:");
// TODO: Use StringBuilder sb.Append('[');
for (var i = 0; i < list.Count; i++)
return $"{StringifyName}:[{string.Join(',', strings)}]"; {
if (i > 0)
sb.Append(',');
sb.Append(list[i].Stringify(false));
}
sb.Append(']');
return sb.ToString();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -71,10 +71,6 @@ public class LongArrayTag : ArrayTag<long>
return $"TAG_Long_Array({PrettyName}): [{Count} {word}]"; return $"TAG_Long_Array({PrettyName}): [{Count} {word}]";
} }
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => Stringify(named, 'L', 'l');
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => Stringify('L', 'l');
} }

View File

@ -69,10 +69,6 @@ public class LongTag : NumericTag<long>
[CLSCompliant(false)] [CLSCompliant(false)]
public static implicit operator ulong(LongTag tag) => unchecked((ulong)tag.Value); public static implicit operator ulong(LongTag tag) => unchecked((ulong)tag.Value);
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}L" : $"{Value}L";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value}L";
} }

View File

@ -75,10 +75,6 @@ public class ShortTag : NumericTag<short>
[CLSCompliant(false)] [CLSCompliant(false)]
public static implicit operator ushort(ShortTag tag) => unchecked((ushort)tag.Value); public static implicit operator ushort(ShortTag tag) => unchecked((ushort)tag.Value);
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}S" : $"{Value}S";
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify() => $"{StringifyName}:{Value}S";
} }

View File

@ -47,13 +47,12 @@ public class StringTag : Tag, IEquatable<StringTag>
/// <param name="tag">The tag to convert.</param> /// <param name="tag">The tag to convert.</param>
/// <returns>The tag represented as a <see cref="string"/>.</returns> /// <returns>The tag represented as a <see cref="string"/>.</returns>
public static implicit operator string(StringTag tag) => tag.Value; public static implicit operator string(StringTag tag) => tag.Value;
/// <summary> /// <inheritdoc />
/// Gets the <i>string</i> representation of this NBT tag (SNBT). public override string Stringify(bool named = true)
/// </summary> {
/// <returns>This NBT tag in SNBT format.</returns> return named ? $"{StringifyName}:\"{Value}\"" : $"\"{Value}\"";
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/> }
public override string Stringify() => $"{StringifyName}:\"{Value}\""; // TODO: Does this get properly escaped?
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(StringTag? other) public bool Equals(StringTag? other)

View File

@ -212,9 +212,10 @@ public abstract class Tag : IEquatable<Tag>, ICloneable
/// <summary> /// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT). /// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </summary> /// </summary>
/// <param name="named">Flag indicating if the name of the tag should be written.</param>
/// <returns>This NBT tag in SNBT format.</returns> /// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/> /// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public abstract string Stringify(); public abstract string Stringify(bool named = true);
/// <summary> /// <summary>
/// Gets the name in a formatted properly for SNBT. /// Gets the name in a formatted properly for SNBT.