A game about forced loneliness, made by TACStudios
at master 125 lines 3.7 kB view raw
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6namespace UnityEditor.U2D.Animation 7{ 8 [Serializable] 9 internal class SerializableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ISerializationCallbackReceiver 10 { 11 [SerializeField] 12 private List<TKey> m_Keys; 13 [SerializeField] 14 private List<TValue> m_Values; 15 private Dictionary<TKey, TValue> m_Dictionary = new Dictionary<TKey, TValue>(); 16 17 public TValue this[TKey key] 18 { 19 get { return m_Dictionary[key]; } 20 set { m_Dictionary[key] = value; } 21 } 22 23 public TKey this[TValue value] 24 { 25 get 26 { 27 m_Keys = new List<TKey>(m_Dictionary.Keys); 28 m_Values = new List<TValue>(m_Dictionary.Values); 29 var index = m_Values.FindIndex(x => x.Equals(value)); 30 if (index < 0) 31 throw new KeyNotFoundException(); 32 return m_Keys[index]; 33 } 34 } 35 36 public ICollection<TKey> Keys 37 { 38 get { return m_Dictionary.Keys; } 39 } 40 41 public ICollection<TValue> Values 42 { 43 get { return m_Dictionary.Values; } 44 } 45 46 public void Add(TKey key, TValue value) 47 { 48 m_Dictionary.Add(key, value); 49 } 50 51 public bool ContainsKey(TKey key) 52 { 53 return m_Dictionary.ContainsKey(key); 54 } 55 56 public bool Remove(TKey key) 57 { 58 return m_Dictionary.Remove(key); 59 } 60 61 public bool TryGetValue(TKey key, out TValue value) 62 { 63 return m_Dictionary.TryGetValue(key, out value); 64 } 65 66 public void Clear() 67 { 68 m_Dictionary.Clear(); 69 } 70 71 public int Count 72 { 73 get { return m_Dictionary.Count; } 74 } 75 76 bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly 77 { 78 get { return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).IsReadOnly; } 79 } 80 81 void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) 82 { 83 (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Add(item); 84 } 85 86 bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) 87 { 88 return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Contains(item); 89 } 90 91 void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) 92 { 93 (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).CopyTo(array, arrayIndex); 94 } 95 96 bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) 97 { 98 return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Remove(item); 99 } 100 101 IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() 102 { 103 return (m_Dictionary as IEnumerable<KeyValuePair<TKey, TValue>>).GetEnumerator(); 104 } 105 106 public IEnumerator GetEnumerator() 107 { 108 return m_Dictionary.GetEnumerator(); 109 } 110 111 void ISerializationCallbackReceiver.OnBeforeSerialize() 112 { 113 m_Keys = new List<TKey>(m_Dictionary.Keys); 114 m_Values = new List<TValue>(m_Dictionary.Values); 115 } 116 117 void ISerializationCallbackReceiver.OnAfterDeserialize() 118 { 119 Debug.Assert(m_Keys.Count == m_Values.Count); 120 Clear(); 121 for (var i = 0; i < m_Keys.Count; ++i) 122 Add(m_Keys[i], m_Values[i]); 123 } 124 } 125}