using System.Buffers.Binary; using EvilEngine.Adapters.Source.Textures; namespace EvilEngine.Adapters.Tests.Support; public sealed class VtfWriter { public int Minor { get; init; } = 5; public VtfFormat Format { get; init; } = VtfFormat.Rgba8888; public int Width { get; init; } = 4; public int Height { get; init; } = 4; public int Depth { get; init; } = 1; public int Frames { get; init; } = 1; public ushort StartFrame { get; init; } public int? Mips { get; init; } public VtfFlags Flags { get; init; } public bool LowRes { get; init; } = true; public uint? Lod { get; init; } public byte[]? Sheet { get; init; } public int Faces => !Flags.HasFlag(VtfFlags.Envmap) ? 1 : Minor < 5 && StartFrame != VtfFile.NoStartFrame ? 7 : 6; public int MipCount => Mips ?? VtfFile.FullMips(Width, Height, Depth); public byte[] Image() { return Pattern((int)VtfFile.ImageBytes(Format, Width, Height, Depth, MipCount, Frames, Faces), 0); } public static byte[] Pattern(int length, int seed) { var bytes = new byte[length]; for (var i = 0; i < length; i++) bytes[i] = (byte)(i * 13 + seed * 5 + 1); return bytes; } public byte[] Write() { return Write(Image()); } public byte[] Write(byte[] image) { var lowRes = LowRes ? Pattern((int)VtfFormats.ImageBytes(VtfFormat.Dxt1, 4, 4), 9) : []; var resources = new List<(uint Tag, byte Flags, uint Value, byte[]? Data)>(); if (LowRes) resources.Add((VtfResource.LowResImage, 0, 0, null)); if (Sheet is { } sheet) resources.Add((VtfResource.Sheet, 0, 0, sheet)); resources.Add((VtfResource.HighResImage, 0, 0, null)); if (Lod is { } lod) resources.Add((VtfResource.LodSettings, VtfResource.NoData, lod, null)); var header = Minor >= 3 ? 80 + resources.Count * 8 : 80; using var file = new MemoryStream(); file.Write(new byte[header]); var span = new byte[header]; BinaryPrimitives.WriteUInt32LittleEndian(span, VtfFile.Signature); BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(4), 7); BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(8), Minor); BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(12), header); BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(16), (ushort)Width); BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(18), (ushort)Height); BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(20), (uint)Flags); BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(24), (ushort)Frames); BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(26), StartFrame); BinaryPrimitives.WriteSingleLittleEndian(span.AsSpan(32), 0.5f); BinaryPrimitives.WriteSingleLittleEndian(span.AsSpan(48), 1); BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(52), (int)Format); span[56] = (byte)MipCount; BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(57), LowRes ? (int)VtfFormat.Dxt1 : (int)VtfFormat.None); span[61] = (byte)(LowRes ? 4 : 0); span[62] = (byte)(LowRes ? 4 : 0); if (Minor >= 2) BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(63), (ushort)Depth); if (Minor < 3) { file.Write(lowRes); file.Write(image); } else { BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(68), (uint)resources.Count); for (var i = 0; i < resources.Count; i++) { var (tag, flags, value, data) = resources[i]; if (tag == VtfResource.LowResImage) { value = (uint)file.Position; file.Write(lowRes); } else if (tag == VtfResource.HighResImage) { value = (uint)file.Position; file.Write(image); } else if (data is not null) { value = (uint)file.Position; var length = new byte[4]; BinaryPrimitives.WriteUInt32LittleEndian(length, (uint)data.Length); file.Write(length); file.Write(data); } BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(80 + i * 8), tag | ((uint)flags << 24)); BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(84 + i * 8), value); } } var bytes = file.ToArray(); span.CopyTo(bytes, 0); return bytes; } public static byte[] SheetData(int version, params (int Number, bool Clamp, float Duration, (float Duration, float[] Rects)[] Frames)[] sequences) { using var stream = new MemoryStream(); using var w = new BinaryWriter(stream); w.Write(version); w.Write(sequences.Length); foreach (var (number, clamp, duration, frames) in sequences) { w.Write(number); w.Write(clamp ? 1 : 0); w.Write(frames.Length); w.Write(duration); foreach (var (frameDuration, rects) in frames) { w.Write(frameDuration); foreach (var value in rects) w.Write(value); } } w.Flush(); return stream.ToArray(); } }