diff --git a/src/nxDumpFuse/Services/FuseService.cs b/src/nxDumpFuse/Services/FuseService.cs index 9254f26..095100a 100644 --- a/src/nxDumpFuse/Services/FuseService.cs +++ b/src/nxDumpFuse/Services/FuseService.cs @@ -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; diff --git a/src/nxDumpFuse/ViewModels/FuseViewModel.cs b/src/nxDumpFuse/ViewModels/FuseViewModel.cs index 4349976..5bb38c2 100644 --- a/src/nxDumpFuse/ViewModels/FuseViewModel.cs +++ b/src/nxDumpFuse/ViewModels/FuseViewModel.cs @@ -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 ClearLogCommand { get; } - public ReactiveCommand FuseCommand { get; } + public ReactiveCommand StartCommand { get; } public ReactiveCommand 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 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() }); - } - - 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() }); + } + + 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; } } -} +} \ No newline at end of file diff --git a/src/nxDumpFuse/Views/FuseView.axaml b/src/nxDumpFuse/Views/FuseView.axaml index f3b48e6..7eb8410 100644 --- a/src/nxDumpFuse/Views/FuseView.axaml +++ b/src/nxDumpFuse/Views/FuseView.axaml @@ -27,7 +27,7 @@ -