diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor new file mode 100644 index 0000000..6a912b3 --- /dev/null +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor @@ -0,0 +1,16 @@ +@implements IDialogContentComponent +@inject ILogger Logger + + + + + @StatusMessage + + + + + Start + Close + + + \ No newline at end of file diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs new file mode 100644 index 0000000..b6ab5e3 --- /dev/null +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs @@ -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; } + + /// + 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(); +} \ No newline at end of file diff --git a/RomRepoMgr.Blazor/Components/Layout/MainLayout.razor b/RomRepoMgr.Blazor/Components/Layout/MainLayout.razor index 06c4d6f..9b8c9de 100644 --- a/RomRepoMgr.Blazor/Components/Layout/MainLayout.razor +++ b/RomRepoMgr.Blazor/Components/Layout/MainLayout.razor @@ -16,10 +16,11 @@ About Blazor +
An unhandled error has occurred. Reload 🗙 -
\ No newline at end of file + diff --git a/RomRepoMgr.Blazor/Components/Pages/Home.razor b/RomRepoMgr.Blazor/Components/Pages/Home.razor index 6f0e1f0..e1a94d0 100644 --- a/RomRepoMgr.Blazor/Components/Pages/Home.razor +++ b/RomRepoMgr.Blazor/Components/Pages/Home.razor @@ -1,9 +1,11 @@ @page "/" +@rendermode InteractiveServer +@inject IDialogService DialogService ROM Repository Manager - Import DATs + Import DATs Export DAT Remove DAT Import ROMs diff --git a/RomRepoMgr.Blazor/Components/Pages/Home.razor.cs b/RomRepoMgr.Blazor/Components/Pages/Home.razor.cs new file mode 100644 index 0000000..88d600b --- /dev/null +++ b/RomRepoMgr.Blazor/Components/Pages/Home.razor.cs @@ -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(new DialogParameters()); + } +} \ No newline at end of file diff --git a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj index 2535e02..32ee31d 100644 --- a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj +++ b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj @@ -16,6 +16,7 @@ +