Created
June 16, 2015 21:59
-
-
Save copygirl/d9fd18bb25474bd5e7ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace copyNBTlib.Text | |
{ | |
class NbtFormatterNotch : INbtFormatter | |
{ | |
public static readonly NbtFormatterNotch Single = new NbtFormatterNotch(); | |
public static readonly NbtFormatterNotch All = new NbtFormatterNotch(int.MaxValue); | |
public int Depth { get; private set; } | |
public string Indent { get; private set; } | |
public NbtFormatterNotch(int depth = 0, string indent = " ") | |
{ | |
if (indent == null) | |
throw new ArgumentNullException(); | |
Depth = depth; | |
Indent = indent; | |
} | |
#region INBTFormatter implementation | |
public void AppendTag(StringBuilder sb, TagBase tag, string name = null) { | |
AppendTag(sb, tag, name, Depth, string.Empty); | |
} | |
void AppendTag(StringBuilder sb, TagBase tag, string name, int depth, string curIndent) { | |
sb.Append(curIndent); | |
sb.Append(TypeToString(tag.Type)); | |
if (name != null) | |
sb.Append("(\"").Append(name).Append("\")"); | |
sb.Append(": "); | |
AppendTagLookup(sb, tag); | |
if (!(tag is TagList) && !(tag is TagCompound) || (tag.Count <= 0)) return; | |
if (depth > 0) { | |
sb.AppendLine().Append(curIndent).AppendLine("{"); | |
var newIndent = curIndent + Indent; | |
if (tag is TagList) { | |
foreach (var childTag in (TagList)tag) { | |
AppendTag(sb, childTag, null, depth - 1, newIndent); | |
sb.AppendLine(); | |
} | |
} else foreach (var nameTagPair in (TagCompound)tag) { | |
AppendTag(sb, nameTagPair.Value, nameTagPair.Key, depth - 1, newIndent); | |
sb.AppendLine(); | |
} | |
sb.Append(curIndent).Append("}"); | |
} else sb.Append(" { ... }"); | |
} | |
#endregion | |
#region Utility functions | |
static string TypeToString(TagType type) | |
{ | |
return _typeToString[(byte)type - 1]; | |
} | |
static readonly string[] _typeToString = { | |
"TAG_Byte", "TAG_Short", "TAG_Int", "TAG_Long", | |
"TAG_Float", "TAG_Double", "TAG_Byte_Array", "TAG_String", | |
"TAG_List", "TAG_Compound", "TAG_Int_Array" | |
}; | |
static void AppendTagLookup(StringBuilder sb, TagBase tag) | |
{ | |
_tagValueToString[(byte)tag.Type - 1](sb, tag); | |
} | |
static readonly Action<StringBuilder, TagBase>[] _tagValueToString = { | |
(sb, tag) => sb.Append((byte)tag), | |
(sb, tag) => sb.Append((short)tag), | |
(sb, tag) => sb.Append((int)tag), | |
(sb, tag) => sb.Append((long)tag), | |
(sb, tag) => sb.Append((float)tag), | |
(sb, tag) => sb.Append((double)tag), | |
(sb, tag) => sb.Append('[').Append(((byte[])tag).Length).Append(" bytes]"), | |
(sb, tag) => sb.Append((string)tag), | |
(sb, tag) => TagListToString(sb, (TagList)tag), | |
(sb, tag) => sb.Append(tag.Count).Append((tag.Count == 1) ? " entry" : " entries"), | |
(sb, tag) => sb.Append('[').Append(((int[])tag).Length).Append(" ints]") | |
}; | |
static void TagListToString(StringBuilder sb, TagList list) | |
{ | |
sb.Append(list.Count).Append((list.Count == 1) ? " entry" : " entries"); | |
if (list.ListType.HasValue) | |
sb.Append(" of type ").Append(TypeToString(list.ListType.Value)); | |
} | |
#endregion | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment