wabbajack/Wabbajack.VirtualFileSystem/FileExtractor.cs

239 lines
8.1 KiB
C#
Raw Normal View History

2019-12-26 16:47:10 +00:00
using System;
using System.Collections.Generic;
2019-08-25 23:52:03 +00:00
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
2019-08-28 03:22:57 +00:00
using Alphaleonis.Win32.Filesystem;
2019-09-14 04:35:42 +00:00
using Compression.BSA;
using OMODFramework;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed;
using Wabbajack.Common.StatusFeed.Errors;
using Wabbajack.Common;
using Utils = Wabbajack.Common.Utils;
namespace Wabbajack.VirtualFileSystem
{
public class FileExtractor
{
public static async Task<ExtractedFiles> ExtractAll(WorkQueue queue, AbsolutePath source, IEnumerable<RelativePath> OnlyFiles = null)
{
2019-09-08 22:44:15 +00:00
try
{
if (await BSADispatch.MightBeBSA(source))
return await ExtractAllWithBSA(queue, source);
2020-03-23 12:57:18 +00:00
else if (source.Extension == Consts.OMOD)
return await ExtractAllWithOMOD(source);
2020-03-23 12:57:18 +00:00
else if (source.Extension == Consts.EXE)
return await ExtractAllExe(source);
2019-09-08 22:44:15 +00:00
else
return await ExtractAllWith7Zip(source, OnlyFiles);
}
2019-09-08 22:44:15 +00:00
catch (Exception ex)
{
2019-12-05 05:26:15 +00:00
Utils.ErrorThrow(ex, $"Error while extracting {source}");
throw new Exception();
}
}
private static async Task<ExtractedFiles> ExtractAllExe(AbsolutePath source)
{
var isArchive = await TestWith7z(source);
if (isArchive)
{
return await ExtractAllWith7Zip(source, null);
}
var dest = await TempFolder.Create();
2020-03-23 12:57:18 +00:00
Utils.Log($"Extracting {(string)source.FileName}");
var process = new ProcessHelper
{
Path = @"Extractors\innounp.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"-x", "-y", "-b", $"-d\"{dest.Dir}\"", source}
};
var result = process.Output.Where(d => d.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(p =>
{
var (_, line) = p;
if (line == null)
return;
if (line.Length <= 4 || line[3] != '%')
return;
2020-02-08 04:35:08 +00:00
int.TryParse(line.Substring(0, 3), out var percentInt);
Utils.Status($"Extracting {source.FileName} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
});
await process.Start();
return new ExtractedFiles(dest);
}
private class OMODProgress : ICodeProgress
{
private long _total;
public void SetProgress(long inSize, long outSize)
{
Utils.Status("Extracting OMOD", Percent.FactoryPutInRange(inSize, _total));
}
public void Init(long totalSize, bool compressing)
{
_total = totalSize;
}
public void Dispose()
{
//
}
}
private static async Task<ExtractedFiles> ExtractAllWithOMOD(AbsolutePath source)
2019-09-24 16:03:50 +00:00
{
var dest = await TempFolder.Create();
2020-03-23 12:57:18 +00:00
Utils.Log($"Extracting {(string)source.FileName}");
Framework.Settings.TempPath = (string)dest.Dir;
Framework.Settings.CodeProgress = new OMODProgress();
2020-03-23 12:57:18 +00:00
var omod = new OMOD((string)source);
omod.GetDataFiles();
omod.GetPlugins();
return new ExtractedFiles(dest);
2019-09-24 16:03:50 +00:00
}
2019-12-26 16:47:10 +00:00
private static async Task<ExtractedFiles> ExtractAllWithBSA(WorkQueue queue, AbsolutePath source)
{
try
{
await using var arch = await BSADispatch.OpenRead(source);
var files = arch.Files.ToDictionary(f => f.Path, f => (IExtractedFile)new ExtractedBSAFile(f));
return new ExtractedFiles(files, arch);
}
catch (Exception ex)
{
2019-12-05 05:26:15 +00:00
Utils.ErrorThrow(ex, $"While Extracting {source}");
throw new Exception();
}
}
private static async Task<ExtractedFiles> ExtractAllWith7Zip(AbsolutePath source, IEnumerable<RelativePath> onlyFiles)
{
TempFile tmpFile = null;
var dest = await TempFolder.Create();
2020-03-23 12:57:18 +00:00
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 process = new ProcessHelper
{
Path = @"Extractors\7z.exe".RelativeTo(AbsolutePath.EntryPoint),
2019-08-25 23:52:03 +00:00
};
if (onlyFiles != null)
{
2020-04-17 13:06:28 +00:00
//It's stupid that we have to do this, but 7zip's file pattern matching isn't very fuzzy
IEnumerable<string> AllVariants(string input)
{
yield return $"\"{input}\"";
yield return $"\"\\{input}\"";
2020-04-17 13:06:28 +00:00
}
tmpFile = new TempFile();
2020-04-17 13:06:28 +00:00
await tmpFile.Path.WriteAllLinesAsync(onlyFiles.SelectMany(f => AllVariants((string)f)).ToArray());
process.Arguments = new object[]
{
"x", "-bsp1", "-y", $"-o\"{dest.Dir}\"", source, $"@\"{tmpFile.Path}\"", "-mmt=off"
};
}
else
{
process.Arguments = new object[] {"x", "-bsp1", "-y", $"-o\"{dest.Dir}\"", source, "-mmt=off"};
}
var result = process.Output.Where(d => d.Type == ProcessHelper.StreamType.Output)
.ForEachAsync(p =>
2019-08-29 22:49:48 +00:00
{
var (_, line) = p;
if (line == null)
return;
if (line.Length <= 4 || line[3] != '%') return;
2020-02-08 04:35:08 +00:00
int.TryParse(line.Substring(0, 3), out var percentInt);
Utils.Status($"Extracting {(string)source.FileName} - {line.Trim()}", Percent.FactoryPutInRange(percentInt / 100d));
});
2019-08-29 22:49:48 +00:00
var exitCode = await process.Start();
if (exitCode != 0)
2019-08-25 23:52:03 +00:00
{
Utils.Error(new _7zipReturnError(exitCode, source, dest.Dir, ""));
}
else
{
Utils.Status($"Extracting {source.FileName} - done", Percent.One, alsoLog: true);
}
if (tmpFile != null)
{
await tmpFile.DisposeAsync();
}
return new ExtractedFiles(dest);
}
/// <summary>
2019-09-14 04:35:42 +00:00
/// Returns true if the given extension type can be extracted
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static async Task<bool> CanExtract(AbsolutePath v)
{
2020-03-23 12:57:18 +00:00
var ext = v.Extension;
if(ext != _exeExtension && !Consts.TestArchivesBeforeExtraction.Contains(ext))
return Consts.SupportedArchives.Contains(ext) || Consts.SupportedBSAs.Contains(ext);
var isArchive = await TestWith7z(v);
2019-12-15 11:58:03 +00:00
if (isArchive)
return true;
2019-12-15 11:58:03 +00:00
var process = new ProcessHelper
{
Path = @"Extractors\innounp.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"-t", v},
};
2019-12-15 11:58:03 +00:00
return await process.Start() == 0;
}
public static async Task<bool> TestWith7z(AbsolutePath file)
{
var process = new ProcessHelper()
2019-12-15 11:58:03 +00:00
{
Path = @"Extractors\7z.exe".RelativeTo(AbsolutePath.EntryPoint),
Arguments = new object[] {"t", file},
};
return await process.Start() == 0;
}
2020-03-23 12:57:18 +00:00
private static Extension _exeExtension = new Extension(".exe");
public static bool MightBeArchive(Extension ext)
{
2020-03-23 12:57:18 +00:00
return ext == _exeExtension || Consts.SupportedArchives.Contains(ext) || Consts.SupportedBSAs.Contains(ext);
}
}
}