mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 19:42:55 +08: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.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Extensions.TypeExtensions;
|
using osu.Framework.Extensions.TypeExtensions;
|
||||||
@ -129,6 +130,17 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public virtual Type[] IncompatibleMods => Array.Empty<Type>();
|
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>
|
/// <summary>
|
||||||
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -191,10 +203,7 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
if (ReferenceEquals(this, other)) return true;
|
if (ReferenceEquals(this, other)) return true;
|
||||||
|
|
||||||
return GetType() == other.GetType() &&
|
return GetType() == other.GetType() &&
|
||||||
this.GetSettingsSourceProperties().All(pair =>
|
Settings.SequenceEqual(other.Settings, ModSettingsEqualityComparer.Default);
|
||||||
EqualityComparer<object>.Default.Equals(
|
|
||||||
ModUtils.GetSettingUnderlyingValue(pair.Item2.GetValue(this)),
|
|
||||||
ModUtils.GetSettingUnderlyingValue(pair.Item2.GetValue(other))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
@ -203,8 +212,8 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
|
|
||||||
hashCode.Add(GetType());
|
hashCode.Add(GetType());
|
||||||
|
|
||||||
foreach (var (_, prop) in this.GetSettingsSourceProperties())
|
foreach (var setting in Settings)
|
||||||
hashCode.Add(ModUtils.GetSettingUnderlyingValue(prop.GetValue(this)));
|
hashCode.Add(ModUtils.GetSettingUnderlyingValue(setting));
|
||||||
|
|
||||||
return hashCode.ToHashCode();
|
return hashCode.ToHashCode();
|
||||||
}
|
}
|
||||||
@ -213,5 +222,20 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
/// Reset all custom settings for this mod back to their defaults.
|
/// Reset all custom settings for this mod back to their defaults.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void ResetSettingsToDefaults() => CopyFrom((Mod)Activator.CreateInstance(GetType()));
|
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