Port NBT impl from LibAC Dart

This commit is contained in:
zontreck 2024-12-16 04:25:24 -07:00
parent 0a022634c1
commit ad7b619706
55 changed files with 3226 additions and 2983 deletions

73
NBT/StringTag.cs Normal file
View file

@ -0,0 +1,73 @@
using System;
using LibAC.NBT.API;
namespace LibAC.NBT
{
public class StringTag : Tag
{
public string value { get; private set; } = "";
public StringTag() { }
public StringTag(string str)
{
value = str;
}
public static StringTag ValueOf(string str)
{
return new StringTag(str);
}
public override void ReadValue(ByteLayer data)
{
value = data.ReadString();
}
public override void WriteValue(ByteLayer data)
{
data.WriteString(value);
}
public override TagType GetTagType()
{
return TagType.String;
}
public override void SetValue(dynamic val)
{
if (val is string) value = val;
}
public override dynamic GetValue()
{
return value;
}
public override void PrettyPrint(int indent, bool recurse)
{
Console.WriteLine($"{new string('\t', indent)}{Tag.GetCanonicalName(GetTagType())}: {value}");
}
public override void WriteStringifiedValue(StringBuilder builder, int indent, bool isList)
{
if (ShouldQuote(value))
builder.Append($"{(isList ? new string('\t', indent) : "")}\"{value}\"");
else
builder.Append($"{(isList ? new string('\t', indent) : "")}{value}");
}
public override void ReadStringifiedValue(StringReader reader)
{
string str = reader.ReadString();
value = str;
}
// This method will need to be implemented for checking if a string should be quoted
private bool ShouldQuote(string str)
{
// Implement logic for determining if the string needs to be quoted
return false;
}
}
}