Harden WORLD_INPUT parsing to accept client field variants and nested input payloads

This commit is contained in:
dingo
2026-02-15 09:25:00 -05:00
parent 69e457d943
commit 117e3fd37a
+108 -11
View File
@@ -568,23 +568,120 @@ public sealed class WorldHandler : IClientMessageHandler
{
if (payload.IsEmpty) return null;
var direct = Parse<InputReq>(payload);
if (direct != null) return direct;
var legacy = Parse<LegacyInputReq>(payload);
if (legacy != null)
try
{
using var doc = JsonDocument.Parse(Encoding.UTF8.GetString(payload.Span));
var root = doc.RootElement;
if (root.ValueKind != JsonValueKind.Object) return null;
// Some clients wrap movement inside an "input" object.
var source = root;
if (TryGetProperty(root, "input", out var nested) && nested.ValueKind == JsonValueKind.Object)
{
source = nested;
}
var hasMoveX = TryGetFloat(source, out var moveX, "moveX", "dx", "inputX", "x", "horizontal");
var hasMoveY = TryGetFloat(source, out var moveY, "moveY", "dy", "inputY", "y", "vertical");
if (!hasMoveX && !hasMoveY)
{
return null;
}
TryGetUInt(source, out var seq, "seq", "clientSeq", "inputSeq");
TryGetBool(source, out var isRunning, "isRunning", "running", "sprint");
TryGetString(source, out var facing, "facing", "dir", "direction");
return new InputReq
{
Seq = legacy.ClientSeq,
MoveX = legacy.Dx,
MoveY = legacy.Dy,
IsRunning = legacy.IsRunning,
Facing = legacy.Facing
Seq = seq,
MoveX = moveX,
MoveY = moveY,
IsRunning = isRunning,
Facing = facing
};
}
catch
{
return null;
}
}
return null;
private static bool TryGetProperty(JsonElement source, string name, out JsonElement value)
{
foreach (var p in source.EnumerateObject())
{
if (string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase))
{
value = p.Value;
return true;
}
}
value = default;
return false;
}
private static bool TryGetFloat(JsonElement source, out float value, params string[] names)
{
foreach (var n in names)
{
if (!TryGetProperty(source, n, out var e)) continue;
if (e.ValueKind == JsonValueKind.Number && e.TryGetSingle(out value)) return true;
if (e.ValueKind == JsonValueKind.String && float.TryParse(e.GetString(), out value)) return true;
}
value = 0f;
return false;
}
private static bool TryGetUInt(JsonElement source, out uint value, params string[] names)
{
foreach (var n in names)
{
if (!TryGetProperty(source, n, out var e)) continue;
if (e.ValueKind == JsonValueKind.Number && e.TryGetUInt32(out value)) return true;
if (e.ValueKind == JsonValueKind.String && uint.TryParse(e.GetString(), out value)) return true;
}
value = 0;
return false;
}
private static bool TryGetBool(JsonElement source, out bool value, params string[] names)
{
foreach (var n in names)
{
if (!TryGetProperty(source, n, out var e)) continue;
if (e.ValueKind == JsonValueKind.True || e.ValueKind == JsonValueKind.False)
{
value = e.GetBoolean();
return true;
}
if (e.ValueKind == JsonValueKind.Number && e.TryGetInt32(out var num))
{
value = num != 0;
return true;
}
if (e.ValueKind == JsonValueKind.String && bool.TryParse(e.GetString(), out value))
{
return true;
}
}
value = false;
return false;
}
private static bool TryGetString(JsonElement source, out string? value, params string[] names)
{
foreach (var n in names)
{
if (!TryGetProperty(source, n, out var e)) continue;
if (e.ValueKind == JsonValueKind.String)
{
value = e.GetString();
return true;
}
}
value = null;
return false;
}
private async Task BroadcastAsync(int opCode, object payload, int regionId, Guid? exceptConnectionId = null)