< Summary

Information
Class: Lattice.Math.FPLutHeader
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLutVersion.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 64
Coverable lines: 64
Total lines: 188
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
FromBytes(...)0%620%
ToBytes()100%210%
Validate()0%110100%
VerifyChecksum(...)100%210%
ComputeChecksum(...)0%2040%
VersionToString(...)0%620%
Equals(...)0%2040%
Equals(...)0%620%
GetHashCode()100%210%
ToString()100%210%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLutVersion.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4#nullable enable
 5
 6using System;
 7using System.IO;
 8using System.Text;
 9
 10namespace Lattice.Math
 11{
 12    /// <summary>
 13    /// LUT 文件版本头
 14    /// </summary>
 15    public struct FPLutHeader : IEquatable<FPLutHeader>
 16    {
 17        /// <summary>魔数:'FPLT' = 0x464P4C54 (FP = 0x464P, LT = 0x4C54)</summary>
 18        public const uint Magic = 0x464F4C54;  // 'F' 'P' 'L' 'T' ASCII
 19
 20        /// <summary>当前版本:1.0.0 = 0x00010000</summary>
 21        public const uint CurrentVersion = 0x00010000;
 22
 23        /// <summary>兼容的最旧版本:1.0.0</summary>
 24        public const uint MinCompatibleVersion = 0x00010000;
 25
 26        /// <summary>魔数(4 bytes)</summary>
 27        public uint FileMagic;
 28
 29        /// <summary>版本号(4 bytes):主.次.修.预 = 1 byte each</summary>
 30        public uint Version;
 31
 32        /// <summary>LUT 类型(4 bytes)</summary>
 33        public LutType Type;
 34
 35        /// <summary>条目数量(4 bytes)</summary>
 36        public uint EntryCount;
 37
 38        /// <summary>每个条目字节数(4 bytes)</summary>
 39        public uint EntrySize;
 40
 41        /// <summary>校验和 CRC32(4 bytes)</summary>
 42        public uint Checksum;
 43
 44        /// <summary>保留字段(16 bytes)</summary>
 45        public ulong Reserved1;
 46        public ulong Reserved2;
 47
 48        /// <summary>头部总大小:32 bytes</summary>
 49        public const int HeaderSize = 32;
 50
 51        public FPLutHeader(uint version, LutType type, uint entryCount, uint entrySize, uint checksum)
 52        {
 053            FileMagic = Magic;
 054            Version = version;
 055            Type = type;
 056            EntryCount = entryCount;
 057            EntrySize = entrySize;
 058            Checksum = checksum;
 059            Reserved1 = 0;
 060            Reserved2 = 0;
 061        }
 62
 63        /// <summary>
 64        /// 从字节数组解析头部
 65        /// </summary>
 66        public static FPLutHeader FromBytes(byte[] data)
 67        {
 068            if (data.Length < HeaderSize)
 069                throw new InvalidDataException($"LUT header too small: {data.Length} bytes");
 70
 071            var header = new FPLutHeader
 072            {
 073                FileMagic = BitConverter.ToUInt32(data, 0),
 074                Version = BitConverter.ToUInt32(data, 4),
 075                Type = (LutType)BitConverter.ToUInt32(data, 8),
 076                EntryCount = BitConverter.ToUInt32(data, 12),
 077                EntrySize = BitConverter.ToUInt32(data, 16),
 078                Checksum = BitConverter.ToUInt32(data, 20),
 079                Reserved1 = BitConverter.ToUInt64(data, 24),
 080                Reserved2 = BitConverter.ToUInt64(data, 32)
 081            };
 82
 083            header.Validate();
 084            return header;
 85        }
 86
 87        /// <summary>
 88        /// 将头部写入字节数组
 89        /// </summary>
 90        public byte[] ToBytes()
 91        {
 092            var data = new byte[HeaderSize];
 093            BitConverter.GetBytes(FileMagic).CopyTo(data, 0);
 094            BitConverter.GetBytes(Version).CopyTo(data, 4);
 095            BitConverter.GetBytes((uint)Type).CopyTo(data, 8);
 096            BitConverter.GetBytes(EntryCount).CopyTo(data, 12);
 097            BitConverter.GetBytes(EntrySize).CopyTo(data, 16);
 098            BitConverter.GetBytes(Checksum).CopyTo(data, 20);
 099            BitConverter.GetBytes(Reserved1).CopyTo(data, 24);
 0100            BitConverter.GetBytes(Reserved2).CopyTo(data, 32);
 0101            return data;
 102        }
 103
 104        /// <summary>
 105        /// 验证头部有效性
 106        /// </summary>
 107        public void Validate()
 108        {
 0109            if (FileMagic != Magic)
 0110                throw new InvalidDataException($"Invalid LUT magic: 0x{FileMagic:X8}, expected 0x{Magic:X8}");
 111
 0112            if (Version < MinCompatibleVersion)
 0113                throw new InvalidDataException($"LUT version {VersionToString(Version)} is too old. Minimum: {VersionToS
 114
 0115            if (EntryCount == 0)
 0116                throw new InvalidDataException("LUT entry count is zero");
 117
 0118            if (EntrySize != 4 && EntrySize != 8)
 0119                throw new InvalidDataException($"Invalid entry size: {EntrySize}, expected 4 or 8");
 0120        }
 121
 122        /// <summary>
 123        /// 验证数据校验和
 124        /// </summary>
 125        public bool VerifyChecksum(byte[] data)
 126        {
 0127            uint computed = ComputeChecksum(data);
 0128            return computed == Checksum;
 129        }
 130
 131        /// <summary>
 132        /// 计算 CRC32 校验和
 133        /// </summary>
 134        public static uint ComputeChecksum(byte[] data)
 135        {
 136            // 简化实现:实际使用 System.IO.Hashing.Crc32
 0137            uint crc = 0xFFFFFFFF;
 0138            foreach (byte b in data)
 139            {
 0140                crc ^= b;
 0141                for (int i = 0; i < 8; i++)
 0142                    crc = (crc >> 1) ^ (0xEDB88320u & (uint)(-(crc & 1)));
 143            }
 0144            return ~crc;
 145        }
 146
 147        /// <summary>
 148        /// 版本号转字符串
 149        /// </summary>
 150        public static string VersionToString(uint version)
 151        {
 0152            byte major = (byte)(version >> 24);
 0153            byte minor = (byte)(version >> 16);
 0154            byte patch = (byte)(version >> 8);
 0155            byte pre = (byte)version;
 156
 0157            if (pre == 0)
 0158                return $"{major}.{minor}.{patch}";
 0159            return $"{major}.{minor}.{patch}-pre{pre}";
 160        }
 161
 162        public bool Equals(FPLutHeader other) =>
 0163            FileMagic == other.FileMagic &&
 0164            Version == other.Version &&
 0165            Type == other.Type;
 166
 0167        public override bool Equals(object? obj) => obj is FPLutHeader other && Equals(other);
 0168        public override int GetHashCode() => HashCode.Combine(FileMagic, Version, Type);
 0169        public override string ToString() => $"FPLut v{VersionToString(Version)}, {Type}, {EntryCount} entries";
 170    }
 171
 172    /// <summary>
 173    /// LUT 类型枚举
 174    /// </summary>
 175    public enum LutType : uint
 176    {
 177        Unknown = 0,
 178        SinCos = 1,
 179        Tan = 2,
 180        Asin = 3,
 181        Acos = 4,
 182        Atan = 5,
 183        Sqrt = 6,
 184        Exp = 7,
 185        Log = 8,
 186        Custom = 0xFFFFFFFF
 187    }
 188}