99 lines
3.4 KiB
C#
Raw Normal View History

using System.Diagnostics;
using System.Linq;
2023-11-12 09:52:02 -05:00
using Avalonia;
2023-05-11 23:11:39 -04:00
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
2023-09-05 20:09:11 -04:00
using ReactiveUI;
using Serilog;
2023-05-11 23:11:39 -04:00
using SPTInstaller.ViewModels;
using SPTInstaller.Views;
2023-09-05 20:09:11 -04:00
using System.Reactive;
using System.Text;
2024-07-10 13:33:19 -04:00
using SPTInstaller.Helpers;
using SPTInstaller.Models;
2023-05-11 23:11:39 -04:00
namespace SPTInstaller;
public partial class App : Application
2023-05-11 23:11:39 -04:00
{
public static string LogPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "spt-installer", "spt-installer.log");
2024-07-10 13:33:19 -04:00
public static string LogDebugPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"spt-installer", "spt-isntaller-debug.log");
2024-05-01 10:31:55 -04:00
public static void ReLaunch(bool debug, string installPath = "")
{
var installerPath = Path.Join(Environment.CurrentDirectory, "SPTInstaller.exe");
var args = new StringBuilder()
.Append(debug ? "debug " : "")
.Append(!string.IsNullOrEmpty(installPath) ? $"installPath=\"{installPath}\"" : "")
.ToString();
Process.Start(new ProcessStartInfo()
{
FileName = installerPath,
Arguments = args
});
Environment.Exit(0);
}
public override void Initialize()
2023-05-11 23:11:39 -04:00
{
AvaloniaXamlLoader.Load(this);
2024-02-06 18:59:39 -05:00
2023-09-05 20:23:28 -04:00
Log.Logger = new LoggerConfiguration()
2024-02-06 18:59:39 -05:00
.MinimumLevel.Information()
2023-09-05 20:23:28 -04:00
.WriteTo
2024-06-09 16:14:57 -04:00
.File(path: LogPath,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
2023-09-05 20:23:28 -04:00
.CreateLogger();
2024-05-01 10:31:55 -04:00
2023-09-05 20:09:11 -04:00
RxApp.DefaultExceptionHandler = Observer.Create<Exception>((exception) =>
{
Log.Error(exception, "An application exception occurred");
});
}
2024-05-01 10:31:55 -04:00
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
2023-05-11 23:11:39 -04:00
{
2024-07-10 13:33:19 -04:00
var data = ServiceHelper.Get<InternalData>() ?? throw new Exception("failed to get internal data");
data.DebugMode = false;
var providedPath = "";
if (desktop.Args != null)
{
2024-07-10 13:33:19 -04:00
data.DebugMode = desktop.Args.Any(x => x.ToLower() == "debug");
var installPath = desktop.Args.FirstOrDefault(x => x.StartsWith("installPath=", StringComparison.CurrentCultureIgnoreCase));
providedPath = installPath != null && installPath.Contains('=') ? installPath?.Split('=')[1] ?? "" : "";
}
2024-07-10 13:33:19 -04:00
if (data.DebugMode)
2023-11-12 09:52:02 -05:00
{
2024-07-10 13:33:19 -04:00
Log.CloseAndFlush();
2024-02-06 18:59:39 -05:00
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo
2024-07-10 13:33:19 -04:00
.File(path: LogDebugPath,
2024-06-09 16:14:57 -04:00
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug)
2024-02-06 18:59:39 -05:00
.CreateLogger();
2024-07-10 13:33:19 -04:00
Trace.Listeners.Add(new SerilogTraceListener.SerilogTraceListener());
2024-02-06 18:59:39 -05:00
2023-11-12 09:52:02 -05:00
Log.Debug("TraceListener is registered");
}
desktop.MainWindow = new MainWindow
2023-05-11 23:11:39 -04:00
{
2024-07-10 13:33:19 -04:00
DataContext = new MainWindowViewModel(providedPath),
};
2023-05-11 23:11:39 -04:00
}
2024-05-01 10:31:55 -04:00
base.OnFrameworkInitializationCompleted();
2023-05-11 23:11:39 -04:00
}
}