]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix RoleLoadout equality (#28737)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Sat, 15 Jun 2024 06:52:49 +0000 (16:52 +1000)
committerGitHub <noreply@github.com>
Sat, 15 Jun 2024 06:52:49 +0000 (16:52 +1000)
* Fix RoleLoadout equality

Knew it was janky but thought SequenceEqual was better than it is so we just do it manually.

* Also implement this

Content.Shared/Preferences/Loadouts/Loadout.cs
Content.Shared/Preferences/Loadouts/RoleLoadout.cs

index dbe440f58b8e70184fd562d8c8dfe4fef80fe6b3..c1d20cd3de422225565c3e56612fa161d63b1f8a 100644 (file)
@@ -7,8 +7,25 @@ namespace Content.Shared.Preferences.Loadouts;
 /// Specifies the selected prototype and custom data for a loadout.
 /// </summary>
 [Serializable, NetSerializable, DataDefinition]
-public sealed partial class Loadout
+public sealed partial class Loadout : IEquatable<Loadout>
 {
     [DataField]
     public ProtoId<LoadoutPrototype> Prototype;
+
+    public bool Equals(Loadout? other)
+    {
+        if (ReferenceEquals(null, other)) return false;
+        if (ReferenceEquals(this, other)) return true;
+        return Prototype.Equals(other.Prototype);
+    }
+
+    public override bool Equals(object? obj)
+    {
+        return ReferenceEquals(this, obj) || obj is Loadout other && Equals(other);
+    }
+
+    public override int GetHashCode()
+    {
+        return Prototype.GetHashCode();
+    }
 }
index 5f8e24621a979ecd6061f7f199028e4e100a8d5f..d2fc8df5591060f0e3ac927309e55ef456c93264 100644 (file)
@@ -295,7 +295,25 @@ public sealed partial class RoleLoadout : IEquatable<RoleLoadout>
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
-        return Role.Equals(other.Role) && SelectedLoadouts.SequenceEqual(other.SelectedLoadouts) && Points == other.Points;
+
+        if (!Role.Equals(other.Role) ||
+            SelectedLoadouts.Count != other.SelectedLoadouts.Count ||
+            Points != other.Points)
+        {
+            return false;
+        }
+
+        // Tried using SequenceEqual but it stinky so.
+        foreach (var (key, value) in SelectedLoadouts)
+        {
+            if (!other.SelectedLoadouts.TryGetValue(key, out var otherValue) ||
+                !otherValue.SequenceEqual(value))
+            {
+                return false;
+            }
+        }
+
+        return true;
     }
 
     public override bool Equals(object? obj)