mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 21:02:55 +08:00
Add Messagepack support for serialising unknown bindable types
This commit is contained in:
parent
70924319e4
commit
07b661e28c
@ -68,6 +68,16 @@ namespace osu.Game.Tests.Online
|
||||
Assert.That(converted.FinalRate.Value, Is.EqualTo(0.25));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDeserialiseEnumMod()
|
||||
{
|
||||
var apiMod = new APIMod(new TestModEnum { TestSetting = { Value = TestEnum.Value2 } });
|
||||
|
||||
var deserialized = MessagePackSerializer.Deserialize<APIMod>(MessagePackSerializer.Serialize(apiMod));
|
||||
|
||||
Assert.That(deserialized.Settings, Contains.Key("test_setting").With.ContainValue(1));
|
||||
}
|
||||
|
||||
private class TestRuleset : Ruleset
|
||||
{
|
||||
public override IEnumerable<Mod> GetModsFor(ModType type) => new Mod[]
|
||||
@ -135,5 +145,22 @@ namespace osu.Game.Tests.Online
|
||||
Value = true
|
||||
};
|
||||
}
|
||||
|
||||
private class TestModEnum : Mod
|
||||
{
|
||||
public override string Name => "Test Mod";
|
||||
public override string Acronym => "TM";
|
||||
public override double ScoreMultiplier => 1;
|
||||
|
||||
[SettingSource("Test")]
|
||||
public Bindable<TestEnum> TestSetting { get; } = new Bindable<TestEnum>();
|
||||
}
|
||||
|
||||
private enum TestEnum
|
||||
{
|
||||
Value1 = 0,
|
||||
Value2 = 1,
|
||||
Value3 = 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
@ -41,6 +42,13 @@ namespace osu.Game.Online.API
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user