PluralKit/PluralKit.Core/Utils/ProtobufUtils.cs
2022-01-22 03:52:52 -05:00

22 lines
608 B
C#

using Google.Protobuf;
namespace PluralKit.Core;
public static class Proto
{
private static Dictionary<string, MessageParser> _parser = new();
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))
return (T)_parser[type].ParseFrom(message);
else
{
_parser.Add(type, new MessageParser<T>(() => new T()));
return Unmarshal<T>(message);
}
}
}