mirror of
https://github.com/ZuneDev/ZuneModdingHelper.git
synced 2026-07-27 13:12:21 -07:00
Add settings infrastructure
This commit is contained in:
+3
-1
@@ -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<Mod>? _mods;
|
||||
public static IReadOnlyList<Mod> AvailableMods
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ZuneModCore.Services
|
||||
{
|
||||
public interface IModCoreConfig
|
||||
{
|
||||
string ZuneInstallDir { get; }
|
||||
}
|
||||
}
|
||||
@@ -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<IModCoreConfig>(Settings.Default);
|
||||
services.AddSingleton<OwlCore.ComponentModel.SettingsBase>(Settings.Default);
|
||||
|
||||
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IAsyncSerializer{TSerialized}"/> and implementation for serializing and deserializing streams using System.Text.Json.
|
||||
/// </summary>
|
||||
public class SystemTextJsonStreamSerializer : IAsyncSerializer<Stream>, ISerializer<Stream>
|
||||
{
|
||||
/// <summary>
|
||||
/// A singleton instance for <see cref="SystemTextJsonStreamSerializer"/>.
|
||||
/// </summary>
|
||||
public static SystemTextJsonStreamSerializer Singleton { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Stream> SerializeAsync<T>(T data, CancellationToken? cancellationToken = null)
|
||||
{
|
||||
MemoryStream stream = new();
|
||||
await JsonSerializer.SerializeAsync(stream, data, cancellationToken: cancellationToken.GetValueOrDefault());
|
||||
return stream;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Stream> SerializeAsync(Type inputType, object data, CancellationToken? cancellationToken = null)
|
||||
{
|
||||
MemoryStream stream = new();
|
||||
await JsonSerializer.SerializeAsync(stream, data, inputType, cancellationToken: cancellationToken.GetValueOrDefault());
|
||||
return stream;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<TResult> DeserializeAsync<TResult>(Stream serialized, CancellationToken? cancellationToken = null)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<TResult>(serialized, cancellationToken: cancellationToken.GetValueOrDefault())
|
||||
?? default;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<object> DeserializeAsync(Type returnType, Stream serialized, CancellationToken? cancellationToken = null)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync(serialized, returnType, cancellationToken: cancellationToken.GetValueOrDefault())
|
||||
?? default;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Stream Serialize<T>(T data) => Serialize(typeof(T), data);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Stream Serialize(Type type, object data)
|
||||
{
|
||||
MemoryStream stream = new();
|
||||
JsonSerializer.Serialize(stream, data, type);
|
||||
return stream;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TResult Deserialize<TResult>(Stream serialized) => JsonSerializer.Deserialize<TResult>(serialized);
|
||||
|
||||
/// <inheritdoc />
|
||||
public object Deserialize(Type type, Stream serialized) => JsonSerializer.Deserialize(serialized, type);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user