-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUtSerialize.cs
More file actions
102 lines (94 loc) · 4.75 KB
/
UtSerialize.cs
File metadata and controls
102 lines (94 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using RT.Util.Serialization;
using D = System.Drawing;
using W = System.Windows.Media;
namespace TankIconMaker
{
/// <summary>
/// Enables <see cref="XmlClassify"/> to save color properties as strings of a human-editable form.
/// </summary>
sealed class colorTypeOptions : ClassifyTypeOptions,
IClassifySubstitute<W.Color, string>,
IClassifySubstitute<D.Color, string>
{
public W.Color FromSubstitute(string instance)
{
return (this as IClassifySubstitute<D.Color, string>).FromSubstitute(instance).ToColorWpf();
}
public string ToSubstitute(W.Color instance)
{
return ToSubstitute(instance.ToColorGdi());
}
D.Color IClassifySubstitute<D.Color, string>.FromSubstitute(string instance) { return FromSubstituteD(instance); }
private D.Color FromSubstituteD(string instance)
{
if (instance == null || !instance.StartsWith("#") || (instance.Length != 7 && instance.Length != 9))
throw new ClassifyDesubstitutionFailedException();
try
{
int alpha = instance.Length == 7 ? 255 : int.Parse(instance.Substring(1, 2), NumberStyles.HexNumber);
int r = int.Parse(instance.Substring(instance.Length == 7 ? 1 : 3, 2), NumberStyles.HexNumber);
int g = int.Parse(instance.Substring(instance.Length == 7 ? 3 : 5, 2), NumberStyles.HexNumber);
int b = int.Parse(instance.Substring(instance.Length == 7 ? 5 : 7, 2), NumberStyles.HexNumber);
return D.Color.FromArgb(alpha, r, g, b);
}
catch
{
throw new ClassifyDesubstitutionFailedException();
}
}
public string ToSubstitute(D.Color instance)
{
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);
}
}
/// <summary>
/// Filters lists of <see cref="LayerBase"/> objects before XmlClassify attempts to decode them, removing all
/// entries pertaining to layer types that no longer exist in the assembly and hence can't possibly be instantiated.
/// </summary>
sealed class listLayerBaseOptions : ClassifyTypeOptions, IClassifyXmlTypeProcessor
{
void IClassifyTypeProcessor<XElement>.AfterSerialize(object obj, XElement element) { }
void IClassifyTypeProcessor<XElement>.AfterDeserialize(object obj, XElement element) { }
void IClassifyTypeProcessor<XElement>.BeforeSerialize(object obj) { }
void IClassifyTypeProcessor<XElement>.BeforeDeserialize(XElement element)
{
foreach (var item in element.Nodes().OfType<XElement>().Where(e => e.Name == "item").ToArray())
{
var type = item.Attribute("type");
if (type == null)
item.Remove();
else if (!App.LayerTypes.Any(lt => lt.Type.Name == type.Value || lt.Type.FullName == type.Value))
item.Remove();
}
}
}
/// <summary>
/// Filters lists of <see cref="EffectBase"/> objects before XmlClassify attempts to decode them, removing all
/// entries pertaining to layer types that no longer exist in the assembly and hence can't possibly be instantiated.
/// </summary>
sealed class listEffectBaseOptions : ClassifyTypeOptions, IClassifyXmlTypeProcessor
{
void IClassifyTypeProcessor<XElement>.AfterSerialize(object obj, XElement element) { }
void IClassifyTypeProcessor<XElement>.AfterDeserialize(object obj, XElement element) { }
void IClassifyTypeProcessor<XElement>.BeforeSerialize(object obj) { }
void IClassifyTypeProcessor<XElement>.BeforeDeserialize(XElement element)
{
foreach (var item in element.Nodes().OfType<XElement>().Where(e => e.Name == "item").ToArray())
{
var type = item.Attribute("type");
if (type == null)
item.Remove();
else if (type.Value == "TankIconMaker.Effects.BrightnessAdjustmentEffect")
type.Value = "TankIconMaker.Effects.NormalizeBrightnessEffect";
else if (type.Value == "TankIconMaker.Effects.ModulateEffect")
type.Value = "TankIconMaker.Effects.HueSaturationLightnessEffect";
else if (!App.EffectTypes.Any(lt => lt.Type.Name == type.Value || lt.Type.FullName == type.Value))
item.Remove();
}
}
}
}