Files
StarterNG/StarterNG/Infrastructure/Diagnostics.cs
maj00r 07987a60a9 Write settings, presets and favourites whole or not at all
A failed write leaves the old file in place. Settings that could not be read
are not saved over. UI exceptions reach the log. Started from a shortcut set
up elsewhere, the starter moves to its own folder when only that one holds a
game.
2026-11-01 16:06:15 +01:00

85 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
using StarterNG.Application;
using StarterNG.Views;
namespace StarterNG.Infrastructure;
public static class Diagnostics
{
private static Application.Abstractions.IDiagnosticsLog Log_ => AppServices.Current.Log;
public static string LogPath => AppServices.Current.Paths.DiagnosticsLog;
public static string Text => Log_.Text;
public static void Clear() => Log_.Clear();
public static void Log(string message) => Log_.Log(message);
public static void Log(IEnumerable<string> messages) => Log_.Log(messages);
public static void Log(string context, Exception ex) => Log_.Log(context, ex);
public static void Flush() => Log_.Flush();
private static Window? Owner =>
(Avalonia.Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)
?.MainWindow;
public static Task ReportAsync(string message, string? title = null)
{
Log_.Log(message.Replace(Environment.NewLine, " | "));
var owner = Owner;
if (owner is null)
return Task.CompletedTask;
return MessageBox.Show(owner, message, title ?? App.Loc["FaultTitle"], MessageBoxButtons.Ok);
}
public static Task ReportAsync(string context, Exception ex, string? title = null) =>
ReportAsync($"{context}{Environment.NewLine}{Environment.NewLine}{App.Loc["FaultDetail"]} {ex.Message}", title);
public static void ReportOnUiThread(string message, string? title = null)
{
if (Dispatcher.UIThread.CheckAccess())
_ = ReportAsync(message, title);
else
Dispatcher.UIThread.Post(() => _ = ReportAsync(message, title));
}
/// <summary>Opens a file or folder the way the system does; a dialog says if it is not there or will not open.</summary>
public static void OpenOrReport(string path)
{
if (!File.Exists(path) && !Directory.Exists(path))
{
ReportOnUiThread(string.Format(App.Loc["FaultFileNotFound"], path));
return;
}
try
{
Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
}
catch (Exception ex)
{
_ = ReportAsync(path, ex);
}
}
public static InstallationCheck InstallationCheck() =>
new(AppServices.Current.Paths,
AppServices.Current.Files,
AppServices.Current.Localization,
AppServices.Current.Physics,
AppServices.Current.Library,
AppServices.Current.SettingsStore);
}