tModLoader v0.11.8.9
A mod to make and play Terraria mods
TagPrinter.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6
8{
9 public class TagPrinter
10 {
11 private string indent = "";
12 private StringBuilder sb = new StringBuilder();
13
14 public override string ToString() {
15 return sb.ToString();
16 }
17
18 private string TypeString(Type type) {
19 if (type == typeof(byte)) return "byte";
20 if (type == typeof(short)) return "short";
21 if (type == typeof(int)) return "int";
22 if (type == typeof(long)) return "long";
23 if (type == typeof(float)) return "float";
24 if (type == typeof(double)) return "double";
25 if (type == typeof(string)) return "string";
26 if (type == typeof(byte[])) return "byte[]";
27 if (type == typeof(int[])) return "int[]";
28 if (type == typeof(TagCompound)) return "object";
29 if (type == typeof(IList)) return "list";
30 throw new ArgumentException("Unknown Type: " + type);
31 }
32
33 private void WriteList<T>(char start, char end, bool multiline, IEnumerable<T> list, Action<T> write) {
34 sb.Append(start);
35 indent += " ";
36 var first = true;
37 foreach (var entry in list) {
38 if (first) first = false;
39 else sb.Append(multiline ? "," : ", ");
40
41 if (multiline) sb.AppendLine().Append(indent);
42 write(entry);
43 }
44 indent = indent.Substring(2);
45 if (multiline && !first) sb.AppendLine().Append(indent);
46 sb.Append(end);
47 }
48
49 private void WriteEntry(KeyValuePair<string, object> entry) {
50 if (entry.Value == null) {
51 sb.Append('"').Append(entry.Key).Append("\" = null");
52 return;
53 }
54
55 var type = entry.Value.GetType();
56 var isList = entry.Value is IList && !(entry.Value is Array);
57 sb.Append(TypeString(isList ? type.GetGenericArguments()[0] : type));
58
59 sb.Append(" \"").Append(entry.Key).Append("\" ");
60
61 if (type != typeof(TagCompound) && !isList)
62 sb.Append("= ");
63
64 WriteValue(entry.Value);
65 }
66
67 private void WriteValue(object elem) {
68 if (elem is byte)
69 sb.Append((byte)elem);
70 else if (elem is short)
71 sb.Append((short)elem);
72 else if (elem is int)
73 sb.Append((int)elem);
74 else if (elem is long)
75 sb.Append((long)elem);
76 else if (elem is float)
77 sb.Append((float)elem);
78 else if (elem is double)
79 sb.Append((double)elem);
80 else if (elem is string)
81 sb.Append('"').Append((string)elem).Append('"');
82 else if (elem is byte[])
83 sb.Append('[').Append(string.Join(", ", (byte[])elem)).Append(']');
84 else if (elem is int[])
85 sb.Append('[').Append(string.Join(", ", (int[])elem)).Append(']');
86 else if (elem is TagCompound) {
87 var tag = (TagCompound)elem;
88 WriteList('{', '}', true, tag, WriteEntry);
89 }
90 else if (elem is IList) {
91 var type = elem.GetType().GetGenericArguments()[0];
92 WriteList('[', ']',
93 type == typeof(string) || type == typeof(TagCompound) || typeof(IList).IsAssignableFrom(type),
94 ((IList)elem).Cast<object>(),
95 o => {
96 if (type == typeof(IList)) //lists of lists need their subtype printed
97 sb.Append(TypeString(o.GetType().GetGenericArguments()[0])).Append(' ');
98
99 WriteValue(o);
100 });
101 }
102 }
103
104 public static string Print(TagCompound tag) {
105 var printer = new TagPrinter();
106 printer.WriteValue(tag);
107 return printer.ToString();
108 }
109
110 public static string Print(KeyValuePair<string, object> entry) {
111 var printer = new TagPrinter();
112 printer.WriteEntry(entry);
113 return printer.ToString();
114 }
115 }
116}
void WriteEntry(KeyValuePair< string, object > entry)
Definition: TagPrinter.cs:49
static string Print(KeyValuePair< string, object > entry)
Definition: TagPrinter.cs:110
string TypeString(Type type)
Definition: TagPrinter.cs:18
static string Print(TagCompound tag)
Definition: TagPrinter.cs:104
void WriteValue(object elem)
Definition: TagPrinter.cs:67
void WriteList< T >(char start, char end, bool multiline, IEnumerable< T > list, Action< T > write)
Definition: TagPrinter.cs:33
override string ToString()
Definition: TagPrinter.cs:14