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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    static uint GalaxyPowerActivitySelfTest()
    {
        var memory=Platform.Allocate(0x10000);if(memory==0)return 0;
        try
        {
            uint passed=0;var service=memory+0x100;var owner=memory+0x5000;var expectedImage=unchecked((nuint)0x140000000UL);
            const ulong address=9007199254740995;
            Write(service,expectedImage+0x51BB600);Write(service-0x58,expectedImage+0x51BB4D0);
            Write(service-0x50,expectedImage+0x51BB500);Write(service-0x48,owner);
            Write(service+0x308,(byte)1);Write(service+0x309,(byte)1);Write(service+0x30A,(byte)1);
            var metadataMap=service+0x3C0;var counterMap=service+0x3218;var pairMap=service+0x3240;
            var metadataStorage=memory+0x6000;var counterStorage=memory+0x7000;var pairStorage=memory+0x7800;
            void Header(nuint map,nuint storage,ulong count)
            {Write(map+8,count);Write(map+0x10,8UL);Write(map+0x18,storage);}
            Header(metadataMap,metadataStorage,1);Header(counterMap,counterStorage,1);Header(pairMap,pairStorage,2);
            // Fixed vectors independently evaluated from the original getter
            // instructions, so a broken production hash cannot build its own
            // matching fixture table.
            const ulong metadataSlot=5;
            Write(metadataStorage,1UL<<(int)metadataSlot);
            var node=metadataStorage+16+(nuint)metadataSlot*0x180;var metadata=node+0x10;
            Write(node,address);Write(metadata,1UL);Write(metadata+0xC,7);Write(metadata+0x124,.875f);
            const ulong counterSlot=5;Write(counterStorage,1UL<<(int)counterSlot);
            Write(counterStorage+8+(nuint)counterSlot*0x10,address);Write(counterStorage+16+(nuint)counterSlot*0x10,.375f);
            void Pair(uint mode,float value)
            {
                var slot=mode==0?5u:7u;var occupied=Read<ulong>(pairStorage);
                while((occupied&(1UL<<(int)slot))!=0)slot=(slot+1)&7;
                Write(pairStorage,occupied|(1UL<<(int)slot));var at=pairStorage+8+(nuint)slot*0x18;
                Write(at,address);Write(at+8,mode);Write(at+0x10,123);Write(at+0x14,value);
            }
            Pair(0,.25f);Pair(1,.625f);
            bool ReadCounter(bool all,int index,int action,float expected)
                =>CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,all,index,3,action,out var value)&&value==expected;
            var before=Platform.ReadBytes(memory,0x10000);
            if(GalaxyFactHash(address)==0x357C1BED&&GalaxyPowerActivityPairHash(address,0)==0x357C1BED&&
               GalaxyPowerActivityPairHash(address,1)==0x4ECBB4CF&&ReadCounter(false,1,1,.25f)&&ReadCounter(false,0,1,.625f))passed|=1;
            if(ReadCounter(false,1,4,.625f)&&ReadCounter(false,0,4,.25f)&&
               ReadCounter(false,1,2,.25f)&&ReadCounter(false,1,3,.25f))passed|=2;
            if(ReadCounter(true,-1,1,.625f)&&ReadCounter(true,-1,2,.25f)&&
               ReadCounter(true,-1,3,.625f)&&ReadCounter(true,-1,4,.625f))passed|=4;
            if(ReadCounter(true,-1,5,.875f)&&ReadCounter(false,0,5,.875f)&&ReadCounter(false,2,5,.875f)&&
               Platform.ReadBytes(memory,0x10000).AsSpan().SequenceEqual(before))passed|=8;
            Write(metadata+0xC,0);
            if(Enumerable.Range(1,4).All(action=>ReadCounter(true,-1,action,.375f)&&ReadCounter(false,2,action,.375f)))passed|=16;
            Write(counterMap+8,0UL);Write(pairMap+8,0UL);
            var emptyZero=ReadCounter(true,-1,1,0);Write(metadata+0xC,7);emptyZero&=ReadCounter(false,1,1,0);
            Write(metadataMap+8,0UL);
            if(emptyZero&&CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,1,out var unknown)&&unknown is null)passed|=32;
            Write(metadataMap+8,1UL);
            var rejected=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,0,true,-1,3,1,out _)&&
                !CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,false,3,3,1,out _)&&
                !CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,6,out _);
            Write(metadata+0x124,float.NaN);rejected&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,5,out _);
            Write(metadata+0x124,-.1f);rejected&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,5,out _);
            Write(metadata+0x124,2.5f);rejected&=ReadCounter(true,-1,5,2.5f);
            Write(metadata,3UL);rejected&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,1,out _);
            Write(metadata,1UL);if(rejected)passed|=64;
            Write(metadataMap+0x10,3UL);var guarded=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,1,out _);
            Write(metadataMap+0x10,8UL);Write(service+0x30C,(byte)1);guarded&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,1,out _);
            Write(service+0x30C,(byte)0);Write(service+0x30A,(byte)0);guarded&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner,address,true,-1,3,1,out _);
            Write(service+0x30A,(byte)1);guarded&=!CopyGalaxyPowerActivityCounter(service,expectedImage,owner+8,address,true,-1,3,1,out _);
            if(guarded)passed|=128;
            return passed;
        }
        finally{Platform.Free(memory);}
    }

    static uint GalaxyPowerActivityWireSelfTest()
    {
        static JsonElement Parse(string text){using var document=JsonDocument.Parse(text);return document.RootElement.Clone();}
        lock(galaxyFactsMutex)
        {
            var savedWork=galaxyFactsWork;var savedGeneration=galaxyFactsGeneration;
            try
            {
                uint passed=0;galaxyFactsWork=new();galaxyFactsGeneration=0;
                JsonElement Wire(string op,string json)=>Parse(GalaxyFilterFactsRequest(op,Parse(json)).ToJsonString());
                var initial=Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],\"includePowerActivity\":true,\"powerScope\":\"selected\",\"powerInternalId\":17,\"powerActivityId\":4}");
                if(initial.GetProperty("items").GetArrayLength()==0&&galaxyFactsWork.includePowerActivity&&!galaxyFactsWork.powerAllPowers&&
                   galaxyFactsWork.powerInternalId==17&&galaxyFactsWork.powerActivityId==4)passed|=1;
                galaxyFactsWork.items.Add((300,default));galaxyFactsWork.powerCounters[300]=.375f;
                var activity=Wire("galaxy.facts.result","{\"generation\":1}").GetProperty("items")[0].GetProperty("powerActivity");
                if(activity.GetProperty("scope").GetString()=="selected"&&activity.GetProperty("internalPowerId").GetInt32()==17&&
                   activity.GetProperty("actionId").GetInt32()==4&&activity.GetProperty("recentActivity").GetSingle()==.375f&&!activity.TryGetProperty("eligible",out _))passed|=2;
                galaxyFactsWork.powerCounters.Clear();
                activity=Wire("galaxy.facts.result","{\"generation\":1}").GetProperty("items")[0].GetProperty("powerActivity");
                if(activity.GetProperty("scope").GetString()=="selected"&&activity.GetProperty("internalPowerId").GetInt32()==17&&activity.GetProperty("recentActivity").ValueKind==JsonValueKind.Null)passed|=4;
                Wire("galaxy.facts.cancel","{\"generation\":1}");
                Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerInternalId\":null,\"powerActivityId\":5}");
                galaxyFactsWork.items.Add((300,default));galaxyFactsWork.powerCounters[300]=0;
                activity=Wire("galaxy.facts.result","{\"generation\":2}").GetProperty("items")[0].GetProperty("powerActivity");
                if(galaxyFactsWork.powerAllPowers&&galaxyFactsWork.powerInternalId==-1&&activity.GetProperty("scope").GetString()=="all"&&
                   activity.GetProperty("internalPowerId").ValueKind==JsonValueKind.Null&&activity.GetProperty("actionId").GetInt32()==5&&activity.GetProperty("recentActivity").GetSingle()==0)passed|=8;
                Wire("galaxy.facts.cancel","{\"generation\":2}");
                var bad=new[]{
                    "\"includePowerActivity\":1", "\"includePowerActivity\":true", "\"includePowerActivity\":true,\"powerScope\":\"other\",\"powerActivityId\":1",
                    "\"includePowerActivity\":true,\"powerScope\":\"selected\",\"powerActivityId\":1", "\"includePowerActivity\":true,\"powerScope\":\"selected\",\"powerInternalId\":-1,\"powerActivityId\":1",
                    "\"includePowerActivity\":true,\"powerScope\":\"selected\",\"powerInternalId\":1.5,\"powerActivityId\":1", "\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerInternalId\":0,\"powerActivityId\":1",
                    "\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerActivityId\":0", "\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerActivityId\":6",
                    "\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerActivityId\":1.5", "\"includePowerActivity\":false,\"powerScope\":\"all\"", "\"powerInternalId\":17"};
                var rejected=0;foreach(var fields in bad)try{Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],"+fields+"}");}catch(InvalidDataException){rejected++;}
                if(rejected==bad.Length&&galaxyFactsGeneration==2)passed|=16;
                Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],\"includePowerActivity\":false,\"powerScope\":null,\"powerInternalId\":null,\"powerActivityId\":null}");
                galaxyFactsWork.items.Add((300,default));
                if(!galaxyFactsWork.includePowerActivity&&Wire("galaxy.facts.result","{\"generation\":3}").GetProperty("items")[0].GetProperty("powerActivity").ValueKind==JsonValueKind.Null)passed|=32;
                var old=galaxyFactsWork;Wire("galaxy.facts.cancel","{\"generation\":3}");
                Wire("galaxy.facts","{\"systemAddresses\":[\"301\"],\"includePowerActivity\":true,\"powerScope\":\"all\",\"powerActivityId\":1}");
                var mismatch=false;try{Wire("galaxy.facts.result","{\"generation\":3}");}catch(InvalidOperationException){mismatch=true;}
                if(old.cancelled&&old.done&&mismatch&&galaxyFactsWork.powerCounters.Count==0&&galaxyFactsWork.powerActivityId==1)passed|=64;
                var frame=new Snapshot{valid=true,entity=9,referenceWorld=300};var work=galaxyFactsWork;
                var bound=BindGalaxyFilterFactsWork(work,frame,1);frame.entity=10;
                if(bound&&!BindGalaxyFilterFactsWork(work,frame,2)&&work.done&&work.error.Contains("changed")&&work.contextSystem==300)passed|=128;
                return passed;
            }
            finally{galaxyFactsWork=savedWork;galaxyFactsGeneration=savedGeneration;}
        }
    }
#if PLUGIN_NAVIGATION
    [UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyPowerActivitySelfTest")]
    internal static uint ExportGalaxyPowerActivitySelfTest()=>RunTest(GalaxyPowerActivitySelfTest);
    [UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyPowerActivityWireSelfTest")]
    internal static uint ExportGalaxyPowerActivityWireSelfTest()=>RunTest(GalaxyPowerActivityWireSelfTest);
#endif
}