using System.Buffers.Binary;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// 1184310/1192F80 construct ColonisationDataCache. Its interface type
// getter 1196460 uses 5F5A820. 11968D0 copies a 256-byte claim record
// from this map; JSON decoder 11A5360 proves every exported field.
internal sealed record GalaxyAccountColonisationEntry(ulong systemAddress,ulong commanderId,string architectName,
byte status,long megashipMarketId,bool unusedConstructionSlots,bool inProgressConstructionSlots,ulong statusUpdated)
{
internal string statusName=>status switch{0=>"failed",1=>"initial",2=>"deployed",3=>"established",_=>"unknown"};
internal bool active=>status is 1 or 2 or 3;
}
internal sealed record GalaxyAccountColonisation(ulong tick,ulong entity,GalaxyAccountColonisationEntry[] items)
{
internal bool complete=>false;
internal string source=>"original-colonisation-data-cache";
}
static bool CopyGalaxyColonisationManager(nuint manager,nuint owner,nuint expectedImage,out GalaxyAccountColonisationEntry[] items)
{
items=[];
try
{
if(manager==0||owner==0||Pointer(manager)!=expectedImage+0x519DA40||
Pointer(manager+8)!=expectedImage+0x519DA70||Pointer(manager+0x58)!=expectedImage+0x519DB70||
Pointer(manager+0x10)!=owner)return false;
var map=manager+0x160;var count=Read<ulong>(map+8);var capacity=Read<ulong>(map+0x10);var storage=Pointer(map+0x18);
if(count>16384||capacity>65536||count>capacity)return false;
if(count==0)return true;
if(capacity<8||(capacity&(capacity-1))!=0||storage<0x10000)return false;
var bitBytes=(int)((capacity/8+15)&~15UL);var bits=Platform.ReadBytes(storage,bitBytes);
var rows=storage+(nuint)bitBytes;var result=new GalaxyAccountColonisationEntry[(int)count];var index=0;
HashSet<ulong> addresses=[];
for(nuint slot=0;slot<(nuint)capacity;slot++)
{
if((bits[(int)(slot/8)]&(1<<((int)slot&7)))==0)continue;
if(index>=result.Length)return false;
var at=rows+slot*0x110;var row=Platform.ReadBytes(at,0x110);
ulong U64(int offset)=>BinaryPrimitives.ReadUInt64LittleEndian(row.AsSpan(offset));
var system=U64(0);var valueSystem=U64(0x18);var commander=U64(0x20);var status=row[0x10];
if(system==0||system>=0x80000000000000UL||valueSystem!=system||!addresses.Add(system)||
status>3||row[0xD8]>1||row[0xD9]>1||!GalaxyAccountString((nuint)U64(0x28),out var architect))return false;
if(!Platform.ReadBytes(at,0x110).AsSpan().SequenceEqual(row))return false;
result[index++]=new(system,commander,architect,status,unchecked((long)U64(0xD0)),row[0xD8]!=0,row[0xD9]!=0,U64(0xE0));
}
if(index!=result.Length||Read<ulong>(map+8)!=count||Read<ulong>(map+0x10)!=capacity||Pointer(map+0x18)!=storage||
!Platform.ReadBytes(storage,bitBytes).AsSpan().SequenceEqual(bits)||Pointer(manager+0x10)!=owner||
Pointer(manager)!=expectedImage+0x519DA40)return false;
items=result;return true;
}
catch{return false;}
}
static bool TryReadGalaxyAccountColonisation(Snapshot frame,out GalaxyAccountColonisation result)
{
result=new(0,0,[]);
if(!Enabled()||!inGame||!frame.valid||frame.physics==0||owningThread==0||GetCurrentThreadId()!=owningThread)return false;
try
{
if(!TryGetGalaxyAccountRegistry(frame,out var registry))return false;
var component=RegisteredComponent(registry,Read<uint>(image+0x5F5A820));
if(component<0x10058||Pointer(component)!=image+0x519DB70)return false;
var manager=component-0x58;var owner=Pointer(manager+0x10);if(owner==0)return false;
if(!CopyGalaxyColonisationManager(manager,owner,image,out var items))return false;
result=new(frame.tick,frame.entity,items);return true;
}
catch{return false;}
}
}
using System.Buffers.Binary;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// 1184310/1192F80 construct ColonisationDataCache. Its interface type
// getter 1196460 uses 5F5A820. 11968D0 copies a 256-byte claim record
// from this map; JSON decoder 11A5360 proves every exported field.
internal sealed record GalaxyAccountColonisationEntry(ulong systemAddress,ulong commanderId,string architectName,
byte status,long megashipMarketId,bool unusedConstructionSlots,bool inProgressConstructionSlots,ulong statusUpdated)
{
internal string statusName=>status switch{0=>"failed",1=>"initial",2=>"deployed",3=>"established",_=>"unknown"};
internal bool active=>status is 1 or 2 or 3;
}
internal sealed record GalaxyAccountColonisation(ulong tick,ulong entity,GalaxyAccountColonisationEntry[] items)
{
internal bool complete=>false;
internal string source=>"original-colonisation-data-cache";
}
static bool CopyGalaxyColonisationManager(nuint manager,nuint owner,nuint expectedImage,out GalaxyAccountColonisationEntry[] items)
{
items=[];
try
{
if(manager==0||owner==0||Pointer(manager)!=expectedImage+0x519DA40||
Pointer(manager+8)!=expectedImage+0x519DA70||Pointer(manager+0x58)!=expectedImage+0x519DB70||
Pointer(manager+0x10)!=owner)return false;
var map=manager+0x160;var count=Read<ulong>(map+8);var capacity=Read<ulong>(map+0x10);var storage=Pointer(map+0x18);
if(count>16384||capacity>65536||count>capacity)return false;
if(count==0)return true;
if(capacity<8||(capacity&(capacity-1))!=0||storage<0x10000)return false;
var bitBytes=(int)((capacity/8+15)&~15UL);var bits=Platform.ReadBytes(storage,bitBytes);
var rows=storage+(nuint)bitBytes;var result=new GalaxyAccountColonisationEntry[(int)count];var index=0;
HashSet<ulong> addresses=[];
for(nuint slot=0;slot<(nuint)capacity;slot++)
{
if((bits[(int)(slot/8)]&(1<<((int)slot&7)))==0)continue;
if(index>=result.Length)return false;
var at=rows+slot*0x110;var row=Platform.ReadBytes(at,0x110);
ulong U64(int offset)=>BinaryPrimitives.ReadUInt64LittleEndian(row.AsSpan(offset));
var system=U64(0);var valueSystem=U64(0x18);var commander=U64(0x20);var status=row[0x10];
if(system==0||system>=0x80000000000000UL||valueSystem!=system||!addresses.Add(system)||
status>3||row[0xD8]>1||row[0xD9]>1||!GalaxyAccountString((nuint)U64(0x28),out var architect))return false;
if(!Platform.ReadBytes(at,0x110).AsSpan().SequenceEqual(row))return false;
result[index++]=new(system,commander,architect,status,unchecked((long)U64(0xD0)),row[0xD8]!=0,row[0xD9]!=0,U64(0xE0));
}
if(index!=result.Length||Read<ulong>(map+8)!=count||Read<ulong>(map+0x10)!=capacity||Pointer(map+0x18)!=storage||
!Platform.ReadBytes(storage,bitBytes).AsSpan().SequenceEqual(bits)||Pointer(manager+0x10)!=owner||
Pointer(manager)!=expectedImage+0x519DA40)return false;
items=result;return true;
}
catch{return false;}
}
static bool TryReadGalaxyAccountColonisation(Snapshot frame,out GalaxyAccountColonisation result)
{
result=new(0,0,[]);
if(!Enabled()||!inGame||!frame.valid||frame.physics==0||owningThread==0||GetCurrentThreadId()!=owningThread)return false;
try
{
if(!TryGetGalaxyAccountRegistry(frame,out var registry))return false;
var component=RegisteredComponent(registry,Read<uint>(image+0x5F5A820));
if(component<0x10058||Pointer(component)!=image+0x519DB70)return false;
var manager=component-0x58;var owner=Pointer(manager+0x10);if(owner==0)return false;
if(!CopyGalaxyColonisationManager(manager,owner,image,out var items))return false;
result=new(frame.tick,frame.entity,items);return true;
}
catch{return false;}
}
}