diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 81d3e85..10e9979 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -29,6 +29,7 @@ public partial class MainWindow : AdonisWindow { static readonly string CONVERTER_PATH = Path.Join(AppDomain.CurrentDomain.BaseDirectory, CONVERTER_NAME); static readonly string SETTINGS_PATH = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "settings.json"); static readonly string TEMPORARY_PATH = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "tmp"); + static readonly string LOG_PATH = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "log.txt"); private ApplicationSettings appSettings = new ApplicationSettings(); private MediaPlayer mediaPlayer = new MediaPlayer(); @@ -50,6 +51,7 @@ public MainWindow() { appSettings = JsonConvert.DeserializeObject(File.ReadAllText(SETTINGS_PATH)); } catch (Exception ex) { Trace.WriteLine(ex.Message); + File.AppendAllText(LOG_PATH, ex.Message); } } @@ -67,6 +69,7 @@ public MainWindow() { appSettings.lastUpdateCheck = DateTime.Now; } catch (Exception ex) { Trace.WriteLine(ex.Message); + File.AppendAllText(LOG_PATH, ex.Message); } } @@ -95,6 +98,7 @@ private void OnReleaseDataFetched(object sender, DownloadStringCompletedEventArg latestRelease = allReleases.FirstOrDefault(r => !r.draft && !r.prerelease); } catch (Exception ex) { Trace.WriteLine(ex.Message); + File.AppendAllText(LOG_PATH, ex.Message); } if (latestRelease == null) { return; @@ -307,16 +311,13 @@ private void OnRecentFileClick(object sender, RoutedEventArgs e) { } } - private void OnConvertLooseFilesClick(object sender, RoutedEventArgs e) - { - var diag = new OpenFileDialog - { - Filter = "Stream Audio File (*.stream)|*.stream|Waveform Audio File (*.wav)|*.wav", + private void OnConvertLooseFilesClick(object sender, RoutedEventArgs e) { + var diag = new OpenFileDialog { + Filter = "Stream audio files (*.stream)|*.stream|Wave audio files (*.wav)|*.wav", Multiselect = true, - + }; - if (diag.ShowDialog() != true) - { + if (diag.ShowDialog() != true) { return; } @@ -425,15 +426,15 @@ private void ExtractStreams(object sender, DoWorkEventArgs e) { Directory.CreateDirectory(savePath); } var n = 0; - var errors = 0; + var errors = new List(); foreach (var info in streamDescriptions) { - var fileName = Path.Join(savePath, $"{info.Id}.stream"); - var convertedFileName = Path.ChangeExtension(fileName, "wav"); + var file = Path.Join(savePath, $"{info.Id}.stream"); + var convertedFileName = Path.ChangeExtension(file, "wav"); try { - info.Save(fileName); - StartConverterProcess($"-d \"{fileName}\" \"{convertedFileName}\""); - } catch (Exception) { - errors++; + info.Save(file); + StartConverterProcess($"-d \"{file}\" \"{convertedFileName}\""); + } catch (Exception ex) { + errors.Add(ex.Message); } (sender as BackgroundWorker).ReportProgress((int)(++n / (float)streamDescriptions.Count() * 100)); } @@ -441,9 +442,10 @@ private void ExtractStreams(object sender, DoWorkEventArgs e) { } void OnExtractStreamsFinished(object sender, RunWorkerCompletedEventArgs e) { - var errors = (int)e.Result; - if (errors > 0) { - MessageBox.Show($"Extraction finished with {errors} converter {(errors == 1 ? "error" : "errors")}!", "Information", MessageBoxButton.OK, MessageBoxImage.Warning); + var errors = (List)e.Result; + if (errors.Count > 0) { + MessageBox.Show($"Extraction finished with {errors.Count} error(s)!", "Information", MessageBoxButton.OK, MessageBoxImage.Warning); + File.AppendAllLines(LOG_PATH, errors); } else { MessageBox.Show("Extraction complete!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } @@ -452,7 +454,7 @@ void OnExtractStreamsFinished(object sender, RunWorkerCompletedEventArgs e) { private void ReplaceStreams(object sender, DoWorkEventArgs e) { var fileMappings = (Dictionary)e.Argument; var n = 0; - var errors = 0; + var errors = new List(); if (!Directory.Exists(TEMPORARY_PATH)) { Directory.CreateDirectory(TEMPORARY_PATH); } @@ -463,8 +465,8 @@ private void ReplaceStreams(object sender, DoWorkEventArgs e) { var fileName = Path.Combine(TEMPORARY_PATH, fileNameNoExt + ".stream"); try { StartConverterProcess($"-e \"{file}\" \"{fileName}\""); - } catch (Exception) { - errors++; + } catch (Exception ex) { + errors.Add(ex.Message); } targetStreamInfo.Data = File.ReadAllBytes(fileName); targetStreamInfo.ReplacementFile = fileNameNoExt + ".wav"; @@ -479,9 +481,10 @@ private void ReplaceStreams(object sender, DoWorkEventArgs e) { } void OnReplaceStreamsFinished(object sender, RunWorkerCompletedEventArgs e) { - var errors = (int)e.Result; - if (errors > 0) { - MessageBox.Show($"Sound replacement finished with {errors} errors!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); + var errors = (List)e.Result; + if (errors.Count > 0) { + MessageBox.Show($"Sound replacement finished with {errors.Count} error(s)!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); + File.AppendAllLines(LOG_PATH, errors); } else { MessageBox.Show($"Sound replacement finished successfully!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } @@ -490,39 +493,31 @@ void OnReplaceStreamsFinished(object sender, RunWorkerCompletedEventArgs e) { } } - private void ConvertLooseFiles(object sender, DoWorkEventArgs e) - { + private void ConvertLooseFiles(object sender, DoWorkEventArgs e) { var files = (string[])e.Argument; var n = 0; - var errors = 0; - foreach (var file in files) - { + var errors = new List(); + foreach (var file in files) { var fileExt = Path.GetExtension(file); var fileNameNoExt = Path.GetFileNameWithoutExtension(file); var fileDir = Path.GetDirectoryName(file); var fileName = Path.Combine(fileDir, fileNameNoExt + (fileExt == ".wav" ? ".stream" : ".wav")); - try - { + try { StartConverterProcess($"-{(fileExt == ".wav" ? "e" : "d")} \"{file}\" \"{fileName}\""); - } - catch (Exception) - { - errors++; + } catch (Exception ex) { + errors.Add(ex.Message); } (sender as BackgroundWorker).ReportProgress((int)(++n / (float)files.Length * 100)); } e.Result = errors; } - void OnConvertLooseFilesFinished(object sender, RunWorkerCompletedEventArgs e) - { - var errors = (int)e.Result; - if (errors > 0) - { - MessageBox.Show($"Conversion finished with {errors} errors!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); - } - else - { + void OnConvertLooseFilesFinished(object sender, RunWorkerCompletedEventArgs e) { + var errors = (List)e.Result; + if (errors.Count > 0) { + MessageBox.Show($"Conversion finished with {errors.Count} error(s)!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); + File.AppendAllLines(LOG_PATH, errors); + } else { MessageBox.Show($"Conversion finished successfully!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } }