-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29992 from smoogipoo/fix-ios-realm-crashes
Fix reflection-related iOS crashes
- Loading branch information
Showing
9 changed files
with
118 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. 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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. 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(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IBindable<>)); | ||
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().SingleOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Bindable<>)); | ||
if (bindableWithValueType == null) | ||
return; | ||
|
||
set_method.MakeGenericMethod(bindableWithValueType.GenericTypeArguments[0]).Invoke(null, [bindable, value]); | ||
} | ||
|
||
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; | ||
} | ||
} |