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

Fixed errors covered in new tests

This commit is contained in:
Terochi 2023-03-07 20:39:50 +01:00
parent 9ea93e0a9f
commit 8bf84869a5

View File

@ -165,6 +165,10 @@ namespace osu.Game.Rulesets.Mods
/// <param name="source">The mod to copy properties from.</param> /// <param name="source">The mod to copy properties from.</param>
internal void CopySharedSettings(Mod source) internal void CopySharedSettings(Mod source)
{ {
const string min_value = nameof(BindableNumber<int>.MinValue);
const string max_value = nameof(BindableNumber<int>.MaxValue);
const string value = nameof(Bindable<int>.Value);
Dictionary<string, object> oldSettings = new Dictionary<string, object>(); Dictionary<string, object> oldSettings = new Dictionary<string, object>();
foreach (var (_, property) in source.GetSettingsSourceProperties()) foreach (var (_, property) in source.GetSettingsSourceProperties())
@ -190,27 +194,47 @@ namespace osu.Game.Rulesets.Mods
{ {
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())
{
// change settings only if the type is the same // change settings only if the type is the same
CopyAdjustedSetting((IBindable)targetSetting, sourceSetting); setValue(targetSetting, value, getValue(sourceSetting, value));
}
continue; continue;
} }
double targetMin = getValue(targetSetting, nameof(IBindableNumber<int>.MinValue)); Type targetGenericType = targetType.GenericTypeArguments.Single();
double targetMax = getValue(targetSetting, nameof(IBindableNumber<int>.MaxValue)); Type sourceGenericType = sourceType.GenericTypeArguments.Single();
double sourceMin = getValue(sourceSetting, nameof(IBindableNumber<int>.MinValue));
double sourceMax = getValue(sourceSetting, nameof(IBindableNumber<int>.MaxValue)); double allowedMin = Math.Max(
double sourceValue = getValue(sourceSetting, nameof(IBindableNumber<int>.Value)); Convert.ToDouble(targetGenericType.GetField("MinValue")!.GetValue(null)),
Convert.ToDouble(sourceGenericType.GetField("MinValue")!.GetValue(null))
);
double allowedMax = Math.Min(
Convert.ToDouble(targetGenericType.GetField("MaxValue")!.GetValue(null)),
Convert.ToDouble(sourceGenericType.GetField("MaxValue")!.GetValue(null))
);
double targetMin = getValueDouble(targetSetting, min_value);
double targetMax = getValueDouble(targetSetting, max_value);
double sourceMin = getValueDouble(sourceSetting, min_value);
double sourceMax = getValueDouble(sourceSetting, max_value);
double sourceValue = getValueDouble(sourceSetting, value);
// convert value to same ratio // convert value to same ratio
double targetValue = (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin; double targetValue = (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin;
targetType.GetProperty(nameof(IBindableNumber<int>.Value))!.SetValue(targetSetting, setValue(targetSetting, value, Convert.ChangeType(targetValue, targetType.GenericTypeArguments.Single()));
Convert.ChangeType(targetValue, targetType.GenericTypeArguments.Single()));
double getValueDouble(object target, string name) =>
Math.Clamp(Convert.ToDouble(getValue(target, name)!), allowedMin, allowedMax);
} }
double getValue(object target, string name) => object? getValue(object target, string name) =>
Convert.ToDouble(target.GetType().GetProperty(name)!.GetValue(target)!); target.GetType().GetProperty(name)!.GetValue(target);
void setValue(object target, string name, object? newValue) =>
target.GetType().GetProperty(name)!.SetValue(target, newValue);
Type? getGenericBaseType(object target, Type genericType) => Type? getGenericBaseType(object target, Type genericType) =>
target.GetType().GetTypeInheritance().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericType); target.GetType().GetTypeInheritance().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericType);