XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using XFEEDToolBox.Core.Rendering;

namespace XFEEDToolBox.Core.Galaxy;

public sealed partial class NativeGalaxyCatalog
{
    public bool SupportsDustInputs {get;private set;}

    private static GalaxyCatalogueDustInputs? ReadCatalogueDustInputs(JsonElement profile)
    {
        if(!profile.TryGetProperty("dustInputs",out var value)||value.ValueKind==JsonValueKind.Null)return null;
        try
        {
            if(value.ValueKind!=JsonValueKind.Object||value.GetProperty("scope").GetString()!="observed-game-dust-producer-inputs"||
               value.GetProperty("runtimeTint").ValueKind!=JsonValueKind.Null)throw new InvalidDataException("原Dust输入来源或未知色调语义无效。");
            uint UInt(JsonElement source,string name)
            {if(!source.GetProperty(name).TryGetUInt32(out var n))throw new InvalidDataException("原Dust输入整数无效。");return n;}
            float Single(JsonElement source,string name)=>Number(source.GetProperty(name));
            static float Number(JsonElement n)
            {if(n.ValueKind!=JsonValueKind.Number||!n.TryGetSingle(out var v)||!float.IsFinite(v)||v<0)throw new InvalidDataException("原Dust输入数值无效。");return v;}
            var step=UInt(value,"step");var height=UInt(value,"height");
            if(step==0||height==0)throw new InvalidDataException("原Dust生产器尚未初始化。");
            var raw=value.GetProperty("layerAlphaSize");
            if(raw.ValueKind!=JsonValueKind.Array||raw.GetArrayLength()!=6)throw new InvalidDataException("原Dust三层参数不完整。");
            var factors=raw.EnumerateArray().Select(Number).ToArray();
            GalaxyOriginalDustLayerFactors[] layers=[new(factors[0],factors[1]),new(factors[2],factors[3]),new(factors[4],factors[5])];
            var observed=value.GetProperty("observedGamePublications");
            if(observed.ValueKind!=JsonValueKind.Array||observed.GetArrayLength()!=3)throw new InvalidDataException("原Dust发布观察不完整。");
            var publications=new GalaxyObservedGameDustPublication[3];var index=0;
            foreach(var p in observed.EnumerateArray())
            {
                if(p.ValueKind!=JsonValueKind.Object||UInt(p,"ordinal")!=index||!p.GetProperty("records").TryGetUInt64(out var count)||count>1_048_576)
                    throw new InvalidDataException("原Dust发布观察编号或数量无效。");
                var revision=UInt(p,"publishedRevision");var requested=UInt(p,"requestedRevision");
                var radius=p.GetProperty("radius");float? r=radius.ValueKind==JsonValueKind.Null?null:Number(radius);
                if((revision==0)!=!r.HasValue)throw new InvalidDataException("原Dust未发布半径不能假定就绪。");
                publications[index]=new(index,revision,requested,r,count);index++;
            }
            var settings=new GalaxyOriginalDustInputs(step,height,Array.AsReadOnly(layers),Single(value,"alphaMultiplier"),Single(value,"sizeMultiplier"),
                Single(value,"transitionPerStep"),Single(value,"minimumDistance"));
            // Deliberately not GalaxyOriginalDustPublication: these observations
            // cannot satisfy GalaxyOriginalDustFrame's tool-cache ready argument.
            return new(settings,Array.AsReadOnly(publications));
        }
        catch(Exception error)when(error is KeyNotFoundException or InvalidOperationException or FormatException or OverflowException)
        {throw new InvalidDataException("原Dust输入快照格式无效。",error);}
    }
}