mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
Removed old VFS files
This commit is contained in:
parent
046907499b
commit
71862beb86
@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
|
||||
</startup>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
|
||||
</configuration>
|
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using Wabbajack.Common;
|
||||
using Microsoft.Win32;
|
||||
using System.Reactive;
|
||||
|
||||
namespace VirtualFileSystem.Test
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var result = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\7-zip\");
|
||||
|
||||
Utils.LogMessages.Subscribe(s => Console.WriteLine(s));
|
||||
Utils.StatusUpdates.Subscribe((i) => Console.Write(i.Message + "\r"));
|
||||
VFS.VirtualFileSystem.VFS.AddRoot(@"D:\tmp\archivetests");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("VirtualFileSystem.Test")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("VirtualFileSystem.Test")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2019")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("a2913dfe-18ff-468b-a6c1-55f7c0cc0ce8")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
@ -1,106 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{A2913DFE-18FF-468B-A6C1-55F7C0CC0CE8}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>VirtualFileSystem.Test</RootNamespace>
|
||||
<AssemblyName>VirtualFileSystem.Test</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<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>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>true</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>true</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>true</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>true</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Wabbajack.Common\Wabbajack.Common.csproj">
|
||||
<Project>{B3F3FB6E-B9EB-4F49-9875-D78578BC7AE5}</Project>
|
||||
<Name>Wabbajack.Common</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Reactive">
|
||||
<Version>4.2.0</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
@ -1,35 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("VirtualFileSystem")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("VirtualFileSystem")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2019")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("5128b489-bc28-4f66-9f0b-b4565af36cbc")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
@ -1,752 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Ceras;
|
||||
using Compression.BSA;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Newtonsoft.Json;
|
||||
using Wabbajack.Common;
|
||||
using Directory = Alphaleonis.Win32.Filesystem.Directory;
|
||||
using File = Alphaleonis.Win32.Filesystem.File;
|
||||
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
|
||||
using Path = Alphaleonis.Win32.Filesystem.Path;
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
public class VirtualFileSystem
|
||||
{
|
||||
public const ulong FileVersion = 0x01;
|
||||
public const string Magic = "WABBAJACK VFS FILE";
|
||||
|
||||
internal static string _stagedRoot;
|
||||
public static VirtualFileSystem VFS;
|
||||
private bool _disableDiskCache;
|
||||
private Dictionary<string, VirtualFile> _files = new Dictionary<string, VirtualFile>();
|
||||
private volatile bool _isSyncing;
|
||||
|
||||
static VirtualFileSystem()
|
||||
{
|
||||
VFS = new VirtualFileSystem();
|
||||
var entry = Assembly.GetEntryAssembly();
|
||||
if (entry != null && !string.IsNullOrEmpty(entry.Location))
|
||||
{
|
||||
RootFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||
_stagedRoot = Path.Combine(RootFolder, "vfs_staged_files");
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reconfigure(string root)
|
||||
{
|
||||
RootFolder = root;
|
||||
if (RootFolder != null)
|
||||
_stagedRoot = Path.Combine(RootFolder, "vfs_staged_files");
|
||||
else
|
||||
_stagedRoot = "vfs_staged_files";
|
||||
}
|
||||
|
||||
public static void Clean()
|
||||
{
|
||||
if (Directory.Exists(_stagedRoot))
|
||||
{
|
||||
Directory.EnumerateDirectories(_stagedRoot)
|
||||
.PMap(f => DeleteDirectory(f));
|
||||
DeleteDirectory(_stagedRoot);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(_stagedRoot);
|
||||
}
|
||||
|
||||
public VirtualFileSystem()
|
||||
{
|
||||
LoadFromDisk();
|
||||
}
|
||||
|
||||
public static string RootFolder { get; private set; }
|
||||
public Dictionary<string, IEnumerable<VirtualFile>> HashIndex { get; private set; }
|
||||
|
||||
public VirtualFile this[string path] => Lookup(path);
|
||||
|
||||
public static void DeleteDirectory(string path)
|
||||
{
|
||||
var info = new ProcessStartInfo
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $"/c del /f /q /s \"{path}\" && rmdir /q /s \"{path}\" ",
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
while (!p.HasExited)
|
||||
{
|
||||
var line = p.StandardOutput.ReadLine();
|
||||
if (line == null) break;
|
||||
Utils.Status(line);
|
||||
}
|
||||
p.WaitForExit();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
LoadFromDisk();
|
||||
}
|
||||
|
||||
private void LoadFromDisk()
|
||||
{
|
||||
try
|
||||
{
|
||||
HashIndex = new Dictionary<string, IEnumerable<VirtualFile>>();
|
||||
Utils.Log("Loading VFS Cache");
|
||||
if (!File.Exists("vfs_cache.bin")) return;
|
||||
_files = new Dictionary<string, VirtualFile>();
|
||||
|
||||
try
|
||||
{
|
||||
using (var fs = File.OpenRead("vfs_cache.bin"))
|
||||
using (var br = new BinaryReader(fs))
|
||||
{
|
||||
var magic = Encoding.ASCII.GetString(br.ReadBytes(Magic.Length));
|
||||
if (magic != Magic || br.ReadUInt64() != FileVersion)
|
||||
{
|
||||
fs.Close();
|
||||
File.Delete("vfs_cache.bin");
|
||||
return;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var fr = VirtualFile.Read(br);
|
||||
_files.Add(fr.FullPath, fr);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
}
|
||||
|
||||
CleanDB();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Utils.Log($"Purging cache due to {ex}");
|
||||
File.Delete("vfs_cache.bson");
|
||||
_files.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void SyncToDisk()
|
||||
{
|
||||
if (!_disableDiskCache)
|
||||
{
|
||||
Utils.Status("Syncing VFS Cache");
|
||||
lock (this)
|
||||
{
|
||||
try
|
||||
{
|
||||
_isSyncing = true;
|
||||
|
||||
if (File.Exists("vfs_cache.bin_new"))
|
||||
File.Delete("vfs_cache.bin_new");
|
||||
|
||||
using (var fs = File.OpenWrite("vfs_cache.bin_new"))
|
||||
using (var bw = new BinaryWriter(fs))
|
||||
{
|
||||
bw.Write(Encoding.ASCII.GetBytes(Magic));
|
||||
bw.Write(FileVersion);
|
||||
|
||||
Utils.Log($"Syncing VFS to Disk: {_files.Count} entries");
|
||||
foreach (var f in _files.Values) f.Write(bw);
|
||||
}
|
||||
|
||||
if (File.Exists("vfs_cache.bin"))
|
||||
File.Delete("vfs_cache.bin");
|
||||
|
||||
File.Move("vfs_cache.bin_new", "vfs_cache.bin");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSyncing = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IList<VirtualFile> FilesInArchive(VirtualFile f)
|
||||
{
|
||||
var path = f.FullPath + "|";
|
||||
return _files.Values
|
||||
.Where(v => v.FullPath.StartsWith(path))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
||||
public void Purge(VirtualFile f)
|
||||
{
|
||||
var path = f.FullPath + "|";
|
||||
lock (this)
|
||||
{
|
||||
_files.Values
|
||||
.Where(v => v.FullPath.StartsWith(path) || v.FullPath == f.FullPath)
|
||||
.ToList()
|
||||
.Do(r =>
|
||||
{
|
||||
_files.Remove(r.FullPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(VirtualFile f)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (_files.ContainsKey(f.FullPath))
|
||||
Purge(f);
|
||||
_files.Add(f.FullPath, f);
|
||||
}
|
||||
}
|
||||
|
||||
public VirtualFile Lookup(string f)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (_files.TryGetValue(f, out var found))
|
||||
return found;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove any orphaned files in the DB.
|
||||
/// </summary>
|
||||
private void CleanDB()
|
||||
{
|
||||
Utils.Log("Cleaning VFS cache");
|
||||
lock (this)
|
||||
{
|
||||
_files.Values
|
||||
.Where(f =>
|
||||
{
|
||||
if (f.IsConcrete)
|
||||
return !File.Exists(f.StagedPath);
|
||||
if (f.Hash == null)
|
||||
return true;
|
||||
while (f.ParentPath != null)
|
||||
{
|
||||
if (Lookup(f.ParentPath) == null)
|
||||
return true;
|
||||
if (f.Hash == null)
|
||||
return true;
|
||||
f = Lookup(f.ParentPath);
|
||||
}
|
||||
|
||||
return false;
|
||||
})
|
||||
.ToList()
|
||||
.Do(f =>
|
||||
{
|
||||
_files.Remove(f.FullPath);
|
||||
});
|
||||
SyncToDisk();
|
||||
}
|
||||
}
|
||||
|
||||
public void BackfillMissing()
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
_files.Values
|
||||
.Select(f => f.ParentPath)
|
||||
.Where(s => s != null)
|
||||
.Where(s => !_files.ContainsKey(s))
|
||||
.ToHashSet()
|
||||
.Do(s => { AddKnown(new VirtualFile {Paths = s.Split('|')}); });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a known file to the index, bit of a hack as we won't assume that all the fields for the archive are filled in.
|
||||
/// you will need to manually update the SHA hash when you are done adding files, by calling `RefreshIndexes`
|
||||
/// </summary>
|
||||
/// <param name="virtualFile"></param>
|
||||
public void AddKnown(VirtualFile virtualFile)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
// We don't know enough about these files to be able to store them in the disk cache
|
||||
_disableDiskCache = true;
|
||||
_files[virtualFile.FullPath] = virtualFile;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the root path to the filesystem. This may take quite some time as every file in the folder will be hashed,
|
||||
/// and every archive examined.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
public void AddRoot(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) return;
|
||||
IndexPath(path);
|
||||
RefreshIndexes();
|
||||
}
|
||||
|
||||
public void RefreshIndexes()
|
||||
{
|
||||
Utils.Log("Building Hash Index");
|
||||
lock (this)
|
||||
{
|
||||
HashIndex = _files.Values
|
||||
.GroupBy(f => f.Hash)
|
||||
.Where(f => f.Key != null)
|
||||
.ToDictionary(f => f.Key, f => (IEnumerable<VirtualFile>) f);
|
||||
}
|
||||
}
|
||||
|
||||
private void IndexPath(string path)
|
||||
{
|
||||
var file_list = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).ToList();
|
||||
Utils.Log($"Updating the cache for {file_list.Count} files");
|
||||
file_list.PMap(f => UpdateFile(f));
|
||||
SyncToDisk();
|
||||
}
|
||||
|
||||
private void UpdateFile(string f)
|
||||
{
|
||||
TOP:
|
||||
var lv = Lookup(f);
|
||||
if (lv == null)
|
||||
{
|
||||
Utils.Status($"Analyzing {f}");
|
||||
|
||||
lv = new VirtualFile
|
||||
{
|
||||
Paths = new[] {f}
|
||||
};
|
||||
|
||||
lv.Analyze();
|
||||
Add(lv);
|
||||
if (lv.IsArchive)
|
||||
{
|
||||
UpdateArchive(lv);
|
||||
// Upsert after extraction incase extraction fails
|
||||
lv.FinishedIndexing = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (lv.IsOutdated)
|
||||
{
|
||||
Purge(lv);
|
||||
goto TOP;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateArchive(VirtualFile f)
|
||||
{
|
||||
if (!f.IsStaged)
|
||||
throw new InvalidDataException("Can't analyze an unstaged file");
|
||||
|
||||
var tmp_dir = Path.Combine(_stagedRoot, Guid.NewGuid().ToString());
|
||||
Utils.Status($"Extracting Archive {Path.GetFileName(f.StagedPath)}");
|
||||
|
||||
FileExtractor.ExtractAll(f.StagedPath, tmp_dir).Wait();
|
||||
|
||||
|
||||
Utils.Status($"Updating Archive {Path.GetFileName(f.StagedPath)}");
|
||||
|
||||
var entries = Directory.EnumerateFiles(tmp_dir, "*", SearchOption.AllDirectories)
|
||||
.Select(path => path.RelativeTo(tmp_dir));
|
||||
|
||||
var new_files = entries.Select(e =>
|
||||
{
|
||||
var new_path = new string[f.Paths.Length + 1];
|
||||
f.Paths.CopyTo(new_path, 0);
|
||||
new_path[f.Paths.Length] = e;
|
||||
var nf = new VirtualFile
|
||||
{
|
||||
Paths = new_path
|
||||
};
|
||||
nf._stagedPath = Path.Combine(tmp_dir, e);
|
||||
Add(nf);
|
||||
return nf;
|
||||
}).ToList();
|
||||
|
||||
// Analyze them
|
||||
new_files.PMap(file =>
|
||||
{
|
||||
Utils.Status($"Analyzing {Path.GetFileName(file.StagedPath)}");
|
||||
file.Analyze();
|
||||
});
|
||||
// Recurse into any archives in this archive
|
||||
new_files.Where(file => file.IsArchive).Do(file => UpdateArchive(file));
|
||||
|
||||
f.FinishedIndexing = true;
|
||||
|
||||
if (!_isSyncing)
|
||||
SyncToDisk();
|
||||
|
||||
Utils.Status("Cleaning Directory");
|
||||
DeleteDirectory(tmp_dir);
|
||||
}
|
||||
|
||||
public Action Stage(IEnumerable<VirtualFile> files)
|
||||
{
|
||||
var grouped = files.SelectMany(f => f.FilesInPath)
|
||||
.Distinct()
|
||||
.Where(f => f.ParentArchive != null)
|
||||
.GroupBy(f => f.ParentArchive)
|
||||
.OrderBy(f => f.Key == null ? 0 : f.Key.Paths.Length)
|
||||
.ToList();
|
||||
|
||||
var Paths = new List<string>();
|
||||
|
||||
foreach (var group in grouped)
|
||||
{
|
||||
var tmp_path = Path.Combine(_stagedRoot, Guid.NewGuid().ToString());
|
||||
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]);
|
||||
}
|
||||
|
||||
return () =>
|
||||
{
|
||||
Paths.Do(p =>
|
||||
{
|
||||
if (Directory.Exists(p)) DeleteDirectory(p);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public StagingGroup StageWith(IEnumerable<VirtualFile> files)
|
||||
{
|
||||
var grp = new StagingGroup(files);
|
||||
grp.Stage();
|
||||
return grp;
|
||||
}
|
||||
|
||||
internal List<string> GetArchiveEntryNames(VirtualFile file)
|
||||
{
|
||||
if (!file.IsStaged)
|
||||
throw new InvalidDataException("File is not staged");
|
||||
|
||||
if (file.Extension == ".bsa")
|
||||
using (var ar = new BSAReader(file.StagedPath))
|
||||
{
|
||||
return ar.Files.Select(f => f.Path).ToList();
|
||||
}
|
||||
|
||||
if (file.Extension == ".zip")
|
||||
using (var s = new ZipFile(File.OpenRead(file.StagedPath)))
|
||||
{
|
||||
s.IsStreamOwner = true;
|
||||
s.UseZip64 = UseZip64.On;
|
||||
|
||||
if (s.OfType<ZipEntry>().FirstOrDefault(e => !e.CanDecompress) == null)
|
||||
return s.OfType<ZipEntry>()
|
||||
.Where(f => f.IsFile)
|
||||
.Select(f => f.Name.Replace('/', '\\'))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/*
|
||||
using (var e = new ArchiveFile(file.StagedPath))
|
||||
{
|
||||
return e.Entries
|
||||
.Where(f => !f.IsFolder)
|
||||
.Select(f => f.FileName).ToList();
|
||||
}*/
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given a path that starts with a HASH, return the Virtual file referenced
|
||||
/// </summary>
|
||||
/// <param name="archiveHashPath"></param>
|
||||
/// <returns></returns>
|
||||
public VirtualFile FileForArchiveHashPath(string[] archiveHashPath)
|
||||
{
|
||||
if (archiveHashPath.Length == 1)
|
||||
return HashIndex[archiveHashPath[0]].First();
|
||||
|
||||
var archive = HashIndex[archiveHashPath[0]].Where(a => a.IsArchive).OrderByDescending(a => a.LastModified)
|
||||
.First();
|
||||
var fullPath = archive.FullPath + "|" + string.Join("|", archiveHashPath.Skip(1));
|
||||
return Lookup(fullPath);
|
||||
}
|
||||
|
||||
public IDictionary<VirtualFile, IEnumerable<VirtualFile>> GroupedByArchive()
|
||||
{
|
||||
return _files.Values
|
||||
.Where(f => f.TopLevelArchive != null)
|
||||
.GroupBy(f => f.TopLevelArchive)
|
||||
.ToDictionary(f => f.Key, f => (IEnumerable<VirtualFile>) f);
|
||||
}
|
||||
}
|
||||
|
||||
public class StagingGroup : List<VirtualFile>, IDisposable
|
||||
{
|
||||
public StagingGroup(IEnumerable<VirtualFile> files) : base(files)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.Do(f => f.Unstage());
|
||||
}
|
||||
|
||||
internal void Stage()
|
||||
{
|
||||
VirtualFileSystem.VFS.Stage(this);
|
||||
}
|
||||
}
|
||||
|
||||
[MemberConfig(TargetMember.None)]
|
||||
public class VirtualFile
|
||||
{
|
||||
private string _fullPath;
|
||||
|
||||
private bool? _isArchive;
|
||||
|
||||
private string _parentPath;
|
||||
public string[] _paths;
|
||||
|
||||
internal string _stagedPath;
|
||||
|
||||
[Include]
|
||||
public string[] Paths
|
||||
{
|
||||
get => _paths;
|
||||
set
|
||||
{
|
||||
for (var idx = 0; idx < value.Length; idx += 1)
|
||||
value[idx] = string.Intern(value[idx]);
|
||||
_paths = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Include] public string Hash { get; set; }
|
||||
|
||||
[Include] public long Size { get; set; }
|
||||
|
||||
[Include] public ulong LastModified { get; set; }
|
||||
|
||||
[Include]
|
||||
public bool? FinishedIndexing { get; set; }
|
||||
|
||||
|
||||
public string FullPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_fullPath != null) return _fullPath;
|
||||
_fullPath = string.Join("|", Paths);
|
||||
return _fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
public string Extension => Path.GetExtension(Paths.Last());
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// If this file is in an archive, return the Archive File, otherwise return null.
|
||||
/// </summary>
|
||||
public VirtualFile TopLevelArchive
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Paths.Length == 0) return null;
|
||||
return VirtualFileSystem.VFS[Paths[0]];
|
||||
}
|
||||
}
|
||||
|
||||
public VirtualFile ParentArchive
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ParentPath == null) return null;
|
||||
return VirtualFileSystem.VFS.Lookup(ParentPath);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsArchive
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_isArchive == null)
|
||||
_isArchive = FileExtractor.CanExtract(Extension);
|
||||
return (bool) _isArchive;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsStaged
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsConcrete) return true;
|
||||
return _stagedPath != null;
|
||||
}
|
||||
}
|
||||
|
||||
public string StagedPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsStaged)
|
||||
throw new InvalidDataException("File is not staged");
|
||||
if (IsConcrete) return Paths[0];
|
||||
return _stagedPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (IsStaged && value != null)
|
||||
throw new InvalidDataException("Can't change the path of a already staged file");
|
||||
_stagedPath = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this file always exists on-disk, and doesn't need to be staged.
|
||||
/// </summary>
|
||||
public bool IsConcrete => Paths.Length == 1;
|
||||
|
||||
public bool IsOutdated
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsStaged)
|
||||
{
|
||||
var fi = new FileInfo(StagedPath);
|
||||
if (fi.LastWriteTime.ToMilliseconds() != LastModified || fi.Length != Size)
|
||||
return true;
|
||||
if (IsArchive)
|
||||
if (!FinishedIndexing ?? true)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string ParentPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_parentPath == null && !IsConcrete)
|
||||
_parentPath = string.Join("|", Paths.Take(Paths.Length - 1));
|
||||
return _parentPath;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<VirtualFile> FileInArchive => VirtualFileSystem.VFS.FilesInArchive(this);
|
||||
|
||||
public IEnumerable<VirtualFile> FilesInPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return Enumerable.Range(1, Paths.Length)
|
||||
.Select(i => Paths.Take(i))
|
||||
.Select(path => VirtualFileSystem.VFS.Lookup(string.Join("|", path)));
|
||||
}
|
||||
}
|
||||
|
||||
public FileStream OpenRead()
|
||||
{
|
||||
if (!IsStaged)
|
||||
throw new InvalidDataException("File is not staged, cannot open");
|
||||
return File.OpenRead(_stagedPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the file's SHA, size and last modified
|
||||
/// </summary>
|
||||
internal void Analyze()
|
||||
{
|
||||
if (!IsStaged)
|
||||
throw new InvalidDataException("Cannot analyze an unstaged file");
|
||||
|
||||
var fio = new FileInfo(StagedPath);
|
||||
Size = fio.Length;
|
||||
Hash = StagedPath.FileHash();
|
||||
LastModified = fio.LastWriteTime.ToMilliseconds();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Delete the temoporary file associated with this file
|
||||
/// </summary>
|
||||
internal void Unstage()
|
||||
{
|
||||
if (IsStaged && !IsConcrete)
|
||||
{
|
||||
File.Delete(_stagedPath);
|
||||
_stagedPath = null;
|
||||
}
|
||||
}
|
||||
|
||||
internal string GenerateStagedName()
|
||||
{
|
||||
if (_stagedPath != null) return _stagedPath;
|
||||
_stagedPath = Path.Combine(VirtualFileSystem._stagedRoot, Guid.NewGuid() + Path.GetExtension(Paths.Last()));
|
||||
return _stagedPath;
|
||||
}
|
||||
|
||||
public string[] MakeRelativePaths()
|
||||
{
|
||||
var path_copy = (string[]) Paths.Clone();
|
||||
path_copy[0] = VirtualFileSystem.VFS.Lookup(Paths[0]).Hash;
|
||||
return path_copy;
|
||||
}
|
||||
|
||||
public void Write(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(FullPath);
|
||||
bw.Write(Hash ?? "");
|
||||
bw.Write(Size);
|
||||
bw.Write(LastModified);
|
||||
bw.Write(FinishedIndexing ?? false);
|
||||
}
|
||||
|
||||
public static VirtualFile Read(BinaryReader rdr)
|
||||
{
|
||||
var vf = new VirtualFile();
|
||||
var full_path = rdr.ReadString();
|
||||
vf.Paths = full_path.Split('|');
|
||||
|
||||
for (var x = 0; x < vf.Paths.Length; x++)
|
||||
vf.Paths[x] = string.Intern(vf.Paths[x]);
|
||||
|
||||
vf._fullPath = full_path;
|
||||
vf.Hash = rdr.ReadString();
|
||||
if (vf.Hash == "") vf.Hash = null;
|
||||
vf.Size = rdr.ReadInt64();
|
||||
vf.LastModified = rdr.ReadUInt64();
|
||||
vf.FinishedIndexing = rdr.ReadBoolean();
|
||||
if (vf.FinishedIndexing == false) vf.FinishedIndexing = null;
|
||||
return vf;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{5128B489-BC28-4F66-9F0B-B4565AF36CBC}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>VirtualFileSystem</RootNamespace>
|
||||
<AssemblyName>VirtualFileSystem</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<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>
|
||||
<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>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Transactions" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="VirtualFileSystem.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Compression.BSA\Compression.BSA.csproj">
|
||||
<Project>{ff5d892f-8ff4-44fc-8f7f-cd58f307ad1b}</Project>
|
||||
<Name>Compression.BSA</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wabbajack.Common\Wabbajack.Common.csproj">
|
||||
<Project>{b3f3fb6e-b9eb-4f49-9875-d78578bc7ae5}</Project>
|
||||
<Name>Wabbajack.Common</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AlphaFS">
|
||||
<Version>2.2.6</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Ceras">
|
||||
<Version>4.1.7</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json">
|
||||
<Version>12.0.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="SharpZipLib">
|
||||
<Version>1.2.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Collections.Immutable">
|
||||
<Version>1.6.0</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
Loading…
Reference in New Issue
Block a user