ps2_manager/PS2_Manager/App.axaml.cs
2025-04-22 03:24:56 +02:00

74 lines
No EOL
2.5 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices.JavaScript;
using System.Text.Json;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using PS2_Manager.Core;
using Tmds.DBus.Protocol;
namespace PS2_Manager;
public partial class App : Application
{
private void SetExceptionHandler()
{
AppDomain currentDomain = default(AppDomain);
currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
}
private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
Console.Error("Exception: " + ex.Message);
DateTime currentDateTime = DateTime.Now;
string formattedDateTime = currentDateTime.ToString("dddd, dd MMMM yyyy HH:mm:ss");
using (StreamWriter writer = new StreamWriter("error_log.txt", append: true))
{
writer.WriteLine(formattedDateTime);
writer.WriteLine(ex.ToString());
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
}
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
SetExceptionHandler();
Console.Info("App started at " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Globals.LoadSettings();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (String.IsNullOrEmpty(settings.library_path.GetValue<string>()))
{
var setupWindow = new Setup();
setupWindow.Closed += (_, _) =>
{
// Replace MainWindow after setup finishes
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var mainWindow = new MainWindow();
desktop.MainWindow = mainWindow;
mainWindow.Show();
}
};
desktop.MainWindow = setupWindow;
}
else
{
desktop.MainWindow = new MainWindow();
}
}
base.OnFrameworkInitializationCompleted();
}
}