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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.InteropServices;

namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    static uint GalaxyServiceFactsSelfTest()
    {
        var memory=Platform.Allocate(0x10000);if(memory==0)return 0;
        try
        {
            var table=memory+0x1000;var expectedImage=table-0x5242530;var source=memory+0x4000;
            var map=source+0x118;var storage=memory+0x6000;var rows=storage+8;const ulong capacity=16;
            const ulong address=9007199254740995UL;var slot=GalaxyFactHash(address)&15;var row=rows+(nuint)slot*16;
            Write(source,table);Write(source-0x58,expectedImage+0x52423E8);Write(source-0x50,expectedImage+0x5242430);
            Write(table,expectedImage+0x1E110C0);Write(table+8,expectedImage+0x201130);Write(table+0x50,expectedImage+0x1E220B0);
            Write(map+8,0UL);Write(map+0x10,0UL);Write(map+0x18,(nuint)0);uint passed=0;
            if(CopyGalaxyServiceFacts(source,expectedImage,address,out var fact)&&fact is null)passed|=1;
            Write(map+8,1UL);Write(map+0x10,capacity);Write(map+0x18,storage);Write(storage,1UL<<(int)slot);Write(row,address);
            // All ten options are exercised through their real six-byte fields.
            var all=true;
            for(var index=0;index<10;index++)
            {
                for(nuint i=0;i<6;i++)Write(row+8+i,(byte)0);
                if(index<4)Write(row+10,(byte)(index+1));else if(index<6)Write(row+11,(byte)(index-3));
                else Write(row+8+(nuint)(index switch{6=>1,7=>0,8=>4,_=>5}),(byte)1);
                all&=CopyGalaxyServiceFacts(source,expectedImage,address,out fact)&&fact is{} actual&&actual.mask==(1<<index)&&!actual.defaulted;
            }
            if(all)passed|=2;
            Write(row+8,(byte)0x80);Write(row+9,(byte)2);Write(row+10,(byte)4);Write(row+11,(byte)2);Write(row+12,(byte)3);Write(row+13,(byte)255);
            if(CopyGalaxyServiceFacts(source,expectedImage,address,out fact)&&fact is{mask:1000,defaulted:false})passed|=4;
            // Getter misses produce a zero summary only after an initialized,
            // structurally valid table; no write or request is performed.
            Write(map+8,0UL);Write(storage,0UL);var before=Platform.ReadBytes(memory,0x10000);
            if(CopyGalaxyServiceFacts(source,expectedImage,address,out fact)&&fact is{mask:0,defaulted:true}&&Platform.ReadBytes(memory,0x10000).AsSpan().SequenceEqual(before))passed|=8;
            Write(map+8,1UL);Write(storage,1UL<<(int)slot);Write(row,address);
            Write(row+10,(byte)255);Write(row+11,(byte)255);Write(row+8,(byte)0);Write(row+9,(byte)0);Write(row+12,(byte)0);Write(row+13,(byte)0);
            if(CopyGalaxyServiceFacts(source,expectedImage,address,out fact)&&fact is{mask:0,defaulted:false})passed|=16;
            Write(map+0x10,15UL);var invalid=!CopyGalaxyServiceFacts(source,expectedImage,address,out _);Write(map+0x10,capacity);
            Write(map+8,capacity);invalid&=!CopyGalaxyServiceFacts(source,expectedImage,address,out _);Write(map+8,1UL);
            Write(map+0x18,(nuint)1);invalid&=!CopyGalaxyServiceFacts(source,expectedImage,address,out _);Write(map+0x18,storage);
            if(invalid)passed|=32;
            Write(source,table+8);var binding=!CopyGalaxyServiceFacts(source,expectedImage,address,out _);Write(source,table);
            Write(table+0x50,expectedImage+0x1E220B8);binding&=!CopyGalaxyServiceFacts(source,expectedImage,address,out _);Write(table+0x50,expectedImage+0x1E220B0);
            if(binding&&!CopyGalaxyServiceFacts(source,expectedImage,0,out _)&&!CopyGalaxyServiceFacts(source,expectedImage,1UL<<55,out _))passed|=64;
            // Linear probing collision: preserve the full64-bit key and do not
            // return a neighbouring system's summary.
            var following=(slot+1)&15;Write(row,address-1);Write(storage,(1UL<<(int)slot)|(1UL<<(int)following));Write(map+8,2UL);
            var second=rows+(nuint)following*16;Write(second,address);Write(second+8,(byte)1);
            for(nuint i=1;i<6;i++)Write(second+8+i,(byte)0);
            if(CopyGalaxyServiceFacts(source,expectedImage,address,out fact)&&fact is{mask:128,defaulted:false})passed|=128;
            return passed;
        }
        finally{Platform.Free(memory);}
    }
#if PLUGIN_NAVIGATION
    [UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyServiceFactsSelfTest")]
    internal static uint ExportGalaxyServiceFactsSelfTest()=>RunTest(GalaxyServiceFactsSelfTest);
#endif
}