PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/UtSerialize.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 102 lines | 83 code | 8 blank | 11 comment | 34 complexity | d129f411d90bfc3cb8bbf1b95197ad67 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using RT.Util.Serialization;
  6. using D = System.Drawing;
  7. using W = System.Windows.Media;
  8. namespace TankIconMaker
  9. {
  10. /// <summary>
  11. /// Enables <see cref="XmlClassify"/> to save color properties as strings of a human-editable form.
  12. /// </summary>
  13. sealed class colorTypeOptions : ClassifyTypeOptions,
  14. IClassifySubstitute<W.Color, string>,
  15. IClassifySubstitute<D.Color, string>
  16. {
  17. public W.Color FromSubstitute(string instance)
  18. {
  19. return (this as IClassifySubstitute<D.Color, string>).FromSubstitute(instance).ToColorWpf();
  20. }
  21. public string ToSubstitute(W.Color instance)
  22. {
  23. return ToSubstitute(instance.ToColorGdi());
  24. }
  25. D.Color IClassifySubstitute<D.Color, string>.FromSubstitute(string instance) { return FromSubstituteD(instance); }
  26. private D.Color FromSubstituteD(string instance)
  27. {
  28. if (instance == null || !instance.StartsWith("#") || (instance.Length != 7 && instance.Length != 9))
  29. throw new ClassifyDesubstitutionFailedException();
  30. try
  31. {
  32. int alpha = instance.Length == 7 ? 255 : int.Parse(instance.Substring(1, 2), NumberStyles.HexNumber);
  33. int r = int.Parse(instance.Substring(instance.Length == 7 ? 1 : 3, 2), NumberStyles.HexNumber);
  34. int g = int.Parse(instance.Substring(instance.Length == 7 ? 3 : 5, 2), NumberStyles.HexNumber);
  35. int b = int.Parse(instance.Substring(instance.Length == 7 ? 5 : 7, 2), NumberStyles.HexNumber);
  36. return D.Color.FromArgb(alpha, r, g, b);
  37. }
  38. catch
  39. {
  40. throw new ClassifyDesubstitutionFailedException();
  41. }
  42. }
  43. public string ToSubstitute(D.Color instance)
  44. {
  45. return instance.A == 255 ? "#{0:X2}{1:X2}{2:X2}".Fmt(instance.R, instance.G, instance.B) : "#{0:X2}{1:X2}{2:X2}{3:X2}".Fmt(instance.A, instance.R, instance.G, instance.B);
  46. }
  47. }
  48. /// <summary>
  49. /// Filters lists of <see cref="LayerBase"/> objects before XmlClassify attempts to decode them, removing all
  50. /// entries pertaining to layer types that no longer exist in the assembly and hence can't possibly be instantiated.
  51. /// </summary>
  52. sealed class listLayerBaseOptions : ClassifyTypeOptions, IClassifyXmlTypeProcessor
  53. {
  54. void IClassifyTypeProcessor<XElement>.AfterSerialize(object obj, XElement element) { }
  55. void IClassifyTypeProcessor<XElement>.AfterDeserialize(object obj, XElement element) { }
  56. void IClassifyTypeProcessor<XElement>.BeforeSerialize(object obj) { }
  57. void IClassifyTypeProcessor<XElement>.BeforeDeserialize(XElement element)
  58. {
  59. foreach (var item in element.Nodes().OfType<XElement>().Where(e => e.Name == "item").ToArray())
  60. {
  61. var type = item.Attribute("type");
  62. if (type == null)
  63. item.Remove();
  64. else if (!App.LayerTypes.Any(lt => lt.Type.Name == type.Value || lt.Type.FullName == type.Value))
  65. item.Remove();
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Filters lists of <see cref="EffectBase"/> objects before XmlClassify attempts to decode them, removing all
  71. /// entries pertaining to layer types that no longer exist in the assembly and hence can't possibly be instantiated.
  72. /// </summary>
  73. sealed class listEffectBaseOptions : ClassifyTypeOptions, IClassifyXmlTypeProcessor
  74. {
  75. void IClassifyTypeProcessor<XElement>.AfterSerialize(object obj, XElement element) { }
  76. void IClassifyTypeProcessor<XElement>.AfterDeserialize(object obj, XElement element) { }
  77. void IClassifyTypeProcessor<XElement>.BeforeSerialize(object obj) { }
  78. void IClassifyTypeProcessor<XElement>.BeforeDeserialize(XElement element)
  79. {
  80. foreach (var item in element.Nodes().OfType<XElement>().Where(e => e.Name == "item").ToArray())
  81. {
  82. var type = item.Attribute("type");
  83. if (type == null)
  84. item.Remove();
  85. else if (type.Value == "TankIconMaker.Effects.BrightnessAdjustmentEffect")
  86. type.Value = "TankIconMaker.Effects.NormalizeBrightnessEffect";
  87. else if (type.Value == "TankIconMaker.Effects.ModulateEffect")
  88. type.Value = "TankIconMaker.Effects.HueSaturationLightnessEffect";
  89. else if (!App.EffectTypes.Any(lt => lt.Type.Name == type.Value || lt.Type.FullName == type.Value))
  90. item.Remove();
  91. }
  92. }
  93. }
  94. }