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.Native;

namespace XFEEDToolBox.Core.Navigation;

public sealed partial class NativeSystemFlightControlDriver
{
    public Task EnsureDepartureClearanceAsync(long? localDestinationMarket,CancellationToken token)
        =>FlightControlCleanup.RunAsync(()=>EnsureDepartureClearanceCoreAsync(localDestinationMarket,token),ReleaseOwnedDepartureControlsAsync);
    private async Task EnsureDepartureClearanceCoreAsync(long? localDestinationMarket,CancellationToken token)
    {
        var initial=player();if(initial.Docked)return;
        var fit=NativeSystemFlightObservation.LoadoutFingerprint(initial.Ships.SingleOrDefault(ship=>ship.ShipId==initial.CurrentShipId)
            ??throw new InvalidDataException("当前飞船装配缺失,未接续原版离站。"));
        async Task Check(CancellationToken cancellation)
        {
            cancellation.ThrowIfCancellationRequested();var current=player();
            if(!current.InGame||current.CommanderId!=initial.CommanderId||current.CurrentShipId!=initial.CurrentShipId||current.LoadGameAt!=initial.LoadGameAt
                ||current.Navigation.CurrentSystemAddress!=initial.Navigation.CurrentSystemAddress)
                throw new InvalidDataException("接续原版离站期间飞船、会话或星系改变。");
            if(current.Ships.SingleOrDefault(ship=>ship.ShipId==current.CurrentShipId) is not {} ship||NativeSystemFlightObservation.LoadoutFingerprint(ship)!=fit)
                throw new InvalidDataException("接续原版离站期间实际装配改变,未继续自动驾驶。");
            if(ensureFlightProtection is not null)await ensureFlightProtection(cancellation);
            cancellation.ThrowIfCancellationRequested();
        }
        await InspectAsync(token);await Check(token);
        var raw=await pipe.CallAsync(new{op="read"},token);await Check(token);
        if(StationDepartureContinuity.RequireFreshMain(player(),raw,DateTimeOffset.UtcNow))return;
        var known=StationDepartureContinuity.KnownOrigin(player(),DateTimeOffset.UtcNow);
        SystemNavigationTarget? origin=null;
        async Task SelectForObservation(bool requireOrigin)
        {
            if(known is null&&requireOrigin)throw new InvalidDataException("原版离站尚未确认结束,且本次 Journal 原站身份缺失;未启动后续驾驶。");
            using var source=new NativeCurrentSystemSource(processId,gameHash,player);
            var catalogue=await source.ReadLocationsAsync(token);await Check(token);
            SystemNavigationTarget observation;
            if(known is not null)observation=origin=StationDepartureTarget.ResolveContinuation(catalogue,player(),known);
            else
            {
                // A real catalogue body can expose the original computer's
                // state without inventing a station origin. Never infer an
                // exit path or station identity from this observation target.
                var reference=ulong.Parse(raw.GetProperty("referenceWorldAddress").GetString()!,System.Globalization.CultureInfo.InvariantCulture);
                observation=StationDepartureContinuity.ObservationTarget(catalogue,reference);
            }
            var beforeSelection=await pipe.CallAsync(new{op="read"},token);await Check(token);
            if(StationDepartureContinuity.RequireFreshMain(player(),beforeSelection,DateTimeOffset.UtcNow))
                throw new FlightTelemetryUnavailableException("观察前原版场景已切换,未替换当前飞行目标。");
            // Selection establishes observation only; no launch, axes, boost,
            // FSD, throttle or cancellation command is sent here.
            await SelectAsync(observation,token);await Check(token);
        }
        JsonElement local;
        try{local=await pipe.CallAsync(new{op="local.read"},token);}
        catch(InvalidOperationException error)when(error.Message=="Local target telemetry unavailable or stale")
        {
            await SelectForObservation(requireOrigin:false);
            local=await NavigationRecovery.ReadWithRecoveryAsync(async cancellation=>
            {await Check(cancellation);return await pipe.CallAsync(new{op="local.read"},cancellation);},null,token,TimeSpan.FromSeconds(10));
        }
        await Check(token);raw=await pipe.CallAsync(new{op="read"},token);await Check(token);
        if(!StationDepartureContinuity.NeedsContinuation(player(),raw,local,localDestinationMarket,DateTimeOffset.UtcNow))return;
        if(origin is null)await SelectForObservation(requireOrigin:true);
        var undocked=player().Navigation.UndockedAt??throw new InvalidDataException("本次实际离站回执缺失。");
        await DepartDockedStationAsync(token,restoreDestination:false,resumeOrigin:origin,resumedUndockedAt:undocked,
            returnToOrigin:localDestinationMarket==origin!.MarketId);
    }
}