corrade-vassal – Diff between revs 13 and 16
?pathlinks?
Rev 13 | Rev 16 | |||
---|---|---|---|---|
Line 2... | Line 2... | |||
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 | /////////////////////////////////////////////////////////////////////////// |
|
Line -... | Line 6... | |||
- | 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; |
|
Line 10... | Line 12... | |||
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. |
|
Line 22... | Line 24... | |||
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 |
|
Line -... | Line 28... | |||
- | 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; |
|
Line 30... | Line 74... | |||
30 | } |
74 | } |
|
31 | |
75 | |
|
32 | public void ReadXml(XmlReader reader) |
76 | public void ReadXml(XmlReader reader) |
|
33 | { |
77 | { |
|
Line 34... | Line 78... | |||
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)); |
|
Line 36... | Line 80... | |||
36 | |
80 | |
|
37 | bool wasEmpty = reader.IsEmptyElement; |
81 | var wasEmpty = reader.IsEmptyElement; |
|
Line 38... | Line 82... | |||
38 | reader.Read(); |
82 | reader.Read(); |
|
39 | |
83 | |
|
40 | if (wasEmpty) |
84 | if (wasEmpty) |
|
Line 41... | Line 85... | |||
41 | return; |
85 | return; |
|
42 | |
86 | |
|
43 | while (reader.NodeType != XmlNodeType.EndElement) |
87 | while (!reader.NodeType.Equals(XmlNodeType.EndElement)) |
|
Line 44... | Line 88... | |||
44 | { |
88 | { |
|
45 | reader.ReadStartElement("Item"); |
89 | reader.ReadStartElement("Item"); |
|
46 | |
90 | |
|
Line 47... | Line 91... | |||
47 | reader.ReadStartElement("Key"); |
91 | reader.ReadStartElement("Key"); |
|
Line 48... | Line 92... | |||
48 | TKey key = (TKey) keySerializer.Deserialize(reader); |
92 | var key = (TKey) keySerializer.Deserialize(reader); |
|
Line 60... | Line 104... | |||
60 | reader.ReadEndElement(); |
104 | reader.ReadEndElement(); |
|
61 | } |
105 | } |
|
Line 62... | Line 106... | |||
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)); |
|
Line 66... | Line 110... | |||
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) |
|
Line 69... | Line 113... | |||
69 | { |
113 | { |
|
70 | writer.WriteStartElement("Item"); |
114 | writer.WriteStartElement("Item"); |
|
71 | |
115 | |
|
Line 72... | Line 116... | |||
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 | |
|
Line 76... | Line 120... | |||
76 | writer.WriteStartElement("Value"); |
120 | writer.WriteStartElement("Value"); |
|
77 | TValue value = this[key]; |
121 | var value = this[key]; |