wabbajack/Wabbajack.App.Blazor/Pages/Configure.razor.cs

160 lines
5.8 KiB
C#
Raw Normal View History

2022-01-15 06:29:44 +00:00
using System;
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 Configure
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!;
[Inject] private JSRuntime JSRuntime { get; set; } = default!;
private ModList? Modlist => StateContainer.Modlist;
private AbsolutePath ModlistPath => StateContainer.ModlistPath;
private AbsolutePath InstallPath { get; set; }
2022-01-20 08:34:38 +00:00
private AbsolutePath DownloadPath { get; set; }
2022-01-21 13:41:37 +00:00
private string StatusText { get; set; } = string.Empty;
private InstallState InstallState => StateContainer.InstallState;
// private LoggerProvider.ILogMessage CurrentLog { get; set; }
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;
2022-01-20 08:34:38 +00:00
protected override async Task OnInitializedAsync()
2022-01-15 06:29:44 +00:00
{
2022-01-20 08:34:38 +00:00
// var Location = KnownFolders.EntryPoint.Combine("downloaded_mod_lists", machineURL).WithExtension(Ext.Wabbajack);
2022-01-21 13:41:37 +00:00
2022-01-20 08:34:38 +00:00
await CheckValidInstallPath();
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-20 08:34:38 +00:00
private async Task CheckValidInstallPath()
{
2022-01-21 13:41:37 +00:00
if (ModlistPath == AbsolutePath.Empty) return;
var modlist = await StandardInstaller.LoadFromFile(DTOs, ModlistPath);
StateContainer.Modlist = modlist;
2022-01-21 13:41:37 +00:00
var hex = (await ModlistPath.ToString().Hash()).ToHex();
var prevSettings = await SettingsManager.Load<SavedInstallSettings>(InstallSettingsPrefix + hex);
2022-01-21 13:41:37 +00:00
if (prevSettings.ModlistLocation == ModlistPath)
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
StateContainer.ModlistPath = prevSettings.ModlistLocation;
2022-01-20 08:34:38 +00:00
InstallPath = prevSettings.InstallLocation;
2022-01-21 13:41:37 +00:00
DownloadPath = prevSettings.DownloadLocation;
2022-01-20 08:34:38 +00:00
//ModlistMetadata = metadata ?? prevSettings.Metadata;
2022-01-15 06:29:44 +00:00
}
2022-01-21 13:41:37 +00:00
// see https://docs.microsoft.com/en-us/aspnet/core/blazor/images?view=aspnetcore-6.0#streaming-examples
var imageStream = await StandardInstaller.ModListImageStream(ModlistPath);
var dotnetImageStream = new DotNetStreamReference(imageStream);
// setImageUsingStreaming accepts the img id and the data stream
await JSRuntime.InvokeVoidAsync("setImageUsingStreaming", "background-image", dotnetImageStream);
2022-01-20 08:34:38 +00:00
}
private async void SelectInstallFolder()
{
try
{
2022-01-21 13:41:37 +00:00
var installPath = await Dialog.ShowDialogNonBlocking(true);
if (installPath is not null) InstallPath = (AbsolutePath)installPath;
}
2022-01-21 13:41:37 +00:00
catch (Exception e)
{
2022-01-21 13:41:37 +00:00
Logger.LogError(e, "Exception selecting install folder");
}
2022-01-20 08:34:38 +00:00
}
2022-01-20 08:34:38 +00:00
private async void SelectDownloadFolder()
{
try
{
2022-01-21 13:41:37 +00:00
var downloadPath = await Dialog.ShowDialogNonBlocking(true);
if (downloadPath is not null) DownloadPath = (AbsolutePath)downloadPath;
}
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 selecting download folder");
2022-01-20 08:34:38 +00:00
}
}
private async Task Install()
{
2022-01-21 13:41:37 +00:00
if (Modlist is null) return;
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-21 13:41:37 +00:00
2022-01-20 08:34:38 +00:00
installer.OnStatusUpdate = update =>
{
2022-01-21 13:41:37 +00:00
var (statusText, _, _) = update;
if (StatusText == statusText) return;
StatusText = statusText;
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
}
2022-01-20 08:34:38 +00:00
internal class SavedInstallSettings
{
2022-01-21 13:41:37 +00:00
public AbsolutePath ModlistLocation { get; set; } = AbsolutePath.Empty;
public AbsolutePath InstallLocation { get; set; } = AbsolutePath.Empty;
public AbsolutePath DownloadLocation { get; set; } = AbsolutePath.Empty;
// public ModlistMetadata Metadata { get; set; }
}