using System.Text.Json;
using System.Text.Json.Nodes;
using static XFEEDToolBox.Plugin.JsonWire;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// This is a copied input profile, not coverage and not the game's current
// radius reused at a different tool centre. All coordinates are ly.
sealed partial record GalaxyCatalogueProfile(double[] origin,double[]? queryCentre,float? surfaceDensity,
uint? qualityStarCount,float? runtimeRadiusMultiplier,float? sparseExpansion,
bool? smallMode,int? originalViewMode,bool? debugEnabled)
{internal float? colourBlend {get;init;}internal int? navRouteViewMode {get;init;}internal GalaxyPointOverrides? pointOverrides {get;init;}}
sealed class GalaxyCatalogueProfileWork
{
internal ulong generation,expires;
internal bool done,cancelled;
internal string error="";
internal double[] eye=[],focus=[];
internal GalaxyCatalogueProfile? profile;
}
static readonly object galaxyProfileMutex=new();
static GalaxyCatalogueProfileWork galaxyProfileWork=new();
static ulong galaxyProfileGeneration;
// Pipe threads only copy JSON. The owner-thread poll below reads memory.
static JsonObject GalaxyCatalogueProfileRequest(string op,JsonElement request)
{
lock(galaxyProfileMutex)
{
var work=galaxyProfileWork;
if(op=="galaxy.catalogue.profile")
{
static double[] Position(JsonElement request,string name)
{
var input=request.Required(name);
if(input.ValueKind!=JsonValueKind.Array||input.GetArrayLength()!=3)throw new InvalidDataException("Invalid catalogue profile position");
var p=input.EnumerateArray().Select(v=>v.GetDouble()).ToArray();
if(p.Any(v=>!double.IsFinite(v)||Math.Abs(v)>200000))throw new InvalidDataException("Invalid catalogue profile position");
return p;
}
var eye=Position(request,"eye");var focus=Position(request,"focus");
if(work.generation!=0&&!work.done)throw new InvalidOperationException("A catalogue profile query is already pending");
work=new(){generation=++galaxyProfileGeneration,expires=GetTickCount64()+5000,eye=eye,focus=focus};
galaxyProfileWork=work;
}
else
{
if(work.generation==0||request.U64("generation")!=work.generation)throw new InvalidOperationException("No matching catalogue profile query");
if(op=="galaxy.catalogue.profile.cancel"){work.cancelled=true;work.done=true;}
else if(op!="galaxy.catalogue.profile.result")throw new InvalidDataException("Unknown catalogue profile operation");
}
return GalaxyCatalogueProfileDescription(work,request);
}
}
static JsonObject GalaxyCatalogueProfileDescription(GalaxyCatalogueProfileWork work,JsonElement request=default)
{
var p=work.profile;
static JsonArray? Position(double[]? p)=>p is{Length:3}?Arr(p[0],p[1],p[2]):null;
return Obj(("generation",work.generation),("done",work.done),("cancelled",work.cancelled),("error",work.error),
("source","original-catalogue-inputs"),("profile",p is null?null:Obj(("origin",Position(p.origin)),
("queryCentre",Position(p.queryCentre)),("surfaceDensity",p.surfaceDensity),("qualityStarCount",p.qualityStarCount),
("runtimeRadiusMultiplier",p.runtimeRadiusMultiplier),("sparseExpansion",p.sparseExpansion),
("smallMode",p.smallMode),("originalViewMode",p.originalViewMode),("debugEnabled",p.debugEnabled),
("colourBlend",p.colourBlend),
("routeFilterChoices",GalaxyRouteChoicesDescription(p.routeFilterChoices)),
("dustInputs",GalaxyCatalogueDustDescription(p.dustInputs)),
("navRouteViewMode",p.navRouteViewMode),("navRouteMembershipMode",p.navRouteViewMode is{} routeView?routeView==1?1:0:(int?)null),
("pointOverrides",p.pointOverrides is{} points?GalaxyPointOverridesDescription(points,request):null),
("referenceKind","camera-eye"))));
}
static bool CopyGalaxyCatalogueRenderer(nuint component,nuint expectedImage,out int? mode,out float? expansion)
{
mode=null;expansion=null;
try
{
// Registered type5FA0A10 returns base60; constructor3A066A0
// and paired old/new tables are in catalogue-profile-bindings.json.
if(component<0x10060||Pointer(component)!=expectedImage+0x551F518||
Pointer(component-8)!=expectedImage+0x551F3D8||Pointer(component-0x60)!=expectedImage+0x551F2A8||
Pointer(component-0x58)!=expectedImage+0x551F2D8||
Pointer(expectedImage+0x551F3D8+0x80)!=expectedImage+0x3A66550)return false;
var view=Read<int>(component-8+0x98C);var sparse=Read<float>(component-0x60+0x9B4);
if(view>=0)mode=view;
if(float.IsFinite(sparse)&&sparse>=0)expansion=sparse;
if(Pointer(component)!=expectedImage+0x551F518||Pointer(component-8)!=expectedImage+0x551F3D8||
Read<int>(component-8+0x98C)!=view||!Read<float>(component-0x60+0x9B4).Equals(sparse))
{mode=null;expansion=null;return false;}
return true;
}
catch{mode=null;expansion=null;return false;}
}
static bool? CopyGalaxyCatalogueSmallMode(nuint component,nuint expectedImage)
{
if(component<0x10000)return null;
var table=Pointer(component);
if(table==expectedImage+0x552AE60&&Pointer(table+0x158)==expectedImage+0x201130)return true;
if(table!=expectedImage+0x552A9B8||Pointer(table+0x158)!=expectedImage+0x33D8220)return null;
var value=Read<byte>(component+0x624);
return value<=1&&Pointer(component)==table&&Read<byte>(component+0x624)==value?value!=0:null;
}
static float? CopyGalaxyCatalogueColourBlend(nuint component,nuint expectedImage)
{
// Same pure field as GalaxyAppearanceSettings/old3A55CF0. Read the
// current published value rather than its short-lived appearance cache.
if(!CopyGalaxyCatalogueRenderer(component,expectedImage,out _,out _))return null;
var value=Read<float>(component-8+0x99C);
return float.IsFinite(value)&&value is >=0 and <=1&&Read<float>(component-8+0x99C)==value&&
Pointer(component)==expectedImage+0x551F518&&Pointer(component-8)==expectedImage+0x551F3D8?value:null;
}
static void RefreshGalaxyCatalogueAppearanceCache(nuint registry,nuint expectedImage,float? currentBlend)
{
// A freshly observed option change must not reload new cell entries
// using the previous 100ms managed appearance-settings snapshot.
var cached=galaxyAppearanceSource;
if(cached.registry==registry&&cached.expectedImage==expectedImage&&cached.blend!=currentBlend)
galaxyAppearanceSource=default;
}
// 4CA6040, ushort RLE. Copy just the requested adjacent pixels; bound
// every run and iteration. The uint mass-density map at +38 is different.
static bool CopyGalaxyCatalogueSurfacePair(nuint rows,uint width,uint row,uint column,out uint a,out uint b)
{
a=b=0;var start=Pointer(rows+row*8);if(start<0x10000)return false;
var cursor=start;uint x=0;var foundA=false;var foundB=false;
for(uint runs=0;x<width&&runs<width;runs++)
{
var header=Read<ushort>(cursor);cursor+=2;var length=(uint)(header&0x7FFF);
if(length==0||length>width-x)return false;
if((header&0x8000)!=0)
{
var value=Read<ushort>(cursor);cursor+=2;
if(column>=x&&column<x+length){a=value;foundA=true;}
if(column+1>=x&&column+1<x+length){b=value;foundB=true;}
}
else
{
if(column>=x&&column<x+length){a=Read<ushort>(cursor+(column-x)*2);foundA=true;}
if(column+1>=x&&column+1<x+length){b=Read<ushort>(cursor+(column+1-x)*2);foundB=true;}
cursor+=length*2;
}
if(foundA&&foundB)return Pointer(rows+row*8)==start;
x+=length;
}
return false;
}
static bool CopyGalaxyCatalogueSurface(nuint data,ulong cell,out float density)
{
density=0;
try
{
if(data<0x10000||(cell&7)!=0)return false;
var map=Pointer(data+0x138);if(map<0x10000)return false;
var width=Read<uint>(map);var height=Read<uint>(map+4);var bytes=Read<uint>(map+8);var rows=Pointer(map+0x10);
var extentX=Read<uint>(data+0x2C);var extentZ=Read<uint>(data+0x34);
if(width is <2 or >8192||height is <2 or >8192||rows<0x10000||extentX is <1 or >131071||extentZ is <1 or >131071||
bytes<height*8||bytes>268435456)return false;
var sx=((ulong)width<<30)/((ulong)extentX<<15);var sz=((ulong)height<<30)/((ulong)extentZ<<15);
var x=(uint)(cell>>30)&0x3FFF;var z=(uint)(cell>>3)&0x3FFF;
var qx=(uint)(((ulong)(5+x*10)*512*sx)>>15);var qz=(uint)(((ulong)(5+z*10)*512*sz)>>15);
var px=qx>>9;var pz=qz>>9;uint value=0;
if(px+1<width&&pz+1<height)
{
if(!CopyGalaxyCatalogueSurfacePair(rows,width,pz,px,out var a,out var b)||
!CopyGalaxyCatalogueSurfacePair(rows,width,pz+1,px,out var c,out var d))return false;
var wx=(qx&511)*64;var wz=(qz&511)*64;
var top=((ulong)a*(32768-wx)>>15)+((ulong)b*wx>>15);
var bottom=((ulong)c*(32768-wx)>>15)+((ulong)d*wx>>15);
value=(uint)((top*(32768-wz)>>15)+(bottom*wz>>15));
}
if(Pointer(data+0x138)!=map||Read<uint>(map)!=width||Read<uint>(map+4)!=height||Read<uint>(map+8)!=bytes||
Pointer(map+0x10)!=rows||Read<uint>(data+0x2C)!=extentX||Read<uint>(data+0x34)!=extentZ)return false;
density=value*(1f/32768);return true;
}
catch{return false;}
}
static bool GalaxyCatalogueProfileCentre(double[] relative,double[] origin,float[] cellLimits,out double[] clamped,out ulong cell)
{
clamped=[];cell=0;
if(relative.Length!=3||origin.Length!=3||cellLimits.Length!=3||cellLimits.Any(v=>!float.IsFinite(v)||v<=100||v>200000))return false;
clamped=new double[3];Span<uint> coordinates=stackalloc uint[3];
for(var i=0;i<3;i++)
{
var input=(float)(relative[i]+origin[i]);if(!float.IsFinite(input))return false;
var world=Math.Clamp(input,100f,i==1?81818f:163738f);
clamped[i]=world-origin[i];
coordinates[i]=(uint)(int)(Math.Clamp(world,0,cellLimits[i]-.1f)*.1f);
}
if(coordinates[0]>=16384||coordinates[1]>=8192||coordinates[2]>=16384)return false;
cell=((((ulong)coordinates[0]<<13)|coordinates[1])<<14|coordinates[2])<<3;return true;
}
static GalaxyCatalogueProfile CopyGalaxyCatalogueProfile(GalaxyServices services,double[] eye,double[] focus)
{
var origin=services.origin.Select(v=>v/32d).ToArray();int? mode=null;float? expansion=null;bool? small=null;bool? debug=null;
float? multiplier=null;float? density=null;uint? quality=null;double[]? centre=null;
var renderer=RegisteredComponent(services.registry,Read<uint>(image+0x5FA0A10));
CopyGalaxyCatalogueRenderer(renderer,image,out mode,out expansion);
var blend=CopyGalaxyCatalogueColourBlend(renderer,image);
RefreshGalaxyCatalogueAppearanceCache(services.registry,image,blend);
var navigation=RegisteredComponent(services.registry,Read<uint>(image+0x5F05DD4));
small=CopyGalaxyCatalogueSmallMode(navigation,image);var navRouteViewMode=CopyGalaxyNavRouteViewMode(navigation,image);
var range=Read<float>(image+0x5E9C928);if(float.IsFinite(range)&&range>=0)multiplier=range;
var flag=Read<byte>(image+0x5E9D38D);if(flag<=1)debug=flag!=0;
if(TryReadGalaxyCatalogueQuality(services.registry,image,out var n))quality=n;
var data=Pointer(services.galaxy+0x100);
var limits=Enumerable.Range(0,3).Select(i=>Read<float>(image+0x5FDF210+(nuint)i*4)).ToArray();
if(mode.HasValue&&GalaxyCatalogueProfileCentre(mode==0?eye:focus,origin,limits,out var p,out var cell))
{centre=p;if(CopyGalaxyCatalogueSurface(data,cell,out var d))density=d;}
// All reads occur on the owning thread. Recheck published component
// identities and source scalars before claiming a usable copied input.
if(RegisteredComponent(services.registry,Read<uint>(image+0x5FA0A10))!=renderer||Pointer(services.galaxy+0x100)!=data||
Read<float>(image+0x5E9C928)!=range||Read<byte>(image+0x5E9D38D)!=flag)
return new(origin,null,null,null,null,null,null,null,null);
return new(origin,centre,density,quality,multiplier,expansion,small,mode,debug){colourBlend=blend,navRouteViewMode=navRouteViewMode};
}
static void PollGalaxyCatalogueProfile(Snapshot frame)
{
if(owningThread==0||GetCurrentThreadId()!=owningThread||!Enabled()||!inGame)return;
GalaxyCatalogueProfileWork work;
lock(galaxyProfileMutex)
{
work=galaxyProfileWork;if(work.generation==0||work.done)return;
if(GetTickCount64()>=work.expires){work.error="Catalogue profile deadline exceeded";work.done=true;return;}
}
if(!frame.valid||frame.entity==0||!FindGalaxyServices(frame,out var services))return;
GalaxyCatalogueProfile? profile=null;var error="";
try
{
profile=CopyGalaxyCatalogueProfile(services,work.eye,work.focus);
// Optional constant-size copied inputs never delay LOD for a Dust
// worker publication, and never trigger a load or a renderer job.
profile=profile with{dustInputs=CopyGalaxyCatalogueDust(frame,services)};
profile=profile with{routeFilterChoices=CopyGalaxyCatalogueRouteChoices(frame,services)};
var points=CopyGalaxyPointOverrides(services,work.generation);
// A route mode changed during the independent copies cannot bind
// either result to this view. Unknown remains explicit.
if(points.routeViewMode!=profile.navRouteViewMode)
points=new(work.generation,points.tick,null,false,false,[],[]);
profile=profile with{pointOverrides=points};
var current=Current();var origin=Pointer(services.galaxy+0x188);
if(!current.valid||current.entity!=frame.entity||origin<0x10000||
Enumerable.Range(0,3).Any(i=>Read<int>(origin+(nuint)i*4)!=services.origin[i]))
{profile=null;error="Catalogue profile player context changed";}
}
catch{error="Catalogue profile unavailable";}
lock(galaxyProfileMutex)
{
if(!ReferenceEquals(galaxyProfileWork,work)||work.done)return;
work.profile=profile;work.error=error;work.done=true;
}
}
}
using System.Text.Json;
using System.Text.Json.Nodes;
using static XFEEDToolBox.Plugin.JsonWire;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// This is a copied input profile, not coverage and not the game's current
// radius reused at a different tool centre. All coordinates are ly.
sealed partial record GalaxyCatalogueProfile(double[] origin,double[]? queryCentre,float? surfaceDensity,
uint? qualityStarCount,float? runtimeRadiusMultiplier,float? sparseExpansion,
bool? smallMode,int? originalViewMode,bool? debugEnabled)
{internal float? colourBlend {get;init;}internal int? navRouteViewMode {get;init;}internal GalaxyPointOverrides? pointOverrides {get;init;}}
sealed class GalaxyCatalogueProfileWork
{
internal ulong generation,expires;
internal bool done,cancelled;
internal string error="";
internal double[] eye=[],focus=[];
internal GalaxyCatalogueProfile? profile;
}
static readonly object galaxyProfileMutex=new();
static GalaxyCatalogueProfileWork galaxyProfileWork=new();
static ulong galaxyProfileGeneration;
// Pipe threads only copy JSON. The owner-thread poll below reads memory.
static JsonObject GalaxyCatalogueProfileRequest(string op,JsonElement request)
{
lock(galaxyProfileMutex)
{
var work=galaxyProfileWork;
if(op=="galaxy.catalogue.profile")
{
static double[] Position(JsonElement request,string name)
{
var input=request.Required(name);
if(input.ValueKind!=JsonValueKind.Array||input.GetArrayLength()!=3)throw new InvalidDataException("Invalid catalogue profile position");
var p=input.EnumerateArray().Select(v=>v.GetDouble()).ToArray();
if(p.Any(v=>!double.IsFinite(v)||Math.Abs(v)>200000))throw new InvalidDataException("Invalid catalogue profile position");
return p;
}
var eye=Position(request,"eye");var focus=Position(request,"focus");
if(work.generation!=0&&!work.done)throw new InvalidOperationException("A catalogue profile query is already pending");
work=new(){generation=++galaxyProfileGeneration,expires=GetTickCount64()+5000,eye=eye,focus=focus};
galaxyProfileWork=work;
}
else
{
if(work.generation==0||request.U64("generation")!=work.generation)throw new InvalidOperationException("No matching catalogue profile query");
if(op=="galaxy.catalogue.profile.cancel"){work.cancelled=true;work.done=true;}
else if(op!="galaxy.catalogue.profile.result")throw new InvalidDataException("Unknown catalogue profile operation");
}
return GalaxyCatalogueProfileDescription(work,request);
}
}
static JsonObject GalaxyCatalogueProfileDescription(GalaxyCatalogueProfileWork work,JsonElement request=default)
{
var p=work.profile;
static JsonArray? Position(double[]? p)=>p is{Length:3}?Arr(p[0],p[1],p[2]):null;
return Obj(("generation",work.generation),("done",work.done),("cancelled",work.cancelled),("error",work.error),
("source","original-catalogue-inputs"),("profile",p is null?null:Obj(("origin",Position(p.origin)),
("queryCentre",Position(p.queryCentre)),("surfaceDensity",p.surfaceDensity),("qualityStarCount",p.qualityStarCount),
("runtimeRadiusMultiplier",p.runtimeRadiusMultiplier),("sparseExpansion",p.sparseExpansion),
("smallMode",p.smallMode),("originalViewMode",p.originalViewMode),("debugEnabled",p.debugEnabled),
("colourBlend",p.colourBlend),
("routeFilterChoices",GalaxyRouteChoicesDescription(p.routeFilterChoices)),
("dustInputs",GalaxyCatalogueDustDescription(p.dustInputs)),
("navRouteViewMode",p.navRouteViewMode),("navRouteMembershipMode",p.navRouteViewMode is{} routeView?routeView==1?1:0:(int?)null),
("pointOverrides",p.pointOverrides is{} points?GalaxyPointOverridesDescription(points,request):null),
("referenceKind","camera-eye"))));
}
static bool CopyGalaxyCatalogueRenderer(nuint component,nuint expectedImage,out int? mode,out float? expansion)
{
mode=null;expansion=null;
try
{
// Registered type5FA0A10 returns base60; constructor3A066A0
// and paired old/new tables are in catalogue-profile-bindings.json.
if(component<0x10060||Pointer(component)!=expectedImage+0x551F518||
Pointer(component-8)!=expectedImage+0x551F3D8||Pointer(component-0x60)!=expectedImage+0x551F2A8||
Pointer(component-0x58)!=expectedImage+0x551F2D8||
Pointer(expectedImage+0x551F3D8+0x80)!=expectedImage+0x3A66550)return false;
var view=Read<int>(component-8+0x98C);var sparse=Read<float>(component-0x60+0x9B4);
if(view>=0)mode=view;
if(float.IsFinite(sparse)&&sparse>=0)expansion=sparse;
if(Pointer(component)!=expectedImage+0x551F518||Pointer(component-8)!=expectedImage+0x551F3D8||
Read<int>(component-8+0x98C)!=view||!Read<float>(component-0x60+0x9B4).Equals(sparse))
{mode=null;expansion=null;return false;}
return true;
}
catch{mode=null;expansion=null;return false;}
}
static bool? CopyGalaxyCatalogueSmallMode(nuint component,nuint expectedImage)
{
if(component<0x10000)return null;
var table=Pointer(component);
if(table==expectedImage+0x552AE60&&Pointer(table+0x158)==expectedImage+0x201130)return true;
if(table!=expectedImage+0x552A9B8||Pointer(table+0x158)!=expectedImage+0x33D8220)return null;
var value=Read<byte>(component+0x624);
return value<=1&&Pointer(component)==table&&Read<byte>(component+0x624)==value?value!=0:null;
}
static float? CopyGalaxyCatalogueColourBlend(nuint component,nuint expectedImage)
{
// Same pure field as GalaxyAppearanceSettings/old3A55CF0. Read the
// current published value rather than its short-lived appearance cache.
if(!CopyGalaxyCatalogueRenderer(component,expectedImage,out _,out _))return null;
var value=Read<float>(component-8+0x99C);
return float.IsFinite(value)&&value is >=0 and <=1&&Read<float>(component-8+0x99C)==value&&
Pointer(component)==expectedImage+0x551F518&&Pointer(component-8)==expectedImage+0x551F3D8?value:null;
}
static void RefreshGalaxyCatalogueAppearanceCache(nuint registry,nuint expectedImage,float? currentBlend)
{
// A freshly observed option change must not reload new cell entries
// using the previous 100ms managed appearance-settings snapshot.
var cached=galaxyAppearanceSource;
if(cached.registry==registry&&cached.expectedImage==expectedImage&&cached.blend!=currentBlend)
galaxyAppearanceSource=default;
}
// 4CA6040, ushort RLE. Copy just the requested adjacent pixels; bound
// every run and iteration. The uint mass-density map at +38 is different.
static bool CopyGalaxyCatalogueSurfacePair(nuint rows,uint width,uint row,uint column,out uint a,out uint b)
{
a=b=0;var start=Pointer(rows+row*8);if(start<0x10000)return false;
var cursor=start;uint x=0;var foundA=false;var foundB=false;
for(uint runs=0;x<width&&runs<width;runs++)
{
var header=Read<ushort>(cursor);cursor+=2;var length=(uint)(header&0x7FFF);
if(length==0||length>width-x)return false;
if((header&0x8000)!=0)
{
var value=Read<ushort>(cursor);cursor+=2;
if(column>=x&&column<x+length){a=value;foundA=true;}
if(column+1>=x&&column+1<x+length){b=value;foundB=true;}
}
else
{
if(column>=x&&column<x+length){a=Read<ushort>(cursor+(column-x)*2);foundA=true;}
if(column+1>=x&&column+1<x+length){b=Read<ushort>(cursor+(column+1-x)*2);foundB=true;}
cursor+=length*2;
}
if(foundA&&foundB)return Pointer(rows+row*8)==start;
x+=length;
}
return false;
}
static bool CopyGalaxyCatalogueSurface(nuint data,ulong cell,out float density)
{
density=0;
try
{
if(data<0x10000||(cell&7)!=0)return false;
var map=Pointer(data+0x138);if(map<0x10000)return false;
var width=Read<uint>(map);var height=Read<uint>(map+4);var bytes=Read<uint>(map+8);var rows=Pointer(map+0x10);
var extentX=Read<uint>(data+0x2C);var extentZ=Read<uint>(data+0x34);
if(width is <2 or >8192||height is <2 or >8192||rows<0x10000||extentX is <1 or >131071||extentZ is <1 or >131071||
bytes<height*8||bytes>268435456)return false;
var sx=((ulong)width<<30)/((ulong)extentX<<15);var sz=((ulong)height<<30)/((ulong)extentZ<<15);
var x=(uint)(cell>>30)&0x3FFF;var z=(uint)(cell>>3)&0x3FFF;
var qx=(uint)(((ulong)(5+x*10)*512*sx)>>15);var qz=(uint)(((ulong)(5+z*10)*512*sz)>>15);
var px=qx>>9;var pz=qz>>9;uint value=0;
if(px+1<width&&pz+1<height)
{
if(!CopyGalaxyCatalogueSurfacePair(rows,width,pz,px,out var a,out var b)||
!CopyGalaxyCatalogueSurfacePair(rows,width,pz+1,px,out var c,out var d))return false;
var wx=(qx&511)*64;var wz=(qz&511)*64;
var top=((ulong)a*(32768-wx)>>15)+((ulong)b*wx>>15);
var bottom=((ulong)c*(32768-wx)>>15)+((ulong)d*wx>>15);
value=(uint)((top*(32768-wz)>>15)+(bottom*wz>>15));
}
if(Pointer(data+0x138)!=map||Read<uint>(map)!=width||Read<uint>(map+4)!=height||Read<uint>(map+8)!=bytes||
Pointer(map+0x10)!=rows||Read<uint>(data+0x2C)!=extentX||Read<uint>(data+0x34)!=extentZ)return false;
density=value*(1f/32768);return true;
}
catch{return false;}
}
static bool GalaxyCatalogueProfileCentre(double[] relative,double[] origin,float[] cellLimits,out double[] clamped,out ulong cell)
{
clamped=[];cell=0;
if(relative.Length!=3||origin.Length!=3||cellLimits.Length!=3||cellLimits.Any(v=>!float.IsFinite(v)||v<=100||v>200000))return false;
clamped=new double[3];Span<uint> coordinates=stackalloc uint[3];
for(var i=0;i<3;i++)
{
var input=(float)(relative[i]+origin[i]);if(!float.IsFinite(input))return false;
var world=Math.Clamp(input,100f,i==1?81818f:163738f);
clamped[i]=world-origin[i];
coordinates[i]=(uint)(int)(Math.Clamp(world,0,cellLimits[i]-.1f)*.1f);
}
if(coordinates[0]>=16384||coordinates[1]>=8192||coordinates[2]>=16384)return false;
cell=((((ulong)coordinates[0]<<13)|coordinates[1])<<14|coordinates[2])<<3;return true;
}
static GalaxyCatalogueProfile CopyGalaxyCatalogueProfile(GalaxyServices services,double[] eye,double[] focus)
{
var origin=services.origin.Select(v=>v/32d).ToArray();int? mode=null;float? expansion=null;bool? small=null;bool? debug=null;
float? multiplier=null;float? density=null;uint? quality=null;double[]? centre=null;
var renderer=RegisteredComponent(services.registry,Read<uint>(image+0x5FA0A10));
CopyGalaxyCatalogueRenderer(renderer,image,out mode,out expansion);
var blend=CopyGalaxyCatalogueColourBlend(renderer,image);
RefreshGalaxyCatalogueAppearanceCache(services.registry,image,blend);
var navigation=RegisteredComponent(services.registry,Read<uint>(image+0x5F05DD4));
small=CopyGalaxyCatalogueSmallMode(navigation,image);var navRouteViewMode=CopyGalaxyNavRouteViewMode(navigation,image);
var range=Read<float>(image+0x5E9C928);if(float.IsFinite(range)&&range>=0)multiplier=range;
var flag=Read<byte>(image+0x5E9D38D);if(flag<=1)debug=flag!=0;
if(TryReadGalaxyCatalogueQuality(services.registry,image,out var n))quality=n;
var data=Pointer(services.galaxy+0x100);
var limits=Enumerable.Range(0,3).Select(i=>Read<float>(image+0x5FDF210+(nuint)i*4)).ToArray();
if(mode.HasValue&&GalaxyCatalogueProfileCentre(mode==0?eye:focus,origin,limits,out var p,out var cell))
{centre=p;if(CopyGalaxyCatalogueSurface(data,cell,out var d))density=d;}
// All reads occur on the owning thread. Recheck published component
// identities and source scalars before claiming a usable copied input.
if(RegisteredComponent(services.registry,Read<uint>(image+0x5FA0A10))!=renderer||Pointer(services.galaxy+0x100)!=data||
Read<float>(image+0x5E9C928)!=range||Read<byte>(image+0x5E9D38D)!=flag)
return new(origin,null,null,null,null,null,null,null,null);
return new(origin,centre,density,quality,multiplier,expansion,small,mode,debug){colourBlend=blend,navRouteViewMode=navRouteViewMode};
}
static void PollGalaxyCatalogueProfile(Snapshot frame)
{
if(owningThread==0||GetCurrentThreadId()!=owningThread||!Enabled()||!inGame)return;
GalaxyCatalogueProfileWork work;
lock(galaxyProfileMutex)
{
work=galaxyProfileWork;if(work.generation==0||work.done)return;
if(GetTickCount64()>=work.expires){work.error="Catalogue profile deadline exceeded";work.done=true;return;}
}
if(!frame.valid||frame.entity==0||!FindGalaxyServices(frame,out var services))return;
GalaxyCatalogueProfile? profile=null;var error="";
try
{
profile=CopyGalaxyCatalogueProfile(services,work.eye,work.focus);
// Optional constant-size copied inputs never delay LOD for a Dust
// worker publication, and never trigger a load or a renderer job.
profile=profile with{dustInputs=CopyGalaxyCatalogueDust(frame,services)};
profile=profile with{routeFilterChoices=CopyGalaxyCatalogueRouteChoices(frame,services)};
var points=CopyGalaxyPointOverrides(services,work.generation);
// A route mode changed during the independent copies cannot bind
// either result to this view. Unknown remains explicit.
if(points.routeViewMode!=profile.navRouteViewMode)
points=new(work.generation,points.tick,null,false,false,[],[]);
profile=profile with{pointOverrides=points};
var current=Current();var origin=Pointer(services.galaxy+0x188);
if(!current.valid||current.entity!=frame.entity||origin<0x10000||
Enumerable.Range(0,3).Any(i=>Read<int>(origin+(nuint)i*4)!=services.origin[i]))
{profile=null;error="Catalogue profile player context changed";}
}
catch{error="Catalogue profile unavailable";}
lock(galaxyProfileMutex)
{
if(!ReferenceEquals(galaxyProfileWork,work)||work.done)return;
work.profile=profile;work.error=error;work.done=true;
}
}
}