Skip to content

Commit

Permalink
bunch of fixes to file extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Aug 29, 2019
1 parent 5ddbd83 commit b72a64f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
59 changes: 39 additions & 20 deletions VirtualFileSystem/VirtualFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using Alphaleonis.Win32.Filesystem;
using Wabbajack.Common;
using Directory = Alphaleonis.Win32.Filesystem.Directory;
using DirectoryInfo = Alphaleonis.Win32.Filesystem.DirectoryInfo;
using File = Alphaleonis.Win32.Filesystem.File;
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
using Path = Alphaleonis.Win32.Filesystem.Path;
Expand All @@ -32,33 +35,49 @@ static VirtualFileSystem()
VFS = new VirtualFileSystem();
RootFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
_stagedRoot = Path.Combine(RootFolder, "vfs_staged_files");
if (Directory.Exists(_stagedRoot))
DeleteDirectory(_stagedRoot);
Utils.OnQueue(() =>
{
if (Directory.Exists(_stagedRoot))
DeleteDirectory(_stagedRoot);
});

Directory.CreateDirectory(_stagedRoot);
}

private static void DeleteDirectory(string path)
{
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
{
File.SetAttributes(file, FileAttributes.Normal);
try
Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.DoProgress("Cleaning VFS Files", file =>
{
File.Delete(file);
}
catch (Exception ex)
try
{
var fi = new FileInfo(file);
fi.Attributes &= ~FileAttributes.ReadOnly;
File.Delete(file);
}
catch (Exception ex)
{
Utils.Log(ex.ToString());
}
});

Directory.EnumerateDirectories(path, DirectoryEnumerationOptions.Recursive)
.DoProgress("Cleaning VFS Folders", folder =>
{
Utils.Log(ex.ToString());
}
}
try {
Directory.Delete(path, true);
}
catch (Exception ex)
{
Utils.Log(ex.ToString());
}
try
{
if (!Directory.Exists(folder))
return;
var di = new DirectoryInfo(folder);
di.Attributes &= ~FileAttributes.ReadOnly;
Directory.Delete(path, true);
}
catch (Exception ex)
{
Utils.Log(ex.ToString());
}
});


}

Expand Down Expand Up @@ -365,7 +384,7 @@ public Action Stage(IEnumerable<VirtualFile> files)
{
var tmp_path = Path.Combine(_stagedRoot, Guid.NewGuid().ToString());
FileExtractor.ExtractAll(group.Key.StagedPath, tmp_path);

Paths.Add(tmp_path);
foreach (var file in group)
file._stagedPath = Path.Combine(tmp_path, file.Paths[group.Key.Paths.Length]);

Expand Down
25 changes: 23 additions & 2 deletions Wabbajack.Common/FileExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void ExtractAllWith7Zip(string source, string dest)
var info = new ProcessStartInfo
{
FileName = "7z.exe",
Arguments = $"x -o\"{dest}\" \"{source}\"",
Arguments = $"x -bsp1 -y -o\"{dest}\" \"{source}\"",
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
Expand All @@ -104,6 +104,28 @@ private static void ExtractAllWith7Zip(string source, string dest)
}
catch (Exception) { }

var name = Path.GetFileName(source);
try
{
while (!p.HasExited)
{
var line = p.StandardOutput.ReadLine();
if (line == null)
break;
int percent = 0;
if (line.Length > 4 && line[3] == '%')
{
Int32.TryParse(line.Substring(0, 3), out percent);
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
}

}
}
catch (Exception ex)
{

}

p.WaitForExit();
if (p.ExitCode != 0)
{
Expand All @@ -113,7 +135,6 @@ private static void ExtractAllWith7Zip(string source, string dest)

}


/// <summary>
/// Returns true if the given extension type can be extracted
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions Wabbajack.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Configuration;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using File = Alphaleonis.Win32.Filesystem.File;
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
using Path = Alphaleonis.Win32.Filesystem.Path;

namespace Wabbajack.Common
{
Expand Down Expand Up @@ -301,6 +305,21 @@ public static void PMap<TI>(this IEnumerable<TI> coll, Action<TI> f)
return;
}

public static void DoProgress<T>(this IEnumerable<T> coll, String msg, Action<T> f)
{
var lst = coll.ToList();
lst.DoIndexed((idx, i) =>
{
Status(msg, idx * 100 / lst.Count);
f(i);
});
}

public static void OnQueue(Action f)
{
new List<bool>().Do(_ => f());
}

public static HttpResponseMessage GetSync(this HttpClient client, string url)
{
var result = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
Expand Down
2 changes: 1 addition & 1 deletion Wabbajack.Common/WorkQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Wabbajack.Common
{
public class WorkQueue
{
internal static BlockingCollection<Action> Queue = new BlockingCollection<Action>();
internal static BlockingCollection<Action> Queue = new BlockingCollection<Action>(new ConcurrentStack<Action>());

[ThreadStatic]
private static int CpuId;
Expand Down

0 comments on commit b72a64f

Please sign in to comment.