2using System.Collections;
3using System.Collections.Generic;
12 private StringBuilder
sb =
new StringBuilder();
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[]";
29 if (type == typeof(IList))
return "list";
30 throw new ArgumentException(
"Unknown Type: " + type);
37 foreach (var entry
in list) {
38 if (first) first =
false;
39 else sb.Append(multiline ?
"," :
", ");
41 if (multiline)
sb.AppendLine().Append(
indent);
45 if (multiline && !first)
sb.AppendLine().Append(
indent);
49 private void WriteEntry(KeyValuePair<string, object> entry) {
50 if (entry.Value ==
null) {
51 sb.Append(
'"').Append(entry.Key).Append(
"\" = null");
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));
59 sb.Append(
" \"").Append(entry.Key).Append(
"\" ");
69 sb.Append((
byte)elem);
70 else if (elem is
short)
71 sb.Append((
short)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(
']');
90 else if (elem is IList) {
91 var type = elem.GetType().GetGenericArguments()[0];
93 type == typeof(
string) || type == typeof(
TagCompound) || typeof(IList).IsAssignableFrom(type),
94 ((IList)elem).Cast<object>(),
96 if (type == typeof(IList))
97 sb.Append(
TypeString(o.GetType().GetGenericArguments()[0])).Append(
' ');
106 printer.WriteValue(tag);
107 return printer.ToString();
110 public static string Print(KeyValuePair<string, object> entry) {
112 printer.WriteEntry(entry);
113 return printer.ToString();
void WriteEntry(KeyValuePair< string, object > entry)
static string Print(KeyValuePair< string, object > entry)
string TypeString(Type type)
static string Print(TagCompound tag)
void WriteValue(object elem)
void WriteList< T >(char start, char end, bool multiline, IEnumerable< T > list, Action< T > write)
override string ToString()