mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 12:57:36 +08:00
Implemented a more complex setting conversion logic + tests
This commit is contained in:
parent
e321536acc
commit
09e7c21b23
@ -2,6 +2,9 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Localisation;
|
||||||
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
using osu.Game.Rulesets.Osu.Mods;
|
using osu.Game.Rulesets.Osu.Mods;
|
||||||
|
|
||||||
@ -32,5 +35,44 @@ namespace osu.Game.Tests.Mods
|
|||||||
Assert.That(((OsuModDoubleTime)original.Mods[0]).SpeedChange.Value, Is.EqualTo(2.0));
|
Assert.That(((OsuModDoubleTime)original.Mods[0]).SpeedChange.Value, Is.EqualTo(2.0));
|
||||||
Assert.That(((OsuModDoubleTime)copy.Mods[0]).SpeedChange.Value, Is.EqualTo(1.5));
|
Assert.That(((OsuModDoubleTime)copy.Mods[0]).SpeedChange.Value, Is.EqualTo(1.5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestCopySharedSettingsOfDifferentType()
|
||||||
|
{
|
||||||
|
const double setting_change = 2.5;
|
||||||
|
|
||||||
|
var osuMod = new TestNonMatchinSettingTypeOsuMod();
|
||||||
|
var maniaMod = new TestNonMatchinSettingTypeManiaMod();
|
||||||
|
|
||||||
|
osuMod.TestSetting.Value = setting_change;
|
||||||
|
maniaMod.CopySharedSettings(osuMod);
|
||||||
|
osuMod.CopySharedSettings(maniaMod);
|
||||||
|
|
||||||
|
Assert.That(maniaMod.TestSetting.IsDefault, "Value has been changed");
|
||||||
|
Assert.That(osuMod.TestSetting.Value == setting_change);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestNonMatchinSettingTypeOsuMod : TestNonMatchinSettingTypeMod
|
||||||
|
{
|
||||||
|
public override string Acronym => "NMO";
|
||||||
|
public override BindableNumber<double> TestSetting { get; } = new BindableDouble(3.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestNonMatchinSettingTypeManiaMod : TestNonMatchinSettingTypeMod
|
||||||
|
{
|
||||||
|
public override string Acronym => "NMM";
|
||||||
|
public override Bindable<bool> TestSetting { get; } = new BindableBool(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private abstract class TestNonMatchinSettingTypeMod : Mod
|
||||||
|
{
|
||||||
|
public override string Name => "Non-matching setting type mod";
|
||||||
|
public override LocalisableString Description => "Description";
|
||||||
|
public override double ScoreMultiplier => 1;
|
||||||
|
public override ModType Type => ModType.Conversion;
|
||||||
|
|
||||||
|
[SettingSource("Test setting")]
|
||||||
|
public abstract IBindable TestSetting { get; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ using osu.Game.Overlays.Mods;
|
|||||||
using osu.Game.Overlays.Settings;
|
using osu.Game.Overlays.Settings;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Rulesets.Catch.Mods;
|
using osu.Game.Rulesets.Catch.Mods;
|
||||||
|
using osu.Game.Rulesets.Mania.Mods;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
using osu.Game.Rulesets.Osu;
|
using osu.Game.Rulesets.Osu;
|
||||||
using osu.Game.Rulesets.Osu.Mods;
|
using osu.Game.Rulesets.Osu.Mods;
|
||||||
@ -387,7 +388,8 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
|
|
||||||
AddStep("change mod settings", () =>
|
AddStep("change mod settings", () =>
|
||||||
{
|
{
|
||||||
var osuMod = (getMod() as OsuModDifficultyAdjust)!;
|
var osuMod = getMod<OsuModDifficultyAdjust>();
|
||||||
|
|
||||||
osuMod.ExtendedLimits.Value = true;
|
osuMod.ExtendedLimits.Value = true;
|
||||||
osuMod.CircleSize.Value = setting_change;
|
osuMod.CircleSize.Value = setting_change;
|
||||||
osuMod.DrainRate.Value = setting_change;
|
osuMod.DrainRate.Value = setting_change;
|
||||||
@ -400,23 +402,60 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
|
|
||||||
AddAssert("shared settings didn't change", () =>
|
AddAssert("shared settings didn't change", () =>
|
||||||
{
|
{
|
||||||
var taikoMod = getMod() as TaikoModDifficultyAdjust;
|
var taikoMod = getMod<TaikoModDifficultyAdjust>();
|
||||||
return
|
|
||||||
taikoMod?.ExtendedLimits.Value == true &&
|
return taikoMod.ExtendedLimits.Value &&
|
||||||
taikoMod.DrainRate.Value == setting_change &&
|
taikoMod.DrainRate.Value == setting_change &&
|
||||||
taikoMod.OverallDifficulty.Value == setting_change;
|
taikoMod.OverallDifficulty.Value == setting_change;
|
||||||
});
|
});
|
||||||
|
|
||||||
AddAssert("non-shared settings at default", () =>
|
AddAssert("non-shared settings unchanged", () =>
|
||||||
{
|
{
|
||||||
var taikoMod = getMod() as TaikoModDifficultyAdjust;
|
var taikoMod = getMod<TaikoModDifficultyAdjust>();
|
||||||
if (taikoMod == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return taikoMod.ScrollSpeed.Value == taikoMod.ScrollSpeed.Default;
|
return taikoMod.ScrollSpeed.IsDefault;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestKeepSharedSettingsRatio()
|
||||||
|
{
|
||||||
|
const float setting_change = 1.8f;
|
||||||
|
|
||||||
|
createScreen();
|
||||||
|
changeRuleset(0);
|
||||||
|
|
||||||
|
AddStep("select flashlight mod", () => SelectedMods.Value = new[] { Ruleset.Value.CreateInstance().CreateMod<ModFlashlight>()! });
|
||||||
|
|
||||||
|
changeRuleset(0);
|
||||||
|
AddAssert("ensure mod still selected", () => SelectedMods.Value.SingleOrDefault() is OsuModFlashlight);
|
||||||
|
|
||||||
|
AddStep("change mod settings", () =>
|
||||||
|
{
|
||||||
|
var osuMod = getMod<OsuModFlashlight>();
|
||||||
|
|
||||||
|
// range <0.5 - 2>
|
||||||
|
osuMod.SizeMultiplier.Value = setting_change;
|
||||||
});
|
});
|
||||||
|
|
||||||
ModDifficultyAdjust getMod() => (SelectedMods.Value.Single() as ModDifficultyAdjust)!;
|
changeRuleset(3);
|
||||||
|
AddAssert("mania variant selected", () => SelectedMods.Value.SingleOrDefault() is ManiaModFlashlight);
|
||||||
|
|
||||||
|
AddAssert("shared settings changed to closest ratio", () =>
|
||||||
|
{
|
||||||
|
var maniaMod = getMod<ManiaModFlashlight>();
|
||||||
|
|
||||||
|
// range <0.5 - 3>
|
||||||
|
// converted value based on ratio = (setting_change - 0.5) / (2 - 0.5) * (3 - 0.5) + 0.5 = 2.66
|
||||||
|
return maniaMod.SizeMultiplier.Value == 2.7f; // taking precision into account
|
||||||
|
});
|
||||||
|
|
||||||
|
AddAssert("other settings unchanged", () =>
|
||||||
|
{
|
||||||
|
var maniaMod = getMod<ManiaModFlashlight>();
|
||||||
|
|
||||||
|
return maniaMod.ComboBasedSize.IsDefault;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -514,6 +553,8 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
waitForColumnLoad();
|
waitForColumnLoad();
|
||||||
|
|
||||||
AddAssert("unimplemented mod panel is filtered", () => getPanelForMod(typeof(TestUnimplementedMod)).Filtered.Value);
|
AddAssert("unimplemented mod panel is filtered", () => getPanelForMod(typeof(TestUnimplementedMod)).Filtered.Value);
|
||||||
|
|
||||||
|
AddStep("disable panel filtering", () => getPanelForMod(typeof(TestUnimplementedMod)).Filtered.Value = false);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -629,6 +670,8 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
AddAssert($"customisation toggle is {(active ? "" : "not ")}active", () => modSelectOverlay.CustomisationButton.AsNonNull().Active.Value == active);
|
AddAssert($"customisation toggle is {(active ? "" : "not ")}active", () => modSelectOverlay.CustomisationButton.AsNonNull().Active.Value == active);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private T getMod<T>() where T : Mod => (T)SelectedMods.Value.Single();
|
||||||
|
|
||||||
private ModPanel getPanelForMod(Type modType)
|
private ModPanel getPanelForMod(Type modType)
|
||||||
=> modSelectOverlay.ChildrenOfType<ModPanel>().Single(panel => panel.Mod.GetType() == modType);
|
=> modSelectOverlay.ChildrenOfType<ModPanel>().Single(panel => panel.Mod.GetType() == modType);
|
||||||
|
|
||||||
|
@ -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 AutoMapper.Internal;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Extensions.TypeExtensions;
|
using osu.Framework.Extensions.TypeExtensions;
|
||||||
@ -173,13 +174,46 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
|
|
||||||
foreach (var (_, property) in this.GetSettingsSourceProperties())
|
foreach (var (_, property) in this.GetSettingsSourceProperties())
|
||||||
{
|
{
|
||||||
var targetBindable = (IBindable)property.GetValue(this)!;
|
object targetSetting = property.GetValue(this)!;
|
||||||
|
|
||||||
if (!oldSettings.TryGetValue(property.Name.ToSnakeCase(), out object? sourceSetting))
|
if (!oldSettings.TryGetValue(property.Name.ToSnakeCase(), out object? sourceSetting))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
CopyAdjustedSetting(targetBindable, sourceSetting);
|
if (((IBindable)sourceSetting).IsDefault)
|
||||||
|
// keep at default value if the source is default
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Type? targetType = getGenericBaseType(targetSetting, typeof(BindableNumber<>));
|
||||||
|
Type? sourceType = getGenericBaseType(sourceSetting, typeof(BindableNumber<>));
|
||||||
|
|
||||||
|
if (targetType == null || sourceType == null)
|
||||||
|
{
|
||||||
|
if (getGenericBaseType(targetSetting, typeof(Bindable<>))!.GenericTypeArguments.Single() ==
|
||||||
|
getGenericBaseType(sourceSetting, typeof(Bindable<>))!.GenericTypeArguments.Single())
|
||||||
|
// change settings only if the type is the same
|
||||||
|
CopyAdjustedSetting((IBindable)targetSetting, sourceSetting);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
double targetMin = getValue(targetSetting, nameof(IBindableNumber<int>.MinValue));
|
||||||
|
double targetMax = getValue(targetSetting, nameof(IBindableNumber<int>.MaxValue));
|
||||||
|
double sourceMin = getValue(sourceSetting, nameof(IBindableNumber<int>.MinValue));
|
||||||
|
double sourceMax = getValue(sourceSetting, nameof(IBindableNumber<int>.MaxValue));
|
||||||
|
double sourceValue = getValue(sourceSetting, nameof(IBindableNumber<int>.Value));
|
||||||
|
|
||||||
|
// convert value to same ratio
|
||||||
|
double targetValue = (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin;
|
||||||
|
|
||||||
|
targetType.GetProperty(nameof(IBindableNumber<int>.Value))!.SetValue(targetSetting,
|
||||||
|
Convert.ChangeType(targetValue, targetType.GenericTypeArguments.Single()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double getValue(object target, string name) =>
|
||||||
|
Convert.ToDouble(target.GetType().GetProperty(name)!.GetValue(target)!);
|
||||||
|
|
||||||
|
Type? getGenericBaseType(object target, Type genericType) =>
|
||||||
|
target.GetType().GetTypeInheritance().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user