Settings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.SettingsManagement
  6. {
  7. /// <summary>
  8. /// Settings manages a collection of <see cref="ISettingsRepository"/>.
  9. /// </summary>
  10. public sealed class Settings
  11. {
  12. ISettingsRepository[] m_SettingsRepositories;
  13. /// <value>
  14. /// An event that is raised prior to an `ISettingsRepository` serializing it's current state.
  15. /// </value>
  16. public event Action beforeSettingsSaved;
  17. /// <value>
  18. /// An event that is raised after an `ISettingsRepository` has serialized it's current state.
  19. /// </value>
  20. public event Action afterSettingsSaved;
  21. Settings()
  22. {
  23. }
  24. /// <summary>
  25. /// Create a new Settings instance with a <see cref="UserSettingsRepository"/> and <see cref="PackageSettingsRepository"/>.
  26. /// </summary>
  27. /// <param name="package">The package name. Ex, `com.unity.my-package`.</param>
  28. /// <param name="settingsFileName">The name of the settings file. Defaults to "Settings."</param>
  29. public Settings(string package, string settingsFileName = "Settings")
  30. {
  31. m_SettingsRepositories = new ISettingsRepository[]
  32. {
  33. new PackageSettingsRepository(package, settingsFileName),
  34. new UserSettingsRepository()
  35. };
  36. }
  37. /// <summary>
  38. /// Create a new Settings instance with a collection of <see cref="ISettingsRepository"/>.
  39. /// </summary>
  40. public Settings(IEnumerable<ISettingsRepository> repositories)
  41. {
  42. m_SettingsRepositories = repositories.ToArray();
  43. }
  44. /// <summary>
  45. /// Find a settings repository that matches the requested scope.
  46. /// </summary>
  47. /// <param name="scope">The scope of the settings repository to match.</param>
  48. /// <returns>
  49. /// An ISettingsRepository instance that is implementing the requested scope. May return null if no
  50. /// matching repository is found.
  51. /// </returns>
  52. public ISettingsRepository GetRepository(SettingsScope scope)
  53. {
  54. foreach (var repo in m_SettingsRepositories)
  55. if (repo.scope == scope)
  56. return repo;
  57. return null;
  58. }
  59. /// <summary>
  60. /// Find a settings repository that matches the requested scope and name.
  61. /// </summary>
  62. /// <param name="scope">The scope of the settings repository to match.</param>
  63. /// <param name="name">The name of the <see cref="ISettingsRepository"/> to match.</param>
  64. /// <returns>
  65. /// An <see cref="ISettingsRepository"/> instance that is implementing the requested scope, and matches name.
  66. /// May return null if no matching repository is found.
  67. /// </returns>
  68. public ISettingsRepository GetRepository(SettingsScope scope, string name)
  69. {
  70. foreach (var repo in m_SettingsRepositories)
  71. if (repo.scope == scope && string.Equals(repo.name, name))
  72. return repo;
  73. return null;
  74. }
  75. /// <summary>
  76. /// Serialize the state of all settings repositories.
  77. /// </summary>
  78. public void Save()
  79. {
  80. if (beforeSettingsSaved != null)
  81. beforeSettingsSaved();
  82. foreach (var repo in m_SettingsRepositories)
  83. repo.Save();
  84. if (afterSettingsSaved != null)
  85. afterSettingsSaved();
  86. }
  87. /// <summary>
  88. /// Set a value for key of type T.
  89. /// </summary>
  90. /// <param name="key">The settings key.</param>
  91. /// <param name="value">The value to set. Must be serializable.</param>
  92. /// <param name="scope">Which scope this settings should be saved in.</param>
  93. /// <typeparam name="T">Type of value.</typeparam>
  94. public void Set<T>(string key, T value, SettingsScope scope = SettingsScope.Project)
  95. {
  96. bool foundScopeRepository = false;
  97. foreach (var repo in m_SettingsRepositories)
  98. {
  99. if (repo.scope == scope)
  100. {
  101. repo.Set<T>(key, value);
  102. foundScopeRepository = true;
  103. }
  104. }
  105. if (!foundScopeRepository)
  106. Debug.LogWarning("No repository for scope " + scope + " found!");
  107. }
  108. /// <summary>
  109. /// Set a value for key of type T.
  110. /// </summary>
  111. /// <param name="key">The settings key.</param>
  112. /// <param name="value">The value to set. Must be serializable.</param>
  113. /// <param name="repositoryName">The repository name to match.</param>
  114. /// <param name="scope">Which scope this settings should be saved in.</param>
  115. /// <typeparam name="T">Type of value.</typeparam>
  116. public void Set<T>(string key, T value, string repositoryName, SettingsScope scope = SettingsScope.Project)
  117. {
  118. bool foundScopeRepository = false;
  119. foreach (var repo in m_SettingsRepositories)
  120. {
  121. if (repo.scope == scope && string.Equals(repo.name, repositoryName))
  122. {
  123. repo.Set<T>(key, value);
  124. foundScopeRepository = true;
  125. }
  126. }
  127. if (!foundScopeRepository)
  128. Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
  129. }
  130. /// <summary>
  131. /// Get a value with key of type T, or return the fallback value if no matching key is found.
  132. /// </summary>
  133. /// <param name="key">The settings key.</param>
  134. /// <param name="scope">Which scope this settings should be retrieved from.</param>
  135. /// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
  136. /// <typeparam name="T">Type of value to search for.</typeparam>
  137. public T Get<T>(string key, SettingsScope scope = SettingsScope.Project, T fallback = default(T))
  138. {
  139. foreach (var repo in m_SettingsRepositories)
  140. {
  141. if (repo.scope == scope)
  142. return repo.Get<T>(key, fallback);
  143. }
  144. Debug.LogWarning("No repository for scope " + scope + " found!");
  145. return fallback;
  146. }
  147. /// <summary>
  148. /// Get a value with key of type T, or return the fallback value if no matching key is found.
  149. /// </summary>
  150. /// <param name="key">The settings key.</param>
  151. /// <param name="repositoryName">The repository name to match.</param>
  152. /// <param name="scope">Which scope this settings should be retrieved from.</param>
  153. /// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
  154. /// <typeparam name="T">Type of value to search for.</typeparam>
  155. public T Get<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project, T fallback = default(T))
  156. {
  157. foreach (var repo in m_SettingsRepositories)
  158. {
  159. if (repo.scope == scope && string.Equals(repo.name, repositoryName))
  160. return repo.Get<T>(key, fallback);
  161. }
  162. Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
  163. return fallback;
  164. }
  165. /// <summary>
  166. /// Does the repository contain a setting with key and type.
  167. /// </summary>
  168. /// <param name="key">The settings key.</param>
  169. /// <typeparam name="T">The type of value to search for.</typeparam>
  170. /// <param name="scope">Which scope should be searched for matching key.</param>
  171. /// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
  172. public bool ContainsKey<T>(string key, SettingsScope scope = SettingsScope.Project)
  173. {
  174. foreach (var repo in m_SettingsRepositories)
  175. {
  176. if (repo.scope == scope)
  177. return repo.ContainsKey<T>(key);
  178. }
  179. Debug.LogWarning("No repository for scope " + scope + " found!");
  180. return false;
  181. }
  182. /// <summary>
  183. /// Does the repository contain a setting with key and type.
  184. /// </summary>
  185. /// <param name="key">The settings key.</param>
  186. /// <param name="repositoryName">The repository name to match.</param>
  187. /// <typeparam name="T">The type of value to search for.</typeparam>
  188. /// <param name="scope">Which scope should be searched for matching key.</param>
  189. /// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
  190. public bool ContainsKey<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project)
  191. {
  192. foreach (var repo in m_SettingsRepositories)
  193. {
  194. if (repo.scope == scope && string.Equals(repo.name, repositoryName))
  195. return repo.ContainsKey<T>(key);
  196. }
  197. Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
  198. return false;
  199. }
  200. /// <summary>
  201. /// Remove a key value pair from a settings repository.
  202. /// </summary>
  203. /// <param name="key">The key to remove.</param>
  204. /// <param name="scope">Which scope should be searched for matching key.</param>
  205. /// <typeparam name="T">The type that this key is pointing to.</typeparam>
  206. public void DeleteKey<T>(string key, SettingsScope scope = SettingsScope.Project)
  207. {
  208. bool foundScopeRepository = false;
  209. foreach (var repo in m_SettingsRepositories)
  210. {
  211. if (repo.scope == scope)
  212. {
  213. foundScopeRepository = true;
  214. repo.Remove<T>(key);
  215. }
  216. }
  217. if (!foundScopeRepository)
  218. Debug.LogWarning("No repository for scope " + scope + " found!");
  219. }
  220. /// <summary>
  221. /// Remove a key value pair from a settings repository.
  222. /// </summary>
  223. /// <param name="key">The key to remove.</param>
  224. /// <param name="repositoryName">The repository name to match.</param>
  225. /// <param name="scope">Which scope should be searched for matching key.</param>
  226. /// <typeparam name="T">The type that this key is pointing to.</typeparam>
  227. public void DeleteKey<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project)
  228. {
  229. bool foundScopeRepository = false;
  230. foreach (var repo in m_SettingsRepositories)
  231. {
  232. if (repo.scope == scope && string.Equals(repo.name, repositoryName))
  233. {
  234. foundScopeRepository = true;
  235. repo.Remove<T>(key);
  236. }
  237. }
  238. if (!foundScopeRepository)
  239. Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
  240. }
  241. }
  242. }