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