mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 10:02:59 +08:00
Move GetSettingUnderlyingValue
to a SettingSource
extension method
This commit is contained in:
parent
6d5692fcec
commit
1814a325d8
@ -6,6 +6,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@ -170,6 +171,39 @@ namespace osu.Game.Configuration
|
|||||||
|
|
||||||
private static readonly ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]> property_info_cache = new ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]>();
|
private static readonly ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]> property_info_cache = new ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the underlying value of the given mod setting object.
|
||||||
|
/// Can be used for serialization and equality comparison purposes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="setting">A <see cref="SettingSourceAttribute"/> bindable.</param>
|
||||||
|
public static object GetUnderlyingSettingValue(this object setting)
|
||||||
|
{
|
||||||
|
switch (setting)
|
||||||
|
{
|
||||||
|
case Bindable<double> d:
|
||||||
|
return d.Value;
|
||||||
|
|
||||||
|
case Bindable<int> i:
|
||||||
|
return i.Value;
|
||||||
|
|
||||||
|
case Bindable<float> f:
|
||||||
|
return f.Value;
|
||||||
|
|
||||||
|
case Bindable<bool> b:
|
||||||
|
return b.Value;
|
||||||
|
|
||||||
|
case IBindable u:
|
||||||
|
// An unknown (e.g. enum) generic type.
|
||||||
|
var valueMethod = u.GetType().GetProperty(nameof(IBindable<int>.Value));
|
||||||
|
Debug.Assert(valueMethod != null);
|
||||||
|
return valueMethod.GetValue(u);
|
||||||
|
|
||||||
|
default:
|
||||||
|
// fall back for non-bindable cases.
|
||||||
|
return setting;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
|
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
|
||||||
{
|
{
|
||||||
var type = obj.GetType();
|
var type = obj.GetType();
|
||||||
|
@ -12,7 +12,6 @@ using osu.Framework.Logging;
|
|||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
using osu.Game.Utils;
|
|
||||||
|
|
||||||
namespace osu.Game.Online.API
|
namespace osu.Game.Online.API
|
||||||
{
|
{
|
||||||
@ -43,7 +42,7 @@ namespace osu.Game.Online.API
|
|||||||
var bindable = (IBindable)property.GetValue(mod);
|
var bindable = (IBindable)property.GetValue(mod);
|
||||||
|
|
||||||
if (!bindable.IsDefault)
|
if (!bindable.IsDefault)
|
||||||
Settings.Add(property.Name.Underscore(), ModUtils.GetSettingUnderlyingValue(bindable));
|
Settings.Add(property.Name.Underscore(), bindable.GetUnderlyingSettingValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,13 +92,13 @@ namespace osu.Game.Online.API
|
|||||||
|
|
||||||
public bool Equals(KeyValuePair<string, object> x, KeyValuePair<string, object> y)
|
public bool Equals(KeyValuePair<string, object> x, KeyValuePair<string, object> y)
|
||||||
{
|
{
|
||||||
object xValue = ModUtils.GetSettingUnderlyingValue(x.Value);
|
object xValue = x.Value.GetUnderlyingSettingValue();
|
||||||
object yValue = ModUtils.GetSettingUnderlyingValue(y.Value);
|
object yValue = y.Value.GetUnderlyingSettingValue();
|
||||||
|
|
||||||
return x.Key == y.Key && EqualityComparer<object>.Default.Equals(xValue, yValue);
|
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));
|
public int GetHashCode(KeyValuePair<string, object> obj) => HashCode.Combine(obj.Key, obj.Value.GetUnderlyingSettingValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using MessagePack;
|
using MessagePack;
|
||||||
using MessagePack.Formatters;
|
using MessagePack.Formatters;
|
||||||
using osu.Game.Utils;
|
using osu.Game.Configuration;
|
||||||
|
|
||||||
namespace osu.Game.Online.API
|
namespace osu.Game.Online.API
|
||||||
{
|
{
|
||||||
@ -23,7 +23,7 @@ namespace osu.Game.Online.API
|
|||||||
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
|
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
|
||||||
writer.WriteString(in stringBytes);
|
writer.WriteString(in stringBytes);
|
||||||
|
|
||||||
primitiveFormatter.Serialize(ref writer, ModUtils.GetSettingUnderlyingValue(kvp.Value), options);
|
primitiveFormatter.Serialize(ref writer, kvp.Value.GetUnderlyingSettingValue(), options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
hashCode.Add(GetType());
|
hashCode.Add(GetType());
|
||||||
|
|
||||||
foreach (var setting in Settings)
|
foreach (var setting in Settings)
|
||||||
hashCode.Add(ModUtils.GetSettingUnderlyingValue(setting));
|
hashCode.Add(setting.GetUnderlyingSettingValue());
|
||||||
|
|
||||||
return hashCode.ToHashCode();
|
return hashCode.ToHashCode();
|
||||||
}
|
}
|
||||||
@ -208,13 +208,13 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
|
|
||||||
public bool Equals(IBindable x, IBindable y)
|
public bool Equals(IBindable x, IBindable y)
|
||||||
{
|
{
|
||||||
object xValue = x == null ? null : ModUtils.GetSettingUnderlyingValue(x);
|
object xValue = x?.GetUnderlyingSettingValue();
|
||||||
object yValue = y == null ? null : ModUtils.GetSettingUnderlyingValue(y);
|
object yValue = y?.GetUnderlyingSettingValue();
|
||||||
|
|
||||||
return EqualityComparer<object>.Default.Equals(xValue, yValue);
|
return EqualityComparer<object>.Default.Equals(xValue, yValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetHashCode(IBindable obj) => ModUtils.GetSettingUnderlyingValue(obj).GetHashCode();
|
public int GetHashCode(IBindable obj) => obj.GetUnderlyingSettingValue().GetHashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ using osu.Framework.Graphics.Containers;
|
|||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Extensions;
|
using osu.Game.Extensions;
|
||||||
using osu.Game.Skinning;
|
using osu.Game.Skinning;
|
||||||
using osu.Game.Utils;
|
|
||||||
using osuTK;
|
using osuTK;
|
||||||
|
|
||||||
namespace osu.Game.Screens.Play.HUD
|
namespace osu.Game.Screens.Play.HUD
|
||||||
@ -69,7 +68,7 @@ namespace osu.Game.Screens.Play.HUD
|
|||||||
var bindable = (IBindable)property.GetValue(component);
|
var bindable = (IBindable)property.GetValue(component);
|
||||||
|
|
||||||
if (!bindable.IsDefault)
|
if (!bindable.IsDefault)
|
||||||
Settings.Add(property.Name.Underscore(), ModUtils.GetSettingUnderlyingValue(bindable));
|
Settings.Add(property.Name.Underscore(), bindable.GetUnderlyingSettingValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (component is Container<Drawable> container)
|
if (component is Container<Drawable> container)
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using osu.Framework.Bindables;
|
|
||||||
using osu.Game.Online.API;
|
using osu.Game.Online.API;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
|
|
||||||
#nullable enable
|
|
||||||
|
|
||||||
namespace osu.Game.Utils
|
namespace osu.Game.Utils
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -154,39 +152,6 @@ namespace osu.Game.Utils
|
|||||||
yield return mod;
|
yield return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the underlying value of the given mod setting object.
|
|
||||||
/// Used in <see cref="APIMod"/> for serialization and equality comparison purposes.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="setting">The mod setting.</param>
|
|
||||||
public static object GetSettingUnderlyingValue(object setting)
|
|
||||||
{
|
|
||||||
switch (setting)
|
|
||||||
{
|
|
||||||
case Bindable<double> d:
|
|
||||||
return d.Value;
|
|
||||||
|
|
||||||
case Bindable<int> i:
|
|
||||||
return i.Value;
|
|
||||||
|
|
||||||
case Bindable<float> f:
|
|
||||||
return f.Value;
|
|
||||||
|
|
||||||
case Bindable<bool> b:
|
|
||||||
return b.Value;
|
|
||||||
|
|
||||||
case IBindable u:
|
|
||||||
// A mod with unknown (e.g. enum) generic type.
|
|
||||||
var valueMethod = u.GetType().GetProperty(nameof(IBindable<int>.Value));
|
|
||||||
Debug.Assert(valueMethod != null);
|
|
||||||
return valueMethod.GetValue(u);
|
|
||||||
|
|
||||||
default:
|
|
||||||
// fall back for non-bindable cases.
|
|
||||||
return setting;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Verifies all proposed mods are valid for a given ruleset and returns instantiated <see cref="Mod"/>s for further processing.
|
/// Verifies all proposed mods are valid for a given ruleset and returns instantiated <see cref="Mod"/>s for further processing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user