Files
StarterNG/StarterNG/Program.cs
maj00r 6e2dbf9eac test: the system graphics libraries and the software render switch
Taking the libraries and the switch's options are apart from Main, so they are
tested on every system, not only where Windows has them to load.
2026-11-15 17:30:13 +01:00

87 lines
2.6 KiB
C#

using Avalonia;
using Avalonia.Threading;
using System;
using System.Threading.Tasks;
using StarterNG.Application;
using StarterNG.Infrastructure;
namespace StarterNG;
static class Program
{
private static bool _softwareRender;
[STAThread]
public static void Main(string[] args)
{
// Ahead of everything that could draw: what lies beside the starter is the simulator's.
var graphics = SystemGraphicsLibraries.TakeOnWindows();
graphics?.ServeTo(typeof(Win32PlatformOptions).Assembly);
_softwareRender = SoftwareRender.AskedIn(args);
// Composition root first: everything below, including the crash handlers,
// resolves its collaborators from this graph.
AppServices.Initialize();
if (graphics?.Note is { } note)
Diagnostics.Log(note);
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
if (e.ExceptionObject is Exception ex)
Diagnostics.Log("Unhandled exception", ex);
Diagnostics.Flush();
};
TaskScheduler.UnobservedTaskException += (_, e) =>
{
Diagnostics.Log("Unobserved task exception", e.Exception);
e.SetObserved();
};
try
{
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
Diagnostics.Log("Fatal", ex);
throw;
}
finally
{
Diagnostics.Flush();
}
}
private static AppBuilder WithDiagnosticsLogging(this AppBuilder builder)
{
DiagnosticsLogSink.Install();
return builder;
}
// After setup: the dispatcher takes its platform's implementation when first asked
// for. Not marked handled: a handler that failed half way leaves the state unknown,
// and the exception goes on to end the program as before, only no longer unexplained.
private static void LogUiExceptions()
{
Dispatcher.UIThread.UnhandledException += (_, e) =>
{
Diagnostics.Log("Unhandled UI exception", e.Exception);
Diagnostics.Flush();
};
}
private static AppBuilder WithSoftwareRenderIfAsked(this AppBuilder builder) =>
_softwareRender ? SoftwareRender.ApplyTo(builder) : builder;
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithSoftwareRenderIfAsked()
.WithInterFont()
.WithDiagnosticsLogging()
.AfterSetup(_ => LogUiExceptions());
}