2022-01-22 08:52:52 +00:00
|
|
|
using Google.Protobuf;
|
|
|
|
|
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public static class Proto
|
|
|
|
{
|
2022-03-30 08:36:22 +00:00
|
|
|
private static readonly Dictionary<string, MessageParser> _parser = new();
|
2022-01-22 08:52:52 +00:00
|
|
|
|
|
|
|
public static byte[] Marshal(this IMessage message) => message.ToByteArray();
|
|
|
|
|
|
|
|
public static T Unmarshal<T>(this byte[] message) where T : IMessage<T>, new()
|
|
|
|
{
|
|
|
|
var type = typeof(T).ToString();
|
|
|
|
if (_parser.ContainsKey(type))
|
|
|
|
{
|
2022-03-30 08:36:22 +00:00
|
|
|
return (T)_parser[type].ParseFrom(message);
|
2022-01-22 08:52:52 +00:00
|
|
|
}
|
2022-03-30 08:36:22 +00:00
|
|
|
|
|
|
|
_parser.Add(type, new MessageParser<T>(() => new T()));
|
|
|
|
return Unmarshal<T>(message);
|
2022-01-22 08:52:52 +00:00
|
|
|
}
|
|
|
|
}
|