2021-02-03 19:06:25 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2021-02-03 19:42:27 +08:00
|
|
|
using System.Buffers;
|
2021-02-03 18:46:47 +08:00
|
|
|
using System.Collections.Generic;
|
2021-02-03 19:42:27 +08:00
|
|
|
using System.Text;
|
2021-02-03 18:46:47 +08:00
|
|
|
using MessagePack;
|
|
|
|
using MessagePack.Formatters;
|
2021-04-12 08:37:03 +08:00
|
|
|
using osu.Game.Utils;
|
2021-02-03 18:46:47 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
{
|
|
|
|
public class ModSettingsDictionaryFormatter : IMessagePackFormatter<Dictionary<string, object>>
|
|
|
|
{
|
2021-02-03 19:42:27 +08:00
|
|
|
public void Serialize(ref MessagePackWriter writer, Dictionary<string, object> value, MessagePackSerializerOptions options)
|
2021-02-03 18:46:47 +08:00
|
|
|
{
|
2021-02-03 19:19:27 +08:00
|
|
|
var primitiveFormatter = PrimitiveObjectFormatter.Instance;
|
|
|
|
|
2021-02-03 19:42:27 +08:00
|
|
|
writer.WriteArrayHeader(value.Count);
|
2021-02-03 18:46:47 +08:00
|
|
|
|
|
|
|
foreach (var kvp in value)
|
|
|
|
{
|
2021-02-03 19:42:27 +08:00
|
|
|
var stringBytes = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(kvp.Key));
|
|
|
|
writer.WriteString(in stringBytes);
|
2021-02-03 18:46:47 +08:00
|
|
|
|
2021-04-12 08:37:03 +08:00
|
|
|
primitiveFormatter.Serialize(ref writer, ModUtils.GetSettingUnderlyingValue(kvp.Value), options);
|
2021-02-03 18:46:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:42:27 +08:00
|
|
|
public Dictionary<string, object> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
2021-02-03 18:46:47 +08:00
|
|
|
{
|
|
|
|
var output = new Dictionary<string, object>();
|
|
|
|
|
2021-02-03 19:42:27 +08:00
|
|
|
int itemCount = reader.ReadArrayHeader();
|
2021-02-03 18:46:47 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
{
|
2021-02-03 19:42:27 +08:00
|
|
|
output[reader.ReadString()] =
|
|
|
|
PrimitiveObjectFormatter.Instance.Deserialize(ref reader, options);
|
2021-02-03 18:46:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|