Add mod setting (de)serialization support

This commit is contained in:
smoogipoo
2020-01-17 13:27:47 +09:00
parent 86ae442d54
commit 2bc7458abf
5 changed files with 162 additions and 27 deletions

View File

@ -35,16 +35,11 @@ namespace osu.Game.Configuration
{
public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
{
foreach (var property in obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
foreach (var (attr, property) in obj.GetSettingsSourceProperties())
{
var attr = property.GetCustomAttribute<SettingSourceAttribute>(true);
object value = property.GetValue(obj);
if (attr == null)
continue;
var prop = property.GetValue(obj);
switch (prop)
switch (value)
{
case BindableNumber<float> bNumber:
yield return new SettingsSlider<float>
@ -102,9 +97,22 @@ namespace osu.Game.Configuration
break;
default:
throw new InvalidOperationException($"{nameof(SettingSourceAttribute)} was attached to an unsupported type ({prop})");
throw new InvalidOperationException($"{nameof(SettingSourceAttribute)} was attached to an unsupported type ({value})");
}
}
}
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
{
foreach (var property in obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
var attr = property.GetCustomAttribute<SettingSourceAttribute>(true);
if (attr == null)
continue;
yield return (attr, property);
}
}
}
}