mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 17:43:05 +08:00
Extract mod setting value handling to utils class
This commit is contained in:
parent
18fb9f5ac9
commit
fbd5195738
@ -3,11 +3,10 @@
|
|||||||
|
|
||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using MessagePack;
|
using MessagePack;
|
||||||
using MessagePack.Formatters;
|
using MessagePack.Formatters;
|
||||||
using osu.Framework.Bindables;
|
using osu.Game.Utils;
|
||||||
|
|
||||||
namespace osu.Game.Online.API
|
namespace osu.Game.Online.API
|
||||||
{
|
{
|
||||||
@ -24,36 +23,7 @@ namespace osu.Game.Online.API
|
|||||||
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
|
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
|
||||||
writer.WriteString(in stringBytes);
|
writer.WriteString(in stringBytes);
|
||||||
|
|
||||||
switch (kvp.Value)
|
primitiveFormatter.Serialize(ref writer, ModUtils.GetSettingUnderlyingValue(kvp.Value), options);
|
||||||
{
|
|
||||||
case Bindable<double> d:
|
|
||||||
primitiveFormatter.Serialize(ref writer, d.Value, options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Bindable<int> i:
|
|
||||||
primitiveFormatter.Serialize(ref writer, i.Value, options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Bindable<float> f:
|
|
||||||
primitiveFormatter.Serialize(ref writer, f.Value, options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Bindable<bool> b:
|
|
||||||
primitiveFormatter.Serialize(ref writer, b.Value, options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IBindable u:
|
|
||||||
// A mod with unknown (e.g. enum) generic type.
|
|
||||||
var valueMethod = u.GetType().GetProperty(nameof(IBindable<int>.Value));
|
|
||||||
Debug.Assert(valueMethod != null);
|
|
||||||
primitiveFormatter.Serialize(ref writer, valueMethod.GetValue(u), options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// fall back for non-bindable cases.
|
|
||||||
primitiveFormatter.Serialize(ref writer, kvp.Value, options);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,11 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Game.Online.API;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
@ -129,5 +132,38 @@ namespace osu.Game.Utils
|
|||||||
else
|
else
|
||||||
yield return mod;
|
yield return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the underlying value of the given mod setting object.
|
||||||
|
/// Used in <see cref="APIMod"/> for serialization and equality comparison purposes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="setting">The mod setting.</param>
|
||||||
|
public static object GetSettingUnderlyingValue(object setting)
|
||||||
|
{
|
||||||
|
switch (setting)
|
||||||
|
{
|
||||||
|
case Bindable<double> d:
|
||||||
|
return d.Value;
|
||||||
|
|
||||||
|
case Bindable<int> i:
|
||||||
|
return i.Value;
|
||||||
|
|
||||||
|
case Bindable<float> f:
|
||||||
|
return f.Value;
|
||||||
|
|
||||||
|
case Bindable<bool> b:
|
||||||
|
return b.Value;
|
||||||
|
|
||||||
|
case IBindable u:
|
||||||
|
// A mod with unknown (e.g. enum) generic type.
|
||||||
|
var valueMethod = u.GetType().GetProperty(nameof(IBindable<int>.Value));
|
||||||
|
Debug.Assert(valueMethod != null);
|
||||||
|
return valueMethod.GetValue(u);
|
||||||
|
|
||||||
|
default:
|
||||||
|
// fall back for non-bindable cases.
|
||||||
|
return setting;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user