diff --git a/LibSidWiz/MultiDumperWrapper.cs b/LibSidWiz/MultiDumperWrapper.cs index 151ad19..626650d 100644 --- a/LibSidWiz/MultiDumperWrapper.cs +++ b/LibSidWiz/MultiDumperWrapper.cs @@ -50,22 +50,7 @@ public IEnumerable GetSongs(string filename) throw new FileNotFoundException("Cannot find VGM file", filename); } - string json; - using (var p = new ProcessWrapper( - _multiDumperPath, - $"\"{filename}\" --json")) - { - json = string.Join("", p.Lines()); - // Try to decode any UTF-8 in there - try - { - json = Encoding.UTF8.GetString(Encoding.Default.GetBytes(json)); - } - catch (Exception) - { - // Ignore it, use unfixed string - } - } + var json = GetOutputText($"\"{filename}\" --json", false); if (string.IsNullOrEmpty(json)) { @@ -119,13 +104,43 @@ public IEnumerable GetSongs(string filename) Game = Clean(metadata.containerinfo.game), System = Clean(metadata.containerinfo.system) }); - } + } + + private string GetOutputText(string args, bool includeStdErr) + { + using (var p = new ProcessWrapper( + _multiDumperPath, + args, + includeStdErr)) + { + string text = string.Join("", p.Lines()); + // Try to decode any UTF-8 in there + try + { + text = Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)); + } + catch (Exception) + { + // Ignore it, use unfixed string + } + return text; + } + } public IEnumerable Dump(Song song, Action onProgress) { + // We check the help first to check for allowed parameters + var helpText = GetOutputText("", true); + + var args = new StringBuilder($"\"{song.Filename}\" {song.Index}"); + if (helpText.Contains("--sampling_rate=")) + { + args.Append($" --sampling_rate={_samplingRate}"); + } + _processWrapper = new ProcessWrapper( _multiDumperPath, - $"\"{song.Filename}\" {song.Index} --sampling_rate={_samplingRate}"); + args.ToString()); var progressParts = Enumerable.Repeat(0.0, song.Channels.Count).ToList(); var r = new Regex(@"(?\d+)\|(?\d+)\|(?\d+)"); var stopwatch = Stopwatch.StartNew(); diff --git a/LibSidWiz/ProcessWrapper.cs b/LibSidWiz/ProcessWrapper.cs index b0750a7..9bd8d48 100644 --- a/LibSidWiz/ProcessWrapper.cs +++ b/LibSidWiz/ProcessWrapper.cs @@ -12,7 +12,7 @@ public class ProcessWrapper: IDisposable private readonly BlockingCollection _lines = new BlockingCollection(new ConcurrentQueue()); private readonly CancellationTokenSource _cancellationTokenSource; - public ProcessWrapper(string filename, string arguments) + public ProcessWrapper(string filename, string arguments, bool captureStdErr = false) { _cancellationTokenSource = new CancellationTokenSource(); @@ -23,6 +23,7 @@ public ProcessWrapper(string filename, string arguments) FileName = filename, Arguments = arguments, RedirectStandardOutput = true, + RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }, @@ -32,22 +33,33 @@ public ProcessWrapper(string filename, string arguments) { throw new Exception($"Error running {filename} {arguments}"); } - _process.OutputDataReceived += (sender, e) => + _process.OutputDataReceived += OnText; + if (captureStdErr) { - if (!_cancellationTokenSource.IsCancellationRequested) - { - try - { - _lines.TryAdd(e.Data, 0, _cancellationTokenSource.Token); - } - catch (OperationCanceledException) - { - // Discard it - } - } - }; + _process.ErrorDataReceived += OnText; + } _process.Start(); _process.BeginOutputReadLine(); + if (captureStdErr) + { + _process.BeginErrorReadLine(); + } + } + + private void OnText(object sender, DataReceivedEventArgs e) + { + if (_cancellationTokenSource.IsCancellationRequested) + { + return; + } + try + { + _lines.TryAdd(e.Data, 0, _cancellationTokenSource.Token); + } + catch (OperationCanceledException) + { + // Discard it + } } ///