Merge pull request #748 from wabbajack-tools/fix-vfs-downloading

Fix serialization of IPaths
This commit is contained in:
Timothy Baldridge 2020-04-24 08:19:58 -06:00 committed by GitHub
commit 0ece811619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 21 deletions

View File

@ -9,6 +9,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Wabbajack.Common.Serialization.Json;
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;
namespace Wabbajack.Common
{
@ -23,6 +24,7 @@ namespace Wabbajack.Common
new FullPathConverter(),
new GameConverter(),
new PercentConverter(),
new IPathConverter(),
};
public static JsonSerializerSettings JsonSettings =>
@ -233,6 +235,28 @@ namespace Wabbajack.Common
return game.Game;
}
}
public class IPathConverter : JsonConverter<IPath>
{
public override void WriteJson(JsonWriter writer, IPath value, JsonSerializer serializer)
{
writer.WriteValue(Enum.GetName(typeof(Game), value));
}
public override IPath ReadJson(JsonReader reader, Type objectType, IPath existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
// Backwards compatibility support
var str = reader.Value?.ToString();
if (string.IsNullOrWhiteSpace(str)) return default(RelativePath);
if (Path.IsPathRooted(str))
return (AbsolutePath)str;
return (RelativePath)str;
}
}

View File

@ -250,7 +250,14 @@ namespace Wabbajack.VirtualFileSystem
public async Task BackfillMissing()
{
var newFiles = _knownArchives.ToDictionary(kv => kv.Key,
kv => new VirtualFile {Name = kv.Value, Size = kv.Value.Size, Hash = kv.Key});
kv => new VirtualFile
{
Name = kv.Value,
Size = kv.Value.Size,
Hash = kv.Key,
});
newFiles.Values.Do(f => f.FillFullPath(0));
var parentchild = new Dictionary<(VirtualFile, RelativePath), VirtualFile>();
@ -266,6 +273,7 @@ namespace Wabbajack.VirtualFileSystem
}
var nf = new VirtualFile {Name = path, Parent = parent};
nf.FillFullPath();
parent.Children = parent.Children.Add(nf);
parentchild.Add((parent, path), nf);
parent = nf;

View File

@ -153,6 +153,8 @@ namespace Wabbajack.VirtualFileSystem
LastAnalyzed = DateTime.Now.AsUnixTime(),
Hash = file.Hash
};
vself.FillFullPath();
vself.Children = file.Children.Select(f => Convert(f, f.Name, vself)).ToImmutableList();
@ -192,11 +194,24 @@ namespace Wabbajack.VirtualFileSystem
return self;
}
private void FillFullPath(in int depth)
internal void FillFullPath()
{
int depth = 0;
var self = this;
while (self.Parent != null)
{
depth += 1;
self = self.Parent;
}
FillFullPath(depth);
}
internal void FillFullPath(int depth)
{
if (depth == 0)
{
FullPath = new FullPath((AbsolutePath)Name, new RelativePath[0]);
FullPath = new FullPath((AbsolutePath)Name);
}
else
{
@ -333,27 +348,25 @@ namespace Wabbajack.VirtualFileSystem
public static ExtendedHashes FromFile(IExtractedFile file)
{
var hashes = new ExtendedHashes();
using (var stream = file.OpenRead())
using var stream = file.OpenRead();
hashes.SHA256 = System.Security.Cryptography.SHA256.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
hashes.SHA1 = System.Security.Cryptography.SHA1.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
hashes.MD5 = System.Security.Cryptography.MD5.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
var bytes = new byte[1024 * 8];
var crc = new Crc32();
while (true)
{
hashes.SHA256 = System.Security.Cryptography.SHA256.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
hashes.SHA1 = System.Security.Cryptography.SHA1.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
hashes.MD5 = System.Security.Cryptography.MD5.Create().ComputeHash(stream).ToHex();
stream.Position = 0;
var bytes = new byte[1024 * 8];
var crc = new Crc32();
while (true)
{
var read = stream.Read(bytes, 0, bytes.Length);
if (read == 0) break;
crc.Update(bytes, 0, read);
}
hashes.CRC = crc.DigestBytes().ToHex();
var read = stream.Read(bytes, 0, bytes.Length);
if (read == 0) break;
crc.Update(bytes, 0, read);
}
hashes.CRC = crc.DigestBytes().ToHex();
return hashes;
}
}