A game about forced loneliness, made by TACStudios
1using System;
2using System.Diagnostics;
3using System.Collections.Generic;
4
5namespace UnityEngine.Rendering
6{
7 internal sealed class SerializedDictionaryDebugView<K, V>
8 {
9 private IDictionary<K, V> dict;
10
11 public SerializedDictionaryDebugView(IDictionary<K, V> dictionary)
12 {
13 if (dictionary == null)
14 throw new ArgumentNullException(dictionary.ToString());
15
16 this.dict = dictionary;
17 }
18
19 [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
20 public KeyValuePair<K, V>[] Items
21 {
22 get
23 {
24 KeyValuePair<K, V>[] items = new KeyValuePair<K, V>[dict.Count];
25 dict.CopyTo(items, 0);
26 return items;
27 }
28 }
29 }
30
31 /// <summary>
32 /// Unity can't serialize Dictionary so here's a custom wrapper that does. Note that you have to
33 /// extend it before it can be serialized as Unity won't serialized generic-based types either.
34 /// </summary>
35 /// <typeparam name="K">The key type</typeparam>
36 /// <typeparam name="V">The value</typeparam>
37 /// <example>
38 /// public sealed class MyDictionary : SerializedDictionary<KeyType, ValueType> {}
39 /// </example>
40 [Serializable]
41 [DebuggerDisplay("Count = {Count}")]
42 [DebuggerTypeProxy(typeof(SerializedDictionaryDebugView<, >))]
43 public class SerializedDictionary<K, V> : SerializedDictionary<K, V, K, V>
44 {
45 /// <summary>
46 /// Conversion to serialize a key
47 /// </summary>
48 /// <param name="key">The key to serialize</param>
49 /// <returns>The Key that has been serialized</returns>
50 public override K SerializeKey(K key) => key;
51
52 /// <summary>
53 /// Conversion to serialize a value
54 /// </summary>
55 /// <param name="val">The value</param>
56 /// <returns>The value</returns>
57 public override V SerializeValue(V val) => val;
58
59 /// <summary>
60 /// Conversion to serialize a key
61 /// </summary>
62 /// <param name="key">The key to serialize</param>
63 /// <returns>The Key that has been serialized</returns>
64 public override K DeserializeKey(K key) => key;
65
66 /// <summary>
67 /// Conversion to serialize a value
68 /// </summary>
69 /// <param name="val">The value</param>
70 /// <returns>The value</returns>
71 public override V DeserializeValue(V val) => val;
72 }
73
74 /// <summary>
75 /// Dictionary that can serialize keys and values as other types
76 /// </summary>
77 /// <typeparam name="K">The key type</typeparam>
78 /// <typeparam name="V">The value type</typeparam>
79 /// <typeparam name="SK">The type which the key will be serialized for</typeparam>
80 /// <typeparam name="SV">The type which the value will be serialized for</typeparam>
81 [Serializable]
82 public abstract class SerializedDictionary<K, V, SK, SV> : Dictionary<K, V>, ISerializationCallbackReceiver
83 {
84 [SerializeField]
85 List<SK> m_Keys = new List<SK>();
86
87 [SerializeField]
88 List<SV> m_Values = new List<SV>();
89
90 /// <summary>
91 /// From <see cref="K"/> to <see cref="SK"/>
92 /// </summary>
93 /// <param name="key">They key in <see cref="K"/></param>
94 /// <returns>The key in <see cref="SK"/></returns>
95 public abstract SK SerializeKey(K key);
96
97 /// <summary>
98 /// From <see cref="V"/> to <see cref="SV"/>
99 /// </summary>
100 /// <param name="value">The value in <see cref="V"/></param>
101 /// <returns>The value in <see cref="SV"/></returns>
102 public abstract SV SerializeValue(V value);
103
104
105 /// <summary>
106 /// From <see cref="SK"/> to <see cref="K"/>
107 /// </summary>
108 /// <param name="serializedKey">They key in <see cref="SK"/></param>
109 /// <returns>The key in <see cref="K"/></returns>
110 public abstract K DeserializeKey(SK serializedKey);
111
112 /// <summary>
113 /// From <see cref="SV"/> to <see cref="V"/>
114 /// </summary>
115 /// <param name="serializedValue">The value in <see cref="SV"/></param>
116 /// <returns>The value in <see cref="V"/></returns>
117 public abstract V DeserializeValue(SV serializedValue);
118
119 /// <summary>
120 /// OnBeforeSerialize implementation.
121 /// </summary>
122 public void OnBeforeSerialize()
123 {
124 m_Keys.Clear();
125 m_Values.Clear();
126
127 foreach (var kvp in this)
128 {
129 m_Keys.Add(SerializeKey(kvp.Key));
130 m_Values.Add(SerializeValue(kvp.Value));
131 }
132 }
133
134 /// <summary>
135 /// OnAfterDeserialize implementation.
136 /// </summary>
137 public void OnAfterDeserialize()
138 {
139 Clear();
140
141 for (int i = 0; i < m_Keys.Count; i++)
142 Add(DeserializeKey(m_Keys[i]), DeserializeValue(m_Values[i]));
143 }
144 }
145}