2019-09-14 04:35:42 +00:00
|
|
|
|
using System;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-09-24 11:27:43 +00:00
|
|
|
|
using System.Linq;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
using System.Net.Http;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
using System.Reflection;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
using System.Security.Cryptography;
|
2019-12-04 01:26:26 +00:00
|
|
|
|
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;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
using ICSharpCode.SharpZipLib.GZip;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-23 16:14:34 +00:00
|
|
|
|
using OMODFramework;
|
2019-12-04 04:12:08 +00:00
|
|
|
|
using Wabbajack.Common.StatusFeed;
|
|
|
|
|
using Wabbajack.Common.StatusFeed.Errors;
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public class FileExtractor
|
|
|
|
|
{
|
2019-08-25 02:26:24 +00:00
|
|
|
|
static FileExtractor()
|
|
|
|
|
{
|
2019-08-25 23:52:03 +00:00
|
|
|
|
ExtractResource("Wabbajack.Common.7z.dll.gz", "7z.dll");
|
|
|
|
|
ExtractResource("Wabbajack.Common.7z.exe.gz", "7z.exe");
|
2019-12-01 13:57:28 +00:00
|
|
|
|
ExtractResource("Wabbajack.Common.innounp.exe.gz", "innounp.exe");
|
2019-08-25 23:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ExtractResource(string from, string to)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(to))
|
|
|
|
|
File.Delete(to);
|
|
|
|
|
|
|
|
|
|
using (var ous = File.OpenWrite(to))
|
|
|
|
|
using (var ins = new GZipInputStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(from)))
|
|
|
|
|
{
|
|
|
|
|
ins.CopyTo(ous);
|
|
|
|
|
}
|
2019-08-25 02:26:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
public static async Task ExtractAll(WorkQueue queue, string source, string dest)
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-09-08 22:44:15 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-10-11 23:31:36 +00:00
|
|
|
|
if (Consts.SupportedBSAs.Any(b => source.ToLower().EndsWith(b)))
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await ExtractAllWithBSA(queue, source, dest);
|
2019-09-24 16:03:50 +00:00
|
|
|
|
else if (source.EndsWith(".omod"))
|
2019-11-16 00:01:37 +00:00
|
|
|
|
ExtractAllWithOMOD(source, dest);
|
2019-12-01 13:57:28 +00:00
|
|
|
|
else if (source.EndsWith(".exe"))
|
|
|
|
|
ExtractAllWithInno(source, dest);
|
2019-09-08 22:44:15 +00:00
|
|
|
|
else
|
2019-12-05 05:26:15 +00:00
|
|
|
|
ExtractAllWith7Zip(source, dest);
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
2019-09-08 22:44:15 +00:00
|
|
|
|
catch (Exception ex)
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-12-05 05:26:15 +00:00
|
|
|
|
Utils.ErrorThrow(ex, $"Error while extracting {source}");
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-01 13:57:28 +00:00
|
|
|
|
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 e)
|
|
|
|
|
{
|
2019-12-05 05:07:44 +00:00
|
|
|
|
Utils.Error(e, "Error while setting process priority level for innounp.exe");
|
2019-12-01 13:57:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var name = Path.GetFileName(source);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (!p.HasExited)
|
|
|
|
|
{
|
|
|
|
|
var line = p.StandardOutput.ReadLine();
|
|
|
|
|
if (line == null)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
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 e)
|
|
|
|
|
{
|
2019-12-05 05:07:44 +00:00
|
|
|
|
Utils.Error(e, "Error while reading StandardOutput for innounp.exe");
|
2019-12-01 13:57:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
if (p.ExitCode == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Utils.Log(p.StandardOutput.ReadToEnd());
|
|
|
|
|
Utils.Log($"Extraction error extracting {source}");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
private static string ExtractAllWithOMOD(string source, string dest)
|
2019-09-24 16:03:50 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
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;
|
2019-09-24 16:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
private static async Task ExtractAllWithBSA(WorkQueue queue, string source, string dest)
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-09-12 23:44:35 +00:00
|
|
|
|
try
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
using (var arch = BSADispatch.OpenRead(source))
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await arch.Files
|
2019-11-17 04:16:42 +00:00
|
|
|
|
.PMap(queue, f =>
|
2019-11-16 00:01:37 +00:00
|
|
|
|
{
|
|
|
|
|
var path = f.Path;
|
|
|
|
|
if (f.Path.StartsWith("\\"))
|
|
|
|
|
path = f.Path.Substring(1);
|
|
|
|
|
Utils.Status($"Extracting {path}");
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var outPath = Path.Combine(dest, path);
|
|
|
|
|
var parent = Path.GetDirectoryName(outPath);
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
if (!Directory.Exists(parent))
|
|
|
|
|
Directory.CreateDirectory(parent);
|
2019-11-12 04:35:07 +00:00
|
|
|
|
|
2019-11-21 15:49:14 +00:00
|
|
|
|
using (var fs = File.OpenWrite(outPath))
|
2019-11-16 00:01:37 +00:00
|
|
|
|
{
|
|
|
|
|
f.CopyDataTo(fs);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-09-12 23:44:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-12-05 05:26:15 +00:00
|
|
|
|
Utils.ErrorThrow(ex, $"While Extracting {source}");
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 05:26:15 +00:00
|
|
|
|
private static void ExtractAllWith7Zip(string source, string dest)
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-12-05 05:26:15 +00:00
|
|
|
|
Utils.Log(new GenericInfo($"Extracting {Path.GetFileName(source)}", $"The contents of {source} are being extracted to {dest} using 7zip.exe"));
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-08-25 23:52:03 +00:00
|
|
|
|
var info = new ProcessStartInfo
|
2019-08-09 04:07:23 +00:00
|
|
|
|
{
|
2019-08-25 23:52:03 +00:00
|
|
|
|
FileName = "7z.exe",
|
2019-08-29 22:49:48 +00:00
|
|
|
|
Arguments = $"x -bsp1 -y -o\"{dest}\" \"{source}\"",
|
2019-08-25 23:52:03 +00:00
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
2019-09-14 04:35:42 +00:00
|
|
|
|
CreateNoWindow = true
|
2019-08-25 23:52:03 +00:00
|
|
|
|
};
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-12-01 13:57:28 +00:00
|
|
|
|
var p = new Process {StartInfo = info};
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-08-25 23:52:03 +00:00
|
|
|
|
p.Start();
|
2019-08-26 23:02:49 +00:00
|
|
|
|
ChildProcessTracker.AddProcess(p);
|
2019-08-28 03:22:57 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
p.PriorityClass = ProcessPriorityClass.BelowNormal;
|
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-08-09 04:07:23 +00:00
|
|
|
|
|
2019-08-29 22:49:48 +00:00
|
|
|
|
var name = Path.GetFileName(source);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (!p.HasExited)
|
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var line = p.StandardOutput.ReadLine();
|
2019-09-24 11:27:43 +00:00
|
|
|
|
if (line == null)
|
|
|
|
|
break;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
|
2019-11-12 05:33:32 +00:00
|
|
|
|
if (line.Length <= 4 || line[3] != '%') continue;
|
2019-12-01 13:57:28 +00:00
|
|
|
|
|
2019-11-12 05:33:32 +00:00
|
|
|
|
int.TryParse(line.Substring(0, 3), out var percent);
|
|
|
|
|
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
|
2019-08-29 22:49:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-16 21:36:14 +00:00
|
|
|
|
catch (Exception)
|
2019-08-29 22:49:48 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:52:03 +00:00
|
|
|
|
p.WaitForExit();
|
2019-12-04 04:12:08 +00:00
|
|
|
|
if (p.ExitCode == 0)
|
2019-08-25 23:52:03 +00:00
|
|
|
|
{
|
2019-12-04 04:12:08 +00:00
|
|
|
|
return;
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
2019-12-05 05:26:15 +00:00
|
|
|
|
Utils.Log(new _7zipReturnError(p.ExitCode, source, dest, p.StandardOutput.ReadToEnd()));
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 04:30:37 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Returns true if the given extension type can be extracted
|
2019-08-15 04:30:37 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="v"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool CanExtract(string v)
|
|
|
|
|
{
|
2019-12-01 13:57:28 +00:00
|
|
|
|
var ext = Path.GetExtension(v.ToLower());
|
|
|
|
|
if(ext != ".exe")
|
|
|
|
|
return Consts.SupportedArchives.Contains(ext) || Consts.SupportedBSAs.Contains(ext);
|
|
|
|
|
|
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "innounp.exe",
|
|
|
|
|
Arguments = $"-t \"{v}\" ",
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var p = new Process {StartInfo = info};
|
|
|
|
|
|
|
|
|
|
p.Start();
|
|
|
|
|
ChildProcessTracker.AddProcess(p);
|
|
|
|
|
|
|
|
|
|
var name = Path.GetFileName(v);
|
|
|
|
|
while (!p.HasExited)
|
|
|
|
|
{
|
|
|
|
|
var line = p.StandardOutput.ReadLine();
|
|
|
|
|
if (line == null)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (line[0] != '#')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Utils.Status($"Testing {name} - {line.Trim()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
return p.ExitCode == 0;
|
2019-08-15 04:30:37 +00:00
|
|
|
|
}
|
2019-08-09 04:07:23 +00:00
|
|
|
|
}
|
2019-12-01 13:57:28 +00:00
|
|
|
|
}
|