mirror of
https://github.com/ppy/osu
synced 2025-01-09 15:49:32 +00:00
Cache mod settings rather than fetching everytime
This commit is contained in:
parent
eb6c6830bc
commit
1ae4a1910a
@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Extensions.TypeExtensions;
|
||||
@ -129,6 +130,17 @@ namespace osu.Game.Rulesets.Mods
|
||||
[JsonIgnore]
|
||||
public virtual Type[] IncompatibleMods => Array.Empty<Type>();
|
||||
|
||||
private IReadOnlyList<IBindable> settingsBacking;
|
||||
|
||||
/// <summary>
|
||||
/// A list of the all <see cref="IBindable"/> settings within this mod.
|
||||
/// </summary>
|
||||
internal IReadOnlyList<IBindable> Settings =>
|
||||
settingsBacking ??= this.GetSettingsSourceProperties()
|
||||
.Select(p => p.Item2.GetValue(this))
|
||||
.Cast<IBindable>()
|
||||
.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
||||
/// </summary>
|
||||
@ -191,10 +203,7 @@ namespace osu.Game.Rulesets.Mods
|
||||
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))));
|
||||
Settings.SequenceEqual(other.Settings, ModSettingsEqualityComparer.Default);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
@ -203,8 +212,8 @@ namespace osu.Game.Rulesets.Mods
|
||||
|
||||
hashCode.Add(GetType());
|
||||
|
||||
foreach (var (_, prop) in this.GetSettingsSourceProperties())
|
||||
hashCode.Add(ModUtils.GetSettingUnderlyingValue(prop.GetValue(this)));
|
||||
foreach (var setting in Settings)
|
||||
hashCode.Add(ModUtils.GetSettingUnderlyingValue(setting));
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
@ -213,5 +222,20 @@ namespace osu.Game.Rulesets.Mods
|
||||
/// Reset all custom settings for this mod back to their defaults.
|
||||
/// </summary>
|
||||
public virtual void ResetSettingsToDefaults() => CopyFrom((Mod)Activator.CreateInstance(GetType()));
|
||||
|
||||
private class ModSettingsEqualityComparer : IEqualityComparer<IBindable>
|
||||
{
|
||||
public static ModSettingsEqualityComparer Default { get; } = new ModSettingsEqualityComparer();
|
||||
|
||||
public bool Equals(IBindable x, IBindable y)
|
||||
{
|
||||
object xValue = x == null ? null : ModUtils.GetSettingUnderlyingValue(x);
|
||||
object yValue = y == null ? null : ModUtils.GetSettingUnderlyingValue(y);
|
||||
|
||||
return EqualityComparer<object>.Default.Equals(xValue, yValue);
|
||||
}
|
||||
|
||||
public int GetHashCode(IBindable obj) => ModUtils.GetSettingUnderlyingValue(obj).GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user