using System;
using System.IO;
using UnityEngine;
namespace UnityEditor.SettingsManagement
{
///
/// A settings repository that stores data to a JSON file.
///
[Serializable]
public class ProjectSettingsRepository : ISettingsRepository
{
const bool k_PrettyPrintJson = true;
string m_Path;
bool m_Initialized;
[SerializeField]
SettingsDictionary m_Dictionary = new SettingsDictionary();
///
/// Constructor sets the serialized data path.
///
/// The path to which settings will be saved in JSON format.
public ProjectSettingsRepository(string path)
{
m_Path = path;
m_Initialized = false;
}
void Init()
{
if (m_Initialized)
return;
m_Initialized = true;
if (File.Exists(path))
{
m_Dictionary = null;
var json = File.ReadAllText(path);
EditorJsonUtility.FromJsonOverwrite(json, this);
}
}
///
/// This repository implementation is relevant to the Project scope.
///
///
public SettingsScope scope
{
get { return SettingsScope.Project; }
}
///
/// File path to the serialized settings data.
///
///
public string path
{
get { return m_Path; }
}
///
/// Save all settings to their serialized state.
///
///
public void Save()
{
Init();
File.WriteAllText(path, EditorJsonUtility.ToJson(this, k_PrettyPrintJson));
}
///
/// Set a value for key of type T.
///
/// The settings key.
/// The value to set. Must be serializable.
/// Type of value.
///
public void Set(string key, T value)
{
Init();
m_Dictionary.Set(key, value);
}
///
/// Get a value with key of type T, or return the fallback value if no matching key is found.
///
/// The settings key.
/// 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, T fallback = default(T))
{
Init();
return m_Dictionary.Get(key, fallback);
}
///
/// Does the repository contain a setting with key and type.
///
/// The settings key.
/// The type of value to search for.
/// True if a setting matching both key and type is found, false if no entry is found.
///
public bool ContainsKey(string key)
{
Init();
return m_Dictionary.ContainsKey(key);
}
///
/// Remove a key value pair from the settings repository.
///
///
///
///
public void Remove(string key)
{
Init();
m_Dictionary.Remove(key);
}
}
}