Fix RoleLoadout equality (#28737)

* Fix RoleLoadout equality

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

* Also implement this
This commit is contained in:
metalgearsloth
2024-06-15 16:52:49 +10:00
committed by GitHub
parent a3d9f7291d
commit f0fbedd640
2 changed files with 37 additions and 2 deletions

View File

@@ -7,8 +7,25 @@ namespace Content.Shared.Preferences.Loadouts;
/// Specifies the selected prototype and custom data for a loadout. /// Specifies the selected prototype and custom data for a loadout.
/// </summary> /// </summary>
[Serializable, NetSerializable, DataDefinition] [Serializable, NetSerializable, DataDefinition]
public sealed partial class Loadout public sealed partial class Loadout : IEquatable<Loadout>
{ {
[DataField] [DataField]
public ProtoId<LoadoutPrototype> Prototype; 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();
}
} }

View File

@@ -295,7 +295,25 @@ public sealed partial class RoleLoadout : IEquatable<RoleLoadout>
{ {
if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true; 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) public override bool Equals(object? obj)