using System.Text.Json; using XFEEDToolBox.Core.Data; using XFEEDToolBox.Core.Galaxy; using XFEEDToolBox.Core.Models; using XFEEDToolBox.Core.Navigation; internal static class InterstellarTravelAcceptance { public static async Task RunAsync(int pid,string name,string reportPath,CancellationToken externalToken=default,bool manualVisual=false) { if(File.Exists(reportPath)||File.Exists(reportPath+".jsonl"))throw new IOException("Use a new report path."); var settings=ToolboxSettings.Load(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"XFEstudio","XFEEDToolBox","settings.json")); await using var journal=new JournalMonitor(settings.JournalDirectory);await journal.StartAsync();var initial=journal.State; if(!initial.InGame||initial.Docked)throw new InvalidOperationException("Interstellar acceptance starts only after a verified departure."); var hello=await new NavigationPipeClient(pid).CallAsync(new{op="describe"});var hash=hello.GetProperty("gameSha256").GetString()!; var destination=await new NativeGalaxyCatalog(pid).FindAsync(name);var target=new NavWaypoint(destination.SystemAddress!.Value,destination.Name,destination.Position,destination.StarClass); var started=DateTimeOffset.UtcNow;FlightTestProtectionScope? protection=await FlightTestProtectionScope.ArmAsync(pid,!manualVisual); async Task EnsureProtection(CancellationToken token) { var current=journal.State; if(current.CurrentShipId!=initial.CurrentShipId||current.CommanderId!=initial.CommanderId||current.LoadGameAt!=initial.LoadGameAt||current.CargoCount!=initial.CargoCount) throw new InvalidOperationException("Interstellar session, ship or reserved cargo changed."); if(protection?.Stopped!=true)return; var at=new[]{current.Navigation.SupercruiseEnteredAt,current.Navigation.SupercruiseExitedAt,current.Navigation.LastJumpAt}.Max(); if(at is null||atTimeSpan.FromSeconds(30))throw new InvalidOperationException("Unconfirmed interstellar entity transition."); await protection.DisposeAsync();protection=await FlightTestProtectionScope.ArmAsync(pid,!manualVisual); } var driver=new NativeFlightControlDriver(pid,hash,()=>journal.State,()=>settings.NavigationFuelReserve, new NativeGameInputChannel(pid,hash,Path.GetFullPath("XFEEDToolBox/PluginPackage")),EnsureProtection); var runner=new NavigationRunner(()=>journal.State);GameRoutePlan? plan=null;string? failure=null;var released=false;double peakHeat=0;var frames=0;var last=""; await using var log=new StreamWriter(new FileStream(reportPath+".jsonl",FileMode.CreateNew,FileAccess.Write,FileShare.Read));var gate=new object(); void Record(object value){lock(gate){log.WriteLine(JsonSerializer.Serialize(value));log.Flush();}} using var stop=CancellationTokenSource.CreateLinkedTokenSource(externalToken);stop.CancelAfter(TimeSpan.FromMinutes(20)); using var sentinel=new Timer(_=>{if(File.Exists(reportPath+".stop"))stop.Cancel();},null,100,100); driver.Observed+=(observation,decision)=> { var frame=observation.Frame;peakHeat=Math.Max(peakHeat,frame.HeatPercent??100);frames++;Record(new{at=DateTimeOffset.UtcNow,frame,decision}); if(peakHeat>=75){failure="Interstellar acceptance heat bound exceeded.";stop.Cancel();} }; runner.Progress+=progress=>{Record(new{at=DateTimeOffset.UtcNow,progress});var key=progress.NextSystem+":"+progress.Message;if(key!=last){last=key;Console.WriteLine(JsonSerializer.Serialize(progress));}}; try { if(!(await driver.PreflightAsync(stop.Token)).Ready)throw new InvalidOperationException("Original interstellar controls are unavailable."); plan=await new GameRoutePlanner(()=>journal.State).PlanAsync(target,driver,null,stop.Token); Record(new{at=DateTimeOffset.UtcNow,actualGameRoute=plan}); Console.WriteLine(JsonSerializer.Serialize(new{planned=true,destination=target.Name,waypoints=plan.Route.Count})); await runner.RunAsync(target.Name,driver,stop.Token); if(runner.State!=NavigationRunState.Completed||journal.State.Navigation.CurrentSystemAddress!=target.SystemAddress||journal.State.Navigation.LastJumpAt