internal WzImageProperty ParsePropertyFromXMLElement(XmlElement element) { switch (element.Name) { case "imgdir": WzSubProperty sub = new WzSubProperty(element.GetAttribute("name")); foreach (XmlElement subelement in element) { sub.AddProperty(ParsePropertyFromXMLElement(subelement)); } return(sub); case "canvas": WzCanvasProperty canvas = new WzCanvasProperty(element.GetAttribute("name")); if (!element.HasAttribute("basedata")) { throw new NoBase64DataException("no base64 data in canvas element with name " + canvas.Name); } canvas.PngProperty = new WzPngProperty(); MemoryStream pngstream = new MemoryStream(Convert.FromBase64String(element.GetAttribute("basedata"))); canvas.PngProperty.SetPNG((Bitmap)Image.FromStream(pngstream, true, true)); foreach (XmlElement subelement in element) { canvas.AddProperty(ParsePropertyFromXMLElement(subelement)); } return(canvas); case "int": WzIntProperty compressedInt = new WzIntProperty(element.GetAttribute("name"), int.Parse(element.GetAttribute("value"), formattingInfo)); return(compressedInt); case "double": WzDoubleProperty doubleProp = new WzDoubleProperty(element.GetAttribute("name"), double.Parse(element.GetAttribute("value"), formattingInfo)); return(doubleProp); case "null": WzNullProperty nullProp = new WzNullProperty(element.GetAttribute("name")); return(nullProp); case "sound": if (!element.HasAttribute("basedata") || !element.HasAttribute("basehead") || !element.HasAttribute("length")) { throw new NoBase64DataException("no base64 data in sound element with name " + element.GetAttribute("name")); } WzSoundProperty sound = new WzSoundProperty(element.GetAttribute("name"), int.Parse(element.GetAttribute("length")), Convert.FromBase64String(element.GetAttribute("basehead")), Convert.FromBase64String(element.GetAttribute("basedata"))); return(sound); case "string": WzStringProperty stringProp = new WzStringProperty(element.GetAttribute("name"), element.GetAttribute("value")); return(stringProp); case "short": WzShortProperty shortProp = new WzShortProperty(element.GetAttribute("name"), short.Parse(element.GetAttribute("value"), formattingInfo)); return(shortProp); case "long": WzLongProperty longProp = new WzLongProperty(element.GetAttribute("name"), long.Parse(element.GetAttribute("value"), formattingInfo)); return(longProp); case "uol": WzUOLProperty uol = new WzUOLProperty(element.GetAttribute("name"), element.GetAttribute("value")); return(uol); case "vector": WzVectorProperty vector = new WzVectorProperty(element.GetAttribute("name"), new WzIntProperty("x", Convert.ToInt32(element.GetAttribute("x"))), new WzIntProperty("y", Convert.ToInt32(element.GetAttribute("y")))); return(vector); case "float": WzFloatProperty floatProp = new WzFloatProperty(element.GetAttribute("name"), float.Parse(element.GetAttribute("value"), formattingInfo)); return(floatProp); case "extended": WzConvexProperty convex = new WzConvexProperty(element.GetAttribute("name")); foreach (XmlElement subelement in element) { convex.AddProperty(ParsePropertyFromXMLElement(subelement)); } return(convex); } throw new InvalidDataException("unknown XML prop " + element.Name); }
protected void WritePropertyToXML(TextWriter tw, string depth, WzImageProperty prop) { if (prop is WzCanvasProperty) { WzCanvasProperty property3 = (WzCanvasProperty)prop; if (ExportBase64Data) { MemoryStream stream = new MemoryStream(); property3.PngProperty.GetPNG(false).Save(stream, System.Drawing.Imaging.ImageFormat.Png); byte[] pngbytes = stream.ToArray(); stream.Close(); tw.Write(string.Concat(new object[] { depth, "<canvas name=\"", XmlUtil.SanitizeText(property3.Name), "\" width=\"", property3.PngProperty.Width, "\" height=\"", property3.PngProperty.Height, "\" basedata=\"", Convert.ToBase64String(pngbytes), "\">" }) + lineBreak); } else { tw.Write(string.Concat(new object[] { depth, "<canvas name=\"", XmlUtil.SanitizeText(property3.Name), "\" width=\"", property3.PngProperty.Width, "\" height=\"", property3.PngProperty.Height, "\">" }) + lineBreak); } string newDepth = depth + indent; foreach (WzImageProperty property in property3.WzProperties) { WritePropertyToXML(tw, newDepth, property); } tw.Write(depth + "</canvas>" + lineBreak); } else if (prop is WzIntProperty) { WzIntProperty property4 = (WzIntProperty)prop; tw.Write(string.Concat(new object[] { depth, "<int name=\"", XmlUtil.SanitizeText(property4.Name), "\" value=\"", property4.Value, "\"/>" }) + lineBreak); } else if (prop is WzDoubleProperty) { WzDoubleProperty property5 = (WzDoubleProperty)prop; tw.Write(string.Concat(new object[] { depth, "<double name=\"", XmlUtil.SanitizeText(property5.Name), "\" value=\"", property5.Value, "\"/>" }) + lineBreak); } else if (prop is WzNullProperty) { WzNullProperty property6 = (WzNullProperty)prop; tw.Write(depth + "<null name=\"" + XmlUtil.SanitizeText(property6.Name) + "\"/>" + lineBreak); } else if (prop is WzSoundProperty) { WzSoundProperty property7 = (WzSoundProperty)prop; if (ExportBase64Data) { tw.Write(string.Concat(new object[] { depth, "<sound name=\"", XmlUtil.SanitizeText(property7.Name), "\" length=\"", property7.Length.ToString(), "\" basehead=\"", Convert.ToBase64String(property7.Header), "\" basedata=\"", Convert.ToBase64String(property7.GetBytes(false)), "\"/>" }) + lineBreak); } else { tw.Write(depth + "<sound name=\"" + XmlUtil.SanitizeText(property7.Name) + "\"/>" + lineBreak); } } else if (prop is WzStringProperty) { WzStringProperty property8 = (WzStringProperty)prop; string str = XmlUtil.SanitizeText(property8.Value); tw.Write(depth + "<string name=\"" + XmlUtil.SanitizeText(property8.Name) + "\" value=\"" + str + "\"/>" + lineBreak); } else if (prop is WzSubProperty) { WzSubProperty property9 = (WzSubProperty)prop; tw.Write(depth + "<imgdir name=\"" + XmlUtil.SanitizeText(property9.Name) + "\">" + lineBreak); string newDepth = depth + indent; foreach (WzImageProperty property in property9.WzProperties) { WritePropertyToXML(tw, newDepth, property); } tw.Write(depth + "</imgdir>" + lineBreak); } else if (prop is WzShortProperty) { WzShortProperty property10 = (WzShortProperty)prop; tw.Write(string.Concat(new object[] { depth, "<short name=\"", XmlUtil.SanitizeText(property10.Name), "\" value=\"", property10.Value, "\"/>" }) + lineBreak); } else if (prop is WzLongProperty) { WzLongProperty long_prop = (WzLongProperty)prop; tw.Write(string.Concat(new object[] { depth, "<long name=\"", XmlUtil.SanitizeText(long_prop.Name), "\" value=\"", long_prop.Value, "\"/>" }) + lineBreak); } else if (prop is WzUOLProperty) { WzUOLProperty property11 = (WzUOLProperty)prop; tw.Write(depth + "<uol name=\"" + property11.Name + "\" value=\"" + XmlUtil.SanitizeText(property11.Value) + "\"/>" + lineBreak); } else if (prop is WzVectorProperty) { WzVectorProperty property12 = (WzVectorProperty)prop; tw.Write(string.Concat(new object[] { depth, "<vector name=\"", XmlUtil.SanitizeText(property12.Name), "\" x=\"", property12.X.Value, "\" y=\"", property12.Y.Value, "\"/>" }) + lineBreak); } else if (prop is WzFloatProperty) { WzFloatProperty property13 = (WzFloatProperty)prop; string str2 = Convert.ToString(property13.Value, formattingInfo); if (!str2.Contains(".")) { str2 = str2 + ".0"; } tw.Write(depth + "<float name=\"" + XmlUtil.SanitizeText(property13.Name) + "\" value=\"" + str2 + "\"/>" + lineBreak); } else if (prop is WzConvexProperty) { tw.Write(depth + "<extended name=\"" + XmlUtil.SanitizeText(prop.Name) + "\">" + lineBreak); WzConvexProperty property14 = (WzConvexProperty)prop; string newDepth = depth + indent; foreach (WzImageProperty property in property14.WzProperties) { WritePropertyToXML(tw, newDepth, property); } tw.Write(depth + "</extended>" + lineBreak); } }
private static void ParseXML(XmlElement element, IPropertyContainer wo) { foreach (XmlNode node in element) { if (!(node is XmlElement)) { continue; } XmlElement childElement = (XmlElement)node; if (childElement.Name == "imgdir") { WzSubProperty sub = new WzSubProperty(childElement.GetAttribute("name")); wo.AddProperty(sub); ParseXML(childElement, (IPropertyContainer)sub); } else if (childElement.Name == "canvas") { WzCanvasProperty canvas = new WzCanvasProperty(childElement.GetAttribute("name")); canvas.PngProperty = new WzPngProperty(); MemoryStream pngstream = new MemoryStream(Convert.FromBase64String(childElement.GetAttribute("basedata"))); canvas.PngProperty.SetPNG((Bitmap)Image.FromStream(pngstream, true, true)); wo.AddProperty(canvas); ParseXML(childElement, (IPropertyContainer)canvas); } if (childElement.Name == "int") { WzCompressedIntProperty compressedInt = new WzCompressedIntProperty(childElement.GetAttribute("name"), int.Parse(childElement.GetAttribute("value"), formattingInfo)); wo.AddProperty(compressedInt); } if (childElement.Name == "double") { WzDoubleProperty doubleProp = new WzDoubleProperty(childElement.GetAttribute("name"), double.Parse(childElement.GetAttribute("value"), formattingInfo)); wo.AddProperty(doubleProp); } if (childElement.Name == "null") { WzNullProperty nullProp = new WzNullProperty(childElement.GetAttribute("name")); wo.AddProperty(nullProp); } if (childElement.Name == "sound") { WzSoundProperty sound = new WzSoundProperty(childElement.GetAttribute("name")); sound.SetDataUnsafe(Convert.FromBase64String(childElement.GetAttribute("basedata"))); wo.AddProperty(sound); } if (childElement.Name == "string") { string str = childElement.GetAttribute("value").Replace("<", "<").Replace("&", "&").Replace(">", ">").Replace("'", "'").Replace(""", "\""); WzStringProperty stringProp = new WzStringProperty(childElement.GetAttribute("name"), str); wo.AddProperty(stringProp); } if (childElement.Name == "short") { WzUnsignedShortProperty shortProp = new WzUnsignedShortProperty(childElement.GetAttribute("name"), ushort.Parse(childElement.GetAttribute("value"), formattingInfo)); wo.AddProperty(shortProp); } if (childElement.Name == "uol") { WzUOLProperty uol = new WzUOLProperty(childElement.GetAttribute("name"), childElement.GetAttribute("value")); wo.AddProperty(uol); } if (childElement.Name == "vector") { WzVectorProperty vector = new WzVectorProperty(childElement.GetAttribute("name"), new WzCompressedIntProperty("x", Convert.ToInt32(childElement.GetAttribute("x"))), new WzCompressedIntProperty("y", Convert.ToInt32(childElement.GetAttribute("y")))); wo.AddProperty(vector); } if (childElement.Name == "float") { WzByteFloatProperty floatProp = new WzByteFloatProperty(childElement.GetAttribute("name"), float.Parse(childElement.GetAttribute("value"), formattingInfo)); wo.AddProperty(floatProp); } if (childElement.Name == "extended") { WzConvexProperty convex = new WzConvexProperty(childElement.GetAttribute("name")); wo.AddProperty(convex); ParseXML(childElement, (IPropertyContainer)convex); } } }
private static void DumpXML(TextWriter tw, string depth, List <IWzImageProperty> props) { foreach (IWzImageProperty property in props) { if (property != null) { if (property is WzCanvasProperty) { WzCanvasProperty canvas = (WzCanvasProperty)property; MemoryStream stream = new MemoryStream(); canvas.PngProperty.GetPNG(false).Save(stream, System.Drawing.Imaging.ImageFormat.Png); byte[] pngbytes = stream.ToArray(); stream.Close(); tw.WriteLine(string.Concat(new object[] { depth, "<canvas name=\"", canvas.Name, "\" width=\"", canvas.PngProperty.Width, "\" height=\"", canvas.PngProperty.Height, "\" basedata=\"", Convert.ToBase64String(pngbytes), "\">" })); DumpXML(tw, depth + indent, canvas.WzProperties); tw.WriteLine(depth + "</canvas>"); } else if (property is WzCompressedIntProperty) { WzCompressedIntProperty compressedInt = (WzCompressedIntProperty)property; tw.WriteLine(string.Concat(new object[] { depth, "<int name=\"", compressedInt.Name, "\" value=\"", compressedInt.Value, "\"/>" })); } else if (property is WzDoubleProperty) { WzDoubleProperty doubleProp = (WzDoubleProperty)property; tw.WriteLine(string.Concat(new object[] { depth, "<double name=\"", doubleProp.Name, "\" value=\"", doubleProp.Value.ToString(formattingInfo), "\"/>" })); } else if (property is WzNullProperty) { WzNullProperty nullProp = (WzNullProperty)property; tw.WriteLine(depth + "<null name=\"" + nullProp.Name + "\"/>"); } else if (property is WzSoundProperty) { WzSoundProperty sound = (WzSoundProperty)property; tw.WriteLine(string.Concat(new object[] { depth, "<sound name=\"", sound.Name, "\" basedata=\"", Convert.ToBase64String(sound.GetBytes(false)), "\"/>" })); } else if (property is WzStringProperty) { WzStringProperty stringProp = (WzStringProperty)property; string str = stringProp.Value.Replace("<", "<").Replace("&", "&").Replace(">", ">").Replace("'", "'").Replace("\"", """); tw.WriteLine(depth + "<string name=\"" + stringProp.Name + "\" value=\"" + str + "\"/>"); } else if (property is WzSubProperty) { WzSubProperty sub = (WzSubProperty)property; tw.WriteLine(depth + "<imgdir name=\"" + sub.Name + "\">"); DumpXML(tw, depth + indent, sub.WzProperties); tw.WriteLine(depth + "</imgdir>"); } else if (property is WzUnsignedShortProperty) { WzUnsignedShortProperty ushortProp = (WzUnsignedShortProperty)property; tw.WriteLine(string.Concat(new object[] { depth, "<short name=\"", ushortProp.Name, "\" value=\"", ushortProp.Value.ToString(formattingInfo), "\"/>" })); } else if (property is WzUOLProperty) { WzUOLProperty uol = (WzUOLProperty)property; tw.WriteLine(depth + "<uol name=\"" + uol.Name + "\" value=\"" + uol.Value + "\"/>"); } else if (property is WzVectorProperty) { WzVectorProperty vector = (WzVectorProperty)property; tw.WriteLine(string.Concat(new object[] { depth, "<vector name=\"", vector.Name, "\" x=\"", vector.X.Value, "\" y=\"", vector.Y.Value, "\"/>" })); } else if (property is WzByteFloatProperty) { WzByteFloatProperty floatProp = (WzByteFloatProperty)property; string str2 = floatProp.Value.ToString(formattingInfo); if (!str2.Contains(".")) { str2 = str2 + ".0"; } tw.WriteLine(depth + "<float name=\"" + floatProp.Name + "\" value=\"" + str2 + "\"/>"); } else if (property is WzConvexProperty) { tw.WriteLine(depth + "<extended name=\"" + property.Name + "\">"); DumpXML(tw, depth + indent, ((WzConvexProperty)property).WzProperties); tw.WriteLine(depth + "</extended>"); } } } }
// TODO: this is not deserializable due to missing type information protected void WritePropertyToJson(TextWriter tw, WzImageProperty prop, bool isArray = false) { tw.Write("\n"); if (prop is WzCanvasProperty) { WzCanvasProperty property = (WzCanvasProperty)prop; if (!isArray) { tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\":"); } if (ExportBase64Data) { MemoryStream stream = new MemoryStream(); property.PngProperty.GetPNG(false).Save(stream, System.Drawing.Imaging.ImageFormat.Png); byte[] pngbytes = stream.ToArray(); stream.Close(); tw.Write($"{{" + $"\"width\": {property.PngProperty.Width}, " + $"\"height\": {property.PngProperty.Height}, " + $"\"basedata\": {Convert.ToBase64String(pngbytes)}\","); } else { tw.Write($"{{" + $"\"width\": {property.PngProperty.Width}, " + $"\"height\": {property.PngProperty.Height},"); } if (property.WzProperties.Count() > 0) { var last = property.WzProperties.Last(); foreach (WzImageProperty p in property.WzProperties) { WritePropertyToJson(tw, p); if (!p.Equals(last)) { tw.Write(","); } } } tw.Write("}"); } else if (prop is WzIntProperty) { WzIntProperty property = (WzIntProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {property.Value}"); } else if (prop is WzDoubleProperty) { WzDoubleProperty property = (WzDoubleProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {property.Value}"); } else if (prop is WzNullProperty) { WzNullProperty property = (WzNullProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": null"); } else if (prop is WzSoundProperty) { WzSoundProperty property = (WzSoundProperty)prop; if (!isArray) { tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\":"); } if (ExportBase64Data) { tw.Write($"{{" + $"\"length\":\"{property.Length}\", " + $"\"basehead\": \"{Convert.ToBase64String(property.Header)}\"" + $"\"basedata\": \"{Convert.ToBase64String(property.GetBytes(false))}\"" + $"}}"); } else { tw.Write("{}"); } } else if (prop is WzStringProperty) { WzStringProperty property = (WzStringProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {JsonConvert.ToString(property.Value)}"); } else if (prop is WzSubProperty) { WzSubProperty property = (WzSubProperty)prop; if (!isArray) { tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\":"); } // This has the same problem as the convex property bool propertyIsArray = property.WzProperties.TrueForAll(x => { int num; return(int.TryParse(x.Name, out num)); }); tw.Write(propertyIsArray ? "[" : "{"); if (property.WzProperties.Count() > 0) { var last = property.WzProperties.Last(); foreach (WzImageProperty p in property.WzProperties) { bool isObject = p is WzConvexProperty || p is WzSubProperty || p is WzSoundProperty || p is WzCanvasProperty || p is WzVectorProperty; if (propertyIsArray) { tw.Write($"{{\"index\":{p.Name}, \"item\":"); tw.Write(!isObject ? "{" : ""); } WritePropertyToJson(tw, p, propertyIsArray); if (propertyIsArray) { tw.Write(!isObject ? "}" : ""); tw.Write("}"); } if (!p.Equals(last)) { tw.Write(","); } } } tw.Write(propertyIsArray ? "]" : "}"); } else if (prop is WzShortProperty) { WzShortProperty property = (WzShortProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {property.Value}"); } else if (prop is WzLongProperty) { WzLongProperty property = (WzLongProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {property.Value}"); } else if (prop is WzUOLProperty) { WzUOLProperty property = (WzUOLProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": \"{property.Value}\""); } else if (prop is WzVectorProperty) { WzVectorProperty property = (WzVectorProperty)prop; if (!isArray) { tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\":"); } tw.Write($"{{" + $"\"x\": {property.X.Value}, " + $"\"y\": {property.Y.Value}" + $"}}"); } else if (prop is WzFloatProperty) { WzFloatProperty property = (WzFloatProperty)prop; tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\": {property.Value * 1.0}"); } else if (prop is WzConvexProperty) { WzConvexProperty property = (WzConvexProperty)prop; if (!isArray) { tw.Write($"\"{XmlUtil.SanitizeText(property.Name)}\":"); } tw.Write("["); if (property.WzProperties.Count() > 0) { var last = property.WzProperties.Last(); foreach (WzImageProperty p in property.WzProperties) { bool isObject = p is WzConvexProperty || p is WzSubProperty || p is WzSoundProperty || p is WzCanvasProperty || p is WzVectorProperty; tw.Write(isObject ? "" : "{"); WritePropertyToJson(tw, p, true); tw.Write(isObject ? "" : "}"); if (!p.Equals(last)) { tw.Write(","); } } } tw.Write("]"); } }