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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    // Pinned offline evidence: 24E47F0 allocates pool 5F93E30 (1E0 bytes),
    // 25BB580 installs these vtables, and 261F820 decodes the 60-byte rows.
    // This adapter never calls BookmarkManager.Update or its request methods.
    const int GalaxyBookmarkMaximum=4096;
    static readonly UTF8Encoding galaxyAccountUtf8=new(false,true);

    internal sealed record GalaxyAccountBookmark(ulong uid,ulong systemAddress,ulong bodyAddress,long marketId,
        string name,string systemName,string bodyName,string stationName,uint icon,uint bodyType,
        bool cluster,bool rings,bool favourite,int sourceKind);
    internal sealed record GalaxyAccountBookmarks(ulong tick,ulong entity,GalaxyAccountBookmark[] items)
    {
        // Neither dirty +1D0 nor default-1 +1D8 proves response completion.
        // Positive rows are useful; absence must remain unknown.
        internal bool complete=>false;
        internal string source=>"original-bookmark-manager";
    }

    static bool GalaxyAccountString(nuint reference,out string value)
    {
        value="";if(reference==0)return true;
        var length=Read<ulong>(reference);var capacity=Read<ulong>(reference+8);
        if(length>512||capacity<length||capacity>1024*1024||Read<int>(reference+16)<=0)return false;
        var bytes=Platform.ReadBytes(reference+20,(int)length);
        if(Read<byte>(reference+20+(nuint)length)!=0||bytes.AsSpan().Contains((byte)0))return false;
        value=galaxyAccountUtf8.GetString(bytes);
        return Read<ulong>(reference)==length&&Read<ulong>(reference+8)==capacity;
    }

    static bool CopyGalaxyBookmarkManager(nuint manager,nuint owner,nuint expectedImage,out GalaxyAccountBookmark[] items)
    {
        items=[];
        try
        {
            if(manager==0||owner==0||Pointer(manager)!=expectedImage+0x529B850||
               Pointer(manager+8)!=expectedImage+0x529B880||Pointer(manager+0x58)!=expectedImage+0x529B980||
               Pointer(manager+0x10)!=owner)return false;
            var count=Read<ulong>(manager+0x190);var rows=Pointer(manager+0x198);
            if(count>GalaxyBookmarkMaximum||(count!=0&&rows==0))return false;
            var result=new GalaxyAccountBookmark[(int)count];
            HashSet<(ulong,int)> identities=[];
            for(nuint i=0;i<(nuint)count;i++)
            {
                var at=rows+i*0x60;var row=Platform.ReadBytes(at,0x60);
                ulong U64(int offset)=>BinaryPrimitives.ReadUInt64LittleEndian(row.AsSpan(offset));
                uint U32(int offset)=>BinaryPrimitives.ReadUInt32LittleEndian(row.AsSpan(offset));
                var uid=U64(0);var system=U64(0x18);var sourceKind=unchecked((int)U32(0x5C));
                if(uid==ulong.MaxValue||system==0||system>=0x80000000000000UL||
                   sourceKind is not (1 or 2)||!identities.Add((uid,sourceKind))||
                   row[0x48]>1||row[0x58]>1||row[0x59]>1)return false;
                if(!GalaxyAccountString((nuint)U64(0x10),out var name)||
                   !GalaxyAccountString((nuint)U64(0x20),out var systemName)||
                   !GalaxyAccountString((nuint)U64(0x30),out var bodyName)||
                   !GalaxyAccountString((nuint)U64(0x40),out var stationName))return false;
                // Reject a changed row; even a valid old pointer must not become a
                // bookmark for an unrelated system while the copy is in flight.
                if(!Platform.ReadBytes(at,0x60).AsSpan().SequenceEqual(row))return false;
                result[(int)i]=new(uid,system,U64(0x28),unchecked((long)U64(0x38)),name,systemName,bodyName,
                    stationName,U32(0x4C),U32(0x54),row[0x48]!=0,row[0x58]!=0,row[0x59]!=0,sourceKind);
            }
            if(Read<ulong>(manager+0x190)!=count||Pointer(manager+0x198)!=rows||Pointer(manager+0x10)!=owner||
               Pointer(manager)!=expectedImage+0x529B850)return false;
            items=result;return true;
        }
        catch{return false;}
    }

    // Call only on the existing original owning-thread observation path. The
    // caller may copy the returned managed snapshot to IPC under its own lock.
    static bool TryReadGalaxyAccountBookmarks(Snapshot frame,out GalaxyAccountBookmarks 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;
            // 26007D0 (interface vtable[0]) returns type ID 5F05CC8.
            var component=RegisteredComponent(registry,Read<uint>(image+0x5F05CC8));
            if(component<0x10058||Pointer(component)!=image+0x529B980)return false;
            var manager=component-0x58;var owner=Pointer(manager+0x10);if(owner==0)return false;
            if(!CopyGalaxyBookmarkManager(manager,owner,image,out var items))return false;
            result=new(frame.tick,frame.entity,items);return true;
        }
        catch{return false;}
    }
}