using XFEEDToolBox.Core.Models; namespace XFEEDToolBox.Core.Navigation; public sealed record GameRouteMirrorUpdate(bool Changed,NavWaypoint? Destination,bool Cleared,bool Arrived,bool Replaced); /// Mirrors actual game route observations; browsing alone cannot fabricate or clear a route. public sealed class GameRouteMirror(Func? clock=null) { private string commander=""; private IReadOnlyList previous=[]; private DateTimeOffset? observedAt; public GameRouteMirrorUpdate Observe(PlayerState player,bool ownPlanning=false) { if(ownPlanning||!player.InGame)return new(false,null,false,false,false); if(commander!=player.CommanderId){commander=player.CommanderId;previous=[];observedAt=null;} var nav=player.Navigation;var route=nav.Route; var destination=route.Count>=2?route[^1]:null; var changed=!previous.SequenceEqual(route)||observedAt!=nav.RouteAt; if(!changed)return new(false,destination,false,false,false); var oldEnd=previous.LastOrDefault(); if(route.Count==0&&oldEnd is not null&&nav.JumpInProgress &&FinalJumpRouteClear.Matches(nav,oldEnd,player.LoadGameAt??DateTimeOffset.MinValue,GameJournalClock.Now(player,(clock??(()=>DateTimeOffset.UtcNow))()))) return new(false,oldEnd,false,false,false); var arrived=route.Count==0&&oldEnd?.SystemAddress==nav.CurrentSystemAddress; var cleared=route.Count==0&&previous.Count>0&&!arrived; var replaced=oldEnd is not null&&destination is not null&&oldEnd.SystemAddress!=destination.SystemAddress; previous=route.ToArray();observedAt=nav.RouteAt; return new(true,destination,cleared,arrived,replaced); } } public interface IGameRouteCancellationDriver { bool SupportsGameRouteCancellation{get;} Task RequestGameRouteCancellationAsync(long expectedDestination,CancellationToken token); Task ReadGameRouteStatusAsync(long generation,long destinationAddress,CancellationToken token); } public sealed class GameRouteCancellation(Func player,TimeSpan? timeout=null) { public async Task CancelAsync(long expectedDestination,IGameRouteCancellationDriver driver,Action? progress=null,CancellationToken token=default) { var before=player(); if(!driver.SupportsGameRouteCancellation)throw new InvalidOperationException("当前游戏加载的插件尚未接入航线取消;请在游戏下次启动后使用更新的插件。"); if(!before.InGame||before.Navigation.Route.LastOrDefault()?.SystemAddress!=expectedDestination||before.Navigation.JumpInProgress) throw new InvalidOperationException("游戏航线或飞行状态已经改变,请重新确认目的地。"); using var deadline=CancellationTokenSource.CreateLinkedTokenSource(token);deadline.CancelAfter(timeout??TimeSpan.FromSeconds(15)); var generation=await driver.RequestGameRouteCancellationAsync(expectedDestination,deadline.Token); if(generation<=0)throw new InvalidDataException("航线取消缺少有效回执。"); progress?.Invoke("正在取消游戏航线,等待游戏确认…"); while(true) { deadline.Token.ThrowIfCancellationRequested();var current=player(); if(!current.InGame||current.LoadGameAt!=before.LoadGameAt||current.CommanderId!=before.CommanderId)throw new InvalidOperationException("游戏会话改变,已停止等待航线取消。"); if(current.Navigation.Route.LastOrDefault() is {} end&&end.SystemAddress!=expectedDestination)throw new InvalidOperationException("游戏内已规划另一条航线,已保留新的航线。"); GameRouteStatus status; try{status=await driver.ReadGameRouteStatusAsync(generation,expectedDestination,deadline.Token);} catch(Exception error)when(NavigationRecovery.CanRetry(error,deadline.Token)){await Task.Delay(100,deadline.Token);continue;} if(status.Generation!=generation||status.Failed)throw new InvalidOperationException("游戏未确认本次航线取消,已保留界面中的航线。"); if(status.Applied&&status.TopologyFresh&&status.WaypointCount==0&¤t.Navigation.Route.Count==0 &¤t.Navigation.RouteAt>=before.Navigation.RouteAt)return; await Task.Delay(80,deadline.Token); } } }