using System.Text.Json.Nodes;
namespace XFEWarframe.Client.Runtime;
/// Validate an entire diagnostic command before any engine action is queued.
public static class GameTestProtocol
{
public static JsonObject Parse(string operation, ReadOnlySpan 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(out var index) || index is < 0 or > 3 ||
data["pressed"] is not JsonValue pressed || !pressed.TryGetValue(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(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(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(out _)) Text(value, 2048);
else if (!value.TryGetValue(out _) && (!value.TryGetValue(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(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(out var reference) || reference < 0 || reference >= index)
throw new ArgumentException("Invalid script step reference.");
}
}