2020-01-09 22:56:29 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-10 22:59:39 +00:00
|
|
|
|
using System.IO;
|
2021-06-16 21:07:16 +00:00
|
|
|
|
using System.IO.Compression;
|
2020-07-10 22:59:39 +00:00
|
|
|
|
using System.Linq;
|
2021-01-05 22:09:32 +00:00
|
|
|
|
using System.Text;
|
2020-07-10 22:59:39 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-22 15:50:53 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-05-12 21:28:09 +00:00
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2021-06-16 05:16:25 +00:00
|
|
|
|
using Wabbajack.ImageHashing;
|
2020-01-09 22:56:29 +00:00
|
|
|
|
|
2020-01-10 04:47:06 +00:00
|
|
|
|
namespace Wabbajack.VirtualFileSystem
|
2020-01-09 22:56:29 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Response from the Build server for a indexed file
|
|
|
|
|
/// </summary>
|
2020-05-12 21:28:09 +00:00
|
|
|
|
[JsonName("IndexedVirtualFile")]
|
2020-01-09 22:56:29 +00:00
|
|
|
|
public class IndexedVirtualFile
|
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public IPath Name { get; set; }
|
2020-03-22 15:50:53 +00:00
|
|
|
|
public Hash Hash { get; set; }
|
2021-06-16 05:16:25 +00:00
|
|
|
|
|
2021-06-17 23:09:03 +00:00
|
|
|
|
public ImageState ImageState { get; set; }
|
2020-01-09 22:56:29 +00:00
|
|
|
|
public long Size { get; set; }
|
2021-06-16 05:16:25 +00:00
|
|
|
|
public List<IndexedVirtualFile> Children { get; set; } = new();
|
2020-07-10 22:59:39 +00:00
|
|
|
|
|
|
|
|
|
private void Write(BinaryWriter bw)
|
|
|
|
|
{
|
|
|
|
|
bw.Write(Name.ToString());
|
|
|
|
|
bw.Write((ulong)Hash);
|
2021-06-16 21:07:16 +00:00
|
|
|
|
|
|
|
|
|
if (ImageState == null)
|
|
|
|
|
bw.Write(false);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bw.Write(true);
|
|
|
|
|
ImageState.Write(bw);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 22:59:39 +00:00
|
|
|
|
bw.Write(Size);
|
|
|
|
|
bw.Write(Children.Count);
|
|
|
|
|
foreach (var file in Children)
|
|
|
|
|
file.Write(bw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(Stream s)
|
|
|
|
|
{
|
2021-06-16 21:07:16 +00:00
|
|
|
|
using var cs = new GZipStream(s, CompressionLevel.Optimal , true);
|
|
|
|
|
using var bw = new BinaryWriter(cs, Encoding.UTF8, true);
|
2021-07-17 05:32:37 +00:00
|
|
|
|
Write(bw);
|
2020-07-10 22:59:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IndexedVirtualFile Read(BinaryReader br)
|
|
|
|
|
{
|
|
|
|
|
var ivf = new IndexedVirtualFile
|
|
|
|
|
{
|
|
|
|
|
Name = (RelativePath)br.ReadString(),
|
|
|
|
|
Hash = Hash.FromULong(br.ReadUInt64()),
|
|
|
|
|
};
|
2021-06-16 21:07:16 +00:00
|
|
|
|
|
|
|
|
|
if (br.ReadBoolean())
|
|
|
|
|
ivf.ImageState = ImageState.Read(br);
|
|
|
|
|
|
|
|
|
|
ivf.Size = br.ReadInt64();
|
|
|
|
|
|
2020-07-10 22:59:39 +00:00
|
|
|
|
var lst = new List<IndexedVirtualFile>();
|
2020-07-15 03:04:31 +00:00
|
|
|
|
ivf.Children = lst;
|
2020-07-10 22:59:39 +00:00
|
|
|
|
var count = br.ReadInt32();
|
|
|
|
|
for (int x = 0; x < count; x++)
|
|
|
|
|
{
|
|
|
|
|
lst.Add(Read(br));
|
|
|
|
|
}
|
2020-07-15 03:04:31 +00:00
|
|
|
|
|
2020-07-10 22:59:39 +00:00
|
|
|
|
return ivf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IndexedVirtualFile Read(Stream s)
|
|
|
|
|
{
|
2021-06-16 21:07:16 +00:00
|
|
|
|
using var cs = new GZipStream(s, CompressionMode.Decompress, true);
|
|
|
|
|
using var br = new BinaryReader(cs);
|
2021-07-17 05:32:37 +00:00
|
|
|
|
return Read(br);
|
2020-07-10 22:59:39 +00:00
|
|
|
|
}
|
2020-01-09 22:56:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|