1
0
Fork 0
mirror of https://github.com/oMaN-Rod/nxDumpFuse.git synced 2024-11-22 18:26:40 +00:00

Add start/stop logic to avoid multiple clicks when starting fuse

This commit is contained in:
Omar 2021-11-02 13:01:11 -04:00
parent c34cf7670a
commit 21720adfae
3 changed files with 74 additions and 55 deletions

View file

@ -15,10 +15,7 @@ namespace nxDumpFuse.Services
{ {
private CancellationTokenSource? _cts; private CancellationTokenSource? _cts;
private string? _inputFilePath;
private string? _outputDir;
private string? _outputFilePath; private string? _outputFilePath;
private FileCase _fileCase;
private readonly Stopwatch _sw = new(); private readonly Stopwatch _sw = new();
public event EventHandlers.FuseUpdateEventHandler? FuseUpdateEvent; public event EventHandlers.FuseUpdateEventHandler? FuseUpdateEvent;
@ -54,29 +51,28 @@ namespace nxDumpFuse.Services
public void Start(string inputFilePath, string outputDir) public void Start(string inputFilePath, string outputDir)
{ {
_inputFilePath = inputFilePath;
_outputDir = outputDir;
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
if (string.IsNullOrEmpty(_inputFilePath)) if (string.IsNullOrEmpty(inputFilePath))
{ {
Log(FuseSimpleLogType.Error, "Input File cannot be empty"); Log(FuseSimpleLogType.Error, "Input File cannot be empty");
return; return;
} }
if (string.IsNullOrEmpty(_outputDir)) if (string.IsNullOrEmpty(outputDir))
{ {
Log(FuseSimpleLogType.Error, "Output Directory cannot be empty"); Log(FuseSimpleLogType.Error, "Output Directory cannot be empty");
return; return;
} }
(_outputFilePath, _fileCase) = _inputFilePath.GetOutputFilePath(_outputDir); FileCase fileCase;
if (string.IsNullOrEmpty(_outputFilePath) || _fileCase == FileCase.Invalid) (_outputFilePath, fileCase) = inputFilePath.GetOutputFilePath(outputDir);
if (string.IsNullOrEmpty(_outputFilePath) || fileCase == FileCase.Invalid)
{ {
Log(FuseSimpleLogType.Error, "Output path was null"); Log(FuseSimpleLogType.Error, "Output path was null");
return; return;
} }
var inputFiles = _inputFilePath.GetInputFiles(_fileCase); var inputFiles = inputFilePath.GetInputFiles(fileCase);
if (inputFiles.Count == 0) if (inputFiles.Count == 0)
{ {
Log(FuseSimpleLogType.Error, "No input files found"); Log(FuseSimpleLogType.Error, "No input files found");
@ -88,12 +84,12 @@ namespace nxDumpFuse.Services
public void Stop() public void Stop()
{ {
_cts.Cancel(); _cts?.Cancel();
_sw.Stop(); _sw.Stop();
Log(FuseSimpleLogType.Information, "Fuse Stopped"); Log(FuseSimpleLogType.Information, "Fuse Stopped");
if (File.Exists(_outputFilePath)) if (!string.IsNullOrEmpty(_outputFilePath) && File.Exists(_outputFilePath))
{ {
Task.Run((() => Task.Run((() =>
{ {
@ -126,10 +122,10 @@ namespace nxDumpFuse.Services
Log(FuseSimpleLogType.Information, $"Fusing {inputFiles.Count} parts to {_outputFilePath} ({totalFileLength.ToMb()}MB)"); Log(FuseSimpleLogType.Information, $"Fusing {inputFiles.Count} parts to {_outputFilePath} ({totalFileLength.ToMb()}MB)");
_sw.Start(); _sw.Start();
await using var outputStream = File.Create(_outputFilePath); await using var outputStream = File.Create(_outputFilePath!);
foreach (var inputFilePath in inputFiles) foreach (var inputFilePath in inputFiles)
{ {
if (_cts.Token.IsCancellationRequested) return; if (_cts!.Token.IsCancellationRequested) return;
long currentBytes = 0; long currentBytes = 0;
int currentBlockSize; int currentBlockSize;
long copySpeed = 0; long copySpeed = 0;

View file

@ -23,12 +23,24 @@ namespace nxDumpFuse.ViewModels
{ {
_dialogService = dialogService; _dialogService = dialogService;
_fuseService = fuseServiceService; _fuseService = fuseServiceService;
_fuseService.FuseUpdateEvent += OnFuseServiceUpdate;
_fuseService.FuseSimpleLogEvent += OnFuseServiceSimpleLogEvent;
SelectInputFileCommand = ReactiveCommand.Create(SelectInputFile); SelectInputFileCommand = ReactiveCommand.Create(SelectInputFile);
SelectOutputFolderCommand = ReactiveCommand.Create(SelectOutputFolder); SelectOutputFolderCommand = ReactiveCommand.Create(SelectOutputFolder);
FuseCommand = ReactiveCommand.Create(FuseNxDump);
StopCommand = ReactiveCommand.Create(StopDump);
ClearLogCommand = ReactiveCommand.Create(ClearLog); ClearLogCommand = ReactiveCommand.Create(ClearLog);
var canFuse = this.WhenAnyValue(vm => vm.InputFilePath, vm => vm.OutputDir, vm => vm.IsBusy,
(input, output, isBusy) => !string.IsNullOrWhiteSpace(input) &&
!string.IsNullOrWhiteSpace(output) &&
!isBusy);
var canStop = this.WhenAnyValue(vm => vm.IsBusy);
StartCommand = ReactiveCommand.Create(StartFuse, canFuse);
StopCommand = ReactiveCommand.Create(StopFuse, canStop);
ProgressPartText = "Part 0/0"; ProgressPartText = "Part 0/0";
} }
@ -38,10 +50,17 @@ namespace nxDumpFuse.ViewModels
public ReactiveCommand<Unit, Unit> ClearLogCommand { get; } public ReactiveCommand<Unit, Unit> ClearLogCommand { get; }
public ReactiveCommand<Unit, Unit> FuseCommand { get; } public ReactiveCommand<Unit, Unit> StartCommand { get; }
public ReactiveCommand<Unit, Unit> StopCommand { get; } public ReactiveCommand<Unit, Unit> StopCommand { get; }
private bool _isBusy;
public bool IsBusy
{
get => _isBusy;
set => this.RaiseAndSetIfChanged(ref _isBusy, value);
}
private string _inputFilePath = string.Empty; private string _inputFilePath = string.Empty;
public string InputFilePath public string InputFilePath
{ {
@ -88,39 +107,7 @@ namespace nxDumpFuse.ViewModels
public ObservableCollection<FuseSimpleLog> LogItems public ObservableCollection<FuseSimpleLog> LogItems
{ {
get => _logItems; get => _logItems;
set => this.RaiseAndSetIfChanged( ref _logItems, value); set => this.RaiseAndSetIfChanged(ref _logItems, value);
}
private async void SelectInputFile()
{
InputFilePath = await _dialogService.ShowOpenFileDialogAsync("Choose Input File", new FileDialogFilter { Name = string.Empty, Extensions = new List<string>() });
}
private async void SelectOutputFolder()
{
OutputDir = await _dialogService.ShowOpenFolderDialogAsync("Choose Output Folder");
}
private void FuseNxDump()
{
_fuseService.FuseUpdateEvent += OnFuseServiceUpdate;
_fuseService.FuseSimpleLogEvent += OnFuseServiceSimpleLogEvent;
_sw.Start();
try
{
_fuseService.Start(InputFilePath, OutputDir);
}
catch (Exception e) {
_sw.Stop();
OnFuseServiceSimpleLogEvent(new FuseSimpleLog(FuseSimpleLogType.Error, DateTime.Now, e.Message));
}
}
private void StopDump()
{
_sw.Stop();
_fuseService?.Stop();
ProgressText = string.Empty;
} }
private void ClearLog() private void ClearLog()
@ -128,14 +115,21 @@ namespace nxDumpFuse.ViewModels
LogItems.Clear(); LogItems.Clear();
} }
private void OnFuseServiceSimpleLogEvent(FuseSimpleLog log)
{
LogItems.Add(log);
}
private void OnFuseServiceUpdate(FuseUpdateInfo fuseUpdateInfo) private void OnFuseServiceUpdate(FuseUpdateInfo fuseUpdateInfo)
{ {
if (fuseUpdateInfo.Complete) if (fuseUpdateInfo.Complete)
{ {
_sw.Stop(); _sw.Stop();
ProgressText = string.Empty; ProgressText = string.Empty;
IsBusy = false;
return; return;
} }
ProgressPart = fuseUpdateInfo.ProgressPart; ProgressPart = fuseUpdateInfo.ProgressPart;
ProgressPartText = $"Part {fuseUpdateInfo.Part}/{fuseUpdateInfo.Parts}"; ProgressPartText = $"Part {fuseUpdateInfo.Part}/{fuseUpdateInfo.Parts}";
Progress = fuseUpdateInfo.Progress; Progress = fuseUpdateInfo.Progress;
@ -146,9 +140,38 @@ namespace nxDumpFuse.ViewModels
ProgressText = $"({fuseUpdateInfo.Speed:0}MB/s) {Progress:0}% "; ProgressText = $"({fuseUpdateInfo.Speed:0}MB/s) {Progress:0}% ";
} }
private void OnFuseServiceSimpleLogEvent(FuseSimpleLog log) private async void SelectInputFile()
{ {
LogItems.Add(log); InputFilePath = await _dialogService.ShowOpenFileDialogAsync("Choose Input File",
new FileDialogFilter { Name = string.Empty, Extensions = new List<string>() });
}
private async void SelectOutputFolder()
{
OutputDir = await _dialogService.ShowOpenFolderDialogAsync("Choose Output Folder");
}
private void StartFuse()
{
IsBusy = true;
_sw.Start();
try
{
_fuseService.Start(InputFilePath, OutputDir);
}
catch (Exception e)
{
_sw.Stop();
OnFuseServiceSimpleLogEvent(new FuseSimpleLog(FuseSimpleLogType.Error, DateTime.Now, e.Message));
}
}
private void StopFuse()
{
_sw.Stop();
_fuseService.Stop();
ProgressText = string.Empty;
IsBusy = false;
} }
} }
} }

View file

@ -27,7 +27,7 @@
<StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Horizontal" <StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Horizontal"
HorizontalAlignment="Right"> HorizontalAlignment="Right">
<Button Command="{Binding FuseCommand}" Content="Fuse" /> <Button Command="{Binding StartCommand}" Content="Fuse" />
<Button Command="{Binding StopCommand}" Content="Stop" /> <Button Command="{Binding StopCommand}" Content="Stop" />
</StackPanel> </StackPanel>