diff --git a/Wabbajack.Common/Json.cs b/Wabbajack.Common/Json.cs index a6600108..c4708842 100644 --- a/Wabbajack.Common/Json.cs +++ b/Wabbajack.Common/Json.cs @@ -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 + { + 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; + + } + } diff --git a/Wabbajack.VirtualFileSystem/Context.cs b/Wabbajack.VirtualFileSystem/Context.cs index d2ed40dc..cc1d45ce 100644 --- a/Wabbajack.VirtualFileSystem/Context.cs +++ b/Wabbajack.VirtualFileSystem/Context.cs @@ -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; diff --git a/Wabbajack.VirtualFileSystem/VirtualFile.cs b/Wabbajack.VirtualFileSystem/VirtualFile.cs index 27593fba..7e589678 100644 --- a/Wabbajack.VirtualFileSystem/VirtualFile.cs +++ b/Wabbajack.VirtualFileSystem/VirtualFile.cs @@ -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; } }