1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:37:28 +08:00

Addressed changes

This commit is contained in:
Terochi 2023-03-09 20:14:58 +01:00
parent 8bf84869a5
commit b51c41a804
2 changed files with 109 additions and 52 deletions

View File

@ -14,7 +14,7 @@ namespace osu.Game.Tests.Mods
public class ModSettingsTest public class ModSettingsTest
{ {
[Test] [Test]
public void TestModSettingsUnboundWhenCopied() public void TestModSettingsUnboundWhenCloned()
{ {
var original = new OsuModDoubleTime(); var original = new OsuModDoubleTime();
var copy = (OsuModDoubleTime)original.DeepClone(); var copy = (OsuModDoubleTime)original.DeepClone();
@ -26,7 +26,7 @@ namespace osu.Game.Tests.Mods
} }
[Test] [Test]
public void TestMultiModSettingsUnboundWhenCopied() public void TestMultiModSettingsUnboundWhenCloned()
{ {
var original = new MultiMod(new OsuModDoubleTime()); var original = new MultiMod(new OsuModDoubleTime());
var copy = (MultiMod)original.DeepClone(); var copy = (MultiMod)original.DeepClone();
@ -54,6 +54,42 @@ namespace osu.Game.Tests.Mods
Assert.That(modBool.TestSetting.Value, Is.EqualTo(!modBool.TestSetting.Default)); Assert.That(modBool.TestSetting.Value, Is.EqualTo(!modBool.TestSetting.Default));
} }
[Test]
public void TestDefaultValueKeptWhenCopied()
{
var modBoolTrue = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = true, Value = false } };
var modBoolFalse = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = false, Value = true } };
modBoolFalse.CopySharedSettings(modBoolTrue);
Assert.That(modBoolFalse.TestSetting.Default, Is.EqualTo(false));
Assert.That(modBoolFalse.TestSetting.Value, Is.EqualTo(modBoolTrue.TestSetting.Value));
}
[Test]
public void TestValueResetsToDefaultWhenCopied()
{
var modDouble = new TestNonMatchingSettingTypeModDouble();
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { Value = 1 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(modInt.TestSetting.Default));
}
[Test]
public void TestRelativelyScaleWithClampedRangeWhenCopied()
{
const double setting_change = 50.4;
var modDouble100 = new TestNonMatchingSettingTypeModDouble { TestSetting = { MaxValue = 100, MinValue = 0, Value = setting_change } };
var modDouble200 = new TestNonMatchingSettingTypeModDouble { TestSetting = { MaxValue = 200, MinValue = 0 } };
modDouble200.CopySharedSettings(modDouble100);
Assert.That(modDouble200.TestSetting.Value, Is.EqualTo(setting_change * 2));
}
[Test] [Test]
public void TestCopyDoubleToIntWithDefaultRange() public void TestCopyDoubleToIntWithDefaultRange()
{ {
@ -64,7 +100,32 @@ namespace osu.Game.Tests.Mods
modInt.CopySharedSettings(modDouble); modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo((int)setting_change)); Assert.That(modInt.TestSetting.Value, Is.EqualTo(Convert.ToInt32(setting_change)));
}
[Test]
public void TestCopyDoubleToIntWithOutOfBoundsRange()
{
const double setting_change = 50.4;
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { MinValue = int.MinValue - 1d, Value = setting_change } };
// make RangeConstrainedBindable.HasDefinedRange return true
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { MinValue = int.MinValue + 1 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(Convert.ToInt32(setting_change)));
}
[Test]
public void TestCopyDoubleToIntWithOutOfBoundsValue()
{
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { MinValue = int.MinValue + 1, Value = int.MaxValue + 1d } };
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { MinValue = int.MinValue + 1 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(int.MaxValue));
} }
[Test] [Test]
@ -80,31 +141,6 @@ namespace osu.Game.Tests.Mods
Assert.That(modDouble.TestSetting.Value, Is.EqualTo(setting_change)); Assert.That(modDouble.TestSetting.Value, Is.EqualTo(setting_change));
} }
[Test]
public void TestCopyDoubleToIntWithClampedRange()
{
const double setting_change = 50.4;
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { MaxValue = 100, MinValue = 0, Value = setting_change } };
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { MaxValue = 200, MinValue = 0 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(Convert.ToInt32(setting_change * 2)));
}
[Test]
public void TestDefaultValueKeptWhenCopied()
{
var modBoolTrue = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = true, Value = false } };
var modBoolFalse = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = false, Value = true } };
modBoolFalse.CopySharedSettings(modBoolTrue);
Assert.That(modBoolFalse.TestSetting.Default, Is.EqualTo(false));
Assert.That(modBoolFalse.TestSetting.Value, Is.EqualTo(modBoolTrue.TestSetting.Value));
}
private class TestNonMatchingSettingTypeModDouble : TestNonMatchingSettingTypeMod private class TestNonMatchingSettingTypeModDouble : TestNonMatchingSettingTypeMod
{ {
public override string Acronym => "NMD"; public override string Acronym => "NMD";

View File

@ -169,28 +169,31 @@ namespace osu.Game.Rulesets.Mods
const string max_value = nameof(BindableNumber<int>.MaxValue); const string max_value = nameof(BindableNumber<int>.MaxValue);
const string value = nameof(Bindable<int>.Value); const string value = nameof(Bindable<int>.Value);
Dictionary<string, object> oldSettings = new Dictionary<string, object>(); Dictionary<string, object> sourceSettings = new Dictionary<string, object>();
foreach (var (_, property) in source.GetSettingsSourceProperties()) foreach (var (_, sourceProperty) in source.GetSettingsSourceProperties())
{ {
oldSettings.Add(property.Name.ToSnakeCase(), property.GetValue(source)!); sourceSettings.Add(sourceProperty.Name.ToSnakeCase(), sourceProperty.GetValue(source)!);
} }
foreach (var (_, property) in this.GetSettingsSourceProperties()) foreach (var (_, targetProperty) in this.GetSettingsSourceProperties())
{ {
object targetSetting = property.GetValue(this)!; object targetSetting = targetProperty.GetValue(this)!;
if (!oldSettings.TryGetValue(property.Name.ToSnakeCase(), out object? sourceSetting)) if (!sourceSettings.TryGetValue(targetProperty.Name.ToSnakeCase(), out object? sourceSetting))
continue; continue;
if (((IBindable)sourceSetting).IsDefault) if (((IBindable)sourceSetting).IsDefault)
// keep at default value if the source is default {
// reset to default value if the source is default
targetSetting.GetType().GetMethod(nameof(Bindable<int>.SetDefault))!.Invoke(targetSetting, null);
continue; continue;
}
Type? targetType = getGenericBaseType(targetSetting, typeof(BindableNumber<>)); Type? targetBindableNumberType = getGenericBaseType(targetSetting, typeof(BindableNumber<>));
Type? sourceType = getGenericBaseType(sourceSetting, typeof(BindableNumber<>)); Type? sourceBindableNumberType = getGenericBaseType(sourceSetting, typeof(BindableNumber<>));
if (targetType == null || sourceType == null) if (targetBindableNumberType == null || sourceBindableNumberType == null)
{ {
if (getGenericBaseType(targetSetting, typeof(Bindable<>))!.GenericTypeArguments.Single() == if (getGenericBaseType(targetSetting, typeof(Bindable<>))!.GenericTypeArguments.Single() ==
getGenericBaseType(sourceSetting, typeof(Bindable<>))!.GenericTypeArguments.Single()) getGenericBaseType(sourceSetting, typeof(Bindable<>))!.GenericTypeArguments.Single())
@ -202,8 +205,16 @@ namespace osu.Game.Rulesets.Mods
continue; continue;
} }
Type targetGenericType = targetType.GenericTypeArguments.Single(); bool rangeOutOfBounds = false;
Type sourceGenericType = sourceType.GenericTypeArguments.Single();
Type targetGenericType = targetBindableNumberType.GenericTypeArguments.Single();
Type sourceGenericType = sourceBindableNumberType.GenericTypeArguments.Single();
if (!Convert.ToBoolean(getValue(targetSetting, nameof(RangeConstrainedBindable<int>.HasDefinedRange))) ||
!Convert.ToBoolean(getValue(sourceSetting, nameof(RangeConstrainedBindable<int>.HasDefinedRange))))
// check if we have a range to rescale from and a range to rescale to
// if not, copy the raw value
rangeOutOfBounds = true;
double allowedMin = Math.Max( double allowedMin = Math.Max(
Convert.ToDouble(targetGenericType.GetField("MinValue")!.GetValue(null)), Convert.ToDouble(targetGenericType.GetField("MinValue")!.GetValue(null)),
@ -219,25 +230,35 @@ namespace osu.Game.Rulesets.Mods
double targetMax = getValueDouble(targetSetting, max_value); double targetMax = getValueDouble(targetSetting, max_value);
double sourceMin = getValueDouble(sourceSetting, min_value); double sourceMin = getValueDouble(sourceSetting, min_value);
double sourceMax = getValueDouble(sourceSetting, max_value); double sourceMax = getValueDouble(sourceSetting, max_value);
double sourceValue = getValueDouble(sourceSetting, value); double sourceValue = Math.Clamp(getValueDouble(sourceSetting, value), allowedMin, allowedMax);
// convert value to same ratio double targetValue = rangeOutOfBounds
double targetValue = (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin; // keep raw value
? sourceValue
// convert value to same ratio
: (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin;
setValue(targetSetting, value, Convert.ChangeType(targetValue, targetType.GenericTypeArguments.Single())); setValue(targetSetting, value, Convert.ChangeType(targetValue, targetBindableNumberType.GenericTypeArguments.Single()));
double getValueDouble(object target, string name) => double getValueDouble(object setting, string name)
Math.Clamp(Convert.ToDouble(getValue(target, name)!), allowedMin, allowedMax); {
double settingValue = Convert.ToDouble(getValue(setting, name)!);
if (settingValue < allowedMin || settingValue > allowedMax)
rangeOutOfBounds = true;
return settingValue;
}
} }
object? getValue(object target, string name) => object? getValue(object setting, string name) =>
target.GetType().GetProperty(name)!.GetValue(target); setting.GetType().GetProperty(name)!.GetValue(setting);
void setValue(object target, string name, object? newValue) => void setValue(object setting, string name, object? newValue) =>
target.GetType().GetProperty(name)!.SetValue(target, newValue); setting.GetType().GetProperty(name)!.SetValue(setting, newValue);
Type? getGenericBaseType(object target, Type genericType) => Type? getGenericBaseType(object setting, Type genericType) =>
target.GetType().GetTypeInheritance().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericType); setting.GetType().GetTypeInheritance().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericType);
} }
/// <summary> /// <summary>