public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("hws2xml [infile] [outfile]"); Console.WriteLine("if [infile] has .hws it will converted to .xml"); Console.WriteLine("if [infile] has .xml it will converted to .hws"); } else { string inFile = args[0]; string outFile = args[1]; if (File.Exists(inFile)) { string inType = Path.GetExtension(inFile).ToUpper(); if (inType == ".XML") { TextReader TR = new StreamReader(inFile); SValue OBJ = SValue.FromXMLFile(TR); BinaryWriter BW = new BinaryWriter(File.Open(outFile, FileMode.Create)); SValue.SaveStream(OBJ, BW); BW.Close(); Console.WriteLine("Converted from XML to HWS."); } else // .HWS { BinaryReader BR = new BinaryReader(File.Open(inFile, FileMode.Open)); SValue OBJ = SValue.LoadStream(BR); TextWriter TW = new StreamWriter(outFile); SValue.SaveXML(OBJ, TW); TW.Close(); Console.WriteLine("Converted from HWS to XML."); } } else { Console.WriteLine("Error: Invalid input file."); } Console.WriteLine("Done."); } }
public static SValue LoadStream(BinaryReader sr) { var type = sr.ReadByte(); switch (type) { case 1: { var len = sr.ReadInt32(); var arr = new SValue[len]; for (var j = 0; j < arr.Length; j++) { arr[j] = LoadStream(sr); } return(FromArray(arr.ToArray())); } case 2: { var len = sr.ReadInt32(); var dict = new SObject(); for (var j = 0; j < len; j++) { var name = sr.ReadString(); dict.Set(name, LoadStream(sr)); } return(FromObject(dict)); } case 3: return(FromBoolean(sr.ReadBoolean())); case 4: return(FromDouble(sr.ReadDouble())); case 5: return(FromFloat(sr.ReadSingle())); case 6: return(FromInteger(sr.ReadInt32())); case 7: return(FromString(sr.ReadString())); case 8: { var len = sr.ReadInt32(); var iarr = new int[len]; for (var j = 0; j < iarr.Length; j++) { iarr[j] = sr.ReadInt32(); } return(FromIntegerArray(iarr)); } case 9: return(FromVector2(new Vector2(sr.ReadSingle(), sr.ReadSingle()))); } return(new SValue(null, SValueType.Null)); }
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(); } }