From f234433cf2e5fc38e51e87bff39cacc70d7d9ac0 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Thu, 16 May 2024 21:01:24 -0500 Subject: [PATCH] Add settings infrastructure --- ZuneModCore/Mod.cs | 4 +- ZuneModCore/Services/IModCoreConfig.cs | 7 ++ ZuneModdingHelper/App.xaml.cs | 5 ++ ZuneModdingHelper/Services/Settings.cs | 28 ++++++++ .../SystemTextJsonStreanSerializer.cs | 67 +++++++++++++++++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 ZuneModCore/Services/IModCoreConfig.cs create mode 100644 ZuneModdingHelper/Services/Settings.cs create mode 100644 ZuneModdingHelper/Services/SystemTextJsonStreanSerializer.cs diff --git a/ZuneModCore/Mod.cs b/ZuneModCore/Mod.cs index dcb33a9..6526cdf 100644 --- a/ZuneModCore/Mod.cs +++ b/ZuneModCore/Mod.cs @@ -22,7 +22,9 @@ namespace ZuneModCore typeof(MbidLocatorMod), }.AsReadOnly(); - public static string ZuneInstallDir { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Zune"); + public static string DefaultZuneInstallDir { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Zune"); + + public string ZuneInstallDir { get; set; } = DefaultZuneInstallDir; private static List? _mods; public static IReadOnlyList AvailableMods diff --git a/ZuneModCore/Services/IModCoreConfig.cs b/ZuneModCore/Services/IModCoreConfig.cs new file mode 100644 index 0000000..e402270 --- /dev/null +++ b/ZuneModCore/Services/IModCoreConfig.cs @@ -0,0 +1,7 @@ +namespace ZuneModCore.Services +{ + public interface IModCoreConfig + { + string ZuneInstallDir { get; } + } +} diff --git a/ZuneModdingHelper/App.xaml.cs b/ZuneModdingHelper/App.xaml.cs index 68e8d87..abd1afa 100644 --- a/ZuneModdingHelper/App.xaml.cs +++ b/ZuneModdingHelper/App.xaml.cs @@ -1,6 +1,8 @@ using System.Windows; using CommunityToolkit.Mvvm.DependencyInjection; using Microsoft.Extensions.DependencyInjection; +using ZuneModCore.Services; +using ZuneModdingHelper.Services; namespace ZuneModdingHelper { @@ -39,6 +41,9 @@ namespace ZuneModdingHelper Octokit.IGitHubClient github = new Octokit.GitHubClient(new Octokit.ProductHeaderValue(Title.Replace(" ", ""), VersionStr)); services.AddSingleton(github); + services.AddSingleton(Settings.Default); + services.AddSingleton(Settings.Default); + Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } } diff --git a/ZuneModdingHelper/Services/Settings.cs b/ZuneModdingHelper/Services/Settings.cs new file mode 100644 index 0000000..ab9b460 --- /dev/null +++ b/ZuneModdingHelper/Services/Settings.cs @@ -0,0 +1,28 @@ +using OwlCore.ComponentModel; +using OwlCore.Storage; +using System.IO; +using System; +using ZuneModCore; +using ZuneModCore.Services; + +namespace ZuneModdingHelper.Services; + +public class Settings(IModifiableFolder folder) : SettingsBase(folder, SystemTextJsonStreamSerializer.Singleton), IModCoreConfig +{ + static Settings() + { + string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ZuneModCore", "Settings"); + Directory.CreateDirectory(dir); + + OwlCore.Storage.SystemIO.SystemFolder settingsFolder = new(dir); + Default = new(settingsFolder); + } + + public static Settings Default { get; } + + public string ZuneInstallDir + { + get => GetSetting(() => Mod.DefaultZuneInstallDir); + set => SetSetting(value); + } +} diff --git a/ZuneModdingHelper/Services/SystemTextJsonStreanSerializer.cs b/ZuneModdingHelper/Services/SystemTextJsonStreanSerializer.cs new file mode 100644 index 0000000..a9de638 --- /dev/null +++ b/ZuneModdingHelper/Services/SystemTextJsonStreanSerializer.cs @@ -0,0 +1,67 @@ +using System; +using System.IO; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using OwlCore.ComponentModel; + +namespace ZuneModdingHelper.Services +{ + /// + /// An and implementation for serializing and deserializing streams using System.Text.Json. + /// + public class SystemTextJsonStreamSerializer : IAsyncSerializer, ISerializer + { + /// + /// A singleton instance for . + /// + public static SystemTextJsonStreamSerializer Singleton { get; } = new(); + + /// + public async Task SerializeAsync(T data, CancellationToken? cancellationToken = null) + { + MemoryStream stream = new(); + await JsonSerializer.SerializeAsync(stream, data, cancellationToken: cancellationToken.GetValueOrDefault()); + return stream; + } + + /// + public async Task SerializeAsync(Type inputType, object data, CancellationToken? cancellationToken = null) + { + MemoryStream stream = new(); + await JsonSerializer.SerializeAsync(stream, data, inputType, cancellationToken: cancellationToken.GetValueOrDefault()); + return stream; + } + + /// + public async Task DeserializeAsync(Stream serialized, CancellationToken? cancellationToken = null) + { + return await JsonSerializer.DeserializeAsync(serialized, cancellationToken: cancellationToken.GetValueOrDefault()) + ?? default; + } + + /// + public async Task DeserializeAsync(Type returnType, Stream serialized, CancellationToken? cancellationToken = null) + { + return await JsonSerializer.DeserializeAsync(serialized, returnType, cancellationToken: cancellationToken.GetValueOrDefault()) + ?? default; + } + + /// + public Stream Serialize(T data) => Serialize(typeof(T), data); + + /// + public Stream Serialize(Type type, object data) + { + MemoryStream stream = new(); + JsonSerializer.Serialize(stream, data, type); + return stream; + } + + /// + public TResult Deserialize(Stream serialized) => JsonSerializer.Deserialize(serialized); + + /// + public object Deserialize(Type type, Stream serialized) => JsonSerializer.Deserialize(serialized, type); + } +} \ No newline at end of file