Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
V2023.11.4
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Nov 22, 2023
1 parent a866f8a commit 41c9a35
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 90 deletions.
49 changes: 28 additions & 21 deletions Nickvision.Aura/Aura.cs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -15,29 +16,26 @@ public class Aura
{
private static Aura? _instance;

private readonly Dictionary<string, ConfigurationBase> _configFiles;

/// <summary>
/// The AppInfo object
/// </summary>
public AppInfo AppInfo { get; init; }

/// <summary>
/// Dictionary of configuration files that were set
/// </summary>
public Dictionary<string, ConfigurationBase> ConfigFiles { get; init; }

/// <summary>
/// Construct Aura
/// </summary>
/// <param name="id">ID for AppInfo</param>
/// <param name="name">Name for AppInfo</param>
private Aura(string id, string name)
{
_configFiles = new Dictionary<string, ConfigurationBase>();
AppInfo = new AppInfo()
{
ID = id,
Name = name
};
ConfigFiles = new Dictionary<string, ConfigurationBase>();
}

/// <summary>
Expand Down Expand Up @@ -88,22 +86,31 @@ public IPCServer Communicate(string[] args)
}

/// <summary>
/// Set config to be loaded from JSON file
/// Gets a config object
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="key">File name</param>
public void SetConfig<T>(string key) where T : ConfigurationBase => ConfigFiles[key] = ConfigurationLoader.Load<T>(key)!;

/// <summary>
/// Save config to JSON file
/// </summary>
/// <param name="key">File name</param>
public void SaveConfig(string key)
/// <typeparam name="T">The type of ConfigurationBase</typeparam>
/// <param name="key">The name of the config object</param>
/// <returns>The initalized config object</returns>

Check failure on line 93 in Nickvision.Aura/Aura.cs

View workflow job for this annotation

GitHub Actions / codespell

initalized ==> initialized
public T GetConfig<T>(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<T>(File.ReadAllText(path))!;
}
catch
{
if (File.Exists(path))
{
File.Move(path, $"{path}.bak", true);
}
File.WriteAllText(path, "{}");
_configFiles[key] = JsonSerializer.Deserialize<T>("{}")!;
}
_configFiles[key].Key = key;
}
ConfigurationLoader.Save(ConfigFiles[key], key);
return (T)_configFiles[key];
}
}
19 changes: 0 additions & 19 deletions Nickvision.Aura/Configuration/ConfigurationBase.cs

This file was deleted.

47 changes: 0 additions & 47 deletions Nickvision.Aura/Configuration/ConfigurationLoader.cs

This file was deleted.

36 changes: 36 additions & 0 deletions Nickvision.Aura/ConfigurationBase.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Base class for configuration files
/// </summary>
public abstract class ConfigurationBase
{
/// <summary>
/// The key of the Configuration object
/// </summary>
internal string? Key { get; set; }

/// <summary>
/// Occurs when the configuration object is saved
/// </summary>
public event EventHandler<EventArgs>? Saved;

/// <summary>
/// Saves the configuration file
/// </summary>
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);
}
}
5 changes: 2 additions & 3 deletions Nickvision.Aura/Nickvision.Aura.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>Nickvision.Aura</PackageId>
<Version>2023.11.3</Version>
<Version>2023.11.4</Version>
<Company>Nickvision</Company>
<Authors>Nickvision</Authors>
<Description>A cross-platform base for Nickvision applications</Description>
Expand All @@ -14,8 +14,7 @@
<Copyright>(c) Nickvision 2021-2023</Copyright>
<PackageProjectUrl>https://nickvision.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/NickvisionApps/Aura</RepositoryUrl>
<PackageReleaseNotes>- Added NotificationSentEventArgs and ShellNotificationSentEventArgs to Nickvision.Aura.Events namespace
- Updated to .NET 8</PackageReleaseNotes>
<PackageReleaseNotes>- Improved the Configuration API</PackageReleaseNotes>
<PackageIcon>logo-r.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down

0 comments on commit 41c9a35

Please sign in to comment.