using System; using System.Collections; using System.Collections.Generic; using MessagePack; using MessagePack.Formatters; using Unity.Mathematics; using static Unity.Mathematics.math; public class MathResolver : IFormatterResolver { public static readonly MathResolver Instance = new MathResolver(); private MathResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { Formatter = (IMessagePackFormatter)MathResolverResolverGetFormatterHelper.GetFormatter(typeof(T)); } } } internal static class MathResolverResolverGetFormatterHelper { private static readonly Dictionary FormatterMap = new Dictionary() { // standard { typeof(float2), new Float2Formatter() }, { typeof(float3), new Float3Formatter() }, { typeof(float4), new Float4Formatter() }, { typeof(float2?), new StaticNullableFormatter(new Float2Formatter()) }, { typeof(float3?), new StaticNullableFormatter(new Float3Formatter()) }, { typeof(float4?), new StaticNullableFormatter(new Float4Formatter()) }, // standard + array { typeof(float2[]), new ArrayFormatter() }, { typeof(float3[]), new ArrayFormatter() }, { typeof(float4[]), new ArrayFormatter() }, { typeof(float2?[]), new ArrayFormatter() }, { typeof(float3?[]), new ArrayFormatter() }, { typeof(float4?[]), new ArrayFormatter() }, // standard + list { typeof(List), new ListFormatter() }, { typeof(List), new ListFormatter() }, { typeof(List), new ListFormatter() }, { typeof(List), new ListFormatter() }, { typeof(List), new ListFormatter() }, { typeof(List), new ListFormatter() }, }; internal static object GetFormatter(Type t) { object formatter; if (FormatterMap.TryGetValue(t, out formatter)) { return formatter; } return null; } }