0.2.0-dev Download ZIP
142 lines ·5.4 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using EvilEngine.Adapters.Source.Textures; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Tests.Support; |
| 5 | |
| 6 | public sealed class VtfWriter |
| 7 | { |
| 8 | public int Minor { get; init; } = 5; |
| 9 | public VtfFormat Format { get; init; } = VtfFormat.Rgba8888; |
| 10 | public int Width { get; init; } = 4; |
| 11 | public int Height { get; init; } = 4; |
| 12 | public int Depth { get; init; } = 1; |
| 13 | public int Frames { get; init; } = 1; |
| 14 | public ushort StartFrame { get; init; } |
| 15 | public int? Mips { get; init; } |
| 16 | public VtfFlags Flags { get; init; } |
| 17 | public bool LowRes { get; init; } = true; |
| 18 | public uint? Lod { get; init; } |
| 19 | public byte[]? Sheet { get; init; } |
| 20 | |
| 21 | public int Faces => !Flags.HasFlag(VtfFlags.Envmap) ? 1 : Minor < 5 && StartFrame != VtfFile.NoStartFrame ? 7 : 6; |
| 22 | |
| 23 | public int MipCount => Mips ?? VtfFile.FullMips(Width, Height, Depth); |
| 24 | |
| 25 | public byte[] Image() |
| 26 | { |
| 27 | return Pattern((int)VtfFile.ImageBytes(Format, Width, Height, Depth, MipCount, Frames, Faces), 0); |
| 28 | } |
| 29 | |
| 30 | public static byte[] Pattern(int length, int seed) |
| 31 | { |
| 32 | var bytes = new byte[length]; |
| 33 | for (var i = 0; i < length; i++) |
| 34 | bytes[i] = (byte)(i * 13 + seed * 5 + 1); |
| 35 | return bytes; |
| 36 | } |
| 37 | |
| 38 | public byte[] Write() |
| 39 | { |
| 40 | return Write(Image()); |
| 41 | } |
| 42 | |
| 43 | public byte[] Write(byte[] image) |
| 44 | { |
| 45 | var lowRes = LowRes ? Pattern((int)VtfFormats.ImageBytes(VtfFormat.Dxt1, 4, 4), 9) : []; |
| 46 | var resources = new List<(uint Tag, byte Flags, uint Value, byte[]? Data)>(); |
| 47 | if (LowRes) |
| 48 | resources.Add((VtfResource.LowResImage, 0, 0, null)); |
| 49 | if (Sheet is { } sheet) |
| 50 | resources.Add((VtfResource.Sheet, 0, 0, sheet)); |
| 51 | resources.Add((VtfResource.HighResImage, 0, 0, null)); |
| 52 | if (Lod is { } lod) |
| 53 | resources.Add((VtfResource.LodSettings, VtfResource.NoData, lod, null)); |
| 54 | |
| 55 | var header = Minor >= 3 ? 80 + resources.Count * 8 : 80; |
| 56 | using var file = new MemoryStream(); |
| 57 | file.Write(new byte[header]); |
| 58 | var span = new byte[header]; |
| 59 | BinaryPrimitives.WriteUInt32LittleEndian(span, VtfFile.Signature); |
| 60 | BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(4), 7); |
| 61 | BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(8), Minor); |
| 62 | BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(12), header); |
| 63 | BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(16), (ushort)Width); |
| 64 | BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(18), (ushort)Height); |
| 65 | BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(20), (uint)Flags); |
| 66 | BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(24), (ushort)Frames); |
| 67 | BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(26), StartFrame); |
| 68 | BinaryPrimitives.WriteSingleLittleEndian(span.AsSpan(32), 0.5f); |
| 69 | BinaryPrimitives.WriteSingleLittleEndian(span.AsSpan(48), 1); |
| 70 | BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(52), (int)Format); |
| 71 | span[56] = (byte)MipCount; |
| 72 | BinaryPrimitives.WriteInt32LittleEndian(span.AsSpan(57), LowRes ? (int)VtfFormat.Dxt1 : (int)VtfFormat.None); |
| 73 | span[61] = (byte)(LowRes ? 4 : 0); |
| 74 | span[62] = (byte)(LowRes ? 4 : 0); |
| 75 | if (Minor >= 2) |
| 76 | BinaryPrimitives.WriteUInt16LittleEndian(span.AsSpan(63), (ushort)Depth); |
| 77 | |
| 78 | if (Minor < 3) |
| 79 | { |
| 80 | file.Write(lowRes); |
| 81 | file.Write(image); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(68), (uint)resources.Count); |
| 86 | for (var i = 0; i < resources.Count; i++) |
| 87 | { |
| 88 | var (tag, flags, value, data) = resources[i]; |
| 89 | if (tag == VtfResource.LowResImage) |
| 90 | { |
| 91 | value = (uint)file.Position; |
| 92 | file.Write(lowRes); |
| 93 | } |
| 94 | else if (tag == VtfResource.HighResImage) |
| 95 | { |
| 96 | value = (uint)file.Position; |
| 97 | file.Write(image); |
| 98 | } |
| 99 | else if (data is not null) |
| 100 | { |
| 101 | value = (uint)file.Position; |
| 102 | var length = new byte[4]; |
| 103 | BinaryPrimitives.WriteUInt32LittleEndian(length, (uint)data.Length); |
| 104 | file.Write(length); |
| 105 | file.Write(data); |
| 106 | } |
| 107 | |
| 108 | BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(80 + i * 8), tag | ((uint)flags << 24)); |
| 109 | BinaryPrimitives.WriteUInt32LittleEndian(span.AsSpan(84 + i * 8), value); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | var bytes = file.ToArray(); |
| 114 | span.CopyTo(bytes, 0); |
| 115 | return bytes; |
| 116 | } |
| 117 | |
| 118 | public static byte[] SheetData(int version, |
| 119 | params (int Number, bool Clamp, float Duration, (float Duration, float[] Rects)[] Frames)[] sequences) |
| 120 | { |
| 121 | using var stream = new MemoryStream(); |
| 122 | using var w = new BinaryWriter(stream); |
| 123 | w.Write(version); |
| 124 | w.Write(sequences.Length); |
| 125 | foreach (var (number, clamp, duration, frames) in sequences) |
| 126 | { |
| 127 | w.Write(number); |
| 128 | w.Write(clamp ? 1 : 0); |
| 129 | w.Write(frames.Length); |
| 130 | w.Write(duration); |
| 131 | foreach (var (frameDuration, rects) in frames) |
| 132 | { |
| 133 | w.Write(frameDuration); |
| 134 | foreach (var value in rects) |
| 135 | w.Write(value); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | w.Flush(); |
| 140 | return stream.ToArray(); |
| 141 | } |
| 142 | } |