diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Settings/SettingsManager.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/SettingsManager.cs | 190 |
1 files changed, 78 insertions, 112 deletions
diff --git a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs index 7bc15d950..1f0083bb5 100644 --- a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs +++ b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs @@ -1,162 +1,128 @@ -using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; -using Tango.Logging; -using Tango.Serialization; namespace Tango.Settings { /// <summary> - /// Represents a settings manager for loading and saving application settings. + /// Represents a settings manager capable of holding a collection of settings objects, saving and loading them using JSON. /// </summary> - public static class SettingsManager + public class SettingsManager { - private static LogManager LogManager = LogManager.Default; + #region Singleton + private static SettingsManager _default; /// <summary> - /// Gets or sets a value indicating whether the settings manager is initialized. + /// Gets the default settings manager instance. /// </summary> - public static bool IsInitialized { get; private set; } + public static SettingsManager Default + { + get + { + if (_default == null) + { + _default = new SettingsManager(); + } - /// <summary> - /// Gets or sets the default settings. - /// </summary> - /// <value> - /// The default. - /// </value> - public static SettingsCollection Default { get; private set; } + return _default; + } + } - /// <summary> - /// Gets or sets the default file path. - /// </summary> - /// <value> - /// The default file path. - /// </value> - public static String DefaultFilePath { get; private set; } + #endregion - /// <summary> - /// Gets or sets the default folder for application settings. - /// </summary> - public static String DefaultFolder { get; private set; } + private List<SettingsBase> _settingsCollection; + private JsonSerializerSettings _jsonSettings; + private bool _loaded; /// <summary> - /// Saves application settings to XML file. + /// Gets or sets the settings file path. /// </summary> - /// <param name="filePath">The path to the XML file.</param> - /// <param name="stCollection">The instance of the SettingsCollection to save.</param> - public static void SaveToXML(String filePath, SettingsCollection stCollection) - { - LogManager.Log("Saving application configuration to " + "'" + filePath + "'"); - XmlDataSerializer serializer = new XmlDataSerializer(); - serializer.SerializeToFile<SettingsCollection>(stCollection, filePath); - } + public String FilePath { get; protected set; } /// <summary> - /// Loads application settings from XML file. + /// Gets or sets the settings folder. /// </summary> - /// <param name="filePath">The file path.</param> - /// <returns></returns> - public static SettingsCollection LoadFromXML(String filePath) - { - LogManager.Log("Loading application configuration from " + "'" + filePath + "'"); - XmlDataSerializer serialier = new XmlDataSerializer(); - return serialier.DeserializeFromFile<SettingsCollection>(filePath); - } + public String Folder { get; protected set; } /// <summary> - /// Saves the default settings. + /// Prevents a default instance of the <see cref="SettingsManager"/> class from being created. /// </summary> - public static void SaveDefaultSettings() + private SettingsManager() { - SaveToXML(DefaultFilePath, Default); - } + FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.json"); + Folder = Path.GetDirectoryName(FilePath); + _settingsCollection = new List<SettingsBase>(); - /// <summary> - /// Restores to default. - /// </summary> - public static void RestoreToDefault() - { - Default = new SettingsCollection(); - SaveDefaultSettings(); + _jsonSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All, + Formatting = Formatting.Indented, + }; + + _jsonSettings.Converters.Add(new StringEnumConverter(false)); } - /// <summary> - /// Initializes the <see cref="SettingsManager"/> class. - /// </summary> - static SettingsManager() + private void EnsureLoaded() { - Initialize(); + if (!_loaded) + { + Load(); + } } /// <summary> - /// Initializes the settings manager. + /// Gets or creates the specified settings object type. /// </summary> - private static void Initialize() + /// <typeparam name="T"></typeparam> + /// <returns></returns> + public T GetOrCreate<T>() where T : SettingsBase { - if (IsInitialized) return; - - IsInitialized = true; + EnsureLoaded(); - LogManager.Log("Initializing application configuration..."); + var settings = _settingsCollection.SingleOrDefault(x => x.GetType() == typeof(T)) as T; - DefaultFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.xml"); - DefaultFolder = Path.GetDirectoryName(DefaultFilePath); - - bool waited = false; - - Retry: - - try + if (settings == null) { - Default = LoadFromXML(DefaultFilePath); + settings = Activator.CreateInstance<T>(); + settings.SaveAction = Save; + _settingsCollection.Add(settings); } - catch (Exception ex) - { - if (!waited) - { - LogManager.Log(ex, "Could not load application configuration. Retrying in 1 second..."); - waited = true; - Thread.Sleep(1000); - goto Retry; - } - else - { - LogManager.Log(ex, "Could not load application configuration."); - } - try - { - LogManager.Log("Creating application configuration directory structure..."); - Directory.CreateDirectory(DefaultFolder); - } - catch (Exception ex2) - { - LogManager.Log(ex2, "Could not generate directory structure."); - } - } + return settings; + } - if (Default == null) + /// <summary> + /// Loads the settings from the <see cref="FilePath"/>. + /// </summary> + protected virtual void Load() + { + if (File.Exists(FilePath)) { - Default = new SettingsCollection(); + _settingsCollection = JsonConvert.DeserializeObject<List<SettingsBase>>(File.ReadAllText(FilePath), _jsonSettings); - try + foreach (var settings in _settingsCollection) { - LogManager.Log("Generating default configuration file."); - SaveToXML(DefaultFilePath, Default); + settings.SaveAction = Save; } - catch (Exception ex) - { - LogManager.Log(ex, "Could not generate default configuration file."); - } - } - else - { - LogManager.Log("Application configuration loaded successfully."); } + + _loaded = true; + } + + /// <summary> + /// Saves settings. + /// </summary> + public virtual void Save() + { + EnsureLoaded(); + + String json = JsonConvert.SerializeObject(_settingsCollection, _jsonSettings); + File.WriteAllText(FilePath, json); } } } |
