using MessagePack.Formatters; using System; using System.Collections.Generic; using UnityEngine; namespace MessagePack.Unity { public class UnityResolver : IFormatterResolver { public static IFormatterResolver Instance = new UnityResolver(); UnityResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { formatter = (IMessagePackFormatter)UnityResolveryResolverGetFormatterHelper.GetFormatter(typeof(T)); } } } internal static class UnityResolveryResolverGetFormatterHelper { static readonly Dictionary formatterMap = new Dictionary() { // standard {typeof(Vector2), new Vector2Formatter()}, {typeof(Vector3), new Vector3Formatter()}, {typeof(Vector4), new Vector4Formatter()}, {typeof(Quaternion), new QuaternionFormatter()}, {typeof(Color), new ColorFormatter()}, {typeof(Bounds), new BoundsFormatter()}, {typeof(Rect), new RectFormatter()}, {typeof(Vector2?), new StaticNullableFormatter(new Vector2Formatter())}, {typeof(Vector3?), new StaticNullableFormatter(new Vector3Formatter())}, {typeof(Vector4?), new StaticNullableFormatter(new Vector4Formatter())}, {typeof(Quaternion?),new StaticNullableFormatter(new QuaternionFormatter())}, {typeof(Color?),new StaticNullableFormatter(new ColorFormatter())}, {typeof(Bounds?),new StaticNullableFormatter(new BoundsFormatter())}, {typeof(Rect?),new StaticNullableFormatter(new RectFormatter())}, // standard + array {typeof(Vector2[]),new ArrayFormatter()}, {typeof(Vector3[]), new ArrayFormatter()}, {typeof(Vector4[]), new ArrayFormatter()}, {typeof(Quaternion[]), new ArrayFormatter()}, {typeof(Color[]), new ArrayFormatter()}, {typeof(Bounds[]), new ArrayFormatter()}, {typeof(Rect[]), new ArrayFormatter()}, {typeof(Vector2?[]), new ArrayFormatter()}, {typeof(Vector3?[]), new ArrayFormatter()}, {typeof(Vector4?[]), new ArrayFormatter()}, {typeof(Quaternion?[]),new ArrayFormatter()}, {typeof(Color?[]),new ArrayFormatter()}, {typeof(Bounds?[]),new ArrayFormatter()}, {typeof(Rect?[]),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()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List),new ListFormatter()}, {typeof(List),new ListFormatter()}, {typeof(List),new ListFormatter()}, {typeof(List),new ListFormatter()}, // new {typeof(AnimationCurve), new AnimationCurveFormatter()}, {typeof(RectOffset), new RectOffsetFormatter()}, {typeof(Gradient), new GradientFormatter() }, {typeof(WrapMode), new WrapModeFormatter()}, {typeof(GradientMode), new GradientModeFormatter()}, {typeof(Keyframe), new KeyframeFormatter()}, {typeof(Matrix4x4), new Matrix4x4Formatter()}, {typeof(GradientColorKey), new GradientColorKeyFormatter()}, {typeof(GradientAlphaKey), new GradientAlphaKeyFormatter()}, {typeof(Color32), new Color32Formatter()}, {typeof(LayerMask), new LayerMaskFormatter()}, {typeof(WrapMode?), new StaticNullableFormatter(new WrapModeFormatter())}, {typeof(GradientMode?), new StaticNullableFormatter(new GradientModeFormatter())}, {typeof(Keyframe?), new StaticNullableFormatter(new KeyframeFormatter())}, {typeof(Matrix4x4?), new StaticNullableFormatter(new Matrix4x4Formatter())}, {typeof(GradientColorKey?), new StaticNullableFormatter(new GradientColorKeyFormatter())}, {typeof(GradientAlphaKey?), new StaticNullableFormatter(new GradientAlphaKeyFormatter())}, {typeof(Color32?), new StaticNullableFormatter(new Color32Formatter())}, {typeof(LayerMask?), new StaticNullableFormatter(new LayerMaskFormatter())}, // new + array {typeof(AnimationCurve[]), new ArrayFormatter()}, {typeof(RectOffset[]), new ArrayFormatter()}, {typeof(Gradient[]), new ArrayFormatter() }, {typeof(WrapMode[]), new ArrayFormatter()}, {typeof(GradientMode[]), new ArrayFormatter()}, {typeof(Keyframe[]), new ArrayFormatter()}, {typeof(Matrix4x4[]), new ArrayFormatter()}, {typeof(GradientColorKey[]), new ArrayFormatter()}, {typeof(GradientAlphaKey[]), new ArrayFormatter()}, {typeof(Color32[]), new ArrayFormatter()}, {typeof(LayerMask[]), new ArrayFormatter()}, {typeof(WrapMode?[]), new ArrayFormatter()}, {typeof(GradientMode?[]), new ArrayFormatter()}, {typeof(Keyframe?[]), new ArrayFormatter()}, {typeof(Matrix4x4?[]), new ArrayFormatter()}, {typeof(GradientColorKey?[]), new ArrayFormatter()}, {typeof(GradientAlphaKey?[]), new ArrayFormatter()}, {typeof(Color32?[]), new ArrayFormatter()}, {typeof(LayerMask?[]), new ArrayFormatter()}, // new + 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()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, // unity 2017.2 #if UNITY_2017_2_OR_NEWER {typeof(Vector2Int), new Vector2IntFormatter()}, {typeof(Vector3Int), new Vector3IntFormatter()}, {typeof(RangeInt), new RangeIntFormatter()}, {typeof(RectInt), new RectIntFormatter()}, {typeof(BoundsInt), new BoundsIntFormatter()}, {typeof(Vector2Int?), new StaticNullableFormatter(new Vector2IntFormatter())}, {typeof(Vector3Int?), new StaticNullableFormatter(new Vector3IntFormatter())}, {typeof(RangeInt?), new StaticNullableFormatter(new RangeIntFormatter())}, {typeof(RectInt?), new StaticNullableFormatter(new RectIntFormatter())}, {typeof(BoundsInt?), new StaticNullableFormatter(new BoundsIntFormatter())}, // unity 2017.2 + array {typeof(Vector2Int[]), new ArrayFormatter()}, {typeof(Vector3Int[]), new ArrayFormatter()}, {typeof(RangeInt[]), new ArrayFormatter()}, {typeof(RectInt[]), new ArrayFormatter()}, {typeof(BoundsInt[]), new ArrayFormatter()}, {typeof(Vector2Int?[]), new ArrayFormatter()}, {typeof(Vector3Int?[]), new ArrayFormatter()}, {typeof(RangeInt?[]), new ArrayFormatter()}, {typeof(RectInt?[]), new ArrayFormatter()}, {typeof(BoundsInt?[]), new ArrayFormatter()}, // unity 2017.2 + 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()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, #endif }; internal static object GetFormatter(Type t) { object formatter; if (formatterMap.TryGetValue(t, out formatter)) { return formatter; } return null; } } }