1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 12:42:54 +08:00

Add Messagepack support for serialising unknown bindable types

This commit is contained in:
smoogipoo 2021-02-10 23:44:06 +09:00
parent 70924319e4
commit 07b661e28c
2 changed files with 35 additions and 0 deletions

View File

@ -68,6 +68,16 @@ namespace osu.Game.Tests.Online
Assert.That(converted.FinalRate.Value, Is.EqualTo(0.25)); 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 private class TestRuleset : Ruleset
{ {
public override IEnumerable<Mod> GetModsFor(ModType type) => new Mod[] public override IEnumerable<Mod> GetModsFor(ModType type) => new Mod[]
@ -135,5 +145,22 @@ namespace osu.Game.Tests.Online
Value = true 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
}
} }
} }

View File

@ -3,6 +3,7 @@
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;
@ -41,6 +42,13 @@ namespace osu.Game.Online.API
primitiveFormatter.Serialize(ref writer, b.Value, options); primitiveFormatter.Serialize(ref writer, b.Value, options);
break; 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: default:
// fall back for non-bindable cases. // fall back for non-bindable cases.
primitiveFormatter.Serialize(ref writer, kvp.Value, options); primitiveFormatter.Serialize(ref writer, kvp.Value, options);