123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace UnityEditor.SettingsManagement
- {
- /// <summary>
- /// Settings manages a collection of <see cref="ISettingsRepository"/>.
- /// </summary>
- public sealed class Settings
- {
- ISettingsRepository[] m_SettingsRepositories;
- /// <value>
- /// An event that is raised prior to an `ISettingsRepository` serializing it's current state.
- /// </value>
- public event Action beforeSettingsSaved;
- /// <value>
- /// An event that is raised after an `ISettingsRepository` has serialized it's current state.
- /// </value>
- public event Action afterSettingsSaved;
- Settings()
- {
- }
- /// <summary>
- /// Create a new Settings instance with a <see cref="UserSettingsRepository"/> and <see cref="PackageSettingsRepository"/>.
- /// </summary>
- /// <param name="package">The package name. Ex, `com.unity.my-package`.</param>
- /// <param name="settingsFileName">The name of the settings file. Defaults to "Settings."</param>
- public Settings(string package, string settingsFileName = "Settings")
- {
- m_SettingsRepositories = new ISettingsRepository[]
- {
- new PackageSettingsRepository(package, settingsFileName),
- new UserSettingsRepository()
- };
- }
- /// <summary>
- /// Create a new Settings instance with a collection of <see cref="ISettingsRepository"/>.
- /// </summary>
- public Settings(IEnumerable<ISettingsRepository> repositories)
- {
- m_SettingsRepositories = repositories.ToArray();
- }
- /// <summary>
- /// Find a settings repository that matches the requested scope.
- /// </summary>
- /// <param name="scope">The scope of the settings repository to match.</param>
- /// <returns>
- /// An ISettingsRepository instance that is implementing the requested scope. May return null if no
- /// matching repository is found.
- /// </returns>
- public ISettingsRepository GetRepository(SettingsScope scope)
- {
- foreach (var repo in m_SettingsRepositories)
- if (repo.scope == scope)
- return repo;
- return null;
- }
- /// <summary>
- /// Find a settings repository that matches the requested scope and name.
- /// </summary>
- /// <param name="scope">The scope of the settings repository to match.</param>
- /// <param name="name">The name of the <see cref="ISettingsRepository"/> to match.</param>
- /// <returns>
- /// An <see cref="ISettingsRepository"/> instance that is implementing the requested scope, and matches name.
- /// May return null if no matching repository is found.
- /// </returns>
- public ISettingsRepository GetRepository(SettingsScope scope, string name)
- {
- foreach (var repo in m_SettingsRepositories)
- if (repo.scope == scope && string.Equals(repo.name, name))
- return repo;
- return null;
- }
- /// <summary>
- /// Serialize the state of all settings repositories.
- /// </summary>
- public void Save()
- {
- if (beforeSettingsSaved != null)
- beforeSettingsSaved();
- foreach (var repo in m_SettingsRepositories)
- repo.Save();
- if (afterSettingsSaved != null)
- afterSettingsSaved();
- }
- /// <summary>
- /// Set a value for key of type T.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <param name="value">The value to set. Must be serializable.</param>
- /// <param name="scope">Which scope this settings should be saved in.</param>
- /// <typeparam name="T">Type of value.</typeparam>
- public void Set<T>(string key, T value, SettingsScope scope = SettingsScope.Project)
- {
- bool foundScopeRepository = false;
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope)
- {
- repo.Set<T>(key, value);
- foundScopeRepository = true;
- }
- }
- if (!foundScopeRepository)
- Debug.LogWarning("No repository for scope " + scope + " found!");
- }
- /// <summary>
- /// Set a value for key of type T.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <param name="value">The value to set. Must be serializable.</param>
- /// <param name="repositoryName">The repository name to match.</param>
- /// <param name="scope">Which scope this settings should be saved in.</param>
- /// <typeparam name="T">Type of value.</typeparam>
- public void Set<T>(string key, T value, string repositoryName, SettingsScope scope = SettingsScope.Project)
- {
- bool foundScopeRepository = false;
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope && string.Equals(repo.name, repositoryName))
- {
- repo.Set<T>(key, value);
- foundScopeRepository = true;
- }
- }
- if (!foundScopeRepository)
- Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
- }
- /// <summary>
- /// Get a value with key of type T, or return the fallback value if no matching key is found.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <param name="scope">Which scope this settings should be retrieved from.</param>
- /// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
- /// <typeparam name="T">Type of value to search for.</typeparam>
- public T Get<T>(string key, SettingsScope scope = SettingsScope.Project, T fallback = default(T))
- {
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope)
- return repo.Get<T>(key, fallback);
- }
- Debug.LogWarning("No repository for scope " + scope + " found!");
- return fallback;
- }
- /// <summary>
- /// Get a value with key of type T, or return the fallback value if no matching key is found.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <param name="repositoryName">The repository name to match.</param>
- /// <param name="scope">Which scope this settings should be retrieved from.</param>
- /// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
- /// <typeparam name="T">Type of value to search for.</typeparam>
- public T Get<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project, T fallback = default(T))
- {
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope && string.Equals(repo.name, repositoryName))
- return repo.Get<T>(key, fallback);
- }
- Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
- return fallback;
- }
- /// <summary>
- /// Does the repository contain a setting with key and type.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <typeparam name="T">The type of value to search for.</typeparam>
- /// <param name="scope">Which scope should be searched for matching key.</param>
- /// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
- public bool ContainsKey<T>(string key, SettingsScope scope = SettingsScope.Project)
- {
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope)
- return repo.ContainsKey<T>(key);
- }
- Debug.LogWarning("No repository for scope " + scope + " found!");
- return false;
- }
- /// <summary>
- /// Does the repository contain a setting with key and type.
- /// </summary>
- /// <param name="key">The settings key.</param>
- /// <param name="repositoryName">The repository name to match.</param>
- /// <typeparam name="T">The type of value to search for.</typeparam>
- /// <param name="scope">Which scope should be searched for matching key.</param>
- /// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
- public bool ContainsKey<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project)
- {
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope && string.Equals(repo.name, repositoryName))
- return repo.ContainsKey<T>(key);
- }
- Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
- return false;
- }
- /// <summary>
- /// Remove a key value pair from a settings repository.
- /// </summary>
- /// <param name="key">The key to remove.</param>
- /// <param name="scope">Which scope should be searched for matching key.</param>
- /// <typeparam name="T">The type that this key is pointing to.</typeparam>
- public void DeleteKey<T>(string key, SettingsScope scope = SettingsScope.Project)
- {
- bool foundScopeRepository = false;
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope)
- {
- foundScopeRepository = true;
- repo.Remove<T>(key);
- }
- }
- if (!foundScopeRepository)
- Debug.LogWarning("No repository for scope " + scope + " found!");
- }
- /// <summary>
- /// Remove a key value pair from a settings repository.
- /// </summary>
- /// <param name="key">The key to remove.</param>
- /// <param name="repositoryName">The repository name to match.</param>
- /// <param name="scope">Which scope should be searched for matching key.</param>
- /// <typeparam name="T">The type that this key is pointing to.</typeparam>
- public void DeleteKey<T>(string key, string repositoryName, SettingsScope scope = SettingsScope.Project)
- {
- bool foundScopeRepository = false;
- foreach (var repo in m_SettingsRepositories)
- {
- if (repo.scope == scope && string.Equals(repo.name, repositoryName))
- {
- foundScopeRepository = true;
- repo.Remove<T>(key);
- }
- }
- if (!foundScopeRepository)
- Debug.LogWarning("No repository matching name \"" + repositoryName + "\" and scope " + scope + " found in settings.");
- }
- }
- }
|