2021-09-27 12:42:46 +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;
|
2021-01-05 22:09:32 +00:00
|
|
|
using System.Text;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.DTOs.Texture;
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
|
|
|
using Wabbajack.Paths;
|
2020-01-09 22:56:29 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.VFS;
|
|
|
|
|
|
|
|
public class IndexedVirtualFile
|
2020-01-09 22:56:29 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
public IPath Name { get; set; }
|
|
|
|
public Hash Hash { get; set; }
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public ImageState? ImageState { get; set; }
|
|
|
|
public long Size { get; set; }
|
|
|
|
public List<IndexedVirtualFile> Children { get; set; } = new();
|
2020-07-10 22:59:39 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
private void Write(BinaryWriter bw)
|
|
|
|
{
|
|
|
|
bw.Write(Name.ToString()!);
|
|
|
|
bw.Write((ulong) Hash);
|
2020-07-10 22:59:39 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
if (ImageState == null)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
bw.Write(false);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
else
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
bw.Write(true);
|
|
|
|
WriteImageState(bw, ImageState);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
bw.Write(Size);
|
|
|
|
bw.Write(Children.Count);
|
|
|
|
foreach (var file in Children)
|
|
|
|
file.Write(bw);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteImageState(BinaryWriter bw, ImageState state)
|
|
|
|
{
|
|
|
|
bw.Write((ushort) state.Width);
|
|
|
|
bw.Write((ushort) state.Height);
|
|
|
|
bw.Write((byte) state.Format);
|
|
|
|
state.PerceptualHash.Write(bw);
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static ImageState ReadImageState(BinaryReader br)
|
|
|
|
{
|
|
|
|
return new ImageState
|
2020-07-10 22:59:39 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
Width = br.ReadUInt16(),
|
|
|
|
Height = br.ReadUInt16(),
|
|
|
|
Format = (DXGI_FORMAT) br.ReadByte(),
|
|
|
|
PerceptualHash = PHash.Read(br)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(Stream s)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
private static IndexedVirtualFile Read(BinaryReader br)
|
|
|
|
{
|
|
|
|
var ivf = new IndexedVirtualFile
|
2020-07-10 22:59:39 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
Name = (RelativePath) br.ReadString(),
|
|
|
|
Hash = Hash.FromULong(br.ReadUInt64())
|
|
|
|
};
|
2021-06-16 21:07:16 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
if (br.ReadBoolean())
|
|
|
|
ivf.ImageState = ReadImageState(br);
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
ivf.Size = br.ReadInt64();
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var lst = new List<IndexedVirtualFile>();
|
|
|
|
ivf.Children = lst;
|
|
|
|
var count = br.ReadInt32();
|
|
|
|
for (var x = 0; x < count; x++) lst.Add(Read(br));
|
2020-07-15 03:04:31 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return ivf;
|
|
|
|
}
|
2020-07-10 22:59:39 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static IndexedVirtualFile Read(Stream s)
|
|
|
|
{
|
|
|
|
using var cs = new GZipStream(s, CompressionMode.Decompress, true);
|
|
|
|
using var br = new BinaryReader(cs);
|
|
|
|
return Read(br);
|
2020-01-09 22:56:29 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|