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
{
    // Names describe actual field consumers. allegianceId is a definition
    // identifier, not a guessed Allegiance enum. factionStateId is the base
    // metadata state, not the independent pseudo-unpopulated/Thargoid layers.
    internal sealed record GalaxyBasicSystemFact(ulong population,uint governmentId,uint primaryEconomyId,
        uint secondaryEconomyId,uint factionStateId,ulong allegianceId,byte securityValue)
    {
        internal string? allegianceKey {get;init;}
        // A ready original classifier's missing-row result, not an observed
        // descriptor or an assertion about the galaxy's physical population.
        internal bool defaulted {get;init;}
    }

    static bool GalaxyBasicConfirmedMissing(nuint map,ulong address)
    {
        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||
           !GalaxyFactFindHash(map,address,out var node)||node!=0)return false;
        // The general positive-only reader can skip empty buckets. A default
        // is negative evidence, so an empty selected bucket must itself be a
        // readable sentinel; an inaccessible/stale empty map is not absence.
        if(count==0)
        {
            var sentinel=buckets+(nuint)(GalaxyFactHash(address)%capacity)*8;
            if(Pointer(sentinel)!=sentinel)return false;
        }
        return Pointer(map)==buckets&&Read<ulong>(map+8)==capacity&&Read<ulong>(map+0x18)==count;
    }

    static bool CopyGalaxyBasicSystemFacts(GalaxyFilterContext context,ulong address,out GalaxyBasicSystemFact? result)
    {
        result=null;
        if(address==0||address>=0x80000000000000UL)return false;
        try
        {
            var source=context.metadata;
            if(source<0x10000||Pointer(source)!=context.expectedImage+0x551A1B0)return false;
            // Exact39BED80 traversal, including its second cache. No request,
            // lazy constructor or original getter is invoked.
            if(!GalaxyFactFindHash(source+0xA8,address,out var node))return false;
            if(node==0&&!GalaxyFactFindHash(source+0xC8,address,out node))return false;
            if(node==0)
            {
                // Original39C9CD0 (+18) and39C9C50 (+20) require both the
                // component lifecycle byte(base+48) and classification-ready
                // byte(base+168). Pending absence stays unknown. Once ready,
                // the actual colour/size consumers use these exact fallbacks:
                // Gov3B26180=1; Economy3B25D40=1/1; State3B262E0=0;
                // Population3B27FA0=0; Allegiance3B26030=Max;
                // Security1C40B90(null) returns0. Preserve their provenance.
                if(Read<byte>(source-0x18)==0||Read<byte>(source+0x108)==0)return true;
                if(!GalaxyBasicConfirmedMissing(source+0xA8,address)||!GalaxyBasicConfirmedMissing(source+0xC8,address))return false;
                if(Pointer(source)!=context.expectedImage+0x551A1B0||
                   Read<byte>(source-0x18)==0||Read<byte>(source+0x108)==0)return false;
                result=new(0,1,1,1,0,ulong.MaxValue,0){defaulted=true,allegianceKey="None"};
                return true;
            }
            var descriptor=node+0x10;var bytes=Platform.ReadBytes(descriptor,0x3B);
            var fact=new GalaxyBasicSystemFact(BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(0x30)),
                BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(0x18)),BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(0x20)),
                BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(0x24)),BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(0x1C)),
                BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(8)),bytes[0x3A]);
            if(Pointer(source)!=context.expectedImage+0x551A1B0||Read<ulong>(node+8)!=address||
               !Platform.ReadBytes(descriptor,0x3B).AsSpan().SequenceEqual(bytes))return false;
            // Re-resolve the key to reject a moved/replaced cache node even if
            // its old allocation has not yet been reclaimed.
            if(!GalaxyFactFindHash(source+0xA8,address,out var confirmed))return false;
            if(confirmed==0&&!GalaxyFactFindHash(source+0xC8,address,out confirmed))return false;
            if(confirmed!=node)return false;
            result=fact;return true;
        }
        catch{return false;}
    }

    static bool TryReadGalaxyBasicSystemFacts(GalaxyFilterContext context,ulong address,out GalaxyBasicSystemFact? result)
    {
        result=null;
        if(!Enabled()||!inGame||context.expectedImage!=image||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 CopyGalaxyBasicSystemFacts(context,address,out result);
    }
}