Skip to content

Commit

Permalink
Check MultiDumper's help text before passing it custom arguments - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed Aug 28, 2021
1 parent 1381830 commit 61f13a2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
51 changes: 33 additions & 18 deletions LibSidWiz/MultiDumperWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,7 @@ public IEnumerable<Song> 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))
{
Expand Down Expand Up @@ -119,13 +104,43 @@ public IEnumerable<Song> 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<string> Dump(Song song, Action<double> 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(@"(?<channel>\d+)\|(?<position>\d+)\|(?<total>\d+)");
var stopwatch = Stopwatch.StartNew();
Expand Down
40 changes: 26 additions & 14 deletions LibSidWiz/ProcessWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ProcessWrapper: IDisposable
private readonly BlockingCollection<string> _lines = new BlockingCollection<string>(new ConcurrentQueue<string>());
private readonly CancellationTokenSource _cancellationTokenSource;

public ProcessWrapper(string filename, string arguments)
public ProcessWrapper(string filename, string arguments, bool captureStdErr = false)
{
_cancellationTokenSource = new CancellationTokenSource();

Expand All @@ -23,6 +23,7 @@ public ProcessWrapper(string filename, string arguments)
FileName = filename,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
},
Expand All @@ -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
}
}

/// <summary>
Expand Down

0 comments on commit 61f13a2

Please sign in to comment.