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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Core.Galaxy;

/// <summary>The original classifier's cached CommanderID equality for a present
/// claim. Missing/pending cache data remains null, never a false ownership fact.</summary>
public sealed record GalaxyNativeColonisationOwnership(ulong LocalCommanderId,ulong ClaimCommanderId,byte ClaimStatus)
{
    public const string Source="original-colonisation-classifier-owner";
    public bool Own=>LocalCommanderId==ClaimCommanderId;
    public static GalaxyNativeColonisationOwnership? FromWire(JsonElement value)
    {
        if(value.ValueKind==JsonValueKind.Null)return null;
        try
        {
            ulong Id(string name)=>value.GetProperty(name).ValueKind==JsonValueKind.String&&
                ulong.TryParse(value.GetProperty(name).GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var id)&&id is >0 and <ulong.MaxValue
                ?id:throw new InvalidDataException("原殖民CommanderID资料无效。");
            if(value.ValueKind!=JsonValueKind.Object||value.GetProperty("source").GetString()!=Source)throw new InvalidDataException("原殖民身份来源无效。");
            var local=Id("localCommanderId");var claim=Id("claimCommanderId");var status=value.GetProperty("claimStatus").GetByte();var own=value.GetProperty("own");
            if(status>3||own.ValueKind is not(JsonValueKind.True or JsonValueKind.False)||own.GetBoolean()!=(local==claim))throw new InvalidDataException("原殖民身份判定不一致。");
            return new(local,claim,status);
        }
        catch(Exception error)when(error is KeyNotFoundException or InvalidOperationException or FormatException or OverflowException)
        {throw new InvalidDataException("原殖民身份回执无效。",error);}
    }
}

public sealed partial record GalaxyNativeSystemFacts
{
    public GalaxyNativeColonisationOwnership? ColonisationOwnership {get;init;}
    public bool ColonisationOwnershipRequested {get;init;}
}