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;
|
2022-06-22 01:38:42 +00:00
|
|
|
using Wabbajack.DTOs.Vfs;
|
2021-09-27 12:42:46 +00:00
|
|
|
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;
|
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
public static class IndexedVirtualFileExtensions
|
2020-01-09 22:56:29 +00:00
|
|
|
{
|
2022-06-22 01:38:42 +00:00
|
|
|
public static void Write(this IndexedVirtualFile ivf, BinaryWriter bw)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
2022-06-22 01:38:42 +00:00
|
|
|
bw.Write(ivf.Name.ToString()!);
|
|
|
|
bw.Write((ulong) ivf.Hash);
|
2020-07-10 22:59:39 +00:00
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
if (ivf.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);
|
2022-06-22 01:38:42 +00:00
|
|
|
WriteImageState(bw, ivf.ImageState);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
bw.Write(ivf.Size);
|
|
|
|
bw.Write(ivf.Children.Count);
|
|
|
|
foreach (var file in ivf.Children)
|
2021-10-23 16:51:17 +00:00
|
|
|
file.Write(bw);
|
|
|
|
}
|
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
private static void WriteImageState(BinaryWriter bw, ImageState state)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
static ImageState ReadImageState(BinaryReader br)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
public static void Write(this IndexedVirtualFile ivf, Stream s)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
using var cs = new GZipStream(s, CompressionLevel.Optimal, true);
|
|
|
|
using var bw = new BinaryWriter(cs, Encoding.UTF8, true);
|
2022-06-22 01:38:42 +00:00
|
|
|
ivf.Write(bw);
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
2020-07-10 22:59:39 +00:00
|
|
|
|
2022-06-22 01:38:42 +00:00
|
|
|
public static IndexedVirtualFile Read(BinaryReader br)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|