public static void SaveStream(SValue val, BinaryWriter sw) { switch (val.Type) { case SValueType.Array: { sw.Write((byte)1); var arr = val.GetArray(); sw.Write(arr.Length); foreach (var arrEntry in arr) { SaveStream(arrEntry, sw); } } break; case SValueType.Object: { sw.Write((byte)2); var dict = val.GetObject(); sw.Write(dict.Size); foreach (var dictEntry in dict) { sw.Write(dictEntry.Key); SaveStream(dictEntry.Value, sw); } } break; case SValueType.Boolean: sw.Write((byte)3); sw.Write(val.GetBoolean()); break; case SValueType.Double: sw.Write((byte)4); sw.Write(val.GetDouble()); break; case SValueType.Float: sw.Write((byte)5); sw.Write(val.GetFloat()); break; case SValueType.Integer: sw.Write((byte)6); sw.Write(val.GetInteger()); break; case SValueType.String: sw.Write((byte)7); sw.Write(val.GetString() ?? ""); break; case SValueType.IntegerArray: sw.Write((byte)8); var iArr = val.GetIntegerArray(); sw.Write(iArr.Length); for (var i = 0; i < iArr.Length; i++) { sw.Write(iArr[i]); } break; case SValueType.Vector2: sw.Write((byte)9); var v2 = val.GetVector2(); sw.Write(v2.X); sw.Write(v2.Y); break; case SValueType.Null: sw.Write((byte)0); break; } }
public static void SaveXML(SValue val, TextWriter tw, string indent = "", string name = null, bool compact = false) { if (!compact) { tw.Write(indent); } switch (val.Type) { case SValueType.Array: { var arr = val.GetArray(); var childCompact = compact; if (arr.Length < 10 && !childCompact) { childCompact = true; foreach (var arrEntry in arr) { if (arrEntry.Type == SValueType.Object || arrEntry.Type == SValueType.IntegerArray || arrEntry.Type == SValueType.Array) { childCompact = false; break; } } } tw.Write(name == null ? "<array>" : "<array name=\"" + name + "\">"); if (!childCompact) { tw.WriteLine(); } foreach (var arrEntry in arr) { SaveXML(arrEntry, tw, indent + "\t", null, childCompact); } if (!childCompact) { tw.Write(indent); } tw.Write("</array>"); } break; case SValueType.Object: { tw.WriteLine(name == null ? "<dictionary>" : "<dictionary name=\"" + name + "\">"); var dict = val.GetObject(); foreach (var dictEntry in dict) { SaveXML(dictEntry.Value, tw, indent + "\t", dictEntry.Key, compact); } if (!compact) { tw.Write(indent); } tw.Write("</dictionary>"); } break; case SValueType.Boolean: tw.Write(name == null ? "<bool>" : "<bool name=\"" + name + "\">"); tw.Write(val.GetBoolean() + "</bool>"); break; case SValueType.Double: tw.Write(name == null ? "<double>" : "<double name=\"" + name + "\">"); tw.Write(val.GetDouble().ToString(CultureInfo.InvariantCulture) + "</double>"); break; case SValueType.Float: tw.Write(name == null ? "<float>" : "<float name=\"" + name + "\">"); tw.Write(val.GetFloat().ToString(CultureInfo.InvariantCulture) + "</float>"); break; case SValueType.Integer: tw.Write(name == null ? "<int>" : "<int name=\"" + name + "\">"); tw.Write(val.GetInteger() + "</int>"); break; case SValueType.String: tw.Write(name == null ? "<string>" : "<string name=\"" + name + "\">"); tw.Write(val.GetString() + "</string>"); break; case SValueType.Vector2: tw.Write(name == null ? "<vec2>" : "<vec2 name=\"" + name + "\">"); var v2 = val.GetVector2(); tw.Write(v2.X.ToString(CultureInfo.InvariantCulture)); tw.Write(" "); tw.Write(v2.Y.ToString(CultureInfo.InvariantCulture)); tw.Write("</vec2>"); break; case SValueType.IntegerArray: tw.Write(name == null ? "<int-arr>" : "<int-arr name=\"" + name + "\">"); var v = val.GetIntegerArray(); for (var i = 0; i < v.Length; i++) { tw.Write(v[i]); if (i != v.Length - 1) { tw.Write(' '); } } tw.Write("</int-arr>"); break; case SValueType.Null: tw.Write(name == null ? "<null/>" : "<null name=\"" + name + "\"/>"); break; } if (!compact) { tw.WriteLine(); } }