using System.Diagnostics;
using System.Text.Json;
using XFEEDToolBox.Core.Navigation;
internal static class SupercruiseResponseProbe
{
public static async Task RunAsync(int pid,string profilePath,string output)
{
if(File.Exists(output))throw new IOException("Use a new response evidence path.");
using var profile=JsonDocument.Parse(await File.ReadAllTextAsync(profilePath));
using var process=Process.GetProcessById(pid);var started=process.StartTime;
if(!string.Equals(process.MainModule?.FileName,profile.RootElement.GetProperty("image").GetString(),StringComparison.OrdinalIgnoreCase))throw new InvalidDataException("Unexpected selected process.");
var pipe=new NavigationPipeClient(pid);var owner=Guid.NewGuid().ToString("N");var rows=new List<object>();
var hello=await pipe.CallAsync(new{op="describe"});if(hello.GetProperty("gameSha256").GetString()!=profile.RootElement.GetProperty("sha256").GetString())throw new InvalidDataException("Game build mismatch.");
Vector3d V(JsonElement e,string key){var v=e.GetProperty(key);return new(v[0].GetDouble(),v[1].GetDouble(),v[2].GetDouble());}
string? error=null;var watch=Stopwatch.StartNew();var initial=await pipe.CallAsync(new{op="read"});var forward=V(initial,"forward");var position=V(initial,"position");
try
{
while(watch.Elapsed.TotalSeconds<8)
{
if(process.HasExited||process.StartTime!=started)throw new InvalidOperationException("Selected process changed.");
var frame=await pipe.CallAsync(new{op="read"});var radius=V(frame,"position").Length;var exclusion=frame.GetProperty("geometry").GetProperty("exclusionRadius").GetDouble();
var heat=frame.GetProperty("heatPercent").GetDouble();var speed=frame.GetProperty("speedMetresPerSecond").GetDouble();
if(!frame.GetProperty("inGame").GetBoolean()||frame.GetProperty("rawFsdState").GetInt32()!=2||frame.GetProperty("playerOverride").GetBoolean()
||heat>=85||radius<exclusion*1.25||speed>2000000||(V(frame,"position")-position).Length>20000000)throw new InvalidOperationException("Bounded SC response envelope reached.");
var seconds=watch.Elapsed.TotalSeconds;var throttle=seconds<3?0:.5;var yaw=seconds<1.5?.3:seconds<3?-.3:0;
var applied=await pipe.CallAsync(new{op="calibrate.axes",owner,sequence=frame.GetProperty("sequence").GetInt64(),ttlMilliseconds=180,
axes=new[]{0d,0,0,yaw,0,0},throttle});
rows.Add(new{seconds,heat,speed,radius,throttle,yaw,forward=V(frame,"forward"),angleFromStart=Vector3d.AngleDegrees(forward,V(frame,"forward")),applied});
await Task.Delay(40);
}
}
catch(Exception failure){error=failure.Message;}
finally{await pipe.CallAsync(new{op="release",owner});}
await File.WriteAllTextAsync(output,JsonSerializer.Serialize(new{pid,started,rows,error,released=true,screenUse=false,fsdInputsSent=0},new JsonSerializerOptions{WriteIndented=true}));
Console.WriteLine(JsonSerializer.Serialize(new{pid,samples=rows.Count,error,released=true}));
}
}
using System.Diagnostics;
using System.Text.Json;
using XFEEDToolBox.Core.Navigation;
internal static class SupercruiseResponseProbe
{
public static async Task RunAsync(int pid,string profilePath,string output)
{
if(File.Exists(output))throw new IOException("Use a new response evidence path.");
using var profile=JsonDocument.Parse(await File.ReadAllTextAsync(profilePath));
using var process=Process.GetProcessById(pid);var started=process.StartTime;
if(!string.Equals(process.MainModule?.FileName,profile.RootElement.GetProperty("image").GetString(),StringComparison.OrdinalIgnoreCase))throw new InvalidDataException("Unexpected selected process.");
var pipe=new NavigationPipeClient(pid);var owner=Guid.NewGuid().ToString("N");var rows=new List<object>();
var hello=await pipe.CallAsync(new{op="describe"});if(hello.GetProperty("gameSha256").GetString()!=profile.RootElement.GetProperty("sha256").GetString())throw new InvalidDataException("Game build mismatch.");
Vector3d V(JsonElement e,string key){var v=e.GetProperty(key);return new(v[0].GetDouble(),v[1].GetDouble(),v[2].GetDouble());}
string? error=null;var watch=Stopwatch.StartNew();var initial=await pipe.CallAsync(new{op="read"});var forward=V(initial,"forward");var position=V(initial,"position");
try
{
while(watch.Elapsed.TotalSeconds<8)
{
if(process.HasExited||process.StartTime!=started)throw new InvalidOperationException("Selected process changed.");
var frame=await pipe.CallAsync(new{op="read"});var radius=V(frame,"position").Length;var exclusion=frame.GetProperty("geometry").GetProperty("exclusionRadius").GetDouble();
var heat=frame.GetProperty("heatPercent").GetDouble();var speed=frame.GetProperty("speedMetresPerSecond").GetDouble();
if(!frame.GetProperty("inGame").GetBoolean()||frame.GetProperty("rawFsdState").GetInt32()!=2||frame.GetProperty("playerOverride").GetBoolean()
||heat>=85||radius<exclusion*1.25||speed>2000000||(V(frame,"position")-position).Length>20000000)throw new InvalidOperationException("Bounded SC response envelope reached.");
var seconds=watch.Elapsed.TotalSeconds;var throttle=seconds<3?0:.5;var yaw=seconds<1.5?.3:seconds<3?-.3:0;
var applied=await pipe.CallAsync(new{op="calibrate.axes",owner,sequence=frame.GetProperty("sequence").GetInt64(),ttlMilliseconds=180,
axes=new[]{0d,0,0,yaw,0,0},throttle});
rows.Add(new{seconds,heat,speed,radius,throttle,yaw,forward=V(frame,"forward"),angleFromStart=Vector3d.AngleDegrees(forward,V(frame,"forward")),applied});
await Task.Delay(40);
}
}
catch(Exception failure){error=failure.Message;}
finally{await pipe.CallAsync(new{op="release",owner});}
await File.WriteAllTextAsync(output,JsonSerializer.Serialize(new{pid,started,rows,error,released=true,screenUse=false,fsdInputsSent=0},new JsonSerializerOptions{WriteIndented=true}));
Console.WriteLine(JsonSerializer.Serialize(new{pid,samples=rows.Count,error,released=true}));
}
}