215 lines
6.6 KiB
C#
Raw Normal View History

2024-07-04 12:25:19 -04:00
using System.Diagnostics;
using Avalonia;
2023-05-11 23:11:39 -04:00
using ReactiveUI;
2023-05-14 22:35:06 -04:00
using Serilog;
2023-08-25 23:46:11 -04:00
using SPTInstaller.CustomControls;
using SPTInstaller.Helpers;
using SPTInstaller.Interfaces;
2023-08-25 23:46:11 -04:00
using System.Threading.Tasks;
2023-05-11 23:11:39 -04:00
using System.Windows.Input;
2024-06-09 16:14:57 -04:00
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Platform.Storage;
2024-07-04 12:25:19 -04:00
using SPTInstaller.Models;
2023-05-11 23:11:39 -04:00
namespace SPTInstaller.ViewModels;
public class MessageViewModel : ViewModelBase
2023-05-11 23:11:39 -04:00
{
private bool _HasErrors;
2024-05-01 10:31:55 -04:00
public bool HasErrors
2023-05-11 23:11:39 -04:00
{
get => _HasErrors;
set => this.RaiseAndSetIfChanged(ref _HasErrors, value);
}
2024-05-01 10:31:55 -04:00
private string _Message;
2024-05-01 10:31:55 -04:00
public string Message
{
get => _Message;
set => this.RaiseAndSetIfChanged(ref _Message, value);
}
2024-05-01 10:31:55 -04:00
2023-07-30 16:15:52 -04:00
private bool _showCloseButton;
2024-05-01 10:31:55 -04:00
2023-07-30 16:15:52 -04:00
public bool ShowCloseButton
{
get => _showCloseButton;
set => this.RaiseAndSetIfChanged(ref _showCloseButton, value);
}
2024-05-01 10:31:55 -04:00
2024-07-04 12:25:19 -04:00
private bool _showOptions;
public bool ShowOptions
{
get => _showOptions;
set => this.RaiseAndSetIfChanged(ref _showOptions, value);
}
2023-08-25 23:46:11 -04:00
private string _cacheInfoText;
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
public string CacheInfoText
{
get => _cacheInfoText;
set => this.RaiseAndSetIfChanged(ref _cacheInfoText, value);
}
2024-05-01 10:31:55 -04:00
2024-06-09 16:14:57 -04:00
private string _clipCommandText;
public string ClipCommandText
{
get => _clipCommandText;
set => this.RaiseAndSetIfChanged(ref _clipCommandText, value);
}
2024-07-04 12:25:19 -04:00
private bool _addShortcuts;
public bool AddShortcuts
{
get => _addShortcuts;
set => this.RaiseAndSetIfChanged(ref _addShortcuts, value);
}
private bool _openInstallFolder = true;
public bool OpenInstallFolder
{
get => _openInstallFolder;
set => this.RaiseAndSetIfChanged(ref _openInstallFolder, value);
}
2024-06-09 16:14:57 -04:00
public ICommand CopyLogFileToClipboard => ReactiveCommand.CreateFromTask(async () =>
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
try
{
if (desktop.MainWindow?.Clipboard == null)
{
ClipCommandText = "Could not get clipboard :(";
return;
}
var dataObject = new DataObject();
var logFile = await desktop.MainWindow.StorageProvider.TryGetFileFromPathAsync(App.LogPath);
if (logFile == null)
{
ClipCommandText = "Could not get log file :(";
return;
}
dataObject.Set(DataFormats.Files, new[] {logFile});
await desktop.MainWindow.Clipboard.SetDataObjectAsync(dataObject);
ClipCommandText = "Copied!";
}
catch (Exception ex)
{
ClipCommandText = ex.Message;
}
}
});
2023-08-25 23:46:11 -04:00
private StatusSpinner.SpinnerState _cacheCheckState;
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
public StatusSpinner.SpinnerState CacheCheckState
{
get => _cacheCheckState;
set => this.RaiseAndSetIfChanged(ref _cacheCheckState, value);
}
2024-05-01 10:31:55 -04:00
public ICommand CloseCommand { get; set; } = ReactiveCommand.Create(() =>
{
2024-07-04 12:25:19 -04:00
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopApp)
2023-05-11 23:11:39 -04:00
{
desktopApp.MainWindow.Close();
2023-05-11 23:11:39 -04:00
}
});
2024-05-01 10:31:55 -04:00
public MessageViewModel(IScreen Host, IResult result, bool showCloseButton = true, bool noLog = false) : base(Host)
{
2023-07-30 16:15:52 -04:00
ShowCloseButton = showCloseButton;
Message = result.Message;
2024-06-09 16:14:57 -04:00
ClipCommandText = "Copy installer log to clipboard";
2024-05-01 10:31:55 -04:00
2024-07-04 12:25:19 -04:00
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopApp)
{
var data = ServiceHelper.Get<InternalData?>();
desktopApp.MainWindow.Closing += (_, _) =>
{
if (ShowOptions)
{
if (OpenInstallFolder)
{
Process.Start(new ProcessStartInfo()
{
FileName = "explorer.exe",
Arguments = data.TargetInstallPath
});
}
if (AddShortcuts)
{
var shortcuts = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "add_shortcuts.ps1"));
if (!FileHelper.StreamAssemblyResourceOut("add_shortcuts.ps1", shortcuts.FullName))
{
Log.Fatal("Failed to prepare shortcuts file");
return;
}
if (!File.Exists(shortcuts.FullName))
{
Log.Fatal("Shortcuts file not found");
return;
}
Log.Information("Running add shortcuts script ...");
Process.Start(new ProcessStartInfo
{
FileName = "powershell.exe",
CreateNoWindow = true,
ArgumentList =
{
"-ExecutionPolicy", "Bypass", "-File", $"{shortcuts.FullName}", $"{data.TargetInstallPath}"
}
});
}
}
try
{
File.Copy(App.LogPath, Path.Join(data.TargetInstallPath, "spt-installer.log"), true);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to copy installer log to install path");
}
};
}
2023-08-25 23:46:11 -04:00
Task.Run(() =>
{
CacheInfoText = "Getting cache size ...";
CacheCheckState = StatusSpinner.SpinnerState.Running;
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
CacheInfoText = $"Cache Size: {DownloadCacheHelper.GetCacheSizeText()}";
CacheCheckState = StatusSpinner.SpinnerState.OK;
});
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
if (result.Succeeded)
2023-05-11 23:11:39 -04:00
{
Log.Information(Message);
2024-07-04 12:25:19 -04:00
ShowOptions = true;
return;
2023-05-11 23:11:39 -04:00
}
2024-05-01 10:31:55 -04:00
HasErrors = true;
2024-05-01 10:31:55 -04:00
if (!noLog)
Log.Error(Message);
2023-05-11 23:11:39 -04:00
}
}