wabbajack/Wabbajack.App.Blazor/Pages/Install/Installing.razor.cs

106 lines
3.6 KiB
C#
Raw Normal View History

2022-01-15 06:29:44 +00:00
using System;
using System.Collections.Generic;
using System.Threading;
2022-01-15 06:29:44 +00:00
using Microsoft.AspNetCore.Components;
using Wabbajack.DTOs;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Installer;
using Wabbajack.Paths;
using Wabbajack.App.Blazor.Utility;
using Wabbajack.Downloaders.GameFile;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Services.OSIntegrated;
using System.Threading.Tasks;
2022-01-21 13:41:37 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
2022-01-20 08:34:38 +00:00
using Wabbajack.App.Blazor.State;
2022-01-15 06:29:44 +00:00
2022-01-20 08:34:38 +00:00
namespace Wabbajack.App.Blazor.Pages;
public partial class Installing
2022-01-15 06:29:44 +00:00
{
2022-01-21 13:41:37 +00:00
[Inject] private ILogger<Configure> Logger { get; set; } = default!;
[Inject] private IStateContainer StateContainer { get; set; } = default!;
[Inject] private DTOSerializer DTOs { get; set; } = default!;
[Inject] private IServiceProvider ServiceProvider { get; set; } = default!;
[Inject] private SystemParametersConstructor ParametersConstructor { get; set; } = default!;
[Inject] private IGameLocator GameLocator { get; set; } = default!;
[Inject] private SettingsManager SettingsManager { get; set; } = default!;
2022-01-21 14:44:05 +00:00
[Inject] private IJSRuntime JSRuntime { get; set; } = default!;
2022-01-21 13:41:37 +00:00
private ModList? Modlist => StateContainer.Modlist;
private string ModlistImage => StateContainer.ModlistImage;
2022-01-21 13:41:37 +00:00
private AbsolutePath ModlistPath => StateContainer.ModlistPath;
private AbsolutePath InstallPath => StateContainer.InstallPath;
private AbsolutePath DownloadPath => StateContainer.DownloadPath;
public string StatusCategory { get; set; }
private string LastStatus { get; set; }
public List<string> StatusStep { get; set; } = new();
2022-01-20 08:34:38 +00:00
2022-01-21 13:41:37 +00:00
private InstallState InstallState => StateContainer.InstallState;
2022-01-20 08:34:38 +00:00
private const string InstallSettingsPrefix = "install-settings-";
2022-01-21 13:41:37 +00:00
private bool _shouldRender;
protected override bool ShouldRender() => _shouldRender;
protected override void OnInitialized()
2022-01-15 06:29:44 +00:00
{
Install();
2022-01-21 13:41:37 +00:00
_shouldRender = true;
2022-01-20 08:34:38 +00:00
}
2022-01-15 06:29:44 +00:00
2022-01-28 05:05:30 +00:00
private async Task Install()
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
if (Modlist is null) return;
2022-01-21 13:41:37 +00:00
StateContainer.InstallState = InstallState.Installing;
await Task.Run(() => BeginInstall(Modlist));
2022-01-20 08:34:38 +00:00
}
2022-01-21 13:41:37 +00:00
private async Task BeginInstall(ModList modlist)
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
var postfix = (await ModlistPath.ToString().Hash()).ToHex();
await SettingsManager.Save(InstallSettingsPrefix + postfix, new SavedInstallSettings
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
ModlistLocation = ModlistPath,
InstallLocation = InstallPath,
DownloadLocation = DownloadPath
2022-01-20 08:34:38 +00:00
});
try
{
2022-01-21 13:41:37 +00:00
var installer = StandardInstaller.Create(ServiceProvider, new InstallerConfiguration
{
2022-01-21 13:41:37 +00:00
Game = modlist.GameType,
Downloads = DownloadPath,
Install = InstallPath,
ModList = modlist,
ModlistArchive = ModlistPath,
SystemParameters = ParametersConstructor.Create(),
GameFolder = GameLocator.GameLocation(modlist.GameType)
});
2022-01-20 08:34:38 +00:00
installer.OnStatusUpdate = update =>
{
if (LastStatus == update.StatusText) return;
StatusStep.Insert(0, update.StatusText);
StatusCategory = update.StatusCategory;
LastStatus = update.StatusText;
InvokeAsync(StateHasChanged);
2022-01-20 08:34:38 +00:00
};
2022-01-20 08:34:38 +00:00
await installer.Begin(CancellationToken.None);
2022-01-21 13:41:37 +00:00
StateContainer.InstallState = InstallState.Success;
2022-01-20 08:34:38 +00:00
}
2022-01-21 13:41:37 +00:00
catch (Exception e)
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
Logger.LogError(e, "Exception installing Modlist");
StateContainer.InstallState = InstallState.Failure;
}
2022-01-15 06:29:44 +00:00
}
2022-01-20 08:34:38 +00:00
}