[Blazor] Implement importing DAT folder.

This commit is contained in:
2025-07-27 03:30:51 +01:00
parent 122e397d0a
commit cf7186adbb
6 changed files with 143 additions and 2 deletions

View File

@@ -0,0 +1,16 @@
@implements IDialogContentComponent
@inject ILogger<ImportDats> Logger
<FluentDialog Width="800px" Height="400px" Title="Import DATs" Modal="true" TrapFocus="true">
<FluentDialogBody>
<p hidden="@IsBusy">DAT files will be imported from @path.</p>
<FluentLabel Color="@StatusColor">@StatusMessage</FluentLabel>
<FluentProgress Max="@ProgressMax" Min="@ProgressMin" Value="@ProgressValue" Visible="@ProgressVisible"/>
</FluentDialogBody>
<FluentDialogFooter>
<FluentStack Orientation="Orientation.Horizontal">
<FluentButton OnClick="@StartAsync" Disabled="@IsBusy">Start</FluentButton>
<FluentButton OnClick="@CloseAsync" Disabled="@CannotClose">Close</FluentButton>
</FluentStack>
</FluentDialogFooter>
</FluentDialog>

View File

@@ -0,0 +1,108 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using RomRepoMgr.Core.Workers;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.Blazor.Components.Dialogs;
public partial class ImportDats : ComponentBase
{
readonly Stopwatch _stopwatch = new();
string[] _datFiles;
int _listPosition;
int _workers;
string path;
public string StatusMessage { get; set; }
public bool IsBusy { get; set; }
[CascadingParameter]
public FluentDialog Dialog { get; set; }
public int? ProgressMax { get; set; }
public int? ProgressMin { get; set; }
public int? ProgressValue { get; set; }
public bool CannotClose { get; set; }
public bool ProgressVisible { get; set; }
public Color? StatusColor { get; set; }
/// <inheritdoc />
protected override void OnInitialized()
{
base.OnInitialized();
path = Path.Combine(Environment.CurrentDirectory, Consts.IncomingDatFolder);
StatusMessage = string.Empty;
IsBusy = false;
CannotClose = false;
ProgressVisible = false;
}
Task StartAsync()
{
IsBusy = true;
CannotClose = true;
ProgressVisible = true;
ProgressValue = null;
StatusMessage = "Searching for files...";
_stopwatch.Restart();
string[] dats = Directory.GetFiles(path, "*.dat", SearchOption.AllDirectories);
string[] xmls = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
_datFiles = dats.Concat(xmls).Order().ToArray();
_stopwatch.Stop();
Logger.LogDebug("Took {TotalSeconds} to find {Length} DAT files",
_stopwatch.Elapsed.TotalSeconds,
_datFiles.Length);
StatusMessage = string.Format("Found {0} files...", _datFiles.Length);
ProgressMin = 0;
ProgressMax = _datFiles.Length;
ProgressValue = 0;
_listPosition = 0;
_workers = 0;
StateHasChanged();
return ImportAsync();
}
async Task ImportAsync()
{
_stopwatch.Restart();
Logger.LogDebug("Starting to import DAT files...");
Parallel.ForEach(_datFiles,
datFile =>
{
_ = InvokeAsync(() =>
{
StatusMessage = string.Format("Importing {0}...", Path.GetFileName(datFile));
ProgressValue = _listPosition;
StateHasChanged();
});
var worker = new DatImporter(datFile, null, new SerilogLoggerFactory(Log.Logger));
worker.Import();
Interlocked.Increment(ref _listPosition);
});
ProgressVisible = false;
StatusMessage = "Finished";
CannotClose = false;
_stopwatch.Stop();
Logger.LogDebug("Took {TotalSeconds} seconds to import {Length} DAT files",
_stopwatch.Elapsed.TotalSeconds,
_datFiles.Length);
StateHasChanged();
}
Task CloseAsync() => Dialog.CloseAsync();
}

View File

@@ -16,6 +16,7 @@
<FluentSpacer/>
<a href="https://learn.microsoft.com/en-us/aspnet/core/blazor" target="_blank">About Blazor</a>
</FluentFooter>
<FluentDialogProvider/>
</FluentLayout>
<div data-nosnippet id="blazor-error-ui">

View File

@@ -1,9 +1,11 @@
@page "/"
@rendermode InteractiveServer
@inject IDialogService DialogService
<PageTitle>ROM Repository Manager</PageTitle>
<FluentToolbar>
<FluentButton Disabled="true">Import DATs</FluentButton>
<FluentButton OnClick="@ImportDatsAsync">Import DATs</FluentButton>
<FluentButton Disabled="true">Export DAT</FluentButton>
<FluentButton Disabled="true">Remove DAT</FluentButton>
<FluentButton Disabled="true">Import ROMs</FluentButton>

View File

@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using RomRepoMgr.Blazor.Components.Dialogs;
namespace RomRepoMgr.Blazor.Components.Pages;
public partial class Home : ComponentBase
{
async Task ImportDatsAsync()
{
IDialogReference dialog = await DialogService.ShowDialogAsync<ImportDats>(new DialogParameters());
}
}

View File

@@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RomRepoMgr.Core\RomRepoMgr.Core.csproj"/>
<ProjectReference Include="..\RomRepoMgr.Database\RomRepoMgr.Database.csproj"/>
</ItemGroup>
</Project>