123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace UnityEditor.SettingsManagement
- {
-
-
-
- public sealed class Settings
- {
- ISettingsRepository[] m_SettingsRepositories;
-
-
-
- public event Action beforeSettingsSaved;
-
-
-
- public event Action afterSettingsSaved;
- Settings()
- {
- }
-
-
-
-
-
- public Settings(string package, string settingsFileName = "Settings")
- {
- m_SettingsRepositories = new ISettingsRepository[]
- {
- new PackageSettingsRepository(package, settingsFileName),
- new UserSettingsRepository()
- };
- }
-
-
-
- public Settings(IEnumerable<ISettingsRepository> repositories)
- {
- m_SettingsRepositories = repositories.ToArray();
- }
-
-
-
-
-
-
-
-
- public ISettingsRepository GetRepository(SettingsScope scope)
- {
- foreach (var repo in m_SettingsRepositories)
- if (repo.scope == scope)
- return repo;
- return null;
- }
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
- public void Save()
- {
- if (beforeSettingsSaved != null)
- beforeSettingsSaved();
- foreach (var repo in m_SettingsRepositories)
- repo.Save();
- if (afterSettingsSaved != null)
- afterSettingsSaved();
- }
-
-
-
-
-
-
-
- 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!");
- }
-
-
-
-
-
-
-
-
- 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.");
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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!");
- }
-
-
-
-
-
-
-
- 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.");
- }
- }
- }
|