mirror of
https://github.com/ppy/osu.git
synced 2024-11-15 12:27:26 +08:00
Fix similar Bindable-related crashes
This commit is contained in:
parent
3ab04d98f6
commit
fd4891cf31
52
osu.Game.Tests/Utils/BindableValueAccessorTest.cs
Normal file
52
osu.Game.Tests/Utils/BindableValueAccessorTest.cs
Normal file
@ -0,0 +1,52 @@
|
||||
// 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.
|
||||
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Tests.Utils
|
||||
{
|
||||
[TestFixture]
|
||||
public class BindableValueAccessorTest
|
||||
{
|
||||
[Test]
|
||||
public void GetValue()
|
||||
{
|
||||
const int value = 1337;
|
||||
|
||||
BindableInt bindable = new BindableInt(value);
|
||||
Assert.That(BindableValueAccessor.GetValue(bindable), Is.EqualTo(value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetValue()
|
||||
{
|
||||
const int value = 1337;
|
||||
|
||||
BindableInt bindable = new BindableInt();
|
||||
BindableValueAccessor.SetValue(bindable, value);
|
||||
|
||||
Assert.That(bindable.Value, Is.EqualTo(value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetInvalidBindable()
|
||||
{
|
||||
BindableList<object> list = new BindableList<object>();
|
||||
Assert.That(BindableValueAccessor.GetValue(list), Is.EqualTo(list));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetInvalidBindable()
|
||||
{
|
||||
const int value = 1337;
|
||||
|
||||
BindableList<int> list = new BindableList<int> { value };
|
||||
BindableValueAccessor.SetValue(list, 2);
|
||||
|
||||
Assert.That(list, Has.Exactly(1).Items);
|
||||
Assert.That(list[0], Is.EqualTo(value));
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using JetBrains.Annotations;
|
||||
@ -15,6 +14,7 @@ using osu.Framework.Graphics;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Overlays.Settings;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Configuration
|
||||
{
|
||||
@ -228,10 +228,7 @@ namespace osu.Game.Configuration
|
||||
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)!;
|
||||
return BindableValueAccessor.GetValue(u);
|
||||
|
||||
default:
|
||||
// fall back for non-bindable cases.
|
||||
|
@ -266,8 +266,7 @@ namespace osu.Game.Rulesets.Mods
|
||||
|
||||
// TODO: special case for handling number types
|
||||
|
||||
PropertyInfo property = targetSetting.GetType().GetProperty(nameof(Bindable<bool>.Value))!;
|
||||
property.SetValue(targetSetting, property.GetValue(sourceSetting));
|
||||
BindableValueAccessor.SetValue(targetSetting, BindableValueAccessor.GetValue(sourceSetting));
|
||||
}
|
||||
}
|
||||
|
||||
|
44
osu.Game/Utils/BindableValueAccessor.cs
Normal file
44
osu.Game/Utils/BindableValueAccessor.cs
Normal file
@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Extensions.TypeExtensions;
|
||||
|
||||
namespace osu.Game.Utils
|
||||
{
|
||||
internal static class BindableValueAccessor
|
||||
{
|
||||
private static readonly MethodInfo get_method = typeof(BindableValueAccessor).GetMethod(nameof(getValue), BindingFlags.Static | BindingFlags.NonPublic)!;
|
||||
private static readonly MethodInfo set_method = typeof(BindableValueAccessor).GetMethod(nameof(setValue), BindingFlags.Static | BindingFlags.NonPublic)!;
|
||||
|
||||
public static object GetValue(IBindable bindable)
|
||||
{
|
||||
Type? bindableWithValueType = bindable.GetType().GetInterfaces().FirstOrDefault(isBindableT);
|
||||
if (bindableWithValueType == null)
|
||||
return bindable;
|
||||
|
||||
return get_method.MakeGenericMethod(bindableWithValueType.GenericTypeArguments[0]).Invoke(null, [bindable])!;
|
||||
}
|
||||
|
||||
public static void SetValue(IBindable bindable, object value)
|
||||
{
|
||||
Type? bindableWithValueType = bindable.GetType().EnumerateBaseTypes().FirstOrDefault(isBindableT);
|
||||
if (bindableWithValueType == null)
|
||||
return;
|
||||
|
||||
set_method.MakeGenericMethod(bindableWithValueType.GenericTypeArguments[0]).Invoke(null, [bindable, value]);
|
||||
}
|
||||
|
||||
private static bool isBindableT(Type type)
|
||||
=> type.IsGenericType
|
||||
&& (type.GetGenericTypeDefinition() == typeof(Bindable<>)
|
||||
|| type.GetGenericTypeDefinition() == typeof(IBindable<>));
|
||||
|
||||
private static object getValue<T>(object bindable) => ((IBindable<T>)bindable).Value!;
|
||||
|
||||
private static object setValue<T>(object bindable, object value) => ((Bindable<T>)bindable).Value = (T)value;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user