using System.Buffers.Binary; using System.Text; using EvilEngine.Adapters.UnityFs; namespace EvilEngine.Adapters.Tests.Support; public sealed class UnityFsWriter { private readonly List<(string Name, byte[] Data, uint Flags)> _nodes = []; public UnityFsWriter Add(string name, byte[] data, uint flags = 4) { _nodes.Add((name, data, flags)); return this; } public UnityFsWriter Add(string name, string text, uint flags = 4) { return Add(name, Encoding.UTF8.GetBytes(text), flags); } public byte[] Build( uint version = 6, UnityFsCompression blocks = UnityFsCompression.None, UnityFsCompression directory = UnityFsCompression.Lz4HC, int blockSize = int.MaxValue, bool directoryAtEnd = false, bool padBlocks = false, byte[]? lzmaBlock = null) { var data = _nodes.SelectMany(node => node.Data).ToArray(); var stored = new List<(int Size, byte[] Bytes)>(); if (blocks == UnityFsCompression.Lzma) stored.Add((data.Length, lzmaBlock ?? throw new ArgumentException("an LZMA bundle needs its block given", nameof(lzmaBlock)))); else for (var at = 0; at < data.Length; at += blockSize) { var block = data[at..Math.Min(data.Length, at + blockSize)]; stored.Add((block.Length, blocks == UnityFsCompression.None ? block : Lz4Writer.Encode(block))); } var info = new MemoryStream(); info.Write(new byte[16]); Int32(info, stored.Count); foreach (var (size, bytes) in stored) { UInt32(info, (uint)size); UInt32(info, (uint)bytes.Length); UInt16(info, (ushort)blocks); } Int32(info, _nodes.Count); long offset = 0; foreach (var (name, nodeData, flags) in _nodes) { Int64(info, offset); Int64(info, nodeData.Length); UInt32(info, flags); info.Write(Encoding.UTF8.GetBytes(name + "\0")); offset += nodeData.Length; } var plain = info.ToArray(); var packed = directory == UnityFsCompression.None ? plain : Lz4Writer.Encode(plain); var header = new MemoryStream(); header.Write("UnityFS\0"u8); UInt32(header, version); header.Write("5.x.x\0"u8); header.Write("2019.4.18f1\0"u8); var sizeAt = header.Position; Int64(header, 0); UInt32(header, (uint)packed.Length); UInt32(header, (uint)plain.Length); UInt32(header, (uint)directory | 0x40 | (directoryAtEnd ? 0x80u : 0) | (padBlocks ? 0x200u : 0)); if (version >= 7) Pad(header); if (!directoryAtEnd) header.Write(packed); if (padBlocks) Pad(header); foreach (var (_, bytes) in stored) header.Write(bytes); if (directoryAtEnd) header.Write(packed); var bundle = header.ToArray(); BinaryPrimitives.WriteInt64BigEndian(bundle.AsSpan((int)sizeAt), bundle.Length); return bundle; } private static void Pad(MemoryStream stream) { while (stream.Position % 16 != 0) stream.WriteByte(0); } private static void UInt16(Stream stream, ushort value) { Span bytes = stackalloc byte[2]; BinaryPrimitives.WriteUInt16BigEndian(bytes, value); stream.Write(bytes); } private static void UInt32(Stream stream, uint value) { Span bytes = stackalloc byte[4]; BinaryPrimitives.WriteUInt32BigEndian(bytes, value); stream.Write(bytes); } private static void Int32(Stream stream, int value) { UInt32(stream, (uint)value); } private static void Int64(Stream stream, long value) { Span bytes = stackalloc byte[8]; BinaryPrimitives.WriteInt64BigEndian(bytes, value); stream.Write(bytes); } } public static class Lz4Writer { public static byte[] Encode(byte[] input) { var output = new MemoryStream(); var table = new int[4096]; Array.Fill(table, -1); int anchor = 0, at = 0; while (at + 4 <= input.Length - 5) { var word = BinaryPrimitives.ReadUInt32LittleEndian(input.AsSpan(at)); var hash = (int)((word * 2654435761u) >> 20); var candidate = table[hash]; table[hash] = at; if (candidate >= 0 && at - candidate <= 65535 && BinaryPrimitives.ReadUInt32LittleEndian(input.AsSpan(candidate)) == word) { var length = 4; while (at + length < input.Length - 5 && input[candidate + length] == input[at + length]) length++; Sequence(output, input.AsSpan(anchor, at - anchor), at - candidate, length); at += length; anchor = at; } else { at++; } } Sequence(output, input.AsSpan(anchor), 0, 0); return output.ToArray(); } private static void Sequence(MemoryStream output, ReadOnlySpan literals, int distance, int match) { var extra = match == 0 ? 0 : match - 4; output.WriteByte((byte)((Math.Min(literals.Length, 15) << 4) | Math.Min(extra, 15))); Length(output, literals.Length); output.Write(literals); if (match == 0) return; output.WriteByte((byte)distance); output.WriteByte((byte)(distance >> 8)); Length(output, extra); } private static void Length(MemoryStream output, int length) { if (length < 15) return; length -= 15; while (length >= 255) { output.WriteByte(255); length -= 255; } output.WriteByte((byte)length); } }