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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    // ED5 source, paired to old3AC7FA0/552B260/+18=3B06440.
    // No original classifier, hash lookup, loader or request is invoked.
    static bool GalaxyCarrierSource(nuint source,nuint expectedImage)=>source>=0x10058&&
        Pointer(source)==expectedImage+0x552B230&&Pointer(source-0x58)==expectedImage+0x552B100&&
        Pointer(source-0x50)==expectedImage+0x552B130&&
        Pointer(expectedImage+0x552B230)==expectedImage+0x3B12D10&&
        Pointer(expectedImage+0x552B230+0x18)==expectedImage+0x3B063A0;

    static bool CopyGalaxyCarrierFacts(nuint source,nuint expectedImage,ulong address,out bool? dockable)
    {
        dockable=null;
        if(address==0||address>=1UL<<55)return false;
        try
        {
            if(!GalaxyCarrierSource(source,expectedImage))return false;
            var set=source+0x48;var count=Read<ulong>(set+8);var capacity=Read<ulong>(set+0x10);var storage=Pointer(set+0x18);
            bool Stable()=>GalaxyCarrierSource(source,expectedImage)&&Read<ulong>(set+8)==count&&
                Read<ulong>(set+0x10)==capacity&&Pointer(set+0x18)==storage;
            // The original getter has no independent ready test and returns
            // false for an empty current set. This is its cache classification,
            // never a claim that no physical carrier exists in the system.
            if(count==0&&capacity==0&&storage==0){if(!Stable())return false;dockable=false;return true;}
            if(capacity is <8 or >1_048_576||(capacity&(capacity-1))!=0||count>capacity||storage<0x10000)return false;
            var observed=new List<(nuint at,ulong value)>();
            var keys=storage+(nuint)(((capacity>>3)+7)&~7UL);var slot=GalaxyNavRouteHash(address)&(capacity-1);bool? found=null;
            for(ulong step=0;step<Math.Min(capacity,4096UL);step++,slot=(slot+1)&(capacity-1))
            {
                var at=storage+(nuint)(slot>>6)*8;var bits=Read<ulong>(at);observed.Add((at,bits));
                if((bits&(1UL<<(int)(slot&63)))==0){found=false;break;}
                if(count==0)return false;
                at=keys+(nuint)slot*8;var key=Read<ulong>(at);observed.Add((at,key));
                if(key==address){found=true;break;}
            }
            if(found is null||!Stable()||observed.Any(v=>Read<ulong>(v.at)!=v.value))return false;
            dockable=found;return true;
        }
        catch{dockable=null;return false;}
    }

    static bool TryReadGalaxyCarrierFacts(GalaxyFilterContext context,nuint registry,ulong address,out bool? dockable)
    {
        dockable=null;
        if(!Enabled()||!inGame||context.expectedImage!=image||context.thread==0||context.thread!=owningThread||
           GetCurrentThreadId()!=context.thread||GetTickCount64()-context.tick>500||registry<0x10000)return false;
        var frame=Current();
        if(!frame.valid||frame.entity!=context.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=context.currentSystem)return false;
        try
        {
            var type=Read<uint>(image+0x5FDC5C8);var source=RegisteredComponent(registry,type);
            if(!CopyGalaxyCarrierFacts(source,image,address,out var value))return false;
            var after=Current();
            if(!after.valid||after.entity!=frame.entity||(after.referenceWorld&0x007FFFFFFFFFFFFFUL)!=context.currentSystem||
               Read<uint>(image+0x5FDC5C8)!=type||RegisteredComponent(registry,type)!=source)return false;
            dockable=value;return true;
        }
        catch{return false;}
    }
}