Thealexbarney / VGAudio

A library for encoding, decoding, and manipulating audio files from video games.
MIT License
219 stars 37 forks source link

The CLI conversion runs indefinitely when called from a .NET app #126

Open Developer-exe opened 3 years ago

Developer-exe commented 3 years ago

I currently experiment with the VGAudio CLI application. The app I'm trying to make calls the CLI with a set of parameters. The application then reads the standard output of the CLI to get a result of the conversion (and the time elapsed, if applicable).

The main issue when calling the CLI is that it doesn't close itself upon converting to certain file formats. It keeps running in the background (even though the conversion is finished) leaving the converted file locked.

A summary of the file formats The file formats currently used by the app for the conversion: - WAV (works) - DSP (runs indefinitely) - IDSP (runs indefinitely) - BRSTM (runs indefinitely) - BCSTM (runs indefinitely) - BFSTM (runs indefinitely) - HPS (runs indefinitely) - ADX (works) - HCA (works)

The only ones that work with all approaches are WAV and the CRIWARE audio formats (ADX and HCA). When converting the other formats (DSP, IDSP, BRSTM, BCSTM, BFSTM, HPS) directly using the command prompt, the CLI "hangs" for a second before starting the conversion. This is not the case for the WAV, ADX and HCA file formats.

Different ways to tackle this issue I've tried several approaches: 1. Executing the command, waiting for it to exit and then read the output with `proc.StandardOutput.ReadToEnd()` ``` // This doesn't work with certain file formats var process = Process.Start(processInfo); process.WaitForExit(); output = proc.StandardOutput.ReadToEnd(); ``` 2. Executing the command and appending the lines using `proc.StandardOutput.ReadLine()` ``` // This one always works and is the thing I've been using up until now var process = Process.Start(processInfo); while (!process.StandardOutput.EndOfStream) { output += process.StandardOutput.ReadLine() + "\r\n"; } ``` 3. Running the process asynchronously and appending the lines using `DataReceivedEventHandler` ``` // This one doesn't work with the same file formats as the first approach var tcs = new TaskCompletionSource(); var process = new Process { StartInfo = processInfo, EnableRaisingEvents = true }; process.OutputDataReceived += new DataReceivedEventHandler((sender, e) => { if (!String.IsNullOrEmpty(e.Data)) { MessageBox.Show(e.Data); output += e.Data + "\r\n"; } }); process.Exited += (sender, args) => { tcs.SetResult(process.ExitCode); process.Dispose(); }; process.Start(); return tcs.Task; ``` 4. Not redirecting the output and show the command line instead ``` // This one always works but then the app can't read the output processInfo.RedirectStandardOutput = false; processInfo.UseShellExecute = true; ```

The reason I'd like to switch to the third approach (asynchronous) is that I don't want the CLI to hang the application until the conversion is complete. The problem is that using DataReceivedEventHandler asynchronously makes the CLI keep running in the background as if I waited for it to exit and then read the output with proc.StandardOutput.ReadToEnd() synchronously.

Is there a different way the CLI handles the CRIWARE audio formats from the other ones (DSP, IDSP, BRSTM, BCSTM, BFSTM, HPS)? Could the conversion process be somehow unified or is the problem somewhere else? Many thanks.