using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Channels;
using XFEEDToolBox.Core.Data;
using XFEEDToolBox.Core.Models;
using XFEEDToolBox.Core.Native;
using XFEEDToolBox.Core.Navigation;
using XFEEDToolBox.Core.Session;
using XFEEDToolBox.NativeSupport;
/// One sustained SCO interval on the already selected, clear SC leg. No launch, trade, target replacement or scene transition.
internal static class SCOContinuityAcceptance
{
private const double LightSecond = 299792458;
private const double RequestedActiveSeconds = 20;
private static readonly JsonSerializerOptions EvidenceJson = new(ToolboxSettings.JsonOptions)
{ NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals };
private static readonly JsonSerializerOptions EventJson = new(EvidenceJson) { WriteIndented = false };
private static bool True(JsonElement value, string key) => value.TryGetProperty(key, out var item) && item.ValueKind == JsonValueKind.True;
private static Vector3d Vector(JsonElement value, string key)
{ var array = value.GetProperty(key); return new(array[0].GetDouble(), array[1].GetDouble(), array[2].GetDouble()); }
private static string Cargo(PlayerState state) => JsonSerializer.Serialize(state.Inventory
.OrderBy(item => item.Key, StringComparer.Ordinal).ThenBy(item => item.MissionId)
.Select(item => new { item.Key, item.Count, item.Stolen, item.MissionId, item.Group }), ToolboxSettings.JsonOptions);
internal static async Task RunAsync(int pid, string directory)
{
directory = Path.GetFullPath(directory);
if (Directory.Exists(directory)) throw new IOException("Use a fresh SCO continuity evidence directory.");
var parent = Path.GetDirectoryName(directory)!; Directory.CreateDirectory(parent);
using var exclusive = new FileStream(Path.Combine(parent, "docking-fixture-wait.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
Directory.CreateDirectory(directory);
var stopFile = Path.Combine(directory, "stop");
using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(3));
using var sentinel = new Timer(_ => { if (File.Exists(stopFile)) timeout.Cancel(); }, null, 100, 100);
var token = timeout.Token;
using var process = Process.GetProcessById(pid); var processStarted = process.StartTime.ToUniversalTime().Ticks;
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(token);
var initial = journal.State; var initialCargo = Cargo(initial); var start = DateTimeOffset.UtcNow;
FlightTestProtectionScope? protection = null; NativeSystemFlightControlDriver? driver = null; SystemNavigationTarget? target = null;
var previousGuardian = PersistentProtectionProcess.Current;
string phase = "preflight", stopReason = "not-started"; string? failure = null, writerFailure = null, releaseFailure = null;
var commands = new List(); var stops = new List(); var active = new SortedDictionary();
JsonElement? firstEnable = null, actualOff = null; SystemFlightFrame? firstFrame = null, lastFrame = null;
JsonElement? initialInputOwnership = null, finalInputOwnership = null;
var enableCalls = 0; var inputSubmitted = false; var zeroThrottleObserved = false; var targetPreserved = false; var overallPassed = false;
double initialSpeed = 0, maximumSpeed = 0, maximumHeat = 0, reserve = double.NaN;
var events = Channel.CreateUnbounded