using System.Buffers.Binary;
using System.Numerics;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// Pure port of1E6BC80 over the existing all-commodity cache. It does not
// select another definition, subscribe a neighbourhood or invoke game code.
internal readonly record struct GalaxyCommodityQuote(uint Buy,uint Sell,uint Flags);
static IReadOnlyDictionary<ulong,nuint> GalaxyCommodityAddressNodes(GalaxyCommodityCopy copy,nuint map)
{
var buckets=copy.Ptr(map);var capacity=copy.U64(map+8);var count=copy.U64(map+0x18);
if(capacity is 0 or >65536||count>4096)throw new InvalidDataException();
var heads=copy.Bytes(buckets,checked((int)capacity*8));var seen=new HashSet<nuint>();var result=new Dictionary<ulong,nuint>();
for(nuint index=0;index<(nuint)capacity;index++)
{
var sentinel=buckets+index*8;var node=(nuint)BinaryPrimitives.ReadUInt64LittleEndian(heads.AsSpan((int)index*8,8));
while(node!=sentinel)
{
if(node<0x10000||!seen.Add(node)||(ulong)seen.Count>count)throw new InvalidDataException();
var id=copy.U64(node+8);if(id==0||GalaxyCommoditySystemHash(id)%capacity!=(ulong)index||!result.TryAdd(id,node))throw new InvalidDataException();
node=copy.Ptr(node);
}
}
if((ulong)result.Count!=count)throw new InvalidDataException();return result;
}
static GalaxyCommodityQuote? GalaxyCommodityPrice(GalaxyCommodityCopy copy,nuint table,uint marketId)
{
// B1F390: occupancy bits, then16-byte rows; linear probing wraps at
// capacity. A missing row supplies the original default quote below.
var count=copy.U64(table+8);var capacity=copy.U64(table+0x10);var data=copy.Ptr(table+0x18);
if(count==0)return null;
if(capacity is 0 or >65536||(capacity&(capacity-1))!=0||count>=capacity)throw new InvalidDataException();
var bytes=checked((int)(((capacity>>3)+7)&~7UL));var bitmap=copy.Bytes(data,bytes);
var occupied=0;for(var i=0;i<bitmap.Length;i+=8)occupied+=BitOperations.PopCount(BinaryPrimitives.ReadUInt64LittleEndian(bitmap.AsSpan(i,8)));
if((ulong)occupied!=count)throw new InvalidDataException();
var slot=(ulong)GalaxyCommodityHash(marketId)&(capacity-1);
for(ulong attempt=0;attempt<capacity;attempt++,slot=(slot+1)&(capacity-1))
{
if((bitmap[slot>>3]&(1<<(int)(slot&7)))==0)return null;
var row=data+(nuint)bytes+(nuint)slot*0x10;
if(copy.U32(row)==marketId)return new(copy.U32(row+4),copy.U32(row+8),copy.U32(row+0xC));
}
throw new InvalidDataException();
}
static GalaxyCommodityBalanceRow[] CopyGalaxyCommodityRebuild(GalaxyCommodityCopy copy,nuint service,nuint enumeration,nuint definitions,nuint expectedImage,uint uid,out Func<bool> stable)
{
stable=()=>false;
if(enumeration<0x10058||copy.Ptr(enumeration)!=expectedImage+0x5241A18||copy.Ptr(enumeration-0x58)!=expectedImage+0x52418E8||
copy.Ptr(enumeration-0x50)!=expectedImage+0x5241918||copy.Ptr(enumeration-0x48)<0x10000||copy.Bytes(enumeration+0x50,1)[0]!=1||
copy.Ptr(service+0x50)!=enumeration||copy.Ptr(service+0x48)!=definitions+0x58)throw new InvalidDataException();
var entries=GalaxyCommodityNodes(copy,enumeration+0x58);
var defined=GalaxyCommodityNodes(copy,definitions+0x1B8);
if(!entries.TryGetValue(uid,out var entry)||!defined.TryGetValue(uid,out var definition)||copy.U32(entry+0x14) is not(1 or 2))throw new InvalidDataException();
_=copy.Name(copy.Ptr(definition+0x18));
// Original enumeration parser1E34530 stores cost_mean and id in this
// exact row. Neither the UID nor any observed Market ID is guessed.
// 1E102E0 returns MOVSS[node+C] in XMM0: cost_mean is float32 bits,
// unlike integer station quotes. Ghidra's undefined4 is not uint here.
var average=GalaxyCommodityFloat(copy,entry+0xC);var marketId=copy.U32(entry+0x10);if(marketId==0||!float.IsFinite(average)||average<0)throw new InvalidDataException();
// The original dirty/pending/revision gates guard raw cache rebuilds.
// Independently retain only the small enum/definition identity here;
// rechecking the entire market bitmap for every target would be wasteful.
var identity=new GalaxyCommodityCopy();
_=identity.Bytes(enumeration,8);_=identity.Bytes(enumeration+0x50,1);_=identity.Bytes(service+0x48,0x10);
foreach(var map in new[]{enumeration+0x58,definitions+0x1B8})
{var buckets=identity.Ptr(map);var capacity=identity.U64(map+8);_=identity.Bytes(map,0x20);_=identity.Bytes(buckets,checked((int)capacity*8));}
foreach(var node in entries.Values.Concat(defined.Values))_=identity.Bytes(node,8);
_=identity.Bytes(entry+8,0x14);_=identity.Bytes(definition+8,0x18);
var source=service-0x58;var surface=copy.Bytes(source+0x190,1)[0];var large=copy.Bytes(source+0x191,1)[0];var maximum=GalaxyCommodityFloat(copy,source+0x194);
if(surface>1||large>1||!float.IsFinite(maximum)||maximum<0)throw new InvalidDataException();
var markets=GalaxyCommodityAddressNodes(copy,source+0xF8);var systems=GalaxyCommodityAddressNodes(copy,source+0x118);
var temporary=new Dictionary<ulong,(uint Buy,uint Sell)>();uint lowest=int.MaxValue,highest=0;
foreach(var pair in systems)
{
if(pair.Key>=0x80000000000000UL)throw new InvalidDataException();
var count=copy.U64(pair.Value+0x40);var rows=copy.Ptr(pair.Value+0x48);if(count>4096)throw new InvalidDataException();
for(nuint index=0;index<(nuint)count;index++)
{
var row=rows+index*0x20;var id=copy.U64(row);var distance=GalaxyCommodityFloat(copy,row+0x10);var onSurface=copy.Bytes(row+0x14,1)[0];var largePads=copy.Bytes(row+0x17,1)[0];
if(id==0||!float.IsFinite(distance)||distance<0||onSurface>1||largePads>1)throw new InvalidDataException();
if(!markets.TryGetValue(id,out var market)||surface==0&&onSurface!=0||large!=0&&largePads==0||distance>maximum)continue;
var quote=GalaxyCommodityPrice(copy,market+0x10,marketId)??new(int.MaxValue,0,0);
// The original creates a system temporary even for a missing
// commodity or an ignored/illegal quote, yielding real zero.
var best=temporary.GetValueOrDefault(pair.Key,((uint)int.MaxValue,0u));
if((quote.Flags&4)==0&&(quote.Flags&3)!=0)
{
if((quote.Flags&2)!=0&"e.Buy<best.Item1){best.Item1=quote.Buy;lowest=Math.Min(lowest,quote.Buy);}
if((quote.Flags&1)!=0&"e.Sell>best.Item2){best.Item2=quote.Sell;highest=Math.Max(highest,quote.Sell);}
}
temporary[pair.Key]=best;
}
}
var buyScale=MathF.Max(average-lowest,1);var sellScale=MathF.Max(highest-average,1);var result=new List<GalaxyCommodityBalanceRow>(temporary.Count);
foreach(var pair in temporary)
{
var buy=(average-pair.Value.Buy)/buyScale;var sell=(pair.Value.Sell-average)/sellScale;float value=0;
if(pair.Value.Buy!=int.MaxValue&&(pair.Value.Sell==0||sell<=buy))value=-MathF.Max(buy,.01f);
else if(pair.Value.Sell!=0)value=MathF.Max(sell,.01f);
if(!float.IsFinite(value))throw new InvalidDataException();result.Add(new(pair.Key,value));
}
stable=identity.Stable;return result.ToArray();
}
}
using System.Buffers.Binary;
using System.Numerics;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
// Pure port of1E6BC80 over the existing all-commodity cache. It does not
// select another definition, subscribe a neighbourhood or invoke game code.
internal readonly record struct GalaxyCommodityQuote(uint Buy,uint Sell,uint Flags);
static IReadOnlyDictionary<ulong,nuint> GalaxyCommodityAddressNodes(GalaxyCommodityCopy copy,nuint map)
{
var buckets=copy.Ptr(map);var capacity=copy.U64(map+8);var count=copy.U64(map+0x18);
if(capacity is 0 or >65536||count>4096)throw new InvalidDataException();
var heads=copy.Bytes(buckets,checked((int)capacity*8));var seen=new HashSet<nuint>();var result=new Dictionary<ulong,nuint>();
for(nuint index=0;index<(nuint)capacity;index++)
{
var sentinel=buckets+index*8;var node=(nuint)BinaryPrimitives.ReadUInt64LittleEndian(heads.AsSpan((int)index*8,8));
while(node!=sentinel)
{
if(node<0x10000||!seen.Add(node)||(ulong)seen.Count>count)throw new InvalidDataException();
var id=copy.U64(node+8);if(id==0||GalaxyCommoditySystemHash(id)%capacity!=(ulong)index||!result.TryAdd(id,node))throw new InvalidDataException();
node=copy.Ptr(node);
}
}
if((ulong)result.Count!=count)throw new InvalidDataException();return result;
}
static GalaxyCommodityQuote? GalaxyCommodityPrice(GalaxyCommodityCopy copy,nuint table,uint marketId)
{
// B1F390: occupancy bits, then16-byte rows; linear probing wraps at
// capacity. A missing row supplies the original default quote below.
var count=copy.U64(table+8);var capacity=copy.U64(table+0x10);var data=copy.Ptr(table+0x18);
if(count==0)return null;
if(capacity is 0 or >65536||(capacity&(capacity-1))!=0||count>=capacity)throw new InvalidDataException();
var bytes=checked((int)(((capacity>>3)+7)&~7UL));var bitmap=copy.Bytes(data,bytes);
var occupied=0;for(var i=0;i<bitmap.Length;i+=8)occupied+=BitOperations.PopCount(BinaryPrimitives.ReadUInt64LittleEndian(bitmap.AsSpan(i,8)));
if((ulong)occupied!=count)throw new InvalidDataException();
var slot=(ulong)GalaxyCommodityHash(marketId)&(capacity-1);
for(ulong attempt=0;attempt<capacity;attempt++,slot=(slot+1)&(capacity-1))
{
if((bitmap[slot>>3]&(1<<(int)(slot&7)))==0)return null;
var row=data+(nuint)bytes+(nuint)slot*0x10;
if(copy.U32(row)==marketId)return new(copy.U32(row+4),copy.U32(row+8),copy.U32(row+0xC));
}
throw new InvalidDataException();
}
static GalaxyCommodityBalanceRow[] CopyGalaxyCommodityRebuild(GalaxyCommodityCopy copy,nuint service,nuint enumeration,nuint definitions,nuint expectedImage,uint uid,out Func<bool> stable)
{
stable=()=>false;
if(enumeration<0x10058||copy.Ptr(enumeration)!=expectedImage+0x5241A18||copy.Ptr(enumeration-0x58)!=expectedImage+0x52418E8||
copy.Ptr(enumeration-0x50)!=expectedImage+0x5241918||copy.Ptr(enumeration-0x48)<0x10000||copy.Bytes(enumeration+0x50,1)[0]!=1||
copy.Ptr(service+0x50)!=enumeration||copy.Ptr(service+0x48)!=definitions+0x58)throw new InvalidDataException();
var entries=GalaxyCommodityNodes(copy,enumeration+0x58);
var defined=GalaxyCommodityNodes(copy,definitions+0x1B8);
if(!entries.TryGetValue(uid,out var entry)||!defined.TryGetValue(uid,out var definition)||copy.U32(entry+0x14) is not(1 or 2))throw new InvalidDataException();
_=copy.Name(copy.Ptr(definition+0x18));
// Original enumeration parser1E34530 stores cost_mean and id in this
// exact row. Neither the UID nor any observed Market ID is guessed.
// 1E102E0 returns MOVSS[node+C] in XMM0: cost_mean is float32 bits,
// unlike integer station quotes. Ghidra's undefined4 is not uint here.
var average=GalaxyCommodityFloat(copy,entry+0xC);var marketId=copy.U32(entry+0x10);if(marketId==0||!float.IsFinite(average)||average<0)throw new InvalidDataException();
// The original dirty/pending/revision gates guard raw cache rebuilds.
// Independently retain only the small enum/definition identity here;
// rechecking the entire market bitmap for every target would be wasteful.
var identity=new GalaxyCommodityCopy();
_=identity.Bytes(enumeration,8);_=identity.Bytes(enumeration+0x50,1);_=identity.Bytes(service+0x48,0x10);
foreach(var map in new[]{enumeration+0x58,definitions+0x1B8})
{var buckets=identity.Ptr(map);var capacity=identity.U64(map+8);_=identity.Bytes(map,0x20);_=identity.Bytes(buckets,checked((int)capacity*8));}
foreach(var node in entries.Values.Concat(defined.Values))_=identity.Bytes(node,8);
_=identity.Bytes(entry+8,0x14);_=identity.Bytes(definition+8,0x18);
var source=service-0x58;var surface=copy.Bytes(source+0x190,1)[0];var large=copy.Bytes(source+0x191,1)[0];var maximum=GalaxyCommodityFloat(copy,source+0x194);
if(surface>1||large>1||!float.IsFinite(maximum)||maximum<0)throw new InvalidDataException();
var markets=GalaxyCommodityAddressNodes(copy,source+0xF8);var systems=GalaxyCommodityAddressNodes(copy,source+0x118);
var temporary=new Dictionary<ulong,(uint Buy,uint Sell)>();uint lowest=int.MaxValue,highest=0;
foreach(var pair in systems)
{
if(pair.Key>=0x80000000000000UL)throw new InvalidDataException();
var count=copy.U64(pair.Value+0x40);var rows=copy.Ptr(pair.Value+0x48);if(count>4096)throw new InvalidDataException();
for(nuint index=0;index<(nuint)count;index++)
{
var row=rows+index*0x20;var id=copy.U64(row);var distance=GalaxyCommodityFloat(copy,row+0x10);var onSurface=copy.Bytes(row+0x14,1)[0];var largePads=copy.Bytes(row+0x17,1)[0];
if(id==0||!float.IsFinite(distance)||distance<0||onSurface>1||largePads>1)throw new InvalidDataException();
if(!markets.TryGetValue(id,out var market)||surface==0&&onSurface!=0||large!=0&&largePads==0||distance>maximum)continue;
var quote=GalaxyCommodityPrice(copy,market+0x10,marketId)??new(int.MaxValue,0,0);
// The original creates a system temporary even for a missing
// commodity or an ignored/illegal quote, yielding real zero.
var best=temporary.GetValueOrDefault(pair.Key,((uint)int.MaxValue,0u));
if((quote.Flags&4)==0&&(quote.Flags&3)!=0)
{
if((quote.Flags&2)!=0&"e.Buy<best.Item1){best.Item1=quote.Buy;lowest=Math.Min(lowest,quote.Buy);}
if((quote.Flags&1)!=0&"e.Sell>best.Item2){best.Item2=quote.Sell;highest=Math.Max(highest,quote.Sell);}
}
temporary[pair.Key]=best;
}
}
var buyScale=MathF.Max(average-lowest,1);var sellScale=MathF.Max(highest-average,1);var result=new List<GalaxyCommodityBalanceRow>(temporary.Count);
foreach(var pair in temporary)
{
var buy=(average-pair.Value.Buy)/buyScale;var sell=(pair.Value.Sell-average)/sellScale;float value=0;
if(pair.Value.Buy!=int.MaxValue&&(pair.Value.Sell==0||sell<=buy))value=-MathF.Max(buy,.01f);
else if(pair.Value.Sell!=0)value=MathF.Max(sell,.01f);
if(!float.IsFinite(value))throw new InvalidDataException();result.Add(new(pair.Key,value));
}
stable=identity.Stable;return result.ToArray();
}
}