diff options
author | chai <chaifix@163.com> | 2021-09-08 10:52:35 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-09-08 10:52:35 +0800 |
commit | 21e186f75b504d832d9c7bef0456edd7d5d3155e (patch) | |
tree | e73c43fc78d0326f32da5fadfda57fa8e23c1d90 /Assets/SerializableDictionary/SerializableDictionary.cs | |
parent | 7bd7b4c6c3be6840cab06aa4d8a38619bce44705 (diff) |
+behavior design
Diffstat (limited to 'Assets/SerializableDictionary/SerializableDictionary.cs')
-rw-r--r-- | Assets/SerializableDictionary/SerializableDictionary.cs | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/Assets/SerializableDictionary/SerializableDictionary.cs b/Assets/SerializableDictionary/SerializableDictionary.cs deleted file mode 100644 index 4614ed7f..00000000 --- a/Assets/SerializableDictionary/SerializableDictionary.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.Serialization; -using UnityEngine; - -public abstract class SerializableDictionaryBase<TKey, TValue, TValueStorage> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver -{ - [SerializeField] - TKey[] m_keys; - [SerializeField] - TValueStorage[] m_values; - - public SerializableDictionaryBase() - { - } - - public SerializableDictionaryBase(IDictionary<TKey, TValue> dict) : base(dict.Count) - { - foreach (var kvp in dict) - { - this[kvp.Key] = kvp.Value; - } - } - - protected SerializableDictionaryBase(SerializationInfo info, StreamingContext context) : base(info,context){} - - protected abstract void SetValue(TValueStorage[] storage, int i, TValue value); - protected abstract TValue GetValue(TValueStorage[] storage, int i); - - public void CopyFrom(IDictionary<TKey, TValue> dict) - { - this.Clear(); - foreach (var kvp in dict) - { - this[kvp.Key] = kvp.Value; - } - } - - public void OnAfterDeserialize() - { - if(m_keys != null && m_values != null && m_keys.Length == m_values.Length) - { - this.Clear(); - int n = m_keys.Length; - for(int i = 0; i < n; ++i) - { - this[m_keys[i]] = GetValue(m_values, i); - } - - m_keys = null; - m_values = null; - } - - } - - public void OnBeforeSerialize() - { - int n = this.Count; - m_keys = new TKey[n]; - m_values = new TValueStorage[n]; - - int i = 0; - foreach(var kvp in this) - { - m_keys[i] = kvp.Key; - SetValue(m_values, i, kvp.Value); - ++i; - } - } -} - -public class SerializableDictionary<TKey, TValue> : SerializableDictionaryBase<TKey, TValue, TValue> -{ - public SerializableDictionary() - { - } - - public SerializableDictionary(IDictionary<TKey, TValue> dict) : base(dict) - { - } - - protected SerializableDictionary(SerializationInfo info, StreamingContext context) : base(info,context){} - - protected override TValue GetValue(TValue[] storage, int i) - { - return storage[i]; - } - - protected override void SetValue(TValue[] storage, int i, TValue value) - { - storage[i] = value; - } -} - -public static class SerializableDictionary -{ - public class Storage<T> - { - public T data; - } -} - -public class SerializableDictionary<TKey, TValue, TValueStorage> : SerializableDictionaryBase<TKey, TValue, TValueStorage> where TValueStorage : SerializableDictionary.Storage<TValue>, new() -{ - public SerializableDictionary() - { - } - - public SerializableDictionary(IDictionary<TKey, TValue> dict) : base(dict) - { - } - - protected SerializableDictionary(SerializationInfo info, StreamingContext context) : base(info,context){} - - protected override TValue GetValue(TValueStorage[] storage, int i) - { - return storage[i].data; - } - - protected override void SetValue(TValueStorage[] storage, int i, TValue value) - { - storage[i] = new TValueStorage(); - storage[i].data = value; - } -} |