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

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

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

namespace XFEWarframe.Client.Runtime;

/// <summary>Validate an entire diagnostic command before any engine action is queued.</summary>
public static class GameTestProtocol
{
    public static JsonObject Parse(string operation, ReadOnlySpan<byte> body)
    {
        if (operation is not ("GET status" or "GET ability-state" or "POST login" or "POST lua" or "POST ability-input"))
            throw new ArgumentException("Unknown game test operation.");
        var data = body.IsEmpty ? new JsonObject() : JsonNode.Parse(body) as JsonObject ?? throw new ArgumentException("JSON object required.");
        if (operation is "GET status" or "GET ability-state")
        {
            if (data.Count != 0) throw new ArgumentException("Status does not accept parameters.");
        }
        else if (operation == "POST login")
        {
            var email = Text(data["email"], 254);
            var hash = Text(data["passwordHash"], 128);
            if (data.Count != 2 || email.Length < 3 || !email.Contains('@') || email.Any(char.IsControl) ||
                hash.Length != 128 || hash.Any(c => !Uri.IsHexDigit(c))) throw new ArgumentException("Invalid login parameters.");
        }
        else if (operation == "POST ability-input")
        {
            if (data.Any(p => p.Key is not ("slot" or "pressed" or "exclusive")) || data["slot"] is not JsonValue slot || !slot.TryGetValue<int>(out var index) || index is < 0 or > 3 ||
                data["pressed"] is not JsonValue pressed || !pressed.TryGetValue<bool>(out _))
                throw new ArgumentException("Use a skill slot from 0 to 3 and a pressed boolean.");
            if (data.ContainsKey("exclusive") && (data["exclusive"] is not JsonValue exclusive || !exclusive.TryGetValue<bool>(out _)))
                throw new ArgumentException("Exclusive input must be boolean.");
        }
        else
        {
            if (data.Count != 1 || data["steps"] is not JsonArray steps || steps.Count is < 1 or > 24)
                throw new ArgumentException("Use 1 to 24 script steps.");
            for (var index = 0; index < steps.Count; index++)
            {
                if (steps[index] is not JsonObject step) throw new ArgumentException("Script step object required.");
                var operations = new[] { "global", "field", "meta", "call" }.Where(step.ContainsKey).ToArray();
                if (operations.Length != 1) throw new ArgumentException("One operation is required per step.");
                var allowed = operations[0] switch
                {
                    "global" => new[] { "global", "inspect" },
                    "field" => new[] { "field", "from", "literal", "inspect" },
                    "meta" => new[] { "meta", "inspect" },
                    _ => new[] { "call", "args", "inspect" }
                };
                if (step.Any(p => !allowed.Contains(p.Key))) throw new ArgumentException("Unknown script parameter.");
                foreach (var flag in new[] { "inspect", "literal" })
                    if (step.ContainsKey(flag) && (step[flag] is not JsonValue v || !v.TryGetValue<bool>(out _)))
                        throw new ArgumentException("Boolean script parameter required.");
                if (step.ContainsKey("global") || step.ContainsKey("field"))
                {
                    var name = Text(step[operations[0]], 128);
                    if (name.Length == 0 || name.Any(char.IsControl) ||
                        name.StartsWith("0x", StringComparison.OrdinalIgnoreCase) && (name.Length != 10 || name[2..].Any(c => !Uri.IsHexDigit(c))))
                        throw new ArgumentException("Invalid script name.");
                }
                if (step.ContainsKey("field")) Reference(step["from"], index);
                if (step.ContainsKey("meta")) Reference(step["meta"], index);
                if (step.ContainsKey("call"))
                {
                    Reference(step["call"], index);
                    if (step.ContainsKey("args") && step["args"] is not JsonArray) throw new ArgumentException("Script arguments must be an array.");
                    var args = step["args"] as JsonArray ?? [];
                    if (args.Count > 16) throw new ArgumentException("Too many script arguments.");
                    foreach (var argument in args)
                    {
                        if (argument is null) continue;
                        if (argument is JsonObject reference && reference.Count == 1 && reference.ContainsKey("ref")) Reference(reference["ref"], index);
                        else if (argument is JsonValue value)
                        {
                            if (value.TryGetValue<string>(out _)) Text(value, 2048);
                            else if (!value.TryGetValue<bool>(out _) && (!value.TryGetValue<float>(out var number) || !float.IsFinite(number)))
                                throw new ArgumentException("Invalid script scalar.");
                        }
                        else throw new ArgumentException("Invalid script argument.");
                    }
                }
            }
        }
        return data;
    }

    private static string Text(JsonNode? node, int maximum) => node is JsonValue value && value.TryGetValue<string>(out var text) &&
        text.Length <= maximum && !text.Contains('\0') ? text : throw new ArgumentException("Invalid script string.");

    private static void Reference(JsonNode? node, int index)
    {
        if (node is not JsonValue value || !value.TryGetValue<int>(out var reference) || reference < 0 || reference >= index)
            throw new ArgumentException("Invalid script step reference.");
    }
}