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>
<Copyright>Copyright © Eric Freed 2021</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>1.2.0</PackageVersion>
<AssemblyVersion>1.2.0</AssemblyVersion>
<FileVersion>1.2.0</FileVersion>
<PackageVersion>1.3.1</PackageVersion>
<AssemblyVersion>1.3.1</AssemblyVersion>
<FileVersion>1.3.1</FileVersion>
<LangVersion>latestmajor</LangVersion>
<Nullable>enable</Nullable>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>Hotfix to correct bug with Stringified output.</PackageReleaseNotes>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@ -25,6 +27,7 @@
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" Visible="true" PackagePath="/"/>
<None Include="icon.png" Pack="true" Visible="true" PackagePath="" />
</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>
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++)
{

View File

@ -50,11 +50,11 @@ public class BoolTag : Tag
/// <returns>The tag represented as a <see cref="byte"/>.</returns>
public static implicit operator bool(BoolTag tag) => tag.Value;
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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 ? "true" : "false")}";
/// <inheritdoc />
public override string Stringify(bool named = true)
{
var value = Value ? "true" : "false";
return named ? $"{StringifyName}:{value}" : value;
}
}

View File

@ -76,10 +76,6 @@ public class ByteArrayTag : ArrayTag<byte>
return $"TAG_Byte_Array({PrettyName}): [{Count} {word}]";
}
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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');
/// <inheritdoc />
public override string Stringify(bool named = true) => Stringify(named, 'B', 'b');
}

View File

@ -132,10 +132,6 @@ public class ByteTag : NumericTag<byte>
[CLSCompliant(false)]
public static implicit operator sbyte(ByteTag tag) => unchecked((sbyte)tag.Value);
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}B" : $"{Value}B";
}

View File

@ -242,22 +242,20 @@ public class CompoundTag : Tag, IDictionary<string, Tag>, ICollection<Tag>
buffer.AppendLine(space + "}");
}
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <seealso href="https://minecraft.fandom.com/wiki/NBT_format#SNBT_format"/>
public override string Stringify()
/// <inheritdoc />
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();
@ -267,11 +265,12 @@ public class CompoundTag : Tag, IDictionary<string, Tag>, ICollection<Tag>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </summary>
/// <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>
/// <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;
}

View File

@ -41,10 +41,6 @@ public class DoubleTag : NumericTag<double>
/// <returns>The tag represented as a <see cref="double"/>.</returns>
public static implicit operator double(DoubleTag tag) => tag.Value;
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}D" : $"{Value}D";
}

View File

@ -32,10 +32,6 @@ public sealed class EndTag : Tag
// Do nothing
}
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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;
/// <inheritdoc />
public override string Stringify(bool named = true) => string.Empty;
}

View File

@ -41,10 +41,6 @@ public class FloatTag : NumericTag<float>
/// <returns>The tag represented as a <see cref="float"/>.</returns>
public static implicit operator float(FloatTag tag) => tag.Value;
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}F" : $"{Value}F";
}

View File

@ -74,10 +74,6 @@ public class IntArrayTag : ArrayTag<int>
return $"TAG_Int_Array({PrettyName}): [{Count} {word}]";
}
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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);
/// <inheritdoc />
public override string Stringify(bool named = true) => Stringify(named, 'I', null);
}

View File

@ -69,10 +69,6 @@ public class IntTag : NumericTag<int>
[CLSCompliant(false)]
public static implicit operator uint(IntTag tag) => unchecked((uint)tag.Value);
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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}";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}" : $"{Value}";
}

View File

@ -175,15 +175,21 @@ public class ListTag : Tag, IList<Tag>
/// </summary>
/// <returns>This NBT tag in SNBT format.</returns>
/// <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];
for (var i = 0; i < strings.Length; i++)
strings[i] = this[i].Stringify();
var sb = new StringBuilder();
if (named)
sb.Append($"{StringifyName}:");
// TODO: Use StringBuilder
return $"{StringifyName}:[{string.Join(',', strings)}]";
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)]

View File

@ -71,10 +71,6 @@ public class LongArrayTag : ArrayTag<long>
return $"TAG_Long_Array({PrettyName}): [{Count} {word}]";
}
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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');
/// <inheritdoc />
public override string Stringify(bool named = true) => Stringify(named, 'L', 'l');
}

View File

@ -69,10 +69,6 @@ public class LongTag : NumericTag<long>
[CLSCompliant(false)]
public static implicit operator ulong(LongTag tag) => unchecked((ulong)tag.Value);
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}L" : $"{Value}L";
}

View File

@ -75,10 +75,6 @@ public class ShortTag : NumericTag<short>
[CLSCompliant(false)]
public static implicit operator ushort(ShortTag tag) => unchecked((ushort)tag.Value);
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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";
/// <inheritdoc />
public override string Stringify(bool named = true) => named ? $"{StringifyName}:{Value}S" : $"{Value}S";
}

View File

@ -48,12 +48,11 @@ public class StringTag : Tag, IEquatable<StringTag>
/// <returns>The tag represented as a <see cref="string"/>.</returns>
public static implicit operator string(StringTag tag) => tag.Value;
/// <summary>
/// Gets the <i>string</i> representation of this NBT tag (SNBT).
/// </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}\""; // TODO: Does this get properly escaped?
/// <inheritdoc />
public override string Stringify(bool named = true)
{
return named ? $"{StringifyName}:\"{Value}\"" : $"\"{Value}\"";
}
/// <inheritdoc />
public bool Equals(StringTag? other)

View File

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