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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Plugin;

internal static unsafe partial class Navigation
{
    private static int CommerceKeyCapacity(bool formal,bool batch)=>formal||batch?4096:32;
    private static bool QuotedWalletMatches(JsonElement response,ulong actual)
        =>!response.TryGetProperty("credits",out var value)||value.ValueKind==JsonValueKind.Number&&value.TryGetUInt64(out var quoted)&&quoted==actual;
    private static bool QuotedWalletFixture(string body,ulong actual,bool expected)
    {using var value=JsonDocument.Parse(body);return QuotedWalletMatches(value.RootElement,actual)==expected;}
    private static bool FormalCommodityName(string name) => name.Length is >0 and <128
        && name.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-');
    private static string CommerceExpectedCommodity(in CommerceReadRequest work)
    {
        if (!work.formal) return CommerceCommodityName(work.commodity);
        fixed (byte* bytes = work.commodityName)
        {
            var span = new ReadOnlySpan<byte>(bytes,128); var end = span.IndexOf((byte)0);
            return end > 0 ? Encoding.UTF8.GetString(span[..end]) : "";
        }
    }
    private static void SetCommerceCommodity(ref CommerceReadRequest work,string name)
    {
        if(!FormalCommodityName(name))throw new InvalidDataException("Original commodity identity is invalid");
        fixed(byte* bytes=work.commodityName){var span=new Span<byte>(bytes,128);span.Clear();Encoding.UTF8.GetBytes(name.ToLowerInvariant(),span);}
    }
    private static bool FormalCommerceFixture()
    {
        if(CommerceKeyCapacity(true,false)!=4096||CommerceKeyCapacity(false,true)!=4096||CommerceKeyCapacity(false,false)!=32)return false;
        if(!QuotedWalletFixture("{\"inventory\":[]}",100000000,true)
            ||!QuotedWalletFixture("{\"credits\":100000000}",100000000,true)
            ||!QuotedWalletFixture("{\"credits\":99999999}",100000000,false)
            ||!QuotedWalletFixture("{\"credits\":null}",100000000,false)
            ||!QuotedWalletFixture("{\"credits\":\"100000000\"}",100000000,false))return false;
        var work=new CommerceReadRequest{formal=true,capacityBound=752,kind=1,commodity=123456,delta=752,before=0,after=752,totalBefore=0,creditsBefore=100000000,price=7241};
        SetCommerceCommodity(ref work,"gold");
        if(!CommerceExpectedCredits(work,out var expected)||expected!=94554768||CommerceExpectedCommodity(work)!="gold")return false;
        if(!CommerceReceiptFixture(work,"{\"credits\":94554768,\"inventory\":[{\"commodity\":\"gold\",\"qty\":752}]}"))return false;
        if(CommerceReceiptFixture(work,"{\"credits\":94554768,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":752}]}"))return false;
        work.delta=753;if(CommerceExpectedCredits(work,out _))return false;
        work.delta=-752;work.before=752;work.after=0;work.totalBefore=752;
        if(!CommerceExpectedCredits(work,out expected)||expected!=105445232)return false;
        work.price=uint.MaxValue;work.creditsBefore=1;if(CommerceExpectedCredits(work with { delta=1 },out _))return false;
        return !FormalCommodityName("")&&!FormalCommodityName("../invalid")&&!FormalCommodityName(new string('x',128));
    }
}