XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using XFEEDToolBox.Views;

namespace XFEEDToolBox;

public partial class App
{
    private static async Task<object> RunSystemCompositionProbeAsync(string directory)
    {
        var recording=GetArgument(Environment.GetCommandLineArgs(),"--recorded-system-map");
        if(ViewModel.DemoMode)throw new InvalidOperationException("Composition acceptance must not use demonstration data.");
        if(recording is not null)
        {
            var isolated=Path.GetFullPath(Path.Combine(directory,"isolated-data"));
            if(!ViewModel.OfflinePreview||!Path.GetFullPath(Runtime.DataDirectory).Equals(isolated,StringComparison.OrdinalIgnoreCase)||!Path.IsPathFullyQualified(recording))
                throw new InvalidOperationException("Recorded actual locations require the explicit offline report-directory fixture.");
            var snapshot=JsonSerializer.Deserialize<XFEEDToolBox.Core.Navigation.CurrentSystemSnapshot>(await File.ReadAllTextAsync(recording),XFEEDToolBox.Core.Models.ToolboxSettings.JsonOptions)
                ??throw new InvalidDataException("Missing recorded actual snapshot.");
            if(snapshot.SystemAddress<=0||snapshot.Locations.Count==0||snapshot.ObservedAt==default)throw new InvalidDataException("Recorded actual location identity is missing.");
            ViewModel.ApplyCurrentSystemSnapshot(snapshot);
        }
        else if(ViewModel.OfflinePreview)throw new InvalidOperationException("Live composition acceptance requires actual game observations.");
        var observations=new List<object>();var selectionChecks=new List<object>();
        var createdAt=DateTimeOffset.UtcNow;
        MainWindow.AppWindow.Resize(new(3840,2088));
        AppShellPage.Current!.Navigate(typeof(CurrentSystemPage));
        var page=(CurrentSystemPage)AppShellPage.Current.CurrentPage!;
        var until=DateTimeOffset.UtcNow.AddSeconds(30);
        while((ViewModel.CurrentSystemSceneSnapshot is null||page.MapForProbe.FrameCount==0)&&DateTimeOffset.UtcNow<until)
            await Task.Delay(100);
        if(ViewModel.CurrentSystemSceneSnapshot is not{} source||source.Locations.Count==0)
            throw new InvalidOperationException("Actual current-system locations were not available: "+ViewModel.CurrentSystemStatus);
        async Task Stage(string name)
        {
            await Task.Delay(300);page.UpdateLayout();
            var presentation=JsonSerializer.SerializeToElement(page.MapForProbe.PresentationForProbe);
            var orbits=JsonSerializer.SerializeToElement(page.MapForProbe.OrbitPresentationForProbe);
            if(presentation.GetProperty("container").GetProperty("effectiveWidth").GetDouble()<=1
                ||presentation.GetProperty("container").GetProperty("effectiveHeight").GetDouble()<=1
                ||presentation.GetProperty("sprite").GetProperty("effectiveWidth").GetDouble()<=1
                ||presentation.GetProperty("sprite").GetProperty("effectiveHeight").GetDouble()<=1
                ||page.MapForProbe.FrameCount<=0||page.MapForProbe.GraphicsError.Length>0)
                throw new InvalidOperationException("The natural composition path has no nonzero presentation area: "+presentation);
            var ready=new{stage=name,pid=Environment.ProcessId,hwnd=WinRT.Interop.WindowNative.GetWindowHandle(MainWindow).ToInt64(),
                presentation,orbits,system=ViewModel.CurrentSystemTitle,locationCount=ViewModel.CurrentSystemLocations.Count,
                observedAt=ViewModel.CurrentSystemSceneSnapshot?.ObservedAt,freezeForXamlCalled=false,renderNowForProbeCalled=false,
                recordedActualLocations=recording is not null,gameCommandsInvoked=false,tradeOrdersSubmitted=false};
            await File.WriteAllTextAsync(Path.Combine(directory,"composition-ready.json"),JsonSerializer.Serialize(ready));
            var acknowledgement=Path.Combine(directory,"captured-"+name+".json");
            var expires=DateTimeOffset.UtcNow.AddSeconds(35);
            while(!File.Exists(acknowledgement)&&DateTimeOffset.UtcNow<expires)await Task.Delay(100);
            if(!File.Exists(acknowledgement))throw new TimeoutException("No external background-window capture acknowledgement for "+name);
            using var captured=JsonDocument.Parse(await File.ReadAllTextAsync(acknowledgement));
            observations.Add(new{ready,capture=captured.RootElement.Clone()});
        }
        await Stage("loaded");
        var list=(ListView)page.FindName("LocationsList");
        var preferred=source.Orbits.FirstOrDefault(orbit=>orbit.Renderable)?.ChildIdentity;
        var row=ViewModel.CurrentSystemLocations.FirstOrDefault(value=>value.Record.Identity==preferred)
            ??ViewModel.CurrentSystemLocations.First(value=>value.Record.Position is {Finite:true});
        list.SelectedItem=row;list.ScrollIntoView(row);await Task.Delay(200);
        var fromList=ViewModel.SelectedSystemLocation?.Record.Identity==row.Record.Identity
            &&page.MapForProbe.ProjectedLocations.Any(point=>point.Selected&&point.Node.Location.Identity==row.Record.Identity);
        selectionChecks.Add(new{direction="list-to-3D",passed=fromList,identity=row.Record.Identity});
        if(!fromList)throw new InvalidOperationException("List selection did not reach the naturally rendered 3D scene.");
        page.MapForProbe.ResetCamera();await Task.Delay(150);
        var hit=page.MapForProbe.InteractiveLocations.FirstOrDefault(point=>point.X>18&&point.X<page.MapForProbe.ProjectionWidth-18&&point.Y>80&&point.Y<page.MapForProbe.ActualHeight-45)
            ??throw new InvalidOperationException("No actual marker is available for production hit testing.");
        var expected=XFEEDToolBox.Core.Rendering.LocalSystemScene.HitTest(page.MapForProbe.InteractiveLocations,hit.X,hit.Y).First();
        page.MapForProbe.SelectAtForProbe(new(hit.X,hit.Y));await Task.Delay(150);
        var fromMap=ViewModel.SelectedSystemLocation?.Record.Identity==expected.Node.Location.Identity
            &&(list.SelectedItem as XFEEDToolBox.Core.Presentation.SystemLocationLiveRow)?.Record.Identity==expected.Node.Location.Identity;
        selectionChecks.Add(new{direction="3D-to-list",passed=fromMap,identity=expected.Node.Location.Identity});
        if(!fromMap||ViewModel.SystemFlightActive)throw new InvalidOperationException("3D selection did not reach the list, or unexpectedly started flight.");
        MainWindow.AppWindow.Resize(new(1280,1040));await Stage("resized");
        AppShellPage.Current.Navigate(typeof(SettingPage));await Task.Delay(150);
        AppShellPage.Current.Navigate(typeof(CurrentSystemPage));await Task.Delay(150);
        page=(CurrentSystemPage)AppShellPage.Current.CurrentPage!;
        await Stage("reloaded");
        return new{createdAt,completedAt=DateTimeOffset.UtcNow,observations,selectionChecks,sourceSystem=source.SystemName,sourceSystemAddress=source.SystemAddress,
            sourceObservedAt=source.ObservedAt,sourceOrbitRecords=source.Orbits.Count,sourceRenderableOrbitRecords=source.Orbits.Count(orbit=>orbit.Renderable),
            recordedActualLocations=recording is not null,recording,syntheticLocations=false,freezeForXamlCalled=false,renderNowForProbeCalled=false,
            gameCommandsInvoked=false,tradeOrdersSubmitted=false,visualReview="External PrintWindow images must be inspected; nonzero sizes and successful capture alone are not a pixel verdict."};
    }
}