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;

public sealed record GalaxyCommunityGoalsSnapshot(bool Available,IReadOnlyList<long> SystemAddresses)
{
    public bool Complete=>false;
}
public sealed partial class NativeGalaxyAccountCatalog
{
    public async Task<GalaxyCommunityGoalsSnapshot> ReadCommunityGoalsAsync(CancellationToken token=default)
    {
        var page=await pipe.CallAsync(new{op="galaxy.communitygoals",limit=96},token);
        if(!page.GetProperty("available").GetBoolean())return new(false,[]);
        var generation=page.GetProperty("generation").GetUInt64();var total=page.GetProperty("total").GetInt32();var entity=page.GetProperty("entityId").GetString();
        if(generation==0||total is <0 or >4096||string.IsNullOrEmpty(entity))throw new InvalidDataException("原游戏社区目标快照标识无效。");
        var addresses=new List<long>();var offset=0;
        while(true)
        {
            token.ThrowIfCancellationRequested();
            if(!page.GetProperty("available").GetBoolean()||page.GetProperty("generation").GetUInt64()!=generation||page.GetProperty("entityId").GetString()!=entity||page.GetProperty("total").GetInt32()!=total)
                throw new InvalidDataException("原游戏社区目标快照已变化,请重试。");
            var rows=page.GetProperty("items");if(rows.ValueKind!=JsonValueKind.Array||rows.GetArrayLength()>96||offset+rows.GetArrayLength()>total)throw new InvalidDataException("原游戏社区目标分页无效。");
            foreach(var row in rows.EnumerateArray())
            {
                if(!long.TryParse(row.GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var address)||address<=0||address>=1L<<55)throw new InvalidDataException("原游戏社区目标星系地址无效。");
                // Preserve repeated systems: the source map list has no goal ID.
                addresses.Add(address);
            }
            offset+=rows.GetArrayLength();if(offset==total)break;if(rows.GetArrayLength()==0)throw new InvalidDataException("原游戏社区目标分页未继续。");
            page=await pipe.CallAsync(new{op="galaxy.communitygoals",generation,offset,limit=96},token);
        }
        return new(true,addresses);
    }
}