using System.IO.Compression;
using System.Numerics;
using System.Security.Cryptography;
using System.Text.Json;
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Rendering;
/// Exact SectorLine diffuse texture, including the eight authored pattern rows.
public sealed class GalaxyOriginalGridTexture
{
public const int Width=512,Height=512,MipCount=10,PayloadBytes=353280;
public const string PayloadSha256="218423e603214f4957bfbc49cab9d91c22f088acdc01a845b079864e589b68bb";
public static GalaxyOriginalGridTexture Reference {get;}=Read();
public ReadOnlyMemory Payload {get;}
public IReadOnlyList Mips {get;}
// Original3AB44E0 writes rendercfg+98 through the named scalar wrappers.
// Keep the shader's Radius,RadiusFar,Range,RangeFar order explicit.
public Vector4 AuthoredFade {get;}
private GalaxyOriginalGridTexture(JsonElement metadata,byte[] payload)
{
if(metadata.GetProperty("sourceFormat").GetInt32()!=107||metadata.GetProperty("nativeFormat").GetInt32()!=98||
metadata.GetProperty("format").GetString()!="BC7_UNorm"||metadata.GetProperty("width").GetInt32()!=Width||
metadata.GetProperty("height").GetInt32()!=Height||metadata.GetProperty("mipCount").GetInt32()!=MipCount||
metadata.GetProperty("viewportSlot").GetInt32()!=8||metadata.GetProperty("configurationLabel").GetString()!="SectorLine"||
payload.Length!=PayloadBytes||Convert.ToHexStringLower(SHA256.HashData(payload))!=PayloadSha256)
throw new InvalidDataException("原银河网格线条纹理校验失败。");
var mips=JsonSerializer.Deserialize(metadata.GetProperty("mips"),ToolboxSettings.JsonOptions)??[];
if(mips.Length!=MipCount)throw new InvalidDataException("原银河网格线条层级无效。");
var end=0;
for(var level=0;level>level)+3)/4);var rows=Math.Max(1,((Height>>level)+3)/4);
if(mip.Offset!=end||mip.Size!=mip.SliceBytes||mip.RowPitch