using System.Globalization;
using System.Text.Json;
namespace XFEEDToolBox.Core.Navigation;
public sealed class FlightDestinationChangedException(string expected,string actual,Exception inner)
:InvalidOperationException($"游戏目标已从“{expected}”改为“{actual}”,当前航段已交还控制。",inner);
/// <summary>Read-only evidence after a failed local sample; it never selects, restarts or authorizes flight.</summary>
public sealed record LocalFlightTelemetryStatus(bool Selected,bool TelemetryReady,string SampleStage,bool TargetChanged,string ActualTarget)
{
public static LocalFlightTelemetryStatus Parse(JsonElement status,long generation,SystemNavigationTarget expected)
{
try
{
if(generation<=0||status.GetProperty("generation").GetInt64()!=generation)throw new InvalidDataException("当前地点诊断回执不属于本次驾驶。");
var selected=status.GetProperty("selected").GetBoolean();var ready=status.GetProperty("telemetryReady").GetBoolean();
var stage=status.GetProperty("sampleStage").GetString()??throw new InvalidDataException("当前地点诊断缺少采样阶段。");
var actual=status.GetProperty("target");var key=actual.GetProperty("key").GetInt32();var kind=actual.GetProperty("kind").GetInt32();
var name=actual.GetProperty("name").GetString()??"";var market=actual.GetProperty("marketId").GetInt64();
var bodyKnown=ulong.TryParse(actual.GetProperty("bodyAddress").GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var body)&&body>0;
// A missing catalogue entry during a transition is not evidence of
// another destination. Require the complete actual identity first.
var known=key>=0&&kind>0&&bodyKnown&&name.Length>0;
var matches=known&&key==expected.NativeKey&&kind==expected.NativeKind&&body==expected.BodyAddress
&&name.Equals(expected.Name,StringComparison.Ordinal)&&market==(expected.MarketId??uint.MaxValue);
if(selected&&known&&!matches||ready&&(!selected||!matches))throw new InvalidDataException("当前地点诊断的选中标志和实际身份不一致。");
return new(selected,ready,stage,!selected&&known&&!matches,name);
}
catch(Exception error)when(error is KeyNotFoundException or InvalidOperationException or FormatException or OverflowException)
{throw new InvalidDataException("当前地点诊断结构未通过核对。",error);}
}
}
using System.Globalization;
using System.Text.Json;
namespace XFEEDToolBox.Core.Navigation;
public sealed class FlightDestinationChangedException(string expected,string actual,Exception inner)
:InvalidOperationException($"游戏目标已从“{expected}”改为“{actual}”,当前航段已交还控制。",inner);
/// <summary>Read-only evidence after a failed local sample; it never selects, restarts or authorizes flight.</summary>
public sealed record LocalFlightTelemetryStatus(bool Selected,bool TelemetryReady,string SampleStage,bool TargetChanged,string ActualTarget)
{
public static LocalFlightTelemetryStatus Parse(JsonElement status,long generation,SystemNavigationTarget expected)
{
try
{
if(generation<=0||status.GetProperty("generation").GetInt64()!=generation)throw new InvalidDataException("当前地点诊断回执不属于本次驾驶。");
var selected=status.GetProperty("selected").GetBoolean();var ready=status.GetProperty("telemetryReady").GetBoolean();
var stage=status.GetProperty("sampleStage").GetString()??throw new InvalidDataException("当前地点诊断缺少采样阶段。");
var actual=status.GetProperty("target");var key=actual.GetProperty("key").GetInt32();var kind=actual.GetProperty("kind").GetInt32();
var name=actual.GetProperty("name").GetString()??"";var market=actual.GetProperty("marketId").GetInt64();
var bodyKnown=ulong.TryParse(actual.GetProperty("bodyAddress").GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var body)&&body>0;
// A missing catalogue entry during a transition is not evidence of
// another destination. Require the complete actual identity first.
var known=key>=0&&kind>0&&bodyKnown&&name.Length>0;
var matches=known&&key==expected.NativeKey&&kind==expected.NativeKind&&body==expected.BodyAddress
&&name.Equals(expected.Name,StringComparison.Ordinal)&&market==(expected.MarketId??uint.MaxValue);
if(selected&&known&&!matches||ready&&(!selected||!matches))throw new InvalidDataException("当前地点诊断的选中标志和实际身份不一致。");
return new(selected,ready,stage,!selected&&known&&!matches,name);
}
catch(Exception error)when(error is KeyNotFoundException or InvalidOperationException or FormatException or OverflowException)
{throw new InvalidDataException("当前地点诊断结构未通过核对。",error);}
}
}