namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
internal readonly record struct GalaxyFilterFacts(bool? visited,string? exploration,string? tradeData,bool? withinDataRange,int? metadataStatus,int? colonisationStatus=null,int? colonisationNormalStatus=null);
internal sealed class GalaxyFilterContext
{
internal nuint expectedImage,metadata,trade,explorationChild,visited,colonisation;
internal ulong entity,currentSystem,tick;
internal uint thread;
internal Vector currentPosition;
internal bool hasCurrentPosition;
}
static ulong GalaxyFactHash(ulong address)
{
unchecked
{
var hash=address*0x40000+~address;
hash=((hash>>31)^hash)*0x15;hash=((hash>>11)^hash)*0x41;
return (uint)(hash>>22)^(uint)hash;
}
}
// Same bucket/key traversal as 39BED80 (metadata) and 3B36220 (trade).
// A failed read, malformed chain or changed table is unknown, never absent.
static bool GalaxyFactFindHash(nuint map,ulong address,out nuint found)
{
found=0;
try
{
var buckets=Pointer(map);var capacity=Read<ulong>(map+8);var count=Read<ulong>(map+0x18);
if(buckets<0x10000||capacity==0||capacity>1_048_576||count>4_000_000)return false;
if(count!=0)
{
var sentinel=buckets+(nuint)(GalaxyFactHash(address)%capacity)*8;var node=Pointer(sentinel);
var maximum=(int)Math.Min(count,256);var ended=false;
for(var step=0;step<=maximum;step++)
{
if(node==sentinel){ended=true;break;}
if(step==maximum||node<0x10000)return false;
if(Read<ulong>(node+8)==address){found=node;ended=true;break;}
var next=Pointer(node);if(next==node)return false;node=next;
}
if(!ended)return false;
}
if(Pointer(map)!=buckets||Read<ulong>(map+8)!=capacity||Read<ulong>(map+0x18)!=count){found=0;return false;}
return true;
}
catch{found=0;return false;}
}
static bool GalaxyFactFindTree(nuint sentinel,ulong address,out nuint found)
{
found=0;
try
{
var root=Pointer(sentinel);var node=root;var ended=false;
for(var step=0;step<256;step++)
{
if(node==0||node==sentinel){ended=true;break;}
if(node<0x10000)return false;
var key=Read<ulong>(node+0x20);
if(key==address){found=node;ended=true;break;}
var next=Pointer(node+(key<address?8u:0u));if(next==node)return false;node=next;
}
if(!ended||Pointer(sentinel)!=root){found=0;return false;}
return true;
}
catch{found=0;return false;}
}
static bool GalaxyFactCurrentPosition(GalaxyServices services,ulong address,out Vector position)
{
position=default;
try
{
// 3C331E0 reads this existing object tree before allocating. We only
// traverse it; this path never creates or loads a galaxy object.
if(!GalaxyFactFindTree(services.galaxy+0x370,address,out var node)||node==0)return false;
var obj=Pointer(node+0x28);
if(obj<0x10000||Read<ulong>(obj+0x28)!=address||Read<uint>(obj+0x20)!=2)return false;
var compact=Pointer(obj+0x38);if(compact<0x10000||Read<ulong>(compact)!=address)return false;
var m=(int)(address&7);
var x=(((address>>(30-2*m))<<m)&0x3FFF)*320+Read<ushort>(compact+0x42);
var y=(((address>>(17-m))<<m)&0x1FFF)*320+Read<ushort>(compact+0x44);
var z=(((address>>3)<<m)&0x3FFF)*320+Read<ushort>(compact+0x46);
if(services.origin is not{Length:3})return false;
position=new(((double)x-services.origin[0])/32,((double)y-services.origin[1])/32,((double)z-services.origin[2])/32);
return Read<uint>(obj+0x20)==2&&Pointer(obj+0x38)==compact&&Read<ulong>(compact)==address;
}
catch{return false;}
}
// Build once per existing owning-thread region batch, then reuse for its
// real compact-star positions. Every operation below is a guarded read.
static bool TryReadGalaxyFilterContext(Snapshot frame,GalaxyServices services,out GalaxyFilterContext context)
{
context=new();
if(!Enabled()||!inGame||!frame.valid||owningThread==0||GetCurrentThreadId()!=owningThread||services.registry==0||services.galaxy==0)return false;
try
{
if(Pointer(services.galaxy)!=image+0x5539C28)return false;
context.expectedImage=image;context.entity=frame.entity;context.tick=frame.tick;context.thread=owningThread;
context.currentSystem=frame.referenceWorld&0x007FFFFFFFFFFFFFUL;
if(context.currentSystem==0)return false;
context.hasCurrentPosition=GalaxyFactCurrentPosition(services,context.currentSystem,out context.currentPosition);
var metadata=RegisteredComponent(services.registry,Read<uint>(image+0x5F05C24));
if(metadata>=0x10000&&Pointer(metadata)==image+0x551A1B0)context.metadata=metadata;
var trade=RegisteredComponent(services.registry,Read<uint>(image+0x5F856E0));
if(trade>=0x10058&&Pointer(trade)==image+0x552EBA0&&Pointer(trade-0x58)==image+0x552EA70&&Pointer(trade-0x50)==image+0x552EAA0)context.trade=trade;
var visited=RegisteredComponent(services.registry,Read<uint>(image+0x5F856E4));
if(visited>=0x10058&&Pointer(visited)==image+0x52A5258&&Pointer(visited-0x58)==image+0x52A5128&&Pointer(visited-0x50)==image+0x52A5158)context.visited=visited;
var colony=RegisteredComponent(services.registry,Read<uint>(image+0x5F5A814));
if(colony>=0x10058&&Pointer(colony)==image+0x519D720&&Pointer(colony-0x58)==image+0x519D5F0&&Pointer(colony-0x50)==image+0x519D620)context.colonisation=colony;
var exploration=RegisteredComponent(services.registry,Read<uint>(image+0x5F60E14));
if(exploration>=0x10058&&Pointer(exploration)==image+0x51B3A10&&Pointer(exploration-0x58)==image+0x51B38E0&&Pointer(exploration-0x50)==image+0x51B3910)
{
// 1357780 owns this child at base+158; 138C9A0 returns child+110
// exactly when child+100 is ready (3). 1397F40 binds +108 to
// the original current system. Do not call its request path.
var child=Pointer(exploration+0x100);
if(child>=0x10000&&Read<ulong>(child+0x108)==context.currentSystem)context.explorationChild=child;
}
return true;
}
catch{context=new();return false;}
}
static bool? GalaxyFactExploration(GalaxyFilterContext context,ulong address)
{
try
{
var child=context.explorationChild;if(child==0||Read<int>(child+0x100)!=3||Read<ulong>(child+0x108)!=context.currentSystem)return null;
if(!GalaxyFactFindTree(child+0x120,address,out var node))return null;
if(Read<int>(child+0x100)!=3||Read<ulong>(child+0x108)!=context.currentSystem)return null;
return node!=0;
}
catch{return null;}
}
static int? GalaxyFactMetadataStatus(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.metadata;if(source==0||Pointer(source)!=context.expectedImage+0x551A1B0)return null;
if(!GalaxyFactFindHash(source+0xA8,address,out var node))return null;
if(node==0&&!GalaxyFactFindHash(source+0xC8,address,out node))return null;
if(node==0)return null; // Original cache missing is not a known no-data descriptor.
// Original3B26E30 tests descriptor+20 !=1. Economy getter
// 3B25D40 proves that this is the primary economy enum, not a
// four-state ready flag: Industrial4 through Carrier28 are valid.
var state=Read<int>(node+0x30);if(state is <0 or >28||Read<ulong>(node+8)!=address)return null;
return state;
}
catch{return null;}
}
static bool? GalaxyFactTradeOwnedOrPending(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.trade;if(source==0||Pointer(source)!=context.expectedImage+0x552EBA0)return null;
if(!GalaxyFactFindHash(source+0xB8,address,out var owned))return null;
if(owned!=0)return true;
if(!GalaxyFactFindHash(source+0xD8,address,out var pending))return null;
return pending!=0;
}
catch{return null;}
}
static bool? GalaxyFactVisited(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.visited;
if(source==0||Pointer(source)!=context.expectedImage+0x52A5258||Read<byte>(source+0xB0)!=1)return null;
// 3B27080 -> interface+20 (239BCF0) tests this exact runtime map.
// 25DAEA0 marks it ready only after 2634B80 loads the original
// cache. A positive is valid even when that cache is partial.
if(!GalaxyFactFindHash(source+0x90,address,out var node)||Read<byte>(source+0xB0)!=1)return null;
if(node!=0)return true;
// The original file can be limited. Require its live manager's
// $stat_Systems_Visited count before accepting a negative. Missing
// statistics use limit FFFFFFFF and cannot establish completeness.
var expected=Read<uint>(source+0xB4);var limit=Read<uint>(source+0xB8);var count=Read<ulong>(source+0xA8);
if(limit==uint.MaxValue||expected>4_000_000||limit!=expected+(expected>>3)||count<expected)return null;
return false;
}
catch{return null;}
}
static bool CopyGalaxyFilterFacts(GalaxyFilterContext context,ulong address,double[] position,out GalaxyFilterFacts facts)
{
facts=default;
if(address==0||address>=0x80000000000000UL||position is not{Length:3}||!double.IsFinite(position[0])||!double.IsFinite(position[1])||!double.IsFinite(position[2]))return false;
var visited=GalaxyFactVisited(context,address);facts=new(visited,null,null,null,null);
if(!context.hasCurrentPosition)return true;
// Match 3B25E20/3B26E30's float-distance test and strict 20ly boundary.
var dx=(float)(position[0]-context.currentPosition.x);var dy=(float)(position[1]-context.currentPosition.y);var dz=(float)(position[2]-context.currentPosition.z);
var within=MathF.Sqrt(dx*dx+dy*dy+dz*dz)<20;
if(!within){facts=new(visited,null,null,false,null);return true;}
var available=GalaxyFactExploration(context,address);var status=GalaxyFactMetadataStatus(context,address);
string? trade=null;
if(status==1&&context.trade!=0)trade="nodataavailable";
else if(status is not null&&GalaxyFactTradeOwnedOrPending(context,address) is {} owned)trade=owned?"havedata":"dataavailable";
facts=new(visited,available is {} known?known?"dataavailable":"nodataavailable":null,trade,true,status);
return true;
}
static bool TryReadGalaxyFilterFacts(GalaxyFilterContext context,ulong systemAddress,double[] position,out GalaxyFilterFacts facts)
{
facts=default;
if(!Enabled()||!inGame||context.thread==0||context.thread!=owningThread||GetCurrentThreadId()!=context.thread||
GetTickCount64()-context.tick>500)return false;
var frame=Current();
if(!frame.valid||frame.entity!=context.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=context.currentSystem)return false;
return CopyGalaxyFilterFacts(context,systemAddress,position,out facts);
}
}
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
internal readonly record struct GalaxyFilterFacts(bool? visited,string? exploration,string? tradeData,bool? withinDataRange,int? metadataStatus,int? colonisationStatus=null,int? colonisationNormalStatus=null);
internal sealed class GalaxyFilterContext
{
internal nuint expectedImage,metadata,trade,explorationChild,visited,colonisation;
internal ulong entity,currentSystem,tick;
internal uint thread;
internal Vector currentPosition;
internal bool hasCurrentPosition;
}
static ulong GalaxyFactHash(ulong address)
{
unchecked
{
var hash=address*0x40000+~address;
hash=((hash>>31)^hash)*0x15;hash=((hash>>11)^hash)*0x41;
return (uint)(hash>>22)^(uint)hash;
}
}
// Same bucket/key traversal as 39BED80 (metadata) and 3B36220 (trade).
// A failed read, malformed chain or changed table is unknown, never absent.
static bool GalaxyFactFindHash(nuint map,ulong address,out nuint found)
{
found=0;
try
{
var buckets=Pointer(map);var capacity=Read<ulong>(map+8);var count=Read<ulong>(map+0x18);
if(buckets<0x10000||capacity==0||capacity>1_048_576||count>4_000_000)return false;
if(count!=0)
{
var sentinel=buckets+(nuint)(GalaxyFactHash(address)%capacity)*8;var node=Pointer(sentinel);
var maximum=(int)Math.Min(count,256);var ended=false;
for(var step=0;step<=maximum;step++)
{
if(node==sentinel){ended=true;break;}
if(step==maximum||node<0x10000)return false;
if(Read<ulong>(node+8)==address){found=node;ended=true;break;}
var next=Pointer(node);if(next==node)return false;node=next;
}
if(!ended)return false;
}
if(Pointer(map)!=buckets||Read<ulong>(map+8)!=capacity||Read<ulong>(map+0x18)!=count){found=0;return false;}
return true;
}
catch{found=0;return false;}
}
static bool GalaxyFactFindTree(nuint sentinel,ulong address,out nuint found)
{
found=0;
try
{
var root=Pointer(sentinel);var node=root;var ended=false;
for(var step=0;step<256;step++)
{
if(node==0||node==sentinel){ended=true;break;}
if(node<0x10000)return false;
var key=Read<ulong>(node+0x20);
if(key==address){found=node;ended=true;break;}
var next=Pointer(node+(key<address?8u:0u));if(next==node)return false;node=next;
}
if(!ended||Pointer(sentinel)!=root){found=0;return false;}
return true;
}
catch{found=0;return false;}
}
static bool GalaxyFactCurrentPosition(GalaxyServices services,ulong address,out Vector position)
{
position=default;
try
{
// 3C331E0 reads this existing object tree before allocating. We only
// traverse it; this path never creates or loads a galaxy object.
if(!GalaxyFactFindTree(services.galaxy+0x370,address,out var node)||node==0)return false;
var obj=Pointer(node+0x28);
if(obj<0x10000||Read<ulong>(obj+0x28)!=address||Read<uint>(obj+0x20)!=2)return false;
var compact=Pointer(obj+0x38);if(compact<0x10000||Read<ulong>(compact)!=address)return false;
var m=(int)(address&7);
var x=(((address>>(30-2*m))<<m)&0x3FFF)*320+Read<ushort>(compact+0x42);
var y=(((address>>(17-m))<<m)&0x1FFF)*320+Read<ushort>(compact+0x44);
var z=(((address>>3)<<m)&0x3FFF)*320+Read<ushort>(compact+0x46);
if(services.origin is not{Length:3})return false;
position=new(((double)x-services.origin[0])/32,((double)y-services.origin[1])/32,((double)z-services.origin[2])/32);
return Read<uint>(obj+0x20)==2&&Pointer(obj+0x38)==compact&&Read<ulong>(compact)==address;
}
catch{return false;}
}
// Build once per existing owning-thread region batch, then reuse for its
// real compact-star positions. Every operation below is a guarded read.
static bool TryReadGalaxyFilterContext(Snapshot frame,GalaxyServices services,out GalaxyFilterContext context)
{
context=new();
if(!Enabled()||!inGame||!frame.valid||owningThread==0||GetCurrentThreadId()!=owningThread||services.registry==0||services.galaxy==0)return false;
try
{
if(Pointer(services.galaxy)!=image+0x5539C28)return false;
context.expectedImage=image;context.entity=frame.entity;context.tick=frame.tick;context.thread=owningThread;
context.currentSystem=frame.referenceWorld&0x007FFFFFFFFFFFFFUL;
if(context.currentSystem==0)return false;
context.hasCurrentPosition=GalaxyFactCurrentPosition(services,context.currentSystem,out context.currentPosition);
var metadata=RegisteredComponent(services.registry,Read<uint>(image+0x5F05C24));
if(metadata>=0x10000&&Pointer(metadata)==image+0x551A1B0)context.metadata=metadata;
var trade=RegisteredComponent(services.registry,Read<uint>(image+0x5F856E0));
if(trade>=0x10058&&Pointer(trade)==image+0x552EBA0&&Pointer(trade-0x58)==image+0x552EA70&&Pointer(trade-0x50)==image+0x552EAA0)context.trade=trade;
var visited=RegisteredComponent(services.registry,Read<uint>(image+0x5F856E4));
if(visited>=0x10058&&Pointer(visited)==image+0x52A5258&&Pointer(visited-0x58)==image+0x52A5128&&Pointer(visited-0x50)==image+0x52A5158)context.visited=visited;
var colony=RegisteredComponent(services.registry,Read<uint>(image+0x5F5A814));
if(colony>=0x10058&&Pointer(colony)==image+0x519D720&&Pointer(colony-0x58)==image+0x519D5F0&&Pointer(colony-0x50)==image+0x519D620)context.colonisation=colony;
var exploration=RegisteredComponent(services.registry,Read<uint>(image+0x5F60E14));
if(exploration>=0x10058&&Pointer(exploration)==image+0x51B3A10&&Pointer(exploration-0x58)==image+0x51B38E0&&Pointer(exploration-0x50)==image+0x51B3910)
{
// 1357780 owns this child at base+158; 138C9A0 returns child+110
// exactly when child+100 is ready (3). 1397F40 binds +108 to
// the original current system. Do not call its request path.
var child=Pointer(exploration+0x100);
if(child>=0x10000&&Read<ulong>(child+0x108)==context.currentSystem)context.explorationChild=child;
}
return true;
}
catch{context=new();return false;}
}
static bool? GalaxyFactExploration(GalaxyFilterContext context,ulong address)
{
try
{
var child=context.explorationChild;if(child==0||Read<int>(child+0x100)!=3||Read<ulong>(child+0x108)!=context.currentSystem)return null;
if(!GalaxyFactFindTree(child+0x120,address,out var node))return null;
if(Read<int>(child+0x100)!=3||Read<ulong>(child+0x108)!=context.currentSystem)return null;
return node!=0;
}
catch{return null;}
}
static int? GalaxyFactMetadataStatus(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.metadata;if(source==0||Pointer(source)!=context.expectedImage+0x551A1B0)return null;
if(!GalaxyFactFindHash(source+0xA8,address,out var node))return null;
if(node==0&&!GalaxyFactFindHash(source+0xC8,address,out node))return null;
if(node==0)return null; // Original cache missing is not a known no-data descriptor.
// Original3B26E30 tests descriptor+20 !=1. Economy getter
// 3B25D40 proves that this is the primary economy enum, not a
// four-state ready flag: Industrial4 through Carrier28 are valid.
var state=Read<int>(node+0x30);if(state is <0 or >28||Read<ulong>(node+8)!=address)return null;
return state;
}
catch{return null;}
}
static bool? GalaxyFactTradeOwnedOrPending(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.trade;if(source==0||Pointer(source)!=context.expectedImage+0x552EBA0)return null;
if(!GalaxyFactFindHash(source+0xB8,address,out var owned))return null;
if(owned!=0)return true;
if(!GalaxyFactFindHash(source+0xD8,address,out var pending))return null;
return pending!=0;
}
catch{return null;}
}
static bool? GalaxyFactVisited(GalaxyFilterContext context,ulong address)
{
try
{
var source=context.visited;
if(source==0||Pointer(source)!=context.expectedImage+0x52A5258||Read<byte>(source+0xB0)!=1)return null;
// 3B27080 -> interface+20 (239BCF0) tests this exact runtime map.
// 25DAEA0 marks it ready only after 2634B80 loads the original
// cache. A positive is valid even when that cache is partial.
if(!GalaxyFactFindHash(source+0x90,address,out var node)||Read<byte>(source+0xB0)!=1)return null;
if(node!=0)return true;
// The original file can be limited. Require its live manager's
// $stat_Systems_Visited count before accepting a negative. Missing
// statistics use limit FFFFFFFF and cannot establish completeness.
var expected=Read<uint>(source+0xB4);var limit=Read<uint>(source+0xB8);var count=Read<ulong>(source+0xA8);
if(limit==uint.MaxValue||expected>4_000_000||limit!=expected+(expected>>3)||count<expected)return null;
return false;
}
catch{return null;}
}
static bool CopyGalaxyFilterFacts(GalaxyFilterContext context,ulong address,double[] position,out GalaxyFilterFacts facts)
{
facts=default;
if(address==0||address>=0x80000000000000UL||position is not{Length:3}||!double.IsFinite(position[0])||!double.IsFinite(position[1])||!double.IsFinite(position[2]))return false;
var visited=GalaxyFactVisited(context,address);facts=new(visited,null,null,null,null);
if(!context.hasCurrentPosition)return true;
// Match 3B25E20/3B26E30's float-distance test and strict 20ly boundary.
var dx=(float)(position[0]-context.currentPosition.x);var dy=(float)(position[1]-context.currentPosition.y);var dz=(float)(position[2]-context.currentPosition.z);
var within=MathF.Sqrt(dx*dx+dy*dy+dz*dz)<20;
if(!within){facts=new(visited,null,null,false,null);return true;}
var available=GalaxyFactExploration(context,address);var status=GalaxyFactMetadataStatus(context,address);
string? trade=null;
if(status==1&&context.trade!=0)trade="nodataavailable";
else if(status is not null&&GalaxyFactTradeOwnedOrPending(context,address) is {} owned)trade=owned?"havedata":"dataavailable";
facts=new(visited,available is {} known?known?"dataavailable":"nodataavailable":null,trade,true,status);
return true;
}
static bool TryReadGalaxyFilterFacts(GalaxyFilterContext context,ulong systemAddress,double[] position,out GalaxyFilterFacts facts)
{
facts=default;
if(!Enabled()||!inGame||context.thread==0||context.thread!=owningThread||GetCurrentThreadId()!=context.thread||
GetTickCount64()-context.tick>500)return false;
var frame=Current();
if(!frame.valid||frame.entity!=context.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=context.currentSystem)return false;
return CopyGalaxyFilterFacts(context,systemAddress,position,out facts);
}
}