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

internal static class NativeDockedServicesAcceptance
{
    public static async Task RunAsync(int pid,string path)
    {
        if(File.Exists(path))throw new IOException("Use a new evidence 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||initial.CurrentShipId!=7||!initial.StationServicesKnown||!initial.CargoReliable||initial.CargoCount!=16||initial.Inventory.Any(x=>x.Key!="drones"))throw new InvalidOperationException("Expected the restored, docked acceptance ship.");
        await using var protection=await FlightTestProtectionScope.ArmAsync(pid);var channel=new NativeCommerceChannel(pid);var wallets=new List<NativeWalletObservation>();var messages=new List<string>();
        var driver=new NativeTradeCommandDriver(channel,()=>journal.State,Path.ChangeExtension(path,"operations"),(_,_,_)=>throw new InvalidOperationException("No travel in this acceptance."),_=>Task.CompletedTask);
        driver.WalletObserved+=wallets.Add;driver.Status+=message=>{messages.Add(message);Console.WriteLine(message);};
        var before=await channel.CallAsync(new{op="station.services.inventory"},default);var started=DateTimeOffset.UtcNow;string? failure=null;JsonElement? after=null;
        try
        {
            using var stop=new CancellationTokenSource(TimeSpan.FromMinutes(3));await driver.PreflightAsync(stop.Token);
            await driver.ServiceDockedAsync(new(initial.MarketId!.Value,1),_=>Task.CompletedTask,stop.Token);
            after=await channel.CallAsync(new{op="station.services.inventory"},stop.Token);
            if(!journal.State.Docked||journal.State.MarketId!=initial.MarketId||journal.State.CargoCount!=initial.CargoCount||journal.State.CurrentShipId!=initial.CurrentShipId||journal.State.LoadGameAt!=initial.LoadGameAt)throw new InvalidOperationException("Docking, session or cargo changed during maintenance acceptance.");
            if(after.Value.GetProperty("cargoCount").GetInt32()!=before.GetProperty("cargoCount").GetInt32()||Math.Abs(after.Value.GetProperty("fuelLevel").GetDouble()-after.Value.GetProperty("fuelCapacity").GetDouble())>.01)throw new InvalidOperationException("Original inventory or fuel did not confirm maintenance.");
        }
        catch(Exception error){failure=error.ToString();}
        await driver.ReleaseAllAsync();var passed=failure is null;
        await File.WriteAllTextAsync(path,JsonSerializer.Serialize(new{pid,started,finished=DateTimeOffset.UtcNow,passed,initial.MarketId,initial.StationName,initial.CommanderId,initial.CurrentShipId,initial.LoadGameAt,before,after,wallets,messages,failure,screenUse=false,osInputsSent=0,journalEventsSynthesized=0},ToolboxSettings.JsonOptions));
        Console.WriteLine(JsonSerializer.Serialize(new{passed,failure}));if(!passed)throw new InvalidOperationException("Original docked service driver acceptance failed.");
    }
}