SettingsDictionary.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.SettingsManagement
  5. {
  6. [Serializable]
  7. sealed class SettingsDictionary : ISerializationCallbackReceiver
  8. {
  9. [Serializable]
  10. struct SettingsKeyValuePair
  11. {
  12. public string type;
  13. public string key;
  14. public string value;
  15. }
  16. #pragma warning disable 0649
  17. [SerializeField]
  18. List<SettingsKeyValuePair> m_DictionaryValues = new List<SettingsKeyValuePair>();
  19. #pragma warning restore 0649
  20. internal Dictionary<Type, Dictionary<string, string>> dictionary = new Dictionary<Type, Dictionary<string, string>>();
  21. public bool ContainsKey<T>(string key)
  22. {
  23. return dictionary.ContainsKey(typeof(T)) && dictionary[typeof(T)].ContainsKey(key);
  24. }
  25. public void Set<T>(string key, T value)
  26. {
  27. if (string.IsNullOrEmpty(key))
  28. throw new ArgumentNullException("key");
  29. var type = typeof(T).AssemblyQualifiedName;
  30. SetJson(type, key, ValueWrapper<T>.Serialize(value));
  31. }
  32. internal void SetJson(string type, string key, string value)
  33. {
  34. var typeValue = Type.GetType(type);
  35. if (typeValue == null)
  36. throw new ArgumentException("\"type\" must be an assembly qualified type name.");
  37. Dictionary<string, string> entries;
  38. if (!dictionary.TryGetValue(typeValue, out entries))
  39. dictionary.Add(typeValue, entries = new Dictionary<string, string>());
  40. if (entries.ContainsKey(key))
  41. entries[key] = value;
  42. else
  43. entries.Add(key, value);
  44. }
  45. public T Get<T>(string key, T fallback = default(T))
  46. {
  47. if (string.IsNullOrEmpty(key))
  48. throw new ArgumentNullException("key");
  49. Dictionary<string, string> entries;
  50. if (dictionary.TryGetValue(typeof(T), out entries) && entries.ContainsKey(key))
  51. {
  52. try
  53. {
  54. return ValueWrapper<T>.Deserialize(entries[key]);
  55. }
  56. catch
  57. {
  58. return fallback;
  59. }
  60. }
  61. return fallback;
  62. }
  63. public void Remove<T>(string key)
  64. {
  65. Dictionary<string, string> entries;
  66. if (!dictionary.TryGetValue(typeof(T), out entries) || !entries.ContainsKey(key))
  67. return;
  68. entries.Remove(key);
  69. }
  70. public void OnBeforeSerialize()
  71. {
  72. if (m_DictionaryValues == null)
  73. return;
  74. m_DictionaryValues.Clear();
  75. foreach (var type in dictionary)
  76. {
  77. foreach (var entry in type.Value)
  78. {
  79. m_DictionaryValues.Add(new SettingsKeyValuePair()
  80. {
  81. type = type.Key.AssemblyQualifiedName,
  82. key = entry.Key,
  83. value = entry.Value
  84. });
  85. }
  86. }
  87. }
  88. public void OnAfterDeserialize()
  89. {
  90. dictionary.Clear();
  91. foreach (var entry in m_DictionaryValues)
  92. {
  93. Dictionary<string, string> entries;
  94. var type = Type.GetType(entry.type);
  95. if (type == null)
  96. {
  97. Debug.LogWarning("Could not instantiate type \"" + entry.type + "\". Skipping key: " + entry.key + ".");
  98. continue;
  99. }
  100. if (dictionary.TryGetValue(type, out entries))
  101. entries.Add(entry.key, entry.value);
  102. else
  103. dictionary.Add(type, new Dictionary<string, string>() { { entry.key, entry.value } });
  104. }
  105. }
  106. public override string ToString()
  107. {
  108. var sb = new System.Text.StringBuilder();
  109. foreach (var type in dictionary)
  110. {
  111. sb.AppendLine("Type: " + type.Key);
  112. foreach (var entry in type.Value)
  113. {
  114. sb.AppendLine(string.Format(" {0,-64}{1}", entry.Key, entry.Value));
  115. }
  116. }
  117. return sb.ToString();
  118. }
  119. }
  120. }