using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace UnityEditor.SettingsManagement { /// /// Represents a collection of objects that implement . /// public sealed class Settings { ISettingsRepository[] m_SettingsRepositories; /// /// Called prior to when an instance of serializes its current state. /// public event Action beforeSettingsSaved; /// /// Called immediately after an instance of serializes its current state. /// public event Action afterSettingsSaved; Settings() { } /// /// Creates a new Settings instance with a and . /// /// The package name, such as `com.example.my-package`. /// The name of the settings file. The default value is `Settings`. public Settings(string package, string settingsFileName = "Settings") { m_SettingsRepositories = new ISettingsRepository[] { new PackageSettingsRepository(package, settingsFileName), new UserSettingsRepository() }; } /// /// Creates a new Settings instance with a collection of objects that implement . /// /// The repositories to populate the Settings instance with. public Settings(IEnumerable repositories) { m_SettingsRepositories = repositories.ToArray(); } /// /// Finds and returns a settings repository that matches the specified scope. /// /// The scope of the settings repository to match. /// /// An instance that implements the requested scope; or null if no /// matching repository is found. /// public ISettingsRepository GetRepository(SettingsScope scope) { foreach (var repo in m_SettingsRepositories) if (repo.scope == scope) return repo; return null; } /// /// Finds and returns a settings repository that matches the specified scope and name. /// /// The scope of the settings repository to match. /// The name of the to match. /// /// An instance that implements the specified scope and matches the name; or /// null if no matching repository is found. /// 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; } /// /// Serializes the state of all settings repositories. /// public void Save() { if (beforeSettingsSaved != null) beforeSettingsSaved(); foreach (var repo in m_SettingsRepositories) repo.Save(); if (afterSettingsSaved != null) afterSettingsSaved(); } /// /// Sets a value for a settings entry with a matching key and type `T`. /// /// The key used to identify the settings entry. /// The value to set. This must be serializable. /// The scope of the settings repository to match. /// The type of value that this key points to. public void Set(string key, T value, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) Set(key, value); Set(key, value); } /// /// Sets a value for a settings entry with a matching key and type `T` from the specified repository. /// /// The key used to identify the settings entry. /// The value to set. This must be serializable. /// Optional. The name of the repository to set this value in. /// The scope of the settings repository to match. /// The type of value that this key points to. public void Set(string key, T value, string repositoryName, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) Set(key, value, repositoryName); Set(key, value, repositoryName); } /// /// Sets a value for a settings entry with a matching key and type `T` from the specified repository of type `K`. /// /// The key used to identify the settings entry. /// The value to set. This must be serializable. /// Optional. The name of the repository to set this value in. /// The type of value that this key points to. /// The type of repository to search for matching keys. public void Set(string key, T value, string repositoryName = null) where K : ISettingsRepository { bool foundScopeRepository = false; foreach (var repo in m_SettingsRepositories) { if (repo is K && (string.IsNullOrEmpty(repositoryName) || repo.name == repositoryName)) { repo.Set(key, value); foundScopeRepository = true; } } if (!foundScopeRepository) Debug.LogWarning($"No repository with type {typeof(K)} found."); } /// /// Returns a value for a settings entry with a matching key and type `T`. /// /// The key used to identify the settings entry. /// The scope of the settings repository to match. /// Specify the value of type `T` to return if the entry can't be found. /// The type of value that this key points to. /// The value from a matching settings entry; or the default value if not found. public T Get(string key, SettingsScope scope = SettingsScope.Project, T fallback = default(T)) { if (scope == SettingsScope.Project) return Get(key, fallback); return Get(key, fallback); } /// /// Returns a value for a settings entry with a matching key and type `T`. /// /// The key used to identify the settings entry. /// The repository name to match. /// The scope of the settings repository to match. /// Specify the value of type `T` to return if the entry can't be found. /// The type of value that this key points to. /// The value from a matching settings entry; or the default value if not found. public T Get(string key, string repositoryName, SettingsScope scope = SettingsScope.Project, T fallback = default(T)) { if (scope == SettingsScope.Project) return Get(key, fallback, repositoryName); return Get(key, fallback, repositoryName); } /// /// Returns a value for a settings entry with a matching key and type `T` from the specified repository of type `K`. /// /// The key used to identify the settings entry. /// Specify the value of type `T` to return if the entry can't be found. /// If provided, only repositories with a matching name will be searched for the key. /// The type of value that this key points to. /// The type of repository to search for matching keys. /// The value from a matching settings entry; or the default value if not found. public T Get(string key, T fallback = default(T), string repositoryName = null) where K : ISettingsRepository { foreach (var repo in m_SettingsRepositories) { if (repo is K && (string.IsNullOrEmpty(repositoryName) || repo.name == repositoryName)) return repo.Get(key, fallback); } Debug.LogWarning($"No repository with type {typeof(K)} found."); return fallback; } /// /// Determines whether the repository in the specified scope contains a settings entry /// that matches the specified key and is of type `T`. /// /// The key used to identify the settings entry. /// The scope of the settings repository to match. /// The type of value that this key points to. /// True if a setting matching both key and type is found; false if no entry is found. public bool ContainsKey(string key, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) return ContainsKey(key); return ContainsKey(key); } /// /// Determines whether the specified repository contains a settings entry that matches the specified key and is of type `T`. /// /// The key used to identify the settings entry. /// The repository name to match. /// The scope of the settings repository to match. /// The type of value that this key points to. /// True if a setting matching both key and type is found; false if no entry is found. public bool ContainsKey(string key, string repositoryName, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) return ContainsKey(key, repositoryName); return ContainsKey(key, repositoryName); } /// /// Determines whether the specified repository of type `K` contains a settings entry that matches the specified key and is of type `T`. /// /// The key used to identify the settings entry. /// The repository name to match. /// The type of value that this key points to. /// The type of repository to search for matching keys. /// True if a setting matching both key and type is found; false if no entry is found. public bool ContainsKey(string key, string repositoryName = null) where K : ISettingsRepository { foreach (var repo in m_SettingsRepositories) { if (repo is K && (string.IsNullOrEmpty(repositoryName) || repositoryName == repo.name)) return repo.ContainsKey(key); } Debug.LogWarning($"No repository with type {typeof(K)} found."); return false; } /// /// Removes a key-value pair from a settings repository. This method identifies the settings entry to remove /// from any repository in the specified scope by matching the specified key /// for a value of type `T`. /// /// The key used to identify the settings entry. /// The scope of the settings repository to match. /// The type of value that this key points to. public void DeleteKey(string key, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) DeleteKey(key); DeleteKey(key); } /// /// Removes a key-value pair from a settings repository. This method identifies the settings entry to remove /// from the specified repository by matching the specified key for a value of type `T`. /// /// The key used to identify the settings entry. /// The repository name to match. /// The scope of the settings repository to match. /// The type of value that this key points to. public void DeleteKey(string key, string repositoryName, SettingsScope scope = SettingsScope.Project) { if (scope == SettingsScope.Project) DeleteKey(key, repositoryName); DeleteKey(key, repositoryName); } /// /// Removes a key-value pair from a settings repository. This method identifies the settings entry to remove /// from the specified repository of type `K` by matching the specified key for a value of type `T`. /// /// The key used to identify the settings entry. /// The repository name to match. /// The type of value that this key points to. /// The type of repository to search for matching keys. public void DeleteKey(string key, string repositoryName = null) where K : ISettingsRepository { bool foundScopeRepository = false; foreach (var repo in m_SettingsRepositories) { if (repo is K && (string.IsNullOrEmpty(repositoryName) || repositoryName == repo.name)) { foundScopeRepository = true; repo.Remove(key); } } if (!foundScopeRepository) Debug.LogWarning($"No repository with type {typeof(K)} found."); } } }