using System; using System.Collections.Generic; using UnityEngine; namespace UnityEditor.SettingsManagement { [Serializable] sealed class SettingsDictionary : ISerializationCallbackReceiver { [Serializable] struct SettingsKeyValuePair { public string type; public string key; public string value; } #pragma warning disable 0649 [SerializeField] List m_DictionaryValues = new List(); #pragma warning restore 0649 internal Dictionary> dictionary = new Dictionary>(); public bool ContainsKey(string key) { return dictionary.ContainsKey(typeof(T)) && dictionary[typeof(T)].ContainsKey(key); } public void Set(string key, T value) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); var type = typeof(T).AssemblyQualifiedName; SetJson(type, key, ValueWrapper.Serialize(value)); } internal void SetJson(string type, string key, string value) { var typeValue = Type.GetType(type); if (typeValue == null) throw new ArgumentException("\"type\" must be an assembly qualified type name."); Dictionary entries; if (!dictionary.TryGetValue(typeValue, out entries)) dictionary.Add(typeValue, entries = new Dictionary()); if (entries.ContainsKey(key)) entries[key] = value; else entries.Add(key, value); } public T Get(string key, T fallback = default(T)) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); Dictionary entries; if (dictionary.TryGetValue(typeof(T), out entries) && entries.ContainsKey(key)) { try { return ValueWrapper.Deserialize(entries[key]); } catch { return fallback; } } return fallback; } public void Remove(string key) { Dictionary entries; if (!dictionary.TryGetValue(typeof(T), out entries) || !entries.ContainsKey(key)) return; entries.Remove(key); } public void OnBeforeSerialize() { if (m_DictionaryValues == null) return; m_DictionaryValues.Clear(); foreach (var type in dictionary) { foreach (var entry in type.Value) { m_DictionaryValues.Add(new SettingsKeyValuePair() { type = type.Key.AssemblyQualifiedName, key = entry.Key, value = entry.Value }); } } } public void OnAfterDeserialize() { dictionary.Clear(); foreach (var entry in m_DictionaryValues) { Dictionary entries; var type = Type.GetType(entry.type); if (type == null) { Debug.LogWarning("Could not instantiate type \"" + entry.type + "\". Skipping key: " + entry.key + "."); continue; } if (dictionary.TryGetValue(type, out entries)) entries.Add(entry.key, entry.value); else dictionary.Add(type, new Dictionary() { { entry.key, entry.value } }); } } public override string ToString() { var sb = new System.Text.StringBuilder(); foreach (var type in dictionary) { sb.AppendLine("Type: " + type.Key); foreach (var entry in type.Value) { sb.AppendLine(string.Format(" {0,-64}{1}", entry.Key, entry.Value)); } } return sb.ToString(); } } }