XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Buffers.Binary;

namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    internal sealed record GalaxyCommodityBalanceRow(ulong systemAddress,float balance);
    internal sealed record GalaxyCommodityBalance(ulong tick,ulong entity,uint commodityUid,bool ready,uint revision,GalaxyCommodityBalanceRow[] items)
    {
        // This is the original component's selected-commodity cache. It does
        // not prove the original filter's distance/descriptor gates or absence.
        internal bool complete=>false;
    }
    static uint GalaxyCommoditySystemHash(ulong address)
    {
        unchecked
        {var value=address*0x40000+~address;value=(value>>31^value)*0x15;value=(value>>11^value)*0x41;return (uint)(value>>22)^(uint)value;}
    }
    static GalaxyCommodityBalanceRow[] CopyGalaxyCommodityBalances(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 nodes=new HashSet<nuint>();var ids=new HashSet<ulong>();var rows=new List<GalaxyCommodityBalanceRow>();
        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||!nodes.Add(node)||(ulong)nodes.Count>count)throw new InvalidDataException();
                var id=copy.U64(node+8);var balance=BitConverter.Int32BitsToSingle(unchecked((int)copy.U32(node+0x10)));
                if(id==0||id>=0x80000000000000UL||GalaxyCommoditySystemHash(id)%capacity!=(ulong)index||!ids.Add(id)||!float.IsFinite(balance))throw new InvalidDataException();
                rows.Add(new(id,balance));node=copy.Ptr(node);
            }
        }
        if((ulong)rows.Count!=count)throw new InvalidDataException();return rows.ToArray();
    }
    static bool CopyGalaxyCommodityBalance(nuint service,nuint definitions,nuint sourceOwner,nuint definitionsOwner,nuint expectedImage,
        out uint uid,out bool ready,out uint revision,out GalaxyCommodityBalanceRow[] items)
    {
        uid=0;ready=false;revision=0;items=[];
        try
        {
            var copy=new GalaxyCommodityCopy();
            if(service<0x10058||definitions<0x10000||sourceOwner<0x10000||definitionsOwner<0x10000||
               copy.Ptr(service)!=expectedImage+0x52420C0||copy.Ptr(service-0x58)!=expectedImage+0x5241F80||copy.Ptr(service-0x50)!=expectedImage+0x5241FC0||copy.Ptr(service-0x48)!=sourceOwner||
               copy.Ptr(definitions)!=expectedImage+0x5219600||copy.Ptr(definitions+8)!=expectedImage+0x5219630||copy.Ptr(definitions+0x58)!=expectedImage+0x5219730||copy.Ptr(definitions+0x10)!=definitionsOwner||copy.U32(definitions+0x284)!=1)return false;
            // 1E4D5E0 stores the actual selected definition at interface+E8.
            // Resolve by exact instance in the real definition map, not a
            // guessed market ID, table order or a reconstructed name hash.
            var selected=copy.Ptr(service+0xE8);if(selected<0x10000||copy.U32(service+0xF0)==0)return false;
            var definitionsById=GalaxyCommodityNodes(copy,definitions+0x1B8);var matching=definitionsById.Where(pair=>pair.Value+0x10==selected).ToArray();
            if(matching.Length!=1)return false;var currentUid=matching[0].Key;
            _=copy.Name(copy.Ptr(selected+8));
            var loaded=copy.Bytes(service+0xE0,1)[0];var dirty=copy.Bytes(service+0x140,1)[0];
            if(loaded>1||dirty>1)return false;
            var currentReady=loaded==1&&copy.U64(service+0x2C0)==0&&dirty==0;
            var currentRevision=copy.U32(service+0x144);
            // 1E6BC80 clears/rebuilds object+150 (interface+F8) and advances
            // object+19C. Pending/dirty caches retain no publishable old rows.
            var result=currentReady?CopyGalaxyCommodityBalances(copy,service+0xF8):[];
            if(!copy.Stable())return false;uid=currentUid;ready=currentReady;revision=currentRevision;items=result;return true;
        }
        catch{return false;}
    }
    static bool TryReadGalaxyCommodityBalance(Snapshot frame,out GalaxyCommodityBalance result)
    {
        result=new(0,0,0,false,0,[]);
        if(!Enabled()||!inGame||!frame.valid||frame.physics==0||owningThread==0||GetCurrentThreadId()!=owningThread||GetTickCount64()-frame.tick>500)return false;
        try
        {
            if(!TryGetGalaxyAccountRegistry(frame,out var registry))return false;
            var sourceType=Read<uint>(image+0x5F7E844);var definitionsType=Read<uint>(image+0x5F05C34);
            var source=RegisteredComponent(registry,sourceType);var definitions=RegisteredComponent(registry,definitionsType);
            if(source<0x10058||definitions<0x10058)return false;var definitionBase=definitions-0x58;
            if(!CopyGalaxyCommodityBalance(source,definitionBase,Pointer(source-0x48),Pointer(definitionBase+0x10),image,out var uid,out var ready,out var revision,out var rows)||
               !TryGetGalaxyAccountRegistry(frame,out var currentRegistry)||currentRegistry!=registry||RegisteredComponent(registry,sourceType)!=source||RegisteredComponent(registry,definitionsType)!=definitions||
               Read<uint>(image+0x5F7E844)!=sourceType||Read<uint>(image+0x5F05C34)!=definitionsType||Current().entity!=frame.entity)return false;
            result=new(frame.tick,frame.entity,uid,ready,revision,rows);return true;
        }
        catch{return false;}
    }
}