corrade-vassal – Diff between revs 13 and 16
?pathlinks?
Rev 13 | Rev 16 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
- | 6 | |
||
6 | |
7 | using System; |
|
- | 8 | using System.Collections.Generic; |
||
7 | using System.Collections.Generic; |
9 | using System.IO; |
|
8 | using System.Xml; |
10 | using System.Xml; |
|
9 | using System.Xml.Schema; |
11 | using System.Xml.Schema; |
|
10 | using System.Xml.Serialization; |
12 | using System.Xml.Serialization; |
|
11 | |
13 | |
|
12 | namespace wasSharp |
14 | namespace wasSharp |
|
13 | { |
15 | { |
|
14 | public class Collections |
16 | public static class Collections |
|
15 | { |
17 | { |
|
16 | /// <summary> |
18 | /// <summary> |
|
17 | /// A serializable dictionary class. |
19 | /// A serializable dictionary class. |
|
18 | /// </summary> |
20 | /// </summary> |
|
19 | /// <typeparam name="TKey">the key</typeparam> |
21 | /// <typeparam name="TKey">the key</typeparam> |
|
20 | /// <typeparam name="TValue">the value</typeparam> |
22 | /// <typeparam name="TValue">the value</typeparam> |
|
21 | [XmlRoot("Dictionary")] |
23 | [XmlRoot("Dictionary")] |
|
22 | public class SerializableDictionary<TKey, TValue> |
24 | public class SerializableDictionary<TKey, TValue> |
|
23 | : Dictionary<TKey, TValue>, IXmlSerializable |
25 | : Dictionary<TKey, TValue>, IXmlSerializable |
|
24 | { |
26 | { |
|
25 | #region IXmlSerializable Members |
27 | #region IXmlSerializable Members |
|
- | 28 | |
||
- | 29 | public SerializableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> kvp) |
||
- | 30 | { |
||
- | 31 | foreach (var i in kvp) |
||
- | 32 | { |
||
- | 33 | Add(i.Key, i.Value); |
||
- | 34 | } |
||
- | 35 | } |
||
- | 36 | |
||
- | 37 | public SerializableDictionary() |
||
- | 38 | { |
||
- | 39 | } |
||
- | 40 | |
||
- | 41 | /// <summary> |
||
- | 42 | /// Deep-clones the serializable dictionary. |
||
- | 43 | /// </summary> |
||
- | 44 | /// <returns>a deep clone of the original dictionary</returns> |
||
- | 45 | public SerializableDictionary<TKey, TValue> Clone() |
||
- | 46 | { |
||
- | 47 | SerializableDictionary<TKey, TValue> clone; |
||
- | 48 | try |
||
- | 49 | { |
||
- | 50 | using (var writer = new MemoryStream()) |
||
- | 51 | { |
||
- | 52 | var serializer = |
||
- | 53 | new XmlSerializer( |
||
- | 54 | typeof (SerializableDictionary<TKey, TValue>)); |
||
- | 55 | serializer.Serialize(writer, this); |
||
- | 56 | writer.Seek(0, SeekOrigin.Begin); |
||
- | 57 | clone = (SerializableDictionary<TKey, TValue>) |
||
- | 58 | new XmlSerializer( |
||
- | 59 | typeof (SerializableDictionary<TKey, TValue>)) |
||
- | 60 | .Deserialize(writer); |
||
- | 61 | } |
||
- | 62 | } |
||
- | 63 | /* cloning failed so return an empty dictionary */ |
||
- | 64 | catch (Exception) |
||
- | 65 | { |
||
- | 66 | clone = new SerializableDictionary<TKey, TValue>(); |
||
- | 67 | } |
||
- | 68 | return clone; |
||
- | 69 | } |
||
26 | |
70 | |
|
27 | public XmlSchema GetSchema() |
71 | public XmlSchema GetSchema() |
|
28 | { |
72 | { |
|
29 | return null; |
73 | return null; |
|
30 | } |
74 | } |
|
31 | |
75 | |
|
32 | public void ReadXml(XmlReader reader) |
76 | public void ReadXml(XmlReader reader) |
|
33 | { |
77 | { |
|
34 | XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
78 | var keySerializer = new XmlSerializer(typeof (TKey)); |
|
35 | XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
79 | var valueSerializer = new XmlSerializer(typeof (TValue)); |
|
36 | |
80 | |
|
37 | bool wasEmpty = reader.IsEmptyElement; |
81 | var wasEmpty = reader.IsEmptyElement; |
|
38 | reader.Read(); |
82 | reader.Read(); |
|
39 | |
83 | |
|
40 | if (wasEmpty) |
84 | if (wasEmpty) |
|
41 | return; |
85 | return; |
|
42 | |
86 | |
|
43 | while (reader.NodeType != XmlNodeType.EndElement) |
87 | while (!reader.NodeType.Equals(XmlNodeType.EndElement)) |
|
44 | { |
88 | { |
|
45 | reader.ReadStartElement("Item"); |
89 | reader.ReadStartElement("Item"); |
|
46 | |
90 | |
|
47 | reader.ReadStartElement("Key"); |
91 | reader.ReadStartElement("Key"); |
|
48 | TKey key = (TKey) keySerializer.Deserialize(reader); |
92 | var key = (TKey) keySerializer.Deserialize(reader); |
|
49 | reader.ReadEndElement(); |
93 | reader.ReadEndElement(); |
|
50 | |
94 | |
|
51 | reader.ReadStartElement("Value"); |
95 | reader.ReadStartElement("Value"); |
|
52 | TValue value = (TValue) valueSerializer.Deserialize(reader); |
96 | var value = (TValue) valueSerializer.Deserialize(reader); |
|
53 | reader.ReadEndElement(); |
97 | reader.ReadEndElement(); |
|
54 | |
98 | |
|
55 | Add(key, value); |
99 | Add(key, value); |
|
56 | |
100 | |
|
57 | reader.ReadEndElement(); |
101 | reader.ReadEndElement(); |
|
58 | reader.MoveToContent(); |
102 | reader.MoveToContent(); |
|
59 | } |
103 | } |
|
60 | reader.ReadEndElement(); |
104 | reader.ReadEndElement(); |
|
61 | } |
105 | } |
|
62 | |
106 | |
|
63 | public void WriteXml(XmlWriter writer) |
107 | public void WriteXml(XmlWriter writer) |
|
64 | { |
108 | { |
|
65 | XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
109 | var keySerializer = new XmlSerializer(typeof (TKey)); |
|
66 | XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
110 | var valueSerializer = new XmlSerializer(typeof (TValue)); |
|
67 | |
111 | |
|
68 | foreach (TKey key in Keys) |
112 | foreach (var key in Keys) |
|
69 | { |
113 | { |
|
70 | writer.WriteStartElement("Item"); |
114 | writer.WriteStartElement("Item"); |
|
71 | |
115 | |
|
72 | writer.WriteStartElement("Key"); |
116 | writer.WriteStartElement("Key"); |
|
73 | keySerializer.Serialize(writer, key); |
117 | keySerializer.Serialize(writer, key); |
|
74 | writer.WriteEndElement(); |
118 | writer.WriteEndElement(); |
|
75 | |
119 | |
|
76 | writer.WriteStartElement("Value"); |
120 | writer.WriteStartElement("Value"); |
|
77 | TValue value = this[key]; |
121 | var value = this[key]; |
|
78 | valueSerializer.Serialize(writer, value); |
122 | valueSerializer.Serialize(writer, value); |
|
79 | writer.WriteEndElement(); |
123 | writer.WriteEndElement(); |
|
80 | |
124 | |
|
81 | writer.WriteEndElement(); |
125 | writer.WriteEndElement(); |
|
82 | } |
126 | } |
|
83 | } |
127 | } |
|
84 | |
128 | |
|
85 | #endregion |
129 | #endregion |
|
86 | } |
130 | } |
|
87 | } |
131 | } |
|
88 | } |
132 | } |
|
89 | |
133 | |
|
90 |
|
134 |
|
|
91 | |
135 | |
|
92 | |
136 | |
|
93 | |
137 | |