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.Models;
using XFEEDToolBox.Core.Navigation;

namespace XFEEDToolBox.Core.Session;

/// <summary>Authorizes only an original-menu update, never flight inputs or orders.</summary>
public sealed class SessionMaintenanceFlightGate(PlayerState initial)
{
    private DateTimeOffset? stableSince;
    private string? entity;
    private string? world;
    private double? previousHull;
    public static readonly TimeSpan StableDuration=TimeSpan.FromSeconds(10);
    private static bool Flag(JsonElement row,string name)=>row.GetProperty(name).GetBoolean();
    private static bool Fresh(JsonElement row,DateTimeOffset now)
    {
        var at=DateTimeOffset.FromUnixTimeMilliseconds(row.GetProperty("observedAtUnixMilliseconds").GetInt64());
        return row.GetProperty("sequence").GetInt64()>0&&at<=now.AddMilliseconds(100)&&now-at<=TimeSpan.FromMilliseconds(350);
    }
    public bool Observe(PlayerState current,JsonElement raw,JsonElement local,DateTimeOffset now)
    {
        if(string.IsNullOrEmpty(initial.CommanderId)||initial.CurrentShipId is null||current.CommanderId!=initial.CommanderId
            ||current.CurrentShipId!=initial.CurrentShipId||current.LoadGameAt!=initial.LoadGameAt||current.CargoCapacity!=initial.CargoCapacity)
            throw new InvalidOperationException("更新前身份、飞船或会话已改变,已保留当前游戏。");
        if(Flag(raw,"playerOverride"))throw new InvalidOperationException("玩家已接管,未继续插件更新。");
        var health=raw.GetProperty("shipHealth");var docking=local.GetProperty("docking");var hull=health.GetProperty("hullFraction").GetDouble();
        var id=raw.GetProperty("entityId").GetString();var reference=raw.GetProperty("referenceWorldAddress").GetString();
        var ready=initial.CargoReliable&&initial.CargoCount==0&&current.InGame&&!current.Docked&&current.CargoReliable&&current.CreditsReliable&&current.CargoCount==0
            &&current.StatusReadAt is{} read&&read<=now.AddSeconds(1)&&now-read<=TimeSpan.FromSeconds(2)
            &&!NavigationSafety.HasExternalThreat(current)&&!NavigationSafety.UnhealthyHull(current)
            &&Flag(raw,"inGame")&&Flag(raw,"localPlayerShipVerified")&&Fresh(raw,now)&&Fresh(local,now)
            &&Flag(health,"verified")&&Fresh(health,now)&&health.GetProperty("sequence").GetInt64()==raw.GetProperty("sequence").GetInt64()
            &&!Flag(health,"damageObserved")&&!Flag(health,"emergency")&&double.IsFinite(hull)&&hull is >0 and <=1
            &&ulong.TryParse(id,out var parsed)&&parsed>0&&id==local.GetProperty("entityId").GetString()&&id==health.GetProperty("entityId").GetString()
            &&ulong.TryParse(reference,out var worldId)&&worldId>0&&reference==local.GetProperty("referenceWorldAddress").GetString()
            &&local.GetProperty("rawFsdState").GetInt32()==2&&!Flag(local,"inSupercruise")
            &&Math.Abs(local.GetProperty("speedMetresPerSecond").GetDouble())<=.1&&Math.Abs(local.GetProperty("throttle").GetDouble())<=.001
            &&Flag(docking,"verified")&&!Flag(docking,"nativeDutyActive")&&!Flag(docking,"nativeLaunchActive");
        if(!ready){stableSince=null;entity=world=null;previousHull=null;return false;}
        if(entity!=id||world!=reference){stableSince=null;previousHull=null;entity=id;world=reference;}
        if(previousHull is {} previous&&hull<previous-.000001)stableSince=null;
        previousHull=hull;
        stableSince??=now;return now-stableSince>=StableDuration;
    }
}