Files
OpenNest/OpenNest.Avalonia/DesktopSettings.cs
T
ajandClaude Opus 5.5 8fe38398e9 feat(avalonia): load posts and machines at startup
The main window loads the post processors in Posts/ beside the app (their
materials registered) as it loads engine plug-ins, and reports any that
fail in the status line. It then reads the machines off the UI thread
through the shared MachineSettingsSource, following storage.json's mode
(the nest server's copy in Database mode), copies legacy clamps onto the
defaults' or only machine as WinForms does, and shows the offline-copy or
clamp warning in the status line. The machines and posts are kept in
DesktopSettings for later screens; nothing uses them yet.

The two posts the WinForms app ships now build with the Avalonia app and
copy to its Posts/ directory, with their settings JSON unless an edited
copy is already deployed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:18:14 -05:00

67 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenNest.Data;
using OpenNest.Desktop;
namespace OpenNest.Avalonia
{
/// <summary>
/// The machines, nest defaults and post processors this app works with, from the shared
/// desktop library: in Database mode (storage.json, shared with the WinForms app) the nest
/// server's copy once it holds one, otherwise this PC's files, with the user settings both
/// apps keep in user-settings.json.
/// </summary>
internal static class DesktopSettings
{
public static SharedSettingsSource Shared { get; } = SharedSettingsSource.FromStorageFile();
public static MachineSettingsSource Machines { get; } =
new(Shared, new MachineUserSettingsFile(UserSettingsFile.DefaultPath));
/// <summary>The post processors loaded from Posts/ beside the app, by load order.</summary>
public static IReadOnlyList<IPostProcessor> PostProcessors { get; private set; } = [];
/// <summary>The readable machine configurations, once <see cref="LoadMachinesAsync"/> has run.</summary>
public static IReadOnlyList<MachineConfig> MachineConfigs { get; private set; } = [];
/// <summary>
/// Loads the post processors in <paramref name="directory"/> (each DLL on its own, so one
/// that fails does not stop the others) and registers their materials. Returns the
/// failures to show, or null when every post loaded.
/// </summary>
public static string LoadPosts(string directory)
{
var result = PostProcessorLoader.Load(directory);
PostProcessors = result.PostProcessors;
return result.Failures.Count == 0
? null
: "These post processors could not be loaded: " + string.Join("; ", result.Failures);
}
/// <summary>
/// Reads the machines off the UI thread (the nest server can take seconds to answer), then
/// copies the legacy clamps onto the default or only machine, as the WinForms app does at
/// startup (with several machines and no default it waits for the WinForms app to ask).
/// Returns what to tell the user: the machines' source when the server could not be
/// reached, or why the clamps were not copied; null when there is nothing to say.
/// </summary>
public static Task<string> LoadMachinesAsync() => Task.Run(() =>
{
MachineConfigs = Machines.LoadMachines();
var clamps = Machines.MigrateLegacyClamps(askMachine: null);
var offline = Shared.TakeOfflineWarning();
var notices = new[] { offline, clamps }
.Where(text => !string.IsNullOrEmpty(text))
.Select(OneLine)
.ToList();
return notices.Count == 0 ? null : string.Join(" ", notices);
});
private static string OneLine(string text) =>
string.Join(" ", text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}
}