Take current mod settings value into account on equality comparsion

This commit is contained in:
Salman Ahmed 2021-04-12 20:10:22 +03:00
parent fbd5195738
commit 92fab653e1
2 changed files with 38 additions and 2 deletions

View File

@ -11,6 +11,7 @@ using osu.Framework.Bindables;
using osu.Game.Configuration;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Utils;
namespace osu.Game.Online.API
{
@ -64,7 +65,15 @@ namespace osu.Game.Online.API
}
public bool Equals(IMod other) => other is APIMod them && Equals(them);
public bool Equals(APIMod other) => Acronym == other?.Acronym;
public bool Equals(APIMod other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Acronym == other.Acronym &&
Settings.SequenceEqual(other.Settings, ModSettingsEqualityComparer.Default);
}
public override string ToString()
{
@ -73,5 +82,20 @@ namespace osu.Game.Online.API
return $"{Acronym}";
}
private class ModSettingsEqualityComparer : IEqualityComparer<KeyValuePair<string, object>>
{
public static ModSettingsEqualityComparer Default { get; } = new ModSettingsEqualityComparer();
public bool Equals(KeyValuePair<string, object> x, KeyValuePair<string, object> y)
{
object xValue = ModUtils.GetSettingUnderlyingValue(x.Value);
object yValue = ModUtils.GetSettingUnderlyingValue(y.Value);
return x.Key == y.Key && EqualityComparer<object>.Default.Equals(xValue, yValue);
}
public int GetHashCode(KeyValuePair<string, object> obj) => HashCode.Combine(obj.Key, ModUtils.GetSettingUnderlyingValue(obj.Value));
}
}
}

View File

@ -12,6 +12,7 @@ using osu.Framework.Testing;
using osu.Game.Configuration;
using osu.Game.IO.Serialization;
using osu.Game.Rulesets.UI;
using osu.Game.Utils;
namespace osu.Game.Rulesets.Mods
{
@ -173,7 +174,18 @@ namespace osu.Game.Rulesets.Mods
}
public bool Equals(IMod other) => other is Mod them && Equals(them);
public bool Equals(Mod other) => GetType() == other?.GetType();
public bool Equals(Mod other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return GetType() == other.GetType() &&
this.GetSettingsSourceProperties().All(pair =>
EqualityComparer<object>.Default.Equals(
ModUtils.GetSettingUnderlyingValue(pair.Item2.GetValue(this)),
ModUtils.GetSettingUnderlyingValue(pair.Item2.GetValue(other))));
}
/// <summary>
/// Reset all custom settings for this mod back to their defaults.