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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using XFEEDToolBox.Core.Galaxy;
using XFEEDToolBox.Core.Navigation;

namespace XFEEDToolBox.ViewModels;

public partial class ToolboxViewModel
{
    [ObservableProperty] public partial IReadOnlyList<GalaxyAccountMapTarget> GalaxyCommunityGoalTargets {get;set;}=[];
    [ObservableProperty] public partial string GalaxyCommunityGoalStatus {get;set;}="原游戏社区活动发布缓存尚未连接;本机已订阅记录另由 Journal 提供。";
    private IReadOnlySet<long> galaxyCommunityGoalAddresses=new HashSet<long>();
    private DateTimeOffset galaxyCommunityGoalsReadAt;
    private string galaxyCommunityGoalsContext="";
    private long galaxyMapTargetSelectionVersion;
    private void SetGalaxyCommunityGoals(IReadOnlyList<long> addresses)
    {
        if(!galaxyCommunityGoalAddresses.SetEquals(addresses)){galaxyCommunityGoalAddresses=addresses.ToHashSet();galaxyNativeFactsVersion++;}
        var names=galaxyCache.Values.Where(s=>s.SystemAddress is {} address&&galaxyCommunityGoalAddresses.Contains(address)&&s.Position.IsValid).GroupBy(s=>s.SystemAddress!.Value).ToDictionary(g=>g.Key,g=>g.First().Name);
        var targets=addresses.Select(a=>new GalaxyAccountMapTarget(a,names.GetValueOrDefault(a)??"社区活动 · 地址 "+a,"原游戏社区活动发布缓存")).ToArray();
        if(!GalaxyCommunityGoalTargets.SequenceEqual(targets))GalaxyCommunityGoalTargets=targets;
    }
    private async Task ReadGalaxyCommunityGoalsAsync(CancellationToken token)
    {
        var context=ThargoidContext();
        if(galaxyCommunityGoalsContext.Length>0&&galaxyCommunityGoalsContext!=context)
        {SetGalaxyCommunityGoals([]);galaxyCommunityGoalsContext="";galaxyCommunityGoalsReadAt=default;}
        if(DateTimeOffset.UtcNow-galaxyCommunityGoalsReadAt<TimeSpan.FromSeconds(2))return;galaxyCommunityGoalsReadAt=DateTimeOffset.UtcNow;
        if(context.Length==0||runtime.FlightDriver is not NativeFlightControlDriver{SupportsGalaxyCommunityGoals:true} driver)
        {SetGalaxyCommunityGoals([]);galaxyCommunityGoalsContext="";GalaxyCommunityGoalStatus="原游戏社区活动发布缓存尚未连接;已订阅活动仍按 Journal 的实际状态保留。";return;}
        bool Latest()=>context==ThargoidContext();
        try
        {
            var snapshot=await new NativeGalaxyAccountCatalog(driver.ProcessId).ReadCommunityGoalsAsync(token);token.ThrowIfCancellationRequested();if(!Latest())return;
            SetGalaxyCommunityGoals(snapshot.Available?snapshot.SystemAddresses:[]);galaxyCommunityGoalsContext=snapshot.Available?context:"";
            GalaxyCommunityGoalStatus=snapshot.Available?$"原发布缓存 · {snapshot.SystemAddresses.Count:N0} 个实际星系;完整性和刷新时效未确认。已订阅 Journal 活动另行保留。":"原游戏社区活动发布缓存尚未就绪;不把空记录当全球无活动。";
        }
        catch(OperationCanceledException)when(!token.IsCancellationRequested)
        {if(Latest()){SetGalaxyCommunityGoals([]);galaxyCommunityGoalsContext="";GalaxyCommunityGoalStatus="原社区活动缓存读取超时;本机有效已订阅记录保留。";}}
        catch(Exception error)when(error is IOException or TimeoutException or InvalidDataException or InvalidOperationException or System.Text.Json.JsonException or KeyNotFoundException or FormatException)
        {if(Latest()){SetGalaxyCommunityGoals([]);galaxyCommunityGoalsContext="";GalaxyCommunityGoalStatus="原社区活动缓存尚未确认:"+error.Message;}}
    }
    [RelayCommand] private async Task SelectGalaxyCommunityGoalAsync(GalaxyAccountMapTarget? target)
    {
        if(target is null||galaxyCommunityGoalsContext.Length==0||galaxyCommunityGoalsContext!=ThargoidContext())return;
        var context=galaxyCommunityGoalsContext;var request=++galaxyMapTargetSelectionVersion;
        bool Latest()=>request==galaxyMapTargetSelectionVersion&&context==ThargoidContext()&&context==galaxyCommunityGoalsContext&&galaxyCommunityGoalAddresses.Contains(target.SystemAddress);
        try
        {
            var system=galaxyCache.Values.FirstOrDefault(s=>s.SystemAddress==target.SystemAddress&&s.Position.IsValid&&!string.IsNullOrWhiteSpace(s.Name));
            if(system is null)
            {
                if(runtime.FlightDriver is not NativeFlightControlDriver{SupportsGalaxyResolveAddress:true}||NativeGalaxy() is not {} catalog)
                {GalaxyCommunityGoalStatus="已读取实际活动星系地址,尚未连接精确地址解析。";return;}
                system=await catalog.ResolveAddressAsync(target.SystemAddress,lifetime.Token);
            }
            if(!Latest())return;if(system.SystemAddress!=target.SystemAddress||!system.Position.IsValid)throw new InvalidDataException("社区活动地址与实际目录记录不一致。");
            system=RememberGalaxy(system);GalaxySelection=system;GalaxyCentre=system.Position;PublishGalaxy();GalaxyCommunityGoalStatus="已定位原缓存中的社区活动星系 · "+system.Name;
        }
        catch(OperationCanceledException){ }
        catch(Exception error)when(error is IOException or TimeoutException or InvalidDataException or InvalidOperationException or KeyNotFoundException or System.Text.Json.JsonException)
        {if(Latest())GalaxyCommunityGoalStatus="社区活动定位尚未完成:"+error.Message;}
    }
}