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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.Core.Native;
using XFEEDToolBox.Core.Navigation;
using XFEEDToolBox.Core.Services;
using XFEEDToolBox.NativeSupport;

namespace XFEEDToolBox.ViewModels;

public partial class ToolboxViewModel
{
    private bool currentSystemExtensionsAttempted;
    private DateTimeOffset currentSystemExtensionRetryAt;
    private static SystemNavigationTarget SystemTarget(CurrentSystemSnapshot snapshot,SystemLocation location)
        =>new(snapshot.SystemAddress,location.WorldAddress,location.Name,
            location.Category is SystemLocationCategory.Station or SystemLocationCategory.Carrier?SystemTargetKind.Station:
            location.Category==SystemLocationCategory.Settlement?SystemTargetKind.SurfacePort:SystemTargetKind.Orbit,location.MarketId)
            {NativeKey=location.Key,NativeKind=location.NativeKind,NativeIdentity=location.Identity};
    private async Task RefreshCurrentSystemControlAsync(int pid,string hash,CancellationToken token)
    {
        if(currentSystemCapabilityDriver is not NativeSystemFlightControlDriver driver)
        {
            Task Protection(CancellationToken cancellation)=>EnsureNativeFlightProtectionAsync(pid,hash,cancellation);
            driver=new(pid,hash,()=>runtime.Player,new NativeGameInputChannel(pid,hash,NativeSupportPackage.Directory),Protection,useNativeSupercruiseAssist:false);
            currentSystemCapabilityDriver=driver;currentSystemExtensionsAttempted=false;currentSystemCapabilitiesReadAt=default;
        }
        var capabilities=await driver.InspectAsync(token);
        if(capabilities.LocalFlightReady&&!currentSystemExtensionsAttempted&&!SystemFlightActive&&DateTimeOffset.UtcNow>=currentSystemExtensionRetryAt
            &&(!Environment.GetCommandLineArgs().Contains("--system-map-only")||Environment.GetCommandLineArgs().Contains("--prepare-system-map-interfaces"))
            &&!Environment.GetCommandLineArgs().Contains("--activity-only"))
        {
            currentSystemExtensionsAttempted=true;
            var failed=false;
            try{await driver.ConfigureOriginalDockingControlAsync(Path.Combine(NativeSupportPackage.Directory,"DockingControl"),token);runtime.ResolveDiagnostic("current-system.docking","星系内停靠","停靠扩展已连接。");}
            catch(Exception error)when(!token.IsCancellationRequested)
            {failed=true;runtime.ReportDiagnostic("current-system.docking","星系内停靠",error,ActivitySeverity.Warning,"停靠扩展未连接,暂不开放空间站自动停靠。");}
            try
            {
                var channel=new NativeCruiseControlChannel(pid,hash);await channel.PrepareAsync(Path.Combine(NativeSupportPackage.Directory,"CruiseControl"),token);
                await driver.ConfigureOriginalCruiseControlAsync(()=>runtime.Settings.NavigationFuelReserve,token:token,diagnostics:runtime.RecordScoDiagnostic);
                runtime.ResolveDiagnostic("current-system.overcharge","星系内驾驶","SCO扩展已连接。");
            }
            catch(Exception error)when(!token.IsCancellationRequested)
            {failed=true;runtime.ReportDiagnostic("current-system.overcharge","星系内驾驶",error,ActivitySeverity.Warning,"SCO暂未连接;可用航段仍支持普通超巡。");}
            if(failed){currentSystemExtensionsAttempted=false;currentSystemExtensionRetryAt=DateTimeOffset.UtcNow.AddSeconds(30);}
            capabilities=await driver.InspectAsync(token);
        }
        currentSystemCapabilities=capabilities;currentSystemCapabilitiesReadAt=DateTimeOffset.UtcNow;
        SystemFlightReady=capabilities.LocalFlightReady&&capabilities.TelemetryReady;
        UpdateSystemControlAvailability();
    }
    private string? CurrentSystemTargetIssue()
    {
        if(SelectedSystemLocation is not {} selected||currentSystemSnapshot is not {} snapshot)return "在场景或左侧列表选择目的地。";
        if(selected.Record.Position is not {Finite:true})return "地点坐标尚未确认,暂不能自动前往。";
        if(currentSystemCapabilities is not {} capabilities)return "正在检查当前游戏的驾驶接口。";
        if(!capabilities.TelemetryReady)return capabilities.TelemetryIssue??"等待最新飞船遥测。";
        if(currentSystemReadFailures>0)return "地点遥测正在恢复,暂不能启动驾驶。";
        return SystemDestinationPolicy.UnsupportedReason(snapshot,selected.Record,capabilities,NavigationAssists.From(runtime.Player));
    }
    private void UpdateSystemControlAvailability()
    {
        var issue=CurrentSystemTargetIssue();
        if(NavigationActive)issue="星图导航正在运行,停止后可开始星系内驾驶。";
        else if(RunActive)issue="跑商任务正在运行,停止后可开始独立驾驶。";
        else if(SessionBusy)issue="正在处理游戏会话,稍后可开始驾驶。";
        SystemControlAvailability=issue??(SelectedSystemLocation?.Record.Category is SystemLocationCategory.Station or SystemLocationCategory.Carrier
            ?"可自动前往 · 原生停靠辅助接管进场":"可自动前往 · 工具箱控制超巡进近");
        if(!SystemFlightActive)SystemFlightStatus=SystemControlAvailability;
        StartSystemFlightCommand.NotifyCanExecuteChanged();
    }
}