wabbajack/Wabbajack.VirtualFileSystem/IndexedVirtualFile.cs

84 lines
2.3 KiB
C#
Raw Normal View History

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;
using Wabbajack.Common.Serialization.Json;
2021-06-16 05:16:25 +00:00
using Wabbajack.ImageHashing;
namespace Wabbajack.VirtualFileSystem
{
/// <summary>
/// Response from the Build server for a indexed file
/// </summary>
[JsonName("IndexedVirtualFile")]
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; }
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);
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);
return Read(br);
2020-07-10 22:59:39 +00:00
}
}
}