diff --git a/Nickvision.Aura/Aura.cs b/Nickvision.Aura/Aura.cs index 325bb89..a4d17cd 100644 --- a/Nickvision.Aura/Aura.cs +++ b/Nickvision.Aura/Aura.cs @@ -1,7 +1,8 @@ -using Nickvision.Aura.Configuration; -using System; +using System; using System.Collections.Generic; +using System.IO; using System.Runtime.CompilerServices; +using System.Text.Json; using Tmds.DBus; [assembly: InternalsVisibleTo(Connection.DynamicAssemblyName)] @@ -15,16 +16,13 @@ public class Aura { private static Aura? _instance; + private readonly Dictionary _configFiles; + /// /// The AppInfo object /// public AppInfo AppInfo { get; init; } - /// - /// Dictionary of configuration files that were set - /// - public Dictionary ConfigFiles { get; init; } - /// /// Construct Aura /// @@ -32,12 +30,12 @@ public class Aura /// Name for AppInfo private Aura(string id, string name) { + _configFiles = new Dictionary(); AppInfo = new AppInfo() { ID = id, Name = name }; - ConfigFiles = new Dictionary(); } /// @@ -88,22 +86,31 @@ public IPCServer Communicate(string[] args) } /// - /// Set config to be loaded from JSON file + /// Gets a config object /// - /// Object type - /// File name - public void SetConfig(string key) where T : ConfigurationBase => ConfigFiles[key] = ConfigurationLoader.Load(key)!; - - /// - /// Save config to JSON file - /// - /// File name - public void SaveConfig(string key) + /// The type of ConfigurationBase + /// The name of the config object + /// The initalized config object + public T GetConfig(string key) where T : ConfigurationBase { - if (!ConfigFiles.ContainsKey(key)) + if(!_configFiles.ContainsKey(key)) { - throw new AuraException($"Configuration file \"{key}\" was not set."); + var path = $"{UserDirectories.ApplicationConfig}{Path.DirectorySeparatorChar}{key}.json"; + try + { + _configFiles[key] = JsonSerializer.Deserialize(File.ReadAllText(path))!; + } + catch + { + if (File.Exists(path)) + { + File.Move(path, $"{path}.bak", true); + } + File.WriteAllText(path, "{}"); + _configFiles[key] = JsonSerializer.Deserialize("{}")!; + } + _configFiles[key].Key = key; } - ConfigurationLoader.Save(ConfigFiles[key], key); + return (T)_configFiles[key]; } } diff --git a/Nickvision.Aura/Configuration/ConfigurationBase.cs b/Nickvision.Aura/Configuration/ConfigurationBase.cs deleted file mode 100644 index 5ab0083..0000000 --- a/Nickvision.Aura/Configuration/ConfigurationBase.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace Nickvision.Aura.Configuration; - -/// -/// Base class for configuration files -/// -public abstract class ConfigurationBase -{ - /// - /// Occurs when the configuration object is saved - /// - public event EventHandler? Saved; - - /// - /// Raises the Saved event of the object - /// - internal void RaiseSavedEvent() => Saved?.Invoke(this, EventArgs.Empty); -} \ No newline at end of file diff --git a/Nickvision.Aura/Configuration/ConfigurationLoader.cs b/Nickvision.Aura/Configuration/ConfigurationLoader.cs deleted file mode 100644 index 701a391..0000000 --- a/Nickvision.Aura/Configuration/ConfigurationLoader.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.IO; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Text.Unicode; - -namespace Nickvision.Aura.Configuration; - -/// -/// Loader for JSON configuration files -/// -internal static class ConfigurationLoader -{ - /// - /// Load object from JSON file - /// - /// Type of the object to deserialize - /// File name - /// Loaded or new object - public static T Load(string key) where T : ConfigurationBase - { - var path = $"{UserDirectories.ApplicationConfig}{Path.DirectorySeparatorChar}{key}.json"; - try - { - return JsonSerializer.Deserialize(File.ReadAllText(path))!; - } - catch - { - if (File.Exists(path)) - { - File.Move(path, $"{path}.bak", true); - } - File.WriteAllText(path, "{}"); - return JsonSerializer.Deserialize("{}")!; - } - } - - /// - /// Save object to JSON file - /// - /// IConfiguration object to save - /// File name - public static void Save(ConfigurationBase obj, string key) - { - File.WriteAllText($"{UserDirectories.ApplicationConfig}{Path.DirectorySeparatorChar}{key}.json", JsonSerializer.Serialize((object)obj, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) })); - obj.RaiseSavedEvent(); - } -} diff --git a/Nickvision.Aura/ConfigurationBase.cs b/Nickvision.Aura/ConfigurationBase.cs new file mode 100644 index 0000000..f854ae8 --- /dev/null +++ b/Nickvision.Aura/ConfigurationBase.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Unicode; + +namespace Nickvision.Aura; + +/// +/// Base class for configuration files +/// +public abstract class ConfigurationBase +{ + /// + /// The key of the Configuration object + /// + internal string? Key { get; set; } + + /// + /// Occurs when the configuration object is saved + /// + public event EventHandler? Saved; + + /// + /// Saves the configuration file + /// + public void Save() + { + if(string.IsNullOrWhiteSpace(Key)) + { + throw new ArgumentException("ConfigurationBase.Key must not be empty"); + } + File.WriteAllText($"{UserDirectories.ApplicationConfig}{Path.DirectorySeparatorChar}{Key}.json", JsonSerializer.Serialize((object)this, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) })); + Saved?.Invoke(this, EventArgs.Empty); + } +} \ No newline at end of file diff --git a/Nickvision.Aura/Nickvision.Aura.csproj b/Nickvision.Aura/Nickvision.Aura.csproj index 8874ac3..928f50b 100644 --- a/Nickvision.Aura/Nickvision.Aura.csproj +++ b/Nickvision.Aura/Nickvision.Aura.csproj @@ -5,7 +5,7 @@ enable true Nickvision.Aura - 2023.11.3 + 2023.11.4 Nickvision Nickvision A cross-platform base for Nickvision applications @@ -14,8 +14,7 @@ (c) Nickvision 2021-2023 https://nickvision.org https://github.com/NickvisionApps/Aura - - Added NotificationSentEventArgs and ShellNotificationSentEventArgs to Nickvision.Aura.Events namespace -- Updated to .NET 8 + - Improved the Configuration API logo-r.png README.md