mirror of
https://github.com/oMaN-Rod/nxDumpFuse.git
synced 2024-11-08 11:51:49 +00:00
Add start/stop logic to avoid multiple clicks when starting fuse
This commit is contained in:
parent
c34cf7670a
commit
21720adfae
3 changed files with 74 additions and 55 deletions
|
@ -15,10 +15,7 @@ namespace nxDumpFuse.Services
|
|||
{
|
||||
|
||||
private CancellationTokenSource? _cts;
|
||||
private string? _inputFilePath;
|
||||
private string? _outputDir;
|
||||
private string? _outputFilePath;
|
||||
private FileCase _fileCase;
|
||||
private readonly Stopwatch _sw = new();
|
||||
|
||||
public event EventHandlers.FuseUpdateEventHandler? FuseUpdateEvent;
|
||||
|
@ -54,29 +51,28 @@ namespace nxDumpFuse.Services
|
|||
|
||||
public void Start(string inputFilePath, string outputDir)
|
||||
{
|
||||
_inputFilePath = inputFilePath;
|
||||
_outputDir = outputDir;
|
||||
_cts = new CancellationTokenSource();
|
||||
|
||||
if (string.IsNullOrEmpty(_inputFilePath))
|
||||
if (string.IsNullOrEmpty(inputFilePath))
|
||||
{
|
||||
Log(FuseSimpleLogType.Error, "Input File cannot be empty");
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_outputDir))
|
||||
if (string.IsNullOrEmpty(outputDir))
|
||||
{
|
||||
Log(FuseSimpleLogType.Error, "Output Directory cannot be empty");
|
||||
return;
|
||||
}
|
||||
|
||||
(_outputFilePath, _fileCase) = _inputFilePath.GetOutputFilePath(_outputDir);
|
||||
if (string.IsNullOrEmpty(_outputFilePath) || _fileCase == FileCase.Invalid)
|
||||
FileCase fileCase;
|
||||
(_outputFilePath, fileCase) = inputFilePath.GetOutputFilePath(outputDir);
|
||||
if (string.IsNullOrEmpty(_outputFilePath) || fileCase == FileCase.Invalid)
|
||||
{
|
||||
Log(FuseSimpleLogType.Error, "Output path was null");
|
||||
return;
|
||||
}
|
||||
|
||||
var inputFiles = _inputFilePath.GetInputFiles(_fileCase);
|
||||
var inputFiles = inputFilePath.GetInputFiles(fileCase);
|
||||
if (inputFiles.Count == 0)
|
||||
{
|
||||
Log(FuseSimpleLogType.Error, "No input files found");
|
||||
|
@ -88,12 +84,12 @@ namespace nxDumpFuse.Services
|
|||
|
||||
public void Stop()
|
||||
{
|
||||
_cts.Cancel();
|
||||
_cts?.Cancel();
|
||||
_sw.Stop();
|
||||
|
||||
Log(FuseSimpleLogType.Information, "Fuse Stopped");
|
||||
|
||||
if (File.Exists(_outputFilePath))
|
||||
if (!string.IsNullOrEmpty(_outputFilePath) && File.Exists(_outputFilePath))
|
||||
{
|
||||
Task.Run((() =>
|
||||
{
|
||||
|
@ -126,10 +122,10 @@ namespace nxDumpFuse.Services
|
|||
Log(FuseSimpleLogType.Information, $"Fusing {inputFiles.Count} parts to {_outputFilePath} ({totalFileLength.ToMb()}MB)");
|
||||
|
||||
_sw.Start();
|
||||
await using var outputStream = File.Create(_outputFilePath);
|
||||
await using var outputStream = File.Create(_outputFilePath!);
|
||||
foreach (var inputFilePath in inputFiles)
|
||||
{
|
||||
if (_cts.Token.IsCancellationRequested) return;
|
||||
if (_cts!.Token.IsCancellationRequested) return;
|
||||
long currentBytes = 0;
|
||||
int currentBlockSize;
|
||||
long copySpeed = 0;
|
||||
|
|
|
@ -23,12 +23,24 @@ namespace nxDumpFuse.ViewModels
|
|||
{
|
||||
_dialogService = dialogService;
|
||||
_fuseService = fuseServiceService;
|
||||
_fuseService.FuseUpdateEvent += OnFuseServiceUpdate;
|
||||
_fuseService.FuseSimpleLogEvent += OnFuseServiceSimpleLogEvent;
|
||||
|
||||
SelectInputFileCommand = ReactiveCommand.Create(SelectInputFile);
|
||||
SelectOutputFolderCommand = ReactiveCommand.Create(SelectOutputFolder);
|
||||
FuseCommand = ReactiveCommand.Create(FuseNxDump);
|
||||
StopCommand = ReactiveCommand.Create(StopDump);
|
||||
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";
|
||||
}
|
||||
|
||||
|
@ -38,10 +50,17 @@ namespace nxDumpFuse.ViewModels
|
|||
|
||||
public ReactiveCommand<Unit, Unit> ClearLogCommand { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> FuseCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> StartCommand { 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;
|
||||
public string InputFilePath
|
||||
{
|
||||
|
@ -88,39 +107,7 @@ namespace nxDumpFuse.ViewModels
|
|||
public ObservableCollection<FuseSimpleLog> LogItems
|
||||
{
|
||||
get => _logItems;
|
||||
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;
|
||||
set => this.RaiseAndSetIfChanged(ref _logItems, value);
|
||||
}
|
||||
|
||||
private void ClearLog()
|
||||
|
@ -128,14 +115,21 @@ namespace nxDumpFuse.ViewModels
|
|||
LogItems.Clear();
|
||||
}
|
||||
|
||||
private void OnFuseServiceSimpleLogEvent(FuseSimpleLog log)
|
||||
{
|
||||
LogItems.Add(log);
|
||||
}
|
||||
|
||||
private void OnFuseServiceUpdate(FuseUpdateInfo fuseUpdateInfo)
|
||||
{
|
||||
if (fuseUpdateInfo.Complete)
|
||||
{
|
||||
_sw.Stop();
|
||||
ProgressText = string.Empty;
|
||||
IsBusy = false;
|
||||
return;
|
||||
}
|
||||
|
||||
ProgressPart = fuseUpdateInfo.ProgressPart;
|
||||
ProgressPartText = $"Part {fuseUpdateInfo.Part}/{fuseUpdateInfo.Parts}";
|
||||
Progress = fuseUpdateInfo.Progress;
|
||||
|
@ -146,9 +140,38 @@ namespace nxDumpFuse.ViewModels
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
<StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Horizontal"
|
||||
HorizontalAlignment="Right">
|
||||
<Button Command="{Binding FuseCommand}" Content="Fuse" />
|
||||
<Button Command="{Binding StartCommand}" Content="Fuse" />
|
||||
<Button Command="{Binding StopCommand}" Content="Stop" />
|
||||
</StackPanel>
|
||||
|
||||
|
|
Loading…
Reference in a new issue