Several ProcessStartInfo -> ProcessHelper conversions

This commit is contained in:
Timothy Baldridge 2020-04-10 06:58:55 -06:00
parent 18d5f56f52
commit 712438c6a6
5 changed files with 102 additions and 218 deletions

View File

@ -16,24 +16,35 @@ namespace Wabbajack.Common
Error,
}
public string Path { get; set; } = string.Empty;
public AbsolutePath Path { get; set; }
public IEnumerable<object> Arguments { get; set; } = Enumerable.Empty<object>();
public bool LogError { get; set; } = true;
public readonly Subject<(StreamType Type, string Line)> Output = new Subject<(StreamType Type, string)>();
public readonly Subject<(StreamType Type, string Line)> Output = new Subject<(StreamType Type, string)>();
public bool ThrowOnNonZeroExitCode { get; set; } = false;
public ProcessHelper()
{
}
public async Task<int> Start()
{
var args = Arguments.Select(arg =>
{
return arg switch
{
AbsolutePath abs => $"\"{abs}\"",
RelativePath rel => $"\"{rel}\"",
_ => arg.ToString()
};
});
var info = new ProcessStartInfo
{
FileName = (string)Path,
Arguments = string.Join(" ", Arguments),
Arguments = string.Join(" ", args),
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
@ -65,7 +76,7 @@ namespace Wabbajack.Common
if (string.IsNullOrEmpty(data.Data)) return;
Output.OnNext((StreamType.Error, data.Data));
if (LogError)
Utils.Log($"{AlphaPath.GetFileName(Path)} ({p.Id}) StdErr: {data.Data}");
Utils.Log($"{Path.FileName} ({p.Id}) StdErr: {data.Data}");
};
p.ErrorDataReceived += ErrorEventHandler;
@ -92,6 +103,9 @@ namespace Wabbajack.Common
p.Exited -= Exited;
Output.OnCompleted();
if (result != 0 && ThrowOnNonZeroExitCode)
throw new Exception($"Error executing {Path} - Exit Code {result} - Check the log for more information");
return result;
}

View File

@ -517,7 +517,6 @@ namespace Wabbajack.Common
return await Task.WhenAll(tasks);
}
public static async Task<TR[]> PMap<TI, TR>(this IEnumerable<TI> coll, WorkQueue queue,
Func<TI, Task<TR>> f)
{
@ -956,7 +955,7 @@ namespace Wabbajack.Common
{
var process = new ProcessHelper
{
Path = "cmd.exe",
Path = ((RelativePath)"cmd.exe").RelativeToSystemDirectory(),
Arguments = new object[] {"/c", "del", "/f", "/q", "/s", $"\"{(string)path}\"", "&&", "rmdir", "/q", "/s", $"\"{(string)path}\""},
};
var result = process.Output.Where(d => d.Type == ProcessHelper.StreamType.Output)

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Compression;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
@ -144,71 +145,46 @@ namespace Wabbajack.Lib.Downloaders
}
private const string FFMpegPath = "Downloaders/Converters/ffmpeg.exe";
private const string xWMAEncodePath = "Downloaders/Converters/xWMAEncode.exe";
private async Task ExtractTrack(AbsolutePath source, AbsolutePath dest_folder, Track track)
private AbsolutePath FFMpegPath => "Downloaders/Converters/ffmpeg.exe".RelativeTo(AbsolutePath.EntryPoint);
private AbsolutePath xWMAEncodePath = "Downloaders/Converters/xWMAEncode.exe".RelativeTo(AbsolutePath.EntryPoint);
private Extension WAVExtension = new Extension(".wav");
private Extension XWMExtension = new Extension(".xwm");
private async Task ExtractTrack(AbsolutePath source, AbsolutePath destFolder, Track track)
{
var info = new ProcessStartInfo
var process = new ProcessHelper
{
FileName = FFMpegPath,
Arguments =
$"-threads 1 -i \"{source}\" -ss {track.Start} -t {track.End - track.Start} \"{dest_folder}\\{track.Name}.wav\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = FFMpegPath,
Arguments = new object[] {"-threads", 1, "-i", source, "-ss", track.Start, "-t", track.End - track.Start, track.Name.RelativeTo(destFolder).WithExtension(WAVExtension)},
ThrowOnNonZeroExitCode = true
};
var ffmpegLogs = process.Output.Where(arg => arg.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(val =>
{
Utils.Status($"Extracting {track.Name} - {val.Line}");
});
var p = new Process {StartInfo = info};
p.Start();
ChildProcessTracker.AddProcess(p);
var output = await p.StandardError.ReadToEndAsync();
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception e)
{
Utils.Error(e, "Error while setting process priority level for ffmpeg.exe");
}
p.WaitForExit();
await process.Start();
if (track.Format == Track.FormatEnum.WAV) return;
info = new ProcessStartInfo
process = new ProcessHelper()
{
FileName = xWMAEncodePath,
Arguments =
$"-b 192000 \"{dest_folder}\\{track.Name}.wav\" \"{dest_folder}\\{track.Name}.xwm\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = xWMAEncodePath,
Arguments = new object[] {"-b", 192000, track.Name.RelativeTo(destFolder).WithExtension(WAVExtension), track.Name.RelativeTo(destFolder).WithExtension(XWMExtension)},
ThrowOnNonZeroExitCode = true
};
p = new Process {StartInfo = info};
var xwmLogs = process.Output.Where(arg => arg.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(val =>
{
Utils.Status($"Encoding {track.Name} - {val.Line}");
});
p.Start();
ChildProcessTracker.AddProcess(p);
var output2 = await p.StandardError.ReadToEndAsync();
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception e)
{
Utils.Error(e, "Error while setting process priority level for ffmpeg.exe");
}
p.WaitForExit();
await process.Start();
if (File.Exists($"{dest_folder}\\{track.Name}.wav"))
File.Delete($"{dest_folder}\\{track.Name}.wav");
if (File.Exists($"{destFolder}\\{track.Name}.wav"))
File.Delete($"{destFolder}\\{track.Name}.wav");
}

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using Compression.BSA;
@ -25,9 +26,9 @@ namespace Wabbajack.VirtualFileSystem
else if (source.Extension == Consts.OMOD)
ExtractAllWithOMOD(source, dest);
else if (source.Extension == Consts.EXE)
ExtractAllEXE(source, dest);
await ExtractAllExe(source, dest);
else
ExtractAllWith7Zip(source, dest);
await ExtractAllWith7Zip(source, dest);
}
catch (Exception ex)
{
@ -35,71 +36,40 @@ namespace Wabbajack.VirtualFileSystem
}
}
private static void ExtractAllEXE(AbsolutePath source, AbsolutePath dest)
private static async Task ExtractAllExe(AbsolutePath source, AbsolutePath dest)
{
var isArchive = TestWith7z(source);
var isArchive = await TestWith7z(source);
if (isArchive)
{
ExtractAllWith7Zip(source, dest);
await ExtractAllWith7Zip(source, dest);
return;
}
Utils.Log($"Extracting {(string)source.FileName}");
var info = new ProcessStartInfo
var process = new ProcessHelper
{
FileName = @"Extractors\innounp.exe",
Arguments = $"-x -y -b -d\"{(string)dest}\" \"{(string)source}\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = @"Extractors\innounp.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"-x", "-y", "-b", $"-d\"{dest}\"", source}
};
var p = new Process {StartInfo = info};
p.Start();
ChildProcessTracker.AddProcess(p);
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception e)
{
Utils.Error(e, "Error while setting process priority level for innounp.exe");
}
var name = source.FileName;
try
{
while (!p.HasExited)
var result = process.Output.Where(d => d.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(p =>
{
var line = p.StandardOutput.ReadLine();
var (_, line) = p;
if (line == null)
break;
return;
if (line.Length <= 4 || line[3] != '%')
continue;
return;
int.TryParse(line.Substring(0, 3), out var percentInt);
Utils.Status($"Extracting {(string)name} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
}
}
catch (Exception e)
{
Utils.Error(e, "Error while reading StandardOutput for innounp.exe");
}
p.WaitForExitAndWarn(TimeSpan.FromSeconds(30), $"Extracting {(string)name}");
if (p.ExitCode == 0)
return;
Utils.Log(p.StandardOutput.ReadToEnd());
Utils.Log($"Extraction error extracting {source}");
}
Utils.Status($"Extracting {source.FileName} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
});
await process.Start();
}
private class OMODProgress : ICodeProgress
{
@ -159,60 +129,42 @@ namespace Wabbajack.VirtualFileSystem
}
}
private static void ExtractAllWith7Zip(AbsolutePath source, AbsolutePath dest)
private static async Task ExtractAllWith7Zip(AbsolutePath source, AbsolutePath dest)
{
Utils.Log(new GenericInfo($"Extracting {(string)source.FileName}", $"The contents of {(string)source.FileName} are being extracted to {(string)source.FileName} using 7zip.exe"));
var info = new ProcessStartInfo
var process = new ProcessHelper
{
FileName = @"Extractors\7z.exe",
Arguments = $"x -bsp1 -y -o\"{(string)dest}\" \"{(string)source}\" -mmt=off",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = @"Extractors\7z.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"x", "-bsp1", "-y", $"-o\"{dest}\"", source, "-mmt=off"}
};
var p = new Process {StartInfo = info};
p.Start();
ChildProcessTracker.AddProcess(p);
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception)
{
}
var name = source.FileName;
try
{
while (!p.HasExited)
var result = process.Output.Where(d => d.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(p =>
{
var line = p.StandardOutput.ReadLine();
var (_, line) = p;
if (line == null)
break;
return;
if (line.Length <= 4 || line[3] != '%') continue;
if (line.Length <= 4 || line[3] != '%') return;
int.TryParse(line.Substring(0, 3), out var percentInt);
Utils.Status($"Extracting {(string)name} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
}
}
catch (Exception)
{
}
Utils.Status($"Extracting {(string)source.FileName} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
});
p.WaitForExitAndWarn(TimeSpan.FromSeconds(30), $"Extracting {name}");
var exitCode = await process.Start();
if (p.ExitCode == 0)
if (exitCode != 0)
{
Utils.Status($"Extracting {name} - 100%", Percent.One, alsoLog: true);
return;
Utils.Error(new _7zipReturnError(exitCode, source, dest, ""));
}
else
{
Utils.Status($"Extracting {source.FileName} - done", Percent.One, alsoLog: true);
}
Utils.Error(new _7zipReturnError(p.ExitCode, source, dest, p.StandardOutput.ReadToEnd()));
}
/// <summary>
@ -220,92 +172,35 @@ namespace Wabbajack.VirtualFileSystem
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static bool CanExtract(AbsolutePath v)
public static async Task<bool> CanExtract(AbsolutePath v)
{
var ext = v.Extension;
if(ext != _exeExtension && !Consts.TestArchivesBeforeExtraction.Contains(ext))
return Consts.SupportedArchives.Contains(ext) || Consts.SupportedBSAs.Contains(ext);
var isArchive = TestWith7z(v);
var isArchive = await TestWith7z(v);
if (isArchive)
return true;
var info = new ProcessStartInfo
var process = new ProcessHelper
{
FileName = @"Extractors\innounp.exe",
Arguments = $"-t \"{v}\" ",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = @"Extractors\innounp.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"-t", v},
};
var p = new Process {StartInfo = info};
p.Start();
ChildProcessTracker.AddProcess(p);
var name = v.FileName;
while (!p.HasExited)
{
var line = p.StandardOutput.ReadLine();
if (line == null)
break;
if (line[0] != '#')
continue;
Utils.Status($"Testing {(string)name} - {line.Trim()}");
}
p.WaitForExitAndWarn(TimeSpan.FromSeconds(30), $"Testing {name}");
return p.ExitCode == 0;
return await process.Start() == 0;
}
public static bool TestWith7z(AbsolutePath file)
public static async Task<bool> TestWith7z(AbsolutePath file)
{
var testInfo = new ProcessStartInfo
var process = new ProcessHelper()
{
FileName = @"Extractors\7z.exe",
Arguments = $"t \"{file}\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
Path = @"Extractors\7z.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"t", file},
};
var testP = new Process {StartInfo = testInfo};
testP.Start();
ChildProcessTracker.AddProcess(testP);
try
{
testP.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception)
{
return false;
}
try
{
while (!testP.HasExited)
{
var line = testP.StandardOutput.ReadLine();
if (line == null)
break;
}
}
catch (Exception)
{
return false;
}
testP.WaitForExitAndWarn(TimeSpan.FromSeconds(30), $"Can Extract Check {file}");
return testP.ExitCode == 0;
return await process.Start() == 0;
}
private static Extension _exeExtension = new Extension(".exe");

View File

@ -180,7 +180,7 @@ namespace Wabbajack.VirtualFileSystem
if (context.UseExtendedHashes)
self.ExtendedHashes = ExtendedHashes.FromFile(absPath);
if (FileExtractor.CanExtract(absPath))
if (await FileExtractor.CanExtract(absPath))
{
await using var tempFolder = Context.GetTemporaryFolder();
await FileExtractor.ExtractAll(context.Queue, absPath, tempFolder.FullName);