Make File Extractor async

This commit is contained in:
Timothy Baldridge 2019-11-11 22:33:32 -07:00
parent 29e6d577d2
commit 0cb6f2fa69
6 changed files with 53 additions and 97 deletions

View File

@ -28,7 +28,7 @@ namespace Compression.BSA.Test
public TestContext TestContext { get; set; }
[ClassInitialize]
public static void Setup(TestContext TestContext)
public static async Task Setup(TestContext TestContext)
{
Utils.LogMessages.Subscribe(f => TestContext.WriteLine(f));
@ -46,15 +46,14 @@ namespace Compression.BSA.Test
(Game.Fallout4, 22223) // 10mm SMG
};
mod_ids.Do(info =>
foreach (var info in mod_ids)
{
var filename = DownloadMod(info);
var folder = Path.Combine(BSAFolder, info.Item1.ToString(), info.Item2.ToString());
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
FileExtractor.ExtractAll(filename, folder);
});
await FileExtractor.ExtractAll(filename, folder);
}
}
private static string DownloadMod((Game, int) info)

View File

@ -368,7 +368,7 @@ namespace VFS
var tmp_dir = Path.Combine(_stagedRoot, Guid.NewGuid().ToString());
Utils.Status($"Extracting Archive {Path.GetFileName(f.StagedPath)}");
FileExtractor.ExtractAll(f.StagedPath, tmp_dir);
FileExtractor.ExtractAll(f.StagedPath, tmp_dir).Wait();
Utils.Status($"Updating Archive {Path.GetFileName(f.StagedPath)}");
@ -422,7 +422,7 @@ namespace VFS
foreach (var group in grouped)
{
var tmp_path = Path.Combine(_stagedRoot, Guid.NewGuid().ToString());
FileExtractor.ExtractAll(group.Key.StagedPath, tmp_path);
FileExtractor.ExtractAll(group.Key.StagedPath, tmp_path).Wait();
Paths.Add(tmp_path);
foreach (var file in group)
file._stagedPath = Path.Combine(tmp_path, file.Paths[group.Key.Paths.Length]);

View File

@ -30,6 +30,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

View File

@ -33,18 +33,16 @@ namespace Wabbajack.Common
}
public static void ExtractAll(string source, string dest)
public static async Task ExtractAll(string source, string dest)
{
try
{
if (Consts.SupportedBSAs.Any(b => source.ToLower().EndsWith(b)))
ExtractAllWithBSA(source, dest).Wait();
else if (source.EndsWith(".exe"))
ExtractAllWithInno(source, dest);
await ExtractAllWithBSA(source, dest);
else if (source.EndsWith(".omod"))
ExtractAllWithOMOD(source, dest);
await ExtractAllWithOMOD(source, dest);
else
ExtractAllWith7Zip(source, dest);
await ExtractAllWith7Zip(source, dest);
}
catch (Exception ex)
{
@ -53,14 +51,18 @@ namespace Wabbajack.Common
}
}
private static void ExtractAllWithOMOD(string source, string dest)
private static Task ExtractAllWithOMOD(string source, string dest)
{
Utils.Log($"Extracting {Path.GetFileName(source)}");
var f = new Framework();
f.SetTempDirectory(dest);
var omod = new OMOD(source, ref f);
omod.ExtractDataFiles();
omod.ExtractPlugins();
return CSPExtensions.ThreadedTask(() =>
{
Utils.Log($"Extracting {Path.GetFileName(source)}");
var f = new Framework();
f.SetTempDirectory(dest);
var omod = new OMOD(source, ref f);
omod.ExtractDataFiles();
omod.ExtractPlugins();
return dest;
});
}
private static async Task ExtractAllWithBSA(string source, string dest)
@ -100,7 +102,7 @@ namespace Wabbajack.Common
}
}
private static void ExtractAllWith7Zip(string source, string dest)
private static async Task ExtractAllWith7Zip(string source, string dest)
{
Utils.Log($"Extracting {Path.GetFileName(source)}");
@ -135,73 +137,14 @@ namespace Wabbajack.Common
{
while (!p.HasExited)
{
var line = p.StandardOutput.ReadLine();
var line = await p.StandardOutput.ReadLineAsync();
if (line == null)
break;
var percent = 0;
if (line.Length > 4 && line[3] == '%')
{
int.TryParse(line.Substring(0, 3), out percent);
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
}
}
}
catch (Exception)
{
}
p.WaitForExit();
if (p.ExitCode != 0)
{
Utils.Log(p.StandardOutput.ReadToEnd());
Utils.Log($"Extraction error extracting {source}");
}
}
private static void ExtractAllWithInno(string source, string dest)
{
Utils.Log($"Extracting {Path.GetFileName(source)}");
var info = new ProcessStartInfo
{
FileName = "innounp.exe",
Arguments = $"-x -y -b -d\"{dest}\" \"{source}\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var p = new Process
{
StartInfo = info
};
p.Start();
ChildProcessTracker.AddProcess(p);
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception)
{
}
var name = Path.GetFileName(source);
try
{
while (!p.HasExited)
{
var line = p.StandardOutput.ReadLine();
if (line == null)
break;
var percent = 0;
if (line.Length > 4 && line[3] == '%')
{
int.TryParse(line.Substring(0, 3), out percent);
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
}
if (line.Length <= 4 || line[3] != '%') continue;
int.TryParse(line.Substring(0, 3), out var percent);
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
}
}
catch (Exception)
@ -226,11 +169,5 @@ namespace Wabbajack.Common
v = v.ToLower();
return Consts.SupportedArchives.Contains(v) || Consts.SupportedBSAs.Contains(v);
}
public class Entry
{
public string Name;
public ulong Size;
}
}
}

View File

@ -93,9 +93,9 @@ namespace Wabbajack.Test
File.Copy(src, Path.Combine(utils.DownloadsFolder, filename));
if (mod_name == null)
FileExtractor.ExtractAll(src, utils.MO2Folder);
FileExtractor.ExtractAll(src, utils.MO2Folder).Wait();
else
FileExtractor.ExtractAll(src, Path.Combine(utils.ModsFolder, mod_name));
FileExtractor.ExtractAll(src, Path.Combine(utils.ModsFolder, mod_name)).Wait();
}
@ -130,7 +130,7 @@ namespace Wabbajack.Test
var dest = Path.Combine(utils.DownloadsFolder, file.file_name);
File.Copy(src, dest);
FileExtractor.ExtractAll(src, Path.Combine(utils.ModsFolder, mod_name));
FileExtractor.ExtractAll(src, Path.Combine(utils.ModsFolder, mod_name)).Wait();
File.WriteAllText(dest + ".meta", ini);
}

View File

@ -215,8 +215,8 @@ Global
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug (no commandargs)|x86.Build.0 = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x64.ActiveCfg = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x64.Build.0 = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x64.ActiveCfg = Debug|x64
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x64.Build.0 = Debug|x64
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x86.ActiveCfg = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Debug|x86.Build.0 = Debug|Any CPU
{9E69BC98-1512-4977-B683-6E7E5292C0B8}.Release|Any CPU.ActiveCfg = Release|Any CPU