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 GalaxyCurrentTradeFlowsWireSelfTest()
    {
        static JsonElement Parse(string json){using var document=JsonDocument.Parse(json);return document.RootElement.Clone();}
        lock(galaxyAccountMutex)lock(snapshotLock)
        {
            var savedFrame=snapshot;var savedInGame=inGame;var savedFlows=galaxyCurrentTradeFlows;var savedVersion=galaxyCurrentTradeFlowsVersion;
            try
            {
                uint passed=0;var now=GetTickCount64();const ulong clock=18446744073709550000;
                inGame=true;snapshot=new(){valid=true,entity=42,tick=now};galaxyCurrentTradeFlowsVersion=7;
                var rows=Enumerable.Range(0,18).Select(i=>new GalaxyCurrentTradeFlow(9007199254740993UL+(ulong)i,9007199254741993,
                    (ushort)(62000+i),i==17?null:"黄金"+i,i==0?0:.25f,i==0?0:.5f,clock-10)).ToArray();
                galaxyCurrentTradeFlows=new(now,42,clock,86400,3600,uint.MaxValue,rows,1);
                JsonElement Read(string request)=>Parse(GalaxyCurrentTradeFlowsDescription(Parse(request)).ToJsonString());
                var first=Read("{}");var second=Read("{\"generation\":7,\"offset\":16}");
                if(first.GetProperty("available").GetBoolean()&&!first.GetProperty("complete").GetBoolean()&&first.GetProperty("total").GetInt32()==18&&
                   first.GetProperty("offset").GetInt32()==0&&first.GetProperty("items").GetArrayLength()==16&&first.GetProperty("nextOffset").GetUInt64()==16&&
                   second.GetProperty("items").GetArrayLength()==2&&second.GetProperty("nextOffset").ValueKind==JsonValueKind.Null)passed|=1;
                var row=first.GetProperty("items")[0];
                if(row.GetProperty("fromSystemAddress").GetString()=="9007199254740993"&&row.GetProperty("toSystemAddress").GetString()=="9007199254741993"&&
                   row.GetProperty("timestamp").GetString()==(clock-10).ToString()&&row.GetProperty("npcAmount").GetSingle()==0&&row.GetProperty("playerAmount").GetSingle()==0&&
                   second.GetProperty("items")[1].GetProperty("commodityName").ValueKind==JsonValueKind.Null&&row.GetProperty("commodityToken").GetUInt16()==62000)passed|=2;
                if(first.GetProperty("originalClock").GetString()==clock.ToString()&&first.GetProperty("lifetimeSeconds").GetUInt64()==86400&&
                   first.GetProperty("refreshSeconds").GetUInt64()==3600&&first.GetProperty("revision").GetUInt32()==uint.MaxValue&&
                   first.GetProperty("unresolvedCount").GetInt32()==1&&first.GetProperty("entityId").GetString()=="42"&&
                   first.GetProperty("source").GetString()=="original-current-trade-flow-cache")passed|=4;
                var rejected=false;try{Read("{\"generation\":8}");}catch(InvalidOperationException){rejected=true;}
                bool Unknown(){var value=Read("{}");return !value.GetProperty("available").GetBoolean()&&value.GetProperty("total").GetInt32()==0&&
                    value.GetProperty("originalClock").ValueKind==JsonValueKind.Null&&value.GetProperty("revision").ValueKind==JsonValueKind.Null;}
                snapshot.entity=43;var guarded=Unknown();snapshot.entity=42;galaxyCurrentTradeFlows=galaxyCurrentTradeFlows with{tick=now-6001};guarded&=Unknown();
                galaxyCurrentTradeFlows=galaxyCurrentTradeFlows with{tick=now+1000};guarded&=Unknown();
                galaxyCurrentTradeFlows=galaxyCurrentTradeFlows with{tick=now};inGame=false;guarded&=Unknown();inGame=true;
                if(rejected&&guarded)passed|=8;
                var invalid=0;
                foreach(var request in new[]{"{\"offset\":-1}","{\"offset\":65537}","{\"offset\":1}","{\"offset\":\"0\"}","{\"limit\":0}",
                    "{\"limit\":17}","{\"limit\":null}","{\"generation\":0}","{\"generation\":null}","{\"generation\":\"7\"}","{\"offset\":19,\"generation\":7}"})
                {try{Read(request);}catch(ArgumentException){invalid++;}}
                if(invalid==11&&galaxyCurrentTradeFlowsVersion==7)passed|=16;
                galaxyCurrentTradeFlows=null;var absent=Unknown();
                galaxyCurrentTradeFlows=new(now,42,clock,86400,3600,0,[],0);var empty=Read("{}");
                if(absent&&empty.GetProperty("available").GetBoolean()&&empty.GetProperty("total").GetInt32()==0&&empty.GetProperty("nextOffset").ValueKind==JsonValueKind.Null)passed|=32;
                // Deliberately exceed source-name bounds to exercise transport
                // budgeting independently from the bounded original reader.
                galaxyCurrentTradeFlows=new(now,42,clock,86400,3600,0,Enumerable.Range(0,40).Select(i=>rows[0] with{commodityName=new string('中',3000)}).ToArray(),0);
                var bounded=Read("{}");var number=bounded.GetProperty("items").GetArrayLength();
                var bytes=System.Text.Encoding.UTF8.GetByteCount(bounded.GetRawText());
                var oversizedRejected=false;galaxyCurrentTradeFlows=galaxyCurrentTradeFlows with{items=[rows[0] with{commodityName=new string('中',20000)}]};
                try{Read("{}");}catch(InvalidOperationException){oversizedRejected=true;}
                if(number is >0 and <16&&bounded.GetProperty("nextOffset").GetInt32()==number&&bytes<65535&&oversizedRejected)passed|=64;
                var many=Enumerable.Range(0,80).Select(i=>rows[0] with{fromSystemAddress=9007199254740993UL+(ulong)i}).ToArray();
                galaxyCurrentTradeFlows=new(now,42,clock,86400,3600,0,many,0);var seen=new List<string>();var offset=0;
                do
                {
                    var page=Read("{\"generation\":7,\"offset\":"+offset+"}");
                    seen.AddRange(page.GetProperty("items").EnumerateArray().Select(item=>item.GetProperty("fromSystemAddress").GetString()!));
                    var next=page.GetProperty("nextOffset");if(next.ValueKind==JsonValueKind.Null)break;offset=next.GetInt32();
                    galaxyCurrentTradeFlows=galaxyCurrentTradeFlows with{originalClock=galaxyCurrentTradeFlows.originalClock+1};
                }while(offset<80);
                if(seen.SequenceEqual(many.Select(item=>item.fromSystemAddress.ToString()))&&galaxyCurrentTradeFlowsVersion==7)passed|=128;
                return passed;
            }
            finally{snapshot=savedFrame;inGame=savedInGame;galaxyCurrentTradeFlows=savedFlows;galaxyCurrentTradeFlowsVersion=savedVersion;}
        }
    }
#if PLUGIN_NAVIGATION
    [UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyCurrentTradeFlowsWireSelfTest")]
    internal static uint ExportGalaxyCurrentTradeFlowsWireSelfTest()=>RunTest(GalaxyCurrentTradeFlowsWireSelfTest);
#endif
}