Harden WORLD_INPUT parsing to accept client field variants and nested input payloads
This commit is contained in:
+108
-11
@@ -568,23 +568,120 @@ public sealed class WorldHandler : IClientMessageHandler
|
|||||||
{
|
{
|
||||||
if (payload.IsEmpty) return null;
|
if (payload.IsEmpty) return null;
|
||||||
|
|
||||||
var direct = Parse<InputReq>(payload);
|
try
|
||||||
if (direct != null) return direct;
|
|
||||||
|
|
||||||
var legacy = Parse<LegacyInputReq>(payload);
|
|
||||||
if (legacy != null)
|
|
||||||
{
|
{
|
||||||
|
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
|
return new InputReq
|
||||||
{
|
{
|
||||||
Seq = legacy.ClientSeq,
|
Seq = seq,
|
||||||
MoveX = legacy.Dx,
|
MoveX = moveX,
|
||||||
MoveY = legacy.Dy,
|
MoveY = moveY,
|
||||||
IsRunning = legacy.IsRunning,
|
IsRunning = isRunning,
|
||||||
Facing = legacy.Facing
|
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)
|
private async Task BroadcastAsync(int opCode, object payload, int regionId, Guid? exceptConnectionId = null)
|
||||||
|
|||||||
Reference in New Issue
Block a user