using System.Numerics; namespace EvilEngine.Formats.Humanoids; public sealed record HumanoidBetween(string From, string To); public sealed record HumanoidTwist(string Follows, float Share); public sealed record HumanoidBinding(string Layout, IReadOnlyDictionary Bones) { public int? Root { get; init; } public IReadOnlyDictionary Between { get; init; } = new SortedDictionary(); public IReadOnlyDictionary Twist { get; init; } = new SortedDictionary(); public IReadOnlyDictionary? Reference { get; init; } public bool Equals(HumanoidBinding? other) { return other is not null && Layout == other.Layout && Root == other.Root && Same(Bones, other.Bones) && Same(Between, other.Between) && Same(Twist, other.Twist) && (Reference is null ? other.Reference is null : other.Reference is not null && Same(Reference, other.Reference)); } public bool Has(string bone) { return Bones.ContainsKey(bone); } public override int GetHashCode() { return HashCode.Combine(Layout, Root, Bones.Count, Between.Count, Twist.Count); } public string? Problem(int nodes) { if (!IsId(Layout)) return $"the layout '{Layout}' isn't a namespace:object id"; if (Bones.Count == 0) return "it maps no bones"; var used = new Dictionary(); foreach (var (bone, node) in Bones) { if (!HumanoidProfile.Has(bone)) return $"'{bone}' isn't a bone of the humanoid profile"; if (bone == HumanoidProfile.Root) return "Root is given by root, not in bones"; if (node < 0 || node >= nodes) return $"{bone} names node {node}, and there are {nodes}"; if (!used.TryAdd(node, bone)) return $"{bone} and {used[node]} are both node {node}"; } if (Root is { } root) { if (root < 0 || root >= nodes) return $"root names node {root}, and there are {nodes}"; if (!used.TryAdd(root, HumanoidProfile.Root)) return $"root and {used[root]} are both node {root}"; } foreach (var (node, between) in Between) { if (node < 0 || node >= nodes) return $"between names node {node}, and there are {nodes}"; if (!used.TryAdd(node, "between")) return $"between names node {node}, which {used[node]} already is"; if (!Bones.ContainsKey(between.From) || !Bones.ContainsKey(between.To)) return $"node {node} is between {between.From} and {between.To}, which must both be mapped"; } foreach (var (node, twist) in Twist) { if (node < 0 || node >= nodes) return $"twist names node {node}, and there are {nodes}"; if (!used.TryAdd(node, "twist")) return $"twist names node {node}, which {used[node]} already is"; if (!Bones.ContainsKey(twist.Follows)) return $"node {node} twists with {twist.Follows}, which isn't mapped"; if (!(twist.Share > 0 && twist.Share <= 1)) return $"node {node}'s twist share must be above 0 and at most 1"; } if (Reference is not null) foreach (var (bone, rotation) in Reference) { if (!Bones.ContainsKey(bone)) return $"the reference turns {bone}, which isn't mapped"; if (MathF.Abs(rotation.Length() - 1) > 1e-3f) return $"the reference rotation of {bone} isn't a unit quaternion"; } return null; } private static bool IsId(string text) { try { return ContentId.Parse(text).ToString() == text; } catch (Exception error) when (error is FormatException or ArgumentException) { return false; } } private static bool Same(IReadOnlyDictionary a, IReadOnlyDictionary b) { return a.Count == b.Count && a.All(pair => b.TryGetValue(pair.Key, out var value) && EqualityComparer.Default.Equals(pair.Value, value)); } }